tvm_ffi/big_int/
mod.rs

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
/*
 * Licensed to the Apache Software Foundation (ASF) under one
 * or more contributor license agreements.  See the NOTICE file
 * distributed with this work for additional information
 * regarding copyright ownership.  The ASF licenses this file
 * to you under the Apache License, Version 2.0 (the
 * "License"); you may not use this file except in compliance
 * with the License.  You may obtain a copy of the License at
 *
 *   http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing,
 * software distributed under the License is distributed on an
 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
 * KIND, either express or implied.  See the License for the
 * specific language governing permissions and limitations
 * under the License.
 */
//! Integers with inline `i64` storage and arbitrary-precision arithmetic.
mod int_ops;

use crate::derive::Object;
use crate::error::{Error, Result, OVERFLOW_ERROR, VALUE_ERROR};
use crate::object::{unsafe_, Object, ObjectArc, ObjectCoreWithExtraItems};
use crate::type_traits::AnyCompatible;
use std::alloc::Layout;
use std::cmp::Ordering;
use std::ffi::c_void;
use std::fmt::{Debug, Display, Write as _};
use std::hash::{Hash, Hasher};
use std::ops::{
    Add, AddAssign, BitAnd, BitAndAssign, BitOr, BitOrAssign, BitXor, BitXorAssign, Div, DivAssign,
    Mul, MulAssign, Neg, Not, Rem, RemAssign, Shl, ShlAssign, Shr, ShrAssign, Sub, SubAssign,
};
use std::str::FromStr;
use std::sync::atomic::AtomicU64;
use tvm_ffi_sys::TVMFFIObjectDeleterFlagBitMask::kTVMFFIObjectDeleterFlagBitMaskWeak;
use tvm_ffi_sys::TVMFFITypeIndex as TypeIndex;
use tvm_ffi_sys::{TVMFFIAny, TVMFFIAnyDataUnion, TVMFFIObject, COMBINED_REF_COUNT_BOTH_ONE};

/// ABI stable arbitrary-precision signed integer for ffi.
///
/// Mirrors C++ `tvm::ffi::BigInt`: a value that fits `i64` is stored inline as
/// `kTVMFFIInt`, and any wider value owns a `kTVMFFIBigInt` object holding its
/// minimal two's-complement `i64` words, least-significant word first. Every
/// operation returns that canonical form, so word-wise comparison decides equality.
///
/// Arithmetic is unbounded and signed. `/` and `%` truncate toward zero;
/// [`floor_div`](Self::floor_div) and [`floor_mod`](Self::floor_mod) round down.
/// Bitwise operations sign-extend infinitely, and negative shift counts are errors.
/// Like the primitive operators, `/`, `%`, `<<` and `>>` panic on those errors;
/// the `try_*` methods return them instead, as ffi callbacks need since a panic
/// cannot unwind across them.
#[repr(C)]
pub struct BigInt {
    data: TVMFFIAny,
}

// BigIntObj for heap-allocated integers: the word count, then the canonical
// two's-complement words as trailing items. Mirrors C++ `detail::BigIntObj`.
#[repr(C)]
#[derive(Object)]
#[type_key = "ffi.BigInt"]
#[type_index(TypeIndex::kTVMFFIBigInt)]
#[type_final]
struct BigIntObj {
    object: Object,
    size: usize,
}

unsafe impl ObjectCoreWithExtraItems for BigIntObj {
    type ExtraItem = i64;
    #[inline]
    /// Get the count of extra items (the logical word count)
    fn extra_items_count(this: &Self) -> usize {
        this.size
    }
}

// `BigInt::try_build` allocates from the C allocator like C++
// `make_inplace_array_object`, so it can prune the logical size in place (C++
// `ShrinkSize`): `free` needs no layout, unlike Rust's global allocator.
extern "C" {
    fn malloc(size: usize) -> *mut c_void;
    fn free(ptr: *mut c_void);
}

