-
Notifications
You must be signed in to change notification settings - Fork 1
/
decimal
1265 lines (1008 loc) · 40.9 KB
/
decimal
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
/* ------------------------------------------------------------------ */
/* decimal header */
/* ------------------------------------------------------------------ */
/* Copyright (c) IBM Corporation, 2006. All rights reserved. */
/* */
/* This software is made available under the terms of the IBM */
/* alphaWorks License Agreement (distributed with this software as */
/* alphaWorks-License.txt). Your use of this software indicates */
/* your acceptance of the terms and conditions of that Agreement. */
/* */
/* Please send comments, suggestions, and corrections to the author: */
/* klarer@ca.ibm.com */
/* Robert Klarer */
/* ------------------------------------------------------------------ */
#ifndef _DECBASE_H
#define _DECBASE_H
#include "impl/decCommon.h"
#include "impl/decIO.h"
/* from <fenv.h> */
#define FE_DEC_DOWNWARD DEC_ROUND_DOWN
#define FE_DEC_TONEAREST DEC_ROUND_HALF_EVEN
#define FE_DEC_TONEARESTFROMZERO DEC_ROUND_HALF_UP
#define FE_DEC_TOWARDZERO DEC_ROUND_HALF_DOWN
#define FE_DEC_UPWARD DEC_ROUND_UP
namespace std {
namespace decimal {
struct decimal32;
struct decimal64;
struct decimal128;
// is_decimal_fp unary type trait:
template <class _T>
struct _Is_decimal_fp_aux {
static const bool value = false;
};
template <>
struct _Is_decimal_fp_aux<decimal32> {
static const bool value = true;
};
template <>
struct _Is_decimal_fp_aux<decimal64> {
static const bool value = true;
};
template <>
struct _Is_decimal_fp_aux<decimal128> {
static const bool value = true;
};
template <class _T>
struct is_decimal_floating_point
: std::tr1::integral_constant<bool, _Is_decimal_fp_aux<_T>::value> {
typedef bool value_type;
typedef std::tr1::integral_constant<bool, _Is_decimal_fp_aux<_T>::value>
type;
operator type() const { return type(); }
};
// promotion traits for binary arithmetic operators:
template <bool _Cond, class _First, class _Second>
struct _IfElse {
typedef _First type;
};
template <class _First, class _Second>
struct _IfElse<false, _First, _Second> {
typedef _Second type;
};
// The primary template will match only if _LHS is an integral type.
template <class _LHS, class _RHS>
struct _PromotionTraits {
typedef _RHS type;
};
template <class _RHS>
struct _PromotionTraits<decimal32, _RHS> {
typedef typename _IfElse<_Is_decimal_fp_aux<_RHS>::value,
_RHS, decimal32>::type type;
};
template <class _RHS>
struct _PromotionTraits<decimal128, _RHS> {
typedef decimal128 type;
};
template <class _RHS>
struct _PromotionTraits<decimal64, _RHS> {
typedef decimal64 type;
};
template <>
struct _PromotionTraits<decimal64, decimal128> {
typedef decimal128 type;
};
// template constraints:
template <typename _C>
struct _Is_int_or_dec {
static const bool value = std::tr1::is_integral<_C>::value ||
_Is_decimal_fp_aux<_C>::value;
};
template <bool _B, typename _U = void>
struct _Enable_if_c {
typedef _U type;
};
template <typename _U>
struct _Enable_if_c<false, _U> {};
template <class _U>
struct _Enable_if_dec :
public _Enable_if_c<_Is_decimal_fp_aux<_U>::value, _U> {};
template <class _C, typename _U = void>
struct _Enable_if_int_or_dec :
public _Enable_if_c<_Is_int_or_dec<_C>::value, _U> {};
template <class _LHS, class _RHS, typename _U>
struct _Enable_if_LHS_is_dec_and_RHS_is_int_or_dec :
public _Enable_if_c<_Is_decimal_fp_aux<_LHS>::value &&
_Is_int_or_dec<_RHS>::value,
_U> {};
template <typename _LHS, class _RHS, typename _U>
struct _Enable_if_LHS_is_int_and_RHS_is_dec :
public _Enable_if_c<std::tr1::is_integral<_LHS>::value &&
_Is_decimal_fp_aux<_RHS>::value,
_U> {};
template <class _LHS, class _RHS>
struct _Arith_LHS_is_dec_and_RHS_is_int_or_dec :
public _Enable_if_LHS_is_dec_and_RHS_is_int_or_dec<_LHS,
_RHS,
typename _PromotionTraits<_LHS, _RHS>::type > {};
template <typename _LHS, class _RHS>
struct _Arith_LHS_is_int_and_RHS_is_dec :
public _Enable_if_LHS_is_int_and_RHS_is_dec<_LHS, _RHS, _RHS> {};
template <typename _LHS, class _RHS>
struct _Assign_LHS_is_int_and_RHS_is_dec :
public _Enable_if_LHS_is_int_and_RHS_is_dec<_LHS, _RHS, _LHS &> {};
template <class _LHS, class _RHS>
struct _Compare_LHS_is_dec_and_RHS_is_int_or_dec :
public _Enable_if_LHS_is_dec_and_RHS_is_int_or_dec<_LHS,
_RHS, bool> {};
template <typename _LHS, class _RHS>
struct _Compare_LHS_is_int_and_RHS_is_dec :
public _Enable_if_LHS_is_int_and_RHS_is_dec<_LHS, _RHS, bool> {};
template <class _U, typename _RET>
struct _Constrain_if_dec :
public _Enable_if_c<_Is_decimal_fp_aux<_U>::value, _RET> {};
// FmtTraits specializations
template <>
class _FmtTraits<decimal32> {
public:
typedef _Context32 _Context;
typedef decimal32 _Fmt;
typedef _DecBase<_FmtTraits<decimal32> > _Base;
typedef _DecBase<_FmtTraits<decimal32> > * _BasePtr;
static void _FromNumber(_BasePtr _Tgt, const _DecNumber & _Src)
{
_Context _Ctxt;
_Src._ToDecimal32(_Tgt, &_Ctxt);
}
static void _FromNumber(_BasePtr _Tgt, const _DecNumber & _Src,
_Context * _Ctxt)
{ _Src._ToDecimal32(_Tgt, _Ctxt); }
static const uint8_t _Zero[];
static const uint8_t _Max[];
static const uint8_t _Min[];
static const uint8_t _Epsilon[];
static const uint8_t _Denormalized_min[];
static const uint8_t _INF[];
static const uint8_t _NAN[];
static const uint8_t _SNAN[];
static const std::size_t _NumBytes = 4UL;
};
template <>
class _FmtTraits<decimal64> {
public:
typedef _Context64 _Context;
typedef decimal64 _Fmt;
typedef _DecBase<_FmtTraits<decimal64> > _Base;
typedef _DecBase<_FmtTraits<decimal64> > * _BasePtr;
static void _FromNumber(_BasePtr _Tgt, const _DecNumber & _Src)
{
_Context _Ctxt;
_Src._ToDecimal64(_Tgt, &_Ctxt);
}
static void _FromNumber(_BasePtr _Tgt, const _DecNumber & _Src,
_Context * _Ctxt)
{ _Src._ToDecimal64(_Tgt, _Ctxt); }
static const uint8_t _Zero[];
static const uint8_t _Max[];
static const uint8_t _Min[];
static const uint8_t _Epsilon[];
static const uint8_t _Denormalized_min[];
static const uint8_t _INF[];
static const uint8_t _NAN[];
static const uint8_t _SNAN[];
static const std::size_t _NumBytes = 8UL;
};
template <>
class _FmtTraits<decimal128> {
public:
typedef _Context128 _Context;
typedef decimal128 _Fmt;
typedef _DecBase<_FmtTraits<decimal128> > _Base;
typedef _DecBase<_FmtTraits<decimal128> > * _BasePtr;
static void _FromNumber(_BasePtr _Tgt, const _DecNumber & _Src)
{
_Context _Ctxt;
_Src._ToDecimal128(_Tgt, &_Ctxt);
}
static void _FromNumber(_BasePtr _Tgt, const _DecNumber & _Src,
_Context * _Ctxt)
{ _Src._ToDecimal128(_Tgt, _Ctxt); }
static const uint8_t _Zero[];
static const uint8_t _Max[];
static const uint8_t _Min[];
static const uint8_t _Epsilon[];
static const uint8_t _Denormalized_min[];
static const uint8_t _INF[];
static const uint8_t _NAN[];
static const uint8_t _SNAN[];
static const std::size_t _NumBytes = 16UL;
};
// base class for decimal types:
template <class _Tr>
class _DecBase {
typedef typename _Tr::_Fmt _Derived;
typedef typename _Tr::_Fmt & _DerivedRef;
typedef typename _Tr::_Fmt * _DerivedPtr;
public:
// constants (for <cdecfloat> and <limits>)
static _Derived _GetMax()
{ return _Derived(_Tr::_Max); }
static _Derived _GetMin()
{ return _Derived(_Tr::_Min); }
static _Derived _GetEpsilon()
{ return _Derived(_Tr::_Epsilon); }
static _Derived _GetDenormalizedMin()
{ return _Derived(_Tr::_Denormalized_min); }
static _Derived _GetNAN()
{ return _Derived(_Tr::_NAN); }
static _Derived _GetSNAN()
{ return _Derived(_Tr::_SNAN); }
static _Derived _GetINF()
{ return _Derived(_Tr::_INF); }
// conversion operators
operator long long() const
{ return _DecNumber(*this)._ToSignedIntegral(); }
// increment, decrement
_DerivedRef operator++();
_Derived operator++(int);
_DerivedRef operator--();
_Derived operator--(int);
// compound assignment operators
template <class _T>
typename _Enable_if_int_or_dec<_T, _DerivedRef>::type
operator+=(_T _R)
{
typedef _DecNumber::_Add _Add;
_DecNumber::_BinaryOpInPlace<_Add, _Tr>(*this, _R, this);
return *static_cast<_DerivedPtr>(this);
}
_Derived & operator+=(long double _R) { _DEC_NO_MIXED_RADIX; }
template <class _T>
typename _Enable_if_int_or_dec<_T, _DerivedRef>::type
operator-=(_T _R)
{
typedef _DecNumber::_Sub _Sub;
_DecNumber::_BinaryOpInPlace<_Sub, _Tr>(*this, _R, this);
return *static_cast<_DerivedPtr>(this);
}
_Derived & operator-=(long double _R) { _DEC_NO_MIXED_RADIX; }
template <class _T>
typename _Enable_if_int_or_dec<_T, _DerivedRef>::type
operator*=(_T _R)
{
typedef _DecNumber::_Mul _Mul;
_DecNumber::_BinaryOpInPlace<_Mul, _Tr>(*this, _R, this);
return *static_cast<_DerivedPtr>(this);
}
_Derived & operator*=(long double _R) { _DEC_NO_MIXED_RADIX; }
template <class _T>
typename _Enable_if_int_or_dec<_T, _DerivedRef>::type
operator/=(_T _R)
{
typedef _DecNumber::_Div _Div;
_DecNumber::_BinaryOpInPlace<_Div, _Tr>(*this, _R, this);
return *static_cast<_DerivedPtr>(this);
}
_Derived & operator/=(long double _R) { _DEC_NO_MIXED_RADIX; }
void _FromCoefficientAndExponent(signed long long _Coeff, int _Exp);
void _FromCoefficientAndExponent(unsigned long long _Coeff, int _Exp);
protected:
uint8_t _Bytes[_Tr::_NumBytes];
void _BitwiseInitialize(const uint8_t * _Arr)
{ memcpy(_Bytes, _Arr, _Tr::_NumBytes); }
void _FromDecimal32 (const decimal32 & _Q);
void _FromDecimal64 (const decimal64 & _Q);
void _FromDecimal128(const decimal128 & _Q);
void _FromLongDouble(long double _Src);
void _FromUnsignedIntegral(unsigned long long _Src);
void _FromSignedIntegral(long long _Src);
void _FromString(const std::string & _Str);
};
// increment, decrement
template <class _Tr> inline
typename _DecBase<_Tr>::_DerivedRef _DecBase<_Tr>::operator++()
{
typedef _DecNumber::_Add _Add;
_DecNumber::_BinaryOpInPlace<_Add, _Tr> (*this, _DecNumber(1), this);
return *this;
}
template <class _Tr> inline
typename _DecBase<_Tr>::_Derived _DecBase<_Tr>::operator++(int)
{
typedef _DecNumber::_Add _Add;
_DecBase<_Tr> _Tmp(*this);
_DecNumber::_BinaryOpInPlace<_Add, _Tr> (*this, _DecNumber(1), this);
return _Tmp;
}
template <class _Tr> inline
typename _DecBase<_Tr>::_DerivedRef _DecBase<_Tr>::operator--()
{
typedef _DecNumber::_Sub _Sub;
_DecNumber::_BinaryOpInPlace<_Sub, _Tr> (*this, _DecNumber(1), this);
return *this;
}
template <class _Tr> inline
typename _DecBase<_Tr>::_Derived _DecBase<_Tr>::operator--(int)
{
typedef _DecNumber::_Sub _Sub;
_DecBase<_Tr> _Tmp(*this);
_DecNumber::_BinaryOpInPlace<_Sub, _Tr> (*this, _DecNumber(1), this);
return _Tmp;
}
// decimal arithmetic type decimal32
class decimal32 : public _DecBase<_FmtTraits<decimal32> > {
friend class _DecBase<_FmtTraits<decimal32> >;
public:
// constructors, assignment, and destructor
decimal32()
{ _BitwiseInitialize(_Tr::_Zero); }
decimal32(const decimal32 & _L)
{ _BitwiseInitialize(_L._Bytes); }
decimal32 & operator=(decimal32 _L)
{
if(&_L != this)
{ _BitwiseInitialize(_L._Bytes); }
return *this;
}
~decimal32() { }
decimal32(const char * _Str)
{ _FromString(std::string(_Str)); }
decimal32(std::string _Str)
{ _FromString(_Str); }
explicit decimal32(float _Q)
{ _FromLongDouble(_Q); }
explicit decimal32(double _Q)
{ _FromLongDouble(_Q); }
explicit decimal32(long double _Q)
{ _FromLongDouble(_Q); }
decimal32(decimal64 _Q);
decimal32(decimal128 _Q);
decimal32(int _I)
{ _FromSignedIntegral(_I); }
decimal32(unsigned int _I)
{ _FromUnsignedIntegral(_I); }
decimal32(long _I)
{ _FromSignedIntegral(_I); }
decimal32(unsigned long _I)
{ _FromUnsignedIntegral(_I); }
decimal32(long long _I)
{ _FromSignedIntegral(_I); }
decimal32(unsigned long long _I)
{ _FromUnsignedIntegral(_I); }
private:
typedef _FmtTraits<decimal32> _Tr;
decimal32(const uint8_t * _Arr)
{ _BitwiseInitialize(_Arr); }
};
inline long double decimal32_to_long_double(decimal32 _D)
{ return _DecNumber(_D)._ToLongDouble(); }
inline long double decimal_to_long_double(decimal32 _D)
{ return decimal32_to_long_double(_D); }
inline decimal32 make_decimal32(long long _Coeff, int _Exp)
{
decimal32 _Ret;
_Ret._FromCoefficientAndExponent(_Coeff, _Exp);
return _Ret;
}
inline decimal32 make_decimal32(unsigned long long _Coeff, int _Exp)
{
decimal32 _Ret;
_Ret._FromCoefficientAndExponent(_Coeff, _Exp);
return _Ret;
}
// input/output
template <class _CharType, class _CharTraits> inline
std::basic_istream<_CharType, _CharTraits> & operator>>(
std::basic_istream<_CharType, _CharTraits> & _Is,
decimal32 & _V)
{
return _Extractor<_CharType, _CharTraits,
_FmtTraits<decimal32> >(_Is, _V);
}
template <class _CharType, class _CharTraits> inline
std::basic_ostream<_CharType, _CharTraits> & operator<<(
std::basic_ostream<_CharType, _CharTraits> & _Os,
decimal32 _V)
{
return _Inserter(_Os, _V);
}
// decimal arithmetic type decimal64
class decimal64 : public _DecBase<_FmtTraits<decimal64> > {
friend class _DecBase<_FmtTraits<decimal64> >;
public:
// constructors, assignment, and destructor
decimal64()
{ _BitwiseInitialize(_Tr::_Zero); }
decimal64(const decimal64 & _L)
{ _BitwiseInitialize(_L._Bytes); }
decimal64 & operator=(decimal64 _L)
{
if(&_L != this)
{ _BitwiseInitialize(_L._Bytes); }
return *this;
}
~decimal64() { }
decimal64(const char * _Str)
{ _FromString(std::string(_Str)); }
decimal64(std::string _Str)
{ _FromString(_Str); }
explicit decimal64(float _Q)
{ _FromLongDouble(_Q); }
explicit decimal64(double _Q)
{ _FromLongDouble(_Q); }
explicit decimal64(long double _Q)
{ _FromLongDouble(_Q); }
decimal64(decimal32 _Q)
{ _FromDecimal32(_Q); }
decimal64(decimal128 _Q);
decimal64(int _I)
{ _FromSignedIntegral(_I); }
decimal64(unsigned int _I)
{ _FromUnsignedIntegral(_I); }
decimal64(long _I)
{ _FromSignedIntegral(_I); }
decimal64(unsigned long _I)
{ _FromUnsignedIntegral(_I); }
decimal64(long long _I)
{ _FromSignedIntegral(_I); }
decimal64(unsigned long long _I)
{ _FromUnsignedIntegral(_I); }
private:
typedef _FmtTraits<decimal64> _Tr;
decimal64(const uint8_t * _Arr)
{ _BitwiseInitialize(_Arr); }
};
inline long double decimal64_to_long_double(decimal64 _D)
{ return _DecNumber(_D)._ToLongDouble(); }
inline long double decimal_to_long_double(decimal64 _D)
{ return decimal64_to_long_double(_D); }
inline decimal64 make_decimal64(long long _Coeff, int _Exp)
{
decimal64 _Ret;
_Ret._FromCoefficientAndExponent(_Coeff, _Exp);
return _Ret;
}
inline decimal64 make_decimal64(unsigned long long _Coeff, int _Exp)
{
decimal64 _Ret;
_Ret._FromCoefficientAndExponent(_Coeff, _Exp);
return _Ret;
}
// input/output
template <class _CharType, class _CharTraits> inline
std::basic_istream<_CharType, _CharTraits> & operator>>(
std::basic_istream<_CharType, _CharTraits> & _Is,
decimal64 & _V)
{
return _Extractor<_CharType,
_CharTraits, _FmtTraits<decimal64> >(_Is, _V);
}
template <class _CharType, class _CharTraits> inline
std::basic_ostream<_CharType, _CharTraits> & operator<<(
std::basic_ostream<_CharType, _CharTraits> & _Os,
decimal64 _V)
{
return _Inserter(_Os, _V);
}
// decimal arithmetic type decimal128
class decimal128 : public _DecBase<_FmtTraits<decimal128> > {
friend class _DecBase<_FmtTraits<decimal128> >;
public:
// constructors, assignment, and destructor
decimal128()
{ _BitwiseInitialize(_Tr::_Zero); }
decimal128(const decimal128 & _L)
{ _BitwiseInitialize(_L._Bytes); }
decimal128 & operator=(decimal128 _L)
{
if(&_L != this)
{ _BitwiseInitialize(_L._Bytes); }
return *this;
}
~decimal128() { }
decimal128(const char * _Str)
{ _FromString(std::string(_Str)); }
decimal128(std::string _Str)
{ _FromString(_Str); }
explicit decimal128(float _Q)
{ _FromLongDouble(_Q); }
explicit decimal128(double _Q)
{ _FromLongDouble(_Q); }
explicit decimal128(long double _Q)
{ _FromLongDouble(_Q); }
decimal128(decimal32 _Q)
{ _FromDecimal32(_Q); }
decimal128(decimal64 _Q)
{ _FromDecimal64(_Q); }
decimal128(int _I)
{ _FromSignedIntegral(_I); }
decimal128(unsigned int _I)
{ _FromUnsignedIntegral(_I); }
decimal128(long _I)
{ _FromSignedIntegral(_I); }
decimal128(unsigned long _I)
{ _FromUnsignedIntegral(_I); }
decimal128(long long _I)
{ _FromSignedIntegral(_I); }
decimal128(unsigned long long _I)
{ _FromUnsignedIntegral(_I); }
private:
typedef _FmtTraits<decimal128> _Tr;
decimal128(const uint8_t * _Arr)
{ _BitwiseInitialize(_Arr); }
};
// out-of-line-constructors
inline decimal32::decimal32(decimal64 _Q)
{ _FromDecimal64(_Q); }
inline decimal32::decimal32(decimal128 _Q)
{ _FromDecimal128(_Q); }
inline decimal64::decimal64(decimal128 _Q)
{ _FromDecimal128(_Q); }
// conversion to and from binary float
inline long double decimal128_to_long_double(decimal128 _D)
{ return _DecNumber(_D)._ToLongDouble(); }
inline long double decimal_to_long_double(decimal128 _D)
{ return decimal128_to_long_double(_D); }
inline decimal128 make_decimal128(long long _Coeff, int _Exp)
{
decimal128 _Ret;
_Ret._FromCoefficientAndExponent(_Coeff, _Exp);
return _Ret;
}
inline decimal128 make_decimal128(unsigned long long _Coeff, int _Exp)
{
decimal128 _Ret;
_Ret._FromCoefficientAndExponent(_Coeff, _Exp);
return _Ret;
}
// input/output
template <class _CharType, class _CharTraits> inline
std::basic_istream<_CharType, _CharTraits> & operator>>(
std::basic_istream<_CharType, _CharTraits> & _Is,
decimal128 & _V)
{
return _Extractor<_CharType, _CharTraits,
_FmtTraits<decimal128> >(_Is, _V);
}
template <class _CharType, class _CharTraits> inline
std::basic_ostream<_CharType, _CharTraits> & operator<<(
std::basic_ostream<_CharType, _CharTraits> & _Os,
decimal128 _V)
{
return _Inserter(_Os, _V);
}
// conversions
template <class _Tr> inline
void _DecBase<_Tr>::_FromDecimal32(const decimal32 & _Q)
{
_DecNumber _D(_Q);
_Tr::_FromNumber(this, _D);
}
template <class _Tr> inline
void _DecBase<_Tr>::_FromDecimal64(const decimal64 & _Q)
{
_DecNumber _D(_Q);
_Tr::_FromNumber(this, _D);
}
template <class _Tr> inline
void _DecBase<_Tr>::_FromDecimal128(const decimal128 & _Q)
{
_DecNumber _D(_Q);
_Tr::_FromNumber(this, _D);
}
template <class _Tr> inline
void _DecBase<_Tr>::_FromLongDouble(long double _Src)
{
_DecNumber _Tmp(_Src);
_Tr::_FromNumber(this, _Tmp);
}
template <class _Tr> inline
void _DecBase<_Tr>::_FromUnsignedIntegral(unsigned long long _Src)
{
_DecNumber _Tmp(_Src);
_Tr::_FromNumber(this, _Tmp);
}
template <class _Tr> inline
void _DecBase<_Tr>::_FromSignedIntegral(long long _Src)
{
_DecNumber _Tmp(_Src);
_Tr::_FromNumber(this, _Tmp);
}
template <class _Tr> inline
void _DecBase<_Tr>::_FromCoefficientAndExponent(signed long long _Coeff,
int _Exp)
{
if (_Coeff < 0)
{
_DecNumber _Tmp(-_Coeff, _Exp, true);
_Tr::_FromNumber(this, _Tmp);
}
else
{
_DecNumber _Tmp(_Coeff, _Exp, false);
_Tr::_FromNumber(this, _Tmp);
}
}
template <class _Tr> inline
void _DecBase<_Tr>::_FromCoefficientAndExponent(unsigned long long _Coeff,
int _Exp)
{
_DecNumber _Tmp(_Coeff, _Exp, false);
_Tr::_FromNumber(this, _Tmp);
}
template <class _Tr> inline
void _DecBase<_Tr>::_FromString(const std::string & _Str)
{
_Derived _Tmp;
std::stringstream _Iss(_Str);
_Iss >> _Tmp;
*this = _Iss.good() ? _Tmp : _GetNAN();
}
// free functions for decimal arithmetic classes:
template<class _RHS> inline
typename _Enable_if_dec<_RHS>::type operator+(_RHS _R)
{ return _DecNumber::_UnaryOp<_DecNumber::_Plus,
_FmtTraits<_RHS> >(_R); }
template<class _RHS> inline
typename _Enable_if_dec<_RHS>::type operator-(_RHS _R)
{ return _DecNumber::_UnaryOp<_DecNumber::_Minus,
_FmtTraits<_RHS> >(_R); }
template<class _LHS, class _RHS> inline
typename _Arith_LHS_is_int_and_RHS_is_dec<_LHS, _RHS>::type
operator+(_LHS _L, _RHS _R)
{
typedef _FmtTraits<_RHS> _Tr;
return _DecNumber::_BinaryOp<_DecNumber::_Add, _Tr>(_L, _R);
}
template<class _LHS, class _RHS> inline
typename _Arith_LHS_is_dec_and_RHS_is_int_or_dec<_LHS, _RHS>::type
operator+(_LHS _L, _RHS _R)
{
typedef _FmtTraits<typename _PromotionTraits<_LHS, _RHS>::type> _Tr;
return _DecNumber::_BinaryOp<_DecNumber::_Add, _Tr>(_L, _R);
}
template<class _RHS> inline
typename _Constrain_if_dec<_RHS, _RHS>::type operator+(float, _RHS)
{ _DEC_NO_MIXED_RADIX; }
template<class _RHS> inline
typename _Constrain_if_dec<_RHS, _RHS>::type operator+(double, _RHS)
{ _DEC_NO_MIXED_RADIX; }
template<class _RHS> inline
typename _Constrain_if_dec<_RHS, _RHS>::type operator+(long double, _RHS)
{ _DEC_NO_MIXED_RADIX; }
template<class _LHS> inline
typename _Constrain_if_dec<_LHS, _LHS>::type operator+(_LHS, float)
{ _DEC_NO_MIXED_RADIX; }
template<class _LHS> inline
typename _Constrain_if_dec<_LHS, _LHS>::type operator+(_LHS, double)
{ _DEC_NO_MIXED_RADIX; }
template<class _LHS> inline
typename _Constrain_if_dec<_LHS, _LHS>::type operator+(_LHS, long double)
{ _DEC_NO_MIXED_RADIX; }
template<class _LHS, class _RHS> inline
typename _Arith_LHS_is_int_and_RHS_is_dec<_LHS, _RHS>::type
operator-(_LHS _L, _RHS _R)
{
typedef _FmtTraits<_RHS> _Tr;
return _DecNumber::_BinaryOp<_DecNumber::_Sub, _Tr>(_L, _R);
}
template<class _LHS, class _RHS> inline
typename _Arith_LHS_is_dec_and_RHS_is_int_or_dec<_LHS, _RHS>::type
operator-(_LHS _L, _RHS _R)
{
typedef _FmtTraits<typename _PromotionTraits<_LHS, _RHS>::type> _Tr;
return _DecNumber::_BinaryOp<_DecNumber::_Sub, _Tr>(_L, _R);
}
template<class _RHS> inline
typename _Constrain_if_dec<_RHS, _RHS>::type operator-(float, _RHS)
{ _DEC_NO_MIXED_RADIX; }
template<class _RHS> inline
typename _Constrain_if_dec<_RHS, _RHS>::type operator-(double, _RHS)
{ _DEC_NO_MIXED_RADIX; }
template<class _RHS> inline
typename _Constrain_if_dec<_RHS, _RHS>::type operator-(long double, _RHS)
{ _DEC_NO_MIXED_RADIX; }
template<class _LHS> inline
typename _Constrain_if_dec<_LHS, _LHS>::type operator-(_LHS, float)
{ _DEC_NO_MIXED_RADIX; }
template<class _LHS> inline
typename _Constrain_if_dec<_LHS, _LHS>::type operator-(_LHS, double)
{ _DEC_NO_MIXED_RADIX; }
template<class _LHS> inline
typename _Constrain_if_dec<_LHS, _LHS>::type operator-(_LHS, long double)
{ _DEC_NO_MIXED_RADIX; }
template<class _LHS, class _RHS> inline
typename _Arith_LHS_is_int_and_RHS_is_dec<_LHS, _RHS>::type
operator*(_LHS _L, _RHS _R)
{
typedef _FmtTraits<_RHS> _Tr;
return _DecNumber::_BinaryOp<_DecNumber::_Mul, _Tr>(_L, _R);
}
template<class _LHS, class _RHS> inline
typename _Arith_LHS_is_dec_and_RHS_is_int_or_dec<_LHS, _RHS>::type
operator*(_LHS _L, _RHS _R)
{
typedef _FmtTraits<typename _PromotionTraits<_LHS, _RHS>::type> _Tr;
return _DecNumber::_BinaryOp<_DecNumber::_Mul, _Tr>(_L, _R);
}
template<class _RHS> inline
typename _Constrain_if_dec<_RHS, _RHS>::type operator*(float, _RHS)
{ _DEC_NO_MIXED_RADIX; }
template<class _RHS> inline
typename _Constrain_if_dec<_RHS, _RHS>::type operator*(double, _RHS)
{ _DEC_NO_MIXED_RADIX; }
template<class _RHS> inline
typename _Constrain_if_dec<_RHS, _RHS>::type operator*(long double, _RHS)
{ _DEC_NO_MIXED_RADIX; }
template<class _LHS> inline
typename _Constrain_if_dec<_LHS, _LHS>::type operator*(_LHS, float)
{ _DEC_NO_MIXED_RADIX; }
template<class _LHS> inline
typename _Constrain_if_dec<_LHS, _LHS>::type operator*(_LHS, double)
{ _DEC_NO_MIXED_RADIX; }
template<class _LHS> inline
typename _Constrain_if_dec<_LHS, _LHS>::type operator*(_LHS, long double)
{ _DEC_NO_MIXED_RADIX; }
template<class _LHS, class _RHS> inline
typename _Arith_LHS_is_int_and_RHS_is_dec<_LHS, _RHS>::type
operator/(_LHS _L, _RHS _R)
{
typedef _FmtTraits<_RHS> _Tr;
return _DecNumber::_BinaryOp<_DecNumber::_Div, _Tr>(_L, _R);
}
template<class _LHS, class _RHS> inline
typename _Arith_LHS_is_dec_and_RHS_is_int_or_dec<_LHS, _RHS>::type
operator/(_LHS _L, _RHS _R)
{
typedef _FmtTraits<typename _PromotionTraits<_LHS, _RHS>::type> _Tr;
return _DecNumber::_BinaryOp<_DecNumber::_Div, _Tr>(_L, _R);
}
template<class _RHS> inline
typename _Constrain_if_dec<_RHS, _RHS>::type operator/(float, _RHS)
{ _DEC_NO_MIXED_RADIX; }
template<class _RHS> inline
typename _Constrain_if_dec<_RHS, _RHS>::type operator/(double, _RHS)
{ _DEC_NO_MIXED_RADIX; }
template<class _RHS> inline
typename _Constrain_if_dec<_RHS, _RHS>::type operator/(long double, _RHS)
{ _DEC_NO_MIXED_RADIX; }
template<class _LHS> inline
typename _Constrain_if_dec<_LHS, _LHS>::type operator/(_LHS, float)
{ _DEC_NO_MIXED_RADIX; }
template<class _LHS> inline
typename _Constrain_if_dec<_LHS, _LHS>::type operator/(_LHS, double)
{ _DEC_NO_MIXED_RADIX; }
template<class _LHS> inline
typename _Constrain_if_dec<_LHS, _LHS>::type operator/(_LHS, long double)
{ _DEC_NO_MIXED_RADIX; }
template<class _LHS, class _RHS> inline
typename _Compare_LHS_is_int_and_RHS_is_dec<_LHS, _RHS>::type
operator==(_LHS _L, _RHS _R)
{ return _DecNumber::_Compare<_DecNumber::_Eq>(_L, _R); }
template<class _LHS, class _RHS> inline
typename _Compare_LHS_is_dec_and_RHS_is_int_or_dec<_LHS, _RHS>::type
operator==(_LHS _L, _RHS _R)
{ return _DecNumber::_Compare<_DecNumber::_Eq>(_L, _R); }
template<class _RHS> inline
typename _Constrain_if_dec<_RHS, bool>::type operator==(float, _RHS)
{ _DEC_NO_MIXED_RADIX; }
template<class _RHS> inline
typename _Constrain_if_dec<_RHS, bool>::type operator==(double, _RHS)
{ _DEC_NO_MIXED_RADIX; }
template<class _RHS> inline
typename _Constrain_if_dec<_RHS, bool>::type operator==(long double, _RHS)
{ _DEC_NO_MIXED_RADIX; }
template<class _LHS> inline
typename _Constrain_if_dec<_LHS, bool>::type operator==(_LHS, float)
{ _DEC_NO_MIXED_RADIX; }
template<class _LHS> inline
typename _Constrain_if_dec<_LHS, bool>::type operator==(_LHS, double)
{ _DEC_NO_MIXED_RADIX; }
template<class _LHS> inline
typename _Constrain_if_dec<_LHS, bool>::type operator==(_LHS, long double)
{ _DEC_NO_MIXED_RADIX; }
template<class _LHS, class _RHS> inline
typename _Compare_LHS_is_int_and_RHS_is_dec<_LHS, _RHS>::type
operator!=(_LHS _L, _RHS _R)
{ return _DecNumber::_Compare<_DecNumber::_Ne>(_L, _R); }
template<class _LHS, class _RHS> inline
typename _Compare_LHS_is_dec_and_RHS_is_int_or_dec<_LHS, _RHS>::type