flint-sys 0.9.0

Bindings to the FLINT C library
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
/*
    Copyright (C) 2010, 2011 William Hart
    Copyright (C) 2011 Sebastian Pancratz
    Copyright (C) 2023 Fredrik Johansson

    This file is part of FLINT.

    FLINT 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/>.
*/

#include "mpn_extras.h"
#include "nmod.h"
#include "nmod_vec.h"
#include "nmod_poly.h"
#include "nmod_poly/impl.h"

/* addmul_fits: a + b*c fits in 16/32/64 bits */
/* addmul_addmul_fits: a + b*c + d*e fits in 16/32/64 bits */

#if FLINT_BITS == 64

#define NMOD_ADDMUL_FITS_HALFWORD(mod) ((mod.n) <= UWORD(65535))
#define NMOD_ADDMUL_ADDMUL_FITS_HALFWORD(mod) ((mod.n) <= UWORD(46341))
#define NMOD_ADDMUL_FITS_WORD(mod) ((mod.n) <= UWORD(4294967295))
#define NMOD_ADDMUL_ADDMUL_FITS_WORD(mod) ((mod.n) <= UWORD(3037000500))

#else

#define NMOD_ADDMUL_FITS_HALFWORD(mod) ((mod.n) <= UWORD(255))
#define NMOD_ADDMUL_ADDMUL_FITS_HALFWORD(mod) ((mod.n) <= UWORD(181))
#define NMOD_ADDMUL_FITS_WORD(mod) ((mod.n) <= UWORD(65535))
#define NMOD_ADDMUL_ADDMUL_FITS_WORD(mod) ((mod.n) <= UWORD(46341))

#endif

void _nmod_poly_divrem_q0_preinv1(nn_ptr Q, nn_ptr R,
                          nn_srcptr A, nn_srcptr B, slong lenA, ulong invL, nmod_t mod)
{
    if (lenA == 1)
    {
        _nmod_vec_scalar_mul_nmod(Q, A, lenA, invL, mod);
    }
    else
    {
        Q[0] = nmod_mul(A[lenA-1], invL, mod);

        if (R == A)
        {
            _nmod_vec_scalar_addmul_nmod(R, B, lenA - 1, nmod_neg(Q[0], mod), mod);
        }
        else
        {
            _nmod_vec_scalar_mul_nmod(R, B, lenA - 1, Q[0], mod);
            _nmod_vec_sub(R, A, R, lenA - 1, mod);
        }
    }
}

/* For GCC and Zen 3, the performance of the following critical function is
   extremely finicky, with an immediate 10% slowdown if one reorders some
   instruction in the main loop, makes it inline, etc. Just try to profile
   and do whatever works.
*/

FLINT_STATIC_NOINLINE
void _nmod_poly_divrem_q1_preinv1_fullword(nn_ptr Q, nn_ptr R,
                          nn_srcptr A, slong lenA, nn_srcptr B, slong lenB,
                          ulong invL, nmod_t mod)
{
    slong i;
    ulong t, q0, q1, t1, t0, s1, s0;

    FLINT_ASSERT(lenA == lenB + 1);

    q1 = _nmod_mul_fullword(A[lenA-1], invL, mod);
    t  = _nmod_mul_fullword(q1, B[lenB-2], mod);
    t  = nmod_sub(t, A[lenA-2], mod);
    q0 = _nmod_mul_fullword(t, invL, mod);
    Q[0] = nmod_neg(q0, mod);
    Q[1] = q1;
    q1 = nmod_neg(q1, mod);
    /* R = A + (q1*x + q0)*B */

    /* Hack: nmod_addmul is faster than _nmod_mull_fullword + nmod_add */
    R[0] = nmod_addmul(A[0], q0, B[0], mod);

    /* We want to compute r = a + q0*b + q1*c where a, b, c, q0, q1 <= n - 1.
       If (1 + q0 + q1) < 2^FLINT_BITS, then r < 2^FLINT_BITS * (n-1).
       In this case, r fits in two limbs without overflow and the high limb is
       already reduced mod n. This will almost always be the case when n is
       close to 2^(FLINT_BITS-1), and happens with decent probability when
       n is close to 2^FLINT_BITS too. */
    if (q0 + q1 + 1 >= q0)  /* Checks that q0 + (q1 + 1) doesn't wrap around. */
    {
        for (i = 1; i < lenB - 1; i++)
        {
            umul_ppmm(t1, t0, q1, B[i - 1]);
            add_ssaaaa(t1, t0, t1, t0, 0, A[i]);
            umul_ppmm(s1, s0, q0, B[i]);
            add_ssaaaa(t1, t0, t1, t0, s1, s0);
            FLINT_ASSERT(t1 < mod.n);
            NMOD_RED2_FULLWORD(R[i], t1, t0, mod);
        }
    }
    else
    {
        for (i = 1; i < lenB - 1; i++)
        {
            umul_ppmm(t1, t0, q1, B[i - 1]);
            add_ssaaaa(t1, t0, t1, t0, 0, A[i]);
            umul_ppmm(s1, s0, q0, B[i]);
            add_ssaaaa(t1, t0, t1, t0, 0, s0);
            t1 = nmod_add(t1, s1, mod);
            FLINT_ASSERT(t1 < mod.n);
            NMOD_RED2_FULLWORD(R[i], t1, t0, mod);
        }
    }
}

