Ruby 4.1.0dev (2026-05-15 revision 9b747f5ef98669d571df1b73d5318bc09e4e13fd)
bignum.h
1#ifndef INTERNAL_BIGNUM_H /*-*-C-*-vi:se ft=c:*/
2#define INTERNAL_BIGNUM_H
11#include "ruby/internal/config.h" /* for HAVE_LIBGMP */
12#include "internal/compilers.h" /* for FLEX_ARY_LEN */
13#include <stddef.h> /* for size_t */
14
15#ifdef HAVE_SYS_TYPES_H
16# include <sys/types.h> /* for ssize_t (note: on Windows ssize_t is */
17#endif /* `#define`d in ruby/config.h) */
18
19#include "ruby/internal/stdbool.h" /* for bool */
20#include "ruby/ruby.h" /* for struct RBasic */
21
22#ifndef BDIGIT
23# if SIZEOF_INT*2 <= SIZEOF_LONG_LONG
24# define BDIGIT unsigned int
25# define SIZEOF_BDIGIT SIZEOF_INT
26# define BDIGIT_DBL unsigned LONG_LONG
27# define BDIGIT_DBL_SIGNED LONG_LONG
28# define PRI_BDIGIT_PREFIX ""
29# define PRI_BDIGIT_DBL_PREFIX PRI_LL_PREFIX
30# elif SIZEOF_INT*2 <= SIZEOF_LONG
31# define BDIGIT unsigned int
32# define SIZEOF_BDIGIT SIZEOF_INT
33# define BDIGIT_DBL unsigned long
34# define BDIGIT_DBL_SIGNED long
35# define PRI_BDIGIT_PREFIX ""
36# define PRI_BDIGIT_DBL_PREFIX "l"
37# elif SIZEOF_SHORT*2 <= SIZEOF_LONG
38# define BDIGIT unsigned short
39# define SIZEOF_BDIGIT SIZEOF_SHORT
40# define BDIGIT_DBL unsigned long
41# define BDIGIT_DBL_SIGNED long
42# define PRI_BDIGIT_PREFIX "h"
43# define PRI_BDIGIT_DBL_PREFIX "l"
44# else
45# define BDIGIT unsigned short
46# define SIZEOF_BDIGIT (SIZEOF_LONG/2)
47# define SIZEOF_ACTUAL_BDIGIT SIZEOF_LONG
48# define BDIGIT_DBL unsigned long
49# define BDIGIT_DBL_SIGNED long
50# define PRI_BDIGIT_PREFIX "h"
51# define PRI_BDIGIT_DBL_PREFIX "l"
52# endif
53#endif
54
55#ifndef SIZEOF_ACTUAL_BDIGIT
56# define SIZEOF_ACTUAL_BDIGIT SIZEOF_BDIGIT
57#endif
58
59#ifdef PRI_BDIGIT_PREFIX
60# define PRIdBDIGIT PRI_BDIGIT_PREFIX"d"
61# define PRIiBDIGIT PRI_BDIGIT_PREFIX"i"
62# define PRIoBDIGIT PRI_BDIGIT_PREFIX"o"
63# define PRIuBDIGIT PRI_BDIGIT_PREFIX"u"
64# define PRIxBDIGIT PRI_BDIGIT_PREFIX"x"
65# define PRIXBDIGIT PRI_BDIGIT_PREFIX"X"
66#endif
67
68#ifdef PRI_BDIGIT_DBL_PREFIX
69# define PRIdBDIGIT_DBL PRI_BDIGIT_DBL_PREFIX"d"
70# define PRIiBDIGIT_DBL PRI_BDIGIT_DBL_PREFIX"i"
71# define PRIoBDIGIT_DBL PRI_BDIGIT_DBL_PREFIX"o"
72# define PRIuBDIGIT_DBL PRI_BDIGIT_DBL_PREFIX"u"
73# define PRIxBDIGIT_DBL PRI_BDIGIT_DBL_PREFIX"x"
74# define PRIXBDIGIT_DBL PRI_BDIGIT_DBL_PREFIX"X"
75#endif
76
77#define RBIGNUM(obj) ((struct RBignum *)(obj))
78#define BIGNUM_SIGN_BIT FL_USER1
79#define BIGNUM_EMBED_FLAG ((VALUE)FL_USER2)
80
81/* This is likely more bits than we need today and will also need adjustment if
82 * we change GC slot sizes.
83 */
84#define BIGNUM_EMBED_LEN_NUMBITS 9
85#define BIGNUM_EMBED_LEN_MASK \
86 (RUBY_FL_USER11 | RUBY_FL_USER10 | RUBY_FL_USER9 | RUBY_FL_USER8 | RUBY_FL_USER7 | \
87 RUBY_FL_USER6 | RUBY_FL_USER5 | RUBY_FL_USER4 | RUBY_FL_USER3)
88#define BIGNUM_EMBED_LEN_SHIFT \
89 (FL_USHIFT+3) /* bit offset of BIGNUM_EMBED_LEN_MASK */
90#define BIGNUM_EMBED_LEN_MAX (BIGNUM_EMBED_LEN_MASK >> BIGNUM_EMBED_LEN_SHIFT)
91
92struct RBignum {
93 struct RBasic basic;
94 union {
95 struct {
96 size_t len;
97 BDIGIT *digits;
98 } heap;
99 /* This is a length 1 array because:
100 * 1. GCC has a bug that does not optimize C flexible array members
101 * (https://gcc.gnu.org/bugzilla/show_bug.cgi?id=102452)
102 * 2. Zero length arrays are not supported by all compilers
103 */
104 BDIGIT ary[1];
105 } as;
106};
107
108/* bignum.c */
109extern const char ruby_digitmap[];
110extern const char ruby_decimal_digit_pairs[];
111double rb_big_fdiv_double(VALUE x, VALUE y);
112VALUE rb_big_uminus(VALUE x);
113VALUE rb_big_hash(VALUE);
114VALUE rb_big_odd_p(VALUE);
115VALUE rb_big_even_p(VALUE);
116size_t rb_big_size(VALUE);
117VALUE rb_integer_float_cmp(VALUE x, VALUE y);
118VALUE rb_integer_float_eq(VALUE x, VALUE y);
119VALUE rb_str_convert_to_inum(VALUE str, int base, int badcheck, int raise_exception);
120VALUE rb_big_comp(VALUE x);
121VALUE rb_big_aref(VALUE x, VALUE y);
122VALUE rb_big_aref2(VALUE num, VALUE beg, VALUE len);
123VALUE rb_big_abs(VALUE x);
124VALUE rb_big_size_m(VALUE big);
125VALUE rb_big_bit_length(VALUE big);
126VALUE rb_big_remainder(VALUE x, VALUE y);
127VALUE rb_big_gt(VALUE x, VALUE y);
128VALUE rb_big_ge(VALUE x, VALUE y);
129VALUE rb_big_lt(VALUE x, VALUE y);
130VALUE rb_big_le(VALUE x, VALUE y);
131VALUE rb_int_powm(int const argc, VALUE * const argv, VALUE const num);
132VALUE rb_big_isqrt(VALUE n);
133static inline bool BIGNUM_SIGN(VALUE b);
134static inline bool BIGNUM_POSITIVE_P(VALUE b);
135static inline bool BIGNUM_NEGATIVE_P(VALUE b);
136static inline void BIGNUM_SET_SIGN(VALUE b, bool sign);
137static inline void BIGNUM_NEGATE(VALUE b);
138static inline size_t BIGNUM_LEN(VALUE b);
139static inline BDIGIT *BIGNUM_DIGITS(VALUE b);
140static inline int BIGNUM_LENINT(VALUE b);
141static inline bool BIGNUM_EMBED_P(VALUE b);
142
143RUBY_SYMBOL_EXPORT_BEGIN
144/* bignum.c (export) */
145VALUE rb_big_mul_normal(VALUE x, VALUE y);
146VALUE rb_big_mul_balance(VALUE x, VALUE y);
147VALUE rb_big_mul_karatsuba(VALUE x, VALUE y);
148VALUE rb_big_mul_toom3(VALUE x, VALUE y);
149VALUE rb_big_sq_fast(VALUE x);
150VALUE rb_big_divrem_normal(VALUE x, VALUE y);
151VALUE rb_big2str_poweroftwo(VALUE x, int base);
152VALUE rb_big2str_generic(VALUE x, int base);
153VALUE rb_str2big_poweroftwo(VALUE arg, int base, int badcheck);
154VALUE rb_str2big_normal(VALUE arg, int base, int badcheck);
155VALUE rb_str2big_karatsuba(VALUE arg, int base, int badcheck);
156#if defined(HAVE_LIBGMP) && defined(HAVE_GMP_H)
157VALUE rb_big_mul_gmp(VALUE x, VALUE y);
158VALUE rb_big_divrem_gmp(VALUE x, VALUE y);
159VALUE rb_big2str_gmp(VALUE x, int base);
160VALUE rb_str2big_gmp(VALUE arg, int base, int badcheck);
161#endif
162RUBY_SYMBOL_EXPORT_END
163
164#if HAVE_LONG_LONG
165VALUE rb_ull2big(unsigned LONG_LONG n);
166VALUE rb_ll2big(LONG_LONG n);
167#endif
168
169#if defined(HAVE_INT128_T)
170VALUE rb_uint128t2big(uint128_t n);
171VALUE rb_int128t2big(int128_t n);
172#endif
173
174/* sign: positive:1, negative:0 */
175static inline bool
176BIGNUM_SIGN(VALUE b)
177{
178 return FL_TEST_RAW(b, BIGNUM_SIGN_BIT);
179}
180
181static inline bool
182BIGNUM_POSITIVE_P(VALUE b)
183{
184 return BIGNUM_SIGN(b);
185}
186
187static inline bool
188BIGNUM_NEGATIVE_P(VALUE b)
189{
190 return ! BIGNUM_POSITIVE_P(b);
191}
192
193static inline void
194BIGNUM_SET_SIGN(VALUE b, bool sign)
195{
196 if (sign) {
197 FL_SET_RAW(b, BIGNUM_SIGN_BIT);
198 }
199 else {
200 FL_UNSET_RAW(b, BIGNUM_SIGN_BIT);
201 }
202}
203
204static inline void
205BIGNUM_NEGATE(VALUE b)
206{
207 FL_REVERSE_RAW(b, BIGNUM_SIGN_BIT);
208}
209
210static inline size_t
211BIGNUM_LEN(VALUE b)
212{
213 if (! BIGNUM_EMBED_P(b)) {
214 return RBIGNUM(b)->as.heap.len;
215 }
216 else {
217 size_t ret = RBASIC(b)->flags;
218 ret &= BIGNUM_EMBED_LEN_MASK;
219 ret >>= BIGNUM_EMBED_LEN_SHIFT;
220 return ret;
221 }
222}
223
224static inline int
225BIGNUM_LENINT(VALUE b)
226{
227 return rb_long2int(BIGNUM_LEN(b));
228}
229
230/* LSB:BIGNUM_DIGITS(b)[0], MSB:BIGNUM_DIGITS(b)[BIGNUM_LEN(b)-1] */
231static inline BDIGIT *
232BIGNUM_DIGITS(VALUE b)
233{
234 if (BIGNUM_EMBED_P(b)) {
235 return RBIGNUM(b)->as.ary;
236 }
237 else {
238 return RBIGNUM(b)->as.heap.digits;
239 }
240}
241
242static inline bool
243BIGNUM_EMBED_P(VALUE b)
244{
245 return FL_TEST_RAW(b, BIGNUM_EMBED_FLAG);
246}
247
248#endif /* INTERNAL_BIGNUM_H */
#define LONG_LONG
Definition long_long.h:38
#define FL_UNSET_RAW
Old name of RB_FL_UNSET_RAW.
Definition fl_type.h:130
#define FL_REVERSE_RAW
Old name of RB_FL_REVERSE_RAW.
Definition fl_type.h:124
#define FL_TEST_RAW
Old name of RB_FL_TEST_RAW.
Definition fl_type.h:128
#define FL_SET_RAW
Old name of RB_FL_SET_RAW.
Definition fl_type.h:126
int len
Length of the buffer.
Definition io.h:8
#define rb_long2int
Just another name of rb_long2int_inline.
Definition long.h:62
#define RBASIC(obj)
Convenient casting macro.
Definition rbasic.h:40
C99 shim for <stdbool.h>
Ruby object's base components.
Definition rbasic.h:69
uintptr_t VALUE
Type that represents a Ruby object.
Definition value.h:40