From 7f4d9adf958f00805e60a353d2779434aca36fe2 Mon Sep 17 00:00:00 2001 From: Nikos Mavrogiannopoulos Date: Fri, 3 Sep 2010 08:33:33 +0200 Subject: Optimizations for 32bit machines by using a 64bit word type and 32bit digit. Unfortunately we cannot do the same for 64bit since we don't have an 128bit type in kernel. --- libtommath/bn_mp_div.c | 2 +- libtommath/bn_mp_div_3.c | 2 +- libtommath/bn_mp_div_d.c | 2 +- libtommath/tommath.h | 17 +++++++++++++---- 4 files changed, 16 insertions(+), 7 deletions(-) diff --git a/libtommath/bn_mp_div.c b/libtommath/bn_mp_div.c index aee9c94..723c8b5 100644 --- a/libtommath/bn_mp_div.c +++ b/libtommath/bn_mp_div.c @@ -195,7 +195,7 @@ int mp_div (mp_int * a, mp_int * b, mp_int * c, mp_int * d) mp_word tmp; tmp = ((mp_word) x.dp[i]) << ((mp_word) DIGIT_BIT); tmp |= ((mp_word) x.dp[i - 1]); - tmp /= ((mp_word) y.dp[t]); + tmp = word_div_int(tmp, (int)y.dp[t]); if (tmp > (mp_word) MP_MASK) tmp = MP_MASK; q.dp[i - t - 1] = (mp_digit) (tmp & (mp_word) (MP_MASK)); diff --git a/libtommath/bn_mp_div_3.c b/libtommath/bn_mp_div_3.c index 3c60269..608e759 100644 --- a/libtommath/bn_mp_div_3.c +++ b/libtommath/bn_mp_div_3.c @@ -25,7 +25,7 @@ mp_div_3 (mp_int * a, mp_int *c, mp_digit * d) int res, ix; /* b = 2**DIGIT_BIT / 3 */ - b = (((mp_word)1) << ((mp_word)DIGIT_BIT)) / ((mp_word)3); + b = word_div_int (((mp_word)1) << DIGIT_BIT, 3); if ((res = mp_init_size(&q, a->used)) != MP_OKAY) { return res; diff --git a/libtommath/bn_mp_div_d.c b/libtommath/bn_mp_div_d.c index 6a26d4f..63efc2c 100644 --- a/libtommath/bn_mp_div_d.c +++ b/libtommath/bn_mp_div_d.c @@ -87,7 +87,7 @@ int mp_div_d (mp_int * a, mp_digit b, mp_int * c, mp_digit * d) w = (w << ((mp_word)DIGIT_BIT)) | ((mp_word)a->dp[ix]); if (w >= b) { - t = (mp_digit)(w / b); + t = (mp_digit)(word_div_int(w, b)); w -= ((mp_word)t) * ((mp_word)b); } else { t = 0; diff --git a/libtommath/tommath.h b/libtommath/tommath.h index 6653f55..3fa7ae8 100644 --- a/libtommath/tommath.h +++ b/libtommath/tommath.h @@ -20,6 +20,7 @@ #include #include #include +#include #define CHAR_BIT sizeof(uint8_t)*8 @@ -65,19 +66,22 @@ extern "C" { * [any size beyond that is ok provided it doesn't overflow the data type] */ -/* FIXME: This can be improved, but might require to use a 64bit division - * on 32bit machines and an 128bit on 64. +/* FIXME: This can be improved, but requires to use 128bit division + * on 64bit machines, which is not available in kernel now. */ -#if BITS_PER_LONG <= 32 +#if BITS_PER_LONG < 32 typedef uint16_t mp_digit; typedef uint32_t mp_word; # define DIGIT_BIT 15 -#elif BITS_PER_LONG == 64 +#elif BITS_PER_LONG <= 64 typedef uint32_t mp_digit; typedef uint64_t mp_word; + +# define word_div_int(x,y) div_u64((x),(y)) + # define DIGIT_BIT 31 #endif @@ -92,6 +96,11 @@ extern "C" { #endif +#ifndef word_div_int +# define word_div_int(x,y) ((x)/(y)) +#endif + + /* define heap macros */ #ifndef XMALLOC # define XMALLOC(x) kmalloc(x, GFP_KERNEL) -- cgit