]> git.ipfire.org Git - thirdparty/gcc.git/blob - libstdc++-v3/include/decimal/decimal
*: Use headername alias to associate private includes to public includes.
[thirdparty/gcc.git] / libstdc++-v3 / include / decimal / decimal
1 // <decimal> -*- C++ -*-
2
3 // Copyright (C) 2009 Free Software Foundation, Inc.
4 // This file is part of the GNU ISO C++ Library. This library is free
5 // software; you can redistribute it and/or modify it under the
6 // terms of the GNU General Public License as published by the
7 // Free Software Foundation; either version 3, or (at your option)
8 // any later version.
9
10 // This library is distributed in the hope that it will be useful,
11 // but WITHOUT ANY WARRANTY; without even the implied warranty of
12 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 // GNU General Public License for more details.
14
15 // Under Section 7 of GPL version 3, you are granted additional
16 // permissions described in the GCC Runtime Library Exception, version
17 // 3.1, as published by the Free Software Foundation.
18
19 // You should have received a copy of the GNU General Public License and
20 // a copy of the GCC Runtime Library Exception along with this program;
21 // see the files COPYING3 and COPYING.RUNTIME respectively. If not, see
22 // <http://www.gnu.org/licenses/>.
23
24 /** @file decimal/decimal
25 * This is a Standard C++ Library header.
26 */
27
28 // ISO/IEC TR 24733
29 // Written by Janis Johnson <janis187@us.ibm.com>
30
31 #ifndef _GLIBCXX_DECIMAL
32 #define _GLIBCXX_DECIMAL 1
33
34 #pragma GCC system_header
35
36 #include <bits/c++config.h>
37
38 #ifndef _GLIBCXX_USE_DECIMAL_FLOAT
39 #error This file requires compiler and library support for ISO/IEC TR 24733 \
40 that is currently not available.
41 #endif
42
43 namespace std
44 {
45 /**
46 * @defgroup decimal Decimal Floating-Point Arithmetic
47 * @ingroup numerics
48 *
49 * Classes and functions for decimal floating-point arithmetic.
50 * @{
51 */
52
53 /** @namespace std::decimal
54 * @brief ISO/IEC TR 24733 Decimal floating-point arithmetic.
55 */
56 namespace decimal
57 {
58 class decimal32;
59 class decimal64;
60 class decimal128;
61
62 // 3.2.5 Initialization from coefficient and exponent.
63 static decimal32 make_decimal32(long long __coeff, int __exp);
64 static decimal32 make_decimal32(unsigned long long __coeff, int __exp);
65 static decimal64 make_decimal64(long long __coeff, int __exp);
66 static decimal64 make_decimal64(unsigned long long __coeff, int __exp);
67 static decimal128 make_decimal128(long long __coeff, int __exp);
68 static decimal128 make_decimal128(unsigned long long __coeff, int __exp);
69
70 /// Non-conforming extension: Conversion to integral type.
71 long long decimal32_to_long_long(decimal32 __d);
72 long long decimal64_to_long_long(decimal64 __d);
73 long long decimal128_to_long_long(decimal128 __d);
74 long long decimal_to_long_long(decimal32 __d);
75 long long decimal_to_long_long(decimal64 __d);
76 long long decimal_to_long_long(decimal128 __d);
77
78 // 3.2.6 Conversion to generic floating-point type.
79 float decimal32_to_float(decimal32 __d);
80 float decimal64_to_float(decimal64 __d);
81 float decimal128_to_float(decimal128 __d);
82 float decimal_to_float(decimal32 __d);
83 float decimal_to_float(decimal64 __d);
84 float decimal_to_float(decimal128 __d);
85
86 double decimal32_to_double(decimal32 __d);
87 double decimal64_to_double(decimal64 __d);
88 double decimal128_to_double(decimal128 __d);
89 double decimal_to_double(decimal32 __d);
90 double decimal_to_double(decimal64 __d);
91 double decimal_to_double(decimal128 __d);
92
93 long double decimal32_to_long_double(decimal32 __d);
94 long double decimal64_to_long_double(decimal64 __d);
95 long double decimal128_to_long_double(decimal128 __d);
96 long double decimal_to_long_double(decimal32 __d);
97 long double decimal_to_long_double(decimal64 __d);
98 long double decimal_to_long_double(decimal128 __d);
99
100 // 3.2.7 Unary arithmetic operators.
101 decimal32 operator+(decimal32 __rhs);
102 decimal64 operator+(decimal64 __rhs);
103 decimal128 operator+(decimal128 __rhs);
104 decimal32 operator-(decimal32 __rhs);
105 decimal64 operator-(decimal64 __rhs);
106 decimal128 operator-(decimal128 __rhs);
107
108 // 3.2.8 Binary arithmetic operators.
109 #define _DECLARE_DECIMAL_BINARY_OP_WITH_DEC(_Op, _T1, _T2, _T3) \
110 _T1 operator _Op(_T2 __lhs, _T3 __rhs);
111 #define _DECLARE_DECIMAL_BINARY_OP_WITH_INT(_Op, _Tp) \
112 _Tp operator _Op(_Tp __lhs, int __rhs); \
113 _Tp operator _Op(_Tp __lhs, unsigned int __rhs); \
114 _Tp operator _Op(_Tp __lhs, long __rhs); \
115 _Tp operator _Op(_Tp __lhs, unsigned long __rhs); \
116 _Tp operator _Op(_Tp __lhs, long long __rhs); \
117 _Tp operator _Op(_Tp __lhs, unsigned long long __rhs); \
118 _Tp operator _Op(int __lhs, _Tp __rhs); \
119 _Tp operator _Op(unsigned int __lhs, _Tp __rhs); \
120 _Tp operator _Op(long __lhs, _Tp __rhs); \
121 _Tp operator _Op(unsigned long __lhs, _Tp __rhs); \
122 _Tp operator _Op(long long __lhs, _Tp __rhs); \
123 _Tp operator _Op(unsigned long long __lhs, _Tp __rhs);
124
125 _DECLARE_DECIMAL_BINARY_OP_WITH_DEC(+, decimal32, decimal32, decimal32)
126 _DECLARE_DECIMAL_BINARY_OP_WITH_INT(+, decimal32)
127 _DECLARE_DECIMAL_BINARY_OP_WITH_DEC(+, decimal64, decimal32, decimal64)
128 _DECLARE_DECIMAL_BINARY_OP_WITH_DEC(+, decimal64, decimal64, decimal32)
129 _DECLARE_DECIMAL_BINARY_OP_WITH_DEC(+, decimal64, decimal64, decimal64)
130 _DECLARE_DECIMAL_BINARY_OP_WITH_INT(+, decimal64)
131 _DECLARE_DECIMAL_BINARY_OP_WITH_DEC(+, decimal128, decimal32, decimal128)
132 _DECLARE_DECIMAL_BINARY_OP_WITH_DEC(+, decimal128, decimal64, decimal128)
133 _DECLARE_DECIMAL_BINARY_OP_WITH_DEC(+, decimal128, decimal128, decimal32)
134 _DECLARE_DECIMAL_BINARY_OP_WITH_DEC(+, decimal128, decimal128, decimal64)
135 _DECLARE_DECIMAL_BINARY_OP_WITH_DEC(+, decimal128, decimal128, decimal128)
136 _DECLARE_DECIMAL_BINARY_OP_WITH_INT(+, decimal128)
137
138 _DECLARE_DECIMAL_BINARY_OP_WITH_DEC(-, decimal32, decimal32, decimal32)
139 _DECLARE_DECIMAL_BINARY_OP_WITH_INT(-, decimal32)
140 _DECLARE_DECIMAL_BINARY_OP_WITH_DEC(-, decimal64, decimal32, decimal64)
141 _DECLARE_DECIMAL_BINARY_OP_WITH_DEC(-, decimal64, decimal64, decimal32)
142 _DECLARE_DECIMAL_BINARY_OP_WITH_DEC(-, decimal64, decimal64, decimal64)
143 _DECLARE_DECIMAL_BINARY_OP_WITH_INT(-, decimal64)
144 _DECLARE_DECIMAL_BINARY_OP_WITH_DEC(-, decimal128, decimal32, decimal128)
145 _DECLARE_DECIMAL_BINARY_OP_WITH_DEC(-, decimal128, decimal64, decimal128)
146 _DECLARE_DECIMAL_BINARY_OP_WITH_DEC(-, decimal128, decimal128, decimal32)
147 _DECLARE_DECIMAL_BINARY_OP_WITH_DEC(-, decimal128, decimal128, decimal64)
148 _DECLARE_DECIMAL_BINARY_OP_WITH_DEC(-, decimal128, decimal128, decimal128)
149 _DECLARE_DECIMAL_BINARY_OP_WITH_INT(-, decimal128)
150
151 _DECLARE_DECIMAL_BINARY_OP_WITH_DEC(*, decimal32, decimal32, decimal32)
152 _DECLARE_DECIMAL_BINARY_OP_WITH_INT(*, decimal32)
153 _DECLARE_DECIMAL_BINARY_OP_WITH_DEC(*, decimal64, decimal32, decimal64)
154 _DECLARE_DECIMAL_BINARY_OP_WITH_DEC(*, decimal64, decimal64, decimal32)
155 _DECLARE_DECIMAL_BINARY_OP_WITH_DEC(*, decimal64, decimal64, decimal64)
156 _DECLARE_DECIMAL_BINARY_OP_WITH_INT(*, decimal64)
157 _DECLARE_DECIMAL_BINARY_OP_WITH_DEC(*, decimal128, decimal32, decimal128)
158 _DECLARE_DECIMAL_BINARY_OP_WITH_DEC(*, decimal128, decimal64, decimal128)
159 _DECLARE_DECIMAL_BINARY_OP_WITH_DEC(*, decimal128, decimal128, decimal32)
160 _DECLARE_DECIMAL_BINARY_OP_WITH_DEC(*, decimal128, decimal128, decimal64)
161 _DECLARE_DECIMAL_BINARY_OP_WITH_DEC(*, decimal128, decimal128, decimal128)
162 _DECLARE_DECIMAL_BINARY_OP_WITH_INT(*, decimal128)
163
164 _DECLARE_DECIMAL_BINARY_OP_WITH_DEC(/, decimal32, decimal32, decimal32)
165 _DECLARE_DECIMAL_BINARY_OP_WITH_INT(/, decimal32)
166 _DECLARE_DECIMAL_BINARY_OP_WITH_DEC(/, decimal64, decimal32, decimal64)
167 _DECLARE_DECIMAL_BINARY_OP_WITH_DEC(/, decimal64, decimal64, decimal32)
168 _DECLARE_DECIMAL_BINARY_OP_WITH_DEC(/, decimal64, decimal64, decimal64)
169 _DECLARE_DECIMAL_BINARY_OP_WITH_INT(/, decimal64)
170 _DECLARE_DECIMAL_BINARY_OP_WITH_DEC(/, decimal128, decimal32, decimal128)
171 _DECLARE_DECIMAL_BINARY_OP_WITH_DEC(/, decimal128, decimal64, decimal128)
172 _DECLARE_DECIMAL_BINARY_OP_WITH_DEC(/, decimal128, decimal128, decimal32)
173 _DECLARE_DECIMAL_BINARY_OP_WITH_DEC(/, decimal128, decimal128, decimal64)
174 _DECLARE_DECIMAL_BINARY_OP_WITH_DEC(/, decimal128, decimal128, decimal128)
175 _DECLARE_DECIMAL_BINARY_OP_WITH_INT(/, decimal128)
176
177 #undef _DECLARE_DECIMAL_BINARY_OP_WITH_DEC
178 #undef _DECLARE_DECIMAL_BINARY_OP_WITH_INT
179
180 // 3.2.9 Comparison operators.
181 #define _DECLARE_DECIMAL_COMPARISON(_Op, _Tp) \
182 bool operator _Op(_Tp __lhs, decimal32 __rhs); \
183 bool operator _Op(_Tp __lhs, decimal64 __rhs); \
184 bool operator _Op(_Tp __lhs, decimal128 __rhs); \
185 bool operator _Op(_Tp __lhs, int __rhs); \
186 bool operator _Op(_Tp __lhs, unsigned int __rhs); \
187 bool operator _Op(_Tp __lhs, long __rhs); \
188 bool operator _Op(_Tp __lhs, unsigned long __rhs); \
189 bool operator _Op(_Tp __lhs, long long __rhs); \
190 bool operator _Op(_Tp __lhs, unsigned long long __rhs); \
191 bool operator _Op(int __lhs, _Tp __rhs); \
192 bool operator _Op(unsigned int __lhs, _Tp __rhs); \
193 bool operator _Op(long __lhs, _Tp __rhs); \
194 bool operator _Op(unsigned long __lhs, _Tp __rhs); \
195 bool operator _Op(long long __lhs, _Tp __rhs); \
196 bool operator _Op(unsigned long long __lhs, _Tp __rhs);
197
198 _DECLARE_DECIMAL_COMPARISON(==, decimal32)
199 _DECLARE_DECIMAL_COMPARISON(==, decimal64)
200 _DECLARE_DECIMAL_COMPARISON(==, decimal128)
201
202 _DECLARE_DECIMAL_COMPARISON(!=, decimal32)
203 _DECLARE_DECIMAL_COMPARISON(!=, decimal64)
204 _DECLARE_DECIMAL_COMPARISON(!=, decimal128)
205
206 _DECLARE_DECIMAL_COMPARISON(<, decimal32)
207 _DECLARE_DECIMAL_COMPARISON(<, decimal64)
208 _DECLARE_DECIMAL_COMPARISON(<, decimal128)
209
210 _DECLARE_DECIMAL_COMPARISON(>=, decimal32)
211 _DECLARE_DECIMAL_COMPARISON(>=, decimal64)
212 _DECLARE_DECIMAL_COMPARISON(>=, decimal128)
213
214 _DECLARE_DECIMAL_COMPARISON(>, decimal32)
215 _DECLARE_DECIMAL_COMPARISON(>, decimal64)
216 _DECLARE_DECIMAL_COMPARISON(>, decimal128)
217
218 _DECLARE_DECIMAL_COMPARISON(>=, decimal32)
219 _DECLARE_DECIMAL_COMPARISON(>=, decimal64)
220 _DECLARE_DECIMAL_COMPARISON(>=, decimal128)
221
222 #undef _DECLARE_DECIMAL_COMPARISON
223
224 /// 3.2.2 Class decimal32.
225 class decimal32
226 {
227 public:
228 typedef float __decfloat32 __attribute__((mode(SD)));
229
230 // 3.2.2.2 Construct/copy/destroy.
231 decimal32() : __val(0.e-101DF) {}
232
233 // 3.2.2.3 Conversion from floating-point type.
234 explicit decimal32(decimal64 __d64);
235 explicit decimal32(decimal128 __d128);
236 explicit decimal32(float __r) : __val(__r) {}
237 explicit decimal32(double __r) : __val(__r) {}
238 explicit decimal32(long double __r) : __val(__r) {}
239
240 // 3.2.2.4 Conversion from integral type.
241 decimal32(int __z) : __val(__z) {}
242 decimal32(unsigned int __z) : __val(__z) {}
243 decimal32(long __z) : __val(__z) {}
244 decimal32(unsigned long __z) : __val(__z) {}
245 decimal32(long long __z) : __val(__z) {}
246 decimal32(unsigned long long __z) : __val(__z) {}
247
248 /// Conforming extension: Conversion from scalar decimal type.
249 decimal32(__decfloat32 __z) : __val(__z) {}
250
251 // 3.2.2.5 Conversion to integral type. (DISABLED)
252 //operator long long() const { return (long long)__val; }
253
254 // 3.2.2.6 Increment and decrement operators.
255 decimal32& operator++()
256 {
257 __val += 1;
258 return *this;
259 }
260
261 decimal32 operator++(int)
262 {
263 decimal32 __tmp = *this;
264 __val += 1;
265 return __tmp;
266 }
267
268 decimal32& operator--()
269 {
270 __val -= 1;
271 return *this;
272 }
273
274 decimal32 operator--(int)
275 {
276 decimal32 __tmp = *this;
277 __val -= 1;
278 return __tmp;
279 }
280
281 // 3.2.2.7 Compound assignment.
282 #define _DECLARE_DECIMAL32_COMPOUND_ASSIGNMENT(_Op) \
283 decimal32& operator _Op(decimal32 __rhs); \
284 decimal32& operator _Op(decimal64 __rhs); \
285 decimal32& operator _Op(decimal128 __rhs); \
286 decimal32& operator _Op(int __rhs); \
287 decimal32& operator _Op(unsigned int __rhs); \
288 decimal32& operator _Op(long __rhs); \
289 decimal32& operator _Op(unsigned long __rhs); \
290 decimal32& operator _Op(long long __rhs); \
291 decimal32& operator _Op(unsigned long long __rhs);
292
293 _DECLARE_DECIMAL32_COMPOUND_ASSIGNMENT(+=)
294 _DECLARE_DECIMAL32_COMPOUND_ASSIGNMENT(-=)
295 _DECLARE_DECIMAL32_COMPOUND_ASSIGNMENT(*=)
296 _DECLARE_DECIMAL32_COMPOUND_ASSIGNMENT(/=)
297 #undef _DECLARE_DECIMAL32_COMPOUND_ASSIGNMENT
298
299 private:
300 __decfloat32 __val;
301
302 public:
303 __decfloat32 __getval(void) { return __val; }
304 void __setval(__decfloat32 __x) { __val = __x; }
305 };
306
307 /// 3.2.3 Class decimal64.
308 class decimal64
309 {
310 public:
311 typedef float __decfloat64 __attribute__((mode(DD)));
312
313 // 3.2.3.2 Construct/copy/destroy.
314 decimal64() : __val(0.e-398dd) {}
315
316 // 3.2.3.3 Conversion from floating-point type.
317 decimal64(decimal32 d32);
318 explicit decimal64(decimal128 d128);
319 explicit decimal64(float __r) : __val(__r) {}
320 explicit decimal64(double __r) : __val(__r) {}
321 explicit decimal64(long double __r) : __val(__r) {}
322
323 // 3.2.3.4 Conversion from integral type.
324 decimal64(int __z) : __val(__z) {}
325 decimal64(unsigned int __z) : __val(__z) {}
326 decimal64(long __z) : __val(__z) {}
327 decimal64(unsigned long __z) : __val(__z) {}
328 decimal64(long long __z) : __val(__z) {}
329 decimal64(unsigned long long __z) : __val(__z) {}
330
331 /// Conforming extension: Conversion from scalar decimal type.
332 decimal64(__decfloat64 __z) : __val(__z) {}
333
334 // 3.2.3.5 Conversion to integral type. (DISABLED)
335 //operator long long() const { return (long long)__val; }
336
337 // 3.2.3.6 Increment and decrement operators.
338 decimal64& operator++()
339 {
340 __val += 1;
341 return *this;
342 }
343
344 decimal64 operator++(int)
345 {
346 decimal64 __tmp = *this;
347 __val += 1;
348 return __tmp;
349 }
350
351 decimal64& operator--()
352 {
353 __val -= 1;
354 return *this;
355 }
356
357 decimal64 operator--(int)
358 {
359 decimal64 __tmp = *this;
360 __val -= 1;
361 return __tmp;
362 }
363
364 // 3.2.3.7 Compound assignment.
365 #define _DECLARE_DECIMAL64_COMPOUND_ASSIGNMENT(_Op) \
366 decimal64& operator _Op(decimal32 __rhs); \
367 decimal64& operator _Op(decimal64 __rhs); \
368 decimal64& operator _Op(decimal128 __rhs); \
369 decimal64& operator _Op(int __rhs); \
370 decimal64& operator _Op(unsigned int __rhs); \
371 decimal64& operator _Op(long __rhs); \
372 decimal64& operator _Op(unsigned long __rhs); \
373 decimal64& operator _Op(long long __rhs); \
374 decimal64& operator _Op(unsigned long long __rhs);
375
376 _DECLARE_DECIMAL64_COMPOUND_ASSIGNMENT(+=)
377 _DECLARE_DECIMAL64_COMPOUND_ASSIGNMENT(-=)
378 _DECLARE_DECIMAL64_COMPOUND_ASSIGNMENT(*=)
379 _DECLARE_DECIMAL64_COMPOUND_ASSIGNMENT(/=)
380 #undef _DECLARE_DECIMAL64_COMPOUND_ASSIGNMENT
381
382 private:
383 __decfloat64 __val;
384
385 public:
386 __decfloat64 __getval(void) { return __val; }
387 void __setval(__decfloat64 __x) { __val = __x; }
388 };
389
390 /// 3.2.4 Class decimal128.
391 class decimal128
392 {
393 public:
394 typedef float __decfloat128 __attribute__((mode(TD)));
395
396 // 3.2.4.2 Construct/copy/destroy.
397 decimal128() : __val(0.e-6176DL) {}
398
399 // 3.2.4.3 Conversion from floating-point type.
400 decimal128(decimal32 d32);
401 decimal128(decimal64 d64);
402 explicit decimal128(float __r) : __val(__r) {}
403 explicit decimal128(double __r) : __val(__r) {}
404 explicit decimal128(long double __r) : __val(__r) {}
405
406
407 // 3.2.4.4 Conversion from integral type.
408 decimal128(int __z) : __val(__z) {}
409 decimal128(unsigned int __z) : __val(__z) {}
410 decimal128(long __z) : __val(__z) {}
411 decimal128(unsigned long __z) : __val(__z) {}
412 decimal128(long long __z) : __val(__z) {}
413 decimal128(unsigned long long __z) : __val(__z) {}
414
415 /// Conforming extension: Conversion from scalar decimal type.
416 decimal128(__decfloat128 __z) : __val(__z) {}
417
418 // 3.2.4.5 Conversion to integral type. (DISABLED)
419 //operator long long() const { return (long long)__val; }
420
421 // 3.2.4.6 Increment and decrement operators.
422 decimal128& operator++()
423 {
424 __val += 1;
425 return *this;
426 }
427
428 decimal128 operator++(int)
429 {
430 decimal128 __tmp = *this;
431 __val += 1;
432 return __tmp;
433 }
434
435 decimal128& operator--()
436 {
437 __val -= 1;
438 return *this;
439 }
440
441 decimal128 operator--(int)
442 {
443 decimal128 __tmp = *this;
444 __val -= 1;
445 return __tmp;
446 }
447
448 // 3.2.4.7 Compound assignment.
449 #define _DECLARE_DECIMAL128_COMPOUND_ASSIGNMENT(_Op) \
450 decimal128& operator _Op(decimal32 __rhs); \
451 decimal128& operator _Op(decimal64 __rhs); \
452 decimal128& operator _Op(decimal128 __rhs); \
453 decimal128& operator _Op(int __rhs); \
454 decimal128& operator _Op(unsigned int __rhs); \
455 decimal128& operator _Op(long __rhs); \
456 decimal128& operator _Op(unsigned long __rhs); \
457 decimal128& operator _Op(long long __rhs); \
458 decimal128& operator _Op(unsigned long long __rhs);
459
460 _DECLARE_DECIMAL128_COMPOUND_ASSIGNMENT(+=)
461 _DECLARE_DECIMAL128_COMPOUND_ASSIGNMENT(-=)
462 _DECLARE_DECIMAL128_COMPOUND_ASSIGNMENT(*=)
463 _DECLARE_DECIMAL128_COMPOUND_ASSIGNMENT(/=)
464 #undef _DECLARE_DECIMAL128_COMPOUND_ASSIGNMENT
465
466 private:
467 __decfloat128 __val;
468
469 public:
470 __decfloat128 __getval(void) { return __val; }
471 void __setval(__decfloat128 __x) { __val = __x; }
472 };
473
474 #define _GLIBCXX_USE_DECIMAL_ 1
475
476 } // namespace decimal
477 // @} group decimal
478 } // namespace std
479
480 #include <decimal/decimal.h>
481
482 #endif /* _GLIBCXX_DECIMAL */