[compiler-rt] Fix extendhfxf2 test (PR #117665)

B I Mohammed Abbas via llvm-commits llvm-commits at lists.llvm.org
Mon Nov 25 23:27:04 PST 2024


https://github.com/biabbas updated https://github.com/llvm/llvm-project/pull/117665

>From e63acd3b5b64db144b11370890a1097030815ebd Mon Sep 17 00:00:00 2001
From: B I Mohammed Abbas <bimohammadabbas at gmail.com>
Date: Tue, 26 Nov 2024 12:56:51 +0530
Subject: [PATCH] Fix extendhfxf2_test and update fp_extend.h

---
 .../test/builtins/Unit/extendhfxf2_test.c     | 40 ++++++++++---------
 compiler-rt/test/builtins/Unit/fp_test.h      |  8 +++-
 2 files changed, 28 insertions(+), 20 deletions(-)

diff --git a/compiler-rt/test/builtins/Unit/extendhfxf2_test.c b/compiler-rt/test/builtins/Unit/extendhfxf2_test.c
index 80e6f78cdd9c4f..70105f21dadad3 100644
--- a/compiler-rt/test/builtins/Unit/extendhfxf2_test.c
+++ b/compiler-rt/test/builtins/Unit/extendhfxf2_test.c
@@ -5,59 +5,61 @@
 #include <math.h> // for isnan, isinf
 #include <stdio.h>
 
-#include "int_lib.h"
+#include "fp_test.h"
 
 #if HAS_80_BIT_LONG_DOUBLE && defined(COMPILER_RT_HAS_FLOAT16)
 
-long double __extendhfxf2(_Float16 f);
+xf_float __extendhfxf2(TYPE_FP16 f);
 
-int test_extendhfxf2(_Float16 a, long double expected) {
-  long double x = __extendhfxf2(a);
-  __uint16_t *b = (void *)&a;
-  int ret = !((isnan(x) && isnan(expected)) || x == expected);
+int test_extendhfxf2(TYPE_FP16 a, uint64_t expectedHi, uint64_t expectedLo) {
+  xf_float x = __extendhfxf2(a);
+  int ret = compareResultF80(x, expectedHi, expectedLo);
   if (ret) {
     printf("error in test__extendhfxf2(%#.4x) = %.20Lf, "
            "expected %.20Lf\n",
-           *b, x, expected);
+           toRep16(a), x, F80FromRep128(expectedHi, expectedLo));
   }
   return ret;
 }
 
-char assumption_1[sizeof(_Float16) * CHAR_BIT == 16] = {0};
-
 int main() {
   // Small positive value
-  if (test_extendhfxf2(0.09997558593750000000f, 0.09997558593750000000L))
+  if (test_extendhfxf2(fromRep16(0x2e66), UINT64_C(0x3ffb),
+                       UINT64_C(0xccc0000000000000)))
     return 1;
 
   // Small negative value
-  if (test_extendhfxf2(-0.09997558593750000000f, -0.09997558593750000000L))
+  if (test_extendhfxf2(fromRep16(0xae66), UINT64_C(0xbffb),
+                       UINT64_C(0xccc0000000000000)))
     return 1;
 
   // Zero
-  if (test_extendhfxf2(0.0f, 0.0L))
+  if (test_extendhfxf2(fromRep16(0), UINT64_C(0x0), UINT64_C(0x0)))
     return 1;
 
   // Smallest positive non-zero value
-  if (test_extendhfxf2(0x1p-16f, 0x1p-16L))
+  if (test_extendhfxf2(fromRep16(0x0100), UINT64_C(0x3fef),
+                       UINT64_C(0x8000000000000000)))
     return 1;
 
   // Smallest negative non-zero value
-  if (test_extendhfxf2(-0x1p-16f, -0x1p-16L))
+  if (test_extendhfxf2(fromRep16(0x8100), UINT64_C(0xbfef),
+                       UINT64_C(0x8000000000000000)))
     return 1;
 
   // Positive infinity
-  if (test_extendhfxf2(__builtin_huge_valf16(), __builtin_huge_valf64x()))
+  if (test_extendhfxf2(makeInf16(), UINT64_C(0x7fff),
+                       UINT64_C(0x8000000000000000)))
     return 1;
 
   // Negative infinity
-  if (test_extendhfxf2(-__builtin_huge_valf16(),
-                       (long double)-__builtin_huge_valf64x()))
+  if (test_extendhfxf2(makeNegativeInf16(), UINT64_C(0xffff),
+                       UINT64_C(0x8000000000000000)))
     return 1;
 
   // NaN
-  if (test_extendhfxf2(__builtin_nanf16(""),
-                       (long double)__builtin_nanf64x("")))
+  if (test_extendhfxf2(makeQNaN16(), UINT64_C(0x7fff),
+                       UINT64_C(0xc000000000000000)))
     return 1;
 
   return 0;
diff --git a/compiler-rt/test/builtins/Unit/fp_test.h b/compiler-rt/test/builtins/Unit/fp_test.h
index 4eb54b7425c107..347fb9951282e2 100644
--- a/compiler-rt/test/builtins/Unit/fp_test.h
+++ b/compiler-rt/test/builtins/Unit/fp_test.h
@@ -230,7 +230,7 @@ static inline double makeQNaN64(void)
     return fromRep64(0x7ff8000000000000UL);
 }
 
-#if __LDBL_MANT_DIG__ == 64 && defined(__x86_64__)
+#if HAS_80_BIT_LONG_DOUBLE
 static inline long double F80FromRep128(uint64_t hi, uint64_t lo) {
     __uint128_t x = ((__uint128_t)hi << 64) + lo;
     long double ret;
@@ -265,6 +265,10 @@ static inline long double makeNaN80(uint64_t rand) {
 static inline long double makeInf80(void) {
     return F80FromRep128(0x7fffUL, 0x8000000000000000UL);
 }
+
+static inline long double makeNegativeInf80(void) {
+  return F80FromRep128(0xffffUL, 0x8000000000000000UL);
+}
 #endif
 
 #if defined(CRT_HAS_TF_MODE)
@@ -299,6 +303,8 @@ static inline TYPE_FP16 makeInf16(void)
     return fromRep16(0x7c00U);
 }
 
+static inline TYPE_FP16 makeNegativeInf16(void) { return fromRep16(0xfc00U); }
+
 static inline float makeInf32(void)
 {
     return fromRep32(0x7f800000U);



More information about the llvm-commits mailing list