-
Notifications
You must be signed in to change notification settings - Fork 0
/
fmpz_poly.c
11297 lines (9497 loc) · 315 KB
/
fmpz_poly.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
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
/*============================================================================
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
===============================================================================*/
/****************************************************************************
fmpz_poly.c: Polynomials over Z, implemented as contiguous block of fmpz_t's
Copyright (C) 2007, William Hart and David Harvey
Copyright (C) 2008, William Hart
Copyright (C) 2009, William Hart and Burcin Erocal
Copyright (C) 2010, William Hart
*****************************************************************************/
#include <stdint.h>
#include <string.h>
#include <math.h>
#include <time.h>
#include "mpz_poly.h"
#include "flint.h"
#include "fmpz.h"
#include "fmpz_poly.h"
#include "mpn_extras.h"
#include "longlong_wrapper.h"
#include "longlong.h"
#include "memory-manager.h"
#include "ZmodF_poly.h"
#include "long_extras.h"
#include "zmod_poly.h"
#include "zn_poly/src/zn_poly.h"
#include "F_mpz.h"
#include "F_mpz_poly.h"
/****************************************************************************
Conversion Routines
****************************************************************************/
/*
Convert length coefficients of an fmpz_poly_t to an
already initialised ZmodF_poly_t. Each coefficient will
be represented mod p = 2^Bn+1 where n is given by the field
n of the ZmodF_poly_t. Coefficients will be assumed to
be in the range [-p/2, p/2].
Assumes 0 < length <= poly_fmpz->length
*/
long fmpz_poly_to_ZmodF_poly(ZmodF_poly_t poly_f, const fmpz_poly_t poly_fmpz,
const unsigned long length)
{
unsigned long size_f = poly_f->n + 1;
unsigned long size_m = poly_fmpz->limbs+1;
mp_limb_t * coeffs_m = poly_fmpz->coeffs;
ZmodF_t * coeffs_f = poly_f->coeffs;
unsigned long mask = -1L;
long bits = 0;
long limbs = 0;
long sign = 1;
long size_j;
unsigned long i, j;
for (i = 0, j = 0; i < length; i++, j += size_m)
{
size_j = coeffs_m[j];
if ((long) size_j < 0) sign = -1L;
if (ABS(size_j) > limbs + 1)
{
limbs = ABS(size_j) - 1;
bits = FLINT_BIT_COUNT(coeffs_m[j+ABS(size_j)]);
if (bits == FLINT_BITS) mask = 0L;
else mask = -1L - ((1L<<bits)-1);
} else if (ABS(size_j) == limbs + 1)
{
if (coeffs_m[j+ABS(size_j)] & mask)
{
bits = FLINT_BIT_COUNT(coeffs_m[j+ABS(size_j)]);
if (bits == FLINT_BITS) mask = 0L;
else mask = -1L - ((1L<<bits)-1);
}
}
if (size_j < 0)
{
F_mpn_negate(coeffs_f[i], coeffs_m + j + 1, ABS(size_j));
F_mpn_set(coeffs_f[i] + ABS(size_j), size_f - ABS(size_j));
} else
{
F_mpn_copy(coeffs_f[i], coeffs_m + j + 1, ABS(size_j));
F_mpn_clear(coeffs_f[i] + ABS(size_j), size_f - ABS(size_j));
}
}
poly_f->length = length;
return sign*(FLINT_BITS*limbs+bits);
}
/*
Convert a ZmodF_poly_t to an fmpz_poly_t. Coefficients will
be taken to be in the range [-p/2, p/2] where p = 2^nB+1.
Assumes 0 < poly_f->length
*/
void ZmodF_poly_to_fmpz_poly(fmpz_poly_t poly_fmpz, const ZmodF_poly_t poly_f, const long sign)
{
unsigned long n = poly_f->n;
unsigned long size_m = poly_fmpz->limbs+1;
unsigned long limbs = FLINT_MIN(n, size_m-1);
mp_limb_t * coeffs_m = poly_fmpz->coeffs;
ZmodF_t * coeffs_f = poly_f->coeffs;
if (sign)
{
unsigned long i, j;
for (i = 0, j = 0; i < poly_f->length; i++, j += size_m)
{
ZmodF_normalise(coeffs_f[i], n);
if (coeffs_f[i][n-1]>>(FLINT_BITS-1) || coeffs_f[i][n])
{
F_mpn_negate(coeffs_m + j + 1, coeffs_f[i], limbs);
mpn_add_1(coeffs_m + j + 1, coeffs_m + j + 1, limbs, 1L);
coeffs_m[j] = -limbs;
NORM(coeffs_m + j);
} else
{
F_mpn_copy(coeffs_m + j + 1, coeffs_f[i], limbs);
coeffs_m[j] = limbs;
NORM(coeffs_m + j);
}
}
} else
{
unsigned long i, j;
for (i = 0, j = 0; i < poly_f->length; i++, j += size_m)
{
ZmodF_normalise(coeffs_f[i], n);
F_mpn_copy(coeffs_m + j + 1, coeffs_f[i], limbs);
coeffs_m[j] = limbs;
NORM(coeffs_m + j);
}
}
poly_fmpz->length = poly_f->length;
_fmpz_poly_normalise(poly_fmpz);
}
static inline long __get_next_coeff(const mp_limb_t * coeff_m, long * borrow, long * coeff, const long mask, const long negate)
{
if ((long) coeff_m[0] == 0) *coeff = -*borrow;
else if ((((long) coeff_m[0]) ^ negate) >= 0L) *coeff = coeff_m[1] - *borrow;
else *coeff = (-coeff_m[1] - *borrow);
*borrow = 0UL;
if (*coeff < 0)
{
*borrow = 1UL;
}
*coeff&=mask;
return *coeff;
}
static inline long __get_next_coeff_unsigned(const mp_limb_t * coeff_m, long * coeff)
{
if ((long) coeff_m[0] == 0) *coeff = 0;
else *coeff = coeff_m[1];
return *coeff;
}
void fmpz_poly_bit_pack(fmpz_t array, const fmpz_poly_t poly_fmpz,
const unsigned long length, const long bitwidth,
const long negate)
{
ulong i, k, skip;
mp_limb_t * coeff_m = poly_fmpz->coeffs;
mp_limb_t * last_point;
ulong temp;
half_ulong lower;
long coeff;
long borrow;
mp_limb_t extend;
long bits = bitwidth;
int sign = (bits < 0);
if (sign) bits = ABS(bits);
ulong coeffs_per_limb = FLINT_BITS/bits;
const ulong mask = (1UL<<bits)-1;
k = 0; skip = 0;
coeff = 0; borrow = 0L; temp = 0;
ulong size = poly_fmpz->limbs + 1;
last_point = coeff_m + size*length;
while (coeff_m < last_point)
{
if ((ulong) coeff_m & 7 == 0) FLINT_PREFETCH(coeff_m, 64);
// k is guaranteed to be less than FLINT_BITS at this point
while ((k < HALF_FLINT_BITS) && (coeff_m < last_point))
{
if (sign) temp+=(__get_next_coeff(coeff_m, &borrow, &coeff, mask, negate) << k);
else temp+=(__get_next_coeff_unsigned(coeff_m, &coeff) << k);
coeff_m+=size; k+=bits;
}
// k may exceed FLINT_BITS at this point but is less than 96
if (k > FLINT_BITS)
{
// if k > FLINT_BITS write out a whole limb and read in remaining bits of coeff
array[skip] = temp;
skip++;
temp = (coeff>>(bits+FLINT_BITS-k));
k = (k-FLINT_BITS);
// k < HALF_FLINT_BITS
} else
{
// k <= FLINT_BITS
if (k >= HALF_FLINT_BITS)
{
// if k >= HALF_FLINT_BITS store bottom HALF_FLINT_BITS bits
lower = (half_ulong)temp;
k-=HALF_FLINT_BITS;
temp>>=HALF_FLINT_BITS;
// k is now <= HALF_FLINT_BITS
while ((k<HALF_FLINT_BITS) && (coeff_m < last_point))
{
if (sign) temp+=(__get_next_coeff(coeff_m, &borrow, &coeff, mask, negate) << k);
else temp+=(__get_next_coeff_unsigned(coeff_m, &coeff) << k);
coeff_m+=size; k+=bits;
}
// k may again exceed FLINT_BITS bits but is less than 96
if (k>FLINT_BITS)
{
// if k > FLINT_BITS, write out bottom HALF_FLINT_BITS bits (along with HALF_FLINT_BITS bits from lower)
// read remaining bits from coeff and reduce k by HALF_FLINT_BITS
array[skip] = (temp<<HALF_FLINT_BITS) + (ulong) lower;
skip++;
temp>>=HALF_FLINT_BITS;
temp+=((coeff>>(bits+FLINT_BITS-k))<<HALF_FLINT_BITS);
k = (k-HALF_FLINT_BITS);
// k < FLINT_BITS and we are ready to read next coefficient if there is one
} else if (k >= HALF_FLINT_BITS)
{
// k <= FLINT_BITS
// if k >= HALF_FLINT_BITS write out bottom HALF_FLINT_BITS bits (along with lower)
// and reduce k by HALF_FLINT_BITS
k-=HALF_FLINT_BITS;
array[skip] = (temp<<HALF_FLINT_BITS)+lower;
temp>>=HALF_FLINT_BITS;
skip++;
// k is now less than or equal to HALF_FLINT_BITS and we are now ready to read
// the next coefficient if there is one
} else
{
// k < HALF_FLINT_BITS
// there isn't enough to write out a whole FLINT_BITS bits, so put it all
// together in temp
temp = (temp<<HALF_FLINT_BITS)+lower;
k += HALF_FLINT_BITS;
// k is now guaranteed to be less than FLINT_BITS and we are ready for the
// next coefficient if there is one
} // else
} // if
} // else
} // while
// write out final coefficient/limb
array[skip] = temp;
}
void fmpz_poly_bit_unpack(fmpz_poly_t poly_fmpz, const mp_limb_t * array,
const ulong length, const ulong bits)
{
ulong k, skip;
ulong temp2;
ulong temp;
ulong full_limb;
ulong carry;
const ulong mask = (1UL<<bits)-1;
const ulong sign_mask = (1UL<<(bits-1));
ulong s;
fmpz_poly_fit_length(poly_fmpz, length + 1);
fmpz_poly_fit_limbs(poly_fmpz, 1);
mp_limb_t * coeff_m = poly_fmpz->coeffs;
mp_limb_t * last_point;
ulong size_m = poly_fmpz->limbs + 1;
k=0; skip=0; carry = 0UL; temp2 = 0;
last_point = coeff_m + size_m*length;
while (coeff_m < last_point)
{
// read in a full limb
full_limb = array[skip];
temp2 += l_shift(full_limb,k);
s = FLINT_BITS-k;
k+=s;
while ((k >= bits) && (coeff_m < last_point))
{
if (!(temp2 & sign_mask))
{
fmpz_add_ui_inplace(coeff_m, (temp2 & mask) + carry);
carry = 0UL;
}
else
{
temp = ((-temp2) & mask) - carry;
fmpz_sub_ui_inplace(coeff_m, temp);
carry = 1UL;
}
coeff_m += size_m;
temp2>>=bits;
k-=bits;
}
// k is now less than bits
// read in remainder of full_limb
temp2 += l_shift(r_shift(full_limb, s), k);
k+=(FLINT_BITS - s);
while ((k >= bits)&&(coeff_m < last_point))
{
if (!(temp2 & sign_mask))
{
fmpz_add_ui_inplace(coeff_m, (temp2 & mask) + carry);
carry = 0UL;
}
else
{
temp = ((-temp2) & mask) - carry;
fmpz_sub_ui_inplace(coeff_m, temp);
carry = 1UL;
}
coeff_m += size_m;
temp2>>=bits;
k-=bits;
}
// k is now less than bits
skip++;
}
poly_fmpz->length = length;
_fmpz_poly_normalise(poly_fmpz);
}
void fmpz_poly_bit_unpack_unsigned(fmpz_poly_t poly_fmpz, const mp_limb_t * array,
const unsigned long length, const unsigned long bits)
{
unsigned long k, l, skip;
unsigned long temp2;
unsigned long temp;
unsigned long full_limb;
const unsigned long mask = (1UL<<bits)-1;
unsigned long s;
fmpz_t coeff_m = poly_fmpz->coeffs;
fmpz_t next_point;
unsigned long size_m = poly_fmpz->limbs+1;
k=0; skip=0; temp2 = 0;
next_point = coeff_m + size_m*length;
while (coeff_m < next_point)
{
if (skip & 7 == 0) FLINT_PREFETCH(array + skip, 64);
// read in a full limb
full_limb = array[skip];
temp2 += l_shift(full_limb,k);
s = FLINT_BITS - k;
k += s;
while ((k >= bits) && (coeff_m < next_point))
{
__fmpz_add_ui_inplace(coeff_m, (temp2 & mask));
coeff_m += size_m;
temp2>>=bits;
k-=bits;
}
// k is now less than bits
// read in remainder of full_limb
temp2 += l_shift(r_shift(full_limb,s),k);
k += (FLINT_BITS-s);
while ((k >= bits)&&(coeff_m < next_point))
{
__fmpz_add_ui_inplace(coeff_m, temp2 & mask);
coeff_m += size_m;
temp2 >>= bits;
l++;
k -= bits;
}
// k is now less than bits
skip++;
}
poly_fmpz->length = length;
_fmpz_poly_normalise(poly_fmpz);
}
void fmpz_poly_limb_pack(mp_limb_t * array, const fmpz_poly_t poly_fmpz,
const unsigned long length, const long limbs)
{
unsigned long size_m = poly_fmpz->limbs + 1;
long size_j;
fmpz_t coeffs_m = poly_fmpz->coeffs;
long carry = 0;
unsigned long i, j, k;
for (i = 0, j = 0, k = 0; i < length; i++, j += size_m, k += limbs)
{
size_j = (long) coeffs_m[j];
if (size_j < 0)
{
F_mpn_negate(array + k, coeffs_m + j + 1, ABS(size_j));
F_mpn_set(array + k + ABS(size_j), limbs - ABS(size_j));
if (carry) mpn_sub_1(array + k, array + k, limbs, 1L);
carry = 1L;
} else if (size_j > 0)
{
F_mpn_copy(array + k, coeffs_m + j + 1, ABS(size_j));
F_mpn_clear(array + k + ABS(size_j), limbs - ABS(size_j));
if (carry) mpn_sub_1(array + k, array + k, limbs, 1L);
carry = 0L;
} else
{
if (carry)
{
F_mpn_set(array + k, limbs);
carry = 1L;
} else
{
F_mpn_clear(array + k, limbs);
carry = 0L;
}
}
}
}
void fmpz_poly_limb_pack_neg(mp_limb_t * array, const fmpz_poly_t poly_fmpz,
const unsigned long length, const long limbs)
{
unsigned long size_m = poly_fmpz->limbs + 1;
long size_j;
fmpz_t coeffs_m = poly_fmpz->coeffs;
long carry = 0;
unsigned long i, j, k;
for (i = 0, j = 0, k = 0; i < length; i++, j += size_m, k += limbs)
{
size_j = (long) coeffs_m[j];
if (size_j > 0)
{
F_mpn_negate(array + k, coeffs_m + j + 1, ABS(size_j));
F_mpn_set(array + k + ABS(size_j), limbs - ABS(size_j));
if (carry) mpn_sub_1(array + k, array + k, limbs, 1L);
carry = 1L;
} else if (size_j < 0)
{
F_mpn_copy(array + k, coeffs_m + j + 1, ABS(size_j));
F_mpn_clear(array + k + ABS(size_j), limbs - ABS(size_j));
if (carry) mpn_sub_1(array + k, array + k, limbs, 1L);
carry = 0L;
} else
{
if (carry)
{
F_mpn_set(array + k, limbs);
carry = 1L;
} else
{
F_mpn_clear(array + k, limbs);
carry = 0L;
}
}
}
}
void fmpz_poly_limb_unpack(fmpz_poly_t poly_fmpz, mp_limb_t * array,
const unsigned long length, const unsigned long limbs)
{
unsigned long carry = 0L;
fmpz_poly_fit_length(poly_fmpz, length + 1);
fmpz_poly_fit_limbs(poly_fmpz, limbs);
fmpz_t coeffs_m = poly_fmpz->coeffs;
unsigned long size_m = poly_fmpz->limbs + 1;
ulong i, j, k;
for (i = 0, j = 0, k = 0; i < length; i++, j += size_m, k += limbs)
{
if (array[k+limbs-1]>>(FLINT_BITS-1))
{
F_mpn_negate(coeffs_m + j + 1, array + k, limbs);
coeffs_m[j] = -limbs;
NORM(coeffs_m + j);
if (carry) mpn_sub_1(coeffs_m + j + 1, coeffs_m + j + 1, -coeffs_m[j], 1L);
if (!coeffs_m[j - coeffs_m[j]]) coeffs_m[j]++;
carry = 1L;
} else
{
F_mpn_copy(coeffs_m + j + 1, array + k, limbs);
coeffs_m[j] = limbs;
NORM(coeffs_m + j);
mp_limb_t cry = 0L;
if (carry)
{
if (coeffs_m[j]) cry = mpn_add_1(coeffs_m + j + 1, coeffs_m + j + 1, coeffs_m[j], 1L);
else cry = 1L;
}
if (cry)
{
coeffs_m[j + coeffs_m[j] + 1] = cry;
coeffs_m[j]++;
}
carry = 0L;
}
}
if (carry) // check if there was a carried 1 from the final mpn_add_1
{
coeffs_m[j+1] = 1;
coeffs_m[j] = 1;
poly_fmpz->length = length + 1;
} else
{
poly_fmpz->length = length;
_fmpz_poly_normalise(poly_fmpz);
}
}
void fmpz_poly_limb_pack_1(mp_limb_t * array, const fmpz_poly_t poly_fmpz)
{
unsigned long size_m = poly_fmpz->limbs + 1;
long size_j;
fmpz_t coeffs_m = poly_fmpz->coeffs;
unsigned long length = poly_fmpz->length;
long carry = 0;
unsigned long i, j, k;
for (i = 0, j = 0, k = 0; i < length; i++, j += size_m, k++)
{
size_j = (long) coeffs_m[j];
if (size_j < 0L)
{
if (carry) array[k] = -coeffs_m[j + 1] - 1;
else array[k] = -coeffs_m[j + 1];
carry = 1L;
} else if (size_j > 0L)
{
if (carry) array[k] = coeffs_m[j + 1] - 1;
else array[k] = coeffs_m[j + 1];
carry = 0L;
} else
{
if (carry)
{
array[k] = -1L;
carry = 1L;
} else
{
array[k] = 0L;
carry = 0L;
}
}
}
}
void fmpz_poly_limb_pack_neg_1(mp_limb_t * array, const fmpz_poly_t poly_fmpz)
{
unsigned long size_m = poly_fmpz->limbs + 1;
long size_j;
fmpz_t coeffs_m = poly_fmpz->coeffs;
unsigned long length = poly_fmpz->length;
long carry = 0;
unsigned long i, j, k;
for (i = 0, j = 0, k = 0; i < length; i++, j += size_m, k++)
{
size_j = (long) coeffs_m[j];
if (size_j > 0L)
{
if (carry) array[k] = -coeffs_m[j + 1] - 1;
else array[k] = -coeffs_m[j + 1];
carry = 1L;
} else if (size_j < 0L)
{
if (carry) array[k] = coeffs_m[j + 1] - 1;
else array[k] = coeffs_m[j + 1];
carry = 0L;
} else
{
if (carry)
{
array[k] = -1L;
carry = 1L;
} else
{
array[k] = 0L;
carry = 0L;
}
}
}
}
void fmpz_poly_limb_unpack_1(fmpz_poly_t poly_fmpz, const mp_limb_t * array,
const unsigned long length)
{
fmpz_poly_fit_length(poly_fmpz, length + 1);
fmpz_poly_fit_limbs(poly_fmpz, 1);
unsigned long size_m = poly_fmpz->limbs + 1;
fmpz_t coeffs_m = poly_fmpz->coeffs;
unsigned long carry = 0L;
ulong i, j, k;
for (i = 0, j = 0, k = 0; i < length; i++, j += size_m, k++)
{
if ((long) array[k] < 0L)
{
coeffs_m[j+1] = -array[k] - carry;
if (coeffs_m[j+1] == 0L)
coeffs_m[j] = 0L;
else
coeffs_m[j] = -1;
carry = 1L;
} else if (array[k] == 0L)
{
if (carry)
{
coeffs_m[j+1] = 1L;
coeffs_m[j] = 1L;
carry = 0L;
} else
{
coeffs_m[j] = 0L;
carry = 0L;
}
} else
{
coeffs_m[j+1] = array[k] + carry;
coeffs_m[j] = 1;
NORM(coeffs_m + j);
carry = 0L;
}
}
if (carry)
{
coeffs_m[j+1] = 1;
coeffs_m[j] = 1;
poly_fmpz->length = length + 1;
} else
{
poly_fmpz->length = length;
_fmpz_poly_normalise(poly_fmpz);
}
}
void fmpz_poly_limb_unpack_unsigned(fmpz_poly_t poly_fmpz, const mp_limb_t * array,
const unsigned long length, const unsigned long limbs)
{
unsigned long size_m = poly_fmpz->limbs + 1;
fmpz_t coeffs_m = poly_fmpz->coeffs;
unsigned long i, j, k;
for (i = 0, j = 0, k = 0; i < length; i++, j += size_m, k += limbs)
{
F_mpn_copy(coeffs_m + j + 1, array + k, limbs);
coeffs_m[j] = limbs;
NORM(coeffs_m + j);
}
_fmpz_poly_normalise(poly_fmpz);
}
void __fmpz_poly_write_next_limb(fmpz_t array, unsigned long * temp, unsigned long * offset_limb,
const unsigned long next_limb, const unsigned long shift_1, const unsigned long shift_2)
{
*temp += l_shift(next_limb, shift_1);
array[*offset_limb] = *temp + ((l_shift(1UL,shift_1)-1)&array[*offset_limb]);
(*offset_limb)++;
*temp = r_shift(next_limb, shift_2);
}
void __fmpz_poly_write_whole_limb(fmpz_t array, unsigned long * temp, unsigned long * offset_limb,
const unsigned long next_limb, const unsigned long shift_1, const unsigned long shift_2)
{
*temp += l_shift(next_limb,shift_1);
array[*offset_limb] = *temp;
(*offset_limb)++;
*temp = r_shift(next_limb,shift_2);
}
void fmpz_poly_byte_pack(mp_limb_t * array, const fmpz_poly_t poly_fmpz,
const unsigned long length, const unsigned long coeff_bytes,
const long negate)
{
unsigned long size_m = poly_fmpz->limbs + 1;
fmpz_t coeff_m = poly_fmpz->coeffs;
const unsigned long limbs_per_coeff = (coeff_bytes>>FLINT_LG_BYTES_PER_LIMB);
const unsigned long extra_bytes_per_coeff = coeff_bytes
- (limbs_per_coeff<<FLINT_LG_BYTES_PER_LIMB);
// Start limb of the current coefficient within array
unsigned long coeff_limb;
// Additional offset in bytes of current coefficient within array
unsigned long coeff_byte;
// Where we are up to in the current coefficient: limbs + bytes
unsigned long offset_limb;
// Next limb to be written to bytes
unsigned long next_limb;
unsigned long temp;
unsigned long extend;
unsigned long shift_1, shift_2;
fmpz_t scratch = (fmpz_t) flint_stack_alloc(size_m);
fmpz_t co;
// when a coefficient is negative, we need to borrow from the next coefficient
int borrow;
unsigned long j;
coeff_limb = 0;
coeff_byte = 0;
offset_limb = 0;
temp = 0;
borrow = 0;
fmpz_t next_point = coeff_m + size_m*length;
while (coeff_m < next_point)
{
// compute shifts to be used
shift_1 = coeff_byte << 3;
shift_2 = FLINT_BITS - shift_1;
/* Coefficient is negative after borrow */
if (((negate > 0L) && ((long) coeff_m[0] - (long) borrow < 0L)) || ((negate < 0L) && ((long) -coeff_m[0] - (long) borrow < 0L)))
{
// mpz_t's store the absolute value only, so add 1 then complement
if (borrow)
{
if (coeff_m[0] == 0) next_limb = ~0L;
else next_limb = ~coeff_m[1];
co = coeff_m;
} else
{
if (negate > 0L) fmpz_add_ui(scratch, coeff_m, 1L);
else fmpz_sub_ui(scratch, coeff_m, 1L);
if (scratch[0] == 0) next_limb = ~0L;
else next_limb = ~scratch[1];
co = scratch;
}
// deal with first limb of coefficient
if (limbs_per_coeff == 0)
{
if (coeff_m == next_point - size_m)
{
__fmpz_poly_write_next_limb(array, &temp, &offset_limb, next_limb, shift_1, shift_2);
temp += l_shift(-1UL, shift_1);
array[offset_limb] = temp;
offset_limb++;
extend = -1L;
} else
{
next_limb &= ((1UL<<(extra_bytes_per_coeff<<3))-1);
__fmpz_poly_write_next_limb(array, &temp, &offset_limb, next_limb, shift_1, shift_2);
array[offset_limb] = temp;
}
} else
{
__fmpz_poly_write_next_limb(array, &temp, &offset_limb, next_limb, shift_1, shift_2);
// deal with remaining limbs
for (j = 1; j < ABS(co[0]); j++)
{
next_limb = ~co[j+1];
__fmpz_poly_write_whole_limb(array, &temp, &offset_limb, next_limb, shift_1, shift_2);
}
// write remaining part of coefficient and fill
// remainder of coeff_bytes with binary 1's
if ((offset_limb<<FLINT_LG_BYTES_PER_LIMB) < ((coeff_limb +
limbs_per_coeff)<<FLINT_LG_BYTES_PER_LIMB)+extra_bytes_per_coeff + coeff_byte)
{
temp += l_shift(-1UL,shift_1);
array[offset_limb] = temp;
offset_limb++;
}
for ( ; offset_limb < coeff_limb + limbs_per_coeff; offset_limb++)
{
array[offset_limb] = -1UL;
}
while ((offset_limb<<FLINT_LG_BYTES_PER_LIMB) < ((coeff_limb +
limbs_per_coeff)<<FLINT_LG_BYTES_PER_LIMB)+extra_bytes_per_coeff + coeff_byte)
{
array[offset_limb] = -1UL;
offset_limb++;
}
extend = -1L;
}
temp = 0;
borrow = 1;
}
else
{
if (borrow)
{
if (negate > 0L) fmpz_sub_ui(scratch, coeff_m, 1L);
else fmpz_add_ui(scratch, coeff_m, 1L);
co = scratch;
} else
{
co = coeff_m;
}
/* Coefficient is positive after borrow */
if (co[0] != 0L)
{
// deal with first limb of coefficient
next_limb = co[1];
__fmpz_poly_write_next_limb(array, &temp, &offset_limb, next_limb, shift_1, shift_2);
if (shift_2 == FLINT_BITS) temp = 0;
// deal with remaining limbs
for (j = 1; j < ABS(co[0]); j++)
{
next_limb = co[j+1];
__fmpz_poly_write_whole_limb(array, &temp, &offset_limb, next_limb, shift_1, shift_2);
}
// write remaining part of coefficient
//array[offset_limb] = temp;
//offset_limb++;
for (; offset_limb < coeff_limb + limbs_per_coeff; offset_limb++)
{
array[offset_limb] = temp;
temp = 0;
}
while ((offset_limb<<FLINT_LG_BYTES_PER_LIMB) < ((coeff_limb +
limbs_per_coeff)<<FLINT_LG_BYTES_PER_LIMB)+extra_bytes_per_coeff + coeff_byte)
{
array[offset_limb] = temp;
temp = 0;
offset_limb++;
}
extend = 0L;
temp = 0;
borrow = 0;
}
/* Coefficient is zero after borrow */
else
{
temp = ((l_shift(1UL,shift_1) - 1) & array[offset_limb]);
for ( ; offset_limb < coeff_limb + limbs_per_coeff; offset_limb++)
{
array[offset_limb] = temp;
temp = 0UL;
}
while ((offset_limb<<FLINT_LG_BYTES_PER_LIMB) < ((coeff_limb +
limbs_per_coeff)<<FLINT_LG_BYTES_PER_LIMB) + extra_bytes_per_coeff + coeff_byte)
{
array[offset_limb] = temp;
temp = 0UL;
offset_limb++;
}
extend = 0L;
temp = 0;
borrow = 0;
}
}
// update information for next coefficient
coeff_limb += limbs_per_coeff;
coeff_byte += extra_bytes_per_coeff;
if (coeff_byte > FLINT_BYTES_PER_LIMB)
{
coeff_byte -= FLINT_BYTES_PER_LIMB;
coeff_limb++;
}
offset_limb = coeff_limb;
coeff_m += size_m;
}
flint_stack_release();
}
static inline void __fmpz_poly_unpack_bytes(mp_limb_t * output, const mp_limb_t * array,
const unsigned long limb_start, const unsigned long byte_start,
const unsigned long num_bytes)
{
const unsigned long limbs_to_extract = (num_bytes>>FLINT_LG_BYTES_PER_LIMB);
const unsigned long extra_bytes_to_extract = num_bytes
- (limbs_to_extract<<FLINT_LG_BYTES_PER_LIMB);
unsigned long next_limb;
unsigned long temp = 0;
// the limb we are up to in the array and output respectively
unsigned long coeff_limb = limb_start;
unsigned long output_limb = 0;
unsigned long shift_1, shift_2;
shift_1 = (byte_start<<3);
shift_2 = FLINT_BITS - shift_1;
temp = array[coeff_limb];
coeff_limb++;
while (output_limb < limbs_to_extract)
{
next_limb = r_shift(temp,shift_1);
temp = array[coeff_limb];
coeff_limb++;
next_limb += l_shift(temp,shift_2);
output[output_limb] = next_limb;
output_limb++;
}
if (extra_bytes_to_extract <= FLINT_BYTES_PER_LIMB - byte_start)
{
next_limb = r_shift(temp,shift_1);
output[output_limb] = next_limb&((1UL<<(extra_bytes_to_extract<<3))-1);
} else
{
next_limb = r_shift(temp,shift_1);
temp = array[coeff_limb];
next_limb += l_shift(temp,shift_2);
output[output_limb] = next_limb&((1UL<<(extra_bytes_to_extract<<3))-1);
}
}
static inline unsigned long __fmpz_poly_unpack_signed_bytes(mp_limb_t * output,
const mp_limb_t * array, const unsigned long limb_start,
const unsigned long byte_start, const unsigned long num_bytes)
{
const unsigned long limbs_to_extract = (num_bytes>>FLINT_LG_BYTES_PER_LIMB);
const unsigned long extra_bytes_to_extract = num_bytes
- (limbs_to_extract<<FLINT_LG_BYTES_PER_LIMB);
unsigned long next_limb;
unsigned long temp = 0;
// the limb we are up to in the array and output respectively
unsigned long coeff_limb = limb_start;
unsigned long output_limb = 0;
unsigned long shift_1, shift_2;
shift_1 = (byte_start<<3);
shift_2 = FLINT_BITS - shift_1;
unsigned long sign;
if (byte_start + extra_bytes_to_extract > FLINT_BYTES_PER_LIMB)
{
sign = array[limb_start+limbs_to_extract+1]&(1UL<<(((byte_start
+ extra_bytes_to_extract - FLINT_BYTES_PER_LIMB)<<3)-1));
} else if (byte_start + extra_bytes_to_extract == FLINT_BYTES_PER_LIMB)
{
sign = array[limb_start+limbs_to_extract]&(1UL<<(FLINT_BITS-1));
} else if (byte_start + extra_bytes_to_extract == 0)
{
sign = array[limb_start+limbs_to_extract-1]&(1UL<<(FLINT_BITS-1));
} else
{
sign = array[limb_start+limbs_to_extract]&(1UL<<(((byte_start
+ extra_bytes_to_extract)<<3)-1));
}