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