1// RUN: %clang_builtins %s %librt -o %t && %run %t
2//===-- fixunsdfdi_test.c - Test __fixunsdfdi -----------------------------===//
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 __fixunsdfdi for the compiler_rt library.
11//
12//===----------------------------------------------------------------------===//
13
14#include "int_lib.h"
15#include <stdio.h>
16
17// Returns: convert a to a unsigned long long, rounding toward zero.
18//          Negative values all become zero.
19
20// Assumption: double is a IEEE 64 bit floating point type
21//             du_int is a 64 bit integral type
22//             value in double is representable in du_int or is negative
23//                 (no range checking performed)
24
25// seee eeee eeee mmmm mmmm mmmm mmmm mmmm | mmmm mmmm mmmm mmmm mmmm mmmm mmmm mmmm
26
27COMPILER_RT_ABI du_int __fixunsdfdi(double a);
28
29int test__fixunsdfdi(double a, du_int expected)
30{
31    du_int x = __fixunsdfdi(a);
32    if (x != expected)
33        printf("error in __fixunsdfdi(%A) = %llX, expected %llX\n", a, x, expected);
34    return x != expected;
35}
36
37char assumption_1[sizeof(du_int) == 2*sizeof(su_int)] = {0};
38char assumption_2[sizeof(su_int)*CHAR_BIT == 32] = {0};
39char assumption_3[sizeof(double)*CHAR_BIT == 64] = {0};
40
41int main()
42{
43    if (test__fixunsdfdi(0.0, 0))
44        return 1;
45
46    if (test__fixunsdfdi(0.5, 0))
47        return 1;
48    if (test__fixunsdfdi(0.99, 0))
49        return 1;
50    if (test__fixunsdfdi(1.0, 1))
51        return 1;
52    if (test__fixunsdfdi(1.5, 1))
53        return 1;
54    if (test__fixunsdfdi(1.99, 1))
55        return 1;
56    if (test__fixunsdfdi(2.0, 2))
57        return 1;
58    if (test__fixunsdfdi(2.01, 2))
59        return 1;
60    if (test__fixunsdfdi(-0.5, 0))
61        return 1;
62    if (test__fixunsdfdi(-0.99, 0))
63        return 1;
64#if !TARGET_LIBGCC
65    if (test__fixunsdfdi(-1.0, 0))  // libgcc ignores "returns 0 for negative input" spec
66        return 1;
67    if (test__fixunsdfdi(-1.5, 0))
68        return 1;
69    if (test__fixunsdfdi(-1.99, 0))
70        return 1;
71    if (test__fixunsdfdi(-2.0, 0))
72        return 1;
73    if (test__fixunsdfdi(-2.01, 0))
74        return 1;
75#endif
76
77    if (test__fixunsdfdi(0x1.FFFFFEp+62, 0x7FFFFF8000000000LL))
78        return 1;
79    if (test__fixunsdfdi(0x1.FFFFFCp+62, 0x7FFFFF0000000000LL))
80        return 1;
81
82#if !TARGET_LIBGCC
83    if (test__fixunsdfdi(-0x1.FFFFFEp+62, 0))
84        return 1;
85    if (test__fixunsdfdi(-0x1.FFFFFCp+62, 0))
86        return 1;
87#endif
88
89    if (test__fixunsdfdi(0x1.FFFFFFFFFFFFFp+63, 0xFFFFFFFFFFFFF800LL))
90        return 1;
91    if (test__fixunsdfdi(0x1.0000000000000p+63, 0x8000000000000000LL))
92        return 1;
93    if (test__fixunsdfdi(0x1.FFFFFFFFFFFFFp+62, 0x7FFFFFFFFFFFFC00LL))
94        return 1;
95    if (test__fixunsdfdi(0x1.FFFFFFFFFFFFEp+62, 0x7FFFFFFFFFFFF800LL))
96        return 1;
97
98#if !TARGET_LIBGCC
99    if (test__fixunsdfdi(-0x1.FFFFFFFFFFFFFp+62, 0))
100        return 1;
101    if (test__fixunsdfdi(-0x1.FFFFFFFFFFFFEp+62, 0))
102        return 1;
103#endif
104
105   return 0;
106}
107