-
Notifications
You must be signed in to change notification settings - Fork 0
/
mpz_extras.c
670 lines (548 loc) · 16.6 KB
/
mpz_extras.c
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
/*============================================================================
Copyright (C) 2007, William Hart
This file is part of FLINT.
FLINT is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation; either version 2 of the License, or
(at your option) any later version.
FLINT 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 General Public License for more details.
You should have received a copy of the GNU General Public License
along with FLINT; if not, write to the Free Software
Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
===============================================================================*/
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <gmp.h>
#include "mpz_extras.h"
#include "flint.h"
#include "mpn_extras.h"
#include "F_mpn_mul-tuning.h"
#include "memory-manager.h"
#include "longlong_wrapper.h"
#include "longlong.h"
#define DEBUG2 1
#define DEBUG 0
/*
Memory manager to allocate a single mpz_t. It returns a pointer to the mpz_t.
mpz_t's should be released in the order they were allocated.
*/
#define RESALLOC 100 //allocate this many mpz_t's at once to save on overheads
mpz_t** reservoir; // Array of pointers to mpz_t's in the reservoir
unsigned long rescount=0; //Next available mpz_t in reservoir
unsigned long currentalloc=0; //total number of mpz_t's in reservoir
mpz_t* F_mpz_alloc(void)
{
static int initialised = 0;
static mpz_t** tempres;
mpz_t* alloc_d;
//allocate another block of mpz_t's if none are currently allocated, or the reservoir is depleted
if (rescount==currentalloc) // need more limb_memp_t's
{
if (!initialised)
{
reservoir = (mpz_t**)malloc(RESALLOC*sizeof(mpz_t*)); //allocate space for the array of pointers
reservoir[0] = (mpz_t*)malloc(RESALLOC*sizeof(mpz_t)); //allocate space for the mpz_t's
unsigned long i;
for (i=0; i<RESALLOC-1; i++)
{
reservoir[i+1]=reservoir[i]+1; //initialise the array
mpz_init(*reservoir[i]); //initialise the mpz_t's
}
mpz_init(*reservoir[RESALLOC-1]);
rescount=0;
initialised = 1;
currentalloc = RESALLOC;
} else
{
//copy old reservoir into larger one
tempres = reservoir;
reservoir = (mpz_t**)malloc((currentalloc+RESALLOC)*sizeof(mpz_t*));
reservoir[currentalloc] = (mpz_t*)malloc(RESALLOC*sizeof(mpz_t));
memcpy(reservoir,tempres,currentalloc*sizeof(mpz_t*));
unsigned long i;
for (i=currentalloc; i<RESALLOC+currentalloc-1; i++)
{
reservoir[i+1]=reservoir[i]+1; //initialise the array
mpz_init(*reservoir[i]); //initialise the mpz_t's
}
mpz_init(*reservoir[currentalloc+RESALLOC-1]);
currentalloc+=RESALLOC;
//free old reservoir
free(tempres);
}
}
alloc_d = reservoir[rescount];
rescount++;
return alloc_d;
}
/*
Release a mpz_t back into the reservoir
*/
void F_mpz_release(void)
{
rescount--;
}
/*
sets res to a*b modulo p
assumes res is not p
Does not assume a and b are reduced mod p
*/
void F_mpz_mulmod(mpz_t res, mpz_t a, mpz_t b, mpz_t p)
{
mpz_t* temp = F_mpz_alloc();
mpz_fdiv_r(*temp,a,p);
mpz_fdiv_r(res,b,p);
mpz_mul(res,*temp,res);
mpz_fdiv_r(res,res,p);
F_mpz_release();
return;
}
/*
Sets res to a*b modulo p
Does not assume a and b are reduced mod p
*/
unsigned long F_mpz_mulmod_ui(mpz_t res, mpz_t a, mpz_t b, unsigned long p)
{
unsigned long p1, p2, al, bl;
al = mpz_fdiv_r_ui(res, a, p);
bl = mpz_fdiv_r_ui(res, b, p);
umul_ppmm(p2, p1, al, bl);
unsigned long norm, q, r;
if (p2 >= p) p2 %= p;
#if UDIV_NEEDS_NORMALIZATION
count_lead_zeros(norm, p);
udiv_qrnnd(q, r, (p2<<norm) + (p1>>(FLINT_BITS-norm)), p1<<norm, p<<norm);
#else
udiv_qrnnd(q, r, p2, p1, p);
#endif
mpz_set_ui(res, r);
return r;
}
/*
Sets res to the square root of a modulo p for a prime p
Returns 0 if a is not a square modulo p
*/
int F_mpz_sqrtmod(mpz_t res, mpz_t a, mpz_t p)
{
int r,k,m;
mpz_t* p1 = F_mpz_alloc();
mpz_t* two = F_mpz_alloc();
mpz_t* b = F_mpz_alloc();
mpz_t* g = F_mpz_alloc();
mpz_t* bpow = F_mpz_alloc();
mpz_t* gpow = F_mpz_alloc();
mpz_t* mk = F_mpz_alloc();
if (mpz_kronecker(a,p)!=1)
{
mpz_set_ui(res,0);
return 0; //return 0 if a is not a square mod p
}
if ((mpz_cmp_ui(a,0)==0)||(mpz_cmp_ui(a,1)==0))
{
mpz_set(res,a);
return 1;
}
if ((mpz_tstbit(p,0)==1)||(mpz_tstbit(p,1)==1))
{
mpz_add_ui(*p1,p,1);
mpz_fdiv_q_2exp(*p1,*p1,2);
mpz_powm(res,a,*p1,p);
return 1;
}
mpz_set_ui(*two,2);
mpz_sub_ui(*p1,p,1);
r = mpz_remove(*p1,*p1,*two);
mpz_powm(*b,a,*p1,p);
for (k=2; ;k++)
{
if (mpz_ui_kronecker(k,p) == -1) break;
}
mpz_set_ui(*mk,k);
mpz_powm(*g,*mk,*p1,p);
mpz_add_ui(*p1,*p1,1);
mpz_divexact_ui(*p1,*p1,2);
mpz_powm(res,a,*p1,p);
if (!mpz_cmp_ui(*b,1)) return 1;
while (mpz_cmp_ui(*b,1))
{
mpz_set(*bpow,*b);
for (m=1; (m<=r-1) && (mpz_cmp_ui(*bpow,1));m++)
{
mpz_powm_ui(*bpow,*bpow,2,p);
}
mpz_set(*gpow,*g);
int i;
for (i = 1;i<= r-m-1;i++)
{
mpz_powm_ui(*gpow,*gpow,2,p);
};
F_mpz_mulmod(res,res,*gpow,p);
mpz_powm_ui(*gpow,*gpow,2,p);
F_mpz_mulmod(*b,*b,*gpow,p);
mpz_set(*gpow,*g);
r = m;
}
F_mpz_release();F_mpz_release();F_mpz_release();F_mpz_release();
F_mpz_release();F_mpz_release();F_mpz_release();
return 1;
}
/*
Computes the square root of a modulo p^k when given z, the square root mod p^(k-1)
*/
void __sqrtmodpow(mpz_t res, mpz_t z, mpz_t a, mpz_t pk, mpz_t tempsqpow, mpz_t inv)
{
mpz_mul_ui(inv,z,2);
mpz_invert(inv,inv,pk);
mpz_set(tempsqpow,a);
mpz_submul(tempsqpow,z,z);
mpz_fdiv_r(tempsqpow,tempsqpow,pk);
F_mpz_mulmod(tempsqpow,tempsqpow,inv,pk);
mpz_add(tempsqpow,tempsqpow,z);
mpz_set(res,tempsqpow);
return;
}
/*
Computes the square root of a modulo p^k when given z, the square root mod p^{k-1}
*/
void F_mpz_sqrtmodpklift(mpz_t res, mpz_t z, mpz_t a, mpz_t pk)
{
mpz_t* tempsqpow = F_mpz_alloc();
mpz_t* inv = F_mpz_alloc();
__sqrtmodpow(res, z, a, pk, *tempsqpow, *inv);
F_mpz_release();F_mpz_release();
}
/*
computes the square root of a modulo p^k when given the square root modulo p
*/
void F_mpz_sqrtmodptopk(mpz_t res, mpz_t sqrt, mpz_t a, mpz_t p, int k)
{
mpz_t* tempsqpow = F_mpz_alloc();
mpz_t* inv = F_mpz_alloc();
mpz_t* pk = F_mpz_alloc();
mpz_set(res,sqrt);
mpz_set(*pk,p);
int i;
for (i = 2; i<=k; i++)
{
mpz_mul(*pk,*pk,p);
__sqrtmodpow(res,res,a,*pk, *tempsqpow, *inv);
}
F_mpz_release();F_mpz_release();F_mpz_release();
}
/*
Computes the square root of a modulo p^k
Returns 0 if the square root of a does not exist mod p
*/
int F_mpz_sqrtmodpk(mpz_t res, mpz_t a, mpz_t p, int k)
{
int exists;
mpz_t* sqrtmodp = F_mpz_alloc();
exists = F_mpz_sqrtmod(*sqrtmodp,a,p);
F_mpz_sqrtmodptopk(res,*sqrtmodp,a,p,k);
F_mpz_release();
return exists;
}
/*
Find res mod n=n1*n2 such that res = x1 mod n1 and res = x2 mod n2
*/
void F_mpz_CRT(mpz_t res, mpz_t n, mpz_t x1, mpz_t x2, mpz_t n1, mpz_t n2)
{
mpz_t* ntemp = F_mpz_alloc();
mpz_t* restemp = F_mpz_alloc();
mpz_t* chtemp = F_mpz_alloc();
mpz_mul(*ntemp,n1,n2);
mpz_invert(*restemp,n2,n1);
F_mpz_mulmod(*restemp,res,n2,*ntemp);
F_mpz_mulmod(*restemp,*restemp,x1,*ntemp);
mpz_invert(*chtemp,n1,n2);
F_mpz_mulmod(*chtemp,*chtemp,n1,*ntemp);
F_mpz_mulmod(*chtemp,*chtemp,x2,*ntemp);
mpz_add(res,*restemp,*chtemp);
mpz_fdiv_r(res,res,*ntemp);
mpz_set(n,*ntemp);
F_mpz_release();F_mpz_release();F_mpz_release();
return;
}
/*
Compute the Montgomery reduced form of a mod m
Returns n such that m < 2^n (n will be divisible by FLINT_BITS)
Assumes a is already reduced mod m
*/
unsigned long F_mpz_mont_red(mpz_t res, mpz_t a, mpz_t m)
{
unsigned long n = mpz_size(m)*FLINT_BITS;
mpz_mul_2exp(res, a, n);
mpz_mod(res, res, m);
return n;
}
/*
Compute the Montgomery multiplication r = a*b mod m assuming a and b are in
Montgomery form with respect to 2^n where m < 2^n and R is -m mod 2^n
*/
void F_mpz_mont_mul(mpz_t res, mpz_t a, mpz_t b, mpz_t m, mpz_t R, unsigned long n)
{
mpz_t x, s;
mpz_init(x);
mpz_init(s);
mpz_mul(x, a, b);
mpz_fdiv_r_2exp(s, x, n);
mpz_mul(s, s, R);
mpz_fdiv_r_2exp(s, s, n);
mpz_mul(res, s, m);
mpz_add(res, res, x);
mpz_fdiv_q_2exp(res, res, n);
if (mpz_cmp(res, m) >= 0) mpz_sub(res, res, m);
mpz_clear(x);
mpz_clear(s);
}
/*
Compute a^exp mod m using Montgomery reduction
Requires that m is odd and positive and that exp is positive
*/
void F_mpz_expmod_mont(mpz_t res, mpz_t a, mpz_t exp, mpz_t m)
{
unsigned long n;
unsigned long bits = mpz_sizeinbase(exp, 2);
mpz_t aRED;
mpz_t powRED;
mpz_t R;
mpz_t temp;
int flag = 0;
mpz_init(aRED);
mpz_init(powRED);
mpz_init(R);
mpz_init(temp);
n = F_mpz_mont_red(aRED, a, m);
mpz_set_ui(temp, 1);
mpz_mul_2exp(temp, temp, n);
mpz_invert(R, m, temp);
mpz_sub(R, temp, R);
if (mpz_cmp(R, temp) == 0) mpz_sub(R, R, temp);
mpz_set(powRED, aRED);
#ifdef DEBUG
gmp_printf("powRED = %Zd\n", powRED);
#endif
unsigned long i;
for (i = 0; i < bits - 1; i++)
{
if (mpz_tstbit(exp, i))
{
if (flag) F_mpz_mont_mul(res, res, powRED, m, R, n);
else
{
mpz_set(res, powRED);
flag = 1;
}
}
F_mpz_mont_mul(powRED, powRED, powRED, m, R, n);
#ifdef DEBUG
gmp_printf("powRED = %Zd\n", powRED);
#endif
}
if (flag) F_mpz_mont_mul(res, res, powRED, m, R, n);
else mpz_set(res, powRED);
mpz_set_ui(temp, 1);
F_mpz_mont_mul(res, res, temp, m, R, n);
mpz_clear(temp);
mpz_clear(R);
mpz_clear(powRED);
mpz_clear(aRED);
}
void F_mpz_divrem_BZ(mpz_t Q, mpz_t R, mpz_t A, mpz_t B)
{
unsigned long n = mpz_size(B);
unsigned long m = mpz_size(A) - n;
if ((long) m < 0)
{
mpz_set_ui(Q, 0);
mpz_set(R, A);
return;
}
if (m < 64)
{
mpz_fdiv_qr(Q, R, A, B);
return;
}
unsigned long k = m/2;
mpz_t * B0 = F_mpz_alloc();
mpz_t * B1 = F_mpz_alloc();
mpz_t * A0 = F_mpz_alloc();
mpz_t * A1 = F_mpz_alloc();
mpz_t * Q0 = F_mpz_alloc();
mpz_t * Q1 = F_mpz_alloc();
mpz_t * R0 = F_mpz_alloc();
mpz_t * R1 = F_mpz_alloc();
mpz_t * temp = F_mpz_alloc();
mpz_t * temp2 = F_mpz_alloc();
mpz_t * temp3 = F_mpz_alloc();
mpz_fdiv_q_2exp(*B1, B, FLINT_BITS*k);
mpz_fdiv_q_2exp(*A1, A, FLINT_BITS*2*k);
F_mpz_divrem_BZ(*Q1, *R1, *A1, *B1);
mpz_fdiv_r_2exp(*B0, B, FLINT_BITS*k);
mpz_fdiv_r_2exp(*A0, A, FLINT_BITS*2*k);
mpz_mul_2exp(*temp, *R1, FLINT_BITS*2*k);
mpz_add(*temp, *temp, *A0);
mpz_mul_2exp(*temp2, *Q1, FLINT_BITS*k);
mpz_mul(*temp2, *temp2, *B0);
mpz_sub(*temp, *temp, *temp2);
mpz_mul_2exp(*temp2, B, FLINT_BITS*k);
while (mpz_cmp_ui(*temp, 0) < 0)
{
mpz_sub_ui(*Q1, *Q1, 1);
mpz_add(*temp, *temp, *temp2);
}
mpz_fdiv_q_2exp(*temp2, *temp, FLINT_BITS*k);
F_mpz_divrem_BZ(*Q0, *R0, *temp2, *B1);
mpz_fdiv_r_2exp(*temp2, *temp, FLINT_BITS*k);
mpz_mul_2exp(R, *R0, FLINT_BITS*k);
mpz_add(R, R, *temp2);
mpz_submul(R, *Q0, *B0);
while (mpz_cmp_ui(R, 0) < 0)
{
mpz_sub_ui(*Q0, *Q0, 1);
mpz_add(R, R, B);
}
mpz_mul_2exp(Q, *Q1, FLINT_BITS*k);
mpz_add(Q, Q, *Q0);
F_mpz_release(); F_mpz_release(); F_mpz_release(); F_mpz_release();
F_mpz_release(); F_mpz_release(); F_mpz_release(); F_mpz_release();
F_mpz_release(); F_mpz_release(); F_mpz_release();
}
void F_mpz_rem_BZ(mpz_t R, mpz_t A, mpz_t B)
{
unsigned long n = mpz_size(B);
unsigned long m = mpz_size(A) - n;
if ((long) m < 0)
{
mpz_set(R, A);
return;
}
if (m < 64)
{
mpz_fdiv_r(R, A, B);
return;
}
unsigned long k = m/2;
mpz_t * B0 = F_mpz_alloc();
mpz_t * B1 = F_mpz_alloc();
mpz_t * A0 = F_mpz_alloc();
mpz_t * A1 = F_mpz_alloc();
mpz_t * Q0 = F_mpz_alloc();
mpz_t * Q1 = F_mpz_alloc();
mpz_t * R0 = F_mpz_alloc();
mpz_t * R1 = F_mpz_alloc();
mpz_t * temp = F_mpz_alloc();
mpz_t * temp2 = F_mpz_alloc();
mpz_t * temp3 = F_mpz_alloc();
mpz_fdiv_q_2exp(*B1, B, FLINT_BITS*k);
mpz_fdiv_q_2exp(*A1, A, FLINT_BITS*2*k);
F_mpz_divrem_BZ(*Q1, *R1, *A1, *B1);
mpz_fdiv_r_2exp(*B0, B, FLINT_BITS*k);
mpz_fdiv_r_2exp(*A0, A, FLINT_BITS*2*k);
mpz_mul_2exp(*temp, *R1, FLINT_BITS*2*k);
mpz_add(*temp, *temp, *A0);
mpz_mul_2exp(*temp2, *Q1, FLINT_BITS*k);
mpz_mul(*temp2, *temp2, *B0);
mpz_sub(*temp, *temp, *temp2);
mpz_mul_2exp(*temp2, B, FLINT_BITS*k);
while (mpz_cmp_ui(*temp, 0) < 0)
{
mpz_sub_ui(*Q1, *Q1, 1);
mpz_add(*temp, *temp, *temp2);
}
mpz_fdiv_q_2exp(*temp2, *temp, FLINT_BITS*k);
F_mpz_divrem_BZ(*Q0, *R0, *temp2, *B1);
mpz_fdiv_r_2exp(*temp2, *temp, FLINT_BITS*k);
mpz_mul_2exp(R, *R0, FLINT_BITS*k);
mpz_add(R, R, *temp2);
mpz_submul(R, *Q0, *B0);
while (mpz_cmp_ui(R, 0) < 0)
{
mpz_add(R, R, B);
}
F_mpz_release(); F_mpz_release(); F_mpz_release(); F_mpz_release();
F_mpz_release(); F_mpz_release(); F_mpz_release(); F_mpz_release();
F_mpz_release(); F_mpz_release(); F_mpz_release();
}
void F_mpz_mulmod_BZ(mpz_t res, mpz_t a, mpz_t b, mpz_t m)
{
mpz_t * temp = F_mpz_alloc();
mpz_mul(*temp, a, b);
F_mpz_rem_BZ(res, *temp, m);
F_mpz_release();
}
void F_mpz_expmod_BZ(mpz_t res, mpz_t a, mpz_t exp, mpz_t m)
{
unsigned long n;
unsigned long bits = mpz_sizeinbase(exp, 2);
mpz_t aRED;
mpz_t powRED;
mpz_t temp;
int flag = 0;
mpz_init(aRED);
mpz_init(powRED);
mpz_init(temp);
mpz_set(powRED, a);
#if DEBUG
gmp_printf("powRED = %Zd\n", powRED);
#endif
unsigned long i;
for (i = 0; i < bits - 1; i++)
{
if (mpz_tstbit(exp, i))
{
if (flag) F_mpz_mulmod_BZ(res, res, powRED, m);
else
{
mpz_set(res, powRED);
flag = 1;
}
}
F_mpz_mulmod_BZ(powRED, powRED, powRED, m);
#if DEBUG
gmp_printf("powRED = %Zd\n", powRED);
#endif
}
if (flag) F_mpz_mulmod_BZ(res, res, powRED, m);
else mpz_set(res, powRED);
mpz_clear(temp);
mpz_clear(powRED);
mpz_clear(aRED);
}
/*
Large integer multiplication code
*/
void __F_mpz_mul(mpz_t res, mpz_t a, mpz_t b, unsigned long twk)
{
unsigned long sa = mpz_size(a);
unsigned long sb = mpz_size(b);
if (sa+sb > FLINT_FFT_LIMBS_CROSSOVER)
{
unsigned long s1 = (FLINT_BIT_COUNT(a->_mp_d[sa-1]) + FLINT_BIT_COUNT(b->_mp_d[sb-1]) <= FLINT_BITS);
mp_limb_t* output =
(mp_limb_t*) flint_stack_alloc(sa + sb);
__F_mpn_mul(output, a->_mp_d, sa, b->_mp_d, sb, twk);
mpz_import(res, sa+sb-s1, -1, sizeof(mp_limb_t), 0, 0, output);
if (mpz_sgn(res) != mpz_sgn(a)*mpz_sgn(b)) mpz_neg(res,res);
flint_stack_release();
} else mpz_mul(res, a, b);
}
void F_mpz_mul(mpz_t res, mpz_t a, mpz_t b)
{
unsigned long sa = mpz_size(a);
unsigned long sb = mpz_size(b);
if (sa+sb > FLINT_FFT_LIMBS_CROSSOVER)
{
unsigned long s1 = (FLINT_BIT_COUNT(a->_mp_d[sa-1]) + FLINT_BIT_COUNT(b->_mp_d[sb-1]) <= FLINT_BITS);
mp_limb_t* output =
(mp_limb_t*) flint_stack_alloc(sa + sb);
F_mpn_mul(output, a->_mp_d, sa, b->_mp_d, sb);
mpz_import(res, sa+sb-s1, -1, sizeof(mp_limb_t), 0, 0, output);
if (mpz_sgn(res) != mpz_sgn(a)*mpz_sgn(b)) mpz_neg(res,res);
flint_stack_release();
} else mpz_mul(res, a, b);
}