Back to index

glibc  2.9
ffs.c
Go to the documentation of this file.
00001 /* ffs -- find first set bit in a word, counted from least significant end.
00002    S/390 version.
00003    Copyright (C) 2000, 2001, 2004 Free Software Foundation, Inc.
00004    Contributed by Martin Schwidefsky (schwidefsky@de.ibm.com).
00005    This file is part of the GNU C Library.
00006 
00007    The GNU C Library is free software; you can redistribute it and/or
00008    modify it under the terms of the GNU Lesser General Public
00009    License as published by the Free Software Foundation; either
00010    version 2.1 of the License, or (at your option) any later version.
00011 
00012    The GNU C Library is distributed in the hope that it will be useful,
00013    but WITHOUT ANY WARRANTY; without even the implied warranty of
00014    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
00015    Lesser General Public License for more details.
00016 
00017    You should have received a copy of the GNU Lesser General Public
00018    License along with the GNU C Library; if not, write to the Free
00019    Software Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA
00020    02111-1307 USA.  */
00021 
00022 #include <limits.h>
00023 #define ffsl __something_else
00024 #include <string.h>
00025 
00026 #undef ffs
00027 
00028 /* ffs: find first bit set. This is defined the same way as
00029    the libc and compiler builtin ffs routines, therefore
00030    differs in spirit from the above ffz (man ffs).  */
00031 
00032 int
00033 __ffs (x)
00034      int x;
00035 {
00036        int r;
00037 
00038        if (x == 0)
00039          return 0;
00040        __asm__("    lr        %%r1,%1\n"
00041               "    sr         %0,%0\n"
00042               "    tml  %%r1,0xFFFF\n"
00043               "    jnz  0f\n"
00044               "    ahi  %0,16\n"
00045               "    srl  %%r1,16\n"
00046               "0:  tml  %%r1,0x00FF\n"
00047               "    jnz  1f\n"
00048               "    ahi  %0,8\n"
00049               "    srl  %%r1,8\n"
00050               "1:  tml  %%r1,0x000F\n"
00051               "    jnz  2f\n"
00052               "    ahi  %0,4\n"
00053               "    srl  %%r1,4\n"
00054               "2:  tml  %%r1,0x0003\n"
00055               "    jnz  3f\n"
00056               "    ahi  %0,2\n"
00057               "    srl  %%r1,2\n"
00058               "3:  tml  %%r1,0x0001\n"
00059               "    jnz  4f\n"
00060               "    ahi  %0,1\n"
00061               "4:"
00062               : "=&d" (r) : "d" (x) : "cc", "1" );
00063        return r+1;
00064 }
00065 
00066 weak_alias (__ffs, ffs)
00067 libc_hidden_builtin_def (ffs)
00068 #if ULONG_MAX == UINT_MAX
00069 #undef ffsl
00070 weak_alias (__ffs, ffsl)
00071 #endif