GREM 3.12.0
Game/Rendering Engine Modules for C++
Loading...
Searching...
No Matches
fundamentals.hpp
Go to the documentation of this file.
1// SPDX-FileCopyrightText: 2026 Ivar Härnqvist
2// SPDX-License-Identifier: MIT
3
4#ifndef GREM_CORE_FUNDAMENTALS_HPP
5#define GREM_CORE_FUNDAMENTALS_HPP
6
8
13
14#include <bit> // std::endian, std::bit_..., std::countl_zero, std::countl_one, std::countr_zero, std::countr_one, std::popcount, std::has_single_bit
15#include <climits> // CHAR_BIT
16#include <compare> // std::partial_ordering, std::strong_ordering
17#include <cstddef> // std::size_t, std::ptrdiff_t, std::max_align_t, std::byte
18#include <cstdint> // std::int..._t, std::uint..._t
19#include <cstring> // std::memcpy, std::memmove, std::memset, std::memcmp
20#include <limits> // std::numeric_limits
21#include <type_traits> // std::make_signed_t, std::make_unsigned_t, std::common_type_t
22#include <utility> // std::forward
23
24namespace grem {
25
26template <typename T>
27struct Limits {
28 static constexpr T MIN = std::numeric_limits<T>::lowest();
29 static constexpr T MAX = std::numeric_limits<T>::max();
30};
31
32template <floating_point T>
33struct Limits<T> {
34 static constexpr bool IS_IEC60559 = std::numeric_limits<T>::is_iec559;
35 static constexpr T MIN = std::numeric_limits<T>::lowest();
36 static constexpr T MAX = std::numeric_limits<T>::max();
37 static constexpr T SMALLEST_NORMAL = std::numeric_limits<T>::min();
38 static constexpr T SMALLEST_SUBNORMAL = std::numeric_limits<T>::denorm_min();
39 static constexpr T MACHINE_EPSILON = std::numeric_limits<T>::epsilon();
40 static constexpr T INF = std::numeric_limits<T>::infinity();
41 static constexpr T QUIET_NAN = std::numeric_limits<T>::quiet_NaN();
42};
43
44using std::byte;
45using std::int16_t;
46using std::int32_t;
47using std::int64_t;
48using std::int8_t;
49using std::intmax_t;
50using std::intptr_t;
51using std::max_align_t;
52using std::ptrdiff_t;
53using std::size_t;
54using std::uint16_t;
55using std::uint32_t;
56using std::uint64_t;
57using std::uint8_t;
58using std::uintmax_t;
59using std::uintptr_t;
60using ssize_t = std::make_signed_t<size_t>;
61using std::bit_cast;
62using std::memcmp;
63using std::memcpy;
64using std::memmove;
65using std::memset;
66
67inline constexpr size_t BYTE_BITS = CHAR_BIT;
68
69template <typename T>
71 alignas(T) byte bytes[sizeof(T)];
72
73 [[nodiscard]] constexpr bool operator==(const ValueRepresentation&) const noexcept = default;
74};
75
76using bool8_t = bool;
77enum : bool8_t { false8 = false, true8 = true }; // NOLINT(cppcoreguidelines-use-enum-class)
78static_assert(sizeof(bool8_t) == 1);
79
80template <>
81struct Limits<bool8_t> {
82 static constexpr bool8_t MIN = false8;
83 static constexpr bool8_t MAX = true8;
84};
85
86class bool16_t {
87public:
88 GREM_ALWAYS_INLINE bool16_t() noexcept = default;
89
90 GREM_ALWAYS_INLINE constexpr bool16_t(bool value) noexcept
91 : value(static_cast<uint16_t>(value)) {}
92
93 GREM_ALWAYS_INLINE constexpr operator bool() const noexcept {
94 return static_cast<bool>(value);
95 }
96
97private:
98 uint16_t value;
99};
100inline constexpr bool16_t false16{false};
101inline constexpr bool16_t true16{true};
102
103template <>
105 static constexpr bool16_t MIN = false16;
106 static constexpr bool16_t MAX = true16;
107};
108
109class bool32_t {
110public:
111 GREM_ALWAYS_INLINE bool32_t() noexcept = default;
112
113 GREM_ALWAYS_INLINE constexpr bool32_t(bool value) noexcept
114 : value(static_cast<uint16_t>(value)) {}
115
116 GREM_ALWAYS_INLINE constexpr operator bool() const noexcept {
117 return static_cast<bool>(value);
118 }
119
120private:
121 uint32_t value;
122};
123inline constexpr bool32_t false32{false};
124inline constexpr bool32_t true32{true};
125
126template <>
128 static constexpr bool32_t MIN = false32;
129 static constexpr bool32_t MAX = true32;
130};
131
132class bool64_t {
133public:
134 GREM_ALWAYS_INLINE bool64_t() noexcept = default;
135
136 GREM_ALWAYS_INLINE constexpr bool64_t(bool value) noexcept
137 : value(static_cast<uint16_t>(value)) {}
138
139 GREM_ALWAYS_INLINE constexpr operator bool() const noexcept {
140 return static_cast<bool>(value);
141 }
142
143private:
144 uint64_t value;
145};
146inline constexpr bool64_t false64{false};
147inline constexpr bool64_t true64{true};
148
149template <>
151 static constexpr bool64_t MIN = false64;
152 static constexpr bool64_t MAX = true64;
153};
154
155using float32_t = float;
156static_assert(sizeof(float32_t) == 4);
157using float64_t = double;
158static_assert(sizeof(float64_t) == 8);
159
160struct float16_t {
161 uint16_t _private_value{};
162
163 GREM_ALWAYS_INLINE float16_t() noexcept = default;
164
165 GREM_ALWAYS_INLINE constexpr float16_t(float32_t value) noexcept
166 : _private_value(getFloat16IntegerRepresentationFromFloat32(value)) {}
167
168 constexpr operator float() const noexcept {
169 static_assert(std::numeric_limits<float32_t>::is_iec559);
170
171 constexpr uint32_t F32_EXPONENT_BIAS = 127;
172 constexpr uint32_t F16_EXPONENT_BIAS = 15;
173
174 const uint16_t integerRepresentation = _private_value;
175 // clang-format off
176 const uint32_t signMask = static_cast<uint32_t>(integerRepresentation & 0b1'00000'0000000000) << 16;
177 uint32_t f16BiasedExponent = static_cast<uint32_t>(integerRepresentation & 0b0'11111'0000000000) >> 10;
178 uint32_t mantissa = static_cast<uint32_t>(integerRepresentation & 0b0'00000'1111111111);
179 // clang-format on
180
181 if (f16BiasedExponent == 31) {
182 if (mantissa == 0) {
183 // Infinite. Make infinity with the same sign.
184 return bit_cast<float32_t>(static_cast<uint32_t>(signMask | 0b0'11111111'00000000000000000000000));
185 }
186 // Not a number. Make NaN with the same sign.
187 return bit_cast<float32_t>(static_cast<uint32_t>(signMask | 0b0'11111111'00000000000000000000000 | (mantissa << 13)));
188 }
189
190 if (f16BiasedExponent == 0) {
191 if (mantissa == 0) {
192 return bit_cast<float32_t>(static_cast<uint32_t>(signMask));
193 }
194
195 // Subnormal. Renormalize.
196 while (!(mantissa & 0b0'00001'0000000000)) {
197 mantissa <<= 1;
198 --f16BiasedExponent;
199 }
200 ++f16BiasedExponent;
201 mantissa &= 0b1111111111111111'1'11110'1111111111;
202 }
203
204 // Make a normal float.
205 const uint32_t f32BiasedExponent = f16BiasedExponent + (F32_EXPONENT_BIAS - F16_EXPONENT_BIAS);
206 return bit_cast<float32_t>(static_cast<uint32_t>(signMask | (f32BiasedExponent << 23) | (mantissa << 13)));
207 }
208
209 [[nodiscard]] GREM_ALWAYS_INLINE constexpr bool operator==(const float16_t& other) const noexcept {
210 return static_cast<float>(*this) == static_cast<float>(other);
211 }
212
213 [[nodiscard]] GREM_ALWAYS_INLINE constexpr bool operator==(float other) const noexcept {
214 return static_cast<float>(*this) == other;
215 }
216
217 [[nodiscard]] GREM_ALWAYS_INLINE constexpr std::partial_ordering operator<=>(const float16_t& other) const noexcept {
218 return static_cast<float>(*this) <=> static_cast<float>(other);
219 }
220
221 [[nodiscard]] GREM_ALWAYS_INLINE constexpr std::partial_ordering operator<=>(float other) const noexcept {
222 return static_cast<float>(*this) <=> other;
223 }
224
225private:
226 friend Limits<float16_t>;
227
228 [[nodiscard]] static constexpr uint16_t getFloat16IntegerRepresentationFromFloat32(float32_t value) noexcept {
229 static_assert(std::numeric_limits<float32_t>::is_iec559);
230
231 constexpr uint32_t F32_EXPONENT_BIAS = 127;
232 constexpr uint32_t F16_EXPONENT_BIAS = 15;
233 constexpr uint32_t F32_BIASED_EXPONENT_MAX = 255;
234
235 const uint32_t integerRepresentation = bit_cast<uint32_t>(value);
236 // clang-format off
237 const uint16_t signMask = static_cast<uint16_t>((integerRepresentation & 0b1'00000000'00000000000000000000000) >> 16);
238 const uint32_t f32BiasedExponent = static_cast<uint32_t>((integerRepresentation & 0b0'11111111'00000000000000000000000) >> 23);
239 int32_t mantissa = static_cast< int32_t>((integerRepresentation & 0b0'00000000'11111111111111111111111) );
240 // clang-format on
241
242 if (f32BiasedExponent == F32_BIASED_EXPONENT_MAX) {
243 if (mantissa == 0) {
244 // Infinite. Make infinity with the same sign.
245 return static_cast<uint16_t>(signMask | 0b0'11111'0000000000);
246 }
247 // Not a number. Make NaN with the same sign.
248 mantissa >>= 13;
249 return static_cast<uint16_t>(signMask | 0b0'11111'0000000000 | mantissa | (mantissa == 0));
250 }
251
252 int32_t f16BiasedExponent = static_cast<int32_t>(f32BiasedExponent) - static_cast<int32_t>(F32_EXPONENT_BIAS - F16_EXPONENT_BIAS);
253 if (f16BiasedExponent <= 0) {
254 if (f16BiasedExponent < -10) {
255 // Magnitude is too small for a half float. Round to 0 with the same sign.
256 return static_cast<uint16_t>(signMask);
257 }
258
259 // Magnitude is too small for a normal half float. Round to nearest subnormal half float (ties to even).
260 mantissa |= 0b0'00000001'00000000000000000000000;
261 const int32_t shift = 1 - f16BiasedExponent + 13;
262 mantissa = (mantissa + ((1 << (shift - 1)) - 1) + ((mantissa >> shift) & 1)) >> shift;
263 return static_cast<uint16_t>(signMask | mantissa);
264 }
265
266 // Round to nearest normal half float (ties to even).
267 mantissa += 0b0'00011'1111111111 + ((mantissa >> 13) & 1);
268
269 if ((mantissa & 0b0'00000001'00000000000000000000000) != 0) {
270 // Mantissa overflowed. Adjust exponent.
271 mantissa = 0;
272 ++f16BiasedExponent;
273 }
274
275 if (f16BiasedExponent > 30) {
276 // Exponent overflowed. Make infinity with the same sign.
277 return static_cast<uint16_t>(signMask | 0b0'11111'0000000000);
278 }
279
280 // Make a normal half float.
281 return static_cast<uint16_t>(signMask | (f16BiasedExponent << 10) | (mantissa >> 13));
282 }
283
284 GREM_ALWAYS_INLINE constexpr float16_t(uint16_t value, int)
285 : _private_value(value) {}
286};
287
288static_assert(static_cast<float32_t>(static_cast<float16_t>(0.0f)) == 0.0f);
289static_assert(static_cast<float32_t>(static_cast<float16_t>(-0.0f)) == -0.0f);
290static_assert(static_cast<float32_t>(static_cast<float16_t>(1.0f)) == 1.0f);
291static_assert(static_cast<float32_t>(static_cast<float16_t>(-1.0f)) == -1.0f);
292static_assert(static_cast<float32_t>(static_cast<float16_t>(512.0f)) == 512.0f);
293static_assert(static_cast<float32_t>(static_cast<float16_t>(-512.0f)) == -512.0f);
294static_assert(static_cast<float32_t>(static_cast<float16_t>(0.125f)) == 0.125f);
295static_assert(static_cast<float32_t>(static_cast<float16_t>(-0.125f)) == -0.125f);
296static_assert(static_cast<float32_t>(static_cast<float16_t>(std::numeric_limits<float32_t>::infinity())) == std::numeric_limits<float32_t>::infinity());
297static_assert(static_cast<float32_t>(static_cast<float16_t>(-std::numeric_limits<float32_t>::infinity())) == -std::numeric_limits<float32_t>::infinity());
298static_assert(static_cast<float32_t>(static_cast<float16_t>(std::numeric_limits<float32_t>::quiet_NaN())) !=
299 static_cast<float32_t>(static_cast<float16_t>(std::numeric_limits<float32_t>::quiet_NaN())));
300
301static_assert(static_cast<float16_t>(16.0f) == static_cast<float16_t>(16));
302static_assert(static_cast<float16_t>(16.0f) != static_cast<float16_t>(17.0f));
303static_assert(static_cast<float16_t>(16.0f) < static_cast<float16_t>(17.0f));
304
305template <>
307 static constexpr bool IS_IEC60559 = true;
308 static constexpr float16_t MIN{uint16_t{0b1'11110'1111111111}, 0};
309 static constexpr float16_t MAX{uint16_t{0b0'11110'1111111111}, 0};
310 static constexpr float16_t SMALLEST_NORMAL{uint16_t{0b0'00001'0000000000}, 0};
311 static constexpr float16_t SMALLEST_SUBNORMAL{uint16_t{0b0'00000'0000000001}, 0};
312 static constexpr float16_t MACHINE_EPSILON{uint16_t{0b0'00101'0000000000}, 0};
313 static constexpr float16_t INF{uint16_t{0b0'11111'0000000000}, 0};
314 static constexpr float16_t QUIET_NAN{uint16_t{0b0'11111'1000000000}, 0};
315};
316
317namespace detail {
318
319template <typename T>
320struct NormalizedInteger {
321 T _private_value{};
322
323 GREM_ALWAYS_INLINE NormalizedInteger() noexcept = default;
324
325 GREM_ALWAYS_INLINE constexpr NormalizedInteger(float value) noexcept
326 : _private_value(encode(value)) {}
327
328 constexpr operator float() const noexcept {
329 return decode(_private_value);
330 }
331
332 [[nodiscard]] GREM_ALWAYS_INLINE constexpr bool operator==(const NormalizedInteger& other) const noexcept {
333 return _private_value == other._private_value;
334 }
335
336 [[nodiscard]] GREM_ALWAYS_INLINE constexpr bool operator==(float other) const noexcept {
337 return static_cast<float>(*this) == other;
338 }
339
340 [[nodiscard]] GREM_ALWAYS_INLINE constexpr std::strong_ordering operator<=>(const NormalizedInteger& other) const noexcept {
341 return _private_value <=> other._private_value;
342 }
343
344 [[nodiscard]] GREM_ALWAYS_INLINE constexpr std::partial_ordering operator<=>(float other) const noexcept {
345 return static_cast<float>(*this) <=> other;
346 }
347
348private:
349 friend Limits<NormalizedInteger<T>>;
350
351 [[nodiscard]] static constexpr T encode(float value) noexcept {
352 if constexpr (signed_integral<T>) {
353 value = (value < -1.0f) ? -1.0f : (1.0f < value) ? 1.0f : value;
354 return static_cast<T>((value < 0.0f) ? value * static_cast<float>(Limits<T>::MAX) - 0.5f : value * static_cast<float>(Limits<T>::MAX) + 0.5f);
355 } else {
356 value = (value < 0.0f) ? 0.0f : (1.0f < value) ? 1.0f : value;
357 return static_cast<T>(value * static_cast<float>(Limits<T>::MAX) + 0.5f);
358 }
359 }
360
361 [[nodiscard]] static constexpr float decode(T value) noexcept {
362 if constexpr (signed_integral<T>) {
363 const float result = static_cast<float>(value) / static_cast<float>(Limits<T>::MAX);
364 return (result < -1.0f) ? -1.0f : result;
365 } else {
366 return static_cast<float>(value) / static_cast<float>(Limits<T>::MAX);
367 }
368 }
369
370 GREM_ALWAYS_INLINE constexpr NormalizedInteger(T value, int)
371 : _private_value(value) {}
372};
373
374} // namespace detail
375
376using i8norm = detail::NormalizedInteger<int8_t>;
377using u8norm = detail::NormalizedInteger<uint8_t>;
378using i16norm = detail::NormalizedInteger<int16_t>;
379using u16norm = detail::NormalizedInteger<uint16_t>;
380
381template <typename T>
382struct Limits<detail::NormalizedInteger<T>> {
383 static constexpr detail::NormalizedInteger<T> MIN{Limits<T>::MIN, 0};
384 static constexpr detail::NormalizedInteger<T> MAX{Limits<T>::MAX, 0};
385};
386
394template <strict_integral T>
395[[nodiscard]] GREM_ALWAYS_INLINE constexpr T getBitReversed(T value) {
396 static_assert(sizeof(T) <= 8);
397 if constexpr (sizeof(T) == 1) {
398 uint8_t x = bit_cast<uint8_t>(value);
399 x = static_cast<uint8_t>(((x & 0x55) << 1) | ((x & 0xAA) >> 1));
400 x = static_cast<uint8_t>(((x & 0x33) << 2) | ((x & 0xCC) >> 2));
401 x = static_cast<uint8_t>(((x & 0x0F) << 4) | ((x & 0xF0) >> 4));
402 return bit_cast<T>(x);
403 } else if constexpr (sizeof(T) == 2) {
404 uint16_t x = bit_cast<uint16_t>(value);
405 x = static_cast<uint16_t>(((x & 0x5555) << 1) | ((x & 0xAAAA) >> 1));
406 x = static_cast<uint16_t>(((x & 0x3333) << 2) | ((x & 0xCCCC) >> 2));
407 x = static_cast<uint16_t>(((x & 0x0F0F) << 4) | ((x & 0xF0F0) >> 4));
408 x = static_cast<uint16_t>(((x & 0x00FF) << 8) | ((x & 0xFF00) >> 8));
409 return bit_cast<T>(x);
410 } else if constexpr (sizeof(T) == 4) {
411 uint32_t x = bit_cast<uint32_t>(value);
412 x = ((x & 0x55555555) << 1) | ((x & 0xAAAAAAAA) >> 1);
413 x = ((x & 0x33333333) << 2) | ((x & 0xCCCCCCCC) >> 2);
414 x = ((x & 0x0F0F0F0F) << 4) | ((x & 0xF0F0F0F0) >> 4);
415 x = ((x & 0x00FF00FF) << 8) | ((x & 0xFF00FF00) >> 8);
416 x = ((x & 0x0000FFFF) << 16) | ((x & 0xFFFF0000) >> 16);
417 return bit_cast<T>(x);
418 } else if constexpr (sizeof(T) == 8) {
419 uint64_t x = bit_cast<uint64_t>(value);
420 x = ((x & 0x5555555555555555ull) << 1) | ((x & 0xAAAAAAAAAAAAAAAAull) >> 1);
421 x = ((x & 0x3333333333333333ull) << 2) | ((x & 0xCCCCCCCCCCCCCCCCull) >> 2);
422 x = ((x & 0x0F0F0F0F0F0F0F0Full) << 4) | ((x & 0xF0F0F0F0F0F0F0F0ull) >> 4);
423 x = ((x & 0x00FF00FF00FF00FFull) << 8) | ((x & 0xFF00FF00FF00FF00ull) >> 8);
424 x = ((x & 0x0000FFFF0000FFFFull) << 16) | ((x & 0xFFFF0000FFFF0000ull) >> 16);
425 x = ((x & 0x00000000FFFFFFFFull) << 32) | ((x & 0xFFFFFFFF00000000ull) >> 32);
426 return bit_cast<T>(x);
427 }
428}
429
437template <typename T>
438[[nodiscard]] GREM_ALWAYS_INLINE constexpr T getByteReversed(T value) {
439 if constexpr (same_as<T, int8_t> || same_as<T, uint8_t>) {
440 return value;
441 } else if constexpr (same_as<T, int16_t> || same_as<T, uint16_t>) {
442 uint16_t x = bit_cast<uint16_t>(value);
443 x = static_cast<uint16_t>(((x & 0x00FF) << 8) | ((x & 0xFF00) >> 8));
444 return bit_cast<T>(x);
445 } else if constexpr (same_as<T, int32_t> || same_as<T, uint32_t>) {
446 uint32_t x = bit_cast<uint32_t>(value);
447 x = ((x & 0x000000FF) << 24) | ((x & 0x0000FF00) << 8) | ((x & 0x00FF0000) >> 8) | ((x & 0xFF000000) >> 24);
448 return bit_cast<T>(x);
449 } else if constexpr (same_as<T, int64_t> || same_as<T, uint64_t>) {
450 uint64_t x = bit_cast<uint64_t>(value);
451 x = (x & 0x00000000FFFFFFFFull) << 32 | (x & 0xFFFFFFFF00000000ull) >> 32;
452 x = (x & 0x0000FFFF0000FFFFull) << 16 | (x & 0xFFFF0000FFFF0000ull) >> 16;
453 x = (x & 0x00FF00FF00FF00FFull) << 8 | (x & 0xFF00FF00FF00FF00ull) >> 8;
454 return bit_cast<T>(x);
455 } else {
456 const ValueRepresentation<T> raw = bit_cast<ValueRepresentation<T>>(value);
457 ValueRepresentation<T> reversed;
458 for (size_t i = 0; i < sizeof(T); ++i) {
459 reversed.bytes[i] = raw.bytes[sizeof(T) - 1 - i];
460 }
461 return bit_cast<T>(reversed);
462 }
463}
464
468inline constexpr bool HOST_IS_LITTLE_ENDIAN = std::endian::native == std::endian::little;
469
473inline constexpr bool HOST_IS_BIG_ENDIAN = std::endian::native == std::endian::big;
474
485template <typename T>
486[[nodiscard]] GREM_ALWAYS_INLINE constexpr T convertHostEndianToBigEndian(T value) {
487 if constexpr (HOST_IS_BIG_ENDIAN) {
488 return value;
489 } else {
490 return getByteReversed(value);
491 }
492}
493
504template <typename T>
505[[nodiscard]] GREM_ALWAYS_INLINE constexpr T convertHostEndianToLittleEndian(T value) {
506 if constexpr (HOST_IS_LITTLE_ENDIAN) {
507 return value;
508 } else {
509 return getByteReversed(value);
510 }
511}
512
522template <typename T>
523[[nodiscard]] GREM_ALWAYS_INLINE constexpr T convertBigEndianToHostEndian(T value) {
524 if constexpr (HOST_IS_BIG_ENDIAN) {
525 return value;
526 } else {
527 return getByteReversed(value);
528 }
529}
530
541template <typename T>
542[[nodiscard]] GREM_ALWAYS_INLINE constexpr T convertLittleEndianToHostEndian(T value) {
543 if constexpr (HOST_IS_LITTLE_ENDIAN) {
544 return value;
545 } else {
546 return getByteReversed(value);
547 }
548}
549
558template <strict_arithmetic T>
559[[nodiscard]] GREM_ALWAYS_INLINE constexpr T midpoint(T a, T b) {
560 if constexpr (floating_point<T>) {
561 return (a + b) * T{0.5};
562 } else {
563 using U = std::make_unsigned_t<T>;
564 int sign = 1;
565 U minValue = static_cast<U>(a);
566 U maxValue = static_cast<U>(b);
567 if (a > b) {
568 sign = -1;
569 minValue = static_cast<U>(b);
570 maxValue = static_cast<U>(a);
571 }
572 return static_cast<T>(a + sign * static_cast<T>(static_cast<U>(maxValue - minValue) / 2));
573 }
574}
575
584template <typename A, typename B>
585[[nodiscard]] GREM_ALWAYS_INLINE constexpr decltype(auto) min(A&& a, B&& b) requires(requires(A x, B y) { x < y; }) {
586 return (b < a) ? std::forward<B>(b) : std::forward<A>(a);
587}
588
597template <typename A, typename B>
598[[nodiscard]] GREM_ALWAYS_INLINE constexpr decltype(auto) max(A&& a, B&& b) requires(requires(A x, B y) { x < y; }) {
599 return (a < b) ? std::forward<B>(b) : std::forward<A>(a);
600}
601
611template <typename A, typename B>
612[[nodiscard]] GREM_ALWAYS_INLINE constexpr auto minmax(A&& a, B&& b) requires(requires(A x, B y) { x < y; }) {
613 using T = std::common_type_t<A, B>;
614 return (b < a) ? Pair<T>(std::forward<B>(b), std::forward<A>(a)) : Pair<T>(std::forward<A>(a), std::forward<B>(b));
615}
616
629template <typename T, typename Low, typename High>
630[[nodiscard]] GREM_ALWAYS_INLINE constexpr decltype(auto) clamp(T&& value, Low&& minValue, High&& maxValue)
631 requires(requires(T v, Low l) { v < l; } && requires(T v, High h) { h < v; }) {
632 GREM_ASSERT(minValue <= maxValue);
633 return (value < minValue) ? std::forward<Low>(minValue) : (maxValue < value) ? std::forward<High>(maxValue) : std::forward<T>(value);
634}
635
644template <strict_unsigned_integral UnsignedInteger>
645[[nodiscard]] GREM_ALWAYS_INLINE constexpr UnsignedInteger rotateBitsLeft(UnsignedInteger value, int shift) noexcept {
646 return std::rotl(value, shift);
647}
648
657template <strict_unsigned_integral UnsignedInteger>
658[[nodiscard]] GREM_ALWAYS_INLINE constexpr UnsignedInteger rotateBitsRight(UnsignedInteger value, int shift) noexcept {
659 return std::rotr(value, shift);
660}
661
669template <strict_unsigned_integral UnsignedInteger>
670[[nodiscard]] GREM_ALWAYS_INLINE constexpr int countOneBits(UnsignedInteger value) noexcept {
671 return std::popcount(value);
672}
673
681template <strict_unsigned_integral UnsignedInteger>
682[[nodiscard]] GREM_ALWAYS_INLINE constexpr int countZeroBits(UnsignedInteger value) noexcept {
683 return static_cast<int>(sizeof(UnsignedInteger) * BYTE_BITS) - countOneBits(value);
684}
685
695template <strict_unsigned_integral UnsignedInteger>
696[[nodiscard]] GREM_ALWAYS_INLINE constexpr int countLeadingZeroBits(UnsignedInteger value) noexcept {
697 return std::countl_zero(value);
698}
699
709template <strict_unsigned_integral UnsignedInteger>
710[[nodiscard]] GREM_ALWAYS_INLINE constexpr int countLeadingOneBits(UnsignedInteger value) noexcept {
711 return std::countl_one(value);
712}
713
723template <strict_unsigned_integral UnsignedInteger>
724[[nodiscard]] GREM_ALWAYS_INLINE constexpr int countTrailingZeroBits(UnsignedInteger value) noexcept {
725 return std::countr_zero(value);
726}
727
737template <strict_unsigned_integral UnsignedInteger>
738[[nodiscard]] GREM_ALWAYS_INLINE constexpr int countTrailingOneBits(UnsignedInteger value) noexcept {
739 return std::countr_one(value);
740}
741
749template <strict_unsigned_integral UnsignedInteger>
750[[nodiscard]] GREM_ALWAYS_INLINE constexpr int getRequiredBitCount(UnsignedInteger value) noexcept {
751 return std::bit_width(value);
752}
753
764template <strict_unsigned_integral UnsignedInteger>
765[[nodiscard]] GREM_ALWAYS_INLINE constexpr UnsignedInteger getMaxValueForBits(size_t bits) noexcept {
766 return (bits < sizeof(UnsignedInteger) * BYTE_BITS) ? (UnsignedInteger{1} << bits) - 1 : Limits<UnsignedInteger>::MAX;
767}
768
780template <strict_unsigned_integral UnsignedInteger>
781[[nodiscard]] GREM_ALWAYS_INLINE constexpr bool isPowerOf2(UnsignedInteger value) noexcept {
782 return std::has_single_bit(value);
783}
784
793template <strict_unsigned_integral UnsignedInteger>
794[[nodiscard]] GREM_ALWAYS_INLINE constexpr UnsignedInteger roundUpToPowerOf2(UnsignedInteger value) noexcept {
795 const int bits = static_cast<int>(sizeof(UnsignedInteger) * BYTE_BITS);
796 const int shift = bits - countLeadingZeroBits(static_cast<UnsignedInteger>(value - UnsignedInteger{1}));
797 return (shift >= bits) ? 0 : UnsignedInteger{1} << shift;
798}
799
808template <strict_unsigned_integral UnsignedInteger>
809[[nodiscard]] GREM_ALWAYS_INLINE constexpr UnsignedInteger roundDownToPowerOf2(UnsignedInteger value) noexcept {
810 return (value == 0) ? 0 : UnsignedInteger{1} << (getRequiredBitCount(value) - 1);
811}
812
821template <strict_unsigned_integral UnsignedInteger>
822[[nodiscard]] GREM_ALWAYS_INLINE constexpr UnsignedInteger roundUpToMultiple(UnsignedInteger value, UnsignedInteger n) {
823 const UnsignedInteger remainder = value % n;
824 const UnsignedInteger correction = (remainder == 0) ? 0 : n - remainder;
825 return value + correction;
826}
827
828} // namespace grem
829
830#endif
#define GREM_ASSERT(expression)
Definition assertions.hpp:77
#define GREM_ALWAYS_INLINE
Definition attributes.hpp:10
Definition fundamentals.hpp:86
GREM_ALWAYS_INLINE bool16_t() noexcept=default
Definition fundamentals.hpp:109
GREM_ALWAYS_INLINE bool32_t() noexcept=default
Definition fundamentals.hpp:132
GREM_ALWAYS_INLINE bool64_t() noexcept=default
Definition concepts.hpp:85
Definition concepts.hpp:35
@ T
't'
Definition keyboard.hpp:90
Definition aliases.hpp:9
GREM_ALWAYS_INLINE constexpr T midpoint(T a, T b)
Get the midpoint between two values.
Definition fundamentals.hpp:559
constexpr bool64_t true64
Definition fundamentals.hpp:147
GREM_ALWAYS_INLINE constexpr T getBitReversed(T value)
Get a copy of an integer with its bit order reversed.
Definition fundamentals.hpp:395
constexpr bool16_t true16
Definition fundamentals.hpp:101
detail::NormalizedInteger< int16_t > i16norm
Definition fundamentals.hpp:378
GREM_ALWAYS_INLINE constexpr int countLeadingOneBits(UnsignedInteger value) noexcept
Get the number of consecutive 1 bits starting at the most significant (leftmost) bit of a given value...
Definition fundamentals.hpp:710
constexpr bool HOST_IS_LITTLE_ENDIAN
Whether the host system's native byte order is little-endian or not.
Definition fundamentals.hpp:468
GREM_ALWAYS_INLINE constexpr int getRequiredBitCount(UnsignedInteger value) noexcept
Get the minimum number of bits required to represent a given value.
Definition fundamentals.hpp:750
GREM_ALWAYS_INLINE constexpr auto minmax(A &&a, B &&b)
Get the smaller and greater of two values.
Definition fundamentals.hpp:612
GREM_ALWAYS_INLINE constexpr T convertHostEndianToLittleEndian(T value)
Convert a value in host-endian byte order to the corresponding value in little-endian byte order.
Definition fundamentals.hpp:505
GREM_ALWAYS_INLINE constexpr T convertHostEndianToBigEndian(T value)
Convert a value in host-endian byte order to the corresponding value in big-endian byte order.
Definition fundamentals.hpp:486
GREM_ALWAYS_INLINE constexpr decltype(auto) clamp(T &&value, Low &&minValue, High &&maxValue)
Clamp a value to a specific interval.
Definition fundamentals.hpp:630
std::make_signed_t< size_t > ssize_t
Definition fundamentals.hpp:60
GREM_ALWAYS_INLINE constexpr UnsignedInteger rotateBitsRight(UnsignedInteger value, int shift) noexcept
Get the result of bitwise right-rotating a value by a number of bits.
Definition fundamentals.hpp:658
float float32_t
Definition fundamentals.hpp:155
constexpr bool64_t false64
Definition fundamentals.hpp:146
GREM_ALWAYS_INLINE constexpr T convertBigEndianToHostEndian(T value)
Convert a value in big-endian byte order to the corresponding value in host-endian byte order.
Definition fundamentals.hpp:523
@ false8
Definition fundamentals.hpp:77
@ true8
Definition fundamentals.hpp:77
GREM_ALWAYS_INLINE constexpr bool isPowerOf2(UnsignedInteger value) noexcept
Check if a given integer is a power of 2, i.e. if the number appears in the infinite sequence 2^0,...
Definition fundamentals.hpp:781
double float64_t
Definition fundamentals.hpp:157
constexpr bool32_t true32
Definition fundamentals.hpp:124
constexpr bool16_t false16
Definition fundamentals.hpp:100
GREM_ALWAYS_INLINE constexpr int countLeadingZeroBits(UnsignedInteger value) noexcept
Get the number of consecutive 0 bits starting at the most significant (leftmost) bit of a given value...
Definition fundamentals.hpp:696
detail::NormalizedInteger< uint8_t > u8norm
Definition fundamentals.hpp:377
detail::NormalizedInteger< uint16_t > u16norm
Definition fundamentals.hpp:379
constexpr bool HOST_IS_BIG_ENDIAN
Whether the host system's native byte order is big-endian or not.
Definition fundamentals.hpp:473
GREM_ALWAYS_INLINE constexpr UnsignedInteger roundUpToMultiple(UnsignedInteger value, UnsignedInteger n)
Get the smallest multiple of n that is not greater than a given value.
Definition fundamentals.hpp:822
detail::NormalizedInteger< int8_t > i8norm
Definition fundamentals.hpp:376
GREM_ALWAYS_INLINE constexpr UnsignedInteger roundDownToPowerOf2(UnsignedInteger value) noexcept
Get the largest integral power of 2 that is not greater than a given value.
Definition fundamentals.hpp:809
GREM_ALWAYS_INLINE constexpr UnsignedInteger getMaxValueForBits(size_t bits) noexcept
Get the maximum value that can be represented in a given unsigned integer type using a given number o...
Definition fundamentals.hpp:765
GREM_ALWAYS_INLINE constexpr UnsignedInteger rotateBitsLeft(UnsignedInteger value, int shift) noexcept
Get the result of bitwise left-rotating a value by a number of bits.
Definition fundamentals.hpp:645
Pair(T1, T2) -> Pair< T1, T2 >
GREM_ALWAYS_INLINE constexpr UnsignedInteger roundUpToPowerOf2(UnsignedInteger value) noexcept
Get the smallest integral power of 2 that is not less than a given value.
Definition fundamentals.hpp:794
GREM_ALWAYS_INLINE constexpr T getByteReversed(T value)
Get a copy of a value with its byte order reversed.
Definition fundamentals.hpp:438
GREM_ALWAYS_INLINE constexpr int countOneBits(UnsignedInteger value) noexcept
Get the number of 1 bits in a given value.
Definition fundamentals.hpp:670
GREM_ALWAYS_INLINE constexpr decltype(auto) max(A &&a, B &&b)
Get the greater of two values, or the first value if they are equivalent.
Definition fundamentals.hpp:598
bool bool8_t
Definition fundamentals.hpp:76
constexpr bool32_t false32
Definition fundamentals.hpp:123
GREM_ALWAYS_INLINE constexpr T convertLittleEndianToHostEndian(T value)
Convert a value in little-endian byte order to the corresponding value in host-endian byte order.
Definition fundamentals.hpp:542
GREM_ALWAYS_INLINE constexpr decltype(auto) min(A &&a, B &&b)
Get the smaller of two values, or the first value if they are equivalent.
Definition fundamentals.hpp:585
GREM_ALWAYS_INLINE constexpr T GREM_VECTORCALL sign(T a)
Get the sign multiplier of a scalar.
Definition math.hpp:1204
GREM_ALWAYS_INLINE constexpr int countZeroBits(UnsignedInteger value) noexcept
Get the number of 0 bits in a given value.
Definition fundamentals.hpp:682
GREM_ALWAYS_INLINE constexpr int countTrailingZeroBits(UnsignedInteger value) noexcept
Get the number of consecutive 0 bits starting at the least significant (rightmost) bit of a given val...
Definition fundamentals.hpp:724
GREM_ALWAYS_INLINE constexpr int countTrailingOneBits(UnsignedInteger value) noexcept
Get the number of consecutive 1 bits starting at the least significant (rightmost) bit of a given val...
Definition fundamentals.hpp:738
constexpr size_t BYTE_BITS
Definition fundamentals.hpp:67
static constexpr T MAX
Definition fundamentals.hpp:36
static constexpr bool IS_IEC60559
Definition fundamentals.hpp:34
static constexpr T SMALLEST_NORMAL
Definition fundamentals.hpp:37
static constexpr T SMALLEST_SUBNORMAL
Definition fundamentals.hpp:38
static constexpr T MACHINE_EPSILON
Definition fundamentals.hpp:39
static constexpr T MIN
Definition fundamentals.hpp:35
static constexpr T QUIET_NAN
Definition fundamentals.hpp:41
static constexpr T INF
Definition fundamentals.hpp:40
static constexpr bool16_t MAX
Definition fundamentals.hpp:106
static constexpr bool16_t MIN
Definition fundamentals.hpp:105
static constexpr bool32_t MAX
Definition fundamentals.hpp:129
static constexpr bool32_t MIN
Definition fundamentals.hpp:128
static constexpr bool64_t MAX
Definition fundamentals.hpp:152
static constexpr bool64_t MIN
Definition fundamentals.hpp:151
static constexpr bool8_t MAX
Definition fundamentals.hpp:83
static constexpr bool8_t MIN
Definition fundamentals.hpp:82
static constexpr float16_t QUIET_NAN
Definition fundamentals.hpp:314
static constexpr float16_t SMALLEST_SUBNORMAL
Definition fundamentals.hpp:311
static constexpr float16_t INF
Definition fundamentals.hpp:313
static constexpr bool IS_IEC60559
Definition fundamentals.hpp:307
static constexpr float16_t MIN
Definition fundamentals.hpp:308
static constexpr float16_t SMALLEST_NORMAL
Definition fundamentals.hpp:310
static constexpr float16_t MACHINE_EPSILON
Definition fundamentals.hpp:312
static constexpr float16_t MAX
Definition fundamentals.hpp:309
Definition fundamentals.hpp:27
static constexpr T MAX
Definition fundamentals.hpp:29
static constexpr T MIN
Definition fundamentals.hpp:28
Definition fundamentals.hpp:70
byte bytes[sizeof(T)]
Definition fundamentals.hpp:71
constexpr bool operator==(const ValueRepresentation &) const noexcept=default
Definition fundamentals.hpp:160
GREM_ALWAYS_INLINE constexpr bool operator==(const float16_t &other) const noexcept
Definition fundamentals.hpp:209
GREM_ALWAYS_INLINE constexpr std::partial_ordering operator<=>(float other) const noexcept
Definition fundamentals.hpp:221
GREM_ALWAYS_INLINE constexpr std::partial_ordering operator<=>(const float16_t &other) const noexcept
Definition fundamentals.hpp:217
GREM_ALWAYS_INLINE constexpr bool operator==(float other) const noexcept
Definition fundamentals.hpp:213
GREM_ALWAYS_INLINE float16_t() noexcept=default