1// RUN: %clang_builtins %s %librt -o %t && %run %t
2// REQUIRES: librt_has_fixxfdi
3
4#include "int_lib.h"
5#include <stdio.h>
6
7
8#if HAS_80_BIT_LONG_DOUBLE
9// Returns: convert a to a signed long long, rounding toward zero.
10
11// Assumption: long double is an intel 80 bit floating point type padded with 6 bytes
12// su_int is a 32 bit integral type
13// value in long double is representable in di_int (no range checking performed)
14
15// gggg gggg gggg gggg gggg gggg gggg gggg | gggg gggg gggg gggg seee eeee eeee eeee |
16// 1mmm mmmm mmmm mmmm mmmm mmmm mmmm mmmm | mmmm mmmm mmmm mmmm mmmm mmmm mmmm mmmm
17
18COMPILER_RT_ABI di_int __fixxfdi(long double a);
19
20int test__fixxfdi(long double a, di_int expected)
21{
22 di_int x = __fixxfdi(a);
23 if (x != expected)
24 printf(format: "error in __fixxfdi(%LA) = %llX, expected %llX\n",
25 a, x, expected);
26 return x != expected;
27}
28
29char assumption_1[sizeof(di_int) == 2*sizeof(si_int)] = {0};
30char assumption_2[sizeof(su_int)*CHAR_BIT == 32] = {0};
31char assumption_3[sizeof(long double)*CHAR_BIT == 128] = {0};
32#endif
33
34int main()
35{
36#if HAS_80_BIT_LONG_DOUBLE
37 if (test__fixxfdi(a: 0.0, expected: 0))
38 return 1;
39
40 if (test__fixxfdi(a: 0.5, expected: 0))
41 return 1;
42 if (test__fixxfdi(a: 0.99, expected: 0))
43 return 1;
44 if (test__fixxfdi(a: 1.0, expected: 1))
45 return 1;
46 if (test__fixxfdi(a: 1.5, expected: 1))
47 return 1;
48 if (test__fixxfdi(a: 1.99, expected: 1))
49 return 1;
50 if (test__fixxfdi(a: 2.0, expected: 2))
51 return 1;
52 if (test__fixxfdi(a: 2.01, expected: 2))
53 return 1;
54 if (test__fixxfdi(a: -0.5, expected: 0))
55 return 1;
56 if (test__fixxfdi(a: -0.99, expected: 0))
57 return 1;
58 if (test__fixxfdi(a: -1.0, expected: -1))
59 return 1;
60 if (test__fixxfdi(a: -1.5, expected: -1))
61 return 1;
62 if (test__fixxfdi(a: -1.99, expected: -1))
63 return 1;
64 if (test__fixxfdi(a: -2.0, expected: -2))
65 return 1;
66 if (test__fixxfdi(a: -2.01, expected: -2))
67 return 1;
68
69 if (test__fixxfdi(a: 0x1.FFFFFEp+62, expected: 0x7FFFFF8000000000LL))
70 return 1;
71 if (test__fixxfdi(a: 0x1.FFFFFCp+62, expected: 0x7FFFFF0000000000LL))
72 return 1;
73
74 if (test__fixxfdi(a: -0x1.FFFFFEp+62, expected: 0x8000008000000000LL))
75 return 1;
76 if (test__fixxfdi(a: -0x1.FFFFFCp+62, expected: 0x8000010000000000LL))
77 return 1;
78
79 if (test__fixxfdi(a: 0x1.FFFFFFFFFFFFFp+62, expected: 0x7FFFFFFFFFFFFC00LL))
80 return 1;
81 if (test__fixxfdi(a: 0x1.FFFFFFFFFFFFEp+62, expected: 0x7FFFFFFFFFFFF800LL))
82 return 1;
83
84 if (test__fixxfdi(a: -0x1.FFFFFFFFFFFFFp+62, expected: 0x8000000000000400LL))
85 return 1;
86 if (test__fixxfdi(a: -0x1.FFFFFFFFFFFFEp+62, expected: 0x8000000000000800LL))
87 return 1;
88
89 if (test__fixxfdi(a: 0x1.FFFFFFFFFFFFFFFCp+62L, expected: 0x7FFFFFFFFFFFFFFFLL))
90 return 1;
91 if (test__fixxfdi(a: 0x1.FFFFFFFFFFFFFFF8p+62L, expected: 0x7FFFFFFFFFFFFFFELL))
92 return 1;
93
94 if (test__fixxfdi(a: -0x1.0000000000000000p+63L, expected: 0x8000000000000000LL))
95 return 1;
96 if (test__fixxfdi(a: -0x1.FFFFFFFFFFFFFFFCp+62L, expected: 0x8000000000000001LL))
97 return 1;
98 if (test__fixxfdi(a: -0x1.FFFFFFFFFFFFFFF8p+62L, expected: 0x8000000000000002LL))
99 return 1;
100
101#else
102 printf("skipped\n");
103#endif
104 return 0;
105}
106

source code of compiler-rt/test/builtins/Unit/fixxfdi_test.c