1#ifndef INTERNAL_BIGNUM_H
2#define INTERNAL_BIGNUM_H
11#include "ruby/internal/config.h"
12#include "internal/compilers.h"
15#ifdef HAVE_SYS_TYPES_H
16# include <sys/types.h>
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"
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"
55#ifndef SIZEOF_ACTUAL_BDIGIT
56# define SIZEOF_ACTUAL_BDIGIT SIZEOF_BDIGIT
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"
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"
77#define RBIGNUM(obj) ((struct RBignum *)(obj))
78#define BIGNUM_SIGN_BIT FL_USER1
79#define BIGNUM_EMBED_FLAG ((VALUE)FL_USER2)
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 \
90#define BIGNUM_EMBED_LEN_MAX (BIGNUM_EMBED_LEN_MASK >> BIGNUM_EMBED_LEN_SHIFT)
92enum 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,
117extern const char ruby_digitmap[];
123size_t rb_big_size(
VALUE);
126VALUE rb_str_convert_to_inum(
VALUE str,
int base,
int badcheck,
int raise_exception);
140static inline bool BIGNUM_SIGN(
VALUE b);
141static inline bool BIGNUM_POSITIVE_P(
VALUE b);
142static inline bool BIGNUM_NEGATIVE_P(
VALUE b);
143static inline void BIGNUM_SET_SIGN(
VALUE b,
bool sign);
144static inline void BIGNUM_NEGATE(
VALUE b);
145static inline size_t BIGNUM_LEN(
VALUE b);
146static inline BDIGIT *BIGNUM_DIGITS(
VALUE b);
147static inline int BIGNUM_LENINT(
VALUE b);
148static inline bool BIGNUM_EMBED_P(
VALUE b);
150RUBY_SYMBOL_EXPORT_BEGIN
158VALUE rb_big2str_poweroftwo(
VALUE x,
int base);
160VALUE rb_str2big_poweroftwo(
VALUE arg,
int base,
int badcheck);
161VALUE rb_str2big_normal(
VALUE arg,
int base,
int badcheck);
162VALUE rb_str2big_karatsuba(
VALUE arg,
int base,
int badcheck);
163#if defined(HAVE_LIBGMP) && defined(HAVE_GMP_H)
167VALUE rb_str2big_gmp(
VALUE arg,
int base,
int badcheck);
169VALUE rb_int_parse_cstr(
const char *str, ssize_t
len,
char **endp,
size_t *ndigits,
int base,
int flags);
170RUBY_SYMBOL_EXPORT_END
177#if defined(HAVE_INT128_T)
178VALUE rb_uint128t2big(uint128_t n);
179VALUE rb_int128t2big(int128_t n);
190BIGNUM_POSITIVE_P(
VALUE b)
192 return BIGNUM_SIGN(b);
196BIGNUM_NEGATIVE_P(
VALUE b)
198 return ! BIGNUM_POSITIVE_P(b);
202BIGNUM_SET_SIGN(
VALUE b,
bool sign)
213BIGNUM_NEGATE(
VALUE b)
221 if (! BIGNUM_EMBED_P(b)) {
222 return RBIGNUM(b)->as.heap.len;
225 size_t ret =
RBASIC(b)->flags;
226 ret &= BIGNUM_EMBED_LEN_MASK;
227 ret >>= BIGNUM_EMBED_LEN_SHIFT;
233BIGNUM_LENINT(
VALUE b)
239static inline BDIGIT *
240BIGNUM_DIGITS(
VALUE b)
242 if (BIGNUM_EMBED_P(b)) {
243 return RBIGNUM(b)->as.ary;
246 return RBIGNUM(b)->as.heap.digits;
251BIGNUM_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.