Merge tag 'trace-printf-v6.13' of git://git.kernel.org/pub/scm/linux/kernel/git/trace...
[drm/drm-misc.git] / include / linux / count_zeros.h
blob5b8ff5ac660da01a73582bc581a3dc1acb2c220e
1 /* SPDX-License-Identifier: GPL-2.0-or-later */
2 /* Count leading and trailing zeros functions
4 * Copyright (C) 2012 Red Hat, Inc. All Rights Reserved.
5 * Written by David Howells (dhowells@redhat.com)
6 */
8 #ifndef _LINUX_BITOPS_COUNT_ZEROS_H_
9 #define _LINUX_BITOPS_COUNT_ZEROS_H_
11 #include <asm/bitops.h>
13 /**
14 * count_leading_zeros - Count the number of zeros from the MSB back
15 * @x: The value
17 * Count the number of leading zeros from the MSB going towards the LSB in @x.
19 * If the MSB of @x is set, the result is 0.
20 * If only the LSB of @x is set, then the result is BITS_PER_LONG-1.
21 * If @x is 0 then the result is COUNT_LEADING_ZEROS_0.
23 static inline int count_leading_zeros(unsigned long x)
25 if (sizeof(x) == 4)
26 return BITS_PER_LONG - fls(x);
27 else
28 return BITS_PER_LONG - fls64(x);
31 #define COUNT_LEADING_ZEROS_0 BITS_PER_LONG
33 /**
34 * count_trailing_zeros - Count the number of zeros from the LSB forwards
35 * @x: The value
37 * Count the number of trailing zeros from the LSB going towards the MSB in @x.
39 * If the LSB of @x is set, the result is 0.
40 * If only the MSB of @x is set, then the result is BITS_PER_LONG-1.
41 * If @x is 0 then the result is COUNT_TRAILING_ZEROS_0.
43 static inline int count_trailing_zeros(unsigned long x)
45 #define COUNT_TRAILING_ZEROS_0 (-1)
47 if (sizeof(x) == 4)
48 return ffs(x);
49 else
50 return (x != 0) ? __ffs(x) : COUNT_TRAILING_ZEROS_0;
53 #endif /* _LINUX_BITOPS_COUNT_ZEROS_H_ */