|
| 1 | +#ifndef NPY_SIMDX |
| 2 | +#error "This is not a standalone header. Include simd.hpp instead." |
| 3 | +#define NPY_SIMDX 1 // Prevent editors from graying out the happy branch |
| 4 | +#endif |
| 5 | + |
| 6 | +// Using anonymous namespace instead of inline to ensure each translation unit |
| 7 | +// gets its own copy of constants based on local compilation flags |
| 8 | +namespace { |
| 9 | + |
| 10 | +// NOTE: This file is included by simd.hpp multiple times with different namespaces |
| 11 | +// so avoid including any headers here |
| 12 | + |
| 13 | +/** |
| 14 | + * Determines whether the specified lane type is supported by the SIMD extension. |
| 15 | + * Always defined as false when SIMD is not enabled, so it can be used in SFINAE. |
| 16 | + * |
| 17 | + * @tparam TLane The lane type to check for support. |
| 18 | + */ |
| 19 | +template <typename TLane> |
| 20 | +constexpr bool kSupportLane = NPY_SIMDX != 0; |
| 21 | + |
| 22 | +#if NPY_SIMDX |
| 23 | +// Define lane type support based on Highway capabilities |
| 24 | +template <> |
| 25 | +constexpr bool kSupportLane<hwy::float16_t> = HWY_HAVE_FLOAT16 != 0; |
| 26 | +template <> |
| 27 | +constexpr bool kSupportLane<double> = HWY_HAVE_FLOAT64 != 0; |
| 28 | +template <> |
| 29 | +constexpr bool kSupportLane<long double> = |
| 30 | + HWY_HAVE_FLOAT64 != 0 && sizeof(long double) == sizeof(double); |
| 31 | + |
| 32 | +/// Maximum number of lanes supported by the SIMD extension for the specified lane type. |
| 33 | +template <typename TLane> |
| 34 | +constexpr size_t kMaxLanes = HWY_MAX_LANES_D(_Tag<TLane>); |
| 35 | + |
| 36 | +/// Represents an N-lane vector based on the specified lane type. |
| 37 | +/// @tparam TLane The scalar type for each vector lane |
| 38 | +template <typename TLane> |
| 39 | +using Vec = hn::Vec<_Tag<TLane>>; |
| 40 | + |
| 41 | +/// Represents a mask vector with boolean values or as a bitmask. |
| 42 | +/// @tparam TLane The scalar type the mask corresponds to |
| 43 | +template <typename TLane> |
| 44 | +using Mask = hn::Mask<_Tag<TLane>>; |
| 45 | + |
| 46 | +/// Unaligned load of a vector from memory. |
| 47 | +template <typename TLane> |
| 48 | +HWY_API Vec<TLane> |
| 49 | +LoadU(const TLane *ptr) |
| 50 | +{ |
| 51 | + return hn::LoadU(_Tag<TLane>(), ptr); |
| 52 | +} |
| 53 | + |
| 54 | +/// Unaligned store of a vector to memory. |
| 55 | +template <typename TLane> |
| 56 | +HWY_API void |
| 57 | +StoreU(const Vec<TLane> &a, TLane *ptr) |
| 58 | +{ |
| 59 | + hn::StoreU(a, _Tag<TLane>(), ptr); |
| 60 | +} |
| 61 | + |
| 62 | +/// Returns the number of vector lanes based on the lane type. |
| 63 | +template <typename TLane> |
| 64 | +HWY_API HWY_LANES_CONSTEXPR size_t |
| 65 | +Lanes(TLane tag = 0) |
| 66 | +{ |
| 67 | + return hn::Lanes(_Tag<TLane>()); |
| 68 | +} |
| 69 | + |
| 70 | +/// Returns an uninitialized N-lane vector. |
| 71 | +template <typename TLane> |
| 72 | +HWY_API Vec<TLane> |
| 73 | +Undefined(TLane tag = 0) |
| 74 | +{ |
| 75 | + return hn::Undefined(_Tag<TLane>()); |
| 76 | +} |
| 77 | + |
| 78 | +/// Returns N-lane vector with all lanes equal to zero. |
| 79 | +template <typename TLane> |
| 80 | +HWY_API Vec<TLane> |
| 81 | +Zero(TLane tag = 0) |
| 82 | +{ |
| 83 | + return hn::Zero(_Tag<TLane>()); |
| 84 | +} |
| 85 | + |
| 86 | +/// Returns N-lane vector with all lanes equal to the given value of type `TLane`. |
| 87 | +template <typename TLane> |
| 88 | +HWY_API Vec<TLane> |
| 89 | +Set(TLane val) |
| 90 | +{ |
| 91 | + return hn::Set(_Tag<TLane>(), val); |
| 92 | +} |
| 93 | + |
| 94 | +/// Converts a mask to a vector based on the specified lane type. |
| 95 | +template <typename TLane, typename TMask> |
| 96 | +HWY_API Vec<TLane> |
| 97 | +VecFromMask(const TMask &m) |
| 98 | +{ |
| 99 | + return hn::VecFromMask(_Tag<TLane>(), m); |
| 100 | +} |
| 101 | + |
| 102 | +/// Convert (Reinterpret) an N-lane vector to a different type without modifying the |
| 103 | +/// underlying data. |
| 104 | +template <typename TLaneTo, typename TVec> |
| 105 | +HWY_API Vec<TLaneTo> |
| 106 | +BitCast(const TVec &v) |
| 107 | +{ |
| 108 | + return hn::BitCast(_Tag<TLaneTo>(), v); |
| 109 | +} |
| 110 | + |
| 111 | +// Import common Highway intrinsics |
| 112 | +using hn::Abs; |
| 113 | +using hn::Add; |
| 114 | +using hn::And; |
| 115 | +using hn::AndNot; |
| 116 | +using hn::Div; |
| 117 | +using hn::Eq; |
| 118 | +using hn::Ge; |
| 119 | +using hn::Gt; |
| 120 | +using hn::Le; |
| 121 | +using hn::Lt; |
| 122 | +using hn::Max; |
| 123 | +using hn::Min; |
| 124 | +using hn::Mul; |
| 125 | +using hn::Or; |
| 126 | +using hn::Sqrt; |
| 127 | +using hn::Sub; |
| 128 | +using hn::Xor; |
| 129 | + |
| 130 | +#endif // NPY_SIMDX |
| 131 | + |
| 132 | +} // namespace anonymous |
0 commit comments