void _nmod_poly_divrem_q1_preinv1(nn_ptr Q, nn_ptr R,
                          nn_srcptr A, slong lenA, nn_srcptr B, slong lenB,
                          ulong invL, nmod_t mod)
{
    slong i;
    ulong t, q0, q1, t1, t0, s1, s0;

    FLINT_ASSERT(lenA == lenB + 1);

    if (lenB == 1)
    {
        _nmod_vec_scalar_mul_nmod(Q, A, lenA, invL, mod);
        return;
    }

    if (NMOD_ADDMUL_FITS_HALFWORD(mod))
    {
        ulong n = mod.n;
        ulong ninv = n_lemire_precomp(n);

        q1 = n_mod_lemire(A[lenA-1] * invL, n, ninv);
        t  = n_mod_lemire(q1 * B[lenB-2], n, ninv);
        t  = nmod_sub(t, A[lenA-2], mod);
        q0 = n_mod_lemire(t * invL, n, ninv);
        Q[0] = nmod_neg(q0, mod);
        Q[1] = q1;
        q1 = nmod_neg(q1, mod);
        /* R = A + (q1*x + q0)*B */

        R[0] = n_mod_lemire(A[0] + q0 * B[0], n, ninv);

        if (NMOD_ADDMUL_ADDMUL_FITS_HALFWORD(mod))
        {
            for (i = 1; i < lenB - 1; i++)
                R[i] = n_mod_lemire(A[i] + q1*B[i - 1] + q0*B[i], n, ninv);
        }
        else
        {
            for (i = 1; i < lenB - 1; i++)
                R[i] = n_mod_lemire(n_mod_lemire(A[i] + q1*B[i - 1], n, ninv) + q0*B[i], n, ninv);
        }
    }
    else if (NMOD_ADDMUL_FITS_WORD(mod))
    {
        ulong n = mod.n;
        ulong ninv = n_barrett_precomp(n);

        q1 = n_mod_barrett(A[lenA-1] * invL, n, ninv);
        t  = n_mod_barrett(q1 * B[lenB-2], n, ninv);
        t  = nmod_sub(t, A[lenA-2], mod);
        q0 = n_mod_barrett(t * invL, n, ninv);
        Q[0] = nmod_neg(q0, mod);
        Q[1] = q1;
        q1 = nmod_neg(q1, mod);
        /* R = A + (q1*x + q0)*B */

        R[0] = n_mod_barrett(A[0] + q0 * B[0], n, ninv);

        /* In the second branch, the lazy mod maps
               [0, ..., (n-1) + (n-1)^2] -> [0, 2n-1]
           and the _mod2 maps
                [0, 2n-1 + (n-1)^2] -> [0,n-1]. */

        if (NMOD_ADDMUL_ADDMUL_FITS_WORD(mod))
            for (i = 1; i < lenB - 1; i++)
                R[i] = n_mod_barrett(A[i] + q1*B[i - 1] + q0*B[i], n, ninv);
        else
            for (i = 1; i < lenB - 1; i++)
                R[i] = n_mod_barrett(n_mod_barrett_lazy(A[i] + q1*B[i - 1], n, ninv) + q0*B[i], n, ninv);
    }
    else if (NMOD_BITS(mod) != FLINT_BITS)
    {
        FLINT_ASSERT(lenA == lenB + 1);

        q1 = nmod_mul(A[lenA-1], invL, mod);
        t  = nmod_mul(q1, B[lenB-2], mod);
        t  = nmod_sub(t, A[lenA-2], mod);
        q0 = nmod_mul(t, invL, mod);
        Q[0] = nmod_neg(q0, mod);
        Q[1] = q1;
        q1 = nmod_neg(q1, mod);
        /* R = A + (q1*x + q0)*B */

        R[0] = nmod_addmul(A[0], q0, B[0], mod);

        if (NMOD_BITS(mod) != FLINT_BITS)
        {
            for (i = 1; i < lenB - 1; i++)
            {
                umul_ppmm(t1, t0, q1, B[i - 1]);
                umul_ppmm(s1, s0, q0, B[i]);
                add_ssaaaa(t1, t0, t1, t0, 0, A[i]);
                add_ssaaaa(t1, t0, t1, t0, s1, s0);
                t1 = FLINT_MIN(t1, t1 - mod.n);
                FLINT_ASSERT(t1 < mod.n);
                NMOD_RED2(R[i], t1, t0, mod);
            }
        }
    }
    else
    {
        _nmod_poly_divrem_q1_preinv1_fullword(Q, R, A, lenA, B, lenB, invL, mod);
    }
}

