| 1 | // Copyright (C) 2009-2025 Free Software Foundation, Inc. |
| 2 | // |
| 3 | // This file is part of the GNU ISO C++ Library. This library is free |
| 4 | // software; you can redistribute it and/or modify it under the |
| 5 | // terms of the GNU General Public License as published by the |
| 6 | // Free Software Foundation; either version 3, or (at your option) |
| 7 | // any later version. |
| 8 | |
| 9 | // This library is distributed in the hope that it will be useful, |
| 10 | // but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 11 | // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 12 | // GNU General Public License for more details. |
| 13 | |
| 14 | // You should have received a copy of the GNU General Public License along |
| 15 | // with this library; see the file COPYING3. If not see |
| 16 | // <http://www.gnu.org/licenses/>. |
| 17 | |
| 18 | // { dg-require-effective-target dfp } |
| 19 | |
| 20 | // ISO/IEC TR 24733 3.2.6 Conversion to generic floating-point type. |
| 21 | |
| 22 | #include <decimal/decimal> |
| 23 | #include <testsuite_hooks.h> |
| 24 | |
| 25 | using namespace std::decimal; |
| 26 | |
| 27 | void |
| 28 | conversion_to_generic_float_32 () |
| 29 | { |
| 30 | decimal32 d32(123); |
| 31 | float f; |
| 32 | double d; |
| 33 | long double ld; |
| 34 | |
| 35 | f = decimal32_to_float (d32); |
| 36 | VERIFY (f == 123.F); |
| 37 | d = decimal32_to_double (d32); |
| 38 | VERIFY (d == 123.); |
| 39 | ld = decimal32_to_long_double (d32); |
| 40 | VERIFY (ld == 123.L); |
| 41 | |
| 42 | d32++; |
| 43 | f = decimal_to_float (d32); |
| 44 | VERIFY (f == 124.F); |
| 45 | d = decimal_to_double (d32); |
| 46 | VERIFY (d == 124.); |
| 47 | ld = decimal_to_long_double (d32); |
| 48 | VERIFY (ld == 124.L); |
| 49 | } |
| 50 | |
| 51 | void |
| 52 | conversion_to_generic_float_64 () |
| 53 | { |
| 54 | decimal64 d64(234); |
| 55 | float f; |
| 56 | double d; |
| 57 | long double ld; |
| 58 | |
| 59 | f = decimal64_to_float (d64); |
| 60 | VERIFY (f == 234.F); |
| 61 | d = decimal64_to_double (d64); |
| 62 | VERIFY (d == 234.); |
| 63 | ld = decimal64_to_long_double (d64); |
| 64 | VERIFY (ld == 234.L); |
| 65 | |
| 66 | d64++; |
| 67 | f = decimal_to_float (d64); |
| 68 | VERIFY (f == 235.F); |
| 69 | d = decimal_to_double (d64); |
| 70 | VERIFY (d == 235.); |
| 71 | ld = decimal_to_long_double (d64); |
| 72 | VERIFY (ld == 235.L); |
| 73 | } |
| 74 | |
| 75 | void |
| 76 | conversion_to_generic_float_128 () |
| 77 | { |
| 78 | decimal128 d128(345); |
| 79 | float f; |
| 80 | double d; |
| 81 | long double ld; |
| 82 | |
| 83 | f = decimal128_to_float (d128); |
| 84 | VERIFY (f == 345.F); |
| 85 | d = decimal128_to_double (d128); |
| 86 | VERIFY (d == 345.); |
| 87 | ld = decimal128_to_long_double (d128); |
| 88 | VERIFY (ld == 345.L); |
| 89 | |
| 90 | d128++; |
| 91 | f = decimal_to_float (d128); |
| 92 | VERIFY (f == 346.F); |
| 93 | d = decimal_to_double (d128); |
| 94 | VERIFY (d == 346.); |
| 95 | ld = decimal_to_long_double (d128); |
| 96 | VERIFY (ld == 346.L); |
| 97 | } |
| 98 | |
| 99 | int |
| 100 | main () |
| 101 | { |
| 102 | conversion_to_generic_float_32 (); |
| 103 | conversion_to_generic_float_64 (); |
| 104 | conversion_to_generic_float_128 (); |
| 105 | } |