/// Release an object from `BigInt::try_build` like C++ `ArrayHandler::Deleter_`;
/// the words need no destructor.
unsafe extern "C" fn delete_obj(ptr: *mut c_void, flags: i32) {
    if flags & kTVMFFIObjectDeleterFlagBitMaskWeak as i32 != 0 {
        free(ptr);
    }
}

/// Layout of an object with `len` words; `None` beyond `isize::MAX` bytes.
fn obj_layout(len: usize) -> Option<Layout> {
    let words = Layout::array::<i64>(len).ok()?;
    Some(Layout::new::<BigIntObj>().extend(words).ok()?.0)
}

impl BigInt {
    /// Create the integer zero.
    pub fn new() -> Self {
        Self::from_i64(0)
    }

    #[inline]
    fn from_i64(value: i64) -> Self {
        Self {
            data: TVMFFIAny {
                type_index: TypeIndex::kTVMFFIInt as i32,
                small_str_len: 0,
                data_union: TVMFFIAnyDataUnion { v_int64: value },
            },
        }
    }

    /// Take ownership of a canonical multiword object without an extra reference.
    #[inline]
    fn from_obj(obj: *mut BigIntObj) -> Self {
        Self {
            data: TVMFFIAny {
                type_index: TypeIndex::kTVMFFIBigInt as i32,
                small_str_len: 0,
                data_union: TVMFFIAnyDataUnion {
                    v_obj: obj.cast::<TVMFFIObject>(),
                },
            },
        }
    }

    /// Create from two's-complement `i64` words, least-significant word first.
    ///
    /// Redundant sign-extension words are pruned and a value that fits `i64`
    /// is stored inline; an empty slice is zero.
    #[inline]
    pub fn from_words(words: &[i64]) -> Self {
        let size = int_ops::normalized_len(words);
        if size <= 1 {
            return Self::from_i64(words.first().copied().unwrap_or(0));
        }
        // Allocated at its final length, so the crate's generic extra-items deleter fits.
        unsafe {
            let mut obj = ObjectArc::new_with_extra_items(BigIntObj {
                object: Object::new(),
                size,
            });
            BigIntObj::extra_items_mut(&mut *ObjectArc::as_raw_mut(&mut obj))
                .copy_from_slice(&words[..size]);
            Self::from_obj(ObjectArc::into_raw(obj) as *mut BigIntObj)
        }
    }

    /// Build from `len` words, word `i` being `word(i)` called in increasing order,
    /// written straight into a fresh object that is then pruned in place like C++
    /// `Normalize`; `None` when the object cannot be allocated.
    #[inline]
    fn try_build(len: usize, mut word: impl FnMut(usize) -> i64) -> Option<Self> {
        let layout = obj_layout(len)?;
        unsafe {
            let obj = malloc(layout.size()).cast::<BigIntObj>();
            if obj.is_null() {
                return None;
            }
            let data = obj.add(1).cast::<i64>();
            for i in 0..len {
                data.add(i).write(word(i));
            }
            let size = int_ops::normalized_len(std::slice::from_raw_parts(data, len));
            if size <= 1 {
                let value = if size == 1 { *data } else { 0 };
                free(obj.cast());
                return Some(Self::from_i64(value));
            }
            obj.cast::<TVMFFIObject>().write(TVMFFIObject {
                combined_ref_count: AtomicU64::new(COMBINED_REF_COUNT_BOTH_ONE),
                type_index: TypeIndex::kTVMFFIBigInt as i32,
                __padding: 0,
                deleter: Some(delete_obj),
            });
            std::ptr::addr_of_mut!((*obj).size).write(size);
            Some(Self::from_obj(obj))
        }
    }

    /// Like `try_build`, but like `Vec`, exhaustion aborts.
    #[inline]
    fn build(len: usize, word: impl FnMut(usize) -> i64) -> Self {
        Self::try_build(len, word).unwrap_or_else(|| {
            std::alloc::handle_alloc_error(obj_layout(len).unwrap_or(Layout::new::<BigIntObj>()))
        })
    }

