malachite-nz 0.9.2

The bignum types Natural and Integer, with efficient algorithms partially derived from GMP and FLINT.
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
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
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422
1423
1424
1425
1426
1427
1428
1429
1430
1431
1432
1433
1434
1435
1436
1437
1438
1439
1440
1441
1442
1443
1444
1445
1446
1447
1448
1449
1450
1451
1452
1453
1454
1455
1456
1457
1458
1459
1460
1461
1462
1463
1464
1465
1466
1467
1468
1469
1470
1471
1472
1473
1474
1475
1476
1477
1478
1479
1480
1481
1482
1483
1484
1485
1486
1487
1488
1489
1490
1491
1492
1493
1494
1495
1496
1497
1498
1499
1500
1501
1502
1503
1504
1505
1506
1507
1508
1509
1510
1511
1512
1513
1514
1515
1516
1517
1518
1519
1520
1521
1522
1523
1524
1525
1526
1527
1528
1529
1530
1531
1532
1533
1534
1535
1536
1537
1538
1539
1540
1541
1542
1543
1544
1545
1546
1547
1548
1549
1550
1551
1552
1553
1554
1555
1556
1557
1558
1559
1560
1561
1562
1563
1564
1565
1566
1567
1568
1569
1570
1571
1572
1573
1574
1575
1576
1577
1578
1579
1580
1581
1582
1583
1584
1585
1586
1587
1588
1589
1590
1591
1592
1593
1594
1595
1596
1597
1598
1599
1600
1601
1602
1603
1604
1605
1606
1607
1608
1609
1610
1611
1612
1613
1614
1615
// Copyright © 2026 Mikhail Hogrefe
//
// Uses code adopted from the GNU MP Library.
//
//      Copyright © 1991-2019 Free Software Foundation, Inc.
//
// This file is part of Malachite.
//
// Malachite is free software: you can redistribute it and/or modify it under the terms of the GNU
// Lesser General Public License (LGPL) as published by the Free Software Foundation; either version
// 3 of the License, or (at your option) any later version. See <https://www.gnu.org/licenses/>.

use crate::natural::arithmetic::add::{
    limbs_add_to_out_aliased, limbs_slice_add_greater_in_place_left, limbs_slice_add_limb_in_place,
};
use crate::natural::arithmetic::add_mul::limbs_slice_add_mul_limb_same_length_in_place_left;
use crate::natural::arithmetic::div_mod::{
    limbs_div_limb_to_out_mod, limbs_div_mod_qs_to_out_rs_to_ns,
};
use crate::natural::arithmetic::gcd::matrix_2_2::limbs_matrix_2_2_mul;
use crate::natural::arithmetic::mul::limb::{limbs_mul_limb_to_out, limbs_slice_mul_limb_in_place};
use crate::natural::arithmetic::mul::mul_mod::{
    limbs_mul_mod_base_pow_n_minus_1, limbs_mul_mod_base_pow_n_minus_1_next_size,
    limbs_mul_mod_base_pow_n_minus_1_scratch_len,
};
use crate::natural::arithmetic::mul::{
    limbs_mul_greater_to_out, limbs_mul_greater_to_out_scratch_len, limbs_mul_to_out,
    limbs_mul_to_out_scratch_len,
};
use crate::natural::arithmetic::shr::limbs_slice_shr_in_place;
use crate::natural::arithmetic::sub::{
    limbs_sub_greater_in_place_left, limbs_sub_limb_in_place, limbs_sub_same_length_in_place_left,
};
use crate::natural::arithmetic::sub_mul::limbs_sub_mul_limb_same_length_in_place_left;
use crate::natural::comparison::cmp::limbs_cmp_same_length;
use crate::natural::{bit_to_limb_count_floor, limb_to_bit_count};
use crate::platform::{DoubleLimb, Limb};
use core::cmp::{Ordering::*, max, min};
use core::mem::swap;
use malachite_base::fail_on_untested_path;
use malachite_base::num::arithmetic::traits::{
    DivMod, Gcd, Parity, WrappingAddAssign, XMulYToZZ, XXDivModYToQR, XXSubYYToZZ,
};
use malachite_base::num::basic::integers::PrimitiveInt;
use malachite_base::num::conversion::traits::{ExactFrom, JoinHalves, SplitInHalf, WrappingFrom};
use malachite_base::num::logic::traits::{LeadingZeros, NotAssign, TrailingZeros};
use malachite_base::slices::{slice_set_zero, slice_test_zero, slice_trailing_zeros};

pub(crate) trait GcdSubdivideStepContext {
    fn gcd_subdiv_step_hook(
        &mut self,
        g: Option<&[Limb]>,
        q: Option<&mut [Limb]>,
        q_len: usize,
        d: i8,
    );

    fn gcd_subdiv_step_hook_with_1(&mut self, d: i8) {
        self.gcd_subdiv_step_hook(None, Some(&mut [1]), 1, d);
    }
}

/// This is equivalent to `gcd_ctx` from `mpn/gcd.c`, GMP 6.2.1.
struct GcdContext<'a>(&'a mut [Limb]);

impl GcdSubdivideStepContext for GcdContext<'_> {
    /// This is equivalent to `gcd_hook` from `mpn/gcd.c`, GMP 6.2.1.
    fn gcd_subdiv_step_hook(
        &mut self,
        g: Option<&[Limb]>,
        _q: Option<&mut [Limb]>,
        _q_len: usize,
        _d: i8,
    ) {
        if let Some(g) = g {
            self.0[..g.len()].copy_from_slice(g);
        }
    }
}

#[cfg(feature = "test_build")]
pub struct HalfGcdMatrix<'a> {
    pub(crate) data: &'a mut [Limb],
    pub(crate) s: usize,
    pub(crate) two_s: usize,
    pub(crate) three_s: usize,
    pub(crate) n: usize,
}

#[cfg(not(feature = "test_build"))]
pub(crate) struct HalfGcdMatrix<'a> {
    data: &'a mut [Limb],
    pub(crate) s: usize,
    two_s: usize,
    three_s: usize,
    pub(crate) n: usize,
}

impl HalfGcdMatrix<'_> {
    // # Worst-case complexity
    // Constant time and additional memory.
    pub_crate_test! {get(&self, row: u8, column: u8) -> &[Limb] {
        match (row, column) {
            (0, 0) => &self.data[..self.s],
            (0, 1) => &self.data[self.s..self.two_s],
            (1, 0) => &self.data[self.two_s..self.three_s],
            (1, 1) => &self.data[self.three_s..],
            _ => panic!(),
        }
    }}

    // # Worst-case complexity
    // Constant time and additional memory.
    pub_test! {get_mut(&mut self, row: u8, column: u8) -> &mut [Limb] {
        match (row, column) {
            (0, 0) => &mut self.data[..self.s],
            (0, 1) => &mut self.data[self.s..self.two_s],
            (1, 0) => &mut self.data[self.two_s..self.three_s],
            (1, 1) => &mut self.data[self.three_s..],
            _ => panic!(),
        }
    }}

    // # Worst-case complexity
    // Constant time and additional memory.
    #[inline]
    fn get_two_mut(
        &mut self,
        row_1: u8,
        column_1: u8,
        row_2: u8,
        column_2: u8,
    ) -> (&mut [Limb], &mut [Limb]) {
        match (row_1, column_1, row_2, column_2) {
            (0, 0, 0, 1) => self.data[..self.two_s].split_at_mut(self.s),
            (0, 1, 0, 0) => {
                let (xs, ys) = self.data[..self.two_s].split_at_mut(self.s);
                (ys, xs)
            }
            (1, 0, 1, 1) => self.data[self.two_s..].split_at_mut(self.s),
            (1, 1, 1, 0) => {
                let (xs, ys) = self.data[self.two_s..].split_at_mut(self.s);
                (ys, xs)
            }
            _ => panic!(),
        }
    }

    // # Worst-case complexity
    // Constant time and additional memory.
    #[inline]
    pub(crate) fn get_four(&mut self) -> (&[Limb], &[Limb], &[Limb], &[Limb]) {
        split_into_chunks!(self.data, self.s, [x00, x01, x10], x11);
        (x00, x01, x10, x11)
    }

    // # Worst-case complexity
    // Constant time and additional memory.
    #[inline]
    fn get_four_mut(&mut self) -> (&mut [Limb], &mut [Limb], &mut [Limb], &mut [Limb]) {
        split_into_chunks_mut!(self.data, self.s, [x00, x01, x10], x11);
        (x00, x01, x10, x11)
    }

    // # Worst-case complexity
    // Constant time and additional memory.
    pub_const_test! {min_init_scratch(n: usize) -> usize {
        (((n + 1) >> 1) + 1) << 2
    }}

    // For input of size n, matrix elements are of size at most ceil(n / 2) - 1, but we need two
    // limbs extra.
    //
    // # Worst-case complexity
    // $T(n) = O(n)$
    //
    // $M(n) = O(1)$
    //
    // where $T$ is time, $M$ is additional memory, and $n$ is `p.len()`.
    //
    // This is equivalent to `mpn_hgcd_matrix_init` from `mpn/generic/hgcd_matrix.c`, GMP 6.2.1,
    // where the matrix is returned.
    pub_crate_test! {init(n: usize, p: &mut [Limb]) -> HalfGcdMatrix<'_> {
        let s = n.div_ceil(2) + 1;
        let two_s = s << 1;
        let three_s = two_s + s;
        slice_set_zero(&mut p[..s << 2]);
        let mut m = HalfGcdMatrix {
            data: p,
            s,
            two_s,
            three_s,
            n: 1,
        };
        m.get_mut(0, 0)[0] = 1;
        m.get_mut(1, 1)[0] = 1;
        m
    }}

    // # Worst-case complexity
    // Constant time and additional memory.
    pub_const_test! {update_q_scratch_len(&self, qs_len: usize) -> usize {
        self.n + qs_len
    }}

    // # Worst-case complexity
    // Constant time and additional memory.
    fn all_elements_zero_at_index(&self, i: usize) -> bool {
        self.get(0, 0)[i] == 0
            && self.get(0, 1)[i] == 0
            && self.get(1, 0)[i] == 0
            && self.get(1, 1)[i] == 0
    }
}

