1 #ifndef INTERNAL_BIGNUM_H
2 #define INTERNAL_BIGNUM_H
11 #include "ruby/internal/config.h"
14 #ifdef HAVE_SYS_TYPES_H
15 # include <sys/types.h>
22 # if SIZEOF_INT*2 <= SIZEOF_LONG_LONG
23 # define BDIGIT unsigned int
24 # define SIZEOF_BDIGIT SIZEOF_INT
25 # define BDIGIT_DBL unsigned LONG_LONG
26 # define BDIGIT_DBL_SIGNED LONG_LONG
27 # define PRI_BDIGIT_PREFIX ""
28 # define PRI_BDIGIT_DBL_PREFIX PRI_LL_PREFIX
29 # elif SIZEOF_INT*2 <= SIZEOF_LONG
30 # define BDIGIT unsigned int
31 # define SIZEOF_BDIGIT SIZEOF_INT
32 # define BDIGIT_DBL unsigned long
33 # define BDIGIT_DBL_SIGNED long
34 # define PRI_BDIGIT_PREFIX ""
35 # define PRI_BDIGIT_DBL_PREFIX "l"
36 # elif SIZEOF_SHORT*2 <= SIZEOF_LONG
37 # define BDIGIT unsigned short
38 # define SIZEOF_BDIGIT SIZEOF_SHORT
39 # define BDIGIT_DBL unsigned long
40 # define BDIGIT_DBL_SIGNED long
41 # define PRI_BDIGIT_PREFIX "h"
42 # define PRI_BDIGIT_DBL_PREFIX "l"
44 # define BDIGIT unsigned short
45 # define SIZEOF_BDIGIT (SIZEOF_LONG/2)
46 # define SIZEOF_ACTUAL_BDIGIT SIZEOF_LONG
47 # define BDIGIT_DBL unsigned long
48 # define BDIGIT_DBL_SIGNED long
49 # define PRI_BDIGIT_PREFIX "h"
50 # define PRI_BDIGIT_DBL_PREFIX "l"
54 #ifndef SIZEOF_ACTUAL_BDIGIT
55 # define SIZEOF_ACTUAL_BDIGIT SIZEOF_BDIGIT
58 #ifdef PRI_BDIGIT_PREFIX
59 # define PRIdBDIGIT PRI_BDIGIT_PREFIX"d"
60 # define PRIiBDIGIT PRI_BDIGIT_PREFIX"i"
61 # define PRIoBDIGIT PRI_BDIGIT_PREFIX"o"
62 # define PRIuBDIGIT PRI_BDIGIT_PREFIX"u"
63 # define PRIxBDIGIT PRI_BDIGIT_PREFIX"x"
64 # define PRIXBDIGIT PRI_BDIGIT_PREFIX"X"
67 #ifdef PRI_BDIGIT_DBL_PREFIX
68 # define PRIdBDIGIT_DBL PRI_BDIGIT_DBL_PREFIX"d"
69 # define PRIiBDIGIT_DBL PRI_BDIGIT_DBL_PREFIX"i"
70 # define PRIoBDIGIT_DBL PRI_BDIGIT_DBL_PREFIX"o"
71 # define PRIuBDIGIT_DBL PRI_BDIGIT_DBL_PREFIX"u"
72 # define PRIxBDIGIT_DBL PRI_BDIGIT_DBL_PREFIX"x"
73 # define PRIXBDIGIT_DBL PRI_BDIGIT_DBL_PREFIX"X"
76 #define RBIGNUM(obj) ((struct RBignum *)(obj))
77 #define BIGNUM_SIGN_BIT FL_USER1
78 #define BIGNUM_EMBED_FLAG ((VALUE)FL_USER2)
79 #define BIGNUM_EMBED_LEN_NUMBITS 3
80 #define BIGNUM_EMBED_LEN_MASK \
81 (~(~(VALUE)0U << BIGNUM_EMBED_LEN_NUMBITS) << BIGNUM_EMBED_LEN_SHIFT)
82 #define BIGNUM_EMBED_LEN_SHIFT \
84 #ifndef BIGNUM_EMBED_LEN_MAX
85 # if (SIZEOF_VALUE*RBIMPL_RVALUE_EMBED_LEN_MAX/SIZEOF_ACTUAL_BDIGIT) < (1 << BIGNUM_EMBED_LEN_NUMBITS)-1
86 # define BIGNUM_EMBED_LEN_MAX (SIZEOF_VALUE*RBIMPL_RVALUE_EMBED_LEN_MAX/SIZEOF_ACTUAL_BDIGIT)
88 # define BIGNUM_EMBED_LEN_MAX ((1 << BIGNUM_EMBED_LEN_NUMBITS)-1)
92 enum rb_int_parse_flags {
93 RB_INT_PARSE_SIGN = 0x01,
94 RB_INT_PARSE_UNDERSCORE = 0x02,
95 RB_INT_PARSE_PREFIX = 0x04,
96 RB_INT_PARSE_ALL = 0x07,
97 RB_INT_PARSE_DEFAULT = 0x07,
107 BDIGIT ary[BIGNUM_EMBED_LEN_MAX];
112 extern const char ruby_digitmap[];
118 size_t rb_big_size(
VALUE);
121 VALUE rb_str_convert_to_inum(
VALUE str,
int base,
int badcheck,
int raise_exception);
132 VALUE rb_int_powm(
int const argc,
VALUE *
const argv,
VALUE const num);
134 static inline bool BIGNUM_SIGN(
VALUE b);
135 static inline bool BIGNUM_POSITIVE_P(
VALUE b);
136 static inline bool BIGNUM_NEGATIVE_P(
VALUE b);
137 static inline void BIGNUM_SET_SIGN(
VALUE b,
bool sign);
138 static inline void BIGNUM_NEGATE(
VALUE b);
139 static inline size_t BIGNUM_LEN(
VALUE b);
140 static inline BDIGIT *BIGNUM_DIGITS(
VALUE b);
141 static inline int BIGNUM_LENINT(
VALUE b);
142 static inline bool BIGNUM_EMBED_P(
VALUE b);
144 RUBY_SYMBOL_EXPORT_BEGIN
152 VALUE rb_big2str_poweroftwo(
VALUE x,
int base);
154 VALUE rb_str2big_poweroftwo(
VALUE arg,
int base,
int badcheck);
155 VALUE rb_str2big_normal(
VALUE arg,
int base,
int badcheck);
156 VALUE rb_str2big_karatsuba(
VALUE arg,
int base,
int badcheck);
157 #if defined(HAVE_LIBGMP) && defined(HAVE_GMP_H)
161 VALUE rb_str2big_gmp(
VALUE arg,
int base,
int badcheck);
163 VALUE rb_int_parse_cstr(
const char *str, ssize_t
len,
char **endp,
size_t *ndigits,
int base,
int flags);
164 RUBY_SYMBOL_EXPORT_END
166 #if defined(HAVE_INT128_T)
167 VALUE rb_int128t2big(int128_t n);
178 BIGNUM_POSITIVE_P(
VALUE b)
180 return BIGNUM_SIGN(b);
184 BIGNUM_NEGATIVE_P(
VALUE b)
186 return ! BIGNUM_POSITIVE_P(b);
190 BIGNUM_SET_SIGN(
VALUE b,
bool sign)
201 BIGNUM_NEGATE(
VALUE b)
209 if (! BIGNUM_EMBED_P(b)) {
210 return RBIGNUM(b)->as.heap.len;
213 size_t ret =
RBASIC(b)->flags;
214 ret &= BIGNUM_EMBED_LEN_MASK;
215 ret >>= BIGNUM_EMBED_LEN_SHIFT;
221 BIGNUM_LENINT(
VALUE b)
227 static inline BDIGIT *
228 BIGNUM_DIGITS(
VALUE b)
230 if (BIGNUM_EMBED_P(b)) {
231 return RBIGNUM(b)->as.ary;
234 return RBIGNUM(b)->as.heap.digits;
239 BIGNUM_EMBED_P(
VALUE b)
#define FL_UNSET_RAW
Old name of RB_FL_UNSET_RAW.
#define FL_REVERSE_RAW
Old name of RB_FL_REVERSE_RAW.
#define FL_TEST_RAW
Old name of RB_FL_TEST_RAW.
#define FL_SET_RAW
Old name of RB_FL_SET_RAW.
int len
Length of the buffer.
#define rb_long2int
Just another name of rb_long2int_inline.
#define RBASIC(obj)
Convenient casting macro.
Ruby object's base components.
uintptr_t VALUE
Type that represents a Ruby object.