1// RUN: %clang_builtins %s %librt -o %t && %run %t
2
3//===--------------- truncsfhf2_test.c - Test __truncsfhf2 ----------------===//
4//
5// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
6// See https://llvm.org/LICENSE.txt for license information.
7// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
8//
9//===----------------------------------------------------------------------===//
10//
11// This file tests __truncsfhf2 for the compiler_rt library.
12//
13//===----------------------------------------------------------------------===//
14
15#include <stdio.h>
16
17#include "fp_test.h"
18
19uint16_t __truncsfhf2(float a);
20
21int test__truncsfhf2(float a, uint16_t expected)
22{
23    uint16_t x = __truncsfhf2(a);
24    int ret = compareResultH(x, expected);
25
26    if (ret){
27        printf("error in test__truncsfhf2(%f) = %#.4x, "
28               "expected %#.4x\n", a, x, fromRep16(expected));
29    }
30    return ret;
31}
32
33char assumption_1[sizeof(__fp16) * CHAR_BIT == 16] = {0};
34
35int main()
36{
37    // qNaN
38    if (test__truncsfhf2(makeQNaN32(),
39                         UINT16_C(0x7e00)))
40        return 1;
41    // NaN
42    if (test__truncsfhf2(makeNaN32(UINT32_C(0x8000)),
43                         UINT16_C(0x7e00)))
44        return 1;
45    // inf
46    if (test__truncsfhf2(makeInf32(),
47                         UINT16_C(0x7c00)))
48        return 1;
49    if (test__truncsfhf2(-makeInf32(),
50                         UINT16_C(0xfc00)))
51        return 1;
52    // zero
53    if (test__truncsfhf2(0.0f, UINT16_C(0x0)))
54        return 1;
55    if (test__truncsfhf2(-0.0f, UINT16_C(0x8000)))
56        return 1;
57
58    if (test__truncsfhf2(3.1415926535f,
59                         UINT16_C(0x4248)))
60        return 1;
61    if (test__truncsfhf2(-3.1415926535f,
62                         UINT16_C(0xc248)))
63        return 1;
64    if (test__truncsfhf2(0x1.987124876876324p+100f,
65                         UINT16_C(0x7c00)))
66        return 1;
67    if (test__truncsfhf2(0x1.987124876876324p+12f,
68                         UINT16_C(0x6e62)))
69        return 1;
70    if (test__truncsfhf2(0x1.0p+0f,
71                         UINT16_C(0x3c00)))
72        return 1;
73    if (test__truncsfhf2(0x1.0p-14f,
74                         UINT16_C(0x0400)))
75        return 1;
76    // denormal
77    if (test__truncsfhf2(0x1.0p-20f,
78                         UINT16_C(0x0010)))
79        return 1;
80    if (test__truncsfhf2(0x1.0p-24f,
81                         UINT16_C(0x0001)))
82        return 1;
83    if (test__truncsfhf2(-0x1.0p-24f,
84                         UINT16_C(0x8001)))
85        return 1;
86    if (test__truncsfhf2(0x1.5p-25f,
87                         UINT16_C(0x0001)))
88        return 1;
89    // and back to zero
90    if (test__truncsfhf2(0x1.0p-25f,
91                         UINT16_C(0x0000)))
92        return 1;
93    if (test__truncsfhf2(-0x1.0p-25f,
94                         UINT16_C(0x8000)))
95        return 1;
96    // max (precise)
97    if (test__truncsfhf2(65504.0f,
98                         UINT16_C(0x7bff)))
99        return 1;
100    // max (rounded)
101    if (test__truncsfhf2(65519.0f,
102                         UINT16_C(0x7bff)))
103        return 1;
104    // max (to +inf)
105    if (test__truncsfhf2(65520.0f,
106                         UINT16_C(0x7c00)))
107        return 1;
108    if (test__truncsfhf2(65536.0f,
109                         UINT16_C(0x7c00)))
110        return 1;
111    if (test__truncsfhf2(-65520.0f,
112                         UINT16_C(0xfc00)))
113        return 1;
114    return 0;
115}
116