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