1#ifndef SANISIZER_FLOAT_HPP
2#define SANISIZER_FLOAT_HPP
30template<
typename Float_>
32 static_assert(std::is_floating_point<Float_>::value);
34 assert(std::isfinite(x));
35 assert(std::trunc(x) == x);
37#ifndef SANISIZER_FLOAT_FORCE_FREXP
38 if constexpr(std::numeric_limits<Float_>::radix == 2) {
43 return std::ilogb(x) + 1;
52#ifndef SANISIZER_FLOAT_FORCE_FREXP
72template<
typename Integer_,
typename Float_>
74 static_assert(std::is_floating_point<Float_>::value);
75 static_assert(std::is_integral<Integer_>::value);
77 if (!std::isfinite(x)) {
78 throw std::range_error(
"expected a finite value");
81 throw std::range_error(
"expected a non-negative value");
85 constexpr auto output_precision = std::numeric_limits<Integer_>::digits;
87 throw std::overflow_error(
"value is too large for the specified integer type");
114template<
typename Float_,
typename Value_>
119 constexpr auto xmax =
get_max<I<
decltype(x)> >();
120 if constexpr(xmax == 0) {
122 }
else if (val == 0) {
126 constexpr auto frad = std::numeric_limits<Float_>::radix;
127 constexpr auto fdig = std::numeric_limits<Float_>::digits;
131 static_assert(frad == 2);
132 static_assert(std::numeric_limits<Float_>::is_iec559);
135 if constexpr(std::numeric_limits<I<
decltype(val)> >::digits > fdig) {
138 if constexpr((xmax - 1) >> fdig) {
139 const auto y = (val - 1) >> fdig;
141 throw std::overflow_error(
"value is too large for the specified floating-point type");
Generics for operating on integer representations.
Sanitize sizes to avoid integer overflow.
Definition Bounded.hpp:15
Float_ to_float(Value_ x)
Definition float.hpp:115
constexpr auto get_value(const Value_ x)
Definition generics.hpp:23
Integer_ from_float(Float_ x)
Definition float.hpp:73
int required_bits_for_float(Float_ x)
Definition float.hpp:31
constexpr auto get_max()
Definition generics.hpp:39