Ruby  2.5.0dev(2017-10-22revision60238)
Data Structures | Macros | Typedefs | Enumerations | Functions | Variables
bignum.c File Reference
#include "internal.h"
#include "ruby/thread.h"
#include "ruby/util.h"
#include "id.h"
#include <math.h>
#include <float.h>
#include <ctype.h>
#include "ruby_assert.h"

Go to the source code of this file.

Data Structures

struct  big_div_struct
 
struct  big2str_struct
 

Macros

#define RB_BIGNUM_TYPE_P(x)   RB_TYPE_P((x), T_BIGNUM)
 
#define SIZEOF_BDIGIT_DBL   SIZEOF_LONG_LONG
 
#define HOST_BIGENDIAN_P   0
 
#define ALIGNOF(type)   ((int)offsetof(struct { char f1; type f2; }, f2))
 
#define LSHIFTABLE(d, n)   ((n) < sizeof(d) * CHAR_BIT)
 
#define LSHIFTX(d, n)   (!LSHIFTABLE(d, n) ? 0 : ((d) << (!LSHIFTABLE(d, n) ? 0 : (n))))
 
#define CLEAR_LOWBITS(d, numbits)   ((d) & LSHIFTX(~((d)*0), (numbits)))
 
#define FILL_LOWBITS(d, numbits)   ((d) | (LSHIFTX(((d)*0+1), (numbits))-1))
 
#define POW2_P(x)   (((x)&((x)-1))==0)
 
#define BDIGITS(x)   (BIGNUM_DIGITS(x))
 
#define BITSPERDIG   (SIZEOF_BDIGIT*CHAR_BIT)
 
#define BIGRAD   ((BDIGIT_DBL)1 << BITSPERDIG)
 
#define BIGRAD_HALF   ((BDIGIT)(BIGRAD >> 1))
 
#define BDIGIT_MSB(d)   (((d) & BIGRAD_HALF) != 0)
 
#define BIGUP(x)   LSHIFTX(((x) + (BDIGIT_DBL)0), BITSPERDIG)
 
#define BIGDN(x)   RSHIFT((x),BITSPERDIG)
 
#define BIGLO(x)   ((BDIGIT)((x) & BDIGMAX))
 
#define BDIGMAX   ((BDIGIT)(BIGRAD-1))
 
#define BDIGIT_DBL_MAX   (~(BDIGIT_DBL)0)
 
#define BIGZEROP(x)
 
#define BIGSIZE(x)
 
#define BIGDIVREM_EXTRA_WORDS   1
 
#define bdigit_roomof(n)   roomof(n, SIZEOF_BDIGIT)
 
#define BARY_ARGS(ary)   ary, numberof(ary)
 
#define BARY_ADD(z, x, y)   bary_add(BARY_ARGS(z), BARY_ARGS(x), BARY_ARGS(y))
 
#define BARY_SUB(z, x, y)   bary_sub(BARY_ARGS(z), BARY_ARGS(x), BARY_ARGS(y))
 
#define BARY_SHORT_MUL(z, x, y)   bary_short_mul(BARY_ARGS(z), BARY_ARGS(x), BARY_ARGS(y))
 
#define BARY_DIVMOD(q, r, x, y)   bary_divmod(BARY_ARGS(q), BARY_ARGS(r), BARY_ARGS(x), BARY_ARGS(y))
 
#define BARY_ZERO_P(x)   bary_zero_p(BARY_ARGS(x))
 
#define BIGNUM_SET_NEGATIVE_SIGN(b)   BIGNUM_SET_SIGN(b, 0)
 
#define BIGNUM_SET_POSITIVE_SIGN(b)   BIGNUM_SET_SIGN(b, 1)
 
#define bignew(len, sign)   bignew_1(rb_cInteger,(len),(sign))
 
#define BDIGITS_ZERO(ptr, n)
 
#define BARY_TRUNC(ds, n)
 
#define KARATSUBA_BALANCED(xn, yn)   ((yn)/2 < (xn))
 
#define TOOM3_BALANCED(xn, yn)   (((yn)+2)/3 * 2 < (xn))
 
#define GMP_MUL_DIGITS   20
 
#define KARATSUBA_MUL_DIGITS   70
 
#define TOOM3_MUL_DIGITS   150
 
#define GMP_DIV_DIGITS   20
 
#define GMP_BIG2STR_DIGITS   20
 
#define GMP_STR2BIG_DIGITS   20
 
#define NAIVE_MUL_DIGITS   KARATSUBA_MUL_DIGITS
 
#define U16(a)   ((uint16_t)(a))
 
#define U32(a)   ((uint32_t)(a))
 
#define INTEGER_PACK_WORDORDER_MASK
 
#define INTEGER_PACK_BYTEORDER_MASK
 
#define FILL_DD   integer_pack_fill_dd(&dp, &de, &dd, &numbits_in_dd)
 
#define TAKE_LOWBITS(n)   integer_pack_take_lowbits(n, &dd, &numbits_in_dd)
 
#define PUSH_BITS(data, numbits)   integer_unpack_push_bits(data, numbits, &dd, &numbits_in_dd, &dp)
 
#define BIGNUM_DEBUG   0
 
#define ON_DEBUG(x)
 
#define BIGNUM_SET_LEN(b, l)
 
#define conv_digit(c)   (ruby_digit36_to_number_table[(unsigned char)(c)])
 
#define ADV(n)
 
#define ASSERT_LEN()
 
#define MAX_BASE36_POWER_TABLE_ENTRIES   (SIZEOF_SIZE_T * CHAR_BIT + 1)
 
#define rb_bdigit_dbl_isqrt(x)   (BDIGIT)rb_ulong_isqrt(x)
 
