Skip to content

Commit f830ace

Browse files
committed
object.c: rb_num_to_dbl
* object.c (rb_num_to_dbl): move from num2dbl_with_to_f in math.c. git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@51563 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
1 parent 235e773 commit f830ace

File tree

4 files changed

+189
-106
lines changed

4 files changed

+189
-106
lines changed

ChangeLog

+4
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,7 @@
1+
Thu Aug 13 14:36:31 2015 Nobuyoshi Nakada <nobu@ruby-lang.org>
2+
3+
* object.c (rb_num_to_dbl): move from num2dbl_with_to_f in math.c.
4+
15
Thu Aug 13 09:01:25 2015 Eric Wong <e@80x24.org>
26

37
* load.c (features_index_add): avoid repeat calculation

internal.h

+29-18
Original file line numberDiff line numberDiff line change
@@ -903,31 +903,41 @@ VALUE rb_dbl_hash(double d);
903903
#endif
904904

905905
static inline double
906-
rb_float_value_inline(VALUE v)
906+
rb_float_flonum_value(VALUE v)
907907
{
908908
#if USE_FLONUM
909-
if (FLONUM_P(v)) {
910-
if (v != (VALUE)0x8000000000000002) { /* LIKELY */
911-
union {
912-
double d;
913-
VALUE v;
914-
} t;
915-
916-
VALUE b63 = (v >> 63);
917-
/* e: xx1... -> 011... */
918-
/* xx0... -> 100... */
919-
/* ^b63 */
920-
t.v = RUBY_BIT_ROTR((2 - b63) | (v & ~0x03), 3);
921-
return t.d;
922-
}
923-
else {
924-
return 0.0;
925-
}
909+
if (v != (VALUE)0x8000000000000002) { /* LIKELY */
910+
union {
911+
double d;
912+
VALUE v;
913+
} t;
914+
915+
VALUE b63 = (v >> 63);
916+
/* e: xx1... -> 011... */
917+
/* xx0... -> 100... */
918+
/* ^b63 */
919+
t.v = RUBY_BIT_ROTR((2 - b63) | (v & ~0x03), 3);
920+
return t.d;
926921
}
927922
#endif
923+
return 0.0;
924+
}
925+
926+
static inline double
927+
rb_float_noflonum_value(VALUE v)
928+
{
928929
return ((struct RFloat *)v)->float_value;
929930
}
930931

932+
static inline double
933+
rb_float_value_inline(VALUE v)
934+
{
935+
if (FLONUM_P(v)) {
936+
return rb_float_flonum_value(v);
937+
}
938+
return rb_float_noflonum_value(v);
939+
}
940+
931941
static inline VALUE
932942
rb_float_new_inline(double d)
933943
{
@@ -965,6 +975,7 @@ rb_float_new_inline(double d)
965975
void rb_obj_copy_ivar(VALUE dest, VALUE obj);
966976
VALUE rb_obj_equal(VALUE obj1, VALUE obj2);
967977
VALUE rb_class_search_ancestor(VALUE klass, VALUE super);
978+
double rb_num_to_dbl(VALUE val);
968979

969980
struct RBasicRaw {
970981
VALUE flags;

math.c

+1-46
Original file line numberDiff line numberDiff line change
@@ -21,54 +21,10 @@
2121

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

24-
static ID id_to_f;
25-
2624
VALUE rb_mMath;
2725
VALUE rb_eMathDomainError;
2826

29-
static inline int
30-
basic_to_f_p(VALUE klass)
31-
{
32-
return rb_method_basic_definition_p(klass, id_to_f);
33-
}
34-
35-
#define fix2dbl_without_to_f(x) (double)FIX2LONG(x)
36-
#define big2dbl_without_to_f(x) rb_big2dbl(x)
37-
#define int2dbl_without_to_f(x) (FIXNUM_P(x) ? fix2dbl_without_to_f(x) : big2dbl_without_to_f(x))
38-
#define rat2dbl_without_to_f(x) \
39-
(int2dbl_without_to_f(rb_rational_num(x)) / \
40-
int2dbl_without_to_f(rb_rational_den(x)))
41-
42-
static inline double
43-
num2dbl_with_to_f(VALUE num)
44-
{
45-
if (SPECIAL_CONST_P(num)) {
46-
if (FIXNUM_P(num)) {
47-
if (basic_to_f_p(rb_cFixnum))
48-
return fix2dbl_without_to_f(num);
49-
}
50-
else if (FLONUM_P(num)) {
51-
return RFLOAT_VALUE(num);
52-
}
53-
}
54-
else {
55-
switch (BUILTIN_TYPE(num)) {
56-
case T_FLOAT:
57-
return RFLOAT_VALUE(num);
58-
case T_BIGNUM:
59-
if (basic_to_f_p(rb_cBignum))
60-
return big2dbl_without_to_f(num);
61-
break;
62-
case T_RATIONAL:
63-
if (basic_to_f_p(rb_cRational))
64-
return rat2dbl_without_to_f(num);
65-
break;
66-
}
67-
}
68-
return RFLOAT_VALUE(rb_to_float(num));
69-
}
70-
71-
#define Get_Double(x) num2dbl_with_to_f(x)
27+
#define Get_Double(x) rb_num_to_dbl(x)
7228

7329
#define domain_error(msg) \
7430
rb_raise(rb_eMathDomainError, "Numerical argument is out of domain - " #msg)
@@ -1024,6 +980,5 @@ InitVM_Math(void)
1024980
void
1025981
Init_Math(void)
1026982
{
1027-
id_to_f = rb_intern_const("to_f");
1028983
InitVM(Math);
1029984
}

object.c

+155-42
Original file line numberDiff line numberDiff line change
@@ -2903,33 +2903,84 @@ rb_str_to_dbl(VALUE str, int badcheck)
29032903
return ret;
29042904
}
29052905

2906+
#define fix2dbl_without_to_f(x) (double)FIX2LONG(x)
2907+
#define big2dbl_without_to_f(x) rb_big2dbl(x)
2908+
#define int2dbl_without_to_f(x) \
2909+
(FIXNUM_P(x) ? fix2dbl_without_to_f(x) : big2dbl_without_to_f(x))
2910+
#define rat2dbl_without_to_f(x) \
2911+
(int2dbl_without_to_f(rb_rational_num(x)) / \
2912+
int2dbl_without_to_f(rb_rational_den(x)))
2913+
2914+
#define special_const_to_float(val, pre, post) \
2915+
switch (val) { \
2916+
case Qnil: \
2917+
rb_raise(rb_eTypeError, pre "nil" post); \
2918+
case Qtrue: \
2919+
rb_raise(rb_eTypeError, pre "true" post); \
2920+
case Qfalse: \
2921+
rb_raise(rb_eTypeError, pre "false" post); \
2922+
}
2923+
2924+
static inline void
2925+
conversion_to_float(VALUE val)
2926+
{
2927+
special_const_to_float(val, "can't convert ", " into Float");
2928+
}
2929+
2930+
static inline void
2931+
implicit_conversion_to_float(VALUE val)
2932+
{
2933+
special_const_to_float(val, "no implicit conversion to float from ", "");
2934+
}
2935+
2936+
static int
2937+
to_float(VALUE *valp)
2938+
{
2939+
VALUE val = *valp;
2940+
if (SPECIAL_CONST_P(val)) {
2941+
if (FIXNUM_P(val)) {
2942+
*valp = DBL2NUM(fix2dbl_without_to_f(val));
2943+
return T_FLOAT;
2944+
}
2945+
else if (FLONUM_P(val)) {
2946+
return T_FLOAT;
2947+
}
2948+
else {
2949+
conversion_to_float(val);
2950+
}
2951+
}
2952+
else {
2953+
int type = BUILTIN_TYPE(val);
2954+
switch (type) {
2955+
case T_FLOAT:
2956+
return T_FLOAT;
2957+
case T_BIGNUM:
2958+
*valp = DBL2NUM(big2dbl_without_to_f(val));
2959+
return T_FLOAT;
2960+
case T_RATIONAL:
2961+
*valp = DBL2NUM(rat2dbl_without_to_f(val));
2962+
return T_FLOAT;
2963+
case T_STRING:
2964+
return T_STRING;
2965+
}
2966+
}
2967+
return T_NONE;
2968+
}
2969+
29062970
VALUE
29072971
rb_Float(VALUE val)
29082972
{
2909-
switch (TYPE(val)) {
2910-
case T_FIXNUM:
2911-
return DBL2NUM((double)FIX2LONG(val));
2912-
2973+
switch (to_float(&val)) {
29132974
case T_FLOAT:
29142975
return val;
2915-
2916-
case T_BIGNUM:
2917-
return DBL2NUM(rb_big2dbl(val));
2918-
29192976
case T_STRING:
29202977
return DBL2NUM(rb_str_to_dbl(val, TRUE));
2921-
2922-
case T_NIL:
2923-
rb_raise(rb_eTypeError, "can't convert nil into Float");
2924-
break;
2925-
2926-
default:
2927-
return rb_convert_type(val, T_FLOAT, "Float", "to_f");
29282978
}
2929-
2930-
UNREACHABLE;
2979+
return rb_convert_type(val, T_FLOAT, "Float", "to_f");
29312980
}
29322981

2982+
FUNC_MINIMIZED(static VALUE rb_f_float(VALUE obj, VALUE arg));
2983+
29332984
/*
29342985
* call-seq:
29352986
* Float(arg) -> float
@@ -2948,20 +2999,26 @@ rb_f_float(VALUE obj, VALUE arg)
29482999
return rb_Float(arg);
29493000
}
29503001

2951-
VALUE
2952-
rb_to_float(VALUE val)
3002+
static VALUE
3003+
numeric_to_float(VALUE val)
29533004
{
2954-
if (RB_TYPE_P(val, T_FLOAT)) return val;
29553005
if (!rb_obj_is_kind_of(val, rb_cNumeric)) {
2956-
rb_raise(rb_eTypeError, "can't convert %s into Float",
2957-
NIL_P(val) ? "nil" :
2958-
val == Qtrue ? "true" :
2959-
val == Qfalse ? "false" :
2960-
rb_obj_classname(val));
3006+
rb_raise(rb_eTypeError, "can't convert %"PRIsVALUE" into Float",
3007+
rb_obj_class(val));
29613008
}
29623009
return rb_convert_type(val, T_FLOAT, "Float", "to_f");
29633010
}
29643011

3012+
VALUE
3013+
rb_to_float(VALUE val)
3014+
{
3015+
switch (to_float(&val)) {
3016+
case T_FLOAT:
3017+
return val;
3018+
}
3019+
return numeric_to_float(val);
3020+
}
3021+
29653022
VALUE
29663023
rb_check_to_float(VALUE val)
29673024
{
@@ -2972,26 +3029,75 @@ rb_check_to_float(VALUE val)
29723029
return rb_check_convert_type(val, T_FLOAT, "Float", "to_f");
29733030
}
29743031

2975-
double
2976-
rb_num2dbl(VALUE val)
2977-
{
2978-
switch (TYPE(val)) {
2979-
case T_FLOAT:
2980-
return RFLOAT_VALUE(val);
2981-
2982-
case T_STRING:
2983-
rb_raise(rb_eTypeError, "no implicit conversion to float from string");
2984-
break;
3032+
static ID id_to_f;
29853033

2986-
case T_NIL:
2987-
rb_raise(rb_eTypeError, "no implicit conversion to float from nil");
2988-
break;
3034+
static inline int
3035+
basic_to_f_p(VALUE klass)
3036+
{
3037+
return rb_method_basic_definition_p(klass, id_to_f);
3038+
}
29893039

2990-
default:
2991-
break;
3040+
double
3041+
rb_num_to_dbl(VALUE val)
3042+
{
3043+
if (SPECIAL_CONST_P(val)) {
3044+
if (FIXNUM_P(val)) {
3045+
if (basic_to_f_p(rb_cFixnum))
3046+
return fix2dbl_without_to_f(val);
3047+
}
3048+
else if (FLONUM_P(val)) {
3049+
return rb_float_flonum_value(val);
3050+
}
3051+
else {
3052+
conversion_to_float(val);
3053+
}
29923054
}
3055+
else {
3056+
switch (BUILTIN_TYPE(val)) {
3057+
case T_FLOAT:
3058+
return rb_float_noflonum_value(val);
3059+
case T_BIGNUM:
3060+
if (basic_to_f_p(rb_cBignum))
3061+
return big2dbl_without_to_f(val);
3062+
break;
3063+
case T_RATIONAL:
3064+
if (basic_to_f_p(rb_cRational))
3065+
return rat2dbl_without_to_f(val);
3066+
break;
3067+
}
3068+
}
3069+
val = numeric_to_float(val);
3070+
return RFLOAT_VALUE(val);
3071+
}
29933072

2994-
return RFLOAT_VALUE(rb_Float(val));
3073+
double
3074+
rb_num2dbl(VALUE val)
3075+
{
3076+
if (SPECIAL_CONST_P(val)) {
3077+
if (FIXNUM_P(val)) {
3078+
return fix2dbl_without_to_f(val);
3079+
}
3080+
else if (FLONUM_P(val)) {
3081+
return rb_float_flonum_value(val);
3082+
}
3083+
else {
3084+
implicit_conversion_to_float(val);
3085+
}
3086+
}
3087+
else {
3088+
switch (BUILTIN_TYPE(val)) {
3089+
case T_FLOAT:
3090+
return rb_float_noflonum_value(val);
3091+
case T_BIGNUM:
3092+
return big2dbl_without_to_f(val);
3093+
case T_RATIONAL:
3094+
return rat2dbl_without_to_f(val);
3095+
case T_STRING:
3096+
rb_raise(rb_eTypeError, "no implicit conversion to float from string");
3097+
}
3098+
}
3099+
val = rb_convert_type(val, T_FLOAT, "Float", "to_f");
3100+
return RFLOAT_VALUE(val);
29953101
}
29963102

29973103
VALUE
@@ -3243,7 +3349,7 @@ rb_f_hash(VALUE obj, VALUE arg)
32433349
*/
32443350

32453351
void
3246-
Init_Object(void)
3352+
InitVM_Object(void)
32473353
{
32483354
Init_class_hierarchy();
32493355

@@ -3461,3 +3567,10 @@ Init_Object(void)
34613567
*/
34623568
rb_define_global_const("FALSE", Qfalse);
34633569
}
3570+
3571+
void
3572+
Init_Object(void)
3573+
{
3574+
id_to_f = rb_intern_const("to_f");
3575+
InitVM(Object);
3576+
}

0 commit comments

Comments
 (0)