|
| 1 | +/*************************************************************************** |
| 2 | + * Copyright (c) Johan Mabille, Sylvain Corlay, Wolf Vollprecht and * |
| 3 | + * Martin Renou * |
| 4 | + * Copyright (c) QuantStack * |
| 5 | + * Copyright (c) Serge Guelton * |
| 6 | + * Copyright (c) Marco Barbone * |
| 7 | + * * |
| 8 | + * Distributed under the terms of the BSD 3-Clause License. * |
| 9 | + * * |
| 10 | + * The full license is in the file LICENSE, distributed with this software. * |
| 11 | + ****************************************************************************/ |
| 12 | + |
| 13 | +#ifndef XSIMD_AVX2_128_HPP |
| 14 | +#define XSIMD_AVX2_128_HPP |
| 15 | + |
| 16 | +#include <type_traits> |
| 17 | + |
| 18 | +#include "../types/xsimd_avx2_register.hpp" |
| 19 | +#include "../types/xsimd_batch_constant.hpp" |
| 20 | + |
| 21 | +namespace xsimd |
| 22 | +{ |
| 23 | + namespace kernel |
| 24 | + { |
| 25 | + using namespace types; |
| 26 | + |
| 27 | + // select |
| 28 | + template <class A, class T, bool... Values, class = std::enable_if_t<std::is_integral<T>::value>> |
| 29 | + XSIMD_INLINE batch<T, A> select(batch_bool_constant<T, A, Values...> const&, batch<T, A> const& true_br, batch<T, A> const& false_br, requires_arch<avx2_128>) noexcept |
| 30 | + { |
| 31 | + constexpr int mask = batch_bool_constant<T, A, Values...>::mask(); |
| 32 | + XSIMD_IF_CONSTEXPR(sizeof(T) == 4) |
| 33 | + { |
| 34 | + return _mm_blend_epi32(false_br, true_br, mask); |
| 35 | + } |
| 36 | + else |
| 37 | + { |
| 38 | + return select(batch_bool_constant<T, A, Values...>(), true_br, false_br, avx_128 {}); |
| 39 | + } |
| 40 | + } |
| 41 | + |
| 42 | + // bitwise_lshift |
| 43 | + template <class A, class T, class = std::enable_if_t<std::is_integral<T>::value>> |
| 44 | + XSIMD_INLINE batch<T, A> bitwise_lshift(batch<T, A> const& self, batch<T, A> const& other, requires_arch<avx2_128>) noexcept |
| 45 | + { |
| 46 | + XSIMD_IF_CONSTEXPR(sizeof(T) == 4) |
| 47 | + { |
| 48 | + return _mm_sllv_epi32(self, other); |
| 49 | + } |
| 50 | + else XSIMD_IF_CONSTEXPR(sizeof(T) == 8) |
| 51 | + { |
| 52 | + return _mm_sllv_epi64(self, other); |
| 53 | + } |
| 54 | + else |
| 55 | + { |
| 56 | + return bitwise_lshift(self, other, avx {}); |
| 57 | + } |
| 58 | + } |
| 59 | + |
| 60 | + // bitwise_rshift |
| 61 | + template <class A, class T, class = std::enable_if_t<std::is_integral<T>::value>> |
| 62 | + XSIMD_INLINE batch<T, A> bitwise_rshift(batch<T, A> const& self, batch<T, A> const& other, requires_arch<avx2_128>) noexcept |
| 63 | + { |
| 64 | + if (std::is_signed<T>::value) |
| 65 | + { |
| 66 | + XSIMD_IF_CONSTEXPR(sizeof(T) == 4) |
| 67 | + { |
| 68 | + return _mm_srav_epi32(self, other); |
| 69 | + } |
| 70 | + else |
| 71 | + { |
| 72 | + return bitwise_rshift(self, other, avx_128 {}); |
| 73 | + } |
| 74 | + } |
| 75 | + else |
| 76 | + { |
| 77 | + XSIMD_IF_CONSTEXPR(sizeof(T) == 4) |
| 78 | + { |
| 79 | + return _mm_srlv_epi32(self, other); |
| 80 | + } |
| 81 | + else XSIMD_IF_CONSTEXPR(sizeof(T) == 8) |
| 82 | + { |
| 83 | + return _mm_srlv_epi64(self, other); |
| 84 | + } |
| 85 | + else |
| 86 | + { |
| 87 | + return bitwise_rshift(self, other, avx_128 {}); |
| 88 | + } |
| 89 | + } |
| 90 | + } |
| 91 | + |
| 92 | + // load_masked |
| 93 | + template <class A, bool... Values, class Mode> |
| 94 | + XSIMD_INLINE batch<int32_t, A> load_masked(int32_t const* mem, batch_bool_constant<int32_t, A, Values...> mask, convert<int32_t>, Mode, requires_arch<avx2_128>) noexcept |
| 95 | + { |
| 96 | + return _mm_maskload_epi32(mem, mask.as_batch()); |
| 97 | + } |
| 98 | + template <class A, bool... Values, class Mode> |
| 99 | + XSIMD_INLINE batch<uint32_t, A> load_masked(uint32_t const* mem, batch_bool_constant<uint32_t, A, Values...> mask, convert<uint32_t>, Mode, requires_arch<avx2_128>) noexcept |
| 100 | + { |
| 101 | + return _mm_maskload_epi32((int32_t*)mem, mask.as_batch()); |
| 102 | + } |
| 103 | + template <class A, bool... Values, class Mode> |
| 104 | + XSIMD_INLINE batch<int64_t, A> load_masked(int64_t const* mem, batch_bool_constant<int64_t, A, Values...> mask, convert<double>, Mode, requires_arch<avx_128>) noexcept |
| 105 | + { |
| 106 | + return _mm_maskload_epi64(mem, mask.as_batch()); |
| 107 | + } |
| 108 | + template <class A, bool... Values, class Mode> |
| 109 | + XSIMD_INLINE batch<uint64_t, A> load_masked(uint64_t const* mem, batch_bool_constant<uint64_t, A, Values...> mask, convert<double>, Mode, requires_arch<avx_128>) noexcept |
| 110 | + { |
| 111 | + return _mm_maskload_epi64((int64_t*)mem, mask.as_batch()); |
| 112 | + } |
| 113 | + |
| 114 | + // store_masked |
| 115 | + template <class A, bool... Values, class Mode> |
| 116 | + XSIMD_INLINE void store_masked(int32_t* mem, batch<int32_t, A> const& src, batch_bool_constant<int32_t, A, Values...> mask, Mode, requires_arch<avx2_128>) noexcept |
| 117 | + { |
| 118 | + return _mm_maskstore_epi32(mem, mask.as_batch(), src); |
| 119 | + } |
| 120 | + template <class A, bool... Values, class Mode> |
| 121 | + XSIMD_INLINE void store_masked(uint32_t* mem, batch<uint32_t, A> const& src, batch_bool_constant<uint32_t, A, Values...> mask, Mode, requires_arch<avx2_128>) noexcept |
| 122 | + { |
| 123 | + return _mm_maskstore_epi32((int32_t*)mem, mask.as_batch(), src); |
| 124 | + } |
| 125 | + template <class A, bool... Values, class Mode> |
| 126 | + XSIMD_INLINE void store_masked(int64_t* mem, batch<int64_t, A> const& src, batch_bool_constant<int64_t, A, Values...> mask, Mode, requires_arch<avx_128>) noexcept |
| 127 | + { |
| 128 | + return _mm_maskstore_epi64(mem, mask.as_batch(), src); |
| 129 | + } |
| 130 | + template <class A, bool... Values, class Mode> |
| 131 | + XSIMD_INLINE void store_masked(uint64_t* mem, batch<uint64_t, A> const& src, batch_bool_constant<uint64_t, A, Values...> mask, Mode, requires_arch<avx_128>) noexcept |
| 132 | + { |
| 133 | + return _mm_maskstore_epi64((int64_t*)mem, mask.as_batch(), src); |
| 134 | + } |
| 135 | + |
| 136 | + // gather |
| 137 | + template <class T, class A, class U, detail::enable_sized_integral_t<T, 4> = 0, detail::enable_sized_integral_t<U, 4> = 0> |
| 138 | + XSIMD_INLINE batch<T, A> gather(batch<T, A> const&, T const* src, batch<U, A> const& index, |
| 139 | + kernel::requires_arch<avx2_128>) noexcept |
| 140 | + { |
| 141 | + return _mm_i32gather_epi32(reinterpret_cast<const int*>(src), index, sizeof(T)); |
| 142 | + } |
| 143 | + |
| 144 | + template <class T, class A, class U, detail::enable_sized_integral_t<T, 8> = 0, detail::enable_sized_integral_t<U, 8> = 0> |
| 145 | + XSIMD_INLINE batch<T, A> gather(batch<T, A> const&, T const* src, batch<U, A> const& index, |
| 146 | + kernel::requires_arch<avx2_128>) noexcept |
| 147 | + { |
| 148 | + return _mm_i64gather_epi64(reinterpret_cast<const long long int*>(src), index, sizeof(T)); |
| 149 | + } |
| 150 | + |
| 151 | + template <class A, class U, |
| 152 | + detail::enable_sized_integral_t<U, 4> = 0> |
| 153 | + XSIMD_INLINE batch<float, A> gather(batch<float, A> const&, float const* src, |
| 154 | + batch<U, A> const& index, |
| 155 | + kernel::requires_arch<avx2_128>) noexcept |
| 156 | + { |
| 157 | + return _mm_i32gather_ps(src, index, sizeof(float)); |
| 158 | + } |
| 159 | + |
| 160 | + template <class A, class U, detail::enable_sized_integral_t<U, 8> = 0> |
| 161 | + XSIMD_INLINE batch<double, A> gather(batch<double, A> const&, double const* src, |
| 162 | + batch<U, A> const& index, |
| 163 | + requires_arch<avx2_128>) noexcept |
| 164 | + { |
| 165 | + return _mm_i64gather_pd(src, index, sizeof(double)); |
| 166 | + } |
| 167 | + } |
| 168 | +} |
| 169 | + |
| 170 | +#endif |
0 commit comments