    /// Borrow the canonical two's-complement words, least-significant word first.
    ///
    /// The slice is never empty and its last word carries the sign.
    pub fn words(&self) -> &[i64] {
        unsafe {
            if self.data.type_index == TypeIndex::kTVMFFIInt as i32 {
                std::slice::from_ref(&self.data.data_union.v_int64)
            } else {
                let obj: &BigIntObj = &*(self.data.data_union.v_obj as *const BigIntObj);
                BigIntObj::extra_items(obj)
            }
        }
    }

    /// Whether the value is negative.
    pub fn is_negative(&self) -> bool {
        int_ops::is_negative(self.words())
    }

    /// Whether the value is zero.
    #[inline]
    pub fn is_zero(&self) -> bool {
        self.to_i64() == Some(0)
    }

    /// Return the value when it fits `i64`.
    #[inline]
    pub fn to_i64(&self) -> Option<i64> {
        if self.data.type_index == TypeIndex::kTVMFFIInt as i32 {
            Some(unsafe { self.data.data_union.v_int64 })
        } else {
            None
        }
    }

    /// Return the value when it fits `u64`.
    pub fn to_u64(&self) -> Option<u64> {
        match *self.words() {
            [w0] => u64::try_from(w0).ok(),
            [w0, 0] => Some(w0 as u64),
            _ => None,
        }
    }

    /// Return the value when it fits `i128`.
    pub fn to_i128(&self) -> Option<i128> {
        match *self.words() {
            [w0] => Some(i128::from(w0)),
            [w0, w1] => Some((i128::from(w1) << 64) | i128::from(w0 as u64)),
            _ => None,
        }
    }

    /// Return the value when it fits `u128`.
    pub fn to_u128(&self) -> Option<u128> {
        let combine = |w0: i64, w1: i64| (u128::from(w1 as u64) << 64) | u128::from(w0 as u64);
        match *self.words() {
            [w0] => u128::try_from(w0).ok(),
            [w0, w1] if w1 >= 0 => Some(combine(w0, w1)),
            [w0, w1, 0] => Some(combine(w0, w1)),
            _ => None,
        }
    }

    /// Convert to the nearest finite double, rounding ties to even.
    ///
    /// Raises `OverflowError` when the value rounds beyond the finite range.
    pub fn to_f64(&self) -> Result<f64> {
        match self.to_i64() {
            Some(value) => Ok(value as f64),
            None => int_ops::to_f64(self.words()),
        }
    }

    /// Quotient rounded toward zero; raises `ZeroDivisionError`.
    pub fn try_div(&self, rhs: &BigInt) -> Result<BigInt> {
        div_impl(self, rhs)
    }

    /// Remainder of truncating division, zero or with the dividend's sign.
    pub fn try_rem(&self, rhs: &BigInt) -> Result<BigInt> {
        rem_impl(self, rhs)
    }

    /// Truncating quotient and remainder in one pass; raises `ZeroDivisionError`.
    pub fn div_rem(&self, rhs: &BigInt) -> Result<(BigInt, BigInt)> {
        if let (Some(x), Some(y)) = (self.to_i64(), rhs.to_i64()) {
            if let Some(q) = int_ops::trunc_div_i64(x, y)? {
                return Ok((Self::from_i64(q), Self::from_i64(x.wrapping_rem(y))));
            }
        }
        int_ops::div_rem(self.words(), rhs.words())
    }

    /// Quotient rounded toward negative infinity; raises `ZeroDivisionError`.
    pub fn floor_div(&self, rhs: &BigInt) -> Result<BigInt> {
        floor_div_impl(self, rhs)
    }

    /// Remainder of floor division, zero or with the divisor's sign.
    pub fn floor_mod(&self, rhs: &BigInt) -> Result<BigInt> {
        floor_mod_impl(self, rhs)
    }