// Multiply M by M1 from the right. Needs 3*(M->n + M1->n) + 5 limbs of temporary storage (see
// mpn_matrix22_mul_itch).
//
// # Worst-case complexity
// $T(n) = O(n \log n \log\log n)$
//
// $M(n) = O(n \log n)$
//
// where $T$ is time, $M$ is additional memory, and $n$ is `max(a.n, b.n)`.
//
// This is equivalent to `mpn_hgcd_matrix_mul` from `mpn/generic/hgcd_matrix.c`, GMP 6.2.1.
pub_crate_test! {limbs_half_gcd_matrix_mul_matrix(
    a: &mut HalfGcdMatrix,
    b: &HalfGcdMatrix,
    scratch: &mut [Limb]
) {
    // About the new size of M:s elements. Since M1's diagonal elements are > 0, no element can
    // decrease. The new elements are of size M->n + M1->n, one limb more or less. The computation
    // of the matrix product produces elements of size M->n + M1->n + 1. But the true size, after
    // normalization, may be three limbs smaller.
    //
    // The reason that the product has normalized size >= M->n + M1->n - 2 is subtle. It depends on
    // the fact that M and M1 can be factored as products of (1,1; 0,1) and (1,0; 1,1), and that we
    // can't have M ending with a large power and M1 starting with a large power of the same matrix.
    assert!(a.n + b.n < a.s);
    assert!(!a.all_elements_zero_at_index(a.n - 1));
    let b_n = b.n;
    assert!(!b.all_elements_zero_at_index(b_n - 1));
    let n = a.n;
    let (x00, x01, x10, x11) = a.get_four_mut();
    limbs_matrix_2_2_mul(
        x00,
        x01,
        x10,
        x11,
        n,
        &b.get(0, 0)[..b_n],
        &b.get(0, 1)[..b_n],
        &b.get(1, 0)[..b_n],
        &b.get(1, 1)[..b_n],
        scratch,
    );
    // Index of last potentially non-zero limb, size is one greater.
    let mut n = a.n + b_n;
    for _ in 0..3 {
        if a.all_elements_zero_at_index(n) {
            n -= 1;
        }
    }
    assert!(!a.all_elements_zero_at_index(n));
    a.n = n + 1;
}}

// Multiply M by M1 from the right. Since the M1 elements fit in Limb::WIDTH - 1 bits, M grows by at
// most one limb. Needs temporary space M->n
//
// # Worst-case complexity
// $T(n) = O(n)$
//
// $M(n) = O(1)$
//
// where $T$ is time, $M$ is additional memory, and $n$ is `a.n`.
//
// This is equivalent to `mpn_hgcd_matrix_mul_1` from `mpn/generic/hgcd_matrix.c`, GMP 6.2.1.
pub_crate_test! {limbs_half_gcd_matrix_mul_matrix_1(
    a: &mut HalfGcdMatrix,
    b: &HalfGcdMatrix1,
    scratch: &mut [Limb],
) {
    let n = a.n;
    let scratch = &mut scratch[..n];
    scratch.copy_from_slice(&a.get(0, 0)[..n]);
    let (a_0_0, a_0_1) = a.get_two_mut(0, 0, 0, 1);
    let n0 = limbs_half_gcd_matrix_1_mul_vector(b, a_0_0, scratch, a_0_1);
    scratch.copy_from_slice(&a.get(1, 0)[..n]);
    let (a_1_0, a_1_1) = a.get_two_mut(1, 0, 1, 1);
    let n1 = limbs_half_gcd_matrix_1_mul_vector(b, a_1_0, scratch, a_1_1);
    a.n = max(n0, n1);
    assert!(a.n <= a.s);
}}

// Update column `column`, adding in Q * column (1-`col`). Temporary storage: qn + n <= `self.s`,
// where n is the size of the largest element in column 1 - `column`.
//
// # Worst-case complexity
// $T(n) = O(n)$
//
// $M(n) = O(1)$
//
// where $T$ is time, $M$ is additional memory, and $n$ is `m.n`.
//
// This is equivalent to `mpn_hgcd_matrix_update_q` from `mpn/generic/hgcd_matrix.c`, GMP 6.2.1.
pub_crate_test! {limbs_half_gcd_matrix_update_q(
    m: &mut HalfGcdMatrix,
    qs: &[Limb],
    column: u8,
    scratch: &mut [Limb],
) {
    let qs_len = qs.len();
    assert!(qs_len + m.n <= m.s);
    assert!(column < 2);
    if qs_len == 1 {
        let q = qs[0];
        let n = m.n;
        let (m_0_a, m_0_b) = m.get_two_mut(0, column, 0, 1 - column);
        let carry_0 =
            limbs_slice_add_mul_limb_same_length_in_place_left(&mut m_0_a[..n], &m_0_b[..n], q);
        let (m_1_a, m_1_b) = m.get_two_mut(1, column, 1, 1 - column);
        let carry_1 =
            limbs_slice_add_mul_limb_same_length_in_place_left(&mut m_1_a[..n], &m_1_b[..n], q);
        m.get_mut(0, column)[n] = carry_0;
        m.get_mut(1, column)[n] = carry_1;
        if carry_0 != 0 || carry_1 != 0 {
            m.n += 1;
        }
    } else {
        // Carries for the unlikely case that we get both high words from the multiplication and
        // carries from the addition.
        let mut carries = [0; 2];
        // The matrix will not necessarily grow in size by qn, so we need normalization in order not
        // to overflow m.
        let mut n = m.n;
        while n + qs_len > m.n {
            assert_ne!(n, 0);
            if m.get(0, 1 - column)[n - 1] > 0 || m.get(1, 1 - column)[n - 1] > 0 {
                break;
            }
            n -= 1;
        }
        assert!(qs_len + n <= m.s);
        if n != 0 {
            let mut mul_scratch = vec![0; limbs_mul_to_out_scratch_len(n, qs_len)];
            for row in 0..2 {
                limbs_mul_to_out(
                    scratch,
                    &m.get(row, 1 - column)[..n],
                    &qs[..qs_len],
                    &mut mul_scratch,
                );
                assert!(n + qs_len >= m.n);
                let m_n = m.n;
                if limbs_add_to_out_aliased(m.get_mut(row, column), m_n, &scratch[..n + qs_len]) {
                    carries[usize::wrapping_from(row)] = 1;
                }
            }
        }
        n += qs_len;
        if carries[0] != 0 || carries[1] != 0 {
            m.get_mut(0, column)[n] = carries[0];
            m.get_mut(1, column)[n] = carries[1];
            n += 1;
        } else if m.get(0, column)[n - 1] == 0 && m.get(1, column)[n - 1] == 0 {
            n -= 1;
        }
        m.n = n;
    }
    assert!(m.n <= m.s);
}}

