CachePowers.cs 9.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185
  1. #nullable disable
  2. // Copyright 2010 the V8 project authors. All rights reserved.
  3. // Redistribution and use in source and binary forms, with or without
  4. // modification, are permitted provided that the following conditions are
  5. // met:
  6. //
  7. // * Redistributions of source code must retain the above copyright
  8. // notice, this list of conditions and the following disclaimer.
  9. // * Redistributions in binary form must reproduce the above
  10. // copyright notice, this list of conditions and the following
  11. // disclaimer in the documentation and/or other materials provided
  12. // with the distribution.
  13. // * Neither the name of Google Inc. nor the names of its
  14. // contributors may be used to endorse or promote products derived
  15. // from this software without specific prior written permission.
  16. //
  17. // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
  18. // "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
  19. // LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
  20. // A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
  21. // OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
  22. // SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
  23. // LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
  24. // DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
  25. // THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
  26. // (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
  27. // OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  28. // Ported to Java from Mozilla's version of V8-dtoa by Hannes Wallnoefer.
  29. // The original revision was 67d1049b0bf9 from the mozilla-central tree.
  30. using System.Diagnostics;
  31. using System.Runtime.InteropServices;
  32. namespace Jint.Native.Number.Dtoa
  33. {
  34. internal static class CachedPowers
  35. {
  36. private const double Kd1Log210 = 0.30102999566398114; // 1 / lg(10)
  37. private sealed class CachedPower
  38. {
  39. internal readonly ulong Significand;
  40. internal readonly short BinaryExponent;
  41. internal readonly short DecimalExponent;
  42. internal CachedPower(ulong significand, short binaryExponent, short decimalExponent)
  43. {
  44. Significand = significand;
  45. BinaryExponent = binaryExponent;
  46. DecimalExponent = decimalExponent;
  47. }
  48. }
  49. [StructLayout(LayoutKind.Auto)]
  50. internal readonly struct GetCachedPowerResult
  51. {
  52. public GetCachedPowerResult(short decimalExponent, DiyFp cMk)
  53. {
  54. this.decimalExponent = decimalExponent;
  55. this.cMk = cMk;
  56. }
  57. internal readonly short decimalExponent;
  58. internal readonly DiyFp cMk;
  59. }
  60. internal static GetCachedPowerResult GetCachedPowerForBinaryExponentRange(int min_exponent, int max_exponent)
  61. {
  62. const int kQ = DiyFp.KSignificandSize;
  63. double k = System.Math.Ceiling((min_exponent + kQ - 1) * Kd1Log210);
  64. int foo = kCachedPowersOffset;
  65. int index =
  66. (foo + (int) k - 1) / kDecimalExponentDistance + 1;
  67. Debug.Assert(0 <= index && index < CACHED_POWERS.Length);
  68. CachedPower cachedPower = CACHED_POWERS[index];
  69. Debug.Assert(min_exponent <= cachedPower.BinaryExponent);
  70. Debug.Assert(cachedPower.BinaryExponent <= max_exponent);
  71. var cMk = new DiyFp(cachedPower.Significand, cachedPower.BinaryExponent);
  72. return new GetCachedPowerResult(cachedPower.DecimalExponent, cMk);
  73. }
  74. // Code below is converted from GRISU_CACHE_NAME(8) in file "powers-ten.h"
  75. // Regexp to convert this from original C++ source:
  76. // \{GRISU_UINT64_C\((\w+), (\w+)\), (\-?\d+), (\-?\d+)\}
  77. private static readonly CachedPower[] CACHED_POWERS =
  78. {
  79. new CachedPower(0xFA8FD5A0081C0288, -1220, -348),
  80. new CachedPower(0xBAAEE17FA23EBF76, -1193, -340),
  81. new CachedPower(0x8B16FB203055AC76, -1166, -332),
  82. new CachedPower(0xCF42894A5DCE35EA, -1140, -324),
  83. new CachedPower(0x9A6BB0AA55653B2D, -1113, -316),
  84. new CachedPower(0xE61ACF033D1A45DF, -1087, -308),
  85. new CachedPower(0xAB70FE17C79AC6CA, -1060, -300),
  86. new CachedPower(0xFF77B1FCBEBCDC4F, -1034, -292),
  87. new CachedPower(0xBE5691EF416BD60C, -1007, -284),
  88. new CachedPower(0x8DD01FAD907FFC3C, -980, -276),
  89. new CachedPower(0xD3515C2831559A83, -954, -268),
  90. new CachedPower(0x9D71AC8FADA6C9B5, -927, -260),
  91. new CachedPower(0xEA9C227723EE8BCB, -901, -252),
  92. new CachedPower(0xAECC49914078536D, -874, -244),
  93. new CachedPower(0x823C12795DB6CE57, -847, -236),
  94. new CachedPower(0xC21094364DFB5637, -821, -228),
  95. new CachedPower(0x9096EA6F3848984F, -794, -220),
  96. new CachedPower(0xD77485CB25823AC7, -768, -212),
  97. new CachedPower(0xA086CFCD97BF97F4, -741, -204),
  98. new CachedPower(0xEF340A98172AACE5, -715, -196),
  99. new CachedPower(0xB23867FB2A35B28E, -688, -188),
  100. new CachedPower(0x84C8D4DFD2C63F3B, -661, -180),
  101. new CachedPower(0xC5DD44271AD3CDBA, -635, -172),
  102. new CachedPower(0x936B9FCEBB25C996, -608, -164),
  103. new CachedPower(0xDBAC6C247D62A584, -582, -156),
  104. new CachedPower(0xA3AB66580D5FDAF6, -555, -148),
  105. new CachedPower(0xF3E2F893DEC3F126, -529, -140),
  106. new CachedPower(0xB5B5ADA8AAFF80B8, -502, -132),
  107. new CachedPower(0x87625F056C7C4A8B, -475, -124),
  108. new CachedPower(0xC9BCFF6034C13053, -449, -116),
  109. new CachedPower(0x964E858C91BA2655, -422, -108),
  110. new CachedPower(0xDFF9772470297EBD, -396, -100),
  111. new CachedPower(0xA6DFBD9FB8E5B88F, -369, -92),
  112. new CachedPower(0xF8A95FCF88747D94, -343, -84),
  113. new CachedPower(0xB94470938FA89BCF, -316, -76),
  114. new CachedPower(0x8A08F0F8BF0F156B, -289, -68),
  115. new CachedPower(0xCDB02555653131B6, -263, -60),
  116. new CachedPower(0x993FE2C6D07B7FAC, -236, -52),
  117. new CachedPower(0xE45C10C42A2B3B06, -210, -44),
  118. new CachedPower(0xAA242499697392D3, -183, -36),
  119. new CachedPower(0xFD87B5F28300CA0E, -157, -28),
  120. new CachedPower(0xBCE5086492111AEB, -130, -20),
  121. new CachedPower(0x8CBCCC096F5088CC, -103, -12),
  122. new CachedPower(0xD1B71758E219652C, -77, -4),
  123. new CachedPower(0x9C40000000000000, -50, 4),
  124. new CachedPower(0xE8D4A51000000000, -24, 12),
  125. new CachedPower(0xAD78EBC5AC620000, 3, 20),
  126. new CachedPower(0x813F3978F8940984, 30, 28),
  127. new CachedPower(0xC097CE7BC90715B3, 56, 36),
  128. new CachedPower(0x8F7E32CE7BEA5C70, 83, 44),
  129. new CachedPower(0xD5D238A4ABE98068, 109, 52),
  130. new CachedPower(0x9F4F2726179A2245, 136, 60),
  131. new CachedPower(0xED63A231D4C4FB27, 162, 68),
  132. new CachedPower(0xB0DE65388CC8ADA8, 189, 76),
  133. new CachedPower(0x83C7088E1AAB65DB, 216, 84),
  134. new CachedPower(0xC45D1DF942711D9A, 242, 92),
  135. new CachedPower(0x924D692CA61BE758, 269, 100),
  136. new CachedPower(0xDA01EE641A708DEA, 295, 108),
  137. new CachedPower(0xA26DA3999AEF774A, 322, 116),
  138. new CachedPower(0xF209787BB47D6B85, 348, 124),
  139. new CachedPower(0xB454E4A179DD1877, 375, 132),
  140. new CachedPower(0x865B86925B9BC5C2, 402, 140),
  141. new CachedPower(0xC83553C5C8965D3D, 428, 148),
  142. new CachedPower(0x952AB45CFA97A0B3, 455, 156),
  143. new CachedPower(0xDE469FBD99A05FE3, 481, 164),
  144. new CachedPower(0xA59BC234DB398C25, 508, 172),
  145. new CachedPower(0xF6C69A72A3989F5C, 534, 180),
  146. new CachedPower(0xB7DCBF5354E9BECE, 561, 188),
  147. new CachedPower(0x88FCF317F22241E2, 588, 196),
  148. new CachedPower(0xCC20CE9BD35C78A5, 614, 204),
  149. new CachedPower(0x98165AF37B2153DF, 641, 212),
  150. new CachedPower(0xE2A0B5DC971F303A, 667, 220),
  151. new CachedPower(0xA8D9D1535CE3B396, 694, 228),
  152. new CachedPower(0xFB9B7CD9A4A7443C, 720, 236),
  153. new CachedPower(0xBB764C4CA7A44410, 747, 244),
  154. new CachedPower(0x8BAB8EEFB6409C1A, 774, 252),
  155. new CachedPower(0xD01FEF10A657842C, 800, 260),
  156. new CachedPower(0x9B10A4E5E9913129, 827, 268),
  157. new CachedPower(0xE7109BFBA19C0C9D, 853, 276),
  158. new CachedPower(0xAC2820D9623BF429, 880, 284),
  159. new CachedPower(0x80444B5E7AA7CF85, 907, 292),
  160. new CachedPower(0xBF21E44003ACDD2D, 933, 300),
  161. new CachedPower(0x8E679C2F5E44FF8F, 960, 308),
  162. new CachedPower(0xD433179D9C8CB841, 986, 316),
  163. new CachedPower(0x9E19DB92B4E31BA9, 1013, 324),
  164. new CachedPower(0xEB96BF6EBADF77D9, 1039, 332),
  165. new CachedPower(0xAF87023B9BF0EE6B, 1066, 340)
  166. };
  167. const int kCachedPowersOffset = 348; // -1 * the first decimal_exponent.
  168. const int kDecimalExponentDistance = 8;
  169. const int kMinDecimalExponent = -348;
  170. const int kMaxDecimalExponent = 340;
  171. }
  172. }