Skip to content

Commit bc609de

Browse files
committed
Add unit tests
1 parent 029cee5 commit bc609de

File tree

2 files changed

+99
-0
lines changed

2 files changed

+99
-0
lines changed
Lines changed: 81 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,81 @@
1+
#include <sycl/ext/oneapi/bfloat16.hpp>
2+
#include <sycl/sycl.hpp>
3+
4+
#include <gtest/gtest.h>
5+
6+
#include <cmath>
7+
#include <limits>
8+
#include <string>
9+
10+
namespace {
11+
// Helper to convert the expected bits to float value to compare with the result
12+
typedef union {
13+
float Value;
14+
struct {
15+
uint32_t Mantissa : 23;
16+
uint32_t Exponent : 8;
17+
uint32_t Sign : 1;
18+
} RawData;
19+
} floatConvHelper;
20+
21+
float bitsToFloatConv(std::string Bits) {
22+
floatConvHelper Helper;
23+
Helper.RawData.Sign = static_cast<uint32_t>(Bits[0] - '0');
24+
uint32_t Exponent = 0;
25+
for (size_t I = 1; I != 9; ++I)
26+
Exponent = Exponent + static_cast<uint32_t>(Bits[I] - '0') * std::pow(2, 8 - I);
27+
Helper.RawData.Exponent = Exponent;
28+
uint32_t Mantissa = 0;
29+
for (size_t I = 9; I != 32; ++I)
30+
Mantissa = Mantissa + static_cast<uint32_t>(Bits[I] - '0') * std::pow(2, 31 - I);
31+
Helper.RawData.Mantissa = Mantissa;
32+
return Helper.Value;
33+
}
34+
} // namespace
35+
36+
TEST(BFloat16, BF16FromFloat) {
37+
sycl::ext::oneapi::bfloat16 B = 0.0f;
38+
auto Result = sycl::bit_cast<uint16_t>(B);
39+
ASSERT_EQ(Result, std::stoi("0000000000000000", nullptr, 2));
40+
41+
B = 42.0f;
42+
Result = sycl::bit_cast<uint16_t>(B);
43+
ASSERT_EQ(Result, std::stoi("100001000101000", nullptr, 2));
44+
45+
B = std::numeric_limits<float>::min();
46+
Result = sycl::bit_cast<uint16_t>(B);
47+
ASSERT_EQ(Result, std::stoi("0000000010000000", nullptr, 2));
48+
49+
B = std::numeric_limits<float>::max();
50+
Result = sycl::bit_cast<uint16_t>(B);
51+
ASSERT_EQ(Result, std::stoi("0111111110000000", nullptr, 2));
52+
53+
B = std::numeric_limits<float>::quiet_NaN();
54+
Result = sycl::bit_cast<uint16_t>(B);
55+
ASSERT_EQ(Result, std::stoi("1111111111000001", nullptr, 2));
56+
}
57+
58+
TEST(BFloat16, BF16ToFloat) {
59+
// See https://float.exposed/b0xffff
60+
uint16_t V = 0;
61+
float Res = sycl::bit_cast<sycl::ext::oneapi::bfloat16>(V);
62+
ASSERT_EQ(Res,
63+
bitsToFloatConv(std::string("00000000000000000000000000000000")));
64+
65+
V = 1;
66+
Res = sycl::bit_cast<sycl::ext::oneapi::bfloat16>(V);
67+
ASSERT_EQ(Res,
68+
bitsToFloatConv(std::string("00000000000000010000000000000000")));
69+
70+
V = 42;
71+
Res = sycl::bit_cast<sycl::ext::oneapi::bfloat16>(V);
72+
ASSERT_EQ(Res,
73+
bitsToFloatConv(std::string("00000000001010100000000000000000")));
74+
75+
// std::numeric_limits<uint16_t>::max() - 0xffff is bfloat16 -Nan and
76+
// -Nan == -Nan check in check_bf16_to_float would fail, so use not Nan:
77+
V = 65407;
78+
Res = sycl::bit_cast<sycl::ext::oneapi::bfloat16>(V);
79+
ASSERT_EQ(Res,
80+
bitsToFloatConv(std::string("11111111011111110000000000000000")));
81+
}
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
#include <sycl/sycl.hpp>
2+
3+
#include <gtest/gtest.h>
4+
5+
TEST(HostAccessor, ZeroDimAccessor) {
6+
using DataT = int;
7+
using AccT = sycl::host_accessor<DataT, 0>;
8+
9+
DataT data = 42;
10+
sycl::buffer<DataT, 1> data_buf(&data, sycl::range<1>(1));
11+
AccT acc = {data_buf};
12+
13+
ASSERT_EQ(acc.get_size(), sizeof(DataT));
14+
ASSERT_EQ(acc.size(), 1);
15+
16+
DataT &ref = acc;
17+
ASSERT_EQ(ref, data);
18+
}

0 commit comments

Comments
 (0)