1// RUN: %clang_builtins %s %librt -o %t && %run %t
2// REQUIRES: x86-target-arch
3
4//===-- fixunsxfti_test.c - Test __fixunsxfti -----------------------------===//
5//
6// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
7// See https://llvm.org/LICENSE.txt for license information.
8// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
9//
10//===----------------------------------------------------------------------===//
11//
12// This file tests __fixunsxfti for the compiler_rt library.
13//
14//===----------------------------------------------------------------------===//
15
16#include "int_lib.h"
17#include <stdio.h>
18
19#if defined(CRT_HAS_128BIT) && HAS_80_BIT_LONG_DOUBLE
20
21// Returns: convert a to a unsigned long long, rounding toward zero.
22//          Negative values all become zero.
23
24// Assumption: long double is an intel 80 bit floating point type padded with 6 bytes
25//             tu_int is a 64 bit integral type
26//             value in long double is representable in tu_int or is negative
27//                 (no range checking performed)
28
29// gggg gggg gggg gggg gggg gggg gggg gggg | gggg gggg gggg gggg seee eeee eeee eeee |
30// 1mmm mmmm mmmm mmmm mmmm mmmm mmmm mmmm | mmmm mmmm mmmm mmmm mmmm mmmm mmmm mmmm
31
32COMPILER_RT_ABI tu_int __fixunsxfti(long double a);
33
34int test__fixunsxfti(long double a, tu_int expected)
35{
36    tu_int x = __fixunsxfti(a);
37    if (x != expected)
38    {
39        utwords xt;
40        xt.all = x;
41        utwords expectedt;
42        expectedt.all = expected;
43        printf("error in __fixunsxfti(%LA) = 0x%.16llX%.16llX, expected 0x%.16llX%.16llX\n",
44               a, xt.s.high, xt.s.low, expectedt.s.high, expectedt.s.low);
45    }
46    return x != expected;
47}
48
49char assumption_1[sizeof(tu_int) == 2*sizeof(du_int)] = {0};
50char assumption_2[sizeof(tu_int)*CHAR_BIT == 128] = {0};
51char assumption_3[sizeof(long double)*CHAR_BIT == 128] = {0};
52
53#endif
54
55int main()
56{
57#if defined(CRT_HAS_128BIT) && HAS_80_BIT_LONG_DOUBLE
58    if (test__fixunsxfti(0.0, 0))
59        return 1;
60
61    if (test__fixunsxfti(0.5, 0))
62        return 1;
63    if (test__fixunsxfti(0.99, 0))
64        return 1;
65    if (test__fixunsxfti(1.0, 1))
66        return 1;
67    if (test__fixunsxfti(1.5, 1))
68        return 1;
69    if (test__fixunsxfti(1.99, 1))
70        return 1;
71    if (test__fixunsxfti(2.0, 2))
72        return 1;
73    if (test__fixunsxfti(2.01, 2))
74        return 1;
75    if (test__fixunsxfti(-0.5, 0))
76        return 1;
77    if (test__fixunsxfti(-0.99, 0))
78        return 1;
79    if (test__fixunsxfti(-1.0, 0))
80        return 1;
81    if (test__fixunsxfti(-1.5, 0))
82        return 1;
83    if (test__fixunsxfti(-1.99, 0))
84        return 1;
85    if (test__fixunsxfti(-2.0, 0))
86        return 1;
87    if (test__fixunsxfti(-2.01, 0))
88        return 1;
89
90    if (test__fixunsxfti(0x1.FFFFFEp+62, 0x7FFFFF8000000000LL))
91        return 1;
92    if (test__fixunsxfti(0x1.FFFFFCp+62, 0x7FFFFF0000000000LL))
93        return 1;
94
95    if (test__fixunsxfti(-0x1.FFFFFEp+62, 0))
96        return 1;
97    if (test__fixunsxfti(-0x1.FFFFFCp+62, 0))
98        return 1;
99
100    if (test__fixunsxfti(0x1.FFFFFFFFFFFFFp+62, 0x7FFFFFFFFFFFFC00LL))
101        return 1;
102    if (test__fixunsxfti(0x1.FFFFFFFFFFFFEp+62, 0x7FFFFFFFFFFFF800LL))
103        return 1;
104
105    if (test__fixunsxfti(-0x1.FFFFFFFFFFFFFp+62, 0))
106        return 1;
107    if (test__fixunsxfti(-0x1.FFFFFFFFFFFFEp+62, 0))
108        return 1;
109
110    if (test__fixunsxfti(0x1.FFFFFFFFFFFFFFFEp+63L, 0xFFFFFFFFFFFFFFFFLL))
111        return 1;
112    if (test__fixunsxfti(0x1.0000000000000002p+63L, 0x8000000000000001LL))
113        return 1;
114    if (test__fixunsxfti(0x1.0000000000000000p+63L, 0x8000000000000000LL))
115        return 1;
116    if (test__fixunsxfti(0x1.FFFFFFFFFFFFFFFCp+62L, 0x7FFFFFFFFFFFFFFFLL))
117        return 1;
118    if (test__fixunsxfti(0x1.FFFFFFFFFFFFFFF8p+62L, 0x7FFFFFFFFFFFFFFELL))
119        return 1;
120
121    if (test__fixunsxfti(-0x1.0000000000000000p+63L, 0))
122        return 1;
123    if (test__fixunsxfti(-0x1.FFFFFFFFFFFFFFFCp+62L, 0))
124        return 1;
125    if (test__fixunsxfti(-0x1.FFFFFFFFFFFFFFF8p+62L, 0))
126        return 1;
127
128    if (test__fixunsxfti(0x1.FFFFFFFFFFFFFFFEp+127L, make_ti(0xFFFFFFFFFFFFFFFFLL, 0)))
129        return 1;
130    if (test__fixunsxfti(0x1.0000000000000002p+127L, make_ti(0x8000000000000001LL, 0)))
131        return 1;
132    if (test__fixunsxfti(0x1.0000000000000000p+127L, make_ti(0x8000000000000000LL, 0)))
133        return 1;
134    if (test__fixunsxfti(0x1.FFFFFFFFFFFFFFFCp+126L, make_ti(0x7FFFFFFFFFFFFFFFLL, 0)))
135        return 1;
136    if (test__fixunsxfti(0x1.FFFFFFFFFFFFFFF8p+126L, make_ti(0x7FFFFFFFFFFFFFFELL, 0)))
137        return 1;
138
139#else
140    printf("skipped\n");
141#endif
142   return 0;
143}
144