optimize mp_mul_d

This commit is contained in:
Daniel Mendler 2019-11-06 16:56:34 +01:00
parent 6ec36e0b9b
commit 2e88b571c1
No known key found for this signature in database
GPG Key ID: D88ADB2A2693CA43

View File

@ -10,6 +10,22 @@ mp_err mp_mul_d(const mp_int *a, mp_digit b, mp_int *c)
mp_err err; mp_err err;
int ix, oldused; 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 */ /* make sure c is big enough to hold a*b */
if ((err = mp_grow(c, a->used + 1)) != MP_OKAY) { if ((err = mp_grow(c, a->used + 1)) != MP_OKAY) {
return err; return err;