    /// Left shift; negative counts raise `ValueError`, excessive growth `OverflowError`.
    pub fn try_shl<C: Into<BigInt>>(&self, count: C) -> Result<BigInt> {
        shl_impl(self, &count.into())
    }

    /// Sign-extending right shift; negative counts raise `ValueError`.
    pub fn try_shr<C: Into<BigInt>>(&self, count: C) -> Result<BigInt> {
        shr_impl(self, &count.into())
    }
}

impl Default for BigInt {
    #[inline]
    fn default() -> Self {
        Self::new()
    }
}

/// Macro to implement From for integer types that always fit i64
macro_rules! impl_big_int_from_int {
    ($($int_type:ty),* $(,)?) => {
        $(
            impl From<$int_type> for BigInt {
                #[inline]
                fn from(value: $int_type) -> Self {
                    Self::from_i64(value as i64)
                }
            }
        )*
    };
}

impl_big_int_from_int!(i8, i16, i32, i64, isize, u8, u16, u32);

impl From<bool> for BigInt {
    #[inline]
    fn from(value: bool) -> Self {
        Self::from_i64(i64::from(value))
    }
}

impl From<u64> for BigInt {
    fn from(value: u64) -> Self {
        match i64::try_from(value) {
            Ok(value) => Self::from_i64(value),
            Err(_) => Self::from_words(&[value as i64, 0]),
        }
    }
}

impl From<usize> for BigInt {
    #[inline]
    fn from(value: usize) -> Self {
        Self::from(value as u64)
    }
}

impl From<i128> for BigInt {
    fn from(value: i128) -> Self {
        match i64::try_from(value) {
            Ok(value) => Self::from_i64(value),
            // The arithmetic shift keeps the sign in the high word.
            Err(_) => Self::from_words(&[value as i64, (value >> 64) as i64]),
        }
    }
}

impl From<u128> for BigInt {
    fn from(value: u128) -> Self {
        match i64::try_from(value) {
            Ok(value) => Self::from_i64(value),
            // The zero guard keeps the value positive; normalization prunes it when redundant.
            Err(_) => Self::from_words(&[value as i64, (value >> 64) as i64, 0]),
        }
    }
}

impl TryFrom<f64> for BigInt {
    type Error = Error;

    /// Convert a finite double by truncating toward zero.
    ///
    /// NaN raises `ValueError` and infinity raises `OverflowError`.
    fn try_from(value: f64) -> Result<Self> {
        // The upper bound is exclusive: i64::MAX rounds up to 2^63 as a double.
        const LOWER: f64 = i64::MIN as f64;
        if (LOWER..-LOWER).contains(&value) {
            return Ok(Self::from_i64(value as i64));
        }
        if value.is_nan() {
            return Err(Error::new(VALUE_ERROR, "Cannot convert NaN to BigInt", ""));
        }
        if value.is_infinite() {
            return Err(Error::new(
                OVERFLOW_ERROR,
                "Cannot convert infinity to BigInt",
                "",
            ));
        }
        // |value| >= 2^63 is an integer: shift the 53-bit significand to its binary exponent.
        let bits = value.to_bits();
        let exponent = ((bits >> 52) & 0x7ff) as i64 - 1075;
        let significand = ((bits & ((1u64 << 52) - 1)) | (1u64 << 52)) as i64;
        let magnitude = Self::from_i64(significand) << exponent;
        Ok(if value < 0.0 { -magnitude } else { magnitude })
    }
}

impl FromStr for BigInt {
    type Err = Error;

