srawlins-gmp 0.1.5 → 0.1.5.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (6) hide show
  1. data/CHANGELOG +13 -1
  2. data/ext/gmpf.c +423 -0
  3. data/ext/gmpq.c +679 -0
  4. data/ext/gmpz.c +1598 -0
  5. data/ext/ruby_gmp.h +192 -0
  6. metadata +5 -1
@@ -0,0 +1,192 @@
1
+ #ifndef _RUBY_GMP_H_
2
+ #define _RUBY_GMP_H_
3
+
4
+ #define _GNU_SOURCE
5
+ #include <stdio.h>
6
+
7
+ #include <ruby.h>
8
+ #include <gmp.h>
9
+
10
+ #ifdef MPFR
11
+
12
+ #ifdef HAVE_MPFR_H
13
+ #include <mpfr.h>
14
+ #endif /* HAVE_MPFR_H */
15
+
16
+ #ifdef HAVE_MPF2MPFR_H
17
+ #include <mpf2mpfr.h>
18
+ #endif /* HAVE_MPF2MPFR_H */
19
+
20
+ #endif /* MPFR */
21
+
22
+ #include <stdlib.h>
23
+
24
+ /*
25
+ MP_INT*, MP_RAT* and MP_FLOAT* are used because they don't have side-effects
26
+ of single-element arrays mp*_t
27
+
28
+ MP_FLOAT is defined here, as it's commented out in gmp.h
29
+ */
30
+ #if defined(MPFR) && defined(HAVE_MPFR_H)
31
+ typedef __mpfr_struct MP_FLOAT;
32
+ #else
33
+ typedef __mpf_struct MP_FLOAT;
34
+ #endif /* HAVE_MPF2MPFR_H */
35
+
36
+ #define mpz_get_struct(ruby_var,c_var) { Data_Get_Struct(ruby_var, MP_INT, c_var); }
37
+ #define mpq_get_struct(ruby_var,c_var) { Data_Get_Struct(ruby_var, MP_RAT, c_var); }
38
+ #define mpf_get_struct(ruby_var,c_var) { Data_Get_Struct(ruby_var, MP_FLOAT, c_var); }
39
+ #define mpf_get_struct_prec(ruby_var,c_var,prec) { mpf_get_struct(ruby_var,c_var); prec = mpf_get_prec(c_var); }
40
+ #define mpz_make_struct(ruby_var,c_var) { ruby_var = Data_Make_Struct(cGMP_Z, MP_INT, 0, r_gmpz_free, c_var); }
41
+ #define mpq_make_struct(ruby_var,c_var) { ruby_var = Data_Make_Struct(cGMP_Q, MP_RAT, 0, r_gmpq_free, c_var); }
42
+ #define mpf_make_struct(ruby_var,c_var) { ruby_var = Data_Make_Struct(cGMP_F, MP_FLOAT, 0, r_gmpf_free, c_var); }
43
+ #define mpz_make_struct_init(ruby_var,c_var) { mpz_make_struct(ruby_var,c_var); mpz_init (c_var); }
44
+ #define mpq_make_struct_init(ruby_var,c_var) { mpq_make_struct(ruby_var,c_var); mpq_init (c_var); }
45
+ #define mpf_make_struct_init(ruby_var,c_var,prec) { mpf_make_struct(ruby_var,c_var); mpf_init2 (c_var,prec); }
46
+ #define BIGNUM_P(value) (TYPE(value) == T_BIGNUM)
47
+ #define FLOAT_P(value) (TYPE(value) == T_FLOAT)
48
+ #define STRING_P(value) (TYPE(value) == T_STRING)
49
+ #define GMPZ_P(value) (rb_obj_is_instance_of(value, cGMP_Z) == Qtrue)
50
+ #define GMPQ_P(value) (rb_obj_is_instance_of(value, cGMP_Q) == Qtrue)
51
+ #define GMPF_P(value) (rb_obj_is_instance_of(value, cGMP_F) == Qtrue)
52
+ #define mpz_set_bignum(var_mpz,var_bignum) \
53
+ mpz_set_str (var_mpz, STR2CSTR (rb_funcall (var_bignum, rb_intern ("to_s"), 0)), 0);
54
+ #define mpz_temp_alloc(var) { var=malloc(sizeof(MP_INT)); }
55
+ #define mpz_temp_init(var) { mpz_temp_alloc(var); mpz_init(var); }
56
+ #define mpz_temp_from_bignum(var,var_bignum) \
57
+ { mpz_temp_alloc(var); mpz_init_set_str (var, STR2CSTR (rb_funcall (var_bignum, rb_intern ("to_s"), 0)), 0); }
58
+ #define mpz_temp_free(var) { mpz_clear(var); free(var); }
59
+ #define mpf_temp_alloc(var) { var=malloc(sizeof(MP_FLOAT)); }
60
+ #define mpf_temp_init(var,prec) { mpf_temp_alloc(var); mpf_init2(var,prec); }
61
+ #define mpf_temp_free(var) { mpf_clear(var); free(var); }
62
+ #define FLT2DBL(var) (RFLOAT(var)->value)
63
+ #define prec_max(prec,var) {if(mpf_get_prec(var) > prec) prec = mpf_get_prec(var); }
64
+
65
+ #define EXPECTED_ZQFXBD "Expected GMP::Z, GMP::Q, GMP::F, FixNum, BigNum or Float"
66
+ #define EXPECTED_ZQFXB "Expected GMP::Z, GMP::Q, GMP::F, FixNum or BigNum"
67
+ #define EXPECTED_ZXB "Expected GMP::Z, FixNum or BigNum"
68
+ #define EXPECTED_ZX "Expected GMP::Z or FixNum"
69
+ #define EXPECTED_X "Expected FixNum"
70
+ #define typeerror(expected) rb_raise(rb_eTypeError, EXPECTED_##expected)
71
+ #define typeerror_as(expected, argname) rb_raise(rb_eTypeError, EXPECTED_##expected " as " argname)
72
+
73
+ //should change exception type
74
+ #define not_yet rb_raise(rb_eTypeError,"Not implemented yet")
75
+
76
+ extern VALUE mGMP, cGMP_Z, cGMP_Q, cGMP_F;
77
+
78
+ extern void r_gmpz_free(void *ptr);
79
+ extern void r_gmpq_free(void *ptr);
80
+ extern void r_gmpf_free(void *ptr);
81
+
82
+
83
+ /* from gmpz.h */
84
+
85
+ // Initializing, Assigning Integers
86
+ extern VALUE r_gmpzsg_new(int argc, VALUE *argv, VALUE klass);
87
+ extern void mpz_set_value(MP_INT *target, VALUE source);
88
+ extern VALUE r_gmpmod_z(int argc, VALUE *argv, VALUE module);
89
+ extern VALUE r_gmpz_swap(VALUE self, VALUE arg);
90
+
91
+ // Converting Integers
92
+ extern VALUE r_gmpz_to_i(VALUE self);
93
+ extern VALUE r_gmpz_to_d(VALUE self);
94
+
95
+ // Integer Arithmetic
96
+ extern VALUE r_gmpz_add(VALUE self, VALUE arg);
97
+ extern VALUE r_gmpz_add_self(VALUE self, VALUE arg);
98
+ extern VALUE r_gmpz_sub(VALUE self, VALUE arg);
99
+ extern VALUE r_gmpz_sub_self(VALUE self, VALUE arg);
100
+ extern VALUE r_gmpz_mul(VALUE self, VALUE arg);
101
+
102
+ // Number Theoretic Functions
103
+ extern VALUE r_gmpz_remove(VALUE self, VALUE arg);
104
+
105
+ // Integer Comparisons
106
+ extern VALUE r_gmpz_eq(VALUE self, VALUE arg);
107
+ extern VALUE r_gmpz_cmp(VALUE self, VALUE arg);
108
+ extern VALUE r_gmpz_cmpabs(VALUE self, VALUE arg);
109
+
110
+ // _unsorted_
111
+ extern VALUE r_gmpz_div(VALUE self, VALUE arg);
112
+ extern VALUE r_gmpz_is_probab_prime(int argc, VALUE* argv, VALUE self);
113
+ extern VALUE r_gmpz_popcount(VALUE self);
114
+ extern VALUE r_gmpz_jacobi(VALUE self);
115
+ extern VALUE r_gmpz_legendre(VALUE self);
116
+ extern VALUE r_gmpz_setbit(VALUE self, VALUE bitnr, VALUE set_to);
117
+ extern VALUE r_gmpz_getbit(VALUE self, VALUE bitnr);
118
+ extern VALUE r_gmpz_scan0(VALUE self, VALUE bitnr);
119
+ extern VALUE r_gmpz_scan1(VALUE self, VALUE bitnr);
120
+ extern VALUE r_gmpz_powm(VALUE self, VALUE exp, VALUE mod);
121
+ extern VALUE r_gmpz_sgn(VALUE self);
122
+ extern int mpz_cmp_value(MP_INT *OP, VALUE arg);
123
+
124
+ extern VALUE r_gmpzsg_pow(VALUE klass, VALUE base, VALUE exp);
125
+ extern VALUE r_gmpz_to_s(VALUE self);
126
+
127
+
128
+ /* from gmpq.h */
129
+
130
+ // Initializing Rationals
131
+ extern VALUE r_gmpmod_q(int argc, VALUE *argv, VALUE module);
132
+ extern VALUE r_gmpq_swap(VALUE self, VALUE arg);
133
+
134
+ // Rational Conversions
135
+ extern VALUE r_gmpq_to_d(VALUE self);
136
+
137
+ // Rational Arithmetic
138
+ extern VALUE r_gmpq_add(VALUE self, VALUE arg);
139
+ extern VALUE r_gmpq_sub(VALUE self, VALUE arg);
140
+ extern VALUE r_gmpq_mul(VALUE self, VALUE arg);
141
+ extern VALUE r_gmpq_div(VALUE self, VALUE arg);
142
+ extern VALUE r_gmpq_neg(VALUE self);
143
+ extern VALUE r_gmpq_neg_self(VALUE self);
144
+ extern VALUE r_gmpq_abs(VALUE self);
145
+ extern VALUE r_gmpq_abs_self(VALUE self);
146
+ extern VALUE r_gmpq_inv(VALUE self);
147
+ extern VALUE r_gmpq_inv_self(VALUE self);
148
+
149
+ // Comparing Rationals
150
+ extern VALUE r_gmpq_eq(VALUE self, VALUE arg);
151
+ extern VALUE r_gmpq_cmp(VALUE self, VALUE arg);
152
+ extern int mpq_cmp_value(MP_RAT *OP, VALUE arg);
153
+ extern VALUE r_gmpq_sgn(VALUE self);
154
+
155
+ // Applying Integer Functions
156
+ extern VALUE r_gmpq_num(VALUE self);
157
+ extern VALUE r_gmpq_den(VALUE self);
158
+
159
+ // _unsorted_
160
+
161
+
162
+ /* from gmpf.h */
163
+
164
+ // Initializing, Assigning Floats
165
+ extern void mpf_set_value(MP_FLOAT *self_val, VALUE arg);
166
+ extern VALUE r_gmpmod_f(int argc, VALUE *argv, VALUE module);
167
+
168
+ // Converting Floats
169
+ extern VALUE r_gmpf_to_d(VALUE self);
170
+ extern VALUE r_gmpf_to_s(VALUE self);
171
+
172
+ // Float Arithmetic
173
+ extern VALUE r_gmpf_add(VALUE self, VALUE arg);
174
+ extern VALUE r_gmpf_sub(VALUE self, VALUE arg);
175
+ extern VALUE r_gmpf_mul(VALUE self, VALUE arg);
176
+ extern VALUE r_gmpf_div(VALUE self, VALUE arg);
177
+
178
+ // Float Comparison
179
+ extern VALUE r_gmpf_eq(VALUE self, VALUE arg);
180
+ extern VALUE r_gmpf_cmp(VALUE self, VALUE arg);
181
+ extern int mpf_cmp_value(MP_FLOAT *OP, VALUE arg);
182
+
183
+ // _unsorted_
184
+ extern VALUE r_gmpf_sgn(VALUE self);
185
+ extern VALUE r_gmpf_get_prec(VALUE self);
186
+
187
+
188
+ extern void init_gmpz();
189
+ extern void init_gmpq();
190
+ extern void init_gmpf();
191
+
192
+ #endif
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: srawlins-gmp
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.5
4
+ version: 0.1.5.1
5
5
  platform: ruby
6
6
  authors:
7
7
  - Tomasz Wegrzanowski
@@ -27,9 +27,13 @@ extra_rdoc_files: []
27
27
  files:
28
28
  - ext/extconf.rb
29
29
  - ext/gmp.c
30
+ - ext/gmpf.c
30
31
  - ext/gmpf.h
32
+ - ext/gmpq.c
31
33
  - ext/gmpq.h
34
+ - ext/gmpz.c
32
35
  - ext/gmpz.h
36
+ - ext/ruby_gmp.h
33
37
  - ext/takeover.h
34
38
  - test/README
35
39
  - test/tc_cmp.rb