divideByConstantCodegen.c 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169
  1. /*
  2. Reference implementations of computing and using the "magic number" approach to dividing
  3. by constants, including codegen instructions. The unsigned division incorporates the
  4. "round down" optimization per ridiculous_fish.
  5. This is free and unencumbered software. Any copyright is dedicated to the Public Domain.
  6. */
  7. #include <limits.h> //for CHAR_BIT
  8. #include <assert.h>
  9. #include "divideByConstantCodegen.h"
  10. struct magicu_info compute_unsigned_magic_info(unsigned_type D, unsigned num_bits) {
  11. //The numerator must fit in a unsigned_type
  12. assert(num_bits > 0 && num_bits <= sizeof(unsigned_type) * CHAR_BIT);
  13. // D must be larger than zero and not a power of 2
  14. assert(D & (D - 1));
  15. // The eventual result
  16. struct magicu_info result;
  17. // Bits in a unsigned_type
  18. const unsigned UINT_BITS = sizeof(unsigned_type) * CHAR_BIT;
  19. // The extra shift implicit in the difference between UINT_BITS and num_bits
  20. const unsigned extra_shift = UINT_BITS - num_bits;
  21. // The initial power of 2 is one less than the first one that can possibly work
  22. const unsigned_type initial_power_of_2 = (unsigned_type)1 << (UINT_BITS - 1);
  23. // The remainder and quotient of our power of 2 divided by d
  24. unsigned_type quotient = initial_power_of_2 / D, remainder = initial_power_of_2 % D;
  25. // ceil(log_2 D)
  26. unsigned ceil_log_2_D;
  27. // The magic info for the variant "round down" algorithm
  28. unsigned_type down_multiplier = 0;
  29. unsigned down_exponent = 0;
  30. int has_magic_down = 0;
  31. // Compute ceil(log_2 D)
  32. ceil_log_2_D = 0;
  33. unsigned_type tmp;
  34. for (tmp = D; tmp > 0; tmp >>= 1)
  35. ceil_log_2_D += 1;
  36. // Begin a loop that increments the exponent, until we find a power of 2 that works.
  37. unsigned exponent;
  38. for (exponent = 0; ; exponent++) {
  39. // Quotient and remainder is from previous exponent; compute it for this exponent.
  40. if (remainder >= D - remainder) {
  41. // Doubling remainder will wrap around D
  42. quotient = quotient * 2 + 1;
  43. remainder = remainder * 2 - D;
  44. }
  45. else {
  46. // Remainder will not wrap
  47. quotient = quotient * 2;
  48. remainder = remainder * 2;
  49. }
  50. // We're done if this exponent works for the round_up algorithm.
  51. // Note that exponent may be larger than the maximum shift supported,
  52. // so the check for >= ceil_log_2_D is critical.
  53. if ((exponent + extra_shift >= ceil_log_2_D) || (D - remainder) <= ((unsigned_type)1 << (exponent + extra_shift)))
  54. break;
  55. // Set magic_down if we have not set it yet and this exponent works for the round_down algorithm
  56. if (!has_magic_down && remainder <= ((unsigned_type)1 << (exponent + extra_shift))) {
  57. has_magic_down = 1;
  58. down_multiplier = quotient;
  59. down_exponent = exponent;
  60. }
  61. }
  62. if (exponent < ceil_log_2_D) {
  63. // magic_up is efficient
  64. result.multiplier = quotient + 1;
  65. result.pre_shift = 0;
  66. result.post_shift = exponent;
  67. result.increment = 0;
  68. }
  69. else if (D & 1) {
  70. // Odd divisor, so use magic_down, which must have been set
  71. assert(has_magic_down);
  72. result.multiplier = down_multiplier;
  73. result.pre_shift = 0;
  74. result.post_shift = down_exponent;
  75. result.increment = 1;
  76. }
  77. else {
  78. // Even divisor, so use a prefix-shifted dividend
  79. unsigned pre_shift = 0;
  80. unsigned_type shifted_D = D;
  81. while ((shifted_D & 1) == 0) {
  82. shifted_D >>= 1;
  83. pre_shift += 1;
  84. }
  85. result = compute_unsigned_magic_info(shifted_D, num_bits - pre_shift);
  86. assert(result.increment == 0 && result.pre_shift == 0); //expect no increment or pre_shift in this path
  87. result.pre_shift = pre_shift;
  88. }
  89. return result;
  90. }
  91. struct magics_info compute_signed_magic_info(signed_type D) {
  92. // D must not be zero and must not be a power of 2 (or its negative)
  93. assert(D != 0 && (D & -D) != D && (D & -D) != -D);
  94. // Our result
  95. struct magics_info result;
  96. // Bits in an signed_type
  97. const unsigned SINT_BITS = sizeof(signed_type) * CHAR_BIT;
  98. // Absolute value of D (we know D is not the most negative value since that's a power of 2)
  99. const unsigned_type abs_d = (D < 0 ? -D : D);
  100. // The initial power of 2 is one less than the first one that can possibly work
  101. // "two31" in Warren
  102. unsigned exponent = SINT_BITS - 1;
  103. const unsigned_type initial_power_of_2 = (unsigned_type)1 << exponent;
  104. // Compute the absolute value of our "test numerator,"
  105. // which is the largest dividend whose remainder with d is d-1.
  106. // This is called anc in Warren.
  107. const unsigned_type tmp = initial_power_of_2 + (D < 0);
  108. const unsigned_type abs_test_numer = tmp - 1 - tmp % abs_d;
  109. // Initialize our quotients and remainders (q1, r1, q2, r2 in Warren)
  110. unsigned_type quotient1 = initial_power_of_2 / abs_test_numer, remainder1 = initial_power_of_2 % abs_test_numer;
  111. unsigned_type quotient2 = initial_power_of_2 / abs_d, remainder2 = initial_power_of_2 % abs_d;
  112. unsigned_type delta;
  113. // Begin our loop
  114. do {
  115. // Update the exponent
  116. exponent++;
  117. // Update quotient1 and remainder1
  118. quotient1 *= 2;
  119. remainder1 *= 2;
  120. if (remainder1 >= abs_test_numer) {
  121. quotient1 += 1;
  122. remainder1 -= abs_test_numer;
  123. }
  124. // Update quotient2 and remainder2
  125. quotient2 *= 2;
  126. remainder2 *= 2;
  127. if (remainder2 >= abs_d) {
  128. quotient2 += 1;
  129. remainder2 -= abs_d;
  130. }
  131. // Keep going as long as (2**exponent) / abs_d <= delta
  132. delta = abs_d - remainder2;
  133. } while (quotient1 < delta || (quotient1 == delta && remainder1 == 0));
  134. result.multiplier = quotient2 + 1;
  135. if (D < 0) result.multiplier = -result.multiplier;
  136. result.shift = exponent - SINT_BITS;
  137. return result;
  138. }