optimize mp_div_d

This commit is contained in:
Daniel Mendler 2019-11-06 16:54:07 +01:00
parent 4f00e75b8f
commit 6ec36e0b9b
No known key found for this signature in database
GPG Key ID: D88ADB2A2693CA43

View File

@ -28,7 +28,13 @@ mp_err mp_div_d(const mp_int *a, mp_digit b, mp_int *c, mp_digit *d)
}
/* power of two ? */
if ((b & (b - 1u)) == 0u) {
if (MP_HAS(MP_DIV_2) && (b == 2u)) {
if (d != NULL) {
*d = mp_isodd(a) ? 1u : 0u;
}
return (c == NULL) ? MP_OKAY : mp_div_2(a, c);
}
if (MP_HAS(MP_DIV_2D) && (b != 0u) && ((b & (b - 1u)) == 0u)) {
ix = 1;
while ((ix < MP_DIGIT_BIT) && (b != (((mp_digit)1)<<ix))) {
ix++;
@ -36,10 +42,7 @@ mp_err mp_div_d(const mp_int *a, mp_digit b, mp_int *c, mp_digit *d)
if (d != NULL) {
*d = a->dp[0] & (((mp_digit)1<<(mp_digit)ix) - 1uL);
}
if (c != NULL) {
return mp_div_2d(a, ix, c, NULL);
}
return MP_OKAY;
return (c == NULL) ? MP_OKAY : mp_div_2d(a, ix, c, NULL);
}
/* three? */