libfxd 0.2.dev
A fixed-point library for C++.
Loading...
Searching...
No Matches
limits.hpp
Go to the documentation of this file.
1/*
2 * libfxd - a fixed-point library for C++
3 *
4 * Copyright 2023 Daniel K. O.
5 * SPDX-License-Identifier: Apache-2.0
6 */
7
8#ifndef LIBFXD_LIMITS_HPP
9#define LIBFXD_LIMITS_HPP
10
11#include <algorithm>
12#include <limits>
13
14#include "constructors.hpp"
15#include "fixed.hpp"
16
17#include "detail/types.hpp"
18
19
20#define LIBFXD_LOG10_2(x) ((x) * 643L / 2136)
21#define LIBFXD_LOG10_2_CEIL(x) (((x) * 643L + 2135) / 2136)
22
23namespace std {
24
26 template<int Int,
27 int Frac,
28 typename Raw>
29 requires (numeric_limits<Raw>::is_specialized)
30 struct numeric_limits<fxd::fixed<Int, Frac, Raw>> {
31
32 static_assert(numeric_limits<Raw>::radix == 2);
33 static_assert(Frac < 2136); // the approximations fail after this
34
36 static constexpr bool is_specialized = true;
37
39 static constexpr bool is_signed = numeric_limits<Raw>::is_signed;
40
42 static constexpr bool is_integer = Frac <= 0;
43
45 static constexpr bool is_exact = true;
46
48 static constexpr bool has_infinity = false;
49
51 static constexpr bool has_quiet_NaN = false;
52
54 static constexpr bool has_signaling_NaN = false;
55
57 static constexpr float_denorm_style has_denorm = denorm_absent;
58
60 static constexpr bool has_denorm_loss = false;
61
63 static constexpr float_round_style round_style = round_toward_zero;
64
66 static constexpr bool is_iec559 = false;
67
69 static constexpr bool is_bounded = numeric_limits<Raw>::is_bounded;
70
72 static constexpr bool is_modulo = numeric_limits<Raw>::is_modulo;
73
75 static constexpr int radix = numeric_limits<Raw>::radix;
76
78 static constexpr int digits = fxd::fixed<Int, Frac, Raw>::bits - is_signed;
79
81 static constexpr int digits10 = LIBFXD_LOG10_2(Frac - 1);
82
84 static constexpr int max_digits10 = max<int>(0, 1 + LIBFXD_LOG10_2_CEIL(Frac));
85
87 static constexpr int min_exponent = 1 - Frac;
88
90 static constexpr int min_exponent10 = LIBFXD_LOG10_2(min_exponent);
91
93 static constexpr int max_exponent = Int - is_signed;
94
96 static constexpr int max_exponent10 = LIBFXD_LOG10_2(max_exponent);
97
99 static constexpr bool traps = numeric_limits<Raw>::traps;
100
102 static constexpr bool tinyness_before = false;
103
105 static constexpr int max_bit = Int - is_signed;
106
108 static constexpr int min_bit = - Frac;
109
111 using float_type = fxd::detail::select_float_t<digits>;
112
113
115 static constexpr
118 noexcept
119 {
121 }
122
123
125 static constexpr
128 noexcept
129 {
130 using Fxd = fxd::fixed<Int, Frac, Raw>;
131 if constexpr (is_signed)
132 return Fxd::from_raw(Raw{-1} << (Fxd::bits - 1));
133 else
134 return Fxd::from_raw(0);
135 }
136
137
139 static constexpr
142 noexcept
143 {
144 using Fxd = fxd::fixed<Int, Frac, Raw>;
145 if constexpr (is_signed) {
146 using URaw = make_unsigned_t<Raw>;
147 return Fxd::from_raw((URaw{1} << (Int + Frac - 1)) - URaw{1});
148 } else
149 return Fxd::from_raw(~Raw{0});
150 }
151
152
154 static constexpr
157 noexcept
158 {
160 }
161
162
164 static constexpr
167 noexcept
168 {
169 return epsilon();
170 }
171
172
174 static constexpr
177 noexcept
178 {
179 return 0;
180 }
181
182
184 static constexpr
187 noexcept
188 {
189 return 0;
190 }
191
192
194 static constexpr
197 noexcept
198 {
199 return 0;
200 }
201
202
204 static constexpr
207 noexcept
208 {
209 return min();
210 }
211
212
214 static constexpr
217 noexcept
218 {
219 return fxd::fixed<Int, Frac, Raw>::from_raw(1 << (Int+Frac - is_signed));
220 }
221
222 };
223
224
225}
226
227
228#undef LIBFXD_LOG10_2
229#undef LIBFXD_LOG10_2_CEIL
230
231
232#endif
#define LIBFXD_LOG10_2(x)
Definition: limits.hpp:20
#define LIBFXD_LOG10_2_CEIL(x)
Definition: limits.hpp:21
STL namespace.
The fixed-point class template.
Definition: fixed.hpp:38
static constexpr fixed from_raw(raw_type val) noexcept
Constructs a fixed from any raw bit representation (no conversion.)
static constexpr fxd::fixed< Int, Frac, Raw > infinity() noexcept
Not meaningful, has_infinity is false.
Definition: limits.hpp:176
static constexpr fxd::fixed< Int, Frac, Raw > epsilon() noexcept
Smallest increment.
Definition: limits.hpp:156
static constexpr fxd::fixed< Int, Frac, Raw > min() noexcept
Smallest positive value; same semantics as floating-point.
Definition: limits.hpp:117
static constexpr fxd::fixed< Int, Frac, Raw > lowest() noexcept
Closest value to -∞.
Definition: limits.hpp:127
fxd::detail::select_float_t< digits > float_type
Extension: lossless conversion to a floating-point type.
Definition: limits.hpp:111
static constexpr fxd::fixed< Int, Frac, Raw > max() noexcept
Maximum value.
Definition: limits.hpp:141
static constexpr fxd::fixed< Int, Frac, Raw > round_error() noexcept
Maximum possible error.
Definition: limits.hpp:166
static constexpr fxd::fixed< Int, Frac, Raw > max_pow2() noexcept
Extension: maximum power of two.
Definition: limits.hpp:216
static constexpr fxd::fixed< Int, Frac, Raw > quiet_NaN() noexcept
Not meaningful, has_quiet_NaN is false.
Definition: limits.hpp:186
static constexpr fxd::fixed< Int, Frac, Raw > signaling_NaN() noexcept
Not meaningful, has_signaling_NaN is false.
Definition: limits.hpp:196
static constexpr fxd::fixed< Int, Frac, Raw > denorm_min() noexcept
Same as min() since there are no denormalized values.
Definition: limits.hpp:206