Linux kernel mirror (for testing) git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git
kernel os linux
1
fork

Configure Feed

Select the types of activity you want to include in your feed.

kernel.h: split out mathematical helpers

kernel.h is being used as a dump for all kinds of stuff for a long time.
Here is the attempt to start cleaning it up by splitting out
mathematical helpers.

At the same time convert users in header and lib folder to use new
header. Though for time being include new header back to kernel.h to
avoid twisted indirected includes for existing users.

[sfr@canb.auug.org.au: fix powerpc build]
Link: https://lkml.kernel.org/r/20201029150809.13059608@canb.auug.org.au

Link: https://lkml.kernel.org/r/20201028173212.41768-1-andriy.shevchenko@linux.intel.com
Signed-off-by: Andy Shevchenko <andriy.shevchenko@linux.intel.com>
Cc: "Paul E. McKenney" <paulmck@kernel.org>
Cc: Trond Myklebust <trond.myklebust@hammerspace.com>
Cc: Jeff Layton <jlayton@kernel.org>
Cc: Rasmus Villemoes <linux@rasmusvillemoes.dk>
Signed-off-by: Andrew Morton <akpm@linux-foundation.org>
Signed-off-by: Linus Torvalds <torvalds@linux-foundation.org>

authored by

Andy Shevchenko and committed by
Linus Torvalds
aa6159ab 0a571b08

