Ruby: internal/numeric.h Source File

Ruby 3.3.4p94 (2024-07-09 revision be1089c8ec5ba40e09b1553e36b3174bf4014d9d)

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_lshift(VALUE x, VALUE y);
89 VALUE rb_int_div(VALUE x, VALUE y);
90int rb_int_positive_p(VALUE num);
91int rb_int_negative_p(VALUE num);
92 VALUE rb_check_integer_type(VALUE);
93 VALUE rb_num_pow(VALUE x, VALUE y);
94 VALUE rb_float_ceil(VALUE num, int ndigits);
95 VALUE rb_float_floor(VALUE x, int ndigits);
96 VALUE rb_float_abs(VALUE flt);
97static inline VALUE rb_num_compare_with_zero(VALUE num, ID mid);
98static inline int rb_num_positive_int_p(VALUE num);
99static inline int rb_num_negative_int_p(VALUE num);
100static inline double rb_float_flonum_value(VALUE v);
101static inline double rb_float_noflonum_value(VALUE v);
102static inline double rb_float_value_inline(VALUE v);
103static inline VALUE rb_float_new_inline(double d);
104static inline bool INT_POSITIVE_P(VALUE num);
105static inline bool INT_NEGATIVE_P(VALUE num);
106static inline bool FLOAT_ZERO_P(VALUE num);
107#define rb_float_value rb_float_value_inline
108#define rb_float_new rb_float_new_inline
109
110RUBY_SYMBOL_EXPORT_BEGIN
111/* numeric.c (export) */
112RUBY_SYMBOL_EXPORT_END
113
114 VALUE rb_flo_div_flo(VALUE x, VALUE y);
115double ruby_float_mod(double x, double y);
116 VALUE rb_float_equal(VALUE x, VALUE y);
117int rb_float_cmp(VALUE x, VALUE y);
118 VALUE rb_float_eql(VALUE x, VALUE y);
119 VALUE rb_fix_aref(VALUE fix, VALUE idx);
120 VALUE rb_int_zero_p(VALUE num);
121 VALUE rb_int_even_p(VALUE num);
122 VALUE rb_int_odd_p(VALUE num);
123 VALUE rb_int_abs(VALUE num);
124 VALUE rb_int_bit_length(VALUE num);
125 VALUE rb_int_uminus(VALUE num);
126 VALUE rb_int_comp(VALUE num);
127
128static inline bool
129INT_POSITIVE_P(VALUE num)
130{
131 if (FIXNUM_P(num)) {
132 return FIXNUM_POSITIVE_P(num);
133 }
134 else {
135 return BIGNUM_POSITIVE_P(num);
136 }
137}
138
139static inline bool
140INT_NEGATIVE_P(VALUE num)
141{
142 if (FIXNUM_P(num)) {
143 return FIXNUM_NEGATIVE_P(num);
144 }
145 else {
146 return BIGNUM_NEGATIVE_P(num);
147 }
148}
149
150static inline bool
151FLOAT_ZERO_P(VALUE num)
152{
153 return RFLOAT_VALUE(num) == 0.0;
154}
155
156static inline VALUE
157rb_num_compare_with_zero(VALUE num, ID mid)
158{
159 VALUE zero = INT2FIX(0);
160 VALUE r = rb_check_funcall(num, mid, 1, &zero);
161 if (r == Qundef) {
162 rb_cmperr(num, zero);
163 }
164 return r;
165}
166
167static inline int
168rb_num_positive_int_p(VALUE num)
169{
170 const ID mid = '>';
171
172 if (FIXNUM_P(num)) {
173 if (rb_method_basic_definition_p(rb_cInteger, mid))
174 return FIXNUM_POSITIVE_P(num);
175 }
176 else if (RB_TYPE_P(num, T_BIGNUM)) {
177 if (rb_method_basic_definition_p(rb_cInteger, mid))
178 return BIGNUM_POSITIVE_P(num);
179 }
180 return RTEST(rb_num_compare_with_zero(num, mid));
181}
182
183static inline int
184rb_num_negative_int_p(VALUE num)
185{
186 const ID mid = '<';
187
188 if (FIXNUM_P(num)) {
189 if (rb_method_basic_definition_p(rb_cInteger, mid))
190 return FIXNUM_NEGATIVE_P(num);
191 }
192 else if (RB_TYPE_P(num, T_BIGNUM)) {
193 if (rb_method_basic_definition_p(rb_cInteger, mid))
194 return BIGNUM_NEGATIVE_P(num);
195 }
196 return RTEST(rb_num_compare_with_zero(num, mid));
197}
198
199static inline double
200rb_float_flonum_value(VALUE v)
201{
202#if USE_FLONUM
203 if (v != (VALUE)0x8000000000000002) { /* LIKELY */
204 union {
205 double d;
206 VALUE v;
207 } t;
208
209 VALUE b63 = (v >> 63);
210 /* e: xx1... -> 011... */
211 /* xx0... -> 100... */
212 /* ^b63 */
213 t.v = RUBY_BIT_ROTR((2 - b63) | (v & ~(VALUE)0x03), 3);
214 return t.d;
215 }
216#endif
217 return 0.0;
218}
219
220static inline double
221rb_float_noflonum_value(VALUE v)
222{
223#if SIZEOF_DOUBLE <= SIZEOF_VALUE
224 return RFLOAT(v)->float_value;
225#else
226 union {
227 rb_float_value_type v;
228 double d;
229 } u = {RFLOAT(v)->float_value};
230 return u.d;
231#endif
232}
233
234static inline double
235rb_float_value_inline(VALUE v)
236{
237 if (FLONUM_P(v)) {
238 return rb_float_flonum_value(v);
239 }
240 return rb_float_noflonum_value(v);
241}
242
243static inline VALUE
244rb_float_new_inline(double d)
245{
246#if USE_FLONUM
247 union {
248 double d;
249 VALUE v;
250 } t;
251 int bits;
252
253 t.d = d;
254 bits = (int)((VALUE)(t.v >> 60) & 0x7);
255 /* bits contains 3 bits of b62..b60. */
256 /* bits - 3 = */
257 /* b011 -> b000 */
258 /* b100 -> b001 */
259
260 if (t.v != 0x3000000000000000 /* 1.72723e-77 */ &&
261 !((bits-3) & ~0x01)) {
262 return (RUBY_BIT_ROTL(t.v, 3) & ~(VALUE)0x01) | 0x02;
263 }
264 else if (t.v == (VALUE)0) {
265 /* +0.0 */
266 return 0x8000000000000002;
267 }
268 /* out of range */
269#endif
270 return rb_float_new_in_heap(d);
271}
272
273#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:1019
RFLOAT_VALUE
#define RFLOAT_VALUE
Old name of rb_float_value.
Definition double.h:28
Qundef
#define Qundef
Old name of RUBY_Qundef.
Definition special_consts.h:62
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:198
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:687
intern.h
ruby.h
RTEST
#define RTEST
This is an old name of RB_TEST.
Definition special_consts.h:51
RBasic
Ruby's object's, base components.
Definition rbasic.h:64
RFloat
Definition numeric.h:51
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