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