1b3a6901e66f55b35aa9e01bcb24134e6a65ea004Daniel Dunbar//===-- fixdfti_test.c - Test __fixdfti -----------------------------------===//
2b3a6901e66f55b35aa9e01bcb24134e6a65ea004Daniel Dunbar//
3b3a6901e66f55b35aa9e01bcb24134e6a65ea004Daniel Dunbar//                     The LLVM Compiler Infrastructure
4b3a6901e66f55b35aa9e01bcb24134e6a65ea004Daniel Dunbar//
59ad441ffec97db647fee3725b3424284fb913e14Howard Hinnant// This file is dual licensed under the MIT and the University of Illinois Open
69ad441ffec97db647fee3725b3424284fb913e14Howard Hinnant// Source Licenses. See LICENSE.TXT for details.
7b3a6901e66f55b35aa9e01bcb24134e6a65ea004Daniel Dunbar//
8b3a6901e66f55b35aa9e01bcb24134e6a65ea004Daniel Dunbar//===----------------------------------------------------------------------===//
9b3a6901e66f55b35aa9e01bcb24134e6a65ea004Daniel Dunbar//
10b3a6901e66f55b35aa9e01bcb24134e6a65ea004Daniel Dunbar// This file tests __fixdfti for the compiler_rt library.
11b3a6901e66f55b35aa9e01bcb24134e6a65ea004Daniel Dunbar//
12b3a6901e66f55b35aa9e01bcb24134e6a65ea004Daniel Dunbar//===----------------------------------------------------------------------===//
13b3a6901e66f55b35aa9e01bcb24134e6a65ea004Daniel Dunbar
14b3a6901e66f55b35aa9e01bcb24134e6a65ea004Daniel Dunbar#include "int_lib.h"
15b3a6901e66f55b35aa9e01bcb24134e6a65ea004Daniel Dunbar#include <stdio.h>
16b3a6901e66f55b35aa9e01bcb24134e6a65ea004Daniel Dunbar
172d1fdb26e458c4ddc04155c1d421bced3ba90cd0Stephen Hines#ifdef CRT_HAS_128BIT
182d1fdb26e458c4ddc04155c1d421bced3ba90cd0Stephen Hines
19b3a6901e66f55b35aa9e01bcb24134e6a65ea004Daniel Dunbar// Returns: convert a to a signed long long, rounding toward zero.
20b3a6901e66f55b35aa9e01bcb24134e6a65ea004Daniel Dunbar
21b3a6901e66f55b35aa9e01bcb24134e6a65ea004Daniel Dunbar// Assumption: double is a IEEE 64 bit floating point type
22b3a6901e66f55b35aa9e01bcb24134e6a65ea004Daniel Dunbar//             su_int is a 32 bit integral type
23b3a6901e66f55b35aa9e01bcb24134e6a65ea004Daniel Dunbar//             value in double is representable in ti_int (no range checking performed)
24b3a6901e66f55b35aa9e01bcb24134e6a65ea004Daniel Dunbar
25b3a6901e66f55b35aa9e01bcb24134e6a65ea004Daniel Dunbar// seee eeee eeee mmmm mmmm mmmm mmmm mmmm | mmmm mmmm mmmm mmmm mmmm mmmm mmmm mmmm
26b3a6901e66f55b35aa9e01bcb24134e6a65ea004Daniel Dunbar
27cdce50bda3603770cc4ef80cbb613c78b8e47a17Pirama Arumuga NainarCOMPILER_RT_ABI ti_int __fixdfti(double a);
28b3a6901e66f55b35aa9e01bcb24134e6a65ea004Daniel Dunbar
29b3a6901e66f55b35aa9e01bcb24134e6a65ea004Daniel Dunbarint test__fixdfti(double a, ti_int expected)
30b3a6901e66f55b35aa9e01bcb24134e6a65ea004Daniel Dunbar{
31b3a6901e66f55b35aa9e01bcb24134e6a65ea004Daniel Dunbar    ti_int x = __fixdfti(a);
32b3a6901e66f55b35aa9e01bcb24134e6a65ea004Daniel Dunbar    if (x != expected)
33b3a6901e66f55b35aa9e01bcb24134e6a65ea004Daniel Dunbar    {
34b3a6901e66f55b35aa9e01bcb24134e6a65ea004Daniel Dunbar        twords xt;
35b3a6901e66f55b35aa9e01bcb24134e6a65ea004Daniel Dunbar        xt.all = x;
36b3a6901e66f55b35aa9e01bcb24134e6a65ea004Daniel Dunbar        twords expectedt;
37b3a6901e66f55b35aa9e01bcb24134e6a65ea004Daniel Dunbar        expectedt.all = expected;
38b3a6901e66f55b35aa9e01bcb24134e6a65ea004Daniel Dunbar        printf("error in __fixdfti(%A) = 0x%.16llX%.16llX, expected 0x%.16llX%.16llX\n",
39cff5248a12a36a7225b707b23ac088f5ba214f8aDaniel Dunbar        a, xt.s.high, xt.s.low, expectedt.s.high, expectedt.s.low);
40b3a6901e66f55b35aa9e01bcb24134e6a65ea004Daniel Dunbar    }
41b3a6901e66f55b35aa9e01bcb24134e6a65ea004Daniel Dunbar    return x != expected;
42b3a6901e66f55b35aa9e01bcb24134e6a65ea004Daniel Dunbar}
43b3a6901e66f55b35aa9e01bcb24134e6a65ea004Daniel Dunbar
44b3a6901e66f55b35aa9e01bcb24134e6a65ea004Daniel Dunbarchar assumption_1[sizeof(ti_int) == 2*sizeof(di_int)] = {0};
45b3a6901e66f55b35aa9e01bcb24134e6a65ea004Daniel Dunbarchar assumption_2[sizeof(su_int)*CHAR_BIT == 32] = {0};
46b3a6901e66f55b35aa9e01bcb24134e6a65ea004Daniel Dunbarchar assumption_3[sizeof(double)*CHAR_BIT == 64] = {0};
47b3a6901e66f55b35aa9e01bcb24134e6a65ea004Daniel Dunbar
48b3a6901e66f55b35aa9e01bcb24134e6a65ea004Daniel Dunbar#endif
49b3a6901e66f55b35aa9e01bcb24134e6a65ea004Daniel Dunbar
50b3a6901e66f55b35aa9e01bcb24134e6a65ea004Daniel Dunbarint main()
51b3a6901e66f55b35aa9e01bcb24134e6a65ea004Daniel Dunbar{
522d1fdb26e458c4ddc04155c1d421bced3ba90cd0Stephen Hines#ifdef CRT_HAS_128BIT
53b3a6901e66f55b35aa9e01bcb24134e6a65ea004Daniel Dunbar    if (test__fixdfti(0.0, 0))
54b3a6901e66f55b35aa9e01bcb24134e6a65ea004Daniel Dunbar        return 1;
55b3a6901e66f55b35aa9e01bcb24134e6a65ea004Daniel Dunbar
56b3a6901e66f55b35aa9e01bcb24134e6a65ea004Daniel Dunbar    if (test__fixdfti(0.5, 0))
57b3a6901e66f55b35aa9e01bcb24134e6a65ea004Daniel Dunbar        return 1;
58b3a6901e66f55b35aa9e01bcb24134e6a65ea004Daniel Dunbar    if (test__fixdfti(0.99, 0))
59b3a6901e66f55b35aa9e01bcb24134e6a65ea004Daniel Dunbar        return 1;
60b3a6901e66f55b35aa9e01bcb24134e6a65ea004Daniel Dunbar    if (test__fixdfti(1.0, 1))
61b3a6901e66f55b35aa9e01bcb24134e6a65ea004Daniel Dunbar        return 1;
62b3a6901e66f55b35aa9e01bcb24134e6a65ea004Daniel Dunbar    if (test__fixdfti(1.5, 1))
63b3a6901e66f55b35aa9e01bcb24134e6a65ea004Daniel Dunbar        return 1;
64b3a6901e66f55b35aa9e01bcb24134e6a65ea004Daniel Dunbar    if (test__fixdfti(1.99, 1))
65b3a6901e66f55b35aa9e01bcb24134e6a65ea004Daniel Dunbar        return 1;
66b3a6901e66f55b35aa9e01bcb24134e6a65ea004Daniel Dunbar    if (test__fixdfti(2.0, 2))
67b3a6901e66f55b35aa9e01bcb24134e6a65ea004Daniel Dunbar        return 1;
68b3a6901e66f55b35aa9e01bcb24134e6a65ea004Daniel Dunbar    if (test__fixdfti(2.01, 2))
69b3a6901e66f55b35aa9e01bcb24134e6a65ea004Daniel Dunbar        return 1;
70b3a6901e66f55b35aa9e01bcb24134e6a65ea004Daniel Dunbar    if (test__fixdfti(-0.5, 0))
71b3a6901e66f55b35aa9e01bcb24134e6a65ea004Daniel Dunbar        return 1;
72b3a6901e66f55b35aa9e01bcb24134e6a65ea004Daniel Dunbar    if (test__fixdfti(-0.99, 0))
73b3a6901e66f55b35aa9e01bcb24134e6a65ea004Daniel Dunbar        return 1;
74b3a6901e66f55b35aa9e01bcb24134e6a65ea004Daniel Dunbar    if (test__fixdfti(-1.0, -1))
75b3a6901e66f55b35aa9e01bcb24134e6a65ea004Daniel Dunbar        return 1;
76b3a6901e66f55b35aa9e01bcb24134e6a65ea004Daniel Dunbar    if (test__fixdfti(-1.5, -1))
77b3a6901e66f55b35aa9e01bcb24134e6a65ea004Daniel Dunbar        return 1;
78b3a6901e66f55b35aa9e01bcb24134e6a65ea004Daniel Dunbar    if (test__fixdfti(-1.99, -1))
79b3a6901e66f55b35aa9e01bcb24134e6a65ea004Daniel Dunbar        return 1;
80b3a6901e66f55b35aa9e01bcb24134e6a65ea004Daniel Dunbar    if (test__fixdfti(-2.0, -2))
81b3a6901e66f55b35aa9e01bcb24134e6a65ea004Daniel Dunbar        return 1;
82b3a6901e66f55b35aa9e01bcb24134e6a65ea004Daniel Dunbar    if (test__fixdfti(-2.01, -2))
83b3a6901e66f55b35aa9e01bcb24134e6a65ea004Daniel Dunbar        return 1;
84b3a6901e66f55b35aa9e01bcb24134e6a65ea004Daniel Dunbar
85b3a6901e66f55b35aa9e01bcb24134e6a65ea004Daniel Dunbar    if (test__fixdfti(0x1.FFFFFEp+62, 0x7FFFFF8000000000LL))
86b3a6901e66f55b35aa9e01bcb24134e6a65ea004Daniel Dunbar        return 1;
87b3a6901e66f55b35aa9e01bcb24134e6a65ea004Daniel Dunbar    if (test__fixdfti(0x1.FFFFFCp+62, 0x7FFFFF0000000000LL))
88b3a6901e66f55b35aa9e01bcb24134e6a65ea004Daniel Dunbar        return 1;
89b3a6901e66f55b35aa9e01bcb24134e6a65ea004Daniel Dunbar
90b3a6901e66f55b35aa9e01bcb24134e6a65ea004Daniel Dunbar    if (test__fixdfti(-0x1.FFFFFEp+62, make_ti(0xFFFFFFFFFFFFFFFFLL,
91b3a6901e66f55b35aa9e01bcb24134e6a65ea004Daniel Dunbar                                               0x8000008000000000LL)))
92b3a6901e66f55b35aa9e01bcb24134e6a65ea004Daniel Dunbar        return 1;
93b3a6901e66f55b35aa9e01bcb24134e6a65ea004Daniel Dunbar    if (test__fixdfti(-0x1.FFFFFCp+62, make_ti(0xFFFFFFFFFFFFFFFFLL,
94b3a6901e66f55b35aa9e01bcb24134e6a65ea004Daniel Dunbar                                               0x8000010000000000LL)))
95b3a6901e66f55b35aa9e01bcb24134e6a65ea004Daniel Dunbar        return 1;
96b3a6901e66f55b35aa9e01bcb24134e6a65ea004Daniel Dunbar
97b3a6901e66f55b35aa9e01bcb24134e6a65ea004Daniel Dunbar    if (test__fixdfti(0x1.FFFFFFFFFFFFFp+62, 0x7FFFFFFFFFFFFC00LL))
98b3a6901e66f55b35aa9e01bcb24134e6a65ea004Daniel Dunbar        return 1;
99b3a6901e66f55b35aa9e01bcb24134e6a65ea004Daniel Dunbar    if (test__fixdfti(0x1.FFFFFFFFFFFFEp+62, 0x7FFFFFFFFFFFF800LL))
100b3a6901e66f55b35aa9e01bcb24134e6a65ea004Daniel Dunbar        return 1;
101b3a6901e66f55b35aa9e01bcb24134e6a65ea004Daniel Dunbar
102b3a6901e66f55b35aa9e01bcb24134e6a65ea004Daniel Dunbar    if (test__fixdfti(-0x1.FFFFFFFFFFFFFp+62, make_ti(0xFFFFFFFFFFFFFFFFLL,
103b3a6901e66f55b35aa9e01bcb24134e6a65ea004Daniel Dunbar                                                      0x8000000000000400LL)))
104b3a6901e66f55b35aa9e01bcb24134e6a65ea004Daniel Dunbar        return 1;
105b3a6901e66f55b35aa9e01bcb24134e6a65ea004Daniel Dunbar    if (test__fixdfti(-0x1.FFFFFFFFFFFFEp+62, make_ti(0xFFFFFFFFFFFFFFFFLL,
106b3a6901e66f55b35aa9e01bcb24134e6a65ea004Daniel Dunbar                                                      0x8000000000000800LL)))
107b3a6901e66f55b35aa9e01bcb24134e6a65ea004Daniel Dunbar        return 1;
108b3a6901e66f55b35aa9e01bcb24134e6a65ea004Daniel Dunbar
109b3a6901e66f55b35aa9e01bcb24134e6a65ea004Daniel Dunbar    if (test__fixdfti(0x1.FFFFFFFFFFFFFp+126, make_ti(0x7FFFFFFFFFFFFC00LL, 0)))
110b3a6901e66f55b35aa9e01bcb24134e6a65ea004Daniel Dunbar        return 1;
111b3a6901e66f55b35aa9e01bcb24134e6a65ea004Daniel Dunbar    if (test__fixdfti(0x1.FFFFFFFFFFFFEp+126, make_ti(0x7FFFFFFFFFFFF800LL, 0)))
112b3a6901e66f55b35aa9e01bcb24134e6a65ea004Daniel Dunbar        return 1;
113b3a6901e66f55b35aa9e01bcb24134e6a65ea004Daniel Dunbar
114b3a6901e66f55b35aa9e01bcb24134e6a65ea004Daniel Dunbar    if (test__fixdfti(-0x1.FFFFFFFFFFFFFp+126, make_ti(0x8000000000000400LL, 0)))
115b3a6901e66f55b35aa9e01bcb24134e6a65ea004Daniel Dunbar        return 1;
116b3a6901e66f55b35aa9e01bcb24134e6a65ea004Daniel Dunbar    if (test__fixdfti(-0x1.FFFFFFFFFFFFEp+126, make_ti(0x8000000000000800LL, 0)))
117b3a6901e66f55b35aa9e01bcb24134e6a65ea004Daniel Dunbar        return 1;
118b3a6901e66f55b35aa9e01bcb24134e6a65ea004Daniel Dunbar
1197482815716cd9b87931d82dca7298fc3c707229fJoerg Sonnenberger#else
1207482815716cd9b87931d82dca7298fc3c707229fJoerg Sonnenberger    printf("skipped\n");
121b3a6901e66f55b35aa9e01bcb24134e6a65ea004Daniel Dunbar#endif
122b3a6901e66f55b35aa9e01bcb24134e6a65ea004Daniel Dunbar   return 0;
123b3a6901e66f55b35aa9e01bcb24134e6a65ea004Daniel Dunbar}
124