// - Multiplies the least significant p limbs of (X;Y) by M^-1.
// - Temporary space needed: 2 * (p + m.n)
//
// # Worst-case complexity
// $T(n) = O(n \log n \log\log n)$
//
// $M(n) = O(n \log n)$
//
// where $T$ is time, $M$ is additional memory, and $n$ is `n`.
//
// This is equivalent to `mpn_hgcd_matrix_adjust` from `mpn/generic/hgcd_matrix.c`, GMP 6.2.1.
pub(crate) fn limbs_half_gcd_matrix_adjust(
    m: &HalfGcdMatrix,
    mut n: usize,
    xs: &mut [Limb],
    ys: &mut [Limb],
    p: usize,
    scratch: &mut [Limb],
) -> usize {
    // M^-1 (X; Y) = (r11, -r01; -r10, r00) (a ; b) = (r11 x - r01 y; - r10 x + r00 y)
    let xs_init = &mut xs[..n];
    let ys_init = &mut ys[..n];
    let (scratch_lo, scratch_hi) = scratch.split_at_mut(p + m.n);
    assert!(p + m.n < n);
    let (xs_lo, xs_hi) = xs_init.split_at_mut(p);
    // First compute the two values depending on X, before overwriting X
    let mut mul_scratch = vec![0; limbs_mul_to_out_scratch_len(m.n, p)];
    limbs_mul_to_out(scratch_lo, &m.get(1, 1)[..m.n], xs_lo, &mut mul_scratch);
    limbs_mul_to_out(scratch_hi, &m.get(1, 0)[..m.n], xs_lo, &mut mul_scratch);
    // Update X
    let (scratch_lo_lo, scratch_lo_hi) = scratch_lo.split_at(p);
    xs_lo.copy_from_slice(scratch_lo_lo);
    let mut x_high = limbs_slice_add_greater_in_place_left(xs_hi, scratch_lo_hi);
    let (ys_lo, ys_hi) = ys_init.split_at_mut(p);
    limbs_mul_to_out(scratch_lo, &m.get(0, 1)[..m.n], ys_lo, &mut mul_scratch);
    if limbs_sub_greater_in_place_left(xs_init, scratch_lo) {
        assert!(x_high);
        x_high = false;
    }
    // Update Y
    limbs_mul_to_out(scratch_lo, &m.get(0, 0)[..m.n], ys_lo, &mut mul_scratch);
    let (scratch_lo_lo, scratch_lo_hi) = scratch_lo.split_at(p);
    ys_lo.copy_from_slice(scratch_lo_lo);
    let mut y_high = limbs_slice_add_greater_in_place_left(ys_hi, scratch_lo_hi);
    if limbs_sub_greater_in_place_left(ys_init, &scratch_hi[..p + m.n]) {
        assert!(y_high);
        y_high = false;
    }
    if x_high || y_high {
        xs[n] = Limb::from(x_high);
        ys[n] = Limb::from(y_high);
        n += 1;
    } else {
        // The subtraction can reduce the size by at most one limb.
        if xs[n - 1] == 0 && ys[n - 1] == 0 {
            n -= 1;
        }
    }
    assert!(xs[n - 1] != 0 || ys[n - 1] != 0);
    n
}

// Computes (x, y) <- M^(-1) (x; y)
//
// # Worst-case complexity
// $T(n) = O(n \log n \log\log n)$
//
// $M(n) = O(n \log n)$
//
// where $T$ is time, $M$ is additional memory, and $n$ is `m.len()`.
//
// This is equivalent to `mpn_hgcd_matrix_apply` from `mpn/generic/hgcd_reduce.c`, GMP 6.2.1.
fn limbs_half_gcd_matrix_apply(m: &HalfGcdMatrix, xs: &mut [Limb], ys: &mut [Limb]) -> usize {
    let mut n = xs.len();
    assert_eq!(ys.len(), n);
    assert!(xs[n - 1] != 0 || ys[n - 1] != 0);
    let xs_len = n - slice_trailing_zeros(&xs[..n]);
    let ys_len = n - slice_trailing_zeros(&ys[..n]);
    let xs_lo = &mut xs[..xs_len];
    let ys_lo = &mut ys[..ys_len];
    let mut m_lens = [[0usize; 2]; 2];
    for (i, row) in m_lens.iter_mut().enumerate() {
        for (j, len) in row.iter_mut().enumerate() {
            *len = m.n - slice_trailing_zeros(&m.get(i as u8, j as u8)[..m.n]);
        }
    }
    assert_ne!(m_lens[0][0], 0);
    assert_ne!(m_lens[1][1], 0);
    assert!(m_lens[0][1] != 0 || m_lens[1][0] != 0);
    if m_lens[0][1] == 0 {
        // X unchanged, M = (1, 0; q, 1)
        assert_eq!(m_lens[0][0], 1);
        assert_eq!(m.get(0, 0)[0], 1);
        assert_eq!(m_lens[1][1], 1);
        assert_eq!(m.get(1, 1)[0], 1);
        // Put Y <- Y - q X
        limbs_gcd_sub_mul(ys_lo, xs_lo, &m.get(1, 0)[..m_lens[1][0]])
    } else if m_lens[1][0] == 0 {
        fail_on_untested_path("limbs_half_gcd_matrix_apply, m_lens[1][0] == 0");
        // Y unchanged, M = (1, q; 0, 1)
        assert_eq!(m_lens[0][0], 1);
        assert_eq!(m.get(0, 0)[0], 1);
        assert_eq!(m_lens[1][1], 1);
        assert_eq!(m.get(1, 1)[0], 1);
        // Put X <- X - q * Y
        limbs_gcd_sub_mul(xs_lo, ys_lo, &m.get(0, 1)[..m_lens[0][1]])
    } else {
        // - X = m00 x + m01 y => x <= X / m00, y <= X / m01.
        // - Y = m10 x + m11 y => x <= Y / m10, y <= Y / m11.
        let mut new_n = max(
            min(xs_len - m_lens[0][0], ys_len - m_lens[1][0]),
            min(xs_len - m_lens[0][1], ys_len - m_lens[1][1]),
        ) + 1;
        // In the range of interest, mulmod_bnm1 should always beat mullo.
        let mod_n = limbs_mul_mod_base_pow_n_minus_1_next_size(new_n + 1);
        let mut big_scratch =
            vec![0; (mod_n << 1) + limbs_mul_mod_base_pow_n_minus_1_scratch_len(mod_n, mod_n, m.n)];
        split_into_chunks_mut!(big_scratch, mod_n, [scratch, scratch_lo], scratch_hi);
        assert!(n <= mod_n << 1);
        if n > mod_n {
            let (xs_lo, xs_hi) = xs.split_at_mut(mod_n);
            if limbs_slice_add_greater_in_place_left(xs_lo, xs_hi) {
                assert!(!limbs_slice_add_limb_in_place(xs, 1));
            }
            let (ys_lo, ys_hi) = ys.split_at_mut(mod_n);
            if limbs_slice_add_greater_in_place_left(ys_lo, ys_hi) {
                assert!(!limbs_slice_add_limb_in_place(ys, 1));
            }
            n = mod_n;
        }
        let xs = &mut xs[..n];
        let ys = &mut ys[..n];
        limbs_mul_mod_base_pow_n_minus_1(
            scratch,
            mod_n,
            xs,
            &m.get(1, 1)[..m_lens[1][1]],
            scratch_hi,
        );
        limbs_mul_mod_base_pow_n_minus_1(
            scratch_lo,
            mod_n,
            ys,
            &m.get(0, 1)[..m_lens[0][1]],
            scratch_hi,
        );
        if n + m_lens[1][1] < mod_n {
            slice_set_zero(&mut scratch[n + m_lens[1][1]..]);
        }
        if n + m_lens[0][1] < mod_n {
            slice_set_zero(&mut scratch_lo[n + m_lens[0][1]..]);
        }
        if limbs_sub_same_length_in_place_left(scratch, scratch_lo) {
            assert!(!limbs_sub_limb_in_place(scratch, 1));
        }
        let (scratch_0, scratch_1) = scratch.split_at(new_n);
        assert!(slice_test_zero(scratch_1));
        limbs_mul_mod_base_pow_n_minus_1(
            scratch_lo,
            mod_n,
            xs,
            &m.get(1, 0)[..m_lens[1][0]],
            scratch_hi,
        );
        xs[..new_n].copy_from_slice(scratch_0);
        limbs_mul_mod_base_pow_n_minus_1(
            scratch,
            mod_n,
            ys,
            &m.get(0, 0)[..m_lens[0][0]],
            scratch_hi,
        );
        if n + m_lens[1][0] < mod_n {
            slice_set_zero(&mut scratch_lo[n + m_lens[1][0]..]);
        }
        if n + m_lens[0][0] < mod_n {
            slice_set_zero(&mut scratch[n + m_lens[0][0]..]);
        }
        if limbs_sub_same_length_in_place_left(scratch, scratch_lo) {
            assert!(!limbs_sub_limb_in_place(scratch, 1));
        }
        let (scratch_0, scratch_1) = scratch.split_at(new_n);
        assert!(slice_test_zero(scratch_1));
        ys[..new_n].copy_from_slice(scratch_0);
        while xs[new_n - 1] | ys[new_n - 1] == 0 {
            new_n -= 1;
            assert_ne!(new_n, 0);
        }
        new_n
    }
}

