• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 //===-- Unittests for str_to_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/FPUtil/FPBits.h"
10 #include "src/__support/str_to_float.h"
11 #include "src/__support/uint128.h"
12 #include "src/errno/libc_errno.h"
13 
14 #include "test/UnitTest/Test.h"
15 
16 namespace LIBC_NAMESPACE {
17 
18 template <typename T> struct LlvmLibcStrToFloatTest : public testing::Test {
19   using StorageType = typename fputil::FPBits<T>::StorageType;
20 
clinger_fast_path_testLlvmLibcStrToFloatTest21   void clinger_fast_path_test(const StorageType inputMantissa,
22                               const int32_t inputExp10,
23                               const StorageType expectedOutputMantissa,
24                               const uint32_t expectedOutputExp2) {
25     StorageType actual_output_mantissa = 0;
26     uint32_t actual_output_exp2 = 0;
27 
28     auto result = internal::clinger_fast_path<T>({inputMantissa, inputExp10});
29 
30     ASSERT_TRUE(result.has_value());
31 
32     actual_output_mantissa = result->mantissa;
33     actual_output_exp2 = result->exponent;
34 
35     EXPECT_EQ(actual_output_mantissa, expectedOutputMantissa);
36     EXPECT_EQ(actual_output_exp2, expectedOutputExp2);
37   }
38 
clinger_fast_path_fails_testLlvmLibcStrToFloatTest39   void clinger_fast_path_fails_test(const StorageType inputMantissa,
40                                     const int32_t inputExp10) {
41     ASSERT_FALSE(internal::clinger_fast_path<T>({inputMantissa, inputExp10})
42                      .has_value());
43   }
44 
eisel_lemire_testLlvmLibcStrToFloatTest45   void eisel_lemire_test(const StorageType inputMantissa,
46                          const int32_t inputExp10,
47                          const StorageType expectedOutputMantissa,
48                          const uint32_t expectedOutputExp2) {
49     StorageType actual_output_mantissa = 0;
50     uint32_t actual_output_exp2 = 0;
51 
52     auto result = internal::eisel_lemire<T>({inputMantissa, inputExp10});
53 
54     ASSERT_TRUE(result.has_value());
55 
56     actual_output_mantissa = result->mantissa;
57     actual_output_exp2 = result->exponent;
58 
59     EXPECT_EQ(actual_output_mantissa, expectedOutputMantissa);
60     EXPECT_EQ(actual_output_exp2, expectedOutputExp2);
61   }
62 
63   void simple_decimal_conversion_test(const char *__restrict numStart,
64                                       const StorageType expectedOutputMantissa,
65                                       const uint32_t expectedOutputExp2,
66                                       const int expectedErrno = 0) {
67     StorageType actual_output_mantissa = 0;
68     uint32_t actual_output_exp2 = 0;
69     LIBC_NAMESPACE::libc_errno = 0;
70 
71     auto result = internal::simple_decimal_conversion<T>(numStart);
72 
73     actual_output_mantissa = result.num.mantissa;
74     actual_output_exp2 = result.num.exponent;
75 
76     EXPECT_EQ(actual_output_mantissa, expectedOutputMantissa);
77     EXPECT_EQ(actual_output_exp2, expectedOutputExp2);
78     EXPECT_EQ(result.error, expectedErrno);
79   }
80 };
81 
82 } // namespace LIBC_NAMESPACE
83