1 // RUN: %clang_builtins %s %librt -o %t && %run %t
2 // REQUIRES: librt_has_fixunsxfsi
3 
4 #include "int_lib.h"
5 #include <stdio.h>
6 
7 #if HAS_80_BIT_LONG_DOUBLE
8 // Returns: convert a to a unsigned int, rounding toward zero.
9 //          Negative values all become 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 su_int or is negative
14 //                 (no range checking performed)
15 
16 // gggg gggg gggg gggg gggg gggg gggg gggg | gggg gggg gggg gggg seee eeee eeee eeee |
17 // 1mmm mmmm mmmm mmmm mmmm mmmm mmmm mmmm | mmmm mmmm mmmm mmmm mmmm mmmm mmmm mmmm
18 
19 COMPILER_RT_ABI su_int __fixunsxfsi(long double a);
20 
21 int test__fixunsxfsi(long double a, su_int expected)
22 {
test__fixunstfti(long double a,tu_int expected)23     su_int x = __fixunsxfsi(a);
24     if (x != expected)
25         printf("error in __fixunsxfsi(%LA) = %X, expected %X\n", a, x, expected);
26     return x != expected;
27 }
28 
29 char assumption_2[sizeof(su_int)*CHAR_BIT == 32] = {0};
30 char assumption_3[sizeof(long double)*CHAR_BIT == 128] = {0};
31 #endif
32 
33 int main()
34 {
35 #if HAS_80_BIT_LONG_DOUBLE
36     if (test__fixunsxfsi(0.0, 0))
37         return 1;
38 
39     if (test__fixunsxfsi(0.5, 0))
40         return 1;
41     if (test__fixunsxfsi(0.99, 0))
42         return 1;
43     if (test__fixunsxfsi(1.0, 1))
44         return 1;
45     if (test__fixunsxfsi(1.5, 1))
46         return 1;
main()47     if (test__fixunsxfsi(1.99, 1))
48         return 1;
49     if (test__fixunsxfsi(2.0, 2))
50         return 1;
51     if (test__fixunsxfsi(2.01, 2))
52         return 1;
53     if (test__fixunsxfsi(-0.5, 0))
54         return 1;
55     if (test__fixunsxfsi(-0.99, 0))
56         return 1;
57 #if !TARGET_LIBGCC
58     if (test__fixunsxfsi(-1.0, 0))  // libgcc ignores "returns 0 for negative input" spec
59         return 1;
60     if (test__fixunsxfsi(-1.5, 0))
61         return 1;
62     if (test__fixunsxfsi(-1.99, 0))
63         return 1;
64     if (test__fixunsxfsi(-2.0, 0))
65         return 1;
66     if (test__fixunsxfsi(-2.01, 0))
67         return 1;
68 #endif
69 
70     if (test__fixunsxfsi(0x1.000000p+31, 0x80000000))
71         return 1;
72     if (test__fixunsxfsi(0x1.FFFFFEp+31, 0xFFFFFF00))
73         return 1;
74     if (test__fixunsxfsi(0x1.FFFFFEp+30, 0x7FFFFF80))
75         return 1;
76     if (test__fixunsxfsi(0x1.FFFFFCp+30, 0x7FFFFF00))
77         return 1;
78 
79 #if !TARGET_LIBGCC
80     if (test__fixunsxfsi(-0x1.FFFFFEp+30, 0))
81         return 1;
82     if (test__fixunsxfsi(-0x1.FFFFFCp+30, 0))
83         return 1;
84 #endif
85 
86     if (test__fixunsxfsi(0x1.FFFFFFFEp+31, 0xFFFFFFFF))
87         return 1;
88     if (test__fixunsxfsi(0x1.FFFFFFFC00000p+30, 0x7FFFFFFF))
89         return 1;
90     if (test__fixunsxfsi(0x1.FFFFFFF800000p+30, 0x7FFFFFFE))
91         return 1;
92 
93 #endif
94    return 0;
95 }
96