Commit 6777baaf97c6737bded86c8b7c17c7d280e20057

Daniel Mendler 2019-11-06T16:59:51

add macro MP_IS_2EXPT

diff --git a/mp_div_d.c b/mp_div_d.c
index ab8572f..5697e54 100644
--- a/mp_div_d.c
+++ b/mp_div_d.c
@@ -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);
    }
-   if (MP_HAS(MP_DIV_2D) && (b != 0u) && ((b & (b - 1u)) == 0u)) {
+   if (MP_HAS(MP_DIV_2D) && MP_IS_2EXPT(b)) {
       ix = 1;
       while ((ix < MP_DIGIT_BIT) && (b != (((mp_digit)1)<<ix))) {
          ix++;
diff --git a/mp_log_u32.c b/mp_log_u32.c
index 31d9662..949ef87 100644
--- a/mp_log_u32.c
+++ b/mp_log_u32.c
@@ -17,7 +17,7 @@ mp_err mp_log_u32(const mp_int *a, uint32_t base, uint32_t *c)
       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);
       return MP_OKAY;
    }
diff --git a/mp_mul_d.c b/mp_mul_d.c
index c10312a..2585055 100644
--- a/mp_mul_d.c
+++ b/mp_mul_d.c
@@ -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)) {
       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;
       while ((ix < MP_DIGIT_BIT) && (b != (((mp_digit)1)<<ix))) {
          ix++;
diff --git a/tommath_private.h b/tommath_private.h
index f8af531..f629502 100644
--- a/tommath_private.h
+++ b/tommath_private.h
@@ -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_IS_2EXPT(x) (((x) != 0u) && (((x) & ((x) - 1u)) == 0u))
+
 /* Static assertion */
 #define MP_STATIC_ASSERT(msg, cond) typedef char mp_static_assert_##msg[(cond) ? 1 : -1];