Loading...
1/* SPDX-License-Identifier: GPL-2.0 */
2#ifndef _ASM_GENERIC_DIV64_H
3#define _ASM_GENERIC_DIV64_H
4/*
5 * Copyright (C) 2003 Bernardo Innocenti <bernie@develer.com>
6 * Based on former asm-ppc/div64.h and asm-m68knommu/div64.h
7 *
8 * Optimization for constant divisors on 32-bit machines:
9 * Copyright (C) 2006-2015 Nicolas Pitre
10 *
11 * The semantics of do_div() is, in C++ notation, observing that the name
12 * is a function-like macro and the n parameter has the semantics of a C++
13 * reference:
14 *
15 * uint32_t do_div(uint64_t &n, uint32_t base)
16 * {
17 * uint32_t remainder = n % base;
18 * n = n / base;
19 * return remainder;
20 * }
21 *
22 * NOTE: macro parameter n is evaluated multiple times,
23 * beware of side effects!
24 */
25
26#include <linux/types.h>
27#include <linux/compiler.h>
28
29#if BITS_PER_LONG == 64
30
31/**
32 * do_div - returns 2 values: calculate remainder and update new dividend
33 * @n: uint64_t dividend (will be updated)
34 * @base: uint32_t divisor
35 *
36 * Summary:
37 * ``uint32_t remainder = n % base;``
38 * ``n = n / base;``
39 *
40 * Return: (uint32_t)remainder
41 *
42 * NOTE: macro parameter @n is evaluated multiple times,
43 * beware of side effects!
44 */
45# define do_div(n,base) ({ \
46 uint32_t __base = (base); \
47 uint32_t __rem; \
48 __rem = ((uint64_t)(n)) % __base; \
49 (n) = ((uint64_t)(n)) / __base; \
50 __rem; \
51 })
52
53#elif BITS_PER_LONG == 32
54
55#include <linux/log2.h>
56
57/*
58 * If the divisor happens to be constant, we determine the appropriate
59 * inverse at compile time to turn the division into a few inline
60 * multiplications which ought to be much faster.
61 *
62 * (It is unfortunate that gcc doesn't perform all this internally.)
63 */
64
65#define __div64_const32(n, ___b) \
66({ \
67 /* \
68 * Multiplication by reciprocal of b: n / b = n * (p / b) / p \
69 * \
70 * We rely on the fact that most of this code gets optimized \
71 * away at compile time due to constant propagation and only \
72 * a few multiplication instructions should remain. \
73 * Hence this monstrous macro (static inline doesn't always \
74 * do the trick here). \
75 */ \
76 uint64_t ___res, ___x, ___t, ___m, ___n = (n); \
77 uint32_t ___p; \
78 bool ___bias = false; \
79 \
80 /* determine MSB of b */ \
81 ___p = 1 << ilog2(___b); \
82 \
83 /* compute m = ((p << 64) + b - 1) / b */ \
84 ___m = (~0ULL / ___b) * ___p; \
85 ___m += (((~0ULL % ___b + 1) * ___p) + ___b - 1) / ___b; \
86 \
87 /* one less than the dividend with highest result */ \
88 ___x = ~0ULL / ___b * ___b - 1; \
89 \
90 /* test our ___m with res = m * x / (p << 64) */ \
91 ___res = (___m & 0xffffffff) * (___x & 0xffffffff); \
92 ___t = (___m & 0xffffffff) * (___x >> 32) + (___res >> 32); \
93 ___res = (___m >> 32) * (___x >> 32) + (___t >> 32); \
94 ___t = (___m >> 32) * (___x & 0xffffffff) + (___t & 0xffffffff);\
95 ___res = (___res + (___t >> 32)) / ___p; \
96 \
97 /* Now validate what we've got. */ \
98 if (___res != ___x / ___b) { \
99 /* \
100 * We can't get away without a bias to compensate \
101 * for bit truncation errors. To avoid it we'd need an \
102 * additional bit to represent m which would overflow \
103 * a 64-bit variable. \
104 * \
105 * Instead we do m = p / b and n / b = (n * m + m) / p. \
106 */ \
107 ___bias = true; \
108 /* Compute m = (p << 64) / b */ \
109 ___m = (~0ULL / ___b) * ___p; \
110 ___m += ((~0ULL % ___b + 1) * ___p) / ___b; \
111 } \
112 \
113 /* Reduce m / p to help avoid overflow handling later. */ \
114 ___p /= (___m & -___m); \
115 ___m /= (___m & -___m); \
116 \
117 /* \
118 * Perform (m_bias + m * n) / (1 << 64). \
119 * From now on there will be actual runtime code generated. \
120 */ \
121 ___res = __arch_xprod_64(___m, ___n, ___bias); \
122 \
123 ___res /= ___p; \
124})
125
126#ifndef __arch_xprod_64
127/*
128 * Default C implementation for __arch_xprod_64()
129 *
130 * Prototype: uint64_t __arch_xprod_64(const uint64_t m, uint64_t n, bool bias)
131 * Semantic: retval = ((bias ? m : 0) + m * n) >> 64
132 *
133 * The product is a 128-bit value, scaled down to 64 bits.
134 * Hoping for compile-time optimization of conditional code.
135 * Architectures may provide their own optimized assembly implementation.
136 */
137#ifdef CONFIG_CC_OPTIMIZE_FOR_PERFORMANCE
138static __always_inline
139#else
140static inline
141#endif
142uint64_t __arch_xprod_64(const uint64_t m, uint64_t n, bool bias)
143{
144 uint32_t m_lo = m;
145 uint32_t m_hi = m >> 32;
146 uint32_t n_lo = n;
147 uint32_t n_hi = n >> 32;
148 uint64_t x, y;
149
150 /* Determine if overflow handling can be dispensed with. */
151 bool no_ovf = __builtin_constant_p(m) &&
152 ((m >> 32) + (m & 0xffffffff) < 0x100000000);
153
154 if (no_ovf) {
155 x = (uint64_t)m_lo * n_lo + (bias ? m : 0);
156 x >>= 32;
157 x += (uint64_t)m_lo * n_hi;
158 x += (uint64_t)m_hi * n_lo;
159 x >>= 32;
160 x += (uint64_t)m_hi * n_hi;
161 } else {
162 x = (uint64_t)m_lo * n_lo + (bias ? m_lo : 0);
163 y = (uint64_t)m_lo * n_hi + (uint32_t)(x >> 32) + (bias ? m_hi : 0);
164 x = (uint64_t)m_hi * n_hi + (uint32_t)(y >> 32);
165 y = (uint64_t)m_hi * n_lo + (uint32_t)y;
166 x += (uint32_t)(y >> 32);
167 }
168
169 return x;
170}
171#endif
172
173#ifndef __div64_32
174extern uint32_t __div64_32(uint64_t *dividend, uint32_t divisor);
175#endif
176
177/* The unnecessary pointer compare is there
178 * to check for type safety (n must be 64bit)
179 */
180# define do_div(n,base) ({ \
181 uint32_t __base = (base); \
182 uint32_t __rem; \
183 (void)(((typeof((n)) *)0) == ((uint64_t *)0)); \
184 if (__builtin_constant_p(__base) && \
185 is_power_of_2(__base)) { \
186 __rem = (n) & (__base - 1); \
187 (n) >>= ilog2(__base); \
188 } else if (__builtin_constant_p(__base) && \
189 __base != 0) { \
190 uint32_t __res_lo, __n_lo = (n); \
191 (n) = __div64_const32(n, __base); \
192 /* the remainder can be computed with 32-bit regs */ \
193 __res_lo = (n); \
194 __rem = __n_lo - __res_lo * __base; \
195 } else if (likely(((n) >> 32) == 0)) { \
196 __rem = (uint32_t)(n) % __base; \
197 (n) = (uint32_t)(n) / __base; \
198 } else { \
199 __rem = __div64_32(&(n), __base); \
200 } \
201 __rem; \
202 })
203
204#else /* BITS_PER_LONG == ?? */
205
206# error do_div() does not yet support the C64
207
208#endif /* BITS_PER_LONG */
209
210#endif /* _ASM_GENERIC_DIV64_H */
1#ifndef _ASM_GENERIC_DIV64_H
2#define _ASM_GENERIC_DIV64_H
3/*
4 * Copyright (C) 2003 Bernardo Innocenti <bernie@develer.com>
5 * Based on former asm-ppc/div64.h and asm-m68knommu/div64.h
6 *
7 * Optimization for constant divisors on 32-bit machines:
8 * Copyright (C) 2006-2015 Nicolas Pitre
9 *
10 * The semantics of do_div() are:
11 *
12 * uint32_t do_div(uint64_t *n, uint32_t base)
13 * {
14 * uint32_t remainder = *n % base;
15 * *n = *n / base;
16 * return remainder;
17 * }
18 *
19 * NOTE: macro parameter n is evaluated multiple times,
20 * beware of side effects!
21 */
22
23#include <linux/types.h>
24#include <linux/compiler.h>
25
26#if BITS_PER_LONG == 64
27
28# define do_div(n,base) ({ \
29 uint32_t __base = (base); \
30 uint32_t __rem; \
31 __rem = ((uint64_t)(n)) % __base; \
32 (n) = ((uint64_t)(n)) / __base; \
33 __rem; \
34 })
35
36#elif BITS_PER_LONG == 32
37
38#include <linux/log2.h>
39
40/*
41 * If the divisor happens to be constant, we determine the appropriate
42 * inverse at compile time to turn the division into a few inline
43 * multiplications which ought to be much faster. And yet only if compiling
44 * with a sufficiently recent gcc version to perform proper 64-bit constant
45 * propagation.
46 *
47 * (It is unfortunate that gcc doesn't perform all this internally.)
48 */
49
50#ifndef __div64_const32_is_OK
51#define __div64_const32_is_OK (__GNUC__ >= 4)
52#endif
53
54#define __div64_const32(n, ___b) \
55({ \
56 /* \
57 * Multiplication by reciprocal of b: n / b = n * (p / b) / p \
58 * \
59 * We rely on the fact that most of this code gets optimized \
60 * away at compile time due to constant propagation and only \
61 * a few multiplication instructions should remain. \
62 * Hence this monstrous macro (static inline doesn't always \
63 * do the trick here). \
64 */ \
65 uint64_t ___res, ___x, ___t, ___m, ___n = (n); \
66 uint32_t ___p, ___bias; \
67 \
68 /* determine MSB of b */ \
69 ___p = 1 << ilog2(___b); \
70 \
71 /* compute m = ((p << 64) + b - 1) / b */ \
72 ___m = (~0ULL / ___b) * ___p; \
73 ___m += (((~0ULL % ___b + 1) * ___p) + ___b - 1) / ___b; \
74 \
75 /* one less than the dividend with highest result */ \
76 ___x = ~0ULL / ___b * ___b - 1; \
77 \
78 /* test our ___m with res = m * x / (p << 64) */ \
79 ___res = ((___m & 0xffffffff) * (___x & 0xffffffff)) >> 32; \
80 ___t = ___res += (___m & 0xffffffff) * (___x >> 32); \
81 ___res += (___x & 0xffffffff) * (___m >> 32); \
82 ___t = (___res < ___t) ? (1ULL << 32) : 0; \
83 ___res = (___res >> 32) + ___t; \
84 ___res += (___m >> 32) * (___x >> 32); \
85 ___res /= ___p; \
86 \
87 /* Now sanitize and optimize what we've got. */ \
88 if (~0ULL % (___b / (___b & -___b)) == 0) { \
89 /* special case, can be simplified to ... */ \
90 ___n /= (___b & -___b); \
91 ___m = ~0ULL / (___b / (___b & -___b)); \
92 ___p = 1; \
93 ___bias = 1; \
94 } else if (___res != ___x / ___b) { \
95 /* \
96 * We can't get away without a bias to compensate \
97 * for bit truncation errors. To avoid it we'd need an \
98 * additional bit to represent m which would overflow \
99 * a 64-bit variable. \
100 * \
101 * Instead we do m = p / b and n / b = (n * m + m) / p. \
102 */ \
103 ___bias = 1; \
104 /* Compute m = (p << 64) / b */ \
105 ___m = (~0ULL / ___b) * ___p; \
106 ___m += ((~0ULL % ___b + 1) * ___p) / ___b; \
107 } else { \
108 /* \
109 * Reduce m / p, and try to clear bit 31 of m when \
110 * possible, otherwise that'll need extra overflow \
111 * handling later. \
112 */ \
113 uint32_t ___bits = -(___m & -___m); \
114 ___bits |= ___m >> 32; \
115 ___bits = (~___bits) << 1; \
116 /* \
117 * If ___bits == 0 then setting bit 31 is unavoidable. \
118 * Simply apply the maximum possible reduction in that \
119 * case. Otherwise the MSB of ___bits indicates the \
120 * best reduction we should apply. \
121 */ \
122 if (!___bits) { \
123 ___p /= (___m & -___m); \
124 ___m /= (___m & -___m); \
125 } else { \
126 ___p >>= ilog2(___bits); \
127 ___m >>= ilog2(___bits); \
128 } \
129 /* No bias needed. */ \
130 ___bias = 0; \
131 } \
132 \
133 /* \
134 * Now we have a combination of 2 conditions: \
135 * \
136 * 1) whether or not we need to apply a bias, and \
137 * \
138 * 2) whether or not there might be an overflow in the cross \
139 * product determined by (___m & ((1 << 63) | (1 << 31))). \
140 * \
141 * Select the best way to do (m_bias + m * n) / (1 << 64). \
142 * From now on there will be actual runtime code generated. \
143 */ \
144 ___res = __arch_xprod_64(___m, ___n, ___bias); \
145 \
146 ___res /= ___p; \
147})
148
149#ifndef __arch_xprod_64
150/*
151 * Default C implementation for __arch_xprod_64()
152 *
153 * Prototype: uint64_t __arch_xprod_64(const uint64_t m, uint64_t n, bool bias)
154 * Semantic: retval = ((bias ? m : 0) + m * n) >> 64
155 *
156 * The product is a 128-bit value, scaled down to 64 bits.
157 * Assuming constant propagation to optimize away unused conditional code.
158 * Architectures may provide their own optimized assembly implementation.
159 */
160static inline uint64_t __arch_xprod_64(const uint64_t m, uint64_t n, bool bias)
161{
162 uint32_t m_lo = m;
163 uint32_t m_hi = m >> 32;
164 uint32_t n_lo = n;
165 uint32_t n_hi = n >> 32;
166 uint64_t res, tmp;
167
168 if (!bias) {
169 res = ((uint64_t)m_lo * n_lo) >> 32;
170 } else if (!(m & ((1ULL << 63) | (1ULL << 31)))) {
171 /* there can't be any overflow here */
172 res = (m + (uint64_t)m_lo * n_lo) >> 32;
173 } else {
174 res = m + (uint64_t)m_lo * n_lo;
175 tmp = (res < m) ? (1ULL << 32) : 0;
176 res = (res >> 32) + tmp;
177 }
178
179 if (!(m & ((1ULL << 63) | (1ULL << 31)))) {
180 /* there can't be any overflow here */
181 res += (uint64_t)m_lo * n_hi;
182 res += (uint64_t)m_hi * n_lo;
183 res >>= 32;
184 } else {
185 tmp = res += (uint64_t)m_lo * n_hi;
186 res += (uint64_t)m_hi * n_lo;
187 tmp = (res < tmp) ? (1ULL << 32) : 0;
188 res = (res >> 32) + tmp;
189 }
190
191 res += (uint64_t)m_hi * n_hi;
192
193 return res;
194}
195#endif
196
197#ifndef __div64_32
198extern uint32_t __div64_32(uint64_t *dividend, uint32_t divisor);
199#endif
200
201/* The unnecessary pointer compare is there
202 * to check for type safety (n must be 64bit)
203 */
204# define do_div(n,base) ({ \
205 uint32_t __base = (base); \
206 uint32_t __rem; \
207 (void)(((typeof((n)) *)0) == ((uint64_t *)0)); \
208 if (__builtin_constant_p(__base) && \
209 is_power_of_2(__base)) { \
210 __rem = (n) & (__base - 1); \
211 (n) >>= ilog2(__base); \
212 } else if (__div64_const32_is_OK && \
213 __builtin_constant_p(__base) && \
214 __base != 0) { \
215 uint32_t __res_lo, __n_lo = (n); \
216 (n) = __div64_const32(n, __base); \
217 /* the remainder can be computed with 32-bit regs */ \
218 __res_lo = (n); \
219 __rem = __n_lo - __res_lo * __base; \
220 } else if (likely(((n) >> 32) == 0)) { \
221 __rem = (uint32_t)(n) % __base; \
222 (n) = (uint32_t)(n) / __base; \
223 } else \
224 __rem = __div64_32(&(n), __base); \
225 __rem; \
226 })
227
228#else /* BITS_PER_LONG == ?? */
229
230# error do_div() does not yet support the C64
231
232#endif /* BITS_PER_LONG */
233
234#endif /* _ASM_GENERIC_DIV64_H */