native 0.0.1
Vectors, masks and wide register packs for C++26
Loading...
Searching...
No Matches
instruction_storage.h
1// SPDX-License-Identifier: BSD-2-Clause OR Apache-2.0
2#pragma once
3#include "native/config.h"
4#include <hint.h>
5#include <bit>
6#include <cstdint>
7#if NATIVE_HOST_X86
8#include <immintrin.h>
9
10namespace native::detail {
11 // Instruction boundaries use complete logical register shapes. Permitted
12 // storage can be scalar or split while an operation's hardware is available.
13 // These casts preserve bytes without adding a second public vector API.
14 template<class V> requires(sizeof(typename V::native_type) == 16)
15 [[nodiscard]] hint_inline hint_target("sse2")
16 __m128i x86_integer_register(V value) noexcept {
17 return __builtin_bit_cast(__m128i, value.to_native());
18 }
19
20 template<class V, class R> requires(sizeof(R) == 16)
21 [[nodiscard]] hint_inline hint_target("sse2")
22 V x86_instruction_result(R value) noexcept {
23 static_assert(sizeof(typename V::native_type) == sizeof(R));
24 return V::from_native(__builtin_bit_cast(typename V::native_type, value));
25 }
26
27 template<class V> requires(sizeof(typename V::native_type) == 32)
28 [[nodiscard]] hint_inline hint_target("avx")
29 __m256i x86_integer_register(V value) noexcept {
30 return __builtin_bit_cast(__m256i, value.to_native());
31 }
32
33 template<class V, class R> requires(sizeof(R) == 32)
34 [[nodiscard]] hint_inline hint_target("avx")
35 V x86_instruction_result(R value) noexcept {
36 static_assert(sizeof(typename V::native_type) == sizeof(R));
37 return V::from_native(__builtin_bit_cast(typename V::native_type, value));
38 }
39
40 template<class V> requires(sizeof(typename V::native_type) == 64)
41 [[nodiscard]] hint_inline hint_target("avx512f")
42 __m512i x86_integer_register(V value) noexcept {
43 return __builtin_bit_cast(__m512i, value.to_native());
44 }
45
46 template<class V, class R> requires(sizeof(R) == 64)
47 [[nodiscard]] hint_inline hint_target("avx512f")
48 V x86_instruction_result(R value) noexcept {
49 static_assert(sizeof(typename V::native_type) == sizeof(R));
50 return V::from_native(__builtin_bit_cast(typename V::native_type, value));
51 }
52
53 template<class V> requires(sizeof(typename V::native_type) == 16)
54 [[nodiscard]] hint_inline hint_target("sse2")
55 __m128 x86_float_register(V value) noexcept {
56 return __builtin_bit_cast(__m128, value.to_native());
57 }
58 template<class V> requires(sizeof(typename V::native_type) == 32)
59 [[nodiscard]] hint_inline hint_target("avx")
60 __m256 x86_float_register(V value) noexcept {
61 return __builtin_bit_cast(__m256, value.to_native());
62 }
63 // Four logical half lanes can occupy eight scalar bytes or a native register.
64 // Copy only their representation; unused native register words are zero.
65 template<class V> requires(sizeof(typename V::native_type) == 8 ||
66 sizeof(typename V::native_type) == 16)
67 [[nodiscard]] hint_inline hint_target("sse2")
68 __m128i x86_half_register(V value) noexcept {
69 auto source = value.to_native();
70 __m128i result{};
71 __builtin_memcpy(&result, &source, sizeof(source));
72 return result;
73 }
74 template<class V, class R> requires(sizeof(R) == 16 && (sizeof(V) == 8 || sizeof(V) == 16))
75 [[nodiscard]] hint_inline hint_target("sse2")
76 V x86_half_result(R value) noexcept {
77 if constexpr (sizeof(V) == 16) return __builtin_bit_cast(V, value);
78 else {
79 std::uint64_t bits;
80 __builtin_memcpy(&bits, &value, sizeof(bits));
81 return __builtin_bit_cast(V, bits);
82 }
83 }
84
85}
86#endif