Skip to content

Commit 87bf0b0

Browse files
author
Siva Chandra Reddy
committed
[libc] Add a class called NormalFloat which represents normalized floats.
This class helps in dealing with normal and subnormal numbers uniformly. Moreover, since this class has been designed to handle all floating formats across platforms, it helps implement floating point functions in a uniform manner. The implementations of frexp and logb have been switched to use this new class as it allows us to use just one implementation across all different floating point formats. Reviewed By: lntue Differential Revision: https://reviews.llvm.org/D86241
1 parent 4aaf772 commit 87bf0b0

File tree

3 files changed

+243
-97
lines changed

3 files changed

+243
-97
lines changed

libc/utils/FPUtil/CMakeLists.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@ add_header_library(
1717
BasicOperations.h
1818
ManipulationFunctions.h
1919
NearestIntegerOperations.h
20+
NormalFloat.h
2021
DEPENDS
2122
libc.utils.CPP.standalone_cpp
2223
)

libc/utils/FPUtil/ManipulationFunctions.h

Lines changed: 14 additions & 97 deletions
Original file line numberDiff line numberDiff line change
@@ -6,37 +6,20 @@
66
//
77
//===----------------------------------------------------------------------===//
88

9+
#ifndef LLVM_LIBC_UTILS_FPUTIL_MANIPULATION_FUNCTIONS_H
10+
#define LLVM_LIBC_UTILS_FPUTIL_MANIPULATION_FUNCTIONS_H
11+
912
#include "FPBits.h"
1013
#include "NearestIntegerOperations.h"
14+
#include "NormalFloat.h"
1115

1216
#include "utils/CPP/TypeTraits.h"
1317

