#ifndef LLVM_LIBC_SRC___SUPPORT_FPUTIL_NORMALFLOAT_H
#define LLVM_LIBC_SRC___SUPPORT_FPUTIL_NORMALFLOAT_H
#include "FPBits.h"
#include "src/__support/CPP/type_traits.h"
#include "src/__support/common.h"
#include "src/__support/macros/config.h"
#include <stdint.h>
namespace LIBC_NAMESPACE_DECL {
namespace fputil {
template <typename T> struct NormalFloat {
static_assert(
cpp::is_floating_point_v<T>,
"NormalFloat template parameter has to be a floating point type.");
using StorageType = typename FPBits<T>::StorageType;
static constexpr StorageType ONE =
(StorageType(1) << FPBits<T>::FRACTION_LEN);
int32_t exponent;
StorageType mantissa;
static_assert(sizeof(StorageType) * 8 >= FPBits<T>::FRACTION_LEN + 1,
"Bad type for mantissa in NormalFloat.");
Sign sign = Sign::POS;
LIBC_INLINE NormalFloat(Sign s, int32_t e, StorageType m)
: exponent(e), mantissa(m), sign(s) {
if (mantissa >= ONE)
return;
unsigned normalization_shift = evaluate_normalization_shift(mantissa);
mantissa <<= normalization_shift;
exponent -= normalization_shift;
}
LIBC_INLINE explicit NormalFloat(T x) { init_from_bits(FPBits<T>(x)); }
LIBC_INLINE explicit NormalFloat(FPBits<T> bits) { init_from_bits(bits); }
LIBC_INLINE int cmp(const NormalFloat<T> &other) const {
const int result = sign.is_neg() ? -1 : 1;
if (sign != other.sign)
return result;
if (exponent > other.exponent) {
return result;
} else if (exponent == other.exponent) {
if (mantissa > other.mantissa)
return result;
else if (mantissa == other.mantissa)
return 0;
else
return -result;
} else {
return -result;
}
}
LIBC_INLINE NormalFloat<T> mul2(int e) const {
NormalFloat<T> result = *this;
result.exponent += e;
return result;
}
LIBC_INLINE operator T() const {
int biased_exponent = exponent + FPBits<T>::EXP_BIAS;
constexpr int MAX_EXPONENT_VALUE = (1 << FPBits<T>::EXP_LEN) - 2;
if (biased_exponent > MAX_EXPONENT_VALUE) {
return FPBits<T>::inf(sign).get_val();
}
FPBits<T> result(T(0.0));
result.set_sign(sign);
constexpr int SUBNORMAL_EXPONENT = -FPBits<T>::EXP_BIAS + 1;
if (exponent < SUBNORMAL_EXPONENT) {
unsigned shift = SUBNORMAL_EXPONENT - exponent;
if (shift <= FPBits<T>::FRACTION_LEN + 1) {
const StorageType shift_out_mask =
static_cast<StorageType>(StorageType(1) << shift) - 1;
const StorageType shift_out_value = mantissa & shift_out_mask;
const StorageType halfway_value =
static_cast<StorageType>(StorageType(1) << (shift - 1));
result.set_biased_exponent(0);
result.set_mantissa(mantissa >> shift);
StorageType new_mantissa = result.get_mantissa();
if (shift_out_value > halfway_value) {
new_mantissa += 1;
} else if (shift_out_value == halfway_value) {
if (result.get_mantissa() & 0x1)
new_mantissa += 1;
}
result.set_mantissa(new_mantissa);
if (new_mantissa == ONE)
result.set_biased_exponent(1);
return result.get_val();
} else {
return result.get_val();
}
}
result.set_biased_exponent(
static_cast<StorageType>(exponent + FPBits<T>::EXP_BIAS));
result.set_mantissa(mantissa);
return result.get_val();
}
private:
LIBC_INLINE void init_from_bits(FPBits<T> bits) {
sign = bits.sign();
if (bits.is_inf_or_nan() || bits.is_zero()) {
exponent = 0;
mantissa = 0;
return;
}
if (bits.is_subnormal()) {
unsigned shift = evaluate_normalization_shift(bits.get_mantissa());
mantissa = static_cast<StorageType>(bits.get_mantissa() << shift);
exponent = 1 - FPBits<T>::EXP_BIAS - shift;
} else {
exponent = bits.get_biased_exponent() - FPBits<T>::EXP_BIAS;
mantissa = ONE | bits.get_mantissa();
}
}
LIBC_INLINE unsigned evaluate_normalization_shift(StorageType m) {
unsigned shift = 0;
for (; (ONE & m) == 0 && (shift < FPBits<T>::FRACTION_LEN);
m <<= 1, ++shift)
;
return shift;
}
};
#ifdef LIBC_TYPES_LONG_DOUBLE_IS_X86_FLOAT80
template <>
LIBC_INLINE void
NormalFloat<long double>::init_from_bits(FPBits<long double> bits) {
sign = bits.sign();
if (bits.is_inf_or_nan() || bits.is_zero()) {
exponent = 0;
mantissa = 0;
return;
}
if (bits.is_subnormal()) {
if (bits.get_implicit_bit() == 0) {
int normalization_shift =
evaluate_normalization_shift(bits.get_mantissa());
exponent = -16382 - normalization_shift;
mantissa = (bits.get_mantissa() << normalization_shift);
} else {
exponent = -16382;
mantissa = ONE | bits.get_mantissa();
}
} else {
if (bits.get_implicit_bit() == 0) {
exponent = 0;
mantissa = 0;
} else {
exponent = bits.get_biased_exponent() - 16383;
mantissa = ONE | bits.get_mantissa();
}
}
}
template <> LIBC_INLINE NormalFloat<long double>::operator long double() const {
using LDBits = FPBits<long double>;
int biased_exponent = exponent + LDBits::EXP_BIAS;
constexpr int MAX_EXPONENT_VALUE = (1 << LDBits::EXP_LEN) - 2;
if (biased_exponent > MAX_EXPONENT_VALUE) {
return LDBits::inf(sign).get_val();
}
FPBits<long double> result(0.0l);
result.set_sign(sign);
constexpr int SUBNORMAL_EXPONENT = -LDBits::EXP_BIAS + 1;
if (exponent < SUBNORMAL_EXPONENT) {
unsigned shift = SUBNORMAL_EXPONENT - exponent;
if (shift <= LDBits::FRACTION_LEN + 1) {
const StorageType shift_out_mask = (StorageType(1) << shift) - 1;
const StorageType shift_out_value = mantissa & shift_out_mask;
const StorageType halfway_value = StorageType(1) << (shift - 1);
result.set_biased_exponent(0);
result.set_mantissa(mantissa >> shift);
StorageType new_mantissa = result.get_mantissa();
if (shift_out_value > halfway_value) {
new_mantissa += 1;
} else if (shift_out_value == halfway_value) {
if (result.get_mantissa() & 0x1)
new_mantissa += 1;
}
result.set_mantissa(new_mantissa);
if (new_mantissa == ONE) {
result.set_biased_exponent(1);
result.set_implicit_bit(1);
} else {
result.set_implicit_bit(0);
}
return result.get_val();
} else {
return result.get_val();
}
}
result.set_biased_exponent(biased_exponent);
result.set_mantissa(mantissa);
result.set_implicit_bit(1);
return result.get_val();
}
#endif
}
}
#endif