1use crate::bignum;
8use crate::nat::{alloc_big, big_parts};
9use crate::object::{box_, dec, inc, is_scalar, unbox, Obj};
10use std::cmp::Ordering;
11
12struct Signed {
14 negative: bool,
15 magnitude: bignum::Limbs,
16}
17
18unsafe fn read(o: Obj) -> Signed {
19 if is_scalar(o) {
20 let value = unbox(o) as u32 as i32 as i64;
21 Signed { negative: value < 0, magnitude: magnitude_of(value.unsigned_abs() as u128) }
22 } else {
23 let (negative, limbs) = big_parts(o);
24 Signed { negative, magnitude: limbs.into() }
25 }
26}
27
28fn magnitude_of(value: u128) -> bignum::Limbs {
30 let limbs = [value as u64, (value >> 64) as u64];
31 limbs[..2 - (value.leading_zeros() / 64) as usize].into()
32}
33
34fn write(mut value: Signed) -> Obj {
35 while value.magnitude.last() == Some(&0) {
36 value.magnitude.pop();
37 }
38 let negative = value.negative && !value.magnitude.is_empty();
39 if let [] | [_] = &*value.magnitude {
40 let magnitude = value.magnitude.first().copied().unwrap_or(0) as i128;
41 let signed = if negative { -magnitude } else { magnitude };
42 if let Ok(small) = i32::try_from(signed) {
43 return box_(small as u32 as usize);
44 }
45 }
46 alloc_big(&value.magnitude, negative)
47}
48
49fn from_i64(value: i64) -> Obj {
50 write(Signed { negative: value < 0, magnitude: magnitude_of(value.unsigned_abs() as u128) })
51}
52
53unsafe fn small(o: Obj) -> Option<i64> {
55 is_scalar(o).then(|| unbox(o) as u32 as i32 as i64)
56}
57
58fn add_signed(a: Signed, b: Signed) -> Signed {
59 if a.negative == b.negative {
60 return Signed { negative: a.negative, magnitude: bignum::add(&a.magnitude, &b.magnitude) };
61 }
62 match bignum::compare(&a.magnitude, &b.magnitude) {
63 Ordering::Less => Signed { negative: b.negative, magnitude: bignum::sub(&b.magnitude, &a.magnitude) },
64 _ => Signed { negative: a.negative, magnitude: bignum::sub(&a.magnitude, &b.magnitude) },
65 }
66}
67
68fn negate(a: Signed) -> Signed {
69 Signed { negative: !a.negative, magnitude: a.magnitude }
70}
71
72pub unsafe fn nat_to_int(a: Obj) -> Obj {
74 if is_scalar(a) && unbox(a) > i32::MAX as usize {
75 return alloc_big(&[unbox(a) as u64], false);
76 }
77 a
78}
79
80pub unsafe fn int_to_nat(a: Obj) -> Obj {
82 if is_scalar(a) {
83 return a;
84 }
85 let result = crate::nat::of_limbs(big_parts(a).1);
86 dec(a);
87 result
88}
89
90pub unsafe fn nat_abs(a: Obj) -> Obj {
92 crate::nat::of_limbs(&read(a).magnitude)
93}
94
95pub unsafe fn int_neg(a: Obj) -> Obj {
96 write(negate(read(a)))
97}
98
99pub unsafe fn int_neg_succ_of_nat(n: Obj) -> Obj {
101 let successor = crate::nat::nat_succ(n);
102 dec(n);
103 let as_int = nat_to_int(successor);
104 let result = int_neg(as_int);
105 dec(as_int);
106 result
107}
108
109pub unsafe fn int_add(a: Obj, b: Obj) -> Obj {
110 match (small(a), small(b)) {
111 (Some(x), Some(y)) => from_i64(x + y),
112 _ => write(add_signed(read(a), read(b))),
113 }
114}
115
116pub unsafe fn int_sub(a: Obj, b: Obj) -> Obj {
117 match (small(a), small(b)) {
118 (Some(x), Some(y)) => from_i64(x - y),
119 _ => write(add_signed(read(a), negate(read(b)))),
120 }
121}
122
123pub unsafe fn int_mul(a: Obj, b: Obj) -> Obj {
124 match (small(a), small(b)) {
125 (Some(x), Some(y)) => from_i64(x * y),
126 _ => {
127 let (a, b) = (read(a), read(b));
128 write(Signed { negative: a.negative != b.negative, magnitude: bignum::mul(&a.magnitude, &b.magnitude) })
129 }
130 }
131}
132
133fn divrem(a: &Signed, b: &Signed) -> (Signed, Signed) {
135 let (quotient, remainder) = bignum::divrem(&a.magnitude, &b.magnitude);
136 (Signed { negative: a.negative != b.negative, magnitude: quotient }, Signed { negative: a.negative, magnitude: remainder })
137}
138
139fn one() -> Signed {
140 Signed { negative: false, magnitude: magnitude_of(1) }
141}
142
143pub unsafe fn int_div(a: Obj, b: Obj) -> Obj {
145 let (a, b) = (read(a), read(b));
146 if b.magnitude.is_empty() {
147 return box_(0);
148 }
149 write(divrem(&a, &b).0)
150}
151
152pub unsafe fn int_div_exact(a: Obj, b: Obj) -> Obj {
153 int_div(a, b)
154}
155
156pub unsafe fn int_mod(a: Obj, b: Obj) -> Obj {
158 let (x, y) = (read(a), read(b));
159 if y.magnitude.is_empty() {
160 inc(a);
161 return a;
162 }
163 write(divrem(&x, &y).1)
164}
165
166pub unsafe fn int_ediv(a: Obj, b: Obj) -> Obj {
168 let (x, y) = (read(a), read(b));
169 if y.magnitude.is_empty() {
170 return box_(0);
171 }
172 let (quotient, remainder) = divrem(&x, &y);
173 if remainder.negative && !remainder.magnitude.is_empty() {
174 let step = if y.negative { one() } else { negate(one()) };
175 return write(add_signed(quotient, step));
176 }
177 write(quotient)
178}
179
180pub unsafe fn int_emod(a: Obj, b: Obj) -> Obj {
182 let (x, y) = (read(a), read(b));
183 if y.magnitude.is_empty() {
184 inc(a);
185 return a;
186 }
187 let remainder = divrem(&x, &y).1;
188 if remainder.negative && !remainder.magnitude.is_empty() {
189 return write(add_signed(remainder, Signed { negative: false, magnitude: y.magnitude }));
190 }
191 write(remainder)
192}
193
194unsafe fn compare(a: Obj, b: Obj) -> Ordering {
195 if let (Some(x), Some(y)) = (small(a), small(b)) {
196 return x.cmp(&y);
197 }
198 let (a, b) = (read(a), read(b));
199 match (a.negative, b.negative) {
200 (false, true) => Ordering::Greater,
201 (true, false) => Ordering::Less,
202 (false, false) => bignum::compare(&a.magnitude, &b.magnitude),
203 (true, true) => bignum::compare(&b.magnitude, &a.magnitude),
204 }
205}
206
207pub unsafe fn int_dec_eq(a: Obj, b: Obj) -> u8 {
208 (compare(a, b) == Ordering::Equal) as u8
209}
210
211pub unsafe fn int_dec_lt(a: Obj, b: Obj) -> u8 {
212 (compare(a, b) == Ordering::Less) as u8
213}
214
215pub unsafe fn int_dec_le(a: Obj, b: Obj) -> u8 {
216 (compare(a, b) != Ordering::Greater) as u8
217}
218
219pub unsafe fn int_dec_nonneg(a: Obj) -> u8 {
220 !read(a).negative as u8
221}
222
223pub unsafe fn float_scaleb(a: f64, b: Obj) -> f64 {
225 let exponent = match small(b) {
226 Some(exponent) => exponent as i32,
227 None if read(b).negative => i32::MIN,
228 None => i32::MAX,
229 };
230 let mut value = a;
233 let mut left = exponent.clamp(-2200, 2200);
234 while left != 0 {
235 let step = left.clamp(-1000, 1000);
236 value *= 2f64.powi(step);
237 left -= step;
238 }
239 value
240}
241
242
243
244fn of_i64(value: i64) -> Obj {
245 from_i64(value)
246}
247
248pub fn int8_to_int(a: u8) -> Obj {
250 of_i64(a as i8 as i64)
251}
252
253pub fn int16_to_int(a: u16) -> Obj {
254 of_i64(a as i16 as i64)
255}
256
257pub fn int32_to_int(a: u32) -> Obj {
258 of_i64(a as i32 as i64)
259}
260
261pub fn int64_to_int_sint(a: u64) -> Obj {
262 of_i64(a as i64)
263}
264
265pub fn isize_to_int(a: usize) -> Obj {
266 of_i64(a as isize as i64)
267}
268
269unsafe fn low_bits(a: Obj) -> u64 {
271 let value = read(a);
272 let low = value.magnitude.first().copied().unwrap_or(0);
273 if value.negative {
274 low.wrapping_neg()
275 } else {
276 low
277 }
278}
279
280pub unsafe fn int8_of_int(a: Obj) -> u8 {
282 low_bits(a) as u8
283}
284
285pub unsafe fn int16_of_int(a: Obj) -> u16 {
286 low_bits(a) as u16
287}
288
289pub unsafe fn int32_of_int(a: Obj) -> u32 {
290 low_bits(a) as u32
291}
292
293pub unsafe fn int64_of_int(a: Obj) -> u64 {
294 low_bits(a)
295}
296
297pub unsafe fn isize_of_int(a: Obj) -> usize {
298 low_bits(a) as usize
299}
300
301pub(crate) const OPS: &[&str] = &[
304 "nat_to_int", "int_to_nat", "nat_abs", "int_neg", "int_neg_succ_of_nat", "int_add", "int_sub", "int_mul",
305 "int_div", "int_div_exact", "int_mod", "int_ediv", "int_emod", "int_dec_eq", "int_dec_lt", "int_dec_le",
306 "int_dec_nonneg", "float_scaleb", "int8_to_int", "int16_to_int", "int32_to_int", "int64_to_int_sint",
307 "isize_to_int", "int8_of_int", "int16_of_int", "int32_of_int", "int64_of_int", "isize_of_int",
308];
309
310pub(crate) fn from_decimal(negative: bool, digits: &str) -> Option<Obj> {
312 Some(write(Signed { negative, magnitude: bignum::from_decimal(digits)? }))
313}
314
315pub(crate) unsafe fn to_decimal(a: Obj) -> String {
316 let value = read(a);
317 let digits = bignum::to_decimal(&value.magnitude);
318 if value.negative {
319 format!("-{digits}")
320 } else {
321 digits
322 }
323}
324
325pub(crate) fn of_i128(value: i128) -> Obj {
326 let magnitude = value.unsigned_abs();
327 write(Signed { negative: value < 0, magnitude: magnitude_of(magnitude) })
328}
329
330pub(crate) unsafe fn to_i128(a: Obj) -> Option<i128> {
331 let value = read(a);
332 let magnitude = match &*value.magnitude {
333 [] => 0u128,
334 [low] => *low as u128,
335 [low, high] => (*high as u128) << 64 | *low as u128,
336 _ => return None,
337 };
338 if value.negative {
339 0i128.checked_sub_unsigned(magnitude)
340 } else {
341 i128::try_from(magnitude).ok()
342 }
343}
344
345pub(crate) unsafe fn ordering(a: Obj, b: Obj) -> Ordering {
346 compare(a, b)
347}
348
349pub(crate) unsafe fn hash<H: std::hash::Hasher>(a: Obj, state: &mut H) {
351 use std::hash::Hash;
352 let value = read(a);
353 value.negative.hash(state);
354 (*value.magnitude).hash(state);
355}