#define BDIGIT_DBL_TO_DOUBLE(n)   (double)(n)
 

Typedefs

typedef void() mulfunc_t(BDIGIT *zds, size_t zn, const BDIGIT *xds, size_t xn, const BDIGIT *yds, size_t yn, BDIGIT *wds, size_t wn)
 

Enumerations

enum  big_op_t { big_op_gt, big_op_ge, big_op_lt, big_op_le }
 
enum  { DBL_BIGDIG = ((DBL_MANT_DIG + BITSPERDIG) / BITSPERDIG) }
 

Functions

 STATIC_ASSERT (sizeof_bdigit_dbl, sizeof(BDIGIT_DBL)==SIZEOF_BDIGIT_DBL)
 
 STATIC_ASSERT (sizeof_bdigit_dbl_signed, sizeof(BDIGIT_DBL_SIGNED)==SIZEOF_BDIGIT_DBL)
 
 STATIC_ASSERT (sizeof_bdigit, SIZEOF_BDIGIT<=sizeof(BDIGIT))
 
 STATIC_ASSERT (sizeof_bdigit_and_dbl, SIZEOF_BDIGIT *2<=SIZEOF_BDIGIT_DBL)
 
 STATIC_ASSERT (bdigit_signedness, 0<(BDIGIT) -1)
 
 STATIC_ASSERT (bdigit_dbl_signedness, 0<(BDIGIT_DBL) -1)
 
 STATIC_ASSERT (bdigit_dbl_signed_signedness, 0 >(BDIGIT_DBL_SIGNED) -1)
 
 STATIC_ASSERT (rbignum_embed_len_max, BIGNUM_EMBED_LEN_MAX<=(BIGNUM_EMBED_LEN_MASK >> BIGNUM_EMBED_LEN_SHIFT))
 
 STATIC_ASSERT (sizeof_long_and_sizeof_bdigit, SIZEOF_BDIGIT % SIZEOF_LONG==0)
 
VALUE rb_big_mul_normal (VALUE x, VALUE y)
 
VALUE rb_big_sq_fast (VALUE x)
 
VALUE rb_big_mul_balance (VALUE x, VALUE y)
 
VALUE rb_big_mul_karatsuba (VALUE x, VALUE y)
 
VALUE rb_big_mul_toom3 (VALUE x, VALUE y)
 
VALUE rb_big_divrem_normal (VALUE x, VALUE y)
 
int rb_bigzero_p (VALUE x)
 
int rb_cmpint (VALUE val, VALUE a, VALUE b)
 
void rb_big_resize (VALUE big, size_t len)
 
VALUE rb_big_new (size_t len, int sign)
 
VALUE rb_big_clone (VALUE x)
 
void rb_big_2comp (VALUE x)
 
VALUE rb_big_norm (VALUE x)
 
VALUE rb_uint2big (VALUE n)
 
VALUE rb_int2big (SIGNED_VALUE n)
 
VALUE rb_uint2inum (VALUE n)
 
VALUE rb_int2inum (SIGNED_VALUE n)
 
void rb_big_pack (VALUE val, unsigned long *buf, long num_longs)
 
VALUE rb_big_unpack (unsigned long *buf, long num_longs)
 
size_t rb_absint_size (VALUE val, int *nlz_bits_ret)
 
size_t rb_absint_numwords (VALUE val, size_t word_numbits, size_t *nlz_bits_ret)
 
int rb_absint_singlebit_p (VALUE val)
 
int rb_integer_pack (VALUE val, void *words, size_t numwords, size_t wordsize, size_t nails, int flags)
 
VALUE rb_integer_unpack (const void *words, size_t numwords, size_t wordsize, size_t nails, int flags)
 
 NORETURN (static inline void invalid_radix(int base))
 
 NORETURN (static inline void invalid_integer(VALUE s))
 
VALUE rb_cstr_to_inum (const char *str, int base, int badcheck)
 
VALUE rb_int_parse_cstr (const char *str, ssize_t len, char **endp, size_t *ndigits, int base, int flags)
 
VALUE rb_cstr_parse_inum (const char *str, ssize_t len, char **endp, int base)
 
VALUE rb_str_to_inum (VALUE str, int base, int badcheck)
 
VALUE rb_str2big_poweroftwo (VALUE arg, int base, int badcheck)
 
VALUE rb_str2big_normal (VALUE arg, int base, int badcheck)
 
VALUE rb_str2big_karatsuba (VALUE arg, int base, int badcheck)
 
VALUE rb_cstr2inum (const char *str, int base)
 
VALUE rb_str2inum (VALUE str, int base)
 
VALUE rb_big2str_poweroftwo (VALUE x, int base)
 
VALUE rb_big2str_generic (VALUE x, int base)
 
VALUE rb_big2str (VALUE x, int base)
 
unsigned long rb_big2ulong (VALUE x)
 
long rb_big2long (VALUE x)
 
VALUE rb_dbl2big (double d)
 
double rb_big2dbl (VALUE x)
 
VALUE rb_integer_float_cmp (VALUE x, VALUE y)
 
VALUE rb_integer_float_eq (VALUE x, VALUE y)
 
VALUE rb_big_cmp (VALUE x, VALUE y)
 
VALUE rb_big_gt (VALUE x, VALUE y)
 
VALUE rb_big_ge (VALUE x, VALUE y)
 
VALUE rb_big_lt (VALUE x, VALUE y)
 
VALUE rb_big_le (VALUE x, VALUE y)
 
VALUE rb_big_eq (VALUE x, VALUE y)
 
VALUE rb_big_eql (VALUE x, VALUE y)
 
VALUE rb_big_uminus (VALUE x)
 
VALUE rb_big_comp (VALUE x)
 
VALUE rb_big_plus (VALUE x, VALUE y)
 
