Skip to content

Commit 28ae2ff

Browse files
authored
Add truncxfhf2 with tests to compiler-rt (#120372)
Fixes #105181
1 parent 332d264 commit 28ae2ff

File tree

3 files changed

+90
-0
lines changed

3 files changed

+90
-0
lines changed

compiler-rt/lib/builtins/CMakeLists.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -310,6 +310,7 @@ set(x86_80_BIT_SOURCES
310310
mulxc3.c
311311
powixf2.c
312312
trunctfxf2.c
313+
truncxfhf2.c
313314
)
314315

315316
if (NOT MSVC)

compiler-rt/lib/builtins/truncxfhf2.c

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
//===-- lib/truncsfhf2.c - long double -> half 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_SINGLE
10+
#define DST_HALF
11+
#include "fp_trunc_impl.inc"
12+
13+
COMPILER_RT_ABI dst_t __truncxfhf2(xf_float a) {
14+
return __truncXfYf2__((float)a);
15+
}
Lines changed: 74 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,74 @@
1+
// RUN: %clang_builtins %s %librt -o %t && %run %t
2+
// REQUIRES: librt_has_truncxfhf2
3+
4+
#include <stdio.h>
5+
6+
#include "fp_test.h"
7+
8+
#if HAS_80_BIT_LONG_DOUBLE
9+
10+
TYPE_FP16 __truncxfhf2(xf_float f);
11+
12+
int test_truncxfhf2(uint16_t inputHi, uint64_t inputLo, uint16_t e) {
13+
xf_float a = F80FromRep80(inputHi, inputLo);
14+
TYPE_FP16 x = __truncxfhf2(a);
15+
int ret = compareResultH(x, e);
16+
if (ret) {
17+
printf("error in test__truncxfhf2(%Lf) = %#.4x, "
18+
"expected %#.4x\n",
19+
a, toRep16(x), e);
20+
}
21+
return ret;
22+
}
23+
24+
int main() {
25+
// Small positive value
26+
if (test_truncxfhf2(UINT16_C(0x3ffb), UINT64_C(0xccc0000000000000),
27+
UINT16_C(0x2e66)))
28+
return 1;
29+
30+
// Small negative value
31+
if (test_truncxfhf2(UINT16_C(0xbffb), UINT64_C(0xccc0000000000000),
32+
UINT16_C(0xae66)))
33+
return 1;
34+
35+
// Zero
36+
if (test_truncxfhf2(UINT16_C(0x0), UINT64_C(0x0), UINT16_C(0)))
37+
return 1;
38+
39+
// Smallest positive non-zero value
40+
if (test_truncxfhf2(UINT16_C(0x3fef), UINT64_C(0x8000000000000000),
41+
UINT16_C(0x0100)))
42+
return 1;
43+
44+
// Smallest negative non-zero value
45+
if (test_truncxfhf2(UINT16_C(0xbfef), UINT64_C(0x8000000000000000),
46+
UINT16_C(0x8100)))
47+
return 1;
48+
49+
// Positive infinity
50+
if (test_truncxfhf2(UINT16_C(0x7fff), UINT64_C(0x8000000000000000),
51+
UINT16_C(0x7c00U)))
52+
return 1;
53+
54+
// Negative infinity
55+
if (test_truncxfhf2(UINT16_C(0xffff), UINT64_C(0x8000000000000000),
56+
UINT16_C(0xfc00U)))
57+
return 1;
58+
59+
// NaN
60+
if (test_truncxfhf2(UINT16_C(0x7fff), UINT64_C(0xc000000000000000),
61+
UINT16_C(0x7e00U)))
62+
return 1;
63+
64+
return 0;
65+
}
66+
67+
#else
68+
69+
int main() {
70+
printf("skipped\n");
71+
return 0;
72+
}
73+
74+
#endif

0 commit comments

Comments
 (0)