Annotation of OpenXM_contrib2/asir2018/engine/nd.h, Revision 1.4
1.4 ! noro 1: /* $OpenXM: OpenXM_contrib2/asir2018/engine/nd.h,v 1.3 2018/09/28 08:20:28 noro Exp $ */
1.1 noro 2: #include "ca.h"
3: #include "parse.h"
4: #include "ox.h"
5: #include "base.h"
6: #include "inline.h"
7: #include <time.h>
8:
9: #if defined(__GNUC__)
10: #define INLINE static inline
11: #elif defined(VISUAL) || defined(__MINGW32__)
12: #define INLINE __inline
13: #else
14: #define INLINE
15: #endif
16:
17: typedef unsigned int UINT;
18:
19: #define USE_GEOBUCKET 1
20: #define USE_UNROLL 1
21:
22: #define REDTAB_LEN 32003
23:
24: /* GeoBucket for polynomial addition */
25:
26: typedef struct oPGeoBucket {
27: int m;
28: struct oND *body[32];
29: } *PGeoBucket;
30:
31: /* distributed polynomial; linked list rep. */
32: typedef struct oND {
33: struct oNM *body;
34: int nv;
35: int len;
36: int sugar;
37: } *ND;
38:
39: /* distributed polynomial; array rep. */
40: typedef struct oNDV {
41: struct oNMV *body;
42: int nv;
43: int len;
44: int sugar;
45: } *NDV;
46:
47: typedef union oNDC {
48: int m;
49: Z z;
50: LM lm;
51: Z gz;
52: P p;
53: R r;
54: DAlg a;
55: } *NDC;
56:
57: /* monomial; linked list rep. */
58: typedef struct oNM {
59: struct oNM *next;
60: union oNDC c;
61: UINT dl[1];
62: } *NM;
63:
64: /* monomial; array rep. */
65: typedef struct oNMV {
66: union oNDC c;
67: UINT dl[1];
68: } *NMV;
69:
70: /* history of reducer */
71: typedef struct oRHist {
72: struct oRHist *next;
73: int index;
74: int sugar;
75: UINT dl[1];
76: } *RHist;
77:
78: /* S-pair list */
79: typedef struct oND_pairs {
80: struct oND_pairs *next;
81: int i1,i2;
82: int sugar;
83: int sugar2;
84: UINT lcm[1];
85: } *ND_pairs;
86:
87: /* index and shift count for each exponent */
88: typedef struct oEPOS {
89: int i; /* index */
90: int s; /* shift */
91: } *EPOS;
92:
93: typedef struct oBlockMask {
94: int n;
95: struct order_pair *order_pair;
96: UINT **mask;
97: } *BlockMask;
98:
99: typedef struct oBaseSet {
100: int len;
101: NDV *ps;
102: UINT **bound;
103: } *BaseSet;
104:
105: typedef struct oNM_ind_pair
106: {
107: NM mul;
108: int index,sugar;
109: } *NM_ind_pair;
110:
111: typedef struct oIndArray
112: {
113: char width;
114: int head;
115: union {
116: unsigned char *c;
117: unsigned short *s;
118: unsigned int *i;
119: } index;
120: } *IndArray;
121:
122: typedef struct oNDVI {
123: NDV p;
124: int i;
125: } *NDVI;
126:
127: extern int (*ndl_compare_function)(UINT *a1,UINT *a2);
128: extern int nd_dcomp;
129:
130: extern NM _nm_free_list;
131: extern ND _nd_free_list;
132: extern ND_pairs _ndp_free_list;
133:
134: extern struct order_spec *dp_current_spec;
135: extern char *Demand;
136: extern VL CO;
137: extern int Top,Reverse,DP_Print,dp_nelim,do_weyl,NoSugar;
138: extern int *current_weyl_weight_vector;
139: extern int *current_module_weight_vector;
140:
141: /* fundamental macros */
142: #define TD(d) (d[0])
143: #define HDL(d) ((d)->body->dl)
144: #define HTD(d) (TD(HDL(d)))
145: #define HCU(d) ((d)->body->c)
146: #define HCM(d) ((d)->body->c.m)
147: #define HCLM(d) ((d)->body->c.lm)
1.3 noro 148: #define HCZ(d) ((d)->body->c.z)
1.1 noro 149: #define HCP(d) ((d)->body->c.p)
150: #define HCA(d) ((d)->body->c.a)
151: #define CM(x) ((x)->c.m)
152: #define CLM(x) ((x)->c.lm)
1.3 noro 153: #define CZ(x) ((x)->c.z)
1.1 noro 154: #define CP(x) ((x)->c.p)
155: #define CA(x) ((x)->c.a)
156: #define DL(x) ((x)->dl)
157: #define SG(x) ((x)->sugar)
158: #define LEN(x) ((x)->len)
159: #define LCM(x) ((x)->lcm)
160: #define GET_EXP(d,a) (((d)[nd_epos[a].i]>>nd_epos[a].s)&nd_mask0)
161: #define GET_EXP_MASK(d,a,m) ((((d)[nd_epos[a].i]&(m)[nd_epos[a].i])>>nd_epos[a].s)&nd_mask0)
162: #define PUT_EXP(r,a,e) ((r)[nd_epos[a].i] |= ((e)<<nd_epos[a].s))
163: #define XOR_EXP(r,a,e) ((r)[nd_epos[a].i] ^= ((e)<<nd_epos[a].s))
164:
165: #define GET_EXP_OLD(d,a) (((d)[oepos[a].i]>>oepos[a].s)&omask0)
166: #define PUT_EXP_OLD(r,a,e) ((r)[oepos[a].i] |= ((e)<<oepos[a].s))
167: #define MPOS(d) (d[nd_mpos])
168:
169: #define ROUND_FOR_ALIGN(s) ((((s)+sizeof(void *)-1)/sizeof(void *))*sizeof(void *))
170:
171: /* macros for term comparison */
172: #define TD_DL_COMPARE(d1,d2)\
173: (TD(d1)>TD(d2)?1:(TD(d1)<TD(d2)?-1:ndl_lex_compare(d1,d2)))
174: #if 0
175: #define DL_COMPARE(d1,d2)\
176: (nd_dcomp>0?TD_DL_COMPARE(d1,d2)\
177: :(nd_dcomp==0?ndl_lex_compare(d1,d2)\
178: :(nd_blockmask?ndl_block_compare(d1,d2)\
179: :(*ndl_compare_function)(d1,d2))))
180: #else
181: #define DL_COMPARE(d1,d2)\
182: (nd_dcomp>0?TD_DL_COMPARE(d1,d2):(*ndl_compare_function)(d1,d2))
183: #endif
184:
185: /* allocators */
186: #define NEWRHist(r) \
187: ((r)=(RHist)MALLOC(sizeof(struct oRHist)+(nd_wpd-1)*sizeof(UINT)))
188: #define NEWND_pairs(m) \
189: if(!_ndp_free_list)_NDP_alloc();\
190: (m)=_ndp_free_list; _ndp_free_list = NEXT(_ndp_free_list)
191: #define NEWNM(m)\
192: if(!_nm_free_list)_NM_alloc();\
193: (m)=_nm_free_list; _nm_free_list = NEXT(_nm_free_list)
194: #define MKND(n,m,len,d)\
195: if(!_nd_free_list)_ND_alloc();\
196: (d)=_nd_free_list; _nd_free_list = (ND)BDY(_nd_free_list);\
197: NV(d)=(n); LEN(d)=(len); BDY(d)=(m)
198: #define NEWNDV(d) ((d)=(NDV)MALLOC(sizeof(struct oNDV)))
199: #define MKNDV(n,m,l,d) NEWNDV(d); NV(d)=(n); BDY(d)=(m); LEN(d) = l;
200: #define NEWNM_ind_pair(p)\
201: ((p)=(NM_ind_pair)MALLOC(sizeof(struct oNM_ind_pair)))
202:
203: /* allocate and link a new object */
204: #define NEXTRHist(r,c) \
205: if(!(r)){NEWRHist(r);(c)=(r);}else{NEWRHist(NEXT(c));(c)=NEXT(c);}
206: #define NEXTNM(r,c) \
207: if(!(r)){NEWNM(r);(c)=(r);}else{NEWNM(NEXT(c));(c)=NEXT(c);}
208: #define NEXTNM2(r,c,s) \
209: if(!(r)){(c)=(r)=(s);}else{NEXT(c)=(s);(c)=(s);}
210: #define NEXTND_pairs(r,c) \
211: if(!(r)){NEWND_pairs(r);(c)=(r);}else{NEWND_pairs(NEXT(c));(c)=NEXT(c);}
212: #define MKNM_ind_pair(p,m,i,s) (NEWNM_ind_pair(p),(p)->mul=(m),(p)->index=(i),(p)->sugar = (s))
213:
214: /* deallocators */
215: #define FREENM(m) NEXT(m)=_nm_free_list; _nm_free_list=(m)
216: #define FREENDP(m) NEXT(m)=_ndp_free_list; _ndp_free_list=(m)
217: #define FREEND(m) BDY(m)=(NM)_nd_free_list; _nd_free_list=(m)
218:
219: /* macro for increasing pointer to NMV */
220: #define NMV_ADV(m) (m = (NMV)(((char *)m)+nmv_adv))
221: #define NMV_OADV(m) (m = (NMV)(((char *)m)+oadv))
222: #define NDV_NADV(m) (m = (NMV)(((char *)m)+newadv))
223: #define NMV_PREV(m) (m = (NMV)(((char *)m)-nmv_adv))
224: #define NMV_OPREV(m) (m = (NMV)(((char *)m)-oadv))
225:
226: /* external functions */
227: #if 1
228: void GC_gcollect();
229: #endif
230:
231: /* manipulation of coefficients */
232: void nd_removecont(int mod,ND p);
233: void nd_removecont2(ND p1,ND p2);
234: void removecont_array(P *c,int n,int full);
235: void removecont_array_q(Z *c,int n);
236:
237: /* GeoBucket functions */
238: ND normalize_pbucket(int mod,PGeoBucket g);
239: int head_pbucket(int mod,PGeoBucket g);
240: int head_pbucket_q(PGeoBucket g);
241: int head_pbucket_lf(PGeoBucket g);
242: void add_pbucket_symbolic(PGeoBucket g,ND d);
243: void add_pbucket(int mod,PGeoBucket g,ND d);
244: void free_pbucket(PGeoBucket b);
245: void mulq_pbucket(PGeoBucket g,Z c);
246: NM remove_head_pbucket_symbolic(PGeoBucket g);
247: PGeoBucket create_pbucket();
248:
249: /* manipulation of pairs and bases */
250: int nd_newps(int mod,ND a,ND aq);
251: ND_pairs nd_newpairs( NODE g, int t );
252: ND_pairs nd_minp( ND_pairs d, ND_pairs *prest );
253: ND_pairs nd_minsugarp( ND_pairs d, ND_pairs *prest );
254: NODE update_base(NODE nd,int ndp);
255: ND_pairs update_pairs( ND_pairs d, NODE /* of index */ g, int t, int gensyz);
256: ND_pairs equivalent_pairs( ND_pairs d1, ND_pairs *prest );
257: ND_pairs crit_B( ND_pairs d, int s );
258: ND_pairs crit_M( ND_pairs d1 );
259: ND_pairs crit_F( ND_pairs d1 );
260: int crit_2( int dp1, int dp2 );
261: int ndv_newps(int m,NDV a,NDV aq,int f4);
262:
263: /* top level functions */
264: void nd_gr(LIST f,LIST v,int m,int homo,int retdp,int f4,struct order_spec *ord,LIST *rp);
265: void nd_gr_trace(LIST f,LIST v,int trace,int homo,int f4,struct order_spec *ord,LIST *rp);
266: NODE nd_f4(int m,int checkonly,int **indp);
267: NODE nd_gb(int m,int ishomo,int checkonly,int gensyz,int **indp);
268: NODE nd_gb_trace(int m,int ishomo,int **indp);
269: NODE nd_f4_trace(int m,int **indp);
270:
271: /* ndl functions */
272: int ndl_weight(UINT *d);
273: void ndl_weight_mask(UINT *d);
274: void ndl_homogenize(UINT *d,UINT *r,int obpe,EPOS oepos,int ompos,int weight);
275: void ndl_dehomogenize(UINT *p);
276: void ndl_reconstruct(UINT *d,UINT *r,int obpe,EPOS oepos);
277: INLINE int ndl_reducible(UINT *d1,UINT *d2);
278: INLINE int ndl_lex_compare(UINT *d1,UINT *d2);
279: INLINE int ndl_block_compare(UINT *d1,UINT *d2);
280: INLINE int ndl_matrix_compare(UINT *d1,UINT *d2);
281: INLINE int ndl_composite_compare(UINT *d1,UINT *d2);
282: INLINE int ndl_equal(UINT *d1,UINT *d2);
283: INLINE void ndl_copy(UINT *d1,UINT *d2);
284: INLINE void ndl_zero(UINT *d);
285: INLINE void ndl_add(UINT *d1,UINT *d2,UINT *d);
286: INLINE void ndl_addto(UINT *d1,UINT *d2);
287: INLINE void ndl_sub(UINT *d1,UINT *d2,UINT *d);
288: INLINE int ndl_hash_value(UINT *d);
289:
290: /* normal forms */
291: INLINE int ndl_find_reducer(UINT *g);
292: int nd_sp(int mod,int trace,ND_pairs p,ND *nf);
293: int nd_sp_f4(int m,int trace,ND_pairs l,PGeoBucket bucket);
1.3 noro 294: int nd_nf(int mod,ND d,ND g,NDV *ps,int full,ND *nf);
1.1 noro 295: int nd_nf_pbucket(int mod,ND g,NDV *ps,int full,ND *nf);
296:
297: /* finalizers */
298: NODE ndv_reducebase(NODE x,int *perm);
299: NODE ndv_reduceall(int m,NODE f);
300:
301: /* allocators */
302: void nd_free_private_storage();
303: void _NM_alloc();
304: void _ND_alloc();
305: void nd_free(ND p);
306: void nd_free_redlist();
307:
308: /* printing */
309: void ndl_print(UINT *dl);
310: void nd_print(ND p);
311: void nd_print_q(ND p);
312: void ndp_print(ND_pairs d);
313:
314:
315: /* setup, reconstruct */
316: void nd_init_ord(struct order_spec *spec);
317: ND_pairs nd_reconstruct(int trace,ND_pairs ndp);
318: int ndv_setup(int mod,int trace,NODE f,int dont_sort,int dont_removecont);
319: void nd_setup_parameters(int nvar,int max);
320: BlockMask nd_create_blockmask(struct order_spec *ord);
321: EPOS nd_create_epos(struct order_spec *ord);
322: int nd_get_exporigin(struct order_spec *ord);
323: void ndv_mod(int mod,NDV p);
324: NDV ndv_dup(int mod,NDV p);
325: NDV ndv_symbolic(int mod,NDV p);
326: int nd_symbolic_preproc(PGeoBucket bucket,int trace,UINT **s0vect,NODE *r);
327: ND nd_dup(ND p);
328: int ndv_ishomo(NDV p);
329:
330: /* ND functions */
331: int ndv_check_membership(int m,NODE input,int obpe,int oadv,EPOS oepos,NODE cand);
332: void nd_mul_c(int mod,ND p,int mul);
333: void nd_mul_c_q(ND p,P mul);
334: void nd_mul_c_p(VL vl,ND p,P mul);
335: ND nd_remove_head(ND p);
336: ND nd_separate_head(ND p,ND *head);
337: INLINE int nd_length(ND p);
338: void nd_append_red(UINT *d,int i);
339: UINT *ndv_compute_bound(NDV p);
340: UINT *nd_compute_bound(ND p);
341: ND nd_copy(ND p);
342: ND nd_merge(ND p1,ND p2);
343: ND nd_add(int mod,ND p1,ND p2);
344: ND nd_add_q(ND p1,ND p2);
345: ND nd_add_sf(ND p1,ND p2);
346: ND nd_quo(int mod,PGeoBucket p,NDV d);
347: INLINE int nd_length(ND p);
348: NODE nd_f4_red(int m,ND_pairs sp0,int trace,UINT *s0vect,int col,NODE rp0,ND_pairs *nz);
349: NODE nd_f4_red_dist(int m,ND_pairs sp0,UINT *s0vect,int col,NODE rp0, ND_pairs *nz);
350: NODE nd_f4_red_main(int m,ND_pairs sp0,int nsp,UINT *s0vect,int col,
351: NM_ind_pair *rvect,int *rhead,IndArray *imat,int nred,ND_pairs *nz);
352: NODE nd_f4_red_mod_main(int m,ND_pairs sp0,int nsp,UINT *s0vect,int col,
353: NM_ind_pair *rvect,int *rhead,IndArray *imat,int nred,ND_pairs *nz);
354: NODE nd_f4_red_sf_main(int m,ND_pairs sp0,int nsp,UINT *s0vect,int col,
355: NM_ind_pair *rvect,int *rhead,IndArray *imat,int nred,ND_pairs *nz);
356: NODE nd_f4_red_q_main(ND_pairs sp0,int nsp,int trace,UINT *s0vect,int col,
357: NM_ind_pair *rvect,int *rhead,IndArray *imat,int nred);
358: NODE nd_f4_red_gz_main(ND_pairs sp0,int nsp,int trace,UINT *s0vect,int col,
359: NM_ind_pair *rvect,int *rhead,IndArray *imat,int nred);
360:
361: /* NDV functions */
362: ND weyl_ndv_mul_nm(int mod,NM m0,NDV p);
363: void weyl_mul_nm_nmv(int n,int mod,NM m0,NMV m1,NM *tab,int tlen);
364: void ndv_mul_c(int mod,NDV p,int mul);
365: void ndv_mul_c_q(NDV p,Z mul);
366: ND ndv_mul_nm_symbolic(NM m0,NDV p);
367: ND ndv_mul_nm(int mod,NM m0,NDV p);
368: ND ndv_mul_nmv_trunc(int mod,NMV m0,NDV p,UINT *d);
369: void ndv_realloc(NDV p,int obpe,int oadv,EPOS oepos);
370: NDV ndv_dup_realloc(NDV p,int obpe,int oadv,EPOS oepos);
371: void ndv_homogenize(NDV p,int obpe,int oadv,EPOS eops,int ompos);
372: void ndv_dehomogenize(NDV p,struct order_spec *spec);
373: void ndv_removecont(int mod,NDV p);
374: void ndv_print(NDV p);
375: void ndv_print_q(NDV p);
376: void ndv_free(NDV p);
377: void ndv_save(NDV p,int index);
378: NDV ndv_load(int index);
379:
380: /* converters */
381: ND ptond(VL vl,VL dvl,P p);
382: NDV ptondv(VL vl,VL dvl,P p);
383: P ndvtop(int mod,VL vl,VL dvl,NDV p);
384: NDV ndtondv(int mod,ND p);
385: ND ndvtond(int mod,NDV p);
386: Z *nm_ind_pair_to_vect(int m,UINT *s0,int n,NM_ind_pair pair);
387: IndArray nm_ind_pair_to_vect_compress(int m,UINT *s0,int n,int *s0hash,NM_ind_pair pair);
388: int nd_to_vect(int mod,UINT *s0,int n,ND d,UINT *r);
389: int nd_to_vect_q(UINT *s0,int n,ND d,Z *r);
390: NDV vect_to_ndv_q(Z *vect,int spcol,int col,int *rhead,UINT *s0vect);
391:
392: /* elimination */
393: int nd_gauss_elim_mod(UINT **mat0,int *sugar,ND_pairs *spactive,int row,int col,int md,int *colstat);
394: int nd_gauss_elim_sf(UINT **mat0,int *sugar,int row,int col,int md,int *colstat);
395: int nd_gauss_elim_q(Z **mat0,int *sugar,int row,int col,int *colstat);
396:
397: int ndl_ww_lex_compare(UINT *a1,UINT *a2);
398:
1.4 ! noro 399: #if SIZEOF_LONG == 8
! 400: int nd_to_vect64(int mod,UINT *s0,int n,ND d,mp_limb_t *r);
! 401: int ndv_reduce_vect64(int m,mp_limb_t *svect,mp_limb_t *cvect,int col,IndArray *imat,NM_ind_pair *rp0,int nred);
! 402: NDV vect64_to_ndv(mp_limb_t *vect,int spcol,int col,int *rhead,UINT *s0vect);
! 403: void red_by_vect64(int m, mp_limb_t *p,unsigned int *c,mp_limb_t *r,unsigned int hc,int len);
! 404: int nd_gauss_elim_mod64(mp_limb_t **mat,int *sugar,ND_pairs *spactive,int row,int col,int md,int *colstat);
1.2 noro 405: #endif
406:
1.1 noro 407:
FreeBSD-CVSweb <freebsd-cvsweb@FreeBSD.org>