[compiler-rt] [llvm] Add extendhfxf2 into compiler rt (PR #111099)
B I Mohammed Abbas via llvm-commits
llvm-commits at lists.llvm.org
Fri Oct 4 00:41:16 PDT 2024
https://github.com/biabbas updated https://github.com/llvm/llvm-project/pull/111099
>From 1a6ba6044ec581bbf0a9aa74da8bbe1b795b5d82 Mon Sep 17 00:00:00 2001
From: B I Mohammed Abbas <bimohammadabbas at gmail.com>
Date: Fri, 4 Oct 2024 09:57:19 +0530
Subject: [PATCH 1/2] Add extendhfxf2 into compiler rt
---
compiler-rt/lib/builtins/CMakeLists.txt | 1 +
compiler-rt/lib/builtins/extendhfxf2.c | 18 +++++
.../lib/builtins/macho_embedded/common.txt | 1 +
.../test/builtins/Unit/extendhfxf2_test.c | 81 +++++++++++++++++++
.../compiler-rt/lib/builtins/BUILD.gn | 1 +
5 files changed, 102 insertions(+)
create mode 100644 compiler-rt/lib/builtins/extendhfxf2.c
create mode 100644 compiler-rt/test/builtins/Unit/extendhfxf2_test.c
diff --git a/compiler-rt/lib/builtins/CMakeLists.txt b/compiler-rt/lib/builtins/CMakeLists.txt
index 9a0a50ee7003f1..97a9e508d37a32 100644
--- a/compiler-rt/lib/builtins/CMakeLists.txt
+++ b/compiler-rt/lib/builtins/CMakeLists.txt
@@ -104,6 +104,7 @@ set(GENERIC_SOURCES
divti3.c
extendsfdf2.c
extendhfsf2.c
+ extendhfxf2.c
ffsdi2.c
ffssi2.c
ffsti2.c
diff --git a/compiler-rt/lib/builtins/extendhfxf2.c b/compiler-rt/lib/builtins/extendhfxf2.c
new file mode 100644
index 00000000000000..7425859f79f763
--- /dev/null
+++ b/compiler-rt/lib/builtins/extendhfxf2.c
@@ -0,0 +1,18 @@
+//===-- lib/extendhfxf2.c - half -> long double conversion --------*- C -*-===//
+//
+// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
+// See https://llvm.org/LICENSE.txt for license information.
+// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
+//
+//===----------------------------------------------------------------------===//
+
+#define SRC_HALF
+#define DST_DOUBLE
+#include "fp_extend_impl.inc"
+
+// Use a forwarding definition and noinline to implement a poor man's alias,
+// as there isn't a good cross-platform way of defining one.
+// Long double are expected to be as precise as double.
+COMPILER_RT_ABI NOINLINE long double __extendhfxf2(src_t a) {
+ return (long double)__extendXfYf2__(a);
+}
diff --git a/compiler-rt/lib/builtins/macho_embedded/common.txt b/compiler-rt/lib/builtins/macho_embedded/common.txt
index 819109768f5298..fa99bc239e68f2 100644
--- a/compiler-rt/lib/builtins/macho_embedded/common.txt
+++ b/compiler-rt/lib/builtins/macho_embedded/common.txt
@@ -60,6 +60,7 @@ divsf3
divsi3
extendsfdf2
extendhfsf2
+extendhfxf2
ffssi2
fixdfsi
fixsfsi
diff --git a/compiler-rt/test/builtins/Unit/extendhfxf2_test.c b/compiler-rt/test/builtins/Unit/extendhfxf2_test.c
new file mode 100644
index 00000000000000..402a76354f4bba
--- /dev/null
+++ b/compiler-rt/test/builtins/Unit/extendhfxf2_test.c
@@ -0,0 +1,81 @@
+// RUN: %clang_builtins %s %librt -o %t && %run %t
+// REQUIRES: librt_has_extendhfxf2
+
+#include "int_lib.h"
+#include <stdio.h>
+
+#if __LDBL_MANT_DIG__ == 113 && defined(COMPILER_RT_HAS_FLOAT16)
+
+# include "fp_test.h"
+
+COMPILER_RT_ABI long double __extendhfxf2(TYPE_FP16 a);
+
+int test__extendhfxf2(TYPE_FP16 a, uint64_t expectedHi, uint64_t expectedLo) {
+ long double x = __extendhfxf2(a);
+ int ret = compareResultF128(x, expectedHi, expectedLo);
+
+ if (ret) {
+ printf("error in test__extendhfxf2(%#.4x) = %.20Lf, "
+ "expected %.20Lf\n",
+ toRep16(a), x, fromRep128(expectedHi, expectedLo));
+ }
+ return ret;
+}
+
+char assumption_1[sizeof(TYPE_FP16) * CHAR_BIT == 16] = {0};
+
+#endif
+
+int main() {
+#if __LDBL_MANT_DIG__ == 113 && defined(COMPILER_RT_HAS_FLOAT16)
+ // qNaN
+ if (test__extendhfxf2(makeQNaN16(), UINT64_C(0x7fff800000000000),
+ UINT64_C(0x0)))
+ return 1;
+ // NaN
+ if (test__extendhfxf2(makeNaN16(UINT16_C(0x0100)),
+ UINT64_C(0x7fff400000000000), UINT64_C(0x0)))
+ return 1;
+ // inf
+ if (test__extendhfxf2(makeInf16(), UINT64_C(0x7fff000000000000),
+ UINT64_C(0x0)))
+ return 1;
+ if (test__extendhfxf2(-makeInf16(), UINT64_C(0xffff000000000000),
+ UINT64_C(0x0)))
+ return 1;
+ // zero
+ if (test__extendhfxf2(fromRep16(0x0U), UINT64_C(0x0), UINT64_C(0x0)))
+ return 1;
+ if (test__extendhfxf2(fromRep16(0x8000U), UINT64_C(0x8000000000000000),
+ UINT64_C(0x0)))
+ return 1;
+ // denormal
+ if (test__extendhfxf2(fromRep16(0x0010U), UINT64_C(0x3feb000000000000),
+ UINT64_C(0x0000000000000000)))
+ return 1;
+ if (test__extendhfxf2(fromRep16(0x0001U), UINT64_C(0x3fe7000000000000),
+ UINT64_C(0x0000000000000000)))
+ return 1;
+ if (test__extendhfxf2(fromRep16(0x8001U), UINT64_C(0xbfe7000000000000),
+ UINT64_C(0x0000000000000000)))
+ return 1;
+
+ // pi
+ if (test__extendhfxf2(fromRep16(0x4248U), UINT64_C(0x4000920000000000),
+ UINT64_C(0x0000000000000000)))
+ return 1;
+ if (test__extendhfxf2(fromRep16(0xc248U), UINT64_C(0xc000920000000000),
+ UINT64_C(0x0000000000000000)))
+ return 1;
+
+ if (test__extendhfxf2(fromRep16(0x508cU), UINT64_C(0x4004230000000000),
+ UINT64_C(0x0)))
+ return 1;
+ if (test__extendhfxf2(fromRep16(0x1bb7U), UINT64_C(0x3ff6edc000000000),
+ UINT64_C(0x0)))
+ return 1;
+#else
+ printf("skipped\n");
+#endif
+ return 0;
+}
diff --git a/llvm/utils/gn/secondary/compiler-rt/lib/builtins/BUILD.gn b/llvm/utils/gn/secondary/compiler-rt/lib/builtins/BUILD.gn
index 8904aed28229f1..efbf01960bf907 100644
--- a/llvm/utils/gn/secondary/compiler-rt/lib/builtins/BUILD.gn
+++ b/llvm/utils/gn/secondary/compiler-rt/lib/builtins/BUILD.gn
@@ -126,6 +126,7 @@ static_library("builtins") {
"divsi3.c",
"divti3.c",
"extendhfsf2.c",
+ "extendhfxf2.c"
"extendsfdf2.c",
"ffsdi2.c",
"ffssi2.c",
>From 131ee36df7e3c01706eb93179dfa5133cf516bb8 Mon Sep 17 00:00:00 2001
From: B I Mohammed Abbas <bimohammadabbas at gmail.com>
Date: Fri, 4 Oct 2024 10:27:28 +0530
Subject: [PATCH 2/2] Extendhfxf2 test updated
---
.../test/builtins/Unit/extendhfxf2_test.c | 84 ++++++++-----------
1 file changed, 35 insertions(+), 49 deletions(-)
diff --git a/compiler-rt/test/builtins/Unit/extendhfxf2_test.c b/compiler-rt/test/builtins/Unit/extendhfxf2_test.c
index 402a76354f4bba..71d76e67892bac 100644
--- a/compiler-rt/test/builtins/Unit/extendhfxf2_test.c
+++ b/compiler-rt/test/builtins/Unit/extendhfxf2_test.c
@@ -1,78 +1,64 @@
// RUN: %clang_builtins %s %librt -o %t && %run %t
// REQUIRES: librt_has_extendhfxf2
-#include "int_lib.h"
+#include <limits.h>
+#include <math.h> // for isnan, isinf
#include <stdio.h>
-#if __LDBL_MANT_DIG__ == 113 && defined(COMPILER_RT_HAS_FLOAT16)
+#if __LDBL_MANT_DIG__ >= 64 && defined(COMPILER_RT_HAS_FLOAT16)
+long double __extendhfxf2(_Float16 f);
-# include "fp_test.h"
-
-COMPILER_RT_ABI long double __extendhfxf2(TYPE_FP16 a);
-
-int test__extendhfxf2(TYPE_FP16 a, uint64_t expectedHi, uint64_t expectedLo) {
+int test_extendhfxf2(_Float16 a, long double expected) {
long double x = __extendhfxf2(a);
- int ret = compareResultF128(x, expectedHi, expectedLo);
-
+ __uint16_t *b = (void *)&a;
+ int ret = !(x == expected || (isnan(x) && isnan(expected)) ||
+ (isinf(x) && isinf(expected) && x == expected));
if (ret) {
- printf("error in test__extendhfxf2(%#.4x) = %.20Lf, "
+ printf("error in test__extendhfsf2(%#.4x) = %.20Lf, "
"expected %.20Lf\n",
- toRep16(a), x, fromRep128(expectedHi, expectedLo));
+ *b, x, expected);
}
return ret;
}
-char assumption_1[sizeof(TYPE_FP16) * CHAR_BIT == 16] = {0};
+char assumption_1[sizeof(_Float16) * CHAR_BIT == 16] = {0};
#endif
int main() {
-#if __LDBL_MANT_DIG__ == 113 && defined(COMPILER_RT_HAS_FLOAT16)
- // qNaN
- if (test__extendhfxf2(makeQNaN16(), UINT64_C(0x7fff800000000000),
- UINT64_C(0x0)))
- return 1;
- // NaN
- if (test__extendhfxf2(makeNaN16(UINT16_C(0x0100)),
- UINT64_C(0x7fff400000000000), UINT64_C(0x0)))
- return 1;
- // inf
- if (test__extendhfxf2(makeInf16(), UINT64_C(0x7fff000000000000),
- UINT64_C(0x0)))
- return 1;
- if (test__extendhfxf2(-makeInf16(), UINT64_C(0xffff000000000000),
- UINT64_C(0x0)))
- return 1;
- // zero
- if (test__extendhfxf2(fromRep16(0x0U), UINT64_C(0x0), UINT64_C(0x0)))
+#if __LDBL_MANT_DIG__ >= 64 && defined(COMPILER_RT_HAS_FLOAT16)
+ // Small positive value
+ if (test_extendhfxf2(0.09997558593750000000f, 0.09997558593750000000L))
return 1;
- if (test__extendhfxf2(fromRep16(0x8000U), UINT64_C(0x8000000000000000),
- UINT64_C(0x0)))
- return 1;
- // denormal
- if (test__extendhfxf2(fromRep16(0x0010U), UINT64_C(0x3feb000000000000),
- UINT64_C(0x0000000000000000)))
+
+ // Small negative value
+ if (test_extendhfxf2(-0.09997558593750000000f, -0.09997558593750000000L))
return 1;
- if (test__extendhfxf2(fromRep16(0x0001U), UINT64_C(0x3fe7000000000000),
- UINT64_C(0x0000000000000000)))
+
+ // Zero
+ if (test_extendhfxf2(0.0f, 0.0L))
return 1;
- if (test__extendhfxf2(fromRep16(0x8001U), UINT64_C(0xbfe7000000000000),
- UINT64_C(0x0000000000000000)))
+
+ // Smallest positive non-zero value
+ if (test_extendhfxf2(0x1p-16f, 0x1p-16L))
return 1;
- // pi
- if (test__extendhfxf2(fromRep16(0x4248U), UINT64_C(0x4000920000000000),
- UINT64_C(0x0000000000000000)))
+ // Smallest negative non-zero value
+ if (test_extendhfxf2(-0x1p-16f, -0x1p-16L))
return 1;
- if (test__extendhfxf2(fromRep16(0xc248U), UINT64_C(0xc000920000000000),
- UINT64_C(0x0000000000000000)))
+
+ // Positive infinity
+ if (test_extendhfxf2(__builtin_huge_valf16(), __builtin_huge_valf64x()))
return 1;
- if (test__extendhfxf2(fromRep16(0x508cU), UINT64_C(0x4004230000000000),
- UINT64_C(0x0)))
+ // Negative infinity
+ if (test_extendhfxf2(-__builtin_huge_valf16(),
+ (long double)-__builtin_huge_valf64x()))
return 1;
- if (test__extendhfxf2(fromRep16(0x1bb7U), UINT64_C(0x3ff6edc000000000),
- UINT64_C(0x0)))
+
+ // NaN
+ if (test_extendhfxf2(__builtin_nanf16(""),
+ (long double)__builtin_nanf64x("")))
return 1;
#else
printf("skipped\n");
More information about the llvm-commits
mailing list