okami.mom/strata

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

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

main
9.2 KiB262 linesraw
1//==---- math_internal.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_DETAIL_MATH_DETAIL_HPP__
10#define __DPCT_DETAIL_MATH_DETAIL_HPP__
11
12namespace dpct {
13// forward declar
14template <typename T> inline T clamp(T val, T min_val, T max_val);
15
16namespace detail {
17
18template <typename VecT, class BinaryOperation, class = void>
19class vectorized_binary {
20public:
21  inline VecT operator()(VecT a, VecT b, const BinaryOperation binary_op) {
22    VecT v4;
23    for (size_t i = 0; i < v4.size(); ++i) {
24      v4[i] = binary_op(a[i], b[i]);
25    }
26    return v4;
27  }
28};
29template <typename VecT, class BinaryOperation>
30class vectorized_binary<
31    VecT, BinaryOperation,
32    std::void_t<std::invoke_result_t<BinaryOperation, VecT, VecT>>> {
33public:
34  inline VecT operator()(VecT a, VecT b, const BinaryOperation binary_op) {
35    return binary_op(a, b).template as<VecT>();
36  }
37};
38
39template <typename T> inline bool isnan(const T a) { return sycl::isnan(a); }
40#ifdef SYCL_EXT_ONEAPI_BFLOAT16_MATH_FUNCTIONS
41inline bool isnan(const sycl::ext::oneapi::bfloat16 a) {
42  return sycl::ext::oneapi::experimental::isnan(a);
43}
44#endif
45
46template <typename T>
47constexpr bool is_floating_point =
48    std::disjunction_v<std::is_floating_point<T>, std::is_same<T, sycl::half>
49#ifdef SYCL_EXT_ONEAPI_BFLOAT16_MATH_FUNCTIONS
50                       ,
51                       std::is_same<T, sycl::ext::oneapi::bfloat16>
52#endif
53                       >;
54
55struct shift_left {
56  template <typename T>
57  auto operator()(const T x, const uint32_t offset) const {
58    return x << offset;
59  }
60};
61
62struct shift_right {
63  template <typename T>
64  auto operator()(const T x, const uint32_t offset) const {
65    return x >> offset;
66  }
67};
68
69struct average {
70  template <typename T> auto operator()(const T x, const T y) const {
71    return (x + y + (x + y >= 0)) >> 1;
72  }
73};
74
75/// Extend the 'val' to 'bit' size, zero extend for unsigned int and signed
76/// extend for signed int.
77template <typename T> inline auto zero_or_signed_extent(T val, unsigned bit) {
78  if constexpr (std::is_signed_v<T>) {
79    if constexpr (std::is_same_v<T, int32_t>) {
80      assert(bit < 64 &&
81             "When extend int32 value, bit must be smaller than 64.");
82      return int64_t(val) << (64 - bit) >> (64 - bit);
83    } else if constexpr (std::is_same_v<T, int16_t>) {
84      assert(bit < 32 &&
85             "When extend int16 value, bit must be smaller than 32.");
86      return int32_t(val) << (32 - bit) >> (32 - bit);
87    } else if constexpr (std::is_same_v<T, int8_t>) {
88      assert(bit < 16 &&
89             "When extend int8 value, bit must be smaller than 16.");
90      return int16_t(val) << (16 - bit) >> (16 - bit);
91    } else {
92      assert(bit < 64 && "Cannot extend int64 value.");
93      return val;
94    }
95  } else
96    return val;
97}
98
99template <typename RetT, bool NeedSat, typename AT, typename BT,
100          typename BinaryOperation>
101inline constexpr std::enable_if_t<
102    std::is_integral_v<AT> && std::is_integral_v<BT> &&
103        std::is_integral_v<RetT> && sizeof(AT) == 4 && sizeof(BT) == 4 &&
104        sizeof(RetT) == 4,
105    RetT>
106extend_binary(AT a, BT b, BinaryOperation binary_op) {
107  int64_t extend_a = zero_or_signed_extent(a, 33);
108  int64_t extend_b = zero_or_signed_extent(b, 33);
109  int64_t ret = binary_op(extend_a, extend_b);
110  if constexpr (NeedSat)
111    return dpct::clamp<int64_t>(ret, std::numeric_limits<RetT>::min(),
112                                std::numeric_limits<RetT>::max());
113  return ret;
114}
115
116template <typename RetT, bool NeedSat, typename AT, typename BT, typename CT,
117          typename BinaryOperation1, typename BinaryOperation2>
118inline constexpr std::enable_if_t<
119    std::is_integral_v<AT> && std::is_integral_v<BT> &&
120        std::is_integral_v<CT> && std::is_integral_v<RetT> && sizeof(AT) == 4 &&
121        sizeof(BT) == 4 && sizeof(CT) == 4 && sizeof(RetT) == 4,
122    RetT>
123extend_binary(AT a, BT b, CT c, BinaryOperation1 binary_op,
124              BinaryOperation2 second_op) {
125  int64_t extend_a = zero_or_signed_extent(a, 33);
126  int64_t extend_b = zero_or_signed_extent(b, 33);
127  int64_t extend_temp =
128      zero_or_signed_extent(binary_op(extend_a, extend_b), 34);
129  if constexpr (NeedSat)
130    extend_temp =
131        dpct::clamp<int64_t>(extend_temp, std::numeric_limits<RetT>::min(),
132                             std::numeric_limits<RetT>::max());
133  int64_t extend_c = zero_or_signed_extent(c, 33);
134  return second_op(extend_temp, extend_c);
135}
136
137template <typename T> sycl::vec<int32_t, 2> extractAndExtend2(T a) {
138  sycl::vec<int32_t, 2> ret;
139  sycl::vec<T, 1> va{a};
140  if constexpr (std::is_signed_v<T>) {
141    auto v = va.template as<sycl::vec<int16_t, 2>>();
142    ret[0] = zero_or_signed_extent(v[0], 17);
143    ret[1] = zero_or_signed_extent(v[1], 17);
144  } else {
145    auto v = va.template as<sycl::vec<uint16_t, 2>>();
146    ret[0] = zero_or_signed_extent(v[0], 17);
147    ret[1] = zero_or_signed_extent(v[1], 17);
148  }
149  return ret;
150}
151
152template <typename T> sycl::vec<int16_t, 4> extractAndExtend4(T a) {
153  sycl::vec<int16_t, 4> ret;
154  sycl::vec<T, 1> va{a};
155  if constexpr (std::is_signed_v<T>) {
156    auto v = va.template as<sycl::vec<int8_t, 4>>();
157    ret[0] = zero_or_signed_extent(v[0], 9);
158    ret[1] = zero_or_signed_extent(v[1], 9);
159    ret[2] = zero_or_signed_extent(v[2], 9);
160    ret[3] = zero_or_signed_extent(v[3], 9);
161  } else {
162    auto v = va.template as<sycl::vec<uint8_t, 4>>();
163    ret[0] = zero_or_signed_extent(v[0], 9);
164    ret[1] = zero_or_signed_extent(v[1], 9);
165    ret[2] = zero_or_signed_extent(v[2], 9);
166    ret[3] = zero_or_signed_extent(v[3], 9);
167  }
168  return ret;
169}
170
171template <typename RetT, bool NeedSat, bool NeedAdd, typename AT, typename BT,
172          typename BinaryOperation>
173inline constexpr std::enable_if_t<
174    std::is_integral_v<AT> && std::is_integral_v<BT> &&
175        std::is_integral_v<RetT> && sizeof(AT) == 4 && sizeof(BT) == 4 &&
176        sizeof(RetT) == 4,
177    RetT>
178extend_vbinary2(AT a, BT b, RetT c, BinaryOperation binary_op) {
179  sycl::vec<int32_t, 2> extend_a = extractAndExtend2(a);
180  sycl::vec<int32_t, 2> extend_b = extractAndExtend2(b);
181  sycl::vec<int32_t, 2> temp{binary_op(extend_a[0], extend_b[0]),
182                             binary_op(extend_a[1], extend_b[1])};
183  if constexpr (NeedSat) {
184    int32_t min_val = 0, max_val = 0;
185    if constexpr (std::is_signed_v<RetT>) {
186      min_val = std::numeric_limits<int16_t>::min();
187      max_val = std::numeric_limits<int16_t>::max();
188    } else {
189      min_val = std::numeric_limits<uint16_t>::min();
190      max_val = std::numeric_limits<uint16_t>::max();
191    }
192    temp = dpct::clamp(temp, {min_val, min_val}, {max_val, max_val});
193  }
194  if constexpr (NeedAdd) {
195    return temp[0] + temp[1] + c;
196  }
197  if constexpr (std::is_signed_v<RetT>) {
198    return sycl::vec<int16_t, 2>{temp[0], temp[1]}.as<sycl::vec<RetT, 1>>();
199  } else {
200    return sycl::vec<uint16_t, 2>{temp[0], temp[1]}.as<sycl::vec<RetT, 1>>();
201  }
202}
203
204template <typename RetT, bool NeedSat, bool NeedAdd, typename AT, typename BT,
205          typename BinaryOperation>
206inline constexpr std::enable_if_t<
207    std::is_integral_v<AT> && std::is_integral_v<BT> &&
208        std::is_integral_v<RetT> && sizeof(AT) == 4 && sizeof(BT) == 4 &&
209        sizeof(RetT) == 4,
210    RetT>
211extend_vbinary4(AT a, BT b, RetT c, BinaryOperation binary_op) {
212  sycl::vec<int16_t, 4> extend_a = extractAndExtend4(a);
213  sycl::vec<int16_t, 4> extend_b = extractAndExtend4(b);
214  sycl::vec<int16_t, 4> temp{
215      binary_op(extend_a[0], extend_b[0]), binary_op(extend_a[1], extend_b[1]),
216      binary_op(extend_a[2], extend_b[2]), binary_op(extend_a[3], extend_b[3])};
217  if constexpr (NeedSat) {
218    int16_t min_val = 0, max_val = 0;
219    if constexpr (std::is_signed_v<RetT>) {
220      min_val = std::numeric_limits<int8_t>::min();
221      max_val = std::numeric_limits<int8_t>::max();
222    } else {
223      min_val = std::numeric_limits<uint8_t>::min();
224      max_val = std::numeric_limits<uint8_t>::max();
225    }
226    temp = dpct::clamp(temp, {min_val, min_val, min_val, min_val},
227                       {max_val, max_val, max_val, max_val});
228  }
229  if constexpr (NeedAdd) {
230    return temp[0] + temp[1] + temp[2] + temp[3] + c;
231  }
232  if constexpr (std::is_signed_v<RetT>) {
233    return sycl::vec<int8_t, 4>{temp[0], temp[1], temp[2], temp[3]}
234        .as<sycl::vec<RetT, 1>>();
235  } else {
236    return sycl::vec<uint8_t, 4>{temp[0], temp[1], temp[2], temp[3]}
237        .as<sycl::vec<RetT, 1>>();
238  }
239}
240
241template <typename T1, typename T2>
242using dot_product_acc_t =
243    std::conditional_t<std::is_unsigned_v<T1> && std::is_unsigned_v<T2>,
244                       uint32_t, int32_t>;
245
246template <typename T> sycl::vec<T, 4> extract_and_sign_or_zero_extend4(T val) {
247  return sycl::vec<T, 1>(val)
248      .template as<sycl::vec<
249          std::conditional_t<std::is_signed_v<T>, int8_t, uint8_t>, 4>>()
250      .template convert<T>();
251}
252
253template <typename T> sycl::vec<T, 2> extract_and_sign_or_zero_extend2(T val) {
254  return sycl::vec<T, 1>(val)
255      .template as<sycl::vec<
256          std::conditional_t<std::is_signed_v<T>, int16_t, uint16_t>, 2>>()
257      .template convert<T>();
258}
259
260} // namespace detail
261} // namespace dpct
262#endif //!__DPCT_DETAIL_MATH_DETAIL_HPP__