simd.h 2.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566
  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_SIMD_H
  10. #define _LIBCPP_EXPERIMENTAL___SIMD_SIMD_H
  11. #include <__type_traits/remove_cvref.h>
  12. #include <experimental/__simd/abi_tag.h>
  13. #include <experimental/__simd/declaration.h>
  14. #include <experimental/__simd/reference.h>
  15. #include <experimental/__simd/scalar.h>
  16. #include <experimental/__simd/traits.h>
  17. #include <experimental/__simd/vec_ext.h>
  18. #if _LIBCPP_STD_VER >= 17 && defined(_LIBCPP_ENABLE_EXPERIMENTAL)
  19. _LIBCPP_BEGIN_NAMESPACE_EXPERIMENTAL
  20. inline namespace parallelism_v2 {
  21. // class template simd [simd.class]
  22. // TODO: implement simd class
  23. template <class _Tp, class _Abi>
  24. class simd {
  25. using _Impl = __simd_operations<_Tp, _Abi>;
  26. using _Storage = typename _Impl::_SimdStorage;
  27. _Storage __s_;
  28. public:
  29. using value_type = _Tp;
  30. using reference = __simd_reference<_Tp, _Storage, value_type>;
  31. using mask_type = simd_mask<_Tp, _Abi>;
  32. using abi_type = _Abi;
  33. static _LIBCPP_HIDE_FROM_ABI constexpr size_t size() noexcept { return simd_size_v<value_type, abi_type>; }
  34. // broadcast constructor
  35. template <class _Up, enable_if_t<__can_broadcast_v<value_type, __remove_cvref_t<_Up>>, int> = 0>
  36. _LIBCPP_HIDE_FROM_ABI simd(_Up&& __v) noexcept : __s_(_Impl::__broadcast(static_cast<value_type>(__v))) {}
  37. // generator constructor
  38. template <class _Generator, enable_if_t<__can_generate_v<value_type, _Generator, size()>, int> = 0>
  39. explicit _LIBCPP_HIDE_FROM_ABI simd(_Generator&& __g) : __s_(_Impl::__generate(std::forward<_Generator>(__g))) {}
  40. // scalar access [simd.subscr]
  41. // Add operator[] temporarily to test braodcast. Add test for it in later patch.
  42. _LIBCPP_HIDE_FROM_ABI value_type operator[](size_t __i) const { return __s_.__get(__i); }
  43. };
  44. template <class _Tp>
  45. using native_simd = simd<_Tp, simd_abi::native<_Tp>>;
  46. template <class _Tp, int _Np>
  47. using fixed_size_simd = simd<_Tp, simd_abi::fixed_size<_Np>>;
  48. } // namespace parallelism_v2
  49. _LIBCPP_END_NAMESPACE_EXPERIMENTAL
  50. #endif // _LIBCPP_STD_VER >= 17 && defined(_LIBCPP_ENABLE_EXPERIMENTAL)
  51. #endif // _LIBCPP_EXPERIMENTAL___SIMD_SIMD_H