From aa00a6b8924a7095e9bf650fcda4e69551af69b2 Mon Sep 17 00:00:00 2001 From: Tim Ruffing Date: Fri, 12 Apr 2024 17:24:29 +0200 Subject: [PATCH] Introduce CEIL_DIV macro and use it --- src/ecmult_impl.h | 6 +++--- src/util.h | 5 ++++- 2 files changed, 7 insertions(+), 4 deletions(-) diff --git a/src/ecmult_impl.h b/src/ecmult_impl.h index 6d14c7ac..2cf38399 100644 --- a/src/ecmult_impl.h +++ b/src/ecmult_impl.h @@ -42,7 +42,7 @@ #endif #define WNAF_BITS 128 -#define WNAF_SIZE_BITS(bits, w) (((bits) + (w) - 1) / (w)) +#define WNAF_SIZE_BITS(bits, w) CEIL_DIV(bits, w) #define WNAF_SIZE(w) WNAF_SIZE_BITS(WNAF_BITS, w) /* The number of objects allocated on the scratch space for ecmult_multi algorithms */ @@ -808,8 +808,8 @@ static int secp256k1_ecmult_multi_batch_size_helper(size_t *n_batches, size_t *n return 1; } /* Compute ceil(n/max_n_batch_points) and ceil(n/n_batches) */ - *n_batches = 1 + (n - 1) / max_n_batch_points; - *n_batch_points = 1 + (n - 1) / *n_batches; + *n_batches = CEIL_DIV(n, max_n_batch_points); + *n_batch_points = CEIL_DIV(n, *n_batches); return 1; } diff --git a/src/util.h b/src/util.h index 154d9ebc..b04a239d 100644 --- a/src/util.h +++ b/src/util.h @@ -170,7 +170,10 @@ static SECP256K1_INLINE void *checked_malloc(const secp256k1_callback* cb, size_ #define ALIGNMENT 16 #endif -#define ROUND_TO_ALIGN(size) ((((size) + ALIGNMENT - 1) / ALIGNMENT) * ALIGNMENT) +/* ceil(x/y) for integers x > 0 and y > 0. Here, / denotes rational division. */ +#define CEIL_DIV(x, y) (1 + ((x) - 1) / (y)) + +#define ROUND_TO_ALIGN(size) (CEIL_DIV(size, ALIGNMENT) * ALIGNMENT) /* Macro for restrict, when available and not in a VERIFY build. */ #if defined(SECP256K1_BUILD) && defined(VERIFY)