From 5ccb625fbbd1e774636a9fdbe0bf1c3d38e085d5 Mon Sep 17 00:00:00 2001 From: Nobuyoshi Nakada Date: Tue, 4 Oct 2022 18:10:41 +0900 Subject: [PATCH] Use `roomof` macro for rounding up divisions --- array.c | 2 +- bignum.c | 4 ++-- gc.c | 2 +- internal/numeric.h | 8 ++++++-- numeric.c | 2 +- regcomp.c | 2 +- regenc.h | 7 +++---- 7 files changed, 15 insertions(+), 12 deletions(-) diff --git a/array.c b/array.c index 73e8a3c9ce..a33c43bdbf 100644 --- a/array.c +++ b/array.c @@ -1389,7 +1389,7 @@ ary_make_partial_step(VALUE ary, VALUE klass, long offset, long len, long step) } long ustep = (step < 0) ? -step : step; - len = (len + ustep - 1) / ustep; + len = roomof(len, ustep); long i; long j = offset + ((step > 0) ? 0 : (orig_len - 1)); diff --git a/bignum.c b/bignum.c index 9901a807b1..4f8349dd17 100644 --- a/bignum.c +++ b/bignum.c @@ -977,7 +977,7 @@ integer_unpack_num_bdigits_small(size_t numwords, size_t wordsize, size_t nails, { /* nlp_bits stands for number of leading padding bits */ size_t num_bits = (wordsize * CHAR_BIT - nails) * numwords; - size_t num_bdigits = (num_bits + BITSPERDIG - 1) / BITSPERDIG; + size_t num_bdigits = roomof(num_bits, BITSPERDIG); *nlp_bits_ret = (int)(num_bdigits * BITSPERDIG - num_bits); return num_bdigits; } @@ -987,7 +987,7 @@ integer_unpack_num_bdigits_generic(size_t numwords, size_t wordsize, size_t nail { /* BITSPERDIG = SIZEOF_BDIGIT * CHAR_BIT */ /* num_bits = (wordsize * CHAR_BIT - nails) * numwords */ - /* num_bdigits = (num_bits + BITSPERDIG - 1) / BITSPERDIG */ + /* num_bdigits = roomof(num_bits, BITSPERDIG) */ /* num_bits = CHAR_BIT * (wordsize * numwords) - nails * numwords = CHAR_BIT * num_bytes1 - nails * numwords */ size_t num_bytes1 = wordsize * numwords; diff --git a/gc.c b/gc.c index a5e37b72e2..52d5609c3e 100644 --- a/gc.c +++ b/gc.c @@ -870,7 +870,7 @@ typedef struct rb_objspace { #define BASE_SLOT_SIZE sizeof(RVALUE) -#define CEILDIV(i, mod) (((i) + (mod) - 1)/(mod)) +#define CEILDIV(i, mod) roomof(i, mod) enum { HEAP_PAGE_ALIGN = (1UL << HEAP_PAGE_ALIGN_LOG), HEAP_PAGE_ALIGN_MASK = (~(~0UL << HEAP_PAGE_ALIGN_LOG)), diff --git a/internal/numeric.h b/internal/numeric.h index 19069cb3bc..89bc54b307 100644 --- a/internal/numeric.h +++ b/internal/numeric.h @@ -35,12 +35,16 @@ enum ruby_num_rounding_mode { RUBY_NUM_ROUND_DEFAULT = ROUND_DEFAULT, }; +/* same as internal.h */ +#define numberof(array) ((int)(sizeof(array) / sizeof((array)[0]))) +#define roomof(x, y) (((x) + (y) - 1) / (y)) +#define type_roomof(x, y) roomof(sizeof(x), sizeof(y)) + #if SIZEOF_DOUBLE <= SIZEOF_VALUE typedef double rb_float_value_type; #else typedef struct { - VALUE values[(SIZEOF_DOUBLE + SIZEOF_VALUE - 1) / SIZEOF_VALUE]; - /* roomof() needs internal.h, and the order of some macros may matter */ + VALUE values[roomof(SIZEOF_DOUBLE, SIZEOF_VALUE)]; } rb_float_value_type; #endif diff --git a/numeric.c b/numeric.c index 4f927f00fb..8607d69794 100644 --- a/numeric.c +++ b/numeric.c @@ -1055,7 +1055,7 @@ flo_to_s(VALUE flt) { enum {decimal_mant = DBL_MANT_DIG-DBL_DIG}; enum {float_dig = DBL_DIG+1}; - char buf[float_dig + (decimal_mant + CHAR_BIT - 1) / CHAR_BIT + 10]; + char buf[float_dig + roomof(decimal_mant, CHAR_BIT) + 10]; double value = RFLOAT_VALUE(flt); VALUE s; char *p, *e; diff --git a/regcomp.c b/regcomp.c index 94640639d8..be85d85f93 100644 --- a/regcomp.c +++ b/regcomp.c @@ -341,7 +341,7 @@ static int select_str_opcode(int mb_len, OnigDistance byte_len, int ignore_case) { int op; - OnigDistance str_len = (byte_len + mb_len - 1) / mb_len; + OnigDistance str_len = roomof(byte_len, mb_len); if (ignore_case) { switch (str_len) { diff --git a/regenc.h b/regenc.h index 8c4ff0483b..1c40901054 100644 --- a/regenc.h +++ b/regenc.h @@ -125,10 +125,9 @@ typedef struct { #define POSIX_BRACKET_ENTRY_INIT(name, ctype) \ {(short int )(sizeof(name) - 1), name, (ctype)} -#ifndef numberof -# define numberof(array) (int )(sizeof(array) / sizeof((array)[0])) -#endif - +#define numberof(array) ((int)(sizeof(array) / sizeof((array)[0]))) +#define roomof(x, y) (((x) + (y) - 1) / (y)) +#define type_roomof(x, y) roomof(sizeof(x), sizeof(y)) #define USE_CRNL_AS_LINE_TERMINATOR #define USE_UNICODE_PROPERTIES