tomsfastmath/fp_mul.c

80 lines
1.8 KiB
C
Raw Normal View History

2004-08-25 04:43:43 +02:00
/* TomsFastMath, a fast ISO C bignum library.
*
* This project is meant to fill in where LibTomMath
* falls short. That is speed ;-)
*
* This project is public domain and free for all purposes.
*
2005-07-23 12:43:03 +02:00
* Tom St Denis, tomstdenis@gmail.com
2004-08-25 04:43:43 +02:00
*/
#include <tfm.h>
/* c = a * b */
void fp_mul(fp_int *A, fp_int *B, fp_int *C)
{
2006-04-06 21:49:03 +02:00
int y, yy;
2004-08-25 04:43:43 +02:00
2005-10-31 16:32:05 +01:00
/* call generic if we're out of range */
if (A->used + B->used > FP_SIZE) {
fp_mul_comba(A, B, C);
return ;
}
2004-08-25 04:43:43 +02:00
y = MAX(A->used, B->used);
yy = MIN(A->used, B->used);
/* pick a comba (unrolled 4/8/16/32 x or rolled) based on the size
of the largest input. We also want to avoid doing excess mults if the
inputs are not close to the next power of two. That is, for example,
if say y=17 then we would do (32-17)^2 = 225 unneeded multiplications
*/
2005-07-23 12:43:03 +02:00
#ifdef TFM_SMALL_SET
if (y <= 16) {
fp_mul_comba_small(A,B,C);
2005-11-18 06:16:25 +01:00
return;
}
#endif
2006-04-06 21:49:03 +02:00
#if defined(TFM_MUL20)
if (y <= 20) {
fp_mul_comba20(A,B,C);
return;
}
#endif
#if defined(TFM_MUL24)
if (yy >= 16 && y <= 24) {
fp_mul_comba24(A,B,C);
return;
}
#endif
#if defined(TFM_MUL28)
if (yy >= 20 && y <= 28) {
fp_mul_comba28(A,B,C);
return;
}
#endif
2005-10-31 16:32:05 +01:00
#if defined(TFM_MUL32)
2005-11-18 06:16:25 +01:00
if (yy >= 24 && y <= 32) {
2004-08-25 04:43:43 +02:00
fp_mul_comba32(A,B,C);
2005-11-18 06:16:25 +01:00
return;
}
2005-10-31 16:32:05 +01:00
#endif
#if defined(TFM_MUL48)
2005-11-18 06:16:25 +01:00
if (yy >= 40 && y <= 48) {
2005-07-23 12:43:03 +02:00
fp_mul_comba48(A,B,C);
2005-11-18 06:16:25 +01:00
return;
}
#endif
2005-10-31 16:32:05 +01:00
#if defined(TFM_MUL64)
2005-11-18 06:16:25 +01:00
if (yy >= 56 && y <= 64) {
2005-07-23 12:43:03 +02:00
fp_mul_comba64(A,B,C);
2005-11-18 06:16:25 +01:00
return;
2004-08-25 04:43:43 +02:00
}
2005-11-18 06:16:25 +01:00
#endif
fp_mul_comba(A,B,C);
2004-08-25 04:43:43 +02:00
}
2005-07-23 12:43:03 +02:00
/* $Source$ */
/* $Revision$ */
/* $Date$ */