qemu with hax to log dma reads & writes
jcs.org/2018/11/12/vfio
1/*
2 * QEMU float support
3 *
4 * The code in this source file is derived from release 2a of the SoftFloat
5 * IEC/IEEE Floating-point Arithmetic Package. Those parts of the code (and
6 * some later contributions) are provided under that license, as detailed below.
7 * It has subsequently been modified by contributors to the QEMU Project,
8 * so some portions are provided under:
9 * the SoftFloat-2a license
10 * the BSD license
11 * GPL-v2-or-later
12 *
13 * Any future contributions to this file after December 1st 2014 will be
14 * taken to be licensed under the Softfloat-2a license unless specifically
15 * indicated otherwise.
16 */
17
18/*
19===============================================================================
20This C header file is part of the SoftFloat IEC/IEEE Floating-point
21Arithmetic Package, Release 2a.
22
23Written by John R. Hauser. This work was made possible in part by the
24International Computer Science Institute, located at Suite 600, 1947 Center
25Street, Berkeley, California 94704. Funding was partially provided by the
26National Science Foundation under grant MIP-9311980. The original version
27of this code was written as part of a project to build a fixed-point vector
28processor in collaboration with the University of California at Berkeley,
29overseen by Profs. Nelson Morgan and John Wawrzynek. More information
30is available through the Web page `http://HTTP.CS.Berkeley.EDU/~jhauser/
31arithmetic/SoftFloat.html'.
32
33THIS SOFTWARE IS DISTRIBUTED AS IS, FOR FREE. Although reasonable effort
34has been made to avoid it, THIS SOFTWARE MAY CONTAIN FAULTS THAT WILL AT
35TIMES RESULT IN INCORRECT BEHAVIOR. USE OF THIS SOFTWARE IS RESTRICTED TO
36PERSONS AND ORGANIZATIONS WHO CAN AND WILL TAKE FULL RESPONSIBILITY FOR ANY
37AND ALL LOSSES, COSTS, OR OTHER PROBLEMS ARISING FROM ITS USE.
38
39Derivative works are acceptable, even for commercial purposes, so long as
40(1) they include prominent notice that the work is derivative, and (2) they
41include prominent notice akin to these four paragraphs for those parts of
42this code that are retained.
43
44===============================================================================
45*/
46
47/* BSD licensing:
48 * Copyright (c) 2006, Fabrice Bellard
49 * All rights reserved.
50 *
51 * Redistribution and use in source and binary forms, with or without
52 * modification, are permitted provided that the following conditions are met:
53 *
54 * 1. Redistributions of source code must retain the above copyright notice,
55 * this list of conditions and the following disclaimer.
56 *
57 * 2. Redistributions in binary form must reproduce the above copyright notice,
58 * this list of conditions and the following disclaimer in the documentation
59 * and/or other materials provided with the distribution.
60 *
61 * 3. Neither the name of the copyright holder nor the names of its contributors
62 * may be used to endorse or promote products derived from this software without
63 * specific prior written permission.
64 *
65 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
66 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
67 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
68 * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
69 * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
70 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
71 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
72 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
73 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
74 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF
75 * THE POSSIBILITY OF SUCH DAMAGE.
76 */
77
78/* Portions of this work are licensed under the terms of the GNU GPL,
79 * version 2 or later. See the COPYING file in the top-level directory.
80 */
81
82#ifndef SOFTFLOAT_H
83#define SOFTFLOAT_H
84
85#define LIT64( a ) a##LL
86
87/*----------------------------------------------------------------------------
88| Software IEC/IEEE floating-point ordering relations
89*----------------------------------------------------------------------------*/
90enum {
91 float_relation_less = -1,
92 float_relation_equal = 0,
93 float_relation_greater = 1,
94 float_relation_unordered = 2
95};
96
97#include "fpu/softfloat-types.h"
98
99static inline void set_float_detect_tininess(int val, float_status *status)
100{
101 status->float_detect_tininess = val;
102}
103static inline void set_float_rounding_mode(int val, float_status *status)
104{
105 status->float_rounding_mode = val;
106}
107static inline void set_float_exception_flags(int val, float_status *status)
108{
109 status->float_exception_flags = val;
110}
111static inline void set_floatx80_rounding_precision(int val,
112 float_status *status)
113{
114 status->floatx80_rounding_precision = val;
115}
116static inline void set_flush_to_zero(flag val, float_status *status)
117{
118 status->flush_to_zero = val;
119}
120static inline void set_flush_inputs_to_zero(flag val, float_status *status)
121{
122 status->flush_inputs_to_zero = val;
123}
124static inline void set_default_nan_mode(flag val, float_status *status)
125{
126 status->default_nan_mode = val;
127}
128static inline void set_snan_bit_is_one(flag val, float_status *status)
129{
130 status->snan_bit_is_one = val;
131}
132static inline int get_float_detect_tininess(float_status *status)
133{
134 return status->float_detect_tininess;
135}
136static inline int get_float_rounding_mode(float_status *status)
137{
138 return status->float_rounding_mode;
139}
140static inline int get_float_exception_flags(float_status *status)
141{
142 return status->float_exception_flags;
143}
144static inline int get_floatx80_rounding_precision(float_status *status)
145{
146 return status->floatx80_rounding_precision;
147}
148static inline flag get_flush_to_zero(float_status *status)
149{
150 return status->flush_to_zero;
151}
152static inline flag get_flush_inputs_to_zero(float_status *status)
153{
154 return status->flush_inputs_to_zero;
155}
156static inline flag get_default_nan_mode(float_status *status)
157{
158 return status->default_nan_mode;
159}
160
161/*----------------------------------------------------------------------------
162| Routine to raise any or all of the software IEC/IEEE floating-point
163| exception flags.
164*----------------------------------------------------------------------------*/
165void float_raise(uint8_t flags, float_status *status);
166
167/*----------------------------------------------------------------------------
168| If `a' is denormal and we are in flush-to-zero mode then set the
169| input-denormal exception and return zero. Otherwise just return the value.
170*----------------------------------------------------------------------------*/
171float16 float16_squash_input_denormal(float16 a, float_status *status);
172float32 float32_squash_input_denormal(float32 a, float_status *status);
173float64 float64_squash_input_denormal(float64 a, float_status *status);
174
175/*----------------------------------------------------------------------------
176| Options to indicate which negations to perform in float*_muladd()
177| Using these differs from negating an input or output before calling
178| the muladd function in that this means that a NaN doesn't have its
179| sign bit inverted before it is propagated.
180| We also support halving the result before rounding, as a special
181| case to support the ARM fused-sqrt-step instruction FRSQRTS.
182*----------------------------------------------------------------------------*/
183enum {
184 float_muladd_negate_c = 1,
185 float_muladd_negate_product = 2,
186 float_muladd_negate_result = 4,
187 float_muladd_halve_result = 8,
188};
189
190/*----------------------------------------------------------------------------
191| Software IEC/IEEE integer-to-floating-point conversion routines.
192*----------------------------------------------------------------------------*/
193float32 int16_to_float32(int16_t, float_status *status);
194float32 int32_to_float32(int32_t, float_status *status);
195float64 int16_to_float64(int16_t, float_status *status);
196float64 int32_to_float64(int32_t, float_status *status);
197float32 uint16_to_float32(uint16_t, float_status *status);
198float32 uint32_to_float32(uint32_t, float_status *status);
199float64 uint16_to_float64(uint16_t, float_status *status);
200float64 uint32_to_float64(uint32_t, float_status *status);
201floatx80 int32_to_floatx80(int32_t, float_status *status);
202float128 int32_to_float128(int32_t, float_status *status);
203float32 int64_to_float32(int64_t, float_status *status);
204float64 int64_to_float64(int64_t, float_status *status);
205floatx80 int64_to_floatx80(int64_t, float_status *status);
206float128 int64_to_float128(int64_t, float_status *status);
207float32 uint64_to_float32(uint64_t, float_status *status);
208float64 uint64_to_float64(uint64_t, float_status *status);
209float128 uint64_to_float128(uint64_t, float_status *status);
210
211/*----------------------------------------------------------------------------
212| Software half-precision conversion routines.
213*----------------------------------------------------------------------------*/
214float16 float32_to_float16(float32, flag, float_status *status);
215float32 float16_to_float32(float16, flag, float_status *status);
216float16 float64_to_float16(float64 a, flag ieee, float_status *status);
217float64 float16_to_float64(float16 a, flag ieee, float_status *status);
218int16_t float16_to_int16(float16, float_status *status);
219uint16_t float16_to_uint16(float16 a, float_status *status);
220int16_t float16_to_int16_round_to_zero(float16, float_status *status);
221uint16_t float16_to_uint16_round_to_zero(float16 a, float_status *status);
222int32_t float16_to_int32(float16, float_status *status);
223uint32_t float16_to_uint32(float16 a, float_status *status);
224int32_t float16_to_int32_round_to_zero(float16, float_status *status);
225uint32_t float16_to_uint32_round_to_zero(float16 a, float_status *status);
226int64_t float16_to_int64(float16, float_status *status);
227uint64_t float16_to_uint64(float16 a, float_status *status);
228int64_t float16_to_int64_round_to_zero(float16, float_status *status);
229uint64_t float16_to_uint64_round_to_zero(float16 a, float_status *status);
230float16 int16_to_float16(int16_t a, float_status *status);
231float16 int32_to_float16(int32_t a, float_status *status);
232float16 int64_to_float16(int64_t a, float_status *status);
233float16 uint16_to_float16(uint16_t a, float_status *status);
234float16 uint32_to_float16(uint32_t a, float_status *status);
235float16 uint64_to_float16(uint64_t a, float_status *status);
236
237/*----------------------------------------------------------------------------
238| Software half-precision operations.
239*----------------------------------------------------------------------------*/
240
241float16 float16_round_to_int(float16, float_status *status);
242float16 float16_add(float16, float16, float_status *status);
243float16 float16_sub(float16, float16, float_status *status);
244float16 float16_mul(float16, float16, float_status *status);
245float16 float16_muladd(float16, float16, float16, int, float_status *status);
246float16 float16_div(float16, float16, float_status *status);
247float16 float16_scalbn(float16, int, float_status *status);
248float16 float16_min(float16, float16, float_status *status);
249float16 float16_max(float16, float16, float_status *status);
250float16 float16_minnum(float16, float16, float_status *status);
251float16 float16_maxnum(float16, float16, float_status *status);
252float16 float16_minnummag(float16, float16, float_status *status);
253float16 float16_maxnummag(float16, float16, float_status *status);
254float16 float16_sqrt(float16, float_status *status);
255int float16_compare(float16, float16, float_status *status);
256int float16_compare_quiet(float16, float16, float_status *status);
257
258int float16_is_quiet_nan(float16, float_status *status);
259int float16_is_signaling_nan(float16, float_status *status);
260float16 float16_maybe_silence_nan(float16, float_status *status);
261
262static inline int float16_is_any_nan(float16 a)
263{
264 return ((float16_val(a) & ~0x8000) > 0x7c00);
265}
266
267static inline int float16_is_neg(float16 a)
268{
269 return float16_val(a) >> 15;
270}
271
272static inline int float16_is_infinity(float16 a)
273{
274 return (float16_val(a) & 0x7fff) == 0x7c00;
275}
276
277static inline int float16_is_zero(float16 a)
278{
279 return (float16_val(a) & 0x7fff) == 0;
280}
281
282static inline int float16_is_zero_or_denormal(float16 a)
283{
284 return (float16_val(a) & 0x7c00) == 0;
285}
286
287static inline float16 float16_abs(float16 a)
288{
289 /* Note that abs does *not* handle NaN specially, nor does
290 * it flush denormal inputs to zero.
291 */
292 return make_float16(float16_val(a) & 0x7fff);
293}
294
295static inline float16 float16_chs(float16 a)
296{
297 /* Note that chs does *not* handle NaN specially, nor does
298 * it flush denormal inputs to zero.
299 */
300 return make_float16(float16_val(a) ^ 0x8000);
301}
302
303static inline float16 float16_set_sign(float16 a, int sign)
304{
305 return make_float16((float16_val(a) & 0x7fff) | (sign << 15));
306}
307
308#define float16_zero make_float16(0)
309#define float16_half make_float16(0x3800)
310#define float16_one make_float16(0x3c00)
311#define float16_one_point_five make_float16(0x3e00)
312#define float16_two make_float16(0x4000)
313#define float16_three make_float16(0x4200)
314#define float16_infinity make_float16(0x7c00)
315
316/*----------------------------------------------------------------------------
317| The pattern for a default generated half-precision NaN.
318*----------------------------------------------------------------------------*/
319float16 float16_default_nan(float_status *status);
320
321/*----------------------------------------------------------------------------
322| Software IEC/IEEE single-precision conversion routines.
323*----------------------------------------------------------------------------*/
324int16_t float32_to_int16(float32, float_status *status);
325uint16_t float32_to_uint16(float32, float_status *status);
326int16_t float32_to_int16_round_to_zero(float32, float_status *status);
327uint16_t float32_to_uint16_round_to_zero(float32, float_status *status);
328int32_t float32_to_int32(float32, float_status *status);
329int32_t float32_to_int32_round_to_zero(float32, float_status *status);
330uint32_t float32_to_uint32(float32, float_status *status);
331uint32_t float32_to_uint32_round_to_zero(float32, float_status *status);
332int64_t float32_to_int64(float32, float_status *status);
333uint64_t float32_to_uint64(float32, float_status *status);
334uint64_t float32_to_uint64_round_to_zero(float32, float_status *status);
335int64_t float32_to_int64_round_to_zero(float32, float_status *status);
336float64 float32_to_float64(float32, float_status *status);
337floatx80 float32_to_floatx80(float32, float_status *status);
338float128 float32_to_float128(float32, float_status *status);
339
340/*----------------------------------------------------------------------------
341| Software IEC/IEEE single-precision operations.
342*----------------------------------------------------------------------------*/
343float32 float32_round_to_int(float32, float_status *status);
344float32 float32_add(float32, float32, float_status *status);
345float32 float32_sub(float32, float32, float_status *status);
346float32 float32_mul(float32, float32, float_status *status);
347float32 float32_div(float32, float32, float_status *status);
348float32 float32_rem(float32, float32, float_status *status);
349float32 float32_muladd(float32, float32, float32, int, float_status *status);
350float32 float32_sqrt(float32, float_status *status);
351float32 float32_exp2(float32, float_status *status);
352float32 float32_log2(float32, float_status *status);
353int float32_eq(float32, float32, float_status *status);
354int float32_le(float32, float32, float_status *status);
355int float32_lt(float32, float32, float_status *status);
356int float32_unordered(float32, float32, float_status *status);
357int float32_eq_quiet(float32, float32, float_status *status);
358int float32_le_quiet(float32, float32, float_status *status);
359int float32_lt_quiet(float32, float32, float_status *status);
360int float32_unordered_quiet(float32, float32, float_status *status);
361int float32_compare(float32, float32, float_status *status);
362int float32_compare_quiet(float32, float32, float_status *status);
363float32 float32_min(float32, float32, float_status *status);
364float32 float32_max(float32, float32, float_status *status);
365float32 float32_minnum(float32, float32, float_status *status);
366float32 float32_maxnum(float32, float32, float_status *status);
367float32 float32_minnummag(float32, float32, float_status *status);
368float32 float32_maxnummag(float32, float32, float_status *status);
369int float32_is_quiet_nan(float32, float_status *status);
370int float32_is_signaling_nan(float32, float_status *status);
371float32 float32_maybe_silence_nan(float32, float_status *status);
372float32 float32_scalbn(float32, int, float_status *status);
373
374static inline float32 float32_abs(float32 a)
375{
376 /* Note that abs does *not* handle NaN specially, nor does
377 * it flush denormal inputs to zero.
378 */
379 return make_float32(float32_val(a) & 0x7fffffff);
380}
381
382static inline float32 float32_chs(float32 a)
383{
384 /* Note that chs does *not* handle NaN specially, nor does
385 * it flush denormal inputs to zero.
386 */
387 return make_float32(float32_val(a) ^ 0x80000000);
388}
389
390static inline int float32_is_infinity(float32 a)
391{
392 return (float32_val(a) & 0x7fffffff) == 0x7f800000;
393}
394
395static inline int float32_is_neg(float32 a)
396{
397 return float32_val(a) >> 31;
398}
399
400static inline int float32_is_zero(float32 a)
401{
402 return (float32_val(a) & 0x7fffffff) == 0;
403}
404
405static inline int float32_is_any_nan(float32 a)
406{
407 return ((float32_val(a) & ~(1 << 31)) > 0x7f800000UL);
408}
409
410static inline int float32_is_zero_or_denormal(float32 a)
411{
412 return (float32_val(a) & 0x7f800000) == 0;
413}
414
415static inline float32 float32_set_sign(float32 a, int sign)
416{
417 return make_float32((float32_val(a) & 0x7fffffff) | (sign << 31));
418}
419
420#define float32_zero make_float32(0)
421#define float32_half make_float32(0x3f000000)
422#define float32_one make_float32(0x3f800000)
423#define float32_one_point_five make_float32(0x3fc00000)
424#define float32_two make_float32(0x40000000)
425#define float32_three make_float32(0x40400000)
426#define float32_infinity make_float32(0x7f800000)
427
428/*----------------------------------------------------------------------------
429| Packs the sign `zSign', exponent `zExp', and significand `zSig' into a
430| single-precision floating-point value, returning the result. After being
431| shifted into the proper positions, the three fields are simply added
432| together to form the result. This means that any integer portion of `zSig'
433| will be added into the exponent. Since a properly normalized significand
434| will have an integer portion equal to 1, the `zExp' input should be 1 less
435| than the desired result exponent whenever `zSig' is a complete, normalized
436| significand.
437*----------------------------------------------------------------------------*/
438
439static inline float32 packFloat32(flag zSign, int zExp, uint32_t zSig)
440{
441 return make_float32(
442 (((uint32_t)zSign) << 31) + (((uint32_t)zExp) << 23) + zSig);
443}
444
445/*----------------------------------------------------------------------------
446| The pattern for a default generated single-precision NaN.
447*----------------------------------------------------------------------------*/
448float32 float32_default_nan(float_status *status);
449
450/*----------------------------------------------------------------------------
451| Software IEC/IEEE double-precision conversion routines.
452*----------------------------------------------------------------------------*/
453int16_t float64_to_int16(float64, float_status *status);
454uint16_t float64_to_uint16(float64, float_status *status);
455int16_t float64_to_int16_round_to_zero(float64, float_status *status);
456uint16_t float64_to_uint16_round_to_zero(float64, float_status *status);
457int32_t float64_to_int32(float64, float_status *status);
458int32_t float64_to_int32_round_to_zero(float64, float_status *status);
459uint32_t float64_to_uint32(float64, float_status *status);
460uint32_t float64_to_uint32_round_to_zero(float64, float_status *status);
461int64_t float64_to_int64(float64, float_status *status);
462int64_t float64_to_int64_round_to_zero(float64, float_status *status);
463uint64_t float64_to_uint64(float64 a, float_status *status);
464uint64_t float64_to_uint64_round_to_zero(float64 a, float_status *status);
465float32 float64_to_float32(float64, float_status *status);
466floatx80 float64_to_floatx80(float64, float_status *status);
467float128 float64_to_float128(float64, float_status *status);
468
469/*----------------------------------------------------------------------------
470| Software IEC/IEEE double-precision operations.
471*----------------------------------------------------------------------------*/
472float64 float64_round_to_int(float64, float_status *status);
473float64 float64_trunc_to_int(float64, float_status *status);
474float64 float64_add(float64, float64, float_status *status);
475float64 float64_sub(float64, float64, float_status *status);
476float64 float64_mul(float64, float64, float_status *status);
477float64 float64_div(float64, float64, float_status *status);
478float64 float64_rem(float64, float64, float_status *status);
479float64 float64_muladd(float64, float64, float64, int, float_status *status);
480float64 float64_sqrt(float64, float_status *status);
481float64 float64_log2(float64, float_status *status);
482int float64_eq(float64, float64, float_status *status);
483int float64_le(float64, float64, float_status *status);
484int float64_lt(float64, float64, float_status *status);
485int float64_unordered(float64, float64, float_status *status);
486int float64_eq_quiet(float64, float64, float_status *status);
487int float64_le_quiet(float64, float64, float_status *status);
488int float64_lt_quiet(float64, float64, float_status *status);
489int float64_unordered_quiet(float64, float64, float_status *status);
490int float64_compare(float64, float64, float_status *status);
491int float64_compare_quiet(float64, float64, float_status *status);
492float64 float64_min(float64, float64, float_status *status);
493float64 float64_max(float64, float64, float_status *status);
494float64 float64_minnum(float64, float64, float_status *status);
495float64 float64_maxnum(float64, float64, float_status *status);
496float64 float64_minnummag(float64, float64, float_status *status);
497float64 float64_maxnummag(float64, float64, float_status *status);
498int float64_is_quiet_nan(float64 a, float_status *status);
499int float64_is_signaling_nan(float64, float_status *status);
500float64 float64_maybe_silence_nan(float64, float_status *status);
501float64 float64_scalbn(float64, int, float_status *status);
502
503static inline float64 float64_abs(float64 a)
504{
505 /* Note that abs does *not* handle NaN specially, nor does
506 * it flush denormal inputs to zero.
507 */
508 return make_float64(float64_val(a) & 0x7fffffffffffffffLL);
509}
510
511static inline float64 float64_chs(float64 a)
512{
513 /* Note that chs does *not* handle NaN specially, nor does
514 * it flush denormal inputs to zero.
515 */
516 return make_float64(float64_val(a) ^ 0x8000000000000000LL);
517}
518
519static inline int float64_is_infinity(float64 a)
520{
521 return (float64_val(a) & 0x7fffffffffffffffLL ) == 0x7ff0000000000000LL;
522}
523
524static inline int float64_is_neg(float64 a)
525{
526 return float64_val(a) >> 63;
527}
528
529static inline int float64_is_zero(float64 a)
530{
531 return (float64_val(a) & 0x7fffffffffffffffLL) == 0;
532}
533
534static inline int float64_is_any_nan(float64 a)
535{
536 return ((float64_val(a) & ~(1ULL << 63)) > 0x7ff0000000000000ULL);
537}
538
539static inline int float64_is_zero_or_denormal(float64 a)
540{
541 return (float64_val(a) & 0x7ff0000000000000LL) == 0;
542}
543
544static inline float64 float64_set_sign(float64 a, int sign)
545{
546 return make_float64((float64_val(a) & 0x7fffffffffffffffULL)
547 | ((int64_t)sign << 63));
548}
549
550#define float64_zero make_float64(0)
551#define float64_half make_float64(0x3fe0000000000000LL)
552#define float64_one make_float64(0x3ff0000000000000LL)
553#define float64_one_point_five make_float64(0x3FF8000000000000ULL)
554#define float64_two make_float64(0x4000000000000000ULL)
555#define float64_three make_float64(0x4008000000000000ULL)
556#define float64_ln2 make_float64(0x3fe62e42fefa39efLL)
557#define float64_infinity make_float64(0x7ff0000000000000LL)
558
559/*----------------------------------------------------------------------------
560| The pattern for a default generated double-precision NaN.
561*----------------------------------------------------------------------------*/
562float64 float64_default_nan(float_status *status);
563
564/*----------------------------------------------------------------------------
565| Software IEC/IEEE extended double-precision conversion routines.
566*----------------------------------------------------------------------------*/
567int32_t floatx80_to_int32(floatx80, float_status *status);
568int32_t floatx80_to_int32_round_to_zero(floatx80, float_status *status);
569int64_t floatx80_to_int64(floatx80, float_status *status);
570int64_t floatx80_to_int64_round_to_zero(floatx80, float_status *status);
571float32 floatx80_to_float32(floatx80, float_status *status);
572float64 floatx80_to_float64(floatx80, float_status *status);
573float128 floatx80_to_float128(floatx80, float_status *status);
574
575/*----------------------------------------------------------------------------
576| The pattern for an extended double-precision inf.
577*----------------------------------------------------------------------------*/
578extern const floatx80 floatx80_infinity;
579
580/*----------------------------------------------------------------------------
581| Software IEC/IEEE extended double-precision operations.
582*----------------------------------------------------------------------------*/
583floatx80 floatx80_round(floatx80 a, float_status *status);
584floatx80 floatx80_round_to_int(floatx80, float_status *status);
585floatx80 floatx80_add(floatx80, floatx80, float_status *status);
586floatx80 floatx80_sub(floatx80, floatx80, float_status *status);
587floatx80 floatx80_mul(floatx80, floatx80, float_status *status);
588floatx80 floatx80_div(floatx80, floatx80, float_status *status);
589floatx80 floatx80_rem(floatx80, floatx80, float_status *status);
590floatx80 floatx80_sqrt(floatx80, float_status *status);
591int floatx80_eq(floatx80, floatx80, float_status *status);
592int floatx80_le(floatx80, floatx80, float_status *status);
593int floatx80_lt(floatx80, floatx80, float_status *status);
594int floatx80_unordered(floatx80, floatx80, float_status *status);
595int floatx80_eq_quiet(floatx80, floatx80, float_status *status);
596int floatx80_le_quiet(floatx80, floatx80, float_status *status);
597int floatx80_lt_quiet(floatx80, floatx80, float_status *status);
598int floatx80_unordered_quiet(floatx80, floatx80, float_status *status);
599int floatx80_compare(floatx80, floatx80, float_status *status);
600int floatx80_compare_quiet(floatx80, floatx80, float_status *status);
601int floatx80_is_quiet_nan(floatx80, float_status *status);
602int floatx80_is_signaling_nan(floatx80, float_status *status);
603floatx80 floatx80_maybe_silence_nan(floatx80, float_status *status);
604floatx80 floatx80_scalbn(floatx80, int, float_status *status);
605
606static inline floatx80 floatx80_abs(floatx80 a)
607{
608 a.high &= 0x7fff;
609 return a;
610}
611
612static inline floatx80 floatx80_chs(floatx80 a)
613{
614 a.high ^= 0x8000;
615 return a;
616}
617
618static inline int floatx80_is_infinity(floatx80 a)
619{
620#if defined(TARGET_M68K)
621 return (a.high & 0x7fff) == floatx80_infinity.high && !(a.low << 1);
622#else
623 return (a.high & 0x7fff) == floatx80_infinity.high &&
624 a.low == floatx80_infinity.low;
625#endif
626}
627
628static inline int floatx80_is_neg(floatx80 a)
629{
630 return a.high >> 15;
631}
632
633static inline int floatx80_is_zero(floatx80 a)
634{
635 return (a.high & 0x7fff) == 0 && a.low == 0;
636}
637
638static inline int floatx80_is_zero_or_denormal(floatx80 a)
639{
640 return (a.high & 0x7fff) == 0;
641}
642
643static inline int floatx80_is_any_nan(floatx80 a)
644{
645 return ((a.high & 0x7fff) == 0x7fff) && (a.low<<1);
646}
647
648/*----------------------------------------------------------------------------
649| Return whether the given value is an invalid floatx80 encoding.
650| Invalid floatx80 encodings arise when the integer bit is not set, but
651| the exponent is not zero. The only times the integer bit is permitted to
652| be zero is in subnormal numbers and the value zero.
653| This includes what the Intel software developer's manual calls pseudo-NaNs,
654| pseudo-infinities and un-normal numbers. It does not include
655| pseudo-denormals, which must still be correctly handled as inputs even
656| if they are never generated as outputs.
657*----------------------------------------------------------------------------*/
658static inline bool floatx80_invalid_encoding(floatx80 a)
659{
660 return (a.low & (1ULL << 63)) == 0 && (a.high & 0x7FFF) != 0;
661}
662
663#define floatx80_zero make_floatx80(0x0000, 0x0000000000000000LL)
664#define floatx80_one make_floatx80(0x3fff, 0x8000000000000000LL)
665#define floatx80_ln2 make_floatx80(0x3ffe, 0xb17217f7d1cf79acLL)
666#define floatx80_pi make_floatx80(0x4000, 0xc90fdaa22168c235LL)
667#define floatx80_half make_floatx80(0x3ffe, 0x8000000000000000LL)
668
669/*----------------------------------------------------------------------------
670| Returns the fraction bits of the extended double-precision floating-point
671| value `a'.
672*----------------------------------------------------------------------------*/
673
674static inline uint64_t extractFloatx80Frac(floatx80 a)
675{
676 return a.low;
677}
678
679/*----------------------------------------------------------------------------
680| Returns the exponent bits of the extended double-precision floating-point
681| value `a'.
682*----------------------------------------------------------------------------*/
683
684static inline int32_t extractFloatx80Exp(floatx80 a)
685{
686 return a.high & 0x7FFF;
687}
688
689/*----------------------------------------------------------------------------
690| Returns the sign bit of the extended double-precision floating-point value
691| `a'.
692*----------------------------------------------------------------------------*/
693
694static inline flag extractFloatx80Sign(floatx80 a)
695{
696 return a.high >> 15;
697}
698
699/*----------------------------------------------------------------------------
700| Packs the sign `zSign', exponent `zExp', and significand `zSig' into an
701| extended double-precision floating-point value, returning the result.
702*----------------------------------------------------------------------------*/
703
704static inline floatx80 packFloatx80(flag zSign, int32_t zExp, uint64_t zSig)
705{
706 floatx80 z;
707
708 z.low = zSig;
709 z.high = (((uint16_t)zSign) << 15) + zExp;
710 return z;
711}
712
713/*----------------------------------------------------------------------------
714| Normalizes the subnormal extended double-precision floating-point value
715| represented by the denormalized significand `aSig'. The normalized exponent
716| and significand are stored at the locations pointed to by `zExpPtr' and
717| `zSigPtr', respectively.
718*----------------------------------------------------------------------------*/
719
720void normalizeFloatx80Subnormal(uint64_t aSig, int32_t *zExpPtr,
721 uint64_t *zSigPtr);
722
723/*----------------------------------------------------------------------------
724| Takes two extended double-precision floating-point values `a' and `b', one
725| of which is a NaN, and returns the appropriate NaN result. If either `a' or
726| `b' is a signaling NaN, the invalid exception is raised.
727*----------------------------------------------------------------------------*/
728
729floatx80 propagateFloatx80NaN(floatx80 a, floatx80 b, float_status *status);
730
731/*----------------------------------------------------------------------------
732| Takes an abstract floating-point value having sign `zSign', exponent `zExp',
733| and extended significand formed by the concatenation of `zSig0' and `zSig1',
734| and returns the proper extended double-precision floating-point value
735| corresponding to the abstract input. Ordinarily, the abstract value is
736| rounded and packed into the extended double-precision format, with the
737| inexact exception raised if the abstract input cannot be represented
738| exactly. However, if the abstract value is too large, the overflow and
739| inexact exceptions are raised and an infinity or maximal finite value is
740| returned. If the abstract value is too small, the input value is rounded to
741| a subnormal number, and the underflow and inexact exceptions are raised if
742| the abstract input cannot be represented exactly as a subnormal extended
743| double-precision floating-point number.
744| If `roundingPrecision' is 32 or 64, the result is rounded to the same
745| number of bits as single or double precision, respectively. Otherwise, the
746| result is rounded to the full precision of the extended double-precision
747| format.
748| The input significand must be normalized or smaller. If the input
749| significand is not normalized, `zExp' must be 0; in that case, the result
750| returned is a subnormal number, and it must not require rounding. The
751| handling of underflow and overflow follows the IEC/IEEE Standard for Binary
752| Floating-Point Arithmetic.
753*----------------------------------------------------------------------------*/
754
755floatx80 roundAndPackFloatx80(int8_t roundingPrecision, flag zSign,
756 int32_t zExp, uint64_t zSig0, uint64_t zSig1,
757 float_status *status);
758
759/*----------------------------------------------------------------------------
760| Takes an abstract floating-point value having sign `zSign', exponent
761| `zExp', and significand formed by the concatenation of `zSig0' and `zSig1',
762| and returns the proper extended double-precision floating-point value
763| corresponding to the abstract input. This routine is just like
764| `roundAndPackFloatx80' except that the input significand does not have to be
765| normalized.
766*----------------------------------------------------------------------------*/
767
768floatx80 normalizeRoundAndPackFloatx80(int8_t roundingPrecision,
769 flag zSign, int32_t zExp,
770 uint64_t zSig0, uint64_t zSig1,
771 float_status *status);
772
773/*----------------------------------------------------------------------------
774| The pattern for a default generated extended double-precision NaN.
775*----------------------------------------------------------------------------*/
776floatx80 floatx80_default_nan(float_status *status);
777
778/*----------------------------------------------------------------------------
779| Software IEC/IEEE quadruple-precision conversion routines.
780*----------------------------------------------------------------------------*/
781int32_t float128_to_int32(float128, float_status *status);
782int32_t float128_to_int32_round_to_zero(float128, float_status *status);
783int64_t float128_to_int64(float128, float_status *status);
784int64_t float128_to_int64_round_to_zero(float128, float_status *status);
785uint64_t float128_to_uint64(float128, float_status *status);
786uint64_t float128_to_uint64_round_to_zero(float128, float_status *status);
787uint32_t float128_to_uint32_round_to_zero(float128, float_status *status);
788float32 float128_to_float32(float128, float_status *status);
789float64 float128_to_float64(float128, float_status *status);
790floatx80 float128_to_floatx80(float128, float_status *status);
791
792/*----------------------------------------------------------------------------
793| Software IEC/IEEE quadruple-precision operations.
794*----------------------------------------------------------------------------*/
795float128 float128_round_to_int(float128, float_status *status);
796float128 float128_add(float128, float128, float_status *status);
797float128 float128_sub(float128, float128, float_status *status);
798float128 float128_mul(float128, float128, float_status *status);
799float128 float128_div(float128, float128, float_status *status);
800float128 float128_rem(float128, float128, float_status *status);
801float128 float128_sqrt(float128, float_status *status);
802int float128_eq(float128, float128, float_status *status);
803int float128_le(float128, float128, float_status *status);
804int float128_lt(float128, float128, float_status *status);
805int float128_unordered(float128, float128, float_status *status);
806int float128_eq_quiet(float128, float128, float_status *status);
807int float128_le_quiet(float128, float128, float_status *status);
808int float128_lt_quiet(float128, float128, float_status *status);
809int float128_unordered_quiet(float128, float128, float_status *status);
810int float128_compare(float128, float128, float_status *status);
811int float128_compare_quiet(float128, float128, float_status *status);
812int float128_is_quiet_nan(float128, float_status *status);
813int float128_is_signaling_nan(float128, float_status *status);
814float128 float128_maybe_silence_nan(float128, float_status *status);
815float128 float128_scalbn(float128, int, float_status *status);
816
817static inline float128 float128_abs(float128 a)
818{
819 a.high &= 0x7fffffffffffffffLL;
820 return a;
821}
822
823static inline float128 float128_chs(float128 a)
824{
825 a.high ^= 0x8000000000000000LL;
826 return a;
827}
828
829static inline int float128_is_infinity(float128 a)
830{
831 return (a.high & 0x7fffffffffffffffLL) == 0x7fff000000000000LL && a.low == 0;
832}
833
834static inline int float128_is_neg(float128 a)
835{
836 return a.high >> 63;
837}
838
839static inline int float128_is_zero(float128 a)
840{
841 return (a.high & 0x7fffffffffffffffLL) == 0 && a.low == 0;
842}
843
844static inline int float128_is_zero_or_denormal(float128 a)
845{
846 return (a.high & 0x7fff000000000000LL) == 0;
847}
848
849static inline int float128_is_any_nan(float128 a)
850{
851 return ((a.high >> 48) & 0x7fff) == 0x7fff &&
852 ((a.low != 0) || ((a.high & 0xffffffffffffLL) != 0));
853}
854
855#define float128_zero make_float128(0, 0)
856
857/*----------------------------------------------------------------------------
858| The pattern for a default generated quadruple-precision NaN.
859*----------------------------------------------------------------------------*/
860float128 float128_default_nan(float_status *status);
861
862#endif /* SOFTFLOAT_H */