1 #ifndef INTERNAL_NUMERIC_H
2 #define INTERNAL_NUMERIC_H
11 #include "internal/bignum.h"
12 #include "internal/bits.h"
13 #include "internal/fixnum.h"
14 #include "internal/vm.h"
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)
28 # define ROUND_DEFAULT RUBY_NUM_ROUND_HALF_UP
31 enum 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,
38 #if SIZEOF_DOUBLE <= SIZEOF_VALUE
39 typedef double rb_float_value_type;
44 } rb_float_value_type;
49 rb_float_value_type float_value;
52 #define RFLOAT(obj) ((struct RFloat *)(obj))
55 int rb_num_to_uint(
VALUE val,
unsigned int *ret);
57 double ruby_float_step_size(
double beg,
double end,
double unit,
int excl);
58 int ruby_float_step(
VALUE from,
VALUE to,
VALUE step,
int excl,
int allow_endless);
59 int rb_num_negative_p(
VALUE);
76 enum ruby_num_rounding_mode rb_num_get_rounding_option(
VALUE opts);
86 int rb_int_positive_p(
VALUE num);
87 int rb_int_negative_p(
VALUE num);
93 static inline VALUE rb_num_compare_with_zero(
VALUE num,
ID mid);
94 static inline int rb_num_positive_int_p(
VALUE num);
95 static inline int rb_num_negative_int_p(
VALUE num);
96 static inline double rb_float_flonum_value(
VALUE v);
97 static inline double rb_float_noflonum_value(
VALUE v);
98 static inline double rb_float_value_inline(
VALUE v);
99 static inline VALUE rb_float_new_inline(
double d);
100 static inline bool INT_POSITIVE_P(
VALUE num);
101 static inline bool INT_NEGATIVE_P(
VALUE num);
102 static inline bool FLOAT_ZERO_P(
VALUE num);
103 #define rb_float_value rb_float_value_inline
104 #define rb_float_new rb_float_new_inline
106 RUBY_SYMBOL_EXPORT_BEGIN
108 RUBY_SYMBOL_EXPORT_END
110 MJIT_SYMBOL_EXPORT_BEGIN
112 double ruby_float_mod(
double x,
double y);
124 MJIT_SYMBOL_EXPORT_END
127 INT_POSITIVE_P(
VALUE num)
130 return FIXNUM_POSITIVE_P(num);
133 return BIGNUM_POSITIVE_P(num);
138 INT_NEGATIVE_P(
VALUE num)
141 return FIXNUM_NEGATIVE_P(num);
144 return BIGNUM_NEGATIVE_P(num);
149 FLOAT_ZERO_P(
VALUE num)
155 rb_num_compare_with_zero(
VALUE num,
ID mid)
166 rb_num_positive_int_p(
VALUE num)
172 return FIXNUM_POSITIVE_P(num);
176 return BIGNUM_POSITIVE_P(num);
178 return RTEST(rb_num_compare_with_zero(num, mid));
182 rb_num_negative_int_p(
VALUE num)
188 return FIXNUM_NEGATIVE_P(num);
192 return BIGNUM_NEGATIVE_P(num);
194 return RTEST(rb_num_compare_with_zero(num, mid));
198 rb_float_flonum_value(
VALUE v)
201 if (v != (
VALUE)0x8000000000000002) {
207 VALUE b63 = (v >> 63);
211 t.v = RUBY_BIT_ROTR((2 - b63) | (v & ~(
VALUE)0x03), 3);
219 rb_float_noflonum_value(
VALUE v)
221 #if SIZEOF_DOUBLE <= SIZEOF_VALUE
222 return RFLOAT(v)->float_value;
225 rb_float_value_type v;
227 } u = {RFLOAT(v)->float_value};
233 rb_float_value_inline(
VALUE v)
236 return rb_float_flonum_value(v);
238 return rb_float_noflonum_value(v);
242 rb_float_new_inline(
double d)
252 bits = (int)((
VALUE)(t.v >> 60) & 0x7);
258 if (t.v != 0x3000000000000000 &&
259 !((bits-3) & ~0x01)) {
260 return (RUBY_BIT_ROTL(t.v, 3) & ~(
VALUE)0x01) | 0x02;
262 else if (t.v == (
VALUE)0) {
264 return 0x8000000000000002;
VALUE rb_float_new_in_heap(double d)
Identical to rb_float_new(), except it does not generate Flonums.
#define RFLOAT_VALUE
Old name of rb_float_value.
#define Qundef
Old name of RUBY_Qundef.
#define INT2FIX
Old name of RB_INT2FIX.
#define T_BIGNUM
Old name of RUBY_T_BIGNUM.
#define FLONUM_P
Old name of RB_FLONUM_P.
#define FIXNUM_P
Old name of RB_FIXNUM_P.
VALUE rb_cInteger
Module class.
void rb_cmperr(VALUE a, VALUE b)
Raises "comparison failed" error.
int rb_method_basic_definition_p(VALUE klass, ID mid)
Well...
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.
#define RTEST
This is an old name of RB_TEST.
Ruby's object's, base components.
uintptr_t ID
Type that represents a Ruby identifier such as a variable name.
#define SIZEOF_VALUE
Identical to sizeof(VALUE), except it is a macro that can also be used inside of preprocessor directi...
uintptr_t VALUE
Type that represents a Ruby object.
static bool RB_TYPE_P(VALUE obj, enum ruby_value_type t)
Queries if the given object is of given type.