VALUE rb_big_minus (VALUE x, VALUE y)
 
VALUE rb_big_mul (VALUE x, VALUE y)
 
VALUE rb_big_div (VALUE x, VALUE y)
 
VALUE rb_big_idiv (VALUE x, VALUE y)
 
VALUE rb_big_modulo (VALUE x, VALUE y)
 
VALUE rb_big_remainder (VALUE x, VALUE y)
 
VALUE rb_big_divmod (VALUE x, VALUE y)
 
double rb_big_fdiv_double (VALUE x, VALUE y)
 
VALUE rb_big_fdiv (VALUE x, VALUE y)
 
VALUE rb_big_pow (VALUE x, VALUE y)
 
VALUE rb_big_and (VALUE x, VALUE y)
 
VALUE rb_big_or (VALUE x, VALUE y)
 
VALUE rb_big_xor (VALUE x, VALUE y)
 
VALUE rb_big_lshift (VALUE x, VALUE y)
 
VALUE rb_big_rshift (VALUE x, VALUE y)
 
VALUE rb_big_aref (VALUE x, VALUE y)
 
VALUE rb_big_hash (VALUE x)
 
VALUE rb_big_abs (VALUE x)
 
int rb_big_sign (VALUE x)
 
size_t rb_big_size (VALUE big)
 
VALUE rb_big_size_m (VALUE big)
 
VALUE rb_big_bit_length (VALUE big)
 
VALUE rb_big_odd_p (VALUE num)
 
VALUE rb_big_even_p (VALUE num)
 
unsigned long rb_ulong_isqrt (unsigned long)
 
VALUE rb_big_isqrt (VALUE n)
 
void Init_Bignum (void)
 

Variables

const char ruby_digitmap [] = "0123456789abcdefghijklmnopqrstuvwxyz"
 

Macro Definition Documentation

◆ ADV

#define ADV (   n)
Value:
do {\
if (len > 0 && len <= (n)) goto bad; \
str += (n); \
len -= (n); \
} while (0)
#define bad(x)
Definition: _sdbm.c:124
register unsigned int len
Definition: zonetab.h:51

Referenced by rb_int_parse_cstr().

◆ ALIGNOF

#define ALIGNOF (   type)    ((int)offsetof(struct { char f1; type f2; }, f2))

Definition at line 68 of file bignum.c.

◆ ASSERT_LEN

#define ASSERT_LEN ( )
Value:
do {\
assert(len != 0); \
if (len0 >= 0) assert(s + len0 == str + len); \
} while (0)
register unsigned int len
Definition: zonetab.h:51
#define assert
Definition: ruby_assert.h:37

Referenced by rb_int_parse_cstr().

◆ BARY_ADD

#define BARY_ADD (   z,
  x,
 
)    bary_add(BARY_ARGS(z), BARY_ARGS(x), BARY_ARGS(y))

Definition at line 107 of file bignum.c.

◆ BARY_ARGS

#define BARY_ARGS (   ary)    ary, numberof(ary)

Definition at line 105 of file bignum.c.

◆ BARY_DIVMOD

#define BARY_DIVMOD (   q,
  r,
  x,
 
)    bary_divmod(BARY_ARGS(q), BARY_ARGS(r), BARY_ARGS(x), BARY_ARGS(y))

Definition at line 110 of file bignum.c.

◆ BARY_SHORT_MUL

#define BARY_SHORT_MUL (   z,
  x,
 
)    bary_short_mul(BARY_ARGS(z), BARY_ARGS(x), BARY_ARGS(y))

Definition at line 109 of file bignum.c.

◆ BARY_SUB

#define BARY_SUB (   z,
  x,
 
)    bary_sub(BARY_ARGS(z), BARY_ARGS(x), BARY_ARGS(y))

Definition at line 108 of file bignum.c.

◆ BARY_TRUNC

#define BARY_TRUNC (   ds,
 
)
Value:
do { \
while (0 < (n) && (ds)[(n)-1] == 0) \
(n)--; \
} while (0)

Definition at line 127 of file bignum.c.

Referenced by rb_big_divrem_normal().

◆ BARY_ZERO_P

#define BARY_ZERO_P (   x)    bary_zero_p(BARY_ARGS(x))

Definition at line 111 of file bignum.c.

◆ BDIGIT_DBL_MAX

#define BDIGIT_DBL_MAX   (~(BDIGIT_DBL)0)

Definition at line 85 of file bignum.c.

◆ BDIGIT_DBL_TO_DOUBLE

#define BDIGIT_DBL_TO_DOUBLE (   n)    (double)(n)

Definition at line 6796 of file bignum.c.

◆ BDIGIT_MSB

#define BDIGIT_MSB (   d)    (((d) & BIGRAD_HALF) != 0)

Definition at line 80 of file bignum.c.

◆ bdigit_roomof

#define bdigit_roomof (   n)    roomof(n, SIZEOF_BDIGIT)

◆ BDIGITS

#define BDIGITS (   x)    (BIGNUM_DIGITS(x))

◆ BDIGITS_ZERO

#define BDIGITS_ZERO (   ptr,
 
)
Value:
do { \
BDIGIT *bdigitz_zero_ptr = (ptr); \
size_t bdigitz_zero_n = (n); \
while (bdigitz_zero_n) { \
*bdigitz_zero_ptr++ = 0; \
bdigitz_zero_n--; \
} \
} while (0)

Definition at line 118 of file bignum.c.

◆ BDIGMAX

#define BDIGMAX   ((BDIGIT)(BIGRAD-1))

Definition at line 84 of file bignum.c.

◆ BIGDIVREM_EXTRA_WORDS

#define BIGDIVREM_EXTRA_WORDS   1

