| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
|
| 15 |
|
| 16 |
|
| 17 |
|
| 18 |
|
| 19 |
|
| 20 |
#if defined(HIGHWAY_HWY_OPS_SHARED_TOGGLE) == \ |
| 21 |
defined(HWY_TARGET_TOGGLE) |
| 22 |
#ifdef HIGHWAY_HWY_OPS_SHARED_TOGGLE |
| 23 |
#undef HIGHWAY_HWY_OPS_SHARED_TOGGLE |
| 24 |
#else |
| 25 |
#define HIGHWAY_HWY_OPS_SHARED_TOGGLE |
| 26 |
#endif |
| 27 |
|
| 28 |
#ifndef HWY_NO_LIBCXX |
| 29 |
#include <math.h> |
| 30 |
#endif |
| 31 |
|
| 32 |
#include "hwy/base.h" |
| 33 |
|
| 34 |
|
| 35 |
#include "hwy/ops/set_macros-inl.h" |
| 36 |
|
| 37 |
|
| 38 |
HWY_BEFORE_NAMESPACE(); |
| 39 |
namespace hwy { |
| 40 |
namespace HWY_NAMESPACE { |
| 41 |
|
| 42 |
|
| 43 |
|
| 44 |
|
| 45 |
|
| 46 |
|
| 47 |
|
| 48 |
|
| 49 |
|
| 50 |
|
| 51 |
template <typename Lane, size_t N, int kPow2> |
| 52 |
struct Simd { |
| 53 |
constexpr Simd() = default; |
| 54 |
using T = Lane; |
| 55 |
static_assert((N & (N - 1)) == 0 && N != 0, "N must be a power of two"); |
| 56 |
|
| 57 |
|
| 58 |
|
| 59 |
|
| 60 |
static constexpr size_t kPrivateN = N; |
| 61 |
static constexpr int kPrivatePow2 = kPow2; |
| 62 |
|
| 63 |
template <typename NewT> |
| 64 |
static constexpr size_t NewN() { |
| 65 |
|
| 66 |
return (N * sizeof(T) + sizeof(NewT) - 1) / sizeof(NewT); |
| 67 |
} |
| 68 |
|
| 69 |
#if HWY_HAVE_SCALABLE |
| 70 |
template <typename NewT> |
| 71 |
static constexpr int Pow2Ratio() { |
| 72 |
return (sizeof(NewT) > sizeof(T)) |
| 73 |
? static_cast<int>(CeilLog2(sizeof(NewT) / sizeof(T))) |
| 74 |
: -static_cast<int>(CeilLog2(sizeof(T) / sizeof(NewT))); |
| 75 |
} |
| 76 |
#endif |
| 77 |
|
| 78 |
|
| 79 |
|
| 80 |
|
| 81 |
|
| 82 |
#if HWY_HAVE_SCALABLE |
| 83 |
template <typename NewT> |
| 84 |
using Rebind = Simd<NewT, N, kPow2 + Pow2Ratio<NewT>()>; |
| 85 |
#else |
| 86 |
template <typename NewT> |
| 87 |
using Rebind = Simd<NewT, N, kPow2>; |
| 88 |
#endif |
| 89 |
|
| 90 |
|
| 91 |
template <typename NewT> |
| 92 |
using Repartition = Simd<NewT, NewN<NewT>(), kPow2>; |
| 93 |
|
| 94 |
|
| 95 |
|
| 96 |
#if HWY_HAVE_SCALABLE |
| 97 |
|
| 98 |
|
| 99 |
using Half = Simd<T, (N + 1) / 2, kPow2 - 1>; |
| 100 |
#else |
| 101 |
using Half = Simd<T, (N + 1) / 2, kPow2>; |
| 102 |
#endif |
| 103 |
|
| 104 |
|
| 105 |
#if HWY_HAVE_SCALABLE |
| 106 |
using Twice = Simd<T, 2 * N, kPow2 + 1>; |
| 107 |
#else |
| 108 |
using Twice = Simd<T, 2 * N, kPow2>; |
| 109 |
#endif |
| 110 |
}; |
| 111 |
|
| 112 |
namespace detail { |
| 113 |
|
| 114 |
template <typename T, size_t N, int kPow2> |
| 115 |
constexpr bool IsFull(Simd<T, N, kPow2> ) { |
| 116 |
return N == HWY_LANES(T) && kPow2 == 0; |
| 117 |
} |
| 118 |
|
| 119 |
|
| 120 |
|
| 121 |
|
| 122 |
|
| 123 |
constexpr size_t ScaleByPower(size_t N, int pow2) { |
| 124 |
#if HWY_TARGET == HWY_RVV |
| 125 |
return pow2 >= 0 ? (N << pow2) : (N >> (-pow2)); |
| 126 |
#else |
| 127 |
return pow2 >= 0 ? N : (N >> (-pow2)); |
| 128 |
#endif |
| 129 |
} |
| 130 |
|
| 131 |
|
| 132 |
template <typename T, int kPow2> |
| 133 |
struct ScalableTagChecker { |
| 134 |
static_assert(-3 <= kPow2 && kPow2 <= 3, "Fraction must be 1/8 to 8"); |
| 135 |
#if HWY_TARGET == HWY_RVV |
| 136 |
|
| 137 |
using type = Simd<T, HWY_LANES(T), kPow2>; |
| 138 |
#elif HWY_HAVE_SCALABLE |
| 139 |
|
| 140 |
using type = Simd<T, HWY_LANES(T), HWY_MIN(kPow2, 0)>; |
| 141 |
#elif HWY_TARGET == HWY_SCALAR |
| 142 |
using type = Simd<T, 1, 0>; |
| 143 |
#else |
| 144 |
|
| 145 |
using type = Simd<T, ScaleByPower(HWY_LANES(T), HWY_MIN(kPow2, 0)), 0>; |
| 146 |
#endif |
| 147 |
}; |
| 148 |
|
| 149 |
template <typename T, size_t kLimit> |
| 150 |
struct CappedTagChecker { |
| 151 |
static_assert(kLimit != 0, "Does not make sense to have zero lanes"); |
| 152 |
|
| 153 |
|
| 154 |
static constexpr size_t kLimitPow2 = size_t{1} << hwy::FloorLog2(kLimit); |
| 155 |
using type = Simd<T, HWY_MIN(kLimitPow2, HWY_LANES(T)), 0>; |
| 156 |
}; |
| 157 |
|
| 158 |
template <typename T, size_t kNumLanes> |
| 159 |
struct FixedTagChecker { |
| 160 |
static_assert(kNumLanes != 0, "Does not make sense to have zero lanes"); |
| 161 |
static_assert(kNumLanes <= HWY_LANES(T), "Too many lanes"); |
| 162 |
using type = Simd<T, kNumLanes, 0>; |
| 163 |
}; |
| 164 |
|
| 165 |
} |
| 166 |
|
| 167 |
|
| 168 |
|
| 169 |
|
| 170 |
|
| 171 |
|
| 172 |
template <typename T, int kPow2 = 0> |
| 173 |
using ScalableTag = typename detail::ScalableTagChecker<T, kPow2>::type; |
| 174 |
|
| 175 |
|
| 176 |
|
| 177 |
|
| 178 |
|
| 179 |
|
| 180 |
|
| 181 |
|
| 182 |
|
| 183 |
template <typename T, size_t kLimit> |
| 184 |
using CappedTag = typename detail::CappedTagChecker<T, kLimit>::type; |
| 185 |
|
| 186 |
|
| 187 |
|
| 188 |
|
| 189 |
|
| 190 |
|
| 191 |
|
| 192 |
|
| 193 |
|
| 194 |
|
| 195 |
|
| 196 |
|
| 197 |
|
| 198 |
|
| 199 |
template <typename T, size_t kNumLanes> |
| 200 |
using FixedTag = typename detail::FixedTagChecker<T, kNumLanes>::type; |
| 201 |
|
| 202 |
template <class D> |
| 203 |
using TFromD = typename D::T; |
| 204 |
|
| 205 |
|
| 206 |
template <class T, class D> |
| 207 |
using Rebind = typename D::template Rebind<T>; |
| 208 |
|
| 209 |
template <class D> |
| 210 |
using RebindToSigned = Rebind<MakeSigned<TFromD<D>>, D>; |
| 211 |
template <class D> |
| 212 |
using RebindToUnsigned = Rebind<MakeUnsigned<TFromD<D>>, D>; |
| 213 |
template <class D> |
| 214 |
using RebindToFloat = Rebind<MakeFloat<TFromD<D>>, D>; |
| 215 |
|
| 216 |
|
| 217 |
template <class T, class D> |
| 218 |
using Repartition = typename D::template Repartition<T>; |
| 219 |
|
| 220 |
template <class D> |
| 221 |
using RepartitionToWide = Repartition<MakeWide<TFromD<D>>, D>; |
| 222 |
template <class D> |
| 223 |
using RepartitionToNarrow = Repartition<MakeNarrow<TFromD<D>>, D>; |
| 224 |
|
| 225 |
|
| 226 |
template <class D> |
| 227 |
using Half = typename D::Half; |
| 228 |
|
| 229 |
|
| 230 |
template <class D> |
| 231 |
using Twice = typename D::Twice; |
| 232 |
|
| 233 |
template <typename T> |
| 234 |
using Full16 = Simd<T, 2 / sizeof(T), 0>; |
| 235 |
|
| 236 |
template <typename T> |
| 237 |
using Full32 = Simd<T, 4 / sizeof(T), 0>; |
| 238 |
|
| 239 |
template <typename T> |
| 240 |
using Full64 = Simd<T, 8 / sizeof(T), 0>; |
| 241 |
|
| 242 |
template <typename T> |
| 243 |
using Full128 = Simd<T, 16 / sizeof(T), 0>; |
| 244 |
|
| 245 |
|
| 246 |
#define HWY_IF_UNSIGNED_D(D) HWY_IF_UNSIGNED(TFromD<D>) |
| 247 |
#define HWY_IF_SIGNED_D(D) HWY_IF_SIGNED(TFromD<D>) |
| 248 |
#define HWY_IF_FLOAT_D(D) HWY_IF_FLOAT(TFromD<D>) |
| 249 |
#define HWY_IF_NOT_FLOAT_D(D) HWY_IF_NOT_FLOAT(TFromD<D>) |
| 250 |
#define HWY_IF_LANE_SIZE_D(D, bytes) HWY_IF_LANE_SIZE(TFromD<D>, bytes) |
| 251 |
#define HWY_IF_NOT_LANE_SIZE_D(D, bytes) HWY_IF_NOT_LANE_SIZE(TFromD<D>, bytes) |
| 252 |
#define HWY_IF_LANE_SIZE_ONE_OF_D(D, bit_array) \ |
| 253 |
HWY_IF_LANE_SIZE_ONE_OF(TFromD<D>, bit_array) |
| 254 |
|
| 255 |
|
| 256 |
#define HWY_IF_LT128_D(D) \ |
| 257 |
hwy::EnableIf<D::kPrivateN * sizeof(TFromD<D>) < 16>* = nullptr |
| 258 |
#define HWY_IF_GE128_D(D) \ |
| 259 |
hwy::EnableIf<D::kPrivateN * sizeof(TFromD<D>) >= 16>* = nullptr |
| 260 |
|
| 261 |
|
| 262 |
#define HWY_IF_UNSIGNED_V(V) HWY_IF_UNSIGNED(TFromV<V>) |
| 263 |
#define HWY_IF_SIGNED_V(V) HWY_IF_SIGNED(TFromV<V>) |
| 264 |
#define HWY_IF_FLOAT_V(V) HWY_IF_FLOAT(TFromV<V>) |
| 265 |
#define HWY_IF_LANE_SIZE_V(V, bytes) HWY_IF_LANE_SIZE(TFromV<V>, bytes) |
| 266 |
#define HWY_IF_NOT_LANE_SIZE_V(V, bytes) HWY_IF_NOT_LANE_SIZE(TFromV<V>, bytes) |
| 267 |
#define HWY_IF_LANE_SIZE_ONE_OF_V(V, bit_array) \ |
| 268 |
HWY_IF_LANE_SIZE_ONE_OF(TFromV<V>, bit_array) |
| 269 |
|
| 270 |
template <class D> |
| 271 |
HWY_INLINE HWY_MAYBE_UNUSED constexpr int Pow2(D ) { |
| 272 |
return D::kPrivatePow2; |
| 273 |
} |
| 274 |
|
| 275 |
|
| 276 |
#define HWY_IF_POW2_GE(D, MIN) hwy::EnableIf<Pow2<D>(D()) >= (MIN)>* = nullptr |
| 277 |
|
| 278 |
#if HWY_HAVE_SCALABLE |
| 279 |
|
| 280 |
|
| 281 |
|
| 282 |
|
| 283 |
|
| 284 |
|
| 285 |
template <class D> |
| 286 |
HWY_INLINE HWY_MAYBE_UNUSED constexpr size_t MaxLanes(D) { |
| 287 |
return detail::ScaleByPower(HWY_MIN(D::kPrivateN, HWY_LANES(TFromD<D>)), |
| 288 |
D::kPrivatePow2); |
| 289 |
} |
| 290 |
|
| 291 |
#else |
| 292 |
|
| 293 |
|
| 294 |
template <class D> |
| 295 |
HWY_INLINE HWY_MAYBE_UNUSED constexpr size_t MaxLanes(D) { |
| 296 |
return D::kPrivateN; |
| 297 |
} |
| 298 |
|
| 299 |
|
| 300 |
|
| 301 |
|
| 302 |
template <typename T, size_t N, int kPow2> |
| 303 |
HWY_INLINE HWY_MAYBE_UNUSED size_t Lanes(Simd<T, N, kPow2>) { |
| 304 |
return N; |
| 305 |
} |
| 306 |
|
| 307 |
#endif |
| 308 |
|
| 309 |
|
| 310 |
|
| 311 |
|
| 312 |
|
| 313 |
|
| 314 |
|
| 315 |
|
| 316 |
|
| 317 |
|
| 318 |
|
| 319 |
#if HWY_COMPILER_GCC_ACTUAL && (HWY_OS_WIN || HWY_ARCH_ARM_A64) |
| 320 |
template <class V> |
| 321 |
using VecArg = const V&; |
| 322 |
#else |
| 323 |
template <class V> |
| 324 |
using VecArg = V; |
| 325 |
#endif |
| 326 |
|
| 327 |
|
| 328 |
} |
| 329 |
} |
| 330 |
HWY_AFTER_NAMESPACE(); |
| 331 |
|
| 332 |
#endif |