|
|
1.1 root 1: #define SOFTFLOAT_68K
2:
3: /*
4: * QEMU float support
5: *
6: * The code in this source file is derived from release 2a of the SoftFloat
7: * IEC/IEEE Floating-point Arithmetic Package. Those parts of the code (and
8: * some later contributions) are provided under that license, as detailed below.
9: * It has subsequently been modified by contributors to the QEMU Project,
10: * so some portions are provided under:
11: * the SoftFloat-2a license
12: * the BSD license
13: * GPL-v2-or-later
14: *
15: * Any future contributions to this file after December 1st 2014 will be
16: * taken to be licensed under the Softfloat-2a license unless specifically
17: * indicated otherwise.
18: */
19:
20: /*
21: ===============================================================================
22: This C header file is part of the SoftFloat IEC/IEEE Floating-point
23: Arithmetic Package, Release 2a.
24:
25: Written by John R. Hauser. This work was made possible in part by the
26: International Computer Science Institute, located at Suite 600, 1947 Center
27: Street, Berkeley, California 94704. Funding was partially provided by the
28: National Science Foundation under grant MIP-9311980. The original version
29: of this code was written as part of a project to build a fixed-point vector
30: processor in collaboration with the University of California at Berkeley,
31: overseen by Profs. Nelson Morgan and John Wawrzynek. More information
32: is available through the Web page `http://HTTP.CS.Berkeley.EDU/~jhauser/
33: arithmetic/SoftFloat.html'.
34:
35: THIS SOFTWARE IS DISTRIBUTED AS IS, FOR FREE. Although reasonable effort
36: has been made to avoid it, THIS SOFTWARE MAY CONTAIN FAULTS THAT WILL AT
37: TIMES RESULT IN INCORRECT BEHAVIOR. USE OF THIS SOFTWARE IS RESTRICTED TO
38: PERSONS AND ORGANIZATIONS WHO CAN AND WILL TAKE FULL RESPONSIBILITY FOR ANY
39: AND ALL LOSSES, COSTS, OR OTHER PROBLEMS ARISING FROM ITS USE.
40:
41: Derivative works are acceptable, even for commercial purposes, so long as
42: (1) they include prominent notice that the work is derivative, and (2) they
43: include prominent notice akin to these four paragraphs for those parts of
44: this code that are retained.
45:
46: ===============================================================================
47: */
48:
49: /* BSD licensing:
50: * Copyright (c) 2006, Fabrice Bellard
51: * All rights reserved.
52: *
53: * Redistribution and use in source and binary forms, with or without
54: * modification, are permitted provided that the following conditions are met:
55: *
56: * 1. Redistributions of source code must retain the above copyright notice,
57: * this list of conditions and the following disclaimer.
58: *
59: * 2. Redistributions in binary form must reproduce the above copyright notice,
60: * this list of conditions and the following disclaimer in the documentation
61: * and/or other materials provided with the distribution.
62: *
63: * 3. Neither the name of the copyright holder nor the names of its contributors
64: * may be used to endorse or promote products derived from this software without
65: * specific prior written permission.
66: *
67: * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
68: * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
69: * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
70: * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
71: * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
72: * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
73: * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
74: * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
75: * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
76: * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF
77: * THE POSSIBILITY OF SUCH DAMAGE.
78: */
79:
80: /* Portions of this work are licensed under the terms of the GNU GPL,
81: * version 2 or later. See the COPYING file in the top-level directory.
82: */
83:
84: #ifndef SOFTFLOAT_H
85: #define SOFTFLOAT_H
86:
87: #include <stdbool.h>
88:
89: #if defined(CONFIG_SOLARIS) && defined(CONFIG_NEEDS_LIBSUNMATH)
90: #include <sunmath.h>
91: #endif
92:
93:
94: /* This 'flag' type must be able to hold at least 0 and 1. It should
95: * probably be replaced with 'bool' but the uses would need to be audited
96: * to check that they weren't accidentally relying on it being a larger type.
97: */
98: typedef uint8_t flag;
99:
100: #define LIT64( a ) a##ULL
101:
102: /*----------------------------------------------------------------------------
103: | Software IEC/IEEE floating-point ordering relations
104: *----------------------------------------------------------------------------*/
105: enum {
106: float_relation_less = -1,
107: float_relation_equal = 0,
108: float_relation_greater = 1,
109: float_relation_unordered = 2
110: };
111:
112: /*----------------------------------------------------------------------------
113: | Software IEC/IEEE floating-point types.
114: *----------------------------------------------------------------------------*/
115: /* Use structures for soft-float types. This prevents accidentally mixing
116: them with native int/float types. A sufficiently clever compiler and
117: sane ABI should be able to see though these structs. However
118: x86/gcc 3.x seems to struggle a bit, so leave them disabled by default. */
119: //#define USE_SOFTFLOAT_STRUCT_TYPES
120: #ifdef USE_SOFTFLOAT_STRUCT_TYPES
121: typedef struct {
122: uint16_t v;
123: } float16;
124: #define float16_val(x) (((float16)(x)).v)
125: #define make_float16(x) __extension__ ({ float16 f16_val = {x}; f16_val; })
126: #define const_float16(x) { x }
127: typedef struct {
128: uint32_t v;
129: } float32;
130: /* The cast ensures an error if the wrong type is passed. */
131: #define float32_val(x) (((float32)(x)).v)
132: #define make_float32(x) __extension__ ({ float32 f32_val = {x}; f32_val; })
133: #define const_float32(x) { x }
134: typedef struct {
135: uint64_t v;
136: } float64;
137: #define float64_val(x) (((float64)(x)).v)
138: #define make_float64(x) __extension__ ({ float64 f64_val = {x}; f64_val; })
139: #define const_float64(x) { x }
140: #else
141: typedef uint16_t float16;
142: typedef uint32_t float32;
143: typedef uint64_t float64;
144: #define float16_val(x) (x)
145: #define float32_val(x) (x)
146: #define float64_val(x) (x)
147: #define make_float16(x) (x)
148: #define make_float32(x) (x)
149: #define make_float64(x) (x)
150: #define const_float16(x) (x)
151: #define const_float32(x) (x)
152: #define const_float64(x) (x)
153: #endif
154: typedef struct {
155: uint16_t high;
156: uint64_t low;
157: } floatx80;
158: typedef struct {
159: #ifdef HOST_WORDS_BIGENDIAN
160: uint64_t high, low;
161: #else
162: uint64_t low, high;
163: #endif
164: } float128;
165:
166: /*----------------------------------------------------------------------------
167: | Software IEC/IEEE floating-point underflow tininess-detection mode.
168: *----------------------------------------------------------------------------*/
169: enum {
170: float_tininess_after_rounding = 0,
171: float_tininess_before_rounding = 1
172: };
173:
174: /*----------------------------------------------------------------------------
175: | Software IEC/IEEE floating-point rounding mode.
176: *----------------------------------------------------------------------------*/
177: enum {
178: float_round_nearest_even = 0,
179: float_round_down = 1,
180: float_round_up = 2,
181: float_round_to_zero = 3,
182: float_round_ties_away = 4,
183: };
184:
185: /*----------------------------------------------------------------------------
186: | Software IEC/IEEE floating-point exception flags.
187: *----------------------------------------------------------------------------*/
188: enum {
189: float_flag_invalid = 0x01,
190: float_flag_denormal = 0x02,
191: float_flag_divbyzero = 0x04,
192: float_flag_overflow = 0x08,
193: float_flag_underflow = 0x10,
194: float_flag_inexact = 0x20,
195: float_flag_signaling = 0x40,
196: float_flag_decimal = 0x80
197: };
198:
199: /*----------------------------------------------------------------------------
200: | Variables for storing sign, exponent and significand of overflowed or
201: | underflowed extended double-precision floating-point value.
202: | Variables for storing sign, exponent and significand of internal extended
203: | double-precision floating-point value for external use.
204: *----------------------------------------------------------------------------*/
205:
206: extern flag floatx80_internal_sign;
207: extern int32_t floatx80_internal_exp;
208: extern uint64_t floatx80_internal_sig;
209: extern int32_t floatx80_internal_exp0;
210: extern uint64_t floatx80_internal_sig0;
211: extern uint64_t floatx80_internal_sig1;
212: extern int8_t floatx80_internal_precision;
213: extern int8_t floatx80_internal_mode;
214:
215: typedef struct float_status {
216: signed char float_detect_tininess;
217: signed char float_rounding_mode;
218: uint8_t float_exception_flags;
219: signed char floatx80_rounding_precision;
220: /* should denormalised results go to zero and set the inexact flag? */
221: flag flush_to_zero;
222: /* should denormalised inputs go to zero and set the input_denormal flag? */
223: flag flush_inputs_to_zero;
224: flag default_nan_mode;
225: flag snan_bit_is_one;
226: } float_status;
227:
228: /*----------------------------------------------------------------------------
229: | Function for getting sign, exponent and significand of extended
230: | double-precision floating-point intermediate result for external use.
231: *----------------------------------------------------------------------------*/
232: floatx80 getFloatInternalOverflow( void );
233: floatx80 getFloatInternalUnderflow( void );
234: floatx80 getFloatInternalRoundedAll( void );
235: floatx80 getFloatInternalRoundedSome( void );
236: floatx80 getFloatInternalUnrounded( void );
237: floatx80 getFloatInternalFloatx80( void );
238: uint64_t getFloatInternalGRS( void );
239:
240: static inline void set_float_detect_tininess(int val, float_status *status)
241: {
242: status->float_detect_tininess = val;
243: }
244: static inline void set_float_rounding_mode(int val, float_status *status)
245: {
246: status->float_rounding_mode = val;
247: }
248: static inline void set_float_exception_flags(int val, float_status *status)
249: {
250: status->float_exception_flags = val;
251: }
252: static inline void set_floatx80_rounding_precision(int val,
253: float_status *status)
254: {
255: status->floatx80_rounding_precision = val;
256: }
257: static inline void set_flush_to_zero(flag val, float_status *status)
258: {
259: status->flush_to_zero = val;
260: }
261: static inline void set_flush_inputs_to_zero(flag val, float_status *status)
262: {
263: status->flush_inputs_to_zero = val;
264: }
265: static inline void set_default_nan_mode(flag val, float_status *status)
266: {
267: status->default_nan_mode = val;
268: }
269: static inline void set_snan_bit_is_one(flag val, float_status *status)
270: {
271: status->snan_bit_is_one = val;
272: }
273: static inline int get_float_detect_tininess(float_status *status)
274: {
275: return status->float_detect_tininess;
276: }
277: static inline int get_float_rounding_mode(float_status *status)
278: {
279: return status->float_rounding_mode;
280: }
281: static inline int get_float_exception_flags(float_status *status)
282: {
283: return status->float_exception_flags;
284: }
285: static inline int get_floatx80_rounding_precision(float_status *status)
286: {
287: return status->floatx80_rounding_precision;
288: }
289: static inline flag get_flush_to_zero(float_status *status)
290: {
291: return status->flush_to_zero;
292: }
293: static inline flag get_flush_inputs_to_zero(float_status *status)
294: {
295: return status->flush_inputs_to_zero;
296: }
297: static inline flag get_default_nan_mode(float_status *status)
298: {
299: return status->default_nan_mode;
300: }
301:
302: /*----------------------------------------------------------------------------
303: | Routine to raise any or all of the software IEC/IEEE floating-point
304: | exception flags.
305: *----------------------------------------------------------------------------*/
306: //void float_raise(uint8_t flags, float_status *status);
307:
308:
309: /*----------------------------------------------------------------------------
310: | The pattern for a default generated single-precision NaN.
311: *----------------------------------------------------------------------------*/
312: #define float32_default_nan 0x7FFFFFFF
313:
314: /*----------------------------------------------------------------------------
315: | The pattern for a default generated double-precision NaN.
316: *----------------------------------------------------------------------------*/
317: #define float64_default_nan LIT64( 0x7FFFFFFFFFFFFFFF )
318:
319: /*----------------------------------------------------------------------------
320: | The pattern for a default generated extended double-precision NaN. The
321: | `high' and `low' values hold the most- and least-significant bits,
322: | respectively.
323: *----------------------------------------------------------------------------*/
324: #define floatx80_default_nan_high 0x7FFF
325: #define floatx80_default_nan_low LIT64( 0xFFFFFFFFFFFFFFFF )
326:
327: /*----------------------------------------------------------------------------
328: | The pattern for a default generated extended double-precision infinity.
329: *----------------------------------------------------------------------------*/
330: #define floatx80_default_infinity_low LIT64( 0x0000000000000000 )
331:
332: /*----------------------------------------------------------------------------
333: | If `a' is denormal and we are in flush-to-zero mode then set the
334: | input-denormal exception and return zero. Otherwise just return the value.
335: *----------------------------------------------------------------------------*/
336: float64 float64_squash_input_denormal(float64 a, float_status *status);
337:
338: /*----------------------------------------------------------------------------
339: | Options to indicate which negations to perform in float*_muladd()
340: | Using these differs from negating an input or output before calling
341: | the muladd function in that this means that a NaN doesn't have its
342: | sign bit inverted before it is propagated.
343: | We also support halving the result before rounding, as a special
344: | case to support the ARM fused-sqrt-step instruction FRSQRTS.
345: *----------------------------------------------------------------------------*/
346: enum {
347: float_muladd_negate_c = 1,
348: float_muladd_negate_product = 2,
349: float_muladd_negate_result = 4,
350: float_muladd_halve_result = 8,
351: };
352:
353: /*----------------------------------------------------------------------------
354: | Software IEC/IEEE integer-to-floating-point conversion routines.
355: *----------------------------------------------------------------------------*/
356:
357: floatx80 int32_to_floatx80(int32_t);
358: floatx80 int64_to_floatx80(int64_t);
359:
360: /*----------------------------------------------------------------------------
361: | Software IEC/IEEE single-precision conversion routines.
362: *----------------------------------------------------------------------------*/
363: floatx80 float32_to_floatx80(float32, float_status *status);
364: floatx80 float32_to_floatx80_allowunnormal(float32, float_status *status);
365:
366: /*----------------------------------------------------------------------------
367: | Software IEC/IEEE double-precision conversion routines.
368: *----------------------------------------------------------------------------*/
369: floatx80 float64_to_floatx80(float64, float_status *status);
370:
371: floatx80 float64_to_floatx80_allowunnormal( float64 a, float_status *status );
372:
373: /*----------------------------------------------------------------------------
374: | Software IEC/IEEE extended double-precision conversion routines.
375: *----------------------------------------------------------------------------*/
376: int32_t floatx80_to_int32(floatx80, float_status *status);
377: #ifdef SOFTFLOAT_68K
378: int16_t floatx80_to_int16(floatx80, float_status *status);
379: int8_t floatx80_to_int8(floatx80, float_status *status);
380: #endif
381: int32_t floatx80_to_int32_round_to_zero(floatx80, float_status *status);
382: int64_t floatx80_to_int64(floatx80, float_status *status);
383: float32 floatx80_to_float32(floatx80, float_status *status);
384: float64 floatx80_to_float64(floatx80, float_status *status);
385: #ifdef SOFTFLOAT_68K
386: floatx80 floatx80_to_floatx80( floatx80, float_status *status);
387: floatx80 floatdecimal_to_floatx80(floatx80, float_status *status);
388: floatx80 floatx80_to_floatdecimal(floatx80, int32_t*, float_status *status);
389: #endif
390:
391: uint64_t extractFloatx80Frac( floatx80 a );
392: int32_t extractFloatx80Exp( floatx80 a );
393: flag extractFloatx80Sign( floatx80 a );
394:
395: floatx80 floatx80_round_to_int_toward_zero( floatx80 a, float_status *status);
396: floatx80 floatx80_round_to_float32( floatx80, float_status *status );
397: floatx80 floatx80_round_to_float64( floatx80, float_status *status );
398: floatx80 floatx80_round32( floatx80, float_status *status);
399: floatx80 floatx80_round64( floatx80, float_status *status);
400:
401: flag floatx80_eq( floatx80, floatx80, float_status *status);
402: flag floatx80_le( floatx80, floatx80, float_status *status);
403: flag floatx80_lt( floatx80, floatx80, float_status *status);
404:
405: #ifdef SOFTFLOAT_68K
406: // functions are in softfloat.c
407: floatx80 roundSaveFloatx80Internal( int8_t roundingPrecision, flag zSign, int32_t zExp, uint64_t zSig0, uint64_t zSig1, float_status *status );
408: void getRoundedFloatInternal( int8_t roundingPrecision, flag *pzSign, int32_t *pzExp, uint64_t *pzSig );
409: floatx80 roundSigAndPackFloatx80( int8_t roundingPrecision, flag zSign, int32_t zExp, uint64_t zSig0, uint64_t zSig1, float_status *status );
410: floatx80 floatx80_move( floatx80 a, float_status *status );
411: floatx80 floatx80_abs( floatx80 a, float_status *status );
412: floatx80 floatx80_neg( floatx80 a, float_status *status );
413: floatx80 floatx80_getexp( floatx80 a, float_status *status );
414: floatx80 floatx80_getman( floatx80 a, float_status *status );
415: floatx80 floatx80_scale(floatx80 a, floatx80 b, float_status *status );
416: floatx80 floatx80_rem( floatx80 a, floatx80 b, uint64_t *q, flag *s, float_status *status );
417: floatx80 floatx80_mod( floatx80 a, floatx80 b, uint64_t *q, flag *s, float_status *status );
418: floatx80 floatx80_sglmul( floatx80 a, floatx80 b, float_status *status );
419: floatx80 floatx80_sgldiv( floatx80 a, floatx80 b, float_status *status );
420: floatx80 floatx80_cmp( floatx80 a, floatx80 b, float_status *status );
421: floatx80 floatx80_tst( floatx80 a, float_status *status );
422:
423: // functions are in softfloat_fpsp.c
424: floatx80 floatx80_acos(floatx80 a, float_status *status);
425: floatx80 floatx80_asin(floatx80 a, float_status *status);
426: floatx80 floatx80_atan(floatx80 a, float_status *status);
427: floatx80 floatx80_atanh(floatx80 a, float_status *status);
428: floatx80 floatx80_cos(floatx80 a, float_status *status);
429: floatx80 floatx80_cosh(floatx80 a, float_status *status);
430: floatx80 floatx80_etox(floatx80 a, float_status *status);
431: floatx80 floatx80_etoxm1(floatx80 a, float_status *status);
432: floatx80 floatx80_log10(floatx80 a, float_status *status);
433: floatx80 floatx80_log2(floatx80 a, float_status *status);
434: floatx80 floatx80_logn(floatx80 a, float_status *status);
435: floatx80 floatx80_lognp1(floatx80 a, float_status *status);
436: floatx80 floatx80_sin(floatx80 a, float_status *status);
437: floatx80 floatx80_sinh(floatx80 a, float_status *status);
438: floatx80 floatx80_tan(floatx80 a, float_status *status);
439: floatx80 floatx80_tanh(floatx80 a, float_status *status);
440: floatx80 floatx80_tentox(floatx80 a, float_status *status);
441: floatx80 floatx80_twotox(floatx80 a, float_status *status);
442: #endif
443:
444: // functions originally internal to softfloat.c
445: void normalizeFloatx80Subnormal( uint64_t aSig, int32_t *zExpPtr, uint64_t *zSigPtr );
446: floatx80 packFloatx80( flag zSign, int32_t zExp, uint64_t zSig );
447: floatx80 roundAndPackFloatx80(int8_t roundingPrecision, flag zSign, int32_t zExp, uint64_t zSig0, uint64_t zSig1, float_status *status);
448:
449: /*----------------------------------------------------------------------------
450: | Software IEC/IEEE extended double-precision operations.
451: *----------------------------------------------------------------------------*/
452: floatx80 floatx80_round_to_int(floatx80, float_status *status);
453: floatx80 floatx80_add(floatx80, floatx80, float_status *status);
454: floatx80 floatx80_sub(floatx80, floatx80, float_status *status);
455: floatx80 floatx80_mul(floatx80, floatx80, float_status *status);
456: floatx80 floatx80_div(floatx80, floatx80, float_status *status);
457: floatx80 floatx80_sqrt(floatx80, float_status *status);
458: floatx80 floatx80_normalize(floatx80);
459: floatx80 floatx80_denormalize(floatx80, flag);
460:
461: static inline int floatx80_is_zero_or_denormal(floatx80 a)
462: {
463: return (a.high & 0x7fff) == 0;
464: }
465:
466: static inline int floatx80_is_any_nan(floatx80 a)
467: {
468: return ((a.high & 0x7fff) == 0x7fff) && (a.low<<1);
469: }
470:
471: /*----------------------------------------------------------------------------
472: | Return whether the given value is an invalid floatx80 encoding.
473: | Invalid floatx80 encodings arise when the integer bit is not set, but
474: | the exponent is not zero. The only times the integer bit is permitted to
475: | be zero is in subnormal numbers and the value zero.
476: | This includes what the Intel software developer's manual calls pseudo-NaNs,
477: | pseudo-infinities and un-normal numbers. It does not include
478: | pseudo-denormals, which must still be correctly handled as inputs even
479: | if they are never generated as outputs.
480: *----------------------------------------------------------------------------*/
481: static inline bool floatx80_invalid_encoding(floatx80 a)
482: {
483: return (a.low & (1ULL << 63)) == 0 && (a.high & 0x7FFF) != 0 && (a.high & 0x7FFF) != 0x7FFF;
484: }
485:
486: #define floatx80_zero make_floatx80(0x0000, 0x0000000000000000LL)
487: #define floatx80_one make_floatx80(0x3fff, 0x8000000000000000LL)
488: #define floatx80_ln2 make_floatx80(0x3ffe, 0xb17217f7d1cf79acLL)
489: #define floatx80_pi make_floatx80(0x4000, 0xc90fdaa22168c235LL)
490: #define floatx80_half make_floatx80(0x3ffe, 0x8000000000000000LL)
491: #define floatx80_infinity make_floatx80(0x7fff, 0x8000000000000000LL)
492:
493: #endif /* SOFTFLOAT_H */
This archive runs on limited infrastructure. Preserving old code on modern bandwidth. Automated agents are requested to crawl responsibly.