/// This is equivalent to `mpn_hgcd_reduce` from `mpn/generic/hgcd_reduce.c`, GMP 6.2.1.
fn limbs_half_gcd_matrix_reduce(
    m: &mut HalfGcdMatrix,
    xs: &mut [Limb],
    ys: &mut [Limb],
    p: usize,
    scratch: &mut [Limb],
) -> usize {
    let n = xs.len();
    assert_eq!(ys.len(), n);
    let com_p = n - p;
    if n < HGCD_REDUCE_THRESHOLD {
        let new_n = limbs_half_gcd(&mut xs[p..], &mut ys[p..], m, scratch);
        if new_n == 0 {
            0
        } else {
            // Needs 2 * (p + m.n) <= 2 * (floor(n / 2) + ceil(n / 2) - 1) = 2 (n - 1)
            limbs_half_gcd_matrix_adjust(m, p + new_n, xs, ys, p, scratch)
        }
    } else {
        split_into_chunks_mut!(scratch, com_p, [scratch_0, scratch_1], scratch_2);
        scratch_0.copy_from_slice(&xs[p..]);
        scratch_1.copy_from_slice(&ys[p..]);
        if limbs_half_gcd_approx(scratch_0, scratch_1, m, scratch_2) {
            limbs_half_gcd_matrix_apply(m, xs, ys)
        } else {
            0
        }
    }
}

// Computes R -= X * Y. Result must be non-negative. Normalized down to size xs_len, and resulting
// size is returned.
//
// # Worst-case complexity
// $T(n) = O(n \log n \log\log n)$
//
// $M(n) = O(n \log n)$
//
// where $T$ is time, $M$ is additional memory, and $n$ is `xs.len()`.
//
// This is equivalent to `submul` from `mpn/generic/hgcd_reduce.c`, GMP 6.2.1.
fn limbs_gcd_sub_mul(out: &mut [Limb], xs: &[Limb], ys: &[Limb]) -> usize {
    let mut out_len = out.len();
    let xs_len = xs.len();
    let ys_len = ys.len();
    assert_ne!(ys_len, 0);
    assert!(xs_len >= ys_len);
    assert!(out_len >= xs_len);
    let sum_len = xs_len + ys_len;
    assert!(sum_len <= out_len + 1);
    let mut scratch = vec![0; sum_len];
    let mut mul_scratch = vec![0; limbs_mul_greater_to_out_scratch_len(xs_len, ys_len)];
    limbs_mul_greater_to_out(&mut scratch, xs, ys, &mut mul_scratch);
    assert!(sum_len <= out_len || scratch[out_len] == 0);
    let mut scratch_len = sum_len;
    if scratch_len > out_len {
        scratch_len -= 1;
    }
    assert!(!limbs_sub_greater_in_place_left(
        out,
        &scratch[..scratch_len]
    ));
    while out_len > xs_len && out[out_len - 1] == 0 {
        out_len -= 1;
    }
    out_len
}

#[cfg(feature = "test_build")]
#[derive(Clone, Debug, Default)]
pub struct HalfGcdMatrix1 {
    pub data: [[Limb; 2]; 2],
}

#[cfg(not(feature = "test_build"))]
#[derive(Default)]
pub(crate) struct HalfGcdMatrix1 {
    pub(crate) data: [[Limb; 2]; 2],
}

// Sets (r;b) = (a;b) M, with M = (u00, u01; u10, u11). Vector must have space for n + 1 limbs. Uses
// three buffers to avoid a copy
//
// # Worst-case complexity
// $T(n) = O(n)$
//
// $M(n) = O(1)$
//
// where $T$ is time, $M$ is additional memory, and $n$ is `m.n`.
//
// This is equivalent to `mpn_hgcd_mul_matrix1_vector` from `mpn/generic/hgcd2.c`, GMP 6.2.1.
pub_crate_test! {limbs_half_gcd_matrix_1_mul_vector(
    m: &HalfGcdMatrix1,
    out: &mut [Limb],
    xs: &[Limb],
    ys: &mut [Limb],
) -> usize {
    let n = xs.len();
    assert!(ys.len() > n);
    assert!(out.len() > n);
    let (out_lo, out_hi) = out.split_at_mut(n);
    let (ys_lo, ys_hi) = ys.split_at_mut(n);
    let mut x_high = limbs_mul_limb_to_out::<DoubleLimb, Limb>(out_lo, xs, m.data[0][0]);
    x_high.wrapping_add_assign(
        limbs_slice_add_mul_limb_same_length_in_place_left(out_lo, ys_lo, m.data[1][0])
    );
    let mut y_high = limbs_slice_mul_limb_in_place(ys_lo, m.data[1][1]);
    y_high.wrapping_add_assign(
        limbs_slice_add_mul_limb_same_length_in_place_left(ys_lo, xs, m.data[0][1])
    );
    out_hi[0] = x_high;
    ys_hi[0] = y_high;
    if x_high == 0 && y_high == 0 {
        n
    } else {
        n + 1
    }
}}

// Compute (r;y) <- (u11 x - u01 y; -u10 x + u00 y) xs
// ```
// r  = u11 * x
// r -= u01 * y
// y *= u00
// y -= u10 * x
// ```
//
// # Worst-case complexity
// $T(n) = O(n)$
//
// $M(n) = O(1)$
//
// where $T$ is time, $M$ is additional memory, and $n$ is `m.len()`.
//
// This is equivalent to `mpn_matrix22_mul1_inverse_vector` from
// `mpn/generic/matrix22_mul1_inverse_vector.c`, GMP 6.2.1.
pub(crate) fn limbs_half_gcd_matrix_1_mul_inverse_vector(
    m: &HalfGcdMatrix1,
    out: &mut [Limb],
    xs: &[Limb],
    ys: &mut [Limb],
) -> usize {
    let n = xs.len();
    assert_eq!(ys.len(), n);
    assert_eq!(out.len(), n);
    let h0 = limbs_mul_limb_to_out::<DoubleLimb, Limb>(out, xs, m.data[1][1]);
    let h1 = limbs_sub_mul_limb_same_length_in_place_left(out, ys, m.data[0][1]);
    assert_eq!(h0, h1);
    let h0 = limbs_slice_mul_limb_in_place(ys, m.data[0][0]);
    let h1 = limbs_sub_mul_limb_same_length_in_place_left(ys, xs, m.data[1][0]);
    assert_eq!(h0, h1);
    if out[n - 1] == 0 && ys[n - 1] == 0 {
        n - 1
    } else {
        n
    }
}

