Hardcoding numeric_limits values to work on platforms where our float16 class isn't defined

This commit is contained in:
Vladyslav Baranovskyi
2024-11-07 16:45:17 +02:00
parent f883d58f14
commit f72ceee298
2 changed files with 27 additions and 9 deletions

View File

@@ -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);*/ }
};
}

View File

@@ -32,9 +32,27 @@ TEST_CASE("Basic Conversions", "[float16]")
TEST_CASE("Infinity and NaN", "[float16]")
{
fp16 pos_inf = std::numeric_limits<fp16>::infinity();
fp16 neg_inf = -std::numeric_limits<fp16>::infinity();
fp16 min_val = std::numeric_limits<fp16>::min();
fp16 max_val = std::numeric_limits<fp16>::max();
fp16 epsilon_val = std::numeric_limits<fp16>::epsilon();
fp16 round_err_val = std::numeric_limits<fp16>::round_error();
fp16 denorm_min = std::numeric_limits<fp16>::denorm_min();
fp16 pos_inf = std::numeric_limits<fp16>::infinity();
fp16 nan_val = std::numeric_limits<fp16>::quiet_NaN();
fp16 snan_val = std::numeric_limits<fp16>::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<float>(pos_inf));
INFO("Value of neg_inf: " << static_cast<float>(neg_inf));