tvm-ffi 0.1.16

tvm-ffi rust support
Documentation
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
/*
 * 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.
 */
//! Word-level integer algorithms behind the `BigInt` operators.
//!
//! Every fallback takes canonical two's-complement words, least-significant
//! first, and returns a canonical `BigInt`. Word arithmetic wraps modulo 2^64
//! on purpose, hence the explicit `wrapping_*`/`overflowing_*` forms. A result
//! whose words are each computed once is written straight into a fresh object
//! and pruned in place (`BigInt::build`); multiplication and division
//! accumulate into a zeroed [`WordsBuf`] and finish with [`BigInt::from_words`].

// The functions mirror C++ `detail::int_ops` in `include/tvm/ffi/big_int.h` one
// for one; keep them in sync.
#![allow(clippy::needless_range_loop)]

use super::BigInt;
use crate::error::{Error, OVERFLOW_ERROR, Result, VALUE_ERROR, ZERO_DIVISION_ERROR};
use std::mem::MaybeUninit;

const BASE: u64 = 1 << 32;

/// Words kept on the stack before a result or scratch buffer spills to the heap.
const INLINE_WORDS: usize = 32;

/// Result or scratch words, zeroed in place on first use: up to `INLINE_WORDS` on
/// the caller's stack, longer on the heap. Two phases avoid moving the array.
struct WordsBuf {
    inline: MaybeUninit<[i64; INLINE_WORDS]>,
    heap: Vec<i64>,
}

impl WordsBuf {
    #[inline(always)]
    fn new() -> Self {
        Self {
            inline: MaybeUninit::uninit(),
            heap: Vec::new(),
        }
    }

    /// Zero `len` words in place and expose them; like `Vec`, exhaustion aborts.
    #[inline]
    fn zeroed(&mut self, len: usize) -> &mut [i64] {
        if len <= INLINE_WORDS {
            return self.inline_zeroed(len);
        }
        self.heap.resize(len, 0);
        &mut self.heap
    }

    #[inline]
    fn inline_zeroed(&mut self, len: usize) -> &mut [i64] {
        debug_assert!(len <= INLINE_WORDS);
        let words = self.inline.as_mut_ptr().cast::<i64>();
        // Only these `len` words are ever exposed, and they are initialized here.
        unsafe {
            std::ptr::write_bytes(words, 0, len);
            std::slice::from_raw_parts_mut(words, len)
        }
    }
}

pub(super) fn zero_division() -> Error {
    Error::new(ZERO_DIVISION_ERROR, "Division by zero")
}

fn negative_shift() -> Error {
    Error::new(VALUE_ERROR, "Negative BigInt shift count")
}

fn overflow(message: &str) -> Error {
    Error::new(OVERFLOW_ERROR, message)
}

/// Whether a nonempty signed-word view represents a negative integer.
#[inline]
pub(super) fn is_negative(x: &[i64]) -> bool {
    x[x.len() - 1] < 0
}

/// The word that sign-extends `x` beyond its length.
#[inline]
pub(super) fn extension(x: &[i64]) -> u64 {
    if is_negative(x) { u64::MAX } else { 0 }
}

/// Word `i` of `x`, sign-extended past its length.
#[inline]
pub(super) fn word_at(x: &[i64], i: usize, extension: u64) -> u64 {
    x.get(i).map_or(extension, |&w| w as u64)
}

/// Length after pruning redundant sign words; zero words stay zero.
pub(super) fn normalized_len(words: &[i64]) -> usize {
    let mut size = words.len();
    while size > 1 && words[size - 1] == (if words[size - 2] < 0 { -1 } else { 0 }) {
        size -= 1;
    }
    size
}

// ---- Inline (i64) forms. `Ok(None)` means the result needs the multiword fallback.

/// Quotient of division rounded toward zero.
#[inline]
pub(super) fn trunc_div_i64(a: i64, b: i64) -> Result<Option<i64>> {
    if b == 0 {
        return Err(zero_division());
    }
    // i64::MIN / -1 is the only quotient that needs promotion.
    Ok(a.checked_div(b))
}

