summaryrefslogtreecommitdiff
path: root/src/div.c
blob: 883087b3b3edc92fa0efbb9c31060609963dc9f1 (plain)
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
/* mpfr_div -- divide two floating-point numbers

Copyright 1999, 2001-2021 Free Software Foundation, Inc.
Contributed by the AriC and Caramba projects, INRIA.

This file is part of the GNU MPFR Library.

The GNU MPFR Library is free software; you can redistribute it and/or modify
it under the terms of the GNU Lesser General Public License as published by
the Free Software Foundation; either version 3 of the License, or (at your
option) any later version.

The GNU MPFR Library is distributed in the hope that it will be useful, but
WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU Lesser General Public
License for more details.

You should have received a copy of the GNU Lesser General Public License
along with the GNU MPFR Library; see the file COPYING.LESSER.  If not, see
https://www.gnu.org/licenses/ or write to the Free Software Foundation, Inc.,
51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA. */

/* References:
   [1] Short Division of Long Integers, David Harvey and Paul Zimmermann,
       Proceedings of the 20th Symposium on Computer Arithmetic (ARITH-20),
       July 25-27, 2011, pages 7-14.
   [2] Improved Division by Invariant Integers, Niels Möller and Torbjörn Granlund,
       IEEE Transactions on Computers, volume 60, number 2, pages 165-175, 2011.
*/

#define MPFR_NEED_LONGLONG_H
#include "mpfr-impl.h"

#if !defined(MPFR_GENERIC_ABI)

#if GMP_NUMB_BITS == 64

#include "invert_limb.h"

/* Given u = u1*B+u0 < v = v1*B+v0 with v normalized (high bit of v1 set),
   put in q = Q1*B+Q0 an approximation of floor(u*B^2/v), with:
   B = 2^GMP_NUMB_BITS and q <= floor(u*B^2/v) <= q + 21.
   Note: this function requires __gmpfr_invert_limb_approx (from invert_limb.h)
   which is only provided so far for 64-bit limb.
   Note: __gmpfr_invert_limb_approx can be replaced by __gmpfr_invert_limb,
   in that case the bound 21 reduces to 16. */
static void
mpfr_div2_approx (mpfr_limb_ptr Q1, mpfr_limb_ptr Q0,
                  mp_limb_t u1, mp_limb_t u0,
                  mp_limb_t v1, mp_limb_t v0)
{
  mp_limb_t inv, q1, q0, r1, r0, cy, xx, yy;

  /* first compute an approximation of q1, using a lower approximation of
     B^2/(v1+1) - B */
  if (MPFR_UNLIKELY(v1 == MPFR_LIMB_MAX))
    inv = MPFR_LIMB_ZERO;
  else
    __gmpfr_invert_limb_approx (inv, v1 + 1);
  /* now inv <= B^2/(v1+1) - B */
  umul_ppmm (q1, q0, u1, inv);
  q1 += u1;
  /* now q1 <= u1*B/(v1+1) < (u1*B+u0)*B/(v1*B+v0) */

  /* compute q1*(v1*B+v0) into r1:r0:yy and subtract from u1:u0:0 */
  umul_ppmm (r1, r0, q1, v1);
  umul_ppmm (xx, yy, q1, v0);

  ADD_LIMB (r0, xx, cy);
  r1 += cy;

  /* we ignore yy below, but first increment r0, to ensure we get a lower
     approximation of the remainder */
  r0 += yy != 0;
  r1 += r0 == 0 && yy != 0;
  r0 = u0 - r0;
  r1 = u1 - r1 - (r0 > u0);

  /* r1:r0 should be nonnegative */
  MPFR_ASSERTD((r1 & MPFR_LIMB_HIGHBIT) == 0);

  /* the second quotient limb is approximated by (r1*B^2+r0*B) / v1,
     and since (B+inv)/B approximates B/v1, this is in turn approximated
     by (r1*B+r0)*(B+inv)/B = r1*B*r1*inv+r0+(r0*inv/B) */

  q0 = r0;
  q1 += r1;
  /* add floor(r0*inv/B) to q0 */
  umul_ppmm (xx, yy, r0, inv);
  ADD_LIMB (q0, xx, cy);
  q1 += cy;
  MPFR_ASSERTD (r1 <= 4);
  /* TODO: use value coverage on r1 to check that the 5 cases are tested. */
  while (r1) /* the number of loops is at most 4 */
    {
      /* add inv to q0 */
      ADD_LIMB (q0, inv, cy);
      q1 += cy;
      r1 --;
    }

  *Q1 = q1;
  *Q0 = q0;
}

#endif /* GMP_NUMB_BITS == 64 */