// # Worst-case complexity
// $T(n) = O(n \log n \log\log n)$
//
// $M(n) = O(n \log n)$
//
// where $T$ is time, $M$ is additional memory, and $n$ is `xs.len()`.
//
// This is equivalent to `mpn_gcd_subdiv_step` from `mpn/generic/gcd_subdiv_step.c`, GMP 6.2.1.
pub(crate) fn limbs_gcd_subdivide_step<'a, CTX: GcdSubdivideStepContext>(
    mut xs: &'a mut [Limb],
    mut ys: &'a mut [Limb],
    s: usize,
    context: &mut CTX,
    scratch: &mut [Limb],
) -> usize {
    let n = xs.len();
    assert_eq!(ys.len(), n);
    assert_ne!(n, 0);
    assert!(xs[n - 1] > 0 || ys[n - 1] > 0);
    let mut xs_len = n - slice_trailing_zeros(xs);
    let mut ys_len = n - slice_trailing_zeros(ys);
    let mut xs_init = &mut xs[..xs_len];
    let mut ys_init = &mut ys[..ys_len];
    let mut swapped = false;
    // Arrange so that x < y, subtract y -= x, and maintain normalization.
    match xs_len.cmp(&ys_len) {
        Equal => {
            match limbs_cmp_same_length(xs_init, ys_init) {
                Equal => {
                    // For gcdext, return the smallest of the two cofactors.
                    if s == 0 {
                        context.gcd_subdiv_step_hook(Some(xs_init), None, 0, -1);
                    }
                    return 0;
                }
                Greater => {
                    swap(&mut xs, &mut ys);
                    xs_init = &mut xs[..xs_len];
                    ys_init = &mut ys[..ys_len];
                    swapped.not_assign();
                }
                _ => {}
            }
        }
        Greater => {
            swap(&mut xs, &mut ys);
            swap(&mut xs_len, &mut ys_len);
            xs_init = &mut xs[..xs_len];
            ys_init = &mut ys[..ys_len];
            swapped.not_assign();
        }
        Less => {}
    }
    if xs_len <= s {
        if s == 0 {
            context.gcd_subdiv_step_hook(Some(ys_init), None, 0, i8::from(!swapped));
        }
        return 0;
    }
    assert!(!limbs_sub_greater_in_place_left(ys_init, xs_init));
    ys_len -= slice_trailing_zeros(ys_init);
    ys_init = &mut ys_init[..ys_len];
    assert_ne!(ys_len, 0);
    if ys_len <= s {
        // Undo subtraction
        if limbs_add_to_out_aliased(ys, ys_len, xs_init) {
            ys[xs_len] = 1;
        }
        return 0;
    }
    // Arrange so that x < y
    match xs_len.cmp(&ys_len) {
        Equal => {
            match limbs_cmp_same_length(xs_init, ys_init) {
                Equal => {
                    fail_on_untested_path("limbs_gcd_subdivide_step, c == Equal");
                    if s != 0 {
                        // Just record subtraction and return
                        context.gcd_subdiv_step_hook_with_1(i8::from(swapped));
                        context.gcd_subdiv_step_hook_with_1(i8::from(swapped));
                    } else {
                        // Found gcd.
                        context.gcd_subdiv_step_hook(Some(ys_init), None, 0, i8::from(swapped));
                        return 0;
                    }
                }
                Greater => {
                    context.gcd_subdiv_step_hook_with_1(i8::from(swapped));
                    swap(&mut xs, &mut ys);
                    xs_init = &mut xs[..xs_len];
                    ys_init = &mut ys[..ys_len];
                    swapped.not_assign();
                }
                Less => {
                    context.gcd_subdiv_step_hook_with_1(i8::from(swapped));
                }
            }
        }
        Greater => {
            context.gcd_subdiv_step_hook_with_1(i8::from(swapped));
            swap(&mut xs, &mut ys);
            swap(&mut xs_len, &mut ys_len);
            xs_init = &mut xs[..xs_len];
            ys_init = &mut ys[..ys_len];
            swapped.not_assign();
        }
        Less => {
            context.gcd_subdiv_step_hook_with_1(i8::from(swapped));
        }
    }
    if xs_len == 1 {
        if ys_init.len() == 1 {
            (scratch[0], ys_init[0]) = ys_init[0].div_mod(xs_init[0]);
        } else {
            ys_init[0] = limbs_div_limb_to_out_mod(scratch, ys_init, xs_init[0]);
        }
    } else {
        limbs_div_mod_qs_to_out_rs_to_ns(scratch, ys_init, xs_init);
    }
    let qn = ys_len - xs_len + 1;
    let ys_len = xs_len - slice_trailing_zeros(&ys_init[..xs_len]);
    if ys_len <= s {
        if s == 0 {
            context.gcd_subdiv_step_hook(Some(xs_init), Some(scratch), qn, i8::from(swapped));
            return 0;
        }
        // Quotient is one too large, so decrement it and add back X.
        if ys_len != 0 {
            if limbs_add_to_out_aliased(ys, ys_len, xs_init) {
                ys[xs_len] = 1;
                xs_len += 1;
            }
        } else {
            ys[..xs_len].copy_from_slice(xs_init);
        }
        assert!(!limbs_sub_limb_in_place(&mut scratch[..qn], 1));
    }
    context.gcd_subdiv_step_hook(None, Some(scratch), qn, i8::from(swapped));
    xs_len
}

impl GcdSubdivideStepContext for HalfGcdMatrix<'_> {
    // # Worst-case complexity
    // $T(n) = O(n)$
    //
    // $M(n) = O(1)$
    //
    // where $T$ is time, $M$ is additional memory, and $n$ is `q_len`.
    //
    // This is equivalent to `hgcd_hook` from `mpn/generic/hgcd_step.c`, GMP 6.2.1.
    fn gcd_subdiv_step_hook(
        &mut self,
        g: Option<&[Limb]>,
        q: Option<&mut [Limb]>,
        mut q_len: usize,
        d: i8,
    ) {
        assert!(g.is_none());
        let q = q.unwrap();
        q_len -= slice_trailing_zeros(&q[..q_len]);
        if q_len != 0 {
            let (q, scratch) = q.split_at_mut(q_len);
            limbs_half_gcd_matrix_update_q(self, q, u8::exact_from(d), scratch);
        }
    }
}

// # Worst-case complexity
// Constant time and additional memory.
//
// This is equivalent to `MPN_EXTRACT_NUMB` from `gmp-impl.h`, GMP 6.2.1.
pub(crate) const fn extract_number(count: u64, x1: Limb, x0: Limb) -> Limb {
    (x1 << count) | (x0 >> (Limb::WIDTH - count))
}

// # Worst-case complexity
// Constant time and additional memory.
//
// This is equivalent to `div2` from `mpn/generic/hgcd2.c`, GMP 6.2.1, where `HGCD2_DIV2_METHOD ==
// 1`.
pub_crate_test! {limbs_gcd_div(
    mut n1: Limb,
    mut n0: Limb,
    mut d1: Limb,
    mut d0: Limb
) -> (Limb, Limb, Limb) {
    let (mut q, r) = n1.div_mod(d1);
    if q > d1 {
        // Normalize
        let c = LeadingZeros::leading_zeros(d1);
        let width_comp = Limb::WIDTH - c;
        assert_ne!(c, 0);
        let n2 = n1 >> width_comp;
        n1 = (n1 << c) | (n0 >> width_comp);
        n0 <<= c;
        d1 = (d1 << c) | (d0 >> width_comp);
        d0 <<= c;
        (q, n1) = Limb::xx_div_mod_y_to_qr(n2, n1, d1);
        let (mut t1, mut t0) = Limb::x_mul_y_to_zz(q, d0);
        if t1 > n1 || t1 == n1 && t0 > n0 {
            assert_ne!(q, 0);
            q -= 1;
            (t1, t0) = Limb::xx_sub_yy_to_zz(t1, t0, d1, d0);
        }
        (n1, n0) = Limb::xx_sub_yy_to_zz(n1, n0, t1, t0);
        // Undo normalization
        (q, n1 >> c, (n0 >> c) | (n1 << width_comp))
    } else {
        n1 = r;
        let (mut t1, mut t0) = Limb::x_mul_y_to_zz(q, d0);
        if t1 >= n1 && (t1 > n1 || t0 > n0) {
            assert_ne!(q, 0);
            q -= 1;
            (t1, t0) = Limb::xx_sub_yy_to_zz(t1, t0, d1, d0);
        }
        let (r1, r0) = Limb::xx_sub_yy_to_zz(n1, n0, t1, t0);
        (q, r1, r0)
    }
}}

