• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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_SIMD_H
11 #define _LIBCPP_EXPERIMENTAL___SIMD_SIMD_H
12 
13 #include <__type_traits/is_same.h>
14 #include <__type_traits/remove_cvref.h>
15 #include <__utility/forward.h>
16 #include <cstddef>
17 #include <experimental/__config>
18 #include <experimental/__simd/abi_tag.h>
19 #include <experimental/__simd/declaration.h>
20 #include <experimental/__simd/internal_declaration.h>
21 #include <experimental/__simd/reference.h>
22 #include <experimental/__simd/traits.h>
23 #include <experimental/__simd/utility.h>
24 
25 #if _LIBCPP_STD_VER >= 17 && defined(_LIBCPP_ENABLE_EXPERIMENTAL)
26 
27 _LIBCPP_BEGIN_NAMESPACE_EXPERIMENTAL
28 inline namespace parallelism_v2 {
29 
30 // class template simd [simd.class]
31 // TODO: implement simd class
32 template <class _Tp, class _Abi>
33 class simd {
34   using _Impl    = __simd_operations<_Tp, _Abi>;
35   using _Storage = typename _Impl::_SimdStorage;
36 
37   _Storage __s_;
38 
39 public:
40   using value_type = _Tp;
41   using reference  = __simd_reference<_Tp, _Storage, value_type>;
42   using mask_type  = simd_mask<_Tp, _Abi>;
43   using abi_type   = _Abi;
44 
size()45   static _LIBCPP_HIDE_FROM_ABI constexpr size_t size() noexcept { return simd_size_v<value_type, abi_type>; }
46 
47   _LIBCPP_HIDE_FROM_ABI simd() noexcept = default;
48 
49   // broadcast constructor
50   template <class _Up, enable_if_t<__can_broadcast_v<value_type, __remove_cvref_t<_Up>>, int> = 0>
simd(_Up && __v)51   _LIBCPP_HIDE_FROM_ABI simd(_Up&& __v) noexcept : __s_(_Impl::__broadcast(static_cast<value_type>(__v))) {}
52 
53   // implicit type conversion constructor
54   template <class _Up,
55             enable_if_t<!is_same_v<_Up, _Tp> && is_same_v<abi_type, simd_abi::fixed_size<size()>> &&
56                             __is_non_narrowing_convertible_v<_Up, value_type>,
57                         int> = 0>
simd(const simd<_Up,simd_abi::fixed_size<size ()>> & __v)58   _LIBCPP_HIDE_FROM_ABI simd(const simd<_Up, simd_abi::fixed_size<size()>>& __v) noexcept {
59     for (size_t __i = 0; __i < size(); __i++) {
60       (*this)[__i] = static_cast<value_type>(__v[__i]);
61     }
62   }
63 
64   // generator constructor
65   template <class _Generator, enable_if_t<__can_generate_v<value_type, _Generator, size()>, int> = 0>
simd(_Generator && __g)66   explicit _LIBCPP_HIDE_FROM_ABI simd(_Generator&& __g) noexcept
67       : __s_(_Impl::__generate(std::forward<_Generator>(__g))) {}
68 
69   // scalar access [simd.subscr]
70   _LIBCPP_HIDE_FROM_ABI reference operator[](size_t __i) noexcept { return reference(__s_, __i); }
71   _LIBCPP_HIDE_FROM_ABI value_type operator[](size_t __i) const noexcept { return __s_.__get(__i); }
72 };
73 
74 template <class _Tp, class _Abi>
75 inline constexpr bool is_simd_v<simd<_Tp, _Abi>> = true;
76 
77 template <class _Tp>
78 using native_simd = simd<_Tp, simd_abi::native<_Tp>>;
79 
80 template <class _Tp, int _Np>
81 using fixed_size_simd = simd<_Tp, simd_abi::fixed_size<_Np>>;
82 
83 } // namespace parallelism_v2
84 _LIBCPP_END_NAMESPACE_EXPERIMENTAL
85 
86 #endif // _LIBCPP_STD_VER >= 17 && defined(_LIBCPP_ENABLE_EXPERIMENTAL)
87 #endif // _LIBCPP_EXPERIMENTAL___SIMD_SIMD_H
88