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