/// Remainder of division rounded toward zero.
#[inline]
pub(super) fn trunc_mod_i64(a: i64, b: i64) -> Result<Option<i64>> {
    if b == 0 {
        return Err(zero_division());
    }
    // The remainder of i64::MIN / -1 is zero, which the wrapping form yields.
    Ok(Some(a.wrapping_rem(b)))
}

/// Quotient of division rounded down.
#[inline]
pub(super) fn floor_div_i64(a: i64, b: i64) -> Result<Option<i64>> {
    if b == 0 {
        return Err(zero_division());
    }
    Ok(a.checked_div(b).map(|q| {
        // A nonexact quotient of opposite signs rounds one lower.
        if a.wrapping_rem(b) != 0 && ((a < 0) != (b < 0)) {
            q - 1
        } else {
            q
        }
    }))
}

/// Remainder of division rounded down.
#[inline]
pub(super) fn floor_mod_i64(a: i64, b: i64) -> Result<Option<i64>> {
    if b == 0 {
        return Err(zero_division());
    }
    let r = a.wrapping_rem(b);
    // Compensate for the quotient rounding down, preserving a = q*b + r.
    Ok(Some(if r != 0 && ((a < 0) != (b < 0)) {
        r + b
    } else {
        r
    }))
}

/// Left shift, rejecting negative counts.
#[inline]
pub(super) fn shl_i64(a: i64, b: i64) -> Result<Option<i64>> {
    if b < 0 {
        return Err(negative_shift());
    }
    if b == 0 || a == 0 {
        return Ok(Some(a));
    }
    // For positive a require a <= i64::MAX >> b; for negative a require a >= -2^(63-b).
    if b >= 64 || a > (i64::MAX >> b) || a < -(1i64 << (63 - b)) {
        return Ok(None);
    }
    Ok(Some(a << b))
}

/// Right shift with arithmetic sign extension; this operation cannot overflow.
#[inline]
pub(super) fn shr_i64(a: i64, b: i64) -> Result<i64> {
    if b < 0 {
        return Err(negative_shift());
    }
    Ok(if b >= 64 {
        if a < 0 { -1 } else { 0 }
    } else {
        a >> b
    })
}

// ---- Multiword fallbacks.

/// Add signed integers.
pub(super) fn add(a: &[i64], b: &[i64]) -> BigInt {
    // One sign-extension word retains the carry out until normalization.
    let size = a.len().max(b.len()) + 1;
    let (ext_a, ext_b) = (extension(a), extension(b));
    let mut carry = 0u64;
    BigInt::build(size, |i| {
        let (sum, c1) = word_at(a, i, ext_a).overflowing_add(word_at(b, i, ext_b));
        let (result, c2) = sum.overflowing_add(carry);
        carry = u64::from(c1 | c2);
        result as i64
    })
}

/// Subtract signed integers: a - b = a + !b + 1 after sign extension.
pub(super) fn sub(a: &[i64], b: &[i64]) -> BigInt {
    let size = a.len().max(b.len()) + 1;
    let (ext_a, ext_b) = (extension(a), extension(b));
    let mut carry = 1u64;
    BigInt::build(size, |i| {
        let (sum, c1) = word_at(a, i, ext_a).overflowing_add(!word_at(b, i, ext_b));
        let (result, c2) = sum.overflowing_add(carry);
        carry = u64::from(c1 | c2);
        result as i64
    })
}

/// Negate a signed integer: -x = !x + 1, with room for negating a minimum value.
pub(super) fn negate(x: &[i64]) -> BigInt {
    let size = x.len() + 1;
    let ext = extension(x);
    let mut carry = 1u64;
    BigInt::build(size, |i| {
        let word = !word_at(x, i, ext);
        let (result, overflow) = word.overflowing_add(carry);
        carry = u64::from(overflow);
        result as i64
    })
}

