1 # This file is part of NIT ( http://www.nitlanguage.org ).
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
7 # http://www.apache.org/licenses/LICENSE-2.0
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.
15 # Low-level GMP features
16 module native_gmp
is ldflags
("-lgmp")
22 # Multi precision Integer
23 extern class NativeMPZ `{mpz_ptr`}
28 mpz_ptr
self = (mpz_ptr
)malloc
(sizeof
(mpz_t
));
33 # Arithmetic Functions
35 fun add(res, op: NativeMPZ) `{
36 mpz_add
(res
, self, op
);
39 fun add_ui(res: NativeMPZ, op: UInt64) `{
40 mpz_add_ui
(res
, self, *op
);
43 fun sub(res, op: NativeMPZ) `{
44 mpz_sub
(res
, self, op
);
47 fun sub_ui(res: NativeMPZ, op: UInt64) `{
48 mpz_sub_ui
(res
, self, *op
);
51 fun mul(res, op: NativeMPZ) `{
52 mpz_mul
(res
, self, op
);
55 fun mul_si(res: NativeMPZ, op: Int) `{
56 mpz_mul_si
(res
, self, op
);
59 fun neg(res: NativeMPZ) `{
63 fun abs(res: NativeMPZ) `{
67 fun tdiv_q(res, op: NativeMPZ) `{
68 mpz_tdiv_q
(res
, self, op
);
71 fun tdiv_q_ui(res: NativeMPZ, op: UInt64) `{
72 mpz_tdiv_q_ui
(res
, self, *op
);
75 fun mod(res, op: NativeMPZ) `{
76 mpz_mod
(res
, self, op
);
79 fun mod_ui(res: NativeMPZ, op: UInt64) `{
80 mpz_mod_ui
(res
, self, *op
);
83 fun pow_ui(res: NativeMPZ, op: UInt64) `{
84 mpz_pow_ui
(res
, self, *op
);
87 #Number Theoretic Functions
89 fun probab_prime_p(reps: Int32): Int `{
90 return mpz_probab_prime_p
(self, reps
);
93 fun nextprime(res: NativeMPZ) `{
94 mpz_nextprime
(res
, self);
97 fun gcd(res, op: NativeMPZ) `{
98 mpz_gcd
(res
, self, op
);
101 fun gcd_ui(res: NativeMPZ, op: UInt64) `{
102 mpz_gcd_ui
(res
, self, *op
);
105 # Comparison Functions
107 fun cmp(op: NativeMPZ): Int `{
108 return mpz_cmp
(self, op
);
111 fun cmp_si(op: Int): Int `{
112 return mpz_cmp_si
(self, op
);
117 fun set(op: NativeMPZ) `{ mpz_set(self, op); `}
119 fun set_si
(op
: Int) `{ mpz_set_si(self, op); `}
121 fun set_d(op: Float) `{ mpz_set_d(self, op); `}
123 fun set_q
(op
: NativeMPQ) `{ mpz_set_q(self, op); `}
125 fun set_str(str: CString, base: Int32) `{
126 mpz_set_str
(self, str
, base
);
129 fun swap(op: NativeMPZ) `{ mpz_swap(self, op); `}
131 # Conversion Functions
133 fun get_si
: Int `{ return mpz_get_si(self); `}
135 fun get_d: Float `{ return mpz_get_d(self); `}
137 fun get_str
(base
: Int32): CString `{
138 return mpz_get_str(NULL, base, self);
141 # Delete this NativeMPZ
148 # Multi precision Rational
149 extern class NativeMPQ `{mpq_ptr`}
154 mpq_ptr
self = (mpq_ptr
)malloc
(sizeof
(mpq_t
));
159 # Arithmetic Functions
161 fun add(res, op: NativeMPQ) `{
162 mpq_add
(res
, self, op
);
165 fun sub(res, op: NativeMPQ) `{
166 mpq_sub
(res
, self, op
);
169 fun mul(res, op: NativeMPQ) `{
170 mpq_mul
(res
, self, op
);
173 fun div(res, op: NativeMPQ) `{
174 mpq_div
(res
, self, op
);
177 fun neg(res: NativeMPQ) `{
181 fun abs(res: NativeMPQ) `{
185 fun inv(res: NativeMPQ) `{
191 fun set(op: NativeMPQ) `{ mpq_set(self, op); `}
193 fun set_z
(op
: NativeMPZ) `{ mpq_set_z(self, op); `}
195 fun set_si(op1: Int, op2: Int) `{
196 mpq_set_si
(self, op1
, op2
);
197 mpq_canonicalize
(self);
200 fun set_d(op: Float) `{ mpq_set_d(self, op); `}
202 fun set_str
(str
: CString) `{
203 mpq_set_str(self, str, 10);
204 mpq_canonicalize(self);
207 fun swap
(op
: NativeMPQ) `{ mpq_swap(self, op); `}
209 # Convertion Functions
211 fun get_d: Float `{ return mpq_get_d(self); `}
213 fun get_str
(base
: Int32): CString `{
214 return mpq_get_str(NULL, base, self);
217 # Comparison Functions
219 fun cmp
(op
: NativeMPQ): Int `{
220 return mpq_cmp(self, op);
223 # fun cmp_z(op: NativeMPZ): Int `{
224 # return mpq_cmp_z(self, op);
227 fun cmp_si
(num
: Int, den
: Int): Int `{
228 return mpq_cmp_si(self, num, den);
231 fun equal
(op
: NativeMPQ): Bool `{
232 return mpq_equal(self, op);
237 fun numref
: NativeMPZ `{
238 return mpq_numref(self);
241 fun denref
: NativeMPZ `{
242 return mpq_denref(self);
245 # Delete this NativeMPZ
252 # Boxing of C Unsigned Long
253 extern class UInt64 `{ uint64_t* `}
255 uint64_t
*self = (uint64_t
*)malloc
(sizeof
(uint64_t
));
259 fun set_si(val: Int) `{
260 *self = (uint64_t
)val
;