14-
#ifndef LLVM_LIBC_UTILS_FPUTIL_MANIPULATION_FUNCTIONS_H
15-
#define LLVM_LIBC_UTILS_FPUTIL_MANIPULATION_FUNCTIONS_H
16-
1718
namespace __llvm_libc {
1819
namespace fputil {
1920

20-
#if defined(__x86_64__) || defined(__i386__)
21-
template <typename T> struct Standard754Type {
22-
static constexpr bool Value =
23-
cpp::IsSame<float, cpp::RemoveCVType<T>>::Value ||
24-
cpp::IsSame<double, cpp::RemoveCVType<T>>::Value;
25-
};
26-
#else
27-
template <typename T> struct Standard754Type {
28-
static constexpr bool Value = cpp::IsFloatingPointType<T>::Value;
29-
};
30-
#endif
31-
32-
template <typename T> static inline T frexp_impl(FPBits<T> &bits, int &exp) {
33-
exp = bits.getExponent() + 1;
34-
static constexpr uint16_t resultExponent = FPBits<T>::exponentBias - 1;
35-
bits.exponent = resultExponent;
36-
return bits;
37-
}
38-
39-
template <typename T, cpp::EnableIfType<Standard754Type<T>::Value, int> = 0>
21+
template <typename T,
22+
cpp::EnableIfType<cpp::IsFloatingPointType<T>::Value, int> = 0>
4023
static inline T frexp(T x, int &exp) {
4124
FPBits<T> bits(x);
4225
if (bits.isInfOrNaN())
@@ -46,42 +29,12 @@ static inline T frexp(T x, int &exp) {
4629
return x;
4730
}
4831

49-
return frexp_impl(bits, exp);
32+
NormalFloat<T> normal(bits);
33+
exp = normal.exponent + 1;
34+
normal.exponent = -1;
35+
return normal;
5036
}
5137

52-
#if defined(__x86_64__) || defined(__i386__)
53-
static inline long double frexp(long double x, int &exp) {
54-
FPBits<long double> bits(x);
55-
if (bits.isInfOrNaN())
56-
return x;
57-
if (bits.isZero()) {
58-
exp = 0;
59-
return x;
60-
}
61-
62-
if (bits.exponent != 0 || bits.implicitBit == 1)
63-
return frexp_impl(bits, exp);
64-
65-
exp = bits.getExponent();
66-
int shiftCount = 0;
67-
uint64_t fullMantissa = *reinterpret_cast<uint64_t *>(&bits);
68-
static constexpr uint64_t msBitMask = uint64_t(1) << 63;
69-
for (; (fullMantissa & msBitMask) == uint64_t(0);
70-
fullMantissa <<= 1, ++shiftCount) {
71-
// This for loop will terminate as fullMantissa is != 0. If it were 0,
72-
// then x will be NaN and handled before control reaches here.
73-
// When the loop terminates, fullMantissa will represent the full mantissa
74-
// of a normal long double value. That is, the implicit bit has the value
75-
// of 1.
76-
}
77-
78-
exp = exp - shiftCount + 1;
79-
*reinterpret_cast<uint64_t *>(&bits) = fullMantissa;
80-
bits.exponent = FPBits<long double>::exponentBias - 1;
81-
return bits;
82-
}
83-
#endif
84-
8538
template <typename T,
8639
cpp::EnableIfType<cpp::IsFloatingPointType<T>::Value, int> = 0>
8740
static inline T modf(T x, T &iptr) {
@@ -112,11 +65,8 @@ static inline T copysign(T x, T y) {
11265
return xbits;
11366
}
11467

115-
template <typename T> static inline T logb_impl(const FPBits<T> &bits) {
116-
return bits.getExponent();
117-
}
118-
119-
template <typename T, cpp::EnableIfType<Standard754Type<T>::Value, int> = 0>
68+
template <typename T,
69+
cpp::EnableIfType<cpp::IsFloatingPointType<T>::Value, int> = 0>
12070
static inline T logb(T x) {
12171
FPBits<T> bits(x);
12272
if (bits.isZero()) {
@@ -130,42 +80,9 @@ static inline T logb(T x) {
13080
return FPBits<T>::inf();
13181
}
13282

133-
return logb_impl(bits);
134-
}
135-
136-
#if defined(__x86_64__) || defined(__i386__)
137-
static inline long double logb(long double x) {
138-
FPBits<long double> bits(x);
139-
if (bits.isZero()) {
140-
// TODO(Floating point exception): Raise div-by-zero exception.
141-
// TODO(errno): POSIX requires setting errno to ERANGE.
142-
return FPBits<long double>::negInf();
143-
} else if (bits.isNaN()) {
144-
return x;
145-
} else if (bits.isInf()) {
146-
// Return positive infinity.
147-
return FPBits<long double>::inf();
148-
}
149-
150-
if (bits.exponent != 0 || bits.implicitBit == 1)
151-
return logb_impl(bits);
152-
153-
int exp = bits.getExponent();
154-
int shiftCount = 0;
155-
uint64_t fullMantissa = *reinterpret_cast<uint64_t *>(&bits);
156-
static constexpr uint64_t msBitMask = uint64_t(1) << 63;
157-
for (; (fullMantissa & msBitMask) == uint64_t(0);
158-
fullMantissa <<= 1, ++shiftCount) {
159-
// This for loop will terminate as fullMantissa is != 0. If it were 0,
160-
// then x will be NaN and handled before control reaches here.
161-
// When the loop terminates, fullMantissa will represent the full mantissa
162-
// of a normal long double value. That is, the implicit bit has the value
163-
// of 1.
164-
}
165-
166-
return exp - shiftCount;
83+
NormalFloat<T> normal(bits);
84+
return normal.exponent;
16785
}
168-
#endif
16986

17087
} // namespace fputil
17188
} // namespace __llvm_libc

libc/utils/FPUtil/NormalFloat.h

Lines changed: 228 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,228 @@
1+
//===-- A class to store a normalized floating point number -----*- 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_NORMAL_FLOAT_H
10+
#define LLVM_LIBC_UTILS_FPUTIL_NORMAL_FLOAT_H
11+
12+
#include "FPBits.h"
13+
14+
#include "utils/CPP/TypeTraits.h"
15+
16+
#include <stdint.h>
17+
18+
namespace __llvm_libc {
19+
namespace fputil {
20+
21+
// A class which stores the normalized form of a floating point value.
22+
// The special IEEE-754 bits patterns of Zero, infinity and NaNs are
23+
// are not handled by this class.
24+
//
25+
// A normalized floating point number is of this form:
26+
// (-1)*sign * 2^exponent * <mantissa>
27+
// where <mantissa> is of the form 1.<...>.
28+
template <typename T> struct NormalFloat {
29+
static_assert(
30+
cpp::IsFloatingPointType<T>::Value,
31+
"NormalFloat template parameter has to be a floating point type.");
32+
33+
using UIntType = typename FPBits<T>::UIntType;
34+
static constexpr UIntType one = (UIntType(1) << MantissaWidth<T>::value);
35+
36+
// Unbiased exponent value.
37+
int32_t exponent;
38+
39+
UIntType mantissa;
40+
// We want |UIntType| to have atleast one bit more than the actual mantissa
41+
// bit width to accommodate the implicit 1 value.
42+
static_assert(sizeof(UIntType) * 8 >= MantissaWidth<T>::value + 1,
43+
"Bad type for mantissa in NormalFloat.");
44+
45+
bool sign;
46+
47+
NormalFloat(int32_t e, UIntType m, bool s)
48+
: exponent(e), mantissa(m), sign(s) {
49+
if (mantissa >= one)
50+
return;
51+
52+
unsigned normalizationShift = evaluateNormalizationShift(mantissa);
53+
mantissa = mantissa << normalizationShift;
54+
exponent -= normalizationShift;
55+
}
56+
57+
explicit NormalFloat(T x) { initFromBits(FPBits<T>(x)); }
58+
59+
explicit NormalFloat(FPBits<T> bits) { initFromBits(bits); }
60+
61+
// Compares this normalized number with another normalized number.
62+
// Returns -1 is this number is less than |other|, 0 if this number is equal
63+
// to |other|, and 1 if this number is greater than |other|.
64+
int cmp(const NormalFloat<T> &other) const {
65+
if (sign != other.sign)
66+
return sign ? -1 : 1;
67+
68+
if (exponent > other.exponent) {
69+
return sign ? -1 : 1;
70+
} else if (exponent == other.exponent) {
71+
if (mantissa > other.mantissa)
72+
return sign ? -1 : 1;
73+
else if (mantissa == other.mantissa)
74+
return 0;
75+
else
76+
return sign ? 1 : -1;
77+
} else {
78+
return sign ? 1 : -1;
79+
}
80+
}
81+
82+
// Returns a new normalized floating point number which is equal in value
83+
// to this number multiplied by 2^e. That is:
84+
// new = this * 2^e
85+
NormalFloat<T> mul2(int e) const {
86+
NormalFloat<T> result = *this;
87+
result.exponent += e;
88+
return result;
89+
}
90+
91+
operator T() const {
92+
int biasedExponent = exponent + FPBits<T>::exponentBias;
93+
// Max exponent is of the form 0xFF...E. That is why -2 and not -1.
94+
constexpr int maxExponentValue = (1 << ExponentWidth<T>::value) - 2;
95+
if (biasedExponent > maxExponentValue) {
96+
// TODO: Should infinity with the correct sign be returned?
97+
return FPBits<T>::buildNaN(1);
98+
}
99+
100+
FPBits<T> result(T(0.0));
101+
102+
constexpr int subnormalExponent = -FPBits<T>::exponentBias + 1;
103+
if (exponent < subnormalExponent) {
104+
unsigned shift = subnormalExponent - exponent;
105+
if (shift <= MantissaWidth<T>::value) {
106+
// Generate a subnormal number. Might lead to loss of precision.
107+
result.exponent = 0;
108+
result.mantissa = mantissa >> shift;
109+
result.sign = sign;
110+
return result;
111+
} else {
112+
// TODO: Should zero with the correct sign be returned?
113+
return FPBits<T>::buildNaN(1);
114+
}
115+
}
116+
117+
result.exponent = exponent + FPBits<T>::exponentBias;
118+
result.mantissa = mantissa;
119+
result.sign = sign;
120+
return result;
121+
}
122+
123+
private:
124+
void initFromBits(FPBits<T> bits) {
125+
sign = bits.sign;
126+
127+
if (bits.isInfOrNaN() || bits.isZero()) {
128+
// Ignore special bit patterns. Implementations deal with them separately
129+
// anyway so this should not be a problem.
130+
exponent = 0;
131+
mantissa = 0;
132+
return;
133+
}
134+
135+
// Normalize subnormal numbers.
136+
if (bits.exponent == 0) {
137+
unsigned shift = evaluateNormalizationShift(bits.mantissa);
138+
mantissa = UIntType(bits.mantissa) << shift;
139+
exponent = 1 - FPBits<T>::exponentBias - shift;
140+
} else {
141+
exponent = bits.exponent - FPBits<T>::exponentBias;
142+
mantissa = one | bits.mantissa;
143+
}
144+
}
145+
146+
unsigned evaluateNormalizationShift(UIntType m) {
147+
unsigned shift = 0;
148+
for (; (one & m) == 0 && (shift < MantissaWidth<T>::value);
149+
m <<= 1, ++shift)
150+
;
151+
return shift;
152+
}
153+
};
154+
155+
#if defined(__x86_64__) || defined(__i386__)
156+
template <>
157+
inline void NormalFloat<long double>::initFromBits(FPBits<long double> bits) {
158+
sign = bits.sign;
159+
160+
if (bits.isInfOrNaN() || bits.isZero()) {
161+
// Ignore special bit patterns. Implementations deal with them separately
162+
// anyway so this should not be a problem.
163+
exponent = 0;
164+
mantissa = 0;
165+
return;
166+
}
167+
168+
if (bits.exponent == 0) {
169+
if (bits.implicitBit == 0) {
170+
// Since we ignore zero value, the mantissa in this case is non-zero.
171+
int normalizationShift = evaluateNormalizationShift(bits.mantissa);
172+
exponent = -16382 - normalizationShift;
173+
mantissa = (bits.mantissa << normalizationShift);
174+
} else {
175+
exponent = -16382;
176+
mantissa = one | bits.mantissa;
177+
}
178+
} else {
179+
if (bits.implicitBit == 0) {
180+
// Invalid number so just store 0 similar to a NaN.
181+
exponent = 0;
182+
mantissa = 0;
183+
} else {
184+
exponent = bits.exponent - 16383;
185+
mantissa = one | bits.mantissa;
186+
}
187+
}
188+
}
189+
190+
template <> inline NormalFloat<long double>::operator long double() const {
191+
int biasedExponent = exponent + FPBits<long double>::exponentBias;
192+
// Max exponent is of the form 0xFF...E. That is why -2 and not -1.
193+
constexpr int maxExponentValue = (1 << ExponentWidth<long double>::value) - 2;
194+
if (biasedExponent > maxExponentValue) {
195+
// TODO: Should infinity with the correct sign be returned?
196+
return FPBits<long double>::buildNaN(1);
197+
}
198+
199+
FPBits<long double> result(0.0l);
200+
201+
constexpr int subnormalExponent = -FPBits<long double>::exponentBias + 1;
202+
if (exponent < subnormalExponent) {
203+
unsigned shift = subnormalExponent - exponent;
204+
if (shift <= MantissaWidth<long double>::value) {
205+
// Generate a subnormal number. Might lead to loss of precision.
206+
result.exponent = 0;
207+
result.mantissa = mantissa >> shift;
208+
result.implicitBit = 0;
209+
result.sign = sign;
210+
return result;
211+
} else {
212+
// TODO: Should zero with the correct sign be returned?
213+
return FPBits<long double>::buildNaN(1);
214+
}
215+
}
216+
217+
result.exponent = biasedExponent;
218+
result.mantissa = mantissa;
219+
result.implicitBit = 1;
220+
result.sign = sign;
221+
return result;
222+
}
223+
#endif
224+
225+
} // namespace fputil
226+
} // namespace __llvm_libc
227+
228+
#endif // LLVM_LIBC_UTILS_FPUTIL_NORMAL_FLOAT_H

0 commit comments

Comments
 (0)