/// Negate two's-complement words in place, modulo their fixed width.
pub(super) fn negate_in_place(words: &mut [i64]) {
    let mut carry = true;
    for word in words.iter_mut() {
        let (sum, overflow) = (!(*word as u64)).overflowing_add(u64::from(carry));
        *word = sum as i64;
        carry = overflow;
    }
}

/// Multiply signed integers.
pub(super) fn mul(a: &[i64], b: &[i64]) -> BigInt {
    let (na, nb) = (a.len(), b.len());
    let mut buf = WordsBuf::new();
    let data = buf.zeroed(na + nb);
    for i in 0..na {
        let x = u128::from(a[i] as u64);
        let mut carry = 0u128;
        for j in 0..nb {
            // x*y + word + carry < 2^128: each term is below its width's maximum.
            let product = x * u128::from(b[j] as u64) + u128::from(data[i + j] as u64) + carry;
            data[i + j] = product as u64 as i64;
            carry = product >> 64;
        }
        data[i + nb] = carry as u64 as i64;
    }
    // With stored unsigned A/B and 0/1 negative indicators neg_a/neg_b,
    // a = A - neg_a*2^(64*n), b = B - neg_b*2^(64*m). Thus ab is AB minus
    // neg_a*B*2^(64*n) and neg_b*A*2^(64*m); the remaining
    // neg_a*neg_b*2^(64*(n+m)) vanishes modulo the allocated n+m words.
    let subtract_shifted = |data: &mut [i64], x: &[i64], shift: usize| {
        let mut borrow = 0u64;
        for i in 0..x.len() {
            let word = x[i] as u64;
            let (rhs, o1) = word.overflowing_add(borrow);
            let (result, o2) = (data[shift + i] as u64).overflowing_sub(rhs);
            data[shift + i] = result as i64;
            borrow = u64::from(o1 | o2);
        }
    };
    if is_negative(a) {
        subtract_shifted(data, b, na);
    }
    if is_negative(b) {
        subtract_shifted(data, a, nb);
    }
    BigInt::from_words(data)
}

/// Apply a sign-extended bitwise operation.
fn bitwise(a: &[i64], b: &[i64], op: impl Fn(u64, u64) -> u64) -> BigInt {
    let size = a.len().max(b.len());
    let (ext_a, ext_b) = (extension(a), extension(b));
    BigInt::build(size, |i| {
        op(word_at(a, i, ext_a), word_at(b, i, ext_b)) as i64
    })
}

pub(super) fn and(a: &[i64], b: &[i64]) -> BigInt {
    bitwise(a, b, |x, y| x & y)
}

pub(super) fn or(a: &[i64], b: &[i64]) -> BigInt {
    bitwise(a, b, |x, y| x | y)
}

pub(super) fn xor(a: &[i64], b: &[i64]) -> BigInt {
    bitwise(a, b, |x, y| x ^ y)
}

/// Complement all sign-extended bits.
pub(super) fn not(x: &[i64]) -> BigInt {
    BigInt::build(x.len(), |i| !x[i])
}

/// Convert a nonnegative shift count; `None` when it does not fit `usize`.
fn shift_count(count: &[i64]) -> Result<Option<usize>> {
    if is_negative(count) {
        return Err(negative_shift());
    }
    if count[1..].iter().any(|&w| w != 0) {
        return Ok(None);
    }
    Ok(usize::try_from(count[0] as u64).ok())
}

