1 //===-- TestMatchers.cpp ----------------------------------------*- C++ -*-===//
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 "FPMatcher.h"
10 
11 #include "src/__support/FPUtil/FPBits.h"
12 
13 #include "utils/UnitTest/StringUtils.h"
14 
15 #include <sstream>
16 #include <string>
17 
18 namespace __llvm_libc {
19 namespace fputil {
20 namespace testing {
21 
22 template <typename ValType, typename StreamType>
23 cpp::EnableIfType<cpp::IsFloatingPointType<ValType>::Value, void>
describeValue(const char * label,ValType value,StreamType & stream)24 describeValue(const char *label, ValType value, StreamType &stream) {
25   stream << label;
26 
27   FPBits<ValType> bits(value);
28   if (bits.is_nan()) {
29     stream << "(NaN)";
30   } else if (bits.is_inf()) {
31     if (bits.get_sign())
32       stream << "(-Infinity)";
33     else
34       stream << "(+Infinity)";
35   } else {
36     constexpr int exponentWidthInHex =
37         (fputil::ExponentWidth<ValType>::VALUE - 1) / 4 + 1;
38     constexpr int mantissaWidthInHex =
39         (fputil::MantissaWidth<ValType>::VALUE - 1) / 4 + 1;
40     constexpr int bitsWidthInHex =
41         sizeof(typename fputil::FPBits<ValType>::UIntType) * 2;
42 
43     stream << "0x"
44            << int_to_hex<typename fputil::FPBits<ValType>::UIntType>(
45                   bits.uintval(), bitsWidthInHex)
46            << ", (S | E | M) = (" << (bits.get_sign() ? '1' : '0') << " | 0x"
47            << int_to_hex<uint16_t>(bits.get_unbiased_exponent(),
48                                    exponentWidthInHex)
49            << " | 0x"
50            << int_to_hex<typename fputil::FPBits<ValType>::UIntType>(
51                   bits.get_mantissa(), mantissaWidthInHex)
52            << ")";
53   }
54 
55   stream << '\n';
56 }
57 
58 template void describeValue<float>(const char *, float,
59                                    testutils::StreamWrapper &);
60 template void describeValue<double>(const char *, double,
61                                     testutils::StreamWrapper &);
62 template void describeValue<long double>(const char *, long double,
63                                          testutils::StreamWrapper &);
64 
65 template void describeValue<float>(const char *, float, std::stringstream &);
66 template void describeValue<double>(const char *, double, std::stringstream &);
67 template void describeValue<long double>(const char *, long double,
68                                          std::stringstream &);
69 
70 } // namespace testing
71 } // namespace fputil
72 } // namespace __llvm_libc
73