diff options
author | jan.nijtmans <nijtmans@users.sourceforge.net> | 2019-12-13 21:16:44 (GMT) |
---|---|---|
committer | jan.nijtmans <nijtmans@users.sourceforge.net> | 2019-12-13 21:16:44 (GMT) |
commit | 9eeefad066a16a6e9a3407178f7629a8cd4c0e61 (patch) | |
tree | 5c9ceeaebb47824cebaf15a43b4b469046ebd6d7 | |
parent | 4cef9fbbe29e5ba76aaa7eaa7e91dd9b7d22e471 (diff) | |
download | tcl-9eeefad066a16a6e9a3407178f7629a8cd4c0e61.zip tcl-9eeefad066a16a6e9a3407178f7629a8cd4c0e61.tar.gz tcl-9eeefad066a16a6e9a3407178f7629a8cd4c0e61.tar.bz2 |
Optimize mp_mul for the case a==b
-rw-r--r-- | generic/tclTomMathDecls.h | 3 | ||||
-rw-r--r-- | libtommath/bn_mp_mul.c | 6 | ||||
-rw-r--r-- | libtommath/tommath_private.h | 3 |
3 files changed, 10 insertions, 2 deletions
diff --git a/generic/tclTomMathDecls.h b/generic/tclTomMathDecls.h index f199a2a..52ac5da 100644 --- a/generic/tclTomMathDecls.h +++ b/generic/tclTomMathDecls.h @@ -740,6 +740,9 @@ extern const TclTomMathStubs *tclTomMathStubsPtr; #undef mp_isodd #define mp_iseven(a) (!mp_isodd(a)) #define mp_isodd(a) (((a)->used != 0 && (((a)->dp[0] & 1) != 0)) ? MP_YES : MP_NO) +#undef mp_sqr +#define mp_sqr(a,b) mp_mul(a,a,b) + #undef TCL_STORAGE_CLASS #define TCL_STORAGE_CLASS DLLIMPORT diff --git a/libtommath/bn_mp_mul.c b/libtommath/bn_mp_mul.c index 91707cd..b00334d 100644 --- a/libtommath/bn_mp_mul.c +++ b/libtommath/bn_mp_mul.c @@ -12,12 +12,14 @@ mp_err mp_mul(const mp_int *a, const mp_int *b, mp_int *c) digs = a->used + b->used + 1; mp_sign neg = (a->sign == b->sign) ? MP_ZPOS : MP_NEG; - if (MP_HAS(S_MP_BALANCE_MUL) && + if (a == b) { + return mp_sqr(a,c); + } else if (MP_HAS(S_MP_BALANCE_MUL) && /* Check sizes. The smaller one needs to be larger than the Karatsuba cut-off. * The bigger one needs to be at least about one MP_KARATSUBA_MUL_CUTOFF bigger * to make some sense, but it depends on architecture, OS, position of the * stars... so YMMV. - * Using it to cut the input into slices small enough for s_mp_mul_digs_fast + * Using it to cut the input into slices small enough for fast_s_mp_mul_digs * was actually slower on the author's machine, but YMMV. */ (min_len >= MP_KARATSUBA_MUL_CUTOFF) && diff --git a/libtommath/tommath_private.h b/libtommath/tommath_private.h index 7cef443..60c8838 100644 --- a/libtommath/tommath_private.h +++ b/libtommath/tommath_private.h @@ -249,6 +249,9 @@ MP_DEPRECATED(s_mp_toom_mul) mp_err mp_toom_mul(const mp_int *a, const mp_int *b MP_DEPRECATED(s_mp_toom_sqr) mp_err mp_toom_sqr(const mp_int *a, mp_int *b); MP_DEPRECATED(s_mp_reverse) void bn_reverse(unsigned char *s, int len); +#undef mp_sqr +#define mp_sqr TclBN_mp_sqr + #define MP_GET_ENDIANNESS(x) \ do{\ int16_t n = 0x1; \ |