summaryrefslogtreecommitdiff
path: root/include/ruby/util.h
diff options
context:
space:
mode:
authorNobuyoshi Nakada <nobu@ruby-lang.org>2023-02-04 01:31:56 +0900
committerNobuyoshi Nakada <nobu@ruby-lang.org>2023-02-14 15:18:21 +0900
commit2490b2e1216f02aa896abdf121a28e50cac107c3 (patch)
tree8d76b8bae254eb4f2d9083fe8f52c661230d6cab /include/ruby/util.h
parent45f0e3a673964069a4c9c57ce8665cbc21ac267f (diff)
downloadruby-2490b2e1216f02aa896abdf121a28e50cac107c3.tar.gz
Add utility macros `DECIMAL_SIZE_OF` and `DECIMAL_SIZE_OF_BYTES`
Diffstat (limited to 'include/ruby/util.h')
-rw-r--r--include/ruby/util.h9
1 files changed, 9 insertions, 0 deletions
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