// Reduces a, b until |a - b| (almost) fits in one limb + 1 bit. Constructs matrix M. Returns 1 if
// we make progress, i.e. can perform at least one subtraction. Otherwise returns zero.
//
// This is equivalent to `mpn_hgcd2` from `mpn/generic/hgcd2.c`, GMP 6.2.1.
pub(crate) fn limbs_half_gcd_2(
    mut x_high: Limb,
    mut a_low: Limb,
    mut y_high: Limb,
    mut b_low: Limb,
    m: &mut HalfGcdMatrix1,
) -> bool {
    if x_high < 2 || y_high < 2 {
        return false;
    }
    let mut m01;
    let mut m10;
    if x_high > y_high || x_high == y_high && a_low > b_low {
        (x_high, a_low) = Limb::xx_sub_yy_to_zz(x_high, a_low, y_high, b_low);
        if x_high < 2 {
            return false;
        }
        m01 = 1;
        m10 = 0;
    } else {
        (y_high, b_low) = Limb::xx_sub_yy_to_zz(y_high, b_low, x_high, a_low);
        if y_high < 2 {
            return false;
        }
        m01 = 0;
        m10 = 1;
    }
    let mut m00 = 1;
    let mut m11 = 1;
    const HALF_WIDTH: u64 = Limb::WIDTH >> 1;
    const HALF_LIMIT_1: Limb = 1 << HALF_WIDTH;
    let mut subtract_a = x_high < y_high;
    let mut subtract_a1 = false;
    let mut done = false;
    loop {
        if subtract_a {
            subtract_a = false;
        } else {
            assert!(x_high >= y_high);
            if x_high == y_high {
                done = true;
                break;
            }
            if x_high < HALF_LIMIT_1 {
                x_high = (x_high << HALF_WIDTH) + (a_low >> HALF_WIDTH);
                y_high = (y_high << HALF_WIDTH) + (b_low >> HALF_WIDTH);
                break;
            }
            // Subtract a -= q * b, and multiply M from the right by (1 q ; 0 1), affecting the
            // second column of M.
            assert!(x_high > y_high);
            (x_high, a_low) = Limb::xx_sub_yy_to_zz(x_high, a_low, y_high, b_low);
            if x_high < 2 {
                done = true;
                break;
            }
            if x_high <= y_high {
                // Use q = 1.
                m01 += m00;
                m11 += m10;
            } else {
                let mut q;
                (q, x_high, a_low) = limbs_gcd_div(x_high, a_low, y_high, b_low);
                if x_high < 2 {
                    // A is too small, but q is correct.
                    m01 += q * m00;
                    m11 += q * m10;
                    done = true;
                    break;
                }
                q += 1;
                m01 += q * m00;
                m11 += q * m10;
            }
        }
        assert!(y_high >= x_high);
        if x_high == y_high {
            done = true;
            break;
        }
        if y_high < HALF_LIMIT_1 {
            x_high = (x_high << HALF_WIDTH) + (a_low >> HALF_WIDTH);
            y_high = (y_high << HALF_WIDTH) + (b_low >> HALF_WIDTH);
            subtract_a1 = true;
            break;
        }
        // Subtract b -= q * a, and multiply M from the right by (1 0 ; q 1), affecting the first
        // column of M.
        (y_high, b_low) = Limb::xx_sub_yy_to_zz(y_high, b_low, x_high, a_low);
        if y_high < 2 {
            done = true;
            break;
        }
        if x_high >= y_high {
            // Use q = 1.
            m00 += m01;
            m10 += m11;
        } else {
            let mut q;
            (q, y_high, b_low) = limbs_gcd_div(y_high, b_low, x_high, a_low);
            if y_high < 2 {
                // B is too small, but q is correct.
                m00 += q * m01;
                m10 += q * m11;
                done = true;
                break;
            }
            q += 1;
            m00 += q * m01;
            m10 += q * m11;
        }
    }
    // Since we discard the least significant half limb, we don't get a truly maximal M
    // corresponding to |a - b| < 2 ^ (W + 1)).
    if !done {
        const HALF_LIMIT_2: Limb = 1 << (HALF_WIDTH + 1);
        loop {
            if subtract_a1 {
                subtract_a1 = false;
            } else {
                assert!(x_high >= y_high);
                x_high -= y_high;
                if x_high < HALF_LIMIT_2 {
                    break;
                }
                if x_high <= y_high {
                    // Use q = 1.
                    m01 += m00;
                    m11 += m10;
                } else {
                    let mut q;
                    (q, x_high) = x_high.div_mod(y_high);
                    if x_high < HALF_LIMIT_2 {
                        // A is too small, but q is correct.
                        m01 += q * m00;
                        m11 += q * m10;
                        break;
                    }
                    q += 1;
                    m01 += q * m00;
                    m11 += q * m10;
                }
            }
            assert!(y_high >= x_high);
            y_high -= x_high;
            if y_high < HALF_LIMIT_2 {
                break;
            }
            if x_high >= y_high {
                // Use q = 1.
                m00 += m01;
                m10 += m11;
            } else {
                let mut q;
                (q, y_high) = y_high.div_mod(x_high);
                if y_high < HALF_LIMIT_2 {
                    // B is too small, but q is correct.
                    m00 += q * m01;
                    m10 += q * m11;
                    break;
                }
                q += 1;
                m00 += q * m01;
                m10 += q * m11;
            }
        }
    }
    m.data[0][0] = m00;
    m.data[0][1] = m01;
    m.data[1][0] = m10;
    m.data[1][1] = m11;
    true
}

/// This is equivalent to `mpn_hgcd_step` from `mpn/generic/hgcd_step.c`, GMP 6.2.1.
fn limbs_half_gcd_step(
    xs: &mut [Limb],
    ys: &mut [Limb],
    s: usize,
    a: &mut HalfGcdMatrix,
    scratch: &mut [Limb],
) -> usize {
    let n = xs.len();
    assert_eq!(ys.len(), n);
    assert!(n > s);
    let mask = xs[n - 1] | ys[n - 1];
    assert_ne!(mask, 0);
    let (x_high, a_low, y_high, b_low) = if n == s + 1 {
        if mask < 4 {
            return limbs_gcd_subdivide_step(xs, ys, s, a, scratch);
        }
        (xs[n - 1], xs[n - 2], ys[n - 1], ys[n - 2])
    } else if mask.get_highest_bit() {
        (xs[n - 1], xs[n - 2], ys[n - 1], ys[n - 2])
    } else {
        let shift = LeadingZeros::leading_zeros(mask);
        (
            extract_number(shift, xs[n - 1], xs[n - 2]),
            extract_number(shift, xs[n - 2], xs[n - 3]),
            extract_number(shift, ys[n - 1], ys[n - 2]),
            extract_number(shift, ys[n - 2], ys[n - 3]),
        )
    };
    // Try a limbs_half_gcd_2 step
    let mut b = HalfGcdMatrix1::default();
    if limbs_half_gcd_2(x_high, a_low, y_high, b_low, &mut b) {
        // Multiply A <- A * B.
        limbs_half_gcd_matrix_mul_matrix_1(a, &b, scratch);
        let scratch = &mut scratch[..n];
        // Can't swap inputs, so we need to copy.
        scratch.copy_from_slice(xs);
        // Multiply B^(-1) (x; y).
        limbs_half_gcd_matrix_1_mul_inverse_vector(&b, xs, scratch, ys)
    } else {
        limbs_gcd_subdivide_step(xs, ys, s, a, scratch)
    }
}

// # Worst-case complexity
// Constant time and additional memory.
//
// This is equivalent to `MPN_GCD_SUBDIV_STEP_ITCH` from `gmp-impl.h`, GMP 6.2.1.
pub(crate) const fn limbs_gcd_subdivide_step_scratch_len(n: usize) -> usize {
    n
}

// # Worst-case complexity
// Constant time and additional memory.
//
// This is equivalent to `CHOOSE_P` from `mpn/generic/gcd.c`, GMP 6.2.1.
const fn limbs_gcd_choose_p(n: usize) -> usize {
    (n << 1) / 3
}

// # Worst-case complexity
// Constant time and additional memory.
//
// This is equivalent to `MPN_HGCD_MATRIX_INIT_ITCH` from `gmp-impl.h`, GMP 6.2.1.
pub(crate) const fn limbs_half_gcd_matrix_init_scratch_len(n: usize) -> usize {
    (((n + 1) >> 1) + 1) << 2
}

// TODO tune
pub(crate) const HGCD_THRESHOLD: usize = 101;

// # Worst-case complexity
// Constant time and additional memory.
//
// This is equivalent to `mpn_hgcd_itch` from `mpn/generic/hgcd.c`, GMP 6.2.1.
pub(crate) fn limbs_half_gcd_scratch_len(n: usize) -> usize {
    if n < HGCD_THRESHOLD {
        n
    } else {
        // Get the recursion depth.
        let count = LeadingZeros::leading_zeros((n - 1) / (HGCD_THRESHOLD - 1));
        20 * ((n + 3) >> 2) + 22 * usize::exact_from(usize::WIDTH - count) + HGCD_THRESHOLD
    }
}

// TODO tune
const HGCD_REDUCE_THRESHOLD: usize = 1679;

