1 /*	$NetBSD: softfloat.h,v 1.4 2002/05/22 00:01:35 bjh21 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 header file is part of the SoftFloat IEC/IEEE Floating-point
45 Arithmetic Package, Release 2a.
46 
47 Written by John R. Hauser.  This work was made possible in part by the
48 International Computer Science Institute, located at Suite 600, 1947 Center
49 Street, Berkeley, California 94704.  Funding was partially provided by the
50 National Science Foundation under grant MIP-9311980.  The original version
51 of this code was written as part of a project to build a fixed-point vector
52 processor in collaboration with the University of California at Berkeley,
53 overseen by Profs. Nelson Morgan and John Wawrzynek.  More information
54 is available through the Web page `http://HTTP.CS.Berkeley.EDU/~jhauser/
55 arithmetic/SoftFloat.html'.
56 
57 THIS SOFTWARE IS DISTRIBUTED AS IS, FOR FREE.  Although reasonable effort
58 has been made to avoid it, THIS SOFTWARE MAY CONTAIN FAULTS THAT WILL AT
59 TIMES RESULT IN INCORRECT BEHAVIOR.  USE OF THIS SOFTWARE IS RESTRICTED TO
60 PERSONS AND ORGANIZATIONS WHO CAN AND WILL TAKE FULL RESPONSIBILITY FOR ANY
61 AND ALL LOSSES, COSTS, OR OTHER PROBLEMS ARISING FROM ITS USE.
62 
63 Derivative works are acceptable, even for commercial purposes, so long as
64 (1) they include prominent notice that the work is derivative, and (2) they
65 include prominent notice akin to these four paragraphs for those parts of
66 this code that are retained.
67 
68 ===============================================================================
69 */
70 
71 #include <ieeefp.h>
72 #include <inttypes.h>
73 #include <sys/endian.h>
74 
75 /*
76 -------------------------------------------------------------------------------
77 The macro `FLOATX80' must be defined to enable the extended double-precision
78 floating-point format `floatx80'.  If this macro is not defined, the
79 `floatx80' type will not be defined, and none of the functions that either
80 input or output the `floatx80' type will be defined.  The same applies to
81 the `FLOAT128' macro and the quadruple-precision format `float128'.
82 -------------------------------------------------------------------------------
83 */
84 /* #define FLOATX80 */
85 /* #define FLOAT128 */
86 
87 /*
88 -------------------------------------------------------------------------------
89 Software IEC/IEEE floating-point types.
90 -------------------------------------------------------------------------------
91 */
92 typedef uint32_t float32;
93 typedef uint64_t float64;
94 #ifdef FLOATX80
95 typedef struct {
96 #if BYTE_ORDER == BIG_ENDIAN
97     uint16_t high;
98     uint64_t low;
99 #else
100     uint64_t low;
101     uint16_t high;
102 #endif
103 } floatx80;
104 #endif
105 #ifdef FLOAT128
106 typedef struct {
107     uint64_t high, low;
108 } float128;
109 #endif
110 
111 /*
112 -------------------------------------------------------------------------------
113 Software IEC/IEEE floating-point underflow tininess-detection mode.
114 -------------------------------------------------------------------------------
115 */
116 extern int float_detect_tininess;
117 enum {
118     float_tininess_after_rounding  = 0,
119     float_tininess_before_rounding = 1
120 };
121 
122 /*
123 -------------------------------------------------------------------------------
124 Software IEC/IEEE floating-point rounding mode.
125 -------------------------------------------------------------------------------
126 */
127 extern fp_rnd float_rounding_mode;
128 enum {
129     float_round_nearest_even = FP_RN,
130     float_round_to_zero      = FP_RZ,
131     float_round_down         = FP_RM,
132     float_round_up           = FP_RP
133 };
134 
135 /*
136 -------------------------------------------------------------------------------
137 Software IEC/IEEE floating-point exception flags.
138 -------------------------------------------------------------------------------
139 */
140 extern fp_except float_exception_flags;
141 enum {
142     float_flag_inexact   =  FP_X_IMP,
143     float_flag_underflow =  FP_X_UFL,
144     float_flag_overflow  =  FP_X_OFL,
145     float_flag_divbyzero =  FP_X_DZ,
146     float_flag_invalid   = FP_X_INV
147 };
148 
149 /*
150 -------------------------------------------------------------------------------
151 Routine to raise any or all of the software IEC/IEEE floating-point
152 exception flags.
153 -------------------------------------------------------------------------------
154 */
155 void float_raise( int );
156 
157 /*
158 -------------------------------------------------------------------------------
159 Software IEC/IEEE integer-to-floating-point conversion routines.
160 -------------------------------------------------------------------------------
161 */
162 float32 int32_to_float32( int );
163 float64 int32_to_float64( int );
164 #ifdef FLOATX80
165 floatx80 int32_to_floatx80( int );
166 #endif
167 #ifdef FLOAT128
168 float128 int32_to_float128( int );
169 #endif
170 #ifndef SOFTFLOAT_FOR_GCC /* __floatdi?f is in libgcc2.c */
171 float32 int64_to_float32( int64_t );
172 float64 int64_to_float64( int64_t );
173 #ifdef FLOATX80
174 floatx80 int64_to_floatx80( int64_t );
175 #endif
176 #ifdef FLOAT128
177 float128 int64_to_float128( int64_t );
178 #endif
179 #endif
180 
181 /*
182 -------------------------------------------------------------------------------
183 Software IEC/IEEE single-precision conversion routines.
184 -------------------------------------------------------------------------------
185 */
186 int float32_to_int32( float32 );
187 int float32_to_int32_round_to_zero( float32 );
188 #ifndef SOFTFLOAT_FOR_GCC /* __fix?fdi provided by libgcc2.c */
189 int64_t float32_to_int64( float32 );
190 int64_t float32_to_int64_round_to_zero( float32 );
191 #endif
192 float64 float32_to_float64( float32 );
193 #ifdef FLOATX80
194 floatx80 float32_to_floatx80( float32 );
195 #endif
196 #ifdef FLOAT128
197 float128 float32_to_float128( float32 );
198 #endif
199 
200 /*
201 -------------------------------------------------------------------------------
202 Software IEC/IEEE single-precision operations.
203 -------------------------------------------------------------------------------
204 */
205 float32 float32_round_to_int( float32 );
206 float32 float32_add( float32, float32 );
207 float32 float32_sub( float32, float32 );
208 float32 float32_mul( float32, float32 );
209 float32 float32_div( float32, float32 );
210 float32 float32_rem( float32, float32 );
211 float32 float32_sqrt( float32 );
212 int float32_eq( float32, float32 );
213 int float32_le( float32, float32 );
214 int float32_lt( float32, float32 );
215 int float32_eq_signaling( float32, float32 );
216 int float32_le_quiet( float32, float32 );
217 int float32_lt_quiet( float32, float32 );
218 #ifndef SOFTFLOAT_FOR_GCC
219 int float32_is_signaling_nan( float32 );
220 #endif
221 
222 /*
223 -------------------------------------------------------------------------------
224 Software IEC/IEEE double-precision conversion routines.
225 -------------------------------------------------------------------------------
226 */
227 int float64_to_int32( float64 );
228 int float64_to_int32_round_to_zero( float64 );
229 #ifndef SOFTFLOAT_FOR_GCC /* __fix?fdi provided by libgcc2.c */
230 int64_t float64_to_int64( float64 );
231 int64_t float64_to_int64_round_to_zero( float64 );
232 #endif
233 float32 float64_to_float32( float64 );
234 #ifdef FLOATX80
235 floatx80 float64_to_floatx80( float64 );
236 #endif
237 #ifdef FLOAT128
238 float128 float64_to_float128( float64 );
239 #endif
240 
241 /*
242 -------------------------------------------------------------------------------
243 Software IEC/IEEE double-precision operations.
244 -------------------------------------------------------------------------------
245 */
246 float64 float64_round_to_int( float64 );
247 float64 float64_add( float64, float64 );
248 float64 float64_sub( float64, float64 );
249 float64 float64_mul( float64, float64 );
250 float64 float64_div( float64, float64 );
251 float64 float64_rem( float64, float64 );
252 float64 float64_sqrt( float64 );
253 int float64_eq( float64, float64 );
254 int float64_le( float64, float64 );
255 int float64_lt( float64, float64 );
256 int float64_eq_signaling( float64, float64 );
257 int float64_le_quiet( float64, float64 );
258 int float64_lt_quiet( float64, float64 );
259 #ifndef SOFTFLOAT_FOR_GCC
260 int float64_is_signaling_nan( float64 );
261 #endif
262 
263 #ifdef FLOATX80
264 
265 /*
266 -------------------------------------------------------------------------------
267 Software IEC/IEEE extended double-precision conversion routines.
268 -------------------------------------------------------------------------------
269 */
270 int floatx80_to_int32( floatx80 );
271 int floatx80_to_int32_round_to_zero( floatx80 );
272 int64_t floatx80_to_int64( floatx80 );
273 int64_t floatx80_to_int64_round_to_zero( floatx80 );
274 float32 floatx80_to_float32( floatx80 );
275 float64 floatx80_to_float64( floatx80 );
276 #ifdef FLOAT128
277 float128 floatx80_to_float128( floatx80 );
278 #endif
279 
280 /*
281 -------------------------------------------------------------------------------
282 Software IEC/IEEE extended double-precision rounding precision.  Valid
283 values are 32, 64, and 80.
284 -------------------------------------------------------------------------------
285 */
286 extern int floatx80_rounding_precision;
287 
288 /*
289 -------------------------------------------------------------------------------
290 Software IEC/IEEE extended double-precision operations.
291 -------------------------------------------------------------------------------
292 */
293 floatx80 floatx80_round_to_int( floatx80 );
294 floatx80 floatx80_add( floatx80, floatx80 );
295 floatx80 floatx80_sub( floatx80, floatx80 );
296 floatx80 floatx80_mul( floatx80, floatx80 );
297 floatx80 floatx80_div( floatx80, floatx80 );
298 floatx80 floatx80_rem( floatx80, floatx80 );
299 floatx80 floatx80_sqrt( floatx80 );
300 int floatx80_eq( floatx80, floatx80 );
301 int floatx80_le( floatx80, floatx80 );
302 int floatx80_lt( floatx80, floatx80 );
303 int floatx80_eq_signaling( floatx80, floatx80 );
304 int floatx80_le_quiet( floatx80, floatx80 );
305 int floatx80_lt_quiet( floatx80, floatx80 );
306 int floatx80_is_signaling_nan( floatx80 );
307 
308 #endif
309 
310 #ifdef FLOAT128
311 
312 /*
313 -------------------------------------------------------------------------------
314 Software IEC/IEEE quadruple-precision conversion routines.
315 -------------------------------------------------------------------------------
316 */
317 int float128_to_int32( float128 );
318 int float128_to_int32_round_to_zero( float128 );
319 int64_t float128_to_int64( float128 );
320 int64_t float128_to_int64_round_to_zero( float128 );
321 float32 float128_to_float32( float128 );
322 float64 float128_to_float64( float128 );
323 #ifdef FLOATX80
324 floatx80 float128_to_floatx80( float128 );
325 #endif
326 
327 /*
328 -------------------------------------------------------------------------------
329 Software IEC/IEEE quadruple-precision operations.
330 -------------------------------------------------------------------------------
331 */
332 float128 float128_round_to_int( float128 );
333 float128 float128_add( float128, float128 );
334 float128 float128_sub( float128, float128 );
335 float128 float128_mul( float128, float128 );
336 float128 float128_div( float128, float128 );
337 float128 float128_rem( float128, float128 );
338 float128 float128_sqrt( float128 );
339 int float128_eq( float128, float128 );
340 int float128_le( float128, float128 );
341 int float128_lt( float128, float128 );
342 int float128_eq_signaling( float128, float128 );
343 int float128_le_quiet( float128, float128 );
344 int float128_lt_quiet( float128, float128 );
345 int float128_is_signaling_nan( float128 );
346 
347 #endif
348 
349