Definition at line 103 of file bignum.c.

Referenced by rb_big_divrem_normal().

◆ BIGDN

#define BIGDN (   x)    RSHIFT((x),BITSPERDIG)

Definition at line 82 of file bignum.c.

Referenced by rb_absint_singlebit_p(), rb_absint_size(), rb_integer_pack(), and rb_uint2big().

◆ BIGLO

#define BIGLO (   x)    ((BDIGIT)((x) & BDIGMAX))

Definition at line 83 of file bignum.c.

Referenced by rb_absint_singlebit_p(), rb_absint_size(), rb_integer_pack(), and rb_uint2big().

◆ bignew

#define bignew (   len,
  sign 
)    bignew_1(rb_cInteger,(len),(sign))

◆ BIGNUM_DEBUG

#define BIGNUM_DEBUG   0

Definition at line 2869 of file bignum.c.

◆ BIGNUM_SET_LEN

#define BIGNUM_SET_LEN (   b,
 
)
Value:
((RBASIC(b)->flags & BIGNUM_EMBED_FLAG) ? \
(void)(RBASIC(b)->flags = \
(RBASIC(b)->flags & ~BIGNUM_EMBED_LEN_MASK) | \
((l) << BIGNUM_EMBED_LEN_SHIFT)) : \
(void)(RBIGNUM(b)->as.heap.len = (l)))
#define BIGNUM_EMBED_LEN_MASK
Definition: internal.h:608
#define BIGNUM_EMBED_LEN_SHIFT
Definition: internal.h:609
#define RBIGNUM(obj)
Definition: internal.h:622
#define RBASIC(obj)
Definition: ruby.h:1197
#define BIGNUM_EMBED_FLAG
Definition: internal.h:607

Definition at line 2928 of file bignum.c.

Referenced by rb_uint2big().

◆ BIGNUM_SET_NEGATIVE_SIGN

#define BIGNUM_SET_NEGATIVE_SIGN (   b)    BIGNUM_SET_SIGN(b, 0)

Definition at line 113 of file bignum.c.

Referenced by rb_int2big().

◆ BIGNUM_SET_POSITIVE_SIGN

#define BIGNUM_SET_POSITIVE_SIGN (   b)    BIGNUM_SET_SIGN(b, 1)

Definition at line 114 of file bignum.c.

Referenced by rb_big_abs().

◆ BIGRAD

#define BIGRAD   ((BDIGIT_DBL)1 << BITSPERDIG)

Definition at line 78 of file bignum.c.

◆ BIGRAD_HALF

#define BIGRAD_HALF   ((BDIGIT)(BIGRAD >> 1))

Definition at line 79 of file bignum.c.

◆ BIGSIZE

#define BIGSIZE (   x)
Value:
(BIGNUM_LEN(x) == 0 ? (size_t)0 : \
BDIGITS(x)[BIGNUM_LEN(x)-1] ? \
(size_t)(BIGNUM_LEN(x)*SIZEOF_BDIGIT - nlz(BDIGITS(x)[BIGNUM_LEN(x)-1])/CHAR_BIT) : \
#define BIGNUM_LEN(b)
Definition: internal.h:610
#define SIZEOF_BDIGIT
Definition: internal.h:527
#define BDIGITS(x)
Definition: bignum.c:76
size_t rb_absint_size(VALUE val, int *nlz_bits_ret)
Definition: bignum.c:3229
#define CHAR_BIT
Definition: ruby.h:196
#define NULL
Definition: _sdbm.c:102

Definition at line 98 of file bignum.c.

Referenced by rb_big_size().

◆ BIGUP

#define BIGUP (   x)    LSHIFTX(((x) + (BDIGIT_DBL)0), BITSPERDIG)

Definition at line 81 of file bignum.c.

◆ BIGZEROP

#define BIGZEROP (   x)
Value:
(BIGNUM_LEN(x) == 0 || \
(BDIGITS(x)[0] == 0 && \
(BIGNUM_LEN(x) == 1 || bigzero_p(x))))
#define BIGNUM_LEN(b)
Definition: internal.h:610
#define BDIGITS(x)
Definition: bignum.c:76

Definition at line 95 of file bignum.c.

Referenced by rb_big_pow(), rb_bigzero_p(), and rb_cmpint().

◆ BITSPERDIG

#define BITSPERDIG   (SIZEOF_BDIGIT*CHAR_BIT)

Definition at line 77 of file bignum.c.

◆ CLEAR_LOWBITS

#define CLEAR_LOWBITS (   d,
  numbits 
)    ((d) & LSHIFTX(~((d)*0), (numbits)))

Definition at line 72 of file bignum.c.

◆ conv_digit

#define conv_digit (   c)    (ruby_digit36_to_number_table[(unsigned char)(c)])

Definition at line 3686 of file bignum.c.

◆ FILL_DD

#define FILL_DD   integer_pack_fill_dd(&dp, &de, &dd, &numbits_in_dd)

◆ FILL_LOWBITS

#define FILL_LOWBITS (   d,
  numbits 
)    ((d) | (LSHIFTX(((d)*0+1), (numbits))-1))

Definition at line 73 of file bignum.c.

◆ GMP_BIG2STR_DIGITS

#define GMP_BIG2STR_DIGITS   20

Definition at line 140 of file bignum.c.

◆ GMP_DIV_DIGITS

#define GMP_DIV_DIGITS   20

Definition at line 139 of file bignum.c.

◆ GMP_MUL_DIGITS

#define GMP_MUL_DIGITS   20

Definition at line 135 of file bignum.c.

◆ GMP_STR2BIG_DIGITS

#define GMP_STR2BIG_DIGITS   20

Definition at line 141 of file bignum.c.

◆ HOST_BIGENDIAN_P

