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