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