#define HOST_BIGENDIAN_P   0

Definition at line 66 of file bignum.c.

◆ INTEGER_PACK_BYTEORDER_MASK

#define INTEGER_PACK_BYTEORDER_MASK
Value:
INTEGER_PACK_LSBYTE_FIRST | \
INTEGER_PACK_NATIVE_BYTE_ORDER)
#define INTEGER_PACK_MSBYTE_FIRST
Definition: intern.h:140

Definition at line 489 of file bignum.c.

◆ INTEGER_PACK_WORDORDER_MASK

#define INTEGER_PACK_WORDORDER_MASK
Value:
INTEGER_PACK_LSWORD_FIRST)
#define INTEGER_PACK_MSWORD_FIRST
Definition: intern.h:138

Definition at line 486 of file bignum.c.

◆ KARATSUBA_BALANCED

#define KARATSUBA_BALANCED (   xn,
  yn 
)    ((yn)/2 < (xn))

Definition at line 132 of file bignum.c.

Referenced by rb_big_mul_karatsuba().

◆ KARATSUBA_MUL_DIGITS

#define KARATSUBA_MUL_DIGITS   70

Definition at line 136 of file bignum.c.

◆ LSHIFTABLE

#define LSHIFTABLE (   d,
 
)    ((n) < sizeof(d) * CHAR_BIT)

Definition at line 70 of file bignum.c.

◆ LSHIFTX

#define LSHIFTX (   d,
 
)    (!LSHIFTABLE(d, n) ? 0 : ((d) << (!LSHIFTABLE(d, n) ? 0 : (n))))

Definition at line 71 of file bignum.c.

◆ MAX_BASE36_POWER_TABLE_ENTRIES

#define MAX_BASE36_POWER_TABLE_ENTRIES   (SIZEOF_SIZE_T * CHAR_BIT + 1)

Definition at line 4618 of file bignum.c.

◆ NAIVE_MUL_DIGITS

#define NAIVE_MUL_DIGITS   KARATSUBA_MUL_DIGITS

Definition at line 145 of file bignum.c.

◆ ON_DEBUG

#define ON_DEBUG (   x)

Definition at line 2891 of file bignum.c.

◆ POW2_P

#define POW2_P (   x)    (((x)&((x)-1))==0)

Definition at line 74 of file bignum.c.

Referenced by rb_absint_singlebit_p().

◆ PUSH_BITS

#define PUSH_BITS (   data,
  numbits 
)    integer_unpack_push_bits(data, numbits, &dd, &numbits_in_dd, &dp)

◆ rb_bdigit_dbl_isqrt

#define rb_bdigit_dbl_isqrt (   x)    (BDIGIT)rb_ulong_isqrt(x)

Definition at line 6793 of file bignum.c.

Referenced by rb_big_even_p(), and rb_big_isqrt().

◆ RB_BIGNUM_TYPE_P

#define RB_BIGNUM_TYPE_P (   x)    RB_TYPE_P((x), T_BIGNUM)

◆ SIZEOF_BDIGIT_DBL

#define SIZEOF_BDIGIT_DBL   SIZEOF_LONG_LONG

Definition at line 42 of file bignum.c.

◆ TAKE_LOWBITS

#define TAKE_LOWBITS (   n)    integer_pack_take_lowbits(n, &dd, &numbits_in_dd)

◆ TOOM3_BALANCED

#define TOOM3_BALANCED (   xn,
  yn 
)    (((yn)+2)/3 * 2 < (xn))

Definition at line 133 of file bignum.c.

Referenced by rb_big_mul_toom3().

◆ TOOM3_MUL_DIGITS

#define TOOM3_MUL_DIGITS   150

Definition at line 137 of file bignum.c.

◆ U16

#define U16 (   a)    ((uint16_t)(a))

Definition at line 174 of file bignum.c.

◆ U32

#define U32 (   a)    ((uint32_t)(a))

Definition at line 175 of file bignum.c.

Typedef Documentation

◆ mulfunc_t

typedef void() mulfunc_t(BDIGIT *zds, size_t zn, const BDIGIT *xds, size_t xn, const BDIGIT *yds, size_t yn, BDIGIT *wds, size_t wn)

Definition at line 148 of file bignum.c.

Enumeration Type Documentation

◆ anonymous enum

anonymous enum
Enumerator
DBL_BIGDIG 

Definition at line 6105 of file bignum.c.

◆ big_op_t

enum big_op_t
Enumerator
big_op_gt 
big_op_ge 
big_op_lt 
big_op_le 

Definition at line 5394 of file bignum.c.

Function Documentation

◆ Init_Bignum()

void Init_Bignum ( void  )

◆ NORETURN() [1/2]

NORETURN ( static inline void   invalid_radixint base)

◆ NORETURN() [2/2]

NORETURN ( static inline void   invalid_integerVALUE s)

◆ rb_absint_numwords()

size_t rb_absint_numwords ( VALUE  val,
size_t  word_numbits,
size_t *  nlz_bits_ret 
)

Definition at line 3364 of file bignum.c.

References CHAR_BIT, rb_absint_size(), and SIZE_MAX.

◆ rb_absint_singlebit_p()

int rb_absint_singlebit_p ( VALUE  val)

Definition at line 3428 of file bignum.c.

References BDIGIT, bdigit_roomof, BDIGITS, BIGDN, BIGLO, BIGNUM_LEN, dp, FIX2LONG, FIXNUM_P, numberof, POW2_P, and rb_to_int().

Referenced by rb_big_bit_length().

◆ rb_absint_size()

size_t rb_absint_size ( VALUE  val,
int *  nlz_bits_ret 
)

◆ rb_big2dbl()

double rb_big2dbl ( VALUE  x)