/// Shift a signed integer left, growing as needed.
///
/// Inlined so callers unwrap the `Result` in registers: moving a `BigInt` out of a
/// returned `Result` reloads it in one 16-byte read that store forwarding misses.
#[inline]
pub(super) fn shl(x: &[i64], count: &[i64]) -> Result<BigInt> {
    let shift = shift_count(count)?;
    if x.len() == 1 && x[0] == 0 {
        return Ok(BigInt::from_i64(0));
    }
    let too_large = || overflow("BigInt shift count is too large");
    let shift = shift.ok_or_else(too_large)?;
    let whole = shift / 64;
    let part = (shift % 64) as u32;
    let size = x
        .len()
        .checked_add(whole)
        .and_then(|size| size.checked_add(1))
        .ok_or_else(too_large)?;
    let ext = extension(x);
    BigInt::try_build(size, |i| {
        // The low `whole` words are zero.
        if i < whole {
            return 0;
        }
        let j = i - whole;
        let mut word = word_at(x, j, ext) << part;
        if part != 0 && j != 0 {
            word |= (x[j - 1] as u64) >> (64 - part);
        }
        word as i64
    })
    .ok_or_else(|| overflow("BigInt allocation is too large"))
}

/// Shift a signed integer right with sign extension; huge counts give 0 or -1.
pub(super) fn shr(x: &[i64], count: &[i64]) -> Result<BigInt> {
    let shift = match shift_count(count)? {
        Some(shift) if shift / 64 < x.len() => shift,
        _ => return Ok(BigInt::from_i64(if is_negative(x) { -1 } else { 0 })),
    };
    let whole = shift / 64;
    let size = x.len() - whole;
    let part = (shift % 64) as u32;
    let ext = extension(x);
    let result = BigInt::build(size, |i| {
        let mut word = (x[i + whole] as u64) >> part;
        // The last partial word draws its high bits from the sign extension.
        if part != 0 {
            word |= word_at(x, i + whole + 1, ext) << (64 - part);
        }
        word as i64
    });
    Ok(result)
}

// Absolute-value access without copying operands: negation carries through the
// low zero words, negates the first nonzero word, then complements higher words.

/// The first nonzero index, or `x.len()` for zero.
fn first_nonzero(x: &[i64]) -> usize {
    x.iter().position(|&w| w != 0).unwrap_or(x.len())
}

/// Raw word `i` of the absolute value of `x`; indices beyond the view read as zero.
#[inline]
fn abs_word(x: &[i64], first_nonzero: usize, i: usize) -> i64 {
    if i >= x.len() {
        return 0;
    }
    let word = x[i];
    if !is_negative(x) {
        return word;
    }
    // Negation maps [0,0,w,v] to [0,0,-w,!v] as modulo-2^64 word patterns.
    if i < first_nonzero {
        0
    } else if i == first_nonzero {
        word.wrapping_neg()
    } else {
        !word
    }
}

