1#ifndef SISCO_BIT_FLOAT_H_
2#define SISCO_BIT_FLOAT_H_
29 return "negative zero";
35 return "signalling nan";
39 return "negative infinity";
43 __builtin_unreachable();
62 :
mantissa_((
std::bit_cast<uint32_t>(f) & 0x007FFFFF) | 0x00800000),
64 (
std::bit_cast<uint32_t>(f) & 0x7F800000) >> 23) -
66 sign_(
std::bit_cast<uint32_t>(f) & 0x80000000) {
91 throw std::overflow_error(
92 "An overflow occured! Value = " + std::to_string(
ToFloat()) +
93 ", exponent = " + std::to_string(
exponent_) +
", mantissa = " +
104 uint32_t mantissa_with_sign) {
105 const uint32_t mantissa = (mantissa_with_sign & 0x7FFFFFFFu);
106 const bool sign = (mantissa_with_sign & 0x80000000u) != 0u;
107 return {mantissa,
sign};
117 const std::pair<uint32_t, bool> unpacked =
119 return BitFloat(unpacked.first, exponent, unpacked.second);
188 while (result & 0xFF000000) {
190 result = (result >> 1);
192 while ((result & 0x00800000) == 0) {
194 result = (result << 1);
199 (result & 0x007FFFFF) |
200 (
static_cast<uint32_t
>(
static_cast<uint8_t
>(exponent + 127)) << 23) |
201 (
sign_ ? 0x80000000 : 0x0);
202 return std::bit_cast<float>(result);
205 explicit constexpr operator float()
const {
return ToFloat(); }
217 return exponent !=
static_cast<int8_t
>(128u) && exponent != -127;
246 int8_t value_exponent) {
247 if (input.
Exponent() == value_exponent) {
249 }
else if (input.
Exponent() > value_exponent) {
250 const uint8_t shift = input.
Exponent() - value_exponent;
257 const uint8_t shift = value_exponent - input.
Exponent();
272 const uint32_t
value = std::bit_cast<uint32_t>(f);
275 }
else if (
value == 0x80000000) {
277 }
else if ((
value & 0x7F800000) == 0x7F800000) {
279 if (
value & 0x00400000)
283 }
else if ((
value & 0x7FFFFFFF) == 0b1111111100000000000000000000000) {
284 if (
value & 0x80000000)
288 }
else if ((
value & 0x7F800000) == 0) {
static constexpr BitFloat FromCompressed(uint32_t mantissa_with_sign, int8_t exponent)
Constructs a BitFloat from a 'packed mantissa' (see PackMantissa()) and the exponent.
constexpr BitFloat & operator-=(const BitFloat &rhs)
Like operator+=, but subtracts rhs.
constexpr bool Sign() const
constexpr bool MantissaOverflow() const
Returns true if bit 32 is set.
constexpr uint32_t PackMantissa() const
Combines the sign and the mantissa into one uint32_t.
constexpr BitFloat()
Constructs a zero-value BitFloat.
friend constexpr bool operator==(const BitFloat &lhs, const BitFloat &rhs)
static constexpr std::pair< uint32_t, bool > UnpackMantissa(uint32_t mantissa_with_sign)
Given a result from PackMantissa(), this function reversed the packing.
constexpr BitFloat & operator*=(unsigned factor)
Multiplies the value by an integer factor.
static constexpr BitFloatKind GetKind(float f)
Determine what kind of float the specified value is: normal, nan, inf, etc.
constexpr BitFloat(uint32_t mantissa, int8_t exponent, bool sign)
friend constexpr std::optional< BitFloat > Match(const BitFloat &input, int8_t value_exponent)
Shifts the input value such that its exponent matches the specified exponent.
constexpr BitFloat & operator+=(const BitFloat &rhs)
Adds the value rhs to this.
constexpr int8_t Exponent() const
static constexpr bool AllowsMath(int8_t exponent)
Based on the exponent, determines if this is a special value and should not be used in mathematical o...
constexpr BitFloat & operator/=(unsigned factor)
Divides the value by an integer factor.
friend constexpr BitFloat operator-(const BitFloat &input)
Negation; flips the sign of the value.
constexpr BitFloat(float f)
Constructs a BitFloat by decomposing the specified floating point value.
constexpr float ToFloat() const
Compose this value back into a single-precision floating point value.
constexpr uint32_t Mantissa() const
constexpr bool AllowsMath() const
Normal or Gaussian distribution.
this file contains all the compiler specific defines
constexpr std::string_view ToString(BitFloatKind kind)
LatticeExprNode sign(const LatticeExprNode &expr)
NewDelAllocator< T > NewDelAllocator< T >::value
Define real & complex conjugation for non-complex types and put comparisons into std namespace.