Ruby 4.1.0dev (2026-05-15 revision a8bcae043f931d9b79f1cb1fe2c021985d07b984)
bignum.h
Go to the documentation of this file.
1#ifndef RBIMPL_INTERN_BIGNUM_H /*-*-C++-*-vi:se ft=cpp:*/
2#define RBIMPL_INTERN_BIGNUM_H
23#include "ruby/internal/config.h"
24
25#ifdef STDC_HEADERS
26# include <stddef.h>
27#endif
28
29#ifdef HAVE_SYS_TYPES_H
30# include <sys/types.h> /* for ssize_t (note: on Windows ssize_t is */
31#endif /* `#define`d in ruby/config.h) */
32
35#include "ruby/internal/value.h"
37
39
40/* bignum.c */
41
42
55VALUE rb_big_new(size_t len, int sign);
56
68int rb_bigzero_p(VALUE x);
69
77
85void rb_big_2comp(VALUE num);
86
95
102void rb_big_resize(VALUE big, size_t len);
103
129VALUE rb_cstr_to_inum(const char *str, int base, int badcheck);
130
153VALUE rb_str_to_inum(VALUE str, int base, int badcheck);
154
175VALUE rb_cstr2inum(const char *str, int base);
176
200VALUE rb_str2inum(VALUE str, int base);
201
211VALUE rb_big2str(VALUE x, int base);
212
220long rb_big2long(VALUE x);
221
223#define rb_big2int(x) rb_big2long(x)
224
239unsigned long rb_big2ulong(VALUE x);
240
242#define rb_big2uint(x) rb_big2ulong(x)
243
244#if HAVE_LONG_LONG
252LONG_LONG rb_big2ll(VALUE);
253
268unsigned LONG_LONG rb_big2ull(VALUE);
269
270#endif /* HAVE_LONG_LONG */
271
285void rb_big_pack(VALUE val, unsigned long *buf, long num_longs);
286
300VALUE rb_big_unpack(unsigned long *buf, long num_longs);
301
302/* pack.c */
303
314int rb_uv_to_utf8(char buf[6], unsigned long uv);
315
316/* bignum.c */
317
330VALUE rb_dbl2big(double d);
331
343double rb_big2dbl(VALUE x);
344
355VALUE rb_big_cmp(VALUE lhs, VALUE rhs);
356
366VALUE rb_big_eq(VALUE lhs, VALUE rhs);
367
378VALUE rb_big_eql(VALUE lhs, VALUE rhs);
379
389
399
409
419
429
444
455
468
478
488
498
510
522
529#define INTEGER_PACK_MSWORD_FIRST 0x01
530
532#define INTEGER_PACK_LSWORD_FIRST 0x02
533
538#define INTEGER_PACK_MSBYTE_FIRST 0x10
539
544#define INTEGER_PACK_LSBYTE_FIRST 0x20
545
550#define INTEGER_PACK_NATIVE_BYTE_ORDER 0x40
551
553#define INTEGER_PACK_2COMP 0x80
554
556#define INTEGER_PACK_FORCE_GENERIC_IMPLEMENTATION 0x400
557
562#define INTEGER_PACK_FORCE_BIGNUM 0x100
563
568#define INTEGER_PACK_NEGATIVE 0x200
569
571#define INTEGER_PACK_LITTLE_ENDIAN \
572 (INTEGER_PACK_LSWORD_FIRST | \
573 INTEGER_PACK_LSBYTE_FIRST)
574
576#define INTEGER_PACK_BIG_ENDIAN \
577 (INTEGER_PACK_MSWORD_FIRST | \
578 INTEGER_PACK_MSBYTE_FIRST)
579
683int rb_integer_pack(VALUE val, void *words, size_t numwords, size_t wordsize, size_t nails, int flags);
684
765VALUE rb_integer_unpack(const void *words, size_t numwords, size_t wordsize, size_t nails, int flags);
766
785size_t rb_absint_size(VALUE val, int *nlz_bits_ret);
786
808size_t rb_absint_numwords(VALUE val, size_t word_numbits, size_t *nlz_bits_ret);
809
847
854#define RB_INT_PARSE_SIGN 0x01
855
857#define RB_INT_PARSE_UNDERSCORE 0x02
858
860#define RB_INT_PARSE_PREFIX 0x04
861
863#define RB_INT_PARSE_ALL 0x07
864
866#define RB_INT_PARSE_DEFAULT 0x07
867
887VALUE rb_int_parse_cstr(const char *str, ssize_t len, char **endp, size_t *ndigits, int base, int flags);
888
890
891#endif /* RBIMPL_INTERN_BIGNUM_H */
Defines old LONG_LONG.
#define LONG_LONG
Definition long_long.h:38
Tweaking visibility of C variables/functions.
#define RBIMPL_SYMBOL_EXPORT_END()
Counterpart of RBIMPL_SYMBOL_EXPORT_BEGIN.
Definition dllexport.h:74
#define RBIMPL_SYMBOL_EXPORT_BEGIN()
Shortcut macro equivalent to RUBY_SYMBOL_EXPORT_BEGIN extern "C" {.
Definition dllexport.h:65
Defines RBIMPL_ATTR_NONNULL.
#define RBIMPL_ATTR_NONNULL(list)
Wraps (or simulates) __attribute__((nonnull))
Definition nonnull.h:30
int rb_integer_pack(VALUE val, void *words, size_t numwords, size_t wordsize, size_t nails, int flags)
Exports an integer into a buffer.
Definition bignum.c:3636
VALUE rb_big_lshift(VALUE x, VALUE y)
Performs shift left.
Definition bignum.c:6755
VALUE rb_big_and(VALUE x, VALUE y)
Performs bitwise and of the passed two objects.
Definition bignum.c:6494
VALUE rb_big_or(VALUE x, VALUE y)
Performs bitwise or of the passed two objects.
Definition bignum.c:6613
VALUE rb_big_minus(VALUE x, VALUE y)
Performs subtraction of the passed two objects.
Definition bignum.c:5980
VALUE rb_big_modulo(VALUE x, VALUE y)
Performs modulo of the passed two objects.
Definition bignum.c:6234
VALUE rb_big_new(size_t len, int sign)
Allocates a bignum object.
Definition bignum.c:3102
VALUE rb_big_pow(VALUE x, VALUE y)
Raises x to the powerof y.
Definition bignum.c:6375
int rb_bigzero_p(VALUE x)
Queries if the passed bignum instance is a "bigzero".
Definition bignum.c:2968
VALUE rb_big_plus(VALUE x, VALUE y)
Performs addition of the passed two objects.
Definition bignum.c:5951
VALUE rb_str_to_inum(VALUE str, int base, int badcheck)
Identical to rb_cstr2inum(), except it takes Ruby's strings instead of C's.
Definition bignum.c:4356
VALUE rb_big_clone(VALUE num)
Duplicates the given bignum.
Definition bignum.c:3110
size_t rb_absint_size(VALUE val, int *nlz_bits_ret)
Calculates the number of bytes needed to represent the absolute value of the passed integer.
Definition bignum.c:3337
size_t rb_absint_numwords(VALUE val, size_t word_numbits, size_t *nlz_bits_ret)
Calculates the number of words needed represent the absolute value of the passed integer.
Definition bignum.c:3472
int rb_absint_singlebit_p(VALUE val)
Tests abs(val) consists only of a bit or not.
Definition bignum.c:3535
VALUE rb_integer_unpack(const void *words, size_t numwords, size_t wordsize, size_t nails, int flags)
Import an integer from a buffer.
Definition bignum.c:3722
unsigned long rb_big2ulong(VALUE x)
Converts a bignum into C's unsigned long.
Definition bignum.c:5260
VALUE rb_big_idiv(VALUE x, VALUE y)
Performs "integer division".
Definition bignum.c:6228
void rb_big_2comp(VALUE num)
Destructively modify the passed bignum into 2's complement representation.
Definition bignum.c:3139
VALUE rb_big2str(VALUE x, int base)
Generates a place-value representation of the passed integer.
Definition bignum.c:5226
VALUE rb_big_cmp(VALUE lhs, VALUE rhs)
Compares the passed two bignums.
Definition bignum.c:5548
VALUE rb_str2inum(VALUE str, int base)
Identical to rb_str_to_inum(), except the second argument controls the base and badcheck at once.
Definition bignum.c:4632
VALUE rb_dbl2big(double d)
Converts a C's double into a bignum.
Definition bignum.c:5384
VALUE rb_big_mul(VALUE x, VALUE y)
Performs multiplication of the passed two objects.
Definition bignum.c:6064
VALUE rb_big_eql(VALUE lhs, VALUE rhs)
Equality, in terms of eql?.
Definition bignum.c:5672
VALUE rb_cstr2inum(const char *str, int base)
Identical to rb_cstr_to_inum(), except the second argument controls the base and badcheck at once.
Definition bignum.c:4626
int rb_uv_to_utf8(char buf[6], unsigned long uv)
Encodes a Unicode codepoint into its UTF-8 representation.
Definition pack.c:1691
VALUE rb_big_unpack(unsigned long *buf, long num_longs)
Constructs a (possibly very big) bignum from a series of integers.
Definition bignum.c:3313
VALUE rb_big_divmod(VALUE x, VALUE y)
Performs "divmod" operation.
Definition bignum.c:6266
VALUE rb_big_xor(VALUE x, VALUE y)
Performs exclusive or of the passed two objects.
Definition bignum.c:6707
VALUE rb_big_div(VALUE x, VALUE y)
Performs division of the passed two objects.
Definition bignum.c:6222
VALUE rb_big_norm(VALUE x)
Normalises the passed bignum.
Definition bignum.c:3242
VALUE rb_cstr_to_inum(const char *str, int base, int badcheck)
Parses C's string to convert into a Ruby's integer.
Definition bignum.c:4099
void rb_big_pack(VALUE val, unsigned long *buf, long num_longs)
Converts a bignum into a series of its parts.
Definition bignum.c:3305
VALUE rb_big_rshift(VALUE x, VALUE y)
Performs shift right.
Definition bignum.c:6785
double rb_big2dbl(VALUE x)
Converts a bignum into C's double.
Definition bignum.c:5445
long rb_big2long(VALUE x)
Converts a bignum into C's long.
Definition bignum.c:5275
void rb_big_resize(VALUE big, size_t len)
Destructively resizes the backend storage of the passed bignum.
Definition bignum.c:3070
VALUE rb_big_eq(VALUE lhs, VALUE rhs)
Equality, in terms of ==.
Definition bignum.c:5653
VALUE rb_int_parse_cstr(const char *str, ssize_t len, char **endp, size_t *ndigits, int base, int flags)
Parses a C string to convert into a Ruby integer, with detailed control over parsing behaviour.
Definition bignum.c:4128
int len
Length of the buffer.
Definition io.h:8
Defines VALUE and ID.
uintptr_t VALUE
Type that represents a Ruby object.
Definition value.h:40