native 0.0.1
Vectors, masks and wide register packs for C++26
Loading...
Searching...
No Matches
native.numerics.ccm
1// SPDX-FileCopyrightText: 2024 Edward Kmett <ekmett@gmail.com>
2// SPDX-License-Identifier: BSD-2-Clause OR Apache-2.0
4module;
5#include "native/isa_import.h"
6#include <bit>
7#include <cmath>
8#include <compare>
9#include <concepts>
10#include <cstddef>
11#include <cstdint>
12#include <limits>
13#include <type_traits>
14#include <utility>
15#include <hint.h>
16#include "native/simd/common.h"
17
18#if !defined(NATIVE_HAS_NATIVE_FP16) || !defined(NATIVE_HAS_NATIVE_BF16)
19#error Configure native half capabilities before compiling native.numerics.
20#endif
21
22export module native.numerics;
23export import native.types;
24
25export namespace native {
26
34struct fp16 {
35private:
36 // Round binary32 to binary16, ties to even, independently of the FP controls.
37 // Raw native-half projections below remain representation copies.
38 static constexpr uint16_t encode(float value) noexcept {
39 auto word = std::bit_cast<uint32_t>(value);
40 auto sign = (word >> 16) & 0x8000u;
41 auto a = word & 0x7fffffffu;
42 if (a >= 0x7f800000u)
43 return uint16_t(sign | 0x7c00u | (a == 0x7f800000u ? 0u : 0x200u | ((a >> 13) & 0x3ffu)));
44 if (a >= 0x47800000u) return uint16_t(sign | 0x7c00u);
45 if (a >= 0x38800000u)
46 return uint16_t(sign | (((a + 0xfffu + ((a >> 13) & 1u)) >> 13) - 0x1c000u));
47 if (a < 0x33000000u) return uint16_t(sign);
48 auto shift = 126u - (a >> 23);
49 auto significand = (a & 0x7fffffu) | 0x800000u;
50 auto result = significand >> shift;
51 auto remainder = significand & ((1u << shift) - 1u);
52 auto half = 1u << (shift - 1u);
53 result += remainder > half || (remainder == half && (result & 1u));
54 return uint16_t(sign | result);
55 }
56 static constexpr float decode(uint16_t word) noexcept {
57 auto sign = uint32_t(word & 0x8000u) << 16;
58 auto exponent = (word >> 10) & 31u;
59 auto fraction = uint32_t(word & 0x3ffu);
60 uint32_t result;
61 if (exponent == 0) {
62 if (fraction == 0) return std::bit_cast<float>(sign);
63 auto shift = unsigned(std::countl_zero(fraction)) - 21u;
64 result = ((113u - shift) << 23) | (((fraction << shift) & 0x3ffu) << 13);
65 } else if (exponent == 31) {
66 result = 0x7f800000u | (fraction << 13) | (fraction ? 0x400000u : 0u);
67 } else result = ((exponent + 112u) << 23) | (fraction << 13);
68 return std::bit_cast<float>(sign | result);
69 }
70public:
72#if NATIVE_HAS_NATIVE_FP16
73 using underlying_type = _Float16;
74#else
75 using underlying_type = uint16_t;
76#endif
77
80
82 hint_inline hint_artificial
83 constexpr fp16() noexcept = default;
84
86 hint_inline hint_artificial
87 constexpr fp16(hint_noescape fp16 const &) noexcept = default;
88
90 hint_inline hint_artificial
91 constexpr fp16(hint_noescape fp16 &&) noexcept = default;
92
94 hint_inline hint_artificial
95 constexpr fp16(float value) noexcept : content(std::bit_cast<underlying_type>(encode(value))) {}
96
97#if NATIVE_HAS_NATIVE_FP16
99 hint_inline hint_artificial
100 constexpr fp16(_Float16 content) noexcept : content(content) {}
101#endif
102
104 [[nodiscard]] hint_inline hint_artificial
105 constexpr operator float (this fp16 self) noexcept { return decode(self.to_bits()); }
106
107#if NATIVE_HAS_NATIVE_FP16
109 [[nodiscard]] hint_inline hint_artificial
110 constexpr operator _Float16 (this fp16 self) noexcept { return self.content; }
111#endif
112
114 hint_reinitializes hint_inline hint_artificial
115 constexpr fp16 & operator = (hint_noescape fp16 const &) noexcept
116 hint_lifetimebound = default;
117
119 hint_reinitializes hint_inline hint_artificial
120 constexpr fp16 & operator = (hint_noescape fp16 &&) noexcept
121 hint_lifetimebound = default;
122
124 [[nodiscard]] hint_inline hint_artificial hint_const
125 friend constexpr bool operator == (fp16 x, fp16 y) noexcept {
126 return static_cast<float>(x) == static_cast<float>(y);
127 }
128
130 [[nodiscard]] hint_inline hint_artificial hint_const
131 friend constexpr bool operator != (fp16 x, fp16 y) noexcept {
132 return static_cast<float>(x) != static_cast<float>(y);
133 }
134
136 [[nodiscard]] hint_inline hint_artificial hint_const
137 friend constexpr std::partial_ordering operator <=> (fp16 x, fp16 y) noexcept {
138 return static_cast<float>(x) <=> static_cast<float>(y);
139 }
140
142 [[nodiscard]] hint_inline hint_artificial hint_const
143 friend constexpr bool operator < (fp16 x, fp16 y) noexcept {
144 return static_cast<float>(x) < static_cast<float>(y);
145 }
146
148 [[nodiscard]] hint_inline hint_artificial hint_const
149 friend constexpr bool operator <= (fp16 x, fp16 y) noexcept {
150 return static_cast<float>(x) <= static_cast<float>(y);
151 }
152
153 [[nodiscard]] hint_inline hint_artificial hint_const
154 friend constexpr bool operator > (fp16 x, fp16 y) noexcept {
155 return static_cast<float>(x) > static_cast<float>(y);
156 }
157
159 [[nodiscard]] hint_inline hint_artificial hint_const
160 friend constexpr bool operator >= (fp16 x, fp16 y) noexcept {
161 return static_cast<float>(x) >= static_cast<float>(y);
162 }
163
165 hint_inline hint_artificial
166 friend constexpr void swap(
167 hint_noescape fp16 & x,
168 hint_noescape fp16 & y
169 ) noexcept {
170 using std::swap;
171 swap(x.content,y.content);
172 }
173
175 hint_inline hint_artificial hint_const
176 static constexpr fp16 from_bits(uint16_t data) noexcept {
177 return std::bit_cast<fp16>(data);
178 }
179
181 [[nodiscard]] hint_inline hint_const
182 constexpr uint16_t to_bits(this fp16 self) noexcept {
183 return std::bit_cast<uint16_t>(self.content);
184 }
185
186};
187
190template<> struct mask_traits<fp16> { using type = bool; };
191
193[[nodiscard]] hint_inline hint_artificial hint_const
194constexpr fp16 operator""_fp16(long double v) noexcept {
195 return fp16(static_cast<float>(v));
196}
197
198} // namespace native
199
202export namespace std {
203 using ::std::numeric_limits;
205 [[nodiscard]] hint_artificial hint_inline hint_const
206 constexpr bool isnan(::native::fp16 x) noexcept {
207 // Sign doesn't matter, frac not zero (infinity)
208 return (x.to_bits() & 0x7FFF) > 0x7c00;
209 }
210
213 template <>
214 class numeric_limits<::native::fp16> {
215 public:
216 static constexpr bool is_specialized = true;
218 hint_inline hint_artificial hint_const
219 static constexpr ::native::fp16 min() noexcept {
220 return ::native::fp16::from_bits(0x0400);
221 }
222
223 hint_inline hint_artificial hint_const
224 static constexpr ::native::fp16 max() noexcept {
225 return ::native::fp16::from_bits(0x7BFF);
226 }
227
228 hint_inline hint_artificial hint_const
229 static constexpr ::native::fp16 lowest() noexcept {
230 return ::native::fp16::from_bits(0xFBFF);
231 }
232 static constexpr int digits = 11;
233 static constexpr int digits10 = 3;
234 static constexpr int max_digits10 = 5;
235 static constexpr bool is_signed = true;
236 static constexpr bool is_integer = false;
237 static constexpr bool is_exact = false;
238 static constexpr int radix = 2;
240 hint_inline hint_artificial hint_const
241 static constexpr ::native::fp16 epsilon() noexcept {
242 return ::native::fp16::from_bits(0x1400);
243 }
244
245 hint_inline hint_artificial hint_const
246 static constexpr ::native::fp16 round_error() noexcept {
247 return ::native::fp16::from_bits(0x3800);
248 }
249 static constexpr int min_exponent = -13;
250 static constexpr int min_exponent10 = -4;
251 static constexpr int max_exponent = 16;
252 static constexpr int max_exponent10 = 4;
253 static constexpr bool has_infinity = true;
254 static constexpr bool has_quiet_NaN = true;
255 static constexpr bool has_signaling_NaN = true;
256 [[deprecated("std::float_denorm_style is deprecated since C++23")]]
257 static constexpr float_denorm_style has_denorm = denorm_present;
258 [[deprecated("numeric_limits::has_denorm_loss is deprecated since C++23")]]
259 static constexpr bool has_denorm_loss = false;
261 hint_inline hint_artificial hint_const
262 static constexpr ::native::fp16 infinity() noexcept {
263 return ::native::fp16::from_bits(0x7C00);
264 }
265
266 hint_inline hint_artificial hint_const
267 static constexpr ::native::fp16 quiet_NaN() noexcept {
268 return ::native::fp16::from_bits(0x7FFF);
269 }
270
271 hint_inline hint_artificial hint_const
272 static constexpr ::native::fp16 signaling_NaN() noexcept {
273 return ::native::fp16::from_bits(0x7DFF);
274 }
275
276 hint_inline hint_artificial hint_const
277 static constexpr ::native::fp16 denorm_min() noexcept {
278 return ::native::fp16::from_bits(1);
279 }
280 static constexpr bool is_iec559 = false;
281 static constexpr bool is_bounded = true;
282 static constexpr bool is_modulo = false;
283 static constexpr bool traps = false;
284 static constexpr bool tinyness_before = false;
285 static constexpr float_round_style round_style = round_to_nearest;
286 };
287
288
289
290} // namespace std
291
292
293export namespace native {
294
300struct bf16 {
301private:
302 // Conversion is integer RNE, including subnormals, under every FP environment.
303 static constexpr uint16_t encode(float value) noexcept {
304 auto word = std::bit_cast<uint32_t>(value);
305 if ((word & 0x7fffffffu) > 0x7f800000u)
306 return uint16_t((word >> 16) | 0x40u);
307 return uint16_t((word + 0x7fffu + ((word >> 16) & 1u)) >> 16);
308 }
309 static constexpr float decode(uint16_t word) noexcept {
310 auto result = uint32_t(word) << 16;
311 if ((word & 0x7fffu) > 0x7f80u) result |= 0x400000u;
312 return std::bit_cast<float>(result);
313 }
314public:
316 // A native __bf16 field lets optimized aggregate copies widen and narrow
317 // through FP32 on Clang x86, changing encodings during storage operations.
318 using underlying_type = uint16_t;
319
322
324 hint_inline hint_artificial constexpr
325 bf16() noexcept = default;
326
328 hint_inline hint_artificial constexpr
329 bf16(float f) noexcept : content(encode(f)) {}
330
331#if NATIVE_HAS_NATIVE_BF16
333 hint_inline hint_artificial constexpr
334 bf16(__bf16 f) noexcept : content(std::bit_cast<uint16_t>(f)) {}
335#endif
336
338 hint_inline hint_artificial constexpr
339 bf16(hint_noescape bf16 const &) noexcept = default;
340
342 hint_inline hint_artificial constexpr
343 bf16(hint_noescape bf16 &&) noexcept = default;
344
345#if NATIVE_HAS_NATIVE_BF16
347 [[nodiscard]] hint_inline hint_artificial hint_const constexpr
348 operator __bf16 (this bf16 self) noexcept { return std::bit_cast<__bf16>(self.content); }
349#endif
350
352 [[nodiscard]] hint_inline hint_artificial hint_const constexpr
353 operator float (this bf16 self) noexcept { return decode(self.to_bits()); }
354
356 hint_reinitializes hint_inline hint_artificial constexpr
357 bf16 & operator = (hint_noescape bf16 const &) noexcept
358 hint_lifetimebound = default;
359
361 hint_reinitializes hint_inline hint_artificial constexpr
362 bf16 & operator = (hint_noescape bf16 &&) noexcept
363 hint_lifetimebound = default;
364
366 [[nodiscard]] hint_inline hint_artificial hint_const
367 friend constexpr bool operator == (bf16 x, bf16 y) noexcept {
368 return static_cast<float>(x) == static_cast<float>(y);
369 }
370
372 [[nodiscard]] hint_inline hint_artificial hint_const
373 friend constexpr std::partial_ordering operator <=> (bf16 x, bf16 y) noexcept {
374 return static_cast<float>(x) <=> static_cast<float>(y);
375 }
376
378 [[nodiscard]] hint_inline hint_artificial hint_const
379 friend constexpr bool operator != (bf16 x, bf16 y) noexcept {
380 return static_cast<float>(x) != static_cast<float>(y);
381 }
382
384 [[nodiscard]] hint_inline hint_artificial hint_const
385 friend constexpr bool operator < (bf16 x, bf16 y) noexcept {
386 return static_cast<float>(x) < static_cast<float>(y);
387 }
388
390 [[nodiscard]] hint_inline hint_artificial hint_const
391 friend constexpr bool operator > (bf16 x, bf16 y) noexcept {
392 return static_cast<float>(x) > static_cast<float>(y);
393 }
394
396 [[nodiscard]] hint_inline hint_artificial hint_const
397 friend constexpr bool operator <= (bf16 x, bf16 y) noexcept {
398 return static_cast<float>(x) <= static_cast<float>(y);
399 }
400
402 [[nodiscard]] hint_inline hint_artificial hint_const
403 friend constexpr bool operator >= (bf16 x, bf16 y) noexcept {
404 return static_cast<float>(x) >= static_cast<float>(y);
405 }
406
408 hint_inline hint_artificial
409 friend constexpr void swap(
410 hint_noescape bf16 & x,
411 hint_noescape bf16 & y
412 ) noexcept {
413 using std::swap;
414 swap(x.content,y.content);
415 }
416
418 [[nodiscard]] hint_inline hint_artificial hint_const
419 static constexpr bf16 from_bits(uint16_t data) noexcept {
420 return std::bit_cast<bf16>(data);
421 }
422
424 [[nodiscard]] hint_inline hint_const
425 constexpr uint16_t to_bits(this bf16 self) noexcept {
426 return std::bit_cast<uint16_t>(self.content);
427 }
428};
429
432template<> struct mask_traits<bf16> { using type = bool; };
433
435[[nodiscard]] hint_inline hint_artificial
436constexpr bf16 operator""_bf16(long double v) noexcept {
437 return bf16(static_cast<float>(v));
438}
439
443[[nodiscard]] hint_inline hint_artificial hint_const
444constexpr bf16 fast_to_bf16(float f) noexcept {
445 if consteval {
446 return f;
447 } else {
448 return std::bit_cast<bf16>(static_cast<uint16_t>(std::bit_cast<uint32_t>(f)>>16));
449 }
450}
451
453[[nodiscard]] hint_inline hint_artificial hint_const
454constexpr bf16 fast_to_bf16(bf16 f) noexcept {
455 return f;
456}
457
458} // namespace native
459
460export namespace std {
461 using ::std::numeric_limits;
463 [[nodiscard]] hint_artificial hint_inline hint_const
464 constexpr bool isnan(::native::bf16 x) noexcept {
465 // Exponent maxed out and fraction non-zero indicates NaN
466 return (x.to_bits() & 0x7FFF) > 0x7F80;
467 }
468
471 template <> class numeric_limits<::native::bf16> {
472 public:
473 static constexpr bool is_specialized = true;
475 [[nodiscard]] hint_inline hint_artificial hint_const
476 static constexpr ::native::bf16 min() noexcept {
477 return ::native::bf16::from_bits(0x0080);
478 }
479
480 [[nodiscard]] hint_inline hint_artificial hint_const
481 static constexpr ::native::bf16 max() noexcept {
482 return ::native::bf16::from_bits(0x7F7F);
483 }
484
485 [[nodiscard]] hint_inline hint_artificial hint_const
486 static constexpr ::native::bf16 lowest() noexcept {
487 return ::native::bf16::from_bits(0xFF7F);
488 }
489 static constexpr int digits = 8;
490 static constexpr int digits10 = 2;
491 static constexpr int max_digits10 = 4;
492 static constexpr bool is_signed = true;
493 static constexpr bool is_integer = false;
494 static constexpr bool is_exact = false;
495 static constexpr int radix = 2;
497 [[nodiscard]] hint_inline hint_artificial hint_const
498 static constexpr ::native::bf16 epsilon() noexcept {
499 return ::native::bf16::from_bits(0x3C00);
500 }
501
502 [[nodiscard]] hint_inline hint_artificial hint_const
503 static constexpr ::native::bf16 round_error() noexcept {
504 return ::native::bf16::from_bits(0x3F00);
505 }
506 static constexpr int min_exponent = -125;
507 static constexpr int min_exponent10 = -37;
508 static constexpr int max_exponent = 128;
509 static constexpr int max_exponent10 = 38;
510 static constexpr bool has_infinity = true;
511 static constexpr bool has_quiet_NaN = true;
512 static constexpr bool has_signaling_NaN = true;
513 [[deprecated("std::float_denorm_style is deprecated since C++23")]]
514 static constexpr float_denorm_style has_denorm = denorm_present;
515 [[deprecated("numeric_limits::has_denorm_loss is deprecated since C++23")]]
516 static constexpr bool has_denorm_loss = false;
518 [[nodiscard]] hint_inline hint_artificial hint_const
519 static constexpr ::native::bf16 infinity() noexcept {
520 return ::native::bf16::from_bits(0x7F80);
521 }
522
523 [[nodiscard]] hint_inline hint_artificial hint_const
524 static constexpr ::native::bf16 quiet_NaN() noexcept {
525 return ::native::bf16::from_bits(0x7FC0);
526 }
527
528 [[nodiscard]] hint_inline hint_artificial hint_const
529 static constexpr ::native::bf16 signaling_NaN() noexcept {
530 return ::native::bf16::from_bits(0x7F81);
531 }
532
533 [[nodiscard]] hint_inline hint_artificial hint_const
534 static constexpr ::native::bf16 denorm_min() noexcept {
535 return ::native::bf16::from_bits(1);
536 }
537 static constexpr bool is_iec559 = false;
538 static constexpr bool is_bounded = true;
539 static constexpr bool is_modulo = false;
540 static constexpr bool traps = false;
541 static constexpr bool tinyness_before = false;
542 static constexpr float_round_style round_style = round_to_nearest;
543 };
544
545
546
547} // namespace std
548
549
550namespace native { using std::isnan; }
551
552export namespace native {
553 using ::native::imm_t;
554 using ::native::imm;
557
559template <auto N, auto ... candidates>
560concept one_of = ((N==candidates) || ... || false);
561
563template <auto N, auto ... candidates>
564concept not_one_of = (!one_of<N,candidates...>);
565
567template <std::size_t N> struct integer_traits {};
568template <> struct integer_traits<8> { using signed_t = int8_t; using unsigned_t = uint8_t; };
569template <> struct integer_traits<16> { using signed_t = int16_t; using unsigned_t = uint16_t; };
570template <> struct integer_traits<32> { using signed_t = int32_t; using unsigned_t = uint32_t; };
571template <> struct integer_traits<64> { using signed_t = int64_t; using unsigned_t = uint64_t; };
573
576template <typename T>
577requires one_of<sizeof(T),1,2,4,8>
578using int_t = typename integer_traits<sizeof(T)*8>::signed_t;
579
582template <typename T>
583requires one_of<sizeof(T),1,2,4,8>
584using uint_t = typename integer_traits<sizeof(T)*8>::unsigned_t;
585
586// The shared immediate tag is defined by native/common.h.
587
594template <typename T>
595[[nodiscard]] hint_inline
596constexpr bool cmp_unord(T a, T b) noexcept(noexcept(isnan(a) || isnan(b))) {
597 return isnan(a) || isnan(b);
598}
599
601
602
604
611template <typename T>
612[[nodiscard]] hint_inline
613constexpr bool cmp_ord(T a, T b) noexcept(noexcept(!isnan(a) && !isnan(b))) {
614 return !isnan(a) && !isnan(b);
615}
616
618
619
621
630template <one_of_t<float, double> T>
631[[nodiscard]] hint_inline hint_pure
632constexpr T scalef(T x, T y) noexcept {
633 if consteval {
634 using U = uint_t<T>;
635 constexpr int fraction_bits = std::numeric_limits<T>::digits - 1;
636 constexpr int bias = std::numeric_limits<T>::max_exponent - 1;
637 constexpr U hidden = U(1) << fraction_bits;
638 constexpr U sign_mask = U(1) << (sizeof(T) * 8 - 1);
639 constexpr U exponent_mask = U(2 * bias + 1) << fraction_bits;
640 auto const word = std::bit_cast<U>(x);
641 auto const sign = word & sign_mask;
642 auto const field = (word & exponent_mask) >> fraction_bits;
643 auto significand = word & (hidden - 1);
644 if (field == U(2 * bias + 1) || (field == 0 && significand == 0)) return x;
645 int const scale = static_cast<int>(y);
646
647 // Probe behavior, not just a library version: some constexpr math facilities
648 // reject range errors during constant evaluation, including signed underflow.
649 if constexpr (requires {
650 typename std::enable_if_t<
651 std::scalbn(T(1), 1 - bias) == std::numeric_limits<T>::min() &&
652 std::scalbn(T(1), 1 - bias - fraction_bits) == std::numeric_limits<T>::denorm_min() &&
653 std::scalbn(T(-1), bias + 1) == -std::numeric_limits<T>::infinity() &&
654 std::bit_cast<U>(std::scalbn(T(-1), -bias - fraction_bits)) == sign_mask &&
655 std::scalbn(T(1), std::numeric_limits<int>::min()) == T(0) &&
656 std::scalbn(T(1), std::numeric_limits<int>::max()) == std::numeric_limits<T>::infinity()>;
657 }) {
658 return std::scalbn(x, scale);
659 } else {
660 // Normalize even the smallest subnormal before changing its exponent.
661 std::int64_t exponent = field ? std::int64_t(field) - bias : 1 - bias;
662 if (field) significand |= hidden;
663 else while (significand < hidden) { significand <<= 1; --exponent; }
664 exponent += scale; // The full int exponent cannot overflow this wider sum.
665 if (exponent > bias) return std::bit_cast<T>(sign | exponent_mask);
666 if (exponent >= 1 - bias) {
667 return std::bit_cast<T>(sign | (U(exponent + bias) << fraction_bits) |
668 (significand & (hidden - 1)));
669 }
670 auto const shift = (1 - bias) - exponent;
671 if (shift > fraction_bits + 1) return std::bit_cast<T>(sign);
672 U const remainder = significand & ((U(1) << shift) - 1);
673 U const halfway = U(1) << (shift - 1);
674 U rounded = significand >> shift;
675 rounded += remainder > halfway || (remainder == halfway && (rounded & 1));
676 // Rounding can carry into the minimum normal; its encoding is hidden.
677 return std::bit_cast<T>(sign | rounded);
678 }
679 } else {
680 return std::scalbn(x, static_cast<int>(y));
681 }
682}
683
687enum class [[nodiscard]] CMPINT : std::size_t {
688 EQ = 0x0uz
689, LT = 0x1uz
690, LE = 0x2uz
691, FALSE = 0x3uz
692, NE = 0x4uz
693, NLT = 0x5uz
694, NLE = 0x6uz
695, TRUE = 0x7uz
696};
697
700template <CMPINT imm8, typename T>
701requires (one_of_t<T, uint8_t, int8_t, uint16_t, int16_t, uint32_t, int32_t, uint64_t, int64_t> && (std::size_t(imm8) < 8uz))
702[[nodiscard]] hint_inline hint_const
703constexpr bool cmpint(T a, T b) noexcept {
704 using enum CMPINT;
705 if constexpr (imm8 == TRUE) return -1;
706 else if constexpr (imm8 == FALSE) return 0;
707 else if constexpr (imm8 == LT) return a < b;
708 else if constexpr (imm8 == NLT) return a >= b;
709 else if constexpr (imm8 == LE) return a <= b;
710 else if constexpr (imm8 == NLE) return a > b;
711 else if constexpr (imm8 == EQ) return a == b;
712 else if constexpr (imm8 == NE) return a != b;
713 else static_assert(false);
714}
715
719inline constexpr std::size_t max_fp_comparison_predicate = [] {
720#ifdef __AVX512F__
721 return 32uz;
722#else
723 return 8uz;
724#endif
725}();
726
732enum class [[nodiscard]] CMP : std::size_t {
733 EQ_OQ = 0x00uz
734, LT_OS = 0x01uz
735, LE_OS = 0x02uz
736, UNORD_Q = 0x03uz
737, NEQ_UQ = 0x04uz
738, NLT_US = 0x05uz
739, NLE_US = 0x06uz
740, ORD_Q = 0x07uz
741, EQ_UQ = 0x08uz
742, NGE_US = 0x09uz
743, NGT_US = 0x0Auz
744, FALSE_OQ = 0x0Buz
745, NEQ_OQ = 0x0Cuz
746, GE_OS = 0x0Duz
747, GT_OS = 0x0Euz
748, TRUE_UQ = 0x0Fuz
749, EQ_OS = 0x10uz
750, LT_OQ = 0x11uz
751, LE_OQ = 0x12uz
752, UNORD_S = 0x13uz
753, NEQ_US = 0x14uz
754, NLT_UQ = 0x15uz
755, NLE_UQ = 0x16uz
756, ORD_S = 0x17uz
757, EQ_US = 0x18uz
758, NGE_UQ = 0x19uz
759, NGT_UQ = 0x1Auz
760, FALSE_OS = 0x1Buz
761, NEQ_OS = 0x1Cuz
762, GE_OQ = 0x1Duz
763, GT_OQ = 0x1Euz
764, TRUE_US = 0x1Fuz
765};
766
770template <CMP imm8, typename T>
771requires (one_of_t<T, float, double> && (std::size_t(imm8) < 32uz))
772[[nodiscard]] hint_inline hint_pure
773constexpr bool cmp(T a, T b) noexcept {
774 using enum CMP;
775 if constexpr (imm8 == EQ_OQ) return cmp_ord(a, b) && (a == b);
776 else if constexpr (imm8 == LT_OS) return cmp_ord(a, b) && (a < b);
777 else if constexpr (imm8 == LE_OS) return cmp_ord(a, b) && (a <= b);
778 else if constexpr (imm8 == UNORD_Q) return cmp_unord(a, b);
779 else if constexpr (imm8 == NEQ_UQ) return cmp_unord(a, b) || (a != b);
780 else if constexpr (imm8 == NLT_US) return cmp_unord(a, b) || !(a < b);
781 else if constexpr (imm8 == NLE_US) return cmp_unord(a, b) || !(a <= b);
782 else if constexpr (imm8 == ORD_Q) return cmp_ord(a, b);
783 else if constexpr (imm8 == EQ_UQ) return cmp_unord(a, b) || (a == b);
784 else if constexpr (imm8 == NGE_US) return cmp_unord(a, b) || !(a >= b);
785 else if constexpr (imm8 == NGT_US) return cmp_unord(a, b) || !(a > b);
786 else if constexpr (imm8 == FALSE_OQ) return 0;
787 else if constexpr (imm8 == NEQ_OQ) return cmp_ord(a, b) && (a != b);
788 else if constexpr (imm8 == GE_OS) return cmp_ord(a, b) && (a >= b);
789 else if constexpr (imm8 == GT_OS) return cmp_ord(a, b) && (a > b);
790 else if constexpr (imm8 == TRUE_UQ) return -1;
791 else if constexpr (imm8 == EQ_OS) return cmp_ord(a, b) && (a == b);
792 else if constexpr (imm8 == LT_OQ) return cmp_ord(a, b) && (a < b);
793 else if constexpr (imm8 == LE_OQ) return cmp_ord(a, b) && (a <= b);
794 else if constexpr (imm8 == UNORD_S) return cmp_unord(a, b);
795 else if constexpr (imm8 == NEQ_US) return cmp_unord(a, b) || (a != b);
796 else if constexpr (imm8 == NLT_UQ) return cmp_unord(a, b) || !(a < b);
797 else if constexpr (imm8 == NLE_UQ) return cmp_unord(a, b) || !(a <= b);
798 else if constexpr (imm8 == ORD_S) return cmp_ord(a, b);
799 else if constexpr (imm8 == EQ_US) return cmp_unord(a, b) || (a == b);
800 else if constexpr (imm8 == NGE_UQ) return cmp_unord(a, b) || !(a >= b);
801 else if constexpr (imm8 == NGT_UQ) return cmp_unord(a, b) || !(a > b);
802 else if constexpr (imm8 == FALSE_OS) return 0;
803 else if constexpr (imm8 == NEQ_OS) return cmp_ord(a, b) && (a != b);
804 else if constexpr (imm8 == GE_OQ) return cmp_ord(a, b) && (a >= b);
805 else if constexpr (imm8 == GT_OQ) return cmp_ord(a, b) && (a > b);
806 else if constexpr (imm8 == TRUE_US) return -1;
807 else static_assert(false);
808}
809
810
811
812
814}
815
816
817namespace native {
819 template bool cmp_unord(float, float);
821 template bool cmp_unord(double, double);
823 template bool cmp_ord(float, float);
825 template bool cmp_ord(double, double);
828 template float scalef(float, float) noexcept;
831 template double scalef(double, double) noexcept;
832}
static constexpr ::native::bf16 epsilon() noexcept
Return the distance from one to its successor, 2^-7 (bits 0x3c00).
static constexpr float_denorm_style has_denorm
static constexpr ::native::bf16 min() noexcept
Return the least positive normal value, 2^-126 (bits 0x0080).
static constexpr ::native::bf16 denorm_min() noexcept
Return the least positive subnormal value, 2^-133 (bits 0x0001).
static constexpr ::native::bf16 max() noexcept
Return the greatest finite value, (2 - 2^-7) * 2^127 (bits 0x7f7f).
static constexpr ::native::bf16 quiet_NaN() noexcept
Return a quiet NaN with the fixed encoding 0x7fc0.
static constexpr ::native::bf16 round_error() noexcept
Return the round-to-nearest error bound, one half (bits 0x3f00).
static constexpr ::native::bf16 signaling_NaN() noexcept
Return a signaling NaN encoding (bits 0x7f81); decoding to float quiets it.
static constexpr ::native::bf16 infinity() noexcept
Return positive infinity (bits 0x7f80).
static constexpr ::native::bf16 lowest() noexcept
Return the negative of max() (bits 0xff7f).
static constexpr ::native::fp16 round_error() noexcept
Return the round-to-nearest error bound, one half (bits 0x3800).
static constexpr ::native::fp16 min() noexcept
Return the least positive normal value, 2^-14 (bits 0x0400).
static constexpr ::native::fp16 max() noexcept
Return the greatest finite value, 65504 (bits 0x7bff).
static constexpr ::native::fp16 infinity() noexcept
Return positive infinity (bits 0x7c00).
static constexpr ::native::fp16 denorm_min() noexcept
Return the least positive subnormal value, 2^-24 (bits 0x0001).
static constexpr ::native::fp16 quiet_NaN() noexcept
Return a quiet NaN with the fixed encoding 0x7fff.
static constexpr ::native::fp16 signaling_NaN() noexcept
Return a signaling NaN encoding (bits 0x7dff); decoding to float quiets it.
static constexpr float_denorm_style has_denorm
static constexpr ::native::fp16 epsilon() noexcept
Return the distance from one to its successor, 2^-10 (bits 0x1400).
static constexpr ::native::fp16 lowest() noexcept
Return the most negative finite value, -65504 (bits 0xfbff).
Declares SIMD element types and pointer access policies.
N is not one of the candidates
N is one of the candidates
constexpr bool cmp_ord(T a, T b) noexcept(noexcept(!isnan(a) &&!isnan(b)))
Return true if neither argument is NaN.
constexpr std::size_t max_fp_comparison_predicate
constexpr T scalef(T x, T y) noexcept
typename integer_traits< sizeof(T) *8 >::unsigned_t uint_t
constexpr bool cmpint(T a, T b) noexcept
typename integer_traits< sizeof(T) *8 >::signed_t int_t
constexpr bool cmp(T a, T b) noexcept
CMP
Scalar floating-comparison predicates, including ordered and unordered truth tables.
CMPINT
Scalar integer-comparison predicates, encoded like the corresponding x86 immediates.
constexpr bool cmp_unord(T a, T b) noexcept(noexcept(isnan(a)||isnan(b)))
Return true if either argument is NaN.
@ LE_OQ
Less-than-or-equal (ordered, nonsignaling).
@ FALSE_OQ
False (ordered, nonsignaling).
@ NEQ_OS
Not-equal (ordered, signaling).
@ NLE_US
Not-less-than-or-equal (unordered, signaling).
@ TRUE_US
True (unordered, signaling).
@ NGE_UQ
Not-greater-than-or-equal (unordered, nonsignaling).
@ GE_OS
Greater-than-or-equal (ordered, signaling).
@ NEQ_OQ
Not-equal (ordered, nonsignaling).
@ ORD_Q
Ordered (nonsignaling).
@ LT_OS
Less-than (ordered, signaling).
@ GE_OQ
Greater-than-or-equal (ordered, nonsignaling).
@ EQ_US
Equal (unordered, signaling).
@ TRUE_UQ
True (unordered, nonsignaling).
@ EQ_OS
Equal (ordered, signaling).
@ FALSE_OS
False (ordered, signaling).
@ GT_OS
Greater-than (ordered, signaling).
@ ORD_S
Ordered (signaling).
@ NLE_UQ
Not-less-than-or-equal (unordered, nonsignaling).
@ GT_OQ
Greater-than (ordered, nonsignaling).
@ UNORD_S
Unordered (signaling).
@ LE_OS
Less-than-or-equal (ordered, signaling).
@ EQ_OQ
Equal (ordered, nonsignaling).
@ NEQ_UQ
Not-equal (unordered, nonsignaling).
@ EQ_UQ
Equal (unordered, nonsignaling).
@ NLT_US
Not-less-than (unordered, signaling).
@ UNORD_Q
Unordered (nonsignaling).
@ LT_OQ
Less-than (ordered, nonsignaling).
@ NLT_UQ
Not-less-than (unordered, nonsignaling).
@ NGT_US
Not-greater-than (unordered, signaling).
@ NGE_US
Not-greater-than-or-equal (unordered, signaling).
@ NGT_UQ
Not-greater-than (unordered, nonsignaling).
@ NEQ_US
Not-equal (unordered, signaling).
@ FALSE
always false
constexpr imm_t< K > imm
Definition common.h:82
constexpr auto isnan(wide< T, N > const &input) noexcept(noexcept(detail::wide_generic_detail::wide_map< detail::wide_generic_detail::wide_isnan >(input)))
Definition wide.h:910
Scalar half storage, numeric limits and legacy scalar predicates.
Architecture-tagged vectors, register packs and supporting value types. Native arithmetic follows its...
constexpr bf16 fast_to_bf16(float f) noexcept
Standard-library adaptations documented here for SIMD value types.
constexpr bool isnan(::native::fp16 x) noexcept
Classify the stored bits as NaN without performing a floating-point conversion.
friend constexpr bool operator<(bf16 x, bf16 y) noexcept
Return whether the decoded left value is less; false for NaN operands.
constexpr uint16_t to_bits(this bf16 self) noexcept
Return the exact sixteen representation bits.
friend constexpr std::partial_ordering operator<=>(bf16 x, bf16 y) noexcept
Compare decoded values; NaNs are unordered and signed zeros equivalent.
constexpr bf16(bf16 &&) noexcept=default
Copy the native representation; the source retains its bits.
friend constexpr bool operator<=(bf16 x, bf16 y) noexcept
Return whether the decoded left value is at most the right; false for NaN operands.
constexpr bf16(__bf16 f) noexcept
Copy native storage without a numerical conversion.
constexpr bf16(bf16 const &) noexcept=default
Copy the native sixteen-bit representation.
friend constexpr bool operator>=(bf16 x, bf16 y) noexcept
Return whether the decoded left value is at least the right; false for NaN operands.
underlying_type content
Exact storage bits. Use to_bits()/from_bits() for representation access.
uint16_t underlying_type
Sixteen representation bits, including subnormals and signaling NaNs.
friend constexpr bool operator!=(bf16 x, bf16 y) noexcept
Compare decoded values for inequality; a NaN operand makes the result true.
friend constexpr bool operator>(bf16 x, bf16 y) noexcept
Return whether the decoded left value is greater; false for NaN operands.
static constexpr bf16 from_bits(uint16_t data) noexcept
Adopt the exact sixteen representation bits without normalization.
friend constexpr void swap(bf16 &x, bf16 &y) noexcept
Exchange native representations, including signed zeros and NaN payloads.
constexpr bf16 & operator=(bf16 const &) noexcept=default
Copy the native representation, replacing all previous bits.
constexpr bf16() noexcept=default
Default-initialize storage without setting its bits; value-initialization zeroes it.
friend constexpr std::partial_ordering operator<=>(fp16 x, fp16 y) noexcept
Compare decoded values; NaNs are unordered and signed zeros equivalent.
_Float16 underlying_type
Native half storage when supported by the target; otherwise sixteen raw bits.
constexpr fp16 & operator=(fp16 const &) noexcept=default
Copy the native representation, replacing all previous bits.
friend constexpr bool operator<(fp16 x, fp16 y) noexcept
Return whether the decoded left value is less; false for NaN operands.
constexpr uint16_t to_bits(this fp16 self) noexcept
Return the exact sixteen representation bits.
constexpr fp16(_Float16 content) noexcept
Copy native storage without a numerical conversion.
friend constexpr bool operator<=(fp16 x, fp16 y) noexcept
Return whether the decoded left value is at most the right; false for NaN operands.
static constexpr fp16 from_bits(uint16_t data) noexcept
Adopt the exact sixteen representation bits without normalization.
friend constexpr bool operator>=(fp16 x, fp16 y) noexcept
Return whether the decoded left value is at least the right; false for NaN operands.
underlying_type content
Target-dependent storage. Use to_bits()/from_bits() for portable representation access.
friend constexpr bool operator!=(fp16 x, fp16 y) noexcept
Compare decoded values for inequality; a NaN operand makes the result true.
friend constexpr bool operator>(fp16 x, fp16 y) noexcept
Return whether the decoded left value is greater; false for NaN operands.
constexpr fp16() noexcept=default
Default-initialize storage without setting its bits; value-initialization zeroes it.
friend constexpr void swap(fp16 &x, fp16 &y) noexcept
Exchange native representations, including signed zeros and NaN payloads.