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