Home | History | Annotate | Line # | Download | only in testfloat
systfloat.c revision 1.5
      1 /* $NetBSD: systfloat.c,v 1.5 2001/03/22 12:22:18 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 <sys/cdefs.h>
     66 #ifndef __lint
     67 __RCSID("$NetBSD: systfloat.c,v 1.5 2001/03/22 12:22:18 ross Exp $");
     68 #endif
     69 
     70 #include <math.h>
     71 #include <ieeefp.h>
     72 #include "milieu.h"
     73 #include "softfloat.h"
     74 #include "systfloat.h"
     75 #include "systflags.h"
     76 #include "systmodes.h"
     77 
     78 fp_except
     79 syst_float_flags_clear(void)
     80 {
     81     return fpsetsticky(0)
     82 	& (FP_X_IMP | FP_X_UFL | FP_X_OFL | FP_X_DZ | FP_X_INV);
     83 }
     84 
     85 void
     86 syst_float_set_rounding_mode(fp_rnd direction)
     87 {
     88     fpsetround(direction);
     89     fpsetmask(0);
     90 }
     91 
     92 float32 syst_int32_to_float32( int32 a )
     93 {
     94     float32 z;
     95 
     96     *( (float *) &z ) = a;
     97     return z;
     98 
     99 }
    100 
    101 float64 syst_int32_to_float64( int32 a )
    102 {
    103     float64 z;
    104 
    105     *( (double *) &z ) = a;
    106     return z;
    107 
    108 }
    109 
    110 #if defined( FLOATX80 ) && defined( LONG_DOUBLE_IS_FLOATX80 )
    111 
    112 floatx80 syst_int32_to_floatx80( int32 a )
    113 {
    114     floatx80 z;
    115 
    116     *( (long double *) &z ) = a;
    117     return z;
    118 
    119 }
    120 
    121 #endif
    122 
    123 #if defined( FLOAT128 ) && defined( LONG_DOUBLE_IS_FLOAT128 )
    124 
    125 float128 syst_int32_to_float128( int32 a )
    126 {
    127     float128 z;
    128 
    129     *( (long double *) &z ) = a;
    130     return z;
    131 
    132 }
    133 
    134 #endif
    135 
    136 #ifdef BITS64
    137 
    138 float32 syst_int64_to_float32( int64 a )
    139 {
    140     float32 z;
    141 
    142     *( (float *) &z ) = a;
    143     return z;
    144 
    145 }
    146 
    147 float64 syst_int64_to_float64( int64 a )
    148 {
    149     float64 z;
    150 
    151     *( (double *) &z ) = a;
    152     return z;
    153 
    154 }
    155 
    156 #if defined( FLOATX80 ) && defined( LONG_DOUBLE_IS_FLOATX80 )
    157 
    158 floatx80 syst_int64_to_floatx80( int64 a )
    159 {
    160     floatx80 z;
    161 
    162     *( (long double *) &z ) = a;
    163     return z;
    164 
    165 }
    166 
    167 #endif
    168 
    169 #if defined( FLOAT128 ) && defined( LONG_DOUBLE_IS_FLOAT128 )
    170 
    171 float128 syst_int64_to_float128( int64 a )
    172 {
    173     float128 z;
    174 
    175     *( (long double *) &z ) = a;
    176     return z;
    177 
    178 }
    179 
    180 #endif
    181 
    182 #endif
    183 
    184 int32 syst_float32_to_int32_round_to_zero( float32 a )
    185 {
    186 
    187     return *( (float *) &a );
    188 
    189 }
    190 
    191 #ifdef BITS64
    192 
    193 int64 syst_float32_to_int64_round_to_zero( float32 a )
    194 {
    195 
    196     return *( (float *) &a );
    197 
    198 }
    199 
    200 #endif
    201 
    202 float64 syst_float32_to_float64( float32 a )
    203 {
    204     float64 z;
    205 
    206     *( (double *) &z ) = *( (float *) &a );
    207     return z;
    208 
    209 }
    210 
    211 #if defined( FLOATX80 ) && defined( LONG_DOUBLE_IS_FLOATX80 )
    212 
    213 floatx80 syst_float32_to_floatx80( float32 a )
    214 {
    215     floatx80 z;
    216 
    217     *( (long double *) &z ) = *( (float *) &a );
    218     return z;
    219 
    220 }
    221 
    222 #endif
    223 
    224 #if defined( FLOAT128 ) && defined( LONG_DOUBLE_IS_FLOAT128 )
    225 
    226 float128 syst_float32_to_float128( float32 a )
    227 {
    228     float128 z;
    229 
    230     *( (long double *) &z ) = *( (float *) &a );
    231     return z;
    232 
    233 }
    234 
    235 #endif
    236 
    237 float32 syst_float32_add( float32 a, float32 b )
    238 {
    239     float32 z;
    240 
    241     *( (float *) &z ) = *( (float *) &a ) + *( (float *) &b );
    242     return z;
    243 
    244 }
    245 
    246 float32 syst_float32_sub( float32 a, float32 b )
    247 {
    248     float32 z;
    249 
    250     *( (float *) &z ) = *( (float *) &a ) - *( (float *) &b );
    251     return z;
    252 
    253 }
    254 
    255 float32 syst_float32_mul( float32 a, float32 b )
    256 {
    257     float32 z;
    258 
    259     *( (float *) &z ) = *( (float *) &a ) * *( (float *) &b );
    260     return z;
    261 
    262 }
    263 
    264 float32 syst_float32_div( float32 a, float32 b )
    265 {
    266     float32 z;
    267 
    268     *( (float *) &z ) = *( (float *) &a ) / *( (float *) &b );
    269     return z;
    270 
    271 }
    272 
    273 flag syst_float32_eq( float32 a, float32 b )
    274 {
    275 
    276     return ( *( (float *) &a ) == *( (float *) &b ) );
    277 
    278 }
    279 
    280 flag syst_float32_le( float32 a, float32 b )
    281 {
    282 
    283     return ( *( (float *) &a ) <= *( (float *) &b ) );
    284 
    285 }
    286 
    287 flag syst_float32_lt( float32 a, float32 b )
    288 {
    289 
    290     return ( *( (float *) &a ) < *( (float *) &b ) );
    291 
    292 }
    293 
    294 int32 syst_float64_to_int32_round_to_zero( float64 a )
    295 {
    296 
    297     return *( (double *) &a );
    298 
    299 }
    300 
    301 #ifdef BITS64
    302 
    303 int64 syst_float64_to_int64_round_to_zero( float64 a )
    304 {
    305 
    306     return *( (double *) &a );
    307 
    308 }
    309 
    310 #endif
    311 
    312 float32 syst_float64_to_float32( float64 a )
    313 {
    314     float32 z;
    315 
    316     *( (float *) &z ) = *( (double *) &a );
    317     return z;
    318 
    319 }
    320 
    321 #if defined( FLOATX80 ) && defined( LONG_DOUBLE_IS_FLOATX80 )
    322 
    323 floatx80 syst_float64_to_floatx80( float64 a )
    324 {
    325     floatx80 z;
    326 
    327     *( (long double *) &z ) = *( (double *) &a );
    328     return z;
    329 
    330 }
    331 
    332 #endif
    333 
    334 #if defined( FLOAT128 ) && defined( LONG_DOUBLE_IS_FLOAT128 )
    335 
    336 float128 syst_float64_to_float128( float64 a )
    337 {
    338     float128 z;
    339 
    340     *( (long double *) &z ) = *( (double *) &a );
    341     return z;
    342 
    343 }
    344 
    345 #endif
    346 
    347 float64 syst_float64_add( float64 a, float64 b )
    348 {
    349     float64 z;
    350 
    351     *( (double *) &z ) = *( (double *) &a ) + *( (double *) &b );
    352     return z;
    353 
    354 }
    355 
    356 float64 syst_float64_sub( float64 a, float64 b )
    357 {
    358     float64 z;
    359 
    360     *( (double *) &z ) = *( (double *) &a ) - *( (double *) &b );
    361     return z;
    362 
    363 }
    364 
    365 float64 syst_float64_mul( float64 a, float64 b )
    366 {
    367     float64 z;
    368 
    369     *( (double *) &z ) = *( (double *) &a ) * *( (double *) &b );
    370     return z;
    371 
    372 }
    373 
    374 float64 syst_float64_div( float64 a, float64 b )
    375 {
    376     float64 z;
    377 
    378     *( (double *) &z ) = *( (double *) &a ) / *( (double *) &b );
    379     return z;
    380 
    381 }
    382 
    383 float64 syst_float64_sqrt( float64 a )
    384 {
    385     float64 z;
    386 
    387     *( (double *) &z ) = sqrt( *( (double *) &a ) );
    388     return z;
    389 
    390 }
    391 
    392 flag syst_float64_eq( float64 a, float64 b )
    393 {
    394 
    395     return ( *( (double *) &a ) == *( (double *) &b ) );
    396 
    397 }
    398 
    399 flag syst_float64_le( float64 a, float64 b )
    400 {
    401 
    402     return ( *( (double *) &a ) <= *( (double *) &b ) );
    403 
    404 }
    405 
    406 flag syst_float64_lt( float64 a, float64 b )
    407 {
    408 
    409     return ( *( (double *) &a ) < *( (double *) &b ) );
    410 
    411 }
    412 
    413 #if defined( FLOATX80 ) && defined( LONG_DOUBLE_IS_FLOATX80 )
    414 
    415 int32 syst_floatx80_to_int32_round_to_zero( floatx80 a )
    416 {
    417 
    418     return *( (long double *) &a );
    419 
    420 }
    421 
    422 #ifdef BITS64
    423 
    424 int64 syst_floatx80_to_int64_round_to_zero( floatx80 a )
    425 {
    426 
    427     return *( (long double *) &a );
    428 
    429 }
    430 
    431 #endif
    432 
    433 float32 syst_floatx80_to_float32( floatx80 a )
    434 {
    435     float32 z;
    436 
    437     *( (float *) &z ) = *( (long double *) &a );
    438     return z;
    439 
    440 }
    441 
    442 float64 syst_floatx80_to_float64( floatx80 a )
    443 {
    444     float64 z;
    445 
    446     *( (double *) &z ) = *( (long double *) &a );
    447     return z;
    448 
    449 }
    450 
    451 floatx80 syst_floatx80_add( floatx80 a, floatx80 b )
    452 {
    453     floatx80 z;
    454 
    455     *( (long double *) &z ) =
    456         *( (long double *) &a ) + *( (long double *) &b );
    457     return z;
    458 
    459 }
    460 
    461 floatx80 syst_floatx80_sub( floatx80 a, floatx80 b )
    462 {
    463     floatx80 z;
    464 
    465     *( (long double *) &z ) =
    466         *( (long double *) &a ) - *( (long double *) &b );
    467     return z;
    468 
    469 }
    470 
    471 floatx80 syst_floatx80_mul( floatx80 a, floatx80 b )
    472 {
    473     floatx80 z;
    474 
    475     *( (long double *) &z ) =
    476         *( (long double *) &a ) * *( (long double *) &b );
    477     return z;
    478 
    479 }
    480 
    481 floatx80 syst_floatx80_div( floatx80 a, floatx80 b )
    482 {
    483     floatx80 z;
    484 
    485     *( (long double *) &z ) =
    486         *( (long double *) &a ) / *( (long double *) &b );
    487     return z;
    488 
    489 }
    490 
    491 flag syst_floatx80_eq( floatx80 a, floatx80 b )
    492 {
    493 
    494     return ( *( (long double *) &a ) == *( (long double *) &b ) );
    495 
    496 }
    497 
    498 flag syst_floatx80_le( floatx80 a, floatx80 b )
    499 {
    500 
    501     return ( *( (long double *) &a ) <= *( (long double *) &b ) );
    502 
    503 }
    504 
    505 flag syst_floatx80_lt( floatx80 a, floatx80 b )
    506 {
    507 
    508     return ( *( (long double *) &a ) < *( (long double *) &b ) );
    509 
    510 }
    511 
    512 #endif
    513 
    514 #if defined( FLOAT128 ) && defined( LONG_DOUBLE_IS_FLOAT128 )
    515 
    516 int32 syst_float128_to_int32_round_to_zero( float128 a )
    517 {
    518 
    519     return *( (long double *) &a );
    520 
    521 }
    522 
    523 #ifdef BITS64
    524 
    525 int64 syst_float128_to_int64_round_to_zero( float128 a )
    526 {
    527 
    528     return *( (long double *) &a );
    529 
    530 }
    531 
    532 #endif
    533 
    534 float32 syst_float128_to_float32( float128 a )
    535 {
    536     float32 z;
    537 
    538     *( (float *) &z ) = *( (long double *) &a );
    539     return z;
    540 
    541 }
    542 
    543 float64 syst_float128_to_float64( float128 a )
    544 {
    545     float64 z;
    546 
    547     *( (double *) &z ) = *( (long double *) &a );
    548     return z;
    549 
    550 }
    551 
    552 float128 syst_float128_add( float128 a, float128 b )
    553 {
    554     float128 z;
    555 
    556     *( (long double *) &z ) =
    557         *( (long double *) &a ) + *( (long double *) &b );
    558     return z;
    559 
    560 }
    561 
    562 float128 syst_float128_sub( float128 a, float128 b )
    563 {
    564     float128 z;
    565 
    566     *( (long double *) &z ) =
    567         *( (long double *) &a ) - *( (long double *) &b );
    568     return z;
    569 
    570 }
    571 
    572 float128 syst_float128_mul( float128 a, float128 b )
    573 {
    574     float128 z;
    575 
    576     *( (long double *) &z ) =
    577         *( (long double *) &a ) * *( (long double *) &b );
    578     return z;
    579 
    580 }
    581 
    582 float128 syst_float128_div( float128 a, float128 b )
    583 {
    584     float128 z;
    585 
    586     *( (long double *) &z ) =
    587         *( (long double *) &a ) / *( (long double *) &b );
    588     return z;
    589 
    590 }
    591 
    592 flag syst_float128_eq( float128 a, float128 b )
    593 {
    594 
    595     return ( *( (long double *) &a ) == *( (long double *) &b ) );
    596 
    597 }
    598 
    599 flag syst_float128_le( float128 a, float128 b )
    600 {
    601 
    602     return ( *( (long double *) &a ) <= *( (long double *) &b ) );
    603 
    604 }
    605 
    606 flag syst_float128_lt( float128 a, float128 b )
    607 {
    608 
    609     return ( *( (long double *) &a ) < *( (long double *) &b ) );
    610 
    611 }
    612 
    613 #endif
    614 
    615