1// RUN: %clang_builtins %s %librt -o %t && %run %t
2// REQUIRES: x86-target-arch
3
4//===-- fixxfti_test.c - Test __fixxfti -----------------------------------===//
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 __fixxfti 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 signed long long, rounding toward zero.
22
23// Assumption: long double is an intel 80 bit floating point type padded with 6 bytes
24//             su_int is a 32 bit integral type
25//             value in long double is representable in ti_int (no range checking performed)
26
27// gggg gggg gggg gggg gggg gggg gggg gggg | gggg gggg gggg gggg seee eeee eeee eeee |
28// 1mmm mmmm mmmm mmmm mmmm mmmm mmmm mmmm | mmmm mmmm mmmm mmmm mmmm mmmm mmmm mmmm
29
30COMPILER_RT_ABI ti_int __fixxfti(long double a);
31
32int test__fixxfti(long double a, ti_int expected)
33{
34    ti_int x = __fixxfti(a);
35    if (x != expected)
36    {
37        utwords xt;
38        xt.all = x;
39        utwords expectedt;
40        expectedt.all = expected;
41        printf("error in __fixxfti(%LA) = 0x%.16llX%.16llX, expected 0x%.16llX%.16llX\n",
42               a, xt.s.high, xt.s.low, expectedt.s.high, expectedt.s.low);
43    }
44    return x != expected;
45}
46
47COMPILE_TIME_ASSERT(sizeof(ti_int) == 2*sizeof(di_int));
48COMPILE_TIME_ASSERT(sizeof(su_int)*CHAR_BIT == 32);
49COMPILE_TIME_ASSERT(sizeof(long double)*CHAR_BIT == 128);
50
51#endif
52
53int main()
54{
55#if defined(CRT_HAS_128BIT) && HAS_80_BIT_LONG_DOUBLE
56    if (test__fixxfti(0.0, 0))
57        return 1;
58
59    if (test__fixxfti(0.5, 0))
60        return 1;
61    if (test__fixxfti(0.99, 0))
62        return 1;
63    if (test__fixxfti(1.0, 1))
64        return 1;
65    if (test__fixxfti(1.5, 1))
66        return 1;
67    if (test__fixxfti(1.99, 1))
68        return 1;
69    if (test__fixxfti(2.0, 2))
70        return 1;
71    if (test__fixxfti(2.01, 2))
72        return 1;
73    if (test__fixxfti(-0.5, 0))
74        return 1;
75    if (test__fixxfti(-0.99, 0))
76        return 1;
77    if (test__fixxfti(-1.0, -1))
78        return 1;
79    if (test__fixxfti(-1.5, -1))
80        return 1;
81    if (test__fixxfti(-1.99, -1))
82        return 1;
83    if (test__fixxfti(-2.0, -2))
84        return 1;
85    if (test__fixxfti(-2.01, -2))
86        return 1;
87
88    if (test__fixxfti(0x1.FFFFFEp+62, 0x7FFFFF8000000000LL))
89        return 1;
90    if (test__fixxfti(0x1.FFFFFCp+62, 0x7FFFFF0000000000LL))
91        return 1;
92
93    if (test__fixxfti(-0x1.FFFFFEp+62, make_ti(0xFFFFFFFFFFFFFFFFLL,
94                                               0x8000008000000000LL)))
95        return 1;
96    if (test__fixxfti(-0x1.FFFFFCp+62, make_ti(0xFFFFFFFFFFFFFFFFLL,
97                                               0x8000010000000000LL)))
98        return 1;
99
100    if (test__fixxfti(0x1.FFFFFFFFFFFFFp+62, 0x7FFFFFFFFFFFFC00LL))
101        return 1;
102    if (test__fixxfti(0x1.FFFFFFFFFFFFEp+62, 0x7FFFFFFFFFFFF800LL))
103        return 1;
104
105    if (test__fixxfti(-0x1.FFFFFFFFFFFFFp+62, make_ti(0xFFFFFFFFFFFFFFFFLL,
106                                                      0x8000000000000400LL)))
107        return 1;
108    if (test__fixxfti(-0x1.FFFFFFFFFFFFEp+62, make_ti(0xFFFFFFFFFFFFFFFFLL,
109                                                      0x8000000000000800LL)))
110        return 1;
111
112    if (test__fixxfti(0x1.FFFFFFFFFFFFFFFCp+62L, 0x7FFFFFFFFFFFFFFFLL))
113        return 1;
114    if (test__fixxfti(0x1.FFFFFFFFFFFFFFF8p+62L, 0x7FFFFFFFFFFFFFFELL))
115        return 1;
116
117    if (test__fixxfti(-0x1.0000000000000000p+63L, make_ti(0xFFFFFFFFFFFFFFFFLL,
118                                                          0x8000000000000000LL)))
119        return 1;
120    if (test__fixxfti(-0x1.FFFFFFFFFFFFFFFCp+62L, make_ti(0xFFFFFFFFFFFFFFFFLL,
121                                                          0x8000000000000001LL)))
122        return 1;
123    if (test__fixxfti(-0x1.FFFFFFFFFFFFFFF8p+62L, make_ti(0xFFFFFFFFFFFFFFFFLL,
124                                                          0x8000000000000002LL)))
125        return 1;
126
127    if (test__fixxfti(0x1.FFFFFFFFFFFFFFFEp+126L, make_ti(0x7FFFFFFFFFFFFFFFLL,
128                                                          0x8000000000000000LL)))
129        return 1;
130    if (test__fixxfti(0x1.FFFFFFFFFFFFFFFCp+126L, make_ti(0x7FFFFFFFFFFFFFFFLL,
131                                                          0x0000000000000000LL)))
132
133        return 1;
134
135    if (test__fixxfti(-0x1.0000000000000000p+127L, make_ti(0x8000000000000000LL,
136                                                           0x0000000000000000LL)))
137        return 1;
138    if (test__fixxfti(-0x1.FFFFFFFFFFFFFFFEp+126L, make_ti(0x8000000000000000LL,
139                                                           0x8000000000000000LL)))
140        return 1;
141    if (test__fixxfti(-0x1.FFFFFFFFFFFFFFFCp+126L, make_ti(0x8000000000000001LL,
142                                                           0x0000000000000000LL)))
143        return 1;
144#else
145    printf("skipped\n");
146#endif
147   return 0;
148}
149