1 /*
2     Copyright (C) 2017 Daniel Schultz
3 
4     This file is part of FLINT.
5 
6     FLINT is free software: you can redistribute it and/or modify it under
7     the terms of the GNU Lesser General Public License (LGPL) as published
8     by the Free Software Foundation; either version 2.1 of the License, or
9     (at your option) any later version.  See <http://www.gnu.org/licenses/>.
10 */
11 
12 #include "fq_nmod_mpoly.h"
13 
fq_nmod_mpoly_repack_bits(fq_nmod_mpoly_t A,const fq_nmod_mpoly_t B,flint_bitcnt_t Abits,const fq_nmod_mpoly_ctx_t ctx)14 int fq_nmod_mpoly_repack_bits(fq_nmod_mpoly_t A, const fq_nmod_mpoly_t B,
15                               flint_bitcnt_t Abits, const fq_nmod_mpoly_ctx_t ctx)
16 {
17     slong i;
18     int success;
19     fq_nmod_mpoly_t T;
20 
21     Abits = mpoly_fix_bits(Abits, ctx->minfo);
22 
23     if (B->bits == Abits || B->length == 0)
24     {
25         fq_nmod_mpoly_set(A, B, ctx);
26         return 1;
27     }
28 
29     /* must use B->alloc because we are going to swap coeff in aliasing case */
30     fq_nmod_mpoly_init3(T, B->alloc, Abits, ctx);
31     success = mpoly_repack_monomials(T->exps, Abits, B->exps, B->bits,
32                                                         B->length, ctx->minfo);
33     if (success)
34     {
35         if (A == B)
36         {
37             fq_nmod_struct * temp = A->coeffs;
38             A->coeffs = T->coeffs;
39             T->coeffs = temp;
40         }
41         else
42         {
43             for (i = 0; i < B->length; i++)
44             {
45                 fq_nmod_set(T->coeffs + i, B->coeffs + i, ctx->fqctx);
46             }
47         }
48         _fq_nmod_mpoly_set_length(T, B->length, ctx);
49         fq_nmod_mpoly_swap(A, T, ctx);
50     }
51 
52     fq_nmod_mpoly_clear(T, ctx);
53 
54     return success;
55 }
56 
fq_nmod_mpoly_repack_bits_inplace(fq_nmod_mpoly_t A,flint_bitcnt_t Abits,const fq_nmod_mpoly_ctx_t ctx)57 int fq_nmod_mpoly_repack_bits_inplace(
58     fq_nmod_mpoly_t A,
59     flint_bitcnt_t Abits,
60     const fq_nmod_mpoly_ctx_t ctx)
61 {
62     int success;
63     ulong * texps;
64     slong N = mpoly_words_per_exp(Abits, ctx->minfo);
65 
66     if (A->bits == Abits)
67     {
68         return 1;
69     }
70 
71     texps = (ulong *) flint_malloc(A->alloc*N*sizeof(ulong));
72     success = mpoly_repack_monomials(texps, Abits,
73                                       A->exps, A->bits, A->length, ctx->minfo);
74     if (success)
75     {
76         ulong * t = A->exps;
77         A->exps = texps;
78         texps = t;
79         A->bits = Abits;
80     }
81     flint_free(texps);
82     return success;
83 }
84