1 | // Copyright (c) Microsoft Corporation. |
2 | // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception |
3 | |
4 | #ifndef FLOAT_HEX_PRECISION_TO_CHARS_TEST_CASES_HPP |
5 | #define FLOAT_HEX_PRECISION_TO_CHARS_TEST_CASES_HPP |
6 | |
7 | #include <charconv> |
8 | |
9 | #include "test.hpp" |
10 | using namespace std; |
11 | |
12 | inline constexpr FloatPrecisionToCharsTestCase float_hex_precision_to_chars_test_cases[] = { |
13 | // Test special cases (zero, inf, nan) and an ordinary case. Also test negative signs. |
14 | {.value: 0.0f, .fmt: chars_format::hex, .precision: 4, .correct: "0.0000p+0" }, |
15 | {.value: -0.0f, .fmt: chars_format::hex, .precision: 4, .correct: "-0.0000p+0" }, |
16 | {.value: float_inf, .fmt: chars_format::hex, .precision: 4, .correct: "inf" }, |
17 | {.value: -float_inf, .fmt: chars_format::hex, .precision: 4, .correct: "-inf" }, |
18 | {.value: float_nan, .fmt: chars_format::hex, .precision: 4, .correct: "nan" }, |
19 | {.value: -float_nan, .fmt: chars_format::hex, .precision: 4, .correct: "-nan(ind)" }, |
20 | {.value: float_nan_payload, .fmt: chars_format::hex, .precision: 4, .correct: "nan" }, |
21 | {.value: -float_nan_payload, .fmt: chars_format::hex, .precision: 4, .correct: "-nan" }, |
22 | {.value: 0x1.729p+0f, .fmt: chars_format::hex, .precision: 4, .correct: "1.7290p+0" }, |
23 | {.value: -0x1.729p+0f, .fmt: chars_format::hex, .precision: 4, .correct: "-1.7290p+0" }, |
24 | |
25 | // Test hexfloat corner cases. |
26 | {.value: 0x1.728p+0f, .fmt: chars_format::hex, .precision: 6, .correct: "1.728000p+0" }, |
27 | {.value: 0x0.000002p-126f, .fmt: chars_format::hex, .precision: 6, .correct: "0.000002p-126" }, // min subnormal |
28 | {.value: 0x0.fffffep-126f, .fmt: chars_format::hex, .precision: 6, .correct: "0.fffffep-126" }, // max subnormal |
29 | {.value: 0x1p-126f, .fmt: chars_format::hex, .precision: 6, .correct: "1.000000p-126" }, // min normal |
30 | {.value: 0x1.fffffep+127f, .fmt: chars_format::hex, .precision: 6, .correct: "1.fffffep+127" }, // max normal |
31 | |
32 | // Test hexfloat exponents. |
33 | {.value: 0x1p-109f, .fmt: chars_format::hex, .precision: 0, .correct: "1p-109" }, |
34 | {.value: 0x1p-99f, .fmt: chars_format::hex, .precision: 0, .correct: "1p-99" }, |
35 | {.value: 0x1p-9f, .fmt: chars_format::hex, .precision: 0, .correct: "1p-9" }, |
36 | {.value: 0x1p+0f, .fmt: chars_format::hex, .precision: 0, .correct: "1p+0" }, |
37 | {.value: 0x1p+9f, .fmt: chars_format::hex, .precision: 0, .correct: "1p+9" }, |
38 | {.value: 0x1p+99f, .fmt: chars_format::hex, .precision: 0, .correct: "1p+99" }, |
39 | {.value: 0x1p+109f, .fmt: chars_format::hex, .precision: 0, .correct: "1p+109" }, |
40 | |
41 | // Test hexfloat hexits. |
42 | {.value: 0x1.0123p+0f, .fmt: chars_format::hex, .precision: 4, .correct: "1.0123p+0" }, |
43 | {.value: 0x1.4567p+0f, .fmt: chars_format::hex, .precision: 4, .correct: "1.4567p+0" }, |
44 | {.value: 0x1.89abp+0f, .fmt: chars_format::hex, .precision: 4, .correct: "1.89abp+0" }, |
45 | {.value: 0x1.cdefp+0f, .fmt: chars_format::hex, .precision: 4, .correct: "1.cdefp+0" }, |
46 | |
47 | // Test varying precision. Negative precision requests full precision, not shortest round-trip. |
48 | {.value: 0x1.123456p+0f, .fmt: chars_format::hex, .precision: -2, .correct: "1.123456p+0" }, |
49 | {.value: 0x1.123456p+0f, .fmt: chars_format::hex, .precision: -1, .correct: "1.123456p+0" }, |
50 | {.value: 0x1.123456p+0f, .fmt: chars_format::hex, .precision: 0, .correct: "1p+0" }, |
51 | {.value: 0x1.123456p+0f, .fmt: chars_format::hex, .precision: 1, .correct: "1.1p+0" }, |
52 | {.value: 0x1.123456p+0f, .fmt: chars_format::hex, .precision: 2, .correct: "1.12p+0" }, |
53 | {.value: 0x1.123456p+0f, .fmt: chars_format::hex, .precision: 3, .correct: "1.123p+0" }, |
54 | {.value: 0x1.123456p+0f, .fmt: chars_format::hex, .precision: 4, .correct: "1.1234p+0" }, |
55 | {.value: 0x1.123456p+0f, .fmt: chars_format::hex, .precision: 5, .correct: "1.12345p+0" }, |
56 | {.value: 0x1.123456p+0f, .fmt: chars_format::hex, .precision: 6, .correct: "1.123456p+0" }, |
57 | {.value: 0x1.123456p+0f, .fmt: chars_format::hex, .precision: 7, .correct: "1.1234560p+0" }, |
58 | {.value: 0x1.123456p+0f, .fmt: chars_format::hex, .precision: 8, .correct: "1.12345600p+0" }, |
59 | {.value: 0x1.123456p+0f, .fmt: chars_format::hex, .precision: 9, .correct: "1.123456000p+0" }, |
60 | |
61 | // Test rounding at every position. |
62 | {.value: 0x1.ccccccp+0f, .fmt: chars_format::hex, .precision: 0, .correct: "2p+0" }, |
63 | {.value: 0x1.ccccccp+0f, .fmt: chars_format::hex, .precision: 1, .correct: "1.dp+0" }, |
64 | {.value: 0x1.ccccccp+0f, .fmt: chars_format::hex, .precision: 2, .correct: "1.cdp+0" }, |
65 | {.value: 0x1.ccccccp+0f, .fmt: chars_format::hex, .precision: 3, .correct: "1.ccdp+0" }, |
66 | {.value: 0x1.ccccccp+0f, .fmt: chars_format::hex, .precision: 4, .correct: "1.cccdp+0" }, |
67 | {.value: 0x1.ccccccp+0f, .fmt: chars_format::hex, .precision: 5, .correct: "1.ccccdp+0" }, |
68 | {.value: 0x1.ccccccp+0f, .fmt: chars_format::hex, .precision: 6, .correct: "1.ccccccp+0" }, |
69 | |
70 | // Test all combinations of least significant bit, round bit, and trailing bits. |
71 | {.value: 0x1.04000p+0f, .fmt: chars_format::hex, .precision: 2, .correct: "1.04p+0" }, // Lsb 0, Round 0, Trailing 0 |
72 | {.value: 0x1.04001p+0f, .fmt: chars_format::hex, .precision: 2, .correct: "1.04p+0" }, // Lsb 0, Round 0 and Trailing 1 in different hexits |
73 | {.value: 0x1.04200p+0f, .fmt: chars_format::hex, .precision: 2, .correct: "1.04p+0" }, // Lsb 0, Round 0 and Trailing 1 in same hexit |
74 | {.value: 0x1.04800p+0f, .fmt: chars_format::hex, .precision: 2, .correct: "1.04p+0" }, // Lsb 0, Round 1, Trailing 0 |
75 | {.value: 0x1.04801p+0f, .fmt: chars_format::hex, .precision: 2, .correct: "1.05p+0" }, // Lsb 0, Round 1 and Trailing 1 in different hexits |
76 | {.value: 0x1.04900p+0f, .fmt: chars_format::hex, .precision: 2, .correct: "1.05p+0" }, // Lsb 0, Round 1 and Trailing 1 in same hexit |
77 | {.value: 0x1.05000p+0f, .fmt: chars_format::hex, .precision: 2, .correct: "1.05p+0" }, // Lsb 1, Round 0, Trailing 0 |
78 | {.value: 0x1.05001p+0f, .fmt: chars_format::hex, .precision: 2, .correct: "1.05p+0" }, // Lsb 1, Round 0 and Trailing 1 in different hexits |
79 | {.value: 0x1.05200p+0f, .fmt: chars_format::hex, .precision: 2, .correct: "1.05p+0" }, // Lsb 1, Round 0 and Trailing 1 in same hexit |
80 | {.value: 0x1.05800p+0f, .fmt: chars_format::hex, .precision: 2, .correct: "1.06p+0" }, // Lsb 1, Round 1, Trailing 0 |
81 | {.value: 0x1.05801p+0f, .fmt: chars_format::hex, .precision: 2, .correct: "1.06p+0" }, // Lsb 1, Round 1 and Trailing 1 in different hexits |
82 | {.value: 0x1.05900p+0f, .fmt: chars_format::hex, .precision: 2, .correct: "1.06p+0" }, // Lsb 1, Round 1 and Trailing 1 in same hexit |
83 | |
84 | // Test carry propagation. |
85 | {.value: 0x1.0afffep+0f, .fmt: chars_format::hex, .precision: 5, .correct: "1.0b000p+0" }, |
86 | |
87 | // Test carry propagation into the leading hexit. |
88 | {.value: 0x0.fffffep-126f, .fmt: chars_format::hex, .precision: 5, .correct: "1.00000p-126" }, |
89 | {.value: 0x1.fffffep+127f, .fmt: chars_format::hex, .precision: 5, .correct: "2.00000p+127" }, |
90 | |
91 | // Test how the leading hexit participates in the rounding decision. |
92 | {.value: 0x0.000p+0f, .fmt: chars_format::hex, .precision: 0, .correct: "0p+0" }, |
93 | {.value: 0x0.001p-126f, .fmt: chars_format::hex, .precision: 0, .correct: "0p-126" }, |
94 | {.value: 0x0.200p-126f, .fmt: chars_format::hex, .precision: 0, .correct: "0p-126" }, |
95 | {.value: 0x0.800p-126f, .fmt: chars_format::hex, .precision: 0, .correct: "0p-126" }, |
96 | {.value: 0x0.801p-126f, .fmt: chars_format::hex, .precision: 0, .correct: "1p-126" }, |
97 | {.value: 0x0.900p-126f, .fmt: chars_format::hex, .precision: 0, .correct: "1p-126" }, |
98 | {.value: 0x1.000p+0f, .fmt: chars_format::hex, .precision: 0, .correct: "1p+0" }, |
99 | {.value: 0x1.001p+0f, .fmt: chars_format::hex, .precision: 0, .correct: "1p+0" }, |
100 | {.value: 0x1.200p+0f, .fmt: chars_format::hex, .precision: 0, .correct: "1p+0" }, |
101 | {.value: 0x1.800p+0f, .fmt: chars_format::hex, .precision: 0, .correct: "2p+0" }, |
102 | {.value: 0x1.801p+0f, .fmt: chars_format::hex, .precision: 0, .correct: "2p+0" }, |
103 | {.value: 0x1.900p+0f, .fmt: chars_format::hex, .precision: 0, .correct: "2p+0" }, |
104 | }; |
105 | |
106 | #endif // FLOAT_HEX_PRECISION_TO_CHARS_TEST_CASES_HPP |
107 | |