Skip to content

Commit 5bc78b0

Browse files
biabbasSterling-Augustine
authored andcommitted
Add missing extendhfxf2 in compiler rt (llvm#109090)
Issue: llvm#105181 extendhfxf2 calls extendhfXfy to convert _Float16 to double, then type casts this converted value to long double. __uint128_t may not be available on all architectures. Thus I din't use extendhfXfy to widen precision to 128 bits.
1 parent 24b0807 commit 5bc78b0

File tree

5 files changed

+82
-0
lines changed

5 files changed

+82
-0
lines changed

compiler-rt/lib/builtins/CMakeLists.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -104,6 +104,7 @@ set(GENERIC_SOURCES
104104
divti3.c
105105
extendsfdf2.c
106106
extendhfsf2.c
107+
extendhfxf2.c
107108
ffsdi2.c
108109
ffssi2.c
109110
ffsti2.c
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
//===-- lib/extendhfxf2.c - half -> long double conversion --------*- C -*-===//
2+
//
3+
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4+
// See https://llvm.org/LICENSE.txt for license information.
5+
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6+
//
7+
//===----------------------------------------------------------------------===//
8+
9+
#define SRC_HALF
10+
#define DST_DOUBLE
11+
#include "fp_extend_impl.inc"
12+
13+
// Use a forwarding definition and noinline to implement a poor man's alias,
14+
// as there isn't a good cross-platform way of defining one.
15+
// Long double are expected to be as precise as double.
16+
COMPILER_RT_ABI NOINLINE long double __extendhfxf2(src_t a) {
17+
return (long double)__extendXfYf2__(a);
18+
}

compiler-rt/lib/builtins/macho_embedded/common.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -60,6 +60,7 @@ divsf3
6060
divsi3
6161
extendsfdf2
6262
extendhfsf2
63+
extendhfxf2
6364
ffssi2
6465
fixdfsi
6566
fixsfsi
Lines changed: 61 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,61 @@
1+
// RUN: %clang_builtins %s %librt -o %t && %run %t
2+
// REQUIRES: librt_has_extendhfxf2
3+
4+
#include <limits.h>
5+
#include <math.h> // for isnan, isinf
6+
#include <stdio.h>
7+
8+
long double __extendhfxf2(_Float16 f);
9+
10+
int test_extendhfxf2(_Float16 a, long double expected) {
11+
long double x = __extendhfxf2(a);
12+
__uint16_t *b = (void *)&a;
13+
int ret = !(x == expected || (isnan(x) && isnan(expected)) ||
14+
(isinf(x) && isinf(expected) && x == expected));
15+
if (ret) {
16+
printf("error in test__extendhfsf2(%#.4x) = %.20Lf, "
17+
"expected %.20Lf\n",
18+
*b, x, expected);
19+
}
20+
return ret;
21+
}
22+
23+
char assumption_1[sizeof(_Float16) * CHAR_BIT == 16] = {0};
24+
25+
int main() {
26+
// Small positive value
27+
if (test_extendhfxf2(0.09997558593750000000f, 0.09997558593750000000L))
28+
return 1;
29+
30+
// Small negative value
31+
if (test_extendhfxf2(-0.09997558593750000000f, -0.09997558593750000000L))
32+
return 1;
33+
34+
// Zero
35+
if (test_extendhfxf2(0.0f, 0.0L))
36+
return 1;
37+
38+
// Smallest positive non-zero value
39+
if (test_extendhfxf2(0x1p-16f, 0x1p-16L))
40+
return 1;
41+
42+
// Smallest negative non-zero value
43+
if (test_extendhfxf2(-0x1p-16f, -0x1p-16L))
44+
return 1;
45+
46+
// Positive infinity
47+
if (test_extendhfxf2(__builtin_huge_valf16(), __builtin_huge_valf64x()))
48+
return 1;
49+
50+
// Negative infinity
51+
if (test_extendhfxf2(-__builtin_huge_valf16(),
52+
(long double)-__builtin_huge_valf64x()))
53+
return 1;
54+
55+
// NaN
56+
if (test_extendhfxf2(__builtin_nanf16(""),
57+
(long double)__builtin_nanf64x("")))
58+
return 1;
59+
60+
return 0;
61+
}

llvm/utils/gn/secondary/compiler-rt/lib/builtins/BUILD.gn

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -126,6 +126,7 @@ static_library("builtins") {
126126
"divsi3.c",
127127
"divti3.c",
128128
"extendhfsf2.c",
129+
"extendhfxf2.c"
129130
"extendsfdf2.c",
130131
"ffsdi2.c",
131132
"ffssi2.c",

0 commit comments

Comments
 (0)