1 1.5 martin /* $NetBSD: writeHex.c,v 1.5 2008/04/28 20:23:04 martin Exp $ */ 2 1.3 ross 3 1.3 ross /* This is a derivative work. */ 4 1.3 ross 5 1.3 ross /*- 6 1.3 ross * Copyright (c) 2001 The NetBSD Foundation, Inc. 7 1.3 ross * All rights reserved. 8 1.3 ross * 9 1.3 ross * This code is derived from software contributed to The NetBSD Foundation 10 1.3 ross * by Ross Harvey. 11 1.3 ross * 12 1.3 ross * Redistribution and use in source and binary forms, with or without 13 1.3 ross * modification, are permitted provided that the following conditions 14 1.3 ross * are met: 15 1.3 ross * 1. Redistributions of source code must retain the above copyright 16 1.3 ross * notice, this list of conditions and the following disclaimer. 17 1.3 ross * 2. Redistributions in binary form must reproduce the above copyright 18 1.3 ross * notice, this list of conditions and the following disclaimer in the 19 1.3 ross * documentation and/or other materials provided with the distribution. 20 1.3 ross * 21 1.3 ross * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS 22 1.3 ross * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED 23 1.3 ross * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR 24 1.3 ross * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS 25 1.3 ross * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR 26 1.3 ross * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF 27 1.3 ross * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS 28 1.3 ross * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN 29 1.3 ross * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) 30 1.3 ross * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE 31 1.3 ross * POSSIBILITY OF SUCH DAMAGE. 32 1.3 ross */ 33 1.1 ross 34 1.1 ross /* 35 1.1 ross =============================================================================== 36 1.1 ross 37 1.1 ross This C source file is part of TestFloat, Release 2a, a package of programs 38 1.1 ross for testing the correctness of floating-point arithmetic complying to the 39 1.1 ross IEC/IEEE Standard for Floating-Point. 40 1.1 ross 41 1.1 ross Written by John R. Hauser. More information is available through the Web 42 1.1 ross page `http://HTTP.CS.Berkeley.EDU/~jhauser/arithmetic/TestFloat.html'. 43 1.1 ross 44 1.1 ross THIS SOFTWARE IS DISTRIBUTED AS IS, FOR FREE. Although reasonable effort 45 1.1 ross has been made to avoid it, THIS SOFTWARE MAY CONTAIN FAULTS THAT WILL AT 46 1.1 ross TIMES RESULT IN INCORRECT BEHAVIOR. USE OF THIS SOFTWARE IS RESTRICTED TO 47 1.1 ross PERSONS AND ORGANIZATIONS WHO CAN AND WILL TAKE FULL RESPONSIBILITY FOR ANY 48 1.1 ross AND ALL LOSSES, COSTS, OR OTHER PROBLEMS ARISING FROM ITS USE. 49 1.1 ross 50 1.1 ross Derivative works are acceptable, even for commercial purposes, so long as 51 1.1 ross (1) they include prominent notice that the work is derivative, and (2) they 52 1.1 ross include prominent notice akin to these four paragraphs for those parts of 53 1.1 ross this code that are retained. 54 1.1 ross 55 1.1 ross =============================================================================== 56 1.1 ross */ 57 1.1 ross 58 1.1 ross #include <stdio.h> 59 1.1 ross #include "milieu.h" 60 1.1 ross #include "softfloat.h" 61 1.1 ross #include "writeHex.h" 62 1.1 ross 63 1.1 ross void writeHex_flag( flag a, FILE *stream ) 64 1.1 ross { 65 1.1 ross 66 1.1 ross fputc( a ? '1' : '0', stream ); 67 1.1 ross 68 1.1 ross } 69 1.1 ross 70 1.1 ross static void writeHex_bits8( bits8 a, FILE *stream ) 71 1.1 ross { 72 1.1 ross int digit; 73 1.1 ross 74 1.1 ross digit = ( a>>4 ) & 0xF; 75 1.1 ross if ( 9 < digit ) digit += 'A' - ( '0' + 10 ); 76 1.1 ross fputc( '0' + digit, stream ); 77 1.1 ross digit = a & 0xF; 78 1.1 ross if ( 9 < digit ) digit += 'A' - ( '0' + 10 ); 79 1.1 ross fputc( '0' + digit, stream ); 80 1.1 ross 81 1.1 ross } 82 1.1 ross 83 1.1 ross static void writeHex_bits12( int16 a, FILE *stream ) 84 1.1 ross { 85 1.1 ross int digit; 86 1.1 ross 87 1.1 ross digit = ( a>>8 ) & 0xF; 88 1.1 ross if ( 9 < digit ) digit += 'A' - ( '0' + 10 ); 89 1.1 ross fputc( '0' + digit, stream ); 90 1.1 ross digit = ( a>>4 ) & 0xF; 91 1.1 ross if ( 9 < digit ) digit += 'A' - ( '0' + 10 ); 92 1.1 ross fputc( '0' + digit, stream ); 93 1.1 ross digit = a & 0xF; 94 1.1 ross if ( 9 < digit ) digit += 'A' - ( '0' + 10 ); 95 1.1 ross fputc( '0' + digit, stream ); 96 1.1 ross 97 1.1 ross } 98 1.1 ross 99 1.1 ross static void writeHex_bits16( bits16 a, FILE *stream ) 100 1.1 ross { 101 1.1 ross int digit; 102 1.1 ross 103 1.1 ross digit = ( a>>12 ) & 0xF; 104 1.1 ross if ( 9 < digit ) digit += 'A' - ( '0' + 10 ); 105 1.1 ross fputc( '0' + digit, stream ); 106 1.1 ross digit = ( a>>8 ) & 0xF; 107 1.1 ross if ( 9 < digit ) digit += 'A' - ( '0' + 10 ); 108 1.1 ross fputc( '0' + digit, stream ); 109 1.1 ross digit = ( a>>4 ) & 0xF; 110 1.1 ross if ( 9 < digit ) digit += 'A' - ( '0' + 10 ); 111 1.1 ross fputc( '0' + digit, stream ); 112 1.1 ross digit = a & 0xF; 113 1.1 ross if ( 9 < digit ) digit += 'A' - ( '0' + 10 ); 114 1.1 ross fputc( '0' + digit, stream ); 115 1.1 ross 116 1.1 ross } 117 1.1 ross 118 1.1 ross void writeHex_bits32( bits32 a, FILE *stream ) 119 1.1 ross { 120 1.1 ross 121 1.1 ross writeHex_bits16( a>>16, stream ); 122 1.1 ross writeHex_bits16( a, stream ); 123 1.1 ross 124 1.1 ross } 125 1.1 ross 126 1.1 ross #ifdef BITS64 127 1.1 ross 128 1.1 ross void writeHex_bits64( bits64 a, FILE *stream ) 129 1.1 ross { 130 1.1 ross 131 1.1 ross writeHex_bits32( a>>32, stream ); 132 1.1 ross writeHex_bits32( a, stream ); 133 1.1 ross 134 1.1 ross } 135 1.1 ross 136 1.1 ross #endif 137 1.1 ross 138 1.1 ross void writeHex_float32( float32 a, FILE *stream ) 139 1.1 ross { 140 1.1 ross 141 1.1 ross fputc( ( ( (sbits32) a ) < 0 ) ? '8' : '0', stream ); 142 1.1 ross writeHex_bits8( a>>23, stream ); 143 1.1 ross fputc( '.', stream ); 144 1.1 ross writeHex_bits8( ( a>>16 ) & 0x7F, stream ); 145 1.1 ross writeHex_bits16( a, stream ); 146 1.1 ross 147 1.1 ross } 148 1.1 ross 149 1.1 ross #ifdef BITS64 150 1.1 ross 151 1.1 ross void writeHex_float64( float64 a, FILE *stream ) 152 1.1 ross { 153 1.1 ross 154 1.1 ross writeHex_bits12( a>>52, stream ); 155 1.1 ross fputc( '.', stream ); 156 1.1 ross writeHex_bits12( a>>40, stream ); 157 1.1 ross writeHex_bits8( a>>32, stream ); 158 1.1 ross writeHex_bits32( a, stream ); 159 1.1 ross 160 1.1 ross } 161 1.1 ross 162 1.1 ross #else 163 1.1 ross 164 1.1 ross void writeHex_float64( float64 a, FILE *stream ) 165 1.1 ross { 166 1.1 ross 167 1.1 ross writeHex_bits12( a.high>>20, stream ); 168 1.1 ross fputc( '.', stream ); 169 1.1 ross writeHex_bits12( a.high>>8, stream ); 170 1.1 ross writeHex_bits8( a.high, stream ); 171 1.1 ross writeHex_bits32( a.low, stream ); 172 1.1 ross 173 1.1 ross } 174 1.1 ross 175 1.1 ross #endif 176 1.1 ross 177 1.1 ross #ifdef FLOATX80 178 1.1 ross 179 1.1 ross void writeHex_floatx80( floatx80 a, FILE *stream ) 180 1.1 ross { 181 1.1 ross 182 1.1 ross writeHex_bits16( a.high, stream ); 183 1.1 ross fputc( '.', stream ); 184 1.1 ross writeHex_bits64( a.low, stream ); 185 1.1 ross 186 1.1 ross } 187 1.1 ross 188 1.1 ross #endif 189 1.1 ross 190 1.1 ross #ifdef FLOAT128 191 1.1 ross 192 1.1 ross void writeHex_float128( float128 a, FILE *stream ) 193 1.1 ross { 194 1.1 ross 195 1.1 ross writeHex_bits16( a.high>>48, stream ); 196 1.1 ross fputc( '.', stream ); 197 1.1 ross writeHex_bits16( a.high>>32, stream ); 198 1.1 ross writeHex_bits32( a.high, stream ); 199 1.1 ross writeHex_bits64( a.low, stream ); 200 1.1 ross 201 1.1 ross } 202 1.1 ross 203 1.1 ross #endif 204 1.1 ross 205 1.1 ross void writeHex_float_flags( uint8 flags, FILE *stream ) 206 1.1 ross { 207 1.1 ross 208 1.1 ross fputc( flags & float_flag_invalid ? 'v' : '.', stream ); 209 1.1 ross fputc( flags & float_flag_divbyzero ? 'z' : '.', stream ); 210 1.1 ross fputc( flags & float_flag_overflow ? 'o' : '.', stream ); 211 1.1 ross fputc( flags & float_flag_underflow ? 'u' : '.', stream ); 212 1.1 ross fputc( flags & float_flag_inexact ? 'x' : '.', stream ); 213 1.1 ross 214 1.1 ross } 215 1.1 ross 216