@@ -324,55 +324,55 @@ extension Decimal : Strideable {
324
324
}
325
325
}
326
326
327
- extension Decimal {
328
- // (Used by `_powersOfTen` and `ulp`; note that the representation isn't compact.)
329
- fileprivate init ( _length: UInt32 , _mantissa: ( UInt16 , UInt16 , UInt16 , UInt16 , UInt16 , UInt16 , UInt16 , UInt16 ) ) {
330
- self . init ( _exponent: 0 , _length: _length, _isNegative: 0 , _isCompact: 0 ,
327
+ private extension Decimal {
328
+ // Creates a value with zero exponent.
329
+ // (Used by `_powersOfTenDividingUInt128Max`.)
330
+ init ( _length: UInt32 , _isCompact: UInt32 , _mantissa: ( UInt16 , UInt16 , UInt16 , UInt16 , UInt16 , UInt16 , UInt16 , UInt16 ) ) {
331
+ self . init ( _exponent: 0 , _length: _length, _isNegative: 0 , _isCompact: _isCompact,
331
332
_reserved: 0 , _mantissa: _mantissa)
332
333
}
333
334
}
334
335
335
- private let _powersOfTen = [
336
- /*^00*/ 1 as Decimal ,
337
- /*^01*/ Decimal ( _length: 1 , _mantissa: ( 0x000a , 0 , 0 , 0 , 0 , 0 , 0 , 0 ) ) ,
338
- /*^02*/ Decimal ( _length: 1 , _mantissa: ( 0x0064 , 0 , 0 , 0 , 0 , 0 , 0 , 0 ) ) ,
339
- /*^03*/ Decimal ( _length: 1 , _mantissa: ( 0x03e8 , 0 , 0 , 0 , 0 , 0 , 0 , 0 ) ) ,
340
- /*^04*/ Decimal ( _length: 1 , _mantissa: ( 0x2710 , 0 , 0 , 0 , 0 , 0 , 0 , 0 ) ) ,
341
- /*^05*/ Decimal ( _length: 2 , _mantissa: ( 0x86a0 , 0x0001 , 0 , 0 , 0 , 0 , 0 , 0 ) ) ,
342
- /*^06*/ Decimal ( _length: 2 , _mantissa: ( 0x4240 , 0x000f , 0 , 0 , 0 , 0 , 0 , 0 ) ) ,
343
- /*^07*/ Decimal ( _length: 2 , _mantissa: ( 0x9680 , 0x0098 , 0 , 0 , 0 , 0 , 0 , 0 ) ) ,
344
- /*^08*/ Decimal ( _length: 2 , _mantissa: ( 0xe100 , 0x05f5 , 0 , 0 , 0 , 0 , 0 , 0 ) ) ,
345
- /*^09*/ Decimal ( _length: 2 , _mantissa: ( 0xca00 , 0x3b9a , 0 , 0 , 0 , 0 , 0 , 0 ) ) ,
346
- /*^10*/ Decimal ( _length: 3 , _mantissa: ( 0xe400 , 0x540b , 0x0002 , 0 , 0 , 0 , 0 , 0 ) ) ,
347
- /*^11*/ Decimal ( _length: 3 , _mantissa: ( 0xe800 , 0x4876 , 0x0017 , 0 , 0 , 0 , 0 , 0 ) ) ,
348
- /*^12*/ Decimal ( _length: 3 , _mantissa: ( 0x1000 , 0xd4a5 , 0x00e8 , 0 , 0 , 0 , 0 , 0 ) ) ,
349
- /*^13*/ Decimal ( _length: 3 , _mantissa: ( 0xa000 , 0x4e72 , 0x0918 , 0 , 0 , 0 , 0 , 0 ) ) ,
350
- /*^14*/ Decimal ( _length: 3 , _mantissa: ( 0x4000 , 0x107a , 0x5af3 , 0 , 0 , 0 , 0 , 0 ) ) ,
351
- /*^15*/ Decimal ( _length: 4 , _mantissa: ( 0x8000 , 0xa4c6 , 0x8d7e , 0x0003 , 0 , 0 , 0 , 0 ) ) ,
352
- /*^16*/ Decimal ( _length: 4 , _mantissa: ( 0x0000 , 0x6fc1 , 0x86f2 , 0x0023 , 0 , 0 , 0 , 0 ) ) ,
353
- /*^17*/ Decimal ( _length: 4 , _mantissa: ( 0x0000 , 0x5d8a , 0x4578 , 0x0163 , 0 , 0 , 0 , 0 ) ) ,
354
- /*^18*/ Decimal ( _length: 4 , _mantissa: ( 0x0000 , 0xa764 , 0xb6b3 , 0x0de0 , 0 , 0 , 0 , 0 ) ) ,
355
- /*^19*/ Decimal ( _length: 4 , _mantissa: ( 0x0000 , 0x89e8 , 0x2304 , 0x8ac7 , 0 , 0 , 0 , 0 ) ) ,
356
- /*^20*/ Decimal ( _length: 5 , _mantissa: ( 0x0000 , 0x6310 , 0x5e2d , 0x6bc7 , 0x0005 , 0 , 0 , 0 ) ) ,
357
- /*^21*/ Decimal ( _length: 5 , _mantissa: ( 0x0000 , 0xdea0 , 0xadc5 , 0x35c9 , 0x0036 , 0 , 0 , 0 ) ) ,
358
- /*^22*/ Decimal ( _length: 5 , _mantissa: ( 0x0000 , 0xb240 , 0xc9ba , 0x19e0 , 0x021e , 0 , 0 , 0 ) ) ,
359
- /*^23*/ Decimal ( _length: 5 , _mantissa: ( 0x0000 , 0xf680 , 0xe14a , 0x02c7 , 0x152d , 0 , 0 , 0 ) ) ,
360
- /*^24*/ Decimal ( _length: 5 , _mantissa: ( 0x0000 , 0xa100 , 0xcced , 0x1bce , 0xd3c2 , 0 , 0 , 0 ) ) ,
361
- /*^25*/ Decimal ( _length: 6 , _mantissa: ( 0x0000 , 0x4a00 , 0x0148 , 0x1614 , 0x4595 , 0x0008 , 0 , 0 ) ) ,
362
- /*^26*/ Decimal ( _length: 6 , _mantissa: ( 0x0000 , 0xe400 , 0x0cd2 , 0xdcc8 , 0xb7d2 , 0x0052 , 0 , 0 ) ) ,
363
- /*^27*/ Decimal ( _length: 6 , _mantissa: ( 0x0000 , 0xe800 , 0x803c , 0x9fd0 , 0x2e3c , 0x033b , 0 , 0 ) ) ,
364
- /*^28*/ Decimal ( _length: 6 , _mantissa: ( 0x0000 , 0x1000 , 0x0261 , 0x3e25 , 0xce5e , 0x204f , 0 , 0 ) ) ,
365
- /*^29*/ Decimal ( _length: 7 , _mantissa: ( 0x0000 , 0xa000 , 0x17ca , 0x6d72 , 0x0fae , 0x431e , 0x0001 , 0 ) ) ,
366
- /*^30*/ Decimal ( _length: 7 , _mantissa: ( 0x0000 , 0x4000 , 0xedea , 0x4674 , 0x9cd0 , 0x9f2c , 0x000c , 0 ) ) ,
367
- /*^31*/ Decimal ( _length: 7 , _mantissa: ( 0x0000 , 0x8000 , 0x4b26 , 0xc091 , 0x2022 , 0x37be , 0x007e , 0 ) ) ,
368
- /*^32*/ Decimal ( _length: 7 , _mantissa: ( 0x0000 , 0x0000 , 0xef81 , 0x85ac , 0x415b , 0x2d6d , 0x04ee , 0 ) ) ,
369
- /*^33*/ Decimal ( _length: 7 , _mantissa: ( 0x0000 , 0x0000 , 0x5b0a , 0x38c1 , 0x8d93 , 0xc644 , 0x314d , 0 ) ) ,
370
- /*^34*/ Decimal ( _length: 8 , _mantissa: ( 0x0000 , 0x0000 , 0x8e64 , 0x378d , 0x87c0 , 0xbead , 0xed09 , 0x0001 ) ) ,
371
- /*^35*/ Decimal ( _length: 8 , _mantissa: ( 0x0000 , 0x0000 , 0x8fe8 , 0x2b87 , 0x4d82 , 0x72c7 , 0x4261 , 0x0013 ) ) ,
372
- /*^36*/ Decimal ( _length: 8 , _mantissa: ( 0x0000 , 0x0000 , 0x9f10 , 0xb34b , 0x0715 , 0x7bc9 , 0x97ce , 0x00c0 ) ) ,
373
- /*^37*/ Decimal ( _length: 8 , _mantissa: ( 0x0000 , 0x0000 , 0x36a0 , 0x00f4 , 0x46d9 , 0xd5da , 0xee10 , 0x0785 ) ) ,
374
- /*^38*/ Decimal ( _length: 8 , _mantissa: ( 0x0000 , 0x0000 , 0x2240 , 0x098a , 0xc47a , 0x5a86 , 0x4ca8 , 0x4b3b ) )
375
- /*^39 is on 9 shorts.*/
336
+ private let _powersOfTenDividingUInt128Max = [
337
+ /* 10**00 dividing UInt128.max is deliberately omitted. */
338
+ /* 10**01 */ Decimal ( _length: 8 , _isCompact: 1 , _mantissa: ( 0x9999 , 0x9999 , 0x9999 , 0x9999 , 0x9999 , 0x9999 , 0x9999 , 0x1999 ) ) ,
339
+ /* 10**02 */ Decimal ( _length: 8 , _isCompact: 1 , _mantissa: ( 0xf5c2 , 0x5c28 , 0xc28f , 0x28f5 , 0x8f5c , 0xf5c2 , 0x5c28 , 0x028f ) ) ,
340
+ /* 10**03 */ Decimal ( _length: 8 , _isCompact: 1 , _mantissa: ( 0x1893 , 0x5604 , 0x2d0e , 0x9db2 , 0xa7ef , 0x4bc6 , 0x8937 , 0x0041 ) ) ,
341
+ /* 10**04 */ Decimal ( _length: 8 , _isCompact: 1 , _mantissa: ( 0x0275 , 0x089a , 0x9e1b , 0x295e , 0x10cb , 0xbac7 , 0x8db8 , 0x0006 ) ) ,
342
+ /* 10**05 */ Decimal ( _length: 7 , _isCompact: 1 , _mantissa: ( 0x3372 , 0x80dc , 0x0fcf , 0x8423 , 0x1b47 , 0xac47 , 0xa7c5 , 0 ) ) ,
343
+ /* 10**06 */ Decimal ( _length: 7 , _isCompact: 1 , _mantissa: ( 0x3858 , 0xf349 , 0xb4c7 , 0x8d36 , 0xb5ed , 0xf7a0 , 0x10c6 , 0 ) ) ,
344
+ /* 10**07 */ Decimal ( _length: 7 , _isCompact: 1 , _mantissa: ( 0xec08 , 0x6520 , 0x787a , 0xf485 , 0xabca , 0x7f29 , 0x01ad , 0 ) ) ,
345
+ /* 10**08 */ Decimal ( _length: 7 , _isCompact: 1 , _mantissa: ( 0x4acd , 0x7083 , 0xbf3f , 0x1873 , 0xc461 , 0xf31d , 0x002a , 0 ) ) ,
346
+ /* 10**09 */ Decimal ( _length: 7 , _isCompact: 1 , _mantissa: ( 0x5447 , 0x8b40 , 0x2cb9 , 0xb5a5 , 0xfa09 , 0x4b82 , 0x0004 , 0 ) ) ,
347
+ /* 10**10 */ Decimal ( _length: 6 , _isCompact: 1 , _mantissa: ( 0xa207 , 0x5ab9 , 0xeadf , 0x5ef6 , 0x7f67 , 0x6df3 , 0 , 0 ) ) ,
348
+ /* 10**11 */ Decimal ( _length: 6 , _isCompact: 1 , _mantissa: ( 0xf69a , 0xef78 , 0x4aaf , 0xbcb2 , 0xbff0 , 0x0afe , 0 , 0 ) ) ,
349
+ /* 10**12 */ Decimal ( _length: 6 , _isCompact: 1 , _mantissa: ( 0x7f0f , 0x97f2 , 0xa111 , 0x12de , 0x7998 , 0x0119 , 0 , 0 ) ) ,
350
+ /* 10**13 */ Decimal ( _length: 6 , _isCompact: 0 , _mantissa: ( 0x0cb4 , 0xc265 , 0x7681 , 0x6849 , 0x25c2 , 0x001c , 0 , 0 ) ) ,
351
+ /* 10**14 */ Decimal ( _length: 6 , _isCompact: 1 , _mantissa: ( 0x4e12 , 0x603d , 0x2573 , 0x70d4 , 0xd093 , 0x0002 , 0 , 0 ) ) ,
352
+ /* 10**15 */ Decimal ( _length: 5 , _isCompact: 1 , _mantissa: ( 0x87ce , 0x566c , 0x9d58 , 0xbe7b , 0x480e , 0 , 0 , 0 ) ) ,
353
+ /* 10**16 */ Decimal ( _length: 5 , _isCompact: 1 , _mantissa: ( 0xda61 , 0x6f0a , 0xf622 , 0xaca5 , 0x0734 , 0 , 0 , 0 ) ) ,
354
+ /* 10**17 */ Decimal ( _length: 5 , _isCompact: 1 , _mantissa: ( 0x4909 , 0xa4b4 , 0x3236 , 0x77aa , 0x00b8 , 0 , 0 , 0 ) ) ,
355
+ /* 10**18 */ Decimal ( _length: 5 , _isCompact: 1 , _mantissa: ( 0xa0e7 , 0x43ab , 0xd1d2 , 0x725d , 0x0012 , 0 , 0 , 0 ) ) ,
356
+ /* 10**19 */ Decimal ( _length: 5 , _isCompact: 1 , _mantissa: ( 0xc34a , 0x6d2a , 0x94fb , 0xd83c , 0x0001 , 0 , 0 , 0 ) ) ,
357
+ /* 10**20 */ Decimal ( _length: 4 , _isCompact: 1 , _mantissa: ( 0x46ba , 0x2484 , 0x4219 , 0x2f39 , 0 , 0 , 0 , 0 ) ) ,
358
+ /* 10**21 */ Decimal ( _length: 4 , _isCompact: 1 , _mantissa: ( 0xd3df , 0x83a6 , 0xed02 , 0x04b8 , 0 , 0 , 0 , 0 ) ) ,
359
+ /* 10**22 */ Decimal ( _length: 4 , _isCompact: 1 , _mantissa: ( 0x7b96 , 0x405d , 0xe480 , 0x0078 , 0 , 0 , 0 , 0 ) ) ,
360
+ /* 10**23 */ Decimal ( _length: 4 , _isCompact: 1 , _mantissa: ( 0x5928 , 0xa009 , 0x16d9 , 0x000c , 0 , 0 , 0 , 0 ) ) ,
361
+ /* 10**24 */ Decimal ( _length: 4 , _isCompact: 1 , _mantissa: ( 0x88ea , 0x299a , 0x357c , 0x0001 , 0 , 0 , 0 , 0 ) ) ,
362
+ /* 10**25 */ Decimal ( _length: 3 , _isCompact: 1 , _mantissa: ( 0xda7d , 0xd0f5 , 0x1ef2 , 0 , 0 , 0 , 0 , 0 ) ) ,
363
+ /* 10**26 */ Decimal ( _length: 3 , _isCompact: 1 , _mantissa: ( 0x95d9 , 0x4818 , 0x0318 , 0 , 0 , 0 , 0 , 0 ) ) ,
364
+ /* 10**27 */ Decimal ( _length: 3 , _isCompact: 0 , _mantissa: ( 0xdbc8 , 0x3a68 , 0x004f , 0 , 0 , 0 , 0 , 0 ) ) ,
365
+ /* 10**28 */ Decimal ( _length: 3 , _isCompact: 1 , _mantissa: ( 0xaf94 , 0xec3d , 0x0007 , 0 , 0 , 0 , 0 , 0 ) ) ,
366
+ /* 10**29 */ Decimal ( _length: 2 , _isCompact: 1 , _mantissa: ( 0xf7f5 , 0xcad2 , 0 , 0 , 0 , 0 , 0 , 0 ) ) ,
367
+ /* 10**30 */ Decimal ( _length: 2 , _isCompact: 1 , _mantissa: ( 0x4bfe , 0x1448 , 0 , 0 , 0 , 0 , 0 , 0 ) ) ,
368
+ /* 10**31 */ Decimal ( _length: 2 , _isCompact: 1 , _mantissa: ( 0x3acc , 0x0207 , 0 , 0 , 0 , 0 , 0 , 0 ) ) ,
369
+ /* 10**32 */ Decimal ( _length: 2 , _isCompact: 1 , _mantissa: ( 0xec47 , 0x0033 , 0 , 0 , 0 , 0 , 0 , 0 ) ) ,
370
+ /* 10**33 */ Decimal ( _length: 2 , _isCompact: 1 , _mantissa: ( 0x313a , 0x0005 , 0 , 0 , 0 , 0 , 0 , 0 ) ) ,
371
+ /* 10**34 */ Decimal ( _length: 1 , _isCompact: 1 , _mantissa: ( 0x84ec , 0 , 0 , 0 , 0 , 0 , 0 , 0 ) ) ,
372
+ /* 10**35 */ Decimal ( _length: 1 , _isCompact: 1 , _mantissa: ( 0x0d4a , 0 , 0 , 0 , 0 , 0 , 0 , 0 ) ) ,
373
+ /* 10**36 */ Decimal ( _length: 1 , _isCompact: 0 , _mantissa: ( 0x0154 , 0 , 0 , 0 , 0 , 0 , 0 , 0 ) ) ,
374
+ /* 10**37 */ Decimal ( _length: 1 , _isCompact: 1 , _mantissa: ( 0x0022 , 0 , 0 , 0 , 0 , 0 , 0 , 0 ) ) ,
375
+ /* 10**38 */ Decimal ( _length: 1 , _isCompact: 1 , _mantissa: ( 0x0003 , 0 , 0 , 0 , 0 , 0 , 0 , 0 ) )
376
376
]
377
377
378
378
// The methods in this extension exist to match the protocol requirements of
@@ -607,10 +607,11 @@ extension Decimal {
607
607
if isZero {
608
608
exponent = . min
609
609
} else {
610
- let significand = Decimal ( _length: _length, _mantissa: _mantissa)
611
- let maxPowerOfTen = _powersOfTen. count
612
- let powerOfTen = _powersOfTen. firstIndex { $0 > significand } ?? maxPowerOfTen
613
- exponent = _exponent &- Int32 ( maxPowerOfTen &- powerOfTen)
610
+ let significand_ = significand
611
+ let shift =
612
+ _powersOfTenDividingUInt128Max. firstIndex { significand_ > $0 }
613
+ ?? _powersOfTenDividingUInt128Max. count
614
+ exponent = _exponent &- Int32 ( shift)
614
615
}
615
616
616
617
return Decimal (
0 commit comments