Skip to main content

leanfran_runtime/
int.rs

1//! `Int`: scalars in the range of `i32` on 64-bit machines and of 31-bit integers on 32-bit
2//! machines (as in `lean.h`), `num-bigint` values in mpz objects outside it.
3
4use crate::nat::{lean_alloc_mpz, lean_mpz_value, mk_nat, nat_value};
5use crate::object::*;
6use crate::panic::lean_internal_panic;
7use num_bigint::BigInt;
8use num_traits::{Signed, ToPrimitive, Zero};
9
10#[cfg(target_pointer_width = "64")]
11pub const LEAN_MAX_SMALL_INT: i64 = i32::MAX as i64;
12#[cfg(target_pointer_width = "64")]
13pub const LEAN_MIN_SMALL_INT: i64 = i32::MIN as i64;
14#[cfg(not(target_pointer_width = "64"))]
15pub const LEAN_MAX_SMALL_INT: i64 = (1 << 30) - 1;
16#[cfg(not(target_pointer_width = "64"))]
17pub const LEAN_MIN_SMALL_INT: i64 = -(1 << 30);
18
19/// The scalar `Int` of `n`, in the range of the small integers.
20#[inline]
21fn box_small_int(n: i32) -> Obj {
22    #[cfg(target_pointer_width = "64")]
23    {
24        lean_box(n as u32 as usize)
25    }
26    #[cfg(not(target_pointer_width = "64"))]
27    {
28        lean_box(n as usize)
29    }
30}
31
32/// The value of a scalar `Int`.
33#[inline]
34fn unbox_small_int(a: Obj) -> i32 {
35    #[cfg(target_pointer_width = "64")]
36    {
37        lean_unbox(a) as u32 as i32
38    }
39    #[cfg(not(target_pointer_width = "64"))]
40    {
41        (a.addr() as i32) >> 1
42    }
43}
44
45/// The value of an `Int`.
46#[inline]
47pub(crate) unsafe fn int_value(o: Obj) -> BigInt {
48    if lean_is_scalar(o) {
49        BigInt::from(lean_scalar_to_int64(o))
50    } else {
51        lean_mpz_value(o).clone()
52    }
53}
54
55/// An `Int` object for `v`.
56pub(crate) unsafe fn mk_int(v: BigInt) -> Obj {
57    match v.to_i64() {
58        Some(n) if (LEAN_MIN_SMALL_INT..=LEAN_MAX_SMALL_INT).contains(&n) => {
59            box_small_int(n as i32)
60        }
61        _ => lean_alloc_mpz(v),
62    }
63}
64
65#[inline]
66pub unsafe fn lean_scalar_to_int64(a: Obj) -> i64 {
67    unbox_small_int(a) as i64
68}
69
70#[inline]
71pub unsafe fn lean_scalar_to_int(a: Obj) -> i32 {
72    unbox_small_int(a)
73}
74
75#[inline]
76pub unsafe fn lean_int_to_int(n: i32) -> Obj {
77    lean_int64_to_int(n as i64)
78}
79
80#[inline]
81pub unsafe fn lean_int64_to_int(n: i64) -> Obj {
82    if (LEAN_MIN_SMALL_INT..=LEAN_MAX_SMALL_INT).contains(&n) {
83        box_small_int(n as i32)
84    } else {
85        lean_big_int64_to_int(n)
86    }
87}
88
89pub unsafe fn lean_big_int_to_int(n: i32) -> Obj {
90    lean_alloc_mpz(BigInt::from(n))
91}
92
93pub unsafe fn lean_big_size_t_to_int(n: usize) -> Obj {
94    lean_alloc_mpz(BigInt::from(n))
95}
96
97pub unsafe fn lean_big_int64_to_int(n: i64) -> Obj {
98    lean_alloc_mpz(BigInt::from(n))
99}
100
101/// An `Int` from its decimal digits (with an optional `-`).
102pub unsafe fn lean_cstr_to_int(s: &str) -> Obj {
103    match s.parse::<BigInt>() {
104        Ok(v) => mk_int(v),
105        Err(_) => lean_internal_panic("invalid integer literal"),
106    }
107}
108
109/// `Int.ofNat` (consumes `a`).
110#[inline]
111pub unsafe fn lean_nat_to_int(a: Obj) -> Obj {
112    if lean_is_scalar(a) {
113        let v = lean_unbox(a);
114        if v <= LEAN_MAX_SMALL_INT as usize {
115            a
116        } else {
117            lean_big_size_t_to_int(v)
118        }
119    } else {
120        a
121    }
122}
123
124/// `Int.negSucc` (consumes `a`).
125pub unsafe fn lean_int_neg_succ_of_nat(a: Obj) -> Obj {
126    let v = nat_value(a);
127    lean_dec(a);
128    let n: BigInt = v + 1u32;
129    mk_int(-n)
130}
131
132/// `Int.toNat` of a non-negative `Int` (consumes `a`).
133pub unsafe fn lean_int_to_nat(a: Obj) -> Obj {
134    if lean_is_scalar(a) {
135        a
136    } else {
137        lean_big_int_to_nat(a)
138    }
139}
140
141/// Consumes `a`.
142pub unsafe fn lean_big_int_to_nat(a: Obj) -> Obj {
143    let v = int_value(a);
144    lean_dec(a);
145    mk_nat(v)
146}
147
148#[inline]
149pub unsafe fn lean_nat_abs(i: Obj) -> Obj {
150    mk_nat(int_value(i).abs())
151}
152
153/* Arithmetic */
154
155macro_rules! int_binop {
156    ($name:ident, $big:ident, |$x:ident, $y:ident| $small:expr, |$bx:ident, $by:ident| $bigexpr:expr) => {
157        #[inline]
158        pub unsafe fn $name(a1: Obj, a2: Obj) -> Obj {
159            if lean_is_scalar(a1) && lean_is_scalar(a2) {
160                let $x = lean_scalar_to_int64(a1);
161                let $y = lean_scalar_to_int64(a2);
162                $small
163            } else {
164                $big(a1, a2)
165            }
166        }
167
168        pub unsafe fn $big(a1: Obj, a2: Obj) -> Obj {
169            let $bx = int_value(a1);
170            let $by = int_value(a2);
171            $bigexpr
172        }
173    };
174}
175
176int_binop!(
177    lean_int_add,
178    lean_int_big_add,
179    |x, y| lean_int64_to_int(x + y),
180    |x, y| mk_int(x + y)
181);
182int_binop!(
183    lean_int_sub,
184    lean_int_big_sub,
185    |x, y| lean_int64_to_int(x - y),
186    |x, y| mk_int(x - y)
187);
188int_binop!(
189    lean_int_mul,
190    lean_int_big_mul,
191    |x, y| lean_int64_to_int(x * y),
192    |x, y| mk_int(x * y)
193);
194int_binop!(
195    lean_int_div,
196    lean_int_big_div,
197    |x, y| if y == 0 {
198        lean_box(0)
199    } else {
200        lean_int64_to_int(x / y)
201    },
202    |x, y| if y.is_zero() {
203        lean_box(0)
204    } else {
205        mk_int(x / y)
206    }
207);
208int_binop!(
209    lean_int_mod,
210    lean_int_big_mod,
211    |x, y| if y == 0 {
212        lean_int64_to_int(x)
213    } else {
214        lean_int64_to_int(x % y)
215    },
216    |x, y| if y.is_zero() {
217        mk_int(x)
218    } else {
219        mk_int(x % y)
220    }
221);
222int_binop!(
223    lean_int_ediv,
224    lean_int_big_ediv,
225    |x, y| if y == 0 {
226        lean_box(0)
227    } else {
228        lean_int64_to_int(ediv(x, y))
229    },
230    |x, y| if y.is_zero() {
231        lean_box(0)
232    } else {
233        mk_int(big_ediv(&x, &y))
234    }
235);
236int_binop!(
237    lean_int_emod,
238    lean_int_big_emod,
239    |x, y| if y == 0 {
240        lean_int64_to_int(x)
241    } else {
242        lean_int64_to_int(emod(x, y))
243    },
244    |x, y| if y.is_zero() {
245        mk_int(x)
246    } else {
247        mk_int(big_emod(&x, &y))
248    }
249);
250
251#[inline]
252pub unsafe fn lean_int_div_exact(a1: Obj, a2: Obj) -> Obj {
253    lean_int_div(a1, a2)
254}
255
256pub unsafe fn lean_int_big_div_exact(a1: Obj, a2: Obj) -> Obj {
257    lean_int_big_div(a1, a2)
258}
259
260fn ediv(n: i64, d: i64) -> i64 {
261    let q = n / d;
262    if n % d < 0 {
263        if d > 0 {
264            q - 1
265        } else {
266            q + 1
267        }
268    } else {
269        q
270    }
271}
272
273fn emod(n: i64, d: i64) -> i64 {
274    let r = n % d;
275    if r < 0 {
276        if d > 0 {
277            r + d
278        } else {
279            r - d
280        }
281    } else {
282        r
283    }
284}
285
286fn big_ediv(n: &BigInt, d: &BigInt) -> BigInt {
287    let q = n / d;
288    if (n % d).is_negative() {
289        if d.is_positive() {
290            q - 1
291        } else {
292            q + 1
293        }
294    } else {
295        q
296    }
297}
298
299fn big_emod(n: &BigInt, d: &BigInt) -> BigInt {
300    let r = n % d;
301    if r.is_negative() {
302        if d.is_positive() {
303            r + d
304        } else {
305            r - d
306        }
307    } else {
308        r
309    }
310}
311
312#[inline]
313pub unsafe fn lean_int_neg(a: Obj) -> Obj {
314    if lean_is_scalar(a) {
315        lean_int64_to_int(-lean_scalar_to_int64(a))
316    } else {
317        lean_int_big_neg(a)
318    }
319}
320
321pub unsafe fn lean_int_big_neg(a: Obj) -> Obj {
322    mk_int(-int_value(a))
323}
324
325/* Comparisons */
326
327#[inline]
328pub unsafe fn lean_int_eq(a1: Obj, a2: Obj) -> bool {
329    if lean_is_scalar(a1) && lean_is_scalar(a2) {
330        a1 == a2
331    } else {
332        lean_int_big_eq(a1, a2)
333    }
334}
335
336pub unsafe fn lean_int_big_eq(a1: Obj, a2: Obj) -> bool {
337    int_value(a1) == int_value(a2)
338}
339
340#[inline]
341pub unsafe fn lean_int_ne(a1: Obj, a2: Obj) -> bool {
342    !lean_int_eq(a1, a2)
343}
344
345#[inline]
346pub unsafe fn lean_int_le(a1: Obj, a2: Obj) -> bool {
347    if lean_is_scalar(a1) && lean_is_scalar(a2) {
348        lean_scalar_to_int(a1) <= lean_scalar_to_int(a2)
349    } else {
350        lean_int_big_le(a1, a2)
351    }
352}
353
354pub unsafe fn lean_int_big_le(a1: Obj, a2: Obj) -> bool {
355    int_value(a1) <= int_value(a2)
356}
357
358#[inline]
359pub unsafe fn lean_int_lt(a1: Obj, a2: Obj) -> bool {
360    if lean_is_scalar(a1) && lean_is_scalar(a2) {
361        lean_scalar_to_int(a1) < lean_scalar_to_int(a2)
362    } else {
363        lean_int_big_lt(a1, a2)
364    }
365}
366
367pub unsafe fn lean_int_big_lt(a1: Obj, a2: Obj) -> bool {
368    int_value(a1) < int_value(a2)
369}
370
371pub unsafe fn lean_int_big_nonneg(a: Obj) -> bool {
372    !lean_mpz_value(a).is_negative()
373}
374
375#[inline]
376pub unsafe fn lean_int_dec_eq(a1: Obj, a2: Obj) -> u8 {
377    lean_int_eq(a1, a2) as u8
378}
379
380#[inline]
381pub unsafe fn lean_int_dec_le(a1: Obj, a2: Obj) -> u8 {
382    lean_int_le(a1, a2) as u8
383}
384
385#[inline]
386pub unsafe fn lean_int_dec_lt(a1: Obj, a2: Obj) -> u8 {
387    lean_int_lt(a1, a2) as u8
388}
389
390#[inline]
391pub unsafe fn lean_int_dec_nonneg(a: Obj) -> u8 {
392    if lean_is_scalar(a) {
393        (lean_scalar_to_int(a) >= 0) as u8
394    } else {
395        lean_int_big_nonneg(a) as u8
396    }
397}
398
399/* Bitwise operations on `Int` (two's complement semantics) */
400
401pub unsafe fn lean_int_land(a1: Obj, a2: Obj) -> Obj {
402    mk_int(int_value(a1) & int_value(a2))
403}
404
405pub unsafe fn lean_int_lor(a1: Obj, a2: Obj) -> Obj {
406    mk_int(int_value(a1) | int_value(a2))
407}
408
409pub unsafe fn lean_int_xor(a1: Obj, a2: Obj) -> Obj {
410    mk_int(int_value(a1) ^ int_value(a2))
411}
412
413pub unsafe fn lean_int_lnot(a: Obj) -> Obj {
414    mk_int(!int_value(a))
415}
416
417/// `Int.shiftRight` by a `Nat` (arithmetic shift, rounding towards negative infinity).
418pub unsafe fn lean_int_shift_right(a1: Obj, a2: Obj) -> Obj {
419    match crate::nat::nat_to_usize(a2) {
420        Some(s) => mk_int(int_value(a1) >> s),
421        None => mk_int(if int_value(a1).is_negative() {
422            BigInt::from(-1)
423        } else {
424            BigInt::zero()
425        }),
426    }
427}