File Index
Files and directories
Sign in
FILE

math.h

File preview with syntax highlighting.

linux-6.19.14/include/linux/math.h C
  1/* SPDX-License-Identifier: GPL-2.0 */
  2#ifndef _LINUX_MATH_H
  3#define _LINUX_MATH_H
  4
  5#include <linux/types.h>
  6#include <asm/div64.h>
  7#include <uapi/linux/kernel.h>
  8
  9/*
 10 * This looks more complex than it should be. But we need to
 11 * get the type for the ~ right in round_down (it needs to be
 12 * as wide as the result!), and we want to evaluate the macro
 13 * arguments just once each.
 14 */
 15#define __round_mask(x, y) ((__typeof__(x))((y)-1))
 16
 17/**
 18 * round_up - round up to next specified power of 2
 19 * @x: the value to round
 20 * @y: multiple to round up to (must be a power of 2)
 21 *
 22 * Rounds @x up to next multiple of @y (which must be a power of 2).
 23 * To perform arbitrary rounding up, use roundup() below.
 24 */
 25#define round_up(x, y) ((((x)-1) | __round_mask(x, y))+1)
 26
 27/**
 28 * round_down - round down to next specified power of 2
 29 * @x: the value to round
 30 * @y: multiple to round down to (must be a power of 2)
 31 *
 32 * Rounds @x down to next multiple of @y (which must be a power of 2).
 33 * To perform arbitrary rounding down, use rounddown() below.
 34 */
 35#define round_down(x, y) ((x) & ~__round_mask(x, y))
 36
 37/**
 38 * DIV_ROUND_UP_POW2 - divide and round up
 39 * @n: numerator
 40 * @d: denominator (must be a power of 2)
 41 *
 42 * Divides @n by @d and rounds up to next multiple of @d (which must be a power
 43 * of 2). Avoids integer overflows that may occur with __KERNEL_DIV_ROUND_UP().
 44 * Performance is roughly equivalent to __KERNEL_DIV_ROUND_UP().
 45 */
 46#define DIV_ROUND_UP_POW2(n, d) \
 47	((n) / (d) + !!((n) & ((d) - 1)))
 48
 49#define DIV_ROUND_UP __KERNEL_DIV_ROUND_UP
 50
 51#define DIV_ROUND_DOWN_ULL(ll, d) \
 52	({ unsigned long long _tmp = (ll); do_div(_tmp, d); _tmp; })
 53
 54#define DIV_ROUND_UP_ULL(ll, d) \
 55	DIV_ROUND_DOWN_ULL((unsigned long long)(ll) + (d) - 1, (d))
 56
 57#if BITS_PER_LONG == 32
 58# define DIV_ROUND_UP_SECTOR_T(ll,d) DIV_ROUND_UP_ULL(ll, d)
 59#else
 60# define DIV_ROUND_UP_SECTOR_T(ll,d) DIV_ROUND_UP(ll,d)
 61#endif
 62
 63/**
 64 * roundup - round up to the next specified multiple
 65 * @x: the value to up
 66 * @y: multiple to round up to
 67 *
 68 * Rounds @x up to next multiple of @y. If @y will always be a power
 69 * of 2, consider using the faster round_up().
 70 */
 71#define roundup(x, y) (					\
 72{							\
 73	typeof(y) __y = y;				\
 74	(((x) + (__y - 1)) / __y) * __y;		\
 75}							\
 76)
 77/**
 78 * rounddown - round down to next specified multiple
 79 * @x: the value to round
 80 * @y: multiple to round down to
 81 *
 82 * Rounds @x down to next multiple of @y. If @y will always be a power
 83 * of 2, consider using the faster round_down().
 84 */
 85#define rounddown(x, y) (				\
 86{							\
 87	typeof(x) __x = (x);				\
 88	__x - (__x % (y));				\
 89}							\
 90)
 91
 92/*
 93 * Divide positive or negative dividend by positive or negative divisor
 94 * and round to closest integer. Result is undefined for negative
 95 * divisors if the dividend variable type is unsigned and for negative
 96 * dividends if the divisor variable type is unsigned.
 97 */
 98#define DIV_ROUND_CLOSEST(x, divisor)(			\
 99{							\
100	typeof(x) __x = x;				\
101	typeof(divisor) __d = divisor;			\
102	(((typeof(x))-1) > 0 ||				\
103	 ((typeof(divisor))-1) > 0 ||			\
104	 (((__x) > 0) == ((__d) > 0))) ?		\
105		(((__x) + ((__d) / 2)) / (__d)) :	\
106		(((__x) - ((__d) / 2)) / (__d));	\
107}							\
108)
109/*
110 * Same as above but for u64 dividends. divisor must be a 32-bit
111 * number.
112 */
113#define DIV_ROUND_CLOSEST_ULL(x, divisor)(		\
114{							\
115	typeof(divisor) __d = divisor;			\
116	unsigned long long _tmp = (x) + (__d) / 2;	\
117	do_div(_tmp, __d);				\
118	_tmp;						\
119}							\
120)
121
122#define __STRUCT_FRACT(type)				\
123struct type##_fract {					\
124	__##type numerator;				\
125	__##type denominator;				\
126};
127__STRUCT_FRACT(s8)
128__STRUCT_FRACT(u8)
129__STRUCT_FRACT(s16)
130__STRUCT_FRACT(u16)
131__STRUCT_FRACT(s32)
132__STRUCT_FRACT(u32)
133#undef __STRUCT_FRACT
134
135/* Calculate "x * n / d" without unnecessary overflow or loss of precision. */
136#define mult_frac(x, n, d)	\
137({				\
138	typeof(x) x_ = (x);	\
139	typeof(n) n_ = (n);	\
140	typeof(d) d_ = (d);	\
141				\
142	typeof(x_) q = x_ / d_;	\
143	typeof(x_) r = x_ % d_;	\
144	q * n_ + r * n_ / d_;	\
145})
146
147#define sector_div(a, b) do_div(a, b)
148
149/**
150 * abs - return absolute value of an argument
151 * @x: the value.
152 *
153 * If it is unsigned type, @x is converted to signed type first.
154 * char is treated as if it was signed (regardless of whether it really is)
155 * but the macro's return type is preserved as char.
156 *
157 * NOTE, for signed type if @x is the minimum, the returned result is undefined
158 * as there is not enough bits to represent it as a positive number.
159 *
160 * Return: an absolute value of @x.
161 */
162#define abs(x)	__abs_choose_expr(x, long long,				\
163		__abs_choose_expr(x, long,				\
164		__abs_choose_expr(x, int,				\
165		__abs_choose_expr(x, short,				\
166		__abs_choose_expr(x, char,				\
167		__builtin_choose_expr(					\
168			__builtin_types_compatible_p(typeof(x), char),	\
169			(char)({ signed char __x = (x); __x<0?-__x:__x; }), \
170			((void)0)))))))
171
172#define __abs_choose_expr(x, type, other) __builtin_choose_expr(	\
173	__builtin_types_compatible_p(typeof(x),   signed type) ||	\
174	__builtin_types_compatible_p(typeof(x), unsigned type),		\
175	({ signed type __x = (x); __x < 0 ? -__x : __x; }), other)
176
177/**
178 * abs_diff - return absolute value of the difference between the arguments
179 * @a: the first argument
180 * @b: the second argument
181 *
182 * @a and @b have to be of the same type. With this restriction we compare
183 * signed to signed and unsigned to unsigned. The result is the subtraction
184 * the smaller of the two from the bigger, hence result is always a positive
185 * value.
186 *
187 * Return: an absolute value of the difference between the @a and @b.
188 */
189#define abs_diff(a, b) ({			\
190	typeof(a) __a = (a);			\
191	typeof(b) __b = (b);			\
192	(void)(&__a == &__b);			\
193	__a > __b ? (__a - __b) : (__b - __a);	\
194})
195
196/**
197 * reciprocal_scale - "scale" a value into range [0, ep_ro)
198 * @val: value
199 * @ep_ro: right open interval endpoint
200 *
201 * Perform a "reciprocal multiplication" in order to "scale" a value into
202 * range [0, @ep_ro), where the upper interval endpoint is right-open.
203 * This is useful, e.g. for accessing a index of an array containing
204 * @ep_ro elements, for example. Think of it as sort of modulus, only that
205 * the result isn't that of modulo. ;) Note that if initial input is a
206 * small value, then result will return 0.
207 *
208 * Return: a result based on @val in interval [0, @ep_ro).
209 */
210static inline u32 reciprocal_scale(u32 val, u32 ep_ro)
211{
212	return (u32)(((u64) val * ep_ro) >> 32);
213}
214
215u64 int_pow(u64 base, unsigned int exp);
216unsigned long int_sqrt(unsigned long);
217
218#if BITS_PER_LONG < 64
219u32 int_sqrt64(u64 x);
220#else
221static inline u32 int_sqrt64(u64 x)
222{
223	return (u32)int_sqrt(x);
224}
225#endif
226
227#endif	/* _LINUX_MATH_H */
~/ Read-only file preview.