Home | History | Annotate | Line # | Download | only in testfloat
writeHex.c revision 1.2
      1 /* $NetBSD: writeHex.c,v 1.2 2001/03/13 06:48:56 ross Exp $ */
      2 
      3 /*
      4 ===============================================================================
      5 
      6 This C source file is part of TestFloat, Release 2a, a package of programs
      7 for testing the correctness of floating-point arithmetic complying to the
      8 IEC/IEEE Standard for Floating-Point.
      9 
     10 Written by John R. Hauser.  More information is available through the Web
     11 page `http://HTTP.CS.Berkeley.EDU/~jhauser/arithmetic/TestFloat.html'.
     12 
     13 THIS SOFTWARE IS DISTRIBUTED AS IS, FOR FREE.  Although reasonable effort
     14 has been made to avoid it, THIS SOFTWARE MAY CONTAIN FAULTS THAT WILL AT
     15 TIMES RESULT IN INCORRECT BEHAVIOR.  USE OF THIS SOFTWARE IS RESTRICTED TO
     16 PERSONS AND ORGANIZATIONS WHO CAN AND WILL TAKE FULL RESPONSIBILITY FOR ANY
     17 AND ALL LOSSES, COSTS, OR OTHER PROBLEMS ARISING FROM ITS USE.
     18 
     19 Derivative works are acceptable, even for commercial purposes, so long as
     20 (1) they include prominent notice that the work is derivative, and (2) they
     21 include prominent notice akin to these four paragraphs for those parts of
     22 this code that are retained.
     23 
     24 ===============================================================================
     25 */
     26 
     27 #include <stdio.h>
     28 #include "milieu.h"
     29 #include "softfloat.h"
     30 #include "writeHex.h"
     31 
     32 void writeHex_flag( flag a, FILE *stream )
     33 {
     34 
     35     fputc( a ? '1' : '0', stream );
     36 
     37 }
     38 
     39 static void writeHex_bits8( bits8 a, FILE *stream )
     40 {
     41     int digit;
     42 
     43     digit = ( a>>4 ) & 0xF;
     44     if ( 9 < digit ) digit += 'A' - ( '0' + 10 );
     45     fputc( '0' + digit, stream );
     46     digit = a & 0xF;
     47     if ( 9 < digit ) digit += 'A' - ( '0' + 10 );
     48     fputc( '0' + digit, stream );
     49 
     50 }
     51 
     52 static void writeHex_bits12( int16 a, FILE *stream )
     53 {
     54     int digit;
     55 
     56     digit = ( a>>8 ) & 0xF;
     57     if ( 9 < digit ) digit += 'A' - ( '0' + 10 );
     58     fputc( '0' + digit, stream );
     59     digit = ( a>>4 ) & 0xF;
     60     if ( 9 < digit ) digit += 'A' - ( '0' + 10 );
     61     fputc( '0' + digit, stream );
     62     digit = a & 0xF;
     63     if ( 9 < digit ) digit += 'A' - ( '0' + 10 );
     64     fputc( '0' + digit, stream );
     65 
     66 }
     67 
     68 static void writeHex_bits16( bits16 a, FILE *stream )
     69 {
     70     int digit;
     71 
     72     digit = ( a>>12 ) & 0xF;
     73     if ( 9 < digit ) digit += 'A' - ( '0' + 10 );
     74     fputc( '0' + digit, stream );
     75     digit = ( a>>8 ) & 0xF;
     76     if ( 9 < digit ) digit += 'A' - ( '0' + 10 );
     77     fputc( '0' + digit, stream );
     78     digit = ( a>>4 ) & 0xF;
     79     if ( 9 < digit ) digit += 'A' - ( '0' + 10 );
     80     fputc( '0' + digit, stream );
     81     digit = a & 0xF;
     82     if ( 9 < digit ) digit += 'A' - ( '0' + 10 );
     83     fputc( '0' + digit, stream );
     84 
     85 }
     86 
     87 void writeHex_bits32( bits32 a, FILE *stream )
     88 {
     89 
     90     writeHex_bits16( a>>16, stream );
     91     writeHex_bits16( a, stream );
     92 
     93 }
     94 
     95 #ifdef BITS64
     96 
     97 void writeHex_bits64( bits64 a, FILE *stream )
     98 {
     99 
    100     writeHex_bits32( a>>32, stream );
    101     writeHex_bits32( a, stream );
    102 
    103 }
    104 
    105 #endif
    106 
    107 void writeHex_float32( float32 a, FILE *stream )
    108 {
    109 
    110     fputc( ( ( (sbits32) a ) < 0 ) ? '8' : '0', stream );
    111     writeHex_bits8( a>>23, stream );
    112     fputc( '.', stream );
    113     writeHex_bits8( ( a>>16 ) & 0x7F, stream );
    114     writeHex_bits16( a, stream );
    115 
    116 }
    117 
    118 #ifdef BITS64
    119 
    120 void writeHex_float64( float64 a, FILE *stream )
    121 {
    122 
    123     writeHex_bits12( a>>52, stream );
    124     fputc( '.', stream );
    125     writeHex_bits12( a>>40, stream );
    126     writeHex_bits8( a>>32, stream );
    127     writeHex_bits32( a, stream );
    128 
    129 }
    130 
    131 #else
    132 
    133 void writeHex_float64( float64 a, FILE *stream )
    134 {
    135 
    136     writeHex_bits12( a.high>>20, stream );
    137     fputc( '.', stream );
    138     writeHex_bits12( a.high>>8, stream );
    139     writeHex_bits8( a.high, stream );
    140     writeHex_bits32( a.low, stream );
    141 
    142 }
    143 
    144 #endif
    145 
    146 #ifdef FLOATX80
    147 
    148 void writeHex_floatx80( floatx80 a, FILE *stream )
    149 {
    150 
    151     writeHex_bits16( a.high, stream );
    152     fputc( '.', stream );
    153     writeHex_bits64( a.low, stream );
    154 
    155 }
    156 
    157 #endif
    158 
    159 #ifdef FLOAT128
    160 
    161 void writeHex_float128( float128 a, FILE *stream )
    162 {
    163 
    164     writeHex_bits16( a.high>>48, stream );
    165     fputc( '.', stream );
    166     writeHex_bits16( a.high>>32, stream );
    167     writeHex_bits32( a.high, stream );
    168     writeHex_bits64( a.low, stream );
    169 
    170 }
    171 
    172 #endif
    173 
    174 void writeHex_float_flags( uint8 flags, FILE *stream )
    175 {
    176 
    177     fputc( flags & float_flag_invalid   ? 'v' : '.', stream );
    178     fputc( flags & float_flag_divbyzero ? 'z' : '.', stream );
    179     fputc( flags & float_flag_overflow  ? 'o' : '.', stream );
    180     fputc( flags & float_flag_underflow ? 'u' : '.', stream );
    181     fputc( flags & float_flag_inexact   ? 'x' : '.', stream );
    182 
    183 }
    184 
    185