|
| 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