/// Truncating quotient and remainder with an `i64` divisor.
///
/// The remainder is zero or has the dividend's sign.
pub(super) fn div_rem_single(a: &[i64], b: i64) -> Result<(BigInt, BigInt)> {
    if b == 0 {
        return Err(zero_division());
    }
    let divisor = b.unsigned_abs();
    let first_a = first_nonzero(a);
    let nq = a.len() + 1;
    let mut quotient = WordsBuf::new();
    // The top word stays zero as the sign guard.
    let q = quotient.zeroed(nq);
    let mut remainder = 0u64;
    let mut shift = 0u32;
    let mut normalized = divisor;
    if divisor > u64::from(u32::MAX) {
        while normalized >> 63 == 0 {
            normalized <<= 1;
            shift += 1;
        }
    }
    let divisor_high = normalized >> 32;
    let divisor_low = normalized as u32 as u64;
    for i in (0..a.len()).rev() {
        let word = abs_word(a, first_a, i) as u64;
        let quotient_word = if divisor <= u64::from(u32::MAX) {
            // remainder < divisor < 2^32, so each brought-down half fits u64
            // and yields a quotient half below 2^32 while restoring the bound.
            let high = (remainder << 32) | (word >> 32);
            let quotient_high = high / divisor;
            remainder = high % divisor;
            let low = (remainder << 32) | (word as u32 as u64);
            let quotient_low = low / divisor;
            remainder = low % divisor;
            (quotient_high << 32) | quotient_low
        } else {
            // Normalize the carried 128-bit numerator without ever shifting by 64.
            let high = if shift != 0 {
                (remainder << shift) | (word >> (64 - shift))
            } else {
                remainder
            };
            let low = word << shift;
            // Requires upper < normalized; returns a radix-2^32 quotient digit
            // and a remainder below normalized.
            let digit = |upper: u64, next: u64| -> (u64, u64) {
                let mut estimate = upper / divisor_high;
                let mut residual = upper % divisor_high;
                // The normalized high digit bounds overestimation by two. Check
                // the base before multiplying, and stop before a residual shift
                // can overflow.
                while estimate >= BASE || estimate * divisor_low > (residual << 32) + next {
                    estimate -= 1;
                    residual += divisor_high;
                    if residual >= BASE {
                        break;
                    }
                }
                // Wrapped subtraction is exact: the true remainder is below normalized.
                let rest = (upper << 32)
                    .wrapping_add(next)
                    .wrapping_sub(estimate.wrapping_mul(normalized));
                (estimate, rest)
            };
            let upper = digit(high, low >> 32);
            let lower = digit(upper.1, low as u32 as u64);
            remainder = lower.1 >> shift;
            (upper.0 << 32) | lower.0
        };
        q[i] = quotient_word as i64;
    }
    if is_negative(a) != (b < 0) {
        negate_in_place(&mut q[..nq]);
    }
    // remainder < |b| <= 2^63 makes the conversion and signed negation safe.
    let mut signed_remainder = remainder as i64;
    if is_negative(a) {
        signed_remainder = -signed_remainder;
    }
    Ok((BigInt::from_words(q), BigInt::from_i64(signed_remainder)))
}

