|
| 1 | +// RUN: %clangxx -fsycl %s -o %t.out -lOpenCL |
| 2 | +// RUN: env SYCL_DEVICE_TYPE=HOST %t.out |
| 3 | +// RUNx: %CPU_RUN_PLACEHOLDER %t.out |
| 4 | +// RUNx: %GPU_RUN_PLACEHOLDER %t.out |
| 5 | +// RUNx: %ACC_RUN_PLACEHOLDER %t.out |
| 6 | +//==------------ vec_convert.cpp - SYCL vec class convert method test ------==// |
| 7 | +// |
| 8 | +// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. |
| 9 | +// See https://llvm.org/LICENSE.txt for license information. |
| 10 | +// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception |
| 11 | +// |
| 12 | +//===----------------------------------------------------------------------===// |
| 13 | + |
| 14 | +#include <CL/sycl.hpp> |
| 15 | + |
| 16 | +#include <cassert> |
| 17 | + |
| 18 | +// TODO uncomment run lines on non-host devices when the rounding modes will |
| 19 | +// be implemented. |
| 20 | + |
| 21 | +using namespace cl::sycl; |
| 22 | + |
| 23 | +template <typename T, typename convertT, int roundingMode> class kernel_name; |
| 24 | + |
| 25 | +template <int N> struct helper; |
| 26 | + |
| 27 | +template <> struct helper<0> { |
| 28 | + template <typename T, int NumElements> |
| 29 | + static void compare(const vec<T, NumElements> &x, |
| 30 | + const vec<T, NumElements> &y) { |
| 31 | + const T xs = x.template swizzle<0>(); |
| 32 | + const T ys = y.template swizzle<0>(); |
| 33 | + assert(xs == ys); |
| 34 | + } |
| 35 | +}; |
| 36 | + |
| 37 | +template <int N> struct helper { |
| 38 | + template <typename T, int NumElements> |
| 39 | + static void compare(const vec<T, NumElements> &x, |
| 40 | + const vec<T, NumElements> &y) { |
| 41 | + const T xs = x.template swizzle<N>(); |
| 42 | + const T ys = y.template swizzle<N>(); |
| 43 | + helper<N - 1>::compare(x, y); |
| 44 | + assert(xs == ys); |
| 45 | + } |
| 46 | +}; |
| 47 | + |
| 48 | +template <typename T, typename convertT, int NumElements, |
| 49 | + rounding_mode roundingMode> |
| 50 | +void test(const vec<T, NumElements> &ToConvert, |
| 51 | + const vec<convertT, NumElements> &Expected) { |
| 52 | + vec<convertT, NumElements> Converted{0}; |
| 53 | + { |
| 54 | + buffer<vec<convertT, NumElements>, 1> Buffer{&Converted, range<1>{1}}; |
| 55 | + queue Queue; |
| 56 | + Queue.submit([&](handler &CGH) { |
| 57 | + accessor<vec<convertT, NumElements>, 1, access::mode::write> Accessor( |
| 58 | + Buffer, CGH); |
| 59 | + CGH.single_task<class kernel_name<T, convertT, static_cast<int>(roundingMode)>>([=]() { |
| 60 | + Accessor[0] = ToConvert.template convert<convertT, roundingMode>(); |
| 61 | + }); |
| 62 | + }); |
| 63 | + } |
| 64 | + helper<NumElements - 1>::compare(Converted, Expected); |
| 65 | +} |
| 66 | + |
| 67 | +int main() { |
| 68 | + // automatic |
| 69 | + test<int, int, 8, rounding_mode::automatic>( |
| 70 | + int8{2, 3, 3, -2, -3, -3, 0, 0}, |
| 71 | + int8{2, 3, 3, -2, -3, -3, 0, 0}); |
| 72 | + test<float, int, 8, rounding_mode::automatic>( |
| 73 | + float8{+2.3f, +2.5f, +2.7f, -2.3f, -2.5f, -2.7f, 0.f, 0.f}, |
| 74 | + int8{2, 3, 3, -2, -3, -3, 0, 0}); |
| 75 | + test<int, float, 8, rounding_mode::automatic>( |
| 76 | + int8{2, 3, 3, -2, -3, -3, 0, 0}, |
| 77 | + float8{2.f, 3.f, 3.f, -2.f, -3.f, -3.f, 0.f, 0.f}); |
| 78 | + test<float, float, 8, rounding_mode::automatic>( |
| 79 | + float8{+2.3f, +2.5f, +2.7f, -2.3f, -2.5f, -2.7f, 0.f, 0.f}, |
| 80 | + float8{+2.3f, +2.5f, +2.7f, -2.3f, -2.5f, -2.7f, 0.f, 0.f}); |
| 81 | + |
| 82 | + // rte |
| 83 | + test<int, int, 8, rounding_mode::rte>( |
| 84 | + int8{2, 3, 3, -2, -3, -3, 0, 0}, |
| 85 | + int8{2, 3, 3, -2, -3, -3, 0, 0}); |
| 86 | + test<float, int, 8, rounding_mode::rte>( |
| 87 | + float8{+2.3f, +2.5f, +2.7f, -2.3f, -2.5f, -2.7f, 0.f, 0.f}, |
| 88 | + int8{2, 3, 3, -2, -3, -3, 0, 0}); |
| 89 | + test<int, float, 8, rounding_mode::rte>( |
| 90 | + int8{2, 3, 3, -2, -3, -3, 0, 0}, |
| 91 | + float8{2.f, 3.f, 3.f, -2.f, -3.f, -3.f, 0.f, 0.f}); |
| 92 | + test<float, float, 8, rounding_mode::rte>( |
| 93 | + float8{+2.3f, +2.5f, +2.7f, -2.3f, -2.5f, -2.7f, 0.f, 0.f}, |
| 94 | + float8{+2.3f, +2.5f, +2.7f, -2.3f, -2.5f, -2.7f, 0.f, 0.f}); |
| 95 | + |
| 96 | + // rtz |
| 97 | + test<int, int, 8, rounding_mode::rtz>( |
| 98 | + int8{2, 3, 3, -2, -3, -3, 0, 0}, |
| 99 | + int8{2, 3, 3, -2, -3, -3, 0, 0}); |
| 100 | + test<float, int, 8, rounding_mode::rtz>( |
| 101 | + float8{+2.3f, +2.5f, +2.7f, -2.3f, -2.5f, -2.7f, 0.f, 0.f}, |
| 102 | + int8{2, 2, 2, -2, -2, -2, 0, 0}); |
| 103 | + test<int, float, 8, rounding_mode::rtz>( |
| 104 | + int8{2, 3, 3, -2, -3, -3, 0, 0}, |
| 105 | + float8{2.f, 3.f, 3.f, -2.f, -3.f, -3.f, 0.f, 0.f}); |
| 106 | + test<float, float, 8, rounding_mode::rtz>( |
| 107 | + float8{+2.3f, +2.5f, +2.7f, -2.3f, -2.5f, -2.7f, 0.f, 0.f}, |
| 108 | + float8{+2.3f, +2.5f, +2.7f, -2.3f, -2.5f, -2.7f, 0.f, 0.f}); |
| 109 | + |
| 110 | + // rtp |
| 111 | + test<int, int, 8, rounding_mode::rtp>( |
| 112 | + int8{2, 3, 3, -2, -3, -3, 0, 0}, |
| 113 | + int8{2, 3, 3, -2, -3, -3, 0, 0}); |
| 114 | + test<float, int, 8, rounding_mode::rtp>( |
| 115 | + float8{+2.3f, +2.5f, +2.7f, -2.3f, -2.5f, -2.7f, 0.f, 0.f}, |
| 116 | + int8{3, 3, 3, -2, -2, -2, 0, 0}); |
| 117 | + test<int, float, 8, rounding_mode::rtp>( |
| 118 | + int8{2, 3, 3, -2, -3, -3, 0, 0}, |
| 119 | + float8{2.f, 3.f, 3.f, -2.f, -3.f, -3.f, 0.f, 0.f}); |
| 120 | + test<float, float, 8, rounding_mode::rtp>( |
| 121 | + float8{+2.3f, +2.5f, +2.7f, -2.3f, -2.5f, -2.7f, 0.f, 0.f}, |
| 122 | + float8{+2.3f, +2.5f, +2.7f, -2.3f, -2.5f, -2.7f, 0.f, 0.f}); |
| 123 | + |
| 124 | + // rtn |
| 125 | + test<int, int, 8, rounding_mode::rtn>( |
| 126 | + int8{2, 3, 3, -2, -3, -3, 0, 0}, |
| 127 | + int8{2, 3, 3, -2, -3, -3, 0, 0}); |
| 128 | + test<float, int, 8, rounding_mode::rtn>( |
| 129 | + float8{+2.3f, +2.5f, +2.7f, -2.3f, -2.5f, -2.7f, 0.f, 0.f}, |
| 130 | + int8{2, 2, 2, -3, -3, -3, 0, 0}); |
| 131 | + test<int, float, 8, rounding_mode::rtn>( |
| 132 | + int8{2, 3, 3, -2, -3, -3, 0, 0}, |
| 133 | + float8{2.f, 3.f, 3.f, -2.f, -3.f, -3.f, 0.f, 0.f}); |
| 134 | + test<float, float, 8, rounding_mode::rtn>( |
| 135 | + float8{+2.3f, +2.5f, +2.7f, -2.3f, -2.5f, -2.7f, 0.f, 0.f}, |
| 136 | + float8{+2.3f, +2.5f, +2.7f, -2.3f, -2.5f, -2.7f, 0.f, 0.f}); |
| 137 | + |
| 138 | + return 0; |
| 139 | +} |
0 commit comments