From 2490b2e1216f02aa896abdf121a28e50cac107c3 Mon Sep 17 00:00:00 2001 From: Nobuyoshi Nakada Date: Sat, 4 Feb 2023 01:31:56 +0900 Subject: Add utility macros `DECIMAL_SIZE_OF` and `DECIMAL_SIZE_OF_BYTES` --- include/ruby/util.h | 9 +++++++++ 1 file changed, 9 insertions(+) (limited to 'include') diff --git a/include/ruby/util.h b/include/ruby/util.h index e8727a3200..ee11bc940a 100644 --- a/include/ruby/util.h +++ b/include/ruby/util.h @@ -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 -- cgit v1.2.1