Home | History | Annotate | Line # | Download | only in testfloat
testfloat.c revision 1.3
      1 /* $NetBSD: testfloat.c,v 1.3 2001/03/13 07:39:59 ross Exp $ */
      2 
      3 /* This is a derivative work. */
      4 
      5 /*-
      6  * Copyright (c) 2001 The NetBSD Foundation, Inc.
      7  * All rights reserved.
      8  *
      9  * This code is derived from software contributed to The NetBSD Foundation
     10  * by Ross Harvey.
     11  *
     12  * Redistribution and use in source and binary forms, with or without
     13  * modification, are permitted provided that the following conditions
     14  * are met:
     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.
     20  * 3. All advertising materials mentioning features or use of this software
     21  *    must display the following acknowledgement:
     22  *        This product includes software developed by the NetBSD
     23  *        Foundation, Inc. and its contributors.
     24  * 4. Neither the name of The NetBSD Foundation nor the names of its
     25  *    contributors may be used to endorse or promote products derived
     26  *    from this software without specific prior written permission.
     27  *
     28  * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
     29  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
     30  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
     31  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
     32  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
     33  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
     34  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
     35  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
     36  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
     37  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
     38  * POSSIBILITY OF SUCH DAMAGE.
     39  */
     40 
     41 /*
     42 ===============================================================================
     43 
     44 This C source file is part of TestFloat, Release 2a, a package of programs
     45 for testing the correctness of floating-point arithmetic complying to the
     46 IEC/IEEE Standard for Floating-Point.
     47 
     48 Written by John R. Hauser.  More information is available through the Web
     49 page `http://HTTP.CS.Berkeley.EDU/~jhauser/arithmetic/TestFloat.html'.
     50 
     51 THIS SOFTWARE IS DISTRIBUTED AS IS, FOR FREE.  Although reasonable effort
     52 has been made to avoid it, THIS SOFTWARE MAY CONTAIN FAULTS THAT WILL AT
     53 TIMES RESULT IN INCORRECT BEHAVIOR.  USE OF THIS SOFTWARE IS RESTRICTED TO
     54 PERSONS AND ORGANIZATIONS WHO CAN AND WILL TAKE FULL RESPONSIBILITY FOR ANY
     55 AND ALL LOSSES, COSTS, OR OTHER PROBLEMS ARISING FROM ITS USE.
     56 
     57 Derivative works are acceptable, even for commercial purposes, so long as
     58 (1) they include prominent notice that the work is derivative, and (2) they
     59 include prominent notice akin to these four paragraphs for those parts of
     60 this code that are retained.
     61 
     62 ===============================================================================
     63 */
     64 
     65 #include <stdlib.h>
     66 #include <signal.h>
     67 #include <string.h>
     68 #include "milieu.h"
     69 #include "fail.h"
     70 #include "softfloat.h"
     71 #include "testCases.h"
     72 #include "testLoops.h"
     73 #include "systflags.h"
     74 #include "testFunction.h"
     75 
     76 static void catchSIGINT( int signalCode )
     77 {
     78 
     79     if ( stop ) exit( EXIT_FAILURE );
     80     stop = TRUE;
     81 
     82 }
     83 
     84 int
     85 main( int argc, char **argv )
     86 {
     87     char *argPtr;
     88     flag functionArgument;
     89     uint8 functionCode;
     90     int8 operands, roundingPrecision, roundingMode;
     91 
     92     fail_programName = "testfloat";
     93     if ( argc <= 1 ) goto writeHelpMessage;
     94     testCases_setLevel( 1 );
     95     trueName = "soft";
     96     testName = "syst";
     97     errorStop = FALSE;
     98     forever = FALSE;
     99     maxErrorCount = 20;
    100     trueFlagsPtr = &float_exception_flags;
    101     testFlagsFunctionPtr = syst_float_flags_clear;
    102     tininessModeName = 0;
    103     functionArgument = FALSE;
    104     functionCode = 0;
    105     operands = 0;
    106     roundingPrecision = 0;
    107     roundingMode = 0;
    108     --argc;
    109     ++argv;
    110     while ( argc && ( argPtr = argv[ 0 ] ) ) {
    111         if ( argPtr[ 0 ] == '-' ) ++argPtr;
    112         if ( strcmp( argPtr, "help" ) == 0 ) {
    113  writeHelpMessage:
    114             fputs(
    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"
    120 " *  -level 1\n"
    121 "    -errors <num>    --Stop each function test after <num> errors.\n"
    122 " *  -errors 20\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"
    126 #ifdef FLOATX80
    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"
    130 #endif
    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"
    137 "  <function>:\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"
    141 #ifdef BITS64
    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"
    147 "    <float>_sqrt\n"
    148 #else
    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"
    152 #endif
    153 "    -all1            --All 1-operand functions.\n"
    154 "    -all2            --All 2-operand functions.\n"
    155 "    -all             --All functions.\n"
    156 "  <float>:\n"
    157 "    float32          --Single precision.\n"
    158 "    float64          --Double precision.\n"
    159 #ifdef FLOATX80
    160 "    floatx80         --Extended double precision.\n"
    161 #endif
    162 #ifdef FLOAT128
    163 "    float128         --Quadruple precision.\n"
    164 #endif
    165                 ,
    166                 stdout
    167             );
    168             return EXIT_SUCCESS;
    169         }
    170         else if ( strcmp( argPtr, "list" ) == 0 ) {
    171             for ( functionCode = 1;
    172                   functionCode < NUM_FUNCTIONS;
    173                   ++functionCode
    174                 ) {
    175                 if ( functionExists[ functionCode ] ) {
    176                     puts( functions[ functionCode ].name );
    177                 }
    178             }
    179             return EXIT_SUCCESS;
    180         }
    181         else if ( strcmp( argPtr, "level" ) == 0 ) {
    182             if ( argc < 2 ) goto optionError;
    183             testCases_setLevel( atoi( argv[ 1 ] ) );
    184             --argc;
    185             ++argv;
    186         }
    187         else if ( strcmp( argPtr, "level1" ) == 0 ) {
    188             testCases_setLevel( 1 );
    189         }
    190         else if ( strcmp( argPtr, "level2" ) == 0 ) {
    191             testCases_setLevel( 2 );
    192         }
    193         else if ( strcmp( argPtr, "errors" ) == 0 ) {
    194             if ( argc < 2 ) {
    195      optionError:
    196                 fail( "`%s' option requires numeric argument", argv[ 0 ] );
    197             }
    198             maxErrorCount = atoi( argv[ 1 ] );
    199             --argc;
    200             ++argv;
    201         }
    202         else if ( strcmp( argPtr, "errorstop" ) == 0 ) {
    203             errorStop = TRUE;
    204         }
    205         else if ( strcmp( argPtr, "forever" ) == 0 ) {
    206             testCases_setLevel( 2 );
    207             forever = TRUE;
    208         }
    209         else if (    ( strcmp( argPtr, "checkNaNs" ) == 0 )
    210                   || ( strcmp( argPtr, "checknans" ) == 0 ) ) {
    211             checkNaNs = TRUE;
    212         }
    213 #ifdef FLOATX80
    214         else if ( strcmp( argPtr, "precision32" ) == 0 ) {
    215             roundingPrecision = 32;
    216         }
    217         else if ( strcmp( argPtr, "precision64" ) == 0 ) {
    218             roundingPrecision = 64;
    219         }
    220         else if ( strcmp( argPtr, "precision80" ) == 0 ) {
    221             roundingPrecision = 80;
    222         }
    223 #endif
    224         else if (    ( strcmp( argPtr, "nearesteven" ) == 0 )
    225                   || ( strcmp( argPtr, "nearest_even" ) == 0 ) ) {
    226             roundingMode = ROUND_NEAREST_EVEN;
    227         }
    228         else if (    ( strcmp( argPtr, "tozero" ) == 0 )
    229                   || ( strcmp( argPtr, "to_zero" ) == 0 ) ) {
    230             roundingMode = ROUND_TO_ZERO;
    231         }
    232         else if ( strcmp( argPtr, "down" ) == 0 ) {
    233             roundingMode = ROUND_DOWN;
    234         }
    235         else if ( strcmp( argPtr, "up" ) == 0 ) {
    236             roundingMode = ROUND_UP;
    237         }
    238         else if ( strcmp( argPtr, "tininessbefore" ) == 0 ) {
    239             float_detect_tininess = float_tininess_before_rounding;
    240         }
    241         else if ( strcmp( argPtr, "tininessafter" ) == 0 ) {
    242             float_detect_tininess = float_tininess_after_rounding;
    243         }
    244         else if ( strcmp( argPtr, "all1" ) == 0 ) {
    245             functionArgument = TRUE;
    246             functionCode = 0;
    247             operands = 1;
    248         }
    249         else if ( strcmp( argPtr, "all2" ) == 0 ) {
    250             functionArgument = TRUE;
    251             functionCode = 0;
    252             operands = 2;
    253         }
    254         else if ( strcmp( argPtr, "all" ) == 0 ) {
    255             functionArgument = TRUE;
    256             functionCode = 0;
    257             operands = 0;
    258         }
    259         else {
    260             for ( functionCode = 1;
    261                   functionCode < NUM_FUNCTIONS;
    262                   ++functionCode
    263                 ) {
    264                 if ( strcmp( argPtr, functions[ functionCode ].name ) == 0 ) {
    265                     break;
    266                 }
    267             }
    268             if ( functionCode == NUM_FUNCTIONS ) {
    269                 fail( "Invalid option or function `%s'", argv[ 0 ] );
    270             }
    271             if ( ! functionExists[ functionCode ] ) {
    272                 fail(
    273                     "Function `%s' is not supported or cannot be tested",
    274                     argPtr
    275                 );
    276             }
    277             functionArgument = TRUE;
    278         }
    279         --argc;
    280         ++argv;
    281     }
    282     if ( ! functionArgument ) fail( "Function argument required" );
    283     (void) signal( SIGINT, catchSIGINT );
    284     (void) signal( SIGTERM, catchSIGINT );
    285     if ( functionCode ) {
    286         if ( forever ) {
    287             if ( ! roundingPrecision ) roundingPrecision = 80;
    288             if ( ! roundingMode ) roundingMode = ROUND_NEAREST_EVEN;
    289         }
    290         testFunction( functionCode, roundingPrecision, roundingMode );
    291     }
    292     else {
    293         if ( forever ) {
    294             fail( "Can only test one function with `-forever' option" );
    295         }
    296         if ( operands == 1 ) {
    297             for ( functionCode = 1;
    298                   functionCode < NUM_FUNCTIONS;
    299                   ++functionCode
    300                 ) {
    301                 if (    functionExists[ functionCode ]
    302                      && ( functions[ functionCode ].numInputs == 1 ) ) {
    303                     testFunction(
    304                         functionCode, roundingPrecision, roundingMode );
    305                 }
    306             }
    307         }
    308         else if ( operands == 2 ) {
    309             for ( functionCode = 1;
    310                   functionCode < NUM_FUNCTIONS;
    311                   ++functionCode
    312                 ) {
    313                 if (    functionExists[ functionCode ]
    314                      && ( functions[ functionCode ].numInputs == 2 ) ) {
    315                     testFunction(
    316                         functionCode, roundingPrecision, roundingMode );
    317                 }
    318             }
    319         }
    320         else {
    321             for ( functionCode = 1;
    322                   functionCode < NUM_FUNCTIONS;
    323                   ++functionCode
    324                 ) {
    325                 if ( functionExists[ functionCode ] ) {
    326                     testFunction(
    327                         functionCode, roundingPrecision, roundingMode );
    328                 }
    329             }
    330         }
    331     }
    332     exitWithStatus();
    333     return 0;
    334 }
    335 
    336