summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorjan.nijtmans <nijtmans@users.sourceforge.net>2017-09-20 09:21:54 (GMT)
committerjan.nijtmans <nijtmans@users.sourceforge.net>2017-09-20 09:21:54 (GMT)
commita3c9570c9f3b6544c471985ca89b8712daafa603 (patch)
treeb9cbb08ca3ac72de4a6292147b199b36beb9f4a1
parent88b282766104841d47f9878536605bc08ae4c599 (diff)
downloadtcl-a3c9570c9f3b6544c471985ca89b8712daafa603.zip
tcl-a3c9570c9f3b6544c471985ca89b8712daafa603.tar.gz
tcl-a3c9570c9f3b6544c471985ca89b8712daafa603.tar.bz2
Move fast return from s_is_power_of_two() to mp_div_d(). This saves a function call in the common case where b is not a power of 2.
-rw-r--r--libtommath/bn_mp_div_d.c11
1 files changed, 3 insertions, 8 deletions
diff --git a/libtommath/bn_mp_div_d.c b/libtommath/bn_mp_div_d.c
index 0e9dc95..d0131c3 100644
--- a/libtommath/bn_mp_div_d.c
+++ b/libtommath/bn_mp_div_d.c
@@ -19,13 +19,8 @@ static int s_is_power_of_two(mp_digit b, int *p)
{
int x;
- /* fast return if no power of two */
- if ((b & (b-1)) != 0) {
- return 0;
- }
-
- /* This loops gives the wrong result for b==1,
- * but this function is never called for those values. */
+ /* Gives the wrong result for b==1, but this function
+ * is never called for this value anyway. */
for (x = 1; x < DIGIT_BIT; x++) {
if (b == (((mp_digit)1)<<x)) {
*p = x;
@@ -60,7 +55,7 @@ int mp_div_d(const mp_int *a, mp_digit b, mp_int *c, mp_digit *d)
}
/* power of two ? */
- if (s_is_power_of_two(b, &ix) == 1) {
+ if (((b & (b-1)) == 0) && s_is_power_of_two(b, &ix)) {
if (d != NULL) {
*d = a->dp[0] & ((((mp_digit)1)<<ix) - 1);
}