examples: annotate examples
[nit.git] / lib / gmp / native_gmp.nit
1 # This file is part of NIT ( http://www.nitlanguage.org ).
2 #
3 # Licensed under the Apache License, Version 2.0 (the "License");
4 # you may not use this file except in compliance with the License.
5 # You may obtain a copy of the License at
6 #
7 # http://www.apache.org/licenses/LICENSE-2.0
8 #
9 # Unless required by applicable law or agreed to in writing, software
10 # distributed under the License is distributed on an "AS IS" BASIS,
11 # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 # See the License for the specific language governing permissions and
13 # limitations under the License.
14
15 # Low-level GMP features
16 module native_gmp is ldflags("-lgmp")
17
18 in "C header" `{
19 #include <gmp.h>
20 `}
21
22 # Multi precision Integer
23 extern class NativeMPZ `{mpz_ptr`}
24
25 # Initializing
26
27 new `{
28 mpz_ptr self = (mpz_ptr)malloc(sizeof(mpz_t));
29 mpz_init(self);
30 return self;
31 `}
32
33 # Arithmetic Functions
34
35 fun add(res, op: NativeMPZ) `{
36 mpz_add(res, self, op);
37 `}
38
39 fun add_ui(res: NativeMPZ, op: UInt64) `{
40 mpz_add_ui(res, self, *op);
41 `}
42
43 fun sub(res, op: NativeMPZ) `{
44 mpz_sub(res, self, op);
45 `}
46
47 fun sub_ui(res: NativeMPZ, op: UInt64) `{
48 mpz_sub_ui(res, self, *op);
49 `}
50
51 fun mul(res, op: NativeMPZ) `{
52 mpz_mul(res, self, op);
53 `}
54
55 fun mul_si(res: NativeMPZ, op: Int) `{
56 mpz_mul_si(res, self, op);
57 `}
58
59 fun neg(res: NativeMPZ) `{
60 mpz_neg(res, self);
61 `}
62
63 fun abs(res: NativeMPZ) `{
64 mpz_abs(res, self);
65 `}
66
67 fun tdiv_q(res, op: NativeMPZ) `{
68 mpz_tdiv_q(res, self, op);
69 `}
70
71 fun tdiv_q_ui(res: NativeMPZ, op: UInt64) `{
72 mpz_tdiv_q_ui(res, self, *op);
73 `}
74
75 fun mod(res, op: NativeMPZ) `{
76 mpz_mod(res, self, op);
77 `}
78
79 fun mod_ui(res: NativeMPZ, op: UInt64) `{
80 mpz_mod_ui(res, self, *op);
81 `}
82
83 fun pow_ui(res: NativeMPZ, op: UInt64) `{
84 mpz_pow_ui(res, self, *op);
85 `}
86
87 #Number Theoretic Functions
88
89 fun probab_prime_p(reps: Int32): Int `{
90 return mpz_probab_prime_p(self, reps);
91 `}
92
93 fun nextprime(res: NativeMPZ) `{
94 mpz_nextprime(res, self);
95 `}
96
97 fun gcd(res, op: NativeMPZ) `{
98 mpz_gcd(res, self, op);
99 `}
100
101 fun gcd_ui(res: NativeMPZ, op: UInt64) `{
102 mpz_gcd_ui(res, self, *op);
103 `}
104
105 # Comparison Functions
106
107 fun cmp(op: NativeMPZ): Int `{
108 return mpz_cmp(self, op);
109 `}
110
111 fun cmp_si(op: Int): Int `{
112 return mpz_cmp_si(self, op);
113 `}
114
115 # Assignment
116
117 fun set(op: NativeMPZ) `{ mpz_set(self, op); `}
118
119 fun set_si(op: Int) `{ mpz_set_si(self, op); `}
120
121 fun set_d(op: Float) `{ mpz_set_d(self, op); `}
122
123 fun set_q(op: NativeMPQ) `{ mpz_set_q(self, op); `}
124
125 fun set_str(str: CString, base: Int32) `{
126 mpz_set_str(self, str, base);
127 `}
128
129 fun swap(op: NativeMPZ) `{ mpz_swap(self, op); `}
130
131 # Conversion Functions
132
133 fun get_si: Int `{ return mpz_get_si(self); `}
134
135 fun get_d: Float `{ return mpz_get_d(self); `}
136
137 fun get_str(base: Int32): CString `{
138 return mpz_get_str(NULL, base, self);
139 `}
140
141 # Delete this NativeMPZ
142 fun finalize `{
143 mpz_clear(self);
144 free(self);
145 `}
146 end
147
148 # Multi precision Rational
149 extern class NativeMPQ `{mpq_ptr`}
150
151 # Initializing
152
153 new `{
154 mpq_ptr self = (mpq_ptr)malloc(sizeof(mpq_t));
155 mpq_init(self);
156 return self;
157 `}
158
159 # Arithmetic Functions
160
161 fun add(res, op: NativeMPQ) `{
162 mpq_add(res, self, op);
163 `}
164
165 fun sub(res, op: NativeMPQ) `{
166 mpq_sub(res, self, op);
167 `}
168
169 fun mul(res, op: NativeMPQ) `{
170 mpq_mul(res, self, op);
171 `}
172
173 fun div(res, op: NativeMPQ) `{
174 mpq_div(res, self, op);
175 `}
176
177 fun neg(res: NativeMPQ) `{
178 mpq_neg(res, self);
179 `}
180
181 fun abs(res: NativeMPQ) `{
182 mpq_abs(res, self);
183 `}
184
185 fun inv(res: NativeMPQ) `{
186 mpq_inv(res, self);
187 `}
188
189 # Assignment
190
191 fun set(op: NativeMPQ) `{ mpq_set(self, op); `}
192
193 fun set_z(op: NativeMPZ) `{ mpq_set_z(self, op); `}
194
195 fun set_si(op1: Int, op2: Int) `{
196 mpq_set_si(self, op1, op2);
197 mpq_canonicalize(self);
198 `}
199
200 fun set_d(op: Float) `{ mpq_set_d(self, op); `}
201
202 fun set_str(str: CString) `{
203 mpq_set_str(self, str, 10);
204 mpq_canonicalize(self);
205 `}
206
207 fun swap(op: NativeMPQ) `{ mpq_swap(self, op); `}
208
209 # Convertion Functions
210
211 fun get_d: Float `{ return mpq_get_d(self); `}
212
213 fun get_str(base: Int32): CString `{
214 return mpq_get_str(NULL, base, self);
215 `}
216
217 # Comparison Functions
218
219 fun cmp(op: NativeMPQ): Int `{
220 return mpq_cmp(self, op);
221 `}
222
223 # fun cmp_z(op: NativeMPZ): Int `{
224 # return mpq_cmp_z(self, op);
225 # `}
226
227 fun cmp_si(num: Int, den: Int): Int `{
228 return mpq_cmp_si(self, num, den);
229 `}
230
231 fun equal(op: NativeMPQ): Bool `{
232 return mpq_equal(self, op);
233 `}
234
235 # Getter
236
237 fun numref: NativeMPZ `{
238 return mpq_numref(self);
239 `}
240
241 fun denref: NativeMPZ `{
242 return mpq_denref(self);
243 `}
244
245 # Delete this NativeMPZ
246 fun finalize `{
247 mpq_clear(self);
248 free(self);
249 `}
250 end
251
252 # Boxing of C Unsigned Long
253 extern class UInt64 `{ uint64_t* `}
254 new `{
255 uint64_t *self = (uint64_t*)malloc(sizeof(uint64_t));
256 return self;
257 `}
258
259 fun set_si(val: Int) `{
260 *self = (uint64_t)val;
261 `}
262 end