Skip to content

Commit 8ddd5b9

Browse files
committed
Add extendhfxf2 into compiler rt for x86
1 parent 1164bd7 commit 8ddd5b9

File tree

4 files changed

+91
-0
lines changed

4 files changed

+91
-0
lines changed

compiler-rt/lib/builtins/CMakeLists.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -295,6 +295,7 @@ endif ()
295295
# long double is not 80 bits on Android or MSVC.
296296
set(x86_80_BIT_SOURCES
297297
divxc3.c
298+
extendhfxf2.c
298299
extendxftf2.c
299300
fixxfdi.c
300301
fixxfti.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+
}
Lines changed: 71 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,71 @@
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+
#if __LDBL_MANT_DIG__ >= 64 && defined(COMPILER_RT_HAS_FLOAT16)
9+
10+
long double __extendhfxf2(_Float16 f);
11+
12+
int test_extendhfxf2(_Float16 a, long double expected) {
13+
long double x = __extendhfxf2(a);
14+
__uint16_t *b = (void *)&a;
15+
int ret = !((isnan(x) && isnan(expected)) || x == expected);
16+
if (ret) {
17+
printf("error in test__extendhfxf2(%#.4x) = %.20Lf, "
18+
"expected %.20Lf\n",
19+
*b, x, expected);
20+
}
21+
return ret;
22+
}
23+
24+
char assumption_1[sizeof(_Float16) * CHAR_BIT == 16] = {0};
25+
26+
int main() {
27+
// Small positive value
28+
if (test_extendhfxf2(0.09997558593750000000f, 0.09997558593750000000L))
29+
return 1;
30+
31+
// Small negative value
32+
if (test_extendhfxf2(-0.09997558593750000000f, -0.09997558593750000000L))
33+
return 1;
34+
35+
// Zero
36+
if (test_extendhfxf2(0.0f, 0.0L))
37+
return 1;
38+
39+
// Smallest positive non-zero value
40+
if (test_extendhfxf2(0x1p-16f, 0x1p-16L))
41+
return 1;
42+
43+
// Smallest negative non-zero value
44+
if (test_extendhfxf2(-0x1p-16f, -0x1p-16L))
45+
return 1;
46+
47+
// Positive infinity
48+
if (test_extendhfxf2(__builtin_huge_valf16(), __builtin_huge_valf64x()))
49+
return 1;
50+
51+
// Negative infinity
52+
if (test_extendhfxf2(-__builtin_huge_valf16(),
53+
(long double)-__builtin_huge_valf64x()))
54+
return 1;
55+
56+
// NaN
57+
if (test_extendhfxf2(__builtin_nanf16(""),
58+
(long double)__builtin_nanf64x("")))
59+
return 1;
60+
61+
return 0;
62+
}
63+
64+
#else
65+
66+
int main() {
67+
printf("skipped\n");
68+
return 0;
69+
}
70+
71+
#endif

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

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -248,6 +248,7 @@ static_library("builtins") {
248248
"divtf3.c",
249249
"extenddftf2.c",
250250
"extendhftf2.c",
251+
"extendhfxf2.c",
251252
"extendsftf2.c",
252253
"fixtfdi.c",
253254
"fixtfsi.c",

0 commit comments

Comments
 (0)