Skip to content

Commit

Permalink
Use roomof macro for rounding up divisions
Browse files Browse the repository at this point in the history
  • Loading branch information
nobu committed Oct 14, 2022
1 parent ee8bcbf commit 5ccb625
Show file tree
Hide file tree
Showing 7 changed files with 15 additions and 12 deletions.
2 changes: 1 addition & 1 deletion array.c
Expand Up @@ -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));
Expand Down
4 changes: 2 additions & 2 deletions bignum.c
Expand Up @@ -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;
}
Expand All @@ -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;
Expand Down
2 changes: 1 addition & 1 deletion gc.c
Expand Up @@ -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)),
Expand Down
8 changes: 6 additions & 2 deletions internal/numeric.h
Expand Up @@ -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

Expand Down
2 changes: 1 addition & 1 deletion numeric.c
Expand Up @@ -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;
Expand Down
2 changes: 1 addition & 1 deletion regcomp.c
Expand Up @@ -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) {
Expand Down
7 changes: 3 additions & 4 deletions regenc.h
Expand Up @@ -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
Expand Down

0 comments on commit 5ccb625

Please sign in to comment.