/// Truncating quotient and remainder in one division pass.
///
/// The remainder is zero or has the dividend's sign.
pub(super) fn div_rem(a: &[i64], b: &[i64]) -> Result<(BigInt, BigInt)> {
    if b.len() == 1 {
        return div_rem_single(a, b[0]);
    }
    let first_a = first_nonzero(a);
    let first_b = first_nonzero(b);
    // Count magnitude digits, dropping the signed representation's high zero guards.
    let digit_count = |x: &[i64], first: usize| -> usize {
        let mut words = x.len();
        while words > 0 && abs_word(x, first, words - 1) == 0 {
            words -= 1;
        }
        if words == 0 {
            return 0;
        }
        let top = abs_word(x, first, words - 1) as u64;
        2 * words - usize::from(top >> 32 == 0)
    };
    let m = digit_count(a, first_a);
    let n = digit_count(b, first_b);
    if m < n {
        return Ok((BigInt::from_i64(0), BigInt::from_words(a)));
    }

    // Algorithm D in radix 2^32 keeps products and estimates in u64. Digits below
    // are least-significant first; the scalar dispatch left at least two in b.
    let top_word = abs_word(b, first_b, (n - 1) / 2) as u64;
    let mut top_digit = (top_word >> (((n - 1) % 2) * 32)) as u32;
    let mut shift = 0u32;
    // Normalize both inputs until B[0] >= base/2, bounding the quotient estimate error.
    while top_digit < (1u32 << 31) {
        top_digit <<= 1;
        shift += 1;
    }
    let nq = (m - n) / 2 + 2;
    let nr = n.div_ceil(2) + 1;
    // The quotient words come first, then the Algorithm D scratch, in one buffer.
    let capacity = nq
        .checked_add(m + 1)
        .and_then(|words| words.checked_add(n))
        .ok_or_else(|| overflow("BigInt division workspace is too large"))?;
    let mut quotient = WordsBuf::new();
    let mut remainder = WordsBuf::new();
    let (q, scratch) = quotient.zeroed(capacity).split_at_mut(nq);
    let (u, v) = scratch.split_at_mut(m + 1);
    let r = remainder.zeroed(nr);
    let normalize = |x: &[i64], first: usize, digits: usize, out: &mut [i64]| -> u32 {
        let mut carry = 0u64;
        for i in 0..digits {
            let word = abs_word(x, first, i / 2) as u64;
            let part = (word >> ((i % 2) * 32)) as u32 as u64;
            let value = (part << shift) | carry;
            out[i] = (value as u32) as i64;
            carry = value >> 32;
        }
        carry as u32
    };
    // The dividend also keeps its shifted high carry.
    u[m] = i64::from(normalize(a, first_a, m, &mut u[..m]));
    normalize(b, first_b, n, &mut v[..n]);

    let mut quotient_word = 0u64;
    for j in (0..=(m - n)).rev() {
        // Step 1: estimate q = A[0]A[1] / B[0] with A[0] = u[j+n], B[0] = v[n-1];
        // A[0] <= B[0], and equality is clamped so q stays a radix digit.
        let (mut estimate, mut residual) = if u[j + n] == v[n - 1] {
            (BASE - 1, u[j + n - 1] as u64 + v[n - 1] as u64)
        } else {
            let numerator = ((u[j + n] as u64) << 32) | u[j + n - 1] as u64;
            (numerator / v[n - 1] as u64, numerator % v[n - 1] as u64)
        };
        // Step 2: refine q with B[1] until q*B[1] <= r*base + A[2]; at most two
        // decrements, and r < base guards the shift.
        while residual < BASE && estimate * v[n - 2] as u64 > (residual << 32) + u[j + n - 2] as u64
        {
            estimate -= 1;
            residual += v[n - 1] as u64;
        }
        // Step 3: subtract q * B from the window; q is correct or one too high.
        let mut borrow = 0u64;
        for i in 0..n {
            // A radix-digit product plus the incoming borrow fits u64.
            let product = estimate * v[i] as u64 + borrow;
            let low = product as u32;
            let old = u[j + i] as u32;
            u[j + i] = i64::from(old.wrapping_sub(low));
            borrow = (product >> 32) + u64::from(old < low);
        }
        let old = u[j + n] as u64;
        // Only a borrow out of the full window means the result went negative.
        let negative = old < borrow;
        u[j + n] = i64::from(old.wrapping_sub(borrow) as u32);
        if negative {
            // Step 4: decrement q and add B back, restoring 0 <= R < divisor.
            estimate -= 1;
            let mut carry = 0u64;
            for i in 0..n {
                let sum = u[j + i] as u64 + v[i] as u64 + carry;
                u[j + i] = i64::from(sum as u32);
                carry = sum >> 32;
            }
            u[j + n] = i64::from((u[j + n] as u64 + carry) as u32);
        }
        // Pack descending quotient digits into complete words.
        if j % 2 == 1 {
            quotient_word = estimate << 32;
        } else {
            q[j / 2] = (quotient_word | estimate) as i64;
        }
    }
    // Extract the remainder, undoing the normalization shift.
    for i in 0..nr - 1 {
        let digit = 2 * i;
        let mut word = u[digit] as u64;
        if digit + 1 < n {
            word |= (u[digit + 1] as u64) << 32;
        }
        word >>= shift;
        // Bring down bits from the next digit; shift zero never evaluates a shift by 64.
        if shift != 0 && digit + 2 < n {
            word |= (u[digit + 2] as u64) << (64 - shift);
        }
        r[i] = word as i64;
    }
    // The quotient takes the operand-sign XOR, a nonzero remainder the dividend's sign.
    if is_negative(a) != is_negative(b) {
        negate_in_place(q);
    }
    if is_negative(a) {
        negate_in_place(r);
    }
    Ok((BigInt::from_words(q), BigInt::from_words(r)))
}

/// Quotient rounded toward zero.
pub(super) fn trunc_div(a: &[i64], b: &[i64]) -> Result<BigInt> {
    Ok(div_rem(a, b)?.0)
}

