<div dir="auto"><div><br><br><div class="gmail_quote"><div dir="ltr">On Tue, Oct 2, 2018, 1:15 PM Jason Ekstrand <<a href="mailto:jason@jlekstrand.net">jason@jlekstrand.net</a>> wrote:<br></div><blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex"><div dir="ltr"><div>Reading through things in a bit more detail, I do believe that importing this version in some form would be better than using mine for a number of reasons:</div><div><br></div><div> * It is better optimized for signed integers</div><div> * The struct of division factors is much better than what I did.  (I did consider a struct and discarded the idea; I was wrong).</div><div> * Computation of the division factors doesn't involve N*2-bit multiplication</div><div> * The round-up algorithm here results in significantly better code than the N+1-bit round-down.<br></div><div> * I trust ridiculousfish to get this right more than I trust myself</div><div><br></div><div>That said, I have a few caveats on merging this as-is:</div><div><br></div><div> * I would like to see some unit tests.  I already spent the time to write some; they just have to be ported.</div><div> * It needs to be adjusted to handle 64-bit integers (right now, it appears to only work for num_bits <= 32)</div><div> * We shouldn't define uint_t and sint_t in a header</div><div><br></div><div>How do you want to proceed?</div></div></blockquote></div></div><div dir="auto"><br></div><div dir="auto">I don't have a plan. Anything that works for you would be OK with me, so if you wanna just rework it according to you, that's fine. Changing the types is tricky. Template code in a C header included several times would work. C++ templates would be ideal.</div><div dir="auto"><br></div><div dir="auto">What's your timeframe for this? Mine is certainly more than a month.</div><div dir="auto"><br></div><div dir="auto">Marek</div><div dir="auto"><br></div><div dir="auto"><div class="gmail_quote"><blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex"><div dir="ltr"><div><br></div><div>--Jason</div><div><br></div></div><br><div class="gmail_quote"><div dir="ltr">On Sun, Sep 23, 2018 at 11:58 AM Marek Olšák <<a href="mailto:maraeo@gmail.com" target="_blank" rel="noreferrer">maraeo@gmail.com</a>> wrote:<br></div><blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex">From: Marek Olšák <<a href="mailto:marek.olsak@amd.com" target="_blank" rel="noreferrer">marek.olsak@amd.com</a>><br>
<br>
Compilers can use this to generate optimal code for integer division<br>
by a constant.<br>
<br>
Additionally, an unsigned division by a uniform that is constant but not<br>
known at compile time can still be optimized by passing 2-4 division<br>
factors to the shader as uniforms and executing one of the fast_udiv*<br>
variants. The signed division algorithm doesn't have this capability.<br>
---<br>
 src/util/Makefile.sources     |   2 +<br>
 src/util/fast_idiv_by_const.c | 245 ++++++++++++++++++++++++++++++++++++++++++<br>
 src/util/fast_idiv_by_const.h | 173 +++++++++++++++++++++++++++++<br>
 src/util/meson.build          |   2 +<br>
 4 files changed, 422 insertions(+)<br>
 create mode 100644 src/util/fast_idiv_by_const.c<br>
 create mode 100644 src/util/fast_idiv_by_const.h<br>
<br>
diff --git a/src/util/Makefile.sources b/src/util/Makefile.sources<br>
index b562d6c..f741b2a 100644<br>
--- a/src/util/Makefile.sources<br>
+++ b/src/util/Makefile.sources<br>
@@ -3,20 +3,22 @@ MESA_UTIL_FILES := \<br>
        bitscan.h \<br>
        bitset.h \<br>
        build_id.c \<br>
        build_id.h \<br>
        crc32.c \<br>
        crc32.h \<br>
        debug.c \<br>
        debug.h \<br>
        disk_cache.c \<br>
        disk_cache.h \<br>
+       fast_idiv_by_const.c \<br>
+       fast_idiv_by_const.h \<br>
        format_r11g11b10f.h \<br>
        format_rgb9e5.h \<br>
        format_srgb.h \<br>
        futex.h \<br>
        half_float.c \<br>
        half_float.h \<br>
        hash_table.c \<br>
        hash_table.h \<br>
        list.h \<br>
        macros.h \<br>
