1270710Shselasky/*-
2270710Shselasky * Copyright (c) 2007 Cisco Systems, Inc.  All rights reserved.
3289569Shselasky * Copyright (c) 2014-2015 Mellanox Technologies, Ltd. All rights reserved.
4270710Shselasky * All rights reserved.
5270710Shselasky *
6270710Shselasky * Redistribution and use in source and binary forms, with or without
7270710Shselasky * modification, are permitted provided that the following conditions
8270710Shselasky * are met:
9270710Shselasky * 1. Redistributions of source code must retain the above copyright
10270710Shselasky *    notice unmodified, this list of conditions, and the following
11270710Shselasky *    disclaimer.
12270710Shselasky * 2. Redistributions in binary form must reproduce the above copyright
13270710Shselasky *    notice, this list of conditions and the following disclaimer in the
14270710Shselasky *    documentation and/or other materials provided with the distribution.
15270710Shselasky *
16270710Shselasky * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
17270710Shselasky * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
18270710Shselasky * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
19270710Shselasky * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
20270710Shselasky * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
21270710Shselasky * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
22270710Shselasky * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
23270710Shselasky * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
24270710Shselasky * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
25270710Shselasky * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
26289644Shselasky *
27289644Shselasky * $FreeBSD: stable/11/sys/compat/linuxkpi/common/include/linux/math64.h 361195 2020-05-18 09:24:36Z hselasky $
28270710Shselasky */
29328653Shselasky
30270710Shselasky#ifndef _LINUX_MATH64_H
31289569Shselasky#define	_LINUX_MATH64_H
32270710Shselasky
33289569Shselasky#include <sys/stdint.h>
34270710Shselasky
35289569Shselasky#define	do_div(n, base) ({			\
36289569Shselasky	uint32_t __base = (base);		\
37289569Shselasky	uint32_t __rem;				\
38289569Shselasky	__rem = ((uint64_t)(n)) % __base;	\
39289569Shselasky	(n) = ((uint64_t)(n)) / __base;		\
40289569Shselasky	__rem;					\
41270710Shselasky})
42270710Shselasky
43289569Shselaskystatic inline uint64_t
44328653Shselaskydiv64_u64_rem(uint64_t dividend, uint64_t divisor, uint64_t *remainder)
45328653Shselasky{
46328653Shselasky
47328653Shselasky	*remainder = dividend % divisor;
48328653Shselasky	return (dividend / divisor);
49328653Shselasky}
50328653Shselasky
51328653Shselaskystatic inline int64_t
52328653Shselaskydiv64_s64(int64_t dividend, int64_t divisor)
53328653Shselasky{
54328653Shselasky
55328653Shselasky	return (dividend / divisor);
56328653Shselasky}
57328653Shselasky
58328653Shselaskystatic inline uint64_t
59328653Shselaskydiv64_u64(uint64_t dividend, uint64_t divisor)
60328653Shselasky{
61328653Shselasky
62328653Shselasky	return (dividend / divisor);
63328653Shselasky}
64328653Shselasky
65328653Shselaskystatic inline uint64_t
66289569Shselaskydiv_u64_rem(uint64_t dividend, uint32_t divisor, uint32_t *remainder)
67270710Shselasky{
68328653Shselasky
69289569Shselasky	*remainder = dividend % divisor;
70289569Shselasky	return (dividend / divisor);
71270710Shselasky}
72270710Shselasky
73328653Shselaskystatic inline int64_t
74328653Shselaskydiv_s64(int64_t dividend, int32_t divisor)
75328653Shselasky{
76328653Shselasky
77328653Shselasky	return (dividend / divisor);
78328653Shselasky}
79328653Shselasky
80289569Shselaskystatic inline uint64_t
81289569Shselaskydiv_u64(uint64_t dividend, uint32_t divisor)
82270710Shselasky{
83328653Shselasky
84289569Shselasky	return (dividend / divisor);
85270710Shselasky}
86270710Shselasky
87335415Shselaskystatic inline uint64_t
88335415Shselaskymul_u32_u32(uint32_t a, uint32_t b)
89335415Shselasky{
90335415Shselasky
91335415Shselasky	return ((uint64_t)a * b);
92335415Shselasky}
93335415Shselasky
94361195Shselaskystatic inline uint64_t
95361195Shselaskydiv64_u64_round_up(uint64_t dividend, uint64_t divisor)
96361195Shselasky{
97361195Shselasky	return ((dividend + divisor - 1) / divisor);
98361195Shselasky}
99361195Shselasky
100361195Shselasky#define	DIV64_U64_ROUND_UP(...) \
101361195Shselasky	div64_u64_round_up(__VA_ARGS__)
102361195Shselasky
103328653Shselasky#endif /* _LINUX_MATH64_H */
104