/* Special code for PREC(q) = PREC(u) = PREC(v) = p < GMP_NUMB_BITS */
static int
mpfr_div_1 (mpfr_ptr q, mpfr_srcptr u, mpfr_srcptr v, mpfr_rnd_t rnd_mode)
{
  mpfr_prec_t p = MPFR_GET_PREC(q);
  mpfr_limb_ptr qp = MPFR_MANT(q);
  mpfr_exp_t qx = MPFR_GET_EXP(u) - MPFR_GET_EXP(v);
  mpfr_prec_t sh = GMP_NUMB_BITS - p;
  mp_limb_t u0 = MPFR_MANT(u)[0];
  mp_limb_t v0 = MPFR_MANT(v)[0];
  mp_limb_t q0, rb, sb, mask = MPFR_LIMB_MASK(sh);
  int extra;

  if ((extra = (u0 >= v0)))
    u0 -= v0;

#if GMP_NUMB_BITS == 64 /* __gmpfr_invert_limb_approx only exists for 64-bit */
  /* First try with an approximate quotient.
     FIXME: for p<=62 we have sh-1<2 and will never be able to round correctly.
     Even for p=61 we have sh-1=2 and we can round correctly only when the two
     last bist of q0 are 01, which happens with probability 25% only. */
  {
    mp_limb_t inv;
    __gmpfr_invert_limb_approx (inv, v0);
    umul_ppmm (rb, sb, u0, inv);
  }
  rb += u0;
  q0 = rb >> extra;
  /* rb does not exceed the true quotient floor(u0*2^GMP_NUMB_BITS/v0),
     with error at most 2, which means the rational quotient q satisfies
     rb <= q < rb + 3. We can round correctly except when the last sh-1 bits
     of q0 are 000..000 or 111..111 or 111..110. */
  if (MPFR_LIKELY(((q0 + 2) & (mask >> 1)) > 2))
    {
      rb = q0 & (MPFR_LIMB_ONE << (sh - 1));
      sb = 1; /* result cannot be exact in this case */
    }
  else /* the true quotient is rb, rb+1 or rb+2 */
    {
      mp_limb_t h, l;
      q0 = rb;
      umul_ppmm (h, l, q0, v0);
      MPFR_ASSERTD(h < u0 || (h == u0 && l == MPFR_LIMB_ZERO));
      /* subtract {h,l} from {u0,0} */
      sub_ddmmss (h, l, u0, 0, h, l);
      /* the remainder {h, l} should be < v0 */
      /* This while loop is executed at most two times, but does not seem
         slower than two consecutive identical if-statements. */
      while (h || l >= v0)
        {
          q0 ++;
          h -= (l < v0);
          l -= v0;
        }
      MPFR_ASSERTD(h == 0 && l < v0);
      sb = l | (q0 & extra);
      q0 >>= extra;
      rb = q0 & (MPFR_LIMB_ONE << (sh - 1));
      sb |= q0 & (mask >> 1);
    }
#else
  udiv_qrnnd (q0, sb, u0, 0, v0);
  sb |= q0 & extra;
  q0 >>= extra;
  rb = q0 & (MPFR_LIMB_ONE << (sh - 1));
  sb |= q0 & (mask >> 1);
#endif

  qp[0] = (MPFR_LIMB_HIGHBIT | q0) & ~mask;
  qx += extra;
  MPFR_SIGN(q) = MPFR_MULT_SIGN (MPFR_SIGN (u), MPFR_SIGN (v));

  /* rounding */
  if (MPFR_UNLIKELY(qx > __gmpfr_emax))
    return mpfr_overflow (q, rnd_mode, MPFR_SIGN(q));

  /* Warning: underflow should be checked *after* rounding, thus when rounding
     away and when q > 0.111...111*2^(emin-1), or when rounding to nearest and
     q >= 0.111...111[1]*2^(emin-1), there is no underflow. */
  if (MPFR_UNLIKELY(qx < __gmpfr_emin))
    {
      /* Note: the case 0.111...111*2^(emin-1) < q < 2^(emin-1) is not possible
         here since (up to exponent) this would imply 1 - 2^(-p) < u/v < 1,
         thus v - 2^(-p)*v < u < v, and since we can assume 1/2 <= v < 1, it
         would imply v - 2^(-p) = v - ulp(v) < u < v, which has no solution. */

      /* For RNDN, mpfr_underflow always rounds away, thus for |q|<=2^(emin-2)
         we have to change to RNDZ. This corresponds to:
         (a) either qx < emin - 1
         (b) or qx = emin - 1 and qp[0] = 1000....000 and rb = sb = 0.
         Note: in case (b), it suffices to check whether sb = 0, since rb = 1
         and sb = 0 is not possible (the exact quotient would have p+1 bits,
         thus u would need at least p+1 bits). */
      if (rnd_mode == MPFR_RNDN &&
          (qx < __gmpfr_emin - 1 || (qp[0] == MPFR_LIMB_HIGHBIT && sb == 0)))
        rnd_mode = MPFR_RNDZ;
      return mpfr_underflow (q, rnd_mode, MPFR_SIGN(q));
    }

  MPFR_EXP (q) = qx; /* Don't use MPFR_SET_EXP since qx might be < __gmpfr_emin
                        in the cases "goto rounding" above. */
  if ((rb == 0 && sb == 0) || rnd_mode == MPFR_RNDF)
    {
      MPFR_ASSERTD(qx >= __gmpfr_emin);
      MPFR_RET (0);
    }
  else if (rnd_mode == MPFR_RNDN)
    {
      /* It is not possible to have rb <> 0 and sb = 0 here, since it would
         mean a n-bit by n-bit division gives an exact (n+1)-bit number.
         And since the case rb = sb = 0 was already dealt with, we cannot
         have sb = 0. Thus we cannot be in the middle of two numbers. */
      MPFR_ASSERTD(sb != 0);
      if (rb == 0)
        goto truncate;
      else
        goto add_one_ulp;
    }
  else if (MPFR_IS_LIKE_RNDZ(rnd_mode, MPFR_IS_NEG(q)))
    {
    truncate:
      MPFR_ASSERTD(qx >= __gmpfr_emin);
      MPFR_RET(-MPFR_SIGN(q));
    }
  else /* round away from zero */
    {
    add_one_ulp:
      qp[0] += MPFR_LIMB_ONE << sh;
      MPFR_ASSERTD(qp[0] != 0);
      /* It is not possible to have an overflow in the addition above.
         Proof: if p is the precision of the inputs, it would mean we have two
         integers n and d with 2^(p-1) <= n, d < 2^p, such that the binary
         expansion of n/d starts with p '1', and has at least one '1' later.
         We distinguish two cases:
         (1) if n/d < 1, it would mean 1-2^(-p) < n/d < 1
         (2) if n/d >= 1, it would mean 2-2^(1-p) < n/d < 1
         In case (1), multiplying by d we get 1-d/2^p < n < d,
         which has no integer solution since d/2^p < 1.
         In case (2), multiplying by d we get 2d-2d/2^p < n < 2d:
         (2a) if d=2^(p-1), we get 2^p-1 < n < 2^p which has no solution;
              if d>=2^(p-1)+1, then 2d-2d/2^p >= 2^p+2-2 = 2^p, thus there is
              solution n < 2^p either. */
      MPFR_RET(MPFR_SIGN(q));
    }
}

/* Special code for PREC(q) = GMP_NUMB_BITS,
   with PREC(u), PREC(v) <= GMP_NUMB_BITS. */
static int
mpfr_div_1n (mpfr_ptr q, mpfr_srcptr u, mpfr_srcptr v, mpfr_rnd_t rnd_mode)
{
  mpfr_limb_ptr qp = MPFR_MANT(q);
  mpfr_exp_t qx = MPFR_GET_EXP(u) - MPFR_GET_EXP(v);
  mp_limb_t u0 = MPFR_MANT(u)[0];
  mp_limb_t v0 = MPFR_MANT(v)[0];
  mp_limb_t q0, rb, sb, l;
  int extra;

  MPFR_ASSERTD(MPFR_PREC(q) == GMP_NUMB_BITS);
  MPFR_ASSERTD(MPFR_PREC(u) <= GMP_NUMB_BITS);
  MPFR_ASSERTD(MPFR_PREC(v) <= GMP_NUMB_BITS);

  if ((extra = (u0 >= v0)))
    u0 -= v0;

#if GMP_NUMB_BITS == 64 /* __gmpfr_invert_limb_approx only exists for 64-bit */
  {
    mp_limb_t inv, h;

    /* First compute an approximate quotient. */
    __gmpfr_invert_limb_approx (inv, v0);
    umul_ppmm (rb, sb, u0, inv);
    q0 = u0 + rb;
    /* rb does not exceed the true quotient floor(u0*2^GMP_NUMB_BITS/v0),
       with error at most 2, which means the rational quotient q satisfies
       rb <= q < rb + 3, thus the true quotient is rb, rb+1 or rb+2 */
    umul_ppmm (h, l, q0, v0);
    MPFR_ASSERTD(h < u0 || (h == u0 && l == MPFR_LIMB_ZERO));
    /* subtract {h,l} from {u0,0} */
    sub_ddmmss (h, l, u0, 0, h, l);
    /* the remainder {h, l} should be < v0 */
    /* This while loop is executed at most two times, but does not seem
       slower than two consecutive identical if-statements. */
    while (h || l >= v0)
      {
        q0 ++;
        h -= (l < v0);
        l -= v0;
      }
    MPFR_ASSERTD(h == 0 && l < v0);
  }
#else
  udiv_qrnnd (q0, l, u0, 0, v0);
#endif

  /* now (u0 - extra*v0) * 2^GMP_NUMB_BITS = q0*v0 + l with 0 <= l < v0 */

  /* If extra=0, the quotient is q0, the round bit is 1 if l >= v0/2,
     and sb are the remaining bits from l.
     If extra=1, the quotient is MPFR_LIMB_HIGHBIT + (q0 >> 1), the round bit
     is the least significant bit of q0, and sb is l. */

  if (extra == 0)
    {
      qp[0] = q0;
      /* If "l + l < l", then there is a carry in l + l, thus 2*l > v0.
         Otherwise if there is no carry, we check whether 2*l >= v0. */
      rb = (l + l < l) || (l + l >= v0);
      sb = (rb) ? l + l - v0 : l;
    }
  else
    {
      qp[0] = MPFR_LIMB_HIGHBIT | (q0 >> 1);
      rb = q0 & MPFR_LIMB_ONE;
      sb = l;
      qx ++;
    }

  MPFR_SIGN(q) = MPFR_MULT_SIGN (MPFR_SIGN (u), MPFR_SIGN (v));

  /* rounding */
  if (MPFR_UNLIKELY(qx > __gmpfr_emax))
    return mpfr_overflow (q, rnd_mode, MPFR_SIGN(q));

  /* Warning: underflow should be checked *after* rounding, thus when rounding
     away and when q > 0.111...111*2^(emin-1), or when rounding to nearest and
     q >= 0.111...111[1]*2^(emin-1), there is no underflow. */
  if (MPFR_UNLIKELY(qx < __gmpfr_emin))
    {
      /* Note: the case 0.111...111*2^(emin-1) < q < 2^(emin-1) is not possible
         here since (up to exponent) this would imply 1 - 2^(-p) < u/v < 1,
         thus v - 2^(-p)*v < u < v, and since we can assume 1/2 <= v < 1, it
         would imply v - 2^(-p) = v - ulp(v) < u < v, which has no solution. */

      /* For RNDN, mpfr_underflow always rounds away, thus for |q|<=2^(emin-2)
         we have to change to RNDZ. This corresponds to:
         (a) either qx < emin - 1
         (b) or qx = emin - 1 and qp[0] = 1000....000 and rb = sb = 0.
         Note: in case (b), it suffices to check whether sb = 0, since rb = 1
         and sb = 0 is not possible (the exact quotient would have p+1 bits,
         thus u would need at least p+1 bits). */
      if (rnd_mode == MPFR_RNDN &&
          (qx < __gmpfr_emin - 1 || (qp[0] == MPFR_LIMB_HIGHBIT && sb == 0)))
        rnd_mode = MPFR_RNDZ;
      return mpfr_underflow (q, rnd_mode, MPFR_SIGN(q));
    }

  MPFR_EXP (q) = qx; /* Don't use MPFR_SET_EXP since qx might be < __gmpfr_emin
                        in the cases "goto rounding" above. */
  if ((rb == 0 && sb == 0) || rnd_mode == MPFR_RNDF)
    {
      MPFR_ASSERTD(qx >= __gmpfr_emin);
      MPFR_RET (0);
    }
  else if (rnd_mode == MPFR_RNDN)
    {
      /* It is not possible to have rb <> 0 and sb = 0 here, since it would
         mean a n-bit by n-bit division gives an exact (n+1)-bit number.
         And since the case rb = sb = 0 was already dealt with, we cannot
         have sb = 0. Thus we cannot be in the middle of two numbers. */
      MPFR_ASSERTD(sb != 0);
      if (rb == 0)
        goto truncate;
      else
        goto add_one_ulp;
    }
  else if (MPFR_IS_LIKE_RNDZ(rnd_mode, MPFR_IS_NEG(q)))
    {
    truncate:
      MPFR_ASSERTD(qx >= __gmpfr_emin);
      MPFR_RET(-MPFR_SIGN(q));
    }
  else /* round away from zero */
    {
    add_one_ulp:
      qp[0] += MPFR_LIMB_ONE;
      /* there can be no overflow in the addition above,
         see the analysis of mpfr_div_1 */
      MPFR_ASSERTD(qp[0] != 0);
      MPFR_RET(MPFR_SIGN(q));
    }
}

