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