aboutsummaryrefslogtreecommitdiffstats
path: root/bignum.c
diff options
context:
space:
mode:
authormrkn <mrkn@b2dd03c8-39d4-4d8f-98ff-823fe69b080e>2016-11-11 15:55:30 +0000
committermrkn <mrkn@b2dd03c8-39d4-4d8f-98ff-823fe69b080e>2016-11-11 15:55:30 +0000
commit944e6a2804e7d3961964c586694d16ce036da55d (patch)
tree307f02a0d50d4ffdb9e82df1c345d072527b2b14 /bignum.c
parent8492e990ab03eb8eb4415ac5ab05fb024349d7a3 (diff)
downloadruby-944e6a2804e7d3961964c586694d16ce036da55d.tar.gz
rational.c: avoid needless object allocation with nurat_to_double
* rational.c (nurat_to_double): introduce to convert rational to double without object allocation. * rational.c (rb_rational_plus, nurat_{sub,mul,to_f}): rewrite by using nurat_to_double. * bignum.c (rb_big_fdiv_double): introduce to calculate fdiv and return the result as a double value. * bignum.c (big_fdiv{,_int,_float}): change the return types for implementing rb_big_fdiv_double. * bignum.c (rb_big_fdiv): rewrite by using rb_big_fdiv_double. * numeric.c (rb_int_fdiv_double): introduce to calculate fdiv and return the result as a double value. * numeric.c (fix_fdiv_double): rewrite from fix_fdiv to return the result as a double value. * numeric.c (rb_int_fdiv): rewrite by using rb_int_fdiv_double. * internal.h (rb_{big,int}_fdiv_double): exported. git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@56719 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
Diffstat (limited to 'bignum.c')
-rw-r--r--bignum.c28
1 files changed, 17 insertions, 11 deletions
diff --git a/bignum.c b/bignum.c
index 18919d09c3..c0847e620d 100644
--- a/bignum.c
+++ b/bignum.c
@@ -6090,7 +6090,7 @@ big_shift(VALUE x, long n)
return x;
}
-static VALUE
+static double
big_fdiv(VALUE x, VALUE y, long ey)
{
#define DBL_BIGDIG ((DBL_MANT_DIG + BITSPERDIG) / BITSPERDIG)
@@ -6108,14 +6108,14 @@ big_fdiv(VALUE x, VALUE y, long ey)
#if SIZEOF_LONG > SIZEOF_INT
{
/* Visual C++ can't be here */
- if (l > INT_MAX) return DBL2NUM(INFINITY);
- if (l < INT_MIN) return DBL2NUM(0.0);
+ if (l > INT_MAX) return INFINITY;
+ if (l < INT_MIN) return 0.0;
}
#endif
- return DBL2NUM(ldexp(big2dbl(z), (int)l));
+ return ldexp(big2dbl(z), (int)l);
}
-static VALUE
+static double
big_fdiv_int(VALUE x, VALUE y)
{
long l, ey;
@@ -6127,7 +6127,7 @@ big_fdiv_int(VALUE x, VALUE y)
return big_fdiv(x, y, ey);
}
-static VALUE
+static double
big_fdiv_float(VALUE x, VALUE y)
{
int i;
@@ -6135,8 +6135,8 @@ big_fdiv_float(VALUE x, VALUE y)
return big_fdiv(x, y, i - DBL_MANT_DIG);
}
-VALUE
-rb_big_fdiv(VALUE x, VALUE y)
+double
+rb_big_fdiv_double(VALUE x, VALUE y)
{
double dx, dy;
@@ -6154,14 +6154,20 @@ rb_big_fdiv(VALUE x, VALUE y)
else if (RB_FLOAT_TYPE_P(y)) {
dy = RFLOAT_VALUE(y);
if (isnan(dy))
- return y;
+ return dy;
if (isinf(dx))
return big_fdiv_float(x, y);
}
else {
- return rb_num_coerce_bin(x, y, rb_intern("fdiv"));
+ return RFLOAT_VALUE(rb_num_coerce_bin(x, y, rb_intern("fdiv")));
}
- return DBL2NUM(dx / dy);
+ return dx / dy;
+}
+
+VALUE
+rb_big_fdiv(VALUE x, VALUE y)
+{
+ return DBL2NUM(rb_big_fdiv_double(x, y));
}
VALUE