/* Special code for GMP_NUMB_BITS < PREC(q) < 2*GMP_NUMB_BITS and
   PREC(u) = PREC(v) = PREC(q) */
static int
mpfr_div_2 (mpfr_ptr q, mpfr_srcptr u, mpfr_srcptr v, mpfr_rnd_t rnd_mode)
{
  mpfr_prec_t p = MPFR_GET_PREC(q);
  mpfr_limb_ptr qp = MPFR_MANT(q);
  mpfr_exp_t qx = MPFR_GET_EXP(u) - MPFR_GET_EXP(v);
  mpfr_prec_t sh = 2*GMP_NUMB_BITS - p;
  mp_limb_t h, rb, sb, mask = MPFR_LIMB_MASK(sh);
  mp_limb_t v1 = MPFR_MANT(v)[1], v0 = MPFR_MANT(v)[0];
  mp_limb_t q1, q0, r3, r2, r1, r0, l, t;
  int extra;

  r3 = MPFR_MANT(u)[1];
  r2 = MPFR_MANT(u)[0];
  extra = r3 > v1 || (r3 == v1 && r2 >= v0);
  if (extra)
    sub_ddmmss (r3, r2, r3, r2, v1, v0);

  MPFR_ASSERTD(r3 < v1 || (r3 == v1 && r2 < v0));

#if GMP_NUMB_BITS == 64
  mpfr_div2_approx (&q1, &q0, r3, r2, v1, v0);
  /* we know q1*B+q0 is smaller or equal to the exact quotient, with
     difference at most 21 */
  if (MPFR_LIKELY(((q0 + 21) & (mask >> 1)) > 21))
    sb = 1; /* result is not exact when we can round with an approximation */
  else
    {
      /* we know q1:q0 is a good-enough approximation, use it! */
      mp_limb_t s0, s1, s2, h, l;

      /* Since we know the difference should be at most 21*(v1:v0) after the
         subtraction below, thus at most 21*2^128, it suffices to compute the
         lower 3 limbs of (q1:q0) * (v1:v0). */
      umul_ppmm (s1, s0, q0, v0);
      umul_ppmm (s2, l, q0, v1);
      s1 += l;
      s2 += (s1 < l);
      umul_ppmm (h, l, q1, v0);
      s1 += l;
      s2 += h + (s1 < l);
      s2 += q1 * v1;
      /* Subtract s2:s1:s0 from r2:0:0, with result in s2:s1:s0. */
      s2 = r2 - s2;
      /* now negate s1:s0 */
      s0 = -s0;
      s1 = -s1 - (s0 != 0);
      /* there is a borrow in s2 when s0 and s1 are not both zero */
      s2 -= (s1 != 0 || s0 != 0);
      while (s2 > 0 || (s1 > v1) || (s1 == v1 && s0 >= v0))
        {
          /* add 1 to q1:q0 */
          q0 ++;
          q1 += (q0 == 0);
          /* subtract v1:v0 to s2:s1:s0 */
          s2 -= (s1 < v1) || (s1 == v1 && s0 < v0);
          sub_ddmmss (s1, s0, s1, s0, v1, v0);
        }
      sb = s1 | s0;
    }
  goto round_div2;
#endif

  /* now r3:r2 < v1:v0 */
  if (MPFR_UNLIKELY(r3 == v1)) /* can occur in some rare cases */
    {
      /* This can only occur in case extra=0, since otherwise we would have
         u_old >= u_new + v >= B^2/2 + B^2/2 = B^2. In this case we have
         r3 = u1 and r2 = u0, thus the remainder u*B-q1*v is
         v1*B^2+u0*B-(B-1)*(v1*B+v0) = (u0-v0+v1)*B+v0.
         Warning: in this case q1 = B-1 can be too large, for example with
         u = B^2/2 and v = B^2/2 + B - 1, then u*B-(B-1)*u = -1/2*B^2+2*B-1. */
      MPFR_ASSERTD(extra == 0);
      q1 = MPFR_LIMB_MAX;
      r1 = v0;
      t = v0 - r2; /* t > 0 since r3:r2 < v1:v0 */
      r2 = v1 - t;
      if (t > v1) /* q1 = B-1 is too large, we need q1 = B-2, which is ok
                        since u*B - q1*v >= v1*B^2-(B-2)*(v1*B+B-1) =
                        -B^2 + 2*B*v1 + 3*B - 2 >= 0 since v1>=B/2 and B>=2 */
        {
          q1 --;
          /* add v to r2:r1 */
          r1 += v0;
          r2 += v1 + (r1 < v0);
        }
    }
  else
    {
      /* divide r3:r2 by v1: requires r3 < v1 */
      udiv_qrnnd (q1, r2, r3, r2, v1);
      /* u-extra*v = q1 * v1 + r2 */

      /* now subtract q1*v0 to r2:0 */
      umul_ppmm (h, l, q1, v0);
      t = r2; /* save old value of r2 */
      r1 = -l;
      r2 -= h + (l != 0);
      /* Note: h + (l != 0) < 2^GMP_NUMB_BITS. */

      /* we have r2:r1 = oldr2:0 - q1*v0 mod 2^(2*GMP_NUMB_BITS)
         thus (u-extra*v)*B = q1 * v + r2:r1 mod 2^(2*GMP_NUMB_BITS) */

      /* this while loop should be run at most twice */
      while (r2 > t) /* borrow when subtracting h + (l != 0), q1 too large */
        {
          q1 --;
          /* add v1:v0 to r2:r1 */
          t = r2;
          r1 += v0;
          r2 += v1 + (r1 < v0);
          /* note: since 2^(GMP_NUMB_BITS-1) <= v1 + (r1 < v0)
             <= 2^GMP_NUMB_BITS, it suffices to check if r2 <= t to see
             if there was a carry or not. */
        }
    }

  /* now (u-extra*v)*B = q1 * v + r2:r1 with 0 <= r2:r1 < v */

  MPFR_ASSERTD(r2 < v1 || (r2 == v1 && r1 < v0));

  if (MPFR_UNLIKELY(r2 == v1))
    {
      q0 = MPFR_LIMB_MAX;
      /* r2:r1:0 - q0*(v1:v0) = v1:r1:0 - (B-1)*(v1:v0)
         = r1:0 - v0:0 + v1:v0 */
      r0 = v0;
      t = v0 - r1; /* t > 0 since r2:r1 < v1:v0 */
      r1 = v1 - t;
      if (t > v1)
        {
          q0 --;
          /* add v to r1:r0 */
          r0 += v0;
          r1 += v1 + (r0 < v0);
        }
    }
  else
    {
      /* divide r2:r1 by v1: requires r2 < v1 */
      udiv_qrnnd (q0, r1, r2, r1, v1);

      /* r2:r1 = q0*v1 + r1 */

      /* subtract q0*v0 to r1:0 */
      umul_ppmm (h, l, q0, v0);
      t = r1;
      r0 = -l;
      r1 -= h + (l != 0);

      /* this while loop should be run at most twice */
      while (r1 > t) /* borrow when subtracting h + (l != 0),
                        q0 was too large */
        {
          q0 --;
          /* add v1:v0 to r1:r0 */
          t = r1;
          r0 += v0;
          r1 += v1 + (r0 < v0);
          /* note: since 2^(GMP_NUMB_BITS-1) <= v1 + (r0 < v0)
             <= 2^GMP_NUMB_BITS, it suffices to check if r1 <= t to see
             if there was a carry or not. */
        }
    }

  MPFR_ASSERTD(r1 < v1 || (r1 == v1 && r0 < v0));

  /* now (u-extra*v)*B^2 = (q1:q0) * v + r1:r0 */

  sb = r1 | r0;

  /* here, q1:q0 should be an approximation of the quotient (or the exact
     quotient), and sb the sticky bit */

#if GMP_NUMB_BITS == 64
 round_div2:
#endif
  if (extra)
    {
      qx ++;
      sb |= q0 & 1;
      q0 = (q1 << (GMP_NUMB_BITS - 1)) | (q0 >> 1);
      q1 = MPFR_LIMB_HIGHBIT | (q1 >> 1);
    }
  rb = q0 & (MPFR_LIMB_ONE << (sh - 1));
  sb |= (q0 & mask) ^ rb;
  qp[1] = q1;
  qp[0] = q0 & ~mask;

  MPFR_SIGN(q) = MPFR_MULT_SIGN (MPFR_SIGN (u), MPFR_SIGN (v));

  /* rounding */
  if (qx > __gmpfr_emax)
    return mpfr_overflow (q, rnd_mode, MPFR_SIGN(q));

  /* Warning: underflow should be checked *after* rounding, thus when rounding
     away and when q > 0.111...111*2^(emin-1), or when rounding to nearest and
     q >= 0.111...111[1]*2^(emin-1), there is no underflow. */
  if (qx < __gmpfr_emin)
    {
      /* Note: the case 0.111...111*2^(emin-1) < q < 2^(emin-1) is not possible
         here since (up to exponent) this would imply 1 - 2^(-p) < u/v < 1,
         thus v - 2^(-p)*v < u < v, and since we can assume 1/2 <= v < 1, it
         would imply v - 2^(-p) = v - ulp(v) < u < v, which has no solution. */

      /* For RNDN, mpfr_underflow always rounds away, thus for |q|<=2^(emin-2)
         we have to change to RNDZ. This corresponds to:
         (a) either qx < emin - 1
         (b) or qx = emin - 1 and qp[1] = 100....000, qp[0] = 0 and rb = sb = 0.
         Note: in case (b), it suffices to check whether sb = 0, since rb = 1
         and sb = 0 is not possible (the exact quotient would have p+1 bits, thus
         u would need at least p+1 bits). */
      if (rnd_mode == MPFR_RNDN &&
          (qx < __gmpfr_emin - 1 ||
           (qp[1] == MPFR_LIMB_HIGHBIT && qp[0] == MPFR_LIMB_ZERO && sb == 0)))
        rnd_mode = MPFR_RNDZ;
      return mpfr_underflow (q, rnd_mode, MPFR_SIGN(q));
    }

  MPFR_EXP (q) = qx; /* Don't use MPFR_SET_EXP since qx might be < __gmpfr_emin
                        in the cases "goto rounding" above. */
  if ((rb == 0 && sb == 0) || rnd_mode == MPFR_RNDF)
    {
      MPFR_ASSERTD(qx >= __gmpfr_emin);
      MPFR_RET (0);
    }
  else if (rnd_mode == MPFR_RNDN)
    {
      /* See the comment in mpfr_div_1. */
      MPFR_ASSERTD(sb != 0);
      if (rb == 0)
        goto truncate;
      else
        goto add_one_ulp;
    }
  else if (MPFR_IS_LIKE_RNDZ(rnd_mode, MPFR_IS_NEG(q)))
    {
    truncate:
      MPFR_ASSERTD(qx >= __gmpfr_emin);
      MPFR_RET(-MPFR_SIGN(q));
    }
  else /* round away from zero */
    {
    add_one_ulp:
      qp[0] += MPFR_LIMB_ONE << sh;
      qp[1] += qp[0] == 0;
      /* there can be no overflow in the addition above,
         see the analysis of mpfr_div_1 */
      MPFR_ASSERTD(qp[1] != 0);
      MPFR_RET(MPFR_SIGN(q));
    }
}

