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

[SYCL] Test printf with FP64 types only when available on target HW #715

Merged
merged 2 commits into from
Jan 11, 2022
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
85 changes: 85 additions & 0 deletions SYCL/Printf/double.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,85 @@
// This test is written with an aim to check that experimental::printf behaves
// in the same way as printf from C99/C11
//
// The test is written using conversion specifiers table from cppreference [1]
// [1]: https://en.cppreference.com/w/cpp/io/c/fprintf
//
// UNSUPPORTED: hip_amd
//
// RUN: %clangxx -fsycl -fsycl-targets=%sycl_triple %s -o %t.out
// RUN: %CPU_RUN_PLACEHOLDER %t.out %CPU_CHECK_PLACEHOLDER
// RUN: %GPU_RUN_PLACEHOLDER %t.out %GPU_CHECK_PLACEHOLDER
// RUN: %ACC_RUN_PLACEHOLDER %t.out %ACC_CHECK_PLACEHOLDER
// FIXME: Remove dedicated constant address space testing once generic AS
// support is considered stable.
// RUN: %clangxx -fsycl -fsycl-targets=%sycl_triple %s -o %t.constant.out \
// RUN: -DTEST_CONSTANT_AS
// RUN: %CPU_RUN_PLACEHOLDER %t.constant.out %CPU_CHECK_PLACEHOLDER
// RUN: %GPU_RUN_PLACEHOLDER %t.constant.out %GPU_CHECK_PLACEHOLDER
// RUN: %ACC_RUN_PLACEHOLDER %t.constant.out %ACC_CHECK_PLACEHOLDER
//
// CHECK: double -6.813800e+00, -6.813800E+00
// CHECK: mixed 3.140000e+00, -6.813800E+00
// CHECK: double -0x1.b4154d8cccccdp+2, -0X1.B4154D8CCCCCDP+2
// CHECK: mixed 0x1.91eb86{{0*}}p+1, -0X1.B4154D8CCCCCDP+2
// CHECK: double -6.8138, -6.8138
// CHECK: mixed 3.14, -6.8138

#include <iostream>

#include <CL/sycl.hpp>

#include "helper.hpp"

using namespace sycl;

void do_double_test() {
float f = 3.14;
double d = -f * 2.17;

{
// %e, %E floating-point, decimal exponent notation
FORMAT_STRING(fmt_double) = "double %e, %E\n";
FORMAT_STRING(fmt_mixed) = "mixed %e, %E\n";
ext::oneapi::experimental::printf(fmt_double, d, d);
ext::oneapi::experimental::printf(fmt_mixed, f, d);
}

{
// %a, %A floating-point, hexadecimal exponent notation
FORMAT_STRING(fmt_double) = "double %a, %A\n";
FORMAT_STRING(fmt_mixed) = "mixed %a, %A\n";
ext::oneapi::experimental::printf(fmt_double, d, d);
ext::oneapi::experimental::printf(fmt_mixed, f, d);
}

{
// %g, %G floating-point
FORMAT_STRING(fmt_double) = "double %g, %G\n";
FORMAT_STRING(fmt_mixed) = "mixed %g, %G\n";
ext::oneapi::experimental::printf(fmt_double, d, d);
ext::oneapi::experimental::printf(fmt_mixed, f, d);
}
}

class DoubleTest;