// # Worst-case complexity
// Constant time and additional memory.
//
// This is equivalent to `mpn_hgcd_reduce_itch` from `mpn/generic/hgcd_reduce.c`, GMP 6.2.1.
pub_test! {limbs_half_gcd_reduce_scratch_len(n: usize, p: usize) -> usize {
    assert!(n >= p);
    let diff = n - p;
    if n < HGCD_REDUCE_THRESHOLD {
        let scratch_len = limbs_half_gcd_scratch_len(diff);
        // - For arbitrary p, the storage for adjust is
        // - 2 * (p + M.n) = 2 * (p + ceil((n - p) / 2) - 1 <= n + p - 1
        let sum = n + p - 1;
        if scratch_len < sum {
            sum
        } else {
            scratch_len
        }
    } else {
        (diff << 1) + limbs_half_gcd_scratch_len(diff)
    }
}}

// TODO tune
const HGCD_APPR_THRESHOLD: usize = 104;

/// Destroys inputs.
///
/// This is equivalent to `mpn_hgcd_appr` from `mpn/generic/hgcd_appr.c`, GMP 6.2.1.
fn limbs_half_gcd_approx(
    mut xs: &mut [Limb],
    mut ys: &mut [Limb],
    a: &mut HalfGcdMatrix,
    scratch: &mut [Limb],
) -> bool {
    let mut n = xs.len();
    assert_eq!(ys.len(), n);
    assert_ne!(n, 0);
    assert!(xs[n - 1] != 0 || ys[n - 1] != 0);
    if n <= 2 {
        fail_on_untested_path("limbs_half_gcd_approx, n <= 2");
        // Implies s = n
        return false;
    }
    assert!(((n + 1) >> 1) - 1 < a.s);
    // We aim for reduction of to W * s bits. But each time we discard some of the least significant
    // limbs, we must keep one additional bit to account for the truncation error. We maintain the W
    // * s - extra_bits as the current target size.
    let mut s = (n >> 1) + 1;
    let mut offset = 0;
    let mut success = false;
    if n < HGCD_APPR_THRESHOLD {
        let mut extra_bits = 0u64;
        let mut xs_chunk = &mut *xs;
        let mut ys_chunk = &mut *ys;
        while n > 2 {
            assert!(n > s);
            assert!(n <= s << 1);
            let new_n = limbs_half_gcd_step(xs_chunk, ys_chunk, s, a, scratch);
            if new_n == 0 {
                break;
            }
            n = new_n;
            xs_chunk = &mut xs_chunk[..n];
            ys_chunk = &mut ys_chunk[..n];
            success = true;
            // We can truncate and discard the lower p bits whenever n <= 2 * s - p. To account for
            // the truncation error, we must adjust s <- s + 1 - p, rather than just sbits <- sbits
            // - p. This adjustment makes the produced matrix slightly smaller than it could be.
            if limb_to_bit_count(n + 1) + (extra_bits << 1) <= limb_to_bit_count(s << 1) {
                let p =
                    bit_to_limb_count_floor(limb_to_bit_count((s << 1) - n) - (extra_bits << 1));
                if extra_bits == 0 {
                    // We cross a limb boundary and bump s. We can't do that if the result is that
                    // it makes makes min(X, Y) smaller than 2^W * s.
                    if s + 1 == n
                        || slice_test_zero(&xs_chunk[s + 1..])
                        || slice_test_zero(&ys_chunk[s + 1..])
                    {
                        continue;
                    }
                    extra_bits = Limb::WIDTH - 1;
                    s += 1;
                } else {
                    extra_bits -= 1;
                }
                // Drop the p least-significant limbs.
                offset += p;
                xs_chunk = &mut xs_chunk[p..];
                ys_chunk = &mut ys_chunk[p..];
                n -= p;
                s -= p;
            }
        }
        assert_ne!(s, 0);
        if extra_bits != 0 {
            // We can get here only of we have dropped at least one of the least-significant bits,
            // so we can decrement xs and ys. We can then shift left extra bits using
            // limbs_slice_shr_in_place.
            assert_ne!(offset, 0);
            let xs = &mut xs[offset - 1..];
            let ys = &mut ys[offset - 1..];
            let (xs_head, xs_tail) = xs[..=n].split_first_mut().unwrap();
            let (ys_head, ys_tail) = ys[..=n].split_first_mut().unwrap();
            let comp_bits = Limb::WIDTH - extra_bits;
            *xs_head = limbs_slice_shr_in_place(xs_tail, comp_bits);
            *ys_head = limbs_slice_shr_in_place(ys_tail, comp_bits);
            if xs[n] != 0 || ys[n] != 0 {
                n += 1;
            }
            assert!(success);
            while n > 2 {
                assert!(n > s);
                assert!(n <= s << 1);
                n = limbs_half_gcd_step(&mut xs[..n], &mut ys[..n], s, a, scratch);
                if n == 0 {
                    return true;
                }
            }
        }
        if n == 2 {
            fail_on_untested_path("limbs_half_gcd_approx, n == 2");
            assert_eq!(s, 1);
            let mut b = HalfGcdMatrix1::default();
            if limbs_half_gcd_2(xs[1], xs[0], ys[1], ys[0], &mut b) {
                // Multiply A <- A * B.
                limbs_half_gcd_matrix_mul_matrix_1(a, &b, scratch);
                success = true;
            }
        }
        success
    } else {
        let limit = ((3 * n) >> 2) + 1;
        let mut p = n >> 1;
        let new_n = limbs_half_gcd_matrix_reduce(a, xs, ys, p, scratch);
        if new_n != 0 {
            n = new_n;
            xs = &mut xs[..n];
            ys = &mut ys[..n];
            success = true;
        }
        while n > limit {
            // Needs n + 1 storage
            n = limbs_half_gcd_step(xs, ys, s, a, scratch);
            if n == 0 {
                return success;
            }
            xs = &mut xs[..n];
            ys = &mut ys[..n];
            success = true;
        }
        if n > s + 2 {
            p = (s << 1) - n + 1;
            let scratch_len = limbs_half_gcd_matrix_init_scratch_len(n - p);
            let (scratch_lo, scratch_hi) = scratch.split_at_mut(scratch_len);
            let mut b = HalfGcdMatrix::init(n - p, scratch_lo);
            if limbs_half_gcd_approx(&mut xs[p..], &mut ys[p..], &mut b, scratch_hi) {
                // We always have max(A) > 2 ^ (-(W + 1)) * max(B).
                assert!(a.n + 2 >= b.n);
                // Furthermore, assume A ends with a quotient (1, q; 0, 1); then either q or q + 1
                // is a correct quotient, and B will start with either (1, 0; 1, 1) or (2, 1; 1, 1).
                // This rules out the case that the size of A * B is much smaller than the expected
                // A.n + B.n.
                assert!(a.n + b.n < a.s);
                // We need a bound for of A.n + B.n. Let n be the original input size. Then ceil(n /
                // 2) - 1 >= size of product >= A.n + B.n - 2, and it follows that A.n + B.n <=
                // ceil(n / 2) + 1. Then 3 * (A.n + B.n) + 5 <= 3 * ceil(n / 2) + 8 is the amount of
                // needed scratch space.
                limbs_half_gcd_matrix_mul_matrix(a, &b, scratch_hi);
                return true;
            }
        }
        loop {
            assert!(n > s);
            assert!(n <= s << 1);
            let new_n = limbs_half_gcd_step(xs, ys, s, a, scratch);
            if new_n == 0 {
                return success;
            }
            n = new_n;
            xs = &mut xs[..n];
            ys = &mut ys[..n];
            success = true;
        }
    }
}

