1 //===-- Abstract class for bit manipulation of float numbers. ---*- 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 #ifndef LLVM_LIBC_UTILS_FPUTIL_FP_BITS_H
10 #define LLVM_LIBC_UTILS_FPUTIL_FP_BITS_H
11 
12 #include "utils/CPP/TypeTraits.h"
13 
14 #include <stdint.h>
15 
16 namespace __llvm_libc {
17 namespace fputil {
18 
19 template <typename T> struct MantissaWidth {};
20 template <> struct MantissaWidth<float> {
21   static constexpr unsigned value = 23;
22 };
23 template <> struct MantissaWidth<double> {
24   static constexpr unsigned value = 52;
25 };
26 
27 template <typename T> struct ExponentWidth {};
28 template <> struct ExponentWidth<float> {
29   static constexpr unsigned value = 8;
30 };
31 template <> struct ExponentWidth<double> {
32   static constexpr unsigned value = 11;
33 };
34 template <> struct ExponentWidth<long double> {
35   static constexpr unsigned value = 15;
36 };
37 
38 template <typename T> struct FPUIntType {};
39 template <> struct FPUIntType<float> { using Type = uint32_t; };
40 template <> struct FPUIntType<double> { using Type = uint64_t; };
41 
42 #if !(defined(__x86_64__) || defined(__i386__))
43 // TODO: This has to be extended for visual studio where long double and
44 // double are equivalent.
45 template <> struct MantissaWidth<long double> {
46   static constexpr unsigned value = 112;
47 };
48 
49 template <> struct FPUIntType<long double> { using Type = __uint128_t; };
50 #endif
51 
52 // A generic class to represent single precision, double precision, and quad
53 // precision IEEE 754 floating point formats.
54 // On most platforms, the 'float' type corresponds to single precision floating
55 // point numbers, the 'double' type corresponds to double precision floating
56 // point numers, and the 'long double' type corresponds to the quad precision
57 // floating numbers. On x86 platforms however, the 'long double' type maps to
58 // an x87 floating point format. This format is an IEEE 754 extension format.
59 // It is handled as an explicit specialization of this class.
60 template <typename T> struct __attribute__((packed)) FPBits {
61   static_assert(cpp::IsFloatingPointType<T>::Value,
62                 "FPBits instantiated with invalid type.");
63 
64   // Reinterpreting bits as an integer value and interpreting the bits of an
65   // integer value as a floating point value is used in tests. So, a convenient
66   // type is provided for such reinterpretations.
67   using UIntType = typename FPUIntType<T>::Type;
68 
69   UIntType mantissa : MantissaWidth<T>::value;
70   uint16_t exponent : ExponentWidth<T>::value;
71   uint8_t sign : 1;
72 
73   static constexpr int exponentBias = (1 << (ExponentWidth<T>::value - 1)) - 1;
74   static constexpr int maxExponent = (1 << ExponentWidth<T>::value) - 1;
75 
76   static constexpr UIntType minSubnormal = UIntType(1);
77   static constexpr UIntType maxSubnormal =
78       (UIntType(1) << MantissaWidth<T>::value) - 1;
79   static constexpr UIntType minNormal =
80       (UIntType(1) << MantissaWidth<T>::value);
81   static constexpr UIntType maxNormal =
82       ((UIntType(maxExponent) - 1) << MantissaWidth<T>::value) | maxSubnormal;
83 
84   // We don't want accidental type promotions/conversions so we require exact
85   // type match.
86   template <typename XType,
87             cpp::EnableIfType<cpp::IsSame<T, XType>::Value ||
88                                   (cpp::IsIntegral<XType>::Value &&
89                                    (sizeof(XType) == sizeof(UIntType))),
90                               int> = 0>
91   explicit FPBits(XType x) {
92     *this = *reinterpret_cast<FPBits<T> *>(&x);
93   }
94 
95   operator T() { return *reinterpret_cast<T *>(this); }
96 
97   int getExponent() const { return int(exponent) - exponentBias; }
98 
99   bool isZero() const { return mantissa == 0 && exponent == 0; }
100 
101   bool isInf() const { return mantissa == 0 && exponent == maxExponent; }
102 
103   bool isNaN() const { return exponent == maxExponent && mantissa != 0; }
104 
105   bool isInfOrNaN() const { return exponent == maxExponent; }
106 
107   // Methods below this are used by tests.
108   // The to and from integer bits converters are only used in tests. Hence,
109   // the potential software implementations of UIntType will not slow real
110   // code.
111 
112   UIntType bitsAsUInt() const {
113     return *reinterpret_cast<const UIntType *>(this);
114   }
115 
116   static FPBits<T> zero() { return FPBits(T(0.0)); }
117 
118   static FPBits<T> negZero() {
119     FPBits<T> bits(T(0.0));
120     bits.sign = 1;
121     return bits;
122   }
123 
124   static FPBits<T> inf() {
125     FPBits<T> bits(T(0.0));
126     bits.exponent = maxExponent;
127     return bits;
128   }
129 
130   static FPBits<T> negInf() {
131     FPBits<T> bits(T(0.0));
132     bits.exponent = maxExponent;
133     bits.sign = 1;
134     return bits;
135   }
136 
137   static T buildNaN(UIntType v) {
138     FPBits<T> bits(T(0.0));
139     bits.exponent = maxExponent;
140     bits.mantissa = v;
141     return bits;
142   }
143 };
144 
145 } // namespace fputil
146 } // namespace __llvm_libc
147 
148 #if defined(__x86_64__) || defined(__i386__)
149 #include "utils/FPUtil/LongDoubleBitsX86.h"
150 #endif
151 
152 #endif // LLVM_LIBC_UTILS_FPUTIL_FP_BITS_H
153