18c2ecf20Sopenharmony_ci/* SPDX-License-Identifier: GPL-2.0 */
28c2ecf20Sopenharmony_ci#ifndef _ASM_GENERIC_BITOPS___FFS_H_
38c2ecf20Sopenharmony_ci#define _ASM_GENERIC_BITOPS___FFS_H_
48c2ecf20Sopenharmony_ci
58c2ecf20Sopenharmony_ci#include <asm/types.h>
68c2ecf20Sopenharmony_ci
78c2ecf20Sopenharmony_ci/**
88c2ecf20Sopenharmony_ci * __ffs - find first bit in word.
98c2ecf20Sopenharmony_ci * @word: The word to search
108c2ecf20Sopenharmony_ci *
118c2ecf20Sopenharmony_ci * Undefined if no bit exists, so code should check against 0 first.
128c2ecf20Sopenharmony_ci */
138c2ecf20Sopenharmony_cistatic __always_inline unsigned long __ffs(unsigned long word)
148c2ecf20Sopenharmony_ci{
158c2ecf20Sopenharmony_ci	int num = 0;
168c2ecf20Sopenharmony_ci
178c2ecf20Sopenharmony_ci#if BITS_PER_LONG == 64
188c2ecf20Sopenharmony_ci	if ((word & 0xffffffff) == 0) {
198c2ecf20Sopenharmony_ci		num += 32;
208c2ecf20Sopenharmony_ci		word >>= 32;
218c2ecf20Sopenharmony_ci	}
228c2ecf20Sopenharmony_ci#endif
238c2ecf20Sopenharmony_ci	if ((word & 0xffff) == 0) {
248c2ecf20Sopenharmony_ci		num += 16;
258c2ecf20Sopenharmony_ci		word >>= 16;
268c2ecf20Sopenharmony_ci	}
278c2ecf20Sopenharmony_ci	if ((word & 0xff) == 0) {
288c2ecf20Sopenharmony_ci		num += 8;
298c2ecf20Sopenharmony_ci		word >>= 8;
308c2ecf20Sopenharmony_ci	}
318c2ecf20Sopenharmony_ci	if ((word & 0xf) == 0) {
328c2ecf20Sopenharmony_ci		num += 4;
338c2ecf20Sopenharmony_ci		word >>= 4;
348c2ecf20Sopenharmony_ci	}
358c2ecf20Sopenharmony_ci	if ((word & 0x3) == 0) {
368c2ecf20Sopenharmony_ci		num += 2;
378c2ecf20Sopenharmony_ci		word >>= 2;
388c2ecf20Sopenharmony_ci	}
398c2ecf20Sopenharmony_ci	if ((word & 0x1) == 0)
408c2ecf20Sopenharmony_ci		num += 1;
418c2ecf20Sopenharmony_ci	return num;
428c2ecf20Sopenharmony_ci}
438c2ecf20Sopenharmony_ci
448c2ecf20Sopenharmony_ci#endif /* _ASM_GENERIC_BITOPS___FFS_H_ */
45