xref: /dflybsd-src/sys/dev/drm/include/linux/math64.h (revision 3f2dd94a569761201b5b0a18b2f697f97fe1b9dc)
1563a794dSFrançois Tigeot /*-
2563a794dSFrançois Tigeot  * Copyright (c) 2007 Cisco Systems, Inc.  All rights reserved.
34b77aaafSFrançois Tigeot  * Copyright (c) 2014-2015 Mellanox Technologies, Ltd. All rights reserved.
4*3f2dd94aSFrançois Tigeot  * Copyright (c) 2016-2020 François Tigeot <ftigeot@wolfpond.org>
5563a794dSFrançois Tigeot  * All rights reserved.
6563a794dSFrançois Tigeot  *
7563a794dSFrançois Tigeot  * Redistribution and use in source and binary forms, with or without
8563a794dSFrançois Tigeot  * modification, are permitted provided that the following conditions
9563a794dSFrançois Tigeot  * are met:
10563a794dSFrançois Tigeot  * 1. Redistributions of source code must retain the above copyright
11563a794dSFrançois Tigeot  *    notice unmodified, this list of conditions, and the following
12563a794dSFrançois Tigeot  *    disclaimer.
13563a794dSFrançois Tigeot  * 2. Redistributions in binary form must reproduce the above copyright
14563a794dSFrançois Tigeot  *    notice, this list of conditions and the following disclaimer in the
15563a794dSFrançois Tigeot  *    documentation and/or other materials provided with the distribution.
16563a794dSFrançois Tigeot  *
17563a794dSFrançois Tigeot  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
18563a794dSFrançois Tigeot  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
19563a794dSFrançois Tigeot  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
20563a794dSFrançois Tigeot  * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
21563a794dSFrançois Tigeot  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
22563a794dSFrançois Tigeot  * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
23563a794dSFrançois Tigeot  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
24563a794dSFrançois Tigeot  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
25563a794dSFrançois Tigeot  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
26563a794dSFrançois Tigeot  * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
274b77aaafSFrançois Tigeot  *
284b77aaafSFrançois Tigeot  * $FreeBSD: head/sys/compat/linuxkpi/common/include/linux/math64.h 290135 2015-10-29 08:28:39Z hselasky $
29563a794dSFrançois Tigeot  */
30fa3094dbSzrj 
31563a794dSFrançois Tigeot #ifndef _LINUX_MATH64_H
32563a794dSFrançois Tigeot #define _LINUX_MATH64_H
33563a794dSFrançois Tigeot 
34563a794dSFrançois Tigeot #include <linux/types.h>
35563a794dSFrançois Tigeot 
36563a794dSFrançois Tigeot #define	do_div(n, base) ({			\
37563a794dSFrançois Tigeot 	uint32_t __base = (base);		\
38563a794dSFrançois Tigeot 	uint32_t __rem;				\
39563a794dSFrançois Tigeot 	__rem = ((uint64_t)(n)) % __base;	\
40563a794dSFrançois Tigeot 	(n) = ((uint64_t)(n)) / __base;		\
41563a794dSFrançois Tigeot 	__rem;					\
42563a794dSFrançois Tigeot })
43563a794dSFrançois Tigeot 
444b77aaafSFrançois Tigeot static inline uint64_t
div_u64_rem(uint64_t dividend,uint32_t divisor,uint32_t * remainder)454b77aaafSFrançois Tigeot div_u64_rem(uint64_t dividend, uint32_t divisor, uint32_t *remainder)
46563a794dSFrançois Tigeot {
47563a794dSFrançois Tigeot 	*remainder = dividend % divisor;
484b77aaafSFrançois Tigeot 	return (dividend / divisor);
49563a794dSFrançois Tigeot }
50563a794dSFrançois Tigeot 
514b77aaafSFrançois Tigeot static inline uint64_t
div64_u64(uint64_t dividend,uint64_t divisor)524b77aaafSFrançois Tigeot div64_u64(uint64_t dividend, uint64_t divisor)
5357e252bfSMichael Neumann {
544b77aaafSFrançois Tigeot 	return (dividend / divisor);
5557e252bfSMichael Neumann }
5657e252bfSMichael Neumann 
574b77aaafSFrançois Tigeot static inline int64_t
div64_s64(int64_t dividend,int64_t divisor)584b77aaafSFrançois Tigeot div64_s64(int64_t dividend, int64_t divisor)
5957e252bfSMichael Neumann {
604b77aaafSFrançois Tigeot 	return (dividend / divisor);
6157e252bfSMichael Neumann }
62563a794dSFrançois Tigeot 
634b77aaafSFrançois Tigeot static inline uint64_t
div_u64(uint64_t dividend,uint32_t divisor)644b77aaafSFrançois Tigeot div_u64(uint64_t dividend, uint32_t divisor)
65563a794dSFrançois Tigeot {
664b77aaafSFrançois Tigeot 	return (dividend / divisor);
67563a794dSFrançois Tigeot }
68563a794dSFrançois Tigeot 
690a8d41c2SFrançois Tigeot static inline int64_t
div_s64(int64_t dividend,int32_t divisor)700a8d41c2SFrançois Tigeot div_s64(int64_t dividend, int32_t divisor)
710a8d41c2SFrançois Tigeot {
720a8d41c2SFrançois Tigeot 	return (dividend / divisor);
730a8d41c2SFrançois Tigeot }
740a8d41c2SFrançois Tigeot 
75f77dbd6cSFrançois Tigeot static inline u64
div64_u64_rem(u64 dividend,u64 divisor,u64 * remainder)76f77dbd6cSFrançois Tigeot div64_u64_rem(u64 dividend, u64 divisor, u64 *remainder)
77fa3094dbSzrj {
78fa3094dbSzrj 	*remainder = dividend % divisor;
79fa3094dbSzrj 	return (dividend / divisor);
80fa3094dbSzrj }
810a8d41c2SFrançois Tigeot 
82*3f2dd94aSFrançois Tigeot static inline u64
mul_u32_u32(u32 a,u32 b)83*3f2dd94aSFrançois Tigeot mul_u32_u32(u32 a, u32 b)
84*3f2dd94aSFrançois Tigeot {
85*3f2dd94aSFrançois Tigeot 	return (uint64_t)a * b;
86*3f2dd94aSFrançois Tigeot }
87*3f2dd94aSFrançois Tigeot 
88563a794dSFrançois Tigeot #endif	/* _LINUX_MATH64_H */
89