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>
37 #include <asm/ptrace.h>
43 static struct vfp_single vfp_single_default_qnan
= {
46 .significand
= VFP_SINGLE_SIGNIFICAND_QNAN
,
49 static void vfp_single_dump(const char *str
, struct vfp_single
*s
)
51 pr_debug("VFP: %s: sign=%d exponent=%d significand=%08x\n",
52 str
, s
->sign
!= 0, s
->exponent
, s
->significand
);
55 static void vfp_single_normalise_denormal(struct vfp_single
*vs
)
57 int bits
= 31 - fls(vs
->significand
);
59 vfp_single_dump("normalise_denormal: in", vs
);
62 vs
->exponent
-= bits
- 1;
63 vs
->significand
<<= bits
;
66 vfp_single_dump("normalise_denormal: out", vs
);
70 #define vfp_single_normaliseround(sd,vsd,fpscr,except,func) __vfp_single_normaliseround(sd,vsd,fpscr,except)
71 u32
__vfp_single_normaliseround(int sd
, struct vfp_single
*vs
, u32 fpscr
, u32 exceptions
)
73 u32
vfp_single_normaliseround(int sd
, struct vfp_single
*vs
, u32 fpscr
, u32 exceptions
, const char *func
)
76 u32 significand
, incr
, rmode
;
77 int exponent
, shift
, underflow
;
79 vfp_single_dump("pack: in", vs
);
82 * Infinities and NaNs are a special case.
84 if (vs
->exponent
== 255 && (vs
->significand
== 0 || exceptions
))
90 if (vs
->significand
== 0) {
95 exponent
= vs
->exponent
;
96 significand
= vs
->significand
;
99 * Normalise first. Note that we shift the significand up to
100 * bit 31, so we have VFP_SINGLE_LOW_BITS + 1 below the least
103 shift
= 32 - fls(significand
);
104 if (shift
< 32 && shift
) {
106 significand
<<= shift
;
110 vs
->exponent
= exponent
;
111 vs
->significand
= significand
;
112 vfp_single_dump("pack: normalised", vs
);
118 underflow
= exponent
< 0;
120 significand
= vfp_shiftright32jamming(significand
, -exponent
);
123 vs
->exponent
= exponent
;
124 vs
->significand
= significand
;
125 vfp_single_dump("pack: tiny number", vs
);
127 if (!(significand
& ((1 << (VFP_SINGLE_LOW_BITS
+ 1)) - 1)))
132 * Select rounding increment.
135 rmode
= fpscr
& FPSCR_RMODE_MASK
;
137 if (rmode
== FPSCR_ROUND_NEAREST
) {
138 incr
= 1 << VFP_SINGLE_LOW_BITS
;
139 if ((significand
& (1 << (VFP_SINGLE_LOW_BITS
+ 1))) == 0)
141 } else if (rmode
== FPSCR_ROUND_TOZERO
) {
143 } else if ((rmode
== FPSCR_ROUND_PLUSINF
) ^ (vs
->sign
!= 0))
144 incr
= (1 << (VFP_SINGLE_LOW_BITS
+ 1)) - 1;
146 pr_debug("VFP: rounding increment = 0x%08x\n", incr
);
149 * Is our rounding going to overflow?
151 if ((significand
+ incr
) < significand
) {
153 significand
= (significand
>> 1) | (significand
& 1);
156 vs
->exponent
= exponent
;
157 vs
->significand
= significand
;
158 vfp_single_dump("pack: overflow", vs
);
163 * If any of the low bits (which will be shifted out of the
164 * number) are non-zero, the result is inexact.
166 if (significand
& ((1 << (VFP_SINGLE_LOW_BITS
+ 1)) - 1))
167 exceptions
|= FPSCR_IXC
;
177 if (exponent
>= 254) {
178 exceptions
|= FPSCR_OFC
| FPSCR_IXC
;
181 vs
->significand
= 0x7fffffff;
183 vs
->exponent
= 255; /* infinity */
187 if (significand
>> (VFP_SINGLE_LOW_BITS
+ 1) == 0)
189 if (exponent
|| significand
> 0x80000000)
192 exceptions
|= FPSCR_UFC
;
193 vs
->exponent
= exponent
;
194 vs
->significand
= significand
>> 1;
198 vfp_single_dump("pack: final", vs
);
200 s32 d
= vfp_single_pack(vs
);
202 pr_debug("VFP: %s: d(s%d)=%08x exceptions=%08x\n", func
,
205 vfp_put_float(d
, sd
);
212 * Propagate the NaN, setting exceptions if it is signalling.
213 * 'n' is always a NaN. 'm' may be a number, NaN or infinity.
216 vfp_propagate_nan(struct vfp_single
*vsd
, struct vfp_single
*vsn
,
217 struct vfp_single
*vsm
, u32 fpscr
)
219 struct vfp_single
*nan
;
222 tn
= vfp_single_type(vsn
);
225 tm
= vfp_single_type(vsm
);
227 if (fpscr
& FPSCR_DEFAULT_NAN
)
229 * Default NaN mode - always returns a quiet NaN
231 nan
= &vfp_single_default_qnan
;
234 * Contemporary mode - select the first signalling
235 * NAN, or if neither are signalling, the first
238 if (tn
== VFP_SNAN
|| (tm
!= VFP_SNAN
&& tn
== VFP_QNAN
))
243 * Make the NaN quiet.
245 nan
->significand
|= VFP_SINGLE_SIGNIFICAND_QNAN
;
251 * If one was a signalling NAN, raise invalid operation.
253 return tn
== VFP_SNAN
|| tm
== VFP_SNAN
? FPSCR_IOC
: VFP_NAN_FLAG
;
258 * Extended operations
260 static u32
vfp_single_fabs(int sd
, int unused
, s32 m
, u32 fpscr
)
262 vfp_put_float(vfp_single_packed_abs(m
), sd
);
266 static u32
vfp_single_fcpy(int sd
, int unused
, s32 m
, u32 fpscr
)
268 vfp_put_float(m
, sd
);
272 static u32
vfp_single_fneg(int sd
, int unused
, s32 m
, u32 fpscr
)
274 vfp_put_float(vfp_single_packed_negate(m
), sd
);
278 static const u16 sqrt_oddadjust
[] = {
279 0x0004, 0x0022, 0x005d, 0x00b1, 0x011d, 0x019f, 0x0236, 0x02e0,
280 0x039c, 0x0468, 0x0545, 0x0631, 0x072b, 0x0832, 0x0946, 0x0a67
283 static const u16 sqrt_evenadjust
[] = {
284 0x0a2d, 0x08af, 0x075a, 0x0629, 0x051a, 0x0429, 0x0356, 0x029e,
285 0x0200, 0x0179, 0x0109, 0x00af, 0x0068, 0x0034, 0x0012, 0x0002
288 u32
vfp_estimate_sqrt_significand(u32 exponent
, u32 significand
)
293 if ((significand
& 0xc0000000) != 0x40000000) {
294 printk(KERN_WARNING
"VFP: estimate_sqrt: invalid significand\n");
297 a
= significand
<< 1;
298 index
= (a
>> 27) & 15;
300 z
= 0x4000 + (a
>> 17) - sqrt_oddadjust
[index
];
301 z
= ((a
/ z
) << 14) + (z
<< 15);
304 z
= 0x8000 + (a
>> 17) - sqrt_evenadjust
[index
];
306 z
= (z
>= 0x20000) ? 0xffff8000 : (z
<< 15);
311 u64 v
= (u64
)a
<< 31;
317 static u32
vfp_single_fsqrt(int sd
, int unused
, s32 m
, u32 fpscr
)
319 struct vfp_single vsm
, vsd
;
322 vfp_single_unpack(&vsm
, m
);
323 tm
= vfp_single_type(&vsm
);
324 if (tm
& (VFP_NAN
|VFP_INFINITY
)) {
325 struct vfp_single
*vsp
= &vsd
;
328 ret
= vfp_propagate_nan(vsp
, &vsm
, NULL
, fpscr
);
329 else if (vsm
.sign
== 0) {
335 vsp
= &vfp_single_default_qnan
;
338 vfp_put_float(vfp_single_pack(vsp
), sd
);
343 * sqrt(+/- 0) == +/- 0
349 * Normalise a denormalised number
351 if (tm
& VFP_DENORMAL
)
352 vfp_single_normalise_denormal(&vsm
);
360 vfp_single_dump("sqrt", &vsm
);
363 * Estimate the square root.
366 vsd
.exponent
= ((vsm
.exponent
- 127) >> 1) + 127;
367 vsd
.significand
= vfp_estimate_sqrt_significand(vsm
.exponent
, vsm
.significand
) + 2;
369 vfp_single_dump("sqrt estimate", &vsd
);
374 if ((vsd
.significand
& VFP_SINGLE_LOW_BITS_MASK
) <= 5) {
375 if (vsd
.significand
< 2) {
376 vsd
.significand
= 0xffffffff;
380 vsm
.significand
<<= !(vsm
.exponent
& 1);
381 term
= (u64
)vsd
.significand
* vsd
.significand
;
382 rem
= ((u64
)vsm
.significand
<< 32) - term
;
384 pr_debug("VFP: term=%016llx rem=%016llx\n", term
, rem
);
387 vsd
.significand
-= 1;
388 rem
+= ((u64
)vsd
.significand
<< 1) | 1;
390 vsd
.significand
|= rem
!= 0;
393 vsd
.significand
= vfp_shiftright32jamming(vsd
.significand
, 1);
395 return vfp_single_normaliseround(sd
, &vsd
, fpscr
, 0, "fsqrt");
404 static u32
vfp_compare(int sd
, int signal_on_qnan
, s32 m
, u32 fpscr
)
409 d
= vfp_get_float(sd
);
410 if (vfp_single_packed_exponent(m
) == 255 && vfp_single_packed_mantissa(m
)) {
411 ret
|= FPSCR_C
| FPSCR_V
;
412 if (signal_on_qnan
|| !(vfp_single_packed_mantissa(m
) & (1 << (VFP_SINGLE_MANTISSA_BITS
- 1))))
414 * Signalling NaN, or signalling on quiet NaN
419 if (vfp_single_packed_exponent(d
) == 255 && vfp_single_packed_mantissa(d
)) {
420 ret
|= FPSCR_C
| FPSCR_V
;
421 if (signal_on_qnan
|| !(vfp_single_packed_mantissa(d
) & (1 << (VFP_SINGLE_MANTISSA_BITS
- 1))))
423 * Signalling NaN, or signalling on quiet NaN
429 if (d
== m
|| vfp_single_packed_abs(d
| m
) == 0) {
433 ret
|= FPSCR_Z
| FPSCR_C
;
434 } else if (vfp_single_packed_sign(d
^ m
)) {
438 if (vfp_single_packed_sign(d
))
440 * d is negative, so d < m
445 * d is positive, so d > m
448 } else if ((vfp_single_packed_sign(d
) != 0) ^ (d
< m
)) {
453 } else if ((vfp_single_packed_sign(d
) != 0) ^ (d
> m
)) {
463 static u32
vfp_single_fcmp(int sd
, int unused
, s32 m
, u32 fpscr
)
465 return vfp_compare(sd
, 0, m
, fpscr
);
468 static u32
vfp_single_fcmpe(int sd
, int unused
, s32 m
, u32 fpscr
)
470 return vfp_compare(sd
, 1, m
, fpscr
);
473 static u32
vfp_single_fcmpz(int sd
, int unused
, s32 m
, u32 fpscr
)
475 return vfp_compare(sd
, 0, 0, fpscr
);
478 static u32
vfp_single_fcmpez(int sd
, int unused
, s32 m
, u32 fpscr
)
480 return vfp_compare(sd
, 1, 0, fpscr
);
483 static u32
vfp_single_fcvtd(int dd
, int unused
, s32 m
, u32 fpscr
)
485 struct vfp_single vsm
;
486 struct vfp_double vdd
;
490 vfp_single_unpack(&vsm
, m
);
492 tm
= vfp_single_type(&vsm
);
495 * If we have a signalling NaN, signal invalid operation.
498 exceptions
= FPSCR_IOC
;
500 if (tm
& VFP_DENORMAL
)
501 vfp_single_normalise_denormal(&vsm
);
504 vdd
.significand
= (u64
)vsm
.significand
<< 32;
507 * If we have an infinity or NaN, the exponent must be 2047.
509 if (tm
& (VFP_INFINITY
|VFP_NAN
)) {
512 vdd
.significand
|= VFP_DOUBLE_SIGNIFICAND_QNAN
;
514 } else if (tm
& VFP_ZERO
)
517 vdd
.exponent
= vsm
.exponent
+ (1023 - 127);
519 return vfp_double_normaliseround(dd
, &vdd
, fpscr
, exceptions
, "fcvtd");
522 vfp_put_double(vfp_double_pack(&vdd
), dd
);
526 static u32
vfp_single_fuito(int sd
, int unused
, s32 m
, u32 fpscr
)
528 struct vfp_single vs
;
531 vs
.exponent
= 127 + 31 - 1;
532 vs
.significand
= (u32
)m
;
534 return vfp_single_normaliseround(sd
, &vs
, fpscr
, 0, "fuito");
537 static u32
vfp_single_fsito(int sd
, int unused
, s32 m
, u32 fpscr
)
539 struct vfp_single vs
;
541 vs
.sign
= (m
& 0x80000000) >> 16;
542 vs
.exponent
= 127 + 31 - 1;
543 vs
.significand
= vs
.sign
? -m
: m
;
545 return vfp_single_normaliseround(sd
, &vs
, fpscr
, 0, "fsito");
548 static u32
vfp_single_ftoui(int sd
, int unused
, s32 m
, u32 fpscr
)
550 struct vfp_single vsm
;
551 u32 d
, exceptions
= 0;
552 int rmode
= fpscr
& FPSCR_RMODE_MASK
;
555 vfp_single_unpack(&vsm
, m
);
556 vfp_single_dump("VSM", &vsm
);
559 * Do we have a denormalised number?
561 tm
= vfp_single_type(&vsm
);
562 if (tm
& VFP_DENORMAL
)
563 exceptions
|= FPSCR_IDC
;
568 if (vsm
.exponent
>= 127 + 32) {
569 d
= vsm
.sign
? 0 : 0xffffffff;
570 exceptions
= FPSCR_IOC
;
571 } else if (vsm
.exponent
>= 127 - 1) {
572 int shift
= 127 + 31 - vsm
.exponent
;
576 * 2^0 <= m < 2^32-2^8
578 d
= (vsm
.significand
<< 1) >> shift
;
579 rem
= vsm
.significand
<< (33 - shift
);
581 if (rmode
== FPSCR_ROUND_NEAREST
) {
585 } else if (rmode
== FPSCR_ROUND_TOZERO
) {
587 } else if ((rmode
== FPSCR_ROUND_PLUSINF
) ^ (vsm
.sign
!= 0)) {
591 if ((rem
+ incr
) < rem
) {
595 exceptions
|= FPSCR_IOC
;
600 exceptions
|= FPSCR_IOC
;
602 exceptions
|= FPSCR_IXC
;
605 if (vsm
.exponent
| vsm
.significand
) {
606 exceptions
|= FPSCR_IXC
;
607 if (rmode
== FPSCR_ROUND_PLUSINF
&& vsm
.sign
== 0)
609 else if (rmode
== FPSCR_ROUND_MINUSINF
&& vsm
.sign
) {
611 exceptions
|= FPSCR_IOC
;
616 pr_debug("VFP: ftoui: d(s%d)=%08x exceptions=%08x\n", sd
, d
, exceptions
);
618 vfp_put_float(d
, sd
);
623 static u32
vfp_single_ftouiz(int sd
, int unused
, s32 m
, u32 fpscr
)
625 return vfp_single_ftoui(sd
, unused
, m
, FPSCR_ROUND_TOZERO
);
628 static u32
vfp_single_ftosi(int sd
, int unused
, s32 m
, u32 fpscr
)
630 struct vfp_single vsm
;
631 u32 d
, exceptions
= 0;
632 int rmode
= fpscr
& FPSCR_RMODE_MASK
;
635 vfp_single_unpack(&vsm
, m
);
636 vfp_single_dump("VSM", &vsm
);
639 * Do we have a denormalised number?
641 tm
= vfp_single_type(&vsm
);
642 if (vfp_single_type(&vsm
) & VFP_DENORMAL
)
643 exceptions
|= FPSCR_IDC
;
647 exceptions
|= FPSCR_IOC
;
648 } else if (vsm
.exponent
>= 127 + 32) {
650 * m >= 2^31-2^7: invalid
655 exceptions
|= FPSCR_IOC
;
656 } else if (vsm
.exponent
>= 127 - 1) {
657 int shift
= 127 + 31 - vsm
.exponent
;
660 /* 2^0 <= m <= 2^31-2^7 */
661 d
= (vsm
.significand
<< 1) >> shift
;
662 rem
= vsm
.significand
<< (33 - shift
);
664 if (rmode
== FPSCR_ROUND_NEAREST
) {
668 } else if (rmode
== FPSCR_ROUND_TOZERO
) {
670 } else if ((rmode
== FPSCR_ROUND_PLUSINF
) ^ (vsm
.sign
!= 0)) {
674 if ((rem
+ incr
) < rem
&& d
< 0xffffffff)
676 if (d
> 0x7fffffff + (vsm
.sign
!= 0)) {
677 d
= 0x7fffffff + (vsm
.sign
!= 0);
678 exceptions
|= FPSCR_IOC
;
680 exceptions
|= FPSCR_IXC
;
686 if (vsm
.exponent
| vsm
.significand
) {
687 exceptions
|= FPSCR_IXC
;
688 if (rmode
== FPSCR_ROUND_PLUSINF
&& vsm
.sign
== 0)
690 else if (rmode
== FPSCR_ROUND_MINUSINF
&& vsm
.sign
)
695 pr_debug("VFP: ftosi: d(s%d)=%08x exceptions=%08x\n", sd
, d
, exceptions
);
697 vfp_put_float((s32
)d
, sd
);
702 static u32
vfp_single_ftosiz(int sd
, int unused
, s32 m
, u32 fpscr
)
704 return vfp_single_ftosi(sd
, unused
, m
, FPSCR_ROUND_TOZERO
);
707 static struct op fops_ext
[32] = {
708 [FEXT_TO_IDX(FEXT_FCPY
)] = { vfp_single_fcpy
, 0 },
709 [FEXT_TO_IDX(FEXT_FABS
)] = { vfp_single_fabs
, 0 },
710 [FEXT_TO_IDX(FEXT_FNEG
)] = { vfp_single_fneg
, 0 },
711 [FEXT_TO_IDX(FEXT_FSQRT
)] = { vfp_single_fsqrt
, 0 },
712 [FEXT_TO_IDX(FEXT_FCMP
)] = { vfp_single_fcmp
, OP_SCALAR
},
713 [FEXT_TO_IDX(FEXT_FCMPE
)] = { vfp_single_fcmpe
, OP_SCALAR
},
714 [FEXT_TO_IDX(FEXT_FCMPZ
)] = { vfp_single_fcmpz
, OP_SCALAR
},
715 [FEXT_TO_IDX(FEXT_FCMPEZ
)] = { vfp_single_fcmpez
, OP_SCALAR
},
716 [FEXT_TO_IDX(FEXT_FCVT
)] = { vfp_single_fcvtd
, OP_SCALAR
|OP_DD
},
717 [FEXT_TO_IDX(FEXT_FUITO
)] = { vfp_single_fuito
, OP_SCALAR
},
718 [FEXT_TO_IDX(FEXT_FSITO
)] = { vfp_single_fsito
, OP_SCALAR
},
719 [FEXT_TO_IDX(FEXT_FTOUI
)] = { vfp_single_ftoui
, OP_SCALAR
},
720 [FEXT_TO_IDX(FEXT_FTOUIZ
)] = { vfp_single_ftouiz
, OP_SCALAR
},
721 [FEXT_TO_IDX(FEXT_FTOSI
)] = { vfp_single_ftosi
, OP_SCALAR
},
722 [FEXT_TO_IDX(FEXT_FTOSIZ
)] = { vfp_single_ftosiz
, OP_SCALAR
},
730 vfp_single_fadd_nonnumber(struct vfp_single
*vsd
, struct vfp_single
*vsn
,
731 struct vfp_single
*vsm
, u32 fpscr
)
733 struct vfp_single
*vsp
;
737 tn
= vfp_single_type(vsn
);
738 tm
= vfp_single_type(vsm
);
740 if (tn
& tm
& VFP_INFINITY
) {
742 * Two infinities. Are they different signs?
744 if (vsn
->sign
^ vsm
->sign
) {
746 * different signs -> invalid
748 exceptions
= FPSCR_IOC
;
749 vsp
= &vfp_single_default_qnan
;
752 * same signs -> valid
756 } else if (tn
& VFP_INFINITY
&& tm
& VFP_NUMBER
) {
758 * One infinity and one number -> infinity
763 * 'n' is a NaN of some type
765 return vfp_propagate_nan(vsd
, vsn
, vsm
, fpscr
);
772 vfp_single_add(struct vfp_single
*vsd
, struct vfp_single
*vsn
,
773 struct vfp_single
*vsm
, u32 fpscr
)
777 if (vsn
->significand
& 0x80000000 ||
778 vsm
->significand
& 0x80000000) {
779 pr_info("VFP: bad FP values in %s\n", __func__
);
780 vfp_single_dump("VSN", vsn
);
781 vfp_single_dump("VSM", vsm
);
785 * Ensure that 'n' is the largest magnitude number. Note that
786 * if 'n' and 'm' have equal exponents, we do not swap them.
787 * This ensures that NaN propagation works correctly.
789 if (vsn
->exponent
< vsm
->exponent
) {
790 struct vfp_single
*t
= vsn
;
796 * Is 'n' an infinity or a NaN? Note that 'm' may be a number,
797 * infinity or a NaN here.
799 if (vsn
->exponent
== 255)
800 return vfp_single_fadd_nonnumber(vsd
, vsn
, vsm
, fpscr
);
803 * We have two proper numbers, where 'vsn' is the larger magnitude.
805 * Copy 'n' to 'd' before doing the arithmetic.
810 * Align both numbers.
812 exp_diff
= vsn
->exponent
- vsm
->exponent
;
813 m_sig
= vfp_shiftright32jamming(vsm
->significand
, exp_diff
);
816 * If the signs are different, we are really subtracting.
818 if (vsn
->sign
^ vsm
->sign
) {
819 m_sig
= vsn
->significand
- m_sig
;
820 if ((s32
)m_sig
< 0) {
821 vsd
->sign
= vfp_sign_negate(vsd
->sign
);
823 } else if (m_sig
== 0) {
824 vsd
->sign
= (fpscr
& FPSCR_RMODE_MASK
) ==
825 FPSCR_ROUND_MINUSINF
? 0x8000 : 0;
828 m_sig
= vsn
->significand
+ m_sig
;
830 vsd
->significand
= m_sig
;
836 vfp_single_multiply(struct vfp_single
*vsd
, struct vfp_single
*vsn
, struct vfp_single
*vsm
, u32 fpscr
)
838 vfp_single_dump("VSN", vsn
);
839 vfp_single_dump("VSM", vsm
);
842 * Ensure that 'n' is the largest magnitude number. Note that
843 * if 'n' and 'm' have equal exponents, we do not swap them.
844 * This ensures that NaN propagation works correctly.
846 if (vsn
->exponent
< vsm
->exponent
) {
847 struct vfp_single
*t
= vsn
;
850 pr_debug("VFP: swapping M <-> N\n");
853 vsd
->sign
= vsn
->sign
^ vsm
->sign
;
856 * If 'n' is an infinity or NaN, handle it. 'm' may be anything.
858 if (vsn
->exponent
== 255) {
859 if (vsn
->significand
|| (vsm
->exponent
== 255 && vsm
->significand
))
860 return vfp_propagate_nan(vsd
, vsn
, vsm
, fpscr
);
861 if ((vsm
->exponent
| vsm
->significand
) == 0) {
862 *vsd
= vfp_single_default_qnan
;
865 vsd
->exponent
= vsn
->exponent
;
866 vsd
->significand
= 0;
871 * If 'm' is zero, the result is always zero. In this case,
872 * 'n' may be zero or a number, but it doesn't matter which.
874 if ((vsm
->exponent
| vsm
->significand
) == 0) {
876 vsd
->significand
= 0;
881 * We add 2 to the destination exponent for the same reason as
882 * the addition case - though this time we have +1 from each
885 vsd
->exponent
= vsn
->exponent
+ vsm
->exponent
- 127 + 2;
886 vsd
->significand
= vfp_hi64to32jamming((u64
)vsn
->significand
* vsm
->significand
);
888 vfp_single_dump("VSD", vsd
);
892 #define NEG_MULTIPLY (1 << 0)
893 #define NEG_SUBTRACT (1 << 1)
896 vfp_single_multiply_accumulate(int sd
, int sn
, s32 m
, u32 fpscr
, u32 negate
, char *func
)
898 struct vfp_single vsd
, vsp
, vsn
, vsm
;
902 v
= vfp_get_float(sn
);
903 pr_debug("VFP: s%u = %08x\n", sn
, v
);
904 vfp_single_unpack(&vsn
, v
);
905 if (vsn
.exponent
== 0 && vsn
.significand
)
906 vfp_single_normalise_denormal(&vsn
);
908 vfp_single_unpack(&vsm
, m
);
909 if (vsm
.exponent
== 0 && vsm
.significand
)
910 vfp_single_normalise_denormal(&vsm
);
912 exceptions
= vfp_single_multiply(&vsp
, &vsn
, &vsm
, fpscr
);
913 if (negate
& NEG_MULTIPLY
)
914 vsp
.sign
= vfp_sign_negate(vsp
.sign
);
916 v
= vfp_get_float(sd
);
917 pr_debug("VFP: s%u = %08x\n", sd
, v
);
918 vfp_single_unpack(&vsn
, v
);
919 if (negate
& NEG_SUBTRACT
)
920 vsn
.sign
= vfp_sign_negate(vsn
.sign
);
922 exceptions
|= vfp_single_add(&vsd
, &vsn
, &vsp
, fpscr
);
924 return vfp_single_normaliseround(sd
, &vsd
, fpscr
, exceptions
, func
);
928 * Standard operations
932 * sd = sd + (sn * sm)
934 static u32
vfp_single_fmac(int sd
, int sn
, s32 m
, u32 fpscr
)
936 return vfp_single_multiply_accumulate(sd
, sn
, m
, fpscr
, 0, "fmac");
940 * sd = sd - (sn * sm)
942 static u32
vfp_single_fnmac(int sd
, int sn
, s32 m
, u32 fpscr
)
944 return vfp_single_multiply_accumulate(sd
, sn
, m
, fpscr
, NEG_MULTIPLY
, "fnmac");
948 * sd = -sd + (sn * sm)
950 static u32
vfp_single_fmsc(int sd
, int sn
, s32 m
, u32 fpscr
)
952 return vfp_single_multiply_accumulate(sd
, sn
, m
, fpscr
, NEG_SUBTRACT
, "fmsc");
956 * sd = -sd - (sn * sm)
958 static u32
vfp_single_fnmsc(int sd
, int sn
, s32 m
, u32 fpscr
)
960 return vfp_single_multiply_accumulate(sd
, sn
, m
, fpscr
, NEG_SUBTRACT
| NEG_MULTIPLY
, "fnmsc");
966 static u32
vfp_single_fmul(int sd
, int sn
, s32 m
, u32 fpscr
)
968 struct vfp_single vsd
, vsn
, vsm
;
970 s32 n
= vfp_get_float(sn
);
972 pr_debug("VFP: s%u = %08x\n", sn
, n
);
974 vfp_single_unpack(&vsn
, n
);
975 if (vsn
.exponent
== 0 && vsn
.significand
)
976 vfp_single_normalise_denormal(&vsn
);
978 vfp_single_unpack(&vsm
, m
);
979 if (vsm
.exponent
== 0 && vsm
.significand
)
980 vfp_single_normalise_denormal(&vsm
);
982 exceptions
= vfp_single_multiply(&vsd
, &vsn
, &vsm
, fpscr
);
983 return vfp_single_normaliseround(sd
, &vsd
, fpscr
, exceptions
, "fmul");
989 static u32
vfp_single_fnmul(int sd
, int sn
, s32 m
, u32 fpscr
)
991 struct vfp_single vsd
, vsn
, vsm
;
993 s32 n
= vfp_get_float(sn
);
995 pr_debug("VFP: s%u = %08x\n", sn
, n
);
997 vfp_single_unpack(&vsn
, n
);
998 if (vsn
.exponent
== 0 && vsn
.significand
)
999 vfp_single_normalise_denormal(&vsn
);
1001 vfp_single_unpack(&vsm
, m
);
1002 if (vsm
.exponent
== 0 && vsm
.significand
)
1003 vfp_single_normalise_denormal(&vsm
);
1005 exceptions
= vfp_single_multiply(&vsd
, &vsn
, &vsm
, fpscr
);
1006 vsd
.sign
= vfp_sign_negate(vsd
.sign
);
1007 return vfp_single_normaliseround(sd
, &vsd
, fpscr
, exceptions
, "fnmul");
1013 static u32
vfp_single_fadd(int sd
, int sn
, s32 m
, u32 fpscr
)
1015 struct vfp_single vsd
, vsn
, vsm
;
1017 s32 n
= vfp_get_float(sn
);
1019 pr_debug("VFP: s%u = %08x\n", sn
, n
);
1022 * Unpack and normalise denormals.
1024 vfp_single_unpack(&vsn
, n
);
1025 if (vsn
.exponent
== 0 && vsn
.significand
)
1026 vfp_single_normalise_denormal(&vsn
);
1028 vfp_single_unpack(&vsm
, m
);
1029 if (vsm
.exponent
== 0 && vsm
.significand
)
1030 vfp_single_normalise_denormal(&vsm
);
1032 exceptions
= vfp_single_add(&vsd
, &vsn
, &vsm
, fpscr
);
1034 return vfp_single_normaliseround(sd
, &vsd
, fpscr
, exceptions
, "fadd");
1040 static u32
vfp_single_fsub(int sd
, int sn
, s32 m
, u32 fpscr
)
1043 * Subtraction is addition with one sign inverted.
1045 return vfp_single_fadd(sd
, sn
, vfp_single_packed_negate(m
), fpscr
);
1051 static u32
vfp_single_fdiv(int sd
, int sn
, s32 m
, u32 fpscr
)
1053 struct vfp_single vsd
, vsn
, vsm
;
1055 s32 n
= vfp_get_float(sn
);
1058 pr_debug("VFP: s%u = %08x\n", sn
, n
);
1060 vfp_single_unpack(&vsn
, n
);
1061 vfp_single_unpack(&vsm
, m
);
1063 vsd
.sign
= vsn
.sign
^ vsm
.sign
;
1065 tn
= vfp_single_type(&vsn
);
1066 tm
= vfp_single_type(&vsm
);
1081 * If n and m are infinity, the result is invalid
1082 * If n and m are zero, the result is invalid
1084 if (tm
& tn
& (VFP_INFINITY
|VFP_ZERO
))
1088 * If n is infinity, the result is infinity
1090 if (tn
& VFP_INFINITY
)
1094 * If m is zero, raise div0 exception
1100 * If m is infinity, or n is zero, the result is zero
1102 if (tm
& VFP_INFINITY
|| tn
& VFP_ZERO
)
1105 if (tn
& VFP_DENORMAL
)
1106 vfp_single_normalise_denormal(&vsn
);
1107 if (tm
& VFP_DENORMAL
)
1108 vfp_single_normalise_denormal(&vsm
);
1111 * Ok, we have two numbers, we can perform division.
1113 vsd
.exponent
= vsn
.exponent
- vsm
.exponent
+ 127 - 1;
1114 vsm
.significand
<<= 1;
1115 if (vsm
.significand
<= (2 * vsn
.significand
)) {
1116 vsn
.significand
>>= 1;
1120 u64 significand
= (u64
)vsn
.significand
<< 32;
1121 do_div(significand
, vsm
.significand
);
1122 vsd
.significand
= significand
;
1124 if ((vsd
.significand
& 0x3f) == 0)
1125 vsd
.significand
|= ((u64
)vsm
.significand
* vsd
.significand
!= (u64
)vsn
.significand
<< 32);
1127 return vfp_single_normaliseround(sd
, &vsd
, fpscr
, 0, "fdiv");
1130 exceptions
= vfp_propagate_nan(&vsd
, &vsn
, &vsm
, fpscr
);
1132 vfp_put_float(vfp_single_pack(&vsd
), sd
);
1136 exceptions
= vfp_propagate_nan(&vsd
, &vsm
, &vsn
, fpscr
);
1141 vsd
.significand
= 0;
1145 exceptions
= FPSCR_DZC
;
1148 vsd
.significand
= 0;
1152 vfp_put_float(vfp_single_pack(&vfp_single_default_qnan
), sd
);
1156 static struct op fops
[16] = {
1157 [FOP_TO_IDX(FOP_FMAC
)] = { vfp_single_fmac
, 0 },
1158 [FOP_TO_IDX(FOP_FNMAC
)] = { vfp_single_fnmac
, 0 },
1159 [FOP_TO_IDX(FOP_FMSC
)] = { vfp_single_fmsc
, 0 },
1160 [FOP_TO_IDX(FOP_FNMSC
)] = { vfp_single_fnmsc
, 0 },
1161 [FOP_TO_IDX(FOP_FMUL
)] = { vfp_single_fmul
, 0 },
1162 [FOP_TO_IDX(FOP_FNMUL
)] = { vfp_single_fnmul
, 0 },
1163 [FOP_TO_IDX(FOP_FADD
)] = { vfp_single_fadd
, 0 },
1164 [FOP_TO_IDX(FOP_FSUB
)] = { vfp_single_fsub
, 0 },
1165 [FOP_TO_IDX(FOP_FDIV
)] = { vfp_single_fdiv
, 0 },
1168 #define FREG_BANK(x) ((x) & 0x18)
1169 #define FREG_IDX(x) ((x) & 7)
1171 u32
vfp_single_cpdo(u32 inst
, u32 fpscr
)
1173 u32 op
= inst
& FOP_MASK
;
1176 unsigned int sn
= vfp_get_sn(inst
);
1177 unsigned int sm
= vfp_get_sm(inst
);
1178 unsigned int vecitr
, veclen
, vecstride
;
1181 vecstride
= 1 + ((fpscr
& FPSCR_STRIDE_MASK
) == FPSCR_STRIDE_MASK
);
1183 fop
= (op
== FOP_EXT
) ? &fops_ext
[FEXT_TO_IDX(inst
)] : &fops
[FOP_TO_IDX(op
)];
1186 * fcvtsd takes a dN register number as destination, not sN.
1187 * Technically, if bit 0 of dd is set, this is an invalid
1188 * instruction. However, we ignore this for efficiency.
1189 * It also only operates on scalars.
1191 if (fop
->flags
& OP_DD
)
1192 dest
= vfp_get_dd(inst
);
1194 dest
= vfp_get_sd(inst
);
1197 * If destination bank is zero, vector length is always '1'.
1198 * ARM DDI0100F C5.1.3, C5.3.2.
1200 if ((fop
->flags
& OP_SCALAR
) || FREG_BANK(dest
) == 0)
1203 veclen
= fpscr
& FPSCR_LENGTH_MASK
;
1205 pr_debug("VFP: vecstride=%u veclen=%u\n", vecstride
,
1206 (veclen
>> FPSCR_LENGTH_BIT
) + 1);
1211 for (vecitr
= 0; vecitr
<= veclen
; vecitr
+= 1 << FPSCR_LENGTH_BIT
) {
1212 s32 m
= vfp_get_float(sm
);
1216 type
= fop
->flags
& OP_DD
? 'd' : 's';
1218 pr_debug("VFP: itr%d (%c%u) = op[%u] (s%u=%08x)\n",
1219 vecitr
>> FPSCR_LENGTH_BIT
, type
, dest
, sn
,
1222 pr_debug("VFP: itr%d (%c%u) = (s%u) op[%u] (s%u=%08x)\n",
1223 vecitr
>> FPSCR_LENGTH_BIT
, type
, dest
, sn
,
1224 FOP_TO_IDX(op
), sm
, m
);
1226 except
= fop
->fn(dest
, sn
, m
, fpscr
);
1227 pr_debug("VFP: itr%d: exceptions=%08x\n",
1228 vecitr
>> FPSCR_LENGTH_BIT
, except
);
1230 exceptions
|= except
;
1233 * CHECK: It appears to be undefined whether we stop when
1234 * we encounter an exception. We continue.
1236 dest
= FREG_BANK(dest
) + ((FREG_IDX(dest
) + vecstride
) & 7);
1237 sn
= FREG_BANK(sn
) + ((FREG_IDX(sn
) + vecstride
) & 7);
1238 if (FREG_BANK(sm
) != 0)
1239 sm
= FREG_BANK(sm
) + ((FREG_IDX(sm
) + vecstride
) & 7);