Definition at line 5270 of file bignum.c.

Referenced by rb_big_mul(), and rb_float_pow().

◆ rb_big2long()

long rb_big2long ( VALUE  x)

Definition at line 5108 of file bignum.c.

Referenced by rb_num2long().

◆ rb_big2str()

VALUE rb_big2str ( VALUE  x,
int  base 
)

Definition at line 5062 of file bignum.c.

Referenced by rb_int2str().

◆ rb_big2str_generic()

VALUE rb_big2str_generic ( VALUE  x,
int  base 
)

Definition at line 4975 of file bignum.c.

◆ rb_big2str_poweroftwo()

VALUE rb_big2str_poweroftwo ( VALUE  x,
int  base 
)

Definition at line 4892 of file bignum.c.

◆ rb_big2ulong()

unsigned long rb_big2ulong ( VALUE  x)

Definition at line 5093 of file bignum.c.

Referenced by rb_num_to_uint().

◆ rb_big_2comp()

void rb_big_2comp ( VALUE  x)

Definition at line 3031 of file bignum.c.

◆ rb_big_abs()

VALUE rb_big_abs ( VALUE  x)

Definition at line 6700 of file bignum.c.

References BIGNUM_NEGATIVE_P, BIGNUM_SET_POSITIVE_SIGN, and rb_big_clone().

◆ rb_big_and()

VALUE rb_big_and ( VALUE  x,
VALUE  y 
)

◆ rb_big_aref()

VALUE rb_big_aref ( VALUE  x,
VALUE  y 
)

Definition at line 6619 of file bignum.c.

References BDIGIT.

◆ rb_big_bit_length()

VALUE rb_big_bit_length ( VALUE  big)

◆ rb_big_clone()

VALUE rb_big_clone ( VALUE  x)

Definition at line 3002 of file bignum.c.

References BIGNUM_LEN.

Referenced by rb_big_abs(), rb_big_comp(), and rb_big_uminus().

◆ rb_big_cmp()

VALUE rb_big_cmp ( VALUE  x,
VALUE  y 
)

Definition at line 5367 of file bignum.c.

References FIXNUM_P.

Referenced by rb_integer_float_cmp().

◆ rb_big_comp()

VALUE rb_big_comp ( VALUE  x)

Definition at line 5512 of file bignum.c.

References BDIGIT, BDIGITS, BIGNUM_LEN, BIGNUM_POSITIVE_P, INT2FIX, and rb_big_clone().

◆ rb_big_div()

VALUE rb_big_div ( VALUE  x,
VALUE  y 
)

Definition at line 6036 of file bignum.c.

◆ rb_big_divmod()

VALUE rb_big_divmod ( VALUE  x,
VALUE  y 
)

Definition at line 6080 of file bignum.c.

References div, FIX2LONG, FIXNUM_P, mod, RB_BIGNUM_TYPE_P, rb_int2big(), rb_intern, and rb_num_coerce_bin().

◆ rb_big_divrem_normal()

VALUE rb_big_divrem_normal ( VALUE  x,
VALUE  y 
)

◆ rb_big_eq()

VALUE rb_big_eq ( VALUE  x,
VALUE  y 
)

Definition at line 5472 of file bignum.c.

References FIXNUM_P.

Referenced by rb_integer_float_eq().

◆ rb_big_eql()

VALUE rb_big_eql ( VALUE  x,
VALUE  y 
)

Definition at line 5492 of file bignum.c.

References BDIGIT, BDIGITS, BIGNUM_LEN, BIGNUM_SIGN, MEMCMP, Qfalse, Qtrue, and RB_BIGNUM_TYPE_P.

◆ rb_big_even_p()

VALUE rb_big_even_p ( VALUE  num)

Definition at line 6778 of file bignum.c.

References BDIGIT, BDIGIT_DBL, BDIGITS, BIGNUM_LEN, Qfalse, Qtrue, rb_bdigit_dbl_isqrt, and rb_ulong_isqrt().

◆ rb_big_fdiv()

VALUE rb_big_fdiv ( VALUE  x,
VALUE  y 
)

Definition at line 6183 of file bignum.c.

References DBL2NUM, and rb_big_fdiv_double().

◆ rb_big_fdiv_double()

double rb_big_fdiv_double ( VALUE  x,
VALUE  y 
)

Definition at line 6154 of file bignum.c.

Referenced by rb_big_fdiv().

◆ rb_big_ge()

VALUE rb_big_ge ( VALUE  x,
VALUE  y 
)

Definition at line 5443 of file bignum.c.

◆ rb_big_gt()

VALUE rb_big_gt ( VALUE  x,
VALUE  y 
)

Definition at line 5437 of file bignum.c.

◆ rb_big_hash()

VALUE rb_big_hash ( VALUE  x)

Definition at line 6664 of file bignum.c.

◆ rb_big_idiv()

VALUE rb_big_idiv ( VALUE  x,
VALUE  y 
)

Definition at line 6042 of file bignum.c.

◆ rb_big_isqrt()

VALUE rb_big_isqrt ( VALUE  n)

Definition at line 6840 of file bignum.c.

References BDIGIT, BDIGITS, BIGNUM_LEN, and rb_bdigit_dbl_isqrt.

◆ rb_big_le()

VALUE rb_big_le ( VALUE  x,
VALUE  y 
)

Definition at line 5455 of file bignum.c.

◆ rb_big_lshift()

VALUE rb_big_lshift ( VALUE  x,
VALUE  y 
)

Definition at line 6559 of file bignum.c.

References FIX2LONG, and FIXNUM_P.

◆ rb_big_lt()

VALUE rb_big_lt ( VALUE  x,
VALUE  y 
)

Definition at line 5449 of file bignum.c.