    /// Parse an optionally signed decimal integer; anything else raises `ValueError`.
    fn from_str(text: &str) -> Result<Self> {
        if let Ok(value) = text.parse::<i64>() {
            return Ok(Self::from_i64(value));
        }
        let (negative, digits) = match text.as_bytes().first() {
            Some(b'-') => (true, &text[1..]),
            Some(b'+') => (false, &text[1..]),
            _ => (false, text),
        };
        if digits.is_empty() || !digits.bytes().all(|byte| byte.is_ascii_digit()) {
            return Err(Error::new(
                VALUE_ERROR,
                "Invalid decimal string for BigInt",
                "",
            ));
        }
        // Accumulate 19-digit chunks: magnitude = magnitude * 10^len + chunk.
        let mut magnitude = Vec::new();
        for chunk in digits.as_bytes().chunks(19) {
            let value = chunk
                .iter()
                .fold(0u64, |acc, &digit| acc * 10 + u64::from(digit - b'0'));
            int_ops::mul_add_small(&mut magnitude, 10u64.pow(chunk.len() as u32), value);
        }
        let mut words: Vec<i64> = magnitude.into_iter().map(|word| word as i64).collect();
        words.push(0);
        if negative {
            int_ops::negate_in_place(&mut words);
        }
        Ok(Self::from_words(&words))
    }
}

impl Clone for BigInt {
    #[inline]
    fn clone(&self) -> Self {
        if self.data.type_index == TypeIndex::kTVMFFIBigInt as i32 {
            unsafe { unsafe_::inc_ref(self.data.data_union.v_obj) }
        }
        Self { data: self.data }
    }
}

impl Drop for BigInt {
    #[inline]
    fn drop(&mut self) {
        if self.data.type_index == TypeIndex::kTVMFFIBigInt as i32 {
            unsafe { unsafe_::dec_ref(self.data.data_union.v_obj) }
        }
    }
}

impl PartialEq for BigInt {
    /// Canonical words make equal values word-for-word identical.
    #[inline]
    fn eq(&self, other: &Self) -> bool {
        self.words() == other.words()
    }
}

impl Eq for BigInt {}

impl PartialOrd for BigInt {
    #[inline]
    fn partial_cmp(&self, other: &Self) -> Option<Ordering> {
        Some(self.cmp(other))
    }
}

impl Ord for BigInt {
    fn cmp(&self, other: &Self) -> Ordering {
        let (a, b) = (self.words(), other.words());
        let (negative_a, negative_b) = (int_ops::is_negative(a), int_ops::is_negative(b));
        if negative_a != negative_b {
            return if negative_a {
                Ordering::Less
            } else {
                Ordering::Greater
            };
        }
        // With equal signs, sign-extended words compare as unsigned from the top.
        let extension = int_ops::extension(a);
        (0..a.len().max(b.len()))
            .rev()
            .map(|i| int_ops::word_at(a, i, extension).cmp(&int_ops::word_at(b, i, extension)))
            .find(|ordering| ordering.is_ne())
            .unwrap_or(Ordering::Equal)
    }
}

impl Hash for BigInt {
    #[inline]
    fn hash<H: Hasher>(&self, state: &mut H) {
        self.words().hash(state);
    }
}

impl Display for BigInt {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        if let Some(value) = self.to_i64() {
            return Display::fmt(&value, f);
        }
        let words = self.words();
        let negative = words[words.len() - 1] < 0;
        let mut magnitude = words.to_vec();
        if negative {
            int_ops::negate_in_place(&mut magnitude);
        }
        // Peel base-10^19 chunks, least significant first, by long division.
        const CHUNK: u128 = 10_000_000_000_000_000_000;
        let mut chunks = Vec::new();
        while let Some(top) = magnitude.iter().rposition(|&word| word != 0) {
            magnitude.truncate(top + 1);
            let mut remainder = 0u128;
            for word in magnitude.iter_mut().rev() {
                let current = (remainder << 64) | u128::from(*word as u64);
                *word = (current / CHUNK) as i64;
                remainder = current % CHUNK;
            }
            chunks.push(remainder as u64);
        }
        let mut digits = String::new();
        let mut chunks = chunks.iter().rev();
        // A heap value is never zero, so the leading chunk always exists.
        write!(digits, "{}", chunks.next().unwrap_or(&0))?;
        for chunk in chunks {
            write!(digits, "{chunk:019}")?;
        }
        f.pad_integral(!negative, "", &digits)
    }
}

