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

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