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 <https://www.gnu.org/licenses/>.
10 */
11 
12 #include "mpoly.h"
13 
14 /* !!! this file DOES need to change with new orderings */
15 
16 /*
17     compute number of bits required to store user_exp in packed format
18     the returned number of bits includes space for a zero'd signed bit
19 */
mpoly_exp_bits_required_ui(const ulong * user_exp,const mpoly_ctx_t mctx)20 flint_bitcnt_t mpoly_exp_bits_required_ui(const ulong * user_exp,
21                                                         const mpoly_ctx_t mctx)
22 {
23     slong i, nfields = mctx->nfields;
24     ulong max;
25 
26     max = user_exp[0];
27     if (mctx->deg)
28     {
29         for (i = 1; i < nfields - 1; i++)
30         {
31             max += user_exp[i];
32             if (max < user_exp[i])
33                 return 2*FLINT_BITS;
34         }
35     }
36     else
37     {
38         for (i = 1; i < nfields; i++)
39         {
40             max |= user_exp[i];
41         }
42     }
43 
44     return 1 + FLINT_BIT_COUNT(max);
45 }
46 
47 /*
48     compute number of bits required to store user_exp in packed format
49     the returned number of bits includes space for a zero'd signed bit
50 */
mpoly_exp_bits_required_ffmpz(const fmpz * user_exp,const mpoly_ctx_t mctx)51 flint_bitcnt_t mpoly_exp_bits_required_ffmpz(const fmpz * user_exp,
52                                                         const mpoly_ctx_t mctx)
53 {
54     slong i, nvars = mctx->nvars;
55     flint_bitcnt_t exp_bits;
56 
57     if (mctx->deg)
58     {
59         fmpz_t deg;
60         fmpz_init_set(deg, user_exp + 0);
61         for (i = 1; i < nvars; i++)
62         {
63             fmpz_add(deg, deg, user_exp + i);
64         }
65         exp_bits = 1 + fmpz_bits(deg);
66         fmpz_clear(deg);
67     }
68     else
69     {
70         exp_bits = fmpz_bits(user_exp + 0);
71         for (i = 1; i < nvars; i++)
72         {
73             exp_bits = FLINT_MAX(exp_bits, fmpz_bits(user_exp + i));
74         }
75         exp_bits += 1;
76     }
77 
78     return exp_bits;
79 }
80 
81 /*
82     compute number of bits required to store user_exp in packed format
83     the returned number of bits includes space for a zero'd signed bit
84 */
mpoly_exp_bits_required_pfmpz(fmpz * const * user_exp,const mpoly_ctx_t mctx)85 flint_bitcnt_t mpoly_exp_bits_required_pfmpz(fmpz * const * user_exp,
86                                                         const mpoly_ctx_t mctx)
87 {
88     slong i, nvars = mctx->nvars;
89     flint_bitcnt_t exp_bits;
90 
91     if (mctx->deg)
92     {
93         fmpz_t deg;
94         fmpz_init_set(deg, user_exp[0]);
95         for (i = 1; i < nvars; i++)
96         {
97             fmpz_add(deg, deg, user_exp[i]);
98         }
99         exp_bits = 1 + fmpz_bits(deg);
100         fmpz_clear(deg);
101     }
102     else
103     {
104         exp_bits = fmpz_bits(user_exp[0]);
105         for (i = 1; i < nvars; i++)
106         {
107             exp_bits = FLINT_MAX(exp_bits, fmpz_bits(user_exp[i]));
108         }
109         exp_bits += 1;
110     }
111 
112     return exp_bits;
113 }
114