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