|
| 1 | +/*************************************************************************** |
| 2 | +* Copyright (c) Serge Guelton * |
| 3 | +* Copyright (c) QuantStack * |
| 4 | +* * |
| 5 | +* Distributed under the terms of the BSD 3-Clause License. * |
| 6 | +* * |
| 7 | +* The full license is in the file LICENSE, distributed with this software. * |
| 8 | +****************************************************************************/ |
| 9 | + |
| 10 | +#ifndef XSIMD_BASE_CONSTANT_HPP |
| 11 | +#define XSIMD_BASE_CONSTANT_HPP |
| 12 | + |
| 13 | +namespace xsimd |
| 14 | +{ |
| 15 | + template <class X> |
| 16 | + class simd_base; |
| 17 | + |
| 18 | + template <class T, bool... Values> |
| 19 | + struct batch_bool_constant |
| 20 | + { |
| 21 | + static constexpr std::size_t size = sizeof...(Values); |
| 22 | + using value_type = bool; |
| 23 | + using batch_type = |
| 24 | + typename simd_batch_traits<batch<T, size>>::batch_bool_type; |
| 25 | + |
| 26 | + batch_type operator()() const { return *this; } |
| 27 | + |
| 28 | + operator batch_type() const { return {Values...}; } |
| 29 | + |
| 30 | + bool operator[](size_t i) const |
| 31 | + { |
| 32 | + return std::array<value_type, size>{Values...}[i]; |
| 33 | + } |
| 34 | + static constexpr int mask() |
| 35 | + { |
| 36 | + return mask_helper(0, static_cast<int>(Values)...); |
| 37 | + } |
| 38 | + |
| 39 | + private: |
| 40 | + static constexpr int mask_helper(int acc) { return acc; } |
| 41 | + template <class... Tys> |
| 42 | + static constexpr int mask_helper(int acc, int mask, Tys... masks) |
| 43 | + { |
| 44 | + return mask_helper(acc | mask, (masks << 1)...); |
| 45 | + } |
| 46 | + }; |
| 47 | + |
| 48 | + template <class T, T... Values> |
| 49 | + struct batch_constant |
| 50 | + { |
| 51 | + static constexpr std::size_t size = sizeof...(Values); |
| 52 | + using value_type = T; |
| 53 | + using batch_type = batch<T, size>; |
| 54 | + |
| 55 | + batch_type operator()() const { return *this; } |
| 56 | + |
| 57 | + operator batch_type() const { return {Values...}; } |
| 58 | + |
| 59 | + constexpr T operator[](size_t i) const |
| 60 | + { |
| 61 | + return std::array<value_type, size>{Values...}[i]; |
| 62 | + } |
| 63 | + }; |
| 64 | + |
| 65 | + namespace detail |
| 66 | + { |
| 67 | + template <class G, std::size_t... Is> |
| 68 | + constexpr auto make_batch_constant(detail::index_sequence<Is...>) |
| 69 | + -> batch_constant<decltype(G::get(0, 0)), |
| 70 | + G::get(Is, sizeof...(Is))...> |
| 71 | + { |
| 72 | + return {}; |
| 73 | + } |
| 74 | + template <class T, class G, std::size_t... Is> |
| 75 | + constexpr auto make_batch_bool_constant(detail::index_sequence<Is...>) |
| 76 | + -> batch_bool_constant<T, G::get(Is, sizeof...(Is))...> |
| 77 | + { |
| 78 | + return {}; |
| 79 | + } |
| 80 | + template <class T, T value, std::size_t... Is> |
| 81 | + constexpr auto make_batch_constant(detail::index_sequence<Is...>) |
| 82 | + -> batch_constant<T, (Is, value)...> |
| 83 | + { |
| 84 | + return {}; |
| 85 | + } |
| 86 | + template <class T, T value, std::size_t... Is> |
| 87 | + constexpr auto make_batch_bool_constant(detail::index_sequence<Is...>) |
| 88 | + -> batch_bool_constant<T, (Is, value)...> |
| 89 | + { |
| 90 | + return {}; |
| 91 | + } |
| 92 | + } // namespace detail |
| 93 | + |
| 94 | + template <class G, std::size_t N> |
| 95 | + constexpr auto make_batch_constant() -> decltype( |
| 96 | + detail::make_batch_constant<G>(detail::make_index_sequence<N>())) |
| 97 | + { |
| 98 | + return detail::make_batch_constant<G>(detail::make_index_sequence<N>()); |
| 99 | + } |
| 100 | + |
| 101 | + template <class T, class G, std::size_t N> |
| 102 | + constexpr auto make_batch_bool_constant() |
| 103 | + -> decltype(detail::make_batch_bool_constant<T, G>( |
| 104 | + detail::make_index_sequence<N>())) |
| 105 | + { |
| 106 | + return detail::make_batch_bool_constant<T, G>( |
| 107 | + detail::make_index_sequence<N>()); |
| 108 | + } |
| 109 | + |
| 110 | +} // namespace xsimd |
| 111 | + |
| 112 | +#endif |
0 commit comments