diff --git a/src/util/fast_idiv_by_const.c b/src/util/fast_idiv_by_const.c<br>
new file mode 100644<br>
index 0000000..f247b66<br>
--- /dev/null<br>
+++ b/src/util/fast_idiv_by_const.c<br>
@@ -0,0 +1,245 @@<br>
+/*<br>
+ * Copyright © 2018 Advanced Micro Devices, Inc.<br>
+ *<br>
+ * Permission is hereby granted, free of charge, to any person obtaining a<br>
+ * copy of this software and associated documentation files (the "Software"),<br>
+ * to deal in the Software without restriction, including without limitation<br>
+ * the rights to use, copy, modify, merge, publish, distribute, sublicense,<br>
+ * and/or sell copies of the Software, and to permit persons to whom the<br>
+ * Software is furnished to do so, subject to the following conditions:<br>
+ *<br>
+ * The above copyright notice and this permission notice (including the next<br>
+ * paragraph) shall be included in all copies or substantial portions of the<br>
+ * Software.<br>
+ *<br>
+ * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR<br>
+ * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,<br>
+ * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.  IN NO EVENT SHALL<br>
+ * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER<br>
+ * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING<br>
+ * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS<br>
+ * IN THE SOFTWARE.<br>
+ */<br>
+<br>
+/* Imported from:<br>
+ *   <a href="https://raw.githubusercontent.com/ridiculousfish/libdivide/master/divide_by_constants_codegen_reference.c" rel="noreferrer noreferrer" target="_blank">https://raw.githubusercontent.com/ridiculousfish/libdivide/master/divide_by_constants_codegen_reference.c</a><br>
+ * Paper:<br>
+ *   <a href="http://ridiculousfish.com/files/faster_unsigned_division_by_constants.pdf" rel="noreferrer noreferrer" target="_blank">http://ridiculousfish.com/files/faster_unsigned_division_by_constants.pdf</a><br>
+ *<br>
+ * The author, ridiculous_fish, wrote:<br>
+ *<br>
+ *  ''Reference implementations of computing and using the "magic number"<br>
+ *    approach to dividing by constants, including codegen instructions.<br>
+ *    The unsigned division incorporates the "round down" optimization per<br>
+ *    ridiculous_fish.<br>
+ *<br>
+ *    This is free and unencumbered software. Any copyright is dedicated<br>
+ *    to the Public Domain.''<br>
+ */<br>
+<br>
+#include "fast_idiv_by_const.h"<br>
+#include "u_math.h"<br>
+#include <limits.h><br>
+#include <assert.h><br>
+<br>
+/* uint_t and sint_t can be replaced by different integer types and the code<br>
+ * will work as-is. The only requirement is that sizeof(uintN) == sizeof(intN).<br>
+ */<br>
+<br>
+struct util_fast_udiv_info<br>
+util_compute_fast_udiv_info(uint_t D, unsigned num_bits)<br>
+{<br>
+   /* The numerator must fit in a uint_t */<br>
+   assert(num_bits > 0 && num_bits <= sizeof(uint_t) * CHAR_BIT);<br>
+   assert(D != 0);<br>
+<br>
+   /* The eventual result */<br>
+   struct util_fast_udiv_info result;<br>
+<br>
+   if (util_is_power_of_two_nonzero(D)) {<br>
+      unsigned div_shift = util_logbase2(D);<br>
+<br>
+      if (div_shift) {<br>
+         /* Dividing by a power of two. */<br>
+         result.multiplier = 1 << 31;<br>
+         result.pre_shift = 0;<br>
+         result.post_shift = div_shift - 1;<br>
+         result.increment = 0;<br>
+         return result;<br>
+      } else {<br>
+         /* Dividing by 1. */<br>
+         /* Assuming: floor((num + 1) * (2^32 - 1) / 2^32) = num */<br>
+         result.multiplier = UINT_MAX;<br>
+         result.pre_shift = 0;<br>
+         result.post_shift = 0;<br>
+         result.increment = 1;<br>
+         return result;<br>
+      }<br>
+   }<br>
+<br>
+   /* Bits in a uint_t */<br>
+   const unsigned UINT_BITS = sizeof(uint_t) * CHAR_BIT;<br>
+<br>
+   /* The extra shift implicit in the difference between UINT_BITS and num_bits<br>
+    */<br>
+   const unsigned extra_shift = UINT_BITS - num_bits;<br>
+<br>
+   /* The initial power of 2 is one less than the first one that can possibly<br>
+    * work.<br>
+    */<br>
+   const uint_t initial_power_of_2 = (uint_t)1 << (UINT_BITS-1);<br>
+<br>
+   /* The remainder and quotient of our power of 2 divided by d */<br>
+   uint_t quotient = initial_power_of_2 / D;<br>
+   uint_t remainder = initial_power_of_2 % D;<br>
+<br>
+   /* ceil(log_2 D) */<br>
+   unsigned ceil_log_2_D;<br>
+<br>
+   /* The magic info for the variant "round down" algorithm */<br>
+   uint_t down_multiplier = 0;<br>
+   unsigned down_exponent = 0;<br>
+   int has_magic_down = 0;<br>
+<br>
+   /* Compute ceil(log_2 D) */<br>
+   ceil_log_2_D = 0;<br>
+   uint_t tmp;<br>
+   for (tmp = D; tmp > 0; tmp >>= 1)<br>
+      ceil_log_2_D += 1;<br>
+<br>
+<br>
+   /* Begin a loop that increments the exponent, until we find a power of 2<br>
+    * that works.<br>
+    */<br>
+   unsigned exponent;<br>
+   for (exponent = 0; ; exponent++) {<br>
+      /* Quotient and remainder is from previous exponent; compute it for this<br>
+       * exponent.<br>
+       */<br>
+      if (remainder >= D - remainder) {<br>
+         /* Doubling remainder will wrap around D */<br>
+         quotient = quotient * 2 + 1;<br>
+         remainder = remainder * 2 - D;<br>
+      } else {<br>
+         /* Remainder will not wrap */<br>
+         quotient = quotient * 2;<br>
+         remainder = remainder * 2;<br>
+      }<br>
+<br>
+      /* We're done if this exponent works for the round_up algorithm.<br>
+       * Note that exponent may be larger than the maximum shift supported,<br>
+       * so the check for >= ceil_log_2_D is critical.<br>
+       */<br>
+      if ((exponent + extra_shift >= ceil_log_2_D) ||<br>
+          (D - remainder) <= ((uint_t)1 << (exponent + extra_shift)))<br>
+         break;<br>
+<br>
+      /* Set magic_down if we have not set it yet and this exponent works for<br>
+       * the round_down algorithm<br>
+       */<br>
+      if (!has_magic_down &&<br>
+          remainder <= ((uint_t)1 << (exponent + extra_shift))) {<br>
+         has_magic_down = 1;<br>
+         down_multiplier = quotient;<br>
+         down_exponent = exponent;<br>
+      }<br>
+   }<br>
+<br>
+   if (exponent < ceil_log_2_D) {<br>
+      /* magic_up is efficient */<br>
+      result.multiplier = quotient + 1;<br>
+      result.pre_shift = 0;<br>
+      result.post_shift = exponent;<br>
+      result.increment = 0;<br>
+   } else if (D & 1) {<br>
+      /* Odd divisor, so use magic_down, which must have been set */<br>
+      assert(has_magic_down);<br>
+      result.multiplier = down_multiplier;<br>
+      result.pre_shift = 0;<br>
+      result.post_shift = down_exponent;<br>
+      result.increment = 1;<br>
+   } else {<br>
+      /* Even divisor, so use a prefix-shifted dividend */<br>
+      unsigned pre_shift = 0;<br>
+      uint_t shifted_D = D;<br>
+      while ((shifted_D & 1) == 0) {<br>
+         shifted_D >>= 1;<br>
+         pre_shift += 1;<br>
+      }<br>
+      result = util_compute_fast_udiv_info(shifted_D, num_bits - pre_shift);<br>
+      /* expect no increment or pre_shift in this path */<br>
+      assert(result.increment == 0 && result.pre_shift == 0);<br>
+      result.pre_shift = pre_shift;<br>
+   }<br>
+   return result;<br>
+}<br>
+<br>
+struct util_fast_sdiv_info<br>
+util_compute_fast_sdiv_info(sint_t D)<br>
+{<br>
+   /* D must not be zero. */<br>
+   assert(D != 0);<br>
+   /* The result is not correct for these divisors. */<br>
+   assert(D != 1 && D != -1);<br>
+<br>
+   /* Our result */<br>
+   struct util_fast_sdiv_info result;<br>
+<br>
+   /* Bits in an sint_t */<br>
+   const unsigned SINT_BITS = sizeof(sint_t) * CHAR_BIT;<br>
+<br>
+   /* Absolute value of D (we know D is not the most negative value since<br>
+    * that's a power of 2)<br>
+    */<br>
+   const uint_t abs_d = (D < 0 ? -D : D);<br>
+<br>
+   /* The initial power of 2 is one less than the first one that can possibly<br>
+    * work */<br>
+   /* "two31" in Warren */<br>
+   unsigned exponent = SINT_BITS - 1;<br>
+   const uint_t initial_power_of_2 = (uint_t)1 << exponent;<br>
+<br>
+   /* Compute the absolute value of our "test numerator,"<br>
+    * which is the largest dividend whose remainder with d is d-1.<br>
+    * This is called anc in Warren.<br>
+    */<br>
+   const uint_t tmp = initial_power_of_2 + (D < 0);<br>
+   const uint_t abs_test_numer = tmp - 1 - tmp % abs_d;<br>
+<br>
+   /* Initialize our quotients and remainders (q1, r1, q2, r2 in Warren) */<br>
+   uint_t quotient1 = initial_power_of_2 / abs_test_numer;<br>
+   uint_t remainder1 = initial_power_of_2 % abs_test_numer;<br>
+   uint_t quotient2 = initial_power_of_2 / abs_d;<br>
+   uint_t remainder2 = initial_power_of_2 % abs_d;<br>
+   uint_t delta;<br>
+<br>
+   /* Begin our loop */<br>
+   do {<br>
+      /* Update the exponent */<br>
+      exponent++;<br>
+<br>
+      /* Update quotient1 and remainder1 */<br>
+      quotient1 *= 2;<br>
+      remainder1 *= 2;<br>
+      if (remainder1 >= abs_test_numer) {<br>
+         quotient1 += 1;<br>
+         remainder1 -= abs_test_numer;<br>
+      }<br>
+<br>
+      /* Update quotient2 and remainder2 */<br>
+      quotient2 *= 2;<br>
+      remainder2 *= 2;<br>
+      if (remainder2 >= abs_d) {<br>
+         quotient2 += 1;<br>
+         remainder2 -= abs_d;<br>
+      }<br>
+<br>
+      /* Keep going as long as (2**exponent) / abs_d <= delta */<br>
+      delta = abs_d - remainder2;<br>
+   } while (quotient1 < delta || (quotient1 == delta && remainder1 == 0));<br>
+<br>
+   result.multiplier = quotient2 + 1;<br>
+   if (D < 0) result.multiplier = -result.multiplier;<br>
+   result.shift = exponent - SINT_BITS;<br>
+   return result;<br>
+}<br>
diff --git a/src/util/fast_idiv_by_const.h b/src/util/fast_idiv_by_const.h<br>
new file mode 100644<br>
index 0000000..e8debbf<br>
--- /dev/null<br>
+++ b/src/util/fast_idiv_by_const.h<br>
@@ -0,0 +1,173 @@<br>
+/*<br>
+ * Copyright © 2018 Advanced Micro Devices, Inc.<br>
+ *<br>
+ * Permission is hereby granted, free of charge, to any person obtaining a<br>
+ * copy of this software and associated documentation files (the "Software"),<br>
+ * to deal in the Software without restriction, including without limitation<br>
+ * the rights to use, copy, modify, merge, publish, distribute, sublicense,<br>
+ * and/or sell copies of the Software, and to permit persons to whom the<br>
+ * Software is furnished to do so, subject to the following conditions:<br>
+ *<br>
+ * The above copyright notice and this permission notice (including the next<br>
+ * paragraph) shall be included in all copies or substantial portions of the<br>
+ * Software.<br>
+ *<br>
+ * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR<br>
+ * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,<br>
+ * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.  IN NO EVENT SHALL<br>
+ * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER<br>
+ * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING<br>
+ * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS<br>
+ * IN THE SOFTWARE.<br>
+ */<br>
+<br>
+#ifndef FAST_IDIV_BY_CONST_H<br>
+#define FAST_IDIV_BY_CONST_H<br>
+<br>
+/* Imported from:<br>
+ *   <a href="https://raw.githubusercontent.com/ridiculousfish/libdivide/master/divide_by_constants_codegen_reference.c" rel="noreferrer noreferrer" target="_blank">https://raw.githubusercontent.com/ridiculousfish/libdivide/master/divide_by_constants_codegen_reference.c</a><br>
+ */<br>
+<br>
+#include <inttypes.h><br>
+#include <limits.h><br>
+#include <assert.h><br>
+<br>
+/* You can set these to different types to get different precision. */<br>
+typedef int32_t sint_t;<br>
+typedef uint32_t uint_t;<br>
+<br>
+/* Computes "magic info" for performing signed division by a fixed integer D.<br>
+ * The type 'sint_t' is assumed to be defined as a signed integer type large<br>
+ * enough to hold both the dividend and the divisor.<br>
+ * Here >> is arithmetic (signed) shift, and >>> is logical shift.<br>
+ *<br>
+ * To emit code for n/d, rounding towards zero, use the following sequence:<br>
+ *<br>
+ *   m = compute_signed_magic_info(D)<br>
+ *   emit("result = (m.multiplier * n) >> SINT_BITS");<br>
+ *   if d > 0 and m.multiplier < 0: emit("result += n")<br>
+ *   if d < 0 and m.multiplier > 0: emit("result -= n")<br>
+ *   if m.post_shift > 0: emit("result >>= m.shift")<br>
+ *   emit("result += (result < 0)")<br>
+ *<br>
+ * The shifts by SINT_BITS may be "free" if the high half of the full multiply<br>
+ * is put in a separate register.<br>
+ *<br>
+ * The final add can of course be implemented via the sign bit, e.g.<br>
+ *    result += (result >>> (SINT_BITS - 1))<br>
+ * or<br>
+ *    result -= (result >> (SINT_BITS - 1))<br>
+ *<br>
+ * This code is heavily indebted to Hacker's Delight by Henry Warren.<br>
+ * See <a href="http://www.hackersdelight.org/HDcode/magic.c.txt" rel="noreferrer noreferrer" target="_blank">http://www.hackersdelight.org/HDcode/magic.c.txt</a><br>
+ * Used with permission from <a href="http://www.hackersdelight.org/permissions.htm" rel="noreferrer noreferrer" target="_blank">http://www.hackersdelight.org/permissions.htm</a><br>
+ */<br>
+<br>
+struct util_fast_sdiv_info {<br>
+   sint_t multiplier; /* the "magic number" multiplier */<br>
+   unsigned shift; /* shift for the dividend after multiplying */<br>
+};<br>
+<br>
+struct util_fast_sdiv_info<br>
+util_compute_fast_sdiv_info(sint_t D);<br>
+<br>
+/* Computes "magic info" for performing unsigned division by a fixed positive<br>
+ * integer D. The type 'uint_t' is assumed to be defined as an unsigned<br>
+ * integer type large enough to hold both the dividend and the divisor.<br>
+ * num_bits can be set appropriately if n is known to be smaller than<br>
+ * the largest uint_t; if this is not known then pass<br>
+ * "(sizeof(uint_t) * CHAR_BIT)" for num_bits.<br>
+ *<br>
+ * Assume we have a hardware register of width UINT_BITS, a known constant D<br>
+ * which is  not zero and not a power of 2, and a variable n of width num_bits<br>
+ * (which may be up to UINT_BITS). To emit code for n/d, use one of the two<br>
+ * following sequences (here >>> refers to a logical bitshift):<br>
+ *<br>
+ *   m = compute_unsigned_magic_info(D, num_bits)<br>
+ *   if m.pre_shift > 0: emit("n >>>= m.pre_shift")<br>
+ *   if m.increment: emit("n = saturated_increment(n)")<br>
+ *   emit("result = (m.multiplier * n) >>> UINT_BITS")<br>
+ *   if m.post_shift > 0: emit("result >>>= m.post_shift")<br>
+ *<br>
+ * or<br>
+ *<br>
+ *   m = compute_unsigned_magic_info(D, num_bits)<br>
+ *   if m.pre_shift > 0: emit("n >>>= m.pre_shift")<br>
+ *   emit("result = m.multiplier * n")<br>
+ *   if m.increment: emit("result = result + m.multiplier")<br>
+ *   emit("result >>>= UINT_BITS")<br>
+ *   if m.post_shift > 0: emit("result >>>= m.post_shift")<br>
+ *<br>
+ * The shifts by UINT_BITS may be "free" if the high half of the full multiply<br>
+ * is put in a separate register.<br>
+ *<br>
+ * saturated_increment(n) means "increment n unless it would wrap to 0," i.e.<br>
+ *   if n == (1 << UINT_BITS)-1: result = n<br>
+ *   else: result = n+1<br>
+ * A common way to implement this is with the carry bit. For example, on x86:<br>
+ *   add 1<br>
+ *   sbb 0<br>
+ *<br>
+ * Some invariants:<br>
+ *   1: At least one of pre_shift and increment is zero<br>
+ *   2: multiplier is never zero<br>
+ *<br>
+ * This code incorporates the "round down" optimization per ridiculous_fish.<br>
+ */<br>
+<br>
+struct util_fast_udiv_info {<br>
+   uint_t multiplier; /* the "magic number" multiplier */<br>
+   unsigned pre_shift; /* shift for the dividend before multiplying */<br>
+   unsigned post_shift; /* shift for the dividend after multiplying */<br>
+   int increment; /* 0 or 1; if set then increment the numerator, using one of<br>
+                     the two strategies */<br>
+};<br>
+<br>
+struct util_fast_udiv_info<br>
+util_compute_fast_udiv_info(uint_t D, unsigned num_bits);<br>
+<br>
+/* Below are possible options for dividing by a uniform in a shader where<br>
+ * the divisor is constant but not known at compile time.<br>
+ */<br>
+<br>
+/* Full version. */<br>
+static inline unsigned<br>
+fast_udiv(unsigned n, struct util_fast_udiv_info info)<br>
+{<br>
+    n = n >> info.pre_shift;<br>
+    /* For non-power-of-two divisors, use a 32-bit ADD that clamps to UINT_MAX. */<br>
+    n = (((uint64_t)n + info.increment) * info.multiplier) >> 32;<br>
+    n = n >> info.post_shift;<br>
+    return n;<br>
+}<br>
+<br>
+/* A little more efficient version if n != UINT_MAX, i.e. no unsigned<br>
+ * wraparound in the computation.<br>
+ */<br>
+static inline unsigned<br>
+fast_udiv_nuw(unsigned n, struct util_fast_udiv_info info)<br>
+{<br>
+    assert(n != UINT_MAX);<br>
+    n = n >> info.pre_shift;<br>
+    n = n + info.increment;<br>
+    n = ((uint64_t)n * info.multiplier) >> 32;<br>
+    n = n >> info.post_shift;<br>
+    return n;<br>
+}<br>
+<br>
+/* Even faster version but both operands must be 31-bit unsigned integers<br>
+ * and the divisor must be greater than 1.<br>
+ *<br>
+ * info must be computed with num_bits == 31.<br>
+ */<br>
+static inline unsigned<br>
+fast_udiv_u31_d_not_one(unsigned n, struct util_fast_udiv_info info)<br>
+{<br>
+    assert(info.pre_shift == 0);<br>
+    assert(info.increment == 0);<br>
+    n = ((uint64_t)n * info.multiplier) >> 32;<br>
+    n = n >> info.post_shift;<br>
+    return n;<br>
+}<br>
+<br>
+#endif<br>
diff --git a/src/util/meson.build b/src/util/meson.build<br>
index 027bc5b..ebaeb47 100644<br>
--- a/src/util/meson.build<br>
+++ b/src/util/meson.build<br>
@@ -27,20 +27,22 @@ files_mesa_util = files(<br>
   'bitscan.h',<br>
   'bitset.h',<br>
   'build_id.c',<br>
   'build_id.h',<br>
   'crc32.c',<br>
   'crc32.h',<br>
   'debug.c',<br>
   'debug.h',<br>
   'disk_cache.c',<br>
   'disk_cache.h',<br>
+  'fast_idiv_by_const.c',<br>
+  'fast_idiv_by_const.h',<br>
   'format_r11g11b10f.h',<br>
   'format_rgb9e5.h',<br>
   'format_srgb.h',<br>
   'futex.h',<br>
   'half_float.c',<br>
   'half_float.h',<br>
   'hash_table.c',<br>
   'hash_table.h',<br>
   'list.h',<br>
   'macros.h',<br>
-- <br>
2.7.4<br>
<br>
_______________________________________________<br>
mesa-dev mailing list<br>
<a href="mailto:mesa-dev@lists.freedesktop.org" target="_blank" rel="noreferrer">mesa-dev@lists.freedesktop.org</a><br>
<a href="https://lists.freedesktop.org/mailman/listinfo/mesa-dev" rel="noreferrer noreferrer" target="_blank">https://lists.freedesktop.org/mailman/listinfo/mesa-dev</a><br>
</blockquote></div>
</blockquote></div></div></div>