xref: /freebsd/sys/compat/linuxkpi/common/include/linux/math.h (revision 7937bfbc0ca53fe7cdd0d54414f9296e273a518e)
1 /*-
2  * SPDX-License-Identifier: BSD-2-Clause
3  *
4  * Copyright (c) 2013-2015 Mellanox Technologies, Ltd.
5  * Copyright (c) 2014-2015 François Tigeot
6  * Copyright (c) 2016 Matt Macy <mmacy@FreeBSD.org>
7  * Copyright (c) 2019 Johannes Lundberg <johalun@FreeBSD.org>
8  * Copyright (c) 2023 Serenity Cyber Security, LLC.
9  *
10  * Redistribution and use in source and binary forms, with or without
11  * modification, are permitted provided that the following conditions
12  * are met:
13  * 1. Redistributions of source code must retain the above copyright
14  *    notice, this list of conditions and the following disclaimer.
15  * 2. Redistributions in binary form must reproduce the above copyright
16  *    notice, this list of conditions and the following disclaimer in the
17  *    documentation and/or other materials provided with the distribution.
18  *
19  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
20  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
21  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
22  * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
23  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
24  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
25  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
26  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
27  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
28  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
29  * SUCH DAMAGE.
30  */
31 
32 #ifndef _LINUXKPI_LINUX_MATH_H_
33 #define	_LINUXKPI_LINUX_MATH_H_
34 
35 #include <linux/types.h>
36 
37 /*
38  * This looks more complex than it should be. But we need to
39  * get the type for the ~ right in round_down (it needs to be
40  * as wide as the result!), and we want to evaluate the macro
41  * arguments just once each.
42  */
43 #define	__round_mask(x, y)	((__typeof__(x))((y)-1))
44 #define	round_up(x, y)		((((x)-1) | __round_mask(x, y))+1)
45 #define	round_down(x, y)	((x) & ~__round_mask(x, y))
46 
47 #define	DIV_ROUND_UP(x, n)	howmany(x, n)
48 #define	DIV_ROUND_UP_ULL(x, n)	DIV_ROUND_UP((unsigned long long)(x), (n))
49 #define	DIV_ROUND_DOWN_ULL(x, n) (((unsigned long long)(x) / (n)) * (n))
50 
51 #define	DIV_ROUND_CLOSEST(x, divisor)	(((x) + ((divisor) / 2)) / (divisor))
52 #define	DIV_ROUND_CLOSEST_ULL(x, divisor) ({		\
53 	__typeof(divisor) __d = (divisor);		\
54 	unsigned long long __ret = (x) + (__d) / 2;	\
55 	__ret /= __d;					\
56 	__ret;						\
57 })
58 
59 #if defined(LINUXKPI_VERSION) && LINUXKPI_VERSION >= 60600
60 #define abs_diff(x, y) ({		\
61 	__typeof(x) _x = (x);		\
62 	__typeof(y) _y = (y);		\
63 	_x > _y ? _x - _y : _y - _x;	\
64 })
65 #endif
66 
67 static inline uintmax_t
68 mult_frac(uintmax_t x, uintmax_t multiplier, uintmax_t divisor)
69 {
70 	uintmax_t q = (x / divisor);
71 	uintmax_t r = (x % divisor);
72 
73 	return ((q * multiplier) + ((r * multiplier) / divisor));
74 }
75 
76 #endif /* _LINUXKPI_LINUX_MATH_H_ */
77