add macro MP_IS_2EXPT

This commit is contained in:
Daniel Mendler 2019-11-06 16:59:51 +01:00
parent 2e88b571c1
commit 6777baaf97
No known key found for this signature in database
GPG Key ID: D88ADB2A2693CA43
4 changed files with 5 additions and 3 deletions

View File

@ -34,7 +34,7 @@ mp_err mp_div_d(const mp_int *a, mp_digit b, mp_int *c, mp_digit *d)
} }
return (c == NULL) ? MP_OKAY : mp_div_2(a, c); return (c == NULL) ? MP_OKAY : mp_div_2(a, c);
} }
if (MP_HAS(MP_DIV_2D) && (b != 0u) && ((b & (b - 1u)) == 0u)) { if (MP_HAS(MP_DIV_2D) && MP_IS_2EXPT(b)) {
ix = 1; ix = 1;
while ((ix < MP_DIGIT_BIT) && (b != (((mp_digit)1)<<ix))) { while ((ix < MP_DIGIT_BIT) && (b != (((mp_digit)1)<<ix))) {
ix++; ix++;

View File

@ -17,7 +17,7 @@ mp_err mp_log_u32(const mp_int *a, uint32_t base, uint32_t *c)
return MP_VAL; return MP_VAL;
} }
if (MP_HAS(S_MP_LOG_POW2) && ((base & (base - 1u)) == 0u)) { if (MP_HAS(S_MP_LOG_POW2) && MP_IS_2EXPT(base)) {
*c = s_mp_log_pow2(a, base); *c = s_mp_log_pow2(a, base);
return MP_OKAY; return MP_OKAY;
} }

View File

@ -18,7 +18,7 @@ mp_err mp_mul_d(const mp_int *a, mp_digit b, mp_int *c)
if (MP_HAS(MP_MUL_2) && (b == 2u)) { if (MP_HAS(MP_MUL_2) && (b == 2u)) {
return mp_mul_2(a, c); return mp_mul_2(a, c);
} }
if (MP_HAS(MP_MUL_2D) && (b != 0u) && ((b & (b - 1u)) == 0u)) { if (MP_HAS(MP_MUL_2D) && MP_IS_2EXPT(b)) {
ix = 1; ix = 1;
while ((ix < MP_DIGIT_BIT) && (b != (((mp_digit)1)<<ix))) { while ((ix < MP_DIGIT_BIT) && (b != (((mp_digit)1)<<ix))) {
ix++; ix++;

View File

@ -120,6 +120,8 @@ extern void MP_FREE(void *mem, size_t size);
#define MP_EXCH(t, a, b) do { t _c = a; a = b; b = _c; } while (0) #define MP_EXCH(t, a, b) do { t _c = a; a = b; b = _c; } while (0)
#define MP_IS_2EXPT(x) (((x) != 0u) && (((x) & ((x) - 1u)) == 0u))
/* Static assertion */ /* Static assertion */
#define MP_STATIC_ASSERT(msg, cond) typedef char mp_static_assert_##msg[(cond) ? 1 : -1]; #define MP_STATIC_ASSERT(msg, cond) typedef char mp_static_assert_##msg[(cond) ? 1 : -1];