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