2009-10-24 Paul Thomas <pault@gcc.gnu.org>
[official-gcc.git] / libstdc++-v3 / include / decimal / decimal.h
blob002f6ca9b5207c8660efb3c598672ee9b0f9157b
1 // decimal classes -*- C++ -*-
3 // Copyright (C) 2009 Free Software Foundation, Inc.
5 // This file is part of the GNU ISO C++ Library. This library is free
6 // software; you can redistribute it and/or modify it under the
7 // terms of the GNU General Public License as published by the
8 // Free Software Foundation; either version 3, or (at your option)
9 // any later version.
11 // This library is distributed in the hope that it will be useful,
12 // but WITHOUT ANY WARRANTY; without even the implied warranty of
13 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 // GNU General Public License for more details.
16 // Under Section 7 of GPL version 3, you are granted additional
17 // permissions described in the GCC Runtime Library Exception, version
18 // 3.1, as published by the Free Software Foundation.
20 // You should have received a copy of the GNU General Public License and
21 // a copy of the GCC Runtime Library Exception along with this program;
22 // see the files COPYING3 and COPYING.RUNTIME respectively. If not, see
23 // <http://www.gnu.org/licenses/>.
25 /** @file bits/decimal.h
26 * This is an internal header file, included by other library headers.
27 * You should not attempt to use it directly.
30 // ISO/IEC TR 24733
31 // Written by Janis Johnson <janis187@us.ibm.com>
33 #ifndef _GLIBCXX_DECIMAL_IMPL
34 #define _GLIBCXX_DECIMAL_IMPL 1
36 #pragma GCC system_header
38 namespace std
40 namespace decimal
42 // ISO/IEC TR 24733 3.2.[234].1 Construct/copy/destroy.
44 inline decimal32::decimal32(decimal64 r) : __val(r.__getval()) {}
45 inline decimal32::decimal32(decimal128 r) : __val(r.__getval()) {}
46 inline decimal64::decimal64(decimal32 r) : __val(r.__getval()) {}
47 inline decimal64::decimal64(decimal128 r) : __val(r.__getval()) {}
48 inline decimal128::decimal128(decimal32 r) : __val(r.__getval()) {}
49 inline decimal128::decimal128(decimal64 r) : __val(r.__getval()) {}
51 // ISO/IEC TR 24733 3.2.[234].6 Compound assignment.
53 #define _DEFINE_DECIMAL_COMPOUND_ASSIGNMENT_DEC(_Op1, _Op2, _T1, _T2) \
54 inline _T1& _T1::operator _Op1(_T2 __rhs) \
55 { \
56 __setval(__getval() _Op2 __rhs.__getval()); \
57 return *this; \
60 #define _DEFINE_DECIMAL_COMPOUND_ASSIGNMENT_INT(_Op1, _Op2, _T1, _T2) \
61 inline _T1& _T1::operator _Op1(_T2 __rhs) \
62 { \
63 __setval(__getval() _Op2 __rhs); \
64 return *this; \
67 #define _DEFINE_DECIMAL_COMPOUND_ASSIGNMENTS(_O1, _O2, _T1) \
68 _DEFINE_DECIMAL_COMPOUND_ASSIGNMENT_DEC(_O1,_O2,_T1, decimal32) \
69 _DEFINE_DECIMAL_COMPOUND_ASSIGNMENT_DEC(_O1,_O2,_T1, decimal64) \
70 _DEFINE_DECIMAL_COMPOUND_ASSIGNMENT_DEC(_O1,_O2,_T1, decimal128) \
71 _DEFINE_DECIMAL_COMPOUND_ASSIGNMENT_INT(_O1,_O2,_T1, int) \
72 _DEFINE_DECIMAL_COMPOUND_ASSIGNMENT_INT(_O1,_O2,_T1, unsigned int) \
73 _DEFINE_DECIMAL_COMPOUND_ASSIGNMENT_INT(_O1,_O2,_T1, long) \
74 _DEFINE_DECIMAL_COMPOUND_ASSIGNMENT_INT(_O1,_O2,_T1, unsigned long) \
75 _DEFINE_DECIMAL_COMPOUND_ASSIGNMENT_INT(_O1,_O2,_T1, long long) \
76 _DEFINE_DECIMAL_COMPOUND_ASSIGNMENT_INT(_O1,_O2,_T1, unsigned long long)
78 _DEFINE_DECIMAL_COMPOUND_ASSIGNMENTS(+=, +, decimal32)
79 _DEFINE_DECIMAL_COMPOUND_ASSIGNMENTS(-=, -, decimal32)
80 _DEFINE_DECIMAL_COMPOUND_ASSIGNMENTS(*=, *, decimal32)
81 _DEFINE_DECIMAL_COMPOUND_ASSIGNMENTS(/=, /, decimal32)
83 _DEFINE_DECIMAL_COMPOUND_ASSIGNMENTS(+=, +, decimal64)
84 _DEFINE_DECIMAL_COMPOUND_ASSIGNMENTS(-=, -, decimal64)
85 _DEFINE_DECIMAL_COMPOUND_ASSIGNMENTS(*=, *, decimal64)
86 _DEFINE_DECIMAL_COMPOUND_ASSIGNMENTS(/=, /, decimal64)
88 _DEFINE_DECIMAL_COMPOUND_ASSIGNMENTS(+=, +, decimal128)
89 _DEFINE_DECIMAL_COMPOUND_ASSIGNMENTS(-=, -, decimal128)
90 _DEFINE_DECIMAL_COMPOUND_ASSIGNMENTS(*=, *, decimal128)
91 _DEFINE_DECIMAL_COMPOUND_ASSIGNMENTS(/=, /, decimal128)
93 #undef _DEFINE_DECIMAL_COMPOUND_ASSIGNMENT_DEC
94 #undef _DEFINE_DECIMAL_COMPOUND_ASSIGNMENT_INT
95 #undef _DEFINE_DECIMAL_COMPOUND_ASSIGNMENTS
97 // Extension: Conversion to integral type.
99 inline long long decimal32_to_long_long(decimal32 __d)
101 return (long long)__d.__getval();
104 inline long long decimal64_to_long_long(decimal64 __d)
106 return (long long)__d.__getval();
109 inline long long decimal128_to_long_long(decimal128 __d)
111 return (long long)__d.__getval();
114 inline long long decimal_to_long_long(decimal32 __d)
116 return (long long)__d.__getval();
119 inline long long decimal_to_long_long(decimal64 __d)
121 return (long long)__d.__getval();
124 inline long long decimal_to_long_long(decimal128 __d)
126 return (long long)__d.__getval();
129 // ISO/IEC TR 24733 3.2.5 Initialization from coefficient and exponent.
131 static decimal32 make_decimal32(long long __coeff, int __exponent)
133 decimal32 __decexp = 1, __multiplier;
135 if (__exponent < 0)
137 __multiplier = 1.E-1DF;
138 __exponent = -__exponent;
140 else
141 __multiplier = 1.E1DF;
143 for (int i = 0; i < __exponent; i++)
144 __decexp *= __multiplier;
146 return __coeff * __decexp;
149 static decimal32 make_decimal32(unsigned long long __coeff, int __exponent)
151 decimal32 __decexp = 1, __multiplier;
153 if (__exponent < 0)
155 __multiplier = 1.E-1DF;
156 __exponent = -__exponent;
158 else
159 __multiplier = 1.E1DF;
161 for (int i = 0; i < __exponent; i++)
162 __decexp *= __multiplier;
164 return __coeff * __decexp;
167 static decimal64 make_decimal64(long long __coeff, int __exponent)
169 decimal64 __decexp = 1, __multiplier;
171 if (__exponent < 0)
173 __multiplier = 1.E-1DD;
174 __exponent = -__exponent;
176 else
177 __multiplier = 1.E1DD;
179 for (int i = 0; i < __exponent; i++)
180 __decexp *= __multiplier;
182 return __coeff * __decexp;
185 static decimal64 make_decimal64(unsigned long long __coeff, int __exponent)
187 decimal64 __decexp = 1, __multiplier;
189 if (__exponent < 0)
191 __multiplier = 1.E-1DD;
192 __exponent = -__exponent;
194 else
195 __multiplier = 1.E1DD;
197 for (int i = 0; i < __exponent; i++)
198 __decexp *= __multiplier;
200 return __coeff * __decexp;
203 static decimal128 make_decimal128(long long __coeff, int __exponent)
205 decimal128 __decexp = 1, __multiplier;
207 if (__exponent < 0)
209 __multiplier = 1.E-1DL;
210 __exponent = -__exponent;
212 else
213 __multiplier = 1.E1DL;
215 for (int i = 0; i < __exponent; i++)
216 __decexp *= __multiplier;
218 return __coeff * __decexp;
221 static decimal128 make_decimal128(unsigned long long __coeff, int __exponent)
223 decimal128 __decexp = 1, __multiplier;
225 if (__exponent < 0)
227 __multiplier = 1.E-1DL;
228 __exponent = -__exponent;
230 else
231 __multiplier = 1.E1DL;
233 for (int i = 0; i < __exponent; i++)
234 __decexp *= __multiplier;
236 return __coeff * __decexp;
239 // ISO/IEC TR 24733 3.2.6 Conversion to generic floating-point type.
241 inline float decimal32_to_float(decimal32 __d)
243 return (float)__d.__getval();
246 inline float decimal64_to_float(decimal64 __d)
248 return (float)__d.__getval();
251 inline float decimal128_to_float(decimal128 __d)
253 return (float)__d.__getval();
256 inline float decimal_to_float(decimal32 __d)
258 return (float)__d.__getval();
261 inline float decimal_to_float(decimal64 __d)
263 return (float)__d.__getval();
266 inline float decimal_to_float(decimal128 __d)
268 return (float)__d.__getval();
271 inline double decimal32_to_double(decimal32 __d)
273 return (double)__d.__getval();
276 inline double decimal64_to_double(decimal64 __d)
278 return (double)__d.__getval();
281 inline double decimal128_to_double(decimal128 __d)
283 return (double)__d.__getval();
286 inline double decimal_to_double(decimal32 __d)
288 return (double)__d.__getval();
291 inline double decimal_to_double(decimal64 __d)
293 return (double)__d.__getval();
296 inline double decimal_to_double(decimal128 __d)
298 return (double)__d.__getval();
301 inline long double decimal32_to_long_double(decimal32 __d)
303 return (long double)__d.__getval();
306 inline long double decimal64_to_long_double(decimal64 __d)
308 return (long double)__d.__getval();
311 inline long double decimal128_to_long_double(decimal128 __d)
313 return (long double)__d.__getval();
316 inline long double decimal_to_long_double(decimal32 __d)
318 return (long double)__d.__getval();
321 inline long double decimal_to_long_double(decimal64 __d)
323 return (long double)__d.__getval();
326 inline long double decimal_to_long_double(decimal128 __d)
328 return (long double)__d.__getval();
331 // ISO/IEC TR 24733 3.2.7 Unary arithmetic operators.
333 #define _DEFINE_DECIMAL_UNARY_OP(_Op, _T) \
334 inline _T operator _Op(_T __rhs) \
336 _T __tmp; \
337 __tmp.__setval(0 _Op __rhs.__getval()); \
338 return __tmp; \
341 _DEFINE_DECIMAL_UNARY_OP(+, decimal32)
342 _DEFINE_DECIMAL_UNARY_OP(+, decimal64)
343 _DEFINE_DECIMAL_UNARY_OP(+, decimal128)
344 _DEFINE_DECIMAL_UNARY_OP(-, decimal32)
345 _DEFINE_DECIMAL_UNARY_OP(-, decimal64)
346 _DEFINE_DECIMAL_UNARY_OP(-, decimal128)
348 #undef _DEFINE_DECIMAL_UNARY_OP
350 // ISO/IEC TR 24733 3.2.8 Binary arithmetic operators.
352 #define _DEFINE_DECIMAL_BINARY_OP_WITH_DEC(_Op, _T1, _T2, _T3) \
353 inline _T1 operator _Op(_T2 lhs, _T3 __rhs) \
355 _T1 retval; \
356 retval.__setval(lhs.__getval() _Op __rhs.__getval()); \
357 return retval; \
360 #define _DEFINE_DECIMAL_BINARY_OP_BOTH(_Op, _T1, _T2, _T3) \
361 inline _T1 operator _Op(_T2 lhs, _T3 __rhs) \
363 _T1 retval; \
364 retval.__setval(lhs.__getval() _Op __rhs.__getval()); \
365 return retval; \
368 #define _DEFINE_DECIMAL_BINARY_OP_LHS(_Op, _T1, _T2) \
369 inline _T1 operator _Op(_T1 lhs, _T2 __rhs) \
371 _T1 retval; \
372 retval.__setval(lhs.__getval() _Op __rhs); \
373 return retval; \
376 #define _DEFINE_DECIMAL_BINARY_OP_RHS(_Op, _T1, _T2) \
377 inline _T1 operator _Op(_T2 lhs, _T1 __rhs) \
379 _T1 retval; \
380 retval.__setval(lhs _Op __rhs.__getval()); \
381 return retval; \
384 #define _DEFINE_DECIMAL_BINARY_OP_WITH_INT(_Op,_Tp) \
385 _DEFINE_DECIMAL_BINARY_OP_LHS(_Op,_Tp, int); \
386 _DEFINE_DECIMAL_BINARY_OP_LHS(_Op,_Tp, unsigned int); \
387 _DEFINE_DECIMAL_BINARY_OP_LHS(_Op,_Tp, long); \
388 _DEFINE_DECIMAL_BINARY_OP_LHS(_Op,_Tp, unsigned long); \
389 _DEFINE_DECIMAL_BINARY_OP_LHS(_Op,_Tp, long long); \
390 _DEFINE_DECIMAL_BINARY_OP_LHS(_Op,_Tp, unsigned long long); \
391 _DEFINE_DECIMAL_BINARY_OP_RHS(_Op,_Tp, int); \
392 _DEFINE_DECIMAL_BINARY_OP_RHS(_Op,_Tp, unsigned int); \
393 _DEFINE_DECIMAL_BINARY_OP_RHS(_Op,_Tp, long); \
394 _DEFINE_DECIMAL_BINARY_OP_RHS(_Op,_Tp, unsigned long); \
395 _DEFINE_DECIMAL_BINARY_OP_RHS(_Op,_Tp, long long); \
396 _DEFINE_DECIMAL_BINARY_OP_RHS(_Op,_Tp, unsigned long long); \
398 _DEFINE_DECIMAL_BINARY_OP_WITH_DEC(+, decimal32, decimal32, decimal32)
399 _DEFINE_DECIMAL_BINARY_OP_WITH_INT(+, decimal32)
400 _DEFINE_DECIMAL_BINARY_OP_WITH_DEC(+, decimal64, decimal32, decimal64)
401 _DEFINE_DECIMAL_BINARY_OP_WITH_DEC(+, decimal64, decimal64, decimal32)
402 _DEFINE_DECIMAL_BINARY_OP_WITH_DEC(+, decimal64, decimal64, decimal64)
403 _DEFINE_DECIMAL_BINARY_OP_WITH_INT(+, decimal64)
404 _DEFINE_DECIMAL_BINARY_OP_WITH_DEC(+, decimal128, decimal32, decimal128)
405 _DEFINE_DECIMAL_BINARY_OP_WITH_DEC(+, decimal128, decimal64, decimal128)
406 _DEFINE_DECIMAL_BINARY_OP_WITH_DEC(+, decimal128, decimal128, decimal32)
407 _DEFINE_DECIMAL_BINARY_OP_WITH_DEC(+, decimal128, decimal128, decimal64)
408 _DEFINE_DECIMAL_BINARY_OP_WITH_DEC(+, decimal128, decimal128, decimal128)
409 _DEFINE_DECIMAL_BINARY_OP_WITH_INT(+, decimal128)
411 _DEFINE_DECIMAL_BINARY_OP_WITH_DEC(-, decimal32, decimal32, decimal32)
412 _DEFINE_DECIMAL_BINARY_OP_WITH_INT(-, decimal32)
413 _DEFINE_DECIMAL_BINARY_OP_WITH_DEC(-, decimal64, decimal32, decimal64)
414 _DEFINE_DECIMAL_BINARY_OP_WITH_DEC(-, decimal64, decimal64, decimal32)
415 _DEFINE_DECIMAL_BINARY_OP_WITH_DEC(-, decimal64, decimal64, decimal64)
416 _DEFINE_DECIMAL_BINARY_OP_WITH_INT(-, decimal64)
417 _DEFINE_DECIMAL_BINARY_OP_WITH_DEC(-, decimal128, decimal32, decimal128)
418 _DEFINE_DECIMAL_BINARY_OP_WITH_DEC(-, decimal128, decimal64, decimal128)
419 _DEFINE_DECIMAL_BINARY_OP_WITH_DEC(-, decimal128, decimal128, decimal32)
420 _DEFINE_DECIMAL_BINARY_OP_WITH_DEC(-, decimal128, decimal128, decimal64)
421 _DEFINE_DECIMAL_BINARY_OP_WITH_DEC(-, decimal128, decimal128, decimal128)
422 _DEFINE_DECIMAL_BINARY_OP_WITH_INT(-, decimal128)
424 _DEFINE_DECIMAL_BINARY_OP_WITH_DEC(*, decimal32, decimal32, decimal32)
425 _DEFINE_DECIMAL_BINARY_OP_WITH_INT(*, decimal32)
426 _DEFINE_DECIMAL_BINARY_OP_WITH_DEC(*, decimal64, decimal32, decimal64)
427 _DEFINE_DECIMAL_BINARY_OP_WITH_DEC(*, decimal64, decimal64, decimal32)
428 _DEFINE_DECIMAL_BINARY_OP_WITH_DEC(*, decimal64, decimal64, decimal64)
429 _DEFINE_DECIMAL_BINARY_OP_WITH_INT(*, decimal64)
430 _DEFINE_DECIMAL_BINARY_OP_WITH_DEC(*, decimal128, decimal32, decimal128)
431 _DEFINE_DECIMAL_BINARY_OP_WITH_DEC(*, decimal128, decimal64, decimal128)
432 _DEFINE_DECIMAL_BINARY_OP_WITH_DEC(*, decimal128, decimal128, decimal32)
433 _DEFINE_DECIMAL_BINARY_OP_WITH_DEC(*, decimal128, decimal128, decimal64)
434 _DEFINE_DECIMAL_BINARY_OP_WITH_DEC(*, decimal128, decimal128, decimal128)
435 _DEFINE_DECIMAL_BINARY_OP_WITH_INT(*, decimal128)
437 _DEFINE_DECIMAL_BINARY_OP_WITH_DEC(/, decimal32, decimal32, decimal32)
438 _DEFINE_DECIMAL_BINARY_OP_WITH_INT(/, decimal32)
439 _DEFINE_DECIMAL_BINARY_OP_WITH_DEC(/, decimal64, decimal32, decimal64)
440 _DEFINE_DECIMAL_BINARY_OP_WITH_DEC(/, decimal64, decimal64, decimal32)
441 _DEFINE_DECIMAL_BINARY_OP_WITH_DEC(/, decimal64, decimal64, decimal64)
442 _DEFINE_DECIMAL_BINARY_OP_WITH_INT(/, decimal64)
443 _DEFINE_DECIMAL_BINARY_OP_WITH_DEC(/, decimal128, decimal32, decimal128)
444 _DEFINE_DECIMAL_BINARY_OP_WITH_DEC(/, decimal128, decimal64, decimal128)
445 _DEFINE_DECIMAL_BINARY_OP_WITH_DEC(/, decimal128, decimal128, decimal32)
446 _DEFINE_DECIMAL_BINARY_OP_WITH_DEC(/, decimal128, decimal128, decimal64)
447 _DEFINE_DECIMAL_BINARY_OP_WITH_DEC(/, decimal128, decimal128, decimal128)
448 _DEFINE_DECIMAL_BINARY_OP_WITH_INT(/, decimal128)
450 #undef _DEFINE_DECIMAL_BINARY_OP_WITH_DEC
451 #undef _DEFINE_DECIMAL_BINARY_OP_BOTH
452 #undef _DEFINE_DECIMAL_BINARY_OP_LHS
453 #undef _DEFINE_DECIMAL_BINARY_OP_RHS
454 #undef _DEFINE_DECIMAL_BINARY_OP_WITH_INT
456 // ISO/IEC TR 24733 3.2.9 Comparison operators.
458 #define _DEFINE_DECIMAL_COMPARISON_BOTH(_Op, _T1, _T2) \
459 inline bool operator _Op(_T1 lhs, _T2 __rhs) \
461 return lhs.__getval() _Op __rhs.__getval(); \
464 #define _DEFINE_DECIMAL_COMPARISON_LHS(_Op, _T1, _T2) \
465 inline bool operator _Op(_T1 lhs, _T2 __rhs) \
467 return lhs.__getval() _Op __rhs; \
469 #define _DEFINE_DECIMAL_COMPARISON_RHS(_Op, _T1, _T2) \
470 inline bool operator _Op(_T1 lhs, _T2 __rhs) \
472 return lhs _Op __rhs.__getval(); \
475 #define _DEFINE_DECIMAL_COMPARISONS(_Op, _Tp) \
476 _DEFINE_DECIMAL_COMPARISON_BOTH(_Op, _Tp, decimal32) \
477 _DEFINE_DECIMAL_COMPARISON_BOTH(_Op, _Tp, decimal64) \
478 _DEFINE_DECIMAL_COMPARISON_BOTH(_Op, _Tp, decimal128) \
479 _DEFINE_DECIMAL_COMPARISON_LHS(_Op, _Tp, int) \
480 _DEFINE_DECIMAL_COMPARISON_LHS(_Op, _Tp, unsigned int) \
481 _DEFINE_DECIMAL_COMPARISON_LHS(_Op, _Tp, long) \
482 _DEFINE_DECIMAL_COMPARISON_LHS(_Op, _Tp, unsigned long) \
483 _DEFINE_DECIMAL_COMPARISON_LHS(_Op, _Tp, long long) \
484 _DEFINE_DECIMAL_COMPARISON_LHS(_Op, _Tp, unsigned long long) \
485 _DEFINE_DECIMAL_COMPARISON_RHS(_Op, int, _Tp) \
486 _DEFINE_DECIMAL_COMPARISON_RHS(_Op, unsigned int, _Tp) \
487 _DEFINE_DECIMAL_COMPARISON_RHS(_Op, long, _Tp) \
488 _DEFINE_DECIMAL_COMPARISON_RHS(_Op, unsigned long, _Tp) \
489 _DEFINE_DECIMAL_COMPARISON_RHS(_Op, long long, _Tp) \
490 _DEFINE_DECIMAL_COMPARISON_RHS(_Op, unsigned long long, _Tp)
492 _DEFINE_DECIMAL_COMPARISONS(==, decimal32)
493 _DEFINE_DECIMAL_COMPARISONS(==, decimal64)
494 _DEFINE_DECIMAL_COMPARISONS(==, decimal128)
495 _DEFINE_DECIMAL_COMPARISONS(!=, decimal32)
496 _DEFINE_DECIMAL_COMPARISONS(!=, decimal64)
497 _DEFINE_DECIMAL_COMPARISONS(!=, decimal128)
498 _DEFINE_DECIMAL_COMPARISONS(<, decimal32)
499 _DEFINE_DECIMAL_COMPARISONS(<, decimal64)
500 _DEFINE_DECIMAL_COMPARISONS(<, decimal128)
501 _DEFINE_DECIMAL_COMPARISONS(<=, decimal32)
502 _DEFINE_DECIMAL_COMPARISONS(<=, decimal64)
503 _DEFINE_DECIMAL_COMPARISONS(<=, decimal128)
504 _DEFINE_DECIMAL_COMPARISONS(>, decimal32)
505 _DEFINE_DECIMAL_COMPARISONS(>, decimal64)
506 _DEFINE_DECIMAL_COMPARISONS(>, decimal128)
507 _DEFINE_DECIMAL_COMPARISONS(>=, decimal32)
508 _DEFINE_DECIMAL_COMPARISONS(>=, decimal64)
509 _DEFINE_DECIMAL_COMPARISONS(>=, decimal128)
511 #undef _DEFINE_DECIMAL_COMPARISON_BOTH
512 #undef _DEFINE_DECIMAL_COMPARISON_LHS
513 #undef _DEFINE_DECIMAL_COMPARISON_RHS
514 #undef _DEFINE_DECIMAL_COMPARISONS
516 } // namespace decimal
517 } // namespace std
519 #endif /* _GLIBCXX_DECIMAL_IMPL */