1 /*	$NetBSD: writeHex.c,v 1.4 2002/02/21 07:38:16 itojun 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 <stdio.h>
66 #include "milieu.h"
67 #include "softfloat.h"
68 #include "writeHex.h"
69 
70 void writeHex_flag( flag a, FILE *stream )
71 {
72 
73     fputc( a ? '1' : '0', stream );
74 
75 }
76 
77 static void writeHex_bits8( bits8 a, FILE *stream )
78 {
79     int digit;
80 
81     digit = ( a>>4 ) & 0xF;
82     if ( 9 < digit ) digit += 'A' - ( '0' + 10 );
83     fputc( '0' + digit, stream );
84     digit = a & 0xF;
85     if ( 9 < digit ) digit += 'A' - ( '0' + 10 );
86     fputc( '0' + digit, stream );
87 
88 }
89 
90 static void writeHex_bits12( int16 a, FILE *stream )
91 {
92     int digit;
93 
94     digit = ( a>>8 ) & 0xF;
95     if ( 9 < digit ) digit += 'A' - ( '0' + 10 );
96     fputc( '0' + digit, stream );
97     digit = ( a>>4 ) & 0xF;
98     if ( 9 < digit ) digit += 'A' - ( '0' + 10 );
99     fputc( '0' + digit, stream );
100     digit = a & 0xF;
101     if ( 9 < digit ) digit += 'A' - ( '0' + 10 );
102     fputc( '0' + digit, stream );
103 
104 }
105 
106 static void writeHex_bits16( bits16 a, FILE *stream )
107 {
108     int digit;
109 
110     digit = ( a>>12 ) & 0xF;
111     if ( 9 < digit ) digit += 'A' - ( '0' + 10 );
112     fputc( '0' + digit, stream );
113     digit = ( a>>8 ) & 0xF;
114     if ( 9 < digit ) digit += 'A' - ( '0' + 10 );
115     fputc( '0' + digit, stream );
116     digit = ( a>>4 ) & 0xF;
117     if ( 9 < digit ) digit += 'A' - ( '0' + 10 );
118     fputc( '0' + digit, stream );
119     digit = a & 0xF;
120     if ( 9 < digit ) digit += 'A' - ( '0' + 10 );
121     fputc( '0' + digit, stream );
122 
123 }
124 
125 void writeHex_bits32( bits32 a, FILE *stream )
126 {
127 
128     writeHex_bits16( a>>16, stream );
129     writeHex_bits16( a, stream );
130 
131 }
132 
133 #ifdef BITS64
134 
135 void writeHex_bits64( bits64 a, FILE *stream )
136 {
137 
138     writeHex_bits32( a>>32, stream );
139     writeHex_bits32( a, stream );
140 
141 }
142 
143 #endif
144 
145 void writeHex_float32( float32 a, FILE *stream )
146 {
147 
148     fputc( ( ( (sbits32) a ) < 0 ) ? '8' : '0', stream );
149     writeHex_bits8( a>>23, stream );
150     fputc( '.', stream );
151     writeHex_bits8( ( a>>16 ) & 0x7F, stream );
152     writeHex_bits16( a, stream );
153 
154 }
155 
156 #ifdef BITS64
157 
158 void writeHex_float64( float64 a, FILE *stream )
159 {
160 
161     writeHex_bits12( a>>52, stream );
162     fputc( '.', stream );
163     writeHex_bits12( a>>40, stream );
164     writeHex_bits8( a>>32, stream );
165     writeHex_bits32( a, stream );
166 
167 }
168 
169 #else
170 
171 void writeHex_float64( float64 a, FILE *stream )
172 {
173 
174     writeHex_bits12( a.high>>20, stream );
175     fputc( '.', stream );
176     writeHex_bits12( a.high>>8, stream );
177     writeHex_bits8( a.high, stream );
178     writeHex_bits32( a.low, stream );
179 
180 }
181 
182 #endif
183 
184 #ifdef FLOATX80
185 
186 void writeHex_floatx80( floatx80 a, FILE *stream )
187 {
188 
189     writeHex_bits16( a.high, stream );
190     fputc( '.', stream );
191     writeHex_bits64( a.low, stream );
192 
193 }
194 
195 #endif
196 
197 #ifdef FLOAT128
198 
199 void writeHex_float128( float128 a, FILE *stream )
200 {
201 
202     writeHex_bits16( a.high>>48, stream );
203     fputc( '.', stream );
204     writeHex_bits16( a.high>>32, stream );
205     writeHex_bits32( a.high, stream );
206     writeHex_bits64( a.low, stream );
207 
208 }
209 
210 #endif
211 
212 void writeHex_float_flags( uint8 flags, FILE *stream )
213 {
214 
215     fputc( flags & float_flag_invalid   ? 'v' : '.', stream );
216     fputc( flags & float_flag_divbyzero ? 'z' : '.', stream );
217     fputc( flags & float_flag_overflow  ? 'o' : '.', stream );
218     fputc( flags & float_flag_underflow ? 'u' : '.', stream );
219     fputc( flags & float_flag_inexact   ? 'x' : '.', stream );
220 
221 }
222 
223