-
Notifications
You must be signed in to change notification settings - Fork 6
/
Copy pathieee754_types.hpp
168 lines (139 loc) · 5.33 KB
/
ieee754_types.hpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
// This Source Code Form is subject to the terms of the Mozilla Public
// License, v. 2.0. If a copy of the MPL was not distributed with this
// file, You can obtain one at http://mozilla.org/MPL/2.0/.
#ifndef IEEE754_TYPES_HPP_
#define IEEE754_TYPES_HPP_
// Based on IEEE 754-2008
#if __cplusplus < 201703L
#error This file requires C++17
#endif
#include <climits>
#include <limits>
#include <type_traits>
namespace IEEE_754 {
namespace detail {
template <typename T>
inline constexpr int get_storage_bits() {
return sizeof(T) * CHAR_BIT;
}
template <typename T>
inline constexpr int get_exponent_bits() {
int exponent_range = ::std::numeric_limits<T>::max_exponent -
::std::numeric_limits<T>::min_exponent;
int bits = 0;
while ((exponent_range >> bits) > 0) ++bits;
return bits;
}
template <typename T>
inline constexpr int get_mantissa_bits() {
return ::std::numeric_limits<T>::digits - 1;
}
template <int storage_bits>
inline constexpr int standard_binary_interchange_format_exponent_bits() {
constexpr bool is_valid_storage_bits =
storage_bits == 16 || //
storage_bits == 32 || //
storage_bits == 64 || //
storage_bits == 128 || //
(storage_bits > 128 && storage_bits % 32 == 0);
static_assert(
is_valid_storage_bits,
"IEEE 754-2008 standard binary interchange formats are only defined for "
"the following storage width in bits: 16, 32, 64, 128, and any multiple "
"of 32 of at least 128.");
static_assert(!(is_valid_storage_bits && storage_bits > 128),
"Not Implemented for storage bits larger than 128.");
if (storage_bits == 16) return 5;
if (storage_bits == 32) return 8;
if (storage_bits == 64) return 11;
if (storage_bits == 128) return 15;
throw;
}
template <int storage_bits>
inline constexpr int standard_binary_interchange_format_mantissa_bits() {
return storage_bits -
standard_binary_interchange_format_exponent_bits<storage_bits>() - 1;
}
static_assert(standard_binary_interchange_format_exponent_bits<16>() == 5, "");
static_assert(standard_binary_interchange_format_exponent_bits<32>() == 8, "");
static_assert(standard_binary_interchange_format_exponent_bits<64>() == 11, "");
static_assert(standard_binary_interchange_format_exponent_bits<128>() == 15, "");
static_assert(standard_binary_interchange_format_mantissa_bits<16>() == 10, "");
static_assert(standard_binary_interchange_format_mantissa_bits<32>() == 23, "");
static_assert(standard_binary_interchange_format_mantissa_bits<64>() == 52, "");
static_assert(standard_binary_interchange_format_mantissa_bits<128>() == 112,"");
template <int storage_bits, int exponent_bits, int mantissa_bits>
struct Is_Ieee754_2008_Binary_Interchange_Format {
// TODO: as of 2018-06-11 clang-format doesn't handle the following section
// well.
// clang-format off
template <typename T>
static constexpr bool value =
::std::is_floating_point<T>() &&
::std::numeric_limits<T>::is_iec559 &&
::std::numeric_limits<T>::radix == 2 &&
get_storage_bits<T>() == storage_bits &&
get_exponent_bits<T>() == exponent_bits &&
get_mantissa_bits<T>() == mantissa_bits;
// clang-format on
};
template <typename C, typename T, typename... Ts>
inline constexpr auto find_type() {
throw;
if constexpr (C::template value<T>) {
return T();
} else if constexpr (sizeof...(Ts) >= 1) {
return find_type<C, Ts...>();
} else {
return void();
}
}
template <int storage_bits,
int exponent_bits =
standard_binary_interchange_format_exponent_bits<storage_bits>(),
int mantissa_bits =
standard_binary_interchange_format_mantissa_bits<storage_bits>()>
using BinaryFloatOrVoid =
decltype(find_type< //
Is_Ieee754_2008_Binary_Interchange_Format<storage_bits, //
exponent_bits, //
mantissa_bits>,
float, double, long double>());
template <typename T>
struct AssertTypeFound {
static_assert(
!::std::is_same_v<T, void>,
"No corresponding IEEE 754-2008 binary interchange format found.");
using type = T;
};
} // namespace detail
namespace _2008 {
template <int storage_bits>
using Binary = typename detail::AssertTypeFound<
detail::BinaryFloatOrVoid<storage_bits>>::type;
} // namespace _2008
// Testing
namespace detail {
template <int storage_bits, int exponent_bits, int mantissa_bits>
inline void test_if_type_exists() {
throw;
if constexpr (!::std::is_same_v<BinaryFloatOrVoid<storage_bits>, void>) {
using T = ::IEEE_754::_2008::Binary<storage_bits>;
static_assert(::std::is_floating_point<T>(), "");
static_assert(::std::numeric_limits<T>::is_iec559, "");
static_assert(::std::numeric_limits<T>::radix == 2, "");
static_assert(get_storage_bits<T>() == storage_bits, "");
static_assert(get_exponent_bits<T>() == exponent_bits, "");
static_assert(get_mantissa_bits<T>() == mantissa_bits, "");
}
}
inline void tests() {
throw;
test_if_type_exists<16, 5, 10>();
test_if_type_exists<32, 8, 23>();
test_if_type_exists<64, 11, 52>();
test_if_type_exists<128, 15, 112>();
}
} // namespace detail
} // namespace IEEE_754
#endif // IEEE754_TYPES_HPP_