xref: /freebsd/contrib/llvm-project/libcxx/include/experimental/__simd/vec_ext.h (revision 7fdf597e96a02165cfe22ff357b857d5fa15ed8a)
1 // -*- C++ -*-
2 //===----------------------------------------------------------------------===//
3 //
4 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
5 // See https://llvm.org/LICENSE.txt for license information.
6 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
7 //
8 //===----------------------------------------------------------------------===//
9 
10 #ifndef _LIBCPP_EXPERIMENTAL___SIMD_VEC_EXT_H
11 #define _LIBCPP_EXPERIMENTAL___SIMD_VEC_EXT_H
12 
13 #include <__assert>
14 #include <__bit/bit_ceil.h>
15 #include <__utility/forward.h>
16 #include <__utility/integer_sequence.h>
17 #include <cstddef>
18 #include <experimental/__config>
19 #include <experimental/__simd/declaration.h>
20 #include <experimental/__simd/traits.h>
21 #include <experimental/__simd/utility.h>
22 
23 #if _LIBCPP_STD_VER >= 17 && defined(_LIBCPP_ENABLE_EXPERIMENTAL)
24 
25 _LIBCPP_BEGIN_NAMESPACE_EXPERIMENTAL
26 inline namespace parallelism_v2 {
27 namespace simd_abi {
28 template <int _Np>
29 struct __vec_ext {
30   static constexpr size_t __simd_size = _Np;
31 };
32 } // namespace simd_abi
33 
34 template <int _Np>
35 inline constexpr bool is_abi_tag_v<simd_abi::__vec_ext<_Np>> = _Np > 0 && _Np <= 32;
36 
37 template <class _Tp, int _Np>
38 struct __simd_storage<_Tp, simd_abi::__vec_ext<_Np>> {
39   _Tp __data __attribute__((__vector_size__(std::__bit_ceil((sizeof(_Tp) * _Np)))));
40 
41   _LIBCPP_HIDE_FROM_ABI _Tp __get(size_t __idx) const noexcept {
42     _LIBCPP_ASSERT_VALID_ELEMENT_ACCESS(__idx >= 0 && __idx < _Np, "Index is out of bounds");
43     return __data[__idx];
44   }
45   _LIBCPP_HIDE_FROM_ABI void __set(size_t __idx, _Tp __v) noexcept {
46     _LIBCPP_ASSERT_VALID_ELEMENT_ACCESS(__idx >= 0 && __idx < _Np, "Index is out of bounds");
47     __data[__idx] = __v;
48   }
49 };
50 
51 template <class _Tp, int _Np>
52 struct __mask_storage<_Tp, simd_abi::__vec_ext<_Np>>
53     : __simd_storage<decltype(experimental::__choose_mask_type<_Tp>()), simd_abi::__vec_ext<_Np>> {};
54 
55 template <class _Tp, int _Np>
56 struct __simd_operations<_Tp, simd_abi::__vec_ext<_Np>> {
57   using _SimdStorage = __simd_storage<_Tp, simd_abi::__vec_ext<_Np>>;
58   using _MaskStorage = __mask_storage<_Tp, simd_abi::__vec_ext<_Np>>;
59 
60   static _LIBCPP_HIDE_FROM_ABI _SimdStorage __broadcast(_Tp __v) noexcept {
61     _SimdStorage __result;
62     for (int __i = 0; __i < _Np; ++__i) {
63       __result.__set(__i, __v);
64     }
65     return __result;
66   }
67 
68   template <class _Generator, size_t... _Is>
69   static _LIBCPP_HIDE_FROM_ABI _SimdStorage __generate_init(_Generator&& __g, std::index_sequence<_Is...>) {
70     return _SimdStorage{{__g(std::integral_constant<size_t, _Is>())...}};
71   }
72 
73   template <class _Generator>
74   static _LIBCPP_HIDE_FROM_ABI _SimdStorage __generate(_Generator&& __g) noexcept {
75     return __generate_init(std::forward<_Generator>(__g), std::make_index_sequence<_Np>());
76   }
77 
78   template <class _Up>
79   static _LIBCPP_HIDE_FROM_ABI void __load(_SimdStorage& __s, const _Up* __mem) noexcept {
80     for (size_t __i = 0; __i < _Np; __i++)
81       __s.__data[__i] = static_cast<_Tp>(__mem[__i]);
82   }
83 
84   template <class _Up>
85   static _LIBCPP_HIDE_FROM_ABI void __store(_SimdStorage __s, _Up* __mem) noexcept {
86     for (size_t __i = 0; __i < _Np; __i++)
87       __mem[__i] = static_cast<_Up>(__s.__data[__i]);
88   }
89 };
90 
91 template <class _Tp, int _Np>
92 struct __mask_operations<_Tp, simd_abi::__vec_ext<_Np>> {
93   using _MaskStorage = __mask_storage<_Tp, simd_abi::__vec_ext<_Np>>;
94 
95   static _LIBCPP_HIDE_FROM_ABI _MaskStorage __broadcast(bool __v) noexcept {
96     _MaskStorage __result;
97     auto __all_bits_v = experimental::__set_all_bits<_Tp>(__v);
98     for (int __i = 0; __i < _Np; ++__i) {
99       __result.__set(__i, __all_bits_v);
100     }
101     return __result;
102   }
103 
104   static _LIBCPP_HIDE_FROM_ABI void __load(_MaskStorage& __s, const bool* __mem) noexcept {
105     for (size_t __i = 0; __i < _Np; __i++)
106       __s.__data[__i] = experimental::__set_all_bits<_Tp>(__mem[__i]);
107   }
108 
109   static _LIBCPP_HIDE_FROM_ABI void __store(_MaskStorage __s, bool* __mem) noexcept {
110     for (size_t __i = 0; __i < _Np; __i++)
111       __mem[__i] = static_cast<bool>(__s.__data[__i]);
112   }
113 };
114 
115 } // namespace parallelism_v2
116 _LIBCPP_END_NAMESPACE_EXPERIMENTAL
117 
118 #endif // _LIBCPP_STD_VER >= 17 && defined(_LIBCPP_ENABLE_EXPERIMENTAL)
119 #endif // _LIBCPP_EXPERIMENTAL___SIMD_VEC_EXT_H
120