util: Generalize fast integer division to be variable bit-width

There's nothing inherently fixed-width in the code.  All that's required
to generalize it is to make everything internally 64-bit and pass
UINT_BITS in as a parameter to util_compute_fast_[us]div_info.  With
that, it can now handle 8, 16, 32, and 64-bit integer division by a
constant.

We also add support for division by 1 and by other powers of 2.  This is
useful if you want to divide by a uniform value in a shader where you
have the opportunity to adjust the uniform on the CPU before passing it
in.

Reviewed-by: Marek Olšák <marek.olsak@amd.com>
This commit is contained in:
Jason Ekstrand 2018-10-05 20:29:31 -05:00
parent 64eb0738d4
commit 7cde4dbcd7
2 changed files with 39 additions and 45 deletions

View file

@ -42,22 +42,16 @@
#include <limits.h>
#include <assert.h>
/* uint_t and sint_t can be replaced by different integer types and the code
* will work as-is. The only requirement is that sizeof(uintN) == sizeof(intN).
*/
struct util_fast_udiv_info
util_compute_fast_udiv_info(uint_t D, unsigned num_bits)
util_compute_fast_udiv_info(uint64_t D, unsigned num_bits, unsigned UINT_BITS)
{
/* The numerator must fit in a uint_t */
assert(num_bits > 0 && num_bits <= sizeof(uint_t) * CHAR_BIT);
/* The numerator must fit in a uint64_t */
assert(num_bits > 0 && num_bits <= UINT_BITS);
assert(D != 0);
/* The eventual result */
struct util_fast_udiv_info result;
/* Bits in a uint_t */
const unsigned UINT_BITS = sizeof(uint_t) * CHAR_BIT;
/* The extra shift implicit in the difference between UINT_BITS and num_bits
*/
@ -66,23 +60,23 @@ util_compute_fast_udiv_info(uint_t D, unsigned num_bits)
/* The initial power of 2 is one less than the first one that can possibly
* work.
*/
const uint_t initial_power_of_2 = (uint_t)1 << (UINT_BITS-1);
const uint64_t initial_power_of_2 = (uint64_t)1 << (UINT_BITS-1);
/* The remainder and quotient of our power of 2 divided by d */
uint_t quotient = initial_power_of_2 / D;
uint_t remainder = initial_power_of_2 % D;
uint64_t quotient = initial_power_of_2 / D;
uint64_t remainder = initial_power_of_2 % D;
/* ceil(log_2 D) */
unsigned ceil_log_2_D;
/* The magic info for the variant "round down" algorithm */
uint_t down_multiplier = 0;
uint64_t down_multiplier = 0;
unsigned down_exponent = 0;
int has_magic_down = 0;
/* Compute ceil(log_2 D) */
ceil_log_2_D = 0;
uint_t tmp;
uint64_t tmp;
for (tmp = D; tmp > 0; tmp >>= 1)
ceil_log_2_D += 1;
@ -110,14 +104,14 @@ util_compute_fast_udiv_info(uint_t D, unsigned num_bits)
* so the check for >= ceil_log_2_D is critical.
*/
if ((exponent + extra_shift >= ceil_log_2_D) ||
(D - remainder) <= ((uint_t)1 << (exponent + extra_shift)))
(D - remainder) <= ((uint64_t)1 << (exponent + extra_shift)))
break;
/* Set magic_down if we have not set it yet and this exponent works for
* the round_down algorithm
*/
if (!has_magic_down &&
remainder <= ((uint_t)1 << (exponent + extra_shift))) {
remainder <= ((uint64_t)1 << (exponent + extra_shift))) {
has_magic_down = 1;
down_multiplier = quotient;
down_exponent = exponent;
@ -140,12 +134,13 @@ util_compute_fast_udiv_info(uint_t D, unsigned num_bits)
} else {
/* Even divisor, so use a prefix-shifted dividend */
unsigned pre_shift = 0;
uint_t shifted_D = D;
uint64_t shifted_D = D;
while ((shifted_D & 1) == 0) {
shifted_D >>= 1;
pre_shift += 1;
}
result = util_compute_fast_udiv_info(shifted_D, num_bits - pre_shift);
result = util_compute_fast_udiv_info(shifted_D, num_bits - pre_shift,
UINT_BITS);
/* expect no increment or pre_shift in this path */
assert(result.increment == 0 && result.pre_shift == 0);
result.pre_shift = pre_shift;
@ -153,8 +148,14 @@ util_compute_fast_udiv_info(uint_t D, unsigned num_bits)
return result;
}
static inline int64_t
sign_extend(int64_t x, unsigned SINT_BITS)
{
return (x << (64 - SINT_BITS)) >> (64 - SINT_BITS);
}
struct util_fast_sdiv_info
util_compute_fast_sdiv_info(sint_t D)
util_compute_fast_sdiv_info(int64_t D, unsigned SINT_BITS)
{
/* D must not be zero. */
assert(D != 0);
@ -164,33 +165,30 @@ util_compute_fast_sdiv_info(sint_t D)
/* Our result */
struct util_fast_sdiv_info result;
/* Bits in an sint_t */
const unsigned SINT_BITS = sizeof(sint_t) * CHAR_BIT;
/* Absolute value of D (we know D is not the most negative value since
* that's a power of 2)
*/
const uint_t abs_d = (D < 0 ? -D : D);
const uint64_t abs_d = (D < 0 ? -D : D);
/* The initial power of 2 is one less than the first one that can possibly
* work */
/* "two31" in Warren */
unsigned exponent = SINT_BITS - 1;
const uint_t initial_power_of_2 = (uint_t)1 << exponent;
const uint64_t initial_power_of_2 = (uint64_t)1 << exponent;
/* Compute the absolute value of our "test numerator,"
* which is the largest dividend whose remainder with d is d-1.
* This is called anc in Warren.
*/
const uint_t tmp = initial_power_of_2 + (D < 0);
const uint_t abs_test_numer = tmp - 1 - tmp % abs_d;
const uint64_t tmp = initial_power_of_2 + (D < 0);
const uint64_t abs_test_numer = tmp - 1 - tmp % abs_d;
/* Initialize our quotients and remainders (q1, r1, q2, r2 in Warren) */
uint_t quotient1 = initial_power_of_2 / abs_test_numer;
uint_t remainder1 = initial_power_of_2 % abs_test_numer;
uint_t quotient2 = initial_power_of_2 / abs_d;
uint_t remainder2 = initial_power_of_2 % abs_d;
uint_t delta;
uint64_t quotient1 = initial_power_of_2 / abs_test_numer;
uint64_t remainder1 = initial_power_of_2 % abs_test_numer;
uint64_t quotient2 = initial_power_of_2 / abs_d;
uint64_t remainder2 = initial_power_of_2 % abs_d;
uint64_t delta;
/* Begin our loop */
do {
@ -217,7 +215,7 @@ util_compute_fast_sdiv_info(sint_t D)
delta = abs_d - remainder2;
} while (quotient1 < delta || (quotient1 == delta && remainder1 == 0));
result.multiplier = quotient2 + 1;
result.multiplier = sign_extend(quotient2 + 1, SINT_BITS);
if (D < 0) result.multiplier = -result.multiplier;
result.shift = exponent - SINT_BITS;
return result;

View file

@ -36,10 +36,6 @@
extern "C" {
#endif
/* You can set these to different types to get different precision. */
typedef int32_t sint_t;
typedef uint32_t uint_t;
/* Computes "magic info" for performing signed division by a fixed integer D.
* The type 'sint_t' is assumed to be defined as a signed integer type large
* enough to hold both the dividend and the divisor.
@ -68,19 +64,19 @@ typedef uint32_t uint_t;
*/
struct util_fast_sdiv_info {
sint_t multiplier; /* the "magic number" multiplier */
int64_t multiplier; /* the "magic number" multiplier */
unsigned shift; /* shift for the dividend after multiplying */
};
struct util_fast_sdiv_info
util_compute_fast_sdiv_info(sint_t D);
util_compute_fast_sdiv_info(int64_t D, unsigned SINT_BITS);
/* Computes "magic info" for performing unsigned division by a fixed positive
* integer D. The type 'uint_t' is assumed to be defined as an unsigned
* integer type large enough to hold both the dividend and the divisor.
* num_bits can be set appropriately if n is known to be smaller than
* the largest uint_t; if this is not known then pass
* "(sizeof(uint_t) * CHAR_BIT)" for num_bits.
* integer D. UINT_BITS is the bit size at which the final "magic"
* calculation will be performed; it is assumed to be large enough to hold
* both the dividand and the divisor. num_bits can be set appropriately if n
* is known to be smaller than calc_bits; if this is not known then UINT_BITS
* for num_bits.
*
* Assume we have a hardware register of width UINT_BITS, a known constant D
* which is not zero and not a power of 2, and a variable n of width num_bits
@ -120,7 +116,7 @@ util_compute_fast_sdiv_info(sint_t D);
*/
struct util_fast_udiv_info {
uint_t multiplier; /* the "magic number" multiplier */
uint64_t multiplier; /* the "magic number" multiplier */
unsigned pre_shift; /* shift for the dividend before multiplying */
unsigned post_shift; /* shift for the dividend after multiplying */
int increment; /* 0 or 1; if set then increment the numerator, using one of
@ -128,7 +124,7 @@ struct util_fast_udiv_info {
};
struct util_fast_udiv_info
util_compute_fast_udiv_info(uint_t D, unsigned num_bits);
util_compute_fast_udiv_info(uint64_t D, unsigned num_bits, unsigned UINT_BITS);
/* Below are possible options for dividing by a uniform in a shader where
* the divisor is constant but not known at compile time.