1// RUN: %clang_builtins %s %librt -o %t && %run %t
2//===-- fixunstfti_test.c - Test __fixunstfti -----------------------------===//
3//
4// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
5// See https://llvm.org/LICENSE.txt for license information.
6// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
7//
8//===----------------------------------------------------------------------===//
9//
10// This file tests __fixunstfti for the compiler_rt library.
11//
12//===----------------------------------------------------------------------===//
13
14#include <stdio.h>
15
16// UNSUPPORTED: mips
17
18#if __LDBL_MANT_DIG__ == 113
19
20#include "fp_test.h"
21#include "int_lib.h"
22
23// Returns: convert a to a unsigned long long, rounding toward zero.
24//          Negative values all become zero.
25
26// Assumption: long double is a 128 bit floating point type
27//             tu_int is a 128 bit integral type
28//             value in long double is representable in tu_int or is negative
29//                 (no range checking performed)
30
31COMPILER_RT_ABI tu_int __fixunstfti(long double a);
32
33int test__fixunstfti(long double a, tu_int expected)
34{
35    tu_int x = __fixunstfti(a);
36    if (x != expected)
37    {
38        twords xt;
39        xt.all = x;
40
41        twords expectedt;
42        expectedt.all = expected;
43
44        printf("error in __fixunstfti(%.20Lf) = 0x%.16llX%.16llX, "
45               "expected 0x%.16llX%.16llX\n",
46               a, xt.s.high, xt.s.low, expectedt.s.high, expectedt.s.low);
47    }
48    return x != expected;
49}
50
51char assumption_1[sizeof(tu_int) == 4*sizeof(su_int)] = {0};
52char assumption_2[sizeof(tu_int)*CHAR_BIT == 128] = {0};
53char assumption_3[sizeof(long double)*CHAR_BIT == 128] = {0};
54
55#endif
56
57int main()
58{
59#if __LDBL_MANT_DIG__ == 113
60    if (test__fixunstfti(makeInf128(), make_ti(0xffffffffffffffffLL,
61                                               0xffffffffffffffffLL)))
62        return 1;
63
64    if (test__fixunstfti(0.0, 0))
65        return 1;
66
67    if (test__fixunstfti(0.5, 0))
68        return 1;
69    if (test__fixunstfti(0.99, 0))
70        return 1;
71    if (test__fixunstfti(1.0, 1))
72        return 1;
73    if (test__fixunstfti(1.5, 1))
74        return 1;
75    if (test__fixunstfti(1.99, 1))
76        return 1;
77    if (test__fixunstfti(2.0, 2))
78        return 1;
79    if (test__fixunstfti(2.01, 2))
80        return 1;
81    if (test__fixunstfti(-0.01, 0))
82        return 1;
83    if (test__fixunstfti(-0.99, 0))
84        return 1;
85
86    if (test__fixunstfti(0x1.p+128, make_ti(0xffffffffffffffffLL,
87                                            0xffffffffffffffffLL)))
88        return 1;
89
90    if (test__fixunstfti(0x1.FFFFFEp+126, make_ti(0x7fffff8000000000LL, 0x0)))
91        return 1;
92    if (test__fixunstfti(0x1.FFFFFEp+127, make_ti(0xffffff0000000000LL, 0x0)))
93        return 1;
94    if (test__fixunstfti(0x1.FFFFFEp+128, make_ti(0xffffffffffffffffLL,
95                                                  0xffffffffffffffffLL)))
96        return 1;
97    if (test__fixunstfti(0x1.FFFFFEp+129, make_ti(0xffffffffffffffffLL,
98                                                  0xffffffffffffffffLL)))
99        return 1;
100
101#else
102    printf("skipped\n");
103#endif
104   return 0;
105}
106