static void
_nmod_poly_divrem_basecase_preinv1_1(nn_ptr Q, nn_ptr R,
                             nn_srcptr A, slong lenA, nn_srcptr B, slong lenB,
                             ulong invL,
                             nmod_t mod)
{
    slong iR, i;
    nn_ptr R1, ptrQ = Q - lenB + 1;
    ulong r, c;
    int unreduced_fits_halflimb;
    int reduced_fits_quarterlimb;

    FLINT_ASSERT(NMOD_BITS(mod) <= FLINT_BITS / 2);

    ulong unreduced_bound = (mod.n - 1) * (mod.n - 1) * (lenA - lenB + 1);

    unreduced_fits_halflimb = unreduced_bound < UWORD(1) << (FLINT_BITS / 2);
    reduced_fits_quarterlimb = NMOD_BITS(mod) <= FLINT_BITS / 4;

    ulong npre = n_barrett_precomp(mod.n);
    ulong npre2 = n_lemire_precomp(mod.n);

    TMP_INIT;
    TMP_START;
    R1 = TMP_ALLOC(lenA * sizeof(ulong));

    flint_mpn_copyi(R1, A, lenA);

    for (iR = lenA - 1; iR >= lenB - 1; iR--)
    {
        if (unreduced_fits_halflimb)
            r = n_mod_lemire(R1[iR], mod.n, npre2);
        else
            r = n_mod_barrett(R1[iR], mod.n, npre);

        if (r == 0)
        {
            ptrQ[iR] = 0;
        }
        else
        {
            if (invL == 1)
                ptrQ[iR] = r;
            else if (reduced_fits_quarterlimb)
                ptrQ[iR] = n_mod_lemire(invL * r, mod.n, npre2);
            else
                ptrQ[iR] = n_mod_barrett(invL * r, mod.n, npre);

            if (lenB > 1)
            {
                c = mod.n - ptrQ[iR];
                _nmod_vec_nored_scalar_addmul_halflimb(R1 + iR - lenB + 1, B, lenB - 1, c);
            }
        }
    }

    if (lenB > 1)
    {
        if (unreduced_fits_halflimb)
        {
            for (i = 0; i < lenB - 1; i++)
                R[i] = n_mod_lemire(R1[i], mod.n, npre2);
        }
        else
        {
            for (i = 0; i < lenB - 1; i++)
                R[i] = n_mod_barrett(R1[i], mod.n, npre);
        }
    }

    TMP_END;
}

/* Note: we can do better than n_ll_mod_preinv when the high limb
   of the double-limb sum is not reduced, but this benefits only a narrow
   set of parameters, so it is not currently implemented. */
static void
_nmod_poly_divrem_basecase_preinv1_2(nn_ptr Q, nn_ptr R,
                             nn_srcptr A, slong lenA, nn_srcptr B, slong lenB,
                             ulong invL,
                             nmod_t mod)
{
    slong iR, i;
    nn_ptr R2, ptrQ = Q - lenB + 1;
    ulong r, c;
    int halflimb;

    TMP_INIT;
    TMP_START;
    R2 = TMP_ALLOC(2 * lenA * sizeof(ulong));

    for (i = 0; i < lenA; i++)
    {
        R2[2 * i] = A[i];
        R2[2 * i + 1] = 0;
    }

    halflimb = (mod.n <= (UWORD(1) << (FLINT_BITS / 2)));

    for (iR = lenA - 1; iR >= lenB - 1; iR--)
    {
        r = n_ll_mod_preinv(R2[2 * iR + 1], R2[2 * iR], mod.n, mod.ninv);

        if (r == 0)
        {
            ptrQ[iR] = 0;
        }
        else
        {
            if (invL == 1)
                ptrQ[iR] = r;
            else
                ptrQ[iR] = nmod_mul(r, invL, mod);

            if (lenB > 1)
            {
                c = mod.n - ptrQ[iR];

                if (halflimb)
                    _nmod_vec_nored_ll_scalar_addmul_halflimb(R2 + 2 * (iR - lenB + 1), B, lenB - 1, c);
                else
                    _nmod_vec_nored_ll_scalar_addmul(R2 + 2 * (iR - lenB + 1), B, lenB - 1, c);
            }
        }
    }

    for (iR = 0; iR < lenB - 1; iR++)
        R[iR] = n_ll_mod_preinv(R2[2*iR+1], R2[2*iR], mod.n, mod.ninv);

    TMP_END;
}

