1// RUN: %clang_builtins %s %librt -o %t && %run %t
2// REQUIRES: int128
3//===-- fixunsdfti_test.c - Test __fixunsdfti -----------------------------===//
4//
5// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
6// See https://llvm.org/LICENSE.txt for license information.
7// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
8//
9//===----------------------------------------------------------------------===//
10//
11// This file tests __fixunsdfti for the compiler_rt library.
12//
13//===----------------------------------------------------------------------===//
14
15#include "int_lib.h"
16#include <stdio.h>
17
18// Returns: convert a to a unsigned long long, rounding toward zero.
19//          Negative values all become zero.
20
21// Assumption: double is a IEEE 64 bit floating point type
22//             tu_int is a 64 bit integral type
23//             value in double is representable in tu_int or is negative
24//                 (no range checking performed)
25
26// seee eeee eeee mmmm mmmm mmmm mmmm mmmm | mmmm mmmm mmmm mmmm mmmm mmmm mmmm mmmm
27
28#ifdef CRT_HAS_128BIT
29
30COMPILER_RT_ABI tu_int __fixunsdfti(double a);
31
32int test__fixunsdfti(double a, tu_int expected)
33{
34    tu_int x = __fixunsdfti(a);
35    if (x != expected)
36    {
37        utwords xt;
38        xt.all = x;
39        utwords expectedt;
40        expectedt.all = expected;
41        printf("error in __fixunsdfti(%A) = 0x%.16llX%.16llX, expected 0x%.16llX%.16llX\n",
42               a, xt.s.high, xt.s.low, expectedt.s.high, expectedt.s.low);
43    }
44    return x != expected;
45}
46
47char assumption_1[sizeof(tu_int) == 2*sizeof(du_int)] = {0};
48char assumption_2[sizeof(su_int)*CHAR_BIT == 32] = {0};
49char assumption_3[sizeof(double)*CHAR_BIT == 64] = {0};
50
51#endif
52
53int main()
54{
55#ifdef CRT_HAS_128BIT
56    if (test__fixunsdfti(0.0, 0))
57        return 1;
58
59    if (test__fixunsdfti(0.5, 0))
60        return 1;
61    if (test__fixunsdfti(0.99, 0))
62        return 1;
63    if (test__fixunsdfti(1.0, 1))
64        return 1;
65    if (test__fixunsdfti(1.5, 1))
66        return 1;
67    if (test__fixunsdfti(1.99, 1))
68        return 1;
69    if (test__fixunsdfti(2.0, 2))
70        return 1;
71    if (test__fixunsdfti(2.01, 2))
72        return 1;
73    if (test__fixunsdfti(-0.5, 0))
74        return 1;
75    if (test__fixunsdfti(-0.99, 0))
76        return 1;
77#if !TARGET_LIBGCC
78    if (test__fixunsdfti(-1.0, 0))  // libgcc ignores "returns 0 for negative input" spec
79        return 1;
80    if (test__fixunsdfti(-1.5, 0))
81        return 1;
82    if (test__fixunsdfti(-1.99, 0))
83        return 1;
84    if (test__fixunsdfti(-2.0, 0))
85        return 1;
86    if (test__fixunsdfti(-2.01, 0))
87        return 1;
88#endif
89
90    if (test__fixunsdfti(0x1.FFFFFEp+62, 0x7FFFFF8000000000LL))
91        return 1;
92    if (test__fixunsdfti(0x1.FFFFFCp+62, 0x7FFFFF0000000000LL))
93        return 1;
94
95#if !TARGET_LIBGCC
96    if (test__fixunsdfti(-0x1.FFFFFEp+62, 0))
97        return 1;
98    if (test__fixunsdfti(-0x1.FFFFFCp+62, 0))
99        return 1;
100#endif
101
102    if (test__fixunsdfti(0x1.FFFFFFFFFFFFFp+63, 0xFFFFFFFFFFFFF800ULL))
103        return 1;
104    if (test__fixunsdfti(0x1.0000000000000p+63, 0x8000000000000000ULL))
105        return 1;
106    if (test__fixunsdfti(0x1.FFFFFFFFFFFFFp+62, 0x7FFFFFFFFFFFFC00ULL))
107        return 1;
108    if (test__fixunsdfti(0x1.FFFFFFFFFFFFEp+62, 0x7FFFFFFFFFFFF800ULL))
109        return 1;
110
111    if (test__fixunsdfti(0x1.FFFFFFFFFFFFFp+127, make_ti(0xFFFFFFFFFFFFF800ULL, 0)))
112        return 1;
113    if (test__fixunsdfti(0x1.0000000000000p+127, make_ti(0x8000000000000000ULL, 0)))
114        return 1;
115    if (test__fixunsdfti(0x1.FFFFFFFFFFFFFp+126, make_ti(0x7FFFFFFFFFFFFC00ULL, 0)))
116        return 1;
117    if (test__fixunsdfti(0x1.FFFFFFFFFFFFEp+126, make_ti(0x7FFFFFFFFFFFF800ULL, 0)))
118        return 1;
119    if (test__fixunsdfti(0x1.0000000000000p+128, make_ti(0xFFFFFFFFFFFFFFFFULL,
120                                                         0xFFFFFFFFFFFFFFFFULL)))
121        return 1;
122
123#if !TARGET_LIBGCC
124    if (test__fixunsdfti(-0x1.FFFFFFFFFFFFFp+62, 0))
125        return 1;
126    if (test__fixunsdfti(-0x1.FFFFFFFFFFFFEp+62, 0))
127        return 1;
128#endif
129
130#endif
131   return 0;
132}
133