2 * linux/arch/arm/vfp/vfpsingle.c
4 * This code is derived in part from John R. Housers softfloat library, which
5 * carries the following notice:
7 * ===========================================================================
8 * This C source file is part of the SoftFloat IEC/IEEE Floating-point
9 * Arithmetic Package, Release 2.
11 * Written by John R. Hauser. This work was made possible in part by the
12 * International Computer Science Institute, located at Suite 600, 1947 Center
13 * Street, Berkeley, California 94704. Funding was partially provided by the
14 * National Science Foundation under grant MIP-9311980. The original version
15 * of this code was written as part of a project to build a fixed-point vector
16 * processor in collaboration with the University of California at Berkeley,
17 * overseen by Profs. Nelson Morgan and John Wawrzynek. More information
18 * is available through the web page `http://HTTP.CS.Berkeley.EDU/~jhauser/
19 * arithmetic/softfloat.html'.
21 * THIS SOFTWARE IS DISTRIBUTED AS IS, FOR FREE. Although reasonable effort
22 * has been made to avoid it, THIS SOFTWARE MAY CONTAIN FAULTS THAT WILL AT
23 * TIMES RESULT IN INCORRECT BEHAVIOR. USE OF THIS SOFTWARE IS RESTRICTED TO
24 * PERSONS AND ORGANIZATIONS WHO CAN AND WILL TAKE FULL RESPONSIBILITY FOR ANY
25 * AND ALL LOSSES, COSTS, OR OTHER PROBLEMS ARISING FROM ITS USE.
27 * Derivative works are acceptable, even for commercial purposes, so long as
28 * (1) they include prominent notice that the work is derivative, and (2) they
29 * include prominent notice akin to these three paragraphs for those parts of
30 * this code that are retained.
31 * ===========================================================================
33 #include <linux/kernel.h>
34 #include <linux/bitops.h>
36 #include <asm/div64.h>
42 static struct vfp_single vfp_single_default_qnan
= {
45 .significand
= VFP_SINGLE_SIGNIFICAND_QNAN
,
48 static void vfp_single_dump(const char *str
, struct vfp_single
*s
)
50 pr_debug("VFP: %s: sign=%d exponent=%d significand=%08x\n",
51 str
, s
->sign
!= 0, s
->exponent
, s
->significand
);
54 static void vfp_single_normalise_denormal(struct vfp_single
*vs
)
56 int bits
= 31 - fls(vs
->significand
);
58 vfp_single_dump("normalise_denormal: in", vs
);
61 vs
->exponent
-= bits
- 1;
62 vs
->significand
<<= bits
;
65 vfp_single_dump("normalise_denormal: out", vs
);
69 #define vfp_single_normaliseround(sd,vsd,fpscr,except,func) __vfp_single_normaliseround(sd,vsd,fpscr,except)
70 u32
__vfp_single_normaliseround(int sd
, struct vfp_single
*vs
, u32 fpscr
, u32 exceptions
)
72 u32
vfp_single_normaliseround(int sd
, struct vfp_single
*vs
, u32 fpscr
, u32 exceptions
, const char *func
)
75 u32 significand
, incr
, rmode
;
76 int exponent
, shift
, underflow
;
78 vfp_single_dump("pack: in", vs
);
81 * Infinities and NaNs are a special case.
83 if (vs
->exponent
== 255 && (vs
->significand
== 0 || exceptions
))
89 if (vs
->significand
== 0) {
94 exponent
= vs
->exponent
;
95 significand
= vs
->significand
;
98 * Normalise first. Note that we shift the significand up to
99 * bit 31, so we have VFP_SINGLE_LOW_BITS + 1 below the least
102 shift
= 32 - fls(significand
);
103 if (shift
< 32 && shift
) {
105 significand
<<= shift
;
109 vs
->exponent
= exponent
;
110 vs
->significand
= significand
;
111 vfp_single_dump("pack: normalised", vs
);
117 underflow
= exponent
< 0;
119 significand
= vfp_shiftright32jamming(significand
, -exponent
);
122 vs
->exponent
= exponent
;
123 vs
->significand
= significand
;
124 vfp_single_dump("pack: tiny number", vs
);
126 if (!(significand
& ((1 << (VFP_SINGLE_LOW_BITS
+ 1)) - 1)))
131 * Select rounding increment.
134 rmode
= fpscr
& FPSCR_RMODE_MASK
;
136 if (rmode
== FPSCR_ROUND_NEAREST
) {
137 incr
= 1 << VFP_SINGLE_LOW_BITS
;
138 if ((significand
& (1 << (VFP_SINGLE_LOW_BITS
+ 1))) == 0)
140 } else if (rmode
== FPSCR_ROUND_TOZERO
) {
142 } else if ((rmode
== FPSCR_ROUND_PLUSINF
) ^ (vs
->sign
!= 0))
143 incr
= (1 << (VFP_SINGLE_LOW_BITS
+ 1)) - 1;
145 pr_debug("VFP: rounding increment = 0x%08x\n", incr
);
148 * Is our rounding going to overflow?
150 if ((significand
+ incr
) < significand
) {
152 significand
= (significand
>> 1) | (significand
& 1);
155 vs
->exponent
= exponent
;
156 vs
->significand
= significand
;
157 vfp_single_dump("pack: overflow", vs
);
162 * If any of the low bits (which will be shifted out of the
163 * number) are non-zero, the result is inexact.
165 if (significand
& ((1 << (VFP_SINGLE_LOW_BITS
+ 1)) - 1))
166 exceptions
|= FPSCR_IXC
;
176 if (exponent
>= 254) {
177 exceptions
|= FPSCR_OFC
| FPSCR_IXC
;
180 vs
->significand
= 0x7fffffff;
182 vs
->exponent
= 255; /* infinity */
186 if (significand
>> (VFP_SINGLE_LOW_BITS
+ 1) == 0)
188 if (exponent
|| significand
> 0x80000000)
191 exceptions
|= FPSCR_UFC
;
192 vs
->exponent
= exponent
;
193 vs
->significand
= significand
>> 1;
197 vfp_single_dump("pack: final", vs
);
199 s32 d
= vfp_single_pack(vs
);
201 pr_debug("VFP: %s: d(s%d)=%08x exceptions=%08x\n", func
,
204 vfp_put_float(d
, sd
);
211 * Propagate the NaN, setting exceptions if it is signalling.
212 * 'n' is always a NaN. 'm' may be a number, NaN or infinity.
215 vfp_propagate_nan(struct vfp_single
*vsd
, struct vfp_single
*vsn
,
216 struct vfp_single
*vsm
, u32 fpscr
)
218 struct vfp_single
*nan
;
221 tn
= vfp_single_type(vsn
);
224 tm
= vfp_single_type(vsm
);
226 if (fpscr
& FPSCR_DEFAULT_NAN
)
228 * Default NaN mode - always returns a quiet NaN
230 nan
= &vfp_single_default_qnan
;
233 * Contemporary mode - select the first signalling
234 * NAN, or if neither are signalling, the first
237 if (tn
== VFP_SNAN
|| (tm
!= VFP_SNAN
&& tn
== VFP_QNAN
))
242 * Make the NaN quiet.
244 nan
->significand
|= VFP_SINGLE_SIGNIFICAND_QNAN
;
250 * If one was a signalling NAN, raise invalid operation.
252 return tn
== VFP_SNAN
|| tm
== VFP_SNAN
? FPSCR_IOC
: VFP_NAN_FLAG
;
257 * Extended operations
259 static u32
vfp_single_fabs(int sd
, int unused
, s32 m
, u32 fpscr
)
261 vfp_put_float(vfp_single_packed_abs(m
), sd
);
265 static u32
vfp_single_fcpy(int sd
, int unused
, s32 m
, u32 fpscr
)
267 vfp_put_float(m
, sd
);
271 static u32
vfp_single_fneg(int sd
, int unused
, s32 m
, u32 fpscr
)
273 vfp_put_float(vfp_single_packed_negate(m
), sd
);
277 static const u16 sqrt_oddadjust
[] = {
278 0x0004, 0x0022, 0x005d, 0x00b1, 0x011d, 0x019f, 0x0236, 0x02e0,
279 0x039c, 0x0468, 0x0545, 0x0631, 0x072b, 0x0832, 0x0946, 0x0a67
282 static const u16 sqrt_evenadjust
[] = {
283 0x0a2d, 0x08af, 0x075a, 0x0629, 0x051a, 0x0429, 0x0356, 0x029e,
284 0x0200, 0x0179, 0x0109, 0x00af, 0x0068, 0x0034, 0x0012, 0x0002
287 u32
vfp_estimate_sqrt_significand(u32 exponent
, u32 significand
)
292 if ((significand
& 0xc0000000) != 0x40000000) {
293 pr_warn("VFP: estimate_sqrt: invalid significand\n");
296 a
= significand
<< 1;
297 index
= (a
>> 27) & 15;
299 z
= 0x4000 + (a
>> 17) - sqrt_oddadjust
[index
];
300 z
= ((a
/ z
) << 14) + (z
<< 15);
303 z
= 0x8000 + (a
>> 17) - sqrt_evenadjust
[index
];
305 z
= (z
>= 0x20000) ? 0xffff8000 : (z
<< 15);
310 u64 v
= (u64
)a
<< 31;
316 static u32
vfp_single_fsqrt(int sd
, int unused
, s32 m
, u32 fpscr
)
318 struct vfp_single vsm
, vsd
;
321 vfp_single_unpack(&vsm
, m
);
322 tm
= vfp_single_type(&vsm
);
323 if (tm
& (VFP_NAN
|VFP_INFINITY
)) {
324 struct vfp_single
*vsp
= &vsd
;
327 ret
= vfp_propagate_nan(vsp
, &vsm
, NULL
, fpscr
);
328 else if (vsm
.sign
== 0) {
334 vsp
= &vfp_single_default_qnan
;
337 vfp_put_float(vfp_single_pack(vsp
), sd
);
342 * sqrt(+/- 0) == +/- 0
348 * Normalise a denormalised number
350 if (tm
& VFP_DENORMAL
)
351 vfp_single_normalise_denormal(&vsm
);
359 vfp_single_dump("sqrt", &vsm
);
362 * Estimate the square root.
365 vsd
.exponent
= ((vsm
.exponent
- 127) >> 1) + 127;
366 vsd
.significand
= vfp_estimate_sqrt_significand(vsm
.exponent
, vsm
.significand
) + 2;
368 vfp_single_dump("sqrt estimate", &vsd
);
373 if ((vsd
.significand
& VFP_SINGLE_LOW_BITS_MASK
) <= 5) {
374 if (vsd
.significand
< 2) {
375 vsd
.significand
= 0xffffffff;
379 vsm
.significand
<<= !(vsm
.exponent
& 1);
380 term
= (u64
)vsd
.significand
* vsd
.significand
;
381 rem
= ((u64
)vsm
.significand
<< 32) - term
;
383 pr_debug("VFP: term=%016llx rem=%016llx\n", term
, rem
);
386 vsd
.significand
-= 1;
387 rem
+= ((u64
)vsd
.significand
<< 1) | 1;
389 vsd
.significand
|= rem
!= 0;
392 vsd
.significand
= vfp_shiftright32jamming(vsd
.significand
, 1);
394 return vfp_single_normaliseround(sd
, &vsd
, fpscr
, 0, "fsqrt");
403 static u32
vfp_compare(int sd
, int signal_on_qnan
, s32 m
, u32 fpscr
)
408 d
= vfp_get_float(sd
);
409 if (vfp_single_packed_exponent(m
) == 255 && vfp_single_packed_mantissa(m
)) {
410 ret
|= FPSCR_C
| FPSCR_V
;
411 if (signal_on_qnan
|| !(vfp_single_packed_mantissa(m
) & (1 << (VFP_SINGLE_MANTISSA_BITS
- 1))))
413 * Signalling NaN, or signalling on quiet NaN
418 if (vfp_single_packed_exponent(d
) == 255 && vfp_single_packed_mantissa(d
)) {
419 ret
|= FPSCR_C
| FPSCR_V
;
420 if (signal_on_qnan
|| !(vfp_single_packed_mantissa(d
) & (1 << (VFP_SINGLE_MANTISSA_BITS
- 1))))
422 * Signalling NaN, or signalling on quiet NaN
428 if (d
== m
|| vfp_single_packed_abs(d
| m
) == 0) {
432 ret
|= FPSCR_Z
| FPSCR_C
;
433 } else if (vfp_single_packed_sign(d
^ m
)) {
437 if (vfp_single_packed_sign(d
))
439 * d is negative, so d < m
444 * d is positive, so d > m
447 } else if ((vfp_single_packed_sign(d
) != 0) ^ (d
< m
)) {
452 } else if ((vfp_single_packed_sign(d
) != 0) ^ (d
> m
)) {
462 static u32
vfp_single_fcmp(int sd
, int unused
, s32 m
, u32 fpscr
)
464 return vfp_compare(sd
, 0, m
, fpscr
);
467 static u32
vfp_single_fcmpe(int sd
, int unused
, s32 m
, u32 fpscr
)
469 return vfp_compare(sd
, 1, m
, fpscr
);
472 static u32
vfp_single_fcmpz(int sd
, int unused
, s32 m
, u32 fpscr
)
474 return vfp_compare(sd
, 0, 0, fpscr
);
477 static u32
vfp_single_fcmpez(int sd
, int unused
, s32 m
, u32 fpscr
)
479 return vfp_compare(sd
, 1, 0, fpscr
);
482 static u32
vfp_single_fcvtd(int dd
, int unused
, s32 m
, u32 fpscr
)
484 struct vfp_single vsm
;
485 struct vfp_double vdd
;
489 vfp_single_unpack(&vsm
, m
);
491 tm
= vfp_single_type(&vsm
);
494 * If we have a signalling NaN, signal invalid operation.
497 exceptions
= FPSCR_IOC
;
499 if (tm
& VFP_DENORMAL
)
500 vfp_single_normalise_denormal(&vsm
);
503 vdd
.significand
= (u64
)vsm
.significand
<< 32;
506 * If we have an infinity or NaN, the exponent must be 2047.
508 if (tm
& (VFP_INFINITY
|VFP_NAN
)) {
511 vdd
.significand
|= VFP_DOUBLE_SIGNIFICAND_QNAN
;
513 } else if (tm
& VFP_ZERO
)
516 vdd
.exponent
= vsm
.exponent
+ (1023 - 127);
518 return vfp_double_normaliseround(dd
, &vdd
, fpscr
, exceptions
, "fcvtd");
521 vfp_put_double(vfp_double_pack(&vdd
), dd
);
525 static u32
vfp_single_fuito(int sd
, int unused
, s32 m
, u32 fpscr
)
527 struct vfp_single vs
;
530 vs
.exponent
= 127 + 31 - 1;
531 vs
.significand
= (u32
)m
;
533 return vfp_single_normaliseround(sd
, &vs
, fpscr
, 0, "fuito");
536 static u32
vfp_single_fsito(int sd
, int unused
, s32 m
, u32 fpscr
)
538 struct vfp_single vs
;
540 vs
.sign
= (m
& 0x80000000) >> 16;
541 vs
.exponent
= 127 + 31 - 1;
542 vs
.significand
= vs
.sign
? -m
: m
;
544 return vfp_single_normaliseround(sd
, &vs
, fpscr
, 0, "fsito");
547 static u32
vfp_single_ftoui(int sd
, int unused
, s32 m
, u32 fpscr
)
549 struct vfp_single vsm
;
550 u32 d
, exceptions
= 0;
551 int rmode
= fpscr
& FPSCR_RMODE_MASK
;
554 vfp_single_unpack(&vsm
, m
);
555 vfp_single_dump("VSM", &vsm
);
558 * Do we have a denormalised number?
560 tm
= vfp_single_type(&vsm
);
561 if (tm
& VFP_DENORMAL
)
562 exceptions
|= FPSCR_IDC
;
567 if (vsm
.exponent
>= 127 + 32) {
568 d
= vsm
.sign
? 0 : 0xffffffff;
569 exceptions
= FPSCR_IOC
;
570 } else if (vsm
.exponent
>= 127 - 1) {
571 int shift
= 127 + 31 - vsm
.exponent
;
575 * 2^0 <= m < 2^32-2^8
577 d
= (vsm
.significand
<< 1) >> shift
;
578 rem
= vsm
.significand
<< (33 - shift
);
580 if (rmode
== FPSCR_ROUND_NEAREST
) {
584 } else if (rmode
== FPSCR_ROUND_TOZERO
) {
586 } else if ((rmode
== FPSCR_ROUND_PLUSINF
) ^ (vsm
.sign
!= 0)) {
590 if ((rem
+ incr
) < rem
) {
594 exceptions
|= FPSCR_IOC
;
599 exceptions
|= FPSCR_IOC
;
601 exceptions
|= FPSCR_IXC
;
604 if (vsm
.exponent
| vsm
.significand
) {
605 exceptions
|= FPSCR_IXC
;
606 if (rmode
== FPSCR_ROUND_PLUSINF
&& vsm
.sign
== 0)
608 else if (rmode
== FPSCR_ROUND_MINUSINF
&& vsm
.sign
) {
610 exceptions
|= FPSCR_IOC
;
615 pr_debug("VFP: ftoui: d(s%d)=%08x exceptions=%08x\n", sd
, d
, exceptions
);
617 vfp_put_float(d
, sd
);
622 static u32
vfp_single_ftouiz(int sd
, int unused
, s32 m
, u32 fpscr
)
624 return vfp_single_ftoui(sd
, unused
, m
, FPSCR_ROUND_TOZERO
);
627 static u32
vfp_single_ftosi(int sd
, int unused
, s32 m
, u32 fpscr
)
629 struct vfp_single vsm
;
630 u32 d
, exceptions
= 0;
631 int rmode
= fpscr
& FPSCR_RMODE_MASK
;
634 vfp_single_unpack(&vsm
, m
);
635 vfp_single_dump("VSM", &vsm
);
638 * Do we have a denormalised number?
640 tm
= vfp_single_type(&vsm
);
641 if (vfp_single_type(&vsm
) & VFP_DENORMAL
)
642 exceptions
|= FPSCR_IDC
;
646 exceptions
|= FPSCR_IOC
;
647 } else if (vsm
.exponent
>= 127 + 32) {
649 * m >= 2^31-2^7: invalid
654 exceptions
|= FPSCR_IOC
;
655 } else if (vsm
.exponent
>= 127 - 1) {
656 int shift
= 127 + 31 - vsm
.exponent
;
659 /* 2^0 <= m <= 2^31-2^7 */
660 d
= (vsm
.significand
<< 1) >> shift
;
661 rem
= vsm
.significand
<< (33 - shift
);
663 if (rmode
== FPSCR_ROUND_NEAREST
) {
667 } else if (rmode
== FPSCR_ROUND_TOZERO
) {
669 } else if ((rmode
== FPSCR_ROUND_PLUSINF
) ^ (vsm
.sign
!= 0)) {
673 if ((rem
+ incr
) < rem
&& d
< 0xffffffff)
675 if (d
> 0x7fffffff + (vsm
.sign
!= 0)) {
676 d
= 0x7fffffff + (vsm
.sign
!= 0);
677 exceptions
|= FPSCR_IOC
;
679 exceptions
|= FPSCR_IXC
;
685 if (vsm
.exponent
| vsm
.significand
) {
686 exceptions
|= FPSCR_IXC
;
687 if (rmode
== FPSCR_ROUND_PLUSINF
&& vsm
.sign
== 0)
689 else if (rmode
== FPSCR_ROUND_MINUSINF
&& vsm
.sign
)
694 pr_debug("VFP: ftosi: d(s%d)=%08x exceptions=%08x\n", sd
, d
, exceptions
);
696 vfp_put_float((s32
)d
, sd
);
701 static u32
vfp_single_ftosiz(int sd
, int unused
, s32 m
, u32 fpscr
)
703 return vfp_single_ftosi(sd
, unused
, m
, FPSCR_ROUND_TOZERO
);
706 static struct op fops_ext
[32] = {
707 [FEXT_TO_IDX(FEXT_FCPY
)] = { vfp_single_fcpy
, 0 },
708 [FEXT_TO_IDX(FEXT_FABS
)] = { vfp_single_fabs
, 0 },
709 [FEXT_TO_IDX(FEXT_FNEG
)] = { vfp_single_fneg
, 0 },
710 [FEXT_TO_IDX(FEXT_FSQRT
)] = { vfp_single_fsqrt
, 0 },
711 [FEXT_TO_IDX(FEXT_FCMP
)] = { vfp_single_fcmp
, OP_SCALAR
},
712 [FEXT_TO_IDX(FEXT_FCMPE
)] = { vfp_single_fcmpe
, OP_SCALAR
},
713 [FEXT_TO_IDX(FEXT_FCMPZ
)] = { vfp_single_fcmpz
, OP_SCALAR
},
714 [FEXT_TO_IDX(FEXT_FCMPEZ
)] = { vfp_single_fcmpez
, OP_SCALAR
},
715 [FEXT_TO_IDX(FEXT_FCVT
)] = { vfp_single_fcvtd
, OP_SCALAR
|OP_DD
},
716 [FEXT_TO_IDX(FEXT_FUITO
)] = { vfp_single_fuito
, OP_SCALAR
},
717 [FEXT_TO_IDX(FEXT_FSITO
)] = { vfp_single_fsito
, OP_SCALAR
},
718 [FEXT_TO_IDX(FEXT_FTOUI
)] = { vfp_single_ftoui
, OP_SCALAR
},
719 [FEXT_TO_IDX(FEXT_FTOUIZ
)] = { vfp_single_ftouiz
, OP_SCALAR
},
720 [FEXT_TO_IDX(FEXT_FTOSI
)] = { vfp_single_ftosi
, OP_SCALAR
},
721 [FEXT_TO_IDX(FEXT_FTOSIZ
)] = { vfp_single_ftosiz
, OP_SCALAR
},
729 vfp_single_fadd_nonnumber(struct vfp_single
*vsd
, struct vfp_single
*vsn
,
730 struct vfp_single
*vsm
, u32 fpscr
)
732 struct vfp_single
*vsp
;
736 tn
= vfp_single_type(vsn
);
737 tm
= vfp_single_type(vsm
);
739 if (tn
& tm
& VFP_INFINITY
) {
741 * Two infinities. Are they different signs?
743 if (vsn
->sign
^ vsm
->sign
) {
745 * different signs -> invalid
747 exceptions
= FPSCR_IOC
;
748 vsp
= &vfp_single_default_qnan
;
751 * same signs -> valid
755 } else if (tn
& VFP_INFINITY
&& tm
& VFP_NUMBER
) {
757 * One infinity and one number -> infinity
762 * 'n' is a NaN of some type
764 return vfp_propagate_nan(vsd
, vsn
, vsm
, fpscr
);
771 vfp_single_add(struct vfp_single
*vsd
, struct vfp_single
*vsn
,
772 struct vfp_single
*vsm
, u32 fpscr
)
776 if (vsn
->significand
& 0x80000000 ||
777 vsm
->significand
& 0x80000000) {
778 pr_info("VFP: bad FP values in %s\n", __func__
);
779 vfp_single_dump("VSN", vsn
);
780 vfp_single_dump("VSM", vsm
);
784 * Ensure that 'n' is the largest magnitude number. Note that
785 * if 'n' and 'm' have equal exponents, we do not swap them.
786 * This ensures that NaN propagation works correctly.
788 if (vsn
->exponent
< vsm
->exponent
) {
789 struct vfp_single
*t
= vsn
;
795 * Is 'n' an infinity or a NaN? Note that 'm' may be a number,
796 * infinity or a NaN here.
798 if (vsn
->exponent
== 255)
799 return vfp_single_fadd_nonnumber(vsd
, vsn
, vsm
, fpscr
);
802 * We have two proper numbers, where 'vsn' is the larger magnitude.
804 * Copy 'n' to 'd' before doing the arithmetic.
809 * Align both numbers.
811 exp_diff
= vsn
->exponent
- vsm
->exponent
;
812 m_sig
= vfp_shiftright32jamming(vsm
->significand
, exp_diff
);
815 * If the signs are different, we are really subtracting.
817 if (vsn
->sign
^ vsm
->sign
) {
818 m_sig
= vsn
->significand
- m_sig
;
819 if ((s32
)m_sig
< 0) {
820 vsd
->sign
= vfp_sign_negate(vsd
->sign
);
822 } else if (m_sig
== 0) {
823 vsd
->sign
= (fpscr
& FPSCR_RMODE_MASK
) ==
824 FPSCR_ROUND_MINUSINF
? 0x8000 : 0;
827 m_sig
= vsn
->significand
+ m_sig
;
829 vsd
->significand
= m_sig
;
835 vfp_single_multiply(struct vfp_single
*vsd
, struct vfp_single
*vsn
, struct vfp_single
*vsm
, u32 fpscr
)
837 vfp_single_dump("VSN", vsn
);
838 vfp_single_dump("VSM", vsm
);
841 * Ensure that 'n' is the largest magnitude number. Note that
842 * if 'n' and 'm' have equal exponents, we do not swap them.
843 * This ensures that NaN propagation works correctly.
845 if (vsn
->exponent
< vsm
->exponent
) {
846 struct vfp_single
*t
= vsn
;
849 pr_debug("VFP: swapping M <-> N\n");
852 vsd
->sign
= vsn
->sign
^ vsm
->sign
;
855 * If 'n' is an infinity or NaN, handle it. 'm' may be anything.
857 if (vsn
->exponent
== 255) {
858 if (vsn
->significand
|| (vsm
->exponent
== 255 && vsm
->significand
))
859 return vfp_propagate_nan(vsd
, vsn
, vsm
, fpscr
);
860 if ((vsm
->exponent
| vsm
->significand
) == 0) {
861 *vsd
= vfp_single_default_qnan
;
864 vsd
->exponent
= vsn
->exponent
;
865 vsd
->significand
= 0;
870 * If 'm' is zero, the result is always zero. In this case,
871 * 'n' may be zero or a number, but it doesn't matter which.
873 if ((vsm
->exponent
| vsm
->significand
) == 0) {
875 vsd
->significand
= 0;
880 * We add 2 to the destination exponent for the same reason as
881 * the addition case - though this time we have +1 from each
884 vsd
->exponent
= vsn
->exponent
+ vsm
->exponent
- 127 + 2;
885 vsd
->significand
= vfp_hi64to32jamming((u64
)vsn
->significand
* vsm
->significand
);
887 vfp_single_dump("VSD", vsd
);
891 #define NEG_MULTIPLY (1 << 0)
892 #define NEG_SUBTRACT (1 << 1)
895 vfp_single_multiply_accumulate(int sd
, int sn
, s32 m
, u32 fpscr
, u32 negate
, char *func
)
897 struct vfp_single vsd
, vsp
, vsn
, vsm
;
901 v
= vfp_get_float(sn
);
902 pr_debug("VFP: s%u = %08x\n", sn
, v
);
903 vfp_single_unpack(&vsn
, v
);
904 if (vsn
.exponent
== 0 && vsn
.significand
)
905 vfp_single_normalise_denormal(&vsn
);
907 vfp_single_unpack(&vsm
, m
);
908 if (vsm
.exponent
== 0 && vsm
.significand
)
909 vfp_single_normalise_denormal(&vsm
);
911 exceptions
= vfp_single_multiply(&vsp
, &vsn
, &vsm
, fpscr
);
912 if (negate
& NEG_MULTIPLY
)
913 vsp
.sign
= vfp_sign_negate(vsp
.sign
);
915 v
= vfp_get_float(sd
);
916 pr_debug("VFP: s%u = %08x\n", sd
, v
);
917 vfp_single_unpack(&vsn
, v
);
918 if (vsn
.exponent
== 0 && vsn
.significand
)
919 vfp_single_normalise_denormal(&vsn
);
920 if (negate
& NEG_SUBTRACT
)
921 vsn
.sign
= vfp_sign_negate(vsn
.sign
);
923 exceptions
|= vfp_single_add(&vsd
, &vsn
, &vsp
, fpscr
);
925 return vfp_single_normaliseround(sd
, &vsd
, fpscr
, exceptions
, func
);
929 * Standard operations
933 * sd = sd + (sn * sm)
935 static u32
vfp_single_fmac(int sd
, int sn
, s32 m
, u32 fpscr
)
937 return vfp_single_multiply_accumulate(sd
, sn
, m
, fpscr
, 0, "fmac");
941 * sd = sd - (sn * sm)
943 static u32
vfp_single_fnmac(int sd
, int sn
, s32 m
, u32 fpscr
)
945 return vfp_single_multiply_accumulate(sd
, sn
, m
, fpscr
, NEG_MULTIPLY
, "fnmac");
949 * sd = -sd + (sn * sm)
951 static u32
vfp_single_fmsc(int sd
, int sn
, s32 m
, u32 fpscr
)
953 return vfp_single_multiply_accumulate(sd
, sn
, m
, fpscr
, NEG_SUBTRACT
, "fmsc");
957 * sd = -sd - (sn * sm)
959 static u32
vfp_single_fnmsc(int sd
, int sn
, s32 m
, u32 fpscr
)
961 return vfp_single_multiply_accumulate(sd
, sn
, m
, fpscr
, NEG_SUBTRACT
| NEG_MULTIPLY
, "fnmsc");
967 static u32
vfp_single_fmul(int sd
, int sn
, s32 m
, u32 fpscr
)
969 struct vfp_single vsd
, vsn
, vsm
;
971 s32 n
= vfp_get_float(sn
);
973 pr_debug("VFP: s%u = %08x\n", sn
, n
);
975 vfp_single_unpack(&vsn
, n
);
976 if (vsn
.exponent
== 0 && vsn
.significand
)
977 vfp_single_normalise_denormal(&vsn
);
979 vfp_single_unpack(&vsm
, m
);
980 if (vsm
.exponent
== 0 && vsm
.significand
)
981 vfp_single_normalise_denormal(&vsm
);
983 exceptions
= vfp_single_multiply(&vsd
, &vsn
, &vsm
, fpscr
);
984 return vfp_single_normaliseround(sd
, &vsd
, fpscr
, exceptions
, "fmul");
990 static u32
vfp_single_fnmul(int sd
, int sn
, s32 m
, u32 fpscr
)
992 struct vfp_single vsd
, vsn
, vsm
;
994 s32 n
= vfp_get_float(sn
);
996 pr_debug("VFP: s%u = %08x\n", sn
, n
);
998 vfp_single_unpack(&vsn
, n
);
999 if (vsn
.exponent
== 0 && vsn
.significand
)
1000 vfp_single_normalise_denormal(&vsn
);
1002 vfp_single_unpack(&vsm
, m
);
1003 if (vsm
.exponent
== 0 && vsm
.significand
)
1004 vfp_single_normalise_denormal(&vsm
);
1006 exceptions
= vfp_single_multiply(&vsd
, &vsn
, &vsm
, fpscr
);
1007 vsd
.sign
= vfp_sign_negate(vsd
.sign
);
1008 return vfp_single_normaliseround(sd
, &vsd
, fpscr
, exceptions
, "fnmul");
1014 static u32
vfp_single_fadd(int sd
, int sn
, s32 m
, u32 fpscr
)
1016 struct vfp_single vsd
, vsn
, vsm
;
1018 s32 n
= vfp_get_float(sn
);
1020 pr_debug("VFP: s%u = %08x\n", sn
, n
);
1023 * Unpack and normalise denormals.
1025 vfp_single_unpack(&vsn
, n
);
1026 if (vsn
.exponent
== 0 && vsn
.significand
)
1027 vfp_single_normalise_denormal(&vsn
);
1029 vfp_single_unpack(&vsm
, m
);
1030 if (vsm
.exponent
== 0 && vsm
.significand
)
1031 vfp_single_normalise_denormal(&vsm
);
1033 exceptions
= vfp_single_add(&vsd
, &vsn
, &vsm
, fpscr
);
1035 return vfp_single_normaliseround(sd
, &vsd
, fpscr
, exceptions
, "fadd");
1041 static u32
vfp_single_fsub(int sd
, int sn
, s32 m
, u32 fpscr
)
1044 * Subtraction is addition with one sign inverted.
1046 return vfp_single_fadd(sd
, sn
, vfp_single_packed_negate(m
), fpscr
);
1052 static u32
vfp_single_fdiv(int sd
, int sn
, s32 m
, u32 fpscr
)
1054 struct vfp_single vsd
, vsn
, vsm
;
1056 s32 n
= vfp_get_float(sn
);
1059 pr_debug("VFP: s%u = %08x\n", sn
, n
);
1061 vfp_single_unpack(&vsn
, n
);
1062 vfp_single_unpack(&vsm
, m
);
1064 vsd
.sign
= vsn
.sign
^ vsm
.sign
;
1066 tn
= vfp_single_type(&vsn
);
1067 tm
= vfp_single_type(&vsm
);
1082 * If n and m are infinity, the result is invalid
1083 * If n and m are zero, the result is invalid
1085 if (tm
& tn
& (VFP_INFINITY
|VFP_ZERO
))
1089 * If n is infinity, the result is infinity
1091 if (tn
& VFP_INFINITY
)
1095 * If m is zero, raise div0 exception
1101 * If m is infinity, or n is zero, the result is zero
1103 if (tm
& VFP_INFINITY
|| tn
& VFP_ZERO
)
1106 if (tn
& VFP_DENORMAL
)
1107 vfp_single_normalise_denormal(&vsn
);
1108 if (tm
& VFP_DENORMAL
)
1109 vfp_single_normalise_denormal(&vsm
);
1112 * Ok, we have two numbers, we can perform division.
1114 vsd
.exponent
= vsn
.exponent
- vsm
.exponent
+ 127 - 1;
1115 vsm
.significand
<<= 1;
1116 if (vsm
.significand
<= (2 * vsn
.significand
)) {
1117 vsn
.significand
>>= 1;
1121 u64 significand
= (u64
)vsn
.significand
<< 32;
1122 do_div(significand
, vsm
.significand
);
1123 vsd
.significand
= significand
;
1125 if ((vsd
.significand
& 0x3f) == 0)
1126 vsd
.significand
|= ((u64
)vsm
.significand
* vsd
.significand
!= (u64
)vsn
.significand
<< 32);
1128 return vfp_single_normaliseround(sd
, &vsd
, fpscr
, 0, "fdiv");
1131 exceptions
= vfp_propagate_nan(&vsd
, &vsn
, &vsm
, fpscr
);
1133 vfp_put_float(vfp_single_pack(&vsd
), sd
);
1137 exceptions
= vfp_propagate_nan(&vsd
, &vsm
, &vsn
, fpscr
);
1142 vsd
.significand
= 0;
1146 exceptions
= FPSCR_DZC
;
1149 vsd
.significand
= 0;
1153 vfp_put_float(vfp_single_pack(&vfp_single_default_qnan
), sd
);
1157 static struct op fops
[16] = {
1158 [FOP_TO_IDX(FOP_FMAC
)] = { vfp_single_fmac
, 0 },
1159 [FOP_TO_IDX(FOP_FNMAC
)] = { vfp_single_fnmac
, 0 },
1160 [FOP_TO_IDX(FOP_FMSC
)] = { vfp_single_fmsc
, 0 },
1161 [FOP_TO_IDX(FOP_FNMSC
)] = { vfp_single_fnmsc
, 0 },
1162 [FOP_TO_IDX(FOP_FMUL
)] = { vfp_single_fmul
, 0 },
1163 [FOP_TO_IDX(FOP_FNMUL
)] = { vfp_single_fnmul
, 0 },
1164 [FOP_TO_IDX(FOP_FADD
)] = { vfp_single_fadd
, 0 },
1165 [FOP_TO_IDX(FOP_FSUB
)] = { vfp_single_fsub
, 0 },
1166 [FOP_TO_IDX(FOP_FDIV
)] = { vfp_single_fdiv
, 0 },
1169 #define FREG_BANK(x) ((x) & 0x18)
1170 #define FREG_IDX(x) ((x) & 7)
1172 u32
vfp_single_cpdo(u32 inst
, u32 fpscr
)
1174 u32 op
= inst
& FOP_MASK
;
1177 unsigned int sn
= vfp_get_sn(inst
);
1178 unsigned int sm
= vfp_get_sm(inst
);
1179 unsigned int vecitr
, veclen
, vecstride
;
1182 vecstride
= 1 + ((fpscr
& FPSCR_STRIDE_MASK
) == FPSCR_STRIDE_MASK
);
1184 fop
= (op
== FOP_EXT
) ? &fops_ext
[FEXT_TO_IDX(inst
)] : &fops
[FOP_TO_IDX(op
)];
1187 * fcvtsd takes a dN register number as destination, not sN.
1188 * Technically, if bit 0 of dd is set, this is an invalid
1189 * instruction. However, we ignore this for efficiency.
1190 * It also only operates on scalars.
1192 if (fop
->flags
& OP_DD
)
1193 dest
= vfp_get_dd(inst
);
1195 dest
= vfp_get_sd(inst
);
1198 * If destination bank is zero, vector length is always '1'.
1199 * ARM DDI0100F C5.1.3, C5.3.2.
1201 if ((fop
->flags
& OP_SCALAR
) || FREG_BANK(dest
) == 0)
1204 veclen
= fpscr
& FPSCR_LENGTH_MASK
;
1206 pr_debug("VFP: vecstride=%u veclen=%u\n", vecstride
,
1207 (veclen
>> FPSCR_LENGTH_BIT
) + 1);
1212 for (vecitr
= 0; vecitr
<= veclen
; vecitr
+= 1 << FPSCR_LENGTH_BIT
) {
1213 s32 m
= vfp_get_float(sm
);
1217 type
= fop
->flags
& OP_DD
? 'd' : 's';
1219 pr_debug("VFP: itr%d (%c%u) = op[%u] (s%u=%08x)\n",
1220 vecitr
>> FPSCR_LENGTH_BIT
, type
, dest
, sn
,
1223 pr_debug("VFP: itr%d (%c%u) = (s%u) op[%u] (s%u=%08x)\n",
1224 vecitr
>> FPSCR_LENGTH_BIT
, type
, dest
, sn
,
1225 FOP_TO_IDX(op
), sm
, m
);
1227 except
= fop
->fn(dest
, sn
, m
, fpscr
);
1228 pr_debug("VFP: itr%d: exceptions=%08x\n",
1229 vecitr
>> FPSCR_LENGTH_BIT
, except
);
1231 exceptions
|= except
;
1234 * CHECK: It appears to be undefined whether we stop when
1235 * we encounter an exception. We continue.
1237 dest
= FREG_BANK(dest
) + ((FREG_IDX(dest
) + vecstride
) & 7);
1238 sn
= FREG_BANK(sn
) + ((FREG_IDX(sn
) + vecstride
) & 7);
1239 if (FREG_BANK(sm
) != 0)
1240 sm
= FREG_BANK(sm
) + ((FREG_IDX(sm
) + vecstride
) & 7);