#endif /* !defined(MPFR_GENERIC_ABI) */

/* check if {ap, an} is zero */
static int
mpfr_mpn_cmpzero (mpfr_limb_ptr ap, mp_size_t an)
{
  MPFR_ASSERTD (an >= 0);
  while (an > 0)
    if (MPFR_LIKELY(ap[--an] != MPFR_LIMB_ZERO))
      return 1;
  return 0;
}

/* compare {ap, an} and {bp, bn} >> extra,
   aligned by the more significant limbs.
   Takes into account bp[0] for extra=1.
*/
static int
mpfr_mpn_cmp_aux (mpfr_limb_ptr ap, mp_size_t an,
                  mpfr_limb_ptr bp, mp_size_t bn, int extra)
{
  int cmp = 0;
  mp_size_t k;
  mp_limb_t bb;

  MPFR_ASSERTD (an >= 0);
  MPFR_ASSERTD (bn >= 0);
  MPFR_ASSERTD (extra == 0 || extra == 1);

  if (an >= bn)
    {
      k = an - bn;
      while (cmp == 0 && bn > 0)
        {
          bn --;
          bb = (extra) ? ((bp[bn+1] << (GMP_NUMB_BITS - 1)) | (bp[bn] >> 1))
            : bp[bn];
          cmp = (ap[k + bn] > bb) ? 1 : ((ap[k + bn] < bb) ? -1 : 0);
        }
      bb = (extra) ? bp[0] << (GMP_NUMB_BITS - 1) : MPFR_LIMB_ZERO;
      while (cmp == 0 && k > 0)
        {
          k--;
          cmp = (ap[k] > bb) ? 1 : ((ap[k] < bb) ? -1 : 0);
          bb = MPFR_LIMB_ZERO; /* ensure we consider only once bp[0] & 1 */
        }
      if (cmp == 0 && bb != MPFR_LIMB_ZERO)
        cmp = -1;
    }
  else /* an < bn */
    {
      k = bn - an;
      while (cmp == 0 && an > 0)
        {
          an --;
          bb = (extra) ? ((bp[k+an+1] << (GMP_NUMB_BITS - 1)) | (bp[k+an] >> 1))
            : bp[k+an];
          if (ap[an] > bb)
            cmp = 1;
          else if (ap[an] < bb)
            cmp = -1;
        }
      while (cmp == 0 && k > 0)
        {
          k--;
          bb = (extra) ? ((bp[k+1] << (GMP_NUMB_BITS - 1)) | (bp[k] >> 1))
            : bp[k];
          cmp = (bb != MPFR_LIMB_ZERO) ? -1 : 0;
        }
      if (cmp == 0 && extra && (bp[0] & MPFR_LIMB_ONE))
        cmp = -1;
    }
  return cmp;
}

