1 //===-- Unittests for strtof ---------------------------------------------===//
2 //
3 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4 // See https://llvm.org/LICENSE.txt for license information.
5 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6 //
7 //===----------------------------------------------------------------------===//
8 
9 #include "src/__support/FPUtil/FPBits.h"
10 #include "src/stdlib/strtof.h"
11 
12 #include "utils/UnitTest/Test.h"
13 
14 #include <errno.h>
15 #include <limits.h>
16 #include <stddef.h>
17 
18 class LlvmLibcStrToFTest : public __llvm_libc::testing::Test {
19 public:
20   void runTest(const char *inputString, const ptrdiff_t expectedStrLen,
21                const uint32_t expectedRawData, const int expectedErrno = 0) {
22     // expectedRawData is the expected float result as a uint32_t, organized
23     // according to IEEE754:
24     //
25     // +-- 1 Sign Bit      +-- 23 Mantissa bits
26     // |                   |
27     // |        +----------+----------+
28     // |        |                     |
29     // SEEEEEEEEMMMMMMMMMMMMMMMMMMMMMMM
30     //  |      |
31     //  +--+---+
32     //     |
33     //     +-- 8 Exponent Bits
34     //
35     //  This is so that the result can be compared in parts.
36     char *strEnd = nullptr;
37 
38     __llvm_libc::fputil::FPBits<float> expectedFP =
39         __llvm_libc::fputil::FPBits<float>(expectedRawData);
40 
41     errno = 0;
42     float result = __llvm_libc::strtof(inputString, &strEnd);
43 
44     __llvm_libc::fputil::FPBits<float> actualFP =
45         __llvm_libc::fputil::FPBits<float>(result);
46 
47     EXPECT_EQ(strEnd - inputString, expectedStrLen);
48 
49     EXPECT_EQ(actualFP.bits, expectedFP.bits);
50     EXPECT_EQ(actualFP.getSign(), expectedFP.getSign());
51     EXPECT_EQ(actualFP.getExponent(), expectedFP.getExponent());
52     EXPECT_EQ(actualFP.getMantissa(), expectedFP.getMantissa());
53     EXPECT_EQ(errno, expectedErrno);
54   }
55 };
56 
57 // This is the set of tests that I have working (verified correct when compared
58 // to system libc). This is here so I don't break more things when I try to fix
59 // them.
60 
61 TEST_F(LlvmLibcStrToFTest, BasicDecimalTests) {
62   runTest("1", 1, 0x3f800000);
63   runTest("123", 3, 0x42f60000);
64   runTest("1234567890", 10, 0x4e932c06u);
65   runTest("123456789012345678901", 21, 0x60d629d4);
66   runTest("0.1", 3, 0x3dcccccdu);
67   runTest(".1", 2, 0x3dcccccdu);
68   runTest("-0.123456789", 12, 0xbdfcd6eau);
69   runTest("0.11111111111111111111", 22, 0x3de38e39u);
70   runTest("0.0000000000000000000000001", 27, 0x15f79688u);
71 }
72 
73 TEST_F(LlvmLibcStrToFTest, DecimalOutOfRangeTests) {
74   runTest("555E36", 6, 0x7f800000, ERANGE);
75   runTest("1e-10000", 8, 0x0, ERANGE);
76 }
77 
78 TEST_F(LlvmLibcStrToFTest, DecimalsWithRoundingProblems) {
79   runTest("20040229", 8, 0x4b98e512);
80   runTest("20040401", 8, 0x4b98e568);
81   runTest("9E9", 3, 0x50061c46);
82 }
83 
84 TEST_F(LlvmLibcStrToFTest, DecimalSubnormals) {
85   runTest("1.4012984643248170709237295832899161312802619418765e-45", 55, 0x1,
86           ERANGE);
87 }
88 
89 TEST_F(LlvmLibcStrToFTest, DecimalWithLongExponent) {
90   runTest("1e2147483648", 12, 0x7f800000, ERANGE);
91   runTest("1e2147483646", 12, 0x7f800000, ERANGE);
92   runTest("100e2147483646", 14, 0x7f800000, ERANGE);
93   runTest("1e-2147483647", 13, 0x0, ERANGE);
94   runTest("1e-2147483649", 13, 0x0, ERANGE);
95 }
96 
97 TEST_F(LlvmLibcStrToFTest, BasicHexadecimalTests) {
98   runTest("0x1", 3, 0x3f800000);
99   runTest("0x10", 4, 0x41800000);
100   runTest("0x11", 4, 0x41880000);
101   runTest("0x0.1234", 8, 0x3d91a000);
102 }
103 
104 TEST_F(LlvmLibcStrToFTest, HexadecimalSubnormalTests) {
105   runTest("0x0.0000000000000000000000000000000002", 38, 0x4000, ERANGE);
106 
107   // This is the largest subnormal number as represented in hex
108   runTest("0x0.00000000000000000000000000000003fffff8", 42, 0x7fffff, ERANGE);
109 }
110 
111 TEST_F(LlvmLibcStrToFTest, HexadecimalSubnormalRoundingTests) {
112   // This is the largest subnormal number that gets rounded down to 0 (as a
113   // float)
114   runTest("0x0.00000000000000000000000000000000000004", 42, 0x0, ERANGE);
115 
116   // This is slightly larger, and thus rounded up
117   runTest("0x0.000000000000000000000000000000000000041", 43, 0x00000001,
118           ERANGE);
119 
120   // These check that we're rounding to even properly
121   runTest("0x0.0000000000000000000000000000000000000b", 42, 0x00000001, ERANGE);
122   runTest("0x0.0000000000000000000000000000000000000c", 42, 0x00000002, ERANGE);
123 
124   // These check that we're rounding to even properly even when the input bits
125   // are longer than the bit fields can contain.
126   runTest("0x1.000000000000000000000p-150", 30, 0x00000000, ERANGE);
127   runTest("0x1.000010000000000001000p-150", 30, 0x00000001, ERANGE);
128   runTest("0x1.000100000000000001000p-134", 30, 0x00008001, ERANGE);
129   runTest("0x1.FFFFFC000000000001000p-127", 30, 0x007FFFFF, ERANGE);
130   runTest("0x1.FFFFFE000000000000000p-127", 30, 0x00800000);
131 }
132 
133 TEST_F(LlvmLibcStrToFTest, HexadecimalNormalRoundingTests) {
134   // This also checks the round to even behavior by checking three adjacent
135   // numbers.
136   // This gets rounded down to even
137   runTest("0x123456500", 11, 0x4f91a2b2);
138   // This doesn't get rounded at all
139   runTest("0x123456600", 11, 0x4f91a2b3);
140   // This gets rounded up to even
141   runTest("0x123456700", 11, 0x4f91a2b4);
142   // Correct rounding for long input
143   runTest("0x1.000001000000000000000", 25, 0x3f800000);
144   runTest("0x1.000001000000000000100", 25, 0x3f800001);
145 }
146 
147 TEST_F(LlvmLibcStrToFTest, HexadecimalsWithRoundingProblems) {
148   runTest("0xFFFFFFFF", 10, 0x4f800000);
149 }
150 
151 TEST_F(LlvmLibcStrToFTest, HexadecimalOutOfRangeTests) {
152   runTest("0x123456789123456789123456789123456789", 38, 0x7f800000, ERANGE);
153   runTest("-0x123456789123456789123456789123456789", 39, 0xff800000, ERANGE);
154   runTest("0x0.00000000000000000000000000000000000001", 42, 0x0, ERANGE);
155 }
156 
157 TEST_F(LlvmLibcStrToFTest, InfTests) {
158   runTest("INF", 3, 0x7f800000);
159   runTest("INFinity", 8, 0x7f800000);
160   runTest("infnity", 3, 0x7f800000);
161   runTest("infinit", 3, 0x7f800000);
162   runTest("infinfinit", 3, 0x7f800000);
163   runTest("innf", 0, 0x0);
164   runTest("-inf", 4, 0xff800000);
165   runTest("-iNfInItY", 9, 0xff800000);
166 }
167 
168 TEST_F(LlvmLibcStrToFTest, NaNTests) {
169   runTest("NaN", 3, 0x7fc00000);
170   runTest("-nAn", 4, 0xffc00000);
171   runTest("NaN()", 5, 0x7fc00000);
172   runTest("NaN(1234)", 9, 0x7fc004d2);
173   runTest("NaN( 1234)", 3, 0x7fc00000);
174 }
175