1// RUN: %clang_builtins %s %librt -o %t && %run %t
2// REQUIRES: int128
3//===-- floattisf_test.c - Test __floattisf -------------------------------===//
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 __floattisf for the compiler_rt library.
12//
13//===----------------------------------------------------------------------===//
14
15#include "int_lib.h"
16#include <float.h>
17#include <stdio.h>
18
19#ifdef CRT_HAS_128BIT
20
21// Returns: convert a to a float, rounding toward even.
22
23// Assumption: float is a IEEE 32 bit floating point type
24//             ti_int is a 64 bit integral type
25
26// seee eeee emmm mmmm mmmm mmmm mmmm mmmm
27
28COMPILER_RT_ABI float __floattisf(ti_int a);
29
30int test__floattisf(ti_int a, float expected)
31{
32    float x = __floattisf(a);
33    if (x != expected)
34    {
35        twords at;
36        at.all = a;
37        printf("error in __floattisf(0x%.16llX%.16llX) = %a, expected %a\n",
38               at.s.high, at.s.low, x, expected);
39    }
40    return x != expected;
41}
42
43char assumption_1[sizeof(ti_int) == 2*sizeof(di_int)] = {0};
44char assumption_2[sizeof(ti_int)*CHAR_BIT == 128] = {0};
45char assumption_3[sizeof(float)*CHAR_BIT == 32] = {0};
46
47#endif
48
49int main()
50{
51#ifdef CRT_HAS_128BIT
52    if (test__floattisf(0, 0.0F))
53        return 1;
54
55    if (test__floattisf(1, 1.0F))
56        return 1;
57    if (test__floattisf(2, 2.0F))
58        return 1;
59    if (test__floattisf(-1, -1.0F))
60        return 1;
61    if (test__floattisf(-2, -2.0F))
62        return 1;
63
64    if (test__floattisf(0x7FFFFF8000000000LL, 0x1.FFFFFEp+62F))
65        return 1;
66    if (test__floattisf(0x7FFFFF0000000000LL, 0x1.FFFFFCp+62F))
67        return 1;
68
69    if (test__floattisf(make_ti(0xFFFFFFFFFFFFFFFFLL, 0x8000008000000000LL),
70                        -0x1.FFFFFEp+62F))
71        return 1;
72    if (test__floattisf(make_ti(0xFFFFFFFFFFFFFFFFLL, 0x8000010000000000LL),
73                        -0x1.FFFFFCp+62F))
74        return 1;
75
76    if (test__floattisf(make_ti(0xFFFFFFFFFFFFFFFFLL, 0x8000000000000000LL),
77                        -0x1.000000p+63F))
78        return 1;
79    if (test__floattisf(make_ti(0xFFFFFFFFFFFFFFFFLL, 0x8000000000000001LL),
80                        -0x1.000000p+63F))
81        return 1;
82
83    if (test__floattisf(0x0007FB72E8000000LL, 0x1.FEDCBAp+50F))
84        return 1;
85
86    if (test__floattisf(0x0007FB72EA000000LL, 0x1.FEDCBAp+50F))
87        return 1;
88    if (test__floattisf(0x0007FB72EB000000LL, 0x1.FEDCBAp+50F))
89        return 1;
90    if (test__floattisf(0x0007FB72EBFFFFFFLL, 0x1.FEDCBAp+50F))
91        return 1;
92    if (test__floattisf(0x0007FB72EC000000LL, 0x1.FEDCBCp+50F))
93        return 1;
94    if (test__floattisf(0x0007FB72E8000001LL, 0x1.FEDCBAp+50F))
95        return 1;
96
97    if (test__floattisf(0x0007FB72E6000000LL, 0x1.FEDCBAp+50F))
98        return 1;
99    if (test__floattisf(0x0007FB72E7000000LL, 0x1.FEDCBAp+50F))
100        return 1;
101    if (test__floattisf(0x0007FB72E7FFFFFFLL, 0x1.FEDCBAp+50F))
102        return 1;
103    if (test__floattisf(0x0007FB72E4000001LL, 0x1.FEDCBAp+50F))
104        return 1;
105    if (test__floattisf(0x0007FB72E4000000LL, 0x1.FEDCB8p+50F))
106        return 1;
107
108    if (test__floattisf(make_ti(0x0007FB72E8000000LL, 0), 0x1.FEDCBAp+114F))
109        return 1;
110
111    if (test__floattisf(make_ti(0x0007FB72EA000000LL, 0), 0x1.FEDCBAp+114F))
112        return 1;
113    if (test__floattisf(make_ti(0x0007FB72EB000000LL, 0), 0x1.FEDCBAp+114F))
114        return 1;
115    if (test__floattisf(make_ti(0x0007FB72EBFFFFFFLL, 0), 0x1.FEDCBAp+114F))
116        return 1;
117    if (test__floattisf(make_ti(0x0007FB72EC000000LL, 0), 0x1.FEDCBCp+114F))
118        return 1;
119    if (test__floattisf(make_ti(0x0007FB72E8000001LL, 0), 0x1.FEDCBAp+114F))
120        return 1;
121
122    if (test__floattisf(make_ti(0x0007FB72E6000000LL, 0), 0x1.FEDCBAp+114F))
123        return 1;
124    if (test__floattisf(make_ti(0x0007FB72E7000000LL, 0), 0x1.FEDCBAp+114F))
125        return 1;
126    if (test__floattisf(make_ti(0x0007FB72E7FFFFFFLL, 0), 0x1.FEDCBAp+114F))
127        return 1;
128    if (test__floattisf(make_ti(0x0007FB72E4000001LL, 0), 0x1.FEDCBAp+114F))
129        return 1;
130    if (test__floattisf(make_ti(0x0007FB72E4000000LL, 0), 0x1.FEDCB8p+114F))
131        return 1;
132
133#else
134    printf("skipped\n");
135#endif
136   return 0;
137}
138