📄 vfpsingle.c
字号:
return vfp_single_ftoui(sd, unused, m, FPSCR_ROUND_TOZERO);}static u32 vfp_single_ftosi(int sd, int unused, s32 m, u32 fpscr){ struct vfp_single vsm; u32 d, exceptions = 0; int rmode = fpscr & FPSCR_RMODE_MASK; int tm; vfp_single_unpack(&vsm, m); vfp_single_dump("VSM", &vsm); /* * Do we have a denormalised number? */ tm = vfp_single_type(&vsm); if (vfp_single_type(&vsm) & VFP_DENORMAL) exceptions |= FPSCR_IDC; if (tm & VFP_NAN) { d = 0; exceptions |= FPSCR_IOC; } else if (vsm.exponent >= 127 + 32) { /* * m >= 2^31-2^7: invalid */ d = 0x7fffffff; if (vsm.sign) d = ~d; exceptions |= FPSCR_IOC; } else if (vsm.exponent >= 127 - 1) { int shift = 127 + 31 - vsm.exponent; u32 rem, incr = 0; /* 2^0 <= m <= 2^31-2^7 */ d = (vsm.significand << 1) >> shift; rem = vsm.significand << (33 - shift); if (rmode == FPSCR_ROUND_NEAREST) { incr = 0x80000000; if ((d & 1) == 0) incr -= 1; } else if (rmode == FPSCR_ROUND_TOZERO) { incr = 0; } else if ((rmode == FPSCR_ROUND_PLUSINF) ^ (vsm.sign != 0)) { incr = ~0; } if ((rem + incr) < rem && d < 0xffffffff) d += 1; if (d > 0x7fffffff + (vsm.sign != 0)) { d = 0x7fffffff + (vsm.sign != 0); exceptions |= FPSCR_IOC; } else if (rem) exceptions |= FPSCR_IXC; if (vsm.sign) d = -d; } else { d = 0; if (vsm.exponent | vsm.significand) { exceptions |= FPSCR_IXC; if (rmode == FPSCR_ROUND_PLUSINF && vsm.sign == 0) d = 1; else if (rmode == FPSCR_ROUND_MINUSINF && vsm.sign) d = -1; } } pr_debug("VFP: ftosi: d(s%d)=%08x exceptions=%08x\n", sd, d, exceptions); vfp_put_float((s32)d, sd); return exceptions;}static u32 vfp_single_ftosiz(int sd, int unused, s32 m, u32 fpscr){ return vfp_single_ftosi(sd, unused, m, FPSCR_ROUND_TOZERO);}static struct op fops_ext[32] = { [FEXT_TO_IDX(FEXT_FCPY)] = { vfp_single_fcpy, 0 }, [FEXT_TO_IDX(FEXT_FABS)] = { vfp_single_fabs, 0 }, [FEXT_TO_IDX(FEXT_FNEG)] = { vfp_single_fneg, 0 }, [FEXT_TO_IDX(FEXT_FSQRT)] = { vfp_single_fsqrt, 0 }, [FEXT_TO_IDX(FEXT_FCMP)] = { vfp_single_fcmp, OP_SCALAR }, [FEXT_TO_IDX(FEXT_FCMPE)] = { vfp_single_fcmpe, OP_SCALAR }, [FEXT_TO_IDX(FEXT_FCMPZ)] = { vfp_single_fcmpz, OP_SCALAR }, [FEXT_TO_IDX(FEXT_FCMPEZ)] = { vfp_single_fcmpez, OP_SCALAR }, [FEXT_TO_IDX(FEXT_FCVT)] = { vfp_single_fcvtd, OP_SCALAR|OP_DD }, [FEXT_TO_IDX(FEXT_FUITO)] = { vfp_single_fuito, OP_SCALAR }, [FEXT_TO_IDX(FEXT_FSITO)] = { vfp_single_fsito, OP_SCALAR }, [FEXT_TO_IDX(FEXT_FTOUI)] = { vfp_single_ftoui, OP_SCALAR }, [FEXT_TO_IDX(FEXT_FTOUIZ)] = { vfp_single_ftouiz, OP_SCALAR }, [FEXT_TO_IDX(FEXT_FTOSI)] = { vfp_single_ftosi, OP_SCALAR }, [FEXT_TO_IDX(FEXT_FTOSIZ)] = { vfp_single_ftosiz, OP_SCALAR },};static u32vfp_single_fadd_nonnumber(struct vfp_single *vsd, struct vfp_single *vsn, struct vfp_single *vsm, u32 fpscr){ struct vfp_single *vsp; u32 exceptions = 0; int tn, tm; tn = vfp_single_type(vsn); tm = vfp_single_type(vsm); if (tn & tm & VFP_INFINITY) { /* * Two infinities. Are they different signs? */ if (vsn->sign ^ vsm->sign) { /* * different signs -> invalid */ exceptions = FPSCR_IOC; vsp = &vfp_single_default_qnan; } else { /* * same signs -> valid */ vsp = vsn; } } else if (tn & VFP_INFINITY && tm & VFP_NUMBER) { /* * One infinity and one number -> infinity */ vsp = vsn; } else { /* * 'n' is a NaN of some type */ return vfp_propagate_nan(vsd, vsn, vsm, fpscr); } *vsd = *vsp; return exceptions;}static u32vfp_single_add(struct vfp_single *vsd, struct vfp_single *vsn, struct vfp_single *vsm, u32 fpscr){ u32 exp_diff, m_sig; if (vsn->significand & 0x80000000 || vsm->significand & 0x80000000) { pr_info("VFP: bad FP values in %s\n", __func__); vfp_single_dump("VSN", vsn); vfp_single_dump("VSM", vsm); } /* * Ensure that 'n' is the largest magnitude number. Note that * if 'n' and 'm' have equal exponents, we do not swap them. * This ensures that NaN propagation works correctly. */ if (vsn->exponent < vsm->exponent) { struct vfp_single *t = vsn; vsn = vsm; vsm = t; } /* * Is 'n' an infinity or a NaN? Note that 'm' may be a number, * infinity or a NaN here. */ if (vsn->exponent == 255) return vfp_single_fadd_nonnumber(vsd, vsn, vsm, fpscr); /* * We have two proper numbers, where 'vsn' is the larger magnitude. * * Copy 'n' to 'd' before doing the arithmetic. */ *vsd = *vsn; /* * Align both numbers. */ exp_diff = vsn->exponent - vsm->exponent; m_sig = vfp_shiftright32jamming(vsm->significand, exp_diff); /* * If the signs are different, we are really subtracting. */ if (vsn->sign ^ vsm->sign) { m_sig = vsn->significand - m_sig; if ((s32)m_sig < 0) { vsd->sign = vfp_sign_negate(vsd->sign); m_sig = -m_sig; } else if (m_sig == 0) { vsd->sign = (fpscr & FPSCR_RMODE_MASK) == FPSCR_ROUND_MINUSINF ? 0x8000 : 0; } } else { m_sig = vsn->significand + m_sig; } vsd->significand = m_sig; return 0;}static u32vfp_single_multiply(struct vfp_single *vsd, struct vfp_single *vsn, struct vfp_single *vsm, u32 fpscr){ vfp_single_dump("VSN", vsn); vfp_single_dump("VSM", vsm); /* * Ensure that 'n' is the largest magnitude number. Note that * if 'n' and 'm' have equal exponents, we do not swap them. * This ensures that NaN propagation works correctly. */ if (vsn->exponent < vsm->exponent) { struct vfp_single *t = vsn; vsn = vsm; vsm = t; pr_debug("VFP: swapping M <-> N\n"); } vsd->sign = vsn->sign ^ vsm->sign; /* * If 'n' is an infinity or NaN, handle it. 'm' may be anything. */ if (vsn->exponent == 255) { if (vsn->significand || (vsm->exponent == 255 && vsm->significand)) return vfp_propagate_nan(vsd, vsn, vsm, fpscr); if ((vsm->exponent | vsm->significand) == 0) { *vsd = vfp_single_default_qnan; return FPSCR_IOC; } vsd->exponent = vsn->exponent; vsd->significand = 0; return 0; } /* * If 'm' is zero, the result is always zero. In this case, * 'n' may be zero or a number, but it doesn't matter which. */ if ((vsm->exponent | vsm->significand) == 0) { vsd->exponent = 0; vsd->significand = 0; return 0; } /* * We add 2 to the destination exponent for the same reason as * the addition case - though this time we have +1 from each * input operand. */ vsd->exponent = vsn->exponent + vsm->exponent - 127 + 2; vsd->significand = vfp_hi64to32jamming((u64)vsn->significand * vsm->significand); vfp_single_dump("VSD", vsd); return 0;}#define NEG_MULTIPLY (1 << 0)#define NEG_SUBTRACT (1 << 1)static u32vfp_single_multiply_accumulate(int sd, int sn, s32 m, u32 fpscr, u32 negate, char *func){ struct vfp_single vsd, vsp, vsn, vsm; u32 exceptions; s32 v; v = vfp_get_float(sn); pr_debug("VFP: s%u = %08x\n", sn, v); vfp_single_unpack(&vsn, v); if (vsn.exponent == 0 && vsn.significand) vfp_single_normalise_denormal(&vsn); vfp_single_unpack(&vsm, m); if (vsm.exponent == 0 && vsm.significand) vfp_single_normalise_denormal(&vsm); exceptions = vfp_single_multiply(&vsp, &vsn, &vsm, fpscr); if (negate & NEG_MULTIPLY) vsp.sign = vfp_sign_negate(vsp.sign); v = vfp_get_float(sd); pr_debug("VFP: s%u = %08x\n", sd, v); vfp_single_unpack(&vsn, v); if (negate & NEG_SUBTRACT) vsn.sign = vfp_sign_negate(vsn.sign); exceptions |= vfp_single_add(&vsd, &vsn, &vsp, fpscr); return vfp_single_normaliseround(sd, &vsd, fpscr, exceptions, func);}/* * Standard operations *//* * sd = sd + (sn * sm) */static u32 vfp_single_fmac(int sd, int sn, s32 m, u32 fpscr){ return vfp_single_multiply_accumulate(sd, sn, m, fpscr, 0, "fmac");}/* * sd = sd - (sn * sm) */static u32 vfp_single_fnmac(int sd, int sn, s32 m, u32 fpscr){ return vfp_single_multiply_accumulate(sd, sn, m, fpscr, NEG_MULTIPLY, "fnmac");}/* * sd = -sd + (sn * sm) */static u32 vfp_single_fmsc(int sd, int sn, s32 m, u32 fpscr){ return vfp_single_multiply_accumulate(sd, sn, m, fpscr, NEG_SUBTRACT, "fmsc");}/* * sd = -sd - (sn * sm) */static u32 vfp_single_fnmsc(int sd, int sn, s32 m, u32 fpscr){ return vfp_single_multiply_accumulate(sd, sn, m, fpscr, NEG_SUBTRACT | NEG_MULTIPLY, "fnmsc");}/* * sd = sn * sm */static u32 vfp_single_fmul(int sd, int sn, s32 m, u32 fpscr){ struct vfp_single vsd, vsn, vsm; u32 exceptions; s32 n = vfp_get_float(sn); pr_debug("VFP: s%u = %08x\n", sn, n); vfp_single_unpack(&vsn, n); if (vsn.exponent == 0 && vsn.significand) vfp_single_normalise_denormal(&vsn); vfp_single_unpack(&vsm, m); if (vsm.exponent == 0 && vsm.significand) vfp_single_normalise_denormal(&vsm); exceptions = vfp_single_multiply(&vsd, &vsn, &vsm, fpscr); return vfp_single_normaliseround(sd, &vsd, fpscr, exceptions, "fmul");}/* * sd = -(sn * sm) */static u32 vfp_single_fnmul(int sd, int sn, s32 m, u32 fpscr){ struct vfp_single vsd, vsn, vsm; u32 exceptions; s32 n = vfp_get_float(sn); pr_debug("VFP: s%u = %08x\n", sn, n); vfp_single_unpack(&vsn, n); if (vsn.exponent == 0 && vsn.significand) vfp_single_normalise_denormal(&vsn); vfp_single_unpack(&vsm, m); if (vsm.exponent == 0 && vsm.significand) vfp_single_normalise_denormal(&vsm); exceptions = vfp_single_multiply(&vsd, &vsn, &vsm, fpscr); vsd.sign = vfp_sign_negate(vsd.sign); return vfp_single_normaliseround(sd, &vsd, fpscr, exceptions, "fnmul");}/* * sd = sn + sm */static u32 vfp_single_fadd(int sd, int sn, s32 m, u32 fpscr){ struct vfp_single vsd, vsn, vsm; u32 exceptions; s32 n = vfp_get_float(sn); pr_debug("VFP: s%u = %08x\n", sn, n); /* * Unpack and normalise denormals. */ vfp_single_unpack(&vsn, n); if (vsn.exponent == 0 && vsn.significand) vfp_single_normalise_denormal(&vsn); vfp_single_unpack(&vsm, m); if (vsm.exponent == 0 && vsm.significand) vfp_single_normalise_denormal(&vsm); exceptions = vfp_single_add(&vsd, &vsn, &vsm, fpscr); return vfp_single_normaliseround(sd, &vsd, fpscr, exceptions, "fadd");}/* * sd = sn - sm */static u32 vfp_single_fsub(int sd, int sn, s32 m, u32 fpscr){ /* * Subtraction is addition with one sign inverted. */ return vfp_single_fadd(sd, sn, vfp_single_packed_negate(m), fpscr);}/* * sd = sn / sm */static u32 vfp_single_fdiv(int sd, int sn, s32 m, u32 fpscr){ struct vfp_single vsd, vsn, vsm; u32 exceptions = 0; s32 n = vfp_get_float(sn); int tm, tn; pr_debug("VFP: s%u = %08x\n", sn, n); vfp_single_unpack(&vsn, n); vfp_single_unpack(&vsm, m); vsd.sign = vsn.sign ^ vsm.sign; tn = vfp_single_type(&vsn); tm = vfp_single_type(&vsm); /* * Is n a NAN? */ if (tn & VFP_NAN) goto vsn_nan; /* * Is m a NAN? */ if (tm & VFP_NAN) goto vsm_nan; /* * If n and m are infinity, the result is invalid * If n and m are zero, the result is invalid */ if (tm & tn & (VFP_INFINITY|VFP_ZERO)) goto invalid; /* * If n is infinity, the result is infinity */ if (tn & VFP_INFINITY) goto infinity; /* * If m is zero, raise div0 exception */ if (tm & VFP_ZERO) goto divzero; /* * If m is infinity, or n is zero, the result is zero */ if (tm & VFP_INFINITY || tn & VFP_ZERO) goto zero; if (tn & VFP_DENORMAL) vfp_single_normalise_denormal(&vsn); if (tm & VFP_DENORMAL) vfp_single_normalise_denormal(&vsm); /* * Ok, we have two numbers, we can perform division. */ vsd.exponent = vsn.exponent - vsm.exponent + 127 - 1; vsm.significand <<= 1; if (vsm.significand <= (2 * vsn.significand)) { vsn.significand >>= 1; vsd.exponent++; } { u64 significand = (u64)vsn.significand << 32; do_div(significand, vsm.significand); vsd.significand = significand; } if ((vsd.significand & 0x3f) == 0) vsd.significand |= ((u64)vsm.significand * vsd.significand != (u64)vsn.significand << 32); return vfp_single_normaliseround(sd, &vsd, fpscr, 0, "fdiv"); vsn_nan: exceptions = vfp_propagate_nan(&vsd, &vsn, &vsm, fpscr); pack: vfp_put_float(vfp_single_pack(&vsd), sd); return exceptions; vsm_nan: exceptions = vfp_propagate_nan(&vsd, &vsm, &vsn, fpscr); goto pack; zero: vsd.exponent = 0; vsd.significand = 0; goto pack; divzero: exceptions = FPSCR_DZC; infinity: vsd.exponent = 255; vsd.significand = 0; goto pack; invalid: vfp_put_float(vfp_single_pack(&vfp_single_default_qnan), sd); return FPSCR_IOC;}static struct op fops[16] = { [FOP_TO_IDX(FOP_FMAC)] = { vfp_single_fmac, 0 }, [FOP_TO_IDX(FOP_FNMAC)] = { vfp_single_fnmac, 0 }, [FOP_TO_IDX(FOP_FMSC)] = { vfp_single_fmsc, 0 }, [FOP_TO_IDX(FOP_FNMSC)] = { vfp_single_fnmsc, 0 }, [FOP_TO_IDX(FOP_FMUL)] = { vfp_single_fmul, 0 }, [FOP_TO_IDX(FOP_FNMUL)] = { vfp_single_fnmul, 0 }, [FOP_TO_IDX(FOP_FADD)] = { vfp_single_fadd, 0 }, [FOP_TO_IDX(FOP_FSUB)] = { vfp_single_fsub, 0 }, [FOP_TO_IDX(FOP_FDIV)] = { vfp_single_fdiv, 0 },};#define FREG_BANK(x) ((x) & 0x18)#define FREG_IDX(x) ((x) & 7)u32 vfp_single_cpdo(u32 inst, u32 fpscr){ u32 op = inst & FOP_MASK; u32 exceptions = 0; unsigned int dest; unsigned int sn = vfp_get_sn(inst); unsigned int sm = vfp_get_sm(inst); unsigned int vecitr, veclen, vecstride; struct op *fop; vecstride = 1 + ((fpscr & FPSCR_STRIDE_MASK) == FPSCR_STRIDE_MASK); fop = (op == FOP_EXT) ? &fops_ext[FEXT_TO_IDX(inst)] : &fops[FOP_TO_IDX(op)]; /* * fcvtsd takes a dN register number as destination, not sN. * Technically, if bit 0 of dd is set, this is an invalid * instruction. However, we ignore this for efficiency. * It also only operates on scalars. */ if (fop->flags & OP_DD) dest = vfp_get_dd(inst); else dest = vfp_get_sd(inst); /* * If destination bank is zero, vector length is always '1'. * ARM DDI0100F C5.1.3, C5.3.2. */ if ((fop->flags & OP_SCALAR) || FREG_BANK(dest) == 0) veclen = 0; else veclen = fpscr & FPSCR_LENGTH_MASK; pr_debug("VFP: vecstride=%u veclen=%u\n", vecstride, (veclen >> FPSCR_LENGTH_BIT) + 1); if (!fop->fn) goto invalid; for (vecitr = 0; vecitr <= veclen; vecitr += 1 << FPSCR_LENGTH_BIT) { s32 m = vfp_get_float(sm); u32 except; char type; type = fop->flags & OP_DD ? 'd' : 's'; if (op == FOP_EXT) pr_debug("VFP: itr%d (%c%u) = op[%u] (s%u=%08x)\n", vecitr >> FPSCR_LENGTH_BIT, type, dest, sn, sm, m); else pr_debug("VFP: itr%d (%c%u) = (s%u) op[%u] (s%u=%08x)\n", vecitr >> FPSCR_LENGTH_BIT, type, dest, sn, FOP_TO_IDX(op), sm, m); except = fop->fn(dest, sn, m, fpscr); pr_debug("VFP: itr%d: exceptions=%08x\n", vecitr >> FPSCR_LENGTH_BIT, except); exceptions |= except; /* * CHECK: It appears to be undefined whether we stop when * we encounter an exception. We continue. */ dest = FREG_BANK(dest) + ((FREG_IDX(dest) + vecstride) & 7); sn = FREG_BANK(sn) + ((FREG_IDX(sn) + vecstride) & 7); if (FREG_BANK(sm) != 0) sm = FREG_BANK(sm) + ((FREG_IDX(sm) + vecstride) & 7); } return exceptions; invalid: return (u32)-1;}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -