Ruby: internal/numeric.h Source File

Ruby 4.0.6p0 (2026-07-14 revision 03b6d3f8898a28604fe6cb00eae3226b821168f4)

numeric.h
1#ifndef INTERNAL_NUMERIC_H /*-*-C-*-vi:se ft=c:*/
2#define INTERNAL_NUMERIC_H
11#include "internal/bignum.h" /* for BIGNUM_POSITIVE_P */
12#include "internal/bits.h" /* for RUBY_BIT_ROTL */
13#include "internal/fixnum.h" /* for FIXNUM_POSITIVE_P */
14#include "internal/vm.h" /* for rb_method_basic_definition_p */
15#include "ruby/intern.h" /* for rb_cmperr */
16#include "ruby/ruby.h" /* for USE_FLONUM */
17
18#define ROUND_TO(mode, even, up, down) \
19 ((mode) == RUBY_NUM_ROUND_HALF_EVEN ? even : \
20 (mode) == RUBY_NUM_ROUND_HALF_UP ? up : down)
21#define ROUND_FUNC(mode, name) \
22 ROUND_TO(mode, name##_half_even, name##_half_up, name##_half_down)
23#define ROUND_CALL(mode, name, args) \
24 ROUND_TO(mode, name##_half_even args, \
25 name##_half_up args, name##_half_down args)
26
27#ifndef ROUND_DEFAULT
28# define ROUND_DEFAULT RUBY_NUM_ROUND_HALF_UP
29#endif
30
31enum ruby_num_rounding_mode {
32 RUBY_NUM_ROUND_HALF_UP,
33 RUBY_NUM_ROUND_HALF_EVEN,
34 RUBY_NUM_ROUND_HALF_DOWN,
35 RUBY_NUM_ROUND_DEFAULT = ROUND_DEFAULT,
36};
37
38/* same as internal.h */
39#define numberof(array) ((int)(sizeof(array) / sizeof((array)[0])))
40#define roomof(x, y) (((x) + (y) - 1) / (y))
41#define type_roomof(x, y) roomof(sizeof(x), sizeof(y))
42
43#if SIZEOF_DOUBLE <= SIZEOF_VALUE
44typedef double rb_float_value_type;
45#else
46typedef struct {
47 VALUE values[roomof(SIZEOF_DOUBLE, SIZEOF_VALUE)];
48} rb_float_value_type;
49#endif
50
51 struct RFloat {
52 struct RBasic basic;
53 rb_float_value_type float_value;
54};
55
56#define RFLOAT(obj) ((struct RFloat *)(obj))
57
58/* numeric.c */
59int rb_num_to_uint(VALUE val, unsigned int *ret);
60 VALUE ruby_num_interval_step_size(VALUE from, VALUE to, VALUE step, int excl);
61double ruby_float_step_size(double beg, double end, double unit, int excl);
62int ruby_float_step(VALUE from, VALUE to, VALUE step, int excl, int allow_endless);
63int rb_num_negative_p(VALUE);
64 VALUE rb_int_succ(VALUE num);
65 VALUE rb_float_uminus(VALUE num);
66 VALUE rb_int_plus(VALUE x, VALUE y);
67 VALUE rb_float_plus(VALUE x, VALUE y);
68 VALUE rb_int_minus(VALUE x, VALUE y);
69 VALUE rb_float_minus(VALUE x, VALUE y);
70 VALUE rb_int_mul(VALUE x, VALUE y);
71 VALUE rb_float_mul(VALUE x, VALUE y);
72 VALUE rb_float_div(VALUE x, VALUE y);
73 VALUE rb_int_idiv(VALUE x, VALUE y);
74 VALUE rb_int_modulo(VALUE x, VALUE y);
75 VALUE rb_int2str(VALUE num, int base);
76 VALUE rb_fix_plus(VALUE x, VALUE y);
77 VALUE rb_int_gt(VALUE x, VALUE y);
78 VALUE rb_float_gt(VALUE x, VALUE y);
79 VALUE rb_int_ge(VALUE x, VALUE y);
80enum ruby_num_rounding_mode rb_num_get_rounding_option(VALUE opts);
81double rb_int_fdiv_double(VALUE x, VALUE y);
82 VALUE rb_int_pow(VALUE x, VALUE y);
83 VALUE rb_float_pow(VALUE x, VALUE y);
84 VALUE rb_int_cmp(VALUE x, VALUE y);
85 VALUE rb_int_equal(VALUE x, VALUE y);
86 VALUE rb_int_divmod(VALUE x, VALUE y);
87 VALUE rb_int_and(VALUE x, VALUE y);
88 VALUE rb_int_xor(VALUE x, VALUE y);
89 VALUE rb_int_lshift(VALUE x, VALUE y);
90 VALUE rb_int_rshift(VALUE x, VALUE y);
91 VALUE rb_int_div(VALUE x, VALUE y);
92int rb_int_positive_p(VALUE num);
93int rb_int_negative_p(VALUE num);
94 VALUE rb_check_integer_type(VALUE);
95 VALUE rb_num_pow(VALUE x, VALUE y);
96 VALUE rb_float_ceil(VALUE num, int ndigits);
97 VALUE rb_float_floor(VALUE x, int ndigits);
98 VALUE rb_float_abs(VALUE flt);
99static inline VALUE rb_num_compare_with_zero(VALUE num, ID mid);
100static inline int rb_num_positive_int_p(VALUE num);
101static inline int rb_num_negative_int_p(VALUE num);
102static inline double rb_float_flonum_value(VALUE v);
103static inline double rb_float_noflonum_value(VALUE v);
104static inline double rb_float_value_inline(VALUE v);
105static inline VALUE rb_float_new_inline(double d);
106static inline bool INT_POSITIVE_P(VALUE num);
107static inline bool INT_NEGATIVE_P(VALUE num);
108static inline bool FLOAT_ZERO_P(VALUE num);
109#define rb_float_value rb_float_value_inline
110#define rb_float_new rb_float_new_inline
111
112RUBY_SYMBOL_EXPORT_BEGIN
113/* numeric.c (export) */
114RUBY_SYMBOL_EXPORT_END
115
116 VALUE rb_flo_div_flo(VALUE x, VALUE y);
117double ruby_float_mod(double x, double y);
118 VALUE rb_float_equal(VALUE x, VALUE y);
119int rb_float_cmp(VALUE x, VALUE y);
120 VALUE rb_float_eql(VALUE x, VALUE y);
121 VALUE rb_fix_aref(VALUE fix, VALUE idx);
122 VALUE rb_int_zero_p(VALUE num);
123 VALUE rb_int_even_p(VALUE num);
124 VALUE rb_int_odd_p(VALUE num);
125 VALUE rb_int_abs(VALUE num);
126 VALUE rb_int_bit_length(VALUE num);
127 VALUE rb_int_uminus(VALUE num);
128 VALUE rb_int_comp(VALUE num);
129
130// Unified 128-bit integer structures that work with or without native support:
131 union rb_uint128 {
132#ifdef WORDS_BIGENDIAN
133 struct {
134 uint64_t high;
135 uint64_t low;
136 } parts;
137#else
138 struct {
139 uint64_t low;
140 uint64_t high;
141 } parts;
142#endif
143#ifdef HAVE_UINT128_T
144 uint128_t value;
145#endif
146};
147typedef union rb_uint128 rb_uint128_t;
148
149 union rb_int128 {
150#ifdef WORDS_BIGENDIAN
151 struct {
152 uint64_t high;
153 uint64_t low;
154 } parts;
155#else
156 struct {
157 uint64_t low;
158 uint64_t high;
159 } parts;
160#endif
161#ifdef HAVE_UINT128_T
162 int128_t value;
163#endif
164};
165typedef union rb_int128 rb_int128_t;
166
167 union uint128_int128_conversion {
168 rb_uint128_t uint128;
169 rb_int128_t int128;
170};
171
172// Conversion functions for 128-bit integers:
173rb_uint128_t rb_numeric_to_uint128(VALUE x);
174rb_int128_t rb_numeric_to_int128(VALUE x);
175 VALUE rb_uint128_to_numeric(rb_uint128_t n);
176 VALUE rb_int128_to_numeric(rb_int128_t n);
177
178static inline bool
179INT_POSITIVE_P(VALUE num)
180{
181 if (FIXNUM_P(num)) {
182 return FIXNUM_POSITIVE_P(num);
183 }
184 else {
185 return BIGNUM_POSITIVE_P(num);
186 }
187}
188
189static inline bool
190INT_NEGATIVE_P(VALUE num)
191{
192 if (FIXNUM_P(num)) {
193 return FIXNUM_NEGATIVE_P(num);
194 }
195 else {
196 return BIGNUM_NEGATIVE_P(num);
197 }
198}
199
200static inline bool
201FLOAT_ZERO_P(VALUE num)
202{
203 return RFLOAT_VALUE(num) == 0.0;
204}
205
206static inline VALUE
207rb_num_compare_with_zero(VALUE num, ID mid)
208{
209 VALUE zero = INT2FIX(0);
210 VALUE r = rb_check_funcall(num, mid, 1, &zero);
211 if (RB_UNDEF_P(r)) {
212 rb_cmperr(num, zero);
213 }
214 return r;
215}
216
217static inline int
218rb_num_positive_int_p(VALUE num)
219{
220 const ID mid = '>';
221
222 if (FIXNUM_P(num)) {
223 if (rb_method_basic_definition_p(rb_cInteger, mid))
224 return FIXNUM_POSITIVE_P(num);
225 }
226 else if (RB_TYPE_P(num, T_BIGNUM)) {
227 if (rb_method_basic_definition_p(rb_cInteger, mid))
228 return BIGNUM_POSITIVE_P(num);
229 }
230 return RTEST(rb_num_compare_with_zero(num, mid));
231}
232
233static inline int
234rb_num_negative_int_p(VALUE num)
235{
236 const ID mid = '<';
237
238 if (FIXNUM_P(num)) {
239 if (rb_method_basic_definition_p(rb_cInteger, mid))
240 return FIXNUM_NEGATIVE_P(num);
241 }
242 else if (RB_TYPE_P(num, T_BIGNUM)) {
243 if (rb_method_basic_definition_p(rb_cInteger, mid))
244 return BIGNUM_NEGATIVE_P(num);
245 }
246 return RTEST(rb_num_compare_with_zero(num, mid));
247}
248
249static inline double
250rb_float_flonum_value(VALUE v)
251{
252#if USE_FLONUM
253 if (v != (VALUE)0x8000000000000002) { /* LIKELY */
254 union {
255 double d;
256 VALUE v;
257 } t;
258
259 VALUE b63 = (v >> 63);
260 /* e: xx1... -> 011... */
261 /* xx0... -> 100... */
262 /* ^b63 */
263 t.v = RUBY_BIT_ROTR((2 - b63) | (v & ~(VALUE)0x03), 3);
264 return t.d;
265 }
266#endif
267 return 0.0;
268}
269
270static inline double
271rb_float_noflonum_value(VALUE v)
272{
273#if SIZEOF_DOUBLE <= SIZEOF_VALUE
274 return RFLOAT(v)->float_value;
275#else
276 union {
277 rb_float_value_type v;
278 double d;
279 } u = {RFLOAT(v)->float_value};
280 return u.d;
281#endif
282}
283
284static inline double
285rb_float_value_inline(VALUE v)
286{
287 if (FLONUM_P(v)) {
288 return rb_float_flonum_value(v);
289 }
290 return rb_float_noflonum_value(v);
291}
292
293static inline VALUE
294rb_float_new_inline(double d)
295{
296#if USE_FLONUM
297 union {
298 double d;
299 VALUE v;
300 } t;
301 int bits;
302
303 t.d = d;
304 bits = (int)((VALUE)(t.v >> 60) & 0x7);
305 /* bits contains 3 bits of b62..b60. */
306 /* bits - 3 = */
307 /* b011 -> b000 */
308 /* b100 -> b001 */
309
310 if (t.v != 0x3000000000000000 /* 1.72723e-77 */ &&
311 !((bits-3) & ~0x01)) {
312 return (RUBY_BIT_ROTL(t.v, 3) & ~(VALUE)0x01) | 0x02;
313 }
314 else if (t.v == (VALUE)0) {
315 /* +0.0 */
316 return 0x8000000000000002;
317 }
318 /* out of range */
319#endif
320 return rb_float_new_in_heap(d);
321}
322
323#endif /* INTERNAL_NUMERIC_H */
rb_float_new_in_heap
VALUE rb_float_new_in_heap(double d)
Identical to rb_float_new(), except it does not generate Flonums.
Definition numeric.c:912
RFLOAT_VALUE
#define RFLOAT_VALUE
Old name of rb_float_value.
Definition double.h:28
INT2FIX
#define INT2FIX
Old name of RB_INT2FIX.
Definition long.h:48
T_BIGNUM
#define T_BIGNUM
Old name of RUBY_T_BIGNUM.
Definition value_type.h:57
FLONUM_P
#define FLONUM_P
Old name of RB_FLONUM_P.
Definition special_consts.h:67
FIXNUM_P
#define FIXNUM_P
Old name of RB_FIXNUM_P.
Definition special_consts.h:53
rb_cInteger
VALUE rb_cInteger
Module class.
Definition numeric.c:200
rb_check_funcall
VALUE rb_check_funcall(VALUE recv, ID mid, int argc, const VALUE *argv)
Identical to rb_funcallv(), except it returns RUBY_Qundef instead of raising rb_eNoMethodError.
Definition vm_eval.c:686
intern.h
ruby.h
RB_UNDEF_P
static bool RB_UNDEF_P(VALUE obj)
Checks if the given object is undef.
Definition special_consts.h:189
RTEST
#define RTEST
This is an old name of RB_TEST.
Definition special_consts.h:51
RBasic
Ruby object's base components.
Definition rbasic.h:69
RFloat
Definition numeric.h:51
rb_int128
Definition numeric.h:149
rb_uint128
Definition numeric.h:131
uint128_int128_conversion
Definition numeric.h:167
ID
uintptr_t ID
Type that represents a Ruby identifier such as a variable name.
Definition value.h:52
SIZEOF_VALUE
#define SIZEOF_VALUE
Identical to sizeof(VALUE), except it is a macro that can also be used inside of preprocessor directi...
Definition value.h:69
VALUE
uintptr_t VALUE
Type that represents a Ruby object.
Definition value.h:40
RB_TYPE_P
static bool RB_TYPE_P(VALUE obj, enum ruby_value_type t)
Queries if the given object is of given type.
Definition value_type.h:376