]> CyberLeo.Net >> Repos - FreeBSD/releng/10.0.git/blob - tools/regression/lib/msun/test-nearbyint.c
- Copy stable/10 (r259064) to releng/10.0 as part of the
[FreeBSD/releng/10.0.git] / tools / regression / lib / msun / test-nearbyint.c
1 /*-
2  * Copyright (c) 2010 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  * Tests for nearbyint{,f,l}()
29  *
30  * TODO:
31  * - adapt tests for rint(3)
32  * - tests for harder values (more mantissa bits than float)
33  */
34
35 #include <sys/cdefs.h>
36 __FBSDID("$FreeBSD$");
37
38 #include <assert.h>
39 #include <fenv.h>
40 #include <math.h>
41 #include <stdio.h>
42
43 #include "test-utils.h"
44
45 static int testnum;
46
47 static const int rmodes[] = {
48         FE_TONEAREST, FE_DOWNWARD, FE_UPWARD, FE_TOWARDZERO,
49 };
50
51 /* Make sure we're testing the library, not some broken compiler built-ins. */
52 double (*libnearbyint)(double) = nearbyint;
53 float (*libnearbyintf)(float) = nearbyintf;
54 long double (*libnearbyintl)(long double) = nearbyintl;
55 #define nearbyintf libnearbyintf
56 #define nearbyint libnearbyint
57 #define nearbyintl libnearbyintl
58
59 static const struct {
60         float in;
61         float out[3];   /* one answer per rounding mode except towardzero */
62 } tests[] = {
63 /* input        output (expected) */
64     { 0.0,      { 0.0, 0.0, 0.0 }},
65     { 0.5,      { 0.0, 0.0, 1.0 }},
66     { M_PI,     { 3.0, 3.0, 4.0 }},
67     { 65536.5,  { 65536, 65536, 65537 }},
68     { INFINITY, { INFINITY, INFINITY, INFINITY }},
69     { NAN,      { NAN, NAN, NAN }},
70 };
71
72 static const int ntests = sizeof(tests) / sizeof(tests[0]);
73
74 /* Get the appropriate result for the current rounding mode. */
75 static float
76 get_output(int testindex, int rmodeindex, int negative)
77 {
78         double out;
79
80         if (negative) { /* swap downwards and upwards if input is negative */
81                 if (rmodeindex == 1)
82                         rmodeindex = 2;
83                 else if (rmodeindex == 2)
84                         rmodeindex = 1;
85         }
86         if (rmodeindex == 3) /* FE_TOWARDZERO uses the value for downwards */
87                 rmodeindex = 1;
88         out = tests[testindex].out[rmodeindex];
89         return (negative ? -out : out);
90 }
91
92 static void
93 test_nearby(int testindex)
94 {
95         float in, out;
96         int i;
97
98         for (i = 0; i < sizeof(rmodes) / sizeof(rmodes[0]); i++) {
99                 fesetround(rmodes[i]);
100                 feclearexcept(ALL_STD_EXCEPT);
101
102                 in = tests[testindex].in;
103                 out = get_output(testindex, i, 0);
104                 assert(fpequal(out, libnearbyintf(in)));
105                 assert(fpequal(out, nearbyint(in)));
106                 assert(fpequal(out, nearbyintl(in)));
107                 assert(fetestexcept(ALL_STD_EXCEPT) == 0);
108
109                 in = -tests[testindex].in;
110                 out = get_output(testindex, i, 1);
111                 assert(fpequal(out, nearbyintf(in)));
112                 assert(fpequal(out, nearbyint(in)));
113                 assert(fpequal(out, nearbyintl(in)));
114                 assert(fetestexcept(ALL_STD_EXCEPT) == 0);
115         }
116
117         printf("ok %d\t\t# nearbyint(+%g)\n", testnum++, in);
118 }
119
120 static void
121 test_modf(int testindex)
122 {
123         float in, out;
124         float ipartf, ipart_expected;
125         double ipart;
126         long double ipartl;
127         int i;
128
129         for (i = 0; i < sizeof(rmodes) / sizeof(rmodes[0]); i++) {
130                 fesetround(rmodes[i]);
131                 feclearexcept(ALL_STD_EXCEPT);
132
133                 in = tests[testindex].in;
134                 ipart_expected = tests[testindex].out[1];
135                 out = copysignf(
136                     isinf(ipart_expected) ? 0.0 : in - ipart_expected, in);
137                 ipartl = ipart = ipartf = 42.0;
138
139                 assert(fpequal(out, modff(in, &ipartf)));
140                 assert(fpequal(ipart_expected, ipartf));
141                 assert(fpequal(out, modf(in, &ipart)));
142                 assert(fpequal(ipart_expected, ipart));
143                 assert(fpequal(out, modfl(in, &ipartl)));
144                 assert(fpequal(ipart_expected, ipartl));
145                 assert(fetestexcept(ALL_STD_EXCEPT) == 0);
146
147                 in = -in;
148                 ipart_expected = -ipart_expected;
149                 out = -out;
150                 ipartl = ipart = ipartf = 42.0;
151                 assert(fpequal(out, modff(in, &ipartf)));
152                 assert(fpequal(ipart_expected, ipartf));
153                 assert(fpequal(out, modf(in, &ipart)));
154                 assert(fpequal(ipart_expected, ipart));
155                 assert(fpequal(out, modfl(in, &ipartl)));
156                 assert(fpequal(ipart_expected, ipartl));
157                 assert(fetestexcept(ALL_STD_EXCEPT) == 0);
158         }
159
160         printf("ok %d\t\t# modf(+%g)\n", testnum++, in);
161 }
162
163 int
164 main(int argc, char *argv[])
165 {
166         int i;
167
168         printf("1..%d\n", ntests * 2);
169         testnum = 1;
170         for (i = 0; i < ntests; i++) {
171                 test_nearby(i);
172                 test_modf(i);
173         }
174
175         return (0);
176 }