scalar.h 2.6 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980
  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_SCALAR_H
  10. #define _LIBCPP_EXPERIMENTAL___SIMD_SCALAR_H
  11. #include <__assert>
  12. #include <cstddef>
  13. #include <experimental/__config>
  14. #include <experimental/__simd/declaration.h>
  15. #include <experimental/__simd/traits.h>
  16. #if _LIBCPP_STD_VER >= 17 && defined(_LIBCPP_ENABLE_EXPERIMENTAL)
  17. _LIBCPP_BEGIN_NAMESPACE_EXPERIMENTAL
  18. inline namespace parallelism_v2 {
  19. namespace simd_abi {
  20. struct __scalar {
  21. static constexpr size_t __simd_size = 1;
  22. };
  23. } // namespace simd_abi
  24. template <>
  25. inline constexpr bool is_abi_tag_v<simd_abi::__scalar> = true;
  26. template <class _Tp>
  27. struct __simd_storage<_Tp, simd_abi::__scalar> {
  28. _Tp __data;
  29. _LIBCPP_HIDE_FROM_ABI _Tp __get([[maybe_unused]] size_t __idx) const noexcept {
  30. _LIBCPP_ASSERT_UNCATEGORIZED(__idx == 0, "Index is out of bounds");
  31. return __data;
  32. }
  33. _LIBCPP_HIDE_FROM_ABI void __set([[maybe_unused]] size_t __idx, _Tp __v) noexcept {
  34. _LIBCPP_ASSERT_UNCATEGORIZED(__idx == 0, "Index is out of bounds");
  35. __data = __v;
  36. }
  37. };
  38. template <class _Tp>
  39. struct __mask_storage<_Tp, simd_abi::__scalar> : __simd_storage<bool, simd_abi::__scalar> {};
  40. template <class _Tp>
  41. struct __simd_operations<_Tp, simd_abi::__scalar> {
  42. using _SimdStorage = __simd_storage<_Tp, simd_abi::__scalar>;
  43. using _MaskStorage = __mask_storage<_Tp, simd_abi::__scalar>;
  44. static _LIBCPP_HIDE_FROM_ABI _SimdStorage __broadcast(_Tp __v) noexcept { return {__v}; }
  45. template <class _Generator>
  46. static _LIBCPP_HIDE_FROM_ABI _SimdStorage __generate(_Generator&& __g) noexcept {
  47. return {__g(std::integral_constant<size_t, 0>())};
  48. }
  49. template <class _Up>
  50. static _LIBCPP_HIDE_FROM_ABI void __load(_SimdStorage& __s, const _Up* __mem) noexcept {
  51. __s.__data = static_cast<_Tp>(__mem[0]);
  52. }
  53. };
  54. template <class _Tp>
  55. struct __mask_operations<_Tp, simd_abi::__scalar> {
  56. using _MaskStorage = __mask_storage<_Tp, simd_abi::__scalar>;
  57. static _LIBCPP_HIDE_FROM_ABI _MaskStorage __broadcast(bool __v) noexcept { return {__v}; }
  58. static _LIBCPP_HIDE_FROM_ABI void __load(_MaskStorage& __s, const bool* __mem) noexcept { __s.__data = __mem[0]; }
  59. };
  60. } // namespace parallelism_v2
  61. _LIBCPP_END_NAMESPACE_EXPERIMENTAL
  62. #endif // _LIBCPP_STD_VER >= 17 && defined(_LIBCPP_ENABLE_EXPERIMENTAL)
  63. #endif // _LIBCPP_EXPERIMENTAL___SIMD_SCALAR_H