◆ rb_big_minus()

VALUE rb_big_minus ( VALUE  x,
VALUE  y 
)

Definition at line 5801 of file bignum.c.

References BIGNUM_SIGN, FIX2LONG, and FIXNUM_P.

Referenced by rb_int_pred().

◆ rb_big_modulo()

VALUE rb_big_modulo ( VALUE  x,
VALUE  y 
)

Definition at line 6048 of file bignum.c.

References FIX2LONG, FIXNUM_P, RB_BIGNUM_TYPE_P, rb_int2big(), and rb_num_coerce_bin().

◆ rb_big_mul()

VALUE rb_big_mul ( VALUE  x,
VALUE  y 
)

◆ rb_big_mul_balance()

VALUE rb_big_mul_balance ( VALUE  x,
VALUE  y 
)

Definition at line 1671 of file bignum.c.

References bignew, BIGNUM_LEN, and BIGNUM_SIGN.

◆ rb_big_mul_karatsuba()

VALUE rb_big_mul_karatsuba ( VALUE  x,
VALUE  y 
)

Definition at line 1852 of file bignum.c.

References bignew, BIGNUM_LEN, BIGNUM_SIGN, KARATSUBA_BALANCED, rb_eArgError, and rb_raise().

◆ rb_big_mul_normal()

VALUE rb_big_mul_normal ( VALUE  x,
VALUE  y 
)

Definition at line 1543 of file bignum.c.

References bignew, BIGNUM_LEN, and BIGNUM_SIGN.

◆ rb_big_mul_toom3()

VALUE rb_big_mul_toom3 ( VALUE  x,
VALUE  y 
)

Definition at line 2249 of file bignum.c.

References bignew, BIGNUM_LEN, BIGNUM_SIGN, rb_eArgError, rb_raise(), and TOOM3_BALANCED.

◆ rb_big_new()

VALUE rb_big_new ( size_t  len,
int  sign 
)

Definition at line 2996 of file bignum.c.

References bignew.

◆ rb_big_norm()

VALUE rb_big_norm ( VALUE  x)

Definition at line 3134 of file bignum.c.

◆ rb_big_odd_p()

VALUE rb_big_odd_p ( VALUE  num)

Definition at line 6769 of file bignum.c.

References BDIGITS, BIGNUM_LEN, Qfalse, and Qtrue.

◆ rb_big_or()

VALUE rb_big_or ( VALUE  x,
VALUE  y 
)

◆ rb_big_pack()

void rb_big_pack ( VALUE  val,
unsigned long *  buf,
long  num_longs 
)

◆ rb_big_plus()

VALUE rb_big_plus ( VALUE  x,
VALUE  y 
)

Definition at line 5772 of file bignum.c.

References BIGNUM_SIGN, FIX2LONG, and FIXNUM_P.

Referenced by rb_int_succ().

◆ rb_big_pow()

VALUE rb_big_pow ( VALUE  x,
VALUE  y 
)

◆ rb_big_remainder()

VALUE rb_big_remainder ( VALUE  x,
VALUE  y 
)

Definition at line 6064 of file bignum.c.

References FIX2LONG, FIXNUM_P, RB_BIGNUM_TYPE_P, rb_int2big(), rb_intern, and rb_num_coerce_bin().

◆ rb_big_resize()

void rb_big_resize ( VALUE  big,
size_t  len 
)

Definition at line 2971 of file bignum.c.

◆ rb_big_rshift()

VALUE rb_big_rshift ( VALUE  x,
VALUE  y 
)

Definition at line 6589 of file bignum.c.

References FIX2LONG, and FIXNUM_P.

◆ rb_big_sign()

int rb_big_sign ( VALUE  x)

Definition at line 6710 of file bignum.c.

References BIGNUM_SIGN.

◆ rb_big_size()

size_t rb_big_size ( VALUE  big)

Definition at line 6716 of file bignum.c.

References BIGSIZE.

Referenced by rb_big_size_m().

◆ rb_big_size_m()

VALUE rb_big_size_m ( VALUE  big)

Definition at line 6722 of file bignum.c.

References rb_big_size(), and SIZET2NUM.

◆ rb_big_sq_fast()

VALUE rb_big_sq_fast ( VALUE  x)

Definition at line 1612 of file bignum.c.

References bignew, and BIGNUM_LEN.

◆ rb_big_uminus()

VALUE rb_big_uminus ( VALUE  x)

Definition at line 5502 of file bignum.c.

References BIGNUM_NEGATE, and rb_big_clone().

◆ rb_big_unpack()

VALUE rb_big_unpack ( unsigned long *  buf,
long  num_longs 
)

◆ rb_big_xor()

VALUE rb_big_xor ( VALUE  x,
VALUE  y 
)

◆ rb_bigzero_p()

int rb_bigzero_p ( VALUE  x)

Definition at line 2901 of file bignum.c.

References BIGZEROP.

◆ rb_cmpint()

int rb_cmpint ( VALUE  val,
VALUE  a,
VALUE  b 
)

◆ rb_cstr2inum()

VALUE rb_cstr2inum ( const char *  str,
int  base 
)

Definition at line 4509 of file bignum.c.

References rb_cstr_to_inum().

◆ rb_cstr_parse_inum()

VALUE rb_cstr_parse_inum ( const char *  str,
ssize_t  len,
char **  endp,
int  base 
)

Definition at line 4219 of file bignum.c.

References NULL, rb_int_parse_cstr(), and RB_INT_PARSE_DEFAULT.

Referenced by rb_cstr_to_inum(), and rb_str_to_inum().

◆ rb_cstr_to_inum()

VALUE rb_cstr_to_inum ( const char *  str,
int  base,
int  badcheck 
)

Definition at line 3992 of file bignum.c.

