vec_ext.h 3.7 KB

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