d2s_intrinsics.hpp 6.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232
  1. // Copyright 2018 Ulf Adams
  2. //
  3. // The contents of this file may be used under the terms of the Apache License,
  4. // Version 2.0.
  5. //
  6. // (See accompanying file LICENSE-Apache or copy at
  7. // http://www.apache.org/licenses/LICENSE-2.0)
  8. //
  9. // Alternatively, the contents of this file may be used under the terms of
  10. // the Boost Software License, Version 1.0.
  11. // (See accompanying file LICENSE-Boost or copy at
  12. // https://www.boost.org/LICENSE_1_0.txt)
  13. //
  14. // Unless required by applicable law or agreed to in writing, this software
  15. // is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
  16. // KIND, either express or implied.
  17. /*
  18. This is a derivative work
  19. */
  20. #ifndef BOOST_JSON_DETAIL_RYU_DETAIL_D2S_INTRINSICS_HPP
  21. #define BOOST_JSON_DETAIL_RYU_DETAIL_D2S_INTRINSICS_HPP
  22. #include <boost/json/detail/config.hpp>
  23. // This sets BOOST_JSON_RYU_32_BIT_PLATFORM as a side effect if applicable.
  24. #include <boost/json/detail/ryu/detail/common.hpp>
  25. #if defined(BOOST_JSON_RYU_HAS_64_BIT_INTRINSICS)
  26. #include <intrin.h>
  27. #endif
  28. namespace boost {
  29. namespace json {
  30. namespace detail {
  31. namespace ryu {
  32. namespace detail {
  33. #if defined(BOOST_JSON_RYU_HAS_64_BIT_INTRINSICS)
  34. inline uint64_t umul128(const uint64_t a, const uint64_t b, uint64_t* const productHi) {
  35. return _umul128(a, b, productHi);
  36. }
  37. inline uint64_t shiftright128(const uint64_t lo, const uint64_t hi, const uint32_t dist) {
  38. // For the __shiftright128 intrinsic, the shift value is always
  39. // modulo 64.
  40. // In the current implementation of the double-precision version
  41. // of Ryu, the shift value is always < 64. (In the case
  42. // RYU_OPTIMIZE_SIZE == 0, the shift value is in the range [49, 58].
  43. // Otherwise in the range [2, 59].)
  44. // Check this here in case a future change requires larger shift
  45. // values. In this case this function needs to be adjusted.
  46. BOOST_ASSERT(dist < 64);
  47. return __shiftright128(lo, hi, (unsigned char) dist);
  48. }
  49. #else // defined(HAS_64_BIT_INTRINSICS)
  50. inline uint64_t umul128(const uint64_t a, const uint64_t b, uint64_t* const productHi) {
  51. // The casts here help MSVC to avoid calls to the __allmul library function.
  52. const uint32_t aLo = (uint32_t)a;
  53. const uint32_t aHi = (uint32_t)(a >> 32);
  54. const uint32_t bLo = (uint32_t)b;
  55. const uint32_t bHi = (uint32_t)(b >> 32);
  56. const uint64_t b00 = (uint64_t)aLo * bLo;
  57. const uint64_t b01 = (uint64_t)aLo * bHi;
  58. const uint64_t b10 = (uint64_t)aHi * bLo;
  59. const uint64_t b11 = (uint64_t)aHi * bHi;
  60. const uint32_t b00Lo = (uint32_t)b00;
  61. const uint32_t b00Hi = (uint32_t)(b00 >> 32);
  62. const uint64_t mid1 = b10 + b00Hi;
  63. const uint32_t mid1Lo = (uint32_t)(mid1);
  64. const uint32_t mid1Hi = (uint32_t)(mid1 >> 32);
  65. const uint64_t mid2 = b01 + mid1Lo;
  66. const uint32_t mid2Lo = (uint32_t)(mid2);
  67. const uint32_t mid2Hi = (uint32_t)(mid2 >> 32);
  68. const uint64_t pHi = b11 + mid1Hi + mid2Hi;
  69. const uint64_t pLo = ((uint64_t)mid2Lo << 32) | b00Lo;
  70. *productHi = pHi;
  71. return pLo;
  72. }
  73. inline uint64_t shiftright128(const uint64_t lo, const uint64_t hi, const uint32_t dist) {
  74. // We don't need to handle the case dist >= 64 here (see above).
  75. BOOST_ASSERT(dist < 64);
  76. #if defined(RYU_OPTIMIZE_SIZE) || !defined(RYU_32_BIT_PLATFORM)
  77. BOOST_ASSERT(dist > 0);
  78. return (hi << (64 - dist)) | (lo >> dist);
  79. #else
  80. // Avoid a 64-bit shift by taking advantage of the range of shift values.
  81. BOOST_ASSERT(dist >= 32);
  82. return (hi << (64 - dist)) | ((uint32_t)(lo >> 32) >> (dist - 32));
  83. #endif
  84. }
  85. #endif // defined(HAS_64_BIT_INTRINSICS)
  86. #ifdef RYU_32_BIT_PLATFORM
  87. // Returns the high 64 bits of the 128-bit product of a and b.
  88. inline uint64_t umulh(const uint64_t a, const uint64_t b) {
  89. // Reuse the umul128 implementation.
  90. // Optimizers will likely eliminate the instructions used to compute the
  91. // low part of the product.
  92. uint64_t hi;
  93. umul128(a, b, &hi);
  94. return hi;
  95. }
  96. // On 32-bit platforms, compilers typically generate calls to library
  97. // functions for 64-bit divisions, even if the divisor is a constant.
  98. //
  99. // E.g.:
  100. // https://bugs.llvm.org/show_bug.cgi?id=37932
  101. // https://gcc.gnu.org/bugzilla/show_bug.cgi?id=17958
  102. // https://gcc.gnu.org/bugzilla/show_bug.cgi?id=37443
  103. //
  104. // The functions here perform division-by-constant using multiplications
  105. // in the same way as 64-bit compilers would do.
  106. //
  107. // NB:
  108. // The multipliers and shift values are the ones generated by clang x64
  109. // for expressions like x/5, x/10, etc.
  110. inline uint64_t div5(const uint64_t x) {
  111. return umulh(x, 0xCCCCCCCCCCCCCCCDu) >> 2;
  112. }
  113. inline uint64_t div10(const uint64_t x) {
  114. return umulh(x, 0xCCCCCCCCCCCCCCCDu) >> 3;
  115. }
  116. inline uint64_t div100(const uint64_t x) {
  117. return umulh(x >> 2, 0x28F5C28F5C28F5C3u) >> 2;
  118. }
  119. inline uint64_t div1e8(const uint64_t x) {
  120. return umulh(x, 0xABCC77118461CEFDu) >> 26;
  121. }
  122. inline uint64_t div1e9(const uint64_t x) {
  123. return umulh(x >> 9, 0x44B82FA09B5A53u) >> 11;
  124. }
  125. inline uint32_t mod1e9(const uint64_t x) {
  126. // Avoid 64-bit math as much as possible.
  127. // Returning (uint32_t) (x - 1000000000 * div1e9(x)) would
  128. // perform 32x64-bit multiplication and 64-bit subtraction.
  129. // x and 1000000000 * div1e9(x) are guaranteed to differ by
  130. // less than 10^9, so their highest 32 bits must be identical,
  131. // so we can truncate both sides to uint32_t before subtracting.
  132. // We can also simplify (uint32_t) (1000000000 * div1e9(x)).
  133. // We can truncate before multiplying instead of after, as multiplying
  134. // the highest 32 bits of div1e9(x) can't affect the lowest 32 bits.
  135. return ((uint32_t) x) - 1000000000 * ((uint32_t) div1e9(x));
  136. }
  137. #else // RYU_32_BIT_PLATFORM
  138. inline uint64_t div5(const uint64_t x) {
  139. return x / 5;
  140. }
  141. inline uint64_t div10(const uint64_t x) {
  142. return x / 10;
  143. }
  144. inline uint64_t div100(const uint64_t x) {
  145. return x / 100;
  146. }
  147. inline uint64_t div1e8(const uint64_t x) {
  148. return x / 100000000;
  149. }
  150. inline uint64_t div1e9(const uint64_t x) {
  151. return x / 1000000000;
  152. }
  153. inline uint32_t mod1e9(const uint64_t x) {
  154. return (uint32_t) (x - 1000000000 * div1e9(x));
  155. }
  156. #endif // RYU_32_BIT_PLATFORM
  157. inline uint32_t pow5Factor(uint64_t value) {
  158. uint32_t count = 0;
  159. for (;;) {
  160. BOOST_ASSERT(value != 0);
  161. const uint64_t q = div5(value);
  162. const uint32_t r = ((uint32_t) value) - 5 * ((uint32_t) q);
  163. if (r != 0) {
  164. break;
  165. }
  166. value = q;
  167. ++count;
  168. }
  169. return count;
  170. }
  171. // Returns true if value is divisible by 5^p.
  172. inline bool multipleOfPowerOf5(const uint64_t value, const uint32_t p) {
  173. // I tried a case distinction on p, but there was no performance difference.
  174. return pow5Factor(value) >= p;
  175. }
  176. // Returns true if value is divisible by 2^p.
  177. inline bool multipleOfPowerOf2(const uint64_t value, const uint32_t p) {
  178. BOOST_ASSERT(value != 0);
  179. // return __builtin_ctzll(value) >= p;
  180. return (value & ((1ull << p) - 1)) == 0;
  181. }
  182. } // detail
  183. } // ryu
  184. } // detail
  185. } // namespace json
  186. } // namespace boost
  187. #endif