From f72ceee298e1c5c81a8fb60537aa2e9be891ab14 Mon Sep 17 00:00:00 2001 From: Vladyslav Baranovskyi Date: Thu, 7 Nov 2024 16:45:17 +0200 Subject: [PATCH] Hardcoding numeric_limits values to work on platforms where our float16 class isn't defined --- openVulkanoCpp/Math/Float16.hpp | 16 ++++++++-------- tests/Math/Float16.cpp | 20 +++++++++++++++++++- 2 files changed, 27 insertions(+), 9 deletions(-) diff --git a/openVulkanoCpp/Math/Float16.hpp b/openVulkanoCpp/Math/Float16.hpp index 7cc2fae..160e6f4 100644 --- a/openVulkanoCpp/Math/Float16.hpp +++ b/openVulkanoCpp/Math/Float16.hpp @@ -250,8 +250,8 @@ namespace std public: // General -- meaningful for all specializations. static const bool is_specialized = true; - static float16 min() { return float16(0, 1, 0); } - static float16 max() { return float16(~0, 30, 0); } + static float16 min() { uint16_t v = 0x400; return *(float16 *)&v;/*return float16(0, 1, 0);*/ } + static float16 max() { uint16_t v = 0x7bff; return *(float16 *)&v;/*return float16(~0, 30, 0);*/ } static const int radix = 2; static const int digits = 10; // conservative assumption static const int digits10 = 2; // conservative assumption @@ -264,8 +264,8 @@ namespace std // Floating point specific. - static float16 epsilon() { return float16(0.00097656f); } // from OpenEXR, needs to be confirmed - static float16 round_error() { return float16(0.00097656f/2); } + static float16 epsilon() { uint16_t v = 0x13ff; return *(float16 *)&v;/*return float16(0.00097656f);*/ } // from OpenEXR, needs to be confirmed + static float16 round_error() { uint16_t v = 0xfff; return *(float16 *)&v;/*return float16(0.00097656f/2);*/ } static const int min_exponent10 = -9; static const int max_exponent10 = 9; static const int min_exponent = -15; @@ -279,10 +279,10 @@ namespace std static const bool tinyness_before = false; static const float_round_style round_style = round_to_nearest; - static float16 denorm_min() { return float16(1, 0, 1); } - static float16 infinity() { return float16(0, 31, 0); } - static float16 quiet_NaN() { return float16(1, 31, 0); } - static float16 signaling_NaN () { return float16(1, 31, 0); } + static float16 denorm_min() { uint16_t v = 0x8001; return *(float16 *)&v;/*return float16(1, 0, 1);*/ } + static float16 infinity() { uint16_t v = 0x7c00; return *(float16 *)&v;/*return float16(0, 31, 0);*/ } + static float16 quiet_NaN() { uint16_t v = 0x7c01; return *(float16 *)&v;/*return float16(1, 31, 0);*/ } + static float16 signaling_NaN () { uint16_t v = 0x7c01; return *(float16 *)&v;/*return float16(1, 31, 0);*/ } }; } diff --git a/tests/Math/Float16.cpp b/tests/Math/Float16.cpp index 22093a8..a4ebb34 100644 --- a/tests/Math/Float16.cpp +++ b/tests/Math/Float16.cpp @@ -32,9 +32,27 @@ TEST_CASE("Basic Conversions", "[float16]") TEST_CASE("Infinity and NaN", "[float16]") { - fp16 pos_inf = std::numeric_limits::infinity(); fp16 neg_inf = -std::numeric_limits::infinity(); + + fp16 min_val = std::numeric_limits::min(); + fp16 max_val = std::numeric_limits::max(); + fp16 epsilon_val = std::numeric_limits::epsilon(); + fp16 round_err_val = std::numeric_limits::round_error(); + fp16 denorm_min = std::numeric_limits::denorm_min(); + fp16 pos_inf = std::numeric_limits::infinity(); fp16 nan_val = std::numeric_limits::quiet_NaN(); + fp16 snan_val = std::numeric_limits::signaling_NaN(); + + INFO("Value of 16 min: " << std::hex << *(uint16_t *)&min_val); + INFO("Value of 16 max: " << std::hex << *(uint16_t *)&max_val); + INFO("Value of 16 epsilon: " << std::hex << *(uint16_t *)&epsilon_val); + INFO("Value of 16 round_error: " << std::hex << *(uint16_t *)&round_err_val); + + INFO("Value of 16 denorm_min: " << std::hex << *(uint16_t *)&denorm_min); + INFO("Value of 16 pos_inf: " << std::hex << *(uint16_t *)&pos_inf); + INFO("Value of 16 neg_inf: " << std::hex << *(uint16_t *)&neg_inf); + INFO("Value of 16 nan: " << std::hex << *(uint16_t *)&nan_val); + INFO("Value of 16 snan: " << std::hex << *(uint16_t *)&snan_val); INFO("Value of pos_inf: " << static_cast(pos_inf)); INFO("Value of neg_inf: " << static_cast(neg_inf));