FLINT_FORCE_INLINE ulong
n_lll_rem_l_fullword_limited(ulong y2, ulong y1, ulong y0, nmod_t mod, ulong alpha2, ulong alpha1)
{
    ulong c1, c0, t1, t0;
    ulong xhi, xlo;

    FLINT_ASSERT(mod.n >= (UWORD(1) << (FLINT_BITS - 1)));
    FLINT_ASSERT(mod.n < (UWORD(1) << (FLINT_BITS - 1)) + (UWORD(1) << (FLINT_BITS / 2 - 2)));

    umul_ppmm(t1, t0, y2, alpha2);
    umul_ppmm(c1, c0, y1, alpha1);
    add_ssaaaa(xhi, xlo, t1, t0, c1, c0);
    add_ssaaaa(xhi, xlo, xhi, xlo, 0, y0);

    NMOD_RED2_FULLWORD(xlo, xhi, xlo, mod);

    return xlo;
}

FLINT_FORCE_INLINE ulong
n_lll_rem_l(ulong y2, ulong y1, ulong y0, nmod_t mod, ulong alpha2, ulong alpha1)
{
    ulong c1, c0, t1, t0;
    ulong xhi, xlo;

    umul_ppmm(t1, t0, y2, alpha2);
    umul_ppmm(c1, c0, y1, alpha1);
    add_ssaaaa(xhi, xlo, t1, t0, c1, c0);
    add_ssaaaa(xhi, xlo, xhi, xlo, 0, y0);

    if (xhi >= mod.n) xhi -= mod.n;
    NMOD_RED2(xlo, xhi, xlo, mod);

    return xlo;
}

static void
_nmod_poly_divrem_basecase_preinv1_3(nn_ptr Q, nn_ptr R,
                                     nn_srcptr A, slong lenA, nn_srcptr B, slong lenB,
                                     ulong invL,
                                     nmod_t mod)
{
    slong iR, i;
    nn_ptr R3, ptrQ = Q - lenB + 1;
    ulong r, c;

    TMP_INIT;
    TMP_START;
    R3 = TMP_ALLOC(3 * lenA * sizeof(ulong));

    for (i = 0; i < lenA; i++)
    {
        R3[3 * i] = A[i];
        R3[3 * i + 1] = 0;
        R3[3 * i + 2] = 0;
    }

    /* Special case for moduli close to 2^63, which are often used
       in multimodular algorithms. */
    int fullword_limited = (mod.norm == 0) &&
            mod.n < (UWORD(1) << (FLINT_BITS - 1)) + (UWORD(1) << (FLINT_BITS / 2 - 2));

    ulong alpha1, alpha2;

    if (fullword_limited)
    {
        alpha1 = -mod.n;               /* 2^FLINT_BITS */
        alpha2 = 4 * alpha1 * alpha1;  /* 2^(2 FLINT_BITS) */
    }
    else
    {
        alpha1 = nmod_set_ui(UWORD(1) << (FLINT_BITS - 1), mod);
        alpha1 = nmod_add(alpha1, alpha1, mod);    /* 2^FLINT_BITS */
        alpha2 = nmod_mul(alpha1, alpha1, mod);    /* 2^(2 FLINT_BITS) */
    }

    for (iR = lenA - 1; iR >= lenB - 1; iR--)
    {
        if (fullword_limited)
        {
            r = n_lll_rem_l_fullword_limited(R3[3 * iR + 2], R3[3 * iR + 1],
                                 R3[3 * iR], mod, alpha2, alpha1);
        }
        else
        {
            r = n_lll_rem_l(R3[3 * iR + 2], R3[3 * iR + 1],
                                 R3[3 * iR], mod, alpha2, alpha1);
        }

        if (r == 0)
        {
            ptrQ[iR] = 0;
        }
        else
        {
            if (invL == 1)
                ptrQ[iR] = r;
            else
                ptrQ[iR] = nmod_mul(r, invL, mod);

            if (lenB > 1)
            {
                c = mod.n - ptrQ[iR];
                _nmod_vec_nored_lll_scalar_addmul(R3 + 3 * (iR - lenB + 1), B, lenB - 1, c);
            }
        }
    }

    if (fullword_limited)
        for (iR = 0; iR < lenB - 1; iR++)
            R[iR] = n_lll_rem_l_fullword_limited(R3[3 * iR + 2], R3[3 * iR + 1],
                                     R3[3 * iR], mod, alpha2, alpha1);
    else
        for (iR = 0; iR < lenB - 1; iR++)
            R[iR] = n_lll_rem_l(R3[3 * iR + 2], R3[3 * iR + 1],
                                     R3[3 * iR], mod, alpha2, alpha1);

    TMP_END;
}