+215 -183
+5
fs/nfs/callback_proc.c
··· 6 6 * 7 7 * NFSv4 callback procedures 8 8 */ 9 + 10 + #include <linux/errno.h> 11 + #include <linux/math.h> 9 12 #include <linux/nfs4.h> 10 13 #include <linux/nfs_fs.h> 11 14 #include <linux/slab.h> 12 15 #include <linux/rcupdate.h> 16 + #include <linux/types.h> 17 + 13 18 #include "nfs4_fs.h" 14 19 #include "callback.h" 15 20 #include "delegation.h"
+7 -4
include/linux/bitops.h
··· 1 1 /* SPDX-License-Identifier: GPL-2.0 */ 2 2 #ifndef _LINUX_BITOPS_H 3 3 #define _LINUX_BITOPS_H 4 + 4 5 #include <asm/types.h> 5 6 #include <linux/bits.h> 7 + 8 + #include <uapi/linux/kernel.h> 6 9 7 10 /* Set bits in the first 'n' bytes when loaded from memory */ 8 11 #ifdef __LITTLE_ENDIAN ··· 15 12 #endif 16 13 17 14 #define BITS_PER_TYPE(type) (sizeof(type) * BITS_PER_BYTE) 18 - #define BITS_TO_LONGS(nr) DIV_ROUND_UP(nr, BITS_PER_TYPE(long)) 19 - #define BITS_TO_U64(nr) DIV_ROUND_UP(nr, BITS_PER_TYPE(u64)) 20 - #define BITS_TO_U32(nr) DIV_ROUND_UP(nr, BITS_PER_TYPE(u32)) 21 - #define BITS_TO_BYTES(nr) DIV_ROUND_UP(nr, BITS_PER_TYPE(char)) 15 + #define BITS_TO_LONGS(nr) __KERNEL_DIV_ROUND_UP(nr, BITS_PER_TYPE(long)) 16 + #define BITS_TO_U64(nr) __KERNEL_DIV_ROUND_UP(nr, BITS_PER_TYPE(u64)) 17 + #define BITS_TO_U32(nr) __KERNEL_DIV_ROUND_UP(nr, BITS_PER_TYPE(u32)) 18 + #define BITS_TO_BYTES(nr) __KERNEL_DIV_ROUND_UP(nr, BITS_PER_TYPE(char)) 22 19 23 20 extern unsigned int __sw_hweight8(unsigned int w); 24 21 extern unsigned int __sw_hweight16(unsigned int w);
+1
include/linux/dcache.h
··· 4 4 5 5 #include <linux/atomic.h> 6 6 #include <linux/list.h> 7 + #include <linux/math.h> 7 8 #include <linux/rculist.h> 8 9 #include <linux/rculist_bl.h> 9 10 #include <linux/spinlock.h>
+3 -1
include/linux/iommu-helper.h
··· 3 3 #define _LINUX_IOMMU_HELPER_H 4 4 5 5 #include <linux/bug.h> 6 - #include <linux/kernel.h> 6 + #include <linux/log2.h> 7 + #include <linux/math.h> 8 + #include <linux/types.h> 7 9 8 10 static inline unsigned long iommu_device_max_index(unsigned long size, 9 11 unsigned long offset,
+3 -170
include/linux/kernel.h
··· 2 2 #ifndef _LINUX_KERNEL_H 3 3 #define _LINUX_KERNEL_H 4 4 5 - 6 5 #include <stdarg.h> 7 6 #include <linux/limits.h> 8 7 #include <linux/linkage.h> ··· 10 11 #include <linux/compiler.h> 11 12 #include <linux/bitops.h> 12 13 #include <linux/log2.h> 14 + #include <linux/math.h> 13 15 #include <linux/minmax.h> 14 16 #include <linux/typecheck.h> 15 17 #include <linux/printk.h> 16 18 #include <linux/build_bug.h> 19 + 17 20 #include <asm/byteorder.h> 18 - #include <asm/div64.h> 21 + 19 22 #include <uapi/linux/kernel.h> 20 23 21 24 #define STACK_MAGIC 0xdeadbeef ··· 55 54 } \ 56 55 ) 57 56 58 - /* 59 - * This looks more complex than it should be. But we need to 60 - * get the type for the ~ right in round_down (it needs to be 61 - * as wide as the result!), and we want to evaluate the macro 62 - * arguments just once each. 63 - */ 64 - #define __round_mask(x, y) ((__typeof__(x))((y)-1)) 65 - /** 66 - * round_up - round up to next specified power of 2 67 - * @x: the value to round 68 - * @y: multiple to round up to (must be a power of 2) 69 - * 70 - * Rounds @x up to next multiple of @y (which must be a power of 2). 71 - * To perform arbitrary rounding up, use roundup() below. 72 - */ 73 - #define round_up(x, y) ((((x)-1) | __round_mask(x, y))+1) 74 - /** 75 - * round_down - round down to next specified power of 2 76 - * @x: the value to round 77 - * @y: multiple to round down to (must be a power of 2) 78 - * 79 - * Rounds @x down to next multiple of @y (which must be a power of 2). 80 - * To perform arbitrary rounding down, use rounddown() below. 81 - */ 82 - #define round_down(x, y) ((x) & ~__round_mask(x, y)) 83 - 84 57 #define typeof_member(T, m) typeof(((T*)0)->m) 85 - 86 - #define DIV_ROUND_UP __KERNEL_DIV_ROUND_UP 87 - 88 - #define DIV_ROUND_DOWN_ULL(ll, d) \ 89 - ({ unsigned long long _tmp = (ll); do_div(_tmp, d); _tmp; }) 90 - 91 - #define DIV_ROUND_UP_ULL(ll, d) \ 92 - DIV_ROUND_DOWN_ULL((unsigned long long)(ll) + (d) - 1, (d)) 93 - 94 - #if BITS_PER_LONG == 32 95 - # define DIV_ROUND_UP_SECTOR_T(ll,d) DIV_ROUND_UP_ULL(ll, d) 96 - #else 97 - # define DIV_ROUND_UP_SECTOR_T(ll,d) DIV_ROUND_UP(ll,d) 98 - #endif 99 - 100 - /** 101 - * roundup - round up to the next specified multiple 102 - * @x: the value to up 103 - * @y: multiple to round up to 104 - * 105 - * Rounds @x up to next multiple of @y. If @y will always be a power 106 - * of 2, consider using the faster round_up(). 107 - */ 108 - #define roundup(x, y) ( \ 109 - { \ 110 - typeof(y) __y = y; \ 111 - (((x) + (__y - 1)) / __y) * __y; \ 112 - } \ 113 - ) 114 - /** 115 - * rounddown - round down to next specified multiple 116 - * @x: the value to round 117 - * @y: multiple to round down to 118 - * 119 - * Rounds @x down to next multiple of @y. If @y will always be a power 120 - * of 2, consider using the faster round_down(). 121 - */ 122 - #define rounddown(x, y) ( \ 123 - { \ 124 - typeof(x) __x = (x); \ 125 - __x - (__x % (y)); \ 126 - } \ 127 - ) 128 - 129 - /* 130 - * Divide positive or negative dividend by positive or negative divisor 131 - * and round to closest integer. Result is undefined for negative 132 - * divisors if the dividend variable type is unsigned and for negative 133 - * dividends if the divisor variable type is unsigned. 134 - */ 135 - #define DIV_ROUND_CLOSEST(x, divisor)( \ 136 - { \ 137 - typeof(x) __x = x; \ 138 - typeof(divisor) __d = divisor; \ 139 - (((typeof(x))-1) > 0 || \ 140 - ((typeof(divisor))-1) > 0 || \ 141 - (((__x) > 0) == ((__d) > 0))) ? \ 142 - (((__x) + ((__d) / 2)) / (__d)) : \ 143 - (((__x) - ((__d) / 2)) / (__d)); \ 144 - } \ 145 - ) 146 - /* 147 - * Same as above but for u64 dividends. divisor must be a 32-bit 148 - * number. 149 - */ 150 - #define DIV_ROUND_CLOSEST_ULL(x, divisor)( \ 151 - { \ 152 - typeof(divisor) __d = divisor; \ 153 - unsigned long long _tmp = (x) + (__d) / 2; \ 154 - do_div(_tmp, __d); \ 155 - _tmp; \ 156 - } \ 157 - ) 158 - 159 - /* 160 - * Multiplies an integer by a fraction, while avoiding unnecessary 161 - * overflow or loss of precision. 162 - */ 163 - #define mult_frac(x, numer, denom)( \ 164 - { \ 165 - typeof(x) quot = (x) / (denom); \ 166 - typeof(x) rem = (x) % (denom); \ 167 - (quot * (numer)) + ((rem * (numer)) / (denom)); \ 168 - } \ 169 - ) 170 - 171 58 172 59 #define _RET_IP_ (unsigned long)__builtin_return_address(0) 173 60 #define _THIS_IP_ ({ __label__ __here; __here: (unsigned long)&&__here; }) 174 - 175 - #define sector_div(a, b) do_div(a, b) 176 61 177 62 /** 178 63 * upper_32_bits - return bits 32-63 of a number ··· 158 271 #endif 159 272 160 273 #define might_sleep_if(cond) do { if (cond) might_sleep(); } while (0) 161 - 162 - /** 163 - * abs - return absolute value of an argument 164 - * @x: the value. If it is unsigned type, it is converted to signed type first. 165 - * char is treated as if it was signed (regardless of whether it really is) 166 - * but the macro's return type is preserved as char. 167 - * 168 - * Return: an absolute value of x. 169 - */ 170 - #define abs(x) __abs_choose_expr(x, long long, \ 171 - __abs_choose_expr(x, long, \ 172 - __abs_choose_expr(x, int, \ 173 - __abs_choose_expr(x, short, \ 174 - __abs_choose_expr(x, char, \ 175 - __builtin_choose_expr( \ 176 - __builtin_types_compatible_p(typeof(x), char), \ 177 - (char)({ signed char __x = (x); __x<0?-__x:__x; }), \ 178 - ((void)0))))))) 179 - 180 - #define __abs_choose_expr(x, type, other) __builtin_choose_expr( \ 181 - __builtin_types_compatible_p(typeof(x), signed type) || \ 182 - __builtin_types_compatible_p(typeof(x), unsigned type), \ 183 - ({ signed type __x = (x); __x < 0 ? -__x : __x; }), other) 184 - 185 - /** 186 - * reciprocal_scale - "scale" a value into range [0, ep_ro) 187 - * @val: value 188 - * @ep_ro: right open interval endpoint 189 - * 190 - * Perform a "reciprocal multiplication" in order to "scale" a value into 191 - * range [0, @ep_ro), where the upper interval endpoint is right-open. 192 - * This is useful, e.g. for accessing a index of an array containing 193 - * @ep_ro elements, for example. Think of it as sort of modulus, only that 194 - * the result isn't that of modulo. ;) Note that if initial input is a 195 - * small value, then result will return 0. 196 - * 197 - * Return: a result based on @val in interval [0, @ep_ro). 198 - */ 199 - static inline u32 reciprocal_scale(u32 val, u32 ep_ro) 200 - { 201 - return (u32)(((u64) val * ep_ro) >> 32); 202 - } 203 274 204 275 #if defined(CONFIG_MMU) && \ 205 276 (defined(CONFIG_PROVE_LOCKING) || defined(CONFIG_DEBUG_ATOMIC_SLEEP)) ··· 359 514 extern int __kernel_text_address(unsigned long addr); 360 515 extern int kernel_text_address(unsigned long addr); 361 516 extern int func_ptr_is_kernel_text(void *ptr); 362 - 363 - u64 int_pow(u64 base, unsigned int exp); 364 - unsigned long int_sqrt(unsigned long); 365 - 366 - #if BITS_PER_LONG < 64 367 - u32 int_sqrt64(u64 x); 368 - #else 369 - static inline u32 int_sqrt64(u64 x) 370 - { 371 - return (u32)int_sqrt(x); 372 - } 373 - #endif 374 517 375 518 #ifdef CONFIG_SMP 376 519 extern unsigned int sysctl_oops_all_cpu_backtrace;
+177
include/linux/math.h
··· 1 + /* SPDX-License-Identifier: GPL-2.0 */ 2 + #ifndef _LINUX_MATH_H 3 + #define _LINUX_MATH_H 4 + 5 + #include <asm/div64.h> 6 + #include <uapi/linux/kernel.h> 7 + 8 + /* 9 + * This looks more complex than it should be. But we need to 10 + * get the type for the ~ right in round_down (it needs to be 11 + * as wide as the result!), and we want to evaluate the macro 12 + * arguments just once each. 13 + */ 14 + #define __round_mask(x, y) ((__typeof__(x))((y)-1)) 15 + 16 + /** 17 + * round_up - round up to next specified power of 2 18 + * @x: the value to round 19 + * @y: multiple to round up to (must be a power of 2) 20 + * 21 + * Rounds @x up to next multiple of @y (which must be a power of 2). 22 + * To perform arbitrary rounding up, use roundup() below. 23 + */ 24 + #define round_up(x, y) ((((x)-1) | __round_mask(x, y))+1) 25 + 26 + /** 27 + * round_down - round down to next specified power of 2 28 + * @x: the value to round 29 + * @y: multiple to round down to (must be a power of 2) 30 + * 31 + * Rounds @x down to next multiple of @y (which must be a power of 2). 32 + * To perform arbitrary rounding down, use rounddown() below. 33 + */ 34 + #define round_down(x, y) ((x) & ~__round_mask(x, y)) 35 + 36 + #define DIV_ROUND_UP __KERNEL_DIV_ROUND_UP 37 + 38 + #define DIV_ROUND_DOWN_ULL(ll, d) \ 39 + ({ unsigned long long _tmp = (ll); do_div(_tmp, d); _tmp; }) 40 + 41 + #define DIV_ROUND_UP_ULL(ll, d) \ 42 + DIV_ROUND_DOWN_ULL((unsigned long long)(ll) + (d) - 1, (d)) 43 + 44 + #if BITS_PER_LONG == 32 45 + # define DIV_ROUND_UP_SECTOR_T(ll,d) DIV_ROUND_UP_ULL(ll, d) 46 + #else 47 + # define DIV_ROUND_UP_SECTOR_T(ll,d) DIV_ROUND_UP(ll,d) 48 + #endif 49 + 50 + /** 51 + * roundup - round up to the next specified multiple 52 + * @x: the value to up 53 + * @y: multiple to round up to 54 + * 55 + * Rounds @x up to next multiple of @y. If @y will always be a power 56 + * of 2, consider using the faster round_up(). 57 + */ 58 + #define roundup(x, y) ( \ 59 + { \ 60 + typeof(y) __y = y; \ 61 + (((x) + (__y - 1)) / __y) * __y; \ 62 + } \ 63 + ) 64 + /** 65 + * rounddown - round down to next specified multiple 66 + * @x: the value to round 67 + * @y: multiple to round down to 68 + * 69 + * Rounds @x down to next multiple of @y. If @y will always be a power 70 + * of 2, consider using the faster round_down(). 71 + */ 72 + #define rounddown(x, y) ( \ 73 + { \ 74 + typeof(x) __x = (x); \ 75 + __x - (__x % (y)); \ 76 + } \ 77 + ) 78 + 79 + /* 80 + * Divide positive or negative dividend by positive or negative divisor 81 + * and round to closest integer. Result is undefined for negative 82 + * divisors if the dividend variable type is unsigned and for negative 83 + * dividends if the divisor variable type is unsigned. 84 + */ 85 + #define DIV_ROUND_CLOSEST(x, divisor)( \ 86 + { \ 87 + typeof(x) __x = x; \ 88 + typeof(divisor) __d = divisor; \ 89 + (((typeof(x))-1) > 0 || \ 90 + ((typeof(divisor))-1) > 0 || \ 91 + (((__x) > 0) == ((__d) > 0))) ? \ 92 + (((__x) + ((__d) / 2)) / (__d)) : \ 93 + (((__x) - ((__d) / 2)) / (__d)); \ 94 + } \ 95 + ) 96 + /* 97 + * Same as above but for u64 dividends. divisor must be a 32-bit 98 + * number. 99 + */ 100 + #define DIV_ROUND_CLOSEST_ULL(x, divisor)( \ 101 + { \ 102 + typeof(divisor) __d = divisor; \ 103 + unsigned long long _tmp = (x) + (__d) / 2; \ 104 + do_div(_tmp, __d); \ 105 + _tmp; \ 106 + } \ 107 + ) 108 + 109 + /* 110 + * Multiplies an integer by a fraction, while avoiding unnecessary 111 + * overflow or loss of precision. 112 + */ 113 + #define mult_frac(x, numer, denom)( \ 114 + { \ 115 + typeof(x) quot = (x) / (denom); \ 116 + typeof(x) rem = (x) % (denom); \ 117 + (quot * (numer)) + ((rem * (numer)) / (denom)); \ 118 + } \ 119 + ) 120 + 121 + #define sector_div(a, b) do_div(a, b) 122 + 123 + /** 124 + * abs - return absolute value of an argument 125 + * @x: the value. If it is unsigned type, it is converted to signed type first. 126 + * char is treated as if it was signed (regardless of whether it really is) 127 + * but the macro's return type is preserved as char. 128 + * 129 + * Return: an absolute value of x. 130 + */ 131 + #define abs(x) __abs_choose_expr(x, long long, \ 132 + __abs_choose_expr(x, long, \ 133 + __abs_choose_expr(x, int, \ 134 + __abs_choose_expr(x, short, \ 135 + __abs_choose_expr(x, char, \ 136 + __builtin_choose_expr( \ 137 + __builtin_types_compatible_p(typeof(x), char), \ 138 + (char)({ signed char __x = (x); __x<0?-__x:__x; }), \ 139 + ((void)0))))))) 140 + 141 + #define __abs_choose_expr(x, type, other) __builtin_choose_expr( \ 142 + __builtin_types_compatible_p(typeof(x), signed type) || \ 143 + __builtin_types_compatible_p(typeof(x), unsigned type), \ 144 + ({ signed type __x = (x); __x < 0 ? -__x : __x; }), other) 145 + 146 + /** 147 + * reciprocal_scale - "scale" a value into range [0, ep_ro) 148 + * @val: value 149 + * @ep_ro: right open interval endpoint 150 + * 151 + * Perform a "reciprocal multiplication" in order to "scale" a value into 152 + * range [0, @ep_ro), where the upper interval endpoint is right-open. 153 + * This is useful, e.g. for accessing a index of an array containing 154 + * @ep_ro elements, for example. Think of it as sort of modulus, only that 155 + * the result isn't that of modulo. ;) Note that if initial input is a 156 + * small value, then result will return 0. 157 + * 158 + * Return: a result based on @val in interval [0, @ep_ro). 159 + */ 160 + static inline u32 reciprocal_scale(u32 val, u32 ep_ro) 161 + { 162 + return (u32)(((u64) val * ep_ro) >> 32); 163 + } 164 + 165 + u64 int_pow(u64 base, unsigned int exp); 166 + unsigned long int_sqrt(unsigned long); 167 + 168 + #if BITS_PER_LONG < 64 169 + u32 int_sqrt64(u64 x); 170 + #else 171 + static inline u32 int_sqrt64(u64 x) 172 + { 173 + return (u32)int_sqrt(x); 174 + } 175 + #endif 176 + 177 + #endif /* _LINUX_MATH_H */
+2
include/linux/rcu_node_tree.h
··· 20 20 #ifndef __LINUX_RCU_NODE_TREE_H 21 21 #define __LINUX_RCU_NODE_TREE_H 22 22 23 + #include <linux/math.h> 24 + 23 25 /* 24 26 * Define shape of hierarchy based on NR_CPUS, CONFIG_RCU_FANOUT, and 25 27 * CONFIG_RCU_FANOUT_LEAF.
+1 -1
include/linux/units.h
··· 2 2 #ifndef _LINUX_UNITS_H 3 3 #define _LINUX_UNITS_H 4 4 5 - #include <linux/kernel.h> 5 + #include <linux/math.h> 6 6 7 7 #define ABSOLUTE_ZERO_MILLICELSIUS -273150 8 8
+1
lib/errname.c
··· 3 3 #include <linux/errno.h> 4 4 #include <linux/errname.h> 5 5 #include <linux/kernel.h> 6 + #include <linux/math.h> 6 7 7 8 /* 8 9 * Ensure these tables do not accidentally become gigantic if some
+1
lib/errseq.c
··· 3 3 #include <linux/bug.h> 4 4 #include <linux/atomic.h> 5 5 #include <linux/errseq.h> 6 + #include <linux/log2.h> 6 7 7 8 /* 8 9 * An errseq_t is a way of recording errors in one place, and allowing any
+2 -1
lib/find_bit.c
··· 15 15 #include <linux/bitops.h> 16 16 #include <linux/bitmap.h> 17 17 #include <linux/export.h> 18 - #include <linux/kernel.h> 18 + #include <linux/math.h> 19 19 #include <linux/minmax.h> 20 + #include <linux/swab.h> 20 21 21 22 #if !defined(find_next_bit) || !defined(find_next_zero_bit) || \ 22 23 !defined(find_next_bit_le) || !defined(find_next_zero_bit_le) || \
+3 -1
lib/math/div64.c
··· 18 18 * or by defining a preprocessor macro in arch/include/asm/div64.h. 19 19 */ 20 20 21 + #include <linux/bitops.h> 21 22 #include <linux/export.h> 22 - #include <linux/kernel.h> 23 + #include <linux/math.h> 23 24 #include <linux/math64.h> 25 + #include <linux/log2.h> 24 26 25 27 /* Not needed on 64bit architectures */ 26 28 #if BITS_PER_LONG == 32
+1 -1
lib/math/int_pow.c
··· 6 6 */ 7 7 8 8 #include <linux/export.h> 9 - #include <linux/kernel.h> 9 + #include <linux/math.h> 10 10 #include <linux/types.h> 11 11 12 12 /**
+2 -1
lib/math/int_sqrt.c
··· 6 6 * square root from Guy L. Steele. 7 7 */ 8 8 9 - #include <linux/kernel.h> 10 9 #include <linux/export.h> 11 10 #include <linux/bitops.h> 11 + #include <linux/limits.h> 12 + #include <linux/math.h> 12 13 13 14 /** 14 15 * int_sqrt - computes the integer square root
+6 -3
lib/math/reciprocal_div.c
··· 1 1 // SPDX-License-Identifier: GPL-2.0 2 + #include <linux/bitops.h> 2 3 #include <linux/bug.h> 3 - #include <linux/kernel.h> 4 - #include <asm/div64.h> 5 - #include <linux/reciprocal_div.h> 6 4 #include <linux/export.h> 5 + #include <linux/limits.h> 6 + #include <linux/math.h> 7 7 #include <linux/minmax.h> 8 + #include <linux/types.h> 9 + 10 + #include <linux/reciprocal_div.h> 8 11 9 12 /* 10 13 * For a description of the algorithm please have a look at