xref: /llvm-project/libc/test/src/__support/str_to_float_test.cpp (revision 04aaa35d40d8c5ff030014866691f9a56e59c142)
1 //===-- Unittests for str_to_float<float> ---------------------------------===//
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/macros/config.h"
10 #include "str_to_fp_test.h"
11 
12 namespace LIBC_NAMESPACE_DECL {
13 
14 using LlvmLibcStrToFltTest = LlvmLibcStrToFloatTest<float>;
15 
16 TEST_F(LlvmLibcStrToFltTest, ClingerFastPathFloat32Simple) {
17   clinger_fast_path_test(123, 0, 0xf60000, 133);
18   clinger_fast_path_test(1234567, 1, 0xbc6146, 150);
19   clinger_fast_path_test(12345, -5, 0xfcd35b, 123);
20 }
21 
22 TEST_F(LlvmLibcStrToFltTest, ClingerFastPathFloat32ExtendedExp) {
23   clinger_fast_path_test(1, 15, 0xe35fa9, 176);
24   clinger_fast_path_test(1, 17, 0xb1a2bc, 183);
25   clinger_fast_path_fails_test(10, 17);
26   clinger_fast_path_fails_test(1, 50);
27 }
28 
29 TEST_F(LlvmLibcStrToFltTest, ClingerFastPathFloat32NegativeExp) {
30   clinger_fast_path_test(1, -5, 0xa7c5ac, 110);
31   clinger_fast_path_test(1, -10, 0xdbe6ff, 93);
32   clinger_fast_path_fails_test(1, -15);
33 }
34 
35 // Check the fallback states for the algorithm:
36 TEST_F(LlvmLibcStrToFltTest, EiselLemireFallbackStates) {
37   // This number can't be evaluated by Eisel-Lemire since it's exactly 1024 away
38   // from both of its closest floating point approximations
39   // (12345678901234548736 and 12345678901234550784)
40   ASSERT_FALSE(internal::eisel_lemire<float>({20040229, 0}).has_value());
41 }
42 
43 TEST_F(LlvmLibcStrToFltTest, SimpleDecimalConversion32SpecificFailures) {
44   simple_decimal_conversion_test(
45       "1.4012984643248170709237295832899161312802619418765e-45", 0x1, 0,
46       ERANGE);
47   simple_decimal_conversion_test(
48       "7."
49       "006492321624085354618647916449580656401309709382578858785341419448955413"
50       "42930300743319094181060791015625e-46",
51       0x0, 0, ERANGE);
52 }
53 
54 TEST(LlvmLibcStrToFltTest, SimpleDecimalConversionExtraTypes) {
55   uint32_t float_output_mantissa = 0;
56   uint32_t output_exp2 = 0;
57 
58   LIBC_NAMESPACE::libc_errno = 0;
59   auto float_result =
60       internal::simple_decimal_conversion<float>("123456789012345678900");
61   float_output_mantissa = float_result.num.mantissa;
62   output_exp2 = float_result.num.exponent;
63   EXPECT_EQ(float_output_mantissa, uint32_t(0xd629d4));
64   EXPECT_EQ(output_exp2, uint32_t(193));
65   EXPECT_EQ(float_result.error, 0);
66 }
67 
68 } // namespace LIBC_NAMESPACE_DECL
69