okami.mom/strata

git clone https://git.nkp.pet/okami.mom/strata

Niko1221Merge branch 'f-139b-arc' into f-139-relad33b8074

main
124.0 KiB2807 linesraw
1//==---- math.hpp ---------------------------------*- C++ -*----------------==//
2//
3// Copyright (C) Intel Corporation
4// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
5// See https://llvm.org/LICENSE.txt for license information.
6//
7//===----------------------------------------------------------------------===//
8
9#ifndef __DPCT_MATH_HPP__
10#define __DPCT_MATH_HPP__
11
12#include <climits>
13#include <limits>
14#include <sycl/sycl.hpp>
15#include <type_traits>
16
17#include "detail/math_detail.hpp"
18
19namespace dpct {
20
21/// Compute fast_length for variable-length array
22/// \param [in] a The array
23/// \param [in] len Length of the array
24/// \returns The computed fast_length
25inline float fast_length(const float *a, int len) {
26  switch (len) {
27  case 1:
28    return a[0];
29  case 2:
30    return sycl::fast_length(sycl::float2(a[0], a[1]));
31  case 3:
32    return sycl::fast_length(sycl::float3(a[0], a[1], a[2]));
33  case 4:
34    return sycl::fast_length(sycl::float4(a[0], a[1], a[2], a[3]));
35  case 0:
36    return 0;
37  default:
38    float f = 0;
39    for (int i = 0; i < len; ++i)
40      f += a[i] * a[i];
41    return sycl::sqrt(f);
42  }
43}
44
45/// Calculate the square root of the input array.
46/// \param [in] a The array pointer
47/// \param [in] len Length of the array
48/// \returns The square root
49template <typename T> inline T length(const T *a, const int len) {
50  switch (len) {
51  case 1:
52    return a[0];
53  case 2:
54    return sycl::length(sycl::vec<T, 2>(a[0], a[1]));
55  case 3:
56    return sycl::length(sycl::vec<T, 3>(a[0], a[1], a[2]));
57  case 4:
58    return sycl::length(sycl::vec<T, 4>(a[0], a[1], a[2], a[3]));
59  default:
60    T ret = 0;
61    for (int i = 0; i < len; ++i)
62      ret += a[i] * a[i];
63    return sycl::sqrt(ret);
64  }
65}
66
67/// Returns min(max(val, min_val), max_val)
68/// \param [in] val The input value
69/// \param [in] min_val The minimum value
70/// \param [in] max_val The maximum value
71/// \returns the value between min_val and max_val
72template <typename T> inline T clamp(T val, T min_val, T max_val) {
73  return sycl::clamp(val, min_val, max_val);
74}
75#ifdef SYCL_EXT_ONEAPI_BFLOAT16_MATH_FUNCTIONS
76template <>
77inline sycl::ext::oneapi::bfloat16 clamp(sycl::ext::oneapi::bfloat16 val,
78                                         sycl::ext::oneapi::bfloat16 min_val,
79                                         sycl::ext::oneapi::bfloat16 max_val) {
80  if (val < min_val)
81    return min_val;
82  if (val > max_val)
83    return max_val;
84  return val;
85}
86template <>
87inline sycl::vec<sycl::ext::oneapi::bfloat16, 2>
88clamp(sycl::vec<sycl::ext::oneapi::bfloat16, 2> val,
89      sycl::vec<sycl::ext::oneapi::bfloat16, 2> min_val,
90      sycl::vec<sycl::ext::oneapi::bfloat16, 2> max_val) {
91  return {clamp(val[0], min_val[0], max_val[0]),
92          clamp(val[1], min_val[1], max_val[1])};
93}
94#endif
95template <typename T>
96inline sycl::marray<T, 2> clamp(sycl::marray<T, 2> val,
97                                sycl::marray<T, 2> min_val,
98                                sycl::marray<T, 2> max_val) {
99  return {clamp(val[0], min_val[0], max_val[0]),
100          clamp(val[1], min_val[1], max_val[1])};
101}
102
103/// Performs comparison.
104/// \param [in] a The first value
105/// \param [in] b The second value
106/// \param [in] binary_op functor that implements the binary operation
107/// \returns the comparison result
108template <typename T, class BinaryOperation>
109inline std::enable_if_t<
110    std::is_same_v<std::invoke_result_t<BinaryOperation, T, T>, bool>, bool>
111compare(const T a, const T b, const BinaryOperation binary_op) {
112  return binary_op(a, b);
113}
114template <typename T>
115inline std::enable_if_t<
116    std::is_same_v<std::invoke_result_t<std::not_equal_to<>, T, T>, bool>, bool>
117compare(const T a, const T b, const std::not_equal_to<> binary_op) {
118  return !detail::isnan(a) && !detail::isnan(b) && binary_op(a, b);
119}
120
121/// Performs unordered comparison.
122/// \param [in] a The first value
123/// \param [in] b The second value
124/// \param [in] binary_op functor that implements the binary operation
125/// \returns the comparison result
126template <typename T, class BinaryOperation>
127inline std::enable_if_t<
128    std::is_same_v<std::invoke_result_t<BinaryOperation, T, T>, bool>, bool>
129unordered_compare(const T a, const T b, const BinaryOperation binary_op) {
130  return detail::isnan(a) || detail::isnan(b) || binary_op(a, b);
131}
132
133/// Performs 2 element comparison and return true if both results are true.
134/// \param [in] a The first value
135/// \param [in] b The second value
136/// \param [in] binary_op functor that implements the binary operation
137/// \returns the comparison result
138template <typename T, class BinaryOperation>
139inline std::enable_if_t<T::size() == 2, bool>
140compare_both(const T a, const T b, const BinaryOperation binary_op) {
141  return compare(a[0], b[0], binary_op) && compare(a[1], b[1], binary_op);
142}
143
144/// Performs 2 element unordered comparison and return true if both results are
145/// true.
146/// \param [in] a The first value
147/// \param [in] b The second value
148/// \param [in] binary_op functor that implements the binary operation
149/// \returns the comparison result
150template <typename T, class BinaryOperation>
151inline std::enable_if_t<T::size() == 2, bool>
152unordered_compare_both(const T a, const T b, const BinaryOperation binary_op) {
153  return unordered_compare(a[0], b[0], binary_op) &&
154         unordered_compare(a[1], b[1], binary_op);
155}
156
157/// Performs 2 element comparison.
158/// \param [in] a The first value
159/// \param [in] b The second value
160/// \param [in] binary_op functor that implements the binary operation
161/// \returns the comparison result
162template <typename T, class BinaryOperation>
163inline std::enable_if_t<T::size() == 2, T>
164compare(const T a, const T b, const BinaryOperation binary_op) {
165  return {compare(a[0], b[0], binary_op), compare(a[1], b[1], binary_op)};
166}
167
168/// Performs 2 elements comparison, compare result of each element is 0 (false)
169/// or 0xffff (true), returns an unsigned int by composing compare result of two
170/// elements.
171/// \param [in] a The first value
172/// \param [in] b The second value
173/// \param [in] binary_op functor that implements the binary operation
174/// \returns the comparison result
175template <typename T, class BinaryOperation>
176inline unsigned compare_mask(const sycl::vec<T, 2> a, const sycl::vec<T, 2> b,
177                             const BinaryOperation binary_op) {
178  return sycl::vec<short, 2>(-compare(a[0], b[0], binary_op),
179                             -compare(a[1], b[1], binary_op))
180      .as<sycl::vec<unsigned, 1>>();
181}
182template <typename T, class BinaryOperation>
183inline unsigned compare_mask(const sycl::marray<T, 2> a,
184                             const sycl::marray<T, 2> b,
185                             const BinaryOperation binary_op) {
186  return sycl::vec<short, 2>(-compare(a[0], b[0], binary_op),
187                             -compare(a[1], b[1], binary_op))
188      .as<sycl::vec<unsigned, 1>>();
189}
190
191/// Performs 2 element unordered comparison.
192/// \param [in] a The first value
193/// \param [in] b The second value
194/// \param [in] binary_op functor that implements the binary operation
195/// \returns the comparison result
196template <typename T, class BinaryOperation>
197inline std::enable_if_t<T::size() == 2, T>
198unordered_compare(const T a, const T b, const BinaryOperation binary_op) {
199  return {unordered_compare(a[0], b[0], binary_op),
200          unordered_compare(a[1], b[1], binary_op)};
201}
202
203/// Performs 2 elements unordered comparison, compare result of each element is
204/// 0 (false) or 0xffff (true), returns an unsigned int by composing compare
205/// result of two elements.
206/// \param [in] a The first value
207/// \param [in] b The second value
208/// \param [in] binary_op functor that implements the binary operation
209/// \returns the comparison result
210template <typename T, class BinaryOperation>
211inline unsigned unordered_compare_mask(const sycl::vec<T, 2> a,
212                                       const sycl::vec<T, 2> b,
213                                       const BinaryOperation binary_op) {
214  return sycl::vec<short, 2>(-unordered_compare(a[0], b[0], binary_op),
215                             -unordered_compare(a[1], b[1], binary_op))
216      .as<sycl::vec<unsigned, 1>>();
217}
218template <typename T, class BinaryOperation>
219inline unsigned unordered_compare_mask(const sycl::marray<T, 2> a,
220                                       const sycl::marray<T, 2> b,
221                                       const BinaryOperation binary_op) {
222  return sycl::vec<short, 2>(-unordered_compare(a[0], b[0], binary_op),
223                             -unordered_compare(a[1], b[1], binary_op))
224      .as<sycl::vec<unsigned, 1>>();
225}
226
227/// Bitfield-extract.
228///
229/// \tparam T The type of \param source value, must be an integer.
230/// \param source The source value to extracting.
231/// \param bit_start The position to start extracting.
232/// \param num_bits The number of bits to extracting.
233template <typename T>
234inline std::enable_if_t<std::is_unsigned_v<T>, T>
235bfe(const T source, const uint32_t bit_start, const uint32_t num_bits) {
236  const T mask = (T{1} << num_bits) - 1;
237  return (source >> bit_start) & mask;
238}
239
240/// Bitfield-extract with boundary checking.
241///
242/// Extract bit field from \param source and return the zero or sign-extended
243/// result. Source \param bit_start gives the bit field starting bit position,
244/// and source \param num_bits gives the bit field length in bits.
245///
246/// The result is padded with the sign bit of the extracted field. If the start
247/// position is beyond the msb of the input, the result was filled with the
248/// replicated sign bit of the extracted field.
249///
250/// \tparam T The type of \param source value, must be an integer.
251/// \param source The source value to extracting.
252/// \param bit_start The position to start extracting.
253/// \param num_bits The number of bits to extracting.
254template <typename T>
255inline std::enable_if_t<std::is_integral_v<T>, T>
256bfe_safe(const T source, const uint32_t bit_start, const uint32_t num_bits) {
257#if defined(__SYCL_DEVICE_ONLY__) && defined(__NVPTX__)
258  if constexpr (std::is_same_v<T, int8_t> || std::is_same_v<T, int16_t> ||
259                std::is_same_v<T, int32_t>) {
260    int32_t res{};
261    asm volatile("bfe.s32 %0, %1, %2, %3;"
262                 : "=r"(res)
263                 : "r"((int32_t)source), "r"(bit_start), "r"(num_bits));
264    return res;
265  } else if constexpr (std::is_same_v<T, uint8_t> ||
266                       std::is_same_v<T, uint16_t> ||
267                       std::is_same_v<T, uint32_t>) {
268    uint32_t res{};
269    asm volatile("bfe.u32 %0, %1, %2, %3;"
270                 : "=r"(res)
271                 : "r"((uint32_t)source), "r"(bit_start), "r"(num_bits));
272    return res;
273  } else if constexpr (std::is_same_v<T, int64_t>) {
274    T res{};
275    asm volatile("bfe.s64 %0, %1, %2, %3;"
276                 : "=l"(res)
277                 : "l"(source), "r"(bit_start), "r"(num_bits));
278    return res;
279  } else if constexpr (std::is_same_v<T, uint64_t>) {
280    T res{};
281    asm volatile("bfe.u64 %0, %1, %2, %3;"
282                 : "=l"(res)
283                 : "l"(source), "r"(bit_start), "r"(num_bits));
284    return res;
285  }
286#endif
287  const uint32_t bit_width = CHAR_BIT * sizeof(T);
288  const uint32_t pos = (std::min)(bit_start, bit_width);
289  const uint32_t len = (std::min)(pos + num_bits, bit_width) - pos;
290  if constexpr (std::is_signed_v<T>) {
291    const T mask = (T{1} << len) - 1;
292
293    // Find the sign-bit, the result is padded with the sign bit of the
294    // extracted field.
295    //
296    // sign_bit = len == 0 ? 0 : source[min(pos + len - 1, bit_width - 1)]
297    const uint32_t sign_bit_pos = (std::min)(pos + len - 1, bit_width - 1);
298    const T sign_bit = len != 0 && ((source >> sign_bit_pos) & 1);
299    const T sign_bit_padding = (-sign_bit & ~mask);
300    return ((source >> pos) & mask) | sign_bit_padding;
301  } else {
302    return dpct::bfe(source, pos, len);
303  }
304}
305
306/// Bitfield-insert.
307///
308/// \tparam T The type of \param x and \param y , must be an unsigned integer.
309/// \param x The source of the bitfield.
310/// \param y The source where bitfield is inserted.
311/// \param bit_start The position to start insertion.
312/// \param num_bits The number of bits to insertion.
313template <typename T>
314inline std::enable_if_t<std::is_unsigned_v<T>, T>
315bfi(const T x, const T y, const uint32_t bit_start, const uint32_t num_bits) {
316  constexpr unsigned bit_width = CHAR_BIT * sizeof(T);
317
318  // if bit_start > bit_width || len == 0, should return y.
319  const uint32_t ignore_bfi = bit_start > bit_width || num_bits == 0;
320  T extract_bitfield_mask = (~(T{0}) >> (bit_width - num_bits)) << bit_start;
321  T clean_bitfield_mask = ~extract_bitfield_mask;
322  return (y & (-ignore_bfi | clean_bitfield_mask)) |
323         (~-ignore_bfi & ((x << bit_start) & extract_bitfield_mask));
324}
325
326/// Bitfield-insert with boundary checking.
327///
328/// Align and insert a bit field from \param x into \param y . Source \param
329/// bit_start gives the starting bit position for the insertion, and source
330/// \param num_bits gives the bit field length in bits.
331///
332/// \tparam T The type of \param x and \param y , must be an unsigned integer.
333/// \param x The source of the bitfield.
334/// \param y The source where bitfield is inserted.
335/// \param bit_start The position to start insertion.
336/// \param num_bits The number of bits to insertion.
337template <typename T>
338inline std::enable_if_t<std::is_unsigned_v<T>, T>
339bfi_safe(const T x, const T y, const uint32_t bit_start,
340         const uint32_t num_bits) {
341#if defined(__SYCL_DEVICE_ONLY__) && defined(__NVPTX__)
342  if constexpr (std::is_same_v<T, uint8_t> || std::is_same_v<T, uint16_t> ||
343                std::is_same_v<T, uint32_t>) {
344    uint32_t res{};
345    asm volatile("bfi.b32 %0, %1, %2, %3, %4;"
346                 : "=r"(res)
347                 : "r"((uint32_t)x), "r"((uint32_t)y), "r"(bit_start),
348                   "r"(num_bits));
349    return res;
350  } else if constexpr (std::is_same_v<T, uint64_t>) {
351    uint64_t res{};
352    asm volatile("bfi.b64 %0, %1, %2, %3, %4;"
353                 : "=l"(res)
354                 : "l"(x), "l"(y), "r"(bit_start), "r"(num_bits));
355    return res;
356  }
357#endif
358  constexpr unsigned bit_width = CHAR_BIT * sizeof(T);
359  const uint32_t pos = (std::min)(bit_start, bit_width);
360  const uint32_t len = (std::min)(pos + num_bits, bit_width) - pos;
361  return dpct::bfi(x, y, pos, len);
362}
363
364/// Determine whether 2 element value is NaN.
365/// \param [in] a The input value
366/// \returns the comparison result
367template <typename T>
368inline std::enable_if_t<T::size() == 2, T> isnan(const T a) {
369  return {detail::isnan(a[0]), detail::isnan(a[1])};
370}
371
372/// Emulated function for __funnelshift_l
373inline unsigned int funnelshift_l(unsigned int low, unsigned int high,
374                                  unsigned int shift) {
375  return (sycl::upsample(high, low) << (shift & 31U)) >> 32;
376}
377
378/// Emulated function for __funnelshift_lc
379inline unsigned int funnelshift_lc(unsigned int low, unsigned int high,
380                                   unsigned int shift) {
381  return (sycl::upsample(high, low) << sycl::min(shift, 32U)) >> 32;
382}
383
384/// Emulated function for __funnelshift_r
385inline unsigned int funnelshift_r(unsigned int low, unsigned int high,
386                                  unsigned int shift) {
387  return (sycl::upsample(high, low) >> (shift & 31U)) & 0xFFFFFFFF;
388}
389
390/// Emulated function for __funnelshift_rc
391inline unsigned int funnelshift_rc(unsigned int low, unsigned int high,
392                                   unsigned int shift) {
393  return (sycl::upsample(high, low) >> sycl::min(shift, 32U)) & 0xFFFFFFFF;
394}
395
396/// cbrt function wrapper.
397template <typename T> inline T cbrt(T val) { return sycl::cbrt((T)val); }
398
399template <typename T1, typename T2>
400std::enable_if_t<std::is_integral_v<T1> && std::is_integral_v<T2>,
401                 std::common_type_t<T1, T2>>
402min(T1 a, T2 b) {
403  using common_t = std::common_type_t<T1, T2>;
404  return sycl::min(static_cast<common_t>(a), static_cast<common_t>(b));
405}
406template <typename T1, typename T2>
407std::enable_if_t<std::is_floating_point_v<T1> && std::is_floating_point_v<T2>,
408                 std::common_type_t<T1, T2>>
409min(T1 a, T2 b) {
410  using common_t = std::common_type_t<T1, T2>;
411  return sycl::fmin(static_cast<common_t>(a), static_cast<common_t>(b));
412}
413template <typename T1, typename T2>
414std::enable_if_t<std::is_integral_v<T1> && std::is_integral_v<T2>,
415                 std::common_type_t<T1, T2>>
416max(T1 a, T2 b) {
417  using common_t = std::common_type_t<T1, T2>;
418  return sycl::max(static_cast<common_t>(a), static_cast<common_t>(b));
419}
420template <typename T1, typename T2>
421std::enable_if_t<std::is_floating_point_v<T1> && std::is_floating_point_v<T2>,
422                 std::common_type_t<T1, T2>>
423max(T1 a, T2 b) {
424  using common_t = std::common_type_t<T1, T2>;
425  return sycl::fmax(static_cast<common_t>(a), static_cast<common_t>(b));
426}
427
428// pow functions overload.
429inline float pow(const float a, const int b) { return sycl::pown(a, b); }
430inline double pow(const double a, const int b) { return sycl::pown(a, b); }
431inline float pow(const float a, const float b) { return sycl::pow(a, b); }
432inline double pow(const double a, const double b) { return sycl::pow(a, b); }
433template <typename T, typename U>
434inline typename std::enable_if_t<std::is_floating_point_v<T>, T>
435pow(const T a, const U b) {
436  return sycl::pow(a, static_cast<T>(b));
437}
438template <typename T, typename U>
439inline typename std::enable_if_t<!std::is_floating_point_v<T>, double>
440pow(const T a, const U b) {
441  return sycl::pow(static_cast<double>(a), static_cast<double>(b));
442}
443
444/// Performs relu saturation.
445/// \param [in] a The input value
446/// \returns the relu saturation result
447template <typename T> inline T relu(T a) {
448  T zero{};
449  if constexpr (detail::is_floating_point<T>)
450    return !detail::isnan(a) && a < zero ? zero : a;
451  else
452    return a < zero ? zero : a;
453}
454template <typename T, int N>
455inline sycl::vec<T, N> relu(const sycl::vec<T, N> a) {
456  sycl::vec<T, N> ret;
457  for (int i = 0; i < N; ++i)
458    ret[i] = relu(a[i]);
459  return ret;
460}
461template <class T> inline sycl::marray<T, 2> relu(const sycl::marray<T, 2> a) {
462  return {relu(a[0]), relu(a[1])};
463}
464
465/// Performs complex number multiply addition.
466/// \param [in] a The first value
467/// \param [in] b The second value
468/// \param [in] c The third value
469/// \returns the operation result
470template <typename T>
471inline sycl::vec<T, 2> complex_mul_add(const sycl::vec<T, 2> a,
472                                       const sycl::vec<T, 2> b,
473                                       const sycl::vec<T, 2> c) {
474  return sycl::vec<T, 2>{a[0] * b[0] - a[1] * b[1] + c[0],
475                         a[0] * b[1] + a[1] * b[0] + c[1]};
476}
477template <typename T>
478inline sycl::marray<T, 2> complex_mul_add(const sycl::marray<T, 2> a,
479                                          const sycl::marray<T, 2> b,
480                                          const sycl::marray<T, 2> c) {
481  return sycl::marray<T, 2>{a[0] * b[0] - a[1] * b[1] + c[0],
482                            a[0] * b[1] + a[1] * b[0] + c[1]};
483}
484
485/// Performs 2 elements comparison and returns the bigger one. If either of
486/// inputs is NaN, then return NaN.
487/// \param [in] a The first value
488/// \param [in] b The second value
489/// \returns the bigger value
490template <typename T> inline T fmax_nan(const T a, const T b) {
491  if (detail::isnan(a) || detail::isnan(b))
492    return NAN;
493  return sycl::fmax(a, b);
494}
495#ifdef SYCL_EXT_ONEAPI_BFLOAT16_MATH_FUNCTIONS
496template <>
497inline sycl::ext::oneapi::bfloat16
498fmax_nan(const sycl::ext::oneapi::bfloat16 a,
499         const sycl::ext::oneapi::bfloat16 b) {
500  if (detail::isnan(a) || detail::isnan(b))
501    return NAN;
502  return sycl::fmax(float(a), float(b));
503}
504#endif
505template <typename T>
506inline sycl::vec<T, 2> fmax_nan(const sycl::vec<T, 2> a,
507                                const sycl::vec<T, 2> b) {
508  return {fmax_nan(a[0], b[0]), fmax_nan(a[1], b[1])};
509}
510template <typename T>
511inline sycl::marray<T, 2> fmax_nan(const sycl::marray<T, 2> a,
512                                   const sycl::marray<T, 2> b) {
513  return {fmax_nan(a[0], b[0]), fmax_nan(a[1], b[1])};
514}
515
516/// Performs 2 elements comparison and returns the smaller one. If either of
517/// inputs is NaN, then return NaN.
518/// \param [in] a The first value
519/// \param [in] b The second value
520/// \returns the smaller value
521template <typename T> inline T fmin_nan(const T a, const T b) {
522  if (detail::isnan(a) || detail::isnan(b))
523    return NAN;
524  return sycl::fmin(a, b);
525}
526#ifdef SYCL_EXT_ONEAPI_BFLOAT16_MATH_FUNCTIONS
527template <>
528inline sycl::ext::oneapi::bfloat16
529fmin_nan(const sycl::ext::oneapi::bfloat16 a,
530         const sycl::ext::oneapi::bfloat16 b) {
531  if (detail::isnan(a) || detail::isnan(b))
532    return NAN;
533  return sycl::fmin(float(a), float(b));
534}
535#endif
536template <typename T>
537inline sycl::vec<T, 2> fmin_nan(const sycl::vec<T, 2> a,
538                                const sycl::vec<T, 2> b) {
539  return {fmin_nan(a[0], b[0]), fmin_nan(a[1], b[1])};
540}
541template <typename T>
542inline sycl::marray<T, 2> fmin_nan(const sycl::marray<T, 2> a,
543                                   const sycl::marray<T, 2> b) {
544  return {fmin_nan(a[0], b[0]), fmin_nan(a[1], b[1])};
545}
546
547/// A sycl::abs wrapper functors.
548struct abs {
549  template <typename T> auto operator()(const T x) const {
550    return sycl::abs(x);
551  }
552};
553
554/// A sycl::abs_diff wrapper functors.
555struct abs_diff {
556  template <typename T> auto operator()(const T x, const T y) const {
557    return sycl::abs_diff(x, y);
558  }
559};
560
561/// A sycl::add_sat wrapper functors.
562struct add_sat {
563  template <typename T> auto operator()(const T x, const T y) const {
564    return sycl::add_sat(x, y);
565  }
566};
567
568/// A sycl::rhadd wrapper functors.
569struct rhadd {
570  template <typename T> auto operator()(const T x, const T y) const {
571    return sycl::rhadd(x, y);
572  }
573};
574
575/// A sycl::hadd wrapper functors.
576struct hadd {
577  template <typename T> auto operator()(const T x, const T y) const {
578    return sycl::hadd(x, y);
579  }
580};
581
582/// A sycl::max wrapper functors.
583struct maximum {
584  template <typename T> auto operator()(const T x, const T y) const {
585    return sycl::max(x, y);
586  }
587  template <typename T>
588  auto operator()(const T x, const T y, bool *pred) const {
589    return (x >= y) ? ((*pred = true), x) : ((*pred = false), y);
590  }
591};
592
593/// A sycl::min wrapper functors.
594struct minimum {
595  template <typename T> auto operator()(const T x, const T y) const {
596    return sycl::min(x, y);
597  }
598  template <typename T>
599  auto operator()(const T x, const T y, bool *pred) const {
600    return (x <= y) ? ((*pred = true), x) : ((*pred = false), y);
601  }
602};
603
604/// A sycl::sub_sat wrapper functors.
605struct sub_sat {
606  template <typename T> auto operator()(const T x, const T y) const {
607    return sycl::sub_sat(x, y);
608  }
609};
610
611/// Compute vectorized binary operation value for two values, with each value
612/// treated as a vector type \p VecT.
613/// \tparam [in] VecT The type of the vector
614/// \tparam [in] BinaryOperation The binary operation class
615/// \param [in] a The first value
616/// \param [in] b The second value
617/// \param [in] binary_op The operation to do with the two values
618/// \param [in] need_relu Whether the result need relu saturation
619/// \returns The vectorized binary operation value of the two values
620template <typename VecT, class BinaryOperation>
621inline unsigned vectorized_binary(unsigned a, unsigned b,
622                                  const BinaryOperation binary_op,
623                                  bool need_relu = false) {
624  sycl::vec<unsigned, 1> v0{a}, v1{b};
625  auto v2 = v0.as<VecT>();
626  auto v3 = v1.as<VecT>();
627  auto v4 =
628      detail::vectorized_binary<VecT, BinaryOperation>()(v2, v3, binary_op);
629  if (need_relu)
630    v4 = relu(v4);
631  v0 = v4.template as<sycl::vec<unsigned, 1>>();
632  return v0;
633}
634
635/// Compute vectorized binary operation value with pred for two values, with
636/// each value treated as a 2 \p T type elements vector type.
637///
638/// \tparam [in] VecT The type of the vector
639/// \tparam [in] BinaryOperation The binary operation class
640/// \param [in] a The first value
641/// \param [in] b The second value
642/// \param [in] binary_op The operation with pred to do with the two values
643/// \param [out] pred_hi The pred pointer that pass into high halfword operation
644/// \param [out] pred_lo The pred pointer that pass into low halfword operation
645/// \returns The vectorized binary operation value of the two values
646template <typename VecT, typename BinaryOperation>
647inline unsigned vectorized_binary_with_pred(unsigned a, unsigned b,
648                                            const BinaryOperation binary_op,
649                                            bool *pred_hi, bool *pred_lo) {
650  auto v1 = sycl::vec<unsigned, 1>(a).as<VecT>();
651  auto v2 = sycl::vec<unsigned, 1>(b).as<VecT>();
652  VecT ret;
653  ret[0] = binary_op(v1[0], v2[0], pred_lo);
654  ret[1] = binary_op(v1[1], v2[1], pred_hi);
655  return ret.template as<sycl::vec<unsigned, 1>>();
656}
657
658/// Compute vectorized isgreater for two values, with each value treated as a
659/// vector type \p S.
660/// \tparam [in] S The type of the vector
661/// \tparam [in] T The type of the original values
662/// \param [in] a The first value
663/// \param [in] b The second value
664/// \returns The vectorized greater than of the two values
665template <typename S, typename T> inline T vectorized_isgreater(T a, T b) {
666  sycl::vec<T, 1> v0{a}, v1{b};
667  auto v2 = v0.template as<S>();
668  auto v3 = v1.template as<S>();
669  auto v4 = v2 > v3;
670  v0 = v4.template as<sycl::vec<T, 1>>();
671  return v0;
672}
673
674/// Compute vectorized max for two values, with each value treated as a vector
675/// type \p S.
676/// \tparam [in] S The type of the vector
677/// \tparam [in] T The type of the original values
678/// \param [in] a The first value
679/// \param [in] b The second value
680/// \returns The vectorized max of the two values
681template <typename S, typename T> inline T vectorized_max(T a, T b) {
682  sycl::vec<T, 1> v0{a}, v1{b};
683  auto v2 = v0.template as<S>();
684  auto v3 = v1.template as<S>();
685  auto v4 = sycl::max(v2, v3);
686  v0 = v4.template as<sycl::vec<T, 1>>();
687  return v0;
688}
689
690/// Compute vectorized min for two values, with each value treated as a vector
691/// type \p S.
692/// \tparam [in] S The type of the vector
693/// \tparam [in] T The type of the original values
694/// \param [in] a The first value
695/// \param [in] b The second value
696/// \returns The vectorized min of the two values
697template <typename S, typename T> inline T vectorized_min(T a, T b) {
698  sycl::vec<T, 1> v0{a}, v1{b};
699  auto v2 = v0.template as<S>();
700  auto v3 = v1.template as<S>();
701  auto v4 = sycl::min(v2, v3);
702  v0 = v4.template as<sycl::vec<T, 1>>();
703  return v0;
704}
705
706/// Compute vectorized unary operation for a value, with the value treated as a
707/// vector type \p VecT.
708/// \tparam [in] VecT The type of the vector
709/// \tparam [in] UnaryOperation The unary operation class
710/// \param [in] a The input value
711/// \returns The vectorized unary operation value of the input value
712template <typename VecT, class UnaryOperation>
713inline unsigned vectorized_unary(unsigned a, const UnaryOperation unary_op) {
714  sycl::vec<unsigned, 1> v0{a};
715  auto v1 = v0.as<VecT>();
716  auto v2 = unary_op(v1);
717  v0 = v2.template as<sycl::vec<unsigned, 1>>();
718  return v0;
719}
720
721/// Compute vectorized absolute difference for two values without modulo
722/// overflow, with each value treated as a vector type \p VecT.
723/// \tparam [in] VecT The type of the vector
724/// \param [in] a The first value
725/// \param [in] b The second value
726/// \returns The vectorized absolute difference of the two values
727template <typename VecT>
728inline unsigned vectorized_sum_abs_diff(unsigned a, unsigned b) {
729  sycl::vec<unsigned, 1> v0{a}, v1{b};
730  // Need convert element type to wider signed type to avoid overflow.
731  auto v2 = v0.as<VecT>().template convert<int>();
732  auto v3 = v1.as<VecT>().template convert<int>();
733  auto v4 = sycl::abs_diff(v2, v3);
734  unsigned sum = 0;
735  for (size_t i = 0; i < v4.size(); ++i) {
736    sum += v4[i];
737  }
738  return sum;
739}
740
741/// Compute two vectorized binary operation value with pred for three values,
742/// with each value treated as a 2 \p T type elements vector type.
743///
744/// \tparam [in] VecT The type of the vector
745/// \tparam [in] BinaryOperation1 The first binary operation class
746/// \tparam [in] BinaryOperation2 The second binary operation class
747/// \param [in] a The first value
748/// \param [in] b The second value
749/// \param [in] c The third value
750/// \param [in] binary_op1 The first operation to do with the first two values
751/// \param [in] binary_op2 The second operation to do with the third values
752/// \param [in] need_relu Whether the result need relu saturation
753/// \returns The two vectorized binary operation value of the three values
754template <typename VecT, typename BinaryOperation1, typename BinaryOperation2>
755inline unsigned vectorized_ternary(unsigned a, unsigned b, unsigned c,
756                                   const BinaryOperation1 binary_op1,
757                                   const BinaryOperation2 binary_op2,
758                                   bool need_relu = false) {
759  const auto v1 = sycl::vec<unsigned, 1>(a).as<VecT>();
760  const auto v2 = sycl::vec<unsigned, 1>(b).as<VecT>();
761  const auto v3 = sycl::vec<unsigned, 1>(c).as<VecT>();
762  auto v4 =
763      detail::vectorized_binary<VecT, BinaryOperation1>()(v1, v2, binary_op1);
764  v4 = detail::vectorized_binary<VecT, BinaryOperation2>()(v4, v3, binary_op2);
765  if (need_relu)
766    v4 = relu(v4);
767  return v4.template as<sycl::vec<unsigned, 1>>();
768}
769
770/// Two-way dot product-accumulate. Calculate and return interger_vector2(
771/// \param a) dot product interger_vector2(low16_bit( \param b))  + \param c
772///
773/// \tparam [in] T1 The type of first value.
774/// \tparam [in] T2 The type of second value.
775/// \param [in] a The first value.
776/// \param [in] b The second value.
777/// \param [in] c The third value. It has type uint32_t if both T1 and T1 are
778/// uint32_t else has type int32_t.
779/// \return Two-way 16-bit to 8-bit dot product which is accumulated in 32-bit
780/// result.
781template <typename T1, typename T2, typename T3>
782inline auto dp2a_lo(T1 a, T2 b, T3 c) {
783  detail::dot_product_acc_t<T1, T2> res = c;
784#if defined(__SYCL_DEVICE_ONLY__) && defined(__NVPTX__) &&                     \
785    defined(__CUDA_ARCH__) && __CUDA_ARCH__ >= 610
786  res = __dp2a_lo(a, b, c);
787#else
788  auto va = ::dpct::detail::extract_and_sign_or_zero_extend2(a);
789  auto vb = ::dpct::detail::extract_and_sign_or_zero_extend4(b);
790  res += va[0] * vb[0];
791  res += va[1] * vb[1];
792#endif
793  return res;
794}
795
796/// Two-way dot product-accumulate. Calculate and return interger_vector2(
797/// \param a) dot product interger_vector2(high_16bit( \param b)) + \param c
798///
799/// \tparam [in] T1 The type of first value.
800/// \tparam [in] T2 The type of second value.
801/// \param [in] a The first value.
802/// \param [in] b The second value.
803/// \param [in] c The third value. It has type uint32_t if both T1 and T1 are
804/// uint32_t else has type int32_t.
805/// \return Two-way 16-bit to 8-bit dot product which is accumulated in 32-bit
806/// result.
807template <typename T1, typename T2, typename T3>
808inline auto dp2a_hi(T1 a, T2 b, T3 c) {
809  detail::dot_product_acc_t<T1, T2> res = c;
810#if defined(__SYCL_DEVICE_ONLY__) && defined(__NVPTX__) &&                     \
811    defined(__CUDA_ARCH__) && __CUDA_ARCH__ >= 610
812  res = __dp2a_hi(a, b, c);
813#else
814  auto va = ::dpct::detail::extract_and_sign_or_zero_extend2(a);
815  auto vb = ::dpct::detail::extract_and_sign_or_zero_extend4(b);
816  res += va[0] * vb[2];
817  res += va[1] * vb[3];
818#endif
819  return res;
820}
821
822/// Four-way byte dot product-accumulate. Calculate and return interger_vector4(
823/// \param a) dot product interger_vector4( \param b)  + \param c
824///
825/// \tparam [in] T1 The type of first value.
826/// \tparam [in] T2 The type of second value.
827/// \param [in] a The first value.
828/// \param [in] b The second value.
829/// \param [in] c The third value. It has type uint32_t if both T1 and T1 are
830/// uint32_t else has type int32_t.
831/// \return Four-way byte dot product which is accumulated in 32-bit result.
832template <typename T1, typename T2, typename T3>
833inline auto dp4a(T1 a, T2 b, T3 c) {
834  detail::dot_product_acc_t<T1, T2> res = c;
835#if defined(__SYCL_DEVICE_ONLY__) && defined(__NVPTX__) &&                     \
836    defined(__CUDA_ARCH__) && __CUDA_ARCH__ >= 610
837  res = __dp4a(a, b, c);
838#else
839  auto va = ::dpct::detail::extract_and_sign_or_zero_extend4(a);
840  auto vb = ::dpct::detail::extract_and_sign_or_zero_extend4(b);
841  res += va[0] * vb[0];
842  res += va[1] * vb[1];
843  res += va[2] * vb[2];
844  res += va[3] * vb[3];
845#endif
846  return res;
847}
848
849/// Extend \p a and \p b to 33 bit and add them.
850/// \tparam [in] RetT The type of the return value, can only be 32 bit integer
851/// \tparam [in] AT The type of the first value, can only be 32 bit integer
852/// \tparam [in] BT The type of the second value, can only be 32 bit integer
853/// \param [in] a The first value
854/// \param [in] b The second value
855/// \returns The extend addition of the two values
856template <typename RetT, typename AT, typename BT>
857inline constexpr RetT extend_add(AT a, BT b) {
858  return detail::extend_binary<RetT, false>(a, b, std::plus());
859}
860
861/// Extend Inputs to 33 bit, add \p a, \p b, then do \p second_op with \p c.
862/// \tparam [in] RetT The type of the return value, can only be 32 bit integer
863/// \tparam [in] AT The type of the first value, can only be 32 bit integer
864/// \tparam [in] BT The type of the second value, can only be 32 bit integer
865/// \tparam [in] CT The type of the third value, can only be 32 bit integer
866/// \tparam [in] BinaryOperation The type of the second operation
867/// \param [in] a The first value
868/// \param [in] b The second value
869/// \param [in] c The third value
870/// \param [in] second_op The operation to do with the third value
871/// \returns The extend addition of \p a, \p b and \p second_op with \p c
872template <typename RetT, typename AT, typename BT, typename CT,
873          typename BinaryOperation>
874inline constexpr RetT extend_add(AT a, BT b, CT c, BinaryOperation second_op) {
875  return detail::extend_binary<RetT, false>(a, b, c, std::plus(), second_op);
876}
877
878/// Extend \p a and \p b to 33 bit and add them with saturation.
879/// \tparam [in] RetT The type of the return value, can only be 32 bit integer
880/// \tparam [in] AT The type of the first value, can only be 32 bit integer
881/// \tparam [in] BT The type of the second value, can only be 32 bit integer
882/// \param [in] a The first value
883/// \param [in] b The second value
884/// \returns The extend addition of the two values with saturation
885template <typename RetT, typename AT, typename BT>
886inline constexpr RetT extend_add_sat(AT a, BT b) {
887  return detail::extend_binary<RetT, true>(a, b, std::plus());
888}
889
890/// Extend Inputs to 33 bit, add \p a, \p b with saturation, then do \p
891/// second_op with \p c.
892/// \tparam [in] RetT The type of the return value, can only be 32 bit integer
893/// \tparam [in] AT The type of the first value, can only be 32 bit integer
894/// \tparam [in] BT The type of the second value, can only be 32 bit integer
895/// \tparam [in] CT The type of the third value, can only be 32 bit integer
896/// \tparam [in] BinaryOperation The type of the second operation
897/// \param [in] a The first value
898/// \param [in] b The second value
899/// \param [in] c The third value
900/// \param [in] second_op The operation to do with the third value
901/// \returns The extend addition of \p a, \p b with saturation and \p second_op
902/// with \p c
903template <typename RetT, typename AT, typename BT, typename CT,
904          typename BinaryOperation>
905inline constexpr RetT extend_add_sat(AT a, BT b, CT c,
906                                     BinaryOperation second_op) {
907  return detail::extend_binary<RetT, true>(a, b, c, std::plus(), second_op);
908}
909
910/// Extend \p a and \p b to 33 bit and minus them.
911/// \tparam [in] RetT The type of the return value, can only be 32 bit integer
912/// \tparam [in] AT The type of the first value, can only be 32 bit integer
913/// \tparam [in] BT The type of the second value, can only be 32 bit integer
914/// \param [in] a The first value
915/// \param [in] b The second value
916/// \returns The extend subtraction of the two values
917template <typename RetT, typename AT, typename BT>
918inline constexpr RetT extend_sub(AT a, BT b) {
919  return detail::extend_binary<RetT, false>(a, b, std::minus());
920}
921
922/// Extend Inputs to 33 bit, minus \p a, \p b, then do \p second_op with \p c.
923/// \tparam [in] RetT The type of the return value, can only be 32 bit integer
924/// \tparam [in] AT The type of the first value, can only be 32 bit integer
925/// \tparam [in] BT The type of the second value, can only be 32 bit integer
926/// \tparam [in] CT The type of the third value, can only be 32 bit integer
927/// \tparam [in] BinaryOperation The type of the second operation
928/// \param [in] a The first value
929/// \param [in] b The second value
930/// \param [in] c The third value
931/// \param [in] second_op The operation to do with the third value
932/// \returns The extend subtraction of \p a, \p b and \p second_op with \p c
933template <typename RetT, typename AT, typename BT, typename CT,
934          typename BinaryOperation>
935inline constexpr RetT extend_sub(AT a, BT b, CT c, BinaryOperation second_op) {
936  return detail::extend_binary<RetT, false>(a, b, c, std::minus(), second_op);
937}
938
939/// Extend \p a and \p b to 33 bit and minus them with saturation.
940/// \tparam [in] RetT The type of the return value, can only be 32 bit integer
941/// \tparam [in] AT The type of the first value, can only be 32 bit integer
942/// \tparam [in] BT The type of the second value, can only be 32 bit integer
943/// \param [in] a The first value
944/// \param [in] b The second value
945/// \returns The extend subtraction of the two values with saturation
946template <typename RetT, typename AT, typename BT>
947inline constexpr RetT extend_sub_sat(AT a, BT b) {
948  return detail::extend_binary<RetT, true>(a, b, std::minus());
949}
950
951/// Extend Inputs to 33 bit, minus \p a, \p b with saturation, then do \p
952/// second_op with \p c.
953/// \tparam [in] RetT The type of the return value, can only be 32 bit integer
954/// \tparam [in] AT The type of the first value, can only be 32 bit integer
955/// \tparam [in] BT The type of the second value, can only be 32 bit integer
956/// \tparam [in] CT The type of the third value, can only be 32 bit integer
957/// \tparam [in] BinaryOperation The type of the second operation
958/// \param [in] a The first value
959/// \param [in] b The second value
960/// \param [in] c The third value
961/// \param [in] second_op The operation to do with the third value
962/// \returns The extend subtraction of \p a, \p b with saturation and \p
963/// second_op with \p c
964template <typename RetT, typename AT, typename BT, typename CT,
965          typename BinaryOperation>
966inline constexpr RetT extend_sub_sat(AT a, BT b, CT c,
967                                     BinaryOperation second_op) {
968  return detail::extend_binary<RetT, true>(a, b, c, std::minus(), second_op);
969}
970
971/// Extend \p a and \p b to 33 bit and do abs_diff.
972/// \tparam [in] RetT The type of the return value, can only be 32 bit integer
973/// \tparam [in] AT The type of the first value, can only be 32 bit integer
974/// \tparam [in] BT The type of the second value, can only be 32 bit integer
975/// \param [in] a The first value
976/// \param [in] b The second value
977/// \returns The extend abs_diff of the two values
978template <typename RetT, typename AT, typename BT>
979inline constexpr RetT extend_absdiff(AT a, BT b) {
980  return detail::extend_binary<RetT, false>(a, b, abs_diff());
981}
982
983/// Extend Inputs to 33 bit, abs_diff \p a, \p b, then do \p second_op with \p
984/// c.
985/// \tparam [in] RetT The type of the return value, can only be 32 bit integer
986/// \tparam [in] AT The type of the first value, can only be 32 bit integer
987/// \tparam [in] BT The type of the second value, can only be 32 bit integer
988/// \tparam [in] CT The type of the third value, can only be 32 bit integer
989/// \tparam [in] BinaryOperation The type of the second operation
990/// \param [in] a The first value
991/// \param [in] b The second value
992/// \param [in] c The third value
993/// \param [in] second_op The operation to do with the third value
994/// \returns The extend abs_diff of \p a, \p b and \p second_op with \p c
995template <typename RetT, typename AT, typename BT, typename CT,
996          typename BinaryOperation>
997inline constexpr RetT extend_absdiff(AT a, BT b, CT c,
998                                     BinaryOperation second_op) {
999  return detail::extend_binary<RetT, false>(a, b, c, abs_diff(), second_op);
1000}
1001
1002/// Extend \p a and \p b to 33 bit and do abs_diff with saturation.
1003/// \tparam [in] RetT The type of the return value, can only be 32 bit integer
1004/// \tparam [in] AT The type of the first value, can only be 32 bit integer
1005/// \tparam [in] BT The type of the second value, can only be 32 bit integer
1006/// \param [in] a The first value
1007/// \param [in] b The second value
1008/// \returns The extend abs_diff of the two values with saturation
1009template <typename RetT, typename AT, typename BT>
1010inline constexpr RetT extend_absdiff_sat(AT a, BT b) {
1011  return detail::extend_binary<RetT, true>(a, b, abs_diff());
1012}
1013
1014/// Extend Inputs to 33 bit, abs_diff \p a, \p b with saturation, then do \p
1015/// second_op with \p c.
1016/// \tparam [in] RetT The type of the return value, can only be 32 bit integer
1017/// \tparam [in] AT The type of the first value, can only be 32 bit integer
1018/// \tparam [in] BT The type of the second value, can only be 32 bit integer
1019/// \tparam [in] CT The type of the third value, can only be 32 bit integer
1020/// \tparam [in] BinaryOperation The type of the second operation
1021/// \param [in] a The first value
1022/// \param [in] b The second value
1023/// \param [in] c The third value
1024/// \param [in] second_op The operation to do with the third value
1025/// \returns The extend abs_diff of \p a, \p b with saturation and \p
1026/// second_op with \p c
1027template <typename RetT, typename AT, typename BT, typename CT,
1028          typename BinaryOperation>
1029inline constexpr RetT extend_absdiff_sat(AT a, BT b, CT c,
1030                                         BinaryOperation second_op) {
1031  return detail::extend_binary<RetT, true>(a, b, c, abs_diff(), second_op);
1032}
1033
1034/// Extend \p a and \p b to 33 bit and return smaller one.
1035/// \tparam [in] RetT The type of the return value, can only be 32 bit integer
1036/// \tparam [in] AT The type of the first value, can only be 32 bit integer
1037/// \tparam [in] BT The type of the second value, can only be 32 bit integer
1038/// \param [in] a The first value
1039/// \param [in] b The second value
1040/// \returns The smaller one of the two extended values
1041template <typename RetT, typename AT, typename BT>
1042inline constexpr RetT extend_min(AT a, BT b) {
1043  return detail::extend_binary<RetT, false>(a, b, minimum());
1044}
1045
1046/// Extend Inputs to 33 bit, find the smaller one in \p a, \p b, then do \p
1047/// second_op with \p c.
1048/// \tparam [in] RetT The type of the return value, can only be 32 bit integer
1049/// \tparam [in] AT The type of the first value, can only be 32 bit integer
1050/// \tparam [in] BT The type of the second value, can only be 32 bit integer
1051/// \tparam [in] CT The type of the third value, can only be 32 bit integer
1052/// \tparam [in] BinaryOperation The type of the second operation
1053/// \param [in] a The first value
1054/// \param [in] b The second value
1055/// \param [in] c The third value
1056/// \param [in] second_op The operation to do with the third value
1057/// \returns The smaller one of \p a, \p b and \p second_op with \p c
1058template <typename RetT, typename AT, typename BT, typename CT,
1059          typename BinaryOperation>
1060inline constexpr RetT extend_min(AT a, BT b, CT c, BinaryOperation second_op) {
1061  return detail::extend_binary<RetT, false>(a, b, c, minimum(), second_op);
1062}
1063
1064/// Extend \p a and \p b to 33 bit and return smaller one with saturation.
1065/// \tparam [in] RetT The type of the return value, can only be 32 bit integer
1066/// \tparam [in] AT The type of the first value, can only be 32 bit integer
1067/// \tparam [in] BT The type of the second value, can only be 32 bit integer
1068/// \param [in] a The first value
1069/// \param [in] b The second value
1070/// \returns The smaller one of the two extended values with saturation
1071template <typename RetT, typename AT, typename BT>
1072inline constexpr RetT extend_min_sat(AT a, BT b) {
1073  return detail::extend_binary<RetT, true>(a, b, minimum());
1074}
1075
1076/// Extend Inputs to 33 bit, find the smaller one in \p a, \p b with saturation,
1077/// then do \p second_op with \p c.
1078/// \tparam [in] RetT The type of the return value, can only be 32 bit integer
1079/// \tparam [in] AT The type of the first value, can only be 32 bit integer
1080/// \tparam [in] BT The type of the second value, can only be 32 bit integer
1081/// \tparam [in] CT The type of the third value, can only be 32 bit integer
1082/// \tparam [in] BinaryOperation The type of the second operation
1083/// \param [in] a The first value
1084/// \param [in] b The second value
1085/// \param [in] c The third value
1086/// \param [in] second_op The operation to do with the third value
1087/// \returns The smaller one of \p a, \p b with saturation and \p
1088/// second_op with \p c
1089template <typename RetT, typename AT, typename BT, typename CT,
1090          typename BinaryOperation>
1091inline constexpr RetT extend_min_sat(AT a, BT b, CT c,
1092                                     BinaryOperation second_op) {
1093  return detail::extend_binary<RetT, true>(a, b, c, minimum(), second_op);
1094}
1095
1096/// Extend \p a and \p b to 33 bit and return bigger one.
1097/// \tparam [in] RetT The type of the return value, can only be 32 bit integer
1098/// \tparam [in] AT The type of the first value, can only be 32 bit integer
1099/// \tparam [in] BT The type of the second value, can only be 32 bit integer
1100/// \param [in] a The first value
1101/// \param [in] b The second value
1102/// \returns The bigger one of the two extended values
1103template <typename RetT, typename AT, typename BT>
1104inline constexpr RetT extend_max(AT a, BT b) {
1105  return detail::extend_binary<RetT, false>(a, b, maximum());
1106}
1107
1108/// Extend Inputs to 33 bit, find the bigger one in \p a, \p b, then do \p
1109/// second_op with \p c.
1110/// \tparam [in] RetT The type of the return value, can only be 32 bit integer
1111/// \tparam [in] AT The type of the first value, can only be 32 bit integer
1112/// \tparam [in] BT The type of the second value, can only be 32 bit integer
1113/// \tparam [in] CT The type of the third value, can only be 32 bit integer
1114/// \tparam [in] BinaryOperation The type of the second operation
1115/// \param [in] a The first value
1116/// \param [in] b The second value
1117/// \param [in] c The third value
1118/// \param [in] second_op The operation to do with the third value
1119/// \returns The bigger one of \p a, \p b and \p second_op with \p c
1120template <typename RetT, typename AT, typename BT, typename CT,
1121          typename BinaryOperation>
1122inline constexpr RetT extend_max(AT a, BT b, CT c, BinaryOperation second_op) {
1123  return detail::extend_binary<RetT, false>(a, b, c, maximum(), second_op);
1124}
1125
1126/// Extend \p a and \p b to 33 bit and return bigger one with saturation.
1127/// \tparam [in] RetT The type of the return value, can only be 32 bit integer
1128/// \tparam [in] AT The type of the first value, can only be 32 bit integer
1129/// \tparam [in] BT The type of the second value, can only be 32 bit integer
1130/// \param [in] a The first value
1131/// \param [in] b The second value
1132/// \returns The bigger one of the two extended values with saturation
1133template <typename RetT, typename AT, typename BT>
1134inline constexpr RetT extend_max_sat(AT a, BT b) {
1135  return detail::extend_binary<RetT, true>(a, b, maximum());
1136}
1137
1138/// Extend Inputs to 33 bit, find the bigger one in \p a, \p b with saturation,
1139/// then do \p second_op with \p c.
1140/// \tparam [in] RetT The type of the return value, can only be 32 bit integer
1141/// \tparam [in] AT The type of the first value, can only be 32 bit integer
1142/// \tparam [in] BT The type of the second value, can only be 32 bit integer
1143/// \tparam [in] CT The type of the third value, can only be 32 bit integer
1144/// \tparam [in] BinaryOperation The type of the second operation
1145/// \param [in] a The first value
1146/// \param [in] b The second value
1147/// \param [in] c The third value
1148/// \param [in] second_op The operation to do with the third value
1149/// \returns The bigger one of \p a, \p b with saturation and \p
1150/// second_op with \p c
1151template <typename RetT, typename AT, typename BT, typename CT,
1152          typename BinaryOperation>
1153inline constexpr RetT extend_max_sat(AT a, BT b, CT c,
1154                                     BinaryOperation second_op) {
1155  return detail::extend_binary<RetT, true>(a, b, c, maximum(), second_op);
1156}
1157
1158/// Extend \p a and \p b to 33 bit and compare input values using specified
1159/// comparison \p cmp .
1160///
1161/// \tparam [in] AT The type of the first value, can only be 32 bit integer
1162/// \tparam [in] BT The type of the second value, can only be 32 bit integer
1163/// \tparam [in] BinaryOperation The type of the compare operation
1164/// \param [in] a The first value
1165/// \param [in] b The second value
1166/// \param [in] cmp The comparsion operator
1167/// \returns The comparison result of the two extended values.
1168template <typename AT, typename BT, typename BinaryOperation>
1169inline constexpr unsigned extend_compare(AT a, BT b, BinaryOperation cmp) {
1170  return detail::extend_binary<unsigned, false>(a, b, cmp);
1171}
1172
1173/// Extend Inputs to 33 bit, and compare input values using specified comparison
1174/// \p cmp , then do \p second_op with \p c .
1175///
1176/// \tparam [in] AT The type of the first value, can only be 32 bit integer
1177/// \tparam [in] BT The type of the second value, can only be 32 bit integer
1178/// \tparam [in] BinaryOperation The type of the compare operation
1179/// \tparam [in] SecondBinaryOperation The type of the second operation
1180/// \param [in] a The first value
1181/// \param [in] b The second value
1182/// \param [in] c The third value
1183/// \param [in] cmp The comparsion operator
1184/// \param [in] second_op The operation to do with the third value
1185/// \returns The comparison result of the two extended values. and \p second_op
1186/// with \p c .
1187template <typename AT, typename BT, typename BinaryOperation,
1188          typename SecondBinaryOperation>
1189inline constexpr unsigned extend_compare(AT a, BT b, unsigned c,
1190                                         BinaryOperation cmp,
1191                                         SecondBinaryOperation second_op) {
1192  return detail::extend_binary<unsigned, false>(a, b, c, cmp, second_op);
1193}
1194
1195/// Extend \p a and \p b to 33 bit and return a << clamp(b, 0, 32).
1196/// \param [in] a The source value
1197/// \param [in] b The offset to shift
1198/// \returns a << clamp(b, 0, 32)
1199template <typename RetT, typename T>
1200inline constexpr RetT extend_shl_clamp(T a, uint32_t b) {
1201  return detail::extend_binary<RetT, false>(a, sycl::clamp(b, 0u, 32u),
1202                                            detail::shift_left());
1203}
1204
1205/// Extend Inputs to 33 bit, and return second_op(a << clamp(b, 0, 32), c).
1206/// \param [in] a The source value
1207/// \param [in] b The offset to shift
1208/// \param [in] c The value to merge
1209/// \param [in] second_op The operation to do with the third value
1210/// \returns second_op(a << clamp(b, 0, 32), c)
1211template <typename RetT, typename T, typename BinaryOperation>
1212inline constexpr RetT extend_shl_clamp(T a, uint32_t b, uint32_t c,
1213                                       BinaryOperation second_op) {
1214  return detail::extend_binary<RetT, false>(a, sycl::clamp(b, 0u, 32u), c,
1215                                            detail::shift_left(), second_op);
1216}
1217
1218/// Extend \p a and \p b to 33 bit and return sat(a << clamp(b, 0, 32)).
1219/// \param [in] a The source value
1220/// \param [in] b The offset to shift
1221/// \returns sat(a << clamp(b, 0, 32))
1222template <typename RetT, typename T>
1223inline constexpr RetT extend_shl_sat_clamp(T a, uint32_t b) {
1224  return detail::extend_binary<RetT, true>(a, sycl::clamp(b, 0u, 32u),
1225                                           detail::shift_left());
1226}
1227
1228/// Extend Inputs to 33 bit, and return second_op(sat(a << clamp(b, 0, 32)), c).
1229/// \param [in] a The source value
1230/// \param [in] b The offset to shift
1231/// \param [in] c The value to merge
1232/// \param [in] second_op The operation to do with the third value
1233/// \returns second_op(sat(a << clamp(b, 0, 32)), c)
1234template <typename RetT, typename T, typename BinaryOperation>
1235inline constexpr RetT extend_shl_sat_clamp(T a, uint32_t b, uint32_t c,
1236                                           BinaryOperation second_op) {
1237  return detail::extend_binary<RetT, true>(a, sycl::clamp(b, 0u, 32u), c,
1238                                           detail::shift_left(), second_op);
1239}
1240
1241/// Extend \p a and \p b to 33 bit and return a << (b & 0x1F).
1242/// \param [in] a The source value
1243/// \param [in] b The offset to shift
1244/// \returns a << (b & 0x1F)
1245template <typename RetT, typename T>
1246inline constexpr RetT extend_shl_wrap(T a, uint32_t b) {
1247  return detail::extend_binary<RetT, false>(a, b & 0x1F, detail::shift_left());
1248}
1249
1250/// Extend Inputs to 33 bit, and return second_op(a << (b & 0x1F), c).
1251/// \param [in] a The source value
1252/// \param [in] b The offset to shift
1253/// \param [in] c The value to merge
1254/// \param [in] second_op The operation to do with the third value
1255/// \returns second_op(a << (b & 0x1F), c)
1256template <typename RetT, typename T, typename BinaryOperation>
1257inline constexpr RetT extend_shl_wrap(T a, uint32_t b, uint32_t c,
1258                                      BinaryOperation second_op) {
1259  return detail::extend_binary<RetT, false>(a, b & 0x1F, c,
1260                                            detail::shift_left(), second_op);
1261}
1262
1263/// Extend \p a and \p b to 33 bit and return sat(a << (b & 0x1F)).
1264/// \param [in] a The source value
1265/// \param [in] b The offset to shift
1266/// \returns sat(a << (b & 0x1F))
1267template <typename RetT, typename T>
1268inline constexpr RetT extend_shl_sat_wrap(T a, uint32_t b) {
1269  return detail::extend_binary<RetT, true>(a, b & 0x1F, detail::shift_left());
1270}
1271
1272/// Extend Inputs to 33 bit, and return second_op(sat(a << (b & 0x1F)), c).
1273/// \param [in] a The source value
1274/// \param [in] b The offset to shift
1275/// \param [in] c The value to merge
1276/// \param [in] second_op The operation to do with the third value
1277/// \returns second_op(sat(a << (b & 0x1F)), c)
1278template <typename RetT, typename T, typename BinaryOperation>
1279inline constexpr RetT extend_shl_sat_wrap(T a, uint32_t b, uint32_t c,
1280                                          BinaryOperation second_op) {
1281  return detail::extend_binary<RetT, true>(a, b & 0x1F, c, detail::shift_left(),
1282                                           second_op);
1283}
1284
1285/// Extend \p a and \p b to 33 bit and return a >> clamp(b, 0, 32).
1286/// \param [in] a The source value
1287/// \param [in] b The offset to shift
1288/// \returns a >> clamp(b, 0, 32)
1289template <typename RetT, typename T>
1290inline constexpr RetT extend_shr_clamp(T a, uint32_t b) {
1291  return detail::extend_binary<RetT, false>(a, sycl::clamp(b, 0u, 32u),
1292                                            detail::shift_right());
1293}
1294
1295/// Extend Inputs to 33 bit, and return second_op(a >> clamp(b, 0, 32), c).
1296/// \param [in] a The source value
1297/// \param [in] b The offset to shift
1298/// \param [in] c The value to merge
1299/// \param [in] second_op The operation to do with the third value
1300/// \returns second_op(a >> clamp(b, 0, 32), c)
1301template <typename RetT, typename T, typename BinaryOperation>
1302inline constexpr RetT extend_shr_clamp(T a, uint32_t b, uint32_t c,
1303                                       BinaryOperation second_op) {
1304  return detail::extend_binary<RetT, false>(a, sycl::clamp(b, 0u, 32u), c,
1305                                            detail::shift_right(), second_op);
1306}
1307
1308/// Extend \p a and \p b to 33 bit and return sat(a >> clamp(b, 0, 32)).
1309/// \param [in] a The source value
1310/// \param [in] b The offset to shift
1311/// \returns sat(a >> clamp(b, 0, 32))
1312template <typename RetT, typename T>
1313inline constexpr RetT extend_shr_sat_clamp(T a, uint32_t b) {
1314  return detail::extend_binary<RetT, true>(a, sycl::clamp(b, 0u, 32u),
1315                                           detail::shift_right());
1316}
1317
1318/// Extend Inputs to 33 bit, and return second_op(sat(a >> clamp(b, 0, 32)), c).
1319/// \param [in] a The source value
1320/// \param [in] b The offset to shift
1321/// \param [in] c The value to merge
1322/// \param [in] second_op The operation to do with the third value
1323/// \returns second_op(sat(a >> clamp(b, 0, 32)), c)
1324template <typename RetT, typename T, typename BinaryOperation>
1325inline constexpr RetT extend_shr_sat_clamp(T a, uint32_t b, uint32_t c,
1326                                           BinaryOperation second_op) {
1327  return detail::extend_binary<RetT, true>(a, sycl::clamp(b, 0u, 32u), c,
1328                                           detail::shift_right(), second_op);
1329}
1330
1331/// Extend \p a and \p b to 33 bit and return a >> (b & 0x1F).
1332/// \param [in] a The source value
1333/// \param [in] b The offset to shift
1334/// \returns a >> (b & 0x1F)
1335template <typename RetT, typename T>
1336inline constexpr RetT extend_shr_wrap(T a, uint32_t b) {
1337  return detail::extend_binary<RetT, false>(a, b & 0x1F, detail::shift_right());
1338}
1339
1340/// Extend Inputs to 33 bit, and return second_op(a >> (b & 0x1F), c).
1341/// \param [in] a The source value
1342/// \param [in] b The offset to shift
1343/// \param [in] c The value to merge
1344/// \param [in] second_op The operation to do with the third value
1345/// \returns second_op(a >> (b & 0x1F), c)
1346template <typename RetT, typename T, typename BinaryOperation>
1347inline constexpr RetT extend_shr_wrap(T a, uint32_t b, uint32_t c,
1348                                      BinaryOperation second_op) {
1349  return detail::extend_binary<RetT, false>(a, b & 0x1F, c,
1350                                            detail::shift_right(), second_op);
1351}
1352
1353/// Extend \p a and \p b to 33 bit and return sat(a >> (b & 0x1F)).
1354/// \param [in] a The source value
1355/// \param [in] b The offset to shift
1356/// \returns sat(a >> (b & 0x1F))
1357template <typename RetT, typename T>
1358inline constexpr RetT extend_shr_sat_wrap(T a, uint32_t b) {
1359  return detail::extend_binary<RetT, true>(a, b & 0x1F, detail::shift_right());
1360}
1361
1362/// Extend Inputs to 33 bit, and return second_op(sat(a >> (b & 0x1F)), c).
1363/// \param [in] a The source value
1364/// \param [in] b The offset to shift
1365/// \param [in] c The value to merge
1366/// \param [in] second_op The operation to do with the third value
1367/// \returns second_op(sat(a >> (b & 0x1F)), c)
1368template <typename RetT, typename T, typename BinaryOperation>
1369inline constexpr RetT extend_shr_sat_wrap(T a, uint32_t b, uint32_t c,
1370                                          BinaryOperation second_op) {
1371  return detail::extend_binary<RetT, true>(a, b & 0x1F, c,
1372                                           detail::shift_right(), second_op);
1373}
1374
1375/// Compute vectorized addition of \p a and \p b, with each value treated as a
1376/// 2 elements vector type and extend each element to 17 bit.
1377/// \tparam [in] RetT The type of the return value, can only be 32 bit integer
1378/// \tparam [in] AT The type of the first value, can only be 32 bit integer
1379/// \tparam [in] BT The type of the second value, can only be 32 bit integer
1380/// \param [in] a The first value
1381/// \param [in] b The second value
1382/// \param [in] c The third value
1383/// \returns The extend vectorized addition of the two values
1384template <typename RetT, typename AT, typename BT>
1385inline constexpr RetT extend_vadd2(AT a, BT b, RetT c) {
1386  return detail::extend_vbinary2<RetT, false, false>(a, b, c, std::plus());
1387}
1388
1389/// Compute vectorized addition of \p a and \p b, with each value treated as a 2
1390/// elements vector type and extend each element to 17 bit. Then add each half
1391/// of the result and add with \p c.
1392/// \tparam [in] RetT The type of the return value, can only be 32 bit integer
1393/// \tparam [in] AT The type of the first value, can only be 32 bit integer
1394/// \tparam [in] BT The type of the second value, can only be 32 bit integer
1395/// \param [in] a The first value
1396/// \param [in] b The second value
1397/// \param [in] c The third value
1398/// \returns The addition of each half of extend vectorized addition of the two
1399/// values and the third value
1400template <typename RetT, typename AT, typename BT>
1401inline constexpr RetT extend_vadd2_add(AT a, BT b, RetT c) {
1402  return detail::extend_vbinary2<RetT, false, true>(a, b, c, std::plus());
1403}
1404
1405/// Compute vectorized addition of \p a and \p b with saturation, with each
1406/// value treated as a 2 elements vector type and extend each element to 17 bit.
1407/// \tparam [in] RetT The type of the return value, can only be 32 bit integer
1408/// \tparam [in] AT The type of the first value, can only be 32 bit integer
1409/// \tparam [in] BT The type of the second value, can only be 32 bit integer
1410/// \param [in] a The first value
1411/// \param [in] b The second value
1412/// \param [in] c The third value
1413/// \returns The extend vectorized addition of the two values with saturation
1414template <typename RetT, typename AT, typename BT>
1415inline constexpr RetT extend_vadd2_sat(AT a, BT b, RetT c) {
1416  return detail::extend_vbinary2<RetT, true, false>(a, b, c, std::plus());
1417}
1418
1419/// Compute vectorized subtraction of \p a and \p b, with each value treated as
1420/// a 2 elements vector type and extend each element to 17 bit.
1421/// \tparam [in] RetT The type of the return value, can only be 32 bit integer
1422/// \tparam [in] AT The type of the first value, can only be 32 bit integer
1423/// \tparam [in] BT The type of the second value, can only be 32 bit integer
1424/// \param [in] a The first value
1425/// \param [in] b The second value
1426/// \param [in] c The third value
1427/// \returns The extend vectorized subtraction of the two values
1428template <typename RetT, typename AT, typename BT>
1429inline constexpr RetT extend_vsub2(AT a, BT b, RetT c) {
1430  return detail::extend_vbinary2<RetT, false, false>(a, b, c, std::minus());
1431}
1432
1433/// Compute vectorized subtraction of \p a and \p b, with each value treated as
1434/// a 2 elements vector type and extend each element to 17 bit. Then add each
1435/// half of the result and add with \p c.
1436/// \tparam [in] RetT The type of the return value, can only be 32 bit integer
1437/// \tparam [in] AT The type of the first value, can only be 32 bit integer
1438/// \tparam [in] BT The type of the second value, can only be 32 bit integer
1439/// \param [in] a The first value
1440/// \param [in] b The second value
1441/// \param [in] c The third value
1442/// \returns The addition of each half of extend vectorized subtraction of the
1443/// two values and the third value
1444template <typename RetT, typename AT, typename BT>
1445inline constexpr RetT extend_vsub2_add(AT a, BT b, RetT c) {
1446  return detail::extend_vbinary2<RetT, false, true>(a, b, c, std::minus());
1447}
1448
1449/// Compute vectorized subtraction of \p a and \p b with saturation, with each
1450/// value treated as a 2 elements vector type and extend each element to 17 bit.
1451/// \tparam [in] RetT The type of the return value, can only be 32 bit integer
1452/// \tparam [in] AT The type of the first value, can only be 32 bit integer
1453/// \tparam [in] BT The type of the second value, can only be 32 bit integer
1454/// \param [in] a The first value
1455/// \param [in] b The second value
1456/// \param [in] c The third value
1457/// \returns The extend vectorized subtraction of the two values with saturation
1458template <typename RetT, typename AT, typename BT>
1459inline constexpr RetT extend_vsub2_sat(AT a, BT b, RetT c) {
1460  return detail::extend_vbinary2<RetT, true, false>(a, b, c, std::minus());
1461}
1462
1463/// Compute vectorized abs_diff of \p a and \p b, with each value treated as a 2
1464/// elements vector type and extend each element to 17 bit.
1465/// \tparam [in] RetT The type of the return value, can only be 32 bit integer
1466/// \tparam [in] AT The type of the first value, can only be 32 bit integer
1467/// \tparam [in] BT The type of the second value, can only be 32 bit integer
1468/// \param [in] a The first value
1469/// \param [in] b The second value
1470/// \param [in] c The third value
1471/// \returns The extend vectorized abs_diff of the two values
1472template <typename RetT, typename AT, typename BT>
1473inline constexpr RetT extend_vabsdiff2(AT a, BT b, RetT c) {
1474  return detail::extend_vbinary2<RetT, false, false>(a, b, c, abs_diff());
1475}
1476
1477/// Compute vectorized abs_diff of \p a and \p b, with each value treated as a 2
1478/// elements vector type and extend each element to 17 bit. Then add each half
1479/// of the result and add with \p c.
1480/// \tparam [in] RetT The type of the return value, can only be 32 bit integer
1481/// \tparam [in] AT The type of the first value, can only be 32 bit integer
1482/// \tparam [in] BT The type of the second value, can only be 32 bit integer
1483/// \param [in] a The first value
1484/// \param [in] b The second value
1485/// \param [in] c The third value
1486/// \returns The addition of each half of extend vectorized abs_diff of the
1487/// two values and the third value
1488template <typename RetT, typename AT, typename BT>
1489inline constexpr RetT extend_vabsdiff2_add(AT a, BT b, RetT c) {
1490  return detail::extend_vbinary2<RetT, false, true>(a, b, c, abs_diff());
1491}
1492
1493/// Compute vectorized abs_diff of \p a and \p b with saturation, with each
1494/// value treated as a 2 elements vector type and extend each element to 17 bit.
1495/// \tparam [in] RetT The type of the return value, can only be 32 bit integer
1496/// \tparam [in] AT The type of the first value, can only be 32 bit integer
1497/// \tparam [in] BT The type of the second value, can only be 32 bit integer
1498/// \param [in] a The first value
1499/// \param [in] b The second value
1500/// \param [in] c The third value
1501/// \returns The extend vectorized abs_diff of the two values with saturation
1502template <typename RetT, typename AT, typename BT>
1503inline constexpr RetT extend_vabsdiff2_sat(AT a, BT b, RetT c) {
1504  return detail::extend_vbinary2<RetT, true, false>(a, b, c, abs_diff());
1505}
1506
1507/// Compute vectorized minimum of \p a and \p b, with each value treated as a 2
1508/// elements vector type and extend each element to 17 bit.
1509/// \tparam [in] RetT The type of the return value, can only be 32 bit integer
1510/// \tparam [in] AT The type of the first value, can only be 32 bit integer
1511/// \tparam [in] BT The type of the second value, can only be 32 bit integer
1512/// \param [in] a The first value
1513/// \param [in] b The second value
1514/// \param [in] c The third value
1515/// \returns The extend vectorized minimum of the two values
1516template <typename RetT, typename AT, typename BT>
1517inline constexpr RetT extend_vmin2(AT a, BT b, RetT c) {
1518  return detail::extend_vbinary2<RetT, false, false>(a, b, c, minimum());
1519}
1520
1521/// Compute vectorized minimum of \p a and \p b, with each value treated as a 2
1522/// elements vector type and extend each element to 17 bit. Then add each half
1523/// of the result and add with \p c.
1524/// \tparam [in] RetT The type of the return value, can only be 32 bit integer
1525/// \tparam [in] AT The type of the first value, can only be 32 bit integer
1526/// \tparam [in] BT The type of the second value, can only be 32 bit integer
1527/// \param [in] a The first value
1528/// \param [in] b The second value
1529/// \param [in] c The third value
1530/// \returns The addition of each half of extend vectorized minimum of the
1531/// two values and the third value
1532template <typename RetT, typename AT, typename BT>
1533inline constexpr RetT extend_vmin2_add(AT a, BT b, RetT c) {
1534  return detail::extend_vbinary2<RetT, false, true>(a, b, c, minimum());
1535}
1536
1537/// Compute vectorized minimum of \p a and \p b with saturation, with each value
1538/// treated as a 2 elements vector type and extend each element to 17 bit.
1539/// \tparam [in] RetT The type of the return value, can only be 32 bit integer
1540/// \tparam [in] AT The type of the first value, can only be 32 bit integer
1541/// \tparam [in] BT The type of the second value, can only be 32 bit integer
1542/// \param [in] a The first value
1543/// \param [in] b The second value
1544/// \param [in] c The third value
1545/// \returns The extend vectorized minimum of the two values with saturation
1546template <typename RetT, typename AT, typename BT>
1547inline constexpr RetT extend_vmin2_sat(AT a, BT b, RetT c) {
1548  return detail::extend_vbinary2<RetT, true, false>(a, b, c, minimum());
1549}
1550
1551/// Compute vectorized maximum of \p a and \p b, with each value treated as a 2
1552/// elements vector type and extend each element to 17 bit.
1553/// \tparam [in] RetT The type of the return value, can only be 32 bit integer
1554/// \tparam [in] AT The type of the first value, can only be 32 bit integer
1555/// \tparam [in] BT The type of the second value, can only be 32 bit integer
1556/// \param [in] a The first value
1557/// \param [in] b The second value
1558/// \param [in] c The third value
1559/// \returns The extend vectorized maximum of the two values
1560template <typename RetT, typename AT, typename BT>
1561inline constexpr RetT extend_vmax2(AT a, BT b, RetT c) {
1562  return detail::extend_vbinary2<RetT, false, false>(a, b, c, maximum());
1563}
1564
1565/// Compute vectorized maximum of \p a and \p b, with each value treated as a 2
1566/// elements vector type and extend each element to 17 bit. Then add each half
1567/// of the result and add with \p c.
1568/// \tparam [in] RetT The type of the return value, can only be 32 bit integer
1569/// \tparam [in] AT The type of the first value, can only be 32 bit integer
1570/// \tparam [in] BT The type of the second value, can only be 32 bit integer
1571/// \param [in] a The first value
1572/// \param [in] b The second value
1573/// \param [in] c The third value
1574/// \returns The addition of each half of extend vectorized maximum of the
1575/// two values and the third value
1576template <typename RetT, typename AT, typename BT>
1577inline constexpr RetT extend_vmax2_add(AT a, BT b, RetT c) {
1578  return detail::extend_vbinary2<RetT, false, true>(a, b, c, maximum());
1579}
1580
1581/// Compute vectorized maximum of \p a and \p b with saturation, with each value
1582/// treated as a 2 elements vector type and extend each element to 17 bit.
1583/// \tparam [in] RetT The type of the return value, can only be 32 bit integer
1584/// \tparam [in] AT The type of the first value, can only be 32 bit integer
1585/// \tparam [in] BT The type of the second value, can only be 32 bit integer
1586/// \param [in] a The first value
1587/// \param [in] b The second value
1588/// \param [in] c The third value
1589/// \returns The extend vectorized maximum of the two values with saturation
1590template <typename RetT, typename AT, typename BT>
1591inline constexpr RetT extend_vmax2_sat(AT a, BT b, RetT c) {
1592  return detail::extend_vbinary2<RetT, true, false>(a, b, c, maximum());
1593}
1594
1595/// Extend \p a and \p b to 33 bit and vectorized compare input values using
1596///
1597/// specified comparison \p cmp .
1598/// \tparam [in] AT The type of the first value, can only be 32 bit integer
1599/// \tparam [in] BT The type of the second value, can only be 32 bit integer
1600/// \tparam [in] BinaryOperation The type of the compare operation
1601/// \param [in] a The first value
1602/// \param [in] b The second value
1603/// \param [in] cmp The comparsion operator
1604/// \returns The comparison result of the two extended values.
1605template <typename AT, typename BT, typename BinaryOperation>
1606inline constexpr unsigned extend_vcompare2(AT a, BT b, BinaryOperation cmp) {
1607  return detail::extend_vbinary2<unsigned, false, false>(a, b, 0, cmp);
1608}
1609
1610/// Extend Inputs to 33 bit, and vectorized compare input values using specified
1611/// comparison \p cmp , then add the result with \p c .
1612///
1613/// \tparam [in] AT The type of the first value, can only be 32 bit integer
1614/// \tparam [in] BT The type of the second value, can only be 32 bit integer
1615/// \tparam [in] BinaryOperation The type of the compare operation
1616/// \param [in] a The first value
1617/// \param [in] b The second value
1618/// \param [in] c The third value
1619/// \param [in] cmp The comparsion operator
1620/// \returns The comparison result of the two extended values, and add the
1621/// result with \p c .
1622template <typename AT, typename BT, typename BinaryOperation>
1623inline constexpr unsigned extend_vcompare2_add(AT a, BT b, unsigned c,
1624                                               BinaryOperation cmp) {
1625  return detail::extend_vbinary2<unsigned, false, true>(a, b, c, cmp);
1626}
1627
1628/// Compute vectorized average of \p a and \p b, with each value treated as a 2
1629/// elements vector type and extend each element to 17 bit.
1630/// \tparam [in] RetT The type of the return value, can only be 32 bit integer
1631/// \tparam [in] AT The type of the first value, can only be 32 bit integer
1632/// \tparam [in] BT The type of the second value, can only be 32 bit integer
1633/// \param [in] a The first value
1634/// \param [in] b The second value
1635/// \param [in] c The third value
1636/// \returns The extend vectorized average of the two values
1637template <typename RetT, typename AT, typename BT>
1638inline constexpr RetT extend_vavrg2(AT a, BT b, RetT c) {
1639  return detail::extend_vbinary2<RetT, false, false>(a, b, c,
1640                                                     detail::average());
1641}
1642
1643/// Compute vectorized average of \p a and \p b, with each value treated as a 2
1644/// elements vector type and extend each element to 17 bit. Then add each half
1645/// of the result and add with \p c.
1646/// \tparam [in] RetT The type of the return value, can only be 32 bit integer
1647/// \tparam [in] AT The type of the first value, can only be 32 bit integer
1648/// \tparam [in] BT The type of the second value, can only be 32 bit integer
1649/// \param [in] a The first value
1650/// \param [in] b The second value
1651/// \param [in] c The third value
1652/// \returns The addition of each half of extend average maximum of the
1653/// two values and the third value
1654template <typename RetT, typename AT, typename BT>
1655inline constexpr RetT extend_vavrg2_add(AT a, BT b, RetT c) {
1656  return detail::extend_vbinary2<RetT, false, true>(a, b, c, detail::average());
1657}
1658
1659/// Compute vectorized average of \p a and \p b with saturation, with each value
1660/// treated as a 2 elements vector type and extend each element to 17 bit.
1661/// \tparam [in] RetT The type of the return value, can only be 32 bit integer
1662/// \tparam [in] AT The type of the first value, can only be 32 bit integer
1663/// \tparam [in] BT The type of the second value, can only be 32 bit integer
1664/// \param [in] a The first value
1665/// \param [in] b The second value
1666/// \param [in] c The third value
1667/// \returns The extend vectorized average of the two values with saturation
1668template <typename RetT, typename AT, typename BT>
1669inline constexpr RetT extend_vavrg2_sat(AT a, BT b, RetT c) {
1670  return detail::extend_vbinary2<RetT, true, false>(a, b, c, detail::average());
1671}
1672
1673/// Compute vectorized addition of \p a and \p b, with each value treated as a
1674/// 4 elements vector type and extend each element to 9 bit.
1675/// \tparam [in] RetT The type of the return value, can only be 32 bit integer
1676/// \tparam [in] AT The type of the first value, can only be 32 bit integer
1677/// \tparam [in] BT The type of the second value, can only be 32 bit integer
1678/// \param [in] a The first value
1679/// \param [in] b The second value
1680/// \param [in] c The third value
1681/// \returns The extend vectorized addition of the two values
1682template <typename RetT, typename AT, typename BT>
1683inline constexpr RetT extend_vadd4(AT a, BT b, RetT c) {
1684  return detail::extend_vbinary4<RetT, false, false>(a, b, c, std::plus());
1685}
1686
1687/// Compute vectorized addition of \p a and \p b, with each value treated as a 4
1688/// elements vector type and extend each element to 9 bit. Then add each half
1689/// of the result and add with \p c.
1690/// \tparam [in] RetT The type of the return value, can only be 32 bit integer
1691/// \tparam [in] AT The type of the first value, can only be 32 bit integer
1692/// \tparam [in] BT The type of the second value, can only be 32 bit integer
1693/// \param [in] a The first value
1694/// \param [in] b The second value
1695/// \param [in] c The third value
1696/// \returns The addition of each half of extend vectorized addition of the two
1697/// values and the third value
1698template <typename RetT, typename AT, typename BT>
1699inline constexpr RetT extend_vadd4_add(AT a, BT b, RetT c) {
1700  return detail::extend_vbinary4<RetT, false, true>(a, b, c, std::plus());
1701}
1702
1703/// Compute vectorized addition of \p a and \p b with saturation, with each
1704/// value treated as a 4 elements vector type and extend each element to 9 bit.
1705/// \tparam [in] RetT The type of the return value, can only be 32 bit integer
1706/// \tparam [in] AT The type of the first value, can only be 32 bit integer
1707/// \tparam [in] BT The type of the second value, can only be 32 bit integer
1708/// \param [in] a The first value
1709/// \param [in] b The second value
1710/// \param [in] c The third value
1711/// \returns The extend vectorized addition of the two values with saturation
1712template <typename RetT, typename AT, typename BT>
1713inline constexpr RetT extend_vadd4_sat(AT a, BT b, RetT c) {
1714  return detail::extend_vbinary4<RetT, true, false>(a, b, c, std::plus());
1715}
1716
1717/// Compute vectorized subtraction of \p a and \p b, with each value treated as
1718/// a 4 elements vector type and extend each element to 9 bit.
1719/// \tparam [in] RetT The type of the return value, can only be 32 bit integer
1720/// \tparam [in] AT The type of the first value, can only be 32 bit integer
1721/// \tparam [in] BT The type of the second value, can only be 32 bit integer
1722/// \param [in] a The first value
1723/// \param [in] b The second value
1724/// \param [in] c The third value
1725/// \returns The extend vectorized subtraction of the two values
1726template <typename RetT, typename AT, typename BT>
1727inline constexpr RetT extend_vsub4(AT a, BT b, RetT c) {
1728  return detail::extend_vbinary4<RetT, false, false>(a, b, c, std::minus());
1729}
1730
1731/// Compute vectorized subtraction of \p a and \p b, with each value treated as
1732/// a 4 elements vector type and extend each element to 9 bit. Then add each
1733/// half of the result and add with \p c.
1734/// \tparam [in] RetT The type of the return value, can only be 32 bit integer
1735/// \tparam [in] AT The type of the first value, can only be 32 bit integer
1736/// \tparam [in] BT The type of the second value, can only be 32 bit integer
1737/// \param [in] a The first value
1738/// \param [in] b The second value
1739/// \param [in] c The third value
1740/// \returns The addition of each half of extend vectorized subtraction of the
1741/// two values and the third value
1742template <typename RetT, typename AT, typename BT>
1743inline constexpr RetT extend_vsub4_add(AT a, BT b, RetT c) {
1744  return detail::extend_vbinary4<RetT, false, true>(a, b, c, std::minus());
1745}
1746
1747/// Compute vectorized subtraction of \p a and \p b with saturation, with each
1748/// value treated as a 4 elements vector type and extend each element to 9 bit.
1749/// \tparam [in] RetT The type of the return value, can only be 32 bit integer
1750/// \tparam [in] AT The type of the first value, can only be 32 bit integer
1751/// \tparam [in] BT The type of the second value, can only be 32 bit integer
1752/// \param [in] a The first value
1753/// \param [in] b The second value
1754/// \param [in] c The third value
1755/// \returns The extend vectorized subtraction of the two values with saturation
1756template <typename RetT, typename AT, typename BT>
1757inline constexpr RetT extend_vsub4_sat(AT a, BT b, RetT c) {
1758  return detail::extend_vbinary4<RetT, true, false>(a, b, c, std::minus());
1759}
1760
1761/// Compute vectorized abs_diff of \p a and \p b, with each value treated as a 4
1762/// elements vector type and extend each element to 9 bit.
1763/// \tparam [in] RetT The type of the return value, can only be 32 bit integer
1764/// \tparam [in] AT The type of the first value, can only be 32 bit integer
1765/// \tparam [in] BT The type of the second value, can only be 32 bit integer
1766/// \param [in] a The first value
1767/// \param [in] b The second value
1768/// \param [in] c The third value
1769/// \returns The extend vectorized abs_diff of the two values
1770template <typename RetT, typename AT, typename BT>
1771inline constexpr RetT extend_vabsdiff4(AT a, BT b, RetT c) {
1772  return detail::extend_vbinary4<RetT, false, false>(a, b, c, abs_diff());
1773}
1774
1775/// Compute vectorized abs_diff of \p a and \p b, with each value treated as a 4
1776/// elements vector type and extend each element to 9 bit. Then add each half
1777/// of the result and add with \p c.
1778/// \tparam [in] RetT The type of the return value, can only be 32 bit integer
1779/// \tparam [in] AT The type of the first value, can only be 32 bit integer
1780/// \tparam [in] BT The type of the second value, can only be 32 bit integer
1781/// \param [in] a The first value
1782/// \param [in] b The second value
1783/// \param [in] c The third value
1784/// \returns The addition of each half of extend vectorized abs_diff of the
1785/// two values and the third value
1786template <typename RetT, typename AT, typename BT>
1787inline constexpr RetT extend_vabsdiff4_add(AT a, BT b, RetT c) {
1788  return detail::extend_vbinary4<RetT, false, true>(a, b, c, abs_diff());
1789}
1790
1791/// Compute vectorized abs_diff of \p a and \p b with saturation, with each
1792/// value treated as a 4 elements vector type and extend each element to 9 bit.
1793/// \tparam [in] RetT The type of the return value, can only be 32 bit integer
1794/// \tparam [in] AT The type of the first value, can only be 32 bit integer
1795/// \tparam [in] BT The type of the second value, can only be 32 bit integer
1796/// \param [in] a The first value
1797/// \param [in] b The second value
1798/// \param [in] c The third value
1799/// \returns The extend vectorized abs_diff of the two values with saturation
1800template <typename RetT, typename AT, typename BT>
1801inline constexpr RetT extend_vabsdiff4_sat(AT a, BT b, RetT c) {
1802  return detail::extend_vbinary4<RetT, true, false>(a, b, c, abs_diff());
1803}
1804
1805/// Compute vectorized minimum of \p a and \p b, with each value treated as a 4
1806/// elements vector type and extend each element to 9 bit.
1807/// \tparam [in] RetT The type of the return value, can only be 32 bit integer
1808/// \tparam [in] AT The type of the first value, can only be 32 bit integer
1809/// \tparam [in] BT The type of the second value, can only be 32 bit integer
1810/// \param [in] a The first value
1811/// \param [in] b The second value
1812/// \param [in] c The third value
1813/// \returns The extend vectorized minimum of the two values
1814template <typename RetT, typename AT, typename BT>
1815inline constexpr RetT extend_vmin4(AT a, BT b, RetT c) {
1816  return detail::extend_vbinary4<RetT, false, false>(a, b, c, minimum());
1817}
1818
1819/// Compute vectorized minimum of \p a and \p b, with each value treated as a 4
1820/// elements vector type and extend each element to 9 bit. Then add each half
1821/// of the result and add with \p c.
1822/// \tparam [in] RetT The type of the return value, can only be 32 bit integer
1823/// \tparam [in] AT The type of the first value, can only be 32 bit integer
1824/// \tparam [in] BT The type of the second value, can only be 32 bit integer
1825/// \param [in] a The first value
1826/// \param [in] b The second value
1827/// \param [in] c The third value
1828/// \returns The addition of each half of extend vectorized minimum of the
1829/// two values and the third value
1830template <typename RetT, typename AT, typename BT>
1831inline constexpr RetT extend_vmin4_add(AT a, BT b, RetT c) {
1832  return detail::extend_vbinary4<RetT, false, true>(a, b, c, minimum());
1833}
1834
1835/// Compute vectorized minimum of \p a and \p b with saturation, with each value
1836/// treated as a 4 elements vector type and extend each element to 9 bit.
1837/// \tparam [in] RetT The type of the return value, can only be 32 bit integer
1838/// \tparam [in] AT The type of the first value, can only be 32 bit integer
1839/// \tparam [in] BT The type of the second value, can only be 32 bit integer
1840/// \param [in] a The first value
1841/// \param [in] b The second value
1842/// \param [in] c The third value
1843/// \returns The extend vectorized minimum of the two values with saturation
1844template <typename RetT, typename AT, typename BT>
1845inline constexpr RetT extend_vmin4_sat(AT a, BT b, RetT c) {
1846  return detail::extend_vbinary4<RetT, true, false>(a, b, c, minimum());
1847}
1848
1849/// Compute vectorized maximum of \p a and \p b, with each value treated as a 4
1850/// elements vector type and extend each element to 9 bit.
1851/// \tparam [in] RetT The type of the return value, can only be 32 bit integer
1852/// \tparam [in] AT The type of the first value, can only be 32 bit integer
1853/// \tparam [in] BT The type of the second value, can only be 32 bit integer
1854/// \param [in] a The first value
1855/// \param [in] b The second value
1856/// \param [in] c The third value
1857/// \returns The extend vectorized maximum of the two values
1858template <typename RetT, typename AT, typename BT>
1859inline constexpr RetT extend_vmax4(AT a, BT b, RetT c) {
1860  return detail::extend_vbinary4<RetT, false, false>(a, b, c, maximum());
1861}
1862
1863/// Compute vectorized maximum of \p a and \p b, with each value treated as a 4
1864/// elements vector type and extend each element to 9 bit. Then add each half
1865/// of the result and add with \p c.
1866/// \tparam [in] RetT The type of the return value, can only be 32 bit integer
1867/// \tparam [in] AT The type of the first value, can only be 32 bit integer
1868/// \tparam [in] BT The type of the second value, can only be 32 bit integer
1869/// \param [in] a The first value
1870/// \param [in] b The second value
1871/// \param [in] c The third value
1872/// \returns The addition of each half of extend vectorized maximum of the
1873/// two values and the third value
1874template <typename RetT, typename AT, typename BT>
1875inline constexpr RetT extend_vmax4_add(AT a, BT b, RetT c) {
1876  return detail::extend_vbinary4<RetT, false, true>(a, b, c, maximum());
1877}
1878
1879/// Compute vectorized maximum of \p a and \p b with saturation, with each value
1880/// treated as a 4 elements vector type and extend each element to 9 bit.
1881/// \tparam [in] RetT The type of the return value, can only be 32 bit integer
1882/// \tparam [in] AT The type of the first value, can only be 32 bit integer
1883/// \tparam [in] BT The type of the second value, can only be 32 bit integer
1884/// \param [in] a The first value
1885/// \param [in] b The second value
1886/// \param [in] c The third value
1887/// \returns The extend vectorized maximum of the two values with saturation
1888template <typename RetT, typename AT, typename BT>
1889inline constexpr RetT extend_vmax4_sat(AT a, BT b, RetT c) {
1890  return detail::extend_vbinary4<RetT, true, false>(a, b, c, maximum());
1891}
1892
1893/// Extend \p a and \p b to 33 bit and vectorized compare input values using
1894///
1895/// specified comparison \p cmp .
1896/// \tparam [in] AT The type of the first value, can only be 32 bit integer
1897/// \tparam [in] BT The type of the second value, can only be 32 bit integer
1898/// \tparam [in] BinaryOperation The type of the compare operation
1899/// \param [in] a The first value
1900/// \param [in] b The second value
1901/// \param [in] cmp The comparsion operator
1902/// \returns The comparison result of the two extended values.
1903template <typename AT, typename BT, typename BinaryOperation>
1904inline constexpr unsigned extend_vcompare4(AT a, BT b, BinaryOperation cmp) {
1905  return detail::extend_vbinary4<unsigned, false, false>(a, b, 0, cmp);
1906}
1907
1908/// Extend Inputs to 33 bit, and vectorized compare input values using specified
1909/// comparison \p cmp , then add the result with \p c .
1910///
1911/// \tparam [in] AT The type of the first value, can only be 32 bit integer
1912/// \tparam [in] BT The type of the second value, can only be 32 bit integer
1913/// \tparam [in] BinaryOperation The type of the compare operation
1914/// \param [in] a The first value
1915/// \param [in] b The second value
1916/// \param [in] c The third value
1917/// \param [in] cmp The comparsion operator
1918/// \returns The comparison result of the two extended values, and add the
1919/// result with \p c .
1920template <typename AT, typename BT, typename BinaryOperation>
1921inline constexpr unsigned extend_vcompare4_add(AT a, BT b, unsigned c,
1922                                               BinaryOperation cmp) {
1923  return detail::extend_vbinary4<unsigned, false, true>(a, b, c, cmp);
1924}
1925
1926/// Compute vectorized average of \p a and \p b, with each value treated as a 4
1927/// elements vector type and extend each element to 9 bit.
1928/// \tparam [in] RetT The type of the return value, can only be 32 bit integer
1929/// \tparam [in] AT The type of the first value, can only be 32 bit integer
1930/// \tparam [in] BT The type of the second value, can only be 32 bit integer
1931/// \param [in] a The first value
1932/// \param [in] b The second value
1933/// \param [in] c The third value
1934/// \returns The extend vectorized average of the two values
1935template <typename RetT, typename AT, typename BT>
1936inline constexpr RetT extend_vavrg4(AT a, BT b, RetT c) {
1937  return detail::extend_vbinary4<RetT, false, false>(a, b, c,
1938                                                     detail::average());
1939}
1940
1941/// Compute vectorized average of \p a and \p b, with each value treated as a 4
1942/// elements vector type and extend each element to 9 bit. Then add each half
1943/// of the result and add with \p c.
1944/// \tparam [in] RetT The type of the return value, can only be 32 bit integer
1945/// \tparam [in] AT The type of the first value, can only be 32 bit integer
1946/// \tparam [in] BT The type of the second value, can only be 32 bit integer
1947/// \param [in] a The first value
1948/// \param [in] b The second value
1949/// \param [in] c The third value
1950/// \returns The addition of each half of extend vectorized average of the
1951/// two values and the third value
1952template <typename RetT, typename AT, typename BT>
1953inline constexpr RetT extend_vavrg4_add(AT a, BT b, RetT c) {
1954  return detail::extend_vbinary4<RetT, false, true>(a, b, c, detail::average());
1955}
1956
1957/// Compute vectorized average of \p a and \p b with saturation, with each value
1958/// treated as a 4 elements vector type and extend each element to 9 bit.
1959/// \tparam [in] RetT The type of the return value, can only be 32 bit integer
1960/// \tparam [in] AT The type of the first value, can only be 32 bit integer
1961/// \tparam [in] BT The type of the second value, can only be 32 bit integer
1962/// \param [in] a The first value
1963/// \param [in] b The second value
1964/// \param [in] c The third value
1965/// \returns The extend vectorized average of the two values with saturation
1966template <typename RetT, typename AT, typename BT>
1967inline constexpr RetT extend_vavrg4_sat(AT a, BT b, RetT c) {
1968  return detail::extend_vbinary4<RetT, true, false>(a, b, c, detail::average());
1969}
1970
1971namespace experimental {
1972namespace matrix {
1973namespace syclex = sycl::ext::oneapi::experimental;
1974struct row_major
1975    : public std::integral_constant<syclex::matrix::layout,
1976                                    syclex::matrix::layout::row_major> {};
1977struct col_major
1978    : public std::integral_constant<syclex::matrix::layout,
1979                                    syclex::matrix::layout::col_major> {};
1980struct a : public std::integral_constant<syclex::matrix::use,
1981                                         syclex::matrix::use::a> {};
1982struct b : public std::integral_constant<syclex::matrix::use,
1983                                         syclex::matrix::use::b> {};
1984struct accumulator
1985    : public std::integral_constant<syclex::matrix::use,
1986                                    syclex::matrix::use::accumulator> {};
1987
1988template <class use, int m, int n, int k> struct matrix_size_traits;
1989template <int m, int n, int k> struct matrix_size_traits<a, m, n, k> {
1990  static constexpr int rows = m;
1991  static constexpr int cols = k;
1992};
1993
1994template <int m, int n, int k> struct matrix_size_traits<b, m, n, k> {
1995  static constexpr int rows = k;
1996  static constexpr int cols = n;
1997};
1998
1999template <int m, int n, int k> struct matrix_size_traits<accumulator, m, n, k> {
2000  static constexpr int rows = m;
2001  static constexpr int cols = n;
2002};
2003
2004// A class that wraps the syclex::matrix::joint_matrix class and provides
2005// copy constructor and assignment operator.
2006template <typename use, int m, int n, int k, typename T,
2007          typename layout = std::integral_constant<
2008              syclex::matrix::layout, syclex::matrix::layout::dynamic>>
2009class joint_matrix {
2010  using joint_matrix_type = syclex::matrix::joint_matrix<
2011      sycl::sub_group, T, use::value, matrix_size_traits<use, m, n, k>::rows,
2012      matrix_size_traits<use, m, n, k>::cols, layout::value>;
2013
2014  static inline decltype(auto) get_wi_data(joint_matrix_type &matrix) {
2015    return sycl::ext::oneapi::detail::get_wi_data(
2016        sycl::ext::oneapi::this_work_item::get_sub_group(), matrix);
2017  }
2018
2019public:
2020  joint_matrix()
2021      : matrix(), x(matrix), num_elements(get_wi_data(matrix).length()) {}
2022  joint_matrix(joint_matrix &other)
2023      : x(matrix), num_elements(get_wi_data(matrix).length()) {
2024    syclex::matrix::joint_matrix_copy(
2025        sycl::ext::oneapi::this_work_item::get_sub_group(), other.get(),
2026        matrix);
2027  }
2028  joint_matrix &operator=(joint_matrix &other) {
2029    if (this != &other) {
2030      syclex::matrix::joint_matrix_copy(
2031          sycl::ext::oneapi::this_work_item::get_sub_group(), other.get(),
2032          matrix);
2033    }
2034    return *this;
2035  }
2036
2037  joint_matrix_type &get() { return matrix; }
2038
2039  const joint_matrix_type &get() const { return matrix; }
2040
2041  class matrix_accessor {
2042    friend joint_matrix;
2043    joint_matrix_type &matrix;
2044    matrix_accessor(joint_matrix_type &matrix) : matrix(matrix) {}
2045
2046  public:
2047    decltype(auto) operator[](unsigned I) { return get_wi_data(matrix)[I]; }
2048    decltype(auto) operator[](unsigned I) const {
2049      return get_wi_data(matrix)[I];
2050    }
2051  };
2052
2053private:
2054  joint_matrix_type matrix;
2055
2056public:
2057  matrix_accessor x;
2058  const size_t num_elements;
2059};
2060
2061/// Collectively loads 1 8x8 b16 (128 bytes) matrix from local memory to private
2062/// memory per sub-group. Requires the sub-group size of kernel calling this
2063/// function to be 32.
2064/// 'mat' specifies the matrix index to be loaded. The first '(mat + 1) * 8'
2065/// work items of sub-group contain the starting address of their respective
2066/// matrix row in 'addr'.
2067/// After distributing addresses to other work items, each of the 32 work items
2068/// load 32-bits (2 packed 16-bit data) into 'm' for a total of 128 bytes.
2069/// 'trans' specifies to perform a transposed/non-transposed load by each work
2070/// item like below
2071/// Row Major: Each row of the matrix is loaded by a group of 4 work items(wi)
2072/// row-0: wi0 wi0 wi1 wi1 ... wi3 wi3
2073/// row-1: wi4 wi4 wi5 wi5 ... wi7 wi7
2074/// ...
2075/// row-6: wi24 wi24 wi25 wi25 ... wi27 wi27
2076/// row-7: wi28 wi28 wi29 wi29 ... wi31 wi31
2077/// Col Major: Each col of the matrix is loaded by a group of 4 work items(wi)
2078/// row-0: wi0 wi4 wi8 ... wi28
2079/// row-1: wi0 wi4 wi8 ... wi28
2080/// ...
2081/// row-6: wi3 wi7 wi11 ... wi31
2082/// row-7: wi3 wi7 wi11 ... wi31
2083/// \tparam [in] T Type of result variable (currently only supports 16-bit type)
2084/// \param [in] addr The starting address of corresponding matrix row for a work
2085/// item in local memory
2086/// \param [in] m The private memory to store the matrix. It points to 2 b16
2087/// type elements.
2088/// \param [in] trans Indicates whether the matrix to be loaded transposed
2089/// \param [in] mat The matrix index to be loaded
2090template <typename T>
2091void ldmatrix(uintptr_t addr, T *m, bool trans = false, unsigned mat = 0) {
2092  auto sg = sycl::ext::oneapi::this_work_item::get_sub_group();
2093  int lane = sg.get_local_linear_id();
2094
2095  int lane_group8_row = lane / 8;
2096  int lane_group8_col = lane % 8;
2097
2098  if (!trans) {
2099    // calculate the source lane
2100    int src_lane = 2 * lane_group8_row;
2101    if (lane_group8_col >= 4)
2102      src_lane += 1;
2103
2104    // Broadcast the address from the source lane
2105    auto recv_addr_uintp =
2106        dpct::select_from_sub_group(sg, addr, mat * 8 + src_lane);
2107
2108    // Cast the received address from uintptr_t to the type of 'm'
2109    auto recv_addr = reinterpret_cast<T *>(recv_addr_uintp);
2110
2111    // Non-transposed load
2112    *m = recv_addr[lane_group8_col % 4];
2113  } else {
2114    // calculate the source lane
2115    int src_lane = (lane % 4) * 2;
2116
2117    // Broadcast the address from the source lane
2118    auto recv_addr_uintp_1 =
2119        dpct::select_from_sub_group(sg, addr, mat * 8 + src_lane);
2120    auto recv_addr_uintp_2 =
2121        dpct::select_from_sub_group(sg, addr, mat * 8 + src_lane + 1);
2122
2123    // Cast the received address from uintptr_t to 'half *'
2124    auto recv_addr_1 = reinterpret_cast<sycl::half *>(recv_addr_uintp_1);
2125    auto recv_addr_2 = reinterpret_cast<sycl::half *>(recv_addr_uintp_2);
2126
2127    // Transposed load
2128    int index = lane / 4;
2129    sycl::half val0 = recv_addr_1[index];
2130    sycl::half val1 = recv_addr_2[index];
2131
2132    // Combine the two 16-bits into one 32-bit value
2133    sycl::half2 val = sycl::half2(val0, val1);
2134    *m = *reinterpret_cast<T *>(&val);
2135  }
2136}
2137
2138/// Collectively loads 2 8x8 b16 (256 bytes) matrix from local memory to private
2139/// memory per sub-group. Requires the sub-group size of kernel calling this
2140/// function to be 32.
2141/// The first 16 work items of sub-group contain the starting address of their
2142/// respective matrix row in 'addr'.
2143/// After distributing addresses to other work items, each of the 32 work items
2144/// load 64-bits (32-bits per matrix) into 'm1' & 'm2' for a total of 256 bytes.
2145/// 'trans' specifies to perform a transposed/non-transposed load by each work
2146/// item like below
2147/// Row Major: Each row of the matrices is loaded by a group of 4 work items(wi)
2148/// row-0: wi0 wi0 wi1 wi1 ... wi3 wi3
2149/// row-1: wi4 wi4 wi5 wi5 ... wi7 wi7
2150/// ...
2151/// row-6: wi24 wi24 wi25 wi25 ... wi27 wi27
2152/// row-7: wi28 wi28 wi29 wi29 ... wi31 wi31
2153/// Col Major: Each col of the matrices is loaded by a group of 4 work items(wi)
2154/// row-0: wi0 wi4 wi8 ... wi28
2155/// row-1: wi0 wi4 wi8 ... wi28
2156/// ...
2157/// row-6: wi3 wi7 wi11 ... wi31
2158/// row-7: wi3 wi7 wi11 ... wi31
2159/// \tparam [in] T Type of result variable (currently only supports 16-bit type)
2160/// \param [in] addr The starting address of corresponding matrix row for a work
2161/// item in local memory
2162/// \param [in] m1 The private memory to store the data of 1st matrix. It points
2163/// to 2 b16 type elements.
2164/// \param [in] m2 The private memory to store the data of 2nd matrix. It points
2165/// to 2 b16 type elements.
2166/// \param [in] trans Indicates whether the matrix to be loaded transposed
2167template <typename T>
2168void ldmatrix(uintptr_t addr, T *m1, T *m2, bool trans = false) {
2169  // Load 1st matrix
2170  ldmatrix(addr, m1, trans, 0);
2171  // Load 2nd matrix
2172  ldmatrix(addr, m2, trans, 1);
2173}
2174
2175/// Collectively loads 4 8x8 b16 (512 bytes) matrix from local memory to private
2176/// memory per sub-group. Requires the sub-group size of kernel calling this
2177/// function to be 32.
2178/// Each work item of sub-group contains the starting address of their
2179/// respective matrix row in 'addr'.
2180/// After distributing addresses to other work items, each of the 32 work items
2181/// load 128-bits (32-bits per matrix) into 'm1', 'm2', 'm3' & 'm4' for a total
2182/// of 512 bytes.
2183/// 'trans' specifies to perform a transposed/non-transposed load by each work
2184/// item like below
2185/// Row Major: Each row of the matrices is loaded by a group of 4 work items(wi)
2186/// row-0: wi0 wi0 wi1 wi1 ... wi3 wi3
2187/// row-1: wi4 wi4 wi5 wi5 ... wi7 wi7
2188/// ...
2189/// row-6: wi24 wi24 wi25 wi25 ... wi27 wi27
2190/// row-7: wi28 wi28 wi29 wi29 ... wi31 wi31
2191/// Col Major: Each col of the matrices is loaded by a group of 4 work items(wi)
2192/// row-0: wi0 wi4 wi8 ... wi28
2193/// row-1: wi0 wi4 wi8 ... wi28
2194/// ...
2195/// row-6: wi3 wi7 wi11 ... wi31
2196/// row-7: wi3 wi7 wi11 ... wi31
2197/// \tparam [in] T Type of result variable (currently only supports 16-bit type)
2198/// \param [in] addr The starting address of corresponding matrix row for a work
2199/// item in local memory
2200/// \param [in] m1 The private memory to store the data of 1st matrix. It points
2201/// to 2 b16 type elements.
2202/// \param [in] m2 The private memory to store the data of 2nd matrix. It points
2203/// to 2 b16 type elements.
2204/// \param [in] m3 The private memory to store the data of 3rd matrix. It points
2205/// to 2 b16 type elements.
2206/// \param [in] m4 The private memory to store the data of 4th matrix. It points
2207/// to 2 b16 type elements.
2208/// \param [in] trans Indicates whether the matrix to be loaded transposed
2209template <typename T>
2210void ldmatrix(uintptr_t addr, T *m1, T *m2, T *m3, T *m4, bool trans = false) {
2211  // Load 1st matrix
2212  ldmatrix(addr, m1, trans, 0);
2213  // Load 2nd matrix
2214  ldmatrix(addr, m2, trans, 1);
2215  // Load 3rd matrix
2216  ldmatrix(addr, m3, trans, 2);
2217  // Load 4th matrix
2218  ldmatrix(addr, m4, trans, 3);
2219}
2220
2221/// Collectively stores 1 8x8 b16 (128 bytes) matrix from private memory to
2222/// local memory per sub-group.
2223/// Requires the sub-group size of kernel calling this function to be 32.
2224/// 'mat' specifies the matrix index to be stored. The first '(mat + 1) * 8'
2225/// work items of sub-group contain the starting address of their respective
2226/// matrix row in 'addr'.
2227/// After distributing addresses to other work items, each of the 32 work items
2228/// store 32-bits (2 packed 16-bit data) into 'm' for a total of 128 bytes.
2229/// 'trans' specifies to perform a transposed/non-transposed store by each work
2230/// item like below
2231/// Row Major: Each row of the matrix is stored by a group of 4 work items(wi)
2232/// row-0: wi0 wi0 wi1 wi1 ... wi3 wi3
2233/// row-1: wi4 wi4 wi5 wi5 ... wi7 wi7
2234/// ...
2235/// row-6: wi24 wi24 wi25 wi25 ... wi27 wi27
2236/// row-7: wi28 wi28 wi29 wi29 ... wi31 wi31
2237/// Col Major: Each col of the matrix is stored by a group of 4 work items(wi)
2238/// row-0: wi0 wi4 wi8 ... wi28
2239/// row-1: wi0 wi4 wi8 ... wi28
2240/// ...
2241/// row-6: wi3 wi7 wi11 ... wi31
2242/// row-7: wi3 wi7 wi11 ... wi31
2243/// \tparam [in] T Type of result variable (currently only supports 16-bit type)
2244/// \param [in] addr The starting address of corresponding matrix row for a work
2245/// item in local memory
2246/// \param [in] m The local memory to store the matrix. It points to 2 b16
2247/// type elements.
2248/// \param [in] trans Indicates whether the matrix to be stored transposed
2249/// \param [in] mat The matrix index to be stored
2250template <typename T>
2251void stmatrix(uintptr_t addr, T m, bool trans = false, unsigned mat = 0) {
2252  auto sg = sycl::ext::oneapi::this_work_item::get_sub_group();
2253  int lane = sg.get_local_linear_id();
2254
2255  int lane_group8_row = lane / 8;
2256  int lane_group8_col = lane % 8;
2257
2258  if (!trans) {
2259    // calculate the source lane
2260    int src_lane = 2 * lane_group8_row;
2261    if (lane_group8_col >= 4)
2262      src_lane += 1;
2263
2264    // Broadcast the address from the source lane
2265    auto recv_addr_uintp =
2266        dpct::select_from_sub_group(sg, addr, mat * 8 + src_lane);
2267
2268    // Cast the received address from uintptr_t to the type of 'm'
2269    auto recv_addr = reinterpret_cast<T *>(recv_addr_uintp);
2270
2271    // Non-transposed store
2272    recv_addr[lane_group8_col % 4] = m;
2273  } else {
2274    // calculate the source lane
2275    int src_lane = (lane % 4) * 2;
2276
2277    // Broadcast the address from the source lane
2278    auto recv_addr_uintp_1 =
2279        dpct::select_from_sub_group(sg, addr, mat * 8 + src_lane);
2280    auto recv_addr_uintp_2 =
2281        dpct::select_from_sub_group(sg, addr, mat * 8 + src_lane + 1);
2282
2283    // Cast the received address from uintptr_t to 'half *'
2284    auto recv_addr_1 = reinterpret_cast<sycl::half *>(recv_addr_uintp_1);
2285    auto recv_addr_2 = reinterpret_cast<sycl::half *>(recv_addr_uintp_2);
2286
2287    // Split the 32-bit value of 'm' into two 16-bits
2288    sycl::half *val = reinterpret_cast<sycl::half *>(&m);
2289
2290    // Transposed store
2291    int index = lane / 4;
2292    recv_addr_1[index] = val[0];
2293    recv_addr_2[index] = val[1];
2294  }
2295}
2296
2297/// Collectively stores 2 8x8 b16 (256 bytes) matrix from private memory to
2298/// local memory per sub-group.
2299/// Requires the sub-group size of kernel calling this function to be 32.
2300/// The first 16 work items of sub-group contain the starting address of their
2301/// respective matrix row in 'addr'.
2302/// After distributing addresses to other work items, each of the 32 work items
2303/// store 64-bits (32-bits per matrix) into 'm1' & 'm2' for a total of 256
2304/// bytes.
2305/// 'trans' specifies to perform a transposed/non-transposed store by each work
2306/// item like below
2307/// Row Major: Each row of the matrices is stored by a group of 4 work items(wi)
2308/// row-0: wi0 wi0 wi1 wi1 ... wi3 wi3
2309/// row-1: wi4 wi4 wi5 wi5 ... wi7 wi7
2310/// ...
2311/// row-6: wi24 wi24 wi25 wi25 ... wi27 wi27
2312/// row-7: wi28 wi28 wi29 wi29 ... wi31 wi31
2313/// Col Major: Each col of the matrices is stored by a group of 4 work items(wi)
2314/// row-0: wi0 wi4 wi8 ... wi28
2315/// row-1: wi0 wi4 wi8 ... wi28
2316/// ...
2317/// row-6: wi3 wi7 wi11 ... wi31
2318/// row-7: wi3 wi7 wi11 ... wi31
2319/// \tparam [in] T Type of result variable (currently only supports 16-bit type)
2320/// \param [in] addr The starting address of corresponding matrix row for a work
2321/// item in local memory
2322/// \param [in] m1 The local memory to store the data of 1st matrix. It points
2323/// to 2 b16 type elements.
2324/// \param [in] m2 The local memory to store the data of 2nd matrix. It points
2325/// to 2 b16 type elements.
2326/// \param [in] trans Indicates whether the matrix to be stored transposed
2327template <typename T>
2328void stmatrix(uintptr_t addr, T m1, T m2, bool trans = false) {
2329  // Store 1st matrix
2330  stmatrix(addr, m1, trans, 0);
2331  // Store 2nd matrix
2332  stmatrix(addr, m2, trans, 1);
2333}
2334
2335/// Collectively stores 4 8x8 b16 (512 bytes) matrix from private memory to
2336/// local memory per sub-group.
2337/// Requires the sub-group size of kernel calling this function to be 32.
2338/// Each work item of sub-group contains the starting address of their
2339/// respective matrix row in 'addr'.
2340/// After distributing addresses to other work items, each of the 32 work items
2341/// store 128-bits (32-bits per matrix) into 'm1', 'm2', 'm3' & 'm4' for a total
2342/// of 512 bytes.
2343/// 'trans' specifies to perform a transposed/non-transposed store by each work
2344/// item like below
2345/// Row Major: Each row of the matrices is stored by a group of 4 work items(wi)
2346/// row-0: wi0 wi0 wi1 wi1 ... wi3 wi3
2347/// row-1: wi4 wi4 wi5 wi5 ... wi7 wi7
2348/// ...
2349/// row-6: wi24 wi24 wi25 wi25 ... wi27 wi27
2350/// row-7: wi28 wi28 wi29 wi29 ... wi31 wi31
2351/// Col Major: Each col of the matrices is stored by a group of 4 work items(wi)
2352/// row-0: wi0 wi4 wi8 ... wi28
2353/// row-1: wi0 wi4 wi8 ... wi28
2354/// ...
2355/// row-6: wi3 wi7 wi11 ... wi31
2356/// row-7: wi3 wi7 wi11 ... wi31
2357/// \tparam [in] T Type of result variable (currently only supports 16-bit type)
2358/// \param [in] addr The starting address of corresponding matrix row for a work
2359/// item in local memory
2360/// \param [in] m1 The local memory to store the data of 1st matrix. It points
2361/// to 2 b16 type elements.
2362/// \param [in] m2 The local memory to store the data of 2nd matrix. It points
2363/// to 2 b16 type elements.
2364/// \param [in] m3 The local memory to store the data of 3rd matrix. It points
2365/// to 2 b16 type elements.
2366/// \param [in] m4 The local memory to store the data of 4th matrix. It points
2367/// to 2 b16 type elements.
2368/// \param [in] trans Indicates whether the matrix to be stored transposed
2369template <typename T>
2370void stmatrix(uintptr_t addr, T m1, T m2, T m3, T m4, bool trans = false) {
2371  // Store 1st matrix
2372  stmatrix(addr, m1, trans, 0);
2373  // Store 2nd matrix
2374  stmatrix(addr, m2, trans, 1);
2375  // Store 3rd matrix
2376  stmatrix(addr, m3, trans, 2);
2377  // Store 4th matrix
2378  stmatrix(addr, m4, trans, 3);
2379}
2380
2381/// A helper struct that defines the pack type for the input matrix fragments
2382/// of mma() function based on the type of input matrix fragments.
2383/// The MMAType struct is specialized for different types of input matrices.
2384/// Currently, the specialization for f16, bf16 and s8 types is defined below.
2385/// \tparam [in] T The type of the input matrix fragments
2386template <typename T> struct MMAType {
2387  using PackType = uint32_t;
2388};
2389
2390/// Each work item of a sub-group (limited to size 32) calling this function
2391/// calculates a subset fragment for the output matrix D using MAD operation on
2392/// A, B & C matrix fragments (D = A * B + C).
2393/// Current supported shapes & types:
2394/// - m8n8k4 (f32.f16.f16.f32)
2395/// - m8n8k16 (s32.s8.s8.s32)
2396/// - m16n8k8 (f32.f16.f16.f32 & f32.bf16.bf16.f32)
2397/// - m16n8k16 (f32.f16.f16.f32 & s32.s8.s8.s32)
2398/// - m16n8k32 (s32.s8.s8.s32)
2399/// Here, m, n & k define the shapes of A, B & C matrices respectively
2400/// (A = [m x k], B = [k x n], C = [m x n]).
2401/// \tparam [in] M The rows of A, C & D matrices
2402/// \tparam [in] N The columns of B, C, D matrices
2403/// \tparam [in] K The columns & rows of A & B matrices respectively
2404/// \tparam [in] ABType The type of the input matrix (A & B) fragment
2405/// \tparam [in] CDType The type of the output matrix (C & D) fragment
2406/// \param [out] d_mat_frag The fragment of the output matrix D to store the
2407/// result of A * B + C
2408/// \param [in] a_mat_frag The fragment of the input matrix A to be multiplied
2409/// with B matrix fragment
2410/// \param [in] b_mat_frag The fragment of the input matrix B to be multiplied
2411/// with A matrix fragment
2412/// \param [in] c_mat_frag The fragment of the input matrix C to be added with
2413/// the result of A * B fragments
2414template <int M, int N, int K, typename ABType, typename CDType>
2415void mma(volatile void **d_mat_frag, void *a_mat_frag, void *b_mat_frag,
2416         void *c_mat_frag) {
2417  auto d = reinterpret_cast<volatile CDType **>(d_mat_frag);
2418  auto a = reinterpret_cast<typename MMAType<ABType>::PackType *>(a_mat_frag);
2419  auto b = reinterpret_cast<typename MMAType<ABType>::PackType *>(b_mat_frag);
2420  auto c = reinterpret_cast<CDType *>(c_mat_frag);
2421
2422  auto sg = sycl::ext::oneapi::this_work_item::get_sub_group();
2423  int lane = sg.get_local_linear_id();
2424
2425  static_assert((M == 8 && N == 8 && K == 4) || (M == 8 && N == 8 && K == 16) ||
2426                    (M == 16 && N == 8 && K == 8) ||
2427                    (M == 16 && N == 8 && K == 16) ||
2428                    (M == 16 && N == 8 && K == 32),
2429                "Unsupported MMA shape!");
2430
2431  short row_load_offset = 4 * (lane >> 2);
2432  short col_load_offset = 8 * (lane % 4);
2433
2434  if constexpr (M == 8 && N == 8 && K == 4) {
2435    if constexpr (std::is_floating_point_v<CDType>) {
2436      col_load_offset = row_load_offset % 16;
2437
2438      // Init D matrix with fragments of C matrix
2439      *d[0] = c[0];
2440      *d[1] = c[1];
2441      *d[2] = c[2];
2442      *d[3] = c[3];
2443      *d[4] = c[4];
2444      *d[5] = c[5];
2445      *d[6] = c[6];
2446      *d[7] = c[7];
2447
2448      // Calculate the row and col offset indices to iterate through the row &
2449      // col fragments of A & B matrices
2450      int r_ind = (lane % 2) ? 1 : 0;
2451      int c_ind = ((lane % 4) / 2) ? 2 : 0;
2452
2453      // Each sub-group is responsible for computing a fragment size of 8*8
2454      // elements of matrix D for each of 4 MMA computations.
2455      // Each work item computes 8 elements of matrix D by gathering
2456      // their corresponding col & row matrix fragments of length k (4)
2457      // from A & B matrices respectively using below mapping logic:
2458      // row0 = (i % 4) if (lane < 16) else (i % 4) + 4
2459      // col0 = (lane % 4)
2460      // As each row & col fragment of A & B matrices is distributed across
2461      // 4 work items, each iteration of below loop loads a partial fragment of
2462      // matrix A (row) and matrix B (col) using the row & col offsets.
2463      typename MMAType<ABType>::PackType recv_a[2], recv_b[2];
2464
2465      for (int i = 0; i < 4; i++) {
2466        // Load partial fragment from col0 of matrix A ({a0, a1})
2467        recv_a[0] = dpct::select_from_sub_group(sg, a[0], row_load_offset + i);
2468        // Load partial fragment from col0 of matrix A ({a2, a3})
2469        recv_a[1] = dpct::select_from_sub_group(sg, a[1], row_load_offset + i);
2470
2471        // Load partial fragment from row0 of matrix B ({b0, b1})
2472        recv_b[0] = dpct::select_from_sub_group(sg, b[0], col_load_offset + i);
2473        // Load partial fragment from row0 of matrix B ({b2, b3})
2474        recv_b[1] = dpct::select_from_sub_group(sg, b[1], col_load_offset + i);
2475
2476        auto ra = reinterpret_cast<ABType *>(recv_a);
2477        auto rb = reinterpret_cast<ABType *>(recv_b);
2478
2479        // Each work item calculates a partial product of A & B matrix fragments
2480        // and adds it to the corresponding D matrix fragment
2481        // (for even work item indices)
2482        // d0 += col0{ a0 } * row0{ b0 }
2483        // d1 += col0{ a0 } * row0{ b1 }
2484        // d2 += col1{ a2 } * row0{ b0 }
2485        // d3 += col1{ a2 } * row0{ b1 }
2486        // (for odd work item indices)
2487        // d0 += col0{ a1 } * row0{ b2 }
2488        // d1 += col0{ a1 } * row0{ b3 }
2489        // d2 += col1{ a3 } * row0{ b2 }
2490        // d3 += col1{ a3 } * row0{ b3 }
2491        *d[0] += static_cast<float>(ra[r_ind]) * static_cast<float>(rb[c_ind]);
2492        *d[1] +=
2493            static_cast<float>(ra[r_ind]) * static_cast<float>(rb[c_ind + 1]);
2494        *d[2] +=
2495            static_cast<float>(ra[r_ind + 2]) * static_cast<float>(rb[c_ind]);
2496        *d[3] += static_cast<float>(ra[r_ind + 2]) *
2497                 static_cast<float>(rb[c_ind + 1]);
2498
2499        // Load partial fragment from row1 of matrix B ({b0, b1})
2500        recv_b[0] =
2501            dpct::select_from_sub_group(sg, b[0], col_load_offset + i + 16);
2502        // Load partial fragment from row1 of matrix B ({b2, b3})
2503        recv_b[1] =
2504            dpct::select_from_sub_group(sg, b[1], col_load_offset + i + 16);
2505
2506        // (for even work item indices)
2507        // d0 += col0{ a0 } * row1{ b0 }
2508        // d1 += col0{ a0 } * row1{ b1 }
2509        // d2 += col1{ a2 } * row1{ b0 }
2510        // d3 += col1{ a2 } * row1{ b1 }
2511        // (for odd work item indices)
2512        // d0 += col0{ a1 } * row1{ b2 }
2513        // d1 += col0{ a1 } * row1{ b3 }
2514        // d2 += col1{ a3 } * row1{ b2 }
2515        // d3 += col1{ a3 } * row1{ b3 }
2516        *d[4] += static_cast<float>(ra[r_ind]) * static_cast<float>(rb[c_ind]);
2517        *d[5] +=
2518            static_cast<float>(ra[r_ind]) * static_cast<float>(rb[c_ind + 1]);
2519        *d[6] +=
2520            static_cast<float>(ra[r_ind + 2]) * static_cast<float>(rb[c_ind]);
2521        *d[7] += static_cast<float>(ra[r_ind + 2]) *
2522                 static_cast<float>(rb[c_ind + 1]);
2523      }
2524    }
2525  } else if constexpr (M == 8 && N == 8 && K == 16) {
2526    if constexpr (std::is_integral_v<ABType>) {
2527      // Init D matrix with fragments of C matrix
2528      *d[0] = c[0];
2529      *d[1] = c[1];
2530
2531      // Each sub-group is responsible for computing a fragment size of 16*8
2532      // elements of matrix D.
2533      // Each work item computes 2 elements of matrix D by gathering
2534      // their corresponding row & col matrix fragments of length k (16)
2535      // from A & B matrices respectively using below mapping logic:
2536      // row0 = ((lane % 4) * 4) + i
2537      // col0 = (lane >> 2)
2538      // As each row & col fragment of A & B matrices is distributed across
2539      // 4 work items, each iteration of below loop loads a partial fragment of
2540      // matrix A (row) and matrix B (col) using the row & col offsets.
2541      for (int i = 0; i < 4; i++) {
2542        typename MMAType<ABType>::PackType recv_a, recv_b[2];
2543
2544        // Load partial fragment from row0 of matrix A ({a0, a1, a2, a3})
2545        recv_a = dpct::select_from_sub_group(sg, a[0], row_load_offset + i);
2546        // Load partial fragment from col0 of matrix B ({b0, b1, b2, b3})
2547        recv_b[0] = dpct::select_from_sub_group(sg, b[0], col_load_offset + i);
2548        // Load partial fragment from col1 of matrix B ({b0, b1, b2, b3})
2549        recv_b[1] =
2550            dpct::select_from_sub_group(sg, b[0], col_load_offset + i + 4);
2551
2552        auto a = reinterpret_cast<ABType *>(&recv_a);
2553        auto b = reinterpret_cast<ABType *>(recv_b);
2554
2555        // Each work item calculates a partial product of A & B matrix fragments
2556        // and adds it to the corresponding D matrix fragment
2557        // d0 += row0{ a0, a1, a2, a3 } * col0{ b0, b1, b2, b3 }
2558        // d1 += row0{ a0, a1, a2, a3 } * col1{ b0, b1, b2, b3 }
2559        // d2 += row0{ a0, a1, a2, a3 } * col0{ b0, b1, b2, b3 }
2560        // d3 += row0{ a0, a1, a2, a3 } * col1{ b0, b1, b2, b3 }
2561        for (int j = 0; j < 4; j++) {
2562          *d[0] += a[j] * b[j];
2563          *d[1] += a[j] * b[j + 4];
2564        }
2565      }
2566    }
2567  } else if constexpr (M == 16 && N == 8 && K == 8) {
2568    if constexpr (std::is_floating_point_v<CDType>) {
2569      // Init D matrix fragment with C matrix fragment
2570      *d[0] = c[0];
2571      *d[1] = c[1];
2572      *d[2] = c[2];
2573      *d[3] = c[3];
2574
2575      // Each sub-group is responsible for computing a fragment size of 16*8
2576      // elements of matrix D.
2577      // Each work item computes 4 elements of matrix D by gathering
2578      // their corresponding row & col matrix fragments of length k (8)
2579      // from A & B matrices respectively using below mapping logic:
2580      // row0 = (lane >> 2) & row1 = (lane >> 2) + 8
2581      // col0 = (lane % 4) * 2 + (i & 0x1)
2582      // As each row & col fragment of A & B matrices is distributed across
2583      // 4 work items, each iteration of below loop loads a partial fragment of
2584      // matrix A (row) and matrix B (col) using the row & col offsets.
2585      for (int i = 0; i < 4; i++) {
2586        typename MMAType<ABType>::PackType recv_a[2], recv_b[2];
2587
2588        // Load partial fragment from row0 of matrix A ({a0, a1})
2589        recv_a[0] = dpct::select_from_sub_group(sg, a[0], row_load_offset + i);
2590        // Load partial fragment from row1 of matrix A ({a2, a3})
2591        recv_a[1] = dpct::select_from_sub_group(sg, a[1], row_load_offset + i);
2592        // Load partial fragment from col0 of matrix B ({b0, b1})
2593        recv_b[0] = dpct::select_from_sub_group(sg, b[0], col_load_offset + i);
2594        // Load partial fragment from col1 of matrix B ({b0, b1})
2595        recv_b[1] =
2596            dpct::select_from_sub_group(sg, b[0], col_load_offset + i + 4);
2597
2598        auto ra = reinterpret_cast<ABType *>(recv_a);
2599        auto rb = reinterpret_cast<ABType *>(recv_b);
2600
2601        // Each work item calculates a partial product of A & B matrix fragments
2602        // and adds it to the corresponding D matrix fragment
2603        // d0 += row0{ a0, a1 } * col0{ b0, b1 }
2604        // d1 += row0{ a0, a1 } * col1{ b0, b1 }
2605        // d2 += row1{ a2, a3 } * col0{ b0, b1 }
2606        // d3 += row1{ a2, a3 } * col1{ b0, b1 }
2607        for (int j = 0; j < 2; j++) {
2608          *d[0] += static_cast<float>(ra[j]) * static_cast<float>(rb[j]);
2609          *d[1] += static_cast<float>(ra[j]) * static_cast<float>(rb[j + 2]);
2610          *d[2] += static_cast<float>(ra[j + 2]) * static_cast<float>(rb[j]);
2611          *d[3] +=
2612              static_cast<float>(ra[j + 2]) * static_cast<float>(rb[j + 2]);
2613        }
2614      }
2615    }
2616  } else if constexpr (M == 16 && N == 8 && K == 16) {
2617    if constexpr (std::is_floating_point_v<CDType>) {
2618      // Init D matrix fragment with C matrix fragment
2619      *d[0] = c[0];
2620      *d[1] = c[1];
2621      *d[2] = c[2];
2622      *d[3] = c[3];
2623
2624      // Each sub-group is responsible for computing a fragment size of 16*8
2625      // elements of matrix D.
2626      // Each work item computes 4 elements of matrix D by gathering
2627      // their corresponding row & col matrix fragments of length k (8)
2628      // from A & B matrices respectively using below mapping logic:
2629      // row0 = (lane >> 2)    & row1 = (lane >> 2) + 8
2630      // col0 = (lane % 4) * 2 & col1 = (lane % 4) * 2 + 1
2631      // As each row & col fragment of A & B matrices is distributed across
2632      // 4 work items, each iteration of below loop loads a partial fragment of
2633      // matrix A (row) and matrix B (col) using the row & col offsets.
2634      for (int i = 0; i < 4; i++) {
2635        typename MMAType<ABType>::PackType recv_a[4], recv_b[4];
2636
2637        // Load partial fragment from row0 of matrix A ({a0, a1})
2638        recv_a[0] = dpct::select_from_sub_group(sg, a[0], row_load_offset + i);
2639        // Load partial fragment from row0 of matrix A ({a2, a3})
2640        recv_a[1] = dpct::select_from_sub_group(sg, a[2], row_load_offset + i);
2641        // Load partial fragment from row1 of matrix A ({a0, a1})
2642        recv_a[2] = dpct::select_from_sub_group(sg, a[1], row_load_offset + i);
2643        // Load partial fragment from row1 of matrix A ({a2, a3})
2644        recv_a[3] = dpct::select_from_sub_group(sg, a[3], row_load_offset + i);
2645
2646        // Load partial fragment from col0 of matrix B ({b0, b1})
2647        recv_b[0] = dpct::select_from_sub_group(sg, b[0], col_load_offset + i);
2648        // Load partial fragment from col0 of matrix B ({b2, b3})
2649        recv_b[1] = dpct::select_from_sub_group(sg, b[1], col_load_offset + i);
2650        // Load partial fragment from col1 of matrix B ({b0, b1})
2651        recv_b[2] =
2652            dpct::select_from_sub_group(sg, b[0], col_load_offset + 4 + i);
2653        // Load partial fragment from col1 of matrix B ({b2, b3})
2654        recv_b[3] =
2655            dpct::select_from_sub_group(sg, b[1], col_load_offset + 4 + i);
2656
2657        auto ra = reinterpret_cast<ABType *>(recv_a);
2658        auto rb = reinterpret_cast<ABType *>(recv_b);
2659
2660        // Each work item calculates a partial product of A & B matrix fragments
2661        // and adds it to the corresponding D matrix fragment
2662        // d0 += row0{ a0, a1, a2, a3 } * col0{ b0, b1, b2, b3 }
2663        // d1 += row0{ a0, a1, a2, a3 } * col1{ b0, b1, b2, b3 }
2664        // d2 += row1{ a0, a1, a2, a3 } * col0{ b0, b1, b2, b3 }
2665        // d3 += row1{ a0, a1, a2, a3 } * col1{ b0, b1, b2, b3 }
2666        for (int j = 0; j < 4; j++) {
2667          *d[0] += static_cast<CDType>(ra[j]) * static_cast<CDType>(rb[j]);
2668          *d[1] += static_cast<CDType>(ra[j]) * static_cast<CDType>(rb[j + 4]);
2669          *d[2] += static_cast<CDType>(ra[j + 4]) * static_cast<CDType>(rb[j]);
2670          *d[3] +=
2671              static_cast<CDType>(ra[j + 4]) * static_cast<CDType>(rb[j + 4]);
2672        }
2673      }
2674    } else if constexpr (std::is_integral_v<ABType>) {
2675      // Init D matrix with fragments of C matrix
2676      *d[0] = c[0];
2677      *d[1] = c[1];
2678      *d[2] = c[2];
2679      *d[3] = c[3];
2680
2681      // Each sub-group is responsible for computing a fragment size of 16*8
2682      // elements of matrix D.
2683      // Each work item computes 4 elements of matrix D by gathering
2684      // their corresponding row & col matrix fragments of length k (8)
2685      // from A & B matrices respectively using below mapping logic:
2686      // row0 = (lane >> 2)    & row1 = (lane >> 2) + 8
2687      // col0 = (lane % 4) * 2 & col1 = (lane % 4) * 2 + 1
2688      // As each row & col fragment of A & B matrices is distributed across
2689      // 4 work items, each iteration of below loop loads a partial fragment of
2690      // matrix A (row) and matrix B (col) using the row & col offsets.
2691      for (int i = 0; i < 4; i++) {
2692        typename MMAType<ABType>::PackType recv_a[2], recv_b[2];
2693
2694        // Load partial fragment from row0 of matrix A ({a0, a1, a2, a3})
2695        recv_a[0] = dpct::select_from_sub_group(sg, a[0], row_load_offset + i);
2696        // Load partial fragment from row1 of matrix A ({a4, a5, a6, a7})
2697        recv_a[1] = dpct::select_from_sub_group(sg, a[1], row_load_offset + i);
2698        // Load partial fragment from col0 of matrix B ({b0, b1, b2, b3})
2699        recv_b[0] = dpct::select_from_sub_group(sg, b[0], col_load_offset + i);
2700        // Load partial fragment from col1 of matrix B ({b4, b5, b6, b7})
2701        recv_b[1] =
2702            dpct::select_from_sub_group(sg, b[0], col_load_offset + i + 4);
2703
2704        auto ra = reinterpret_cast<ABType *>(recv_a);
2705        auto rb = reinterpret_cast<ABType *>(recv_b);
2706
2707        // Each work item calculates a partial product of A & B matrix fragments
2708        // and adds it to the corresponding D matrix fragment
2709        // d0 += row0{ a0, a1, a2, a3 } * col0{ b0, b1, b2, b3 }
2710        // d1 += row0{ a0, a1, a2, a3 } * col1{ b4, b5, b6, b7 }
2711        // d2 += row1{ a4, a5, a6, a7 } * col0{ b0, b1, b2, b3 }
2712        // d3 += row1{ a4, a5, a6, a7 } * col1{ b4, b5, b6, b7 }
2713        for (int i = 0; i < 4; i++) {
2714          *d[0] += ra[i] * rb[i];
2715          *d[1] += ra[i] * rb[i + 4];
2716          *d[2] += ra[i + 4] * rb[i];
2717          *d[3] += ra[i + 4] * rb[i + 4];
2718        }
2719      }
2720    }
2721  } else if constexpr (M == 16 && N == 8 && K == 32) {
2722    if constexpr (std::is_integral_v<ABType>) {
2723      // Init D matrix with fragments of C matrix
2724      *d[0] = c[0];
2725      *d[1] = c[1];
2726      *d[2] = c[2];
2727      *d[3] = c[3];
2728
2729      // Each sub-group is responsible for computing a fragment size of 16*8
2730      // elements of matrix D.
2731      // Each work item computes 4 elements of matrix D by gathering
2732      // their corresponding row & col matrix fragments of length k (32)
2733      // from A & B matrices respectively using below mapping logic:
2734      // row0 = (lane >> 2)    & row1 = (lane >> 2) + 8
2735      // col0 = ((lane % 4) * 4) + (i & 0x3) & col1 = ((lane % 4) * 4) + (i &
2736      // 0x3) As each row & col fragment of A & B matrices is distributed across
2737      // 4 work items, each iteration of below loop loads a partial fragment of
2738      // matrix A (row) and matrix B (col) using the row & col offsets.
2739      for (int i = 0; i < 4; i++) {
2740        typename MMAType<ABType>::PackType recv_a[2], recv_b[2];
2741
2742        // Load partial fragment from row0 of matrix A ({a0, a1, a2, a3})
2743        recv_a[0] = dpct::select_from_sub_group(sg, a[0], row_load_offset + i);
2744        // Load partial fragment from row1 of matrix A ({a4, a5, a6, a7})
2745        recv_a[1] = dpct::select_from_sub_group(sg, a[1], row_load_offset + i);
2746        // Load partial fragment from col0 of matrix B ({b0, b1, b2, b3})
2747        recv_b[0] = dpct::select_from_sub_group(sg, b[0], col_load_offset + i);
2748        // Load partial fragment from col1 of matrix B ({b0, b1, b2, b3})
2749        recv_b[1] =
2750            dpct::select_from_sub_group(sg, b[0], col_load_offset + i + 4);
2751
2752        auto a = reinterpret_cast<ABType *>(recv_a);
2753        auto b = reinterpret_cast<ABType *>(recv_b);
2754
2755        // Each work item calculates a partial product of A & B matrix fragments
2756        // and adds it to the corresponding D matrix fragment
2757        // d0 += row0{ a0, a1, a2, a3 } * col0{ b0, b1, b2, b3 }
2758        // d1 += row0{ a0, a1, a2, a3 } * col1{ b0, b1, b2, b3 }
2759        // d2 += row1{ a4, a5, a6, a7 } * col0{ b0, b1, b2, b3 }
2760        // d3 += row1{ a4, a5, a6, a7 } * col1{ b0, b1, b2, b3 }
2761        for (int j = 0; j < 4; j++) {
2762          *d[0] += a[j] * b[j];
2763          *d[1] += a[j] * b[j + 4];
2764          *d[2] += a[j + 4] * b[j];
2765          *d[3] += a[j + 4] * b[j + 4];
2766        }
2767      }
2768
2769      for (int i = 0; i < 4; i++) {
2770        typename MMAType<ABType>::PackType recv_a[2], recv_b[2];
2771
2772        // Load partial fragment from row0 of matrix A ({a8, a9, a10, a11})
2773        recv_a[0] = dpct::select_from_sub_group(sg, a[2], row_load_offset + i);
2774        // Load partial fragment from row1 of matrix A ({a12, a13, a14, a15})
2775        recv_a[1] = dpct::select_from_sub_group(sg, a[3], row_load_offset + i);
2776        // Load partial fragment from col0 of matrix B ({b4, b5, b6, b7})
2777        recv_b[0] = dpct::select_from_sub_group(sg, b[1], col_load_offset + i);
2778        // Load partial fragment from col1 of matrix B ({b4, b5, b6, b7})
2779        recv_b[1] =
2780            dpct::select_from_sub_group(sg, b[1], col_load_offset + i + 4);
2781
2782        auto a = reinterpret_cast<ABType *>(recv_a);
2783        auto b = reinterpret_cast<ABType *>(recv_b);
2784
2785        // Each work item calculates a partial product of A & B matrix fragments
2786        // and adds it to the corresponding D matrix fragment
2787        // d0 += row0{ a8, a9, a10, a11 } * col0{ b4, b5, b6, b7 }
2788        // d1 += row0{ a8, a9, a10, a11 } * col1{ b4, b5, b6, b7 }
2789        // d2 += row1{ a12, a13, a14, a15 } * col0{ b4, b5, b6, b7 }
2790        // d3 += row1{ a12, a13, a14, a15 } * col1{ b4, b5, b6, b7 }
2791        for (int j = 0; j < 4; j++) {
2792          *d[0] += a[j] * b[j];
2793          *d[1] += a[j] * b[j + 4];
2794          *d[2] += a[j + 4] * b[j];
2795          *d[3] += a[j + 4] * b[j + 4];
2796        }
2797      }
2798    }
2799  }
2800}
2801
2802} // namespace matrix
2803} // namespace experimental
2804
2805} // namespace dpct
2806
2807#endif // __DPCT_MATH_HPP__