impl Debug for BigInt {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        f.debug_struct("ffi.BigInt")
            .field("data", &format_args!("{self}"))
            .finish()
    }
}

// ---- Arithmetic: an inline fast path, then the word fallbacks.
#[inline]
fn add_impl(a: &BigInt, b: &BigInt) -> BigInt {
    if let (Some(x), Some(y)) = (a.to_i64(), b.to_i64()) {
        if let Some(result) = x.checked_add(y) {
            return BigInt::from_i64(result);
        }
    }
    int_ops::add(a.words(), b.words())
}

#[inline]
fn sub_impl(a: &BigInt, b: &BigInt) -> BigInt {
    if let (Some(x), Some(y)) = (a.to_i64(), b.to_i64()) {
        if let Some(result) = x.checked_sub(y) {
            return BigInt::from_i64(result);
        }
    }
    int_ops::sub(a.words(), b.words())
}

#[inline]
fn mul_impl(a: &BigInt, b: &BigInt) -> BigInt {
    if let (Some(x), Some(y)) = (a.to_i64(), b.to_i64()) {
        if let Some(result) = x.checked_mul(y) {
            return BigInt::from_i64(result);
        }
    }
    int_ops::mul(a.words(), b.words())
}

#[inline]
fn div_impl(a: &BigInt, b: &BigInt) -> Result<BigInt> {
    if let (Some(x), Some(y)) = (a.to_i64(), b.to_i64()) {
        if let Some(result) = int_ops::trunc_div_i64(x, y)? {
            return Ok(BigInt::from_i64(result));
        }
    }
    int_ops::trunc_div(a.words(), b.words())
}

#[inline]
fn rem_impl(a: &BigInt, b: &BigInt) -> Result<BigInt> {
    if let (Some(x), Some(y)) = (a.to_i64(), b.to_i64()) {
        if let Some(result) = int_ops::trunc_mod_i64(x, y)? {
            return Ok(BigInt::from_i64(result));
        }
    }
    int_ops::trunc_mod(a.words(), b.words())
}

#[inline]
fn floor_div_impl(a: &BigInt, b: &BigInt) -> Result<BigInt> {
    if let (Some(x), Some(y)) = (a.to_i64(), b.to_i64()) {
        if let Some(result) = int_ops::floor_div_i64(x, y)? {
            return Ok(BigInt::from_i64(result));
        }
    }
    int_ops::floor_div(a.words(), b.words())
}

#[inline]
fn floor_mod_impl(a: &BigInt, b: &BigInt) -> Result<BigInt> {
    if let (Some(x), Some(y)) = (a.to_i64(), b.to_i64()) {
        if let Some(result) = int_ops::floor_mod_i64(x, y)? {
            return Ok(BigInt::from_i64(result));
        }
    }
    int_ops::floor_mod(a.words(), b.words())
}

#[inline]
fn shl_impl(a: &BigInt, b: &BigInt) -> Result<BigInt> {
    if let (Some(x), Some(y)) = (a.to_i64(), b.to_i64()) {
        if let Some(result) = int_ops::shl_i64(x, y)? {
            return Ok(BigInt::from_i64(result));
        }
    }
    int_ops::shl(a.words(), b.words())
}

#[inline]
fn shr_impl(a: &BigInt, b: &BigInt) -> Result<BigInt> {
    if let (Some(x), Some(y)) = (a.to_i64(), b.to_i64()) {
        return Ok(BigInt::from_i64(int_ops::shr_i64(x, y)?));
    }
    int_ops::shr(a.words(), b.words())
}

