1 /* $OpenBSD: lrint_test.c,v 1.5 2021/12/13 18:04:28 deraadt Exp $ */
2 /*-
3 * Copyright (c) 2005-2008 David Schultz <das@FreeBSD.org>
4 * All rights reserved.
5 *
6 * Redistribution and use in source and binary forms, with or without
7 * modification, are permitted provided that the following conditions
8 * are met:
9 * 1. Redistributions of source code must retain the above copyright
10 * notice, this list of conditions and the following disclaimer.
11 * 2. Redistributions in binary form must reproduce the above copyright
12 * notice, this list of conditions and the following disclaimer in the
13 * documentation and/or other materials provided with the distribution.
14 *
15 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
16 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
17 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
18 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
19 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
20 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
21 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
22 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
23 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
24 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
25 * SUCH DAMAGE.
26 */
27
28 /*
29 * Test for lrint(), lrintf(), llrint(), and llrintf().
30 */
31
32 #include <fenv.h>
33 #include <limits.h>
34 #include <math.h>
35 #include <stdio.h>
36
37 #include "test-utils.h"
38
39 #define test(func, x, result, excepts) do { \
40 ATF_CHECK(feclearexcept(FE_ALL_EXCEPT) == 0); \
41 long long _r = (func)(x); \
42 ATF_CHECK_MSG(_r == (result) || fetestexcept(FE_INVALID), \
43 #func "(%Lg) returned %lld, expected %lld", (long double)x, _r, \
44 (long long)(result)); \
45 CHECK_FP_EXCEPTIONS_MSG(excepts, FE_ALL_EXCEPT & ALL_STD_EXCEPT, \
46 "for %s(%s)", #func, #x); \
47 } while (0)
48
49 #define testall(x, result, excepts) do { \
50 test(lrint, x, result, excepts); \
51 test(lrintf, x, result, excepts); \
52 test(lrintl, x, result, excepts); \
53 test(llrint, x, result, excepts); \
54 test(llrintf, x, result, excepts); \
55 test(llrintl, x, result, excepts); \
56 } while (0)
57
58 #define IGNORE 0
59
60 #pragma STDC FENV_ACCESS ON
61
62 static void
run_tests(void)63 run_tests(void)
64 {
65 ATF_REQUIRE_EQ(0, fesetround(FE_DOWNWARD));
66 testall(0.75, 0, FE_INEXACT);
67 testall(-0.5, -1, FE_INEXACT);
68
69 ATF_REQUIRE_EQ(0, fesetround(FE_TONEAREST));
70 testall(0.0, 0, 0);
71 testall(0.25, 0, FE_INEXACT);
72 testall(0.5, 0, FE_INEXACT);
73 testall(-2.5, -2, FE_INEXACT);
74 testall(1.0, 1, 0);
75 testall(0x12345000p0, 0x12345000, 0);
76 testall(0x1234.fp0, 0x1235, FE_INEXACT);
77 testall(INFINITY, IGNORE, FE_INVALID);
78 testall(NAN, IGNORE, FE_INVALID);
79
80 #if (LONG_MAX == 0x7fffffffl)
81 ATF_REQUIRE_EQ(0, fesetround(FE_UPWARD));
82 test(lrint, 0x7fffffff.8p0, IGNORE, FE_INVALID);
83 test(lrint, -0x80000000.4p0, (long)-0x80000000l, FE_INEXACT);
84
85 ATF_REQUIRE_EQ(0, fesetround(FE_DOWNWARD));
86 test(lrint, -0x80000000.8p0, IGNORE, FE_INVALID);
87 test(lrint, 0x80000000.0p0, IGNORE, FE_INVALID);
88 test(lrint, 0x7fffffff.4p0, 0x7fffffffl, FE_INEXACT);
89 test(lrintf, 0x80000000.0p0f, IGNORE, FE_INVALID);
90 test(lrintf, 0x7fffff80.0p0f, 0x7fffff80l, 0);
91
92 ATF_REQUIRE_EQ(0, fesetround(FE_TOWARDZERO));
93 test(lrint, 0x7fffffff.8p0, 0x7fffffffl, FE_INEXACT);
94 test(lrint, -0x80000000.8p0, -0x80000000l, FE_INEXACT);
95 test(lrint, 0x80000000.0p0, IGNORE, FE_INVALID);
96 test(lrintf, 0x80000000.0p0f, IGNORE, FE_INVALID);
97 test(lrintf, 0x7fffff80.0p0f, 0x7fffff80l, 0);
98 #elif (LONG_MAX == 0x7fffffffffffffffll)
99 ATF_REQUIRE_EQ(0, fesetround(FE_TONEAREST));
100 test(lrint, 0x8000000000000000.0p0, IGNORE, FE_INVALID);
101 test(lrintf, 0x8000000000000000.0p0f, IGNORE, FE_INVALID);
102 test(lrint, 0x7ffffffffffffc00.0p0, 0x7ffffffffffffc00l, 0);
103 test(lrintf, 0x7fffff8000000000.0p0f, 0x7fffff8000000000l, 0);
104 test(lrint, -0x8000000000000800.0p0, IGNORE, FE_INVALID);
105 test(lrintf, -0x8000010000000000.0p0f, IGNORE, FE_INVALID);
106 test(lrint, -0x8000000000000000.0p0, (long long)-0x8000000000000000ul, 0);
107 test(lrintf, -0x8000000000000000.0p0f, (long long)-0x8000000000000000ul, 0);
108 #else
109 #error "Unsupported long size"
110 #endif
111
112 #if (LLONG_MAX == 0x7fffffffffffffffLL)
113 ATF_REQUIRE_EQ(0, fesetround(FE_TONEAREST));
114 test(llrint, 0x8000000000000000.0p0, IGNORE, FE_INVALID);
115 test(llrintf, 0x8000000000000000.0p0f, IGNORE, FE_INVALID);
116 test(llrint, 0x7ffffffffffffc00.0p0, 0x7ffffffffffffc00ll, 0);
117 test(llrintf, 0x7fffff8000000000.0p0f, 0x7fffff8000000000ll, 0);
118 test(llrint, -0x8000000000000800.0p0, IGNORE, FE_INVALID);
119 test(llrintf, -0x8000010000000000.0p0f, IGNORE, FE_INVALID);
120 test(llrint, -0x8000000000000000.0p0, (long long)-0x8000000000000000ull, 0);
121 test(llrintf, -0x8000000000000000.0p0f, (long long)-0x8000000000000000ull, 0);
122 #else
123 #error "Unsupported long long size"
124 #endif
125 }
126
127 ATF_TC_WITHOUT_HEAD(lrint);
ATF_TC_BODY(lrint,tc)128 ATF_TC_BODY(lrint, tc)
129 {
130 run_tests();
131 #ifndef __OpenBSD__
132 #ifdef __i386__
133 fpsetprec(FP_PE);
134 run_tests();
135 #endif
136 #endif
137 }
138
ATF_TP_ADD_TCS(tp)139 ATF_TP_ADD_TCS(tp)
140 {
141 ATF_TP_ADD_TC(tp, lrint);
142 return (atf_no_error());
143 }
144