Commit 2e88b571c1911290bfb0c69826f780d8353bad3a

Daniel Mendler 2019-11-06T16:56:34

optimize mp_mul_d

diff --git a/mp_mul_d.c b/mp_mul_d.c
index 30d6c93..c10312a 100644
--- a/mp_mul_d.c
+++ b/mp_mul_d.c
@@ -10,6 +10,22 @@ mp_err mp_mul_d(const mp_int *a, mp_digit b, mp_int *c)
    mp_err   err;
    int   ix, oldused;
 
+   if (b == 1u) {
+      return mp_copy(a, c);
+   }
+
+   /* power of two ? */
+   if (MP_HAS(MP_MUL_2) && (b == 2u)) {
+      return mp_mul_2(a, c);
+   }
+   if (MP_HAS(MP_MUL_2D) && (b != 0u) && ((b & (b - 1u)) == 0u)) {
+      ix = 1;
+      while ((ix < MP_DIGIT_BIT) && (b != (((mp_digit)1)<<ix))) {
+         ix++;
+      }
+      return mp_mul_2d(a, ix, c);
+   }
+
    /* make sure c is big enough to hold a*b */
    if ((err = mp_grow(c, a->used + 1)) != MP_OKAY) {
       return err;