Ruby 1.9.3p327(2012-11-10revision37606)
missing/ffs.c
Go to the documentation of this file.
00001 /* ffs.c - find first set bit */
00002 /* ffs() is defined by Single Unix Specification. */
00003 
00004 #include "ruby.h"
00005 
00006 int ffs(int arg)
00007 {
00008     unsigned int x = (unsigned int)arg;
00009     int r;
00010 
00011     if (x == 0)
00012         return 0;
00013 
00014     r = 1;
00015 
00016 #if 32 < SIZEOF_INT * CHAR_BIT
00017     if ((x & 0xffffffff) == 0) {
00018         x >>= 32;
00019         r += 32;
00020     }
00021 #endif
00022 
00023     if ((x & 0xffff) == 0) {
00024         x >>= 16;
00025         r += 16;
00026     }
00027 
00028     if ((x & 0xff) == 0) {
00029         x >>= 8;
00030         r += 8;
00031     }
00032 
00033     if ((x & 0xf) == 0) {
00034         x >>= 4;
00035         r += 4;
00036     }
00037 
00038     if ((x & 0x3) == 0) {
00039         x >>= 2;
00040         r += 2;
00041     }
00042 
00043     if ((x & 0x1) == 0) {
00044         x >>= 1;
00045         r += 1;
00046     }
00047 
00048     return r;
00049 }
00050