driconf: Support selection by Vulkan applicationName.
[mesa.git] / src / util / fast_idiv_by_const.c
index 65a9e640789dfe14de0616da1323a4eb476384ce..4f0f6b769b812e4a5ecd9391fe2d5f0b13b50276 100644 (file)
@@ -52,6 +52,27 @@ util_compute_fast_udiv_info(uint64_t D, unsigned num_bits, unsigned UINT_BITS)
    /* The eventual result */
    struct util_fast_udiv_info result;
 
+   if (util_is_power_of_two_or_zero64(D)) {
+      unsigned div_shift = util_logbase2_64(D);
+
+      if (div_shift) {
+         /* Dividing by a power of two. */
+         result.multiplier = 1ull << (UINT_BITS - div_shift);
+         result.pre_shift = 0;
+         result.post_shift = 0;
+         result.increment = 0;
+         return result;
+      } else {
+         /* Dividing by 1. */
+         /* Assuming: floor((num + 1) * (2^32 - 1) / 2^32) = num */
+         result.multiplier = UINT_BITS == 64 ? UINT64_MAX :
+                                               (1ull << UINT_BITS) - 1;
+         result.pre_shift = 0;
+         result.post_shift = 0;
+         result.increment = 1;
+         return result;
+      }
+   }
 
    /* The extra shift implicit in the difference between UINT_BITS and num_bits
     */
@@ -151,7 +172,7 @@ util_compute_fast_udiv_info(uint64_t D, unsigned num_bits, unsigned UINT_BITS)
 static inline int64_t
 sign_extend(int64_t x, unsigned SINT_BITS)
 {
-   return (x << (64 - SINT_BITS)) >> (64 - SINT_BITS);
+   return (int64_t)((uint64_t)x << (64 - SINT_BITS)) >> (64 - SINT_BITS);
 }
 
 struct util_fast_sdiv_info