xref: /openbmc/linux/include/asm-generic/bitops/ffs.h (revision b2441318)
1b2441318SGreg Kroah-Hartman /* SPDX-License-Identifier: GPL-2.0 */
209020adbSAkinobu Mita #ifndef _ASM_GENERIC_BITOPS_FFS_H_
309020adbSAkinobu Mita #define _ASM_GENERIC_BITOPS_FFS_H_
409020adbSAkinobu Mita 
509020adbSAkinobu Mita /**
609020adbSAkinobu Mita  * ffs - find first bit set
709020adbSAkinobu Mita  * @x: the word to search
809020adbSAkinobu Mita  *
909020adbSAkinobu Mita  * This is defined the same way as
1009020adbSAkinobu Mita  * the libc and compiler builtin ffs routines, therefore
1109020adbSAkinobu Mita  * differs in spirit from the above ffz (man ffs).
1209020adbSAkinobu Mita  */
1309020adbSAkinobu Mita static inline int ffs(int x)
1409020adbSAkinobu Mita {
1509020adbSAkinobu Mita 	int r = 1;
1609020adbSAkinobu Mita 
1709020adbSAkinobu Mita 	if (!x)
1809020adbSAkinobu Mita 		return 0;
1909020adbSAkinobu Mita 	if (!(x & 0xffff)) {
2009020adbSAkinobu Mita 		x >>= 16;
2109020adbSAkinobu Mita 		r += 16;
2209020adbSAkinobu Mita 	}
2309020adbSAkinobu Mita 	if (!(x & 0xff)) {
2409020adbSAkinobu Mita 		x >>= 8;
2509020adbSAkinobu Mita 		r += 8;
2609020adbSAkinobu Mita 	}
2709020adbSAkinobu Mita 	if (!(x & 0xf)) {
2809020adbSAkinobu Mita 		x >>= 4;
2909020adbSAkinobu Mita 		r += 4;
3009020adbSAkinobu Mita 	}
3109020adbSAkinobu Mita 	if (!(x & 3)) {
3209020adbSAkinobu Mita 		x >>= 2;
3309020adbSAkinobu Mita 		r += 2;
3409020adbSAkinobu Mita 	}
3509020adbSAkinobu Mita 	if (!(x & 1)) {
3609020adbSAkinobu Mita 		x >>= 1;
3709020adbSAkinobu Mita 		r += 1;
3809020adbSAkinobu Mita 	}
3909020adbSAkinobu Mita 	return r;
4009020adbSAkinobu Mita }
4109020adbSAkinobu Mita 
4209020adbSAkinobu Mita #endif /* _ASM_GENERIC_BITOPS_FFS_H_ */
43