1*b4d0d230SThomas Gleixner /* SPDX-License-Identifier: GPL-2.0-or-later */ 2a1164a3aSChristoph Hellwig /* Count leading and trailing zeros functions 3a1164a3aSChristoph Hellwig * 4a1164a3aSChristoph Hellwig * Copyright (C) 2012 Red Hat, Inc. All Rights Reserved. 5a1164a3aSChristoph Hellwig * Written by David Howells ([email protected]) 6a1164a3aSChristoph Hellwig */ 7a1164a3aSChristoph Hellwig 8a1164a3aSChristoph Hellwig #ifndef _LINUX_BITOPS_COUNT_ZEROS_H_ 9a1164a3aSChristoph Hellwig #define _LINUX_BITOPS_COUNT_ZEROS_H_ 10a1164a3aSChristoph Hellwig 11a1164a3aSChristoph Hellwig #include <asm/bitops.h> 12a1164a3aSChristoph Hellwig 13a1164a3aSChristoph Hellwig /** 14a1164a3aSChristoph Hellwig * count_leading_zeros - Count the number of zeros from the MSB back 15a1164a3aSChristoph Hellwig * @x: The value 16a1164a3aSChristoph Hellwig * 17a1164a3aSChristoph Hellwig * Count the number of leading zeros from the MSB going towards the LSB in @x. 18a1164a3aSChristoph Hellwig * 19a1164a3aSChristoph Hellwig * If the MSB of @x is set, the result is 0. 20a1164a3aSChristoph Hellwig * If only the LSB of @x is set, then the result is BITS_PER_LONG-1. 21a1164a3aSChristoph Hellwig * If @x is 0 then the result is COUNT_LEADING_ZEROS_0. 22a1164a3aSChristoph Hellwig */ count_leading_zeros(unsigned long x)23a1164a3aSChristoph Hellwigstatic inline int count_leading_zeros(unsigned long x) 24a1164a3aSChristoph Hellwig { 25a1164a3aSChristoph Hellwig if (sizeof(x) == 4) 26a1164a3aSChristoph Hellwig return BITS_PER_LONG - fls(x); 27a1164a3aSChristoph Hellwig else 28a1164a3aSChristoph Hellwig return BITS_PER_LONG - fls64(x); 29a1164a3aSChristoph Hellwig } 30a1164a3aSChristoph Hellwig 31a1164a3aSChristoph Hellwig #define COUNT_LEADING_ZEROS_0 BITS_PER_LONG 32a1164a3aSChristoph Hellwig 33a1164a3aSChristoph Hellwig /** 34a1164a3aSChristoph Hellwig * count_trailing_zeros - Count the number of zeros from the LSB forwards 35a1164a3aSChristoph Hellwig * @x: The value 36a1164a3aSChristoph Hellwig * 37a1164a3aSChristoph Hellwig * Count the number of trailing zeros from the LSB going towards the MSB in @x. 38a1164a3aSChristoph Hellwig * 39a1164a3aSChristoph Hellwig * If the LSB of @x is set, the result is 0. 40a1164a3aSChristoph Hellwig * If only the MSB of @x is set, then the result is BITS_PER_LONG-1. 41a1164a3aSChristoph Hellwig * If @x is 0 then the result is COUNT_TRAILING_ZEROS_0. 42a1164a3aSChristoph Hellwig */ count_trailing_zeros(unsigned long x)43a1164a3aSChristoph Hellwigstatic inline int count_trailing_zeros(unsigned long x) 44a1164a3aSChristoph Hellwig { 45a1164a3aSChristoph Hellwig #define COUNT_TRAILING_ZEROS_0 (-1) 46a1164a3aSChristoph Hellwig 47a1164a3aSChristoph Hellwig if (sizeof(x) == 4) 48a1164a3aSChristoph Hellwig return ffs(x); 49a1164a3aSChristoph Hellwig else 50a1164a3aSChristoph Hellwig return (x != 0) ? __ffs(x) : COUNT_TRAILING_ZEROS_0; 51a1164a3aSChristoph Hellwig } 52a1164a3aSChristoph Hellwig 53a1164a3aSChristoph Hellwig #endif /* _LINUX_BITOPS_COUNT_ZEROS_H_ */ 54