int main() {
queue q;

if (q.get_device().has(aspect::fp64)) {
q.submit([](handler &cgh) {
cgh.single_task<DoubleTest>([]() { do_double_test(); });
});
q.wait();
} else
std::cout << "Skipping the actual test. "
"Printing hard-coded output from the host side:\n"
<< "double -6.813800e+00, -6.813800E+00\n"
"mixed 3.140000e+00, -6.813800E+00\n"
"double -0x1.b4154d8cccccdp+2, -0X1.B4154D8CCCCCDP+2\n"
"mixed 0x1.91eb86p+1, -0X1.B4154D8CCCCCDP+2\n"
"double -6.8138, -6.8138\n"
"mixed 3.14, -6.8138\n";
return 0;
}
79 changes: 31 additions & 48 deletions SYCL/Printf/float.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,13 @@
// RUN: %CPU_RUN_PLACEHOLDER %t.out %CPU_CHECK_PLACEHOLDER
// RUN: %GPU_RUN_PLACEHOLDER %t.out %GPU_CHECK_PLACEHOLDER
// RUN: %ACC_RUN_PLACEHOLDER %t.out %ACC_CHECK_PLACEHOLDER
// FIXME: Remove dedicated non-variadic printf testing once the headers
// enforce it by default.
// RUN: %clangxx -fsycl -fsycl-targets=%sycl_triple %s -o %t.nonvar.out \
// RUN: -D__SYCL_USE_NON_VARIADIC_SPIRV_OCL_PRINTF__
// RUN: %CPU_RUN_PLACEHOLDER %t.nonvar.out %CPU_CHECK_PLACEHOLDER
// RUN: %GPU_RUN_PLACEHOLDER %t.nonvar.out %GPU_CHECK_PLACEHOLDER
// RUN: %ACC_RUN_PLACEHOLDER %t.nonvar.out %ACC_CHECK_PLACEHOLDER
// FIXME: Remove dedicated constant address space testing once generic AS
// support is considered stable.
// RUN: %clangxx -fsycl -fsycl-targets=%sycl_triple %s -o %t.constant.out \
Expand All @@ -18,15 +25,11 @@
// RUN: %GPU_RUN_PLACEHOLDER %t.constant.out %GPU_CHECK_PLACEHOLDER
// RUN: %ACC_RUN_PLACEHOLDER %t.constant.out %ACC_CHECK_PLACEHOLDER
//
// CHECK: float 3.140000e+00, 3.140000E+00
// CHECK: double -6.813800e+00, -6.813800E+00
// CHECK: mixed 3.140000e+00, -6.813800E+00
// CHECK: float 0x1.91eb86{{0*}}p+1, 0X1.91EB86{{0*}}P+1
// CHECK: double -0x1.b4154d8cccccdp+2, -0X1.B4154D8CCCCCDP+2
// CHECK: mixed 0x1.91eb86{{0*}}p+1, -0X1.B4154D8CCCCCDP+2
// CHECK: float 3.14, 3.14
// CHECK: double -6.8138, -6.8138
// CHECK: mixed 3.14, -6.8138
// CHECK: 3.140000e+00, 3.140000E+00
// CHECK: 0x1.91eb86{{0*}}p+1, 0X1.91EB86{{0*}}P+1
// CHECK: 3.14, 3.14

#include <iostream>

#include <CL/sycl.hpp>

Expand All @@ -35,55 +38,35 @@
using namespace sycl;

void do_float_test() {
{
// %e, %E floating-point, decimal exponent notation
FORMAT_STRING(fmt1) = "float %e, %E\n";
FORMAT_STRING(fmt2) = "double %e, %E\n";
FORMAT_STRING(fmt3) = "mixed %e, %E\n";

float f = 3.14;
double d = -f * 2.17;
ext::oneapi::experimental::printf(fmt1, f, f);
ext::oneapi::experimental::printf(fmt2, d, d);
ext::oneapi::experimental::printf(fmt3, f, d);
}

{
// %a, %A floating-point, hexadecimal exponent notation
FORMAT_STRING(fmt1) = "float %a, %A\n";
FORMAT_STRING(fmt2) = "double %a, %A\n";
FORMAT_STRING(fmt3) = "mixed %a, %A\n";

float f = 3.14;
double d = -f * 2.17;
ext::oneapi::experimental::printf(fmt1, f, f);
ext::oneapi::experimental::printf(fmt2, d, d);
ext::oneapi::experimental::printf(fmt3, f, d);
}

{
// %g, %G floating-point
FORMAT_STRING(fmt1) = "float %g, %G\n";
FORMAT_STRING(fmt2) = "double %g, %G\n";
FORMAT_STRING(fmt3) = "mixed %g, %G\n";

float f = 3.14;
double d = -f * 2.17;
ext::oneapi::experimental::printf(fmt1, f, f);
ext::oneapi::experimental::printf(fmt2, d, d);
ext::oneapi::experimental::printf(fmt3, f, d);
}
float f = 3.14;
// %e, %E floating-point, decimal exponent notation
FORMAT_STRING(fmt1) = "float %e, %E\n";
ext::oneapi::experimental::printf(fmt1, f, f);
// %a, %A floating-point, hexadecimal exponent notation
FORMAT_STRING(fmt2) = "float %a, %A\n";
ext::oneapi::experimental::printf(fmt2, f, f);
// %g, %G floating-point
FORMAT_STRING(fmt3) = "float %g, %G\n";
ext::oneapi::experimental::printf(fmt3, f, f);
}

class FloatTest;

int main() {
queue q;

#ifndef __SYCL_USE_NON_VARIADIC_SPIRV_OCL_PRINTF__
if (!q.get_device().has(aspect::fp64))
std::cout << "Skipping the actual test due to variadic argument promotion. "
"Printing hard-coded output from the host side:\n"
<< "3.140000e+00, 3.140000E+00\n"
"0x1.91eb86p+1, 0X1.91EB86P+1\n"
"3.14, 3.14\n";
return 0;
#endif // !__SYCL_USE_NON_VARIADIC_SPIRV_OCL_PRINTF__
q.submit([](handler &cgh) {
cgh.single_task<FloatTest>([]() { do_float_test(); });
});
q.wait();

return 0;
}