bitops: Change function return types from long to int
authorThorsten Blum <thorsten.blum@toblux.com>
Sat, 20 Apr 2024 22:38:37 +0000 (00:38 +0200)
committerArnd Bergmann <arnd@arndb.de>
Fri, 3 May 2024 15:04:50 +0000 (17:04 +0200)
Change the return types of bitops functions (ffs, fls, and fns) from
long to int. The expected return values are in the range [0, 64], for
which int is sufficient.

Additionally, int aligns well with the return types of the corresponding
__builtin_* functions, potentially reducing overall type conversions.

Many of the existing bitops functions already return an int and don't
need to be changed. The bitops functions in arch/ should be considered
separately.

Adjust some return variables to match the function return types.

With GCC 13 and defconfig, these changes reduced the size of a test
kernel image by 5,432 bytes on arm64 and by 248 bytes on riscv; there
were no changes in size on x86_64, powerpc, or m68k.

Signed-off-by: Thorsten Blum <thorsten.blum@toblux.com>
Signed-off-by: Arnd Bergmann <arnd@arndb.de>
include/asm-generic/bitops/__ffs.h
include/asm-generic/bitops/__fls.h
include/asm-generic/bitops/builtin-__ffs.h
include/asm-generic/bitops/builtin-__fls.h
include/linux/bitops.h
tools/include/asm-generic/bitops/__ffs.h
tools/include/asm-generic/bitops/__fls.h
tools/include/linux/bitops.h

index 446fea6dda78b97953d363945a1599712e371b39..2d08c750c8a730057e4d274df495b56973b6b0c3 100644 (file)
@@ -10,9 +10,9 @@
  *
  * Undefined if no bit exists, so code should check against 0 first.
  */
-static __always_inline unsigned long generic___ffs(unsigned long word)
+static __always_inline unsigned int generic___ffs(unsigned long word)
 {
-       int num = 0;
+       unsigned int num = 0;
 
 #if BITS_PER_LONG == 64
        if ((word & 0xffffffff) == 0) {
index 54ccccf96e21eaad9cf4a681d304ad8ac689f371..e974ec932ec189f9d88e3043ac76fe79b7a8568e 100644 (file)
@@ -10,9 +10,9 @@
  *
  * Undefined if no set bit exists, so code should check against 0 first.
  */
-static __always_inline unsigned long generic___fls(unsigned long word)
+static __always_inline unsigned int generic___fls(unsigned long word)
 {
-       int num = BITS_PER_LONG - 1;
+       unsigned int num = BITS_PER_LONG - 1;
 
 #if BITS_PER_LONG == 64
        if (!(word & (~0ul << 32))) {
index 87024da44d10c8625113502766f410beaa1f48cd..cf4b3d33bf961e9c37ab329c52ac2e957bbead1d 100644 (file)
@@ -8,7 +8,7 @@
  *
  * Undefined if no bit exists, so code should check against 0 first.
  */
-static __always_inline unsigned long __ffs(unsigned long word)
+static __always_inline unsigned int __ffs(unsigned long word)
 {
        return __builtin_ctzl(word);
 }
index 43a5aa9afbdb7247f136f13fa2d0a1efcdfe0ebf..6d72fc8a52595304c8c9b41b54ecc790db7cd57a 100644 (file)
@@ -8,7 +8,7 @@
  *
  * Undefined if no set bit exists, so code should check against 0 first.
  */
-static __always_inline unsigned long __fls(unsigned long word)
+static __always_inline unsigned int __fls(unsigned long word)
 {
        return (sizeof(word) * 8) - 1 - __builtin_clzl(word);
 }
index 2ba557e067fe69d4d96b5472721001e87a6535cc..f60220f119e255471c0bf5284b23c227eb9dc189 100644 (file)
@@ -200,7 +200,7 @@ static __always_inline __s64 sign_extend64(__u64 value, int index)
        return (__s64)(value << shift) >> shift;
 }
 
-static inline unsigned fls_long(unsigned long l)
+static inline unsigned int fls_long(unsigned long l)
 {
        if (sizeof(l) == 4)
                return fls(l);
@@ -236,7 +236,7 @@ static inline int get_count_order_long(unsigned long l)
  * The result is not defined if no bits are set, so check that @word
  * is non-zero before calling this.
  */
-static inline unsigned long __ffs64(u64 word)
+static inline unsigned int __ffs64(u64 word)
 {
 #if BITS_PER_LONG == 32
        if (((u32)word) == 0UL)
@@ -252,7 +252,7 @@ static inline unsigned long __ffs64(u64 word)
  * @word: The word to search
  * @n: Bit to find
  */
-static inline unsigned long fns(unsigned long word, unsigned int n)
+static inline unsigned int fns(unsigned long word, unsigned int n)
 {
        unsigned int bit;
 
index 9d131051949709e65ae87f1ac156ce52d58ccbbd..2d94c1e9b2f3e1dfe00504051c2974c88e2c8b0e 100644 (file)
@@ -11,9 +11,9 @@
  *
  * Undefined if no bit exists, so code should check against 0 first.
  */
-static __always_inline unsigned long __ffs(unsigned long word)
+static __always_inline unsigned int __ffs(unsigned long word)
 {
-       int num = 0;
+       unsigned int num = 0;
 
 #if __BITS_PER_LONG == 64
        if ((word & 0xffffffff) == 0) {
index 54ccccf96e21eaad9cf4a681d304ad8ac689f371..e974ec932ec189f9d88e3043ac76fe79b7a8568e 100644 (file)
@@ -10,9 +10,9 @@
  *
  * Undefined if no set bit exists, so code should check against 0 first.
  */
-static __always_inline unsigned long generic___fls(unsigned long word)
+static __always_inline unsigned int generic___fls(unsigned long word)
 {
-       int num = BITS_PER_LONG - 1;
+       unsigned int num = BITS_PER_LONG - 1;
 
 #if BITS_PER_LONG == 64
        if (!(word & (~0ul << 32))) {
index 7319f6ced10860e3f0c30c599086384a5268740f..8e073a111d2addbeda0bbfe7ef505d0184bfb514 100644 (file)
@@ -70,7 +70,7 @@ static inline unsigned long hweight_long(unsigned long w)
        return sizeof(w) == 4 ? hweight32(w) : hweight64(w);
 }
 
-static inline unsigned fls_long(unsigned long l)
+static inline unsigned int fls_long(unsigned long l)
 {
        if (sizeof(l) == 4)
                return fls(l);