util: Generalize fast integer division to be variable bit-width
[mesa.git] / src / util / fast_idiv_by_const.c
1 /*
2 * Copyright © 2018 Advanced Micro Devices, Inc.
3 *
4 * Permission is hereby granted, free of charge, to any person obtaining a
5 * copy of this software and associated documentation files (the "Software"),
6 * to deal in the Software without restriction, including without limitation
7 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
8 * and/or sell copies of the Software, and to permit persons to whom the
9 * Software is furnished to do so, subject to the following conditions:
10 *
11 * The above copyright notice and this permission notice (including the next
12 * paragraph) shall be included in all copies or substantial portions of the
13 * Software.
14 *
15 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
18 * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
20 * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
21 * IN THE SOFTWARE.
22 */
23
24 /* Imported from:
25 * https://raw.githubusercontent.com/ridiculousfish/libdivide/master/divide_by_constants_codegen_reference.c
26 * Paper:
27 * http://ridiculousfish.com/files/faster_unsigned_division_by_constants.pdf
28 *
29 * The author, ridiculous_fish, wrote:
30 *
31 * ''Reference implementations of computing and using the "magic number"
32 * approach to dividing by constants, including codegen instructions.
33 * The unsigned division incorporates the "round down" optimization per
34 * ridiculous_fish.
35 *
36 * This is free and unencumbered software. Any copyright is dedicated
37 * to the Public Domain.''
38 */
39
40 #include "fast_idiv_by_const.h"
41 #include "u_math.h"
42 #include <limits.h>
43 #include <assert.h>
44
45 struct util_fast_udiv_info
46 util_compute_fast_udiv_info(uint64_t D, unsigned num_bits, unsigned UINT_BITS)
47 {
48 /* The numerator must fit in a uint64_t */
49 assert(num_bits > 0 && num_bits <= UINT_BITS);
50 assert(D != 0);
51
52 /* The eventual result */
53 struct util_fast_udiv_info result;
54
55
56 /* The extra shift implicit in the difference between UINT_BITS and num_bits
57 */
58 const unsigned extra_shift = UINT_BITS - num_bits;
59
60 /* The initial power of 2 is one less than the first one that can possibly
61 * work.
62 */
63 const uint64_t initial_power_of_2 = (uint64_t)1 << (UINT_BITS-1);
64
65 /* The remainder and quotient of our power of 2 divided by d */
66 uint64_t quotient = initial_power_of_2 / D;
67 uint64_t remainder = initial_power_of_2 % D;
68
69 /* ceil(log_2 D) */
70 unsigned ceil_log_2_D;
71
72 /* The magic info for the variant "round down" algorithm */
73 uint64_t down_multiplier = 0;
74 unsigned down_exponent = 0;
75 int has_magic_down = 0;
76
77 /* Compute ceil(log_2 D) */
78 ceil_log_2_D = 0;
79 uint64_t tmp;
80 for (tmp = D; tmp > 0; tmp >>= 1)
81 ceil_log_2_D += 1;
82
83
84 /* Begin a loop that increments the exponent, until we find a power of 2
85 * that works.
86 */
87 unsigned exponent;
88 for (exponent = 0; ; exponent++) {
89 /* Quotient and remainder is from previous exponent; compute it for this
90 * exponent.
91 */
92 if (remainder >= D - remainder) {
93 /* Doubling remainder will wrap around D */
94 quotient = quotient * 2 + 1;
95 remainder = remainder * 2 - D;
96 } else {
97 /* Remainder will not wrap */
98 quotient = quotient * 2;
99 remainder = remainder * 2;
100 }
101
102 /* We're done if this exponent works for the round_up algorithm.
103 * Note that exponent may be larger than the maximum shift supported,
104 * so the check for >= ceil_log_2_D is critical.
105 */
106 if ((exponent + extra_shift >= ceil_log_2_D) ||
107 (D - remainder) <= ((uint64_t)1 << (exponent + extra_shift)))
108 break;
109
110 /* Set magic_down if we have not set it yet and this exponent works for
111 * the round_down algorithm
112 */
113 if (!has_magic_down &&
114 remainder <= ((uint64_t)1 << (exponent + extra_shift))) {
115 has_magic_down = 1;
116 down_multiplier = quotient;
117 down_exponent = exponent;
118 }
119 }
120
121 if (exponent < ceil_log_2_D) {
122 /* magic_up is efficient */
123 result.multiplier = quotient + 1;
124 result.pre_shift = 0;
125 result.post_shift = exponent;
126 result.increment = 0;
127 } else if (D & 1) {
128 /* Odd divisor, so use magic_down, which must have been set */
129 assert(has_magic_down);
130 result.multiplier = down_multiplier;
131 result.pre_shift = 0;
132 result.post_shift = down_exponent;
133 result.increment = 1;
134 } else {
135 /* Even divisor, so use a prefix-shifted dividend */
136 unsigned pre_shift = 0;
137 uint64_t shifted_D = D;
138 while ((shifted_D & 1) == 0) {
139 shifted_D >>= 1;
140 pre_shift += 1;
141 }
142 result = util_compute_fast_udiv_info(shifted_D, num_bits - pre_shift,
143 UINT_BITS);
144 /* expect no increment or pre_shift in this path */
145 assert(result.increment == 0 && result.pre_shift == 0);
146 result.pre_shift = pre_shift;
147 }
148 return result;
149 }
150
151 static inline int64_t
152 sign_extend(int64_t x, unsigned SINT_BITS)
153 {
154 return (x << (64 - SINT_BITS)) >> (64 - SINT_BITS);
155 }
156
157 struct util_fast_sdiv_info
158 util_compute_fast_sdiv_info(int64_t D, unsigned SINT_BITS)
159 {
160 /* D must not be zero. */
161 assert(D != 0);
162 /* The result is not correct for these divisors. */
163 assert(D != 1 && D != -1);
164
165 /* Our result */
166 struct util_fast_sdiv_info result;
167
168 /* Absolute value of D (we know D is not the most negative value since
169 * that's a power of 2)
170 */
171 const uint64_t abs_d = (D < 0 ? -D : D);
172
173 /* The initial power of 2 is one less than the first one that can possibly
174 * work */
175 /* "two31" in Warren */
176 unsigned exponent = SINT_BITS - 1;
177 const uint64_t initial_power_of_2 = (uint64_t)1 << exponent;
178
179 /* Compute the absolute value of our "test numerator,"
180 * which is the largest dividend whose remainder with d is d-1.
181 * This is called anc in Warren.
182 */
183 const uint64_t tmp = initial_power_of_2 + (D < 0);
184 const uint64_t abs_test_numer = tmp - 1 - tmp % abs_d;
185
186 /* Initialize our quotients and remainders (q1, r1, q2, r2 in Warren) */
187 uint64_t quotient1 = initial_power_of_2 / abs_test_numer;
188 uint64_t remainder1 = initial_power_of_2 % abs_test_numer;
189 uint64_t quotient2 = initial_power_of_2 / abs_d;
190 uint64_t remainder2 = initial_power_of_2 % abs_d;
191 uint64_t delta;
192
193 /* Begin our loop */
194 do {
195 /* Update the exponent */
196 exponent++;
197
198 /* Update quotient1 and remainder1 */
199 quotient1 *= 2;
200 remainder1 *= 2;
201 if (remainder1 >= abs_test_numer) {
202 quotient1 += 1;
203 remainder1 -= abs_test_numer;
204 }
205
206 /* Update quotient2 and remainder2 */
207 quotient2 *= 2;
208 remainder2 *= 2;
209 if (remainder2 >= abs_d) {
210 quotient2 += 1;
211 remainder2 -= abs_d;
212 }
213
214 /* Keep going as long as (2**exponent) / abs_d <= delta */
215 delta = abs_d - remainder2;
216 } while (quotient1 < delta || (quotient1 == delta && remainder1 == 0));
217
218 result.multiplier = sign_extend(quotient2 + 1, SINT_BITS);
219 if (D < 0) result.multiplier = -result.multiplier;
220 result.shift = exponent - SINT_BITS;
221 return result;
222 }