Back to home page

EIC code displayed by LXR

 
 

    


File indexing completed on 2026-05-03 08:14:08

0001 // -*- C++ -*-
0002 //===----------------------------------------------------------------------===//
0003 //
0004 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
0005 // See https://llvm.org/LICENSE.txt for license information.
0006 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
0007 //
0008 //===----------------------------------------------------------------------===//
0009 
0010 #ifndef _LIBCPP_EXPERIMENTAL___SIMD_SIMD_H
0011 #define _LIBCPP_EXPERIMENTAL___SIMD_SIMD_H
0012 
0013 #include <__config>
0014 #include <__cstddef/size_t.h>
0015 #include <__type_traits/enable_if.h>
0016 #include <__type_traits/is_integral.h>
0017 #include <__type_traits/is_same.h>
0018 #include <__type_traits/remove_cvref.h>
0019 #include <__utility/forward.h>
0020 #include <experimental/__simd/declaration.h>
0021 #include <experimental/__simd/reference.h>
0022 #include <experimental/__simd/traits.h>
0023 #include <experimental/__simd/utility.h>
0024 
0025 #if _LIBCPP_STD_VER >= 17 && defined(_LIBCPP_ENABLE_EXPERIMENTAL)
0026 
0027 _LIBCPP_BEGIN_NAMESPACE_EXPERIMENTAL
0028 inline namespace parallelism_v2 {
0029 
0030 template <class _Simd, class _Impl, bool>
0031 class __simd_int_operators {};
0032 
0033 template <class _Simd, class _Impl>
0034 class __simd_int_operators<_Simd, _Impl, true> {
0035 public:
0036   // unary operators for integral _Tp
0037   _LIBCPP_HIDE_FROM_ABI _Simd operator~() const noexcept {
0038     return _Simd(_Impl::__bitwise_not((*static_cast<const _Simd*>(this)).__s_), _Simd::__storage_tag);
0039   }
0040 };
0041 
0042 // class template simd [simd.class]
0043 // TODO: implement simd class
0044 template <class _Tp, class _Abi>
0045 class simd : public __simd_int_operators<simd<_Tp, _Abi>, __simd_operations<_Tp, _Abi>, is_integral_v<_Tp>> {
0046   using _Impl _LIBCPP_NODEBUG    = __simd_operations<_Tp, _Abi>;
0047   using _Storage _LIBCPP_NODEBUG = typename _Impl::_SimdStorage;
0048 
0049   _Storage __s_;
0050 
0051   friend class __simd_int_operators<simd, _Impl, true>;
0052 
0053 public:
0054   using value_type = _Tp;
0055   using reference  = __simd_reference<_Tp, _Storage, value_type>;
0056   using mask_type  = simd_mask<_Tp, _Abi>;
0057   using abi_type   = _Abi;
0058 
0059   static _LIBCPP_HIDE_FROM_ABI constexpr size_t size() noexcept { return simd_size_v<value_type, abi_type>; }
0060 
0061   _LIBCPP_HIDE_FROM_ABI simd() noexcept = default;
0062 
0063   // explicit conversion from and to implementation-defined types
0064   struct __storage_tag_t {};
0065   static constexpr __storage_tag_t __storage_tag{};
0066   explicit _LIBCPP_HIDE_FROM_ABI operator _Storage() const { return __s_; }
0067   explicit _LIBCPP_HIDE_FROM_ABI simd(const _Storage& __s, __storage_tag_t) : __s_(__s) {}
0068 
0069   // broadcast constructor
0070   template <class _Up, enable_if_t<__can_broadcast_v<value_type, __remove_cvref_t<_Up>>, int> = 0>
0071   _LIBCPP_HIDE_FROM_ABI simd(_Up&& __v) noexcept : __s_(_Impl::__broadcast(static_cast<value_type>(__v))) {}
0072 
0073   // implicit type conversion constructor
0074   template <class _Up,
0075             enable_if_t<!is_same_v<_Up, _Tp> && is_same_v<abi_type, simd_abi::fixed_size<size()>> &&
0076                             __is_non_narrowing_convertible_v<_Up, value_type>,
0077                         int> = 0>
0078   _LIBCPP_HIDE_FROM_ABI simd(const simd<_Up, simd_abi::fixed_size<size()>>& __v) noexcept {
0079     for (size_t __i = 0; __i < size(); __i++) {
0080       (*this)[__i] = static_cast<value_type>(__v[__i]);
0081     }
0082   }
0083 
0084   // generator constructor
0085   template <class _Generator, enable_if_t<__can_generate_v<value_type, _Generator, size()>, int> = 0>
0086   explicit _LIBCPP_HIDE_FROM_ABI simd(_Generator&& __g) noexcept
0087       : __s_(_Impl::__generate(std::forward<_Generator>(__g))) {}
0088 
0089   // load constructor
0090   template <class _Up, class _Flags, enable_if_t<__is_vectorizable_v<_Up> && is_simd_flag_type_v<_Flags>, int> = 0>
0091   _LIBCPP_HIDE_FROM_ABI simd(const _Up* __mem, _Flags) {
0092     _Impl::__load(__s_, _Flags::template __apply<simd>(__mem));
0093   }
0094 
0095   // copy functions
0096   template <class _Up, class _Flags, enable_if_t<__is_vectorizable_v<_Up> && is_simd_flag_type_v<_Flags>, int> = 0>
0097   _LIBCPP_HIDE_FROM_ABI void copy_from(const _Up* __mem, _Flags) {
0098     _Impl::__load(__s_, _Flags::template __apply<simd>(__mem));
0099   }
0100 
0101   template <class _Up, class _Flags, enable_if_t<__is_vectorizable_v<_Up> && is_simd_flag_type_v<_Flags>, int> = 0>
0102   _LIBCPP_HIDE_FROM_ABI void copy_to(_Up* __mem, _Flags) const {
0103     _Impl::__store(__s_, _Flags::template __apply<simd>(__mem));
0104   }
0105 
0106   // scalar access [simd.subscr]
0107   _LIBCPP_HIDE_FROM_ABI reference operator[](size_t __i) noexcept { return reference(__s_, __i); }
0108   _LIBCPP_HIDE_FROM_ABI value_type operator[](size_t __i) const noexcept { return __s_.__get(__i); }
0109 
0110   // simd unary operators
0111   _LIBCPP_HIDE_FROM_ABI simd& operator++() noexcept {
0112     _Impl::__increment(__s_);
0113     return *this;
0114   }
0115 
0116   _LIBCPP_HIDE_FROM_ABI simd operator++(int) noexcept {
0117     simd __r = *this;
0118     _Impl::__increment(__s_);
0119     return __r;
0120   }
0121 
0122   _LIBCPP_HIDE_FROM_ABI simd& operator--() noexcept {
0123     _Impl::__decrement(__s_);
0124     return *this;
0125   }
0126 
0127   _LIBCPP_HIDE_FROM_ABI simd operator--(int) noexcept {
0128     simd __r = *this;
0129     _Impl::__decrement(__s_);
0130     return __r;
0131   }
0132 
0133   _LIBCPP_HIDE_FROM_ABI mask_type operator!() const noexcept {
0134     return mask_type(_Impl::__negate(__s_), mask_type::__storage_tag);
0135   }
0136 
0137   _LIBCPP_HIDE_FROM_ABI simd operator+() const noexcept { return *this; }
0138 
0139   _LIBCPP_HIDE_FROM_ABI simd operator-() const noexcept { return simd(_Impl::__unary_minus(__s_), __storage_tag); }
0140 };
0141 
0142 template <class _Tp, class _Abi>
0143 inline constexpr bool is_simd_v<simd<_Tp, _Abi>> = true;
0144 
0145 template <class _Tp>
0146 using native_simd = simd<_Tp, simd_abi::native<_Tp>>;
0147 
0148 template <class _Tp, int _Np>
0149 using fixed_size_simd = simd<_Tp, simd_abi::fixed_size<_Np>>;
0150 
0151 } // namespace parallelism_v2
0152 _LIBCPP_END_NAMESPACE_EXPERIMENTAL
0153 
0154 #endif // _LIBCPP_STD_VER >= 17 && defined(_LIBCPP_ENABLE_EXPERIMENTAL)
0155 #endif // _LIBCPP_EXPERIMENTAL___SIMD_SIMD_H