/// Remainder of truncating division: zero or with the dividend's sign.
pub(super) fn trunc_mod(a: &[i64], b: &[i64]) -> Result<BigInt> {
    if b.len() == 1 {
        if b[0] == 0 {
            return Err(zero_division());
        }
        let divisor = b[0].unsigned_abs();
        if divisor <= u64::from(u32::MAX) {
            // remainder < divisor < 2^32: each brought-down half fits u64 and the
            // result is inline, so no quotient or allocation is needed.
            let first = first_nonzero(a);
            let mut remainder = 0u64;
            for i in (0..a.len()).rev() {
                let word = abs_word(a, first, i) as u64;
                remainder = ((remainder << 32) | (word >> 32)) % divisor;
                remainder = ((remainder << 32) | (word as u32 as u64)) % divisor;
            }
            let result = remainder as i64;
            return Ok(BigInt::from_i64(if is_negative(a) {
                -result
            } else {
                result
            }));
        }
    }
    Ok(div_rem(a, b)?.1)
}

/// Quotient rounded toward negative infinity.
pub(super) fn floor_div(a: &[i64], b: &[i64]) -> Result<BigInt> {
    let (quotient, remainder) = div_rem(a, b)?;
    if !remainder.is_zero() && is_negative(a) != is_negative(b) {
        // With a nonzero remainder, (q-1)*b + (r+b) preserves a while rounding down.
        return Ok(sub(quotient.words(), &[1]));
    }
    Ok(quotient)
}

/// Remainder of floor division: zero or with the divisor's sign.
pub(super) fn floor_mod(a: &[i64], b: &[i64]) -> Result<BigInt> {
    let remainder = trunc_mod(a, b)?;
    if !remainder.is_zero() && is_negative(a) != is_negative(b) {
        // The floor remainder is r+b, which |r| < |b| keeps inline for a scalar divisor.
        if let ([divisor], Some(r)) = (b, remainder.to_i64()) {
            return Ok(BigInt::from_i64(r + divisor));
        }
        return Ok(add(remainder.words(), b));
    }
    Ok(remainder)
}

/// Convert to the nearest double, ties to even; `OverflowError` beyond the finite range.
pub(super) fn to_f64(x: &[i64]) -> Result<f64> {
    let first = first_nonzero(x);
    let mut size = x.len();
    while size > 0 && abs_word(x, first, size - 1) == 0 {
        size -= 1;
    }
    if size == 0 {
        return Ok(0.0);
    }
    let does_not_fit = || overflow("BigInt does not fit finite double");
    if size > 16 {
        return Err(does_not_fit());
    }
    let high = abs_word(x, first, size - 1) as u64;
    let bits = (size - 1) * 64 + (64 - high.leading_zeros() as usize);
    let shift = bits.saturating_sub(53);
    let bit = |i: usize| ((abs_word(x, first, i / 64) as u64) >> (i % 64)) & 1;
    let mut significand = 0u64;
    for i in (shift..bits).rev() {
        significand = (significand << 1) | bit(i);
    }
    if shift > 0 {
        let sticky = (0..shift - 1).any(|i| bit(i) != 0);
        if bit(shift - 1) == 1 && (sticky || significand & 1 == 1) {
            significand += 1;
        }
    }
    if bits == 1024 && significand == 1 << 53 {
        return Err(does_not_fit());
    }
    let result = significand as f64 * 2f64.powi(shift as i32);
    Ok(if is_negative(x) { -result } else { result })
}

/// Multiply magnitude words by `factor` and add `addend`, growing as needed.
pub(super) fn mul_add_small(magnitude: &mut Vec<u64>, factor: u64, addend: u64) {
    let mut carry = u128::from(addend);
    for word in magnitude.iter_mut() {
        let product = u128::from(*word) * u128::from(factor) + carry;
        *word = product as u64;
        carry = product >> 64;
    }
    if carry != 0 {
        magnitude.push(carry as u64);
    }
}