References INT2FIX, NIL_P, NULL, rb_cstr_parse_inum(), and rb_invalid_str().

Referenced by rb_cstr2inum().

◆ rb_dbl2big()

VALUE rb_dbl2big ( double  d)

Definition at line 5214 of file bignum.c.

Referenced by rb_integer_float_cmp(), and rb_integer_float_eq().

◆ rb_int2big()

VALUE rb_int2big ( SIGNED_VALUE  n)

◆ rb_int2inum()

VALUE rb_int2inum ( SIGNED_VALUE  n)

Definition at line 3190 of file bignum.c.

References FIXABLE, LONG2FIX, and rb_int2big().

◆ rb_int_parse_cstr()

VALUE rb_int_parse_cstr ( const char *  str,
ssize_t  len,
char **  endp,
size_t *  ndigits,
int  base,
int  flags 
)

Definition at line 4021 of file bignum.c.

References ADV, ASSERT_LEN, bad, ISSPACE, len, Qnil, RB_INT_PARSE_PREFIX, RB_INT_PARSE_SIGN, and val.

Referenced by rb_cstr_parse_inum().

◆ rb_integer_float_cmp()

VALUE rb_integer_float_cmp ( VALUE  x,
VALUE  y 
)

◆ rb_integer_float_eq()

VALUE rb_integer_float_eq ( VALUE  x,
VALUE  y 
)

◆ rb_integer_pack()

int rb_integer_pack ( VALUE  val,
void *  words,
size_t  numwords,
size_t  wordsize,
size_t  nails,
int  flags 
)

◆ rb_integer_unpack()

VALUE rb_integer_unpack ( const void *  words,
size_t  numwords,
size_t  wordsize,
size_t  nails,
int  flags 
)

Definition at line 3615 of file bignum.c.

References BDIGIT, and val.

Referenced by rb_big_unpack().

◆ rb_str2big_karatsuba()

VALUE rb_str2big_karatsuba ( VALUE  arg,
int  base,
int  badcheck 
)

Definition at line 4323 of file bignum.c.

References len.

◆ rb_str2big_normal()

VALUE rb_str2big_normal ( VALUE  arg,
int  base,
int  badcheck 
)

Definition at line 4281 of file bignum.c.

References len.

◆ rb_str2big_poweroftwo()

VALUE rb_str2big_poweroftwo ( VALUE  arg,
int  base,
int  badcheck 
)

Definition at line 4245 of file bignum.c.

References len.

◆ rb_str2inum()

VALUE rb_str2inum ( VALUE  str,
int  base 
)

Definition at line 4515 of file bignum.c.

References rb_str_to_inum().

◆ rb_str_to_inum()

VALUE rb_str_to_inum ( VALUE  str,
int  base,
int  badcheck 
)

Definition at line 4226 of file bignum.c.

References len, NIL_P, NULL, rb_cstr_parse_inum(), rb_must_asciicompat(), RSTRING_GETMEM, and StringValue.

Referenced by fun1(), and rb_str2inum().

◆ rb_uint2big()

VALUE rb_uint2big ( VALUE  n)

Definition at line 3140 of file bignum.c.

References BDIGIT, bdigit_roomof, BDIGITS, BIGDN, BIGLO, bignew, BIGNUM_SET_LEN, and SIZEOF_VALUE.

Referenced by rb_int2big(), and rb_uint2inum().

◆ rb_uint2inum()

VALUE rb_uint2inum ( VALUE  n)

Definition at line 3183 of file bignum.c.

References LONG2FIX, POSFIXABLE, and rb_uint2big().

◆ rb_ulong_isqrt()

unsigned long rb_ulong_isqrt ( unsigned  long)

Referenced by rb_big_even_p().

◆ STATIC_ASSERT() [1/9]

STATIC_ASSERT ( sizeof_bdigit_dbl  ,
sizeof(BDIGIT_DBL = =SIZEOF_BDIGIT_DBL 
)

◆ STATIC_ASSERT() [2/9]

STATIC_ASSERT ( sizeof_bdigit_dbl_signed  ,
sizeof(BDIGIT_DBL_SIGNED = =SIZEOF_BDIGIT_DBL 
)

◆ STATIC_ASSERT() [3/9]

STATIC_ASSERT ( sizeof_bdigit  ,
SIZEOF_BDIGIT<=  sizeofBDIGIT 
)

◆ STATIC_ASSERT() [4/9]

STATIC_ASSERT ( sizeof_bdigit_and_dbl  ,
SIZEOF_BDIGIT *2<=  SIZEOF_BDIGIT_DBL 
)

◆ STATIC_ASSERT() [5/9]

STATIC_ASSERT ( bdigit_signedness  )

◆ STATIC_ASSERT() [6/9]

STATIC_ASSERT ( bdigit_dbl_signedness  )

◆ STATIC_ASSERT() [7/9]

STATIC_ASSERT ( bdigit_dbl_signed_signedness  ,
,
(BDIGIT_DBL_SIGNED) -  1 
)

◆ STATIC_ASSERT() [8/9]

STATIC_ASSERT ( rbignum_embed_len_max  ,
BIGNUM_EMBED_LEN_MAX<=  BIGNUM_EMBED_LEN_MASK >> BIGNUM_EMBED_LEN_SHIFT 
)

◆ STATIC_ASSERT() [9/9]

STATIC_ASSERT ( sizeof_long_and_sizeof_bdigit  ,
SIZEOF_BDIGIT SIZEOF_LONG = =0 
)

Variable Documentation

◆ ruby_digitmap

const char ruby_digitmap[] = "0123456789abcdefghijklmnopqrstuvwxyz"

Definition at line 38 of file bignum.c.

Referenced by rb_fix2str().