Ruby 4.1.0dev (2026-09-06 revision e536482403c19e68366c359a593e99e9514758d8)
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_bit_count(VALUE big);
127VALUE rb_big_remainder(VALUE x, VALUE y);
128VALUE rb_big_gt(VALUE x, VALUE y);
129VALUE rb_big_ge(VALUE x, VALUE y);
130VALUE rb_big_lt(VALUE x, VALUE y);
131VALUE rb_big_le(VALUE x, VALUE y);
132VALUE rb_int_powm(int const argc, VALUE * const argv, VALUE const num);
133VALUE rb_big_isqrt(VALUE n);
134static inline bool BIGNUM_SIGN(VALUE b);
135static inline bool BIGNUM_POSITIVE_P(VALUE b);
136static inline bool BIGNUM_NEGATIVE_P(VALUE b);
137static inline void BIGNUM_SET_SIGN(VALUE b, bool sign);
138static inline void BIGNUM_NEGATE(VALUE b);
139static inline size_t BIGNUM_LEN(VALUE b);
140static inline BDIGIT *BIGNUM_DIGITS(VALUE b);
141static inline int BIGNUM_LENINT(VALUE b);
142static inline bool BIGNUM_EMBED_P(VALUE b);
143
144RUBY_SYMBOL_EXPORT_BEGIN
145/* bignum.c (export) */
146VALUE rb_big_mul_normal(VALUE x, VALUE y);
147VALUE rb_big_mul_balance(VALUE x, VALUE y);
148VALUE rb_big_mul_karatsuba(VALUE x, VALUE y);
149VALUE rb_big_mul_toom3(VALUE x, VALUE y);
150VALUE rb_big_sq_fast(VALUE x);
151VALUE rb_big_divrem_normal(VALUE x, VALUE y);
152VALUE rb_big2str_poweroftwo(VALUE x, int base);
153VALUE rb_big2str_generic(VALUE x, int base);
154VALUE rb_str2big_poweroftwo(VALUE arg, int base, int badcheck);
155VALUE rb_str2big_normal(VALUE arg, int base, int badcheck);
156VALUE rb_str2big_karatsuba(VALUE arg, int base, int badcheck);
157#if defined(HAVE_LIBGMP) && defined(HAVE_GMP_H)
158VALUE rb_big_mul_gmp(VALUE x, VALUE y);
159VALUE rb_big_divrem_gmp(VALUE x, VALUE y);
160VALUE rb_big2str_gmp(VALUE x, int base);
161VALUE rb_str2big_gmp(VALUE arg, int base, int badcheck);
162#endif
163RUBY_SYMBOL_EXPORT_END
164
165#if HAVE_LONG_LONG
166VALUE rb_ull2big(unsigned LONG_LONG n);
167VALUE rb_ll2big(LONG_LONG n);
168#endif
169
170#if defined(HAVE_INT128_T)
171VALUE rb_uint128t2big(uint128_t n);
172VALUE rb_int128t2big(int128_t n);
173#endif
174
175/* sign: positive:1, negative:0 */
176static inline bool
177BIGNUM_SIGN(VALUE b)
178{
179 return FL_TEST_RAW(b, BIGNUM_SIGN_BIT);
180}
181
182static inline bool
183BIGNUM_POSITIVE_P(VALUE b)
184{
185 return BIGNUM_SIGN(b);
186}
187
188static inline bool
189BIGNUM_NEGATIVE_P(VALUE b)
190{
191 return ! BIGNUM_POSITIVE_P(b);
192}
193
194static inline void
195BIGNUM_SET_SIGN(VALUE b, bool sign)
196{
197 if (sign) {
198 FL_SET_RAW(b, BIGNUM_SIGN_BIT);
199 }
200 else {
201 FL_UNSET_RAW(b, BIGNUM_SIGN_BIT);
202 }
203}
204
205static inline void
206BIGNUM_NEGATE(VALUE b)
207{
208 FL_REVERSE_RAW(b, BIGNUM_SIGN_BIT);
209}
210
211static inline size_t
212BIGNUM_LEN(VALUE b)
213{
214 if (! BIGNUM_EMBED_P(b)) {
215 return RBIGNUM(b)->as.heap.len;
216 }
217 else {
218 size_t ret = RBASIC(b)->flags;
219 ret &= BIGNUM_EMBED_LEN_MASK;
220 ret >>= BIGNUM_EMBED_LEN_SHIFT;
221 return ret;
222 }
223}
224
225static inline int
226BIGNUM_LENINT(VALUE b)
227{
228 return rb_long2int(BIGNUM_LEN(b));
229}
230
231/* LSB:BIGNUM_DIGITS(b)[0], MSB:BIGNUM_DIGITS(b)[BIGNUM_LEN(b)-1] */
232static inline BDIGIT *
233BIGNUM_DIGITS(VALUE b)
234{
235 if (BIGNUM_EMBED_P(b)) {
236 return RBIGNUM(b)->as.ary;
237 }
238 else {
239 return RBIGNUM(b)->as.heap.digits;
240 }
241}
242
243static inline bool
244BIGNUM_EMBED_P(VALUE b)
245{
246 return FL_TEST_RAW(b, BIGNUM_EMBED_FLAG);
247}
248
249#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