summaryrefslogtreecommitdiff
path: root/internal
diff options
context:
space:
mode:
authorJohn Hawthorn <john@hawthorn.email>2025-11-02 21:13:37 -0800
committerJohn Hawthorn <john@hawthorn.email>2025-12-05 06:21:17 -0800
commitb0c9286d98929db56514d8009040fe206b3d310f (patch)
treec76a9a0f71280d190dcd66115d85a8337d89d245 /internal
parentea415e9636d3be8890d5dc97cf0b67fc95403a46 (diff)
Use VWA for bignum
Previously we only allocated bignums from the 40 byte sizepool, and embedded bignum used a fixed size.
Diffstat (limited to 'internal')
-rw-r--r--internal/bignum.h25
1 files changed, 15 insertions, 10 deletions
diff --git a/internal/bignum.h b/internal/bignum.h
index 0ba21a4923..e5b6b42563 100644
--- a/internal/bignum.h
+++ b/internal/bignum.h
@@ -9,6 +9,7 @@
* @brief Internal header for Bignums.
*/
#include "ruby/internal/config.h" /* for HAVE_LIBGMP */
+#include "internal/compilers.h" /* for FLEX_ARY_LEN */
#include <stddef.h> /* for size_t */
#ifdef HAVE_SYS_TYPES_H
@@ -76,18 +77,17 @@
#define RBIGNUM(obj) ((struct RBignum *)(obj))
#define BIGNUM_SIGN_BIT FL_USER1
#define BIGNUM_EMBED_FLAG ((VALUE)FL_USER2)
-#define BIGNUM_EMBED_LEN_NUMBITS 3
+
+/* This is likely more bits than we need today and will also need adjustment if
+ * we change GC slot sizes.
+ */
+#define BIGNUM_EMBED_LEN_NUMBITS 9
#define BIGNUM_EMBED_LEN_MASK \
- (~(~(VALUE)0U << BIGNUM_EMBED_LEN_NUMBITS) << BIGNUM_EMBED_LEN_SHIFT)
+ (RUBY_FL_USER11 | RUBY_FL_USER10 | RUBY_FL_USER9 | RUBY_FL_USER8 | RUBY_FL_USER7 | \
+ RUBY_FL_USER6 | RUBY_FL_USER5 | RUBY_FL_USER4 | RUBY_FL_USER3)
#define BIGNUM_EMBED_LEN_SHIFT \
(FL_USHIFT+3) /* bit offset of BIGNUM_EMBED_LEN_MASK */
-#ifndef BIGNUM_EMBED_LEN_MAX
-# if (SIZEOF_VALUE*RBIMPL_RVALUE_EMBED_LEN_MAX/SIZEOF_ACTUAL_BDIGIT) < (1 << BIGNUM_EMBED_LEN_NUMBITS)-1
-# define BIGNUM_EMBED_LEN_MAX (SIZEOF_VALUE*RBIMPL_RVALUE_EMBED_LEN_MAX/SIZEOF_ACTUAL_BDIGIT)
-# else
-# define BIGNUM_EMBED_LEN_MAX ((1 << BIGNUM_EMBED_LEN_NUMBITS)-1)
-# endif
-#endif
+#define BIGNUM_EMBED_LEN_MAX (BIGNUM_EMBED_LEN_MASK >> BIGNUM_EMBED_LEN_SHIFT)
enum rb_int_parse_flags {
RB_INT_PARSE_SIGN = 0x01,
@@ -104,7 +104,12 @@ struct RBignum {
size_t len;
BDIGIT *digits;
} heap;
- BDIGIT ary[BIGNUM_EMBED_LEN_MAX];
+ /* This is a length 1 array because:
+ * 1. GCC has a bug that does not optimize C flexible array members
+ * (https://gcc.gnu.org/bugzilla/show_bug.cgi?id=102452)
+ * 2. Zero length arrays are not supported by all compilers
+ */
+ BDIGIT ary[1];
} as;
};