// Reduces x, y until |x - y| fits in n / 2 + 1 limbs. Constructs matrix A with elements of size at
// most (n + 1) / 2 - 1. Returns new size of a, b, or zero if no reduction is possible.
//
// # Worst-case complexity
// $T(n) = O(n (\log n)^2 \log\log n)$
//
// $M(n) = O(n \log n)$
//
// where $T$ is time, $M$ is additional memory, and $n$ is `xs.len()`.
//
// This is equivalent to `mpn_hgcd` from `mpn/generic/hgcd.c`, GMP 6.2.1.
pub(crate) fn limbs_half_gcd(
    xs: &mut [Limb],
    ys: &mut [Limb],
    a: &mut HalfGcdMatrix,
    scratch: &mut [Limb],
) -> usize {
    let mut n = xs.len();
    assert_eq!(ys.len(), n);
    let s = (n >> 1) + 1;
    let mut success = false;
    if n <= s {
        fail_on_untested_path("limbs_half_gcd, n <= s");
        return 0;
    }
    assert!(xs[n - 1] != 0 || ys[n - 1] != 0);
    assert!(((n + 1) >> 1) - 1 < a.s);
    if n >= HGCD_THRESHOLD {
        let limit = ((3 * n) >> 2) + 1;
        let p = n >> 1;
        let mut new_n = limbs_half_gcd_matrix_reduce(a, xs, ys, p, scratch);
        if new_n != 0 {
            n = new_n;
            success = true;
        }
        while n > limit {
            // Needs n + 1 storage
            let new_n = limbs_half_gcd_step(&mut xs[..n], &mut ys[..n], s, a, scratch);
            if new_n == 0 {
                return if success { n } else { 0 };
            }
            n = new_n;
            success = true;
        }
        if n > s + 2 {
            let p = (s << 1) - n + 1;
            let scratch_len = limbs_half_gcd_matrix_init_scratch_len(n - p);
            let (scratch_lo, scratch_hi) = scratch.split_at_mut(scratch_len);
            let mut b = HalfGcdMatrix::init(n - p, scratch_lo);
            new_n = limbs_half_gcd(&mut xs[p..n], &mut ys[p..n], &mut b, scratch_hi);
            if new_n != 0 {
                // We always have max(A) > 2 ^ (-(W + 1)) * max(B).
                assert!(a.n + 2 >= b.n);
                // Furthermore, assume A ends with a quotient (1, q; 0, 1); then either q or q + 1
                // is a correct quotient, and B will start with either (1, 0; 1, 1) or (2, 1; 1,
                // 1).This rules out the case that the size of A * B is much smaller than the
                // expected A.n + B.n.
                assert!(a.n + b.n < a.s);
                // Needs 2 * (p + A.n) <= 2 * (2 * s - limit + 1 + limit - s - 1) = 2 * s <= 2 *
                // (floor(n / 2) + 1) <= n + 2.
                n = limbs_half_gcd_matrix_adjust(&b, p + new_n, xs, ys, p, scratch_hi);
                // We need a bound for of A.n + B.n. Let n be the original input size. Then ceil(n /
                // 2) - 1 >= size of product >= A.n + B.n - 2 and it follows that A.n + B.n <=
                // ceil(n / 2) + 1. Then 3 * (A.n + B.n) + 5 <= 3 * ceil(n / 2) + 8 is the amount of
                // needed scratch space.
                limbs_half_gcd_matrix_mul_matrix(a, &b, scratch_hi);
                success = true;
            }
        }
    }
    loop {
        // Needs s + 3 < n
        let new_n = limbs_half_gcd_step(&mut xs[..n], &mut ys[..n], s, a, scratch);
        if new_n == 0 {
            return if success { n } else { 0 };
        }
        n = new_n;
        success = true;
    }
}

// TODO tune
pub(crate) const GCD_DC_THRESHOLD: usize = 330;

// X >= Y, X and Y not both even.
//
// # Worst-case complexity
// $T(n) = O(n (\log n)^2 \log\log n)$
//
// $M(n) = O(n \log n)$
//
// where $T$ is time, $M$ is additional memory, and $n$ is `xs.len()`.
//
// This is equivalent to `mpn_gcd` from `mpn/generic/gcd.c`, GMP 6.2.1.
pub_crate_test! {limbs_gcd_reduced(out: &mut [Limb], xs: &mut [Limb], ys: &mut [Limb]) -> usize {
    let mut xs = &mut *xs;
    let mut ys = &mut *ys;
    let xs_len = xs.len();
    let mut n = ys.len();
    assert!(xs_len >= n);
    assert_ne!(n, 0);
    assert_ne!(ys[n - 1], 0);
    let mut scratch_len = max(xs_len - n + 1, limbs_gcd_subdivide_step_scratch_len(n));
    if n >= GCD_DC_THRESHOLD {
        let p = limbs_gcd_choose_p(n);
        let matrix_scratch_len = limbs_half_gcd_matrix_init_scratch_len(n - p);
        let half_gcd_scratch_len = limbs_half_gcd_scratch_len(n - p);
        let update_scratch_len = p + n - 1;
        scratch_len = max(
            scratch_len,
            matrix_scratch_len + max(half_gcd_scratch_len, update_scratch_len),
        );
    }
    let mut scratch = vec![0; scratch_len];
    let mut scratch = &mut scratch[..];
    if xs_len > n {
        limbs_div_mod_qs_to_out_rs_to_ns(scratch, xs, ys);
        if slice_test_zero(&xs[..n]) {
            out[..n].copy_from_slice(ys);
            return n;
        }
    }
    while n >= GCD_DC_THRESHOLD {
        let xs = &mut xs[..n];
        let ys = &mut ys[..n];
        let p = limbs_gcd_choose_p(n);
        let comp_p = n - p;
        let matrix_scratch_len = limbs_half_gcd_matrix_init_scratch_len(comp_p);
        let (scratch_lo, scratch_hi) = scratch.split_at_mut(matrix_scratch_len);
        let mut m = HalfGcdMatrix::init(comp_p, scratch_lo);
        let new_n = limbs_half_gcd(&mut xs[p..], &mut ys[p..], &mut m, scratch_hi);
        if new_n != 0 {
            assert!(m.n <= (comp_p - 1) >> 1);
            assert!(m.n + p <= (p + n - 1) >> 1);
            // Temporary storage 2 * (p + M.n) <= p + n - 1.
            n = limbs_half_gcd_matrix_adjust(&m, p + new_n, xs, ys, p, scratch_hi);
        } else {
            // Temporary storage n.
            let out_len = n;
            n = limbs_gcd_subdivide_step(xs, ys, 0, &mut GcdContext(out), scratch);
            if n == 0 {
                return out_len;
            }
        }
    }
    while n > 2 {
        let mask = xs[n - 1] | ys[n - 1];
        assert_ne!(mask, 0);
        let (x_hi, x_lo, y_hi, y_lo) = if mask.get_highest_bit() {
            (xs[n - 1], xs[n - 2], ys[n - 1], ys[n - 2])
        } else {
            let shift = LeadingZeros::leading_zeros(mask);
            (
                extract_number(shift, xs[n - 1], xs[n - 2]),
                extract_number(shift, xs[n - 2], xs[n - 3]),
                extract_number(shift, ys[n - 1], ys[n - 2]),
                extract_number(shift, ys[n - 2], ys[n - 3]),
            )
        };
        let mut m = HalfGcdMatrix1::default();
        // Try a limbs_half_gcd_2 step.
        if limbs_half_gcd_2(x_hi, x_lo, y_hi, y_lo, &mut m) {
            n = limbs_half_gcd_matrix_1_mul_inverse_vector(
                &m,
                &mut scratch[..n],
                &xs[..n],
                &mut ys[..n],
            );
            swap(&mut xs, &mut scratch);
        } else {
            // limbs_half_gcd_2 has failed. Then either one of x or y is very small, or the
            // difference is very small. Perform one subtraction followed by one division.
            let out_len = n;
            n = limbs_gcd_subdivide_step(
                &mut xs[..n],
                &mut ys[..n],
                0,
                &mut GcdContext(out),
                scratch,
            );
            if n == 0 {
                return out_len;
            }
        }
    }
    assert!(xs[n - 1] != 0 || ys[n - 1] != 0);
    // Due to the calling convention for limbs_gcd_reduced, at most one can be even.
    if xs[0].even() {
        swap(&mut xs, &mut ys);
    }
    assert!(xs[0].odd());
    let x_0 = xs[0];
    let mut y_0 = ys[0];
    if n == 1 {
        out[0] = x_0.gcd(y_0 >> y_0.trailing_zeros());
        return 1;
    }
    let mut y_1 = ys[1];
    if y_0 == 0 {
        y_0 = y_1;
        y_1 = 0;
    }
    if y_0.even() {
        let zeros = TrailingZeros::trailing_zeros(y_0);
        y_0 = (y_1 << (Limb::WIDTH - zeros)) | (y_0 >> zeros);
        y_1 >>= zeros;
    }
    let x_1 = xs[1];
    // TODO try mpn_gcd_22
    (out[1], out[0]) = DoubleLimb::join_halves(x_1, x_0)
        .gcd(DoubleLimb::join_halves(y_1, y_0))
        .split_in_half();
    if out[1] == 0 {
        1
    } else {
        2
    }
}}