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 <__bit/bit_ceil.h> 14 #include <__utility/forward.h> 15 #include <__utility/integer_sequence.h> 16 #include <cstddef> 17 #include <experimental/__config> 18 #include <experimental/__simd/internal_declaration.h> 19 #include <experimental/__simd/traits.h> 20 #include <experimental/__simd/utility.h> 21 22 #if _LIBCPP_STD_VER >= 17 && defined(_LIBCPP_ENABLE_EXPERIMENTAL) 23 24 _LIBCPP_BEGIN_NAMESPACE_EXPERIMENTAL 25 inline namespace parallelism_v2 { 26 namespace simd_abi { 27 template <int _Np> 28 struct __vec_ext { 29 static constexpr size_t __simd_size = _Np; 30 }; 31 } // namespace simd_abi 32 33 template <int _Np> 34 inline constexpr bool is_abi_tag_v<simd_abi::__vec_ext<_Np>> = _Np > 0 && _Np <= 32; 35 36 template <class _Tp, int _Np> 37 struct __simd_storage<_Tp, simd_abi::__vec_ext<_Np>> { 38 _Tp __data __attribute__((__vector_size__(std::__bit_ceil((sizeof(_Tp) * _Np))))); 39 40 _LIBCPP_HIDE_FROM_ABI _Tp __get(size_t __idx) const noexcept { 41 _LIBCPP_ASSERT_UNCATEGORIZED(__idx >= 0 && __idx < _Np, "Index is out of bounds"); 42 return __data[__idx]; 43 } 44 _LIBCPP_HIDE_FROM_ABI void __set(size_t __idx, _Tp __v) noexcept { 45 _LIBCPP_ASSERT_UNCATEGORIZED(__idx >= 0 && __idx < _Np, "Index is out of bounds"); 46 __data[__idx] = __v; 47 } 48 }; 49 50 template <class _Tp, int _Np> 51 struct __mask_storage<_Tp, simd_abi::__vec_ext<_Np>> 52 : __simd_storage<decltype(experimental::__choose_mask_type<_Tp>()), simd_abi::__vec_ext<_Np>> {}; 53 54 template <class _Tp, int _Np> 55 struct __simd_operations<_Tp, simd_abi::__vec_ext<_Np>> { 56 using _SimdStorage = __simd_storage<_Tp, simd_abi::__vec_ext<_Np>>; 57 using _MaskStorage = __mask_storage<_Tp, simd_abi::__vec_ext<_Np>>; 58 59 static _LIBCPP_HIDE_FROM_ABI _SimdStorage __broadcast(_Tp __v) noexcept { 60 _SimdStorage __result; 61 for (int __i = 0; __i < _Np; ++__i) { 62 __result.__set(__i, __v); 63 } 64 return __result; 65 } 66 67 template <class _Generator, size_t... _Is> 68 static _LIBCPP_HIDE_FROM_ABI _SimdStorage __generate_init(_Generator&& __g, std::index_sequence<_Is...>) { 69 return _SimdStorage{{__g(std::integral_constant<size_t, _Is>())...}}; 70 } 71 72 template <class _Generator> 73 static _LIBCPP_HIDE_FROM_ABI _SimdStorage __generate(_Generator&& __g) noexcept { 74 return __generate_init(std::forward<_Generator>(__g), std::make_index_sequence<_Np>()); 75 } 76 }; 77 78 template <class _Tp, int _Np> 79 struct __mask_operations<_Tp, simd_abi::__vec_ext<_Np>> { 80 using _MaskStorage = __mask_storage<_Tp, simd_abi::__vec_ext<_Np>>; 81 82 static _LIBCPP_HIDE_FROM_ABI _MaskStorage __broadcast(bool __v) noexcept { 83 _MaskStorage __result; 84 auto __all_bits_v = experimental::__set_all_bits<_Tp>(__v); 85 for (int __i = 0; __i < _Np; ++__i) { 86 __result.__set(__i, __all_bits_v); 87 } 88 return __result; 89 } 90 }; 91 92 } // namespace parallelism_v2 93 _LIBCPP_END_NAMESPACE_EXPERIMENTAL 94 95 #endif // _LIBCPP_STD_VER >= 17 && defined(_LIBCPP_ENABLE_EXPERIMENTAL) 96 #endif // _LIBCPP_EXPERIMENTAL___SIMD_VEC_EXT_H 97