summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authortadf <tadf@b2dd03c8-39d4-4d8f-98ff-823fe69b080e>2008-03-19 13:29:04 +0000
committertadf <tadf@b2dd03c8-39d4-4d8f-98ff-823fe69b080e>2008-03-19 13:29:04 +0000
commita3c76eb0c7d7b6d9ff51d42269486d59e5ac809f (patch)
tree85028648ef1c50b6c10b905abd6835de295fadc0
parent1484d1c32a81dc90dd4372defe43445ae1ac0aaa (diff)
added rb_gcd.
git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@15807 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
-rw-r--r--ChangeLog12
-rw-r--r--complex.c77
-rw-r--r--rational.c6
3 files changed, 20 insertions, 75 deletions
diff --git a/ChangeLog b/ChangeLog
index 18c6107faf..626d59abda 100644
--- a/ChangeLog
+++ b/ChangeLog
@@ -1,3 +1,15 @@
+Wed Mar 19 22:27:41 2008 Tadayoshi Funaba <tadf@dotrb.org>
+
+ * rational.c: added rb_gcd.
+
+ * complex.c: use rb_gcd.
+
+Wed Mar 19 18:37:00 2008 Tadayoshi Funaba <tadf@dotrb.org>
+
+ * complex.c: revert.
+
+ * rational.c: revert.
+
Wed Mar 19 17:31:20 2008 Yukihiro Matsumoto <matz@ruby-lang.org>
* eval_intern.h (TH_EXEC_TAG): need not to FLUSH_REGISTER_WINDOWS.
diff --git a/complex.c b/complex.c
index e69cf29cc4..db8e04b150 100644
--- a/complex.c
+++ b/complex.c
@@ -813,80 +813,7 @@ nucomp_inexact_p(VALUE self)
return f_boolcast(!nucomp_exact_p(self));
}
-inline static long
-i_gcd(long x, long y)
-{
- long b;
-
- if (x < 0)
- x = -x;
- if (y < 0)
- y = -y;
-
- if (x == 0)
- return y;
- if (y == 0)
- return x;
-
- b = 0;
- while ((x & 1) == 0 && (y & 1) == 0) {
- b += 1;
- x >>= 1;
- y >>= 1;
- }
-
- while ((x & 1) == 0)
- x >>= 1;
-
- while ((y & 1) == 0)
- y >>= 1;
-
- while (x != y) {
- if (y > x) {
- long t;
- t = x;
- x = y;
- y = t;
- }
- x -= y;
- while ((x & 1) == 0)
- x >>= 1;
- }
-
- return x << b;
-}
-
-inline static VALUE
-f_gcd(VALUE x, VALUE y)
-{
- VALUE z;
-
- if (FIXNUM_P(x) && FIXNUM_P(y))
- return LONG2NUM(i_gcd(FIX2LONG(x), FIX2LONG(y)));
-
- if (f_negative_p(x))
- x = f_negate(x);
- if (f_negative_p(y))
- y = f_negate(y);
-
- if (f_zero_p(x))
- return y;
- if (f_zero_p(y))
- return x;
-
- for (;;) {
- if (FIXNUM_P(x)) {
- if (FIX2INT(x) == 0)
- return y;
- if (FIXNUM_P(y))
- return LONG2NUM(i_gcd(FIX2LONG(x), FIX2LONG(y)));
- }
- z = x;
- x = f_mod(y, x);
- y = z;
- }
- /* NOTREACHED */
-}
+extern VALUE rb_gcd(VALUE x, VALUE y);
static VALUE
f_lcm(VALUE x, VALUE y)
@@ -894,7 +821,7 @@ f_lcm(VALUE x, VALUE y)
if (f_zero_p(x) || f_zero_p(y))
return ZERO;
else
- return f_abs(f_mul(f_div(x, f_gcd(x, y)), y));
+ return f_abs(f_mul(f_div(x, rb_gcd(x, y)), y));
}
static VALUE
diff --git a/rational.c b/rational.c
index 01b336a841..de028ee041 100644
--- a/rational.c
+++ b/rational.c
@@ -133,6 +133,12 @@ f_gcd(VALUE x, VALUE y)
/* NOTREACHED */
}
+VALUE
+rb_gcd(VALUE x, VALUE y)
+{
+ return f_gcd(x, y);
+}
+
#define get_dat1(x) \
struct RRational *dat;\
dat = ((struct RRational *)(x))