15 changes: 8 additions & 7 deletions libc/utils/FPUtil/ManipulationFunctions.h
Original file line number Diff line number Diff line change
Expand Up @@ -47,13 +47,14 @@ static inline T modf(T x, T &iptr) {
return x;
} else if (bits.isInf()) {
iptr = x;
return bits.encoding.sign ? FPBits<T>::negZero() : FPBits<T>::zero();
return bits.encoding.sign ? T(FPBits<T>::negZero()) : T(FPBits<T>::zero());
} else {
iptr = trunc(x);
if (x == iptr) {
// If x is already an integer value, then return zero with the right
// sign.
return bits.encoding.sign ? FPBits<T>::negZero() : FPBits<T>::zero();
return bits.encoding.sign ? T(FPBits<T>::negZero())
: T(FPBits<T>::zero());
} else {
return x - iptr;
}
Expand All @@ -65,7 +66,7 @@ template <typename T,
static inline T copysign(T x, T y) {
FPBits<T> xbits(x);
xbits.encoding.sign = FPBits<T>(y).encoding.sign;
return xbits;
return T(xbits);
}

template <typename T,
Expand Down Expand Up @@ -104,12 +105,12 @@ static inline T logb(T x) {
if (bits.isZero()) {
// TODO(Floating point exception): Raise div-by-zero exception.
// TODO(errno): POSIX requires setting errno to ERANGE.
return FPBits<T>::negInf();
return T(FPBits<T>::negInf());
} else if (bits.isNaN()) {
return x;
} else if (bits.isInf()) {
// Return positive infinity.
return FPBits<T>::inf();
return T(FPBits<T>::inf());
}

NormalFloat<T> normal(bits);
Expand All @@ -131,11 +132,11 @@ static inline T ldexp(T x, int exp) {
// calculating the limit.
int expLimit = FPBits<T>::maxExponent + MantissaWidth<T>::value + 1;
if (exp > expLimit)
return bits.encoding.sign ? FPBits<T>::negInf() : FPBits<T>::inf();
return bits.encoding.sign ? T(FPBits<T>::negInf()) : T(FPBits<T>::inf());

// Similarly on the negative side we return zero early if |exp| is too small.
if (exp < -expLimit)
return bits.encoding.sign ? FPBits<T>::negZero() : FPBits<T>::zero();
return bits.encoding.sign ? T(FPBits<T>::negZero()) : T(FPBits<T>::zero());

// For all other values, NormalFloat to T conversion handles it the right way.
NormalFloat<T> normal(bits);
Expand Down
2 changes: 1 addition & 1 deletion libc/utils/FPUtil/NearestIntegerOperations.h
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ static inline T trunc(T x) {

int trimSize = MantissaWidth<T>::value - exponent;
bits.encoding.mantissa = (bits.encoding.mantissa >> trimSize) << trimSize;
return bits;
return T(bits);
}

template <typename T,
Expand Down
14 changes: 7 additions & 7 deletions libc/utils/FPUtil/NormalFloat.h
Original file line number Diff line number Diff line change
Expand Up @@ -93,7 +93,7 @@ template <typename T> struct NormalFloat {
// Max exponent is of the form 0xFF...E. That is why -2 and not -1.
constexpr int maxExponentValue = (1 << ExponentWidth<T>::value) - 2;
if (biasedExponent > maxExponentValue) {
return sign ? FPBits<T>::negInf() : FPBits<T>::inf();
return sign ? T(FPBits<T>::negInf()) : T(FPBits<T>::inf());
}

FPBits<T> result(T(0.0));
Expand Down Expand Up @@ -126,15 +126,15 @@ template <typename T> struct NormalFloat {
// the overflow into the exponent.
if (newMantissa == one)
result.encoding.exponent = 1;
return result;
return T(result);
} else {
return result;
return T(result);
}
}

result.encoding.exponent = exponent + FPBits<T>::exponentBias;
result.encoding.mantissa = mantissa;
return result;
return T(result);
}

private:
Expand Down Expand Up @@ -245,16 +245,16 @@ template <> inline NormalFloat<long double>::operator long double() const {
} else {
result.encoding.implicitBit = 0;
}
return result;
return static_cast<long double>(result);
} else {
return result;
return static_cast<long double>(result);
}
}

result.encoding.exponent = biasedExponent;
result.encoding.mantissa = mantissa;
result.encoding.implicitBit = 1;
return result;
return static_cast<long double>(result);
}
#endif

Expand Down
10 changes: 5 additions & 5 deletions libc/utils/FPUtil/TestHelpers.h
Original file line number Diff line number Diff line change
Expand Up @@ -68,11 +68,11 @@ FPMatcher<T, C> getMatcher(T expectedValue) {
#define DECLARE_SPECIAL_CONSTANTS(T) \
using FPBits = __llvm_libc::fputil::FPBits<T>; \
using UIntType = typename FPBits::UIntType; \
const T zero = FPBits::zero(); \
const T negZero = FPBits::negZero(); \
const T aNaN = FPBits::buildNaN(1); \
const T inf = FPBits::inf(); \
const T negInf = FPBits::negInf();
const T zero = T(FPBits::zero()); \
const T negZero = T(FPBits::negZero()); \
const T aNaN = T(FPBits::buildNaN(1)); \
const T inf = T(FPBits::inf()); \
const T negInf = T(FPBits::negInf());

#define EXPECT_FP_EQ(expected, actual) \
EXPECT_THAT( \
Expand Down