/* {ap, n} <- {ap, n} - {bp, n} >> extra - cy, with cy = 0 or 1.
   Return borrow out.
*/
static mp_limb_t
mpfr_mpn_sub_aux (mpfr_limb_ptr ap, mpfr_limb_ptr bp, mp_size_t n,
                  mp_limb_t cy, int extra)
{
  mp_limb_t bb, rp;

  MPFR_ASSERTD (cy <= 1);
  MPFR_ASSERTD (n >= 0);

  while (n--)
    {
      bb = (extra) ? (MPFR_LIMB_LSHIFT(bp[1],GMP_NUMB_BITS-1) | (bp[0] >> 1)) : bp[0];
      rp = ap[0] - bb - cy;
      cy = (ap[0] < bb) || (cy && rp == MPFR_LIMB_MAX) ?
        MPFR_LIMB_ONE : MPFR_LIMB_ZERO;
      ap[0] = rp;
      ap ++;
      bp ++;
    }
  MPFR_ASSERTD (cy <= 1);
  return cy;
}

MPFR_HOT_FUNCTION_ATTR int
mpfr_div (mpfr_ptr q, mpfr_srcptr u, mpfr_srcptr v, mpfr_rnd_t rnd_mode)
{
  mp_size_t q0size, usize, vsize;
  mp_size_t qsize; /* number of limbs wanted for the computed quotient */
  mp_size_t qqsize;
  mp_size_t k;
  mpfr_limb_ptr q0p, qp;
  mpfr_limb_ptr up, vp;
  mpfr_limb_ptr ap;
  mpfr_limb_ptr bp;
  mp_limb_t qh;
  mp_limb_t sticky_u, sticky_v;
  mp_limb_t low_u;
  mp_limb_t sticky;
  mp_limb_t sticky3;
  mp_limb_t round_bit;
  mpfr_exp_t qexp;
  int sign_quotient;
  int extra_bit;
  int sh, sh2;
  int inex;
  int like_rndz;
  MPFR_TMP_DECL(marker);

  MPFR_LOG_FUNC (
    ("u[%Pu]=%.*Rg v[%Pu]=%.*Rg rnd=%d",
     mpfr_get_prec(u), mpfr_log_prec, u,
     mpfr_get_prec (v),mpfr_log_prec, v, rnd_mode),
    ("q[%Pu]=%.*Rg inexact=%d", mpfr_get_prec(q), mpfr_log_prec, q, inex));

  /**************************************************************************
   *                                                                        *
   *              This part of the code deals with special cases            *
   *                                                                        *
   **************************************************************************/

  if (MPFR_UNLIKELY(MPFR_ARE_SINGULAR(u,v)))
    {
      if (MPFR_IS_NAN(u) || MPFR_IS_NAN(v))
        {
          MPFR_SET_NAN(q);
          MPFR_RET_NAN;
        }
      sign_quotient = MPFR_MULT_SIGN( MPFR_SIGN(u) , MPFR_SIGN(v) );
      MPFR_SET_SIGN(q, sign_quotient);
      if (MPFR_IS_INF(u))
        {
          if (MPFR_IS_INF(v))
            {
              MPFR_SET_NAN(q);
              MPFR_RET_NAN;
            }
          else
            {
              MPFR_SET_INF(q);
              MPFR_RET(0);
            }
        }
      else if (MPFR_IS_INF(v))
        {
          MPFR_SET_ZERO (q);
          MPFR_RET (0);
        }
      else if (MPFR_IS_ZERO (v))
        {
          if (MPFR_IS_ZERO (u))
            {
              MPFR_SET_NAN(q);
              MPFR_RET_NAN;
            }
          else
            {
              MPFR_ASSERTD (! MPFR_IS_INF (u));
              MPFR_SET_INF(q);
              MPFR_SET_DIVBY0 ();
              MPFR_RET(0);
            }
        }
      else
        {
          MPFR_ASSERTD (MPFR_IS_ZERO (u));
          MPFR_SET_ZERO (q);
          MPFR_RET (0);
        }
    }

  /* When MPFR_GENERIC_ABI is defined, we don't use special code. */
#if !defined(MPFR_GENERIC_ABI)
  if (MPFR_GET_PREC(u) == MPFR_GET_PREC(q) &&
      MPFR_GET_PREC(v) == MPFR_GET_PREC(q))
    {
      if (MPFR_GET_PREC(q) < GMP_NUMB_BITS)
        return mpfr_div_1 (q, u, v, rnd_mode);

      if (GMP_NUMB_BITS < MPFR_GET_PREC(q) &&
          MPFR_GET_PREC(q) < 2 * GMP_NUMB_BITS)
        return mpfr_div_2 (q, u, v, rnd_mode);

      if (MPFR_GET_PREC(q) == GMP_NUMB_BITS)
        return mpfr_div_1n (q, u, v, rnd_mode);
    }
#endif /* !defined(MPFR_GENERIC_ABI) */

  usize = MPFR_LIMB_SIZE(u);
  vsize = MPFR_LIMB_SIZE(v);
  q0size = MPFR_LIMB_SIZE(q); /* number of limbs of destination */
  q0p = MPFR_MANT(q);
  up = MPFR_MANT(u);
  vp = MPFR_MANT(v);
  sticky_u = MPFR_LIMB_ZERO;
  sticky_v = MPFR_LIMB_ZERO;
  round_bit = MPFR_LIMB_ZERO;

  /**************************************************************************
   *                                                                        *
   *              End of the part concerning special values.                *
   *                                                                        *
   **************************************************************************/

  /* When the divisor has one limb and MPFR_LONG_WITHIN_LIMB is defined,
     we can use mpfr_div_ui, which should be faster, assuming there is no
     intermediate overflow or underflow.
     The divisor interpreted as an integer satisfies
     2^(GMP_NUMB_BITS-1) <= vm < 2^GMP_NUMB_BITS, thus the quotient
     satisfies 2^(EXP(u)-1-GMP_NUMB_BITS) < u/vm < 2^(EXP(u)-GMP_NUMB_BITS+1)
     and its exponent is either EXP(u)-GMP_NUMB_BITS or one more. */
#ifdef MPFR_LONG_WITHIN_LIMB
  if (vsize <= 1 && __gmpfr_emin <= MPFR_EXP(u) - GMP_NUMB_BITS
      && MPFR_EXP(u) - GMP_NUMB_BITS + 1 <= __gmpfr_emax
      && vp[0] <= ULONG_MAX)
    {
      mpfr_exp_t exp_v = MPFR_EXP(v); /* save it in case q=v */
      if (MPFR_IS_POS (v))
        inex = mpfr_div_ui (q, u, vp[0], rnd_mode);
      else
        {
          inex = -mpfr_div_ui (q, u, vp[0], MPFR_INVERT_RND(rnd_mode));
          MPFR_CHANGE_SIGN(q);
        }
      /* q did not under/overflow */
      MPFR_EXP(q) -= exp_v;
      /* The following test is needed, otherwise the next addition
         on the exponent may overflow, e.g. when dividing the
         largest finite MPFR number by the smallest positive one. */
      if (MPFR_UNLIKELY (MPFR_EXP(q) > __gmpfr_emax - GMP_NUMB_BITS))
        return mpfr_overflow (q, rnd_mode, MPFR_SIGN(q));
      MPFR_EXP(q) += GMP_NUMB_BITS;
      return mpfr_check_range (q, inex, rnd_mode);
    }
#endif

  MPFR_TMP_MARK(marker);

  /* set sign */
  sign_quotient = MPFR_MULT_SIGN( MPFR_SIGN(u) , MPFR_SIGN(v) );
  MPFR_SET_SIGN(q, sign_quotient);

  /* determine if an extra bit comes from the division, i.e. if the
     significand of u (as a fraction in [1/2, 1[) is larger than that
     of v */
  if (MPFR_LIKELY(up[usize - 1] != vp[vsize - 1]))
    extra_bit = (up[usize - 1] > vp[vsize - 1]) ? 1 : 0;
  else /* most significant limbs are equal, must look at further limbs */
    {
      mp_size_t l;

      k = usize - 1;
      l = vsize - 1;
      while (k != 0 && l != 0 && up[--k] == vp[--l]);
      /* now k=0 or l=0 or up[k] != vp[l] */
      if (up[k] != vp[l])
        extra_bit = (up[k] > vp[l]);
      /* now up[k] = vp[l], thus either k=0 or l=0 */
      else if (l == 0) /* no more divisor limb */
        extra_bit = 1;
      else /* k=0: no more dividend limb */
        extra_bit = mpfr_mpn_cmpzero (vp, l) == 0;
    }

  /* set exponent */
  qexp = MPFR_GET_EXP (u) - MPFR_GET_EXP (v) + extra_bit;

  /* sh is the number of zero bits in the low limb of the quotient */
  MPFR_UNSIGNED_MINUS_MODULO(sh, MPFR_PREC(q));

  like_rndz = rnd_mode == MPFR_RNDZ ||
    rnd_mode == (sign_quotient < 0 ? MPFR_RNDU : MPFR_RNDD);

  /**************************************************************************
   *                                                                        *
   *       We first try Mulders' short division (for large operands)        *
   *                                                                        *
   **************************************************************************/

  if (MPFR_UNLIKELY(q0size >= MPFR_DIV_THRESHOLD &&
                    vsize >= MPFR_DIV_THRESHOLD))
    {
      mp_size_t n = q0size + 1; /* we will perform a short (2n)/n division */
      mpfr_limb_ptr ap, bp, qp;
      mpfr_prec_t p;

      /* since Mulders' short division clobbers the dividend, we have to
         copy it */
      ap = MPFR_TMP_LIMBS_ALLOC (n + n);
      if (usize >= n + n) /* truncate the dividend */
        MPN_COPY(ap, up + usize - (n + n), n + n);
      else                /* zero-pad the dividend */
        {
          MPN_COPY(ap + (n + n) - usize, up, usize);
          MPN_ZERO(ap, (n + n) - usize);
        }

      if (vsize >= n) /* truncate the divisor */
        bp = vp + vsize - n;
      else            /* zero-pad the divisor */
        {
          bp = MPFR_TMP_LIMBS_ALLOC (n);
          MPN_COPY(bp + n - vsize, vp, vsize);
          MPN_ZERO(bp, n - vsize);
        }

      qp = MPFR_TMP_LIMBS_ALLOC (n);
      /* since n = q0size + 1, we have n >= 2 here */
      qh = mpfr_divhigh_n (qp, ap, bp, n);
      MPFR_ASSERTD (qh == 0 || qh == 1);
      /* in all cases, the error is at most (2n+2) ulps on qh*B^n+{qp,n},
         cf algorithms.tex */

      p = n * GMP_NUMB_BITS - MPFR_INT_CEIL_LOG2 (2 * n + 2);
      /* If rnd=RNDN, we need to be able to round with a directed rounding
         and one more bit. */
      if (qh == 1)
        {
          mpn_rshift (qp, qp, n, 1);
          qp[n - 1] |= MPFR_LIMB_HIGHBIT;
        }
      if (MPFR_LIKELY (mpfr_round_p (qp, n, p,
                                     MPFR_PREC(q) + (rnd_mode == MPFR_RNDN))))
        {
          /* we can round correctly whatever the rounding mode */
          MPN_COPY (q0p, qp + 1, q0size);
          q0p[0] &= ~MPFR_LIMB_MASK(sh); /* put to zero low sh bits */

          if (rnd_mode == MPFR_RNDN) /* round to nearest */
            {
              /* we know we can round, thus we are never in the even rule case:
                 if the round bit is 0, we truncate
                 if the round bit is 1, we add 1 */
              if (sh > 0)
                round_bit = (qp[1] >> (sh - 1)) & 1;
              else
                round_bit = qp[0] >> (GMP_NUMB_BITS - 1);
              /* TODO: add value coverage tests in tdiv to check that
                 we reach this part with different values of qh and
                 round_bit (4 cases). */
              if (round_bit == 0)
                {
                  inex = -1;
                  goto truncate;
                }
              else /* round_bit = 1 */
                goto add_one_ulp;
            }
          else if (! like_rndz) /* round away */
            goto add_one_ulp;
          else /* round to zero: nothing to do */
            {
              inex = -1;
              goto truncate;
            }
        }
    }

  /**************************************************************************
   *                                                                        *
   *     Mulders' short division failed: we revert to integer division      *
   *                                                                        *
   **************************************************************************/

  if (MPFR_UNLIKELY(rnd_mode == MPFR_RNDN && sh == 0))
    { /* we compute the quotient with one more limb, in order to get
         the round bit in the quotient, and the remainder only contains
         sticky bits */
      qsize = q0size + 1;
      /* need to allocate memory for the quotient */
      qp = MPFR_TMP_LIMBS_ALLOC (qsize);
    }
  else
    {
      qsize = q0size;
      qp = q0p; /* directly put the quotient in the destination */
    }
  qqsize = qsize + qsize;

  /* prepare the dividend */
  ap = MPFR_TMP_LIMBS_ALLOC (qqsize);
  if (MPFR_LIKELY(qqsize > usize)) /* use the full dividend */
    {
      k = qqsize - usize; /* k > 0 */
      MPN_ZERO(ap, k);
      if (extra_bit)
        ap[k - 1] = mpn_rshift (ap + k, up, usize, 1);
      else
        MPN_COPY(ap + k, up, usize);
    }
  else /* truncate the dividend */
    {
      k = usize - qqsize;
      if (extra_bit)
        sticky_u = mpn_rshift (ap, up + k, qqsize, 1);
      else
        MPN_COPY(ap, up + k, qqsize);
      sticky_u = sticky_u || mpfr_mpn_cmpzero (up, k);
    }
  low_u = sticky_u;

  /* now sticky_u is non-zero iff the truncated part of u is non-zero */

  /* prepare the divisor */
  if (MPFR_LIKELY(vsize >= qsize))
    {
      k = vsize - qsize;
      if (qp != vp)
        bp = vp + k; /* avoid copying the divisor */
      else /* need to copy, since mpn_divrem doesn't allow overlap
              between quotient and divisor, necessarily k = 0
              since quotient and divisor are the same mpfr variable */
        {
          bp = MPFR_TMP_LIMBS_ALLOC (qsize);
          MPN_COPY(bp, vp, vsize);
        }
      sticky_v = sticky_v || mpfr_mpn_cmpzero (vp, k);
      k = 0;
    }
  else /* vsize < qsize: small divisor case */
    {
      bp = vp;
      k = qsize - vsize;
    }

  /**************************************************************************
   *                                                                        *
   *  Here we perform the real division of {ap+k,qqsize-k} by {bp,qsize-k}  *
   *                                                                        *
   **************************************************************************/

  /* In the general case (usize > 2*qsize and vsize > qsize), we have:
       ______________________________________
      |                          |           |   u1 has 2*qsize limbs
      |             u1           |     u0    |   u0 has usize-2*qsize limbs
      |__________________________|___________|

                      ____________________
                     |           |        |      v1 has qsize limbs
                     |    v1     |    v0  |      v0 has vsize-qsize limbs
                     |___________|________|

      We divide u1 by v1, with quotient in qh + {qp, qsize} and
      remainder (denoted r below) stored in place of the low qsize limbs of u1.
  */

  /* if Mulders' short division failed, we revert to division with remainder */
  qh = mpn_divrem (qp, 0, ap + k, qqsize - k, bp, qsize - k);
  /* let u1 be the upper part of u, and v1 the upper part of v (with sticky_u
     and sticky_v representing the lower parts), then the quotient of u1 by v1
     is now in {qp, qsize}, with possible carry in qh, and the remainder in
     {ap + k, qsize - k} */
  /* warning: qh may be 1 if u1 == v1, but u < v */

  k = qsize;
  sticky_u = sticky_u || mpfr_mpn_cmpzero (ap, k);

  sticky = sticky_u | sticky_v;

  /* now sticky is non-zero iff one of the following holds:
     (a) the truncated part of u is non-zero
     (b) the truncated part of v is non-zero
     (c) the remainder from division is non-zero */

  if (MPFR_LIKELY(qsize == q0size))
    {
      sticky3 = qp[0] & MPFR_LIMB_MASK(sh); /* does nothing when sh=0 */
      sh2 = sh;
    }
  else /* qsize = q0size + 1: only happens when rnd_mode=MPFR_RNDN and sh=0 */
    {
      MPN_COPY (q0p, qp + 1, q0size);
      sticky3 = qp[0];
      sh2 = GMP_NUMB_BITS;
    }
  qp[0] ^= sticky3;
  /* sticky3 contains the truncated bits from the quotient,
     including the round bit, and 1 <= sh2 <= GMP_NUMB_BITS
     is the number of bits in sticky3 */
  inex = (sticky != MPFR_LIMB_ZERO) || (sticky3 != MPFR_LIMB_ZERO);

  /* to round, we distinguish two cases:
     (a) vsize <= qsize: we used the full divisor
     (b) vsize > qsize: the divisor was truncated
  */

  if (MPFR_LIKELY(vsize <= qsize)) /* use the full divisor */
    {
      if (MPFR_LIKELY(rnd_mode == MPFR_RNDN))
        {
          round_bit = sticky3 & (MPFR_LIMB_ONE << (sh2 - 1));
          sticky = (sticky3 ^ round_bit) | sticky_u;
        }
      else if (like_rndz || inex == 0)
        sticky = (inex == 0) ? MPFR_LIMB_ZERO : MPFR_LIMB_ONE;
      else  /* round away from zero */
        sticky = MPFR_LIMB_ONE;
      goto case_1;
    }
  else /* vsize > qsize: need to truncate the divisor */
    {
      if (inex == 0)
        goto truncate;
      else
        {
          /* We know the estimated quotient is an upper bound of the exact
             quotient (with rounding toward zero), with a difference of at
             most 2 in qp[0].
             Thus we can round except when sticky3 is 000...000 or 000...001
             for directed rounding, and 100...000 or 100...001 for rounding
             to nearest. (For rounding to nearest, we cannot determine the
             inexact flag for 000...000 or 000...001.)
          */
          mp_limb_t sticky3orig = sticky3;
          if (rnd_mode == MPFR_RNDN)
            {
              round_bit = sticky3 & (MPFR_LIMB_ONE << (sh2 - 1));
              sticky3   = sticky3 ^ round_bit;
            }
          if (sticky3 != MPFR_LIMB_ZERO && sticky3 != MPFR_LIMB_ONE)
            {
              sticky = sticky3;
              goto case_1;
            }
          else /* hard case: we have to compare q1 * v0 and r + u0,
                 where q1 * v0 has qsize + (vsize-qsize) = vsize limbs, and
                 r + u0 has qsize + (usize-2*qsize) = usize-qsize limbs */
            {
              mp_size_t l;
              mpfr_limb_ptr sp;
              int cmp_s_r;
              mp_limb_t qh2;

              sp = MPFR_TMP_LIMBS_ALLOC (vsize);
              k = vsize - qsize;
              /* sp <- {qp, qsize} * {vp, vsize-qsize} */
              qp[0] ^= sticky3orig; /* restore original quotient */
              if (qsize >= k)
                mpn_mul (sp, qp, qsize, vp, k);
              else
                mpn_mul (sp, vp, k, qp, qsize);
              if (qh)
                qh2 = mpn_add_n (sp + qsize, sp + qsize, vp, k);
              else
                qh2 = MPFR_LIMB_ZERO;
              qp[0] ^= sticky3orig; /* restore truncated quotient */

              /* compare qh2 + {sp, k + qsize} to {ap, qsize} + u0 */
              cmp_s_r = (qh2 != 0) ? 1 : mpn_cmp (sp + k, ap, qsize);
              if (cmp_s_r == 0) /* compare {sp, k} and u0 */
                {
                  cmp_s_r = (usize >= qqsize) ?
                    mpfr_mpn_cmp_aux (sp, k, up, usize - qqsize, extra_bit) :
                    mpfr_mpn_cmpzero (sp, k);
                }
              /* now cmp_s_r > 0 if {sp, vsize} > {ap, qsize} + u0
                     cmp_s_r = 0 if {sp, vsize} = {ap, qsize} + u0
                     cmp_s_r < 0 if {sp, vsize} < {ap, qsize} + u0 */
              if (cmp_s_r <= 0) /* quotient is in [q1, q1+1) */
                {
                  sticky = (cmp_s_r == 0) ? sticky3 : MPFR_LIMB_ONE;
                  goto case_1;
                }
              else /* cmp_s_r > 0, quotient is < q1: to determine if it is
                      in [q1-2,q1-1] or in [q1-1,q1], we need to subtract
                      the low part u0 of the dividend from q*v0 */
                {
                  mp_limb_t cy = MPFR_LIMB_ZERO;

                  /* subtract u0 >> extra_bit if non-zero */
                  if (qh2 != 0) /* whatever the value of {up, m + k}, it
                                   will be smaller than qh2 + {sp, k} */
                    cmp_s_r = 1;
                  else
                    {
                      if (low_u != MPFR_LIMB_ZERO)
                        {
                          mp_size_t m;
                          l = usize - qqsize; /* number of limbs in u0 */
                          m = (l > k) ? l - k : 0;
                          cy = (extra_bit) ?
                            (up[m] & MPFR_LIMB_ONE) : MPFR_LIMB_ZERO;
                          if (l >= k) /* u0 has at least as many limbs than s:
                                         first look if {up, m} is not zero,
                                         and compare {sp, k} and {up + m, k} */
                            {
                              cy = cy || mpfr_mpn_cmpzero (up, m);
                              low_u = cy;
                              cy = mpfr_mpn_sub_aux (sp, up + m, k,
                                                     cy, extra_bit);
                            }
                          else /* l < k: s has more limbs than u0 */
                            {
                              low_u = MPFR_LIMB_ZERO;
                              if (cy != MPFR_LIMB_ZERO)
                                cy = mpn_sub_1 (sp + k - l - 1, sp + k - l - 1,
                                                1, MPFR_LIMB_HIGHBIT);
                              cy = mpfr_mpn_sub_aux (sp + k - l, up, l,
                                                     cy, extra_bit);
                            }
                        }
                      MPFR_ASSERTD (cy <= 1);
                      cy = mpn_sub_1 (sp + k, sp + k, qsize, cy);
                      /* subtract r */
                      cy += mpn_sub_n (sp + k, sp + k, ap, qsize);
                      MPFR_ASSERTD (cy <= 1);
                      /* now compare {sp, ssize} to v */
                      cmp_s_r = mpn_cmp (sp, vp, vsize);
                      if (cmp_s_r == 0 && low_u != MPFR_LIMB_ZERO)
                        cmp_s_r = 1; /* since in fact we subtracted
                                        less than 1 */
                    }
                  if (cmp_s_r <= 0) /* q1-1 <= u/v < q1 */
                    {
                      if (sticky3 == MPFR_LIMB_ONE)
                        { /* q1-1 is either representable (directed rounding),
                             or the middle of two numbers (nearest) */
                          sticky = (cmp_s_r) ? MPFR_LIMB_ONE : MPFR_LIMB_ZERO;
                          goto case_1;
                        }
                      /* now necessarily sticky3=0 */
                      else if (round_bit == MPFR_LIMB_ZERO)
                        { /* round_bit=0, sticky3=0: q1-1 is exact only
                             when sh=0 */
                          inex = (cmp_s_r || sh) ? -1 : 0;
                          if (rnd_mode == MPFR_RNDN ||
                              (! like_rndz && inex != 0))
                            {
                              inex = 1;
                              goto truncate_check_qh;
                            }
                          else /* round down */
                            goto sub_one_ulp;
                        }
                      else /* sticky3=0, round_bit=1 ==> rounding to nearest */
                        {
                          inex = cmp_s_r;
                          goto truncate;
                        }
                    }
                  else /* q1-2 < u/v < q1-1 */
                    {
                      /* if rnd=MPFR_RNDN, the result is q1 when
                         q1-2 >= q1-2^(sh-1), i.e. sh >= 2,
                         otherwise (sh=1) it is q1-2 */
                      if (rnd_mode == MPFR_RNDN) /* sh > 0 */
                        {
                          /* Case sh=1: sb=0 always, and q1-rb is exactly
                             representable, like q1-rb-2.
                             rb action
                             0  subtract two ulps, inex=-1
                             1  truncate, inex=1

                             Case sh>1: one ulp is 2^(sh-1) >= 2
                             rb sb action
                             0  0  truncate, inex=1
                             0  1  truncate, inex=1
                             1  x  truncate, inex=-1
                           */
                          if (sh == 1)
                            {
                              if (round_bit == MPFR_LIMB_ZERO)
                                {
                                  inex = -1;
                                  sh = 0;
                                  goto sub_two_ulp;
                                }
                              else
                                {
                                  inex = 1;
                                  goto truncate_check_qh;
                                }
                            }
                          else /* sh > 1 */
                            {
                              inex = (round_bit == MPFR_LIMB_ZERO) ? 1 : -1;
                              goto truncate_check_qh;
                            }
                        }
                      else if (like_rndz)
                        {
                          /* the result is down(q1-2), i.e. subtract one
                             ulp if sh > 0, and two ulps if sh=0 */
                          inex = -1;
                          if (sh > 0)
                            goto sub_one_ulp;
                          else
                            goto sub_two_ulp;
                        }
                      /* if round away from zero, the result is up(q1-1),
                         which is q1 unless sh = 0, where it is q1-1 */
                      else
                        {
                          inex = 1;
                          if (sh > 0)
                            goto truncate_check_qh;
                          else /* sh = 0 */
                            goto sub_one_ulp;
                        }
                    }
                }
            }
        }
    }

 case_1: /* quotient is in [q1, q1+1),
            round_bit is the round_bit (0 for directed rounding),
            sticky the sticky bit */
  if (like_rndz || (round_bit == MPFR_LIMB_ZERO && sticky == MPFR_LIMB_ZERO))
    {
      inex = round_bit == MPFR_LIMB_ZERO && sticky == MPFR_LIMB_ZERO ? 0 : -1;
      goto truncate;
    }
  else if (rnd_mode == MPFR_RNDN) /* sticky <> 0 or round <> 0 */
    {
      if (round_bit == MPFR_LIMB_ZERO) /* necessarily sticky <> 0 */
        {
          inex = -1;
          goto truncate;
        }
      /* round_bit = 1 */
      else if (sticky != MPFR_LIMB_ZERO)
        goto add_one_ulp; /* inex=1 */
      else /* round_bit=1, sticky=0 */
        goto even_rule;
    }
  else /* round away from zero, sticky <> 0 */
    goto add_one_ulp; /* with inex=1 */

 sub_two_ulp:
  /* we cannot subtract MPFR_LIMB_MPFR_LIMB_ONE << (sh+1) since this is
     undefined for sh = GMP_NUMB_BITS */
  qh -= mpn_sub_1 (q0p, q0p, q0size, MPFR_LIMB_ONE << sh);
  /* go through */

 sub_one_ulp:
  qh -= mpn_sub_1 (q0p, q0p, q0size, MPFR_LIMB_ONE << sh);
  /* go through truncate_check_qh */

 truncate_check_qh:
  if (qh)
    {
      if (MPFR_LIKELY (qexp < MPFR_EXP_MAX))
        qexp ++;
      /* else qexp is now incorrect, but one will still get an overflow */
      q0p[q0size - 1] = MPFR_LIMB_HIGHBIT;
    }
  goto truncate;

 even_rule: /* has to set inex */
  inex = (q0p[0] & (MPFR_LIMB_ONE << sh)) ? 1 : -1;
  if (inex < 0)
    goto truncate;
  /* else go through add_one_ulp */

 add_one_ulp:
  inex = 1; /* always here */
  if (mpn_add_1 (q0p, q0p, q0size, MPFR_LIMB_ONE << sh))
    {
      if (MPFR_LIKELY (qexp < MPFR_EXP_MAX))
        qexp ++;
      /* else qexp is now incorrect, but one will still get an overflow */
      q0p[q0size - 1] = MPFR_LIMB_HIGHBIT;
    }

 truncate: /* inex already set */

  MPFR_TMP_FREE(marker);

  /* check for underflow/overflow */
  if (MPFR_UNLIKELY(qexp > __gmpfr_emax))
    return mpfr_overflow (q, rnd_mode, sign_quotient);
  else if (MPFR_UNLIKELY(qexp < __gmpfr_emin))
    {
      if (rnd_mode == MPFR_RNDN && ((qexp < __gmpfr_emin - 1) ||
                                   (inex >= 0 && mpfr_powerof2_raw (q))))
        rnd_mode = MPFR_RNDZ;
      return mpfr_underflow (q, rnd_mode, sign_quotient);
    }
  MPFR_SET_EXP(q, qexp);

  inex *= sign_quotient;
  MPFR_RET (inex);
}