Debian Codegraph @

ext/-test-/rational/rat.c

raw
1 #include "internal/rational.h"
2
3 #if defined(HAVE_LIBGMP) && defined(HAVE_GMP_H)
4 static VALUE
5 big(VALUE x)
6 {
7 if (FIXNUM_P(x))
8 return rb_int2big(FIX2LONG(x));
9 if (RB_TYPE_P(x, T_BIGNUM))
10 return x;
11 rb_raise(rb_eTypeError, "can't convert %s to Bignum",
12 rb_obj_classname(x));
13 }
14 #endif
15
16 static VALUE
17 gcd_normal(VALUE klass, VALUE x, VALUE y)
18 {
19 return rb_big_norm(rb_gcd_normal(rb_to_int(x), rb_to_int(y)));
20 }
21
22 #if defined(HAVE_LIBGMP) && defined(HAVE_GMP_H)
23 static VALUE
24 gcd_gmp(VALUE klass, VALUE x, VALUE y)
25 {
26 return rb_big_norm(rb_gcd_gmp(big(x), big(y)));
27 }
28 #else
29 #define gcd_gmp rb_f_notimplement
30 #endif
31
32 static VALUE
33 s_rational_raw(VALUE klass, VALUE x, VALUE y)
34 {
35 return rb_rational_raw(x, y);
36 }
37
38 void
39 Init_rational(void)
40 {
41 VALUE mBug = rb_define_module("Bug");
42 VALUE klass = rb_define_module_under(mBug, "Rational");
43
44 rb_define_singleton_method(klass, "gcd_normal", gcd_normal, 2);
45 rb_define_singleton_method(klass, "gcd_gmp", gcd_gmp, 2);
46
47 rb_define_singleton_method(klass, "raw", s_rational_raw, 2);
48 }