]> git.ipfire.org Git - thirdparty/gcc.git/blob - libstdc++-v3/include/decimal/decimal
Simplify range-op shift mask generation
[thirdparty/gcc.git] / libstdc++-v3 / include / decimal / decimal
1 // <decimal> -*- C++ -*-
2
3 // Copyright (C) 2009-2024 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 #pragma GCC diagnostic push
37 #pragma GCC diagnostic ignored "-Wpedantic" // DF suffix
38
39 #include <bits/c++config.h>
40
41 #ifndef _GLIBCXX_USE_DECIMAL_FLOAT
42 #error This file requires compiler and library support for ISO/IEC TR 24733 \
43 that is currently not available.
44 #endif
45
46 namespace std _GLIBCXX_VISIBILITY(default)
47 {
48 _GLIBCXX_BEGIN_NAMESPACE_VERSION
49
50 /**
51 * @defgroup decimal Decimal Floating-Point Arithmetic
52 * @ingroup numerics
53 *
54 * Classes and functions for decimal floating-point arithmetic.
55 * @{
56 */
57
58 /** @namespace std::decimal
59 * @brief ISO/IEC TR 24733 Decimal floating-point arithmetic.
60 */
61 namespace decimal
62 {
63 class decimal32;
64 class decimal64;
65 class decimal128;
66
67 // 3.2.5 Initialization from coefficient and exponent.
68 static decimal32 make_decimal32(long long __coeff, int __exp);
69 static decimal32 make_decimal32(unsigned long long __coeff, int __exp);
70 static decimal64 make_decimal64(long long __coeff, int __exp);
71 static decimal64 make_decimal64(unsigned long long __coeff, int __exp);
72 static decimal128 make_decimal128(long long __coeff, int __exp);
73 static decimal128 make_decimal128(unsigned long long __coeff, int __exp);
74
75 /// Non-conforming extension: Conversion to integral type.
76 long long decimal32_to_long_long(decimal32 __d);
77 long long decimal64_to_long_long(decimal64 __d);
78 long long decimal128_to_long_long(decimal128 __d);
79 long long decimal_to_long_long(decimal32 __d);
80 long long decimal_to_long_long(decimal64 __d);
81 long long decimal_to_long_long(decimal128 __d);
82
83 // 3.2.6 Conversion to generic floating-point type.
84 float decimal32_to_float(decimal32 __d);
85 float decimal64_to_float(decimal64 __d);
86 float decimal128_to_float(decimal128 __d);
87 float decimal_to_float(decimal32 __d);
88 float decimal_to_float(decimal64 __d);
89 float decimal_to_float(decimal128 __d);
90
91 double decimal32_to_double(decimal32 __d);
92 double decimal64_to_double(decimal64 __d);
93 double decimal128_to_double(decimal128 __d);
94 double decimal_to_double(decimal32 __d);
95 double decimal_to_double(decimal64 __d);
96 double decimal_to_double(decimal128 __d);
97
98 long double decimal32_to_long_double(decimal32 __d);
99 long double decimal64_to_long_double(decimal64 __d);
100 long double decimal128_to_long_double(decimal128 __d);
101 long double decimal_to_long_double(decimal32 __d);
102 long double decimal_to_long_double(decimal64 __d);
103 long double decimal_to_long_double(decimal128 __d);
104
105 // 3.2.7 Unary arithmetic operators.
106 decimal32 operator+(decimal32 __rhs);
107 decimal64 operator+(decimal64 __rhs);
108 decimal128 operator+(decimal128 __rhs);
109 decimal32 operator-(decimal32 __rhs);
110 decimal64 operator-(decimal64 __rhs);
111 decimal128 operator-(decimal128 __rhs);
112
113 // 3.2.8 Binary arithmetic operators.
114 #define _DECLARE_DECIMAL_BINARY_OP_WITH_DEC(_Op, _T1, _T2, _T3) \
115 _T1 operator _Op(_T2 __lhs, _T3 __rhs);
116 #define _DECLARE_DECIMAL_BINARY_OP_WITH_INT(_Op, _Tp) \
117 _Tp operator _Op(_Tp __lhs, int __rhs); \
118 _Tp operator _Op(_Tp __lhs, unsigned int __rhs); \
119 _Tp operator _Op(_Tp __lhs, long __rhs); \
120 _Tp operator _Op(_Tp __lhs, unsigned long __rhs); \
121 _Tp operator _Op(_Tp __lhs, long long __rhs); \
122 _Tp operator _Op(_Tp __lhs, unsigned long long __rhs); \
123 _Tp operator _Op(int __lhs, _Tp __rhs); \
124 _Tp operator _Op(unsigned int __lhs, _Tp __rhs); \
125 _Tp operator _Op(long __lhs, _Tp __rhs); \
126 _Tp operator _Op(unsigned long __lhs, _Tp __rhs); \
127 _Tp operator _Op(long long __lhs, _Tp __rhs); \
128 _Tp operator _Op(unsigned long long __lhs, _Tp __rhs);
129
130 _DECLARE_DECIMAL_BINARY_OP_WITH_DEC(+, decimal32, decimal32, decimal32)
131 _DECLARE_DECIMAL_BINARY_OP_WITH_INT(+, decimal32)
132 _DECLARE_DECIMAL_BINARY_OP_WITH_DEC(+, decimal64, decimal32, decimal64)
133 _DECLARE_DECIMAL_BINARY_OP_WITH_DEC(+, decimal64, decimal64, decimal32)
134 _DECLARE_DECIMAL_BINARY_OP_WITH_DEC(+, decimal64, decimal64, decimal64)
135 _DECLARE_DECIMAL_BINARY_OP_WITH_INT(+, decimal64)
136 _DECLARE_DECIMAL_BINARY_OP_WITH_DEC(+, decimal128, decimal32, decimal128)
137 _DECLARE_DECIMAL_BINARY_OP_WITH_DEC(+, decimal128, decimal64, decimal128)
138 _DECLARE_DECIMAL_BINARY_OP_WITH_DEC(+, decimal128, decimal128, decimal32)
139 _DECLARE_DECIMAL_BINARY_OP_WITH_DEC(+, decimal128, decimal128, decimal64)
140 _DECLARE_DECIMAL_BINARY_OP_WITH_DEC(+, decimal128, decimal128, decimal128)
141 _DECLARE_DECIMAL_BINARY_OP_WITH_INT(+, decimal128)
142
143 _DECLARE_DECIMAL_BINARY_OP_WITH_DEC(-, decimal32, decimal32, decimal32)
144 _DECLARE_DECIMAL_BINARY_OP_WITH_INT(-, decimal32)
145 _DECLARE_DECIMAL_BINARY_OP_WITH_DEC(-, decimal64, decimal32, decimal64)
146 _DECLARE_DECIMAL_BINARY_OP_WITH_DEC(-, decimal64, decimal64, decimal32)
147 _DECLARE_DECIMAL_BINARY_OP_WITH_DEC(-, decimal64, decimal64, decimal64)
148 _DECLARE_DECIMAL_BINARY_OP_WITH_INT(-, decimal64)
149 _DECLARE_DECIMAL_BINARY_OP_WITH_DEC(-, decimal128, decimal32, decimal128)
150 _DECLARE_DECIMAL_BINARY_OP_WITH_DEC(-, decimal128, decimal64, decimal128)
151 _DECLARE_DECIMAL_BINARY_OP_WITH_DEC(-, decimal128, decimal128, decimal32)
152 _DECLARE_DECIMAL_BINARY_OP_WITH_DEC(-, decimal128, decimal128, decimal64)
153 _DECLARE_DECIMAL_BINARY_OP_WITH_DEC(-, decimal128, decimal128, decimal128)
154 _DECLARE_DECIMAL_BINARY_OP_WITH_INT(-, decimal128)
155
156 _DECLARE_DECIMAL_BINARY_OP_WITH_DEC(*, decimal32, decimal32, decimal32)
157 _DECLARE_DECIMAL_BINARY_OP_WITH_INT(*, decimal32)
158 _DECLARE_DECIMAL_BINARY_OP_WITH_DEC(*, decimal64, decimal32, decimal64)
159 _DECLARE_DECIMAL_BINARY_OP_WITH_DEC(*, decimal64, decimal64, decimal32)
160 _DECLARE_DECIMAL_BINARY_OP_WITH_DEC(*, decimal64, decimal64, decimal64)
161 _DECLARE_DECIMAL_BINARY_OP_WITH_INT(*, decimal64)
162 _DECLARE_DECIMAL_BINARY_OP_WITH_DEC(*, decimal128, decimal32, decimal128)
163 _DECLARE_DECIMAL_BINARY_OP_WITH_DEC(*, decimal128, decimal64, decimal128)
164 _DECLARE_DECIMAL_BINARY_OP_WITH_DEC(*, decimal128, decimal128, decimal32)
165 _DECLARE_DECIMAL_BINARY_OP_WITH_DEC(*, decimal128, decimal128, decimal64)
166 _DECLARE_DECIMAL_BINARY_OP_WITH_DEC(*, decimal128, decimal128, decimal128)
167 _DECLARE_DECIMAL_BINARY_OP_WITH_INT(*, decimal128)
168
169 _DECLARE_DECIMAL_BINARY_OP_WITH_DEC(/, decimal32, decimal32, decimal32)
170 _DECLARE_DECIMAL_BINARY_OP_WITH_INT(/, decimal32)
171 _DECLARE_DECIMAL_BINARY_OP_WITH_DEC(/, decimal64, decimal32, decimal64)
172 _DECLARE_DECIMAL_BINARY_OP_WITH_DEC(/, decimal64, decimal64, decimal32)
173 _DECLARE_DECIMAL_BINARY_OP_WITH_DEC(/, decimal64, decimal64, decimal64)
174 _DECLARE_DECIMAL_BINARY_OP_WITH_INT(/, decimal64)
175 _DECLARE_DECIMAL_BINARY_OP_WITH_DEC(/, decimal128, decimal32, decimal128)
176 _DECLARE_DECIMAL_BINARY_OP_WITH_DEC(/, decimal128, decimal64, decimal128)
177 _DECLARE_DECIMAL_BINARY_OP_WITH_DEC(/, decimal128, decimal128, decimal32)
178 _DECLARE_DECIMAL_BINARY_OP_WITH_DEC(/, decimal128, decimal128, decimal64)
179 _DECLARE_DECIMAL_BINARY_OP_WITH_DEC(/, decimal128, decimal128, decimal128)
180 _DECLARE_DECIMAL_BINARY_OP_WITH_INT(/, decimal128)
181
182 #undef _DECLARE_DECIMAL_BINARY_OP_WITH_DEC
183 #undef _DECLARE_DECIMAL_BINARY_OP_WITH_INT
184
185 // 3.2.9 Comparison operators.
186 #define _DECLARE_DECIMAL_COMPARISON(_Op, _Tp) \
187 bool operator _Op(_Tp __lhs, decimal32 __rhs); \
188 bool operator _Op(_Tp __lhs, decimal64 __rhs); \
189 bool operator _Op(_Tp __lhs, decimal128 __rhs); \
190 bool operator _Op(_Tp __lhs, int __rhs); \
191 bool operator _Op(_Tp __lhs, unsigned int __rhs); \
192 bool operator _Op(_Tp __lhs, long __rhs); \
193 bool operator _Op(_Tp __lhs, unsigned long __rhs); \
194 bool operator _Op(_Tp __lhs, long long __rhs); \
195 bool operator _Op(_Tp __lhs, unsigned long long __rhs); \
196 bool operator _Op(int __lhs, _Tp __rhs); \
197 bool operator _Op(unsigned int __lhs, _Tp __rhs); \
198 bool operator _Op(long __lhs, _Tp __rhs); \
199 bool operator _Op(unsigned long __lhs, _Tp __rhs); \
200 bool operator _Op(long long __lhs, _Tp __rhs); \
201 bool operator _Op(unsigned long long __lhs, _Tp __rhs);
202
203 _DECLARE_DECIMAL_COMPARISON(==, decimal32)
204 _DECLARE_DECIMAL_COMPARISON(==, decimal64)
205 _DECLARE_DECIMAL_COMPARISON(==, decimal128)
206
207 _DECLARE_DECIMAL_COMPARISON(!=, decimal32)
208 _DECLARE_DECIMAL_COMPARISON(!=, decimal64)
209 _DECLARE_DECIMAL_COMPARISON(!=, decimal128)
210
211 _DECLARE_DECIMAL_COMPARISON(<, decimal32)
212 _DECLARE_DECIMAL_COMPARISON(<, decimal64)
213 _DECLARE_DECIMAL_COMPARISON(<, decimal128)
214
215 _DECLARE_DECIMAL_COMPARISON(>=, decimal32)
216 _DECLARE_DECIMAL_COMPARISON(>=, decimal64)
217 _DECLARE_DECIMAL_COMPARISON(>=, decimal128)
218
219 _DECLARE_DECIMAL_COMPARISON(>, decimal32)
220 _DECLARE_DECIMAL_COMPARISON(>, decimal64)
221 _DECLARE_DECIMAL_COMPARISON(>, decimal128)
222
223 _DECLARE_DECIMAL_COMPARISON(>=, decimal32)
224 _DECLARE_DECIMAL_COMPARISON(>=, decimal64)
225 _DECLARE_DECIMAL_COMPARISON(>=, decimal128)
226
227 #undef _DECLARE_DECIMAL_COMPARISON
228
229 /// 3.2.2 Class decimal32.
230 class decimal32
231 {
232 public:
233 typedef float __decfloat32 __attribute__((mode(SD)));
234
235 // 3.2.2.2 Construct/copy/destroy.
236 decimal32() : __val(0.e-101DF) {}
237
238 // 3.2.2.3 Conversion from floating-point type.
239 explicit decimal32(decimal64 __d64);
240 explicit decimal32(decimal128 __d128);
241 explicit decimal32(float __r) : __val(__r) {}
242 explicit decimal32(double __r) : __val(__r) {}
243 explicit decimal32(long double __r) : __val(__r) {}
244
245 // 3.2.2.4 Conversion from integral type.
246 decimal32(int __z) : __val(__z) {}
247 decimal32(unsigned int __z) : __val(__z) {}
248 decimal32(long __z) : __val(__z) {}
249 decimal32(unsigned long __z) : __val(__z) {}
250 decimal32(long long __z) : __val(__z) {}
251 decimal32(unsigned long long __z) : __val(__z) {}
252
253 /// Conforming extension: Conversion from scalar decimal type.
254 decimal32(__decfloat32 __z) : __val(__z) {}
255
256 #if __cplusplus >= 201103L
257 // 3.2.2.5 Conversion to integral type.
258 // Note: explicit per n3407.
259 explicit operator long long() const { return (long long)__val; }
260 #endif
261
262 // 3.2.2.6 Increment and decrement operators.
263 decimal32& operator++()
264 {
265 __val += 1;
266 return *this;
267 }
268
269 decimal32 operator++(int)
270 {
271 decimal32 __tmp = *this;
272 __val += 1;
273 return __tmp;
274 }
275
276 decimal32& operator--()
277 {
278 __val -= 1;
279 return *this;
280 }
281
282 decimal32 operator--(int)
283 {
284 decimal32 __tmp = *this;
285 __val -= 1;
286 return __tmp;
287 }
288
289 // 3.2.2.7 Compound assignment.
290 #define _DECLARE_DECIMAL32_COMPOUND_ASSIGNMENT(_Op) \
291 decimal32& operator _Op(decimal32 __rhs); \
292 decimal32& operator _Op(decimal64 __rhs); \
293 decimal32& operator _Op(decimal128 __rhs); \
294 decimal32& operator _Op(int __rhs); \
295 decimal32& operator _Op(unsigned int __rhs); \
296 decimal32& operator _Op(long __rhs); \
297 decimal32& operator _Op(unsigned long __rhs); \
298 decimal32& operator _Op(long long __rhs); \
299 decimal32& operator _Op(unsigned long long __rhs);
300
301 _DECLARE_DECIMAL32_COMPOUND_ASSIGNMENT(+=)
302 _DECLARE_DECIMAL32_COMPOUND_ASSIGNMENT(-=)
303 _DECLARE_DECIMAL32_COMPOUND_ASSIGNMENT(*=)
304 _DECLARE_DECIMAL32_COMPOUND_ASSIGNMENT(/=)
305 #undef _DECLARE_DECIMAL32_COMPOUND_ASSIGNMENT
306
307 private:
308 __decfloat32 __val;
309
310 public:
311 __decfloat32 __getval(void) { return __val; }
312 void __setval(__decfloat32 __x) { __val = __x; }
313 };
314
315 /// 3.2.3 Class decimal64.
316 class decimal64
317 {
318 public:
319 typedef float __decfloat64 __attribute__((mode(DD)));
320
321 // 3.2.3.2 Construct/copy/destroy.
322 decimal64() : __val(0.e-398dd) {}
323
324 // 3.2.3.3 Conversion from floating-point type.
325 decimal64(decimal32 d32);
326 explicit decimal64(decimal128 d128);
327 explicit decimal64(float __r) : __val(__r) {}
328 explicit decimal64(double __r) : __val(__r) {}
329 explicit decimal64(long double __r) : __val(__r) {}
330
331 // 3.2.3.4 Conversion from integral type.
332 decimal64(int __z) : __val(__z) {}
333 decimal64(unsigned int __z) : __val(__z) {}
334 decimal64(long __z) : __val(__z) {}
335 decimal64(unsigned long __z) : __val(__z) {}
336 decimal64(long long __z) : __val(__z) {}
337 decimal64(unsigned long long __z) : __val(__z) {}
338
339 /// Conforming extension: Conversion from scalar decimal type.
340 decimal64(__decfloat64 __z) : __val(__z) {}
341
342 #if __cplusplus >= 201103L
343 // 3.2.3.5 Conversion to integral type.
344 // Note: explicit per n3407.
345 explicit operator long long() const { return (long long)__val; }
346 #endif
347
348 // 3.2.3.6 Increment and decrement operators.
349 decimal64& operator++()
350 {
351 __val += 1;
352 return *this;
353 }
354
355 decimal64 operator++(int)
356 {
357 decimal64 __tmp = *this;
358 __val += 1;
359 return __tmp;
360 }
361
362 decimal64& operator--()
363 {
364 __val -= 1;
365 return *this;
366 }
367
368 decimal64 operator--(int)
369 {
370 decimal64 __tmp = *this;
371 __val -= 1;
372 return __tmp;
373 }
374
375 // 3.2.3.7 Compound assignment.
376 #define _DECLARE_DECIMAL64_COMPOUND_ASSIGNMENT(_Op) \
377 decimal64& operator _Op(decimal32 __rhs); \
378 decimal64& operator _Op(decimal64 __rhs); \
379 decimal64& operator _Op(decimal128 __rhs); \
380 decimal64& operator _Op(int __rhs); \
381 decimal64& operator _Op(unsigned int __rhs); \
382 decimal64& operator _Op(long __rhs); \
383 decimal64& operator _Op(unsigned long __rhs); \
384 decimal64& operator _Op(long long __rhs); \
385 decimal64& operator _Op(unsigned long long __rhs);
386
387 _DECLARE_DECIMAL64_COMPOUND_ASSIGNMENT(+=)
388 _DECLARE_DECIMAL64_COMPOUND_ASSIGNMENT(-=)
389 _DECLARE_DECIMAL64_COMPOUND_ASSIGNMENT(*=)
390 _DECLARE_DECIMAL64_COMPOUND_ASSIGNMENT(/=)
391 #undef _DECLARE_DECIMAL64_COMPOUND_ASSIGNMENT
392
393 private:
394 __decfloat64 __val;
395
396 public:
397 __decfloat64 __getval(void) { return __val; }
398 void __setval(__decfloat64 __x) { __val = __x; }
399 };
400
401 /// 3.2.4 Class decimal128.
402 class decimal128
403 {
404 public:
405 typedef float __decfloat128 __attribute__((mode(TD)));
406
407 // 3.2.4.2 Construct/copy/destroy.
408 decimal128() : __val(0.e-6176DL) {}
409
410 // 3.2.4.3 Conversion from floating-point type.
411 decimal128(decimal32 d32);
412 decimal128(decimal64 d64);
413 explicit decimal128(float __r) : __val(__r) {}
414 explicit decimal128(double __r) : __val(__r) {}
415 explicit decimal128(long double __r) : __val(__r) {}
416
417
418 // 3.2.4.4 Conversion from integral type.
419 decimal128(int __z) : __val(__z) {}
420 decimal128(unsigned int __z) : __val(__z) {}
421 decimal128(long __z) : __val(__z) {}
422 decimal128(unsigned long __z) : __val(__z) {}
423 decimal128(long long __z) : __val(__z) {}
424 decimal128(unsigned long long __z) : __val(__z) {}
425
426 /// Conforming extension: Conversion from scalar decimal type.
427 decimal128(__decfloat128 __z) : __val(__z) {}
428
429 #if __cplusplus >= 201103L
430 // 3.2.4.5 Conversion to integral type.
431 // Note: explicit per n3407.
432 explicit operator long long() const { return (long long)__val; }
433 #endif
434
435 // 3.2.4.6 Increment and decrement operators.
436 decimal128& operator++()
437 {
438 __val += 1;
439 return *this;
440 }
441
442 decimal128 operator++(int)
443 {
444 decimal128 __tmp = *this;
445 __val += 1;
446 return __tmp;
447 }
448
449 decimal128& operator--()
450 {
451 __val -= 1;
452 return *this;
453 }
454
455 decimal128 operator--(int)
456 {
457 decimal128 __tmp = *this;
458 __val -= 1;
459 return __tmp;
460 }
461
462 // 3.2.4.7 Compound assignment.
463 #define _DECLARE_DECIMAL128_COMPOUND_ASSIGNMENT(_Op) \
464 decimal128& operator _Op(decimal32 __rhs); \
465 decimal128& operator _Op(decimal64 __rhs); \
466 decimal128& operator _Op(decimal128 __rhs); \
467 decimal128& operator _Op(int __rhs); \
468 decimal128& operator _Op(unsigned int __rhs); \
469 decimal128& operator _Op(long __rhs); \
470 decimal128& operator _Op(unsigned long __rhs); \
471 decimal128& operator _Op(long long __rhs); \
472 decimal128& operator _Op(unsigned long long __rhs);
473
474 _DECLARE_DECIMAL128_COMPOUND_ASSIGNMENT(+=)
475 _DECLARE_DECIMAL128_COMPOUND_ASSIGNMENT(-=)
476 _DECLARE_DECIMAL128_COMPOUND_ASSIGNMENT(*=)
477 _DECLARE_DECIMAL128_COMPOUND_ASSIGNMENT(/=)
478 #undef _DECLARE_DECIMAL128_COMPOUND_ASSIGNMENT
479
480 private:
481 __decfloat128 __val;
482
483 public:
484 __decfloat128 __getval(void) { return __val; }
485 void __setval(__decfloat128 __x) { __val = __x; }
486 };
487
488 #define _GLIBCXX_USE_DECIMAL_ 1
489 } // namespace decimal
490 /// @} group decimal
491
492 _GLIBCXX_END_NAMESPACE_VERSION
493 } // namespace std
494
495 #include <decimal/decimal.h>
496
497 #pragma GCC diagnostic pop
498 #endif /* _GLIBCXX_DECIMAL */