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