1
0
Fork 0
mirror of https://github.com/ruby/ruby.git synced 2022-11-09 12:17:21 -05:00

Use roomof macro for rounding up divisions

This commit is contained in:
Nobuyoshi Nakada 2022-10-04 18:10:41 +09:00
parent ee8bcbf405
commit 5ccb625fbb
No known key found for this signature in database
GPG key ID: 7CD2805BFA3770C6
7 changed files with 15 additions and 12 deletions

View file

@ -1389,7 +1389,7 @@ ary_make_partial_step(VALUE ary, VALUE klass, long offset, long len, long step)
} }
long ustep = (step < 0) ? -step : step; long ustep = (step < 0) ? -step : step;
len = (len + ustep - 1) / ustep; len = roomof(len, ustep);
long i; long i;
long j = offset + ((step > 0) ? 0 : (orig_len - 1)); long j = offset + ((step > 0) ? 0 : (orig_len - 1));

View file

@ -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 */ /* nlp_bits stands for number of leading padding bits */
size_t num_bits = (wordsize * CHAR_BIT - nails) * numwords; 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); *nlp_bits_ret = (int)(num_bdigits * BITSPERDIG - num_bits);
return num_bdigits; 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 */ /* BITSPERDIG = SIZEOF_BDIGIT * CHAR_BIT */
/* num_bits = (wordsize * CHAR_BIT - nails) * numwords */ /* 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 */ /* num_bits = CHAR_BIT * (wordsize * numwords) - nails * numwords = CHAR_BIT * num_bytes1 - nails * numwords */
size_t num_bytes1 = wordsize * numwords; size_t num_bytes1 = wordsize * numwords;

2
gc.c
View file

@ -870,7 +870,7 @@ typedef struct rb_objspace {
#define BASE_SLOT_SIZE sizeof(RVALUE) #define BASE_SLOT_SIZE sizeof(RVALUE)
#define CEILDIV(i, mod) (((i) + (mod) - 1)/(mod)) #define CEILDIV(i, mod) roomof(i, mod)
enum { enum {
HEAP_PAGE_ALIGN = (1UL << HEAP_PAGE_ALIGN_LOG), HEAP_PAGE_ALIGN = (1UL << HEAP_PAGE_ALIGN_LOG),
HEAP_PAGE_ALIGN_MASK = (~(~0UL << HEAP_PAGE_ALIGN_LOG)), HEAP_PAGE_ALIGN_MASK = (~(~0UL << HEAP_PAGE_ALIGN_LOG)),

View file

@ -35,12 +35,16 @@ enum ruby_num_rounding_mode {
RUBY_NUM_ROUND_DEFAULT = ROUND_DEFAULT, 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 #if SIZEOF_DOUBLE <= SIZEOF_VALUE
typedef double rb_float_value_type; typedef double rb_float_value_type;
#else #else
typedef struct { typedef struct {
VALUE values[(SIZEOF_DOUBLE + SIZEOF_VALUE - 1) / SIZEOF_VALUE]; VALUE values[roomof(SIZEOF_DOUBLE, SIZEOF_VALUE)];
/* roomof() needs internal.h, and the order of some macros may matter */
} rb_float_value_type; } rb_float_value_type;
#endif #endif

View file

@ -1055,7 +1055,7 @@ flo_to_s(VALUE flt)
{ {
enum {decimal_mant = DBL_MANT_DIG-DBL_DIG}; enum {decimal_mant = DBL_MANT_DIG-DBL_DIG};
enum {float_dig = DBL_DIG+1}; 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); double value = RFLOAT_VALUE(flt);
VALUE s; VALUE s;
char *p, *e; char *p, *e;

View file

@ -341,7 +341,7 @@ static int
select_str_opcode(int mb_len, OnigDistance byte_len, int ignore_case) select_str_opcode(int mb_len, OnigDistance byte_len, int ignore_case)
{ {
int op; int op;
OnigDistance str_len = (byte_len + mb_len - 1) / mb_len; OnigDistance str_len = roomof(byte_len, mb_len);
if (ignore_case) { if (ignore_case) {
switch (str_len) { switch (str_len) {

View file

@ -125,10 +125,9 @@ typedef struct {
#define POSIX_BRACKET_ENTRY_INIT(name, ctype) \ #define POSIX_BRACKET_ENTRY_INIT(name, ctype) \
{(short int )(sizeof(name) - 1), name, (ctype)} {(short int )(sizeof(name) - 1), name, (ctype)}
#ifndef numberof #define numberof(array) ((int)(sizeof(array) / sizeof((array)[0])))
# define numberof(array) (int )(sizeof(array) / sizeof((array)[0])) #define roomof(x, y) (((x) + (y) - 1) / (y))
#endif #define type_roomof(x, y) roomof(sizeof(x), sizeof(y))
#define USE_CRNL_AS_LINE_TERMINATOR #define USE_CRNL_AS_LINE_TERMINATOR
#define USE_UNICODE_PROPERTIES #define USE_UNICODE_PROPERTIES