void mpn_toom63_mul (mp_ptr pp, mp_srcptr ap, mp_size_t an, mp_srcptr bp, mp_size_t bn, mp_ptr scratch) { mp_size_t n, s, t; mp_limb_t cy; int sign; /***************************** decomposition *******************************/ #define a5 (ap + 5 * n) #define b0 (bp + 0 * n) #define b1 (bp + 1 * n) #define b2 (bp + 2 * n) ASSERT (an >= bn); n = 1 + (an >= 2 * bn ? (an - 1) / (size_t) 6 : (bn - 1) / (size_t) 3); s = an - 5 * n; t = bn - 2 * n; ASSERT (0 < s && s <= n); ASSERT (0 < t && t <= n); /* WARNING! it assumes s+t>=n */ ASSERT ( s + t >= n ); ASSERT ( s + t > 4); /* WARNING! it assumes n>1 */ ASSERT ( n > 2); #define r8 pp /* 2n */ #define r7 scratch /* 3n+1 */ #define r5 (pp + 3*n) /* 3n+1 */ #define v0 (pp + 3*n) /* n+1 */ #define v1 (pp + 4*n+1) /* n+1 */ #define v2 (pp + 5*n+2) /* n+1 */ #define v3 (pp + 6*n+3) /* n+1 */ #define r3 (scratch + 3 * n + 1) /* 3n+1 */ #define r1 (pp + 7*n) /* s+t <= 2*n */ #define ws (scratch + 6 * n + 2) /* ??? */ /* Alloc also 3n+1 limbs for ws... mpn_toom_interpolate_8pts may need all of them, when DO_mpn_sublsh_n usea a scratch */ /* if (scratch == NULL) scratch = TMP_SALLOC_LIMBS (9 * n + 3); */ /********************** evaluation and recursive calls *********************/ /* $\pm4$ */ sign = mpn_toom_eval_pm2exp (v2, v0, 5, ap, n, s, 2, pp); pp[n] = mpn_lshift (pp, b1, n, 2); /* 4b1 */ /* FIXME: use addlsh */ v3[t] = mpn_lshift (v3, b2, t, 4);/* 16b2 */ if ( n == t ) v3[n]+= mpn_add_n (v3, v3, b0, n); /* 16b2+b0 */ else v3[n] = mpn_add (v3, b0, n, v3, t+1); /* 16b2+b0 */ sign ^= abs_sub_add_n (v1, v3, pp, n + 1); TOOM_63_MUL_N_REC(pp, v0, v1, n + 1, ws); /* A(-4)*B(-4) */ TOOM_63_MUL_N_REC(r3, v2, v3, n + 1, ws); /* A(+4)*B(+4) */ mpn_toom_couple_handling (r3, 2*n+1, pp, sign, n, 2, 4); /* $\pm1$ */ sign = mpn_toom_eval_pm1 (v2, v0, 5, ap, n, s, pp); /* Compute bs1 and bsm1. Code taken from toom33 */ cy = mpn_add (ws, b0, n, b2, t); #if HAVE_NATIVE_mpn_add_n_sub_n if (cy == 0 && mpn_cmp (ws, b1, n) < 0) { cy = mpn_add_n_sub_n (v3, v1, b1, ws, n); v3[n] = cy >> 1; v1[n] = 0; sign = ~sign; }
void mpn_toom6h_mul (mp_ptr pp, mp_srcptr ap, mp_size_t an, mp_srcptr bp, mp_size_t bn, mp_ptr scratch) { mp_size_t n, s, t; int p, q, half; int sign; /***************************** decomposition *******************************/ ASSERT (an >= bn); /* Can not handle too much unbalancement */ ASSERT (bn >= 42); /* Can not handle too much unbalancement */ ASSERT ((an*3 < bn * 8) || (bn >= 46 && an * 6 < bn * 17)); /* Limit num/den is a rational number between (12/11)^(log(4)/log(2*4-1)) and (12/11)^(log(6)/log(2*6-1)) */ #define LIMIT_numerator (18) #define LIMIT_denominat (17) if (LIKELY (an * LIMIT_denominat < LIMIT_numerator * bn)) /* is 6*... < 6*... */ { n = 1 + (an - 1) / (size_t) 6; p = q = 5; half = 0; s = an - 5 * n; t = bn - 5 * n; } else { if (an * 5 * LIMIT_numerator < LIMIT_denominat * 7 * bn) { p = 7; q = 6; } else if (an * 5 * LIMIT_denominat < LIMIT_numerator * 7 * bn) { p = 7; q = 5; } else if (an * LIMIT_numerator < LIMIT_denominat * 2 * bn) /* is 4*... < 8*... */ { p = 8; q = 5; } else if (an * LIMIT_denominat < LIMIT_numerator * 2 * bn) /* is 4*... < 8*... */ { p = 8; q = 4; } else { p = 9; q = 4; } half = (p ^ q) & 1; n = 1 + (q * an >= p * bn ? (an - 1) / (size_t) p : (bn - 1) / (size_t) q); p--; q--; s = an - p * n; t = bn - q * n; /* With LIMIT = 16/15, the following recover is needed only if bn<=73*/ if (half) { /* Recover from badly chosen splitting */ if (UNLIKELY (s<1)) {p--; s+=n; half=0;} else if (UNLIKELY (t<1)) {q--; t+=n; half=0;} } } #undef LIMIT_numerator #undef LIMIT_denominat ASSERT (0 < s && s <= n); ASSERT (0 < t && t <= n); ASSERT (half || s + t > 3); ASSERT (n > 2); #define r4 (pp + 3 * n) /* 3n+1 */ #define r2 (pp + 7 * n) /* 3n+1 */ #define r0 (pp +11 * n) /* s+t <= 2*n */ #define r5 (scratch) /* 3n+1 */ #define r3 (scratch + 3 * n + 1) /* 3n+1 */ #define r1 (scratch + 6 * n + 2) /* 3n+1 */ #define v0 (pp + 7 * n) /* n+1 */ #define v1 (pp + 8 * n+1) /* n+1 */ #define v2 (pp + 9 * n+2) /* n+1 */ #define v3 (scratch + 9 * n + 3) /* n+1 */ #define wsi (scratch + 9 * n + 3) /* 3n+1 */ #define wse (scratch +10 * n + 4) /* 2n+1 */ /* Alloc also 3n+1 limbs for wsi... toom_interpolate_12pts may need all of them */ /* if (scratch == NULL) */ /* scratch = TMP_SALLOC_LIMBS(mpn_toom6_sqr_itch(n * 6)); */ ASSERT (12 * n + 6 <= mpn_toom6h_mul_itch(an,bn)); ASSERT (12 * n + 6 <= mpn_toom6_sqr_itch(n * 6)); /********************** evaluation and recursive calls *********************/ /* $\pm1/2$ */ sign = mpn_toom_eval_pm2rexp (v2, v0, p, ap, n, s, 1, pp) ^ mpn_toom_eval_pm2rexp (v3, v1, q, bp, n, t, 1, pp); /* A(-1/2)*B(-1/2)*2^. */ /* A(+1/2)*B(+1/2)*2^. */ TOOM6H_MUL_N_REC(pp, v0, v1, 2, r5, v2, v3, n + 1, wse); mpn_toom_couple_handling (r5, 2 * n + 1, pp, sign, n, 1+half , half); /* $\pm1$ */ sign = mpn_toom_eval_pm1 (v2, v0, p, ap, n, s, pp); if (UNLIKELY (q == 3)) sign ^= mpn_toom_eval_dgr3_pm1 (v3, v1, bp, n, t, pp); else sign ^= mpn_toom_eval_pm1 (v3, v1, q, bp, n, t, pp); /* A(-1)*B(-1) */ /* A(1)*B(1) */ TOOM6H_MUL_N_REC(pp, v0, v1, 2, r3, v2, v3, n + 1, wse); mpn_toom_couple_handling (r3, 2 * n + 1, pp, sign, n, 0, 0); /* $\pm4$ */ sign = mpn_toom_eval_pm2exp (v2, v0, p, ap, n, s, 2, pp) ^ mpn_toom_eval_pm2exp (v3, v1, q, bp, n, t, 2, pp); /* A(-4)*B(-4) */ TOOM6H_MUL_N_REC(pp, v0, v1, 2, r1, v2, v3, n + 1, wse); /* A(+4)*B(+4) */ mpn_toom_couple_handling (r1, 2 * n + 1, pp, sign, n, 2, 4); /* $\pm1/4$ */ sign = mpn_toom_eval_pm2rexp (v2, v0, p, ap, n, s, 2, pp) ^ mpn_toom_eval_pm2rexp (v3, v1, q, bp, n, t, 2, pp); /* A(-1/4)*B(-1/4)*4^. */ /* A(+1/4)*B(+1/4)*4^. */ TOOM6H_MUL_N_REC(pp, v0, v1, 2, r4, v2, v3, n + 1, wse); mpn_toom_couple_handling (r4, 2 * n + 1, pp, sign, n, 2*(1+half), 2*(half)); /* $\pm2$ */ sign = mpn_toom_eval_pm2 (v2, v0, p, ap, n, s, pp) ^ mpn_toom_eval_pm2 (v3, v1, q, bp, n, t, pp); /* A(-2)*B(-2) */ /* A(+2)*B(+2) */ TOOM6H_MUL_N_REC(pp, v0, v1, 2, r2, v2, v3, n + 1, wse); mpn_toom_couple_handling (r2, 2 * n + 1, pp, sign, n, 1, 2); #undef v0 #undef v1 #undef v2 #undef v3 #undef wse /* A(0)*B(0) */ TOOM6H_MUL_N_REC(pp, ap, bp, 0, pp, ap, bp, n, wsi); /* Infinity */ if (UNLIKELY (half != 0)) { if (s > t) { TOOM6H_MUL_REC(r0, ap + p * n, s, bp + q * n, t, wsi); } else { TOOM6H_MUL_REC(r0, bp + q * n, t, ap + p * n, s, wsi); }; }; mpn_toom_interpolate_12pts (pp, r1, r3, r5, n, s+t, half, wsi); #undef r0 #undef r1 #undef r2 #undef r3 #undef r4 #undef r5 #undef wsi }
void mpn_toom6_sqr (mp_ptr pp, mp_srcptr ap, mp_size_t an, mp_ptr scratch) { mp_size_t n, s; /***************************** decomposition *******************************/ ASSERT( an >= 18 ); n = 1 + (an - 1) / (size_t) 6; s = an - 5 * n; ASSERT (0 < s && s <= n); #define r4 (pp + 3 * n) /* 3n+1 */ #define r2 (pp + 7 * n) /* 3n+1 */ #define r0 (pp +11 * n) /* s+t <= 2*n */ #define r5 (scratch) /* 3n+1 */ #define r3 (scratch + 3 * n + 1) /* 3n+1 */ #define r1 (scratch + 6 * n + 2) /* 3n+1 */ #define v0 (pp + 7 * n) /* n+1 */ #define v2 (pp + 9 * n+2) /* n+1 */ #define wse (scratch + 9 * n + 3) /* 3n+1 */ /* Alloc also 3n+1 limbs for ws... toom_interpolate_12pts may need all of them, when DO_mpn_sublsh_n usea a scratch */ /* if (scratch== NULL) */ /* scratch = TMP_SALLOC_LIMBS (12 * n + 6); */ /********************** evaluation and recursive calls *********************/ /* $\pm1/2$ */ mpn_toom_eval_pm2rexp (v2, v0, 5, ap, n, s, 1, pp); TOOM6_SQR_REC(pp, v0, n + 1, wse); /* A(-1/2)*B(-1/2)*2^. */ TOOM6_SQR_REC(r5, v2, n + 1, wse); /* A(+1/2)*B(+1/2)*2^. */ mpn_toom_couple_handling (r5, 2 * n + 1, pp, 0, n, 1, 0); /* $\pm1$ */ mpn_toom_eval_pm1 (v2, v0, 5, ap, n, s, pp); TOOM6_SQR_REC(pp, v0, n + 1, wse); /* A(-1)*B(-1) */ TOOM6_SQR_REC(r3, v2, n + 1, wse); /* A(1)*B(1) */ mpn_toom_couple_handling (r3, 2 * n + 1, pp, 0, n, 0, 0); /* $\pm4$ */ mpn_toom_eval_pm2exp (v2, v0, 5, ap, n, s, 2, pp); TOOM6_SQR_REC(pp, v0, n + 1, wse); /* A(-4)*B(-4) */ TOOM6_SQR_REC(r1, v2, n + 1, wse); /* A(+4)*B(+4) */ mpn_toom_couple_handling (r1, 2 * n + 1, pp, 0, n, 2, 4); /* $\pm1/4$ */ mpn_toom_eval_pm2rexp (v2, v0, 5, ap, n, s, 2, pp); TOOM6_SQR_REC(pp, v0, n + 1, wse); /* A(-1/4)*B(-1/4)*4^. */ TOOM6_SQR_REC(r4, v2, n + 1, wse); /* A(+1/4)*B(+1/4)*4^. */ mpn_toom_couple_handling (r4, 2 * n + 1, pp, 0, n, 2, 0); /* $\pm2$ */ mpn_toom_eval_pm2 (v2, v0, 5, ap, n, s, pp); TOOM6_SQR_REC(pp, v0, n + 1, wse); /* A(-2)*B(-2) */ TOOM6_SQR_REC(r2, v2, n + 1, wse); /* A(+2)*B(+2) */ mpn_toom_couple_handling (r2, 2 * n + 1, pp, 0, n, 1, 2); #undef v0 #undef v2 /* A(0)*B(0) */ TOOM6_SQR_REC(pp, ap, n, wse); mpn_toom_interpolate_12pts (pp, r1, r3, r5, n, 2 * s, 0, wse); #undef r0 #undef r1 #undef r2 #undef r3 #undef r4 #undef r5 }
void mpn_toom53_mul (mp_ptr pp, mp_srcptr ap, mp_size_t an, mp_srcptr bp, mp_size_t bn, mp_ptr scratch) { mp_size_t n, s, t; mp_limb_t cy; mp_ptr gp; mp_ptr as1, asm1, as2, asm2, ash; mp_ptr bs1, bsm1, bs2, bsm2, bsh; mp_ptr tmp; enum toom7_flags flags; TMP_DECL; #define a0 ap #define a1 (ap + n) #define a2 (ap + 2*n) #define a3 (ap + 3*n) #define a4 (ap + 4*n) #define b0 bp #define b1 (bp + n) #define b2 (bp + 2*n) n = 1 + (3 * an >= 5 * bn ? (an - 1) / (size_t) 5 : (bn - 1) / (size_t) 3); s = an - 4 * n; t = bn - 2 * n; ASSERT (0 < s && s <= n); ASSERT (0 < t && t <= n); TMP_MARK; tmp = TMP_ALLOC_LIMBS (10 * (n + 1)); as1 = tmp; tmp += n + 1; asm1 = tmp; tmp += n + 1; as2 = tmp; tmp += n + 1; asm2 = tmp; tmp += n + 1; ash = tmp; tmp += n + 1; bs1 = tmp; tmp += n + 1; bsm1 = tmp; tmp += n + 1; bs2 = tmp; tmp += n + 1; bsm2 = tmp; tmp += n + 1; bsh = tmp; tmp += n + 1; gp = pp; /* Compute as1 and asm1. */ flags = (enum toom7_flags) (toom7_w3_neg & mpn_toom_eval_pm1 (as1, asm1, 4, ap, n, s, gp)); /* Compute as2 and asm2. */ flags = (enum toom7_flags) (flags | toom7_w1_neg & mpn_toom_eval_pm2 (as2, asm2, 4, ap, n, s, gp)); /* Compute ash = 16 a0 + 8 a1 + 4 a2 + 2 a3 + a4 = 2*(2*(2*(2*a0 + a1) + a2) + a3) + a4 */ #if HAVE_NATIVE_mpn_addlsh1_n cy = mpn_addlsh1_n (ash, a1, a0, n); cy = 2*cy + mpn_addlsh1_n (ash, a2, ash, n); cy = 2*cy + mpn_addlsh1_n (ash, a3, ash, n); if (s < n) { mp_limb_t cy2; cy2 = mpn_addlsh1_n (ash, a4, ash, s); ash[n] = 2*cy + mpn_lshift (ash + s, ash + s, n - s, 1); MPN_INCR_U (ash + s, n+1-s, cy2); } else ash[n] = 2*cy + mpn_addlsh1_n (ash, a4, ash, n); #else cy = mpn_lshift (ash, a0, n, 1); cy += mpn_add_n (ash, ash, a1, n); cy = 2*cy + mpn_lshift (ash, ash, n, 1); cy += mpn_add_n (ash, ash, a2, n); cy = 2*cy + mpn_lshift (ash, ash, n, 1); cy += mpn_add_n (ash, ash, a3, n); cy = 2*cy + mpn_lshift (ash, ash, n, 1); ash[n] = cy + mpn_add (ash, ash, n, a4, s); #endif /* Compute bs1 and bsm1. */ bs1[n] = mpn_add (bs1, b0, n, b2, t); /* b0 + b2 */ #if HAVE_NATIVE_mpn_add_n_sub_n if (bs1[n] == 0 && mpn_cmp (bs1, b1, n) < 0) { bs1[n] = mpn_add_n_sub_n (bs1, bsm1, b1, bs1, n) >> 1; bsm1[n] = 0; flags = (enum toom7_flags) (flags ^ toom7_w3_neg); }