From a679b584e9d36b3fcf5983acaf0ee516881c2537 Mon Sep 17 00:00:00 2001 From: Andrew Kane Date: Mon, 4 Dec 2023 13:59:48 -0800 Subject: [PATCH] Improved check [skip ci] --- src/half.c | 19 +++++++++++++++---- test/expected/half.out | 2 ++ test/sql/half.sql | 1 + 3 files changed, 18 insertions(+), 4 deletions(-) diff --git a/src/half.c b/src/half.c index bd36322..e40056f 100644 --- a/src/half.c +++ b/src/half.c @@ -294,6 +294,19 @@ HalfIsInf(half num) #endif } +/* + * Check if half is zero + */ +static inline bool +HalfIsZero(half num) +{ +#ifdef FLT16_SUPPORT + return num == 0; +#else + return (num << 1) == 0x0000; +#endif +} + /* * Convert a float4 to a half */ @@ -304,8 +317,7 @@ Float4ToHalf(float num) if (unlikely(HalfIsInf(result)) && !isinf(num)) float_overflow_error(); - /* TODO Perform check without HalfToFloat4 */ - if (unlikely(HalfToFloat4(result) == 0.0f) && num != 0.0) + if (unlikely(HalfIsZero(result)) && num != 0.0) float_underflow_error(); return result; @@ -322,8 +334,7 @@ Float8ToHalf(double num) if (unlikely(HalfIsInf(result)) && !isinf(num)) float_overflow_error(); - /* TODO Perform check without HalfToFloat4 */ - if (unlikely(HalfToFloat4(result) == 0.0f) && num != 0.0) + if (unlikely(HalfIsZero(result)) && num != 0.0) float_underflow_error(); return result; diff --git a/test/expected/half.out b/test/expected/half.out index 7188963..7726a47 100644 --- a/test/expected/half.out +++ b/test/expected/half.out @@ -82,6 +82,8 @@ SELECT 'Infinity'::real::half; Infinity (1 row) +SELECT '1e-38'::real::half; +ERROR: value out of range: underflow SELECT '1.5'::half::real; float4 -------- diff --git a/test/sql/half.sql b/test/sql/half.sql index 6588322..128cb5b 100644 --- a/test/sql/half.sql +++ b/test/sql/half.sql @@ -17,6 +17,7 @@ SELECT '{1,2,3}'::half[]::real[]; SELECT '65505'::integer::half; SELECT 'NaN'::real::half; SELECT 'Infinity'::real::half; +SELECT '1e-38'::real::half; SELECT '1.5'::half::real; SELECT '1.5'::real::half;