Add utility macros DECIMAL_SIZE_OF and DECIMAL_SIZE_OF_BYTES

This commit is contained in:
Nobuyoshi Nakada 2023-02-04 01:31:56 +09:00
parent 45f0e3a673
commit 2490b2e121
Notes: git 2023-02-14 06:18:41 +00:00
3 changed files with 11 additions and 2 deletions

2
ast.c
View File

@ -377,7 +377,7 @@ rest_arg(rb_ast_t *ast, const NODE *rest_arg)
static VALUE
node_children(rb_ast_t *ast, const NODE *node)
{
char name[DECIMAL_SIZE_OF_BITS(sizeof(long) * CHAR_BIT) + 2]; /* including '$' */
char name[sizeof("$") + DECIMAL_SIZE_OF(long)];
enum node_type type = nd_type(node);
switch (type) {

View File

@ -8275,7 +8275,7 @@ compile_builtin_function_call(rb_iseq_t *iseq, LINK_ANCHOR *const ret, const NOD
}
else {
# define BUILTIN_INLINE_PREFIX "_bi"
char inline_func[DECIMAL_SIZE_OF_BITS(sizeof(int) * CHAR_BIT) + sizeof(BUILTIN_INLINE_PREFIX)];
char inline_func[sizeof(BUILTIN_INLINE_PREFIX) + DECIMAL_SIZE_OF(int)];
bool cconst = false;
retry:;
const struct rb_builtin_function *bf = iseq_builtin_function_lookup(iseq, builtin_func);

View File

@ -36,6 +36,15 @@ RBIMPL_SYMBOL_EXPORT_BEGIN()
/** an approximation of ceil(n * log10(2)), up to 65536 at least */
#define DECIMAL_SIZE_OF_BITS(n) (((n) * 3010 + 9998) / 9999)
/** an approximation of decimal representation size for n-bytes */
#define DECIMAL_SIZE_OF_BYTES(n) DECIMAL_SIZE_OF_BITS((n) * CHAR_BIT)
/**
* An approximation of decimal representation size. `expr` may be a
* type name
*/
#define DECIMAL_SIZE_OF(expr) DECIMAL_SIZE_OF_BYTES(sizeof(expr))
/**
* Character to number mapping like `'a'` -> `10`, `'b'` -> `11` etc. For
* punctuation etc., the value is -1. "36" terminology comes from the fact