|
| 1 | +//==------------ bfloat16_host.cpp - SYCL vectors test ---------------------==// |
| 2 | +// |
| 3 | +// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. |
| 4 | +// See https://llvm.org/LICENSE.txt for license information. |
| 5 | +// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception |
| 6 | +// |
| 7 | +//===----------------------------------------------------------------------===// |
| 8 | + |
| 9 | +// RUN: %clangxx -fsycl %s -o %t.out |
| 10 | +// RUN: %RUN_ON_HOST %t.out |
| 11 | +#include <sycl/ext/oneapi/experimental/bfloat16.hpp> |
| 12 | +#include <sycl/sycl.hpp> |
| 13 | + |
| 14 | +#include <cmath> |
| 15 | +#include <cstdint> |
| 16 | +#include <iostream> |
| 17 | +#include <limits> |
| 18 | +#include <string> |
| 19 | + |
| 20 | +// Helper to convert the expected bits to float value to compare with the result |
| 21 | +typedef union { |
| 22 | + float Value; |
| 23 | + struct { |
| 24 | + uint32_t Mantissa : 23; |
| 25 | + uint32_t Exponent : 8; |
| 26 | + uint32_t Sign : 1; |
| 27 | + } RawData; |
| 28 | +} floatConvHelper; |
| 29 | + |
| 30 | +float bitsToFloatConv(std::string Bits) { |
| 31 | + floatConvHelper Helper; |
| 32 | + Helper.RawData.Sign = static_cast<uint32_t>(Bits[0] - '0'); |
| 33 | + uint32_t Exponent = 0; |
| 34 | + for (size_t I = 1; I != 9; ++I) |
| 35 | + Exponent = Exponent + static_cast<uint32_t>(Bits[I] - '0') * pow(2, 8 - I); |
| 36 | + Helper.RawData.Exponent = Exponent; |
| 37 | + uint32_t Mantissa = 0; |
| 38 | + for (size_t I = 9; I != 32; ++I) |
| 39 | + Mantissa = Mantissa + static_cast<uint32_t>(Bits[I] - '0') * pow(2, 31 - I); |
| 40 | + Helper.RawData.Mantissa = Mantissa; |
| 41 | + return Helper.Value; |
| 42 | +} |
| 43 | + |
| 44 | +bool check_bf16_from_float(float Val, uint16_t Expected) { |
| 45 | + uint16_t Result = sycl::ext::oneapi::experimental::bfloat16::from_float(Val); |
| 46 | + if (Result != Expected) { |
| 47 | + std::cout << "from_float check for Val = " << Val << " failed!\n" |
| 48 | + << "Expected " << Expected << " Got " << Result << "\n"; |
| 49 | + return false; |
| 50 | + } |
| 51 | + return true; |
| 52 | +} |
| 53 | + |
| 54 | +bool check_bf16_to_float(uint16_t Val, float Expected) { |
| 55 | + float Result = sycl::ext::oneapi::experimental::bfloat16::to_float(Val); |
| 56 | + if (Result != Expected) { |
| 57 | + std::cout << "to_float check for Val = " << Val << " failed!\n" |
| 58 | + << "Expected " << Expected << " Got " << Result << "\n"; |
| 59 | + return false; |
| 60 | + } |
| 61 | + return true; |
| 62 | +} |
| 63 | + |
| 64 | +int main() { |
| 65 | + bool Success = |
| 66 | + check_bf16_from_float(0.0f, std::stoi("0000000000000000", nullptr, 2)); |
| 67 | + Success &= |
| 68 | + check_bf16_from_float(42.0f, std::stoi("100001000101000", nullptr, 2)); |
| 69 | + Success &= check_bf16_from_float(std::numeric_limits<float>::min(), |
| 70 | + std::stoi("0000000010000000", nullptr, 2)); |
| 71 | + Success &= check_bf16_from_float(std::numeric_limits<float>::max(), |
| 72 | + std::stoi("0111111110000000", nullptr, 2)); |
| 73 | + Success &= check_bf16_from_float(std::numeric_limits<float>::quiet_NaN(), |
| 74 | + std::stoi("1111111111000001", nullptr, 2)); |
| 75 | + |
| 76 | + Success &= check_bf16_to_float( |
| 77 | + 0, bitsToFloatConv(std::string("00000000000000000000000000000000"))); |
| 78 | + Success &= check_bf16_to_float( |
| 79 | + 1, bitsToFloatConv(std::string("01000111100000000000000000000000"))); |
| 80 | + Success &= check_bf16_to_float( |
| 81 | + 42, bitsToFloatConv(std::string("01001010001010000000000000000000"))); |
| 82 | + Success &= check_bf16_to_float( |
| 83 | + std::numeric_limits<uint16_t>::max(), |
| 84 | + bitsToFloatConv(std::string("01001111011111111111111100000000"))); |
| 85 | + if (!Success) |
| 86 | + return -1; |
| 87 | + return 0; |
| 88 | +} |
0 commit comments