diff --git a/sycl/test/basic_tests/accessor/zero-dim-host-accessor.cpp b/sycl/test/basic_tests/accessor/zero-dim-host-accessor.cpp deleted file mode 100644 index fd674f6baca8e..0000000000000 --- a/sycl/test/basic_tests/accessor/zero-dim-host-accessor.cpp +++ /dev/null @@ -1,17 +0,0 @@ -// RUN: %clangxx -fsycl -fsycl-targets=%sycl_triple %s -o %t.out -// RUN: %t.out - -#include - -int main() { - using DataT = int; - using AccT = sycl::host_accessor; - int data(5); - sycl::range<1> r(1); - sycl::buffer data_buf(&data, r); - AccT acc{data_buf}; - assert(acc.get_size() == sizeof(DataT)); - assert(acc.size() == 1); - auto ref = &acc; - assert(*ref == 5); -} diff --git a/sycl/test/extensions/bfloat16_host.cpp b/sycl/test/extensions/bfloat16_host.cpp deleted file mode 100644 index c18e1b2e24957..0000000000000 --- a/sycl/test/extensions/bfloat16_host.cpp +++ /dev/null @@ -1,91 +0,0 @@ -//==------------ bfloat16_host.cpp - SYCL vectors test ---------------------==// -// -// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. -// See https://llvm.org/LICENSE.txt for license information. -// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception -// -//===----------------------------------------------------------------------===// - -// RUN: %clangxx -fsycl %s -o %t.out -// RUN: %t.out -#include -#include - -#include -#include -#include -#include -#include - -// Helper to convert the expected bits to float value to compare with the result -typedef union { - float Value; - struct { - uint32_t Mantissa : 23; - uint32_t Exponent : 8; - uint32_t Sign : 1; - } RawData; -} floatConvHelper; - -float bitsToFloatConv(std::string Bits) { - floatConvHelper Helper; - Helper.RawData.Sign = static_cast(Bits[0] - '0'); - uint32_t Exponent = 0; - for (size_t I = 1; I != 9; ++I) - Exponent = Exponent + static_cast(Bits[I] - '0') * pow(2, 8 - I); - Helper.RawData.Exponent = Exponent; - uint32_t Mantissa = 0; - for (size_t I = 9; I != 32; ++I) - Mantissa = Mantissa + static_cast(Bits[I] - '0') * pow(2, 31 - I); - Helper.RawData.Mantissa = Mantissa; - return Helper.Value; -} - -bool check_bf16_from_float(float Val, uint16_t Expected) { - sycl::ext::oneapi::bfloat16 B = Val; - uint16_t Result = *reinterpret_cast(&B); - if (Result != Expected) { - std::cout << "from_float check for Val = " << Val << " failed!\n" - << "Expected " << Expected << " Got " << Result << "\n"; - return false; - } - return true; -} - -bool check_bf16_to_float(uint16_t Val, float Expected) { - float Result = *reinterpret_cast(&Val); - if (Result != Expected) { - std::cout << "to_float check for Val = " << Val << " failed!\n" - << "Expected " << Expected << " Got " << Result << "\n"; - return false; - } - return true; -} - -int main() { - bool Success = - check_bf16_from_float(0.0f, std::stoi("0000000000000000", nullptr, 2)); - Success &= - check_bf16_from_float(42.0f, std::stoi("100001000101000", nullptr, 2)); - Success &= check_bf16_from_float(std::numeric_limits::min(), - std::stoi("0000000010000000", nullptr, 2)); - Success &= check_bf16_from_float(std::numeric_limits::max(), - std::stoi("0111111110000000", nullptr, 2)); - Success &= check_bf16_from_float(std::numeric_limits::quiet_NaN(), - std::stoi("1111111111000001", nullptr, 2)); - - // see https://float.exposed/b0xffff - Success &= check_bf16_to_float( - 0, bitsToFloatConv(std::string("00000000000000000000000000000000"))); - Success &= check_bf16_to_float( - 1, bitsToFloatConv(std::string("00000000000000010000000000000000"))); - Success &= check_bf16_to_float( - 42, bitsToFloatConv(std::string("00000000001010100000000000000000"))); - Success &= check_bf16_to_float( - // std::numeric_limits::max() - 0xffff is bfloat16 -Nan and - // -Nan == -Nan check in check_bf16_to_float would fail, so use not Nan: - 65407, bitsToFloatConv(std::string("11111111011111110000000000000000"))); - if (!Success) - return -1; - return 0; -} diff --git a/sycl/unittests/Extensions/BFloat16.cpp b/sycl/unittests/Extensions/BFloat16.cpp new file mode 100644 index 0000000000000..6a2bd166ecc3a --- /dev/null +++ b/sycl/unittests/Extensions/BFloat16.cpp @@ -0,0 +1,81 @@ +#include +#include + +#include + +#include +#include +#include + +namespace { +// Helper to convert the expected bits to float value to compare with the result. +typedef union { + float Value; + struct { + uint32_t Mantissa : 23; + uint32_t Exponent : 8; + uint32_t Sign : 1; + } RawData; +} floatConvHelper; + +float bitsToFloatConv(std::string Bits) { + floatConvHelper Helper; + Helper.RawData.Sign = static_cast(Bits[0] - '0'); + uint32_t Exponent = 0; + for (size_t I = 1; I != 9; ++I) + Exponent = Exponent + static_cast(Bits[I] - '0') * std::pow(2, 8 - I); + Helper.RawData.Exponent = Exponent; + uint32_t Mantissa = 0; + for (size_t I = 9; I != 32; ++I) + Mantissa = Mantissa + static_cast(Bits[I] - '0') * std::pow(2, 31 - I); + Helper.RawData.Mantissa = Mantissa; + return Helper.Value; +} +} // namespace + +TEST(BFloat16, BF16FromFloat) { + sycl::ext::oneapi::bfloat16 B = 0.0f; + auto Result = sycl::bit_cast(B); + ASSERT_EQ(Result, std::stoi("0000000000000000", nullptr, 2)); + + B = 42.0f; + Result = sycl::bit_cast(B); + ASSERT_EQ(Result, std::stoi("100001000101000", nullptr, 2)); + + B = std::numeric_limits::min(); + Result = sycl::bit_cast(B); + ASSERT_EQ(Result, std::stoi("0000000010000000", nullptr, 2)); + + B = std::numeric_limits::max(); + Result = sycl::bit_cast(B); + ASSERT_EQ(Result, std::stoi("0111111110000000", nullptr, 2)); + + B = std::numeric_limits::quiet_NaN(); + Result = sycl::bit_cast(B); + ASSERT_EQ(Result, std::stoi("1111111111000001", nullptr, 2)); +} + +TEST(BFloat16, BF16ToFloat) { + // See https://float.exposed/b0xffff + uint16_t V = 0; + float Res = sycl::bit_cast(V); + ASSERT_EQ(Res, + bitsToFloatConv(std::string("00000000000000000000000000000000"))); + + V = 1; + Res = sycl::bit_cast(V); + ASSERT_EQ(Res, + bitsToFloatConv(std::string("00000000000000010000000000000000"))); + + V = 42; + Res = sycl::bit_cast(V); + ASSERT_EQ(Res, + bitsToFloatConv(std::string("00000000001010100000000000000000"))); + + // std::numeric_limits::max() - 0xffff is bfloat16 -Nan and + // -Nan == -Nan check in check_bf16_to_float would fail, so use not Nan: + V = 65407; + Res = sycl::bit_cast(V); + ASSERT_EQ(Res, + bitsToFloatConv(std::string("11111111011111110000000000000000"))); +} diff --git a/sycl/unittests/Extensions/CMakeLists.txt b/sycl/unittests/Extensions/CMakeLists.txt index 44a59f9a5e136..bd57fb22d865c 100644 --- a/sycl/unittests/Extensions/CMakeLists.txt +++ b/sycl/unittests/Extensions/CMakeLists.txt @@ -16,6 +16,7 @@ add_sycl_unittest(ExtensionsTests OBJECT KernelProperties.cpp NoDeviceIPVersion.cpp GetLastEvent.cpp + BFloat16.cpp ) add_subdirectory(CommandGraph) diff --git a/sycl/unittests/accessor/CMakeLists.txt b/sycl/unittests/accessor/CMakeLists.txt index e41fc54a30f8e..312716877a429 100644 --- a/sycl/unittests/accessor/CMakeLists.txt +++ b/sycl/unittests/accessor/CMakeLists.txt @@ -8,4 +8,5 @@ add_sycl_unittest(AccessorTests OBJECT HostAccessorReverseIterator.cpp LocalAccessorDefaultCtor.cpp RuntimeProperties.cpp + HostAccessor.cpp ) diff --git a/sycl/unittests/accessor/HostAccessor.cpp b/sycl/unittests/accessor/HostAccessor.cpp new file mode 100644 index 0000000000000..912d0b356d05c --- /dev/null +++ b/sycl/unittests/accessor/HostAccessor.cpp @@ -0,0 +1,18 @@ +#include + +#include + +TEST(HostAccessor, ZeroDimAccessor) { + using DataT = int; + using AccT = sycl::host_accessor; + + DataT data = 42; + sycl::buffer data_buf(&data, sycl::range<1>(1)); + AccT acc = {data_buf}; + + ASSERT_EQ(acc.get_size(), sizeof(DataT)); + ASSERT_EQ(acc.size(), static_cast(1)); + + DataT &ref = acc; + ASSERT_EQ(ref, data); +}