1 /* $NetBSD: testfloat.c,v 1.6 2008/04/28 20:23:04 martin Exp $ */
3 /* This is a derivative work. */
6 * Copyright (c) 2001 The NetBSD Foundation, Inc.
9 * This code is derived from software contributed to The NetBSD Foundation
12 * Redistribution and use in source and binary forms, with or without
13 * modification, are permitted provided that the following conditions
15 * 1. Redistributions of source code must retain the above copyright
16 * notice, this list of conditions and the following disclaimer.
17 * 2. Redistributions in binary form must reproduce the above copyright
18 * notice, this list of conditions and the following disclaimer in the
19 * documentation and/or other materials provided with the distribution.
21 * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
22 * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
23 * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
24 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
25 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
26 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
27 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
28 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
29 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
30 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
31 * POSSIBILITY OF SUCH DAMAGE.
35 ===============================================================================
37 This C source file is part of TestFloat, Release 2a, a package of programs
38 for testing the correctness of floating-point arithmetic complying to the
39 IEC/IEEE Standard for Floating-Point.
41 Written by John R. Hauser. More information is available through the Web
42 page `http://HTTP.CS.Berkeley.EDU/~jhauser/arithmetic/TestFloat.html'.
44 THIS SOFTWARE IS DISTRIBUTED AS IS, FOR FREE. Although reasonable effort
45 has been made to avoid it, THIS SOFTWARE MAY CONTAIN FAULTS THAT WILL AT
46 TIMES RESULT IN INCORRECT BEHAVIOR. USE OF THIS SOFTWARE IS RESTRICTED TO
47 PERSONS AND ORGANIZATIONS WHO CAN AND WILL TAKE FULL RESPONSIBILITY FOR ANY
48 AND ALL LOSSES, COSTS, OR OTHER PROBLEMS ARISING FROM ITS USE.
50 Derivative works are acceptable, even for commercial purposes, so long as
51 (1) they include prominent notice that the work is derivative, and (2) they
52 include prominent notice akin to these four paragraphs for those parts of
53 this code that are retained.
55 ===============================================================================
58 #include <sys/cdefs.h>
60 __COPYRIGHT("@(#) Copyright (c) 2001\
61 The NetBSD Foundation, inc. All rights reserved.");
62 __RCSID("$NetBSD: testfloat.c,v 1.6 2008/04/28 20:23:04 martin Exp $");
70 #include "softfloat.h"
71 #include "testCases.h"
72 #include "testLoops.h"
73 #include "systflags.h"
74 #include "testFunction.h"
76 static void catchSIGINT( int signalCode
)
79 if ( stop
) exit( EXIT_FAILURE
);
85 main( int argc
, char **argv
)
88 flag functionArgument
;
90 int8 operands
, roundingPrecision
, roundingMode
;
92 fail_programName
= "testfloat";
93 if ( argc
<= 1 ) goto writeHelpMessage
;
94 testCases_setLevel( 1 );
100 trueFlagsPtr
= &float_exception_flags
;
101 testFlagsFunctionPtr
= syst_float_flags_clear
;
102 tininessModeName
= 0;
103 functionArgument
= FALSE
;
106 roundingPrecision
= 0;
110 while ( argc
&& ( argPtr
= argv
[ 0 ] ) ) {
111 if ( argPtr
[ 0 ] == '-' ) ++argPtr
;
112 if ( strcmp( argPtr
, "help" ) == 0 ) {
115 "testfloat [<option>...] <function>\n"
116 " <option>: (* is default)\n"
117 " -help --Write this message and exit.\n"
118 " -list --List all testable functions and exit.\n"
119 " -level <num> --Testing level <num> (1 or 2).\n"
121 " -errors <num> --Stop each function test after <num> errors.\n"
123 " -errorstop --Exit after first function with any error.\n"
124 " -forever --Test one function repeatedly (implies `-level 2').\n"
125 " -checkNaNs --Check for bitwise correctness of NaN results.\n"
127 " -precision32 --Only test rounding precision equivalent to float32.\n"
128 " -precision64 --Only test rounding precision equivalent to float64.\n"
129 " -precision80 --Only test maximum rounding precision.\n"
131 " -nearesteven --Only test rounding to nearest/even.\n"
132 " -tozero --Only test rounding to zero.\n"
133 " -down --Only test rounding down.\n"
134 " -up --Only test rounding up.\n"
135 " -tininessbefore --Underflow tininess detected before rounding.\n"
136 " -tininessafter --Underflow tininess detected after rounding.\n"
138 " int32_to_<float> <float>_add <float>_eq\n"
139 " <float>_to_int32 <float>_sub <float>_le\n"
140 " <float>_to_int32_round_to_zero <float>_mul <float>_lt\n"
142 " int64_to_<float> <float>_div <float>_eq_signaling\n"
143 " <float>_to_int64 <float>_rem <float>_le_quiet\n"
144 " <float>_to_int64_round_to_zero <float>_lt_quiet\n"
145 " <float>_to_<float>\n"
146 " <float>_round_to_int\n"
149 " <float>_to_<float> <float>_div <float>_eq_signaling\n"
150 " <float>_round_to_int <float>_rem <float>_le_quiet\n"
151 " <float>_sqrt <float>_lt_quiet\n"
153 " -all1 --All 1-operand functions.\n"
154 " -all2 --All 2-operand functions.\n"
155 " -all --All functions.\n"
157 " float32 --Single precision.\n"
158 " float64 --Double precision.\n"
160 " floatx80 --Extended double precision.\n"
163 " float128 --Quadruple precision.\n"
170 else if ( strcmp( argPtr
, "list" ) == 0 ) {
171 for ( functionCode
= 1;
172 functionCode
< NUM_FUNCTIONS
;
175 if ( functionExists
[ functionCode
] ) {
176 puts( functions
[ functionCode
].name
);
181 else if ( strcmp( argPtr
, "level" ) == 0 ) {
182 if ( argc
< 2 ) goto optionError
;
183 testCases_setLevel( atoi( argv
[ 1 ] ) );
187 else if ( strcmp( argPtr
, "level1" ) == 0 ) {
188 testCases_setLevel( 1 );
190 else if ( strcmp( argPtr
, "level2" ) == 0 ) {
191 testCases_setLevel( 2 );
193 else if ( strcmp( argPtr
, "errors" ) == 0 ) {
196 fail( "`%s' option requires numeric argument", argv
[ 0 ] );
198 maxErrorCount
= atoi( argv
[ 1 ] );
202 else if ( strcmp( argPtr
, "errorstop" ) == 0 ) {
205 else if ( strcmp( argPtr
, "forever" ) == 0 ) {
206 testCases_setLevel( 2 );
209 else if ( ( strcmp( argPtr
, "checkNaNs" ) == 0 )
210 || ( strcmp( argPtr
, "checknans" ) == 0 ) ) {
214 else if ( strcmp( argPtr
, "precision32" ) == 0 ) {
215 roundingPrecision
= 32;
217 else if ( strcmp( argPtr
, "precision64" ) == 0 ) {
218 roundingPrecision
= 64;
220 else if ( strcmp( argPtr
, "precision80" ) == 0 ) {
221 roundingPrecision
= 80;
224 else if ( ( strcmp( argPtr
, "nearesteven" ) == 0 )
225 || ( strcmp( argPtr
, "nearest_even" ) == 0 ) ) {
226 roundingMode
= ROUND_NEAREST_EVEN
;
228 else if ( ( strcmp( argPtr
, "tozero" ) == 0 )
229 || ( strcmp( argPtr
, "to_zero" ) == 0 ) ) {
230 roundingMode
= ROUND_TO_ZERO
;
232 else if ( strcmp( argPtr
, "down" ) == 0 ) {
233 roundingMode
= ROUND_DOWN
;
235 else if ( strcmp( argPtr
, "up" ) == 0 ) {
236 roundingMode
= ROUND_UP
;
238 else if ( strcmp( argPtr
, "tininessbefore" ) == 0 ) {
239 float_detect_tininess
= float_tininess_before_rounding
;
241 else if ( strcmp( argPtr
, "tininessafter" ) == 0 ) {
242 float_detect_tininess
= float_tininess_after_rounding
;
244 else if ( strcmp( argPtr
, "all1" ) == 0 ) {
245 functionArgument
= TRUE
;
249 else if ( strcmp( argPtr
, "all2" ) == 0 ) {
250 functionArgument
= TRUE
;
254 else if ( strcmp( argPtr
, "all" ) == 0 ) {
255 functionArgument
= TRUE
;
260 for ( functionCode
= 1;
261 functionCode
< NUM_FUNCTIONS
;
264 if ( strcmp( argPtr
, functions
[ functionCode
].name
) == 0 ) {
268 if ( functionCode
== NUM_FUNCTIONS
) {
269 fail( "Invalid option or function `%s'", argv
[ 0 ] );
271 if ( ! functionExists
[ functionCode
] ) {
273 "Function `%s' is not supported or cannot be tested",
277 functionArgument
= TRUE
;
282 if ( ! functionArgument
) fail( "Function argument required" );
283 (void) signal( SIGINT
, catchSIGINT
);
284 (void) signal( SIGTERM
, catchSIGINT
);
285 if ( functionCode
) {
287 if ( ! roundingPrecision
) roundingPrecision
= 80;
288 if ( ! roundingMode
) roundingMode
= ROUND_NEAREST_EVEN
;
290 testFunction( functionCode
, roundingPrecision
, roundingMode
);
294 fail( "Can only test one function with `-forever' option" );
296 if ( operands
== 1 ) {
297 for ( functionCode
= 1;
298 functionCode
< NUM_FUNCTIONS
;
301 if ( functionExists
[ functionCode
]
302 && ( functions
[ functionCode
].numInputs
== 1 ) ) {
304 functionCode
, roundingPrecision
, roundingMode
);
308 else if ( operands
== 2 ) {
309 for ( functionCode
= 1;
310 functionCode
< NUM_FUNCTIONS
;
313 if ( functionExists
[ functionCode
]
314 && ( functions
[ functionCode
].numInputs
== 2 ) ) {
316 functionCode
, roundingPrecision
, roundingMode
);
321 for ( functionCode
= 1;
322 functionCode
< NUM_FUNCTIONS
;
325 if ( functionExists
[ functionCode
] ) {
327 functionCode
, roundingPrecision
, roundingMode
);