native 0.0.1
Vectors, masks and wide register packs for C++26
Loading...
Searching...
No Matches
packing_body.h
1// SPDX-FileCopyrightText: 2026 Edward Kmett <ekmett@gmail.com>
2// SPDX-License-Identifier: BSD-2-Clause OR Apache-2.0
3
4
5namespace native {
6#if NATIVE_HAS_AVX2 || NATIVE_HAS_ARM_NEON || NATIVE_HAS_WASM_SIMD128
9 template <simd_integer_element To, simd_integer_element From, std::size_t N, ::native::isa<> Arch>
10 requires NATIVE_ARCH_REQUIRES(Arch) && (!detail::polyfill_shape<From,N,Arch> &&
11 !detail::polyfill_shape<To,2*N,Arch> && !detail::instruction_only_shape<From,N,Arch> &&
12 (!detail::instruction_only_shape<To,2*N,Arch> ||
13 (NATIVE_HAS_AVX512F != 0 && N*sizeof(From)==64 && sizeof(From)>=4))) && (std::is_unsigned_v<To> && std::is_unsigned_v<From> &&
14 sizeof(From) == 2 * sizeof(To) &&
15 // The selected backend must implement this width. Complete storage alone
16 // does not establish the arithmetic instructions used by this operation.
17 (N * sizeof(From) == 16
18#if NATIVE_HAS_AVX2
19 || N * sizeof(From) == 32
20#endif
21#if NATIVE_HAS_AVX512F
22 || (N * sizeof(From) == 64 && (sizeof(From) >= 4 || NATIVE_HAS_AVX512BW != 0))
23#endif
24 ) &&
25 requires { sizeof(simd<From, N, Arch>); sizeof(simd<To, 2 * N, Arch>); })
26 [[nodiscard]] hint_inline constexpr hint_const simd<To, 2 * N, Arch> narrow_concat(
28 using result = simd<To, 2 * N, Arch>;
29 if consteval {
30 std::array<From,N> first{},second{};
31 a.store(first.data()); b.store(second.data());
32 std::array<To,2*N> lanes{};
33 for(std::size_t i=0;i<N;++i) {
34 lanes[i]=static_cast<To>(first[i]);
35 lanes[N+i]=static_cast<To>(second[i]);
36 }
37 return result(lanes);
38 }
39#if NATIVE_HAS_WASM_SIMD128
40 return [&]<std::size_t... I>(std::index_sequence<I...>) {
41 using vector_type = To __attribute__((ext_vector_type(2 * N)));
42 auto first = __builtin_bit_cast(vector_type, a.to_native());
43 auto second = __builtin_bit_cast(vector_type, b.to_native());
44 return result::from_native(__builtin_bit_cast(
45 v128_t, __builtin_shufflevector(first, second, (2 * I)..., (2 * N + 2 * I)...)));
46 }(std::make_index_sequence<N>{});
47#elif NATIVE_HAS_ARM_NEON
48 if constexpr (sizeof(From) == 8)
49 return result::from_native(vreinterpretq_u8_u32(vcombine_u32(
50 vmovn_u64(vreinterpretq_u64_u8(a.to_native())),
51 vmovn_u64(vreinterpretq_u64_u8(b.to_native())))));
52 else if constexpr (sizeof(From) == 4)
53 return result::from_native(vreinterpretq_u8_u16(vcombine_u16(
54 vmovn_u32(vreinterpretq_u32_u8(a.to_native())),
55 vmovn_u32(vreinterpretq_u32_u8(b.to_native())))));
56 else return result::from_native(vcombine_u8(
57 vmovn_u16(vreinterpretq_u16_u8(a.to_native())),
58 vmovn_u16(vreinterpretq_u16_u8(b.to_native()))));
59#else
60 if constexpr (N * sizeof(From) == 16) {
61 if constexpr (sizeof(From) == 8)
62 return result::from_native(_mm_unpacklo_epi64(
63 _mm_shuffle_epi32(a.to_native(), _MM_SHUFFLE(2, 0, 2, 0)),
64 _mm_shuffle_epi32(b.to_native(), _MM_SHUFFLE(2, 0, 2, 0))));
65 else if constexpr (sizeof(From) == 4) {
66 auto mask = _mm_set1_epi32(0xffff);
67 return result::from_native(_mm_packus_epi32(
68 _mm_and_si128(a.to_native(), mask), _mm_and_si128(b.to_native(), mask)));
69 } else {
70 auto mask = _mm_set1_epi16(0xff);
71 return result::from_native(_mm_packus_epi16(
72 _mm_and_si128(a.to_native(), mask), _mm_and_si128(b.to_native(), mask)));
73 }
74 } else if constexpr (N * sizeof(From) == 32) {
75 if constexpr (sizeof(From) == 8) {
76 auto order = _mm256_setr_epi32(0, 2, 4, 6, 0, 2, 4, 6);
77 auto low = _mm256_castsi256_si128(_mm256_permutevar8x32_epi32(a.to_native(), order));
78 auto high = _mm256_castsi256_si128(_mm256_permutevar8x32_epi32(b.to_native(), order));
79 return result::from_native(_mm256_inserti128_si256(_mm256_castsi128_si256(low), high, 1));
80 } else if constexpr (sizeof(From) == 4) {
81 auto mask = _mm256_set1_epi32(0xffff);
82 auto packed = _mm256_packus_epi32(_mm256_and_si256(a.to_native(), mask),
83 _mm256_and_si256(b.to_native(), mask));
84 return result::from_native(_mm256_permute4x64_epi64(packed, _MM_SHUFFLE(3, 1, 2, 0)));
85 } else {
86 auto mask = _mm256_set1_epi16(0xff);
87 auto packed = _mm256_packus_epi16(_mm256_and_si256(a.to_native(), mask),
88 _mm256_and_si256(b.to_native(), mask));
89 return result::from_native(_mm256_permute4x64_epi64(packed, _MM_SHUFFLE(3, 1, 2, 0)));
90 }
91#if NATIVE_HAS_AVX512F
92 } else {
93 auto truncate = [](auto x) {
94 if constexpr (sizeof(From) == 8) return _mm512_cvtepi64_epi32(x);
95 else if constexpr (sizeof(From) == 4) return _mm512_cvtepi32_epi16(x);
96 else return _mm512_cvtepi16_epi8(x);
97 };
98 auto low = truncate(a.to_native()), high = truncate(b.to_native());
99 return result::from_native(_mm512_inserti64x4(_mm512_castsi256_si512(low), high, 1));
100#endif
101 }
102#endif
103 }
104#endif
105}
typename mask_traits< std::remove_cvref_t< T > >::type mask
Definition mask_traits.h:22
Architecture-tagged vectors, register packs and supporting value types. Native arithmetic follows its...
constexpr simd< To, 2 *N, Arch > narrow_concat(simd< From, N, Arch > a, simd< From, N, Arch > b) noexcept
Omitted architecture arguments use the native.simd provider's baseline.