1/* 2 * @TAG(OTHER_GPL) 3 */ 4 5#pragma once 6 7#include "../unimplemented.h" 8 9/* 10 * ffs: find first bit set. This is defined the same way as 11 * the libc and compiler builtin ffs routines, therefore 12 * differs in spirit from the above ffz (man ffs). 13 */ 14 15static inline int generic_ffs(int x) 16{ 17 int r = 1; 18 19 if (!x) 20 return 0; 21 if (!(x & 0xffff)) { 22 x >>= 16; 23 r += 16; 24 } 25 if (!(x & 0xff)) { 26 x >>= 8; 27 r += 8; 28 } 29 if (!(x & 0xf)) { 30 x >>= 4; 31 r += 4; 32 } 33 if (!(x & 3)) { 34 x >>= 2; 35 r += 2; 36 } 37 if (!(x & 1)) { 38 x >>= 1; 39 r += 1; 40 } 41 return r; 42} 43 44#ifndef PLATFORM_FFS 45# define ffs generic_ffs 46#endif 47