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