-
Notifications
You must be signed in to change notification settings - Fork 4.5k
/
Decimal.DecCalc.cs
2679 lines (2385 loc) · 104 KB
/
Decimal.DecCalc.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
// Licensed to the .NET Foundation under one or more agreements.
// The .NET Foundation licenses this file to you under the MIT license.
using System.Diagnostics;
using System.Numerics;
using System.Runtime.CompilerServices;
using System.Runtime.InteropServices;
using X86 = System.Runtime.Intrinsics.X86;
namespace System
{
public partial struct Decimal
{
// Low level accessors used by a DecCalc and formatting
internal uint High => _hi32;
internal uint Low => (uint)_lo64;
internal uint Mid => (uint)(_lo64 >> 32);
internal ulong Low64 => _lo64;
private static ref DecCalc AsMutable(ref decimal d) => ref Unsafe.As<decimal, DecCalc>(ref d);
#region APIs need by number formatting.
internal static uint DecDivMod1E9(ref decimal value)
{
return DecCalc.DecDivMod1E9(ref AsMutable(ref value));
}
#endregion
/// <summary>
/// Class that contains all the mathematical calculations for decimal. Most of which have been ported from oleaut32.
/// </summary>
[StructLayout(LayoutKind.Explicit)]
private struct DecCalc
{
// NOTE: Do not change the offsets of these fields. This structure must have the same layout as Decimal.
[FieldOffset(0)]
private uint uflags;
[FieldOffset(4)]
private uint uhi;
#if BIGENDIAN
[FieldOffset(8)]
private uint umid;
[FieldOffset(12)]
private uint ulo;
#else
[FieldOffset(8)]
private uint ulo;
[FieldOffset(12)]
private uint umid;
#endif
/// <summary>
/// The low and mid fields combined
/// </summary>
[FieldOffset(8)]
private ulong ulomid;
private uint High
{
get => uhi;
set => uhi = value;
}
private uint Low
{
get => ulo;
set => ulo = value;
}
private uint Mid
{
get => umid;
set => umid = value;
}
private bool IsNegative => (int)uflags < 0;
private int Scale => (byte)(uflags >> ScaleShift);
private ulong Low64
{
get => ulomid;
set => ulomid = value;
}
private const uint SignMask = 0x80000000;
private const uint ScaleMask = 0x00FF0000;
private const int DEC_SCALE_MAX = 28;
private const uint TenToPowerNine = 1000000000;
private const ulong TenToPowerEighteen = 1000000000000000000;
// The maximum power of 10 that a 32 bit integer can store
private const int MaxInt32Scale = 9;
// The maximum power of 10 that a 64 bit integer can store
private const int MaxInt64Scale = 19;
// Fast access for 10^n where n is 0-9
private static readonly uint[] s_powers10 = new uint[] {
1,
10,
100,
1000,
10000,
100000,
1000000,
10000000,
100000000,
1000000000
};
// Fast access for 10^n where n is 1-19
private static readonly ulong[] s_ulongPowers10 = new ulong[] {
10,
100,
1000,
10000,
100000,
1000000,
10000000,
100000000,
1000000000,
10000000000,
100000000000,
1000000000000,
10000000000000,
100000000000000,
1000000000000000,
10000000000000000,
100000000000000000,
1000000000000000000,
10000000000000000000,
};
private static readonly double[] s_doublePowers10 = new double[] {
1, 1e1, 1e2, 1e3, 1e4, 1e5, 1e6, 1e7, 1e8, 1e9,
1e10, 1e11, 1e12, 1e13, 1e14, 1e15, 1e16, 1e17, 1e18, 1e19,
1e20, 1e21, 1e22, 1e23, 1e24, 1e25, 1e26, 1e27, 1e28, 1e29,
1e30, 1e31, 1e32, 1e33, 1e34, 1e35, 1e36, 1e37, 1e38, 1e39,
1e40, 1e41, 1e42, 1e43, 1e44, 1e45, 1e46, 1e47, 1e48, 1e49,
1e50, 1e51, 1e52, 1e53, 1e54, 1e55, 1e56, 1e57, 1e58, 1e59,
1e60, 1e61, 1e62, 1e63, 1e64, 1e65, 1e66, 1e67, 1e68, 1e69,
1e70, 1e71, 1e72, 1e73, 1e74, 1e75, 1e76, 1e77, 1e78, 1e79,
1e80
};
#region Decimal Math Helpers
private static unsafe uint GetExponent(float f)
{
// Based on pulling out the exp from this single struct layout
// typedef struct {
// ULONG mant:23;
// ULONG exp:8;
// ULONG sign:1;
// } SNGSTRUCT;
return (byte)(*(uint*)&f >> 23);
}
private static unsafe uint GetExponent(double d)
{
// Based on pulling out the exp from this double struct layout
// typedef struct {
// DWORDLONG mant:52;
// DWORDLONG signexp:12;
// } DBLSTRUCT;
return (uint)(*(ulong*)&d >> 52) & 0x7FFu;
}
private static ulong UInt32x32To64(uint a, uint b)
{
return (ulong)a * (ulong)b;
}
private static void UInt64x64To128(ulong a, ulong b, ref DecCalc result)
{
ulong low = UInt32x32To64((uint)a, (uint)b); // lo partial prod
ulong mid = UInt32x32To64((uint)a, (uint)(b >> 32)); // mid 1 partial prod
ulong high = UInt32x32To64((uint)(a >> 32), (uint)(b >> 32));
high += mid >> 32;
low += mid <<= 32;
if (low < mid) // test for carry
high++;
mid = UInt32x32To64((uint)(a >> 32), (uint)b);
high += mid >> 32;
low += mid <<= 32;
if (low < mid) // test for carry
high++;
if (high > uint.MaxValue)
Number.ThrowOverflowException(TypeCode.Decimal);
result.Low64 = low;
result.High = (uint)high;
}
/// <summary>
/// Do full divide, yielding 96-bit result and 32-bit remainder.
/// </summary>
/// <param name="bufNum">96-bit dividend as array of uints, least-sig first</param>
/// <param name="den">32-bit divisor</param>
/// <returns>Returns remainder. Quotient overwrites dividend.</returns>
private static uint Div96By32(ref Buf12 bufNum, uint den)
{
// TODO: https://github.com/dotnet/runtime/issues/5213
ulong tmp, div;
if (bufNum.U2 != 0)
{
tmp = bufNum.High64;
div = tmp / den;
bufNum.High64 = div;
tmp = ((tmp - (uint)div * den) << 32) | bufNum.U0;
if (tmp == 0)
return 0;
uint div32 = (uint)(tmp / den);
bufNum.U0 = div32;
return (uint)tmp - div32 * den;
}
tmp = bufNum.Low64;
if (tmp == 0)
return 0;
div = tmp / den;
bufNum.Low64 = div;
return (uint)(tmp - div * den);
}
[MethodImpl(MethodImplOptions.AggressiveInlining)]
private static bool Div96ByConst(ref ulong high64, ref uint low, uint pow)
{
#if TARGET_64BIT
ulong div64 = high64 / pow;
uint div = (uint)((((high64 - div64 * pow) << 32) + low) / pow);
if (low == div * pow)
{
high64 = div64;
low = div;
return true;
}
#else
// 32-bit RyuJIT doesn't convert 64-bit division by constant into multiplication by reciprocal. Do half-width divisions instead.
Debug.Assert(pow <= ushort.MaxValue);
uint num, mid32, low16, div;
if (high64 <= uint.MaxValue)
{
num = (uint)high64;
mid32 = num / pow;
num = (num - mid32 * pow) << 16;
num += low >> 16;
low16 = num / pow;
num = (num - low16 * pow) << 16;
num += (ushort)low;
div = num / pow;
if (num == div * pow)
{
high64 = mid32;
low = (low16 << 16) + div;
return true;
}
}
else
{
num = (uint)(high64 >> 32);
uint high32 = num / pow;
num = (num - high32 * pow) << 16;
num += (uint)high64 >> 16;
mid32 = num / pow;
num = (num - mid32 * pow) << 16;
num += (ushort)high64;
div = num / pow;
num = (num - div * pow) << 16;
mid32 = div + (mid32 << 16);
num += low >> 16;
low16 = num / pow;
num = (num - low16 * pow) << 16;
num += (ushort)low;
div = num / pow;
if (num == div * pow)
{
high64 = ((ulong)high32 << 32) | mid32;
low = (low16 << 16) + div;
return true;
}
}
#endif
return false;
}
/// <summary>
/// Normalize (unscale) the number by trying to divide out 10^8, 10^4, 10^2, and 10^1.
/// If a division by one of these powers returns a zero remainder, then we keep the quotient.
/// </summary>
[MethodImpl(MethodImplOptions.AggressiveInlining)]
private static void Unscale(ref uint low, ref ulong high64, ref int scale)
{
// Since 10 = 2 * 5, there must be a factor of 2 for every power of 10 we can extract.
// We use this as a quick test on whether to try a given power.
#if TARGET_64BIT
while ((byte)low == 0 && scale >= 8 && Div96ByConst(ref high64, ref low, 100000000))
scale -= 8;
if ((low & 0xF) == 0 && scale >= 4 && Div96ByConst(ref high64, ref low, 10000))
scale -= 4;
#else
while ((low & 0xF) == 0 && scale >= 4 && Div96ByConst(ref high64, ref low, 10000))
scale -= 4;
#endif
if ((low & 3) == 0 && scale >= 2 && Div96ByConst(ref high64, ref low, 100))
scale -= 2;
if ((low & 1) == 0 && scale >= 1 && Div96ByConst(ref high64, ref low, 10))
scale--;
}
/// <summary>
/// Do partial divide, yielding 32-bit result and 64-bit remainder.
/// Divisor must be larger than upper 64 bits of dividend.
/// </summary>
/// <param name="bufNum">96-bit dividend as array of uints, least-sig first</param>
/// <param name="den">64-bit divisor</param>
/// <returns>Returns quotient. Remainder overwrites lower 64-bits of dividend.</returns>
private static uint Div96By64(ref Buf12 bufNum, ulong den)
{
Debug.Assert(den > bufNum.High64);
uint quo;
ulong num;
uint num2 = bufNum.U2;
if (num2 == 0)
{
num = bufNum.Low64;
if (num < den)
// Result is zero. Entire dividend is remainder.
return 0;
// TODO: https://github.com/dotnet/runtime/issues/5213
quo = (uint)(num / den);
num -= quo * den; // remainder
bufNum.Low64 = num;
return quo;
}
uint denHigh32 = (uint)(den >> 32);
if (num2 >= denHigh32)
{
// Divide would overflow. Assume a quotient of 2^32, and set
// up remainder accordingly.
//
num = bufNum.Low64;
num -= den << 32;
quo = 0;
// Remainder went negative. Add divisor back in until it's positive,
// a max of 2 times.
//
do
{
quo--;
num += den;
} while (num >= den);
bufNum.Low64 = num;
return quo;
}
// Hardware divide won't overflow
//
ulong num64 = bufNum.High64;
if (num64 < denHigh32)
// Result is zero. Entire dividend is remainder.
//
return 0;
// TODO: https://github.com/dotnet/runtime/issues/5213
quo = (uint)(num64 / denHigh32);
num = bufNum.U0 | ((num64 - quo * denHigh32) << 32); // remainder
// Compute full remainder, rem = dividend - (quo * divisor).
//
ulong prod = UInt32x32To64(quo, (uint)den); // quo * lo divisor
num -= prod;
if (num > ~prod)
{
// Remainder went negative. Add divisor back in until it's positive,
// a max of 2 times.
//
do
{
quo--;
num += den;
} while (num >= den);
}
bufNum.Low64 = num;
return quo;
}
/// <summary>
/// Do partial divide, yielding 32-bit result and 96-bit remainder.
/// Top divisor uint must be larger than top dividend uint. This is
/// assured in the initial call because the divisor is normalized
/// and the dividend can't be. In subsequent calls, the remainder
/// is multiplied by 10^9 (max), so it can be no more than 1/4 of
/// the divisor which is effectively multiplied by 2^32 (4 * 10^9).
/// </summary>
/// <param name="bufNum">128-bit dividend as array of uints, least-sig first</param>
/// <param name="bufDen">96-bit divisor</param>
/// <returns>Returns quotient. Remainder overwrites lower 96-bits of dividend.</returns>
private static uint Div128By96(ref Buf16 bufNum, ref Buf12 bufDen)
{
Debug.Assert(bufDen.U2 > bufNum.U3);
ulong dividend = bufNum.High64;
uint den = bufDen.U2;
if (dividend < den)
// Result is zero. Entire dividend is remainder.
//
return 0;
// TODO: https://github.com/dotnet/runtime/issues/5213
uint quo = (uint)(dividend / den);
uint remainder = (uint)dividend - quo * den;
// Compute full remainder, rem = dividend - (quo * divisor).
//
ulong prod1 = UInt32x32To64(quo, bufDen.U0); // quo * lo divisor
ulong prod2 = UInt32x32To64(quo, bufDen.U1); // quo * mid divisor
prod2 += prod1 >> 32;
prod1 = (uint)prod1 | (prod2 << 32);
prod2 >>= 32;
ulong num = bufNum.Low64;
num -= prod1;
remainder -= (uint)prod2;
// Propagate carries
//
if (num > ~prod1)
{
remainder--;
if (remainder < ~(uint)prod2)
goto PosRem;
}
else if (remainder <= ~(uint)prod2)
goto PosRem;
{
// Remainder went negative. Add divisor back in until it's positive,
// a max of 2 times.
//
prod1 = bufDen.Low64;
while (true)
{
quo--;
num += prod1;
remainder += den;
if (num < prod1)
{
// Detected carry. Check for carry out of top
// before adding it in.
//
if (remainder++ < den)
break;
}
if (remainder < den)
break; // detected carry
}
}
PosRem:
bufNum.Low64 = num;
bufNum.U2 = remainder;
return quo;
}
/// <summary>
/// Multiply the two numbers. The low 96 bits of the result overwrite
/// the input. The last 32 bits of the product are the return value.
/// </summary>
/// <param name="bufNum">96-bit number as array of uints, least-sig first</param>
/// <param name="power">Scale factor to multiply by</param>
/// <returns>Returns highest 32 bits of product</returns>
private static uint IncreaseScale(ref Buf12 bufNum, uint power)
{
ulong tmp = UInt32x32To64(bufNum.U0, power);
bufNum.U0 = (uint)tmp;
tmp >>= 32;
tmp += UInt32x32To64(bufNum.U1, power);
bufNum.U1 = (uint)tmp;
tmp >>= 32;
tmp += UInt32x32To64(bufNum.U2, power);
bufNum.U2 = (uint)tmp;
return (uint)(tmp >> 32);
}
private static void IncreaseScale64(ref Buf12 bufNum, uint power)
{
ulong tmp = UInt32x32To64(bufNum.U0, power);
bufNum.U0 = (uint)tmp;
tmp >>= 32;
tmp += UInt32x32To64(bufNum.U1, power);
bufNum.High64 = tmp;
}
/// <summary>
/// See if we need to scale the result to fit it in 96 bits.
/// Perform needed scaling. Adjust scale factor accordingly.
/// </summary>
/// <param name="bufRes">Array of uints with value, least-significant first</param>
/// <param name="hiRes">Index of last non-zero value in bufRes</param>
/// <param name="scale">Scale factor for this value, range 0 - 2 * DEC_SCALE_MAX</param>
/// <returns>Returns new scale factor. bufRes updated in place, always 3 uints.</returns>
private static unsafe int ScaleResult(Buf24* bufRes, uint hiRes, int scale)
{
Debug.Assert(hiRes < Buf24.Length);
uint* result = (uint*)bufRes;
// See if we need to scale the result. The combined scale must
// be <= DEC_SCALE_MAX and the upper 96 bits must be zero.
//
// Start by figuring a lower bound on the scaling needed to make
// the upper 96 bits zero. hiRes is the index into result[]
// of the highest non-zero uint.
//
int newScale = 0;
if (hiRes > 2)
{
newScale = (int)hiRes * 32 - 64 - 1;
newScale -= BitOperations.LeadingZeroCount(result[hiRes]);
// Multiply bit position by log10(2) to figure it's power of 10.
// We scale the log by 256. log(2) = .30103, * 256 = 77. Doing this
// with a multiply saves a 96-byte lookup table. The power returned
// is <= the power of the number, so we must add one power of 10
// to make it's integer part zero after dividing by 256.
//
// Note: the result of this multiplication by an approximation of
// log10(2) have been exhaustively checked to verify it gives the
// correct result. (There were only 95 to check...)
//
newScale = ((newScale * 77) >> 8) + 1;
// newScale = min scale factor to make high 96 bits zero, 0 - 29.
// This reduces the scale factor of the result. If it exceeds the
// current scale of the result, we'll overflow.
//
if (newScale > scale)
goto ThrowOverflow;
}
// Make sure we scale by enough to bring the current scale factor
// into valid range.
//
if (newScale < scale - DEC_SCALE_MAX)
newScale = scale - DEC_SCALE_MAX;
if (newScale != 0)
{
// Scale by the power of 10 given by newScale. Note that this is
// NOT guaranteed to bring the number within 96 bits -- it could
// be 1 power of 10 short.
//
scale -= newScale;
uint sticky = 0;
uint quotient, remainder = 0;
while (true)
{
sticky |= remainder; // record remainder as sticky bit
uint power;
// Scaling loop specialized for each power of 10 because division by constant is an order of magnitude faster (especially for 64-bit division that's actually done by 128bit DIV on x64)
switch (newScale)
{
case 1:
power = DivByConst(result, hiRes, out quotient, out remainder, 10);
break;
case 2:
power = DivByConst(result, hiRes, out quotient, out remainder, 100);
break;
case 3:
power = DivByConst(result, hiRes, out quotient, out remainder, 1000);
break;
case 4:
power = DivByConst(result, hiRes, out quotient, out remainder, 10000);
break;
#if TARGET_64BIT
case 5:
power = DivByConst(result, hiRes, out quotient, out remainder, 100000);
break;
case 6:
power = DivByConst(result, hiRes, out quotient, out remainder, 1000000);
break;
case 7:
power = DivByConst(result, hiRes, out quotient, out remainder, 10000000);
break;
case 8:
power = DivByConst(result, hiRes, out quotient, out remainder, 100000000);
break;
default:
power = DivByConst(result, hiRes, out quotient, out remainder, TenToPowerNine);
break;
#else
default:
goto case 4;
#endif
}
result[hiRes] = quotient;
// If first quotient was 0, update hiRes.
//
if (quotient == 0 && hiRes != 0)
hiRes--;
#if TARGET_64BIT
newScale -= MaxInt32Scale;
#else
newScale -= 4;
#endif
if (newScale > 0)
continue; // scale some more
// If we scaled enough, hiRes would be 2 or less. If not,
// divide by 10 more.
//
if (hiRes > 2)
{
if (scale == 0)
goto ThrowOverflow;
newScale = 1;
scale--;
continue; // scale by 10
}
// Round final result. See if remainder >= 1/2 of divisor.
// If remainder == 1/2 divisor, round up if odd or sticky bit set.
//
power >>= 1; // power of 10 always even
if (power <= remainder && (power < remainder || ((result[0] & 1) | sticky) != 0) && ++result[0] == 0)
{
uint cur = 0;
do
{
Debug.Assert(cur + 1 < Buf24.Length);
}
while (++result[++cur] == 0);
if (cur > 2)
{
// The rounding caused us to carry beyond 96 bits.
// Scale by 10 more.
//
if (scale == 0)
goto ThrowOverflow;
hiRes = cur;
sticky = 0; // no sticky bit
remainder = 0; // or remainder
newScale = 1;
scale--;
continue; // scale by 10
}
}
break;
} // while (true)
}
return scale;
ThrowOverflow:
Number.ThrowOverflowException(TypeCode.Decimal);
return 0;
}
[MethodImpl(MethodImplOptions.AggressiveInlining)]
private static unsafe uint DivByConst(uint* result, uint hiRes, out uint quotient, out uint remainder, uint power)
{
uint high = result[hiRes];
remainder = high - (quotient = high / power) * power;
for (uint i = hiRes - 1; (int)i >= 0; i--)
{
#if TARGET_64BIT
ulong num = result[i] + ((ulong)remainder << 32);
remainder = (uint)num - (result[i] = (uint)(num / power)) * power;
#else
// 32-bit RyuJIT doesn't convert 64-bit division by constant into multiplication by reciprocal. Do half-width divisions instead.
Debug.Assert(power <= ushort.MaxValue);
#if BIGENDIAN
const int low16 = 2, high16 = 0;
#else
const int low16 = 0, high16 = 2;
#endif
// byte* is used here because Roslyn doesn't do constant propagation for pointer arithmetic
uint num = *(ushort*)((byte*)result + i * 4 + high16) + (remainder << 16);
uint div = num / power;
remainder = num - div * power;
*(ushort*)((byte*)result + i * 4 + high16) = (ushort)div;
num = *(ushort*)((byte*)result + i * 4 + low16) + (remainder << 16);
div = num / power;
remainder = num - div * power;
*(ushort*)((byte*)result + i * 4 + low16) = (ushort)div;
#endif
}
return power;
}
/// <summary>
/// Adjust the quotient to deal with an overflow.
/// We need to divide by 10, feed in the high bit to undo the overflow and then round as required.
/// </summary>
private static int OverflowUnscale(ref Buf12 bufQuo, int scale, bool sticky)
{
if (--scale < 0)
Number.ThrowOverflowException(TypeCode.Decimal);
Debug.Assert(bufQuo.U2 == 0);
// We have overflown, so load the high bit with a one.
const ulong highbit = 1UL << 32;
bufQuo.U2 = (uint)(highbit / 10);
ulong tmp = ((highbit % 10) << 32) + bufQuo.U1;
uint div = (uint)(tmp / 10);
bufQuo.U1 = div;
tmp = ((tmp - div * 10) << 32) + bufQuo.U0;
div = (uint)(tmp / 10);
bufQuo.U0 = div;
uint remainder = (uint)(tmp - div * 10);
// The remainder is the last digit that does not fit, so we can use it to work out if we need to round up
if (remainder > 5 || remainder == 5 && (sticky || (bufQuo.U0 & 1) != 0))
Add32To96(ref bufQuo, 1);
return scale;
}
/// <summary>
/// Determine the max power of 10, <= 9, that the quotient can be scaled
/// up by and still fit in 96 bits.
/// </summary>
/// <param name="bufQuo">96-bit quotient</param>
/// <param name="scale ">Scale factor of quotient, range -DEC_SCALE_MAX to DEC_SCALE_MAX-1</param>
/// <returns>power of 10 to scale by</returns>
private static int SearchScale(ref Buf12 bufQuo, int scale)
{
const uint OVFL_MAX_9_HI = 4;
const uint OVFL_MAX_8_HI = 42;
const uint OVFL_MAX_7_HI = 429;
const uint OVFL_MAX_6_HI = 4294;
const uint OVFL_MAX_5_HI = 42949;
const uint OVFL_MAX_4_HI = 429496;
const uint OVFL_MAX_3_HI = 4294967;
const uint OVFL_MAX_2_HI = 42949672;
const uint OVFL_MAX_1_HI = 429496729;
const ulong OVFL_MAX_9_MIDLO = 5441186219426131129;
uint resHi = bufQuo.U2;
ulong resMidLo = bufQuo.Low64;
int curScale = 0;
// Quick check to stop us from trying to scale any more.
//
if (resHi > OVFL_MAX_1_HI)
{
goto HaveScale;
}
PowerOvfl[] powerOvfl = PowerOvflValues;
if (scale > DEC_SCALE_MAX - 9)
{
// We can't scale by 10^9 without exceeding the max scale factor.
// See if we can scale to the max. If not, we'll fall into
// standard search for scale factor.
//
curScale = DEC_SCALE_MAX - scale;
if (resHi < powerOvfl[curScale - 1].Hi)
goto HaveScale;
}
else if (resHi < OVFL_MAX_9_HI || resHi == OVFL_MAX_9_HI && resMidLo <= OVFL_MAX_9_MIDLO)
return 9;
// Search for a power to scale by < 9. Do a binary search.
//
if (resHi > OVFL_MAX_5_HI)
{
if (resHi > OVFL_MAX_3_HI)
{
curScale = 2;
if (resHi > OVFL_MAX_2_HI)
curScale--;
}
else
{
curScale = 4;
if (resHi > OVFL_MAX_4_HI)
curScale--;
}
}
else
{
if (resHi > OVFL_MAX_7_HI)
{
curScale = 6;
if (resHi > OVFL_MAX_6_HI)
curScale--;
}
else
{
curScale = 8;
if (resHi > OVFL_MAX_8_HI)
curScale--;
}
}
// In all cases, we already found we could not use the power one larger.
// So if we can use this power, it is the biggest, and we're done. If
// we can't use this power, the one below it is correct for all cases
// unless it's 10^1 -- we might have to go to 10^0 (no scaling).
//
if (resHi == powerOvfl[curScale - 1].Hi && resMidLo > powerOvfl[curScale - 1].MidLo)
curScale--;
HaveScale:
// curScale = largest power of 10 we can scale by without overflow,
// curScale < 9. See if this is enough to make scale factor
// positive if it isn't already.
//
if (curScale + scale < 0)
Number.ThrowOverflowException(TypeCode.Decimal);
return curScale;
}
/// <summary>
/// Add a 32-bit uint to an array of 3 uints representing a 96-bit integer.
/// </summary>
/// <returns>Returns false if there is an overflow</returns>
private static bool Add32To96(ref Buf12 bufNum, uint value)
{
if ((bufNum.Low64 += value) < value)
{
if (++bufNum.U2 == 0)
return false;
}
return true;
}
/// <summary>
/// Adds or subtracts two decimal values.
/// On return, d1 contains the result of the operation and d2 is trashed.
/// </summary>
/// <param name="d1">First decimal to add or subtract.</param>
/// <param name="d2">Second decimal to add or subtract.</param>
/// <param name="sign">True means subtract and false means add.</param>
internal static unsafe void DecAddSub(ref DecCalc d1, ref DecCalc d2, bool sign)
{
ulong low64 = d1.Low64;
uint high = d1.High, flags = d1.uflags, d2flags = d2.uflags;
uint xorflags = d2flags ^ flags;
sign ^= (xorflags & SignMask) != 0;
if ((xorflags & ScaleMask) == 0)
{
// Scale factors are equal, no alignment necessary.
//
goto AlignedAdd;
}
else
{
// Scale factors are not equal. Assume that a larger scale
// factor (more decimal places) is likely to mean that number
// is smaller. Start by guessing that the right operand has
// the larger scale factor. The result will have the larger
// scale factor.
//
uint d1flags = flags;
flags = d2flags & ScaleMask | flags & SignMask; // scale factor of "smaller", but sign of "larger"
int scale = (int)(flags - d1flags) >> ScaleShift;
if (scale < 0)
{
// Guessed scale factor wrong. Swap operands.
//
scale = -scale;
flags = d1flags;
if (sign)
flags ^= SignMask;
low64 = d2.Low64;
high = d2.High;
d2 = d1;
}
uint power;
ulong tmp64, tmpLow;
// d1 will need to be multiplied by 10^scale so
// it will have the same scale as d2. We could be
// extending it to up to 192 bits of precision.
// Scan for zeros in the upper words.
//
if (high == 0)
{
if (low64 <= uint.MaxValue)
{
if ((uint)low64 == 0)
{
// Left arg is zero, return right.
//
uint signFlags = flags & SignMask;
if (sign)
signFlags ^= SignMask;
d1 = d2;
d1.uflags = d2.uflags & ScaleMask | signFlags;
return;
}
do
{
if (scale <= MaxInt32Scale)
{
low64 = UInt32x32To64((uint)low64, s_powers10[scale]);
goto AlignedAdd;
}
scale -= MaxInt32Scale;
low64 = UInt32x32To64((uint)low64, TenToPowerNine);
} while (low64 <= uint.MaxValue);
}
do
{
power = TenToPowerNine;
if (scale < MaxInt32Scale)
power = s_powers10[scale];
tmpLow = UInt32x32To64((uint)low64, power);
tmp64 = UInt32x32To64((uint)(low64 >> 32), power) + (tmpLow >> 32);
low64 = (uint)tmpLow + (tmp64 << 32);
high = (uint)(tmp64 >> 32);
if ((scale -= MaxInt32Scale) <= 0)
goto AlignedAdd;
} while (high == 0);
}
while (true)
{
// Scaling won't make it larger than 4 uints
//
power = TenToPowerNine;
if (scale < MaxInt32Scale)
power = s_powers10[scale];
tmpLow = UInt32x32To64((uint)low64, power);
tmp64 = UInt32x32To64((uint)(low64 >> 32), power) + (tmpLow >> 32);
low64 = (uint)tmpLow + (tmp64 << 32);
tmp64 >>= 32;
tmp64 += UInt32x32To64(high, power);
scale -= MaxInt32Scale;
if (tmp64 > uint.MaxValue)
break;
high = (uint)tmp64;
// Result fits in 96 bits. Use standard aligned add.
if (scale <= 0)
goto AlignedAdd;
}
// Have to scale by a bunch. Move the number to a buffer where it has room to grow as it's scaled.
//
Unsafe.SkipInit(out Buf24 bufNum);
bufNum.Low64 = low64;
bufNum.Mid64 = tmp64;
uint hiProd = 3;
// Scaling loop, up to 10^9 at a time. hiProd stays updated with index of highest non-zero uint.
//
for (; scale > 0; scale -= MaxInt32Scale)
{
power = TenToPowerNine;
if (scale < MaxInt32Scale)
power = s_powers10[scale];
tmp64 = 0;
uint* rgulNum = (uint*)&bufNum;
for (uint cur = 0; ;)
{
Debug.Assert(cur < Buf24.Length);
tmp64 += UInt32x32To64(rgulNum[cur], power);
rgulNum[cur] = (uint)tmp64;