github.com/nuvolaris/goja@v0.0.0-20230825100449-967811910c6d/ftoa/internal/fast/cachedpower.go (about)

     1  package fast
     2  
     3  import "math"
     4  
     5  const (
     6  	kCachedPowersOffset      = 348                 // -1 * the first decimal_exponent.
     7  	kD_1_LOG2_10             = 0.30102999566398114 //  1 / lg(10)
     8  	kDecimalExponentDistance = 8
     9  )
    10  
    11  type cachedPower struct {
    12  	significand      uint64
    13  	binary_exponent  int16
    14  	decimal_exponent int16
    15  }
    16  
    17  var (
    18  	cachedPowers = [...]cachedPower{
    19  		{0xFA8FD5A0081C0288, -1220, -348},
    20  		{0xBAAEE17FA23EBF76, -1193, -340},
    21  		{0x8B16FB203055AC76, -1166, -332},
    22  		{0xCF42894A5DCE35EA, -1140, -324},
    23  		{0x9A6BB0AA55653B2D, -1113, -316},
    24  		{0xE61ACF033D1A45DF, -1087, -308},
    25  		{0xAB70FE17C79AC6CA, -1060, -300},
    26  		{0xFF77B1FCBEBCDC4F, -1034, -292},
    27  		{0xBE5691EF416BD60C, -1007, -284},
    28  		{0x8DD01FAD907FFC3C, -980, -276},
    29  		{0xD3515C2831559A83, -954, -268},
    30  		{0x9D71AC8FADA6C9B5, -927, -260},
    31  		{0xEA9C227723EE8BCB, -901, -252},
    32  		{0xAECC49914078536D, -874, -244},
    33  		{0x823C12795DB6CE57, -847, -236},
    34  		{0xC21094364DFB5637, -821, -228},
    35  		{0x9096EA6F3848984F, -794, -220},
    36  		{0xD77485CB25823AC7, -768, -212},
    37  		{0xA086CFCD97BF97F4, -741, -204},
    38  		{0xEF340A98172AACE5, -715, -196},
    39  		{0xB23867FB2A35B28E, -688, -188},
    40  		{0x84C8D4DFD2C63F3B, -661, -180},
    41  		{0xC5DD44271AD3CDBA, -635, -172},
    42  		{0x936B9FCEBB25C996, -608, -164},
    43  		{0xDBAC6C247D62A584, -582, -156},
    44  		{0xA3AB66580D5FDAF6, -555, -148},
    45  		{0xF3E2F893DEC3F126, -529, -140},
    46  		{0xB5B5ADA8AAFF80B8, -502, -132},
    47  		{0x87625F056C7C4A8B, -475, -124},
    48  		{0xC9BCFF6034C13053, -449, -116},
    49  		{0x964E858C91BA2655, -422, -108},
    50  		{0xDFF9772470297EBD, -396, -100},
    51  		{0xA6DFBD9FB8E5B88F, -369, -92},
    52  		{0xF8A95FCF88747D94, -343, -84},
    53  		{0xB94470938FA89BCF, -316, -76},
    54  		{0x8A08F0F8BF0F156B, -289, -68},
    55  		{0xCDB02555653131B6, -263, -60},
    56  		{0x993FE2C6D07B7FAC, -236, -52},
    57  		{0xE45C10C42A2B3B06, -210, -44},
    58  		{0xAA242499697392D3, -183, -36},
    59  		{0xFD87B5F28300CA0E, -157, -28},
    60  		{0xBCE5086492111AEB, -130, -20},
    61  		{0x8CBCCC096F5088CC, -103, -12},
    62  		{0xD1B71758E219652C, -77, -4},
    63  		{0x9C40000000000000, -50, 4},
    64  		{0xE8D4A51000000000, -24, 12},
    65  		{0xAD78EBC5AC620000, 3, 20},
    66  		{0x813F3978F8940984, 30, 28},
    67  		{0xC097CE7BC90715B3, 56, 36},
    68  		{0x8F7E32CE7BEA5C70, 83, 44},
    69  		{0xD5D238A4ABE98068, 109, 52},
    70  		{0x9F4F2726179A2245, 136, 60},
    71  		{0xED63A231D4C4FB27, 162, 68},
    72  		{0xB0DE65388CC8ADA8, 189, 76},
    73  		{0x83C7088E1AAB65DB, 216, 84},
    74  		{0xC45D1DF942711D9A, 242, 92},
    75  		{0x924D692CA61BE758, 269, 100},
    76  		{0xDA01EE641A708DEA, 295, 108},
    77  		{0xA26DA3999AEF774A, 322, 116},
    78  		{0xF209787BB47D6B85, 348, 124},
    79  		{0xB454E4A179DD1877, 375, 132},
    80  		{0x865B86925B9BC5C2, 402, 140},
    81  		{0xC83553C5C8965D3D, 428, 148},
    82  		{0x952AB45CFA97A0B3, 455, 156},
    83  		{0xDE469FBD99A05FE3, 481, 164},
    84  		{0xA59BC234DB398C25, 508, 172},
    85  		{0xF6C69A72A3989F5C, 534, 180},
    86  		{0xB7DCBF5354E9BECE, 561, 188},
    87  		{0x88FCF317F22241E2, 588, 196},
    88  		{0xCC20CE9BD35C78A5, 614, 204},
    89  		{0x98165AF37B2153DF, 641, 212},
    90  		{0xE2A0B5DC971F303A, 667, 220},
    91  		{0xA8D9D1535CE3B396, 694, 228},
    92  		{0xFB9B7CD9A4A7443C, 720, 236},
    93  		{0xBB764C4CA7A44410, 747, 244},
    94  		{0x8BAB8EEFB6409C1A, 774, 252},
    95  		{0xD01FEF10A657842C, 800, 260},
    96  		{0x9B10A4E5E9913129, 827, 268},
    97  		{0xE7109BFBA19C0C9D, 853, 276},
    98  		{0xAC2820D9623BF429, 880, 284},
    99  		{0x80444B5E7AA7CF85, 907, 292},
   100  		{0xBF21E44003ACDD2D, 933, 300},
   101  		{0x8E679C2F5E44FF8F, 960, 308},
   102  		{0xD433179D9C8CB841, 986, 316},
   103  		{0x9E19DB92B4E31BA9, 1013, 324},
   104  		{0xEB96BF6EBADF77D9, 1039, 332},
   105  		{0xAF87023B9BF0EE6B, 1066, 340},
   106  	}
   107  )
   108  
   109  func getCachedPowerForBinaryExponentRange(min_exponent, max_exponent int) (power diyfp, decimal_exponent int) {
   110  	kQ := diyFpKSignificandSize
   111  	k := int(math.Ceil(float64(min_exponent+kQ-1) * kD_1_LOG2_10))
   112  	index := (kCachedPowersOffset+k-1)/kDecimalExponentDistance + 1
   113  	cached_power := cachedPowers[index]
   114  	_DCHECK(min_exponent <= int(cached_power.binary_exponent))
   115  	_DCHECK(int(cached_power.binary_exponent) <= max_exponent)
   116  	decimal_exponent = int(cached_power.decimal_exponent)
   117  	power = diyfp{f: cached_power.significand, e: int(cached_power.binary_exponent)}
   118  
   119  	return
   120  }