|
| 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 | + * SYCLcompat API |
| 16 | + * |
| 17 | + * math_vectorized.cpp |
| 18 | + * |
| 19 | + * Description: |
| 20 | + * math helpers for vectorized operations and fp16 operations |
| 21 | + **************************************************************************/ |
| 22 | + |
| 23 | +// REQUIRES: aspect-fp16 |
| 24 | + |
| 25 | +// RUN: %clangxx -std=c++20 -fsycl -fsycl-targets=%{sycl_triple} %s -o %t.out |
| 26 | +// RUN: %{run} %t.out |
| 27 | + |
| 28 | +#include <syclcompat/math.hpp> |
| 29 | + |
| 30 | +#include "../common.hpp" |
| 31 | +#include "math_fixt.hpp" |
| 32 | + |
| 33 | +template <typename BinaryOp, typename ValueT> |
| 34 | +void vectorized_binary_kernel(ValueT *a, ValueT *b, unsigned *r) { |
| 35 | + unsigned ua = static_cast<unsigned>(*a); |
| 36 | + unsigned ub = static_cast<unsigned>(*b); |
| 37 | + *r = syclcompat::vectorized_binary<sycl::short2>(ua, ub, BinaryOp()); |
| 38 | +} |
| 39 | + |
| 40 | +template <typename BinaryOp, typename ValueT> |
| 41 | +void test_vectorized_binary(ValueT op1, ValueT op2, unsigned expected) { |
| 42 | + std::cout << __PRETTY_FUNCTION__ << std::endl; |
| 43 | + constexpr syclcompat::dim3 grid{1}; |
| 44 | + constexpr syclcompat::dim3 threads{1}; |
| 45 | + |
| 46 | + BinaryOpTestLauncher<ValueT, ValueT, unsigned>(grid, threads) |
| 47 | + .template launch_test<vectorized_binary_kernel<BinaryOp, ValueT>>( |
| 48 | + op1, op2, expected); |
| 49 | +} |
| 50 | + |
| 51 | +template <typename UnaryOp, typename ValueT> |
| 52 | +void vectorized_unary_kernel(ValueT *a, unsigned *r) { |
| 53 | + unsigned ua = static_cast<unsigned>(*a); |
| 54 | + *r = syclcompat::vectorized_unary<sycl::short2>(ua, UnaryOp()); |
| 55 | +} |
| 56 | + |
| 57 | +template <typename UnaryOp, typename ValueT> |
| 58 | +void test_vectorized_unary(ValueT op1, unsigned expected) { |
| 59 | + std::cout << __PRETTY_FUNCTION__ << std::endl; |
| 60 | + constexpr syclcompat::dim3 grid{1}; |
| 61 | + constexpr syclcompat::dim3 threads{1}; |
| 62 | + |
| 63 | + UnaryOpTestLauncher<ValueT, unsigned>(grid, threads) |
| 64 | + .template launch_test<vectorized_unary_kernel<UnaryOp, ValueT>>(op1, |
| 65 | + expected); |
| 66 | +} |
| 67 | + |
| 68 | +template <typename ValueT> |
| 69 | +void vectorized_sum_abs_diff_kernel(ValueT *a, ValueT *b, unsigned *r) { |
| 70 | + unsigned ua = static_cast<unsigned>(*a); |
| 71 | + unsigned ub = static_cast<unsigned>(*b); |
| 72 | + |
| 73 | + *r = syclcompat::vectorized_sum_abs_diff<sycl::short2>(ua, ub); |
| 74 | +} |
| 75 | + |
| 76 | +template <typename ValueT> |
| 77 | +void test_vectorized_sum_abs_diff(ValueT op1, ValueT op2, unsigned expected) { |
| 78 | + std::cout << __PRETTY_FUNCTION__ << std::endl; |
| 79 | + constexpr syclcompat::dim3 grid{1}; |
| 80 | + constexpr syclcompat::dim3 threads{1}; |
| 81 | + |
| 82 | + BinaryOpTestLauncher<ValueT, ValueT, unsigned>(grid, threads) |
| 83 | + .template launch_test<vectorized_sum_abs_diff_kernel<ValueT>>(op1, op2, |
| 84 | + expected); |
| 85 | +} |
| 86 | + |
| 87 | +int main() { |
| 88 | + test_vectorized_binary<syclcompat::abs_diff, uint32_t>(0x00010002, 0x00040002, |
| 89 | + 0x00030000); |
| 90 | + test_vectorized_binary<syclcompat::add_sat, uint32_t>(0x00020002, 0xFFFDFFFF, |
| 91 | + 0xFFFF0001); |
| 92 | + test_vectorized_binary<syclcompat::rhadd, uint32_t>(0x00010008, 0x00020001, |
| 93 | + 0x00020005); |
| 94 | + test_vectorized_binary<syclcompat::hadd, uint32_t>(0x00010003, 0x00020005, |
| 95 | + 0x00010004); |
| 96 | + test_vectorized_binary<syclcompat::maximum, uint32_t>(0x0FFF0000, 0x00000FFF, |
| 97 | + 0x0FFF0FFF); |
| 98 | + test_vectorized_binary<syclcompat::minimum, uint32_t>(0x0FFF0000, 0x00000FFF, |
| 99 | + 0x00000000); |
| 100 | + test_vectorized_binary<syclcompat::sub_sat, uint32_t>(0xFFFB0005, 0x00030008, |
| 101 | + 0xFFF8FFFD); |
| 102 | + test_vectorized_unary<syclcompat::abs, uint32_t>(0xFFFBFFFD, 0x00050003); |
| 103 | + test_vectorized_sum_abs_diff<uint32_t>(0x00010002, 0x00040002, 0x00000003); |
| 104 | + |
| 105 | + return 0; |
| 106 | +} |
0 commit comments