1 /*
2     Copyright (C) 2018 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 "mpoly.h"
13 
14 /* !!! this file DOES need to change with new orderings */
15 
mpoly_monomial_cmp_general(ulong * Aexp,flint_bitcnt_t Abits,ulong * Bexp,flint_bitcnt_t Bbits,const mpoly_ctx_t mctx)16 int mpoly_monomial_cmp_general(ulong * Aexp, flint_bitcnt_t Abits,
17                        ulong * Bexp, flint_bitcnt_t Bbits, const mpoly_ctx_t mctx)
18 {
19     slong N;
20 
21     if (Abits == Bbits)
22     {
23         /* common case */
24         N = mpoly_words_per_exp(Abits, mctx);
25 
26         if (!mctx->rev)
27         {
28             /* ORD_DEGREVLEX and ORD_DEG_LEX */
29             return mpoly_monomial_cmp_nomask(Aexp, Bexp, N);
30         }
31         else
32         {
33             /* ORD_DEGREVLEX */
34             slong i = N - 1;
35             if (Abits <= FLINT_BITS)
36             {
37                 /* compare the highest word */
38                 ulong fpw = FLINT_BITS/Abits;
39                 ulong himask = (UWORD(1) << (mctx->nvars%fpw*Abits)) - UWORD(1);
40                 if (Aexp[i] != Bexp[i])
41                 {
42                     if ((Aexp[i]^himask) > (Bexp[i]^himask))
43                         return 1;
44                     else
45                         return -1;
46                 }
47                 i--;
48             }
49             else
50             {
51                 /* compare the degree field with usual comparison */
52                 ulong wpf = Abits/FLINT_BITS;
53                 do
54                 {
55                     if (Aexp[i] != Bexp[i])
56                     {
57                         if (Aexp[i] > Bexp[i])
58                             return 1;
59                         else
60                             return -1;
61                     }
62                     i--;
63                 } while (--wpf != 0);
64             }
65             /* compare the remaining fields with reversed comparisons */
66             for (; i >= 0; i--)
67             {
68                 if (Aexp[i] != Bexp[i])
69                 {
70                     if (Aexp[i] < Bexp[i])
71                         return 1;
72                     else
73                         return -1;
74                 }
75             }
76             return 0;
77         }
78     }
79     else
80     {
81         int cmp;
82         flint_bitcnt_t newbits;
83         ulong * newAexp, * newBexp, * cmpmask;
84         TMP_INIT;
85 
86         TMP_START;
87 
88         if (Abits > Bbits)
89         {
90             newbits = Abits;
91             N = mpoly_words_per_exp(newbits, mctx);
92             newAexp = Aexp;
93             newBexp = (ulong *) TMP_ALLOC(N*sizeof(ulong));
94             mpoly_repack_monomials(newBexp, newbits, Bexp, Bbits, 1, mctx);
95         }
96         else
97         {
98             FLINT_ASSERT(Abits < Bbits);
99             newbits = Bbits;
100             N = mpoly_words_per_exp(newbits, mctx);
101             newBexp = Bexp;
102             newAexp = (ulong *) TMP_ALLOC(N*sizeof(ulong));
103             mpoly_repack_monomials(newAexp, newbits, Aexp, Abits, 1, mctx);
104         }
105 
106         cmpmask = (ulong *) TMP_ALLOC(N*sizeof(ulong));
107         mpoly_get_cmpmask(cmpmask, N, newbits, mctx);
108         cmp = mpoly_monomial_cmp(newAexp, newBexp, N, cmpmask);
109 
110         TMP_END;
111         return cmp;
112     }
113 }
114