tomsfastmath/src/mul/fp_mul.c

140 lines
3.2 KiB
C
Raw Normal View History

2004-08-25 04:43:43 +02:00
/* TomsFastMath, a fast ISO C bignum library.
2014-06-12 17:35:54 +02:00
*
2004-08-25 04:43:43 +02:00
* This project is meant to fill in where LibTomMath
* falls short. That is speed ;-)
*
* This project is public domain and free for all purposes.
2014-06-12 17:35:54 +02:00
*
2005-07-23 12:43:03 +02:00
* Tom St Denis, tomstdenis@gmail.com
2004-08-25 04:43:43 +02:00
*/
#include <tfm_private.h>
2004-08-25 04:43:43 +02:00
/* c = a * b */
void fp_mul(fp_int *A, fp_int *B, fp_int *C)
{
int y, old_used;
#if FP_SIZE >= 48
int yy;
#endif
old_used = C->used;
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);
goto clean;
2005-10-31 16:32:05 +01:00
}
2004-08-25 04:43:43 +02:00
y = MAX(A->used, B->used);
#if FP_SIZE >= 48
2004-08-25 04:43:43 +02:00
yy = MIN(A->used, B->used);
#endif
2004-08-25 04:43:43 +02:00
/* pick a comba (unrolled 4/8/16/32 x or rolled) based on the size
2014-06-12 17:35:54 +02:00
of the largest input. We also want to avoid doing excess mults if the
2004-08-25 04:43:43 +02:00
inputs are not close to the next power of two. That is, for example,
2014-06-12 17:35:54 +02:00
if say y=17 then we would do (32-17)^2 = 225 unneeded multiplications
2004-08-25 04:43:43 +02:00
*/
2005-07-23 12:43:03 +02:00
#if defined(TFM_MUL3) && FP_SIZE >= 6
2006-11-01 08:42:37 +01:00
if (y <= 3) {
fp_mul_comba3(A,B,C);
goto clean;
2006-11-01 08:42:37 +01:00
}
#endif
#if defined(TFM_MUL4) && FP_SIZE >= 8
2006-11-01 08:42:37 +01:00
if (y == 4) {
fp_mul_comba4(A,B,C);
goto clean;
2006-11-01 08:42:37 +01:00
}
#endif
#if defined(TFM_MUL6) && FP_SIZE >= 12
2006-11-01 08:42:37 +01:00
if (y <= 6) {
fp_mul_comba6(A,B,C);
goto clean;
2006-11-01 08:42:37 +01:00
}
#endif
#if defined(TFM_MUL7) && FP_SIZE >= 14
2006-11-01 08:42:37 +01:00
if (y == 7) {
fp_mul_comba7(A,B,C);
goto clean;
2006-11-01 08:42:37 +01:00
}
#endif
#if defined(TFM_MUL8) && FP_SIZE >= 16
2006-11-01 08:42:37 +01:00
if (y == 8) {
fp_mul_comba8(A,B,C);
goto clean;
2006-11-01 08:42:37 +01:00
}
#endif
#if defined(TFM_MUL9) && FP_SIZE >= 18
2006-11-01 08:42:37 +01:00
if (y == 9) {
fp_mul_comba9(A,B,C);
goto clean;
2006-11-01 08:42:37 +01:00
}
#endif
#if defined(TFM_MUL12) && FP_SIZE >= 24
2006-11-01 08:42:37 +01:00
if (y <= 12) {
fp_mul_comba12(A,B,C);
goto clean;
2006-11-01 08:42:37 +01:00
}
#endif
#if defined(TFM_MUL17) && FP_SIZE >= 34
2006-11-01 08:42:37 +01:00
if (y <= 17) {
fp_mul_comba17(A,B,C);
goto clean;
2006-11-01 08:42:37 +01:00
}
#endif
#if defined(TFM_SMALL_SET) && FP_SIZE >= 32
2005-07-23 12:43:03 +02:00
if (y <= 16) {
fp_mul_comba_small(A,B,C);
goto clean;
2005-11-18 06:16:25 +01:00
}
2014-06-12 17:35:54 +02:00
#endif
2011-09-20 12:00:07 +02:00
#if defined(TFM_MUL20) && FP_SIZE >= 40
2006-04-06 21:49:03 +02:00
if (y <= 20) {
fp_mul_comba20(A,B,C);
goto clean;
2006-04-06 21:49:03 +02:00
}
#endif
2011-09-20 12:00:07 +02:00
#if defined(TFM_MUL24) && FP_SIZE >= 48
2006-04-06 21:49:03 +02:00
if (yy >= 16 && y <= 24) {
fp_mul_comba24(A,B,C);
goto clean;
2006-04-06 21:49:03 +02:00
}
#endif
2011-09-20 12:00:07 +02:00
#if defined(TFM_MUL28) && FP_SIZE >= 56
2006-04-06 21:49:03 +02:00
if (yy >= 20 && y <= 28) {
fp_mul_comba28(A,B,C);
goto clean;
2006-04-06 21:49:03 +02:00
}
#endif
2011-09-20 12:00:07 +02:00
#if defined(TFM_MUL32) && FP_SIZE >= 64
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);
goto clean;
2005-11-18 06:16:25 +01:00
}
2005-10-31 16:32:05 +01:00
#endif
2011-09-20 12:00:07 +02:00
#if defined(TFM_MUL48) && FP_SIZE >= 96
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);
goto clean;
2005-11-18 06:16:25 +01:00
}
2014-06-12 17:35:54 +02:00
#endif
2011-09-20 12:00:07 +02:00
#if defined(TFM_MUL64) && FP_SIZE >= 128
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);
goto clean;
2004-08-25 04:43:43 +02:00
}
2005-11-18 06:16:25 +01:00
#endif
fp_mul_comba(A,B,C);
clean:
for (y = C->used; y < old_used; y++) {
C->dp[y] = 0;
}
2004-08-25 04:43:43 +02:00
}
2005-07-23 12:43:03 +02:00
2011-09-20 12:00:07 +02:00
/* $Source: /cvs/libtom/tomsfastmath/src/mul/fp_mul.c,v $ */
/* $Revision: 1.1 $ */
/* $Date: 2006/12/31 21:25:53 $ */