Suspending karatsuba porting for now

Will return to this once bignum2 is working without optimizations
This commit is contained in:
Justin Ethier 2022-07-06 21:38:15 -04:00
parent 55af4bacec
commit faccbc6dfc

View file

@ -2659,56 +2659,58 @@ static void bignum_digits_multiply(object x, object y, object result)
} }
} }
/* Karatsuba multiplication: invoked when the two numbers are large // JAE TODO: at some point need to add this back for optimized multiplication:
* enough to make it worthwhile, and we still have enough stack left.
* Complexity is O(n^log2(3)), where n is max(len(x), len(y)). The
* description in [Knuth, 4.3.3] leaves a lot to be desired. [MCA,
* 1.3.2] and [MpNT, 3.2] are a bit easier to understand. We assume
* that length(x) <= length(y).
*/
static object
bignum_times_bignum_karatsuba(void *data, object x, object y, int negp)
{
//object kab[C_SIZEOF_FIX_BIGNUM*15+C_SIZEOF_BIGNUM(2)*3], *ka = kab,
object o[18],
xhi, xlo, xmid, yhi, ylo, ymid, a, b, c, n, bits;
int i = 0;
// /* Ran out of stack? Fall back to non-recursive multiplication */
// C_stack_check1(return C_SCHEME_FALSE);
// //
// /* Split |x| in half: <xhi,xlo> and |y|: <yhi,ylo> with len(ylo)=len(xlo) */ ///* Karatsuba multiplication: invoked when the two numbers are large
// x = o[i++] = C_s_a_u_i_integer_abs(1, x); // * enough to make it worthwhile, and we still have enough stack left.
// y = o[i++] = C_s_a_u_i_integer_abs(1, y); // * Complexity is O(n^log2(3)), where n is max(len(x), len(y)). The
// n = C_fix(C_bignum_size(y) >> 1); // * description in [Knuth, 4.3.3] leaves a lot to be desired. [MCA,
xhi = o[i++] = bignum_extract_digits(data, x, n, boolean_f); // * 1.3.2] and [MpNT, 3.2] are a bit easier to understand. We assume
xlo = o[i++] = bignum_extract_digits(data, x, obj_int2obj(0), n); // * that length(x) <= length(y).
yhi = o[i++] = bignum_extract_digits(data, y, n, boolean_f); // */
ylo = o[i++] = bignum_extract_digits(data, y, obj_int2obj(0), n); //static object
//bignum_times_bignum_karatsuba(void *data, object x, object y, int negp)
// /* a = xhi * yhi, b = xlo * ylo, c = (xhi - xlo) * (yhi - ylo) */ //{
// a = o[i++] = C_s_a_u_i_integer_times(&ka, 2, xhi, yhi); // //object kab[C_SIZEOF_FIX_BIGNUM*15+C_SIZEOF_BIGNUM(2)*3], *ka = kab,
// b = o[i++] = C_s_a_u_i_integer_times(&ka, 2, xlo, ylo); // object o[18],
// xmid = o[i++] = C_s_a_u_i_integer_minus(&ka, 2, xhi, xlo); // xhi, xlo, xmid, yhi, ylo, ymid, a, b, c, n, bits;
// ymid = o[i++] = C_s_a_u_i_integer_minus(&ka, 2, yhi, ylo); // int i = 0;
// c = o[i++] = C_s_a_u_i_integer_times(&ka, 2, xmid, ymid);
// //
// /* top(x) = a << (bits - 1) and bottom(y) = ((b + (a - c)) << bits) + b */ //// /* Ran out of stack? Fall back to non-recursive multiplication */
// bits = C_unfix(n) * C_BIGNUM_DIGIT_LENGTH; //// C_stack_check1(return C_SCHEME_FALSE);
// x = o[i++] = C_s_a_i_arithmetic_shift(&ka, 2, a, C_fix((C_uword)bits << 1)); ////
// c = o[i++] = C_s_a_u_i_integer_minus(&ka, 2, a, c); //// /* Split |x| in half: <xhi,xlo> and |y|: <yhi,ylo> with len(ylo)=len(xlo) */
// c = o[i++] = C_s_a_u_i_integer_plus(&ka, 2, b, c); //// x = o[i++] = C_s_a_u_i_integer_abs(1, x);
// c = o[i++] = C_s_a_i_arithmetic_shift(&ka, 2, c, C_fix(bits)); //// y = o[i++] = C_s_a_u_i_integer_abs(1, y);
// y = o[i++] = C_s_a_u_i_integer_plus(&ka, 2, c, b); //// n = C_fix(C_bignum_size(y) >> 1);
// /* Finally, return top + bottom, and correct for negative */ // xhi = o[i++] = bignum_extract_digits(data, x, n, boolean_f);
// n = o[i++] = C_s_a_u_i_integer_plus(&ka, 2, x, y); // xlo = o[i++] = bignum_extract_digits(data, x, obj_int2obj(0), n);
// if (C_truep(negp)) n = o[i++] = C_s_a_u_i_integer_negate(&ka, 1, n); // yhi = o[i++] = bignum_extract_digits(data, y, n, boolean_f);
// ylo = o[i++] = bignum_extract_digits(data, y, obj_int2obj(0), n);
// //
// JAE - no scratch space so believe not needed. Double-check first: //// /* a = xhi * yhi, b = xlo * ylo, c = (xhi - xlo) * (yhi - ylo) */
// n = move_buffer_object(ptr, kab, n); //// a = o[i++] = C_s_a_u_i_integer_times(&ka, 2, xhi, yhi);
// while(i--) clear_buffer_object(kab, o[i]); //// b = o[i++] = C_s_a_u_i_integer_times(&ka, 2, xlo, ylo);
return n; //// xmid = o[i++] = C_s_a_u_i_integer_minus(&ka, 2, xhi, xlo);
} //// ymid = o[i++] = C_s_a_u_i_integer_minus(&ka, 2, yhi, ylo);
//// c = o[i++] = C_s_a_u_i_integer_times(&ka, 2, xmid, ymid);
////
//// /* top(x) = a << (bits - 1) and bottom(y) = ((b + (a - c)) << bits) + b */
//// bits = C_unfix(n) * C_BIGNUM_DIGIT_LENGTH;
//// x = o[i++] = C_s_a_i_arithmetic_shift(&ka, 2, a, C_fix((C_uword)bits << 1));
//// c = o[i++] = C_s_a_u_i_integer_minus(&ka, 2, a, c);
//// c = o[i++] = C_s_a_u_i_integer_plus(&ka, 2, b, c);
//// c = o[i++] = C_s_a_i_arithmetic_shift(&ka, 2, c, C_fix(bits));
//// y = o[i++] = C_s_a_u_i_integer_plus(&ka, 2, c, b);
//// /* Finally, return top + bottom, and correct for negative */
//// n = o[i++] = C_s_a_u_i_integer_plus(&ka, 2, x, y);
//// if (C_truep(negp)) n = o[i++] = C_s_a_u_i_integer_negate(&ka, 1, n);
////
//// JAE - no scratch space so believe not needed. Double-check first:
//// n = move_buffer_object(ptr, kab, n);
//// while(i--) clear_buffer_object(kab, o[i]);
// return n;
//}
// TODO: static // TODO: static