aboutsummaryrefslogtreecommitdiff
path: root/intrinsics/riscv64/include/berberis/intrinsics/riscv64/vector_intrinsics.h
blob: bb3b19d9c2f2d0b78c625e9e3f06e4d8a96241bc (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
/*
 * Copyright (C) 2015 The Android Open Source Project
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

#ifndef BERBERIS_INTRINSICS_RISCV64_VECTOR_INTRINSICS_H_
#define BERBERIS_INTRINSICS_RISCV64_VECTOR_INTRINSICS_H_

#include <cstdint>
#include <limits>
#include <tuple>
#include <type_traits>

#include "berberis/base/dependent_false.h"
#include "berberis/intrinsics/intrinsics.h"        // PreferredIntrinsicsImplementation
#include "berberis/intrinsics/intrinsics_float.h"  // Float32/Float64
#include "berberis/intrinsics/simd_register.h"
#include "berberis/intrinsics/type_traits.h"

namespace berberis::intrinsics {

enum class TailProcessing {
  kUndisturbed = 0,
  kAgnostic = 1,
};

enum class InactiveProcessing {
  kUndisturbed = 0,
  kAgnostic = 1,
};

template <typename ElementType>
int MaskForRegisterInSequence(SIMD128Register mask, size_t register_in_sequence) {
  if constexpr (sizeof(ElementType) == sizeof(uint8_t)) {
    return mask.Get<uint16_t>(register_in_sequence);
  } else if constexpr (sizeof(ElementType) == sizeof(uint16_t)) {
    return mask.Get<uint8_t>(register_in_sequence);
  } else if constexpr (sizeof(ElementType) == sizeof(uint32_t)) {
    return mask.Get<uint32_t>(0) >> (register_in_sequence * 4);
  } else if constexpr (sizeof(ElementType) == sizeof(uint64_t)) {
    return mask.Get<uint32_t>(0) >> (register_in_sequence * 2);
  } else {
    static_assert(kDependentTypeFalse<ElementType>, "Unsupported vector element type");
  }
}

template <typename ElementType>
inline ElementType VectorElement(SIMD128Register src, int index) {
  return src.Get<ElementType>(index);
}

template <typename ElementType>
inline ElementType VectorElement(ElementType src, int) {
  return src;
}

// TODO(b/260725458): Pass lambda as template argument after C++20 would become available.
template <typename ElementType, TailProcessing vta, typename Lambda, typename... SourceType>
inline std::tuple<SIMD128Register> VectorArithmetic(Lambda lambda,
                                                    int vstart,
                                                    int vl,
                                                    SIMD128Register result,
                                                    SourceType... source) {
  static_assert(((std::is_same_v<SourceType, SIMD128Register> ||
                  std::is_same_v<SourceType, ElementType>)&&...));
  constexpr auto fill_value = [] {
    if constexpr (std::is_integral_v<ElementType>) {
      return std::numeric_limits<std::make_unsigned_t<ElementType>>::max();
    } else {
      return std::numeric_limits<
          std::make_unsigned_t<typename TypeTraits<ElementType>::Int>>::max();
    }
  }();
  if (vstart < 0) {
    vstart = 0;
  }
  if (vl > static_cast<int>(16 / sizeof(ElementType))) {
    vl = 16 / sizeof(ElementType);
  }
  if (vstart == 0 && vl == static_cast<int>(16 / sizeof(ElementType))) {
    for (int index = vstart; index < vl; ++index) {
      result.Set<ElementType>(lambda(VectorElement<ElementType>(result, index),
                                     VectorElement<ElementType>(source, index)...), index);
    }
  } else {
#pragma clang loop unroll(disable)
    for (int index = vstart; index < vl; ++index) {
      result.Set<ElementType>(lambda(VectorElement<ElementType>(result, index),
                                     VectorElement<ElementType>(source, index)...), index);
    }
    if constexpr (vta == TailProcessing::kAgnostic) {
      if (vl < static_cast<int>(16 / sizeof(ElementType))) {
#pragma clang loop unroll(disable)
        for (int index = vl; index < 16 / static_cast<int>(sizeof(ElementType)); ++index) {
          result.Set<ElementType>(fill_value, index);
        }
      }
    }
  }
  return result;
}

// TODO(b/260725458): Pass lambda as template argument after C++20 would become available.
template <typename ElementType,
          TailProcessing vta,
          InactiveProcessing vma,
          typename Lambda,
          typename... SourceType>
inline std::tuple<SIMD128Register> VectorArithmetic(Lambda lambda,
                                                    int vstart,
                                                    int vl,
                                                    int mask,
                                                    SIMD128Register result,
                                                    SourceType... source) {
  static_assert(((std::is_same_v<SourceType, SIMD128Register> ||
                  std::is_same_v<SourceType, ElementType>)&&...));
  constexpr auto fill_value = [] {
    if constexpr (std::is_integral_v<ElementType>) {
      return std::numeric_limits<std::make_unsigned_t<ElementType>>::max();
    } else {
      return std::numeric_limits<
          std::make_unsigned_t<typename TypeTraits<ElementType>::Int>>::max();
    }
  }();
  if (vstart < 0) {
    vstart = 0;
  }
  if (vl > static_cast<int>(16 / sizeof(ElementType))) {
    vl = 16 / sizeof(ElementType);
  }
#pragma clang loop unroll(disable)
  for (int index = vstart; index < vl; ++index) {
    if (mask & (1 << index)) {
      result.Set<ElementType>(lambda(VectorElement<ElementType>(result, index),
                                     VectorElement<ElementType>(source, index)...), index);
    } else if constexpr (vma == InactiveProcessing::kAgnostic) {
      result.Set<ElementType>(fill_value, index);
    }
  }
  if constexpr (vta == TailProcessing::kAgnostic) {
    if (vl < static_cast<int>(16 / sizeof(ElementType))) {
#pragma clang loop unroll(disable)
      for (int index = vl; index < 16 / static_cast<int>(sizeof(ElementType)); ++index) {
        result.Set<ElementType>(fill_value, index);
      }
    }
  }
  return result;
}

template <typename ElementType>
inline ElementType mask_bits(ElementType val) {
  // Return only the low n-bits of val, where n is log2(SEW) and SEW is standard element width.
  if constexpr (sizeof(ElementType) <= sizeof(uint8_t)) {
    return val & ((1 << 3) - 1);
  } else if constexpr (sizeof(ElementType) <= sizeof(uint16_t)) {
    return val & ((1 << 4) - 1);
  } else if constexpr (sizeof(ElementType) <= sizeof(uint32_t)) {
    return val & ((1 << 5) - 1);
  } else if constexpr (sizeof(ElementType) <= sizeof(uint64_t)) {
    return val & ((1 << 6) - 1);
  } else {
    static_assert(kDependentTypeFalse<ElementType>, "Unsupported vector element type");
  }
}

#define DEFINE_ARITHMETIC_PARAMETERS_OR_ARGUMENTS(...) __VA_ARGS__
#define DEFINE_ARITHMETIC_INTRINSIC(Name, arithmetic, parameters, arguments)                      \
                                                                                                  \
  template <typename ElementType,                                                                 \
            TailProcessing vta,                                                                   \
            enum PreferredIntrinsicsImplementation = kUseAssemblerImplementationIfPossible>       \
  inline std::tuple<SIMD128Register> Name(int vstart,                                             \
                                          int vl,                                                 \
                                          SIMD128Register result,                                 \
                                          DEFINE_ARITHMETIC_PARAMETERS_OR_ARGUMENTS parameters) { \
    return VectorArithmetic<ElementType, vta>(                                                    \
        []([[maybe_unused]] auto vd, auto... args) {                                              \
          static_assert((std::is_same_v<decltype(args), ElementType> && ...));                    \
          arithmetic;                                                                             \
        },                                                                                        \
        vstart,                                                                                   \
        vl,                                                                                       \
        result,                                                                                   \
        DEFINE_ARITHMETIC_PARAMETERS_OR_ARGUMENTS arguments);                                     \
  }                                                                                               \
                                                                                                  \
  template <typename ElementType,                                                                 \
            TailProcessing vta,                                                                   \
            InactiveProcessing vma,                                                               \
            enum PreferredIntrinsicsImplementation = kUseAssemblerImplementationIfPossible>       \
  inline std::tuple<SIMD128Register> Name##m(                                                     \
      int vstart,                                                                                 \
      int vl,                                                                                     \
      int mask,                                                                                   \
      SIMD128Register result,                                                                     \
      DEFINE_ARITHMETIC_PARAMETERS_OR_ARGUMENTS parameters) {                                     \
    return VectorArithmetic<ElementType, vta, vma>(                                               \
        []([[maybe_unused]] auto vd, auto... args) {                                              \
          static_assert((std::is_same_v<decltype(args), ElementType> && ...));                    \
          arithmetic;                                                                             \
        },                                                                                        \
        vstart,                                                                                   \
        vl,                                                                                       \
        mask,                                                                                     \
        result,                                                                                   \
        DEFINE_ARITHMETIC_PARAMETERS_OR_ARGUMENTS arguments);                                     \
  }

#define DEFINE_2OP_ARITHMETIC_INTRINSIC_VV(name, ...)                 \
  DEFINE_ARITHMETIC_INTRINSIC(V##name##vv, return ({ __VA_ARGS__; }); \
                              , (SIMD128Register src1, SIMD128Register src2), (src1, src2))
#define DEFINE_2OP_ARITHMETIC_INTRINSIC_VX(name, ...)                 \
  DEFINE_ARITHMETIC_INTRINSIC(V##name##vx, return ({ __VA_ARGS__; }); \
                              , (SIMD128Register src1, ElementType src2), (src1, src2))

DEFINE_2OP_ARITHMETIC_INTRINSIC_VV(add, (args + ...))
DEFINE_2OP_ARITHMETIC_INTRINSIC_VX(add, (args + ...))
DEFINE_2OP_ARITHMETIC_INTRINSIC_VX(rsub, auto [arg1, arg2] = std::tuple{args...}; (arg2 - arg1))
DEFINE_2OP_ARITHMETIC_INTRINSIC_VV(sub, (args - ...))
DEFINE_2OP_ARITHMETIC_INTRINSIC_VX(sub, (args - ...))
DEFINE_2OP_ARITHMETIC_INTRINSIC_VV(and, (args & ...))
DEFINE_2OP_ARITHMETIC_INTRINSIC_VX(and, (args & ...))
DEFINE_2OP_ARITHMETIC_INTRINSIC_VV(or, (args | ...))
DEFINE_2OP_ARITHMETIC_INTRINSIC_VX(or, (args | ...))
DEFINE_2OP_ARITHMETIC_INTRINSIC_VV(xor, (args ^ ...))
DEFINE_2OP_ARITHMETIC_INTRINSIC_VX(xor, (args ^ ...))
DEFINE_2OP_ARITHMETIC_INTRINSIC_VV(mseq, (args == ...))
DEFINE_2OP_ARITHMETIC_INTRINSIC_VX(mseq, (args == ...))
DEFINE_2OP_ARITHMETIC_INTRINSIC_VV(msne, (args != ...))
DEFINE_2OP_ARITHMETIC_INTRINSIC_VX(msne, (args != ...))
DEFINE_2OP_ARITHMETIC_INTRINSIC_VV(mslt, (args < ...))
DEFINE_2OP_ARITHMETIC_INTRINSIC_VX(mslt, (args < ...))
DEFINE_2OP_ARITHMETIC_INTRINSIC_VV(msle, (args <= ...))
DEFINE_2OP_ARITHMETIC_INTRINSIC_VX(msle, (args <= ...))
DEFINE_2OP_ARITHMETIC_INTRINSIC_VX(msgt, (args > ...))
DEFINE_2OP_ARITHMETIC_INTRINSIC_VV(sll, auto [arg1, arg2] = std::tuple{args...};
                                   (arg1 << mask_bits(arg2)))
DEFINE_2OP_ARITHMETIC_INTRINSIC_VX(sll, auto [arg1, arg2] = std::tuple{args...};
                                   (arg1 << mask_bits(arg2)))
DEFINE_2OP_ARITHMETIC_INTRINSIC_VV(macc, auto [arg1, arg2] = std::tuple{args...};
                                   ((arg1 * arg2) + vd));
DEFINE_2OP_ARITHMETIC_INTRINSIC_VX(macc, auto [arg1, arg2] = std::tuple{args...};
                                   ((arg1 * arg2) + vd));
#undef DEFINE_ARITHMETIC_INTRINSIC
#undef DEFINE_ARITHMETIC_PARAMETERS_OR_ARGUMENTS

}  // namespace berberis::intrinsics

#endif  // BERBERIS_INTRINSICS_RISCV64_VECTOR_INTRINSICS_H_