void
_nmod_poly_divrem_basecase_preinv1(nn_ptr Q, nn_ptr R,
                           nn_srcptr A, slong lenA, nn_srcptr B, slong lenB,
                           ulong invB,
                           nmod_t mod)
{
    if (lenA == lenB + 1)
    {
        _nmod_poly_divrem_q1_preinv1(Q, R, A, lenA, B, lenB, invB, mod);
    }
    else if (lenA == lenB)
    {
        _nmod_poly_divrem_q0_preinv1(Q, R, A, B, lenB, invB, mod);
    }
    else if (lenB == 1)
    {
        _nmod_vec_scalar_mul_nmod(Q, A, lenA, invB, mod);
    }
    else
    {

        slong bits = 2 * NMOD_BITS(mod) + FLINT_BIT_COUNT(lenA - lenB + 1);

        if (bits <= FLINT_BITS)
            _nmod_poly_divrem_basecase_preinv1_1(Q, R, A, lenA, B, lenB, invB, mod);
        else if (bits <= 2 * FLINT_BITS)
            _nmod_poly_divrem_basecase_preinv1_2(Q, R, A, lenA, B, lenB, invB, mod);
        else
            _nmod_poly_divrem_basecase_preinv1_3(Q, R, A, lenA, B, lenB, invB, mod);
    }
}

void
_nmod_poly_divrem_basecase(nn_ptr Q, nn_ptr R, nn_srcptr A, slong lenA,
                                  nn_srcptr B, slong lenB, nmod_t mod)
{
    ulong invB;

    invB = (B[lenB - 1] == 1) ? 1 : n_invmod(B[lenB - 1], mod.n);
    _nmod_poly_divrem_basecase_preinv1(Q, R, A, lenA, B, lenB, invB, mod);
}

void nmod_poly_divrem_basecase(nmod_poly_t Q, nmod_poly_t R,
                      const nmod_poly_t A, const nmod_poly_t B)
{
    const slong lenA = A->length, lenB = B->length;
    nmod_poly_t tQ, tR;
    nn_ptr q, r;

    if (lenB == 0)
    {
        if (nmod_poly_modulus(B) == 1)
        {
            nmod_poly_set(Q, A);
            nmod_poly_zero(R);
            return;
        } else
        {
            flint_throw(FLINT_DIVZERO, "Exception (nmod_poly_divrem_basecase). Division by zero.");
        }
    }

    if (lenA < lenB)
    {
        nmod_poly_set(R, A);
        nmod_poly_zero(Q);
        return;
    }

    if (Q == A || Q == B)
    {
        nmod_poly_init2_preinv(tQ, A->mod.n, A->mod.ninv, lenA - lenB + 1);
        q = tQ->coeffs;
    }
    else
    {
        nmod_poly_fit_length(Q, lenA - lenB + 1);
        q = Q->coeffs;
    }

    if (R == B)
    {
        nmod_poly_init2_preinv(tR, B->mod.n, B->mod.ninv, lenB - 1);
        r = tR->coeffs;
    }
    else
    {
        nmod_poly_fit_length(R, lenB - 1);
        r = R->coeffs;
    }

    _nmod_poly_divrem_basecase(q, r, A->coeffs, lenA, B->coeffs, lenB, A->mod);

    if (Q == A || Q == B)
    {
        nmod_poly_swap(Q, tQ);
        nmod_poly_clear(tQ);
    }
    if (R == B)
    {
        nmod_poly_swap(R, tR);
        nmod_poly_clear(tR);
    }

    Q->length = lenA - lenB + 1;
    R->length = lenB - 1;

    _nmod_poly_normalise(R);
}