1 /* $NetBSD: softfloat.h,v 1.6 2020/09/02 03:41:56 thorpej 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 This C header file template is part of the Berkeley SoftFloat IEEE Floating-
37 Point Arithmetic Package, Release 2c, by John R. Hauser.
38
39 THIS SOFTWARE IS DISTRIBUTED AS IS, FOR FREE. Although reasonable effort has
40 been made to avoid it, THIS SOFTWARE MAY CONTAIN FAULTS THAT WILL AT TIMES
41 RESULT IN INCORRECT BEHAVIOR. USE OF THIS SOFTWARE IS RESTRICTED TO PERSONS
42 AND ORGANIZATIONS WHO CAN AND WILL TOLERATE ALL LOSSES, COSTS, OR OTHER
43 PROBLEMS THEY INCUR DUE TO THE SOFTWARE WITHOUT RECOMPENSE FROM JOHN HAUSER OR
44 THE INTERNATIONAL COMPUTER SCIENCE INSTITUTE, AND WHO FURTHERMORE EFFECTIVELY
45 INDEMNIFY JOHN HAUSER AND THE INTERNATIONAL COMPUTER SCIENCE INSTITUTE
46 (possibly via similar legal notice) AGAINST ALL LOSSES, COSTS, OR OTHER
47 PROBLEMS INCURRED BY THEIR CUSTOMERS AND CLIENTS DUE TO THE SOFTWARE, OR
48 INCURRED BY ANYONE DUE TO A DERIVATIVE WORK THEY CREATE USING ANY PART OF THE
49 SOFTWARE.
50
51 Derivative works require also that (1) the source code for the derivative work
52 includes prominent notice that the work is derivative, and (2) the source code
53 includes prominent notice of these three paragraphs for those parts of this
54 code that are retained.
55
56 =============================================================================*/
57
58 #include <sys/types.h>
59
60 #if !defined(_KERNEL) && !defined(_STANDALONE)
61 #include <inttypes.h>
62 #include <ieeefp.h>
63 #else
64 #include "sys/inttypes.h"
65 #include "machine/ieeefp.h"
66 #endif
67 #include <sys/endian.h>
68
69 /*----------------------------------------------------------------------------
70 | The macro `FLOATX80' must be defined to enable the double-extended-precision
71 | floating-point format `floatx80'. If this macro is not defined, the
72 | `floatx80' type will not be defined, and none of the functions that either
73 | input or output the `floatx80' type will be defined. The same applies to
74 | the `FLOAT128' macro and the quadruple-precision format `float128'.
75 *----------------------------------------------------------------------------*/
76 /* #define FLOATX80 */
77 /* #define FLOAT128 */
78
79 /*----------------------------------------------------------------------------
80 | Software IEEE floating-point types.
81 *----------------------------------------------------------------------------*/
82 typedef u_int32_t float32;
83 typedef u_int64_t float64;
84 #ifdef FLOATX80
85 typedef struct {
86 #if BYTE_ORDER == BIG_ENDIAN
87 u_int16_t high;
88 u_int64_t low;
89 #else
90 u_int64_t low;
91 u_int16_t high;
92 #endif
93 } floatx80;
94 #endif
95 #ifdef FLOAT128
96 typedef struct {
97 u_int64_t high, low;
98 } float128;
99 #endif
100
101 /*
102 * Some of the global variables that used to be here have been removed for
103 * fairly obvious (defopt-MULTIPROCESSOR) reasons. The rest (which don't
104 * change dynamically) will be removed later. [ross]
105 */
106
107 #define float_rounding_mode() fpgetround()
108
109 /*----------------------------------------------------------------------------
110 | Software IEEE floating-point underflow tininess-detection mode.
111 *----------------------------------------------------------------------------*/
112 extern int float_detect_tininess;
113 enum {
114 float_tininess_after_rounding = 1,
115 float_tininess_before_rounding = 0
116 };
117
118 /*----------------------------------------------------------------------------
119 | Software IEEE floating-point rounding mode.
120 *----------------------------------------------------------------------------*/
121 enum {
122 float_round_nearest_even = FP_RN,
123 float_round_to_zero = FP_RZ,
124 float_round_down = FP_RM,
125 float_round_up = FP_RP
126 };
127
128 /*----------------------------------------------------------------------------
129 | Software IEEE floating-point exception flags.
130 *----------------------------------------------------------------------------*/
131 enum {
132 float_flag_inexact = FP_X_IMP,
133 float_flag_underflow = FP_X_UFL,
134 float_flag_overflow = FP_X_OFL,
135 float_flag_divbyzero = FP_X_DZ,
136 float_flag_invalid = FP_X_INV
137 };
138
139 /*----------------------------------------------------------------------------
140 | Routine to raise any or all of the software IEEE floating-point exception
141 | flags.
142 *----------------------------------------------------------------------------*/
143 /*
144 * Routines provided by <machine/ieeefp.h>:
145 *
146 * float_raise()
147 * float_set_inexact()
148 * float_set_invalid()
149 */
150
151 /*----------------------------------------------------------------------------
152 | Software IEEE integer-to-floating-point conversion routines.
153 *----------------------------------------------------------------------------*/
154 float32 int32_to_float32( int );
155 float64 int32_to_float64( int );
156 #ifdef FLOATX80
157 floatx80 int32_to_floatx80( int );
158 #endif
159 #ifdef FLOAT128
160 float128 int32_to_float128( int );
161 #endif
162 #ifndef SOFTFLOAT_FOR_GCC /* __floatdi?f is in libgcc2.c */
163 float32 int64_to_float32( int64_t );
164 float64 int64_to_float64( int64_t );
165 #ifdef FLOATX80
166 floatx80 int64_to_floatx80( int64_t );
167 #endif
168 #ifdef FLOAT128
169 float128 int64_to_float128( int64_t );
170 #endif
171 #endif
172
173 /*----------------------------------------------------------------------------
174 | Software IEEE single-precision conversion routines.
175 *----------------------------------------------------------------------------*/
176 int float32_to_int32( float32 );
177 int float32_to_int32_round_to_zero( float32 );
178 #ifndef SOFTFLOAT_FOR_GCC /* __fix?fdi provided by libgcc2.c */
179 int64_t float32_to_int64( float32 );
180 int64_t float32_to_int64_round_to_zero( float32 );
181 #endif
182 float64 float32_to_float64( float32 );
183 #ifdef FLOATX80
184 floatx80 float32_to_floatx80( float32 );
185 #endif
186 #ifdef FLOAT128
187 float128 float32_to_float128( float32 );
188 #endif
189
190 /*----------------------------------------------------------------------------
191 | Software IEEE single-precision operations.
192 *----------------------------------------------------------------------------*/
193 float32 float32_round_to_int( float32 );
194 float32 float32_add( float32, float32 );
195 float32 float32_sub( float32, float32 );
196 float32 float32_mul( float32, float32 );
197 float32 float32_div( float32, float32 );
198 float32 float32_rem( float32, float32 );
199 float32 float32_sqrt( float32 );
200 int float32_eq( float32, float32 );
201 int float32_le( float32, float32 );
202 int float32_lt( float32, float32 );
203 int float32_eq_signaling( float32, float32 );
204 int float32_le_quiet( float32, float32 );
205 int float32_lt_quiet( float32, float32 );
206 #ifndef SOFTFLOAT_FOR_GCC
207 int float32_is_signaling_nan( float32 );
208 #endif
209
210 /*----------------------------------------------------------------------------
211 | Software IEEE double-precision conversion routines.
212 *----------------------------------------------------------------------------*/
213 int float64_to_int32( float64 );
214 int float64_to_int32_round_to_zero( float64 );
215 #ifndef SOFTFLOAT_FOR_GCC /* __fix?fdi provided by libgcc2.c */
216 int64_t float64_to_int64( float64 );
217 uint64_t float64_to_uint64( float64 );
218 int64_t float64_to_int64_round_to_zero( float64 );
219 #endif
220 float32 float64_to_float32( float64 );
221 #ifdef FLOATX80
222 floatx80 float64_to_floatx80( float64 );
223 #endif
224 #ifdef FLOAT128
225 float128 float64_to_float128( float64 );
226 #endif
227
228 /*----------------------------------------------------------------------------
229 | Software IEEE double-precision operations.
230 *----------------------------------------------------------------------------*/
231
232 /*----------------------------------------------------------------------------
233 | The pattern for a default generated double-precision NaN.
234 *----------------------------------------------------------------------------*/
235 #define float64_default_nan 0xFFF8000000000000LL
236
237 /*----------------------------------------------------------------------------
238 | Returns 1 if the double-precision floating-point value `a' is a NaN;
239 | otherwise returns 0.
240 *----------------------------------------------------------------------------*/
241
242 static __inline int
float64_is_nan(float64 a)243 float64_is_nan(float64 a)
244 {
245 return 0xFFE0000000000000LL < a << 1;
246 }
247
248 /*----------------------------------------------------------------------------
249 | Returns 1 if the double-precision floating-point value `a' is a signaling
250 | NaN; otherwise returns 0.
251 *----------------------------------------------------------------------------*/
252
253 static __inline int
float64_is_signaling_nan(float64 a)254 float64_is_signaling_nan(float64 a)
255 {
256 return (a >> 51 & 0xFFF) == 0xFFE && (a & 0x0007FFFFFFFFFFFFLL);
257 }
258
259 float64 float64_round_to_int( float64 );
260 float64 float64_add( float64, float64 );
261 float64 float64_sub( float64, float64 );
262 float64 float64_mul( float64, float64 );
263 float64 float64_div( float64, float64 );
264 float64 float64_rem( float64, float64 );
265 float64 float64_sqrt( float64 );
266 int float64_eq( float64, float64 );
267 int float64_le( float64, float64 );
268 int float64_lt( float64, float64 );
269 int float64_eq_signaling( float64, float64 );
270 int float64_le_quiet( float64, float64 );
271 int float64_lt_quiet( float64, float64 );
272 #ifndef SOFTFLOAT_FOR_GCC
273 int float64_is_signaling_nan( float64 );
274 #endif
275
276 #ifdef FLOATX80
277
278 /*----------------------------------------------------------------------------
279 | Software IEEE double-extended-precision conversion routines.
280 *----------------------------------------------------------------------------*/
281 int floatx80_to_int32( floatx80 );
282 int floatx80_to_int32_round_to_zero( floatx80 );
283 int64_t floatx80_to_int64( floatx80 );
284 int64_t floatx80_to_int64_round_to_zero( floatx80 );
285 float32 floatx80_to_float32( floatx80 );
286 float64 floatx80_to_float64( floatx80 );
287 #ifdef FLOAT128
288 float128 floatx80_to_float128( floatx80 );
289 #endif
290
291 /*----------------------------------------------------------------------------
292 | Software IEEE double-extended-precision rounding precision. Valid values
293 | are 32, 64, and 80.
294 *----------------------------------------------------------------------------*/
295 extern int floatx80_rounding_precision;
296
297 /*----------------------------------------------------------------------------
298 | Software IEEE double-extended-precision operations.
299 *----------------------------------------------------------------------------*/
300 floatx80 floatx80_round_to_int( floatx80 );
301 floatx80 floatx80_add( floatx80, floatx80 );
302 floatx80 floatx80_sub( floatx80, floatx80 );
303 floatx80 floatx80_mul( floatx80, floatx80 );
304 floatx80 floatx80_div( floatx80, floatx80 );
305 floatx80 floatx80_rem( floatx80, floatx80 );
306 floatx80 floatx80_sqrt( floatx80 );
307 int floatx80_eq( floatx80, floatx80 );
308 int floatx80_le( floatx80, floatx80 );
309 int floatx80_lt( floatx80, floatx80 );
310 int floatx80_eq_signaling( floatx80, floatx80 );
311 int floatx80_le_quiet( floatx80, floatx80 );
312 int floatx80_lt_quiet( floatx80, floatx80 );
313 int floatx80_is_signaling_nan( floatx80 );
314
315 #endif
316
317 #ifdef FLOAT128
318
319 /*----------------------------------------------------------------------------
320 | Software IEEE quadruple-precision conversion routines.
321 *----------------------------------------------------------------------------*/
322 int float128_to_int32( float128 );
323 int float128_to_int32_round_to_zero( float128 );
324 int64_t float128_to_int64( float128 );
325 int64_t float128_to_int64_round_to_zero( float128 );
326 float32 float128_to_float32( float128 );
327 float64 float128_to_float64( float128 );
328 #ifdef FLOATX80
329 floatx80 float128_to_floatx80( float128 );
330 #endif
331
332 /*----------------------------------------------------------------------------
333 | Software IEEE quadruple-precision operations.
334 *----------------------------------------------------------------------------*/
335 float128 float128_round_to_int( float128 );
336 float128 float128_add( float128, float128 );
337 float128 float128_sub( float128, float128 );
338 float128 float128_mul( float128, float128 );
339 float128 float128_div( float128, float128 );
340 float128 float128_rem( float128, float128 );
341 float128 float128_sqrt( float128 );
342 int float128_eq( float128, float128 );
343 int float128_le( float128, float128 );
344 int float128_lt( float128, float128 );
345 int float128_eq_signaling( float128, float128 );
346 int float128_le_quiet( float128, float128 );
347 int float128_lt_quiet( float128, float128 );
348 int float128_is_signaling_nan( float128 );
349
350 #endif
351
352