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