|
| 1 | +/*************************************************************************** |
| 2 | + * |
| 3 | + * Copyright (C) Codeplay Software Ltd. |
| 4 | + * |
| 5 | + * Part of the LLVM Project, under the Apache License v2.0 with LLVM |
| 6 | + * Exceptions. See https://llvm.org/LICENSE.txt for license information. |
| 7 | + * SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception |
| 8 | + * |
| 9 | + * Unless required by applicable law or agreed to in writing, software |
| 10 | + * distributed under the License is distributed on an "AS IS" BASIS, |
| 11 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 12 | + * See the License for the specific language governing permissions and |
| 13 | + * limitations under the License. |
| 14 | + * |
| 15 | + * SYCL compatibility extension |
| 16 | + * |
| 17 | + * math.hpp |
| 18 | + * |
| 19 | + * Description: |
| 20 | + * math utilities for the SYCL compatibility extension. |
| 21 | + **************************************************************************/ |
| 22 | + |
| 23 | +// The original source was under the license below: |
| 24 | +//==---- math.hpp ---------------------------------*- C++ -*----------------==// |
| 25 | +// |
| 26 | +// Copyright (C) Intel Corporation |
| 27 | +// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception |
| 28 | +// See https://llvm.org/LICENSE.txt for license information. |
| 29 | +// |
| 30 | +//===----------------------------------------------------------------------===// |
| 31 | + |
| 32 | +#pragma once |
| 33 | + |
| 34 | +#include <sycl/sycl.hpp> |
| 35 | + |
| 36 | +#ifndef SYCL_EXT_ONEAPI_COMPLEX |
| 37 | +#define SYCL_EXT_ONEAPI_COMPLEX |
| 38 | +#endif |
| 39 | + |
| 40 | +#include <sycl/ext/oneapi/experimental/complex/complex.hpp> |
| 41 | + |
| 42 | +namespace syclcompat { |
| 43 | +namespace detail { |
| 44 | + |
| 45 | +namespace complex_namespace = sycl::ext::oneapi::experimental; |
| 46 | + |
| 47 | +template <typename ValueT> |
| 48 | +using complex_type = detail::complex_namespace::complex<ValueT>; |
| 49 | + |
| 50 | +} // namespace detail |
| 51 | + |
| 52 | +/// Compute fast_length for variable-length array |
| 53 | +/// \param [in] a The array |
| 54 | +/// \param [in] len Length of the array |
| 55 | +/// \returns The computed fast_length |
| 56 | +inline float fast_length(const float *a, int len) { |
| 57 | + switch (len) { |
| 58 | + case 1: |
| 59 | + return sycl::fast_length(a[0]); |
| 60 | + case 2: |
| 61 | + return sycl::fast_length(sycl::float2(a[0], a[1])); |
| 62 | + case 3: |
| 63 | + return sycl::fast_length(sycl::float3(a[0], a[1], a[2])); |
| 64 | + case 4: |
| 65 | + return sycl::fast_length(sycl::float4(a[0], a[1], a[2], a[3])); |
| 66 | + case 0: |
| 67 | + return 0; |
| 68 | + default: |
| 69 | + float f = 0; |
| 70 | + for (int i = 0; i < len; ++i) |
| 71 | + f += a[i] * a[i]; |
| 72 | + return sycl::sqrt(f); |
| 73 | + } |
| 74 | +} |
| 75 | + |
| 76 | +/// Compute vectorized max for two values, with each value treated as a vector |
| 77 | +/// type \p S |
| 78 | +/// \param [in] S The type of the vector |
| 79 | +/// \param [in] T The type of the original values |
| 80 | +/// \param [in] a The first value |
| 81 | +/// \param [in] b The second value |
| 82 | +/// \returns The vectorized max of the two values |
| 83 | +template <typename S, typename T> inline T vectorized_max(T a, T b) { |
| 84 | + sycl::vec<T, 1> v0{a}, v1{b}; |
| 85 | + auto v2 = v0.template as<S>(); |
| 86 | + auto v3 = v1.template as<S>(); |
| 87 | + v2 = sycl::max(v2, v3); |
| 88 | + v0 = v2.template as<sycl::vec<T, 1>>(); |
| 89 | + return v0; |
| 90 | +} |
| 91 | + |
| 92 | +/// Compute vectorized min for two values, with each value treated as a vector |
| 93 | +/// type \p S |
| 94 | +/// \param [in] S The type of the vector |
| 95 | +/// \param [in] T The type of the original values |
| 96 | +/// \param [in] a The first value |
| 97 | +/// \param [in] b The second value |
| 98 | +/// \returns The vectorized min of the two values |
| 99 | +template <typename S, typename T> inline T vectorized_min(T a, T b) { |
| 100 | + sycl::vec<T, 1> v0{a}, v1{b}; |
| 101 | + auto v2 = v0.template as<S>(); |
| 102 | + auto v3 = v1.template as<S>(); |
| 103 | + v2 = sycl::min(v2, v3); |
| 104 | + v0 = v2.template as<sycl::vec<T, 1>>(); |
| 105 | + return v0; |
| 106 | +} |
| 107 | + |
| 108 | +/// Compute vectorized isgreater for two values, with each value treated as a |
| 109 | +/// vector type \p S |
| 110 | +/// \param [in] S The type of the vector |
| 111 | +/// \param [in] T The type of the original values |
| 112 | +/// \param [in] a The first value |
| 113 | +/// \param [in] b The second value |
| 114 | +/// \returns The vectorized greater than of the two values |
| 115 | +template <typename S, typename T> inline T vectorized_isgreater(T a, T b) { |
| 116 | + sycl::vec<T, 1> v0{a}, v1{b}; |
| 117 | + auto v2 = v0.template as<S>(); |
| 118 | + auto v3 = v1.template as<S>(); |
| 119 | + auto v4 = sycl::isgreater(v2, v3); |
| 120 | + v0 = v4.template as<sycl::vec<T, 1>>(); |
| 121 | + return v0; |
| 122 | +} |
| 123 | + |
| 124 | +/// Compute vectorized isgreater for two unsigned int values, with each value |
| 125 | +/// treated as a vector of two unsigned short |
| 126 | +/// \param [in] a The first value |
| 127 | +/// \param [in] b The second value |
| 128 | +/// \returns The vectorized greater than of the two values |
| 129 | +template <> |
| 130 | +inline unsigned vectorized_isgreater<sycl::ushort2, unsigned>(unsigned a, |
| 131 | + unsigned b) { |
| 132 | + sycl::vec<unsigned, 1> v0{a}, v1{b}; |
| 133 | + auto v2 = v0.template as<sycl::ushort2>(); |
| 134 | + auto v3 = v1.template as<sycl::ushort2>(); |
| 135 | + sycl::ushort2 v4; |
| 136 | + v4[0] = v2[0] > v3[0]; |
| 137 | + v4[1] = v2[1] > v3[1]; |
| 138 | + v0 = v4.template as<sycl::vec<unsigned, 1>>(); |
| 139 | + return v0; |
| 140 | +} |
| 141 | + |
| 142 | +/// Computes the multiplication of two complex numbers. |
| 143 | +/// \tparam T Complex element type |
| 144 | +/// \param [in] x The first input complex number |
| 145 | +/// \param [in] y The second input complex number |
| 146 | +/// \returns The result |
| 147 | +template <typename T> |
| 148 | +sycl::vec<T, 2> cmul(sycl::vec<T, 2> x, sycl::vec<T, 2> y) { |
| 149 | + sycl::ext::oneapi::experimental::complex<T> t1(x[0], x[1]), t2(y[0], y[1]); |
| 150 | + t1 = t1 * t2; |
| 151 | + return sycl::vec<T, 2>(t1.real(), t1.imag()); |
| 152 | +} |
| 153 | + |
| 154 | +/// Computes the division of two complex numbers. |
| 155 | +/// \tparam T Complex element type |
| 156 | +/// \param [in] x The first input complex number |
| 157 | +/// \param [in] y The second input complex number |
| 158 | +/// \returns The result |
| 159 | +template <typename T> |
| 160 | +sycl::vec<T, 2> cdiv(sycl::vec<T, 2> x, sycl::vec<T, 2> y) { |
| 161 | + sycl::ext::oneapi::experimental::complex<T> t1(x[0], x[1]), t2(y[0], y[1]); |
| 162 | + t1 = t1 / t2; |
| 163 | + return sycl::vec<T, 2>(t1.real(), t1.imag()); |
| 164 | +} |
| 165 | + |
| 166 | +/// Computes the magnitude of a complex number. |
| 167 | +/// \tparam T Complex element type |
| 168 | +/// \param [in] x The input complex number |
| 169 | +/// \returns The result |
| 170 | +template <typename T> T cabs(sycl::vec<T, 2> x) { |
| 171 | + sycl::ext::oneapi::experimental::complex<T> t(x[0], x[1]); |
| 172 | + return abs(t); |
| 173 | +} |
| 174 | + |
| 175 | +/// Computes the complex conjugate of a complex number. |
| 176 | +/// \tparam T Complex element type |
| 177 | +/// \param [in] x The input complex number |
| 178 | +/// \returns The result |
| 179 | +template <typename T> sycl::vec<T, 2> conj(sycl::vec<T, 2> x) { |
| 180 | + sycl::ext::oneapi::experimental::complex<T> t(x[0], x[1]); |
| 181 | + t = conj(t); |
| 182 | + return sycl::vec<T, 2>(t.real(), t.imag()); |
| 183 | +} |
| 184 | + |
| 185 | +} // namespace syclcompat |
0 commit comments