[compiler-rt] [compiler-rt] Allow running extendhfxf2_test without int128 support (PR #117818)

Alexander Richardson via llvm-commits llvm-commits at lists.llvm.org
Tue Nov 26 16:47:39 PST 2024


https://github.com/arichardson updated https://github.com/llvm/llvm-project/pull/117818

>From c0bc2d2e39e5943074c6fc12995ffcb47e03b16d Mon Sep 17 00:00:00 2001
From: Alex Richardson <alexrichardson at google.com>
Date: Tue, 26 Nov 2024 16:42:44 -0800
Subject: [PATCH 1/3] =?UTF-8?q?[=F0=9D=98=80=F0=9D=97=BD=F0=9D=97=BF]=20in?=
 =?UTF-8?q?itial=20version?=
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8
Content-Transfer-Encoding: 8bit

Created using spr 1.3.6-beta.1
---
 .../test/builtins/Unit/extendhfxf2_test.c     | 23 ++++-----
 compiler-rt/test/builtins/Unit/fp_test.h      | 50 +++++++++++--------
 2 files changed, 39 insertions(+), 34 deletions(-)

diff --git a/compiler-rt/test/builtins/Unit/extendhfxf2_test.c b/compiler-rt/test/builtins/Unit/extendhfxf2_test.c
index bbe40a433c0085..d7227a9f1d156e 100644
--- a/compiler-rt/test/builtins/Unit/extendhfxf2_test.c
+++ b/compiler-rt/test/builtins/Unit/extendhfxf2_test.c
@@ -7,59 +7,58 @@
 
 #include "fp_test.h"
 
-#if __LDBL_MANT_DIG__ == 64 && defined(__x86_64__) &&                          \
-    defined(COMPILER_RT_HAS_FLOAT16)
+#if HAS_80_BIT_LONG_DOUBLE
 
 xf_float __extendhfxf2(TYPE_FP16 f);
 
-int test_extendhfxf2(TYPE_FP16 a, uint64_t expectedHi, uint64_t expectedLo) {
+int test_extendhfxf2(TYPE_FP16 a, uint16_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",
-           toRep16(a), x, F80FromRep128(expectedHi, expectedLo));
+           toRep16(a), x, F80FromRep80(expectedHi, expectedLo));
   }
   return ret;
 }
 
 int main() {
   // Small positive value
-  if (test_extendhfxf2(fromRep16(0x2e66), UINT64_C(0x3ffb),
+  if (test_extendhfxf2(fromRep16(0x2e66), UINT16_C(0x3ffb),
                        UINT64_C(0xccc0000000000000)))
     return 1;
 
   // Small negative value
-  if (test_extendhfxf2(fromRep16(0xae66), UINT64_C(0xbffb),
+  if (test_extendhfxf2(fromRep16(0xae66), UINT16_C(0xbffb),
                        UINT64_C(0xccc0000000000000)))
     return 1;
 
   // Zero
-  if (test_extendhfxf2(fromRep16(0), UINT64_C(0x0), UINT64_C(0x0)))
+  if (test_extendhfxf2(fromRep16(0), UINT16_C(0x0), UINT64_C(0x0)))
     return 1;
 
   // Smallest positive non-zero value
-  if (test_extendhfxf2(fromRep16(0x0100), UINT64_C(0x3fef),
+  if (test_extendhfxf2(fromRep16(0x0100), UINT16_C(0x3fef),
                        UINT64_C(0x8000000000000000)))
     return 1;
 
   // Smallest negative non-zero value
-  if (test_extendhfxf2(fromRep16(0x8100), UINT64_C(0xbfef),
+  if (test_extendhfxf2(fromRep16(0x8100), UINT16_C(0xbfef),
                        UINT64_C(0x8000000000000000)))
     return 1;
 
   // Positive infinity
-  if (test_extendhfxf2(makeInf16(), UINT64_C(0x7fff),
+  if (test_extendhfxf2(makeInf16(), UINT16_C(0x7fff),
                        UINT64_C(0x8000000000000000)))
     return 1;
 
   // Negative infinity
-  if (test_extendhfxf2(makeNegativeInf16(), UINT64_C(0xffff),
+  if (test_extendhfxf2(makeNegativeInf16(), UINT16_C(0xffff),
                        UINT64_C(0x8000000000000000)))
     return 1;
 
   // NaN
-  if (test_extendhfxf2(makeQNaN16(), UINT64_C(0x7fff),
+  if (test_extendhfxf2(makeQNaN16(), UINT16_C(0x7fff),
                        UINT64_C(0xc000000000000000)))
     return 1;
 
diff --git a/compiler-rt/test/builtins/Unit/fp_test.h b/compiler-rt/test/builtins/Unit/fp_test.h
index b23e2ef7f3a167..65a311ffcd0bf8 100644
--- a/compiler-rt/test/builtins/Unit/fp_test.h
+++ b/compiler-rt/test/builtins/Unit/fp_test.h
@@ -2,6 +2,7 @@
 #include <limits.h>
 #include <string.h>
 #include <stdint.h>
+#include <assert.h>
 
 #include "int_types.h"
 
@@ -230,44 +231,49 @@ static inline double makeQNaN64(void)
     return fromRep64(0x7ff8000000000000UL);
 }
 
-#if __LDBL_MANT_DIG__ == 64 && defined(__x86_64__)
-static inline long double F80FromRep128(uint64_t hi, uint64_t lo) {
-    __uint128_t x = ((__uint128_t)hi << 64) + lo;
-    long double ret;
-    memcpy(&ret, &x, 16);
+#if HAS_80_BIT_LONG_DOUBLE
+static inline xf_float F80FromRep80(uint16_t hi, uint64_t lo) {
+    uqwords bits;
+    bits.high.all = hi;
+    bits.low.all = lo;
+    xf_float ret;
+    static_assert(sizeof(xf_float) <= sizeof(uqwords), "wrong representation");
+    memcpy(&ret, &bits, sizeof(ret));
     return ret;
 }
 
-static inline __uint128_t F80ToRep128(long double x) {
-    __uint128_t ret;
-    memcpy(&ret, &x, 16);
+static inline uqwords F80ToRep80(xf_float x) {
+    uqwords ret;
+    memset(&ret, 0, sizeof(ret));
+    memcpy(&ret, &x, sizeof(x));
+    // Any bits beyond the first 16 in high are undefined.
+    ret.high.all = (uint16_t)ret.high.all;
     return ret;
 }
 
-static inline int compareResultF80(long double result, uint64_t expectedHi,
+static inline int compareResultF80(xf_float result, uint16_t expectedHi,
                                    uint64_t expectedLo) {
-    __uint128_t rep = F80ToRep128(result);
-    // F80 occupies the lower 80 bits of __uint128_t.
-    uint64_t hi = (rep >> 64) & ((1UL << (80 - 64)) - 1);
-    uint64_t lo = rep;
-    return !(hi == expectedHi && lo == expectedLo);
+    uqwords rep = F80ToRep80(result);
+    // F80 high occupies the lower 16 bits of high.
+    assert((rep.high.all & ((1UL << (80 - 64)) - 1)) == rep.high.all);
+    return !(rep.high.all == expectedHi && rep.low.all == expectedLo);
 }
 
-static inline long double makeQNaN80(void) {
-    return F80FromRep128(0x7fffUL, 0xc000000000000000UL);
+static inline xf_float makeQNaN80(void) {
+    return F80FromRep80(0x7fffu, 0xc000000000000000UL);
 }
 
-static inline long double makeNaN80(uint64_t rand) {
-    return F80FromRep128(0x7fffUL,
+static inline xf_float makeNaN80(uint64_t rand) {
+    return F80FromRep80(0x7fffu,
                          0x8000000000000000 | (rand & 0x3fffffffffffffff));
 }
 
-static inline long double makeInf80(void) {
-    return F80FromRep128(0x7fffUL, 0x8000000000000000UL);
+static inline xf_float makeInf80(void) {
+    return F80FromRep80(0x7fffu, 0x8000000000000000UL);
 }
 
-static inline long double makeNegativeInf80(void) {
-  return F80FromRep128(0xffffUL, 0x8000000000000000UL);
+static inline xf_float makeNegativeInf80(void) {
+  return F80FromRep80(0xffffu, 0x8000000000000000UL);
 }
 #endif
 

>From 0175cae212707617459c0aa789e13d5876047751 Mon Sep 17 00:00:00 2001
From: Alex Richardson <alexrichardson at google.com>
Date: Tue, 26 Nov 2024 16:45:05 -0800
Subject: [PATCH 2/3] simplify assert

Created using spr 1.3.6-beta.1
---
 compiler-rt/test/builtins/Unit/fp_test.h | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)

diff --git a/compiler-rt/test/builtins/Unit/fp_test.h b/compiler-rt/test/builtins/Unit/fp_test.h
index 65a311ffcd0bf8..31e2c40f3f1628 100644
--- a/compiler-rt/test/builtins/Unit/fp_test.h
+++ b/compiler-rt/test/builtins/Unit/fp_test.h
@@ -255,7 +255,7 @@ static inline int compareResultF80(xf_float result, uint16_t expectedHi,
                                    uint64_t expectedLo) {
     uqwords rep = F80ToRep80(result);
     // F80 high occupies the lower 16 bits of high.
-    assert((rep.high.all & ((1UL << (80 - 64)) - 1)) == rep.high.all);
+    assert((uint64_t)(uint16_t)rep.high.all == rep.high.all);
     return !(rep.high.all == expectedHi && rep.low.all == expectedLo);
 }
 

>From e60cf25b7f15490883da267eb5ec21b7787f2198 Mon Sep 17 00:00:00 2001
From: Alex Richardson <alexrichardson at google.com>
Date: Tue, 26 Nov 2024 16:47:25 -0800
Subject: [PATCH 3/3] clang-format

Created using spr 1.3.6-beta.1
---
 compiler-rt/test/builtins/Unit/fp_test.h | 48 ++++++++++++------------
 1 file changed, 24 insertions(+), 24 deletions(-)

diff --git a/compiler-rt/test/builtins/Unit/fp_test.h b/compiler-rt/test/builtins/Unit/fp_test.h
index 31e2c40f3f1628..3a8968a9660448 100644
--- a/compiler-rt/test/builtins/Unit/fp_test.h
+++ b/compiler-rt/test/builtins/Unit/fp_test.h
@@ -1,8 +1,8 @@
-#include <stdlib.h>
+#include <assert.h>
 #include <limits.h>
-#include <string.h>
 #include <stdint.h>
-#include <assert.h>
+#include <stdlib.h>
+#include <string.h>
 
 #include "int_types.h"
 
@@ -233,43 +233,43 @@ static inline double makeQNaN64(void)
 
 #if HAS_80_BIT_LONG_DOUBLE
 static inline xf_float F80FromRep80(uint16_t hi, uint64_t lo) {
-    uqwords bits;
-    bits.high.all = hi;
-    bits.low.all = lo;
-    xf_float ret;
-    static_assert(sizeof(xf_float) <= sizeof(uqwords), "wrong representation");
-    memcpy(&ret, &bits, sizeof(ret));
-    return ret;
+  uqwords bits;
+  bits.high.all = hi;
+  bits.low.all = lo;
+  xf_float ret;
+  static_assert(sizeof(xf_float) <= sizeof(uqwords), "wrong representation");
+  memcpy(&ret, &bits, sizeof(ret));
+  return ret;
 }
 
 static inline uqwords F80ToRep80(xf_float x) {
-    uqwords ret;
-    memset(&ret, 0, sizeof(ret));
-    memcpy(&ret, &x, sizeof(x));
-    // Any bits beyond the first 16 in high are undefined.
-    ret.high.all = (uint16_t)ret.high.all;
-    return ret;
+  uqwords ret;
+  memset(&ret, 0, sizeof(ret));
+  memcpy(&ret, &x, sizeof(x));
+  // Any bits beyond the first 16 in high are undefined.
+  ret.high.all = (uint16_t)ret.high.all;
+  return ret;
 }
 
 static inline int compareResultF80(xf_float result, uint16_t expectedHi,
                                    uint64_t expectedLo) {
-    uqwords rep = F80ToRep80(result);
-    // F80 high occupies the lower 16 bits of high.
-    assert((uint64_t)(uint16_t)rep.high.all == rep.high.all);
-    return !(rep.high.all == expectedHi && rep.low.all == expectedLo);
+  uqwords rep = F80ToRep80(result);
+  // F80 high occupies the lower 16 bits of high.
+  assert((uint64_t)(uint16_t)rep.high.all == rep.high.all);
+  return !(rep.high.all == expectedHi && rep.low.all == expectedLo);
 }
 
 static inline xf_float makeQNaN80(void) {
-    return F80FromRep80(0x7fffu, 0xc000000000000000UL);
+  return F80FromRep80(0x7fffu, 0xc000000000000000UL);
 }
 
 static inline xf_float makeNaN80(uint64_t rand) {
-    return F80FromRep80(0x7fffu,
-                         0x8000000000000000 | (rand & 0x3fffffffffffffff));
+  return F80FromRep80(0x7fffu,
+                      0x8000000000000000 | (rand & 0x3fffffffffffffff));
 }
 
 static inline xf_float makeInf80(void) {
-    return F80FromRep80(0x7fffu, 0x8000000000000000UL);
+  return F80FromRep80(0x7fffu, 0x8000000000000000UL);
 }
 
 static inline xf_float makeNegativeInf80(void) {



More information about the llvm-commits mailing list