Skip to content

[SYCL] Check for nan/inf in stream class #522

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Aug 20, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
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
6 changes: 3 additions & 3 deletions sycl/include/CL/sycl/detail/builtins.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@

#define MAKE_CALL_ARG1(call, prefix) \
template <typename R, typename T1> \
ALWAYS_INLINE R __invoke_##call(T1 t1) __NOEXC { \
inline ALWAYS_INLINE R __invoke_##call(T1 t1) __NOEXC { \
using Ret = cl::sycl::detail::ConvertToOpenCLType_t<R>; \
using Arg1 = cl::sycl::detail::ConvertToOpenCLType_t<T1>; \
extern Ret PPCAT(prefix, call)(Arg1); \
Expand All @@ -40,7 +40,7 @@

#define MAKE_CALL_ARG2(call, prefix) \
template <typename R, typename T1, typename T2> \
ALWAYS_INLINE R __invoke_##call(T1 t1, T2 t2) __NOEXC { \
inline ALWAYS_INLINE R __invoke_##call(T1 t1, T2 t2) __NOEXC { \
using Ret = cl::sycl::detail::ConvertToOpenCLType_t<R>; \
using Arg1 = cl::sycl::detail::ConvertToOpenCLType_t<T1>; \
using Arg2 = cl::sycl::detail::ConvertToOpenCLType_t<T2>; \
Expand All @@ -53,7 +53,7 @@

#define MAKE_CALL_ARG3(call, prefix) \
template <typename R, typename T1, typename T2, typename T3> \
ALWAYS_INLINE R __invoke_##call(T1 t1, T2 t2, T3 t3) __NOEXC { \
inline ALWAYS_INLINE R __invoke_##call(T1 t1, T2 t2, T3 t3) __NOEXC { \
using Ret = cl::sycl::detail::ConvertToOpenCLType_t<R>; \
using Arg1 = cl::sycl::detail::ConvertToOpenCLType_t<T1>; \
using Arg2 = cl::sycl::detail::ConvertToOpenCLType_t<T2>; \
Expand Down
40 changes: 39 additions & 1 deletion sycl/include/CL/sycl/detail/stream_impl.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
#pragma once

#include <CL/sycl/accessor.hpp>
#include <CL/sycl/builtins.hpp>
#include <CL/sycl/detail/array.hpp>
#include <CL/sycl/device_selector.hpp>
#include <CL/sycl/queue.hpp>
Expand Down Expand Up @@ -322,15 +323,52 @@ inline unsigned append(char *Dst, const char *Src) {
return Len;
}

template <typename T>
inline typename std::enable_if<std::is_same<T, half>::value, unsigned>::type
checkForInfNan(char *Buf, T Val) {
if (Val != Val)
return append(Buf, "nan");

// Extract the sign from the bits
const uint16_t Sign = reinterpret_cast<uint16_t &>(Val) & 0x8000;
// Extract the exponent from the bits
const uint16_t Exp16 = (reinterpret_cast<uint16_t &>(Val) & 0x7c00) >> 10;

if (Exp16 == 0x1f) {
if (Sign)
return append(Buf, "-inf");
return append(Buf, "inf");
}
return 0;
}

template <typename T>
inline typename std::enable_if<std::is_same<T, float>::value ||
std::is_same<T, double>::value,
unsigned>::type
checkForInfNan(char *Buf, T Val) {
if (isnan(Val))
return append(Buf, "nan");
if (isinf(Val)) {
if (signbit(Val))
return append(Buf, "-inf");
return append(Buf, "inf");
}
return 0;
}

// Returns number of symbols written to the buffer
template <typename T>
inline EnableIfFP<T, unsigned> ScalarToStr(const T &Val, char *Buf,
unsigned Flags, int Width,
int Precision = -1) {
unsigned Offset = checkForInfNan(Buf, Val);
if (Offset)
return Offset;

T Neg = -Val;
auto AbsVal = Val < 0 ? Neg : Val;

unsigned Offset = 0;

if (Val < 0) {
Buf[Offset++] = '-';
Expand Down
12 changes: 12 additions & 0 deletions sycl/test/basic_tests/stream.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -101,11 +101,23 @@ int main() {
Out << -33.4f << sm::endl;
Out << -5.2 << sm::endl;
Out << 0.0003 << sm::endl;
Out << -1.0 / 0.0 << sm::endl;
Out << 1.0 / 0.0 << sm::endl;
Out << cl::sycl::sqrt(-1.0) << sm::endl;
Out << -1.0f / 0.0f << sm::endl;
Out << 1.0f / 0.0f << sm::endl;
Out << sqrt(-1.0f) << sm::endl;
// CHECK-NEXT: 33.4
// CHECK-NEXT: 5.2
// CHECK-NEXT: -33.4
// CHECK-NEXT: -5.2
// CHECK-NEXT: 0.0003
// CHECK-NEXT: -inf
// CHECK-NEXT: inf
// CHECK-NEXT: nan
// CHECK-NEXT: -inf
// CHECK-NEXT: inf
// CHECK-NEXT: nan

// Manipulators for integral types
Out << sm::dec << 0213 << sm::endl;
Expand Down