Skip to content
This repository was archived by the owner on Mar 28, 2023. It is now read-only.

Commit 6646adc

Browse files
authored
[ESIMD] Add a test to validate new implementation of esimd::fmod (#1045)
* Add a test to validate new implementation of esimd::fmod
1 parent 6b9e418 commit 6646adc

File tree

1 file changed

+98
-0
lines changed

1 file changed

+98
-0
lines changed
Lines changed: 98 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,98 @@
1+
// REQUIRES: gpu
2+
// UNSUPPORTED: cuda || hip
3+
// RUN: %clangxx -fsycl %s -o %t.out
4+
// RUN: %GPU_RUN_PLACEHOLDER %t.out
5+
6+
//==- fmod_compatibility_test.cpp - Test for compatibility with std::fmod -==//
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+
#include <cmath>
16+
#include <iostream>
17+
#include <sycl/ext/intel/esimd.hpp>
18+
#include <sycl/ext/intel/esimd/simd.hpp>
19+
#include <vector>
20+
21+
constexpr auto sycl_write = cl::sycl::access::mode::write;
22+
#define SIMD 16
23+
24+
int test_fmod(float x, float y) {
25+
std::vector<float> out(SIMD);
26+
27+
float ha = x;
28+
float hb = y;
29+
float scalar_result = 0;
30+
31+
{
32+
sycl::queue queue;
33+
34+
sycl::buffer<float, 1> vector_buffer(out.data(), out.size());
35+
sycl::buffer<float, 1> scalar_buffer(&scalar_result, sycl::range<1>(1));
36+
37+
sycl::nd_range<1> nd{sycl::range<1>(1), sycl::range<1>(1)};
38+
39+
auto e = queue.submit([&](sycl::handler &cgh) {
40+
sycl::accessor<float, 1, sycl_write> vector_out =
41+
vector_buffer.get_access<sycl_write>(cgh);
42+
sycl::accessor<float, 1, sycl_write> scalar_out =
43+
scalar_buffer.get_access<sycl_write>(cgh);
44+
45+
auto kernel = ([=](sycl::item<1> item) [[intel::sycl_explicit_simd]] {
46+
using namespace sycl::ext::intel::esimd;
47+
48+
simd<float, SIMD> a = ha;
49+
simd<float, SIMD> b = hb;
50+
51+
simd<float, SIMD> vector_result =
52+
sycl::ext::intel::experimental::esimd::fmod(a, b);
53+
simd<float, SIMD> scalar_result =
54+
sycl::ext::intel::experimental::esimd::fmod(ha, hb);
55+
56+
vector_result.copy_to(vector_out, 0);
57+
scalar_result.copy_to(scalar_out, 0);
58+
});
59+
60+
cgh.parallel_for<class Reduction>(nd, kernel);
61+
});
62+
queue.wait();
63+
}
64+
65+
float mod = std::fmod(ha, hb);
66+
67+
if (mod != out[0] || std::signbit(mod) != std::signbit(out[0])) {
68+
std::cout << "Vector test failed for " << x << " and " << y << "."
69+
<< std::endl;
70+
return 1;
71+
}
72+
73+
if (mod != scalar_result ||
74+
std::signbit(mod) != std::signbit(scalar_result)) {
75+
std::cout << "Scalar test failed for " << x << " and " << y << "."
76+
<< std::endl;
77+
return 1;
78+
}
79+
80+
return 0;
81+
}
82+
83+
int main() {
84+
85+
int test_result = 0;
86+
test_result |= test_fmod(49152 * 1364.0f + 626.0f, 509);
87+
test_result |= test_fmod(+5.1f, +3.0f);
88+
test_result |= test_fmod(-5.1f, +3.0f);
89+
test_result |= test_fmod(+5.1f, -3.0f);
90+
test_result |= test_fmod(-5.1f, -3.0f);
91+
test_result |= test_fmod(+0.0f, 1.0f);
92+
test_result |= test_fmod(-0.0f, 1.0f);
93+
94+
if (!test_result) {
95+
std::cout << "Pass" << std::endl;
96+
}
97+
return test_result;
98+
}

0 commit comments

Comments
 (0)