#[inline]
fn and_impl(a: &BigInt, b: &BigInt) -> BigInt {
    if let (Some(x), Some(y)) = (a.to_i64(), b.to_i64()) {
        return BigInt::from_i64(x & y);
    }
    int_ops::and(a.words(), b.words())
}

#[inline]
fn or_impl(a: &BigInt, b: &BigInt) -> BigInt {
    if let (Some(x), Some(y)) = (a.to_i64(), b.to_i64()) {
        return BigInt::from_i64(x | y);
    }
    int_ops::or(a.words(), b.words())
}

#[inline]
fn xor_impl(a: &BigInt, b: &BigInt) -> BigInt {
    if let (Some(x), Some(y)) = (a.to_i64(), b.to_i64()) {
        return BigInt::from_i64(x ^ y);
    }
    int_ops::xor(a.words(), b.words())
}

#[inline]
fn neg_impl(a: &BigInt) -> BigInt {
    if let Some(negated) = a.to_i64().and_then(i64::checked_neg) {
        return BigInt::from_i64(negated);
    }
    int_ops::negate(a.words())
}

#[inline]
fn not_impl(a: &BigInt) -> BigInt {
    if let Some(x) = a.to_i64() {
        return BigInt::from_i64(!x);
    }
    int_ops::not(a.words())
}

/// The operator form of a fallible operation panics like the primitive one would.
macro_rules! define_op_or_panic {
    ($($name:ident => $imp:ident),* $(,)?) => {
        $(
            #[inline]
            fn $name(a: &BigInt, b: &BigInt) -> BigInt {
                $imp(a, b).unwrap_or_else(|error| panic!("{}", error.message()))
            }
        )*
    };
}

define_op_or_panic!(
    div_or_panic => div_impl,
    rem_or_panic => rem_impl,
    shl_or_panic => shl_impl,
    shr_or_panic => shr_impl,
);

/// Forward an operator to its implementation for owned and borrowed operands,
/// with `i64` accepted on either side as C++ does.
macro_rules! forward_binop {
    ($Trait:ident::$method:ident, $Assign:ident::$assign:ident, $imp:ident) => {
        impl $Trait<&BigInt> for &BigInt {
            type Output = BigInt;
            #[inline]
            fn $method(self, rhs: &BigInt) -> BigInt {
                $imp(self, rhs)
            }
        }
        impl $Trait<BigInt> for &BigInt {
            type Output = BigInt;
            #[inline]
            fn $method(self, rhs: BigInt) -> BigInt {
                $imp(self, &rhs)
            }
        }
        impl $Trait<&BigInt> for BigInt {
            type Output = BigInt;
            #[inline]
            fn $method(self, rhs: &BigInt) -> BigInt {
                $imp(&self, rhs)
            }
        }
        impl $Trait<BigInt> for BigInt {
            type Output = BigInt;
            #[inline]
            fn $method(self, rhs: BigInt) -> BigInt {
                $imp(&self, &rhs)
            }
        }
        impl $Trait<i64> for &BigInt {
            type Output = BigInt;
            #[inline]
            fn $method(self, rhs: i64) -> BigInt {
                $imp(self, &BigInt::from_i64(rhs))
            }
        }
        impl $Trait<i64> for BigInt {
            type Output = BigInt;
            #[inline]
            fn $method(self, rhs: i64) -> BigInt {
                $imp(&self, &BigInt::from_i64(rhs))
            }
        }
        impl $Trait<&BigInt> for i64 {
            type Output = BigInt;
            #[inline]
            fn $method(self, rhs: &BigInt) -> BigInt {
                $imp(&BigInt::from_i64(self), rhs)
            }
        }
        impl $Trait<BigInt> for i64 {
            type Output = BigInt;
            #[inline]
            fn $method(self, rhs: BigInt) -> BigInt {
                $imp(&BigInt::from_i64(self), &rhs)
            }
        }
        impl $Assign<&BigInt> for BigInt {
            #[inline]
            fn $assign(&mut self, rhs: &BigInt) {
                *self = $imp(self, rhs);
            }
        }
        impl $Assign<BigInt> for BigInt {
            #[inline]
            fn $assign(&mut self, rhs: BigInt) {
                *self = $imp(self, &rhs);
            }
        }
        impl $Assign<i64> for BigInt {
            #[inline]
            fn $assign(&mut self, rhs: i64) {
                *self = $imp(self, &BigInt::from_i64(rhs));
            }
        }
    };
}

forward_binop!(Add::add, AddAssign::add_assign, add_impl);
forward_binop!(Sub::sub, SubAssign::sub_assign, sub_impl);
forward_binop!(Mul::mul, MulAssign::mul_assign, mul_impl);
forward_binop!(Div::div, DivAssign::div_assign, div_or_panic);
forward_binop!(Rem::rem, RemAssign::rem_assign, rem_or_panic);
forward_binop!(BitAnd::bitand, BitAndAssign::bitand_assign, and_impl);
forward_binop!(BitOr::bitor, BitOrAssign::bitor_assign, or_impl);
forward_binop!(BitXor::bitxor, BitXorAssign::bitxor_assign, xor_impl);
forward_binop!(Shl::shl, ShlAssign::shl_assign, shl_or_panic);
forward_binop!(Shr::shr, ShrAssign::shr_assign, shr_or_panic);

impl Neg for &BigInt {
    type Output = BigInt;
    #[inline]
    fn neg(self) -> BigInt {
        neg_impl(self)
    }
}

impl Neg for BigInt {
    type Output = BigInt;
    #[inline]
    fn neg(self) -> BigInt {
        neg_impl(&self)
    }
}

impl Not for &BigInt {
    type Output = BigInt;
    #[inline]
    fn not(self) -> BigInt {
        not_impl(self)
    }
}

impl Not for BigInt {
    type Output = BigInt;
    #[inline]
    fn not(self) -> BigInt {
        not_impl(&self)
    }
}

// ---- AnyCompatible.
unsafe impl AnyCompatible for BigInt {
    fn type_str() -> std::string::String {
        "BigInt".to_string()
    }

    // A big integer is an inline integer or a big integer object, never None.
    #[inline]
    unsafe fn is_undefined(field: *const Self) -> bool {
        (*std::ptr::addr_of!((*field).data.type_index)) == TypeIndex::kTVMFFINone as i32
    }

    fn type_schema() -> std::string::String {
        crate::type_traits::type_schema(<BigIntObj as crate::ObjectCore>::TYPE_KEY, &[])
    }

    unsafe fn copy_to_any_view(this: &Self, data: &mut TVMFFIAny) {
        *data = this.data;
    }

    unsafe fn move_to_any(src: Self, data: &mut TVMFFIAny) {
        *data = src.data;
        std::mem::forget(src);
    }

    unsafe fn check_any_strict(data: &TVMFFIAny) -> bool {
        data.type_index == TypeIndex::kTVMFFIInt as i32
            || data.type_index == TypeIndex::kTVMFFIBigInt as i32
    }

    unsafe fn copy_from_any_view_after_check(data: &TVMFFIAny) -> Self {
        if data.type_index == TypeIndex::kTVMFFIBigInt as i32 {
            unsafe { unsafe_::inc_ref(data.data_union.v_obj) }
        }
        Self { data: *data }
    }

    unsafe fn move_from_any_after_check(data: &mut TVMFFIAny) -> Self {
        Self { data: *data }
    }

    unsafe fn try_cast_from_any_view(data: &TVMFFIAny) -> std::result::Result<Self, ()> {
        if Self::check_any_strict(data) {
            Ok(Self::copy_from_any_view_after_check(data))
        } else if data.type_index == TypeIndex::kTVMFFIBool as i32 {
            Ok(Self::from_i64(data.data_union.v_int64))
        } else {
            Err(())
        }
    }
}