[BACK]Return to nd.c CVS log [TXT][DIR] Up to [local] / OpenXM_contrib2 / asir2000 / engine

Annotation of OpenXM_contrib2/asir2000/engine/nd.c, Revision 1.194

1.194   ! noro        1: /* $OpenXM: OpenXM_contrib2/asir2000/engine/nd.c,v 1.193 2010/12/22 09:33:57 noro Exp $ */
1.2       noro        2:
1.94      noro        3: #include "nd.h"
1.63      noro        4:
1.131     noro        5: int diag_period = 6;
1.61      noro        6: int (*ndl_compare_function)(UINT *a1,UINT *a2);
1.94      noro        7: int nd_dcomp;
                      8: NM _nm_free_list;
                      9: ND _nd_free_list;
                     10: ND_pairs _ndp_free_list;
1.150     noro       11: NODE nd_hcf;
1.32      noro       12:
1.146     noro       13: static NODE nd_subst;
                     14: static VL nd_vc;
1.121     noro       15: static int nd_ntrans;
1.117     noro       16: static int nd_nalg;
1.103     noro       17: #if 0
1.74      noro       18: static int ndv_alloc;
1.103     noro       19: #endif
1.87      noro       20: #if 1
1.69      noro       21: static int nd_f4_nsp=0x7fffffff;
1.87      noro       22: #else
                     23: static int nd_f4_nsp=50;
                     24: #endif
1.42      noro       25: static double nd_scale=2;
1.61      noro       26: static UINT **nd_bound;
1.42      noro       27: static struct order_spec *nd_ord;
                     28: static EPOS nd_epos;
1.43      noro       29: static BlockMask nd_blockmask;
1.42      noro       30: static int nd_nvar;
                     31: static int nd_isrlex;
                     32: static int nd_epw,nd_bpe,nd_wpd,nd_exporigin;
1.61      noro       33: static UINT nd_mask[32];
                     34: static UINT nd_mask0,nd_mask1;
1.42      noro       35:
1.20      noro       36: static NDV *nd_ps;
1.53      noro       37: static NDV *nd_ps_trace;
1.42      noro       38: static RHist *nd_psh;
                     39: static int nd_psn,nd_pslen;
                     40: static RHist *nd_red;
1.96      noro       41: static int *nd_work_vector;
                     42: static int **nd_matrix;
                     43: static int nd_matrix_len;
1.97      noro       44: static struct weight_or_block *nd_worb;
                     45: static int nd_worb_len;
1.42      noro       46: static int nd_found,nd_create,nd_notfirst;
                     47: static int nmv_adv;
1.77      noro       48: static int nd_demand;
1.174     noro       49: static int nd_module,nd_ispot,nd_mpos,nd_pot_nelim;
1.167     noro       50: static NODE nd_tracelist;
                     51: static NODE nd_alltracelist;
1.192     noro       52: static int nd_gentrace,nd_gensyz,nd_nora,nd_newelim;
1.187     noro       53: static int *nd_gbblock;
1.1       noro       54:
1.119     noro       55: NumberField get_numberfield();
1.114     noro       56: UINT *nd_det_compute_bound(NDV **dm,int n,int j);
                     57: void nd_det_reconstruct(NDV **dm,int n,int j,NDV d);
1.152     ohara      58: void nd_heu_nezgcdnpz(VL vl,P *pl,int m,int full,P *pr);
1.118     noro       59: int nd_monic(int m,ND *p);
1.129     noro       60: NDV plain_vect_to_ndv_q(Q *mat,int col,UINT *s0vect);
1.157     noro       61: LIST ndvtopl(int mod,VL vl,VL dvl,NDV p,int rank);
                     62: NDV pltondv(VL vl,VL dvl,LIST p);
                     63: void pltozpl(LIST l,Q *cont,LIST *pp);
1.159     noro       64: void ndl_max(UINT *d1,unsigned *d2,UINT *d);
                     65: pointer GC_malloc_atomic_ignore_off_page(int);
1.167     noro       66: void nmtodp(int mod,NM m,DP *r);
                     67: NODE reverse_node(NODE n);
                     68: P ndc_div(int mod,union oNDC a,union oNDC b);
                     69: P ndctop(int mod,union oNDC c);
                     70: void finalize_tracelist(int i,P cont);
                     71: void conv_ilist(int demand,int trace,NODE g,int **indp);
1.172     noro       72: void parse_nd_option(NODE opt);
1.114     noro       73:
1.149     noro       74: extern int Denominator,DP_Multiple;
                     75:
1.1       noro       76: void nd_free_private_storage()
                     77: {
1.157     noro       78:     _nm_free_list = 0;
                     79:     _ndp_free_list = 0;
1.71      noro       80: #if 0
1.157     noro       81:     GC_gcollect();
1.71      noro       82: #endif
1.1       noro       83: }
                     84:
                     85: void _NM_alloc()
                     86: {
1.157     noro       87:     NM p;
                     88:     int i;
1.1       noro       89:
1.157     noro       90:     for ( i = 0; i < 1024; i++ ) {
                     91:         p = (NM)GC_malloc(sizeof(struct oNM)+(nd_wpd-1)*sizeof(UINT));
                     92:         p->next = _nm_free_list; _nm_free_list = p;
                     93:     }
1.1       noro       94: }
                     95:
                     96: void _ND_alloc()
                     97: {
1.157     noro       98:     ND p;
                     99:     int i;
1.1       noro      100:
1.157     noro      101:     for ( i = 0; i < 1024; i++ ) {
                    102:         p = (ND)GC_malloc(sizeof(struct oND));
                    103:         p->body = (NM)_nd_free_list; _nd_free_list = p;
                    104:     }
1.1       noro      105: }
                    106:
                    107: void _NDP_alloc()
                    108: {
1.157     noro      109:     ND_pairs p;
                    110:     int i;
1.1       noro      111:
1.157     noro      112:     for ( i = 0; i < 1024; i++ ) {
                    113:         p = (ND_pairs)GC_malloc(sizeof(struct oND_pairs)
                    114:             +(nd_wpd-1)*sizeof(UINT));
                    115:         p->next = _ndp_free_list; _ndp_free_list = p;
                    116:     }
1.1       noro      117: }
                    118:
1.30      noro      119: INLINE int nd_length(ND p)
1.1       noro      120: {
1.157     noro      121:     NM m;
                    122:     int i;
1.1       noro      123:
1.157     noro      124:     if ( !p )
                    125:         return 0;
                    126:     else {
                    127:         for ( i = 0, m = BDY(p); m; m = NEXT(m), i++ );
                    128:         return i;
                    129:     }
1.1       noro      130: }
                    131:
1.61      noro      132: INLINE int ndl_reducible(UINT *d1,UINT *d2)
1.1       noro      133: {
1.157     noro      134:     UINT u1,u2;
                    135:     int i,j;
1.1       noro      136:
1.157     noro      137:     if ( nd_module && (MPOS(d1) != MPOS(d2)) ) return 0;
                    138:
                    139:     if ( TD(d1) < TD(d2) ) return 0;
1.65      noro      140: #if USE_UNROLL
1.157     noro      141:     switch ( nd_bpe ) {
                    142:         case 3:
                    143:             for ( i = nd_exporigin; i < nd_wpd; i++ ) {
                    144:                 u1 = d1[i]; u2 = d2[i];
                    145:                 if ( (u1&0x38000000) < (u2&0x38000000) ) return 0;
                    146:                 if ( (u1& 0x7000000) < (u2& 0x7000000) ) return 0;
                    147:                 if ( (u1&  0xe00000) < (u2&  0xe00000) ) return 0;
                    148:                 if ( (u1&  0x1c0000) < (u2&  0x1c0000) ) return 0;
                    149:                 if ( (u1&   0x38000) < (u2&   0x38000) ) return 0;
                    150:                 if ( (u1&    0x7000) < (u2&    0x7000) ) return 0;
                    151:                 if ( (u1&     0xe00) < (u2&     0xe00) ) return 0;
                    152:                 if ( (u1&     0x1c0) < (u2&     0x1c0) ) return 0;
                    153:                 if ( (u1&      0x38) < (u2&      0x38) ) return 0;
                    154:                 if ( (u1&       0x7) < (u2&       0x7) ) return 0;
                    155:             }
                    156:             return 1;
                    157:             break;
                    158:         case 4:
                    159:             for ( i = nd_exporigin; i < nd_wpd; i++ ) {
                    160:                 u1 = d1[i]; u2 = d2[i];
                    161:                 if ( (u1&0xf0000000) < (u2&0xf0000000) ) return 0;
                    162:                 if ( (u1& 0xf000000) < (u2& 0xf000000) ) return 0;
                    163:                 if ( (u1&  0xf00000) < (u2&  0xf00000) ) return 0;
                    164:                 if ( (u1&   0xf0000) < (u2&   0xf0000) ) return 0;
                    165:                 if ( (u1&    0xf000) < (u2&    0xf000) ) return 0;
                    166:                 if ( (u1&     0xf00) < (u2&     0xf00) ) return 0;
                    167:                 if ( (u1&      0xf0) < (u2&      0xf0) ) return 0;
                    168:                 if ( (u1&       0xf) < (u2&       0xf) ) return 0;
                    169:             }
                    170:             return 1;
                    171:             break;
                    172:         case 6:
                    173:             for ( i = nd_exporigin; i < nd_wpd; i++ ) {
                    174:                 u1 = d1[i]; u2 = d2[i];
                    175:                 if ( (u1&0x3f000000) < (u2&0x3f000000) ) return 0;
                    176:                 if ( (u1&  0xfc0000) < (u2&  0xfc0000) ) return 0;
                    177:                 if ( (u1&   0x3f000) < (u2&   0x3f000) ) return 0;
                    178:                 if ( (u1&     0xfc0) < (u2&     0xfc0) ) return 0;
                    179:                 if ( (u1&      0x3f) < (u2&      0x3f) ) return 0;
                    180:             }
                    181:             return 1;
                    182:             break;
                    183:         case 8:
                    184:             for ( i = nd_exporigin; i < nd_wpd; i++ ) {
                    185:                 u1 = d1[i]; u2 = d2[i];
                    186:                 if ( (u1&0xff000000) < (u2&0xff000000) ) return 0;
                    187:                 if ( (u1&  0xff0000) < (u2&  0xff0000) ) return 0;
                    188:                 if ( (u1&    0xff00) < (u2&    0xff00) ) return 0;
                    189:                 if ( (u1&      0xff) < (u2&      0xff) ) return 0;
                    190:             }
                    191:             return 1;
                    192:             break;
                    193:         case 16:
                    194:             for ( i = nd_exporigin; i < nd_wpd; i++ ) {
                    195:                 u1 = d1[i]; u2 = d2[i];
                    196:                 if ( (u1&0xffff0000) < (u2&0xffff0000) ) return 0;
                    197:                 if ( (u1&    0xffff) < (u2&    0xffff) ) return 0;
                    198:             }
                    199:             return 1;
                    200:             break;
                    201:         case 32:
                    202:             for ( i = nd_exporigin; i < nd_wpd; i++ )
                    203:                 if ( d1[i] < d2[i] ) return 0;
                    204:             return 1;
                    205:             break;
                    206:         default:
                    207:             for ( i = nd_exporigin; i < nd_wpd; i++ ) {
                    208:                 u1 = d1[i]; u2 = d2[i];
                    209:                 for ( j = 0; j < nd_epw; j++ )
                    210:                     if ( (u1&nd_mask[j]) < (u2&nd_mask[j]) ) return 0;
                    211:             }
                    212:             return 1;
                    213:     }
1.65      noro      214: #else
1.157     noro      215:     for ( i = nd_exporigin; i < nd_wpd; i++ ) {
                    216:         u1 = d1[i]; u2 = d2[i];
                    217:         for ( j = 0; j < nd_epw; j++ )
                    218:             if ( (u1&nd_mask[j]) < (u2&nd_mask[j]) ) return 0;
                    219:     }
                    220:     return 1;
1.65      noro      221: #endif
1.1       noro      222: }
                    223:
1.61      noro      224: /*
                    225:  * If the current order is a block order,
                    226:  * then the last block is length 1 and contains
                    227:  * the homo variable. Otherwise, the original
                    228:  * order is either 0 or 2.
                    229:  */
                    230:
1.164     noro      231: void ndl_homogenize(UINT *d,UINT *r,int obpe,EPOS oepos,int ompos,int weight)
1.23      noro      232: {
1.157     noro      233:     int w,i,e,n,omask0;
1.61      noro      234:
1.157     noro      235:     omask0 = obpe==32?0xffffffff:((1<<obpe)-1);
                    236:     n = nd_nvar-1;
                    237:     ndl_zero(r);
                    238:     for ( i = 0; i < n; i++ ) {
                    239:         e = GET_EXP_OLD(d,i);
                    240:         PUT_EXP(r,i,e);
                    241:     }
                    242:     w = TD(d);
                    243:     PUT_EXP(r,nd_nvar-1,weight-w);
1.164     noro      244:     if ( nd_module ) MPOS(r) = d[ompos];
1.157     noro      245:     TD(r) = weight;
                    246:     if ( nd_blockmask ) ndl_weight_mask(r);
1.61      noro      247: }
                    248:
                    249: void ndl_dehomogenize(UINT *d)
                    250: {
1.157     noro      251:     UINT mask;
                    252:     UINT h;
                    253:     int i,bits;
                    254:
                    255:     if ( nd_blockmask ) {
                    256:         h = GET_EXP(d,nd_nvar-1);
                    257:         XOR_EXP(d,nd_nvar-1,h);
                    258:         TD(d) -= h;
                    259:         ndl_weight_mask(d);
                    260:     } else {
                    261:         if ( nd_isrlex ) {
                    262:             if ( nd_bpe == 32 ) {
                    263:                 h = d[nd_exporigin];
                    264:                 for ( i = nd_exporigin+1; i < nd_wpd; i++ )
                    265:                     d[i-1] = d[i];
                    266:                 d[i-1] = 0;
                    267:                 TD(d) -= h;
                    268:             } else {
                    269:                 bits = nd_epw*nd_bpe;
                    270:                 mask = bits==32?0xffffffff:((1<<(nd_epw*nd_bpe))-1);
                    271:                 h = (d[nd_exporigin]>>((nd_epw-1)*nd_bpe))&nd_mask0;
                    272:                 for ( i = nd_exporigin; i < nd_wpd; i++ )
                    273:                     d[i] = ((d[i]<<nd_bpe)&mask)
                    274:                         |(i+1<nd_wpd?((d[i+1]>>((nd_epw-1)*nd_bpe))&nd_mask0):0);
                    275:                 TD(d) -= h;
                    276:             }
                    277:         } else {
                    278:             h = GET_EXP(d,nd_nvar-1);
                    279:             XOR_EXP(d,nd_nvar-1,h);
                    280:             TD(d) -= h;
                    281:         }
                    282:     }
1.23      noro      283: }
                    284:
1.61      noro      285: void ndl_lcm(UINT *d1,unsigned *d2,UINT *d)
1.1       noro      286: {
1.157     noro      287:     UINT t1,t2,u,u1,u2;
                    288:     int i,j,l;
                    289:
                    290:     if ( nd_module && (MPOS(d1) != MPOS(d2)) )
                    291:         error("ndl_lcm : inconsistent monomials");
                    292: #if USE_UNROLL
                    293:     switch ( nd_bpe ) {
                    294:         case 3:
                    295:             for ( i = nd_exporigin; i < nd_wpd; i++ ) {
                    296:                 u1 = d1[i]; u2 = d2[i];
                    297:                 t1 = (u1&0x38000000); t2 = (u2&0x38000000); u = t1>t2?t1:t2;
                    298:                 t1 = (u1& 0x7000000); t2 = (u2& 0x7000000); u |= t1>t2?t1:t2;
                    299:                 t1 = (u1&  0xe00000); t2 = (u2&  0xe00000); u |= t1>t2?t1:t2;
                    300:                 t1 = (u1&  0x1c0000); t2 = (u2&  0x1c0000); u |= t1>t2?t1:t2;
                    301:                 t1 = (u1&   0x38000); t2 = (u2&   0x38000); u |= t1>t2?t1:t2;
                    302:                 t1 = (u1&    0x7000); t2 = (u2&    0x7000); u |= t1>t2?t1:t2;
                    303:                 t1 = (u1&     0xe00); t2 = (u2&     0xe00); u |= t1>t2?t1:t2;
                    304:                 t1 = (u1&     0x1c0); t2 = (u2&     0x1c0); u |= t1>t2?t1:t2;
                    305:                 t1 = (u1&      0x38); t2 = (u2&      0x38); u |= t1>t2?t1:t2;
                    306:                 t1 = (u1&       0x7); t2 = (u2&       0x7); u |= t1>t2?t1:t2;
                    307:                 d[i] = u;
                    308:             }
                    309:             break;
                    310:         case 4:
                    311:             for ( i = nd_exporigin; i < nd_wpd; i++ ) {
                    312:                 u1 = d1[i]; u2 = d2[i];
                    313:                 t1 = (u1&0xf0000000); t2 = (u2&0xf0000000); u = t1>t2?t1:t2;
                    314:                 t1 = (u1& 0xf000000); t2 = (u2& 0xf000000); u |= t1>t2?t1:t2;
                    315:                 t1 = (u1&  0xf00000); t2 = (u2&  0xf00000); u |= t1>t2?t1:t2;
                    316:                 t1 = (u1&   0xf0000); t2 = (u2&   0xf0000); u |= t1>t2?t1:t2;
                    317:                 t1 = (u1&    0xf000); t2 = (u2&    0xf000); u |= t1>t2?t1:t2;
                    318:                 t1 = (u1&     0xf00); t2 = (u2&     0xf00); u |= t1>t2?t1:t2;
                    319:                 t1 = (u1&      0xf0); t2 = (u2&      0xf0); u |= t1>t2?t1:t2;
                    320:                 t1 = (u1&       0xf); t2 = (u2&       0xf); u |= t1>t2?t1:t2;
                    321:                 d[i] = u;
                    322:             }
                    323:             break;
                    324:         case 6:
                    325:             for ( i = nd_exporigin; i < nd_wpd; i++ ) {
                    326:                 u1 = d1[i]; u2 = d2[i];
                    327:                 t1 = (u1&0x3f000000); t2 = (u2&0x3f000000); u = t1>t2?t1:t2;
                    328:                 t1 = (u1&  0xfc0000); t2 = (u2&  0xfc0000); u |= t1>t2?t1:t2;
                    329:                 t1 = (u1&   0x3f000); t2 = (u2&   0x3f000); u |= t1>t2?t1:t2;
                    330:                 t1 = (u1&     0xfc0); t2 = (u2&     0xfc0); u |= t1>t2?t1:t2;
                    331:                 t1 = (u1&      0x3f); t2 = (u2&      0x3f); u |= t1>t2?t1:t2;
                    332:                 d[i] = u;
                    333:             }
                    334:             break;
                    335:         case 8:
                    336:             for ( i = nd_exporigin; i < nd_wpd; i++ ) {
                    337:                 u1 = d1[i]; u2 = d2[i];
                    338:                 t1 = (u1&0xff000000); t2 = (u2&0xff000000); u = t1>t2?t1:t2;
                    339:                 t1 = (u1&  0xff0000); t2 = (u2&  0xff0000); u |= t1>t2?t1:t2;
                    340:                 t1 = (u1&    0xff00); t2 = (u2&    0xff00); u |= t1>t2?t1:t2;
                    341:                 t1 = (u1&      0xff); t2 = (u2&      0xff); u |= t1>t2?t1:t2;
                    342:                 d[i] = u;
                    343:             }
                    344:             break;
                    345:         case 16:
                    346:             for ( i = nd_exporigin; i < nd_wpd; i++ ) {
                    347:                 u1 = d1[i]; u2 = d2[i];
                    348:                 t1 = (u1&0xffff0000); t2 = (u2&0xffff0000); u = t1>t2?t1:t2;
                    349:                 t1 = (u1&    0xffff); t2 = (u2&    0xffff); u |= t1>t2?t1:t2;
                    350:                 d[i] = u;
                    351:             }
                    352:             break;
                    353:         case 32:
                    354:             for ( i = nd_exporigin; i < nd_wpd; i++ ) {
                    355:                 u1 = d1[i]; u2 = d2[i];
                    356:                 d[i] = u1>u2?u1:u2;
                    357:             }
                    358:             break;
                    359:         default:
                    360:             for ( i = nd_exporigin; i < nd_wpd; i++ ) {
                    361:                 u1 = d1[i]; u2 = d2[i];
                    362:                 for ( j = 0, u = 0; j < nd_epw; j++ ) {
                    363:                     t1 = (u1&nd_mask[j]); t2 = (u2&nd_mask[j]); u |= t1>t2?t1:t2;
                    364:                 }
                    365:                 d[i] = u;
                    366:             }
                    367:             break;
                    368:     }
                    369: #else
                    370:     for ( i = nd_exporigin; i < nd_wpd; i++ ) {
                    371:         u1 = d1[i]; u2 = d2[i];
                    372:         for ( j = 0, u = 0; j < nd_epw; j++ ) {
                    373:             t1 = (u1&nd_mask[j]); t2 = (u2&nd_mask[j]); u |= t1>t2?t1:t2;
                    374:         }
                    375:         d[i] = u;
                    376:     }
                    377: #endif
1.163     noro      378:     if ( nd_module ) MPOS(d) = MPOS(d1);
1.157     noro      379:     TD(d) = ndl_weight(d);
                    380:     if ( nd_blockmask ) ndl_weight_mask(d);
                    381: }
                    382:
1.159     noro      383: void ndl_max(UINT *d1,unsigned *d2,UINT *d)
1.157     noro      384: {
                    385:     UINT t1,t2,u,u1,u2;
                    386:     int i,j,l;
1.1       noro      387:
1.157     noro      388:     for ( i = nd_exporigin; i < nd_wpd; i++ ) {
                    389:         u1 = d1[i]; u2 = d2[i];
                    390:         for ( j = 0, u = 0; j < nd_epw; j++ ) {
                    391:             t1 = (u1&nd_mask[j]); t2 = (u2&nd_mask[j]); u |= t1>t2?t1:t2;
                    392:         }
                    393:         d[i] = u;
                    394:     }
1.57      noro      395: }
                    396:
1.61      noro      397: int ndl_weight(UINT *d)
1.1       noro      398: {
1.157     noro      399:     UINT t,u;
                    400:     int i,j;
1.1       noro      401:
1.157     noro      402:     if ( current_dl_weight_vector )
                    403:         for ( i = 0, t = 0; i < nd_nvar; i++ ) {
                    404:             u = GET_EXP(d,i);
                    405:             t += MUL_WEIGHT(u,i);
                    406:         }
                    407:     else
                    408:         for ( t = 0, i = nd_exporigin; i < nd_wpd; i++ ) {
                    409:             u = d[i];
                    410:             for ( j = 0; j < nd_epw; j++, u>>=nd_bpe )
                    411:                 t += (u&nd_mask0);
                    412:         }
1.167     noro      413:     if ( nd_module && current_module_weight_vector && MPOS(d) )
                    414:         t += current_module_weight_vector[MPOS(d)];
1.157     noro      415:     return t;
1.1       noro      416: }
                    417:
1.61      noro      418: void ndl_weight_mask(UINT *d)
1.43      noro      419: {
1.157     noro      420:     UINT t,u;
                    421:     UINT *mask;
                    422:     int i,j,k,l;
                    423:
                    424:     l = nd_blockmask->n;
                    425:     for ( k = 0; k < l; k++ ) {
                    426:         mask = nd_blockmask->mask[k];
                    427:         if ( current_dl_weight_vector )
                    428:             for ( i = 0, t = 0; i < nd_nvar; i++ ) {
                    429:                 u = GET_EXP_MASK(d,i,mask);
                    430:                 t += MUL_WEIGHT(u,i);
                    431:             }
                    432:         else
                    433:             for ( t = 0, i = nd_exporigin; i < nd_wpd; i++ ) {
                    434:                 u = d[i]&mask[i];
                    435:                 for ( j = 0; j < nd_epw; j++, u>>=nd_bpe )
                    436:                     t += (u&nd_mask0);
                    437:             }
                    438:         d[k+1] = t;
                    439:     }
1.43      noro      440: }
                    441:
1.61      noro      442: int ndl_lex_compare(UINT *d1,UINT *d2)
1.1       noro      443: {
1.157     noro      444:     int i;
1.1       noro      445:
1.157     noro      446:     d1 += nd_exporigin;
                    447:     d2 += nd_exporigin;
                    448:     for ( i = nd_exporigin; i < nd_wpd; i++, d1++, d2++ )
                    449:         if ( *d1 > *d2 )
                    450:             return nd_isrlex ? -1 : 1;
                    451:         else if ( *d1 < *d2 )
                    452:             return nd_isrlex ? 1 : -1;
                    453:     return 0;
1.1       noro      454: }
                    455:
1.61      noro      456: int ndl_block_compare(UINT *d1,UINT *d2)
1.43      noro      457: {
1.157     noro      458:     int i,l,j,ord_o,ord_l;
                    459:     struct order_pair *op;
                    460:     UINT t1,t2,m;
                    461:     UINT *mask;
                    462:
                    463:     l = nd_blockmask->n;
                    464:     op = nd_blockmask->order_pair;
                    465:     for ( j = 0; j < l; j++ ) {
                    466:         mask = nd_blockmask->mask[j];
                    467:         ord_o = op[j].order;
                    468:         if ( ord_o < 2 )
                    469:             if ( (t1=d1[j+1]) > (t2=d2[j+1]) ) return 1;
                    470:             else if ( t1 < t2 ) return -1;
                    471:         for ( i = nd_exporigin; i < nd_wpd; i++ ) {
                    472:             m = mask[i];
                    473:             t1 = d1[i]&m;
                    474:             t2 = d2[i]&m;
                    475:             if ( t1 > t2 )
                    476:                 return !ord_o ? -1 : 1;
                    477:             else if ( t1 < t2 )
                    478:                 return !ord_o ? 1 : -1;
                    479:         }
                    480:     }
                    481:     return 0;
1.43      noro      482: }
                    483:
1.96      noro      484: int ndl_matrix_compare(UINT *d1,UINT *d2)
                    485: {
1.157     noro      486:     int i,j,s;
                    487:     int *v;
1.96      noro      488:
1.157     noro      489:     for ( j = 0; j < nd_nvar; j++ )
                    490:         nd_work_vector[j] = GET_EXP(d1,j)-GET_EXP(d2,j);
                    491:     for ( i = 0; i < nd_matrix_len; i++ ) {
                    492:         v = nd_matrix[i];
                    493:         for ( j = 0, s = 0; j < nd_nvar; j++ )
                    494:             s += v[j]*nd_work_vector[j];
                    495:         if ( s > 0 ) return 1;
                    496:         else if ( s < 0 ) return -1;
                    497:     }
                    498:     return 0;
1.96      noro      499: }
                    500:
1.97      noro      501: int ndl_composite_compare(UINT *d1,UINT *d2)
                    502: {
1.157     noro      503:     int i,j,s,start,end,len,o;
                    504:     int *v;
                    505:     struct sparse_weight *sw;
                    506:
                    507:     for ( j = 0; j < nd_nvar; j++ )
                    508:         nd_work_vector[j] = GET_EXP(d1,j)-GET_EXP(d2,j);
                    509:     for ( i = 0; i < nd_worb_len; i++ ) {
                    510:         len = nd_worb[i].length;
                    511:         switch ( nd_worb[i].type ) {
                    512:             case IS_DENSE_WEIGHT:
                    513:                 v = nd_worb[i].body.dense_weight;
                    514:                 for ( j = 0, s = 0; j < len; j++ )
                    515:                     s += v[j]*nd_work_vector[j];
                    516:                 if ( s > 0 ) return 1;
                    517:                 else if ( s < 0 ) return -1;
                    518:                 break;
                    519:             case IS_SPARSE_WEIGHT:
                    520:                 sw = nd_worb[i].body.sparse_weight;
                    521:                 for ( j = 0, s = 0; j < len; j++ )
                    522:                     s += sw[j].value*nd_work_vector[sw[j].pos];
                    523:                 if ( s > 0 ) return 1;
                    524:                 else if ( s < 0 ) return -1;
                    525:                 break;
                    526:             case IS_BLOCK:
                    527:                 o = nd_worb[i].body.block.order;
                    528:                 start = nd_worb[i].body.block.start;
                    529:                 switch ( o ) {
                    530:                     case 0:
                    531:                         end = start+len;
                    532:                         for ( j = start, s = 0; j < end; j++ )
                    533:                             s += MUL_WEIGHT(nd_work_vector[j],j);
                    534:                         if ( s > 0 ) return 1;
                    535:                         else if ( s < 0 ) return -1;
                    536:                         for ( j = end-1; j >= start; j-- )
                    537:                             if ( nd_work_vector[j] < 0 ) return 1;
                    538:                             else if ( nd_work_vector[j] > 0 ) return -1;
                    539:                         break;
                    540:                     case 1:
                    541:                         end = start+len;
                    542:                         for ( j = start, s = 0; j < end; j++ )
                    543:                             s += MUL_WEIGHT(nd_work_vector[j],j);
                    544:                         if ( s > 0 ) return 1;
                    545:                         else if ( s < 0 ) return -1;
                    546:                         for ( j = start; j < end; j++ )
                    547:                             if ( nd_work_vector[j] > 0 ) return 1;
                    548:                             else if ( nd_work_vector[j] < 0 ) return -1;
                    549:                         break;
                    550:                     case 2:
                    551:                         for ( j = start; j < end; j++ )
                    552:                             if ( nd_work_vector[j] > 0 ) return 1;
                    553:                             else if ( nd_work_vector[j] < 0 ) return -1;
                    554:                         break;
                    555:                 }
                    556:                 break;
                    557:         }
                    558:     }
                    559:     return 0;
1.97      noro      560: }
                    561:
1.58      noro      562: /* TDH -> WW -> TD-> RL */
                    563:
1.61      noro      564: int ndl_ww_lex_compare(UINT *d1,UINT *d2)
1.58      noro      565: {
1.157     noro      566:     int i,m,e1,e2;
1.58      noro      567:
1.157     noro      568:     if ( TD(d1) > TD(d2) ) return 1;
                    569:     else if ( TD(d1) < TD(d2) ) return -1;
                    570:     m = nd_nvar>>1;
                    571:     for ( i = 0, e1 = e2 = 0; i < m; i++ ) {
                    572:         e1 += current_weyl_weight_vector[i]*(GET_EXP(d1,m+i)-GET_EXP(d1,i));
                    573:         e2 += current_weyl_weight_vector[i]*(GET_EXP(d2,m+i)-GET_EXP(d2,i));
                    574:     }
                    575:     if ( e1 > e2 ) return 1;
                    576:     else if ( e1 < e2 ) return -1;
                    577:     return ndl_lex_compare(d1,d2);
                    578: }
                    579:
                    580: int ndl_module_grlex_compare(UINT *d1,UINT *d2)
                    581: {
1.164     noro      582:     int i,c;
1.157     noro      583:
1.160     noro      584:     if ( nd_ispot ) {
1.174     noro      585:                if ( nd_pot_nelim && MPOS(d1)>=nd_pot_nelim+1 && MPOS(d2) >= nd_pot_nelim+1 ) {
                    586:             if ( TD(d1) > TD(d2) ) return 1;
                    587:             else if ( TD(d1) < TD(d2) ) return -1;
                    588:             if ( c = ndl_lex_compare(d1,d2) ) return c;
                    589:             if ( MPOS(d1) < MPOS(d2) ) return 1;
                    590:             else if ( MPOS(d1) > MPOS(d2) ) return -1;
                    591:             return 0;
                    592:                }
1.157     noro      593:         if ( MPOS(d1) < MPOS(d2) ) return 1;
                    594:         else if ( MPOS(d1) > MPOS(d2) ) return -1;
                    595:     }
                    596:     if ( TD(d1) > TD(d2) ) return 1;
                    597:     else if ( TD(d1) < TD(d2) ) return -1;
1.167     noro      598:     if ( c = ndl_lex_compare(d1,d2) ) return c;
1.160     noro      599:     if ( !nd_ispot ) {
1.157     noro      600:         if ( MPOS(d1) < MPOS(d2) ) return 1;
                    601:         else if ( MPOS(d1) > MPOS(d2) ) return -1;
                    602:     }
                    603:     return 0;
                    604: }
                    605:
                    606: int ndl_module_glex_compare(UINT *d1,UINT *d2)
                    607: {
1.164     noro      608:     int i,c;
1.157     noro      609:
1.160     noro      610:     if ( nd_ispot ) {
1.157     noro      611:         if ( MPOS(d1) < MPOS(d2) ) return 1;
                    612:         else if ( MPOS(d1) > MPOS(d2) ) return -1;
                    613:     }
                    614:     if ( TD(d1) > TD(d2) ) return 1;
                    615:     else if ( TD(d1) < TD(d2) ) return -1;
1.167     noro      616:     if ( c = ndl_lex_compare(d1,d2) ) return c;
1.160     noro      617:     if ( !nd_ispot ) {
1.157     noro      618:         if ( MPOS(d1) < MPOS(d2) ) return 1;
                    619:         else if ( MPOS(d1) > MPOS(d2) ) return -1;
                    620:     }
                    621:     return 0;
                    622: }
                    623:
                    624: int ndl_module_lex_compare(UINT *d1,UINT *d2)
                    625: {
1.164     noro      626:     int i,c;
1.157     noro      627:
1.160     noro      628:     if ( nd_ispot ) {
1.157     noro      629:         if ( MPOS(d1) < MPOS(d2) ) return 1;
                    630:         else if ( MPOS(d1) > MPOS(d2) ) return -1;
                    631:     }
1.167     noro      632:     if ( c = ndl_lex_compare(d1,d2) ) return c;
1.160     noro      633:     if ( !nd_ispot ) {
1.157     noro      634:         if ( MPOS(d1) < MPOS(d2) ) return 1;
                    635:         else if ( MPOS(d1) > MPOS(d2) ) return -1;
                    636:     }
                    637:     return 0;
                    638: }
                    639:
                    640: int ndl_module_block_compare(UINT *d1,UINT *d2)
                    641: {
                    642:     int i,c;
                    643:
1.160     noro      644:     if ( nd_ispot ) {
1.157     noro      645:         if ( MPOS(d1) < MPOS(d2) ) return 1;
                    646:         else if ( MPOS(d1) > MPOS(d2) ) return -1;
                    647:     }
                    648:     if ( c = ndl_block_compare(d1,d2) ) return c;
1.160     noro      649:     if ( !nd_ispot ) {
1.157     noro      650:         if ( MPOS(d1) < MPOS(d2) ) return 1;
                    651:         else if ( MPOS(d1) > MPOS(d2) ) return -1;
                    652:     }
                    653:     return 0;
                    654: }
                    655:
                    656: int ndl_module_matrix_compare(UINT *d1,UINT *d2)
                    657: {
                    658:     int i,c;
                    659:
1.160     noro      660:     if ( nd_ispot ) {
1.157     noro      661:         if ( MPOS(d1) < MPOS(d2) ) return 1;
                    662:         else if ( MPOS(d1) > MPOS(d2) ) return -1;
                    663:     }
                    664:     if ( c = ndl_matrix_compare(d1,d2) ) return c;
1.160     noro      665:     if ( !nd_ispot ) {
1.157     noro      666:         if ( MPOS(d1) < MPOS(d2) ) return 1;
                    667:         else if ( MPOS(d1) > MPOS(d2) ) return -1;
                    668:     }
                    669:     return 0;
                    670: }
                    671:
                    672: int ndl_module_composite_compare(UINT *d1,UINT *d2)
                    673: {
                    674:     int i,c;
                    675:
1.160     noro      676:     if ( nd_ispot ) {
1.157     noro      677:         if ( MPOS(d1) > MPOS(d2) ) return 1;
                    678:         else if ( MPOS(d1) < MPOS(d2) ) return -1;
                    679:     }
                    680:     if ( c = ndl_composite_compare(d1,d2) ) return c;
1.160     noro      681:     if ( !nd_ispot ) {
1.157     noro      682:         if ( MPOS(d1) > MPOS(d2) ) return 1;
                    683:         else if ( MPOS(d1) < MPOS(d2) ) return -1;
                    684:     }
                    685:     return 0;
1.58      noro      686: }
                    687:
1.61      noro      688: INLINE int ndl_equal(UINT *d1,UINT *d2)
1.1       noro      689: {
1.157     noro      690:     int i;
1.1       noro      691:
1.157     noro      692:     switch ( nd_wpd ) {
                    693:         case 2:
                    694:             if ( TD(d2) != TD(d1) ) return 0;
                    695:             if ( d2[1] != d1[1] ) return 0;
                    696:             return 1;
                    697:             break;
                    698:         case 3:
                    699:             if ( TD(d2) != TD(d1) ) return 0;
                    700:             if ( d2[1] != d1[1] ) return 0;
                    701:             if ( d2[2] != d1[2] ) return 0;
                    702:             return 1;
                    703:             break;
                    704:         default:
                    705:             for ( i = 0; i < nd_wpd; i++ )
                    706:                 if ( *d1++ != *d2++ ) return 0;
                    707:             return 1;
                    708:             break;
                    709:     }
1.1       noro      710: }
                    711:
1.61      noro      712: INLINE void ndl_copy(UINT *d1,UINT *d2)
1.6       noro      713: {
1.157     noro      714:     int i;
1.6       noro      715:
1.157     noro      716:     switch ( nd_wpd ) {
                    717:         case 2:
                    718:             TD(d2) = TD(d1);
                    719:             d2[1] = d1[1];
                    720:             break;
                    721:         case 3:
                    722:             TD(d2) = TD(d1);
                    723:             d2[1] = d1[1];
                    724:             d2[2] = d1[2];
                    725:             break;
                    726:         default:
                    727:             for ( i = 0; i < nd_wpd; i++ )
                    728:                 d2[i] = d1[i];
                    729:             break;
                    730:     }
1.6       noro      731: }
                    732:
1.61      noro      733: INLINE void ndl_zero(UINT *d)
                    734: {
1.157     noro      735:     int i;
                    736:     for ( i = 0; i < nd_wpd; i++ ) d[i] = 0;
1.61      noro      737: }
                    738:
                    739: INLINE void ndl_add(UINT *d1,UINT *d2,UINT *d)
1.1       noro      740: {
1.157     noro      741:     int i;
1.1       noro      742:
1.162     noro      743:     if ( nd_module ) {
                    744:         if ( MPOS(d1) && MPOS(d2) && (MPOS(d1) != MPOS(d2)) )
1.167     noro      745:         error("ndl_add : invalid operation");
1.162     noro      746:     }
1.43      noro      747: #if 1
1.157     noro      748:     switch ( nd_wpd ) {
                    749:         case 2:
                    750:             TD(d) = TD(d1)+TD(d2);
                    751:             d[1] = d1[1]+d2[1];
                    752:             break;
                    753:         case 3:
                    754:             TD(d) = TD(d1)+TD(d2);
                    755:             d[1] = d1[1]+d2[1];
                    756:             d[2] = d1[2]+d2[2];
                    757:             break;
                    758:         default:
                    759:             for ( i = 0; i < nd_wpd; i++ ) d[i] = d1[i]+d2[i];
                    760:             break;
                    761:     }
1.43      noro      762: #else
1.157     noro      763:     for ( i = 0; i < nd_wpd; i++ ) d[i] = d1[i]+d2[i];
1.43      noro      764: #endif
1.6       noro      765: }
                    766:
1.55      noro      767: /* d1 += d2 */
1.61      noro      768: INLINE void ndl_addto(UINT *d1,UINT *d2)
1.55      noro      769: {
1.157     noro      770:     int i;
1.55      noro      771:
1.162     noro      772:     if ( nd_module ) {
                    773:         if ( MPOS(d1) && MPOS(d2) && (MPOS(d1) != MPOS(d2)) )
                    774:             error("ndl_addto : invalid operation");
                    775:     }
1.55      noro      776: #if 1
1.157     noro      777:     switch ( nd_wpd ) {
                    778:         case 2:
                    779:             TD(d1) += TD(d2);
                    780:             d1[1] += d2[1];
                    781:             break;
                    782:         case 3:
                    783:             TD(d1) += TD(d2);
                    784:             d1[1] += d2[1];
                    785:             d1[2] += d2[2];
                    786:             break;
                    787:         default:
                    788:             for ( i = 0; i < nd_wpd; i++ ) d1[i] += d2[i];
                    789:             break;
                    790:     }
1.55      noro      791: #else
1.157     noro      792:     for ( i = 0; i < nd_wpd; i++ ) d1[i] += d2[i];
1.55      noro      793: #endif
                    794: }
                    795:
1.61      noro      796: INLINE void ndl_sub(UINT *d1,UINT *d2,UINT *d)
1.6       noro      797: {
1.157     noro      798:     int i;
1.6       noro      799:
1.157     noro      800:     for ( i = 0; i < nd_wpd; i++ ) d[i] = d1[i]-d2[i];
1.1       noro      801: }
                    802:
1.61      noro      803: int ndl_disjoint(UINT *d1,UINT *d2)
1.1       noro      804: {
1.157     noro      805:     UINT t1,t2,u,u1,u2;
                    806:     int i,j;
1.1       noro      807:
1.157     noro      808:     if ( nd_module && (MPOS(d1) == MPOS(d2)) ) return 0;
1.65      noro      809: #if USE_UNROLL
1.157     noro      810:     switch ( nd_bpe ) {
                    811:         case 3:
                    812:             for ( i = nd_exporigin; i < nd_wpd; i++ ) {
                    813:                 u1 = d1[i]; u2 = d2[i];
                    814:                 t1 = u1&0x38000000; t2 = u2&0x38000000; if ( t1&&t2 ) return 0;
                    815:                 t1 = u1& 0x7000000; t2 = u2& 0x7000000; if ( t1&&t2 ) return 0;
                    816:                 t1 = u1&  0xe00000; t2 = u2&  0xe00000; if ( t1&&t2 ) return 0;
                    817:                 t1 = u1&  0x1c0000; t2 = u2&  0x1c0000; if ( t1&&t2 ) return 0;
                    818:                 t1 = u1&   0x38000; t2 = u2&   0x38000; if ( t1&&t2 ) return 0;
                    819:                 t1 = u1&    0x7000; t2 = u2&    0x7000; if ( t1&&t2 ) return 0;
                    820:                 t1 = u1&     0xe00; t2 = u2&     0xe00; if ( t1&&t2 ) return 0;
                    821:                 t1 = u1&     0x1c0; t2 = u2&     0x1c0; if ( t1&&t2 ) return 0;
                    822:                 t1 = u1&      0x38; t2 = u2&      0x38; if ( t1&&t2 ) return 0;
                    823:                 t1 = u1&       0x7; t2 = u2&       0x7; if ( t1&&t2 ) return 0;
                    824:             }
                    825:             return 1;
                    826:             break;
                    827:         case 4:
                    828:             for ( i = nd_exporigin; i < nd_wpd; i++ ) {
                    829:                 u1 = d1[i]; u2 = d2[i];
                    830:                 t1 = u1&0xf0000000; t2 = u2&0xf0000000; if ( t1&&t2 ) return 0;
                    831:                 t1 = u1& 0xf000000; t2 = u2& 0xf000000; if ( t1&&t2 ) return 0;
                    832:                 t1 = u1&  0xf00000; t2 = u2&  0xf00000; if ( t1&&t2 ) return 0;
                    833:                 t1 = u1&   0xf0000; t2 = u2&   0xf0000; if ( t1&&t2 ) return 0;
                    834:                 t1 = u1&    0xf000; t2 = u2&    0xf000; if ( t1&&t2 ) return 0;
                    835:                 t1 = u1&     0xf00; t2 = u2&     0xf00; if ( t1&&t2 ) return 0;
                    836:                 t1 = u1&      0xf0; t2 = u2&      0xf0; if ( t1&&t2 ) return 0;
                    837:                 t1 = u1&       0xf; t2 = u2&       0xf; if ( t1&&t2 ) return 0;
                    838:             }
                    839:             return 1;
                    840:             break;
                    841:         case 6:
                    842:             for ( i = nd_exporigin; i < nd_wpd; i++ ) {
                    843:                 u1 = d1[i]; u2 = d2[i];
                    844:                 t1 = u1&0x3f000000; t2 = u2&0x3f000000; if ( t1&&t2 ) return 0;
                    845:                 t1 = u1&  0xfc0000; t2 = u2&  0xfc0000; if ( t1&&t2 ) return 0;
                    846:                 t1 = u1&   0x3f000; t2 = u2&   0x3f000; if ( t1&&t2 ) return 0;
                    847:                 t1 = u1&     0xfc0; t2 = u2&     0xfc0; if ( t1&&t2 ) return 0;
                    848:                 t1 = u1&      0x3f; t2 = u2&      0x3f; if ( t1&&t2 ) return 0;
                    849:             }
                    850:             return 1;
                    851:             break;
                    852:         case 8:
                    853:             for ( i = nd_exporigin; i < nd_wpd; i++ ) {
                    854:                 u1 = d1[i]; u2 = d2[i];
                    855:                 t1 = u1&0xff000000; t2 = u2&0xff000000; if ( t1&&t2 ) return 0;
                    856:                 t1 = u1&  0xff0000; t2 = u2&  0xff0000; if ( t1&&t2 ) return 0;
                    857:                 t1 = u1&    0xff00; t2 = u2&    0xff00; if ( t1&&t2 ) return 0;
                    858:                 t1 = u1&      0xff; t2 = u2&      0xff; if ( t1&&t2 ) return 0;
                    859:             }
                    860:             return 1;
                    861:             break;
                    862:         case 16:
                    863:             for ( i = nd_exporigin; i < nd_wpd; i++ ) {
                    864:                 u1 = d1[i]; u2 = d2[i];
                    865:                 t1 = u1&0xffff0000; t2 = u2&0xffff0000; if ( t1&&t2 ) return 0;
                    866:                 t1 = u1&    0xffff; t2 = u2&    0xffff; if ( t1&&t2 ) return 0;
                    867:             }
                    868:             return 1;
                    869:             break;
                    870:         case 32:
                    871:             for ( i = nd_exporigin; i < nd_wpd; i++ )
                    872:                 if ( d1[i] && d2[i] ) return 0;
                    873:             return 1;
                    874:             break;
                    875:         default:
                    876:             for ( i = nd_exporigin; i < nd_wpd; i++ ) {
                    877:                 u1 = d1[i]; u2 = d2[i];
                    878:                 for ( j = 0; j < nd_epw; j++ ) {
                    879:                     if ( (u1&nd_mask0) && (u2&nd_mask0) ) return 0;
                    880:                     u1 >>= nd_bpe; u2 >>= nd_bpe;
                    881:                 }
                    882:             }
                    883:             return 1;
                    884:             break;
                    885:     }
1.65      noro      886: #else
1.157     noro      887:     for ( i = nd_exporigin; i < nd_wpd; i++ ) {
                    888:         u1 = d1[i]; u2 = d2[i];
                    889:         for ( j = 0; j < nd_epw; j++ ) {
                    890:             if ( (u1&nd_mask0) && (u2&nd_mask0) ) return 0;
                    891:             u1 >>= nd_bpe; u2 >>= nd_bpe;
                    892:         }
                    893:     }
                    894:     return 1;
1.65      noro      895: #endif
1.1       noro      896: }
                    897:
1.114     noro      898: int ndl_check_bound(UINT *d1,UINT *d2)
1.1       noro      899: {
1.157     noro      900:     UINT u2;
                    901:     int i,j,ind,k;
1.1       noro      902:
1.157     noro      903:     ind = 0;
1.65      noro      904: #if USE_UNROLL
1.157     noro      905:     switch ( nd_bpe ) {
                    906:         case 3:
                    907:             for ( i = nd_exporigin; i < nd_wpd; i++ ) {
                    908:                 u2 = d2[i];
                    909:                 if ( d1[ind++]+((u2>>27)&0x7) >= 0x8 ) return 1;
                    910:                 if ( d1[ind++]+((u2>>24)&0x7) >= 0x8 ) return 1;
                    911:                 if ( d1[ind++]+((u2>>21)&0x7) >= 0x8 ) return 1;
                    912:                 if ( d1[ind++]+((u2>>18)&0x7) >= 0x8 ) return 1;
                    913:                 if ( d1[ind++]+((u2>>15)&0x7) >= 0x8 ) return 1;
                    914:                 if ( d1[ind++]+((u2>>12)&0x7) >= 0x8 ) return 1;
                    915:                 if ( d1[ind++]+((u2>>9)&0x7) >= 0x8 ) return 1;
                    916:                 if ( d1[ind++]+((u2>>6)&0x7) >= 0x8 ) return 1;
                    917:                 if ( d1[ind++]+((u2>>3)&0x7) >= 0x8 ) return 1;
                    918:                 if ( d1[ind++]+(u2&0x7) >= 0x8 ) return 1;
                    919:             }
                    920:             return 0;
                    921:             break;
                    922:         case 4:
                    923:             for ( i = nd_exporigin; i < nd_wpd; i++ ) {
                    924:                 u2 = d2[i];
                    925:                 if ( d1[ind++]+((u2>>28)&0xf) >= 0x10 ) return 1;
                    926:                 if ( d1[ind++]+((u2>>24)&0xf) >= 0x10 ) return 1;
                    927:                 if ( d1[ind++]+((u2>>20)&0xf) >= 0x10 ) return 1;
                    928:                 if ( d1[ind++]+((u2>>16)&0xf) >= 0x10 ) return 1;
                    929:                 if ( d1[ind++]+((u2>>12)&0xf) >= 0x10 ) return 1;
                    930:                 if ( d1[ind++]+((u2>>8)&0xf) >= 0x10 ) return 1;
                    931:                 if ( d1[ind++]+((u2>>4)&0xf) >= 0x10 ) return 1;
                    932:                 if ( d1[ind++]+(u2&0xf) >= 0x10 ) return 1;
                    933:             }
                    934:             return 0;
                    935:             break;
                    936:         case 6:
                    937:             for ( i = nd_exporigin; i < nd_wpd; i++ ) {
                    938:                 u2 = d2[i];
                    939:                 if ( d1[ind++]+((u2>>24)&0x3f) >= 0x40 ) return 1;
                    940:                 if ( d1[ind++]+((u2>>18)&0x3f) >= 0x40 ) return 1;
                    941:                 if ( d1[ind++]+((u2>>12)&0x3f) >= 0x40 ) return 1;
                    942:                 if ( d1[ind++]+((u2>>6)&0x3f) >= 0x40 ) return 1;
                    943:                 if ( d1[ind++]+(u2&0x3f) >= 0x40 ) return 1;
                    944:             }
                    945:             return 0;
                    946:             break;
                    947:         case 8:
                    948:             for ( i = nd_exporigin; i < nd_wpd; i++ ) {
                    949:                 u2 = d2[i];
                    950:                 if ( d1[ind++]+((u2>>24)&0xff) >= 0x100 ) return 1;
                    951:                 if ( d1[ind++]+((u2>>16)&0xff) >= 0x100 ) return 1;
                    952:                 if ( d1[ind++]+((u2>>8)&0xff) >= 0x100 ) return 1;
                    953:                 if ( d1[ind++]+(u2&0xff) >= 0x100 ) return 1;
                    954:             }
                    955:             return 0;
                    956:             break;
                    957:         case 16:
                    958:             for ( i = nd_exporigin; i < nd_wpd; i++ ) {
                    959:                 u2 = d2[i];
                    960:                 if ( d1[ind++]+((u2>>16)&0xffff) > 0x10000 ) return 1;
                    961:                 if ( d1[ind++]+(u2&0xffff) > 0x10000 ) return 1;
                    962:             }
                    963:             return 0;
                    964:             break;
                    965:         case 32:
                    966:             for ( i = nd_exporigin; i < nd_wpd; i++ )
                    967:                 if ( d1[i]+d2[i]<d1[i] ) return 1;
                    968:             return 0;
                    969:             break;
                    970:         default:
                    971:             for ( i = nd_exporigin; i < nd_wpd; i++ ) {
                    972:                 u2 = d2[i];
                    973:                 k = (nd_epw-1)*nd_bpe;
                    974:                 for ( j = 0; j < nd_epw; j++, k -= nd_bpe )
                    975:                     if ( d1[ind++]+((u2>>k)&nd_mask0) > nd_mask0 ) return 1;
                    976:             }
                    977:             return 0;
                    978:             break;
                    979:     }
1.65      noro      980: #else
1.157     noro      981:     for ( i = nd_exporigin; i < nd_wpd; i++ ) {
                    982:         u2 = d2[i];
                    983:         k = (nd_epw-1)*nd_bpe;
                    984:         for ( j = 0; j < nd_epw; j++, k -= nd_bpe )
                    985:             if ( d1[ind++]+((u2>>k)&nd_mask0) > nd_mask0 ) return 1;
                    986:     }
                    987:     return 0;
1.65      noro      988: #endif
1.1       noro      989: }
                    990:
1.114     noro      991: int ndl_check_bound2(int index,UINT *d2)
                    992: {
1.157     noro      993:     return ndl_check_bound(nd_bound[index],d2);
1.114     noro      994: }
                    995:
1.61      noro      996: INLINE int ndl_hash_value(UINT *d)
1.1       noro      997: {
1.157     noro      998:     int i;
                    999:     int r;
1.1       noro     1000:
1.157     noro     1001:     r = 0;
                   1002:     for ( i = 0; i < nd_wpd; i++ )
                   1003:         r = ((r<<16)+d[i])%REDTAB_LEN;
                   1004:     return r;
1.1       noro     1005: }
                   1006:
1.63      noro     1007: INLINE int ndl_find_reducer(UINT *dg)
1.1       noro     1008: {
1.157     noro     1009:     RHist r;
                   1010:     int d,k,i;
1.1       noro     1011:
1.157     noro     1012:     d = ndl_hash_value(dg);
                   1013:     for ( r = nd_red[d], k = 0; r; r = NEXT(r), k++ ) {
                   1014:         if ( ndl_equal(dg,DL(r)) ) {
                   1015:             if ( k > 0 ) nd_notfirst++;
                   1016:             nd_found++;
                   1017:             return r->index;
                   1018:         }
                   1019:     }
                   1020:     if ( Reverse )
                   1021:         for ( i = nd_psn-1; i >= 0; i-- ) {
                   1022:             r = nd_psh[i];
                   1023:             if ( ndl_reducible(dg,DL(r)) ) {
                   1024:                 nd_create++;
                   1025:                 nd_append_red(dg,i);
                   1026:                 return i;
                   1027:             }
                   1028:         }
                   1029:     else
                   1030:         for ( i = 0; i < nd_psn; i++ ) {
                   1031:             r = nd_psh[i];
                   1032:             if ( ndl_reducible(dg,DL(r)) ) {
                   1033:                 nd_create++;
                   1034:                 nd_append_red(dg,i);
                   1035:                 return i;
                   1036:             }
                   1037:         }
                   1038:     return -1;
1.1       noro     1039: }
                   1040:
1.63      noro     1041: ND nd_merge(ND p1,ND p2)
                   1042: {
1.157     noro     1043:     int n,c;
                   1044:     int t,can,td1,td2;
                   1045:     ND r;
                   1046:     NM m1,m2,mr0,mr,s;
                   1047:
                   1048:     if ( !p1 ) return p2;
                   1049:     else if ( !p2 ) return p1;
                   1050:     else {
                   1051:         can = 0;
                   1052:         for ( n = NV(p1), m1 = BDY(p1), m2 = BDY(p2), mr0 = 0; m1 && m2; ) {
                   1053:             c = DL_COMPARE(DL(m1),DL(m2));
                   1054:             switch ( c ) {
                   1055:                 case 0:
                   1056:                     s = m1; m1 = NEXT(m1);
                   1057:                     can++; NEXTNM2(mr0,mr,s);
                   1058:                     s = m2; m2 = NEXT(m2); FREENM(s);
                   1059:                     break;
                   1060:                 case 1:
                   1061:                     s = m1; m1 = NEXT(m1); NEXTNM2(mr0,mr,s);
                   1062:                     break;
                   1063:                 case -1:
                   1064:                     s = m2; m2 = NEXT(m2); NEXTNM2(mr0,mr,s);
                   1065:                     break;
                   1066:             }
                   1067:         }
                   1068:         if ( !mr0 )
                   1069:             if ( m1 ) mr0 = m1;
                   1070:             else if ( m2 ) mr0 = m2;
                   1071:             else return 0;
                   1072:         else if ( m1 ) NEXT(mr) = m1;
                   1073:         else if ( m2 ) NEXT(mr) = m2;
                   1074:         else NEXT(mr) = 0;
                   1075:         BDY(p1) = mr0;
                   1076:         SG(p1) = MAX(SG(p1),SG(p2));
                   1077:         LEN(p1) = LEN(p1)+LEN(p2)-can;
                   1078:         FREEND(p2);
                   1079:         return p1;
                   1080:     }
1.63      noro     1081: }
                   1082:
1.31      noro     1083: ND nd_add(int mod,ND p1,ND p2)
1.1       noro     1084: {
1.157     noro     1085:     int n,c;
                   1086:     int t,can,td1,td2;
                   1087:     ND r;
                   1088:     NM m1,m2,mr0,mr,s;
                   1089:
                   1090:     if ( !p1 ) return p2;
                   1091:     else if ( !p2 ) return p1;
                   1092:     else if ( mod == -1 ) return nd_add_sf(p1,p2);
                   1093:     else if ( !mod ) return nd_add_q(p1,p2);
                   1094:     else {
                   1095:         can = 0;
                   1096:         for ( n = NV(p1), m1 = BDY(p1), m2 = BDY(p2), mr0 = 0; m1 && m2; ) {
                   1097:             c = DL_COMPARE(DL(m1),DL(m2));
                   1098:             switch ( c ) {
                   1099:                 case 0:
                   1100:                     t = ((CM(m1))+(CM(m2))) - mod;
                   1101:                     if ( t < 0 ) t += mod;
                   1102:                     s = m1; m1 = NEXT(m1);
                   1103:                     if ( t ) {
                   1104:                         can++; NEXTNM2(mr0,mr,s); CM(mr) = (t);
                   1105:                     } else {
                   1106:                         can += 2; FREENM(s);
                   1107:                     }
                   1108:                     s = m2; m2 = NEXT(m2); FREENM(s);
                   1109:                     break;
                   1110:                 case 1:
                   1111:                     s = m1; m1 = NEXT(m1); NEXTNM2(mr0,mr,s);
                   1112:                     break;
                   1113:                 case -1:
                   1114:                     s = m2; m2 = NEXT(m2); NEXTNM2(mr0,mr,s);
                   1115:                     break;
                   1116:             }
                   1117:         }
                   1118:         if ( !mr0 )
                   1119:             if ( m1 ) mr0 = m1;
                   1120:             else if ( m2 ) mr0 = m2;
                   1121:             else return 0;
                   1122:         else if ( m1 ) NEXT(mr) = m1;
                   1123:         else if ( m2 ) NEXT(mr) = m2;
                   1124:         else NEXT(mr) = 0;
                   1125:         BDY(p1) = mr0;
                   1126:         SG(p1) = MAX(SG(p1),SG(p2));
                   1127:         LEN(p1) = LEN(p1)+LEN(p2)-can;
                   1128:         FREEND(p2);
                   1129:         return p1;
                   1130:     }
1.95      noro     1131: }
                   1132:
                   1133: /* XXX on opteron, the inlined manipulation of destructive additon of
                   1134:  * two NM seems to make gcc optimizer get confused, so the part is
                   1135:  * done in a function.
                   1136:  */
                   1137:
1.113     noro     1138: int nm_destructive_add_q(NM *m1,NM *m2,NM *mr0,NM *mr)
1.95      noro     1139: {
1.157     noro     1140:     NM s;
                   1141:     P t;
                   1142:     int can;
                   1143:
                   1144:     addp(nd_vc,CP(*m1),CP(*m2),&t);
                   1145:     s = *m1; *m1 = NEXT(*m1);
                   1146:     if ( t ) {
                   1147:         can = 1; NEXTNM2(*mr0,*mr,s); CP(*mr) = (t);
                   1148:     } else {
                   1149:         can = 2; FREENM(s);
                   1150:     }
                   1151:     s = *m2; *m2 = NEXT(*m2); FREENM(s);
                   1152:     return can;
1.95      noro     1153: }
                   1154:
1.113     noro     1155: ND nd_add_q(ND p1,ND p2)
1.95      noro     1156: {
1.157     noro     1157:     int n,c,can;
                   1158:     ND r;
                   1159:     NM m1,m2,mr0,mr,s;
                   1160:     P t;
                   1161:
                   1162:     if ( !p1 ) return p2;
                   1163:     else if ( !p2 ) return p1;
                   1164:     else {
                   1165:         can = 0;
                   1166:         for ( n = NV(p1), m1 = BDY(p1), m2 = BDY(p2), mr0 = 0; m1 && m2; ) {
                   1167:             c = DL_COMPARE(DL(m1),DL(m2));
                   1168:             switch ( c ) {
                   1169:                 case 0:
1.95      noro     1170: #if defined(__x86_64__)
1.157     noro     1171:                     can += nm_destructive_add_q(&m1,&m2,&mr0,&mr);
1.95      noro     1172: #else
1.157     noro     1173:                     addp(nd_vc,CP(m1),CP(m2),&t);
                   1174:                     s = m1; m1 = NEXT(m1);
                   1175:                     if ( t ) {
                   1176:                         can++; NEXTNM2(mr0,mr,s); CP(mr) = (t);
                   1177:                     } else {
                   1178:                         can += 2; FREENM(s);
                   1179:                     }
                   1180:                     s = m2; m2 = NEXT(m2); FREENM(s);
1.95      noro     1181: #endif
1.157     noro     1182:                     break;
                   1183:                 case 1:
                   1184:                     s = m1; m1 = NEXT(m1); NEXTNM2(mr0,mr,s);
                   1185:                     break;
                   1186:                 case -1:
                   1187:                     s = m2; m2 = NEXT(m2); NEXTNM2(mr0,mr,s);
                   1188:                     break;
                   1189:             }
                   1190:         }
                   1191:         if ( !mr0 )
                   1192:             if ( m1 ) mr0 = m1;
                   1193:             else if ( m2 ) mr0 = m2;
                   1194:             else return 0;
                   1195:         else if ( m1 ) NEXT(mr) = m1;
                   1196:         else if ( m2 ) NEXT(mr) = m2;
                   1197:         else NEXT(mr) = 0;
                   1198:         BDY(p1) = mr0;
                   1199:         SG(p1) = MAX(SG(p1),SG(p2));
                   1200:         LEN(p1) = LEN(p1)+LEN(p2)-can;
                   1201:         FREEND(p2);
                   1202:         return p1;
                   1203:     }
1.17      noro     1204: }
                   1205:
1.71      noro     1206: ND nd_add_sf(ND p1,ND p2)
                   1207: {
1.157     noro     1208:     int n,c,can;
                   1209:     ND r;
                   1210:     NM m1,m2,mr0,mr,s;
                   1211:     int t;
                   1212:
                   1213:     if ( !p1 ) return p2;
                   1214:     else if ( !p2 ) return p1;
                   1215:     else {
                   1216:         can = 0;
                   1217:         for ( n = NV(p1), m1 = BDY(p1), m2 = BDY(p2), mr0 = 0; m1 && m2; ) {
                   1218:             c = DL_COMPARE(DL(m1),DL(m2));
                   1219:             switch ( c ) {
                   1220:                 case 0:
                   1221:                     t = _addsf(CM(m1),CM(m2));
                   1222:                     s = m1; m1 = NEXT(m1);
                   1223:                     if ( t ) {
                   1224:                         can++; NEXTNM2(mr0,mr,s); CM(mr) = (t);
                   1225:                     } else {
                   1226:                         can += 2; FREENM(s);
                   1227:                     }
                   1228:                     s = m2; m2 = NEXT(m2); FREENM(s);
                   1229:                     break;
                   1230:                 case 1:
                   1231:                     s = m1; m1 = NEXT(m1); NEXTNM2(mr0,mr,s);
                   1232:                     break;
                   1233:                 case -1:
                   1234:                     s = m2; m2 = NEXT(m2); NEXTNM2(mr0,mr,s);
                   1235:                     break;
                   1236:             }
                   1237:         }
                   1238:         if ( !mr0 )
                   1239:             if ( m1 ) mr0 = m1;
                   1240:             else if ( m2 ) mr0 = m2;
                   1241:             else return 0;
                   1242:         else if ( m1 ) NEXT(mr) = m1;
                   1243:         else if ( m2 ) NEXT(mr) = m2;
                   1244:         else NEXT(mr) = 0;
                   1245:         BDY(p1) = mr0;
                   1246:         SG(p1) = MAX(SG(p1),SG(p2));
                   1247:         LEN(p1) = LEN(p1)+LEN(p2)-can;
                   1248:         FREEND(p2);
                   1249:         return p1;
                   1250:     }
1.71      noro     1251: }
                   1252:
1.167     noro     1253: ND nd_reduce2(int mod,ND d,ND g,NDV p,NM mul,NDC dn,Obj *divp)
1.146     noro     1254: {
1.157     noro     1255:     int c,c1,c2;
                   1256:     Q cg,cred,gcd,tq;
                   1257:     P cgp,credp,gcdp;
                   1258:     Obj tr,tr1;
                   1259:
1.167     noro     1260:     if ( mod == -1 ) {
1.157     noro     1261:         CM(mul) = _mulsf(_invsf(HCM(p)),_chsgnsf(HCM(g)));
1.167     noro     1262:         *divp = (Obj)ONE;
                   1263:     } else if ( mod ) {
1.157     noro     1264:         c1 = invm(HCM(p),mod); c2 = mod-HCM(g);
                   1265:         DMAR(c1,c2,0,mod,c); CM(mul) = c;
1.167     noro     1266:         *divp = (Obj)ONE;
1.157     noro     1267:     } else if ( nd_vc ) {
                   1268:         ezgcdpz(nd_vc,HCP(g),HCP(p),&gcdp);
                   1269:         divsp(nd_vc,HCP(g),gcdp,&cgp); divsp(nd_vc,HCP(p),gcdp,&credp);
                   1270:         chsgnp(cgp,&CP(mul));
                   1271:         nd_mul_c_q(d,credp); nd_mul_c_q(g,credp);
                   1272:         if ( dn ) {
                   1273:             mulr(nd_vc,(Obj)dn->r,(Obj)credp,&tr);
                   1274:             reductr(nd_vc,tr,&tr1); dn->r = (R)tr1;
                   1275:         }
1.167     noro     1276:         *divp = (Obj)credp;
1.157     noro     1277:     } else {
                   1278:         igcd_cofactor(HCQ(g),HCQ(p),&gcd,&cg,&cred);
                   1279:         chsgnq(cg,&CQ(mul));
                   1280:         nd_mul_c_q(d,(P)cred); nd_mul_c_q(g,(P)cred);
                   1281:         if ( dn ) {
                   1282:             mulq(dn->z,cred,&tq); dn->z = tq;
                   1283:         }
1.167     noro     1284:         *divp = (Obj)cred;
1.157     noro     1285:     }
                   1286:     return nd_add(mod,g,ndv_mul_nm(mod,mul,p));
1.146     noro     1287: }
                   1288:
1.1       noro     1289: /* ret=1 : success, ret=0 : overflow */
1.146     noro     1290: int nd_nf(int mod,ND d,ND g,NDV *ps,int full,NDC dn,ND *rp)
1.1       noro     1291: {
1.157     noro     1292:     NM m,mrd,tail;
                   1293:     NM mul;
                   1294:     int n,sugar,psugar,sugar0,stat,index;
                   1295:     int c,c1,c2,dummy;
                   1296:     RHist h;
                   1297:     NDV p,red;
1.167     noro     1298:     Q cg,cred,gcd,tq,qq,iq;
                   1299:     DP dmul;
                   1300:     NODE node;
                   1301:     LIST hist;
1.157     noro     1302:     double hmag;
                   1303:     P tp,tp1;
1.167     noro     1304:     Obj tr,tr1,div;
                   1305:     union oNDC hg;
                   1306:     P cont;
1.157     noro     1307:
                   1308:     if ( dn ) {
                   1309:         if ( mod )
                   1310:             dn->m = 1;
                   1311:         else if ( nd_vc )
                   1312:             dn->r = (R)ONE;
                   1313:         else
                   1314:             dn->z = ONE;
                   1315:     }
                   1316:     if ( !g ) {
                   1317:         *rp = d;
                   1318:         return 1;
                   1319:     }
                   1320:     if ( !mod ) hmag = ((double)p_mag(HCP(g)))*nd_scale;
                   1321:
                   1322:     sugar0 = sugar = SG(g);
                   1323:     n = NV(g);
                   1324:     mul = (NM)ALLOCA(sizeof(struct oNM)+(nd_wpd-1)*sizeof(UINT));
                   1325:     if ( d )
                   1326:         for ( tail = BDY(d); NEXT(tail); tail = NEXT(tail) );
                   1327:     for ( ; g; ) {
                   1328:         index = ndl_find_reducer(HDL(g));
                   1329:         if ( index >= 0 ) {
                   1330:             h = nd_psh[index];
                   1331:             ndl_sub(HDL(g),DL(h),DL(mul));
                   1332:             if ( ndl_check_bound2(index,DL(mul)) ) {
                   1333:                 nd_free(g); nd_free(d);
                   1334:                 return 0;
                   1335:             }
                   1336:             p = nd_demand ? ndv_load(index) : ps[index];
1.167     noro     1337:             /* d+g -> div*(d+g)+mul*p */
                   1338:             g = nd_reduce2(mod,d,g,p,mul,dn,&div);
1.172     noro     1339:             if ( nd_gentrace ) {
1.167     noro     1340:                 /* Trace=[div,index,mul,ONE] */
                   1341:                 STOQ(index,iq);
                   1342:                 nmtodp(mod,mul,&dmul);
                   1343:                 node = mknode(4,div,iq,dmul,ONE);
                   1344:             }
1.157     noro     1345:             sugar = MAX(sugar,SG(p)+TD(DL(mul)));
1.193     noro     1346:             if ( !mod && g && !nd_vc && ((double)(p_mag(HCP(g))) > hmag) ) {
1.167     noro     1347:                 hg = HCU(g);
1.157     noro     1348:                 nd_removecont2(d,g);
1.172     noro     1349:                 if ( dn || nd_gentrace ) {
1.167     noro     1350:                     /* overwrite cont : Trace=[div,index,mul,cont] */
                   1351:                     cont = ndc_div(mod,hg,HCU(g));
                   1352:                     if ( dn ) {
                   1353:                         if ( nd_vc ) {
                   1354:                             divr(nd_vc,(Obj)dn->r,(Obj)cont,&tr);
                   1355:                             reductr(nd_vc,(Obj)tr,&tr1); dn->r = (R)tr1;
                   1356:                         } else divq(dn->z,(Q)cont,&dn->z);
1.157     noro     1357:                     }
1.172     noro     1358:                     if ( nd_gentrace && !UNIQ(cont) ) ARG3(node) = (pointer)cont;
1.157     noro     1359:                 }
                   1360:                 hmag = ((double)p_mag(HCP(g)))*nd_scale;
                   1361:             }
1.167     noro     1362:             MKLIST(hist,node);
                   1363:             MKNODE(node,hist,nd_tracelist); nd_tracelist = node;
1.157     noro     1364:         } else if ( !full ) {
                   1365:             *rp = g;
                   1366:             return 1;
                   1367:         } else {
                   1368:             m = BDY(g);
                   1369:             if ( NEXT(m) ) {
                   1370:                 BDY(g) = NEXT(m); NEXT(m) = 0; LEN(g)--;
                   1371:             } else {
                   1372:                 FREEND(g); g = 0;
                   1373:             }
                   1374:             if ( d ) {
                   1375:                 NEXT(tail)=m; tail=m; LEN(d)++;
                   1376:             } else {
                   1377:                 MKND(n,m,1,d); tail = BDY(d);
                   1378:             }
                   1379:         }
                   1380:     }
                   1381:     if ( d ) SG(d) = sugar;
                   1382:     *rp = d;
                   1383:     return 1;
1.1       noro     1384: }
1.28      noro     1385:
1.53      noro     1386: int nd_nf_pbucket(int mod,ND g,NDV *ps,int full,ND *rp)
1.25      noro     1387: {
1.157     noro     1388:     int hindex,index;
                   1389:     NDV p;
                   1390:     ND u,d,red;
                   1391:     NODE l;
                   1392:     NM mul,m,mrd,tail;
                   1393:     int sugar,psugar,n,h_reducible;
                   1394:     PGeoBucket bucket;
                   1395:     int c,c1,c2;
                   1396:     Q cg,cred,gcd,zzz;
                   1397:     RHist h;
                   1398:     double hmag,gmag;
                   1399:     int count = 0;
                   1400:     int hcount = 0;
                   1401:
                   1402:     if ( !g ) {
                   1403:         *rp = 0;
                   1404:         return 1;
                   1405:     }
                   1406:     sugar = SG(g);
                   1407:     n = NV(g);
                   1408:     if ( !mod ) hmag = ((double)p_mag((P)HCQ(g)))*nd_scale;
                   1409:     bucket = create_pbucket();
                   1410:     add_pbucket(mod,bucket,g);
                   1411:     d = 0;
                   1412:     mul = (NM)ALLOCA(sizeof(struct oNM)+(nd_wpd-1)*sizeof(UINT));
                   1413:     while ( 1 ) {
                   1414:         hindex = mod?head_pbucket(mod,bucket):head_pbucket_q(bucket);
                   1415:         if ( hindex < 0 ) {
                   1416:             if ( DP_Print > 3 ) printf("(%d %d)",count,hcount);
                   1417:             if ( d ) SG(d) = sugar;
                   1418:             *rp = d;
                   1419:             return 1;
                   1420:         }
                   1421:         g = bucket->body[hindex];
                   1422:         index = ndl_find_reducer(HDL(g));
                   1423:         if ( index >= 0 ) {
                   1424:             count++;
                   1425:             if ( !d ) hcount++;
                   1426:             h = nd_psh[index];
                   1427:             ndl_sub(HDL(g),DL(h),DL(mul));
                   1428:             if ( ndl_check_bound2(index,DL(mul)) ) {
                   1429:                 nd_free(d);
                   1430:                 free_pbucket(bucket);
                   1431:                 *rp = 0;
                   1432:                 return 0;
                   1433:             }
                   1434:             p = ps[index];
                   1435:             if ( mod == -1 )
                   1436:                 CM(mul) = _mulsf(_invsf(HCM(p)),_chsgnsf(HCM(g)));
                   1437:              else if ( mod ) {
                   1438:                 c1 = invm(HCM(p),mod); c2 = mod-HCM(g);
                   1439:                 DMAR(c1,c2,0,mod,c); CM(mul) = c;
                   1440:             } else {
                   1441:                 igcd_cofactor(HCQ(g),HCQ(p),&gcd,&cg,&cred);
                   1442:                 chsgnq(cg,&CQ(mul));
                   1443:                 nd_mul_c_q(d,(P)cred);
                   1444:                 mulq_pbucket(bucket,cred);
                   1445:                 g = bucket->body[hindex];
                   1446:                 gmag = (double)p_mag((P)HCQ(g));
                   1447:             }
                   1448:             red = ndv_mul_nm(mod,mul,p);
                   1449:             bucket->body[hindex] = nd_remove_head(g);
                   1450:             red = nd_remove_head(red);
                   1451:             add_pbucket(mod,bucket,red);
                   1452:             psugar = SG(p)+TD(DL(mul));
                   1453:             sugar = MAX(sugar,psugar);
                   1454:             if ( !mod && hmag && (gmag > hmag) ) {
                   1455:                 g = normalize_pbucket(mod,bucket);
                   1456:                 if ( !g ) {
                   1457:                     if ( d ) SG(d) = sugar;
                   1458:                     *rp = d;
                   1459:                     return 1;
                   1460:                 }
                   1461:                 nd_removecont2(d,g);
                   1462:                 hmag = ((double)p_mag((P)HCQ(g)))*nd_scale;
                   1463:                 add_pbucket(mod,bucket,g);
                   1464:             }
                   1465:         } else if ( !full ) {
                   1466:             g = normalize_pbucket(mod,bucket);
                   1467:             if ( g ) SG(g) = sugar;
                   1468:             *rp = g;
                   1469:             return 1;
                   1470:         } else {
                   1471:             m = BDY(g);
                   1472:             if ( NEXT(m) ) {
                   1473:                 BDY(g) = NEXT(m); NEXT(m) = 0; LEN(g)--;
                   1474:             } else {
                   1475:                 FREEND(g); g = 0;
                   1476:             }
                   1477:             bucket->body[hindex] = g;
                   1478:             NEXT(m) = 0;
                   1479:             if ( d ) {
                   1480:                 NEXT(tail)=m; tail=m; LEN(d)++;
                   1481:             } else {
                   1482:                 MKND(n,m,1,d); tail = BDY(d);
                   1483:             }
                   1484:         }
                   1485:     }
1.25      noro     1486: }
1.27      noro     1487:
1.61      noro     1488: /* input : list of NDV, cand : list of NDV */
1.28      noro     1489:
1.170     noro     1490: int ndv_check_membership(int m,NODE input,int obpe,int oadv,EPOS oepos,NODE cand)
1.28      noro     1491: {
1.157     noro     1492:     int n,i,stat;
                   1493:     ND nf,d;
                   1494:     NDV r;
                   1495:     NODE t,s;
                   1496:     union oNDC dn;
1.168     noro     1497:     Q q;
                   1498:     LIST list;
1.45      noro     1499:
1.172     noro     1500:     ndv_setup(m,0,cand,nd_gentrace?1:0,1);
1.157     noro     1501:     n = length(cand);
1.28      noro     1502:
1.172     noro     1503:        if ( nd_gentrace ) { nd_alltracelist = 0; nd_tracelist = 0; }
1.157     noro     1504:     /* membercheck : list is a subset of Id(cand) ? */
1.168     noro     1505:     for ( t = input, i = 0; t; t = NEXT(t), i++ ) {
1.45      noro     1506: again:
1.168     noro     1507:                nd_tracelist = 0;
1.157     noro     1508:         if ( nd_bpe > obpe )
                   1509:             r = ndv_dup_realloc((NDV)BDY(t),obpe,oadv,oepos);
                   1510:         else
                   1511:             r = (NDV)BDY(t);
1.171     noro     1512:         d = ndvtond(m,r);
                   1513:         stat = nd_nf(m,0,d,nd_ps,0,0,&nf);
1.157     noro     1514:         if ( !stat ) {
                   1515:             nd_reconstruct(0,0);
                   1516:             goto again;
                   1517:         } else if ( nf ) return 0;
1.172     noro     1518:                if ( nd_gentrace ) {
1.168     noro     1519:                        nd_tracelist = reverse_node(nd_tracelist);
                   1520:                        MKLIST(list,nd_tracelist);
                   1521:                        STOQ(i,q); s = mknode(2,q,list); MKLIST(list,s);
                   1522:                        MKNODE(s,list,nd_alltracelist);
                   1523:                        nd_alltracelist = s; nd_tracelist = 0;
                   1524:                }
1.157     noro     1525:         if ( DP_Print ) { printf("."); fflush(stdout); }
                   1526:     }
                   1527:     if ( DP_Print ) { printf("\n"); }
                   1528:     return 1;
1.23      noro     1529: }
1.1       noro     1530:
                   1531: ND nd_remove_head(ND p)
                   1532: {
1.157     noro     1533:     NM m;
1.1       noro     1534:
1.157     noro     1535:     m = BDY(p);
                   1536:     if ( !NEXT(m) ) {
                   1537:         FREEND(p); p = 0;
                   1538:     } else {
                   1539:         BDY(p) = NEXT(m); LEN(p)--;
                   1540:     }
                   1541:     FREENM(m);
                   1542:     return p;
1.1       noro     1543: }
                   1544:
1.69      noro     1545: ND nd_separate_head(ND p,ND *head)
                   1546: {
1.157     noro     1547:     NM m,m0;
                   1548:     ND r;
1.69      noro     1549:
1.157     noro     1550:     m = BDY(p);
                   1551:     if ( !NEXT(m) ) {
                   1552:         *head = p; p = 0;
                   1553:     } else {
                   1554:         m0 = m;
                   1555:         BDY(p) = NEXT(m); LEN(p)--;
                   1556:         NEXT(m0) = 0;
                   1557:         MKND(NV(p),m0,1,r);
                   1558:         *head = r;
                   1559:     }
                   1560:     return p;
1.69      noro     1561: }
                   1562:
1.1       noro     1563: PGeoBucket create_pbucket()
                   1564: {
                   1565:     PGeoBucket g;
1.157     noro     1566:
                   1567:     g = CALLOC(1,sizeof(struct oPGeoBucket));
                   1568:     g->m = -1;
                   1569:     return g;
1.1       noro     1570: }
                   1571:
1.25      noro     1572: void free_pbucket(PGeoBucket b) {
1.157     noro     1573:     int i;
1.25      noro     1574:
1.157     noro     1575:     for ( i = 0; i <= b->m; i++ )
                   1576:         if ( b->body[i] ) {
                   1577:             nd_free(b->body[i]);
                   1578:             b->body[i] = 0;
                   1579:         }
                   1580:     GC_free(b);
1.25      noro     1581: }
                   1582:
1.63      noro     1583: void add_pbucket_symbolic(PGeoBucket g,ND d)
                   1584: {
1.157     noro     1585:     int l,i,k,m;
1.63      noro     1586:
1.157     noro     1587:     if ( !d )
                   1588:         return;
                   1589:     l = LEN(d);
                   1590:     for ( k = 0, m = 1; l > m; k++, m <<= 1 );
                   1591:     /* 2^(k-1) < l <= 2^k (=m) */
                   1592:     d = nd_merge(g->body[k],d);
                   1593:     for ( ; d && LEN(d) > m; k++, m <<= 1 ) {
                   1594:         g->body[k] = 0;
                   1595:         d = nd_merge(g->body[k+1],d);
                   1596:     }
                   1597:     g->body[k] = d;
                   1598:     g->m = MAX(g->m,k);
1.63      noro     1599: }
                   1600:
1.31      noro     1601: void add_pbucket(int mod,PGeoBucket g,ND d)
1.1       noro     1602: {
1.157     noro     1603:     int l,i,k,m;
1.1       noro     1604:
1.157     noro     1605:     if ( !d )
                   1606:         return;
                   1607:     l = LEN(d);
                   1608:     for ( k = 0, m = 1; l > m; k++, m <<= 1 );
                   1609:     /* 2^(k-1) < l <= 2^k (=m) */
                   1610:     d = nd_add(mod,g->body[k],d);
                   1611:     for ( ; d && LEN(d) > m; k++, m <<= 1 ) {
                   1612:         g->body[k] = 0;
                   1613:         d = nd_add(mod,g->body[k+1],d);
                   1614:     }
                   1615:     g->body[k] = d;
                   1616:     g->m = MAX(g->m,k);
1.1       noro     1617: }
                   1618:
1.113     noro     1619: void mulq_pbucket(PGeoBucket g,Q c)
1.26      noro     1620: {
1.157     noro     1621:     int k;
1.26      noro     1622:
1.157     noro     1623:     for ( k = 0; k <= g->m; k++ )
                   1624:         nd_mul_c_q(g->body[k],(P)c);
1.26      noro     1625: }
                   1626:
1.63      noro     1627: NM remove_head_pbucket_symbolic(PGeoBucket g)
                   1628: {
1.157     noro     1629:     int j,i,k,c;
                   1630:     NM head;
                   1631:
                   1632:     k = g->m;
                   1633:     j = -1;
                   1634:     for ( i = 0; i <= k; i++ ) {
                   1635:         if ( !g->body[i] ) continue;
                   1636:         if ( j < 0 ) j = i;
                   1637:         else {
                   1638:             c = DL_COMPARE(HDL(g->body[i]),HDL(g->body[j]));
                   1639:             if ( c > 0 )
                   1640:                 j = i;
                   1641:             else if ( c == 0 )
                   1642:                 g->body[i] = nd_remove_head(g->body[i]);
                   1643:         }
                   1644:     }
                   1645:     if ( j < 0 ) return 0;
                   1646:     else {
                   1647:         head = BDY(g->body[j]);
                   1648:         if ( !NEXT(head) ) {
                   1649:             FREEND(g->body[j]);
                   1650:             g->body[j] = 0;
                   1651:         } else {
                   1652:             BDY(g->body[j]) = NEXT(head);
                   1653:             LEN(g->body[j])--;
                   1654:         }
                   1655:         return head;
                   1656:     }
1.63      noro     1657: }
                   1658:
1.19      noro     1659: int head_pbucket(int mod,PGeoBucket g)
1.1       noro     1660: {
1.157     noro     1661:     int j,i,c,k,nv,sum;
                   1662:     UINT *di,*dj;
                   1663:     ND gi,gj;
                   1664:
                   1665:     k = g->m;
                   1666:     while ( 1 ) {
                   1667:         j = -1;
                   1668:         for ( i = 0; i <= k; i++ ) {
                   1669:             if ( !(gi = g->body[i]) )
                   1670:                 continue;
                   1671:             if ( j < 0 ) {
                   1672:                 j = i;
                   1673:                 gj = g->body[j];
                   1674:                 dj = HDL(gj);
                   1675:                 sum = HCM(gj);
                   1676:             } else {
                   1677:                 c = DL_COMPARE(HDL(gi),dj);
                   1678:                 if ( c > 0 ) {
                   1679:                     if ( sum ) HCM(gj) = sum;
                   1680:                     else g->body[j] = nd_remove_head(gj);
                   1681:                     j = i;
                   1682:                     gj = g->body[j];
                   1683:                     dj = HDL(gj);
                   1684:                     sum = HCM(gj);
                   1685:                 } else if ( c == 0 ) {
                   1686:                     if ( mod == -1 )
                   1687:                         sum = _addsf(sum,HCM(gi));
                   1688:                     else {
                   1689:                         sum = sum+HCM(gi)-mod;
                   1690:                         if ( sum < 0 ) sum += mod;
                   1691:                     }
                   1692:                     g->body[i] = nd_remove_head(gi);
                   1693:                 }
                   1694:             }
                   1695:         }
                   1696:         if ( j < 0 ) return -1;
                   1697:         else if ( sum ) {
                   1698:             HCM(gj) = sum;
                   1699:             return j;
                   1700:         } else
                   1701:             g->body[j] = nd_remove_head(gj);
                   1702:     }
1.26      noro     1703: }
                   1704:
1.113     noro     1705: int head_pbucket_q(PGeoBucket g)
1.26      noro     1706: {
1.157     noro     1707:     int j,i,c,k,nv;
                   1708:     Q sum,t;
                   1709:     ND gi,gj;
                   1710:
                   1711:     k = g->m;
                   1712:     while ( 1 ) {
                   1713:         j = -1;
                   1714:         for ( i = 0; i <= k; i++ ) {
                   1715:             if ( !(gi = g->body[i]) ) continue;
                   1716:             if ( j < 0 ) {
                   1717:                 j = i;
                   1718:                 gj = g->body[j];
                   1719:                 sum = HCQ(gj);
                   1720:             } else {
                   1721:                 nv = NV(gi);
                   1722:                 c = DL_COMPARE(HDL(gi),HDL(gj));
                   1723:                 if ( c > 0 ) {
                   1724:                     if ( sum ) HCQ(gj) = sum;
                   1725:                     else g->body[j] = nd_remove_head(gj);
                   1726:                     j = i;
                   1727:                     gj = g->body[j];
                   1728:                     sum = HCQ(gj);
                   1729:                 } else if ( c == 0 ) {
                   1730:                     addq(sum,HCQ(gi),&t);
                   1731:                     sum = t;
                   1732:                     g->body[i] = nd_remove_head(gi);
                   1733:                 }
                   1734:             }
                   1735:         }
                   1736:         if ( j < 0 ) return -1;
                   1737:         else if ( sum ) {
                   1738:             HCQ(gj) = sum;
                   1739:             return j;
                   1740:         } else
                   1741:             g->body[j] = nd_remove_head(gj);
                   1742:     }
1.1       noro     1743: }
                   1744:
1.25      noro     1745: ND normalize_pbucket(int mod,PGeoBucket g)
1.1       noro     1746: {
1.157     noro     1747:     int i;
                   1748:     ND r,t;
1.1       noro     1749:
1.157     noro     1750:     r = 0;
                   1751:     for ( i = 0; i <= g->m; i++ ) {
                   1752:         r = nd_add(mod,r,g->body[i]);
                   1753:         g->body[i] = 0;
                   1754:     }
                   1755:     g->m = -1;
                   1756:     return r;
1.1       noro     1757: }
                   1758:
1.150     noro     1759: #if 0
                   1760: void register_hcf(NDV p)
                   1761: {
1.157     noro     1762:     DCP dc,t;
                   1763:     P hc,h;
                   1764:     int c;
                   1765:     NODE l,l1,prev;
                   1766:
                   1767:     hc = p->body->c.p;
                   1768:     if ( !nd_vc || NUM(hc) ) return;
                   1769:     fctrp(nd_vc,hc,&dc);
                   1770:     for ( t = dc; t; t = NEXT(t) ) {
                   1771:         h = t->c;
                   1772:         if ( NUM(h) ) continue;
                   1773:         for ( prev = 0, l = nd_hcf; l; prev = l, l = NEXT(l) ) {
                   1774:             c = compp(nd_vc,h,(P)BDY(l));
                   1775:             if ( c >= 0 ) break;
                   1776:         }
                   1777:         if ( !l || c > 0  ) {
                   1778:             MKNODE(l1,h,l);
                   1779:             if ( !prev )
                   1780:                 nd_hcf = l1;
                   1781:             else
                   1782:                 NEXT(prev) = l1;
                   1783:         }
                   1784:     }
1.150     noro     1785: }
                   1786: #else
                   1787: void register_hcf(NDV p)
                   1788: {
1.157     noro     1789:     DCP dc,t;
                   1790:     P hc,h,q;
                   1791:     Q dmy;
                   1792:     int c;
                   1793:     NODE l,l1,prev;
                   1794:
                   1795:     hc = p->body->c.p;
                   1796:     if ( NUM(hc) ) return;
                   1797:     ptozp(hc,1,&dmy,&h);
1.150     noro     1798: #if 1
1.157     noro     1799:     for ( l = nd_hcf; l; l = NEXT(l) ) {
                   1800:         while ( 1 ) {
                   1801:             if ( divtpz(nd_vc,h,(P)BDY(l),&q) ) h = q;
                   1802:             else break;
                   1803:         }
                   1804:     }
                   1805:     if ( NUM(h) ) return;
1.150     noro     1806: #endif
1.157     noro     1807:     for ( prev = 0, l = nd_hcf; l; prev = l, l = NEXT(l) ) {
                   1808:         c = compp(nd_vc,h,(P)BDY(l));
                   1809:         if ( c >= 0 ) break;
                   1810:     }
                   1811:     if ( !l || c > 0  ) {
                   1812:         MKNODE(l1,h,l);
                   1813:         if ( !prev )
                   1814:             nd_hcf = l1;
                   1815:         else
                   1816:             NEXT(prev) = l1;
                   1817:     }
1.150     noro     1818: }
                   1819: #endif
                   1820:
1.122     noro     1821: int do_diagonalize(int sugar,int m)
1.92      noro     1822: {
1.157     noro     1823:     int i,nh,stat;
                   1824:     NODE r,g,t;
                   1825:     ND h,nf,s,head;
                   1826:     NDV nfv;
                   1827:     Q q,num,den;
1.167     noro     1828:     P nm,nmp,dn,mnp,dnp,cont,cont1;
                   1829:     union oNDC hc;
                   1830:     NODE node;
                   1831:     LIST l;
                   1832:     Q iq;
1.157     noro     1833:
                   1834:     for ( i = nd_psn-1; i >= 0 && SG(nd_psh[i]) == sugar; i-- ) {
1.172     noro     1835:         if ( nd_gentrace ) {
1.167     noro     1836:             /* Trace = [1,index,1,1] */
                   1837:             STOQ(i,iq); node = mknode(4,ONE,iq,ONE,ONE);
                   1838:             MKLIST(l,node); MKNODE(nd_tracelist,l,0);
                   1839:         }
1.157     noro     1840:         if ( nd_demand )
                   1841:             nfv = ndv_load(i);
                   1842:         else
                   1843:             nfv = nd_ps[i];
                   1844:         s = ndvtond(m,nfv);
                   1845:         s = nd_separate_head(s,&head);
                   1846:         stat = nd_nf(m,head,s,nd_ps,1,0,&nf);
                   1847:         if ( !stat ) return 0;
                   1848:         ndv_free(nfv);
1.167     noro     1849:         hc = HCU(nf); nd_removecont(m,nf);
                   1850:         cont = ndc_div(m,hc,HCU(nf));
1.172     noro     1851:                if ( nd_gentrace ) finalize_tracelist(i,cont);
1.157     noro     1852:         nfv = ndtondv(m,nf);
                   1853:         nd_free(nf);
                   1854:         nd_bound[i] = ndv_compute_bound(nfv);
                   1855:         if ( !m ) register_hcf(nfv);
                   1856:         if ( nd_demand ) {
                   1857:             ndv_save(nfv,i);
                   1858:             ndv_free(nfv);
                   1859:         } else
                   1860:             nd_ps[i] = nfv;
                   1861:     }
                   1862:     return 1;
1.92      noro     1863: }
                   1864:
1.27      noro     1865: /* return value = 0 => input is not a GB */
                   1866:
1.168     noro     1867: NODE nd_gb(int m,int ishomo,int checkonly,int gensyz,int **indp)
1.1       noro     1868: {
1.157     noro     1869:     int i,nh,sugar,stat;
                   1870:     NODE r,g,t;
                   1871:     ND_pairs d;
                   1872:     ND_pairs l;
                   1873:     ND h,nf,s,head,nf1;
                   1874:     NDV nfv;
                   1875:     Q q,num,den;
1.167     noro     1876:     union oNDC dn,hc;
1.157     noro     1877:     int diag_count = 0;
1.167     noro     1878:     P cont;
                   1879:     LIST list;
1.157     noro     1880:
                   1881:     g = 0; d = 0;
                   1882:     for ( i = 0; i < nd_psn; i++ ) {
1.168     noro     1883:         d = update_pairs(d,g,i,gensyz);
1.157     noro     1884:         g = update_base(g,i);
                   1885:     }
                   1886:     sugar = 0;
                   1887:     while ( d ) {
1.1       noro     1888: again:
1.157     noro     1889:         l = nd_minp(d,&d);
                   1890:         if ( SG(l) != sugar ) {
                   1891:             if ( ishomo ) {
                   1892:                 diag_count = 0;
                   1893:                 stat = do_diagonalize(sugar,m);
                   1894:                 if ( !stat ) {
                   1895:                     NEXT(l) = d; d = l;
                   1896:                     d = nd_reconstruct(0,d);
                   1897:                     goto again;
                   1898:                 }
                   1899:             }
                   1900:             sugar = SG(l);
                   1901:             if ( DP_Print ) fprintf(asir_out,"%d",sugar);
                   1902:         }
                   1903:         stat = nd_sp(m,0,l,&h);
                   1904:         if ( !stat ) {
                   1905:             NEXT(l) = d; d = l;
                   1906:             d = nd_reconstruct(0,d);
                   1907:             goto again;
                   1908:         }
1.41      noro     1909: #if USE_GEOBUCKET
1.172     noro     1910:         stat = (m&&!nd_gentrace)?nd_nf_pbucket(m,h,nd_ps,!Top,&nf)
1.167     noro     1911:                :nd_nf(m,0,h,nd_ps,!Top,0,&nf);
1.41      noro     1912: #else
1.157     noro     1913:         stat = nd_nf(m,0,h,nd_ps,!Top,0,&nf);
1.41      noro     1914: #endif
1.157     noro     1915:         if ( !stat ) {
                   1916:             NEXT(l) = d; d = l;
                   1917:             d = nd_reconstruct(0,d);
                   1918:             goto again;
                   1919:         } else if ( nf ) {
1.168     noro     1920:             if ( checkonly || gensyz ) return 0;
1.192     noro     1921:                        if ( nd_newelim ) {
                   1922:                                if ( nd_module ) {
                   1923:                                        if ( MPOS(HDL(nf)) > 1 ) return 0;
                   1924:                                } else if ( !(HDL(nf)[nd_exporigin] & nd_mask[0]) ) return 0;
                   1925:                        }
1.157     noro     1926:             if ( DP_Print ) { printf("+"); fflush(stdout); }
1.167     noro     1927:             hc = HCU(nf);
1.157     noro     1928:             nd_removecont(m,nf);
                   1929:             if ( !m && nd_nalg ) {
                   1930:                 nd_monic(0,&nf);
                   1931:                 nd_removecont(m,nf);
                   1932:             }
1.172     noro     1933:             if ( nd_gentrace ) {
1.167     noro     1934:                                cont = ndc_div(m,hc,HCU(nf));
                   1935:                                if ( m || !UNIQ(cont) ) {
                   1936:                     t = mknode(4,0,0,0,cont);
                   1937:                     MKLIST(list,t); MKNODE(t,list,nd_tracelist);
                   1938:                                        nd_tracelist = t;
                   1939:                                }
                   1940:             }
1.157     noro     1941:             nfv = ndtondv(m,nf); nd_free(nf);
                   1942:             nh = ndv_newps(m,nfv,0);
                   1943:             if ( !m && (ishomo && ++diag_count == diag_period) ) {
                   1944:                 diag_count = 0;
                   1945:                 stat = do_diagonalize(sugar,m);
                   1946:                 if ( !stat ) {
                   1947:                     NEXT(l) = d; d = l;
                   1948:                     d = nd_reconstruct(1,d);
                   1949:                     goto again;
                   1950:                 }
                   1951:             }
1.168     noro     1952:             d = update_pairs(d,g,nh,0);
1.157     noro     1953:             g = update_base(g,nh);
                   1954:             FREENDP(l);
                   1955:         } else {
1.172     noro     1956:                    if ( nd_gentrace && gensyz ) {
1.168     noro     1957:                 nd_tracelist = reverse_node(nd_tracelist);
                   1958:                                MKLIST(list,nd_tracelist);
                   1959:                 STOQ(-1,q); t = mknode(2,q,list); MKLIST(list,t);
                   1960:                 MKNODE(t,list,nd_alltracelist);
                   1961:                                nd_alltracelist = t; nd_tracelist = 0;
                   1962:                        }
1.157     noro     1963:             if ( DP_Print ) { printf("."); fflush(stdout); }
                   1964:             FREENDP(l);
                   1965:         }
                   1966:     }
1.167     noro     1967:        conv_ilist(nd_demand,0,g,indp);
1.157     noro     1968:     if ( !checkonly && DP_Print ) { printf("nd_gb done.\n"); fflush(stdout); }
                   1969:     return g;
1.1       noro     1970: }
                   1971:
1.122     noro     1972: int do_diagonalize_trace(int sugar,int m)
1.91      noro     1973: {
1.157     noro     1974:     int i,nh,stat;
                   1975:     NODE r,g,t;
                   1976:     ND h,nf,nfq,s,head;
                   1977:     NDV nfv,nfqv;
                   1978:     Q q,den,num;
1.167     noro     1979:     union oNDC hc;
                   1980:     NODE node;
                   1981:     LIST l;
                   1982:     Q iq;
                   1983:     P cont,cont1;
1.157     noro     1984:
                   1985:     for ( i = nd_psn-1; i >= 0 && SG(nd_psh[i]) == sugar; i-- ) {
1.172     noro     1986:         if ( nd_gentrace ) {
1.167     noro     1987:             /* Trace = [1,index,1,1] */
                   1988:             STOQ(i,iq); node = mknode(4,ONE,iq,ONE,ONE);
                   1989:             MKLIST(l,node); MKNODE(nd_tracelist,l,0);
                   1990:         }
1.157     noro     1991:         /* for nd_ps */
                   1992:         s = ndvtond(m,nd_ps[i]);
                   1993:         s = nd_separate_head(s,&head);
                   1994:         stat = nd_nf_pbucket(m,s,nd_ps,1,&nf);
                   1995:         if ( !stat ) return 0;
                   1996:         nf = nd_add(m,head,nf);
                   1997:         ndv_free(nd_ps[i]);
                   1998:         nd_ps[i] = ndtondv(m,nf);
                   1999:         nd_free(nf);
                   2000:
                   2001:         /* for nd_ps_trace */
                   2002:         if ( nd_demand )
                   2003:             nfv = ndv_load(i);
                   2004:         else
                   2005:             nfv = nd_ps_trace[i];
                   2006:         s = ndvtond(0,nfv);
                   2007:         s = nd_separate_head(s,&head);
                   2008:         stat = nd_nf(0,head,s,nd_ps_trace,1,0,&nf);
                   2009:         if ( !stat ) return 0;
                   2010:         ndv_free(nfv);
1.167     noro     2011:         hc = HCU(nf); nd_removecont(0,nf);
                   2012:                cont = ndc_div(0,hc,HCU(nf));
1.172     noro     2013:         if ( nd_gentrace ) finalize_tracelist(i,cont);
1.157     noro     2014:         nfv = ndtondv(0,nf);
                   2015:         nd_free(nf);
                   2016:         nd_bound[i] = ndv_compute_bound(nfv);
                   2017:         register_hcf(nfv);
                   2018:         if ( nd_demand ) {
                   2019:             ndv_save(nfv,i);
                   2020:             ndv_free(nfv);
                   2021:         } else
                   2022:             nd_ps_trace[i] = nfv;
                   2023:     }
                   2024:     return 1;
1.91      noro     2025: }
                   2026:
1.118     noro     2027: static struct oEGT eg_invdalg;
                   2028: struct oEGT eg_le;
                   2029:
1.147     noro     2030: void nd_subst_vector(VL vl,P p,NODE subst,P *r)
                   2031: {
1.157     noro     2032:     NODE tn;
                   2033:     P p1;
1.147     noro     2034:
1.157     noro     2035:     for ( tn = subst; tn; tn = NEXT(NEXT(tn)) ) {
                   2036:         substp(vl,p,BDY(tn),BDY(NEXT(tn)),&p1); p = p1;
                   2037:     }
                   2038:     *r = p;
1.147     noro     2039: }
                   2040:
1.167     noro     2041: NODE nd_gb_trace(int m,int ishomo,int **indp)
1.20      noro     2042: {
1.157     noro     2043:     int i,nh,sugar,stat;
                   2044:     NODE r,g,t;
                   2045:     ND_pairs d;
                   2046:     ND_pairs l;
                   2047:     ND h,nf,nfq,s,head;
                   2048:     NDV nfv,nfqv;
                   2049:     Q q,den,num;
                   2050:     P hc;
1.167     noro     2051:     union oNDC dn,hnfq;
1.157     noro     2052:     struct oEGT eg_monic,egm0,egm1;
                   2053:     int diag_count = 0;
1.167     noro     2054:     P cont;
                   2055:     LIST list;
1.157     noro     2056:
                   2057:     init_eg(&eg_monic);
                   2058:     init_eg(&eg_invdalg);
                   2059:     init_eg(&eg_le);
                   2060:     g = 0; d = 0;
                   2061:     for ( i = 0; i < nd_psn; i++ ) {
1.168     noro     2062:         d = update_pairs(d,g,i,0);
1.157     noro     2063:         g = update_base(g,i);
                   2064:     }
                   2065:     sugar = 0;
                   2066:     while ( d ) {
1.20      noro     2067: again:
1.157     noro     2068:         l = nd_minp(d,&d);
                   2069:         if ( SG(l) != sugar ) {
1.130     noro     2070: #if 1
1.157     noro     2071:             if ( ishomo ) {
                   2072:                 if ( DP_Print > 2 ) fprintf(asir_out,"|");
                   2073:                 stat = do_diagonalize_trace(sugar,m);
                   2074:                 if ( DP_Print > 2 ) fprintf(asir_out,"|");
                   2075:                 diag_count = 0;
                   2076:                 if ( !stat ) {
                   2077:                     NEXT(l) = d; d = l;
                   2078:                     d = nd_reconstruct(1,d);
                   2079:                     goto again;
                   2080:                 }
                   2081:             }
1.130     noro     2082: #endif
1.157     noro     2083:             sugar = SG(l);
                   2084:             if ( DP_Print ) fprintf(asir_out,"%d",sugar);
                   2085:         }
                   2086:         stat = nd_sp(m,0,l,&h);
                   2087:         if ( !stat ) {
                   2088:             NEXT(l) = d; d = l;
                   2089:             d = nd_reconstruct(1,d);
                   2090:             goto again;
                   2091:         }
1.41      noro     2092: #if USE_GEOBUCKET
1.157     noro     2093:         stat = nd_nf_pbucket(m,h,nd_ps,!Top,&nf);
1.41      noro     2094: #else
1.157     noro     2095:         stat = nd_nf(m,0,h,nd_ps,!Top,0,&nf);
1.41      noro     2096: #endif
1.157     noro     2097:         if ( !stat ) {
                   2098:             NEXT(l) = d; d = l;
                   2099:             d = nd_reconstruct(1,d);
                   2100:             goto again;
                   2101:         } else if ( nf ) {
                   2102:             if ( nd_demand ) {
                   2103:                 nfqv = ndv_load(nd_psn);
                   2104:                 nfq = ndvtond(0,nfqv);
                   2105:             } else
                   2106:                 nfq = 0;
                   2107:             if ( !nfq ) {
                   2108:                 if ( !nd_sp(0,1,l,&h) || !nd_nf(0,0,h,nd_ps_trace,!Top,0,&nfq) ) {
                   2109:                     NEXT(l) = d; d = l;
                   2110:                     d = nd_reconstruct(1,d);
                   2111:                     goto again;
                   2112:                 }
                   2113:             }
                   2114:             if ( nfq ) {
                   2115:                 /* m|HC(nfq) => failure */
                   2116:                 if ( nd_vc ) {
                   2117:                     nd_subst_vector(nd_vc,HCP(nfq),nd_subst,&hc); q = (Q)hc;
                   2118:                 } else
                   2119:                     q = HCQ(nfq);
                   2120:                 if ( !rem(NM(q),m) ) return 0;
                   2121:
                   2122:                 if ( DP_Print ) { printf("+"); fflush(stdout); }
1.167     noro     2123:                 hnfq = HCU(nfq);
1.157     noro     2124:                 if ( nd_nalg ) {
                   2125:                     /* m|DN(HC(nf)^(-1)) => failure */
                   2126:                     get_eg(&egm0);
                   2127:                     if ( !nd_monic(m,&nfq) ) return 0;
                   2128:                     get_eg(&egm1); add_eg(&eg_monic,&egm0,&egm1);
                   2129:                     nd_removecont(0,nfq); nfqv = ndtondv(0,nfq); nd_free(nfq);
                   2130:                     nfv = ndv_dup(0,nfqv); ndv_mod(m,nfv); nd_free(nf);
                   2131:                 } else {
                   2132:                     nd_removecont(0,nfq); nfqv = ndtondv(0,nfq); nd_free(nfq);
                   2133:                     nd_removecont(m,nf); nfv = ndtondv(m,nf); nd_free(nf);
                   2134:                 }
1.172     noro     2135:                 if ( nd_gentrace ) {
1.167     noro     2136:                                   cont = ndc_div(0,hnfq,HCU(nfqv));
                   2137:                                   if ( !UNIQ(cont) ) {
                   2138:                        t = mknode(4,0,0,0,cont);
                   2139:                        MKLIST(list,t); MKNODE(t,list,nd_tracelist);
                   2140:                                           nd_tracelist = t;
                   2141:                                   }
                   2142:                 }
1.157     noro     2143:                 nh = ndv_newps(0,nfv,nfqv);
                   2144:                 if ( ishomo && ++diag_count == diag_period ) {
                   2145:                     diag_count = 0;
                   2146:                     if ( DP_Print > 2 ) fprintf(asir_out,"|");
                   2147:                     stat = do_diagonalize_trace(sugar,m);
                   2148:                     if ( DP_Print > 2 ) fprintf(asir_out,"|");
                   2149:                     if ( !stat ) {
                   2150:                         NEXT(l) = d; d = l;
                   2151:                         d = nd_reconstruct(1,d);
                   2152:                         goto again;
                   2153:                     }
                   2154:                 }
1.168     noro     2155:                 d = update_pairs(d,g,nh,0);
1.157     noro     2156:                 g = update_base(g,nh);
                   2157:             } else {
                   2158:                 if ( DP_Print ) { printf("*"); fflush(stdout); }
                   2159:             }
                   2160:         } else {
                   2161:             if ( DP_Print ) { printf("."); fflush(stdout); }
                   2162:         }
                   2163:         FREENDP(l);
                   2164:     }
                   2165:     if ( nd_nalg ) {
                   2166:         print_eg("monic",&eg_monic);
                   2167:         print_eg("invdalg",&eg_invdalg);
                   2168:         print_eg("le",&eg_le);
                   2169:     }
1.167     noro     2170:        conv_ilist(nd_demand,1,g,indp);
1.157     noro     2171:     if ( DP_Print ) { printf("nd_gb_trace done.\n"); fflush(stdout); }
                   2172:     return g;
1.20      noro     2173: }
                   2174:
1.23      noro     2175: int ndv_compare(NDV *p1,NDV *p2)
                   2176: {
1.157     noro     2177:     return DL_COMPARE(HDL(*p1),HDL(*p2));
1.23      noro     2178: }
                   2179:
                   2180: int ndv_compare_rev(NDV *p1,NDV *p2)
                   2181: {
1.157     noro     2182:     return -DL_COMPARE(HDL(*p1),HDL(*p2));
1.23      noro     2183: }
                   2184:
1.167     noro     2185: int ndvi_compare(NDVI p1,NDVI p2)
                   2186: {
                   2187:     return DL_COMPARE(HDL(p1->p),HDL(p2->p));
                   2188: }
                   2189:
                   2190: int ndvi_compare_rev(NDVI p1,NDVI p2)
                   2191: {
                   2192:     return -DL_COMPARE(HDL(p1->p),HDL(p2->p));
                   2193: }
                   2194:
1.61      noro     2195: NODE ndv_reduceall(int m,NODE f)
1.23      noro     2196: {
1.167     noro     2197:     int i,j,n,stat;
1.157     noro     2198:     ND nf,g,head;
                   2199:     NODE t,a0,a;
                   2200:     union oNDC dn;
                   2201:     Q q,num,den;
1.167     noro     2202:     NODE node;
                   2203:        LIST l;
                   2204:        Q iq,jq;
                   2205:     int *perm;
                   2206:     union oNDC hc;
                   2207:     P cont,cont1;
1.23      noro     2208:
1.173     noro     2209:     if ( nd_nora ) return f;
1.157     noro     2210:     n = length(f);
                   2211:     ndv_setup(m,0,f,0,1);
1.167     noro     2212:        perm = (int *)MALLOC(n*sizeof(int));
1.172     noro     2213:        if ( nd_gentrace ) {
1.167     noro     2214:            for ( t = nd_tracelist, i = 0; i < n; i++, t = NEXT(t) )
                   2215:                    perm[i] = QTOS((Q)ARG1(BDY((LIST)BDY(t))));
                   2216:        }
1.157     noro     2217:     for ( i = 0; i < n; ) {
1.172     noro     2218:         if ( nd_gentrace ) {
1.167     noro     2219:             /* Trace = [1,index,1,1] */
                   2220:             STOQ(i,iq); node = mknode(4,ONE,iq,ONE,ONE);
                   2221:             MKLIST(l,node); MKNODE(nd_tracelist,l,0);
                   2222:         }
1.157     noro     2223:         g = ndvtond(m,nd_ps[i]);
                   2224:         g = nd_separate_head(g,&head);
                   2225:         stat = nd_nf(m,head,g,nd_ps,1,0,&nf);
                   2226:         if ( !stat )
                   2227:             nd_reconstruct(0,0);
                   2228:         else {
                   2229:             if ( DP_Print ) { printf("."); fflush(stdout); }
                   2230:             ndv_free(nd_ps[i]);
1.167     noro     2231:             hc = HCU(nf); nd_removecont(m,nf);
1.172     noro     2232:             if ( nd_gentrace ) {
1.167     noro     2233:                                for ( t = nd_tracelist; t; t = NEXT(t) ) {
                   2234:                     jq = ARG1(BDY((LIST)BDY(t))); j = QTOS(jq);
                   2235:                     STOQ(perm[j],jq); ARG1(BDY((LIST)BDY(t))) = jq;
                   2236:                 }
                   2237:                 cont = ndc_div(m,hc,HCU(nf));
                   2238:                 finalize_tracelist(perm[i],cont);
                   2239:             }
1.157     noro     2240:             nd_ps[i] = ndtondv(m,nf); nd_free(nf);
                   2241:             nd_bound[i] = ndv_compute_bound(nd_ps[i]);
                   2242:             i++;
                   2243:         }
                   2244:     }
                   2245:     if ( DP_Print ) { printf("\n"); }
                   2246:     for ( a0 = 0, i = 0; i < n; i++ ) {
                   2247:         NEXTNODE(a0,a);
1.172     noro     2248:                if ( !nd_gentrace ) BDY(a) = (pointer)nd_ps[i];
1.167     noro     2249:                else {
                   2250:                        for ( j = 0; j < n; j++ ) if ( perm[j] == i ) break;
                   2251:                        BDY(a) = (pointer)nd_ps[j];
                   2252:                }
1.157     noro     2253:     }
                   2254:     NEXT(a) = 0;
                   2255:     return a0;
1.23      noro     2256: }
                   2257:
1.168     noro     2258: ND_pairs update_pairs( ND_pairs d, NODE /* of index */ g, int t, int gensyz)
1.1       noro     2259: {
1.157     noro     2260:     ND_pairs d1,nd,cur,head,prev,remove;
1.1       noro     2261:
1.157     noro     2262:     if ( !g ) return d;
1.168     noro     2263:        /* for testing */
1.172     noro     2264:        if ( gensyz && nd_gensyz == 2 ) {
1.168     noro     2265:        d1 = nd_newpairs(g,t);
                   2266:        if ( !d )
                   2267:                 return d1;
                   2268:        else {
                   2269:                 nd = d;
                   2270:                 while ( NEXT(nd) ) nd = NEXT(nd);
                   2271:                 NEXT(nd) = d1;
                   2272:                 return d;
                   2273:        }
                   2274:        }
1.157     noro     2275:     d = crit_B(d,t);
                   2276:     d1 = nd_newpairs(g,t);
                   2277:     d1 = crit_M(d1);
                   2278:     d1 = crit_F(d1);
1.168     noro     2279:     if ( gensyz || do_weyl )
1.157     noro     2280:         head = d1;
                   2281:     else {
                   2282:         prev = 0; cur = head = d1;
                   2283:         while ( cur ) {
                   2284:             if ( crit_2( cur->i1,cur->i2 ) ) {
                   2285:                 remove = cur;
                   2286:                 if ( !prev ) head = cur = NEXT(cur);
                   2287:                 else cur = NEXT(prev) = NEXT(cur);
                   2288:                 FREENDP(remove);
                   2289:             } else {
                   2290:                 prev = cur; cur = NEXT(cur);
                   2291:             }
                   2292:         }
                   2293:     }
                   2294:     if ( !d )
                   2295:         return head;
                   2296:     else {
                   2297:         nd = d;
                   2298:         while ( NEXT(nd) ) nd = NEXT(nd);
                   2299:         NEXT(nd) = head;
                   2300:         return d;
                   2301:     }
1.1       noro     2302: }
                   2303:
1.157     noro     2304:
1.1       noro     2305: ND_pairs nd_newpairs( NODE g, int t )
                   2306: {
1.157     noro     2307:     NODE h;
                   2308:     UINT *dl;
1.187     noro     2309:     int ts,s,i,t0,min,max;
1.157     noro     2310:     ND_pairs r,r0;
                   2311:
                   2312:     dl = DL(nd_psh[t]);
                   2313:     ts = SG(nd_psh[t]) - TD(dl);
                   2314:     for ( r0 = 0, h = g; h; h = NEXT(h) ) {
1.159     noro     2315:         if ( nd_module && (MPOS(DL(nd_psh[(long)BDY(h)])) != MPOS(dl)) )
1.157     noro     2316:                 continue;
1.187     noro     2317:                if ( nd_gbblock ) {
                   2318:                        t0 = (long)BDY(h);
                   2319:                        for ( i = 0; nd_gbblock[i] >= 0; i += 2 ) {
                   2320:                                min = nd_gbblock[i]; max = nd_gbblock[i+1];
                   2321:                                if ( t0 >= min && t0 <= max && t >= min && t <= max )
                   2322:                                        break;
                   2323:                        }
1.188     noro     2324:                        if ( nd_gbblock[i] >= 0 )
1.187     noro     2325:                                continue;
                   2326:                }
1.157     noro     2327:         NEXTND_pairs(r0,r);
1.159     noro     2328:         r->i1 = (long)BDY(h);
1.157     noro     2329:         r->i2 = t;
                   2330:         ndl_lcm(DL(nd_psh[r->i1]),dl,r->lcm);
                   2331:         s = SG(nd_psh[r->i1])-TD(DL(nd_psh[r->i1]));
                   2332:         SG(r) = MAX(s,ts) + TD(LCM(r));
                   2333:     }
                   2334:     if ( r0 ) NEXT(r) = 0;
                   2335:     return r0;
1.1       noro     2336: }
                   2337:
1.157     noro     2338: /* kokokara */
                   2339:
1.1       noro     2340: ND_pairs crit_B( ND_pairs d, int s )
                   2341: {
1.157     noro     2342:     ND_pairs cur,head,prev,remove;
                   2343:     UINT *t,*tl,*lcm;
                   2344:     int td,tdl;
                   2345:
                   2346:     if ( !d ) return 0;
                   2347:     t = DL(nd_psh[s]);
                   2348:     prev = 0;
                   2349:     head = cur = d;
                   2350:     lcm = (UINT *)ALLOCA(nd_wpd*sizeof(UINT));
                   2351:     while ( cur ) {
                   2352:         tl = cur->lcm;
1.163     noro     2353:         if ( ndl_reducible(tl,t) ) {
                   2354:             ndl_lcm(DL(nd_psh[cur->i1]),t,lcm);
1.167     noro     2355:             if ( !ndl_equal(lcm,tl) ) {
                   2356:                 ndl_lcm(DL(nd_psh[cur->i2]),t,lcm);
                   2357:                 if (!ndl_equal(lcm,tl)) {
                   2358:                     remove = cur;
                   2359:                     if ( !prev ) {
                   2360:                         head = cur = NEXT(cur);
                   2361:                     } else {
                   2362:                         cur = NEXT(prev) = NEXT(cur);
                   2363:                     }
                   2364:                     FREENDP(remove);
                   2365:                 } else {
                   2366:                     prev = cur; cur = NEXT(cur);
                   2367:                 }
                   2368:             } else {
                   2369:                     prev = cur; cur = NEXT(cur);
                   2370:             }
1.157     noro     2371:         } else {
                   2372:             prev = cur; cur = NEXT(cur);
                   2373:         }
                   2374:     }
                   2375:     return head;
1.1       noro     2376: }
                   2377:
                   2378: ND_pairs crit_M( ND_pairs d1 )
                   2379: {
1.157     noro     2380:     ND_pairs e,d2,d3,dd,p;
                   2381:     UINT *id,*jd;
1.1       noro     2382:
1.157     noro     2383:     if ( !d1 ) return d1;
                   2384:     for ( dd = 0, e = d1; e; e = d3 ) {
                   2385:         if ( !(d2 = NEXT(e)) ) {
                   2386:             NEXT(e) = dd;
                   2387:             return e;
                   2388:         }
                   2389:         id = LCM(e);
                   2390:         for ( d3 = 0; d2; d2 = p ) {
                   2391:             p = NEXT(d2);
                   2392:             jd = LCM(d2);
                   2393:             if ( ndl_equal(jd,id) )
                   2394:                 ;
                   2395:             else if ( TD(jd) > TD(id) )
                   2396:                 if ( ndl_reducible(jd,id) ) continue;
                   2397:                 else ;
                   2398:             else if ( ndl_reducible(id,jd) ) goto delit;
                   2399:             NEXT(d2) = d3;
                   2400:             d3 = d2;
                   2401:         }
                   2402:         NEXT(e) = dd;
                   2403:         dd = e;
                   2404:         continue;
                   2405:         /**/
                   2406:     delit:    NEXT(d2) = d3;
                   2407:         d3 = d2;
                   2408:         for ( ; p; p = d2 ) {
                   2409:             d2 = NEXT(p);
                   2410:             NEXT(p) = d3;
                   2411:             d3 = p;
                   2412:         }
                   2413:         FREENDP(e);
                   2414:     }
                   2415:     return dd;
1.1       noro     2416: }
                   2417:
                   2418: ND_pairs crit_F( ND_pairs d1 )
                   2419: {
1.157     noro     2420:     ND_pairs rest, head,remove;
                   2421:     ND_pairs last, p, r, w;
                   2422:     int s;
                   2423:
                   2424:     if ( !d1 ) return d1;
                   2425:     for ( head = last = 0, p = d1; NEXT(p); ) {
                   2426:         r = w = equivalent_pairs(p,&rest);
                   2427:         s = SG(r);
                   2428:         w = NEXT(w);
                   2429:         while ( w ) {
                   2430:             if ( crit_2(w->i1,w->i2) ) {
                   2431:                 r = w;
                   2432:                 w = NEXT(w);
                   2433:                 while ( w ) {
                   2434:                     remove = w;
                   2435:                     w = NEXT(w);
                   2436:                     FREENDP(remove);
                   2437:                 }
                   2438:                 break;
                   2439:             } else if ( SG(w) < s ) {
                   2440:                 FREENDP(r);
                   2441:                 r = w;
                   2442:                 s = SG(r);
                   2443:                 w = NEXT(w);
                   2444:             } else {
                   2445:                 remove = w;
                   2446:                 w = NEXT(w);
                   2447:                 FREENDP(remove);
                   2448:             }
                   2449:         }
                   2450:         if ( last ) NEXT(last) = r;
                   2451:         else head = r;
                   2452:         NEXT(last = r) = 0;
                   2453:         p = rest;
                   2454:         if ( !p ) return head;
                   2455:     }
                   2456:     if ( !last ) return p;
                   2457:     NEXT(last) = p;
                   2458:     return head;
1.1       noro     2459: }
                   2460:
                   2461: int crit_2( int dp1, int dp2 )
                   2462: {
1.157     noro     2463:     return ndl_disjoint(DL(nd_psh[dp1]),DL(nd_psh[dp2]));
1.1       noro     2464: }
                   2465:
1.40      noro     2466: ND_pairs equivalent_pairs( ND_pairs d1, ND_pairs *prest )
1.1       noro     2467: {
1.157     noro     2468:     ND_pairs w,p,r,s;
                   2469:     UINT *d;
1.1       noro     2470:
1.157     noro     2471:     w = d1;
                   2472:     d = LCM(w);
                   2473:     s = NEXT(w);
                   2474:     NEXT(w) = 0;
                   2475:     for ( r = 0; s; s = p ) {
                   2476:         p = NEXT(s);
                   2477:         if ( ndl_equal(d,LCM(s)) ) {
                   2478:             NEXT(s) = w; w = s;
                   2479:         } else {
                   2480:             NEXT(s) = r; r = s;
                   2481:         }
                   2482:     }
                   2483:     *prest = r;
                   2484:     return w;
1.1       noro     2485: }
                   2486:
                   2487: NODE update_base(NODE nd,int ndp)
                   2488: {
1.157     noro     2489:     UINT *dl, *dln;
                   2490:     NODE last, p, head;
1.1       noro     2491:
1.157     noro     2492:     dl = DL(nd_psh[ndp]);
                   2493:     for ( head = last = 0, p = nd; p; ) {
1.159     noro     2494:         dln = DL(nd_psh[(long)BDY(p)]);
1.157     noro     2495:         if ( ndl_reducible( dln, dl ) ) {
                   2496:             p = NEXT(p);
                   2497:             if ( last ) NEXT(last) = p;
                   2498:         } else {
                   2499:             if ( !last ) head = p;
                   2500:             p = NEXT(last = p);
                   2501:         }
                   2502:     }
                   2503:     head = append_one(head,ndp);
                   2504:     return head;
1.1       noro     2505: }
                   2506:
                   2507: ND_pairs nd_minp( ND_pairs d, ND_pairs *prest )
                   2508: {
1.157     noro     2509:     ND_pairs m,ml,p,l;
                   2510:     UINT *lcm;
                   2511:     int s,td,len,tlen,c,c1;
                   2512:
                   2513:     if ( !(p = NEXT(m = d)) ) {
                   2514:         *prest = p;
                   2515:         NEXT(m) = 0;
                   2516:         return m;
                   2517:     }
                   2518:     s = SG(m);
                   2519:     if ( !NoSugar ) {
                   2520:         for ( ml = 0, l = m; p; p = NEXT(l = p) )
                   2521:             if ( (SG(p) < s)
                   2522:                 || ((SG(p) == s) && (DL_COMPARE(LCM(p),LCM(m)) < 0)) ) {
                   2523:                 ml = l; m = p; s = SG(m);
                   2524:             }
                   2525:     } else {
                   2526:         for ( ml = 0, l = m; p; p = NEXT(l = p) )
                   2527:             if ( DL_COMPARE(LCM(p),LCM(m)) < 0 ) {
                   2528:                 ml = l; m = p; s = SG(m);
                   2529:             }
                   2530:     }
                   2531:     if ( !ml ) *prest = NEXT(m);
                   2532:     else {
                   2533:         NEXT(ml) = NEXT(m);
                   2534:         *prest = d;
                   2535:     }
                   2536:     NEXT(m) = 0;
                   2537:     return m;
1.1       noro     2538: }
                   2539:
1.63      noro     2540: ND_pairs nd_minsugarp( ND_pairs d, ND_pairs *prest )
                   2541: {
1.157     noro     2542:     int msugar,i;
                   2543:     ND_pairs t,dm0,dm,dr0,dr;
1.63      noro     2544:
1.157     noro     2545:     for ( msugar = SG(d), t = NEXT(d); t; t = NEXT(t) )
                   2546:         if ( SG(t) < msugar ) msugar = SG(t);
                   2547:     dm0 = 0; dr0 = 0;
                   2548:     for ( i = 0, t = d; t; t = NEXT(t) )
                   2549:         if ( i < nd_f4_nsp && SG(t) == msugar ) {
                   2550:             if ( dm0 ) NEXT(dm) = t;
                   2551:             else dm0 = t;
                   2552:             dm = t;
                   2553:             i++;
                   2554:         } else {
                   2555:             if ( dr0 ) NEXT(dr) = t;
                   2556:             else dr0 = t;
                   2557:             dr = t;
                   2558:         }
                   2559:     NEXT(dm) = 0;
                   2560:     if ( dr0 ) NEXT(dr) = 0;
                   2561:     *prest = dr0;
                   2562:     return dm0;
1.63      noro     2563: }
                   2564:
1.77      noro     2565: int ndv_newps(int m,NDV a,NDV aq)
1.1       noro     2566: {
1.157     noro     2567:     int len;
                   2568:     RHist r;
                   2569:     NDV b;
1.167     noro     2570:     NODE tn;
                   2571:     LIST l;
                   2572:     Q iq;
1.157     noro     2573:
                   2574:     if ( nd_psn == nd_pslen ) {
                   2575:         nd_pslen *= 2;
                   2576:         nd_ps = (NDV *)REALLOC((char *)nd_ps,nd_pslen*sizeof(NDV));
                   2577:         nd_ps_trace = (NDV *)REALLOC((char *)nd_ps_trace,nd_pslen*sizeof(NDV));
                   2578:         nd_psh = (RHist *)REALLOC((char *)nd_psh,nd_pslen*sizeof(RHist));
                   2579:         nd_bound = (UINT **)
                   2580:             REALLOC((char *)nd_bound,nd_pslen*sizeof(UINT *));
                   2581:     }
                   2582:     NEWRHist(r); nd_psh[nd_psn] = r;
                   2583:     nd_ps[nd_psn] = a;
                   2584:     if ( aq ) {
                   2585:         nd_ps_trace[nd_psn] = aq;
                   2586:         register_hcf(aq);
                   2587:         nd_bound[nd_psn] = ndv_compute_bound(aq);
                   2588:         SG(r) = SG(aq); ndl_copy(HDL(aq),DL(r));
                   2589:     } else {
                   2590:         if ( !m ) register_hcf(a);
                   2591:         nd_bound[nd_psn] = ndv_compute_bound(a);
                   2592:         SG(r) = SG(a); ndl_copy(HDL(a),DL(r));
                   2593:     }
                   2594:     if ( nd_demand ) {
                   2595:         if ( aq ) {
                   2596:             ndv_save(nd_ps_trace[nd_psn],nd_psn);
                   2597:             nd_ps_trace[nd_psn] = 0;
                   2598:         } else {
                   2599:             ndv_save(nd_ps[nd_psn],nd_psn);
                   2600:             nd_ps[nd_psn] = 0;
                   2601:         }
                   2602:     }
1.172     noro     2603:     if ( nd_gentrace ) {
1.167     noro     2604:         /* reverse the tracelist and append it to alltracelist */
                   2605:         nd_tracelist = reverse_node(nd_tracelist); MKLIST(l,nd_tracelist);
                   2606:         STOQ(nd_psn,iq); tn = mknode(2,iq,l); MKLIST(l,tn);
                   2607:         MKNODE(tn,l,nd_alltracelist); nd_alltracelist = tn; nd_tracelist = 0;
                   2608:     }
1.157     noro     2609:     return nd_psn++;
1.1       noro     2610: }
                   2611:
1.167     noro     2612: /* nd_tracelist = [[0,index,div],...,[nd_psn-1,index,div]] */
1.177     noro     2613: /* return 1 if success, 0 if failure (HC(a mod p)) */
1.167     noro     2614:
1.177     noro     2615: int ndv_setup(int mod,int trace,NODE f,int dont_sort,int dont_removecont)
1.1       noro     2616: {
1.157     noro     2617:     int i,j,td,len,max;
1.167     noro     2618:     NODE s,s0,f0,tn;
1.157     noro     2619:     UINT *d;
                   2620:     RHist r;
1.167     noro     2621:     NDVI w;
1.157     noro     2622:     NDV a,am;
1.167     noro     2623:     union oNDC hc;
                   2624:     NODE node;
                   2625:     P hcp;
                   2626:     Q iq,jq,hcq;
                   2627:     LIST l;
1.157     noro     2628:
                   2629:     nd_found = 0; nd_notfirst = 0; nd_create = 0;
1.167     noro     2630:     /* initialize the tracelist */
                   2631:     nd_tracelist = 0;
1.157     noro     2632:
                   2633:     for ( nd_psn = 0, s = f; s; s = NEXT(s) ) if ( BDY(s) ) nd_psn++;
1.167     noro     2634:     w = (NDVI)ALLOCA(nd_psn*sizeof(struct oNDVI));
                   2635:     for ( i = j = 0, s = f; s; s = NEXT(s), j++ )
                   2636:         if ( BDY(s) ) { w[i].p = BDY(s); w[i].i = j; i++; }
1.157     noro     2637:     if ( !dont_sort ) {
                   2638:         /* XXX heuristic */
                   2639:         if ( !nd_ord->id && (nd_ord->ord.simple<2) )
1.167     noro     2640:             qsort(w,nd_psn,sizeof(struct oNDVI),
                   2641:                 (int (*)(const void *,const void *))ndvi_compare_rev);
1.157     noro     2642:         else
1.167     noro     2643:             qsort(w,nd_psn,sizeof(struct oNDVI),
                   2644:                 (int (*)(const void *,const void *))ndvi_compare);
1.157     noro     2645:     }
                   2646:     nd_pslen = 2*nd_psn;
                   2647:     nd_ps = (NDV *)MALLOC(nd_pslen*sizeof(NDV));
                   2648:     nd_ps_trace = (NDV *)MALLOC(nd_pslen*sizeof(NDV));
                   2649:     nd_psh = (RHist *)MALLOC(nd_pslen*sizeof(RHist));
                   2650:     nd_bound = (UINT **)MALLOC(nd_pslen*sizeof(UINT *));
                   2651:     nd_hcf = 0;
                   2652:
                   2653:     if ( trace && nd_vc )
                   2654:         makesubst(nd_vc,&nd_subst);
                   2655:     else
                   2656:         nd_subst = 0;
                   2657:
                   2658:     if ( !nd_red )
                   2659:         nd_red = (RHist *)MALLOC(REDTAB_LEN*sizeof(RHist));
                   2660:     for ( i = 0; i < REDTAB_LEN; i++ ) nd_red[i] = 0;
                   2661:     for ( i = 0; i < nd_psn; i++ ) {
1.167     noro     2662:         hc = HCU(w[i].p);
1.157     noro     2663:         if ( trace ) {
1.167     noro     2664:             a = nd_ps_trace[i] = ndv_dup(0,w[i].p);
1.157     noro     2665:             if ( !dont_removecont) ndv_removecont(0,a);
                   2666:             register_hcf(a);
                   2667:             am = nd_ps[i] = ndv_dup(mod,a);
                   2668:             ndv_mod(mod,am);
1.177     noro     2669:            if ( DL_COMPARE(HDL(am),HDL(a)) )
                   2670:                return 0;
1.157     noro     2671:             ndv_removecont(mod,am);
                   2672:         } else {
1.167     noro     2673:             a = nd_ps[i] = ndv_dup(mod,w[i].p);
1.157     noro     2674:             if ( mod || !dont_removecont ) ndv_removecont(mod,a);
                   2675:             if ( !mod ) register_hcf(a);
                   2676:         }
1.172     noro     2677:         if ( nd_gentrace ) {
1.167     noro     2678:             STOQ(i,iq); STOQ(w[i].i,jq); node = mknode(3,iq,jq,ONE);
1.168     noro     2679:                        if ( !dont_removecont )
                   2680:                 ARG2(node) = (pointer)ndc_div(trace?0:mod,hc,HCU(a));
1.167     noro     2681:             MKLIST(l,node); NEXTNODE(nd_tracelist,tn); BDY(tn) = l;
                   2682:         }
1.157     noro     2683:         NEWRHist(r); SG(r) = HTD(a); ndl_copy(HDL(a),DL(r));
                   2684:         nd_bound[i] = ndv_compute_bound(a);
                   2685:         nd_psh[i] = r;
                   2686:         if ( nd_demand ) {
                   2687:             if ( trace ) {
                   2688:                 ndv_save(nd_ps_trace[i],i);
                   2689:                 nd_ps_trace[i] = 0;
                   2690:             } else {
                   2691:                 ndv_save(nd_ps[i],i);
                   2692:                 nd_ps[i] = 0;
                   2693:             }
                   2694:         }
                   2695:     }
1.172     noro     2696:     if ( nd_gentrace && nd_tracelist ) NEXT(tn) = 0;
1.177     noro     2697:     return 1;
1.20      noro     2698: }
                   2699:
1.119     noro     2700: struct order_spec *append_block(struct order_spec *spec,
                   2701:     int nv,int nalg,int ord);
                   2702:
1.121     noro     2703: extern VECT current_dl_weight_vector_obj;
                   2704: static VECT prev_weight_vector_obj;
                   2705:
1.120     noro     2706: void preprocess_algcoef(VL vv,VL av,struct order_spec *ord,LIST f,
1.157     noro     2707:     struct order_spec **ord1p,LIST *f1p,NODE *alistp)
1.120     noro     2708: {
1.157     noro     2709:     NODE alist,t,s,r0,r,arg;
                   2710:     VL tv;
                   2711:     P poly;
                   2712:     DP d;
                   2713:     Alg alpha,dp;
                   2714:     DAlg inv,da,hc;
                   2715:     MP m;
                   2716:     int i,nvar,nalg,n;
                   2717:     NumberField nf;
                   2718:     LIST f1,f2;
                   2719:     struct order_spec *current_spec;
                   2720:     VECT obj,obj0;
                   2721:     Obj tmp;
                   2722:
                   2723:     for ( nvar = 0, tv = vv; tv; tv = NEXT(tv), nvar++);
                   2724:     for ( nalg = 0, tv = av; tv; tv = NEXT(tv), nalg++);
                   2725:
                   2726:     for ( alist = 0, tv = av; tv; tv = NEXT(tv) ) {
                   2727:         NEXTNODE(alist,t); MKV(tv->v,poly);
                   2728:         MKAlg(poly,alpha); BDY(t) = (pointer)alpha;
                   2729:         tv->v = tv->v->priv;
                   2730:     }
                   2731:     NEXT(t) = 0;
                   2732:
1.167     noro     2733:     /* simplification, making polynomials monic */
1.157     noro     2734:     setfield_dalg(alist);
                   2735:     obj_algtodalg(f,&f1);
                   2736:     for ( t = BDY(f); t; t = NEXT(t) ) {
                   2737:         initd(ord); ptod(vv,vv,(P)BDY(t),&d);
                   2738:         hc = (DAlg)BDY(d)->c;
                   2739:         if ( NID(hc) == N_DA ) {
                   2740:             invdalg(hc,&inv);
                   2741:             for ( m = BDY(d); m; m = NEXT(m) ) {
                   2742:                 muldalg(inv,(DAlg)m->c,&da); m->c = (P)da;
                   2743:             }
                   2744:         }
                   2745:         initd(ord); dtop(vv,vv,d,&poly); BDY(f) = (pointer)poly;
                   2746:     }
                   2747:     obj_dalgtoalg(f1,&f);
                   2748:
                   2749:     /* append alg vars to the var list */
                   2750:     for ( tv = vv; NEXT(tv); tv = NEXT(tv) );
                   2751:     NEXT(tv) = av;
                   2752:
                   2753:     /* append a block to ord */
                   2754:     *ord1p = append_block(ord,nvar,nalg,2);
                   2755:
                   2756:     /* create generator list */
                   2757:     nf = get_numberfield();
                   2758:     for ( i = nalg-1, t = BDY(f); i >= 0; i-- ) {
                   2759:         MKAlg(nf->defpoly[i],dp);
                   2760:         MKNODE(s,dp,t); t = s;
                   2761:     }
                   2762:     MKLIST(f1,t);
                   2763:     *alistp = alist;
                   2764:     algobjtorat(f1,f1p);
                   2765:
                   2766:     /* creating a new weight vector */
                   2767:     prev_weight_vector_obj = obj0 = current_dl_weight_vector_obj;
                   2768:     n = nvar+nalg+1;
                   2769:     MKVECT(obj,n);
                   2770:     if ( obj0 && obj0->len == nvar )
                   2771:         for ( i = 0; i < nvar; i++ ) BDY(obj)[i] = BDY(obj0)[i];
                   2772:     else
                   2773:         for ( i = 0; i < nvar; i++ ) BDY(obj)[i] = (pointer)ONE;
                   2774:     for ( i = 0; i < nalg; i++ ) BDY(obj)[i+nvar] = 0;
                   2775:     BDY(obj)[n-1] = (pointer)ONE;
                   2776:     arg = mknode(1,obj);
                   2777:     Pdp_set_weight(arg,&tmp);
1.121     noro     2778: }
                   2779:
                   2780: NODE postprocess_algcoef(VL av,NODE alist,NODE r)
                   2781: {
1.157     noro     2782:     NODE s,t,u0,u;
                   2783:     P p;
                   2784:     VL tv;
                   2785:     Obj obj,tmp;
                   2786:     NODE arg;
                   2787:
                   2788:     u0 = 0;
                   2789:     for ( t = r; t; t = NEXT(t) ) {
                   2790:         p = (P)BDY(t);
                   2791:         for ( tv = av, s = alist; tv; tv = NEXT(tv), s = NEXT(s) ) {
                   2792:             substr(CO,0,(Obj)p,tv->v,(Obj)BDY(s),&obj); p = (P)obj;
                   2793:         }
                   2794:         if ( OID(p) == O_P || (OID(p) == O_N && NID((Num)p) != N_A) ) {
                   2795:             NEXTNODE(u0,u);
                   2796:             BDY(u) = (pointer)p;
                   2797:         }
                   2798:     }
                   2799:     arg = mknode(1,prev_weight_vector_obj);
                   2800:     Pdp_set_weight(arg,&tmp);
1.121     noro     2801:
1.157     noro     2802:     return u0;
1.120     noro     2803: }
                   2804:
1.184     noro     2805: void nd_gr(LIST f,LIST v,int m,int homo,int f4,struct order_spec *ord,LIST *rp)
1.1       noro     2806: {
1.157     noro     2807:     VL tv,fv,vv,vc,av;
                   2808:     NODE fd,fd0,r,r0,t,x,s,xx,alist;
                   2809:     int e,max,nvar,i;
                   2810:     NDV b;
1.184     noro     2811:     int ishomo,nalg,mrank,trank,wmax,len;
                   2812:        NMV a;
1.157     noro     2813:     Alg alpha,dp;
                   2814:     P p,zp;
                   2815:     Q dmy;
                   2816:     LIST f1,f2,zpl;
                   2817:     Obj obj;
                   2818:     NumberField nf;
                   2819:     struct order_spec *ord1;
1.170     noro     2820:     NODE tr,tl1,tl2,tl3,tl4;
                   2821:     LIST l1,l2,l3,l4,l5;
1.167     noro     2822:        int j;
                   2823:        Q jq;
                   2824:     int *perm;
1.170     noro     2825:     EPOS oepos;
1.194   ! noro     2826:     int obpe,oadv,ompos,cbpe;
1.1       noro     2827:
1.174     noro     2828:     nd_module = 0;
1.157     noro     2829:     if ( !m && Demand ) nd_demand = 1;
                   2830:     else nd_demand = 0;
1.172     noro     2831:     parse_nd_option(current_option);
1.78      noro     2832:
1.157     noro     2833:     if ( DP_Multiple )
                   2834:         nd_scale = ((double)DP_Multiple)/(double)(Denominator?Denominator:1);
1.103     noro     2835: #if 0
1.157     noro     2836:     ndv_alloc = 0;
1.103     noro     2837: #endif
1.157     noro     2838:     get_vars((Obj)f,&fv); pltovl(v,&vv); vlminus(fv,vv,&nd_vc);
                   2839:     for ( nvar = 0, tv = vv; tv; tv = NEXT(tv), nvar++ );
                   2840:     switch ( ord->id ) {
                   2841:         case 1:
                   2842:             if ( ord->nv != nvar )
                   2843:                 error("nd_{gr,f4} : invalid order specification");
                   2844:             break;
                   2845:         default:
                   2846:             break;
                   2847:     }
                   2848:     nd_nalg = 0;
                   2849:     av = 0;
                   2850:     if ( !m ) {
                   2851:         get_algtree((Obj)f,&av);
                   2852:         for ( nalg = 0, tv = av; tv; tv = NEXT(tv), nalg++ );
                   2853:         nd_ntrans = nvar;
                   2854:         nd_nalg = nalg;
                   2855:         /* #i -> t#i */
                   2856:         if ( nalg ) {
                   2857:             preprocess_algcoef(vv,av,ord,f,&ord1,&f1,&alist);
                   2858:             ord = ord1;
                   2859:             f = f1;
                   2860:         }
                   2861:         nvar += nalg;
                   2862:     }
                   2863:     nd_init_ord(ord);
                   2864:     mrank = 0;
1.178     noro     2865:     for ( t = BDY(f), max = 1; t; t = NEXT(t) )
1.157     noro     2866:         for ( tv = vv; tv; tv = NEXT(tv) ) {
                   2867:             if ( nd_module ) {
                   2868:                 s = BDY((LIST)BDY(t));
                   2869:                 trank = length(s);
                   2870:                 mrank = MAX(mrank,trank);
                   2871:                 for ( ; s; s = NEXT(s) ) {
                   2872:                     e = getdeg(tv->v,(P)BDY(s));
                   2873:                     max = MAX(e,max);
                   2874:                 }
                   2875:             } else {
                   2876:                 e = getdeg(tv->v,(P)BDY(t));
                   2877:                 max = MAX(e,max);
                   2878:             }
                   2879:         }
                   2880:     nd_setup_parameters(nvar,max);
1.170     noro     2881:     obpe = nd_bpe; oadv = nmv_adv; oepos = nd_epos; ompos = nd_mpos;
1.157     noro     2882:     ishomo = 1;
                   2883:     for ( fd0 = 0, t = BDY(f); t; t = NEXT(t) ) {
1.167     noro     2884:         if ( nd_module ) {
1.172     noro     2885:                        if ( !m && !nd_gentrace ) pltozpl((LIST)BDY(t),&dmy,&zpl);
1.167     noro     2886:                        else zpl = (LIST)BDY(t);
1.157     noro     2887:             b = (pointer)pltondv(CO,vv,zpl);
                   2888:         } else {
1.172     noro     2889:                        if ( !m && !nd_gentrace ) ptozp((P)BDY(t),1,&dmy,&zp);
1.167     noro     2890:                        else zp = (P)BDY(t);
1.157     noro     2891:             b = (pointer)ptondv(CO,vv,zp);
1.167     noro     2892:         }
1.157     noro     2893:         if ( ishomo )
                   2894:             ishomo = ishomo && ndv_ishomo(b);
                   2895:         if ( m ) ndv_mod(m,b);
                   2896:         if ( b ) { NEXTNODE(fd0,fd); BDY(fd) = (pointer)b; }
                   2897:     }
                   2898:     if ( fd0 ) NEXT(fd) = 0;
1.184     noro     2899:
                   2900:        if ( !ishomo && homo ) {
                   2901:         for ( t = fd0, wmax = max; t; t = NEXT(t) ) {
                   2902:             b = (NDV)BDY(t); len = LEN(b);
                   2903:             for ( a = BDY(b), i = 0; i < len; i++, NMV_ADV(a) )
                   2904:                 wmax = MAX(TD(DL(a)),wmax);
                   2905:         }
                   2906:         homogenize_order(ord,nvar,&ord1);
                   2907:         nd_init_ord(ord1);
                   2908:         nd_setup_parameters(nvar+1,wmax);
                   2909:         for ( t = fd0; t; t = NEXT(t) )
                   2910:             ndv_homogenize((NDV)BDY(t),obpe,oadv,oepos,ompos);
                   2911:     }
                   2912:
1.187     noro     2913:     ndv_setup(m,0,fd0,nd_gbblock?1:0,0);
1.172     noro     2914:     if ( nd_gentrace ) {
1.167     noro     2915:         MKLIST(l1,nd_tracelist); MKNODE(nd_alltracelist,l1,0);
                   2916:     }
1.186     noro     2917:     x = f4?nd_f4(m,&perm):nd_gb(m,ishomo || homo,0,0,&perm);
1.192     noro     2918:        if ( !x ) {
                   2919:                *rp = 0; return;
                   2920:        }
1.184     noro     2921:        if ( !ishomo && homo ) {
                   2922:                /* dehomogenization */
                   2923:                for ( t = x; t; t = NEXT(t) ) ndv_dehomogenize((NDV)BDY(t),ord);
                   2924:                nd_init_ord(ord);
                   2925:                nd_setup_parameters(nvar,0);
                   2926:        }
1.157     noro     2927:     nd_demand = 0;
1.167     noro     2928:     x = ndv_reducebase(x,perm);
1.172     noro     2929:     if ( nd_gentrace ) { tl1 = nd_alltracelist; nd_alltracelist = 0; }
1.157     noro     2930:     x = ndv_reduceall(m,x);
1.194   ! noro     2931:     cbpe = nd_bpe;
1.172     noro     2932:     if ( nd_gentrace ) {
1.170     noro     2933:         tl2 = nd_alltracelist; nd_alltracelist = 0;
                   2934:         ndv_check_membership(m,fd0,obpe,oadv,oepos,x);
1.172     noro     2935:         if ( nd_gentrace ) {
1.170     noro     2936:             tl3 = nd_alltracelist; nd_alltracelist = 0;
                   2937:         } else tl3 = 0;
1.194   ! noro     2938:         nd_gb(m,0,1,nd_gensyz?1:0,0);
1.172     noro     2939:         if ( nd_gentrace && nd_gensyz ) {
1.170     noro     2940:             tl4 = nd_alltracelist; nd_alltracelist = 0;
                   2941:         } else tl4 = 0;
                   2942:     }
1.194   ! noro     2943:     nd_bpe = cbpe;
        !          2944:     nd_setup_parameters(nd_nvar,0);
1.157     noro     2945:     for ( r0 = 0, t = x; t; t = NEXT(t) ) {
                   2946:         NEXTNODE(r0,r);
1.194   ! noro     2947:         if ( nd_module ) BDY(r) = ndvtopl(m,CO,vv,BDY(t),mrank);
1.157     noro     2948:         else BDY(r) = ndvtop(m,CO,vv,BDY(t));
                   2949:     }
                   2950:     if ( r0 ) NEXT(r) = 0;
                   2951:     if ( nalg )
                   2952:         r0 = postprocess_algcoef(av,alist,r0);
                   2953:     MKLIST(*rp,r0);
1.172     noro     2954:     if ( nd_gentrace ) {
1.167     noro     2955:         tl1 = reverse_node(tl1); tl2 = reverse_node(tl2);
1.170     noro     2956:        tl3 = reverse_node(tl3);
1.167     noro     2957:                /* tl2 = [[i,[[*,j,*,*],...]],...] */
                   2958:         for ( t = tl2; t; t = NEXT(t) ) {
                   2959:                        /* s = [i,[*,j,*,*],...] */
                   2960:             s = BDY((LIST)BDY(t));
                   2961:             j = perm[QTOS((Q)ARG0(s))]; STOQ(j,jq); ARG0(s) = (pointer)jq;
                   2962:                        for ( s = BDY((LIST)ARG1(s)); s; s = NEXT(s) ) {
                   2963:                 j = perm[QTOS((Q)ARG1(BDY((LIST)BDY(s))))]; STOQ(j,jq);
                   2964:                                ARG1(BDY((LIST)BDY(s))) = (pointer)jq;
                   2965:             }
                   2966:                }
                   2967:                for ( j = length(x)-1, t = 0; j >= 0; j-- ) {
                   2968:                    STOQ(perm[j],jq); MKNODE(s,jq,t); t = s;
                   2969:                }
1.170     noro     2970:        MKLIST(l1,tl1); MKLIST(l2,tl2); MKLIST(l3,t); MKLIST(l4,tl3);
                   2971:        MKLIST(l5,tl4);
1.184     noro     2972:       tr = mknode(7,*rp,(!ishomo&&homo)?ONE:0,l1,l2,l3,l4,l5); MKLIST(*rp,tr);
1.167     noro     2973:     }
1.103     noro     2974: #if 0
1.157     noro     2975:     fprintf(asir_out,"ndv_alloc=%d\n",ndv_alloc);
1.103     noro     2976: #endif
1.127     noro     2977: }
                   2978:
                   2979: void nd_gr_postproc(LIST f,LIST v,int m,struct order_spec *ord,int do_check,LIST *rp)
                   2980: {
1.157     noro     2981:     VL tv,fv,vv,vc,av;
                   2982:     NODE fd,fd0,r,r0,t,x,s,xx,alist;
                   2983:     int e,max,nvar,i;
                   2984:     NDV b;
                   2985:     int ishomo,nalg;
                   2986:     Alg alpha,dp;
                   2987:     P p,zp;
                   2988:     Q dmy;
                   2989:     LIST f1,f2;
                   2990:     Obj obj;
                   2991:     NumberField nf;
                   2992:     struct order_spec *ord1;
1.167     noro     2993:     int *perm;
1.157     noro     2994:
                   2995:     get_vars((Obj)f,&fv); pltovl(v,&vv); vlminus(fv,vv,&nd_vc);
                   2996:     for ( nvar = 0, tv = vv; tv; tv = NEXT(tv), nvar++ );
                   2997:     switch ( ord->id ) {
                   2998:         case 1:
                   2999:             if ( ord->nv != nvar )
                   3000:                 error("nd_check : invalid order specification");
                   3001:             break;
                   3002:         default:
                   3003:             break;
                   3004:     }
                   3005:     nd_nalg = 0;
                   3006:     av = 0;
                   3007:     if ( !m ) {
                   3008:         get_algtree((Obj)f,&av);
                   3009:         for ( nalg = 0, tv = av; tv; tv = NEXT(tv), nalg++ );
                   3010:         nd_ntrans = nvar;
                   3011:         nd_nalg = nalg;
                   3012:         /* #i -> t#i */
                   3013:         if ( nalg ) {
                   3014:             preprocess_algcoef(vv,av,ord,f,&ord1,&f1,&alist);
                   3015:             ord = ord1;
                   3016:             f = f1;
                   3017:         }
                   3018:         nvar += nalg;
                   3019:     }
                   3020:     nd_init_ord(ord);
1.178     noro     3021:     for ( t = BDY(f), max = 1; t; t = NEXT(t) )
1.157     noro     3022:         for ( tv = vv; tv; tv = NEXT(tv) ) {
                   3023:             e = getdeg(tv->v,(P)BDY(t));
                   3024:             max = MAX(e,max);
                   3025:         }
                   3026:     nd_setup_parameters(nvar,max);
                   3027:     ishomo = 1;
                   3028:     for ( fd0 = 0, t = BDY(f); t; t = NEXT(t) ) {
                   3029:         ptozp((P)BDY(t),1,&dmy,&zp);
                   3030:         b = (pointer)ptondv(CO,vv,zp);
                   3031:         if ( ishomo )
                   3032:             ishomo = ishomo && ndv_ishomo(b);
                   3033:         if ( m ) ndv_mod(m,b);
                   3034:         if ( b ) { NEXTNODE(fd0,fd); BDY(fd) = (pointer)b; }
                   3035:     }
                   3036:     if ( fd0 ) NEXT(fd) = 0;
                   3037:     ndv_setup(m,0,fd0,0,1);
                   3038:     for ( x = 0, i = 0; i < nd_psn; i++ )
                   3039:         x = update_base(x,i);
                   3040:     if ( do_check ) {
1.168     noro     3041:         x = nd_gb(m,ishomo,1,0,&perm);
1.157     noro     3042:         if ( !x ) {
                   3043:             *rp = 0;
                   3044:             return;
                   3045:         }
                   3046:     } else {
1.175     noro     3047: #if 0
                   3048:                /* bug ? */
1.157     noro     3049:         for ( t = x; t; t = NEXT(t) )
1.159     noro     3050:             BDY(t) = (pointer)nd_ps[(long)BDY(t)];
1.175     noro     3051: #else
                   3052:                conv_ilist(0,0,x,&perm);
                   3053: #endif
1.157     noro     3054:     }
1.167     noro     3055:     x = ndv_reducebase(x,perm);
1.157     noro     3056:     x = ndv_reduceall(m,x);
                   3057:     for ( r0 = 0, t = x; t; t = NEXT(t) ) {
                   3058:         NEXTNODE(r0,r);
                   3059:         BDY(r) = ndvtop(m,CO,vv,BDY(t));
                   3060:     }
                   3061:     if ( r0 ) NEXT(r) = 0;
                   3062:     if ( nalg )
                   3063:         r0 = postprocess_algcoef(av,alist,r0);
                   3064:     MKLIST(*rp,r0);
1.20      noro     3065: }
                   3066:
1.133     noro     3067: void nd_gr_trace(LIST f,LIST v,int trace,int homo,int f4,struct order_spec *ord,LIST *rp)
1.20      noro     3068: {
1.157     noro     3069:     VL tv,fv,vv,vc,av;
                   3070:     NODE fd,fd0,in0,in,r,r0,t,s,cand,alist;
                   3071:     int m,nocheck,nvar,mindex,e,max;
                   3072:     NDV c;
                   3073:     NMV a;
                   3074:     P p,zp;
                   3075:     Q dmy;
                   3076:     EPOS oepos;
1.164     noro     3077:     int obpe,oadv,wmax,i,len,cbpe,ishomo,nalg,mrank,trank,ompos;
1.157     noro     3078:     Alg alpha,dp;
                   3079:     P poly;
1.158     noro     3080:     LIST f1,f2,zpl;
1.157     noro     3081:     Obj obj;
                   3082:     NumberField nf;
                   3083:     struct order_spec *ord1;
                   3084:     struct oEGT eg_check,eg0,eg1;
1.168     noro     3085:     NODE tr,tl1,tl2,tl3,tl4;
                   3086:     LIST l1,l2,l3,l4,l5;
1.167     noro     3087:     int *perm;
1.168     noro     3088:     int j,ret;
1.167     noro     3089:     Q jq;
1.157     noro     3090:
1.167     noro     3091:     nd_module = 0;
1.172     noro     3092:     parse_nd_option(current_option);
1.157     noro     3093:     if ( DP_Multiple )
                   3094:         nd_scale = ((double)DP_Multiple)/(double)(Denominator?Denominator:1);
                   3095:
                   3096:     get_vars((Obj)f,&fv); pltovl(v,&vv); vlminus(fv,vv,&nd_vc);
                   3097:     for ( nvar = 0, tv = vv; tv; tv = NEXT(tv), nvar++ );
                   3098:     switch ( ord->id ) {
                   3099:         case 1:
                   3100:             if ( ord->nv != nvar )
                   3101:                 error("nd_gr_trace : invalid order specification");
                   3102:             break;
                   3103:         default:
                   3104:             break;
                   3105:     }
                   3106:
                   3107:     get_algtree((Obj)f,&av);
                   3108:     for ( nalg = 0, tv = av; tv; tv = NEXT(tv), nalg++ );
                   3109:     nd_ntrans = nvar;
                   3110:     nd_nalg = nalg;
                   3111:     /* #i -> t#i */
                   3112:     if ( nalg ) {
                   3113:         preprocess_algcoef(vv,av,ord,f,&ord1,&f1,&alist);
                   3114:         ord = ord1;
                   3115:         f = f1;
                   3116:     }
                   3117:     nvar += nalg;
                   3118:
                   3119:     nocheck = 0;
                   3120:     mindex = 0;
                   3121:
                   3122:     if ( Demand ) nd_demand = 1;
                   3123:     else nd_demand = 0;
                   3124:
                   3125:     /* setup modulus */
                   3126:     if ( trace < 0 ) {
                   3127:         trace = -trace;
                   3128:         nocheck = 1;
                   3129:     }
                   3130:     m = trace > 1 ? trace : get_lprime(mindex);
1.158     noro     3131:     nd_init_ord(ord);
                   3132:     mrank = 0;
1.178     noro     3133:     for ( t = BDY(f), max = 1; t; t = NEXT(t) )
1.157     noro     3134:         for ( tv = vv; tv; tv = NEXT(tv) ) {
1.158     noro     3135:             if ( nd_module ) {
                   3136:                 s = BDY((LIST)BDY(t));
                   3137:                 trank = length(s);
                   3138:                 mrank = MAX(mrank,trank);
                   3139:                 for ( ; s; s = NEXT(s) ) {
                   3140:                     e = getdeg(tv->v,(P)BDY(s));
                   3141:                     max = MAX(e,max);
                   3142:                 }
                   3143:             } else {
                   3144:                 e = getdeg(tv->v,(P)BDY(t));
                   3145:                 max = MAX(e,max);
                   3146:             }
1.157     noro     3147:         }
                   3148:     nd_setup_parameters(nvar,max);
1.164     noro     3149:     obpe = nd_bpe; oadv = nmv_adv; oepos = nd_epos; ompos = nd_mpos;
1.157     noro     3150:     ishomo = 1;
                   3151:     for ( in0 = 0, fd0 = 0, t = BDY(f); t; t = NEXT(t) ) {
1.167     noro     3152:         if ( nd_module ) {
1.172     noro     3153:                        if ( !nd_gentrace ) pltozpl((LIST)BDY(t),&dmy,&zpl);
1.167     noro     3154:                        else zpl = (LIST)BDY(t);
1.158     noro     3155:             c = (pointer)pltondv(CO,vv,zpl);
                   3156:         } else {
1.172     noro     3157:                        if ( !nd_gentrace ) ptozp((P)BDY(t),1,&dmy,&zp);
1.167     noro     3158:                        else zp = (P)BDY(t);
1.158     noro     3159:             c = (pointer)ptondv(CO,vv,zp);
1.167     noro     3160:         }
1.157     noro     3161:         if ( ishomo )
                   3162:             ishomo = ishomo && ndv_ishomo(c);
                   3163:         if ( c ) {
                   3164:             NEXTNODE(in0,in); BDY(in) = (pointer)c;
                   3165:             NEXTNODE(fd0,fd); BDY(fd) = (pointer)ndv_dup(0,c);
                   3166:         }
                   3167:     }
                   3168:     if ( in0 ) NEXT(in) = 0;
                   3169:     if ( fd0 ) NEXT(fd) = 0;
                   3170:     if ( !ishomo && homo ) {
                   3171:         for ( t = in0, wmax = max; t; t = NEXT(t) ) {
                   3172:             c = (NDV)BDY(t); len = LEN(c);
                   3173:             for ( a = BDY(c), i = 0; i < len; i++, NMV_ADV(a) )
                   3174:                 wmax = MAX(TD(DL(a)),wmax);
                   3175:         }
                   3176:         homogenize_order(ord,nvar,&ord1);
                   3177:         nd_init_ord(ord1);
                   3178:         nd_setup_parameters(nvar+1,wmax);
                   3179:         for ( t = fd0; t; t = NEXT(t) )
1.164     noro     3180:             ndv_homogenize((NDV)BDY(t),obpe,oadv,oepos,ompos);
1.157     noro     3181:     }
                   3182:     while ( 1 ) {
1.183     noro     3183:                tl1 = tl2 = tl3 = tl4 = 0;
1.157     noro     3184:         if ( Demand )
                   3185:             nd_demand = 1;
1.187     noro     3186:         ret = ndv_setup(m,1,fd0,nd_gbblock?1:0,0);
1.172     noro     3187:         if ( nd_gentrace ) {
1.167     noro     3188:             MKLIST(l1,nd_tracelist); MKNODE(nd_alltracelist,l1,0);
                   3189:         }
1.177     noro     3190:         if ( ret )
                   3191:             cand = f4?nd_f4_trace(m,&perm):nd_gb_trace(m,ishomo || homo,&perm);
                   3192:         if ( !ret || !cand ) {
1.157     noro     3193:             /* failure */
                   3194:             if ( trace > 1 ) { *rp = 0; return; }
                   3195:             else m = get_lprime(++mindex);
                   3196:             continue;
                   3197:         }
                   3198:         if ( !ishomo && homo ) {
                   3199:             /* dehomogenization */
                   3200:             for ( t = cand; t; t = NEXT(t) ) ndv_dehomogenize((NDV)BDY(t),ord);
                   3201:             nd_init_ord(ord);
                   3202:             nd_setup_parameters(nvar,0);
                   3203:         }
                   3204:         nd_demand = 0;
1.167     noro     3205:         cand = ndv_reducebase(cand,perm);
1.172     noro     3206:         if ( nd_gentrace ) { tl1 = nd_alltracelist; nd_alltracelist = 0; }
1.157     noro     3207:         cand = ndv_reduceall(0,cand);
                   3208:         cbpe = nd_bpe;
1.172     noro     3209:         if ( nd_gentrace ) { tl2 = nd_alltracelist; nd_alltracelist = 0; }
1.173     noro     3210:         get_eg(&eg0);
1.157     noro     3211:         if ( nocheck )
                   3212:             break;
1.170     noro     3213:         if ( ret = ndv_check_membership(0,in0,obpe,oadv,oepos,cand) ) {
1.172     noro     3214:             if ( nd_gentrace ) {
1.168     noro     3215:                            tl3 = nd_alltracelist; nd_alltracelist = 0;
                   3216:                    } else tl3 = 0;
                   3217:             /* gbcheck : cand is a GB of Id(cand) ? */
1.172     noro     3218:             ret = nd_gb(0,0,1,nd_gensyz?1:0,0)!=0;
                   3219:             if ( nd_gentrace && nd_gensyz ) {
1.168     noro     3220:                            tl4 = nd_alltracelist; nd_alltracelist = 0;
                   3221:                    } else tl4 = 0;
                   3222:                }
                   3223:                if ( ret ) break;
1.157     noro     3224:         else if ( trace > 1 ) {
                   3225:             /* failure */
                   3226:             *rp = 0; return;
                   3227:         } else {
                   3228:             /* try the next modulus */
                   3229:             m = get_lprime(++mindex);
                   3230:             /* reset the parameters */
                   3231:             if ( !ishomo && homo ) {
                   3232:                 nd_init_ord(ord1);
                   3233:                 nd_setup_parameters(nvar+1,wmax);
                   3234:             } else {
                   3235:                 nd_init_ord(ord);
                   3236:                 nd_setup_parameters(nvar,max);
                   3237:             }
                   3238:         }
                   3239:     }
                   3240:     get_eg(&eg1); init_eg(&eg_check); add_eg(&eg_check,&eg0,&eg1);
                   3241:     if ( DP_Print )
                   3242:         fprintf(asir_out,"check=%fsec\n",eg_check.exectime+eg_check.gctime);
                   3243:     /* dp->p */
                   3244:     nd_bpe = cbpe;
                   3245:     nd_setup_parameters(nd_nvar,0);
1.158     noro     3246:     for ( r = cand; r; r = NEXT(r) ) {
1.167     noro     3247:     if ( nd_module ) BDY(r) = ndvtopl(0,CO,vv,BDY(r),mrank);
1.158     noro     3248:         else BDY(r) = (pointer)ndvtop(0,CO,vv,BDY(r));
                   3249:     }
1.157     noro     3250:     if ( nalg )
                   3251:         cand = postprocess_algcoef(av,alist,cand);
                   3252:     MKLIST(*rp,cand);
1.172     noro     3253:     if ( nd_gentrace ) {
1.167     noro     3254:         tl1 = reverse_node(tl1); tl2 = reverse_node(tl2);
1.168     noro     3255:                tl3 = reverse_node(tl3);
1.167     noro     3256:                /* tl2 = [[i,[[*,j,*,*],...]],...] */
                   3257:         for ( t = tl2; t; t = NEXT(t) ) {
                   3258:                        /* s = [i,[*,j,*,*],...] */
                   3259:             s = BDY((LIST)BDY(t));
                   3260:             j = perm[QTOS((Q)ARG0(s))]; STOQ(j,jq); ARG0(s) = (pointer)jq;
                   3261:                        for ( s = BDY((LIST)ARG1(s)); s; s = NEXT(s) ) {
                   3262:                 j = perm[QTOS((Q)ARG1(BDY((LIST)BDY(s))))]; STOQ(j,jq);
                   3263:                                ARG1(BDY((LIST)BDY(s))) = (pointer)jq;
                   3264:             }
                   3265:                }
                   3266:                for ( j = length(cand)-1, t = 0; j >= 0; j-- ) {
                   3267:                    STOQ(perm[j],jq); MKNODE(s,jq,t); t = s;
                   3268:                }
1.168     noro     3269:         MKLIST(l1,tl1); MKLIST(l2,tl2); MKLIST(l3,t); MKLIST(l4,tl3);
                   3270:                MKLIST(l5,tl4);
1.169     noro     3271:         tr = mknode(7,*rp,(!ishomo&&homo)?ONE:0,l1,l2,l3,l4,l5); MKLIST(*rp,tr);
1.167     noro     3272:     }
1.157     noro     3273: }
1.52      noro     3274:
1.157     noro     3275: /* XXX : module element is not considered  */
1.1       noro     3276:
1.61      noro     3277: void dltondl(int n,DL dl,UINT *r)
1.1       noro     3278: {
1.157     noro     3279:     UINT *d;
                   3280:     int i,j,l,s,ord_l;
                   3281:     struct order_pair *op;
                   3282:
                   3283:     d = dl->d;
                   3284:     for ( i = 0; i < nd_wpd; i++ ) r[i] = 0;
                   3285:     if ( nd_blockmask ) {
                   3286:         l = nd_blockmask->n;
                   3287:         op = nd_blockmask->order_pair;
                   3288:         for ( j = 0, s = 0; j < l; j++ ) {
                   3289:             ord_l = op[j].length;
                   3290:             for ( i = 0; i < ord_l; i++, s++ ) PUT_EXP(r,s,d[s]);
                   3291:         }
                   3292:         TD(r) = ndl_weight(r);
                   3293:         ndl_weight_mask(r);
                   3294:     } else {
                   3295:         for ( i = 0; i < n; i++ ) PUT_EXP(r,i,d[i]);
                   3296:         TD(r) = ndl_weight(r);
                   3297:     }
1.1       noro     3298: }
                   3299:
1.61      noro     3300: DL ndltodl(int n,UINT *ndl)
1.1       noro     3301: {
1.157     noro     3302:     DL dl;
                   3303:     int *d;
                   3304:     int i,j,l,s,ord_l;
                   3305:     struct order_pair *op;
                   3306:
                   3307:     NEWDL(dl,n);
                   3308:     dl->td = TD(ndl);
                   3309:     d = dl->d;
                   3310:     if ( nd_blockmask ) {
                   3311:         l = nd_blockmask->n;
                   3312:         op = nd_blockmask->order_pair;
                   3313:         for ( j = 0, s = 0; j < l; j++ ) {
                   3314:             ord_l = op[j].length;
                   3315:             for ( i = 0; i < ord_l; i++, s++ ) d[s] = GET_EXP(ndl,s);
                   3316:         }
                   3317:     } else {
                   3318:         for ( i = 0; i < n; i++ ) d[i] = GET_EXP(ndl,i);
                   3319:     }
                   3320:     return dl;
1.1       noro     3321: }
                   3322:
1.167     noro     3323: void nmtodp(int mod,NM m,DP *r)
                   3324: {
                   3325:     DP dp;
                   3326:     MP mr;
                   3327:
                   3328:     NEWMP(mr);
                   3329:     mr->dl = ndltodl(nd_nvar,DL(m));
                   3330:     mr->c = ndctop(mod,m->c);
                   3331:     NEXT(mr) = 0; MKDP(nd_nvar,mr,dp); dp->sugar = mr->dl->td;
                   3332:     *r = dp;
                   3333: }
                   3334:
1.61      noro     3335: void ndl_print(UINT *dl)
1.1       noro     3336: {
1.157     noro     3337:     int n;
                   3338:     int i,j,l,ord_l,s,s0;
                   3339:     struct order_pair *op;
                   3340:
                   3341:     n = nd_nvar;
                   3342:     printf("<<");
                   3343:     if ( nd_blockmask ) {
                   3344:         l = nd_blockmask->n;
                   3345:         op = nd_blockmask->order_pair;
                   3346:         for ( j = 0, s = s0 = 0; j < l; j++ ) {
                   3347:             ord_l = op[j].length;
                   3348:             for ( i = 0; i < ord_l; i++, s++ )
                   3349:                 printf(s==n-1?"%d":"%d,",GET_EXP(dl,s));
                   3350:         }
                   3351:     } else {
                   3352:         for ( i = 0; i < n; i++ ) printf(i==n-1?"%d":"%d,",GET_EXP(dl,i));
                   3353:     }
                   3354:     printf(">>");
                   3355:     if ( MPOS(dl) )
                   3356:         printf("*e%d",MPOS(dl));
1.1       noro     3357: }
                   3358:
                   3359: void nd_print(ND p)
                   3360: {
1.157     noro     3361:     NM m;
1.1       noro     3362:
1.157     noro     3363:     if ( !p )
                   3364:         printf("0\n");
                   3365:     else {
                   3366:         for ( m = BDY(p); m; m = NEXT(m) ) {
                   3367:             if ( CM(m) & 0x80000000 ) printf("+@_%d*",IFTOF(CM(m)));
                   3368:             else printf("+%d*",CM(m));
                   3369:             ndl_print(DL(m));
                   3370:         }
                   3371:         printf("\n");
                   3372:     }
1.1       noro     3373: }
                   3374:
1.113     noro     3375: void nd_print_q(ND p)
1.16      noro     3376: {
1.157     noro     3377:     NM m;
1.16      noro     3378:
1.157     noro     3379:     if ( !p )
                   3380:         printf("0\n");
                   3381:     else {
                   3382:         for ( m = BDY(p); m; m = NEXT(m) ) {
                   3383:             printf("+");
                   3384:             printexpr(CO,(Obj)CQ(m));
                   3385:             printf("*");
                   3386:             ndl_print(DL(m));
                   3387:         }
                   3388:         printf("\n");
                   3389:     }
1.16      noro     3390: }
                   3391:
1.1       noro     3392: void ndp_print(ND_pairs d)
                   3393: {
1.157     noro     3394:     ND_pairs t;
1.1       noro     3395:
1.157     noro     3396:     for ( t = d; t; t = NEXT(t) ) printf("%d,%d ",t->i1,t->i2);
                   3397:     printf("\n");
1.1       noro     3398: }
                   3399:
1.20      noro     3400: void nd_removecont(int mod,ND p)
1.16      noro     3401: {
1.157     noro     3402:     int i,n;
                   3403:     Q *w;
                   3404:     Q dvr,t;
                   3405:     NM m;
                   3406:     struct oVECT v;
                   3407:     N q,r;
                   3408:
                   3409:     if ( mod == -1 ) nd_mul_c(mod,p,_invsf(HCM(p)));
                   3410:     else if ( mod ) nd_mul_c(mod,p,invm(HCM(p),mod));
                   3411:     else {
                   3412:         for ( m = BDY(p), n = 0; m; m = NEXT(m), n++ );
                   3413:         w = (Q *)ALLOCA(n*sizeof(Q));
                   3414:         v.len = n;
                   3415:         v.body = (pointer *)w;
                   3416:         for ( m = BDY(p), i = 0; i < n; m = NEXT(m), i++ ) w[i] = CQ(m);
                   3417:         removecont_array((P *)w,n,1);
                   3418:         for ( m = BDY(p), i = 0; i < n; m = NEXT(m), i++ ) CQ(m) = w[i];
                   3419:     }
1.16      noro     3420: }
                   3421:
1.21      noro     3422: void nd_removecont2(ND p1,ND p2)
                   3423: {
1.157     noro     3424:     int i,n1,n2,n;
                   3425:     Q *w;
                   3426:     Q dvr,t;
                   3427:     NM m;
                   3428:     struct oVECT v;
                   3429:     N q,r;
                   3430:
                   3431:     n1 = nd_length(p1);
                   3432:     n2 = nd_length(p2);
                   3433:     n = n1+n2;
                   3434:     w = (Q *)ALLOCA(n*sizeof(Q));
                   3435:     v.len = n;
                   3436:     v.body = (pointer *)w;
                   3437:     i = 0;
                   3438:     if ( p1 )
                   3439:         for ( m = BDY(p1); i < n1; m = NEXT(m), i++ ) w[i] = CQ(m);
                   3440:     if ( p2 )
                   3441:         for ( m = BDY(p2); i < n; m = NEXT(m), i++ ) w[i] = CQ(m);
                   3442:     removecont_array((P *)w,n,1);
                   3443:     i = 0;
                   3444:     if ( p1 )
                   3445:         for ( m = BDY(p1); i < n1; m = NEXT(m), i++ ) CQ(m) = w[i];
                   3446:     if ( p2 )
                   3447:         for ( m = BDY(p2); i < n; m = NEXT(m), i++ ) CQ(m) = w[i];
1.21      noro     3448: }
                   3449:
1.20      noro     3450: void ndv_removecont(int mod,NDV p)
1.16      noro     3451: {
1.157     noro     3452:     int i,len,all_p;
                   3453:     Q *c;
                   3454:     P *w;
                   3455:     Q dvr,t;
                   3456:     P g,cont,tp;
                   3457:     NMV m;
                   3458:
                   3459:     if ( mod == -1 )
                   3460:         ndv_mul_c(mod,p,_invsf(HCM(p)));
                   3461:     else if ( mod )
                   3462:         ndv_mul_c(mod,p,invm(HCM(p),mod));
                   3463:     else {
                   3464:         len = p->len;
                   3465:         w = (P *)ALLOCA(len*sizeof(P));
                   3466:         c = (Q *)ALLOCA(len*sizeof(Q));
                   3467:         for ( m = BDY(p), all_p = 1, i = 0; i < len; NMV_ADV(m), i++ ) {
                   3468:             ptozp(CP(m),1,&c[i],&w[i]);
                   3469:             all_p = all_p && !NUM(w[i]);
                   3470:         }
                   3471:         if ( all_p ) {
                   3472:             qltozl(c,len,&dvr); nd_heu_nezgcdnpz(nd_vc,w,len,1,&g);
                   3473:             mulp(nd_vc,(P)dvr,g,&cont);
                   3474:             for ( m = BDY(p), i = 0; i < len; NMV_ADV(m), i++ ) {
                   3475:                 divsp(nd_vc,CP(m),cont,&tp); CP(m) = tp;
                   3476:             }
                   3477:         } else {
                   3478:             sortbynm((Q *)c,len);
                   3479:             qltozl((Q *)c,len,&dvr);
                   3480:             for ( m = BDY(p), i = 0; i < len; NMV_ADV(m), i++ ) {
                   3481:                 divsp(nd_vc,CP(m),(P)dvr,&tp); CP(m) = tp;
                   3482:             }
                   3483:         }
                   3484:     }
1.21      noro     3485: }
                   3486:
1.157     noro     3487: /* koko */
                   3488:
1.164     noro     3489: void ndv_homogenize(NDV p,int obpe,int oadv,EPOS oepos,int ompos)
1.61      noro     3490: {
1.157     noro     3491:     int len,i,max;
                   3492:     NMV m,mr0,mr,t;
1.61      noro     3493:
1.157     noro     3494:     len = p->len;
1.178     noro     3495:     for ( m = BDY(p), i = 0, max = 1; i < len; NMV_OADV(m), i++ )
1.157     noro     3496:         max = MAX(max,TD(DL(m)));
                   3497:     mr0 = nmv_adv>oadv?(NMV)REALLOC(BDY(p),len*nmv_adv):BDY(p);
                   3498:     m = (NMV)((char *)mr0+(len-1)*oadv);
                   3499:     mr = (NMV)((char *)mr0+(len-1)*nmv_adv);
                   3500:     t = (NMV)ALLOCA(nmv_adv);
                   3501:     for ( i = 0; i < len; i++, NMV_OPREV(m), NMV_PREV(mr) ) {
1.164     noro     3502:         ndl_homogenize(DL(m),DL(t),obpe,oepos,ompos,max);
1.157     noro     3503:         CQ(mr) = CQ(m);
                   3504:         ndl_copy(DL(t),DL(mr));
                   3505:     }
                   3506:     NV(p)++;
                   3507:     BDY(p) = mr0;
1.61      noro     3508: }
                   3509:
1.45      noro     3510: void ndv_dehomogenize(NDV p,struct order_spec *ord)
1.23      noro     3511: {
1.164     noro     3512:     int i,j,adj,len,newnvar,newwpd,newadv,newexporigin,newmpos;
1.167     noro     3513:     int pos;
1.157     noro     3514:     Q *w;
                   3515:     Q dvr,t;
                   3516:     NMV m,r;
                   3517:
                   3518:     len = p->len;
                   3519:     newnvar = nd_nvar-1;
                   3520:     newexporigin = nd_get_exporigin(ord);
1.167     noro     3521:     if ( nd_module ) newmpos = newexporigin-1;
1.157     noro     3522:     newwpd = newnvar/nd_epw+(newnvar%nd_epw?1:0)+newexporigin;
                   3523:     for ( m = BDY(p), i = 0; i < len; NMV_ADV(m), i++ )
                   3524:         ndl_dehomogenize(DL(m));
                   3525:     if ( newwpd != nd_wpd ) {
                   3526:         newadv = ROUND_FOR_ALIGN(sizeof(struct oNMV)+(newwpd-1)*sizeof(UINT));
                   3527:         for ( m = r = BDY(p), i = 0; i < len; NMV_ADV(m), NDV_NADV(r), i++ ) {
                   3528:             CQ(r) = CQ(m);
1.167     noro     3529:             if ( nd_module ) pos = MPOS(DL(m));
1.157     noro     3530:             for ( j = 0; j < newexporigin; j++ ) DL(r)[j] = DL(m)[j];
                   3531:             adj = nd_exporigin-newexporigin;
                   3532:             for ( ; j < newwpd; j++ ) DL(r)[j] = DL(m)[j+adj];
1.167     noro     3533:             if ( nd_module ) {
                   3534:                 DL(r)[newmpos] = pos;
                   3535:             }
1.157     noro     3536:         }
                   3537:     }
                   3538:     NV(p)--;
1.23      noro     3539: }
                   3540:
1.150     noro     3541: void nd_heu_nezgcdnpz(VL vl,P *pl,int m,int full,P *pr)
                   3542: {
1.157     noro     3543:     int i;
                   3544:     P *tpl,*tpl1;
                   3545:     NODE l;
                   3546:     P h,gcd,t;
                   3547:
                   3548:     tpl = (P *)ALLOCA(m*sizeof(P));
                   3549:     tpl1 = (P *)ALLOCA(m*sizeof(P));
                   3550:     bcopy(pl,tpl,m*sizeof(P));
                   3551:     gcd = (P)ONE;
                   3552:     for ( l = nd_hcf; l; l = NEXT(l) ) {
                   3553:         h = (P)BDY(l);
                   3554:         while ( 1 ) {
                   3555:             for ( i = 0; i < m; i++ )
                   3556:                 if ( !divtpz(vl,tpl[i],h,&tpl1[i]) )
                   3557:                     break;
                   3558:             if ( i == m ) {
                   3559:                 bcopy(tpl1,tpl,m*sizeof(P));
                   3560:                 mulp(vl,gcd,h,&t); gcd = t;
                   3561:             } else
                   3562:                 break;
                   3563:         }
                   3564:     }
                   3565:     if ( DP_Print > 2 ){fprintf(asir_out,"[%d]",nmonop(gcd)); fflush(asir_out);}
                   3566:     if ( full ) {
                   3567:         heu_nezgcdnpz(vl,tpl,m,&t);
                   3568:         mulp(vl,gcd,t,pr);
                   3569:     } else
                   3570:         *pr = gcd;
1.150     noro     3571: }
                   3572:
                   3573: void removecont_array(P *p,int n,int full)
1.146     noro     3574: {
1.157     noro     3575:     int all_p,all_q,i;
                   3576:     Q *c;
                   3577:     P *w;
                   3578:     P t,s;
                   3579:
                   3580:     for ( all_q = 1, i = 0; i < n; i++ )
                   3581:         all_q = all_q && NUM(p[i]);
                   3582:     if ( all_q ) {
                   3583:         removecont_array_q((Q *)p,n);
                   3584:     } else {
                   3585:         c = (Q *)ALLOCA(n*sizeof(Q));
                   3586:         w = (P *)ALLOCA(n*sizeof(P));
                   3587:         for ( i = 0; i < n; i++ ) {
                   3588:             ptozp(p[i],1,&c[i],&w[i]);
                   3589:         }
                   3590:         removecont_array_q(c,n);
                   3591:         nd_heu_nezgcdnpz(nd_vc,w,n,full,&t);
                   3592:         for ( i = 0; i < n; i++ ) {
                   3593:             divsp(nd_vc,w[i],t,&s); mulp(nd_vc,s,(P)c[i],&p[i]);
                   3594:         }
                   3595:     }
1.146     noro     3596: }
                   3597:
                   3598: void removecont_array_q(Q *c,int n)
1.21      noro     3599: {
1.157     noro     3600:     struct oVECT v;
                   3601:     Q d0,d1,a,u,u1,gcd;
                   3602:     int i,j;
                   3603:     N qn,rn,gn;
                   3604:     Q *q,*r;
                   3605:
                   3606:     q = (Q *)ALLOCA(n*sizeof(Q));
                   3607:     r = (Q *)ALLOCA(n*sizeof(Q));
                   3608:     v.id = O_VECT; v.len = n; v.body = (pointer *)c;
                   3609:     igcdv_estimate(&v,&d0);
                   3610:     for ( i = 0; i < n; i++ ) {
                   3611:         divn(NM(c[i]),NM(d0),&qn,&rn);
                   3612:         NTOQ(qn,SGN(c[i])*SGN(d0),q[i]);
                   3613:         NTOQ(rn,SGN(c[i]),r[i]);
                   3614:     }
                   3615:     for ( i = 0; i < n; i++ ) if ( r[i] ) break;
                   3616:     if ( i < n ) {
                   3617:         v.id = O_VECT; v.len = n; v.body = (pointer *)r;
                   3618:         igcdv(&v,&d1);
                   3619:         gcdn(NM(d0),NM(d1),&gn); NTOQ(gn,1,gcd);
                   3620:         divsn(NM(d0),gn,&qn); NTOQ(qn,1,a);
                   3621:         for ( i = 0; i < n; i++ ) {
                   3622:             mulq(a,q[i],&u);
                   3623:             if ( r[i] ) {
                   3624:                 divsn(NM(r[i]),gn,&qn); NTOQ(qn,SGN(r[i]),u1);
                   3625:                 addq(u,u1,&q[i]);
                   3626:             } else
                   3627:                 q[i] = u;
                   3628:         }
                   3629:     }
                   3630:     for ( i = 0; i < n; i++ ) c[i] = q[i];
1.16      noro     3631: }
                   3632:
1.19      noro     3633: void nd_mul_c(int mod,ND p,int mul)
1.1       noro     3634: {
1.157     noro     3635:     NM m;
                   3636:     int c,c1;
1.1       noro     3637:
1.157     noro     3638:     if ( !p ) return;
                   3639:     if ( mul == 1 ) return;
                   3640:     if ( mod == -1 )
                   3641:         for ( m = BDY(p); m; m = NEXT(m) )
                   3642:             CM(m) = _mulsf(CM(m),mul);
                   3643:     else
                   3644:         for ( m = BDY(p); m; m = NEXT(m) ) {
                   3645:             c1 = CM(m); DMAR(c1,mul,0,mod,c); CM(m) = c;
                   3646:         }
1.1       noro     3647: }
                   3648:
1.146     noro     3649: void nd_mul_c_q(ND p,P mul)
1.16      noro     3650: {
1.157     noro     3651:     NM m;
                   3652:     P c;
1.16      noro     3653:
1.157     noro     3654:     if ( !p ) return;
                   3655:     if ( UNIQ(mul) ) return;
                   3656:     for ( m = BDY(p); m; m = NEXT(m) ) {
                   3657:         mulp(nd_vc,CP(m),mul,&c); CP(m) = c;
                   3658:     }
1.16      noro     3659: }
                   3660:
1.61      noro     3661: void nd_mul_c_p(VL vl,ND p,P mul)
                   3662: {
1.157     noro     3663:     NM m;
                   3664:     P c;
1.61      noro     3665:
1.157     noro     3666:     if ( !p ) return;
                   3667:     for ( m = BDY(p); m; m = NEXT(m) ) {
                   3668:         mulp(vl,CP(m),mul,&c); CP(m) = c;
                   3669:     }
1.61      noro     3670: }
                   3671:
1.1       noro     3672: void nd_free(ND p)
                   3673: {
1.157     noro     3674:     NM t,s;
1.1       noro     3675:
1.157     noro     3676:     if ( !p ) return;
                   3677:     t = BDY(p);
                   3678:     while ( t ) {
                   3679:         s = NEXT(t);
                   3680:         FREENM(t);
                   3681:         t = s;
                   3682:     }
                   3683:     FREEND(p);
1.1       noro     3684: }
                   3685:
1.23      noro     3686: void ndv_free(NDV p)
                   3687: {
1.157     noro     3688:     GC_free(BDY(p));
1.23      noro     3689: }
                   3690:
1.61      noro     3691: void nd_append_red(UINT *d,int i)
1.1       noro     3692: {
1.157     noro     3693:     RHist m,m0;
                   3694:     int h;
1.1       noro     3695:
1.157     noro     3696:     NEWRHist(m);
                   3697:     h = ndl_hash_value(d);
                   3698:     m->index = i;
                   3699:     ndl_copy(d,DL(m));
                   3700:     NEXT(m) = nd_red[h];
                   3701:     nd_red[h] = m;
1.1       noro     3702: }
                   3703:
1.61      noro     3704: UINT *ndv_compute_bound(NDV p)
1.1       noro     3705: {
1.157     noro     3706:     UINT *d1,*d2,*t;
                   3707:     UINT u;
                   3708:     int i,j,k,l,len,ind;
                   3709:     NMV m;
                   3710:
                   3711:     if ( !p )
                   3712:         return 0;
                   3713:     d1 = (UINT *)ALLOCA(nd_wpd*sizeof(UINT));
                   3714:     d2 = (UINT *)ALLOCA(nd_wpd*sizeof(UINT));
                   3715:     len = LEN(p);
                   3716:     m = BDY(p); ndl_copy(DL(m),d1); NMV_ADV(m);
                   3717:     for ( i = 1; i < len; i++, NMV_ADV(m) ) {
1.159     noro     3718:         ndl_max(DL(m),d1,d2);
1.157     noro     3719:         t = d1; d1 = d2; d2 = t;
                   3720:     }
                   3721:     l = nd_nvar+31;
                   3722:     t = (UINT *)MALLOC_ATOMIC(l*sizeof(UINT));
                   3723:     for ( i = nd_exporigin, ind = 0; i < nd_wpd; i++ ) {
                   3724:         u = d1[i];
                   3725:         k = (nd_epw-1)*nd_bpe;
                   3726:         for ( j = 0; j < nd_epw; j++, k -= nd_bpe, ind++ )
                   3727:             t[ind] = (u>>k)&nd_mask0;
                   3728:     }
                   3729:     for ( ; ind < l; ind++ ) t[ind] = 0;
                   3730:     return t;
1.1       noro     3731: }
                   3732:
1.99      noro     3733: UINT *nd_compute_bound(ND p)
                   3734: {
1.157     noro     3735:     UINT *d1,*d2,*t;
                   3736:     UINT u;
                   3737:     int i,j,k,l,len,ind;
                   3738:     NM m;
                   3739:
                   3740:     if ( !p )
                   3741:         return 0;
                   3742:     d1 = (UINT *)ALLOCA(nd_wpd*sizeof(UINT));
                   3743:     d2 = (UINT *)ALLOCA(nd_wpd*sizeof(UINT));
                   3744:     len = LEN(p);
                   3745:     m = BDY(p); ndl_copy(DL(m),d1); m = NEXT(m);
                   3746:     for ( m = NEXT(m); m; m = NEXT(m) ) {
                   3747:         ndl_lcm(DL(m),d1,d2);
                   3748:         t = d1; d1 = d2; d2 = t;
                   3749:     }
                   3750:     l = nd_nvar+31;
                   3751:     t = (UINT *)MALLOC_ATOMIC(l*sizeof(UINT));
                   3752:     for ( i = nd_exporigin, ind = 0; i < nd_wpd; i++ ) {
                   3753:         u = d1[i];
                   3754:         k = (nd_epw-1)*nd_bpe;
                   3755:         for ( j = 0; j < nd_epw; j++, k -= nd_bpe, ind++ )
                   3756:             t[ind] = (u>>k)&nd_mask0;
                   3757:     }
                   3758:     for ( ; ind < l; ind++ ) t[ind] = 0;
                   3759:     return t;
1.99      noro     3760: }
                   3761:
1.157     noro     3762: /* if nd_module == 1 then d[nd_exporigin-1] indicates the position */
                   3763: /* of a term. In this case we need additional 1 word. */
                   3764:
1.48      noro     3765: int nd_get_exporigin(struct order_spec *ord)
                   3766: {
1.157     noro     3767:     switch ( ord->id ) {
                   3768:         case 0: case 2: case 256: case 258:
                   3769:             return 1+nd_module;
                   3770:         case 1: case 257:
                   3771:             /* block order */
                   3772:             /* poly ring d[0]:weight d[1]:w0,...,d[nd_exporigin-1]:w(n-1) */
                   3773:             /* module d[0]:weight d[1]:w0,...,d[nd_exporigin-2]:w(n-1) */
                   3774:             return ord->ord.block.length+1+nd_module;
                   3775:         case 3: case 259:
                   3776:             error("nd_get_exporigin : composite order is not supported yet.");
                   3777:     }
1.48      noro     3778: }
                   3779:
1.61      noro     3780: void nd_setup_parameters(int nvar,int max) {
1.157     noro     3781:     int i,j,n,elen,ord_o,ord_l,l,s,wpd;
                   3782:     struct order_pair *op;
1.48      noro     3783:
1.157     noro     3784:     nd_nvar = nvar;
                   3785:     if ( max ) {
                   3786:         /* XXX */
                   3787:         if ( do_weyl ) nd_bpe = 32;
                   3788:         else if ( max < 2 ) nd_bpe = 1;
                   3789:         else if ( max < 4 ) nd_bpe = 2;
                   3790:         else if ( max < 8 ) nd_bpe = 3;
                   3791:         else if ( max < 16 ) nd_bpe = 4;
                   3792:         else if ( max < 32 ) nd_bpe = 5;
                   3793:         else if ( max < 64 ) nd_bpe = 6;
                   3794:         else if ( max < 256 ) nd_bpe = 8;
                   3795:         else if ( max < 1024 ) nd_bpe = 10;
                   3796:         else if ( max < 65536 ) nd_bpe = 16;
                   3797:         else nd_bpe = 32;
                   3798:     }
                   3799:     nd_epw = (sizeof(UINT)*8)/nd_bpe;
                   3800:     elen = nd_nvar/nd_epw+(nd_nvar%nd_epw?1:0);
                   3801:     nd_exporigin = nd_get_exporigin(nd_ord);
                   3802:     wpd = nd_exporigin+elen;
                   3803:     if ( nd_module )
                   3804:         nd_mpos = nd_exporigin-1;
                   3805:     else
                   3806:         nd_mpos = -1;
                   3807:     if ( wpd != nd_wpd ) {
                   3808:         nd_free_private_storage();
                   3809:         nd_wpd = wpd;
                   3810:     }
                   3811:     if ( nd_bpe < 32 ) {
                   3812:         nd_mask0 = (1<<nd_bpe)-1;
                   3813:     } else {
                   3814:         nd_mask0 = 0xffffffff;
                   3815:     }
                   3816:     bzero(nd_mask,sizeof(nd_mask));
                   3817:     nd_mask1 = 0;
                   3818:     for ( i = 0; i < nd_epw; i++ ) {
                   3819:         nd_mask[nd_epw-i-1] = (nd_mask0<<(i*nd_bpe));
                   3820:         nd_mask1 |= (1<<(nd_bpe-1))<<(i*nd_bpe);
                   3821:     }
                   3822:     nmv_adv = ROUND_FOR_ALIGN(sizeof(struct oNMV)+(nd_wpd-1)*sizeof(UINT));
                   3823:     nd_epos = nd_create_epos(nd_ord);
                   3824:     nd_blockmask = nd_create_blockmask(nd_ord);
                   3825:     nd_work_vector = (int *)REALLOC(nd_work_vector,nd_nvar*sizeof(int));
1.1       noro     3826: }
                   3827:
1.103     noro     3828: ND_pairs nd_reconstruct(int trace,ND_pairs d)
1.1       noro     3829: {
1.157     noro     3830:     int i,obpe,oadv,h;
                   3831:     static NM prev_nm_free_list;
                   3832:     static ND_pairs prev_ndp_free_list;
                   3833:     RHist mr0,mr;
                   3834:     RHist r;
                   3835:     RHist *old_red;
                   3836:     ND_pairs s0,s,t;
                   3837:     EPOS oepos;
                   3838:
                   3839:     obpe = nd_bpe;
                   3840:     oadv = nmv_adv;
                   3841:     oepos = nd_epos;
                   3842:     if ( obpe < 2 ) nd_bpe = 2;
                   3843:     else if ( obpe < 3 ) nd_bpe = 3;
                   3844:     else if ( obpe < 4 ) nd_bpe = 4;
                   3845:     else if ( obpe < 5 ) nd_bpe = 5;
                   3846:     else if ( obpe < 6 ) nd_bpe = 6;
                   3847:     else if ( obpe < 8 ) nd_bpe = 8;
                   3848:     else if ( obpe < 10 ) nd_bpe = 10;
                   3849:     else if ( obpe < 16 ) nd_bpe = 16;
                   3850:     else if ( obpe < 32 ) nd_bpe = 32;
                   3851:     else error("nd_reconstruct : exponent too large");
                   3852:
                   3853:     nd_setup_parameters(nd_nvar,0);
                   3854:     prev_nm_free_list = _nm_free_list;
                   3855:     prev_ndp_free_list = _ndp_free_list;
                   3856:     _nm_free_list = 0;
                   3857:     _ndp_free_list = 0;
                   3858:     for ( i = nd_psn-1; i >= 0; i-- ) ndv_realloc(nd_ps[i],obpe,oadv,oepos);
                   3859:     if ( trace )
                   3860:         for ( i = nd_psn-1; i >= 0; i-- )
                   3861:             ndv_realloc(nd_ps_trace[i],obpe,oadv,oepos);
                   3862:     s0 = 0;
                   3863:     for ( t = d; t; t = NEXT(t) ) {
                   3864:         NEXTND_pairs(s0,s);
                   3865:         s->i1 = t->i1;
                   3866:         s->i2 = t->i2;
                   3867:         SG(s) = SG(t);
                   3868:         ndl_reconstruct(LCM(t),LCM(s),obpe,oepos);
                   3869:     }
                   3870:
                   3871:     old_red = (RHist *)ALLOCA(REDTAB_LEN*sizeof(RHist));
                   3872:     for ( i = 0; i < REDTAB_LEN; i++ ) {
                   3873:         old_red[i] = nd_red[i];
                   3874:         nd_red[i] = 0;
                   3875:     }
                   3876:     for ( i = 0; i < REDTAB_LEN; i++ )
                   3877:         for ( r = old_red[i]; r; r = NEXT(r) ) {
                   3878:             NEWRHist(mr);
                   3879:             mr->index = r->index;
                   3880:             SG(mr) = SG(r);
                   3881:             ndl_reconstruct(DL(r),DL(mr),obpe,oepos);
                   3882:             h = ndl_hash_value(DL(mr));
                   3883:             NEXT(mr) = nd_red[h];
                   3884:             nd_red[h] = mr;
                   3885:         }
                   3886:     for ( i = 0; i < REDTAB_LEN; i++ ) old_red[i] = 0;
                   3887:     old_red = 0;
                   3888:     for ( i = 0; i < nd_psn; i++ ) {
                   3889:         NEWRHist(r); SG(r) = SG(nd_psh[i]);
                   3890:         ndl_reconstruct(DL(nd_psh[i]),DL(r),obpe,oepos);
                   3891:         nd_psh[i] = r;
                   3892:     }
                   3893:     if ( s0 ) NEXT(s) = 0;
                   3894:     prev_nm_free_list = 0;
                   3895:     prev_ndp_free_list = 0;
1.71      noro     3896: #if 0
1.157     noro     3897:     GC_gcollect();
1.71      noro     3898: #endif
1.157     noro     3899:     return s0;
1.1       noro     3900: }
                   3901:
1.61      noro     3902: void ndl_reconstruct(UINT *d,UINT *r,int obpe,EPOS oepos)
1.1       noro     3903: {
1.157     noro     3904:     int n,i,ei,oepw,omask0,j,s,ord_l,l;
                   3905:     struct order_pair *op;
1.1       noro     3906:
1.157     noro     3907:     n = nd_nvar;
                   3908:     oepw = (sizeof(UINT)*8)/obpe;
                   3909:     omask0 = (1<<obpe)-1;
                   3910:     TD(r) = TD(d);
                   3911:     for ( i = nd_exporigin; i < nd_wpd; i++ ) r[i] = 0;
                   3912:     if ( nd_blockmask ) {
                   3913:         l = nd_blockmask->n;
                   3914:         op = nd_blockmask->order_pair;
                   3915:         for ( i = 1; i < nd_exporigin; i++ )
                   3916:             r[i] = d[i];
                   3917:         for ( j = 0, s = 0; j < l; j++ ) {
                   3918:             ord_l = op[j].length;
                   3919:             for ( i = 0; i < ord_l; i++, s++ ) {
                   3920:                 ei =  GET_EXP_OLD(d,s);
                   3921:                 PUT_EXP(r,s,ei);
                   3922:             }
                   3923:         }
                   3924:     } else {
                   3925:         for ( i = 0; i < n; i++ ) {
                   3926:             ei = GET_EXP_OLD(d,i);
                   3927:             PUT_EXP(r,i,ei);
                   3928:         }
                   3929:     }
                   3930:     if ( nd_module ) MPOS(r) = MPOS(d);
1.1       noro     3931: }
1.3       noro     3932:
1.6       noro     3933: ND nd_copy(ND p)
                   3934: {
1.157     noro     3935:     NM m,mr,mr0;
                   3936:     int c,n;
                   3937:     ND r;
                   3938:
                   3939:     if ( !p )
                   3940:         return 0;
                   3941:     else {
                   3942:         for ( mr0 = 0, m = BDY(p); m; m = NEXT(m) ) {
                   3943:             NEXTNM(mr0,mr);
                   3944:             CM(mr) = CM(m);
                   3945:             ndl_copy(DL(m),DL(mr));
                   3946:         }
                   3947:         NEXT(mr) = 0;
                   3948:         MKND(NV(p),mr0,LEN(p),r);
                   3949:         SG(r) = SG(p);
                   3950:         return r;
                   3951:     }
1.6       noro     3952: }
                   3953:
1.53      noro     3954: int nd_sp(int mod,int trace,ND_pairs p,ND *rp)
1.11      noro     3955: {
1.157     noro     3956:     NM m1,m2;
                   3957:     NDV p1,p2;
                   3958:     ND t1,t2;
                   3959:     UINT *lcm;
                   3960:     P gp,tp;
1.167     noro     3961:     Q g,t,iq;
1.157     noro     3962:     int td;
1.167     noro     3963:     LIST hist;
                   3964:     NODE node;
                   3965:     DP d;
1.157     noro     3966:
                   3967:     if ( !mod && nd_demand ) {
                   3968:         p1 = ndv_load(p->i1); p2 = ndv_load(p->i2);
                   3969:     } else {
                   3970:         if ( trace ) {
                   3971:             p1 = nd_ps_trace[p->i1]; p2 = nd_ps_trace[p->i2];
                   3972:         } else {
                   3973:             p1 = nd_ps[p->i1]; p2 = nd_ps[p->i2];
                   3974:         }
                   3975:     }
                   3976:     lcm = LCM(p);
                   3977:     NEWNM(m1); ndl_sub(lcm,HDL(p1),DL(m1));
                   3978:     if ( ndl_check_bound2(p->i1,DL(m1)) ) {
                   3979:         FREENM(m1); return 0;
                   3980:     }
                   3981:     NEWNM(m2); ndl_sub(lcm,HDL(p2),DL(m2));
                   3982:     if ( ndl_check_bound2(p->i2,DL(m2)) ) {
                   3983:         FREENM(m1); FREENM(m2); return 0;
                   3984:     }
                   3985:
                   3986:     if ( mod == -1 ) {
                   3987:         CM(m1) = HCM(p2); CM(m2) = _chsgnsf(HCM(p1));
                   3988:     } else if ( mod ) {
                   3989:         CM(m1) = HCM(p2); CM(m2) = mod-HCM(p1);
                   3990:     } else if ( nd_vc ) {
                   3991:         ezgcdpz(nd_vc,HCP(p1),HCP(p2),&gp);
                   3992:         divsp(nd_vc,HCP(p2),gp,&CP(m1));
                   3993:         divsp(nd_vc,HCP(p1),gp,&tp); chsgnp(tp,&CP(m2));
                   3994:     } else {
                   3995:         igcd_cofactor(HCQ(p1),HCQ(p2),&g,&t,&CQ(m1)); chsgnq(t,&CQ(m2));
                   3996:     }
                   3997:     t1 = ndv_mul_nm(mod,m1,p1); t2 = ndv_mul_nm(mod,m2,p2);
                   3998:     *rp = nd_add(mod,t1,t2);
1.172     noro     3999:     if ( nd_gentrace ) {
1.167     noro     4000:         /* nd_tracelist is initialized */
                   4001:         STOQ(p->i1,iq); nmtodp(mod,m1,&d); node = mknode(4,ONE,iq,d,ONE);
                   4002:         MKLIST(hist,node); MKNODE(nd_tracelist,hist,0);
                   4003:         STOQ(p->i2,iq); nmtodp(mod,m2,&d); node = mknode(4,ONE,iq,d,ONE);
                   4004:         MKLIST(hist,node); MKNODE(node,hist,nd_tracelist);
                   4005:         nd_tracelist = node;
                   4006:     }
1.157     noro     4007:     FREENM(m1); FREENM(m2);
                   4008:     return 1;
1.11      noro     4009: }
                   4010:
1.19      noro     4011: void ndv_mul_c(int mod,NDV p,int mul)
1.11      noro     4012: {
1.157     noro     4013:     NMV m;
                   4014:     int c,c1,len,i;
1.11      noro     4015:
1.157     noro     4016:     if ( !p ) return;
                   4017:     len = LEN(p);
                   4018:     if ( mod == -1 )
                   4019:         for ( m = BDY(p), i = 0; i < len; i++, NMV_ADV(m) )
                   4020:             CM(m) = _mulsf(CM(m),mul);
                   4021:     else
                   4022:         for ( m = BDY(p), i = 0; i < len; i++, NMV_ADV(m) ) {
                   4023:             c1 = CM(m); DMAR(c1,mul,0,mod,c); CM(m) = c;
                   4024:         }
1.11      noro     4025: }
                   4026:
1.113     noro     4027: void ndv_mul_c_q(NDV p,Q mul)
1.16      noro     4028: {
1.157     noro     4029:     NMV m;
                   4030:     Q c;
                   4031:     int len,i;
                   4032:
                   4033:     if ( !p ) return;
                   4034:     len = LEN(p);
                   4035:     for ( m = BDY(p), i = 0; i < len; i++, NMV_ADV(m) ) {
                   4036:         mulq(CQ(m),mul,&c); CQ(m) = c;
                   4037:     }
1.16      noro     4038: }
                   4039:
1.55      noro     4040: ND weyl_ndv_mul_nm(int mod,NM m0,NDV p) {
1.157     noro     4041:     int n2,i,j,l,n,tlen;
                   4042:     UINT *d0;
                   4043:     NM *tab,*psum;
                   4044:     ND s,r;
                   4045:     NM t;
                   4046:     NMV m1;
                   4047:
                   4048:     if ( !p ) return 0;
                   4049:     n = NV(p); n2 = n>>1;
                   4050:     d0 = DL(m0);
                   4051:     l = LEN(p);
                   4052:     for ( i = 0, tlen = 1; i < n2; i++ ) tlen *= (GET_EXP(d0,n2+i)+1);
                   4053:     tab = (NM *)ALLOCA(tlen*sizeof(NM));
                   4054:     psum = (NM *)ALLOCA(tlen*sizeof(NM));
                   4055:     for ( i = 0; i < tlen; i++ ) psum[i] = 0;
                   4056:     m1 = (NMV)(((char *)BDY(p))+nmv_adv*(l-1));
                   4057:     for ( i = l-1; i >= 0; i--, NMV_PREV(m1) ) {
                   4058:         /* m0(NM) * m1(NMV) => tab(NM) */
                   4059:         weyl_mul_nm_nmv(n,mod,m0,m1,tab,tlen);
                   4060:         for ( j = 0; j < tlen; j++ ) {
                   4061:             if ( tab[j] ) {
                   4062:                 NEXT(tab[j]) = psum[j];    psum[j] = tab[j];
                   4063:             }
                   4064:         }
                   4065:     }
                   4066:     for ( i = tlen-1, r = 0; i >= 0; i-- )
                   4067:         if ( psum[i] ) {
                   4068:             for ( j = 0, t = psum[i]; t; t = NEXT(t), j++ );
                   4069:             MKND(n,psum[i],j,s);
                   4070:             r = nd_add(mod,r,s);
                   4071:         }
                   4072:     if ( r ) SG(r) = SG(p)+TD(d0);
                   4073:     return r;
1.55      noro     4074: }
                   4075:
1.56      noro     4076: /* product of monomials */
                   4077: /* XXX block order is not handled correctly */
                   4078:
1.55      noro     4079: void weyl_mul_nm_nmv(int n,int mod,NM m0,NMV m1,NM *tab,int tlen)
                   4080: {
1.157     noro     4081:     int i,n2,j,s,curlen,homo,h,a,b,k,l,u,min;
                   4082:     UINT *d0,*d1,*d,*dt,*ctab;
                   4083:     Q *ctab_q;
                   4084:     Q q,q1;
                   4085:     UINT c0,c1,c;
                   4086:     NM *p;
                   4087:     NM m,t;
                   4088:     int mpos;
                   4089:
                   4090:     for ( i = 0; i < tlen; i++ ) tab[i] = 0;
                   4091:     if ( !m0 || !m1 ) return;
                   4092:     d0 = DL(m0); d1 = DL(m1); n2 = n>>1;
1.166     noro     4093:     if ( nd_module )
                   4094:         if ( MPOS(d0) ) error("weyl_mul_nm_nmv : invalid operation");
                   4095:
1.157     noro     4096:     NEWNM(m); d = DL(m);
                   4097:     if ( mod ) {
                   4098:         c0 = CM(m0); c1 = CM(m1); DMAR(c0,c1,0,mod,c); CM(m) = c;
1.179     noro     4099:     } else if ( nd_vc )
                   4100:         mulp(nd_vc,CP(m0),CP(m1),&CP(m));
                   4101:        else
1.157     noro     4102:         mulq(CQ(m0),CQ(m1),&CQ(m));
                   4103:     for ( i = 0; i < nd_wpd; i++ ) d[i] = 0;
                   4104:     homo = n&1 ? 1 : 0;
                   4105:     if ( homo ) {
                   4106:         /* offset of h-degree */
                   4107:         h = GET_EXP(d0,n-1)+GET_EXP(d1,n-1);
                   4108:         PUT_EXP(DL(m),n-1,h);
                   4109:         TD(DL(m)) = h;
                   4110:         if ( nd_blockmask ) ndl_weight_mask(DL(m));
                   4111:     }
                   4112:     tab[0] = m;
                   4113:     NEWNM(m); d = DL(m);
                   4114:     for ( i = 0, curlen = 1; i < n2; i++ ) {
                   4115:         a = GET_EXP(d0,i); b = GET_EXP(d1,n2+i);
                   4116:         k = GET_EXP(d0,n2+i); l = GET_EXP(d1,i);
                   4117:         /* xi^a*(Di^k*xi^l)*Di^b */
                   4118:         a += l; b += k;
                   4119:         s = MUL_WEIGHT(a,i)+MUL_WEIGHT(b,n2+i);
                   4120:         if ( !k || !l ) {
                   4121:             for ( j = 0; j < curlen; j++ )
                   4122:                 if ( t = tab[j] ) {
                   4123:                     dt = DL(t);
                   4124:                     PUT_EXP(dt,i,a); PUT_EXP(dt,n2+i,b); TD(dt) += s;
                   4125:                     if ( nd_blockmask ) ndl_weight_mask(dt);
                   4126:                 }
                   4127:             curlen *= k+1;
                   4128:             continue;
                   4129:         }
                   4130:         min = MIN(k,l);
                   4131:         if ( mod ) {
                   4132:             ctab = (UINT *)ALLOCA((min+1)*sizeof(UINT));
                   4133:             mkwcm(k,l,mod,ctab);
                   4134:         } else {
                   4135:             ctab_q = (Q *)ALLOCA((min+1)*sizeof(Q));
                   4136:             mkwc(k,l,ctab_q);
                   4137:         }
                   4138:         for ( j = min; j >= 0; j-- ) {
                   4139:             for ( u = 0; u < nd_wpd; u++ ) d[u] = 0;
                   4140:             PUT_EXP(d,i,a-j); PUT_EXP(d,n2+i,b-j);
                   4141:             h = MUL_WEIGHT(a-j,i)+MUL_WEIGHT(b-j,n2+i);
                   4142:             if ( homo ) {
                   4143:                 TD(d) = s;
                   4144:                 PUT_EXP(d,n-1,s-h);
                   4145:             } else TD(d) = h;
                   4146:             if ( nd_blockmask ) ndl_weight_mask(d);
                   4147:             if ( mod ) c = ctab[j];
                   4148:             else q = ctab_q[j];
                   4149:             p = tab+curlen*j;
                   4150:             if ( j == 0 ) {
                   4151:                 for ( u = 0; u < curlen; u++, p++ ) {
                   4152:                     if ( tab[u] ) {
                   4153:                         ndl_addto(DL(tab[u]),d);
                   4154:                         if ( mod ) {
                   4155:                             c0 = CM(tab[u]); DMAR(c0,c,0,mod,c1); CM(tab[u]) = c1;
1.180     noro     4156:                         } else if ( nd_vc )
                   4157:                             mulp(nd_vc,CP(tab[u]),(P)q,&CP(tab[u]));
                   4158:                                                else {
1.157     noro     4159:                             mulq(CQ(tab[u]),q,&q1); CQ(tab[u]) = q1;
                   4160:                         }
                   4161:                     }
                   4162:                 }
                   4163:             } else {
                   4164:                 for ( u = 0; u < curlen; u++, p++ ) {
                   4165:                     if ( tab[u] ) {
                   4166:                         NEWNM(t);
                   4167:                         ndl_add(DL(tab[u]),d,DL(t));
                   4168:                         if ( mod ) {
                   4169:                             c0 = CM(tab[u]); DMAR(c0,c,0,mod,c1); CM(t) = c1;
1.181     noro     4170:                         } else if ( nd_vc )
1.180     noro     4171:                             mulp(nd_vc,CP(tab[u]),(P)q,&CP(t));
                   4172:                                                else
1.157     noro     4173:                             mulq(CQ(tab[u]),q,&CQ(t));
                   4174:                         *p = t;
                   4175:                     }
                   4176:                 }
                   4177:             }
                   4178:         }
                   4179:         curlen *= k+1;
                   4180:     }
                   4181:     FREENM(m);
1.167     noro     4182:     if ( nd_module ) {
1.166     noro     4183:         mpos = MPOS(d1);
1.167     noro     4184:         for ( i = 0; i < tlen; i++ )
                   4185:             if ( tab[i] ) {
                   4186:                 d = DL(tab[i]);
                   4187:                 MPOS(d) = mpos;
                   4188:                 TD(d) = ndl_weight(d);
                   4189:             }
                   4190:     }
1.55      noro     4191: }
                   4192:
1.63      noro     4193: ND ndv_mul_nm_symbolic(NM m0,NDV p)
                   4194: {
1.157     noro     4195:     NM mr,mr0;
                   4196:     NMV m;
                   4197:     UINT *d,*dt,*dm;
                   4198:     int c,n,td,i,c1,c2,len;
                   4199:     Q q;
                   4200:     ND r;
                   4201:
                   4202:     if ( !p ) return 0;
                   4203:     else {
                   4204:         n = NV(p); m = BDY(p);
                   4205:         d = DL(m0);
                   4206:         len = LEN(p);
                   4207:         mr0 = 0;
                   4208:         td = TD(d);
                   4209:         c = CM(m0);
                   4210:         for ( i = 0; i < len; i++, NMV_ADV(m) ) {
                   4211:             NEXTNM(mr0,mr);
                   4212:             CM(mr) = 1;
                   4213:             ndl_add(DL(m),d,DL(mr));
                   4214:         }
                   4215:         NEXT(mr) = 0;
                   4216:         MKND(NV(p),mr0,len,r);
                   4217:         SG(r) = SG(p) + TD(d);
                   4218:         return r;
                   4219:     }
1.63      noro     4220: }
                   4221:
1.55      noro     4222: ND ndv_mul_nm(int mod,NM m0,NDV p)
1.9       noro     4223: {
1.157     noro     4224:     NM mr,mr0;
                   4225:     NMV m;
                   4226:     UINT *d,*dt,*dm;
                   4227:     int c,n,td,i,c1,c2,len;
                   4228:     P q;
                   4229:     ND r;
                   4230:
                   4231:     if ( !p ) return 0;
                   4232:     else if ( do_weyl )
                   4233:         if ( mod == -1 )
                   4234:             error("ndv_mul_nm : not implemented (weyl)");
                   4235:         else
                   4236:             return weyl_ndv_mul_nm(mod,m0,p);
                   4237:     else {
                   4238:         n = NV(p); m = BDY(p);
                   4239:         d = DL(m0);
                   4240:         len = LEN(p);
                   4241:         mr0 = 0;
                   4242:         td = TD(d);
                   4243:         if ( mod == -1 ) {
                   4244:             c = CM(m0);
                   4245:             for ( i = 0; i < len; i++, NMV_ADV(m) ) {
                   4246:                 NEXTNM(mr0,mr);
                   4247:                 CM(mr) = _mulsf(CM(m),c);
                   4248:                 ndl_add(DL(m),d,DL(mr));
                   4249:             }
                   4250:         } else if ( mod ) {
                   4251:             c = CM(m0);
                   4252:             for ( i = 0; i < len; i++, NMV_ADV(m) ) {
                   4253:                 NEXTNM(mr0,mr);
                   4254:                 c1 = CM(m);
                   4255:                 DMAR(c1,c,0,mod,c2);
                   4256:                 CM(mr) = c2;
                   4257:                 ndl_add(DL(m),d,DL(mr));
                   4258:             }
                   4259:         } else {
                   4260:             q = CP(m0);
                   4261:             for ( i = 0; i < len; i++, NMV_ADV(m) ) {
                   4262:                 NEXTNM(mr0,mr);
                   4263:                 mulp(nd_vc,CP(m),q,&CP(mr));
                   4264:                 ndl_add(DL(m),d,DL(mr));
                   4265:             }
                   4266:         }
                   4267:         NEXT(mr) = 0;
                   4268:         MKND(NV(p),mr0,len,r);
                   4269:         SG(r) = SG(p) + TD(d);
                   4270:         return r;
                   4271:     }
1.4       noro     4272: }
                   4273:
1.104     noro     4274: ND nd_quo(int mod,PGeoBucket bucket,NDV d)
1.99      noro     4275: {
1.157     noro     4276:     NM mq0,mq;
                   4277:     NMV tm;
                   4278:     Q q;
                   4279:     int i,nv,sg,c,c1,c2,hindex;
                   4280:     ND p,t,r;
                   4281:     N tnm;
                   4282:
                   4283:     if ( bucket->m < 0 ) return 0;
                   4284:     else {
                   4285:         nv = NV(d);
                   4286:         mq0 = 0;
                   4287:         tm = (NMV)ALLOCA(nmv_adv);
                   4288:         while ( 1 ) {
                   4289:             hindex = mod?head_pbucket(mod,bucket):head_pbucket_q(bucket);
                   4290:             if ( hindex < 0 ) break;
                   4291:             p = bucket->body[hindex];
                   4292:             NEXTNM(mq0,mq);
                   4293:             ndl_sub(HDL(p),HDL(d),DL(mq));
                   4294:             ndl_copy(DL(mq),DL(tm));
                   4295:             if ( mod ) {
                   4296:                 c1 = invm(HCM(d),mod); c2 = HCM(p);
                   4297:                 DMAR(c1,c2,0,mod,c); CM(mq) = c;
                   4298:                 CM(tm) = mod-c;
                   4299:             } else {
                   4300:                 divsn(NM(HCQ(p)),NM(HCQ(d)),&tnm);
                   4301:                 NTOQ(tnm,SGN(HCQ(p))*SGN(HCQ(d)),CQ(mq));
                   4302:                 chsgnq(CQ(mq),&CQ(tm));
                   4303:             }
                   4304:             t = ndv_mul_nmv_trunc(mod,tm,d,HDL(d));
                   4305:             bucket->body[hindex] = nd_remove_head(p);
                   4306:             t = nd_remove_head(t);
                   4307:             add_pbucket(mod,bucket,t);
                   4308:         }
                   4309:         if ( !mq0 )
                   4310:             r = 0;
                   4311:         else {
                   4312:             NEXT(mq) = 0;
                   4313:             for ( i = 0, mq = mq0; mq; mq = NEXT(mq), i++ );
                   4314:             MKND(nv,mq0,i,r);
                   4315:             /* XXX */
                   4316:             SG(r) = HTD(r);
                   4317:         }
                   4318:         return r;
                   4319:     }
1.99      noro     4320: }
                   4321:
1.43      noro     4322: void ndv_realloc(NDV p,int obpe,int oadv,EPOS oepos)
1.11      noro     4323: {
1.157     noro     4324:     NMV m,mr,mr0,t;
                   4325:     int len,i,k;
1.11      noro     4326:
1.157     noro     4327:     if ( !p ) return;
                   4328:     m = BDY(p); len = LEN(p);
                   4329:     mr0 = nmv_adv>oadv?(NMV)REALLOC(BDY(p),len*nmv_adv):BDY(p);
                   4330:     m = (NMV)((char *)mr0+(len-1)*oadv);
                   4331:     mr = (NMV)((char *)mr0+(len-1)*nmv_adv);
                   4332:     t = (NMV)ALLOCA(nmv_adv);
                   4333:     for ( i = 0; i < len; i++, NMV_OPREV(m), NMV_PREV(mr) ) {
                   4334:         CQ(t) = CQ(m);
                   4335:         for ( k = 0; k < nd_wpd; k++ ) DL(t)[k] = 0;
                   4336:         ndl_reconstruct(DL(m),DL(t),obpe,oepos);
                   4337:         CQ(mr) = CQ(t);
                   4338:         ndl_copy(DL(t),DL(mr));
                   4339:     }
                   4340:     BDY(p) = mr0;
1.61      noro     4341: }
                   4342:
                   4343: NDV ndv_dup_realloc(NDV p,int obpe,int oadv,EPOS oepos)
                   4344: {
1.157     noro     4345:     NMV m,mr,mr0;
                   4346:     int len,i;
                   4347:     NDV r;
                   4348:
                   4349:     if ( !p ) return 0;
                   4350:     m = BDY(p); len = LEN(p);
                   4351:     mr0 = mr = (NMV)MALLOC(len*nmv_adv);
                   4352:     for ( i = 0; i < len; i++, NMV_OADV(m), NMV_ADV(mr) ) {
                   4353:         ndl_zero(DL(mr));
                   4354:         ndl_reconstruct(DL(m),DL(mr),obpe,oepos);
                   4355:         CQ(mr) = CQ(m);
                   4356:     }
                   4357:     MKNDV(NV(p),mr0,len,r);
                   4358:     SG(r) = SG(p);
                   4359:     return r;
1.11      noro     4360: }
                   4361:
1.61      noro     4362: /* duplicate p */
                   4363:
                   4364: NDV ndv_dup(int mod,NDV p)
1.3       noro     4365: {
1.157     noro     4366:     NDV d;
                   4367:     NMV t,m,m0;
                   4368:     int i,len;
                   4369:
                   4370:     if ( !p ) return 0;
                   4371:     len = LEN(p);
                   4372:     m0 = m = (NMV)(mod?MALLOC_ATOMIC(len*nmv_adv):MALLOC(len*nmv_adv));
                   4373:     for ( t = BDY(p), i = 0; i < len; i++, NMV_ADV(t), NMV_ADV(m) ) {
                   4374:         ndl_copy(DL(t),DL(m));
                   4375:         CQ(m) = CQ(t);
                   4376:     }
                   4377:     MKNDV(NV(p),m0,len,d);
                   4378:     SG(d) = SG(p);
                   4379:     return d;
1.23      noro     4380: }
                   4381:
1.63      noro     4382: ND nd_dup(ND p)
                   4383: {
1.157     noro     4384:     ND d;
                   4385:     NM t,m,m0;
1.63      noro     4386:
1.157     noro     4387:     if ( !p ) return 0;
                   4388:     for ( m0 = 0, t = BDY(p); t; t = NEXT(t) ) {
                   4389:         NEXTNM(m0,m);
                   4390:         ndl_copy(DL(t),DL(m));
                   4391:         CQ(m) = CQ(t);
                   4392:     }
                   4393:     if ( m0 ) NEXT(m) = 0;
                   4394:     MKND(NV(p),m0,LEN(p),d);
                   4395:     SG(d) = SG(p);
                   4396:     return d;
1.63      noro     4397: }
                   4398:
1.61      noro     4399: /* XXX if p->len == 0 then it represents 0 */
                   4400:
                   4401: void ndv_mod(int mod,NDV p)
                   4402: {
1.157     noro     4403:     NMV t,d;
                   4404:     int r,s,u;
                   4405:     int i,len,dlen;
                   4406:     P cp;
                   4407:     Q c;
                   4408:     Obj gfs;
                   4409:
                   4410:     if ( !p ) return;
                   4411:     len = LEN(p);
                   4412:     dlen = 0;
                   4413:     if ( mod == -1 )
                   4414:         for ( t = d = BDY(p), i = 0; i < len; i++, NMV_ADV(t) ) {
                   4415:             simp_ff((Obj)CP(t),&gfs);
                   4416:             r = FTOIF(CONT((GFS)gfs));
                   4417:             CM(d) = r;
                   4418:             ndl_copy(DL(t),DL(d));
                   4419:             NMV_ADV(d);
                   4420:             dlen++;
                   4421:         }
                   4422:     else
                   4423:         for ( t = d = BDY(p), i = 0; i < len; i++, NMV_ADV(t) ) {
                   4424:             if ( nd_vc ) {
                   4425:                 nd_subst_vector(nd_vc,CP(t),nd_subst,&cp);
                   4426:                 c = (Q)cp;
                   4427:             } else
                   4428:                 c = CQ(t);
                   4429:             r = rem(NM(c),mod);
                   4430:             if ( r ) {
                   4431:                 if ( SGN(c) < 0 )
                   4432:                     r = mod-r;
                   4433:                 if ( DN(c) ) {
                   4434:                     s = rem(DN(c),mod);
                   4435:                     if ( !s )
                   4436:                         error("ndv_mod : division by 0");
                   4437:                     s = invm(s,mod);
                   4438:                     DMAR(r,s,0,mod,u); r = u;
                   4439:                 }
                   4440:                 CM(d) = r;
                   4441:                 ndl_copy(DL(t),DL(d));
                   4442:                 NMV_ADV(d);
                   4443:                 dlen++;
                   4444:             }
                   4445:         }
                   4446:     LEN(p) = dlen;
1.61      noro     4447: }
                   4448:
                   4449: NDV ptondv(VL vl,VL dvl,P p)
                   4450: {
1.157     noro     4451:     ND nd;
                   4452:
                   4453:     nd = ptond(vl,dvl,p);
                   4454:     return ndtondv(0,nd);
                   4455: }
1.61      noro     4456:
1.157     noro     4457: void pltozpl(LIST l,Q *cont,LIST *pp)
                   4458: {
                   4459:     NODE nd,nd1;
                   4460:     int n;
                   4461:     P *pl;
                   4462:     Q *cl;
                   4463:     int i;
                   4464:     P dmy;
                   4465:     Q dvr;
                   4466:     LIST r;
                   4467:
                   4468:     nd = BDY(l); n = length(nd);
                   4469:     pl = (P *)ALLOCA(n*sizeof(P));
                   4470:     cl = (Q *)ALLOCA(n*sizeof(P));
                   4471:     for ( i = 0; i < n; i++, nd = NEXT(nd) )
                   4472:         ptozp((P)BDY(nd),1,&cl[i],&dmy);
                   4473:     qltozl(cl,n,&dvr);
                   4474:     nd = BDY(l);
                   4475:     for ( i = 0; i < n; i++, nd = NEXT(nd) ) {
                   4476:         divsp(CO,(P)BDY(nd),(P)dvr,&pl[i]);
                   4477:     }
                   4478:     nd = 0;
                   4479:     for ( i = n-1; i >= 0; i-- ) {
                   4480:         MKNODE(nd1,pl[i],nd); nd = nd1;
                   4481:     }
                   4482:     MKLIST(r,nd);
                   4483:     *pp = r;
                   4484: }
                   4485:
                   4486: /* (a1,a2,...,an) -> a1*e(1)+...+an*e(n) */
                   4487:
                   4488: NDV pltondv(VL vl,VL dvl,LIST p)
                   4489: {
                   4490:     int i;
                   4491:     NODE t;
                   4492:     ND r,ri;
                   4493:     NM m;
                   4494:
                   4495:     if ( !nd_module ) error("pltond : module order must be set");
                   4496:     r = 0;
                   4497:     for ( i = 1, t = BDY(p); t; t = NEXT(t), i++ ) {
                   4498:         ri = ptond(vl,dvl,(P)BDY(t));
1.163     noro     4499:         if ( ri )
                   4500:             for ( m = BDY(ri); m; m = NEXT(m) ) {
1.167     noro     4501:                 MPOS(DL(m)) = i;
                   4502:                 TD(DL(m)) = ndl_weight(DL(m));
1.163     noro     4503:                 if ( nd_blockmask ) ndl_weight_mask(DL(m));
                   4504:             }
1.157     noro     4505:         r = nd_add(0,r,ri);
                   4506:     }
                   4507:     return ndtondv(0,r);
1.61      noro     4508: }
                   4509:
                   4510: ND ptond(VL vl,VL dvl,P p)
1.23      noro     4511: {
1.157     noro     4512:     int n,i,j,k,e;
                   4513:     VL tvl;
                   4514:     V v;
                   4515:     DCP dc;
                   4516:     DCP *w;
                   4517:     ND r,s,t,u;
                   4518:     P x;
                   4519:     int c;
                   4520:     UINT *d;
                   4521:     NM m,m0;
                   4522:
                   4523:     if ( !p )
                   4524:         return 0;
                   4525:     else if ( NUM(p) ) {
                   4526:         NEWNM(m);
                   4527:         ndl_zero(DL(m));
                   4528:         CQ(m) = (Q)p;
                   4529:         NEXT(m) = 0;
                   4530:         MKND(nd_nvar,m,1,r);
                   4531:         SG(r) = 0;
                   4532:         return r;
                   4533:     } else {
                   4534:         for ( dc = DC(p), k = 0; dc; dc = NEXT(dc), k++ );
                   4535:         w = (DCP *)ALLOCA(k*sizeof(DCP));
                   4536:         for ( dc = DC(p), j = 0; j < k; dc = NEXT(dc), j++ ) w[j] = dc;
                   4537:         for ( i = 0, tvl = dvl, v = VR(p);
                   4538:             tvl && tvl->v != v; tvl = NEXT(tvl), i++ );
                   4539:         if ( !tvl ) {
                   4540:             for ( j = k-1, s = 0, MKV(v,x); j >= 0; j-- ) {
                   4541:                 t = ptond(vl,dvl,COEF(w[j]));
                   4542:                 pwrp(vl,x,DEG(w[j]),&p);
                   4543:                 nd_mul_c_p(CO,t,p); s = nd_add(0,s,t);
                   4544:             }
                   4545:             return s;
                   4546:         } else {
                   4547:             NEWNM(m0); d = DL(m0);
                   4548:             for ( j = k-1, s = 0; j >= 0; j-- ) {
                   4549:                 ndl_zero(d); e = QTOS(DEG(w[j])); PUT_EXP(d,i,e);
                   4550:                 TD(d) = MUL_WEIGHT(e,i);
                   4551:                 if ( nd_blockmask) ndl_weight_mask(d);
                   4552:                 if ( nd_module ) MPOS(d) = 0;
                   4553:                 t = ptond(vl,dvl,COEF(w[j]));
                   4554:                 for ( m = BDY(t); m; m = NEXT(m) )
                   4555:                     ndl_addto(DL(m),d);
                   4556:                 SG(t) += TD(d);
                   4557:                 s = nd_add(0,s,t);
                   4558:             }
                   4559:             FREENM(m0);
                   4560:             return s;
                   4561:         }
                   4562:     }
1.61      noro     4563: }
                   4564:
                   4565: P ndvtop(int mod,VL vl,VL dvl,NDV p)
                   4566: {
1.157     noro     4567:     VL tvl;
                   4568:     int len,n,j,i,e;
                   4569:     NMV m;
                   4570:     Q q;
                   4571:     P c;
                   4572:     UINT *d;
                   4573:     P s,r,u,t,w;
                   4574:     GFS gfs;
                   4575:
                   4576:     if ( !p ) return 0;
                   4577:     else {
                   4578:         len = LEN(p);
                   4579:         n = NV(p);
                   4580:         m = (NMV)(((char *)BDY(p))+nmv_adv*(len-1));
                   4581:         for ( j = len-1, s = 0; j >= 0; j--, NMV_PREV(m) ) {
                   4582:             if ( mod == -1 ) {
                   4583:                 e = IFTOF(CM(m)); MKGFS(e,gfs); c = (P)gfs;
                   4584:             } else if ( mod ) {
                   4585:                 STOQ(CM(m),q); c = (P)q;
                   4586:             } else
                   4587:                 c = CP(m);
                   4588:             d = DL(m);
                   4589:             for ( i = 0, t = c, tvl = dvl; i < n; tvl = NEXT(tvl), i++ ) {
                   4590:                 MKV(tvl->v,r); e = GET_EXP(d,i); STOQ(e,q);
                   4591:                 pwrp(vl,r,q,&u); mulp(vl,t,u,&w); t = w;
                   4592:             }
                   4593:             addp(vl,s,t,&u); s = u;
                   4594:         }
                   4595:         return s;
                   4596:     }
                   4597: }
                   4598:
                   4599: LIST ndvtopl(int mod,VL vl,VL dvl,NDV p,int rank)
                   4600: {
                   4601:     VL tvl;
                   4602:     int len,n,j,i,e;
                   4603:     NMV m;
                   4604:     Q q;
                   4605:     P c;
                   4606:     UINT *d;
                   4607:     P s,r,u,t,w;
                   4608:     GFS gfs;
                   4609:     P *a;
                   4610:     LIST l;
                   4611:     NODE nd,nd1;
                   4612:
                   4613:     if ( !p ) return 0;
                   4614:     else {
                   4615:         a = (P *)ALLOCA((rank+1)*sizeof(P));
                   4616:         for ( i = 0; i <= rank; i++ ) a[i] = 0;
                   4617:         len = LEN(p);
                   4618:         n = NV(p);
                   4619:         m = (NMV)(((char *)BDY(p))+nmv_adv*(len-1));
                   4620:         for ( j = len-1; j >= 0; j--, NMV_PREV(m) ) {
                   4621:             if ( mod == -1 ) {
                   4622:                 e = IFTOF(CM(m)); MKGFS(e,gfs); c = (P)gfs;
                   4623:             } else if ( mod ) {
                   4624:                 STOQ(CM(m),q); c = (P)q;
                   4625:             } else
                   4626:                 c = CP(m);
                   4627:             d = DL(m);
                   4628:             for ( i = 0, t = c, tvl = dvl; i < n; tvl = NEXT(tvl), i++ ) {
                   4629:                 MKV(tvl->v,r); e = GET_EXP(d,i); STOQ(e,q);
                   4630:                 pwrp(vl,r,q,&u); mulp(vl,t,u,&w); t = w;
                   4631:             }
                   4632:             addp(vl,a[MPOS(d)],t,&u); a[MPOS(d)] = u;
                   4633:         }
                   4634:         nd = 0;
                   4635:         for ( i = rank; i > 0; i-- ) {
                   4636:             MKNODE(nd1,a[i],nd); nd = nd1;
                   4637:         }
                   4638:         MKLIST(l,nd);
                   4639:         return l;
                   4640:     }
1.3       noro     4641: }
                   4642:
1.61      noro     4643: NDV ndtondv(int mod,ND p)
1.11      noro     4644: {
1.157     noro     4645:     NDV d;
                   4646:     NMV m,m0;
                   4647:     NM t;
                   4648:     int i,len;
                   4649:
                   4650:     if ( !p ) return 0;
                   4651:     len = LEN(p);
                   4652:     if ( mod )
                   4653:         m0 = m = (NMV)GC_malloc_atomic_ignore_off_page(len*nmv_adv);
                   4654:     else
                   4655:         m0 = m = MALLOC(len*nmv_adv);
1.103     noro     4656: #if 0
1.157     noro     4657:     ndv_alloc += nmv_adv*len;
1.103     noro     4658: #endif
1.157     noro     4659:     for ( t = BDY(p), i = 0; t; t = NEXT(t), i++, NMV_ADV(m) ) {
                   4660:         ndl_copy(DL(t),DL(m));
                   4661:         CQ(m) = CQ(t);
                   4662:     }
                   4663:     MKNDV(NV(p),m0,len,d);
                   4664:     SG(d) = SG(p);
                   4665:     return d;
1.11      noro     4666: }
                   4667:
1.61      noro     4668: ND ndvtond(int mod,NDV p)
1.11      noro     4669: {
1.157     noro     4670:     ND d;
                   4671:     NM m,m0;
                   4672:     NMV t;
                   4673:     int i,len;
                   4674:
                   4675:     if ( !p ) return 0;
                   4676:     m0 = 0;
                   4677:     len = p->len;
                   4678:     for ( t = BDY(p), i = 0; i < len; NMV_ADV(t), i++ ) {
                   4679:         NEXTNM(m0,m);
                   4680:         ndl_copy(DL(t),DL(m));
                   4681:         CQ(m) = CQ(t);
                   4682:     }
                   4683:     NEXT(m) = 0;
                   4684:     MKND(NV(p),m0,len,d);
                   4685:     SG(d) = SG(p);
                   4686:     return d;
1.11      noro     4687: }
                   4688:
1.3       noro     4689: void ndv_print(NDV p)
                   4690: {
1.157     noro     4691:     NMV m;
                   4692:     int i,len;
1.3       noro     4693:
1.157     noro     4694:     if ( !p ) printf("0\n");
                   4695:     else {
                   4696:         len = LEN(p);
                   4697:         for ( m = BDY(p), i = 0; i < len; i++, NMV_ADV(m) ) {
                   4698:             if ( CM(m) & 0x80000000 ) printf("+@_%d*",IFTOF(CM(m)));
                   4699:             else printf("+%d*",CM(m));
                   4700:             ndl_print(DL(m));
                   4701:         }
                   4702:         printf("\n");
                   4703:     }
1.16      noro     4704: }
                   4705:
1.113     noro     4706: void ndv_print_q(NDV p)
1.16      noro     4707: {
1.157     noro     4708:     NMV m;
                   4709:     int i,len;
1.16      noro     4710:
1.157     noro     4711:     if ( !p ) printf("0\n");
                   4712:     else {
                   4713:         len = LEN(p);
                   4714:         for ( m = BDY(p), i = 0; i < len; i++, NMV_ADV(m) ) {
                   4715:             printf("+");
                   4716:             printexpr(CO,(Obj)CQ(m));
                   4717:             printf("*");
                   4718:             ndl_print(DL(m));
                   4719:         }
                   4720:         printf("\n");
                   4721:     }
1.25      noro     4722: }
                   4723:
1.167     noro     4724: NODE ndv_reducebase(NODE x,int *perm)
1.27      noro     4725: {
1.157     noro     4726:     int len,i,j;
1.167     noro     4727:     NDVI w;
1.157     noro     4728:     NODE t,t0;
                   4729:
                   4730:     len = length(x);
1.167     noro     4731:     w = (NDVI)ALLOCA(len*sizeof(struct oNDVI));
                   4732:     for ( i = 0, t = x; i < len; i++, t = NEXT(t) ) {
                   4733:         w[i].p = BDY(t); w[i].i = perm[i];
                   4734:     }
1.157     noro     4735:     for ( i = 0; i < len; i++ ) {
                   4736:         for ( j = 0; j < i; j++ ) {
1.167     noro     4737:             if ( w[i].p && w[j].p )
                   4738:                 if ( ndl_reducible(HDL(w[i].p),HDL(w[j].p)) ) w[i].p = 0;
                   4739:                 else if ( ndl_reducible(HDL(w[j].p),HDL(w[i].p)) ) w[j].p = 0;
1.157     noro     4740:         }
                   4741:     }
1.167     noro     4742:     for ( i = j = 0, t0 = 0; i < len; i++ ) {
                   4743:         if ( w[i].p ) {
                   4744:             NEXTNODE(t0,t); BDY(t) = (pointer)w[i].p;
                   4745:             perm[j++] = w[i].i;
                   4746:         }
1.157     noro     4747:     }
                   4748:     NEXT(t) = 0; x = t0;
                   4749:     return x;
1.11      noro     4750: }
1.32      noro     4751:
1.43      noro     4752: /* XXX incomplete */
                   4753:
1.32      noro     4754: void nd_init_ord(struct order_spec *ord)
                   4755: {
1.157     noro     4756:     nd_module = (ord->id >= 256);
                   4757:     switch ( ord->id ) {
                   4758:         case 0:
                   4759:             switch ( ord->ord.simple ) {
                   4760:                 case 0:
                   4761:                     nd_dcomp = 1;
                   4762:                     nd_isrlex = 1;
                   4763:                     break;
                   4764:                 case 1:
                   4765:                     nd_dcomp = 1;
                   4766:                     nd_isrlex = 0;
                   4767:                     break;
                   4768:                 case 2:
                   4769:                     nd_dcomp = 0;
                   4770:                     nd_isrlex = 0;
                   4771:                     ndl_compare_function = ndl_lex_compare;
                   4772:                     break;
                   4773:                 case 11:
                   4774:                     /* XXX */
                   4775:                     nd_dcomp = 0;
                   4776:                     nd_isrlex = 1;
                   4777:                     ndl_compare_function = ndl_ww_lex_compare;
                   4778:                     break;
                   4779:                 default:
                   4780:                     error("nd_gr : unsupported order");
                   4781:             }
                   4782:             break;
                   4783:         case 1:
                   4784:             /* block order */
                   4785:             /* XXX */
                   4786:             nd_dcomp = -1;
                   4787:             nd_isrlex = 0;
                   4788:             ndl_compare_function = ndl_block_compare;
                   4789:             break;
                   4790:         case 2:
                   4791:             /* matrix order */
                   4792:             /* XXX */
                   4793:             nd_dcomp = -1;
                   4794:             nd_isrlex = 0;
                   4795:             nd_matrix_len = ord->ord.matrix.row;
                   4796:             nd_matrix = ord->ord.matrix.matrix;
                   4797:             ndl_compare_function = ndl_matrix_compare;
                   4798:             break;
                   4799:         case 3:
                   4800:             /* composite order */
                   4801:             nd_dcomp = -1;
                   4802:             nd_isrlex = 0;
                   4803:             nd_worb_len = ord->ord.composite.length;
                   4804:             nd_worb = ord->ord.composite.w_or_b;
                   4805:             ndl_compare_function = ndl_composite_compare;
                   4806:             break;
                   4807:
                   4808:         /* module order */
                   4809:         case 256:
1.160     noro     4810:             nd_ispot = ord->ispot;
1.174     noro     4811:             nd_pot_nelim = ord->pot_nelim;
1.157     noro     4812:             nd_dcomp = -1;
                   4813:             switch ( ord->ord.simple ) {
                   4814:                 case 0:
1.167     noro     4815:                     nd_isrlex = 1;
1.157     noro     4816:                     ndl_compare_function = ndl_module_grlex_compare;
                   4817:                     break;
                   4818:                 case 1:
1.167     noro     4819:                     nd_isrlex = 0;
1.157     noro     4820:                     ndl_compare_function = ndl_module_glex_compare;
                   4821:                     break;
                   4822:                 case 2:
1.167     noro     4823:                     nd_isrlex = 0;
1.157     noro     4824:                     ndl_compare_function = ndl_module_lex_compare;
                   4825:                     break;
                   4826:                 default:
                   4827:                     error("nd_gr : unsupported order");
                   4828:             }
                   4829:             break;
                   4830:         case 257:
                   4831:             /* block order */
1.174     noro     4832:             nd_ispot = ord->ispot;
                   4833:             nd_pot_nelim = ord->pot_nelim;
                   4834:             nd_dcomp = -1;
                   4835:             nd_isrlex = 0;
1.157     noro     4836:             ndl_compare_function = ndl_module_block_compare;
                   4837:             break;
                   4838:         case 258:
                   4839:             /* matrix order */
1.174     noro     4840:             nd_ispot = ord->ispot;
                   4841:             nd_pot_nelim = ord->pot_nelim;
                   4842:             nd_dcomp = -1;
                   4843:             nd_isrlex = 0;
1.157     noro     4844:             nd_matrix_len = ord->ord.matrix.row;
                   4845:             nd_matrix = ord->ord.matrix.matrix;
                   4846:             ndl_compare_function = ndl_module_matrix_compare;
                   4847:             break;
                   4848:         case 259:
                   4849:             /* composite order */
1.174     noro     4850:             nd_ispot = ord->ispot;
                   4851:             nd_pot_nelim = ord->pot_nelim;
                   4852:             nd_dcomp = -1;
                   4853:             nd_isrlex = 0;
1.157     noro     4854:             nd_worb_len = ord->ord.composite.length;
                   4855:             nd_worb = ord->ord.composite.w_or_b;
                   4856:             ndl_compare_function = ndl_module_composite_compare;
                   4857:             break;
                   4858:     }
                   4859:     nd_ord = ord;
1.32      noro     4860: }
                   4861:
1.43      noro     4862: BlockMask nd_create_blockmask(struct order_spec *ord)
                   4863: {
1.157     noro     4864:     int n,i,j,s,l;
                   4865:     UINT *t;
                   4866:     BlockMask bm;
                   4867:
                   4868:     /* we only create mask table for block order */
1.164     noro     4869:     if ( ord->id != 1 && ord->id != 257 )
1.157     noro     4870:         return 0;
                   4871:     n = ord->ord.block.length;
                   4872:     bm = (BlockMask)MALLOC(sizeof(struct oBlockMask));
                   4873:     bm->n = n;
                   4874:     bm->order_pair = ord->ord.block.order_pair;
                   4875:     bm->mask = (UINT **)MALLOC(n*sizeof(UINT *));
                   4876:     for ( i = 0, s = 0; i < n; i++ ) {
                   4877:         bm->mask[i] = t = (UINT *)MALLOC_ATOMIC(nd_wpd*sizeof(UINT));
                   4878:         for ( j = 0; j < nd_wpd; j++ ) t[j] = 0;
                   4879:         l = bm->order_pair[i].length;
                   4880:         for ( j = 0; j < l; j++, s++ ) PUT_EXP(t,s,nd_mask0);
                   4881:     }
                   4882:     return bm;
1.57      noro     4883: }
                   4884:
                   4885: EPOS nd_create_epos(struct order_spec *ord)
                   4886: {
1.157     noro     4887:     int i,j,l,s,ord_l,ord_o;
                   4888:     EPOS epos;
                   4889:     struct order_pair *op;
                   4890:
                   4891:     epos = (EPOS)MALLOC_ATOMIC(nd_nvar*sizeof(struct oEPOS));
                   4892:     switch ( ord->id ) {
1.164     noro     4893:         case 0: case 256:
1.157     noro     4894:             if ( nd_isrlex ) {
                   4895:                 for ( i = 0; i < nd_nvar; i++ ) {
                   4896:                     epos[i].i = nd_exporigin + (nd_nvar-1-i)/nd_epw;
                   4897:                     epos[i].s = (nd_epw-((nd_nvar-1-i)%nd_epw)-1)*nd_bpe;
                   4898:                 }
                   4899:             } else {
                   4900:                 for ( i = 0; i < nd_nvar; i++ ) {
                   4901:                     epos[i].i = nd_exporigin + i/nd_epw;
                   4902:                     epos[i].s = (nd_epw-(i%nd_epw)-1)*nd_bpe;
                   4903:                 }
                   4904:             }
                   4905:             break;
1.164     noro     4906:         case 1: case 257:
1.157     noro     4907:             /* block order */
                   4908:             l = ord->ord.block.length;
                   4909:             op = ord->ord.block.order_pair;
                   4910:             for ( j = 0, s = 0; j < l; j++ ) {
                   4911:                 ord_o = op[j].order;
                   4912:                 ord_l = op[j].length;
                   4913:                 if ( !ord_o )
                   4914:                     for ( i = 0; i < ord_l; i++ ) {
                   4915:                         epos[s+i].i = nd_exporigin + (s+ord_l-i-1)/nd_epw;
                   4916:                         epos[s+i].s = (nd_epw-((s+ord_l-i-1)%nd_epw)-1)*nd_bpe;
                   4917:                     }
                   4918:                 else
                   4919:                     for ( i = 0; i < ord_l; i++ ) {
                   4920:                         epos[s+i].i = nd_exporigin + (s+i)/nd_epw;
                   4921:                         epos[s+i].s = (nd_epw-((s+i)%nd_epw)-1)*nd_bpe;
                   4922:                     }
                   4923:                 s += ord_l;
                   4924:             }
                   4925:             break;
                   4926:         case 2:
                   4927:             /* matrix order */
                   4928:         case 3:
                   4929:             /* composite order */
1.167     noro     4930:         default:
1.157     noro     4931:             for ( i = 0; i < nd_nvar; i++ ) {
                   4932:                 epos[i].i = nd_exporigin + i/nd_epw;
                   4933:                 epos[i].s = (nd_epw-(i%nd_epw)-1)*nd_bpe;
                   4934:             }
                   4935:             break;
                   4936:     }
                   4937:     return epos;
1.43      noro     4938: }
1.59      noro     4939:
                   4940: /* external interface */
                   4941:
1.191     noro     4942: void nd_nf_p(Obj f,LIST g,LIST v,int m,struct order_spec *ord,Obj *rp)
1.59      noro     4943: {
1.157     noro     4944:     NODE t,in0,in;
1.191     noro     4945:     ND ndf,nf;
                   4946:     NDV ndvf;
1.157     noro     4947:     VL vv,tv;
1.191     noro     4948:     int stat,nvar,max,mrank;
1.157     noro     4949:     union oNDC dn;
                   4950:     Q cont;
                   4951:     P pp;
1.191     noro     4952:     LIST ppl;
1.157     noro     4953:
                   4954:     if ( !f ) {
                   4955:         *rp = 0;
                   4956:         return;
                   4957:     }
                   4958:     pltovl(v,&vv);
                   4959:     for ( nvar = 0, tv = vv; tv; tv = NEXT(tv), nvar++ );
                   4960:
1.191     noro     4961:     /* max=65536 implies nd_bpe=32 */
                   4962:     max = 65536;
1.157     noro     4963:
1.191     noro     4964:        nd_module = 0;
                   4965:        /* nd_module will be set if ord is a module ordering */
1.157     noro     4966:     nd_init_ord(ord);
                   4967:     nd_setup_parameters(nvar,max);
1.191     noro     4968:     if ( nd_module && OID(f) != O_LIST )
                   4969:         error("nd_nf_p : the first argument must be a list");
                   4970:        if ( nd_module ) mrank = length(BDY((LIST)f));
1.157     noro     4971:     /* conversion to ndv */
                   4972:     for ( in0 = 0, t = BDY(g); t; t = NEXT(t) ) {
                   4973:         NEXTNODE(in0,in);
1.191     noro     4974:         if ( nd_module ) {
                   4975:           if ( !BDY(t) || OID(BDY(t)) != O_LIST
                   4976:                || length(BDY((LIST)BDY(t))) != mrank )
                   4977:               error("nd_nf_p : inconsistent basis element");
                   4978:           if ( !m ) pltozpl((LIST)BDY(t),&cont,&ppl);
                   4979:           else ppl = (LIST)BDY(t);
                   4980:           BDY(in) = (pointer)pltondv(CO,vv,ppl);
                   4981:         } else {
                   4982:           if ( !m ) ptozp((P)BDY(t),1,&cont,&pp);
                   4983:           else pp = (P)BDY(t);
                   4984:           BDY(in) = (pointer)ptondv(CO,vv,pp);
                   4985:         }
1.157     noro     4986:         if ( m ) ndv_mod(m,(NDV)BDY(in));
                   4987:     }
1.191     noro     4988:     if ( in0 ) NEXT(in) = 0;
                   4989:
                   4990:     if ( nd_module ) ndvf = pltondv(CO,vv,(LIST)f);
                   4991:     else ndvf = ptondv(CO,vv,(P)f);
                   4992:     if ( m ) ndv_mod(m,ndvf);
                   4993:     ndf = (pointer)ndvtond(m,ndvf);
1.157     noro     4994:
                   4995:     /* dont sort, dont removecont */
                   4996:     ndv_setup(m,0,in0,1,1);
                   4997:     nd_scale=2;
1.191     noro     4998:     stat = nd_nf(m,0,ndf,nd_ps,1,0,&nf);
                   4999:     if ( !stat )
                   5000:         error("nd_nf_p : exponent too large");
                   5001:     if ( nd_module ) *rp = (Obj)ndvtopl(m,CO,vv,ndtondv(m,nf),mrank);
                   5002:     else *rp = (Obj)ndvtop(m,CO,vv,ndtondv(m,nf));
1.63      noro     5003: }
                   5004:
                   5005: int nd_to_vect(int mod,UINT *s0,int n,ND d,UINT *r)
                   5006: {
1.157     noro     5007:     NM m;
                   5008:     UINT *t,*s;
                   5009:     int i;
                   5010:
                   5011:     for ( i = 0; i < n; i++ ) r[i] = 0;
                   5012:     for ( i = 0, s = s0, m = BDY(d); m; m = NEXT(m) ) {
                   5013:         t = DL(m);
                   5014:         for ( ; !ndl_equal(t,s); s += nd_wpd, i++ );
                   5015:         r[i] = CM(m);
                   5016:     }
                   5017:     for ( i = 0; !r[i]; i++ );
                   5018:     return i;
1.63      noro     5019: }
                   5020:
1.113     noro     5021: int nd_to_vect_q(UINT *s0,int n,ND d,Q *r)
1.74      noro     5022: {
1.157     noro     5023:     NM m;
                   5024:     UINT *t,*s;
                   5025:     int i;
                   5026:
                   5027:     for ( i = 0; i < n; i++ ) r[i] = 0;
                   5028:     for ( i = 0, s = s0, m = BDY(d); m; m = NEXT(m) ) {
                   5029:         t = DL(m);
                   5030:         for ( ; !ndl_equal(t,s); s += nd_wpd, i++ );
                   5031:         r[i] = CQ(m);
                   5032:     }
                   5033:     for ( i = 0; !r[i]; i++ );
                   5034:     return i;
1.74      noro     5035: }
                   5036:
1.129     noro     5037: Q *nm_ind_pair_to_vect(int mod,UINT *s0,int n,NM_ind_pair pair)
                   5038: {
1.157     noro     5039:     NM m;
                   5040:     NMV mr;
                   5041:     UINT *d,*t,*s;
                   5042:     NDV p;
                   5043:     int i,j,len;
                   5044:     Q *r;
                   5045:
                   5046:     m = pair->mul;
                   5047:     d = DL(m);
                   5048:     p = nd_ps[pair->index];
                   5049:     len = LEN(p);
                   5050:     r = (Q *)CALLOC(n,sizeof(Q));
                   5051:     t = (UINT *)ALLOCA(nd_wpd*sizeof(UINT));
                   5052:     for ( i = j = 0, s = s0, mr = BDY(p); j < len; j++, NMV_ADV(mr) ) {
                   5053:         ndl_add(d,DL(mr),t);
                   5054:         for ( ; !ndl_equal(t,s); s += nd_wpd, i++ );
                   5055:         r[i] = CQ(mr);
                   5056:     }
                   5057:     return r;
1.129     noro     5058: }
                   5059:
1.67      noro     5060: IndArray nm_ind_pair_to_vect_compress(int mod,UINT *s0,int n,NM_ind_pair pair)
1.64      noro     5061: {
1.157     noro     5062:     NM m;
                   5063:     NMV mr;
                   5064:     UINT *d,*t,*s;
                   5065:     NDV p;
                   5066:     unsigned char *ivc;
                   5067:     unsigned short *ivs;
                   5068:     UINT *v,*ivi,*s0v;
                   5069:     int i,j,len,prev,diff,cdiff;
                   5070:     IndArray r;
                   5071:
                   5072:     m = pair->mul;
                   5073:     d = DL(m);
                   5074:     p = nd_ps[pair->index];
                   5075:     len = LEN(p);
                   5076:     t = (UINT *)ALLOCA(nd_wpd*sizeof(UINT));
                   5077:     v = (unsigned int *)ALLOCA(len*sizeof(unsigned int));
                   5078:     for ( i = j = 0, s = s0, mr = BDY(p); j < len; j++, NMV_ADV(mr) ) {
                   5079:         ndl_add(d,DL(mr),t);
                   5080:         for ( ; !ndl_equal(t,s); s += nd_wpd, i++ );
                   5081:         v[j] = i;
                   5082:     }
                   5083:     r = (IndArray)MALLOC(sizeof(struct oIndArray));
                   5084:     r->head = v[0];
                   5085:     diff = 0;
                   5086:     for ( i = 1; i < len; i++ ) {
                   5087:         cdiff = v[i]-v[i-1]; diff = MAX(cdiff,diff);
                   5088:     }
                   5089:     if ( diff < 256 ) {
                   5090:         r->width = 1;
                   5091:         ivc = (unsigned char *)MALLOC_ATOMIC(len*sizeof(unsigned char));
                   5092:         r->index.c = ivc;
                   5093:         for ( i = 1, ivc[0] = 0; i < len; i++ ) ivc[i] = v[i]-v[i-1];
                   5094:     } else if ( diff < 65536 ) {
                   5095:         r->width = 2;
                   5096:         ivs = (unsigned short *)MALLOC_ATOMIC(len*sizeof(unsigned short));
                   5097:         r->index.s = ivs;
                   5098:         for ( i = 1, ivs[0] = 0; i < len; i++ ) ivs[i] = v[i]-v[i-1];
                   5099:     } else {
                   5100:         r->width = 4;
                   5101:         ivi = (unsigned int *)MALLOC_ATOMIC(len*sizeof(unsigned int));
                   5102:         r->index.i = ivi;
                   5103:         for ( i = 1, ivi[0] = 0; i < len; i++ ) ivi[i] = v[i]-v[i-1];
                   5104:     }
                   5105:     return r;
1.64      noro     5106: }
                   5107:
1.135     noro     5108: int compress_array(Q *svect,Q *cvect,int n)
                   5109: {
1.157     noro     5110:     int i,j;
1.135     noro     5111:
1.157     noro     5112:     for ( i = j = 0; i < n; i++ )
                   5113:         if ( svect[i] ) cvect[j++] = svect[i];
                   5114:     return j;
1.135     noro     5115: }
                   5116:
                   5117: void expand_array(Q *svect,Q *cvect,int n)
                   5118: {
1.157     noro     5119:     int i,j;
1.135     noro     5120:
1.157     noro     5121:     for ( i = j = 0; j < n;  i++  )
                   5122:         if ( svect[i] ) svect[i] = cvect[j++];
1.135     noro     5123: }
                   5124:
1.133     noro     5125: int ndv_reduce_vect_q(Q *svect,int trace,int col,IndArray *imat,NM_ind_pair *rp0,int nred)
1.107     noro     5126: {
1.157     noro     5127:     int i,j,k,len,pos,prev,nz;
                   5128:     Q cs,mcs,c1,c2,cr,gcd,t;
                   5129:     IndArray ivect;
                   5130:     unsigned char *ivc;
                   5131:     unsigned short *ivs;
                   5132:     unsigned int *ivi;
                   5133:     NDV redv;
                   5134:     NMV mr;
                   5135:     NODE rp;
                   5136:     int maxrs;
                   5137:     double hmag;
                   5138:     Q *cvect;
                   5139:
                   5140:     maxrs = 0;
                   5141:     for ( i = 0; i < col && !svect[i]; i++ );
                   5142:     if ( i == col ) return maxrs;
                   5143:     hmag = p_mag((P)svect[i])*nd_scale;
                   5144:     cvect = (Q *)ALLOCA(col*sizeof(Q));
                   5145:     for ( i = 0; i < nred; i++ ) {
                   5146:         ivect = imat[i];
                   5147:         k = ivect->head;
                   5148:         if ( svect[k] ) {
                   5149:             maxrs = MAX(maxrs,rp0[i]->sugar);
                   5150:             redv = trace?nd_ps_trace[rp0[i]->index]:nd_ps[rp0[i]->index];
                   5151:             len = LEN(redv); mr = BDY(redv);
                   5152:             igcd_cofactor(svect[k],CQ(mr),&gcd,&cs,&cr);
                   5153:             chsgnq(cs,&mcs);
                   5154:             if ( !UNIQ(cr) ) {
                   5155:                 for ( j = 0; j < col; j++ ) {
                   5156:                     mulq(svect[j],cr,&c1); svect[j] = c1;
                   5157:                 }
                   5158:             }
                   5159:             svect[k] = 0; prev = k;
                   5160:             switch ( ivect->width ) {
                   5161:                 case 1:
                   5162:                     ivc = ivect->index.c;
                   5163:                     for ( j = 1, NMV_ADV(mr); j < len; j++, NMV_ADV(mr) ) {
                   5164:                         pos = prev+ivc[j]; prev = pos;
                   5165:                         mulq(CQ(mr),mcs,&c2); addq(svect[pos],c2,&t); svect[pos] = t;
                   5166:                     }
                   5167:                     break;
                   5168:                 case 2:
                   5169:                     ivs = ivect->index.s;
                   5170:                     for ( j = 1, NMV_ADV(mr); j < len; j++, NMV_ADV(mr) ) {
                   5171:                         pos = prev+ivs[j]; prev = pos;
                   5172:                         mulq(CQ(mr),mcs,&c2); addq(svect[pos],c2,&t); svect[pos] = t;
                   5173:                     }
                   5174:                     break;
                   5175:                 case 4:
                   5176:                     ivi = ivect->index.i;
                   5177:                     for ( j = 1, NMV_ADV(mr); j < len; j++, NMV_ADV(mr) ) {
                   5178:                         pos = prev+ivi[j]; prev = pos;
                   5179:                         mulq(CQ(mr),mcs,&c2); addq(svect[pos],c2,&t); svect[pos] = t;
                   5180:                     }
                   5181:                     break;
                   5182:             }
                   5183:             for ( j = k+1; j < col && !svect[j]; j++ );
                   5184:             if ( j == col ) break;
                   5185:             if ( hmag && ((double)p_mag((P)svect[j]) > hmag) ) {
                   5186:                 nz = compress_array(svect,cvect,col);
                   5187:                 removecont_array((P *)cvect,nz,1);
                   5188:                 expand_array(svect,cvect,nz);
                   5189:                 hmag = ((double)p_mag((P)svect[j]))*nd_scale;
                   5190:             }
                   5191:         }
                   5192:     }
                   5193:     nz = compress_array(svect,cvect,col);
                   5194:     removecont_array((P *)cvect,nz,1);
                   5195:     expand_array(svect,cvect,nz);
                   5196:     if ( DP_Print ) {
                   5197:         fprintf(asir_out,"-"); fflush(asir_out);
                   5198:     }
                   5199:     return maxrs;
1.107     noro     5200: }
                   5201:
1.76      noro     5202: int ndv_reduce_vect(int m,UINT *svect,int col,IndArray *imat,NM_ind_pair *rp0,int nred)
1.65      noro     5203: {
1.157     noro     5204:     int i,j,k,len,pos,prev;
                   5205:     UINT c,c1,c2,c3,up,lo,dmy;
                   5206:     IndArray ivect;
                   5207:     unsigned char *ivc;
                   5208:     unsigned short *ivs;
                   5209:     unsigned int *ivi;
                   5210:     NDV redv;
                   5211:     NMV mr;
                   5212:     NODE rp;
                   5213:     int maxrs;
                   5214:
                   5215:     maxrs = 0;
                   5216:     for ( i = 0; i < nred; i++ ) {
                   5217:         ivect = imat[i];
                   5218:         k = ivect->head; svect[k] %= m;
                   5219:         if ( c = svect[k] ) {
                   5220:             maxrs = MAX(maxrs,rp0[i]->sugar);
                   5221:             c = m-c; redv = nd_ps[rp0[i]->index];
                   5222:             len = LEN(redv); mr = BDY(redv);
                   5223:             svect[k] = 0; prev = k;
                   5224:             switch ( ivect->width ) {
                   5225:                 case 1:
                   5226:                     ivc = ivect->index.c;
                   5227:                     for ( j = 1, NMV_ADV(mr); j < len; j++, NMV_ADV(mr) ) {
                   5228:                         pos = prev+ivc[j]; c1 = CM(mr); c2 = svect[pos];
                   5229:                         prev = pos;
                   5230:                         DMA(c1,c,c2,up,lo);
                   5231:                         if ( up ) { DSAB(m,up,lo,dmy,c3); svect[pos] = c3;
                   5232:                         } else svect[pos] = lo;
                   5233:                     }
                   5234:                     break;
                   5235:                 case 2:
                   5236:                     ivs = ivect->index.s;
                   5237:                     for ( j = 1, NMV_ADV(mr); j < len; j++, NMV_ADV(mr) ) {
                   5238:                         pos = prev+ivs[j]; c1 = CM(mr); c2 = svect[pos];
                   5239:                         prev = pos;
                   5240:                         DMA(c1,c,c2,up,lo);
                   5241:                         if ( up ) { DSAB(m,up,lo,dmy,c3); svect[pos] = c3;
                   5242:                         } else svect[pos] = lo;
                   5243:                     }
                   5244:                     break;
                   5245:                 case 4:
                   5246:                     ivi = ivect->index.i;
                   5247:                     for ( j = 1, NMV_ADV(mr); j < len; j++, NMV_ADV(mr) ) {
                   5248:                         pos = prev+ivi[j]; c1 = CM(mr); c2 = svect[pos];
                   5249:                         prev = pos;
                   5250:                         DMA(c1,c,c2,up,lo);
                   5251:                         if ( up ) { DSAB(m,up,lo,dmy,c3); svect[pos] = c3;
                   5252:                         } else svect[pos] = lo;
                   5253:                     }
                   5254:                     break;
                   5255:             }
                   5256:         }
                   5257:     }
                   5258:     for ( i = 0; i < col; i++ )
                   5259:         if ( svect[i] >= (UINT)m ) svect[i] %= m;
                   5260:     return maxrs;
1.65      noro     5261: }
                   5262:
1.76      noro     5263: int ndv_reduce_vect_sf(int m,UINT *svect,int col,IndArray *imat,NM_ind_pair *rp0,int nred)
1.72      noro     5264: {
1.157     noro     5265:     int i,j,k,len,pos,prev;
                   5266:     UINT c,c1,c2,c3,up,lo,dmy;
                   5267:     IndArray ivect;
                   5268:     unsigned char *ivc;
                   5269:     unsigned short *ivs;
                   5270:     unsigned int *ivi;
                   5271:     NDV redv;
                   5272:     NMV mr;
                   5273:     NODE rp;
                   5274:     int maxrs;
                   5275:
                   5276:     maxrs = 0;
                   5277:     for ( i = 0; i < nred; i++ ) {
                   5278:         ivect = imat[i];
                   5279:         k = ivect->head; svect[k] %= m;
                   5280:         if ( c = svect[k] ) {
                   5281:             maxrs = MAX(maxrs,rp0[i]->sugar);
                   5282:             c = _chsgnsf(c); redv = nd_ps[rp0[i]->index];
                   5283:             len = LEN(redv); mr = BDY(redv);
                   5284:             svect[k] = 0; prev = k;
                   5285:             switch ( ivect->width ) {
                   5286:                 case 1:
                   5287:                     ivc = ivect->index.c;
                   5288:                     for ( j = 1, NMV_ADV(mr); j < len; j++, NMV_ADV(mr) ) {
                   5289:                         pos = prev+ivc[j]; prev = pos;
                   5290:                         svect[pos] = _addsf(_mulsf(CM(mr),c),svect[pos]);
                   5291:                     }
                   5292:                     break;
                   5293:                 case 2:
                   5294:                     ivs = ivect->index.s;
                   5295:                     for ( j = 1, NMV_ADV(mr); j < len; j++, NMV_ADV(mr) ) {
                   5296:                         pos = prev+ivs[j]; prev = pos;
                   5297:                         svect[pos] = _addsf(_mulsf(CM(mr),c),svect[pos]);
                   5298:                     }
                   5299:                     break;
                   5300:                 case 4:
                   5301:                     ivi = ivect->index.i;
                   5302:                     for ( j = 1, NMV_ADV(mr); j < len; j++, NMV_ADV(mr) ) {
                   5303:                         pos = prev+ivi[j]; prev = pos;
                   5304:                         svect[pos] = _addsf(_mulsf(CM(mr),c),svect[pos]);
                   5305:                     }
                   5306:                     break;
                   5307:             }
                   5308:         }
                   5309:     }
                   5310:     return maxrs;
1.72      noro     5311: }
                   5312:
1.65      noro     5313: NDV vect_to_ndv(UINT *vect,int spcol,int col,int *rhead,UINT *s0vect)
                   5314: {
1.157     noro     5315:     int j,k,len;
                   5316:     UINT *p;
                   5317:     UINT c;
                   5318:     NDV r;
                   5319:     NMV mr0,mr;
                   5320:
                   5321:     for ( j = 0, len = 0; j < spcol; j++ ) if ( vect[j] ) len++;
                   5322:     if ( !len ) return 0;
                   5323:     else {
                   5324:         mr0 = (NMV)GC_malloc_atomic_ignore_off_page(nmv_adv*len);
1.103     noro     5325: #if 0
1.157     noro     5326:         ndv_alloc += nmv_adv*len;
1.103     noro     5327: #endif
1.157     noro     5328:         mr = mr0;
                   5329:         p = s0vect;
                   5330:         for ( j = k = 0; j < col; j++, p += nd_wpd )
                   5331:             if ( !rhead[j] ) {
                   5332:                 if ( c = vect[k++] ) {
                   5333:                     ndl_copy(p,DL(mr)); CM(mr) = c; NMV_ADV(mr);
                   5334:                 }
                   5335:             }
                   5336:         MKNDV(nd_nvar,mr0,len,r);
                   5337:         return r;
                   5338:     }
1.65      noro     5339: }
                   5340:
1.129     noro     5341: /* for preprocessed vector */
                   5342:
1.113     noro     5343: NDV vect_to_ndv_q(Q *vect,int spcol,int col,int *rhead,UINT *s0vect)
1.107     noro     5344: {
1.157     noro     5345:     int j,k,len;
                   5346:     UINT *p;
                   5347:     Q c;
                   5348:     NDV r;
                   5349:     NMV mr0,mr;
                   5350:
                   5351:     for ( j = 0, len = 0; j < spcol; j++ ) if ( vect[j] ) len++;
                   5352:     if ( !len ) return 0;
                   5353:     else {
                   5354:         mr0 = (NMV)GC_malloc(nmv_adv*len);
1.107     noro     5355: #if 0
1.157     noro     5356:         ndv_alloc += nmv_adv*len;
1.107     noro     5357: #endif
1.157     noro     5358:         mr = mr0;
                   5359:         p = s0vect;
                   5360:         for ( j = k = 0; j < col; j++, p += nd_wpd )
                   5361:             if ( !rhead[j] ) {
                   5362:                 if ( c = vect[k++] ) {
                   5363:                     if ( DN(c) )
                   5364:                         error("afo");
                   5365:                     ndl_copy(p,DL(mr)); CQ(mr) = c; NMV_ADV(mr);
                   5366:                 }
                   5367:             }
                   5368:         MKNDV(nd_nvar,mr0,len,r);
                   5369:         return r;
                   5370:     }
1.107     noro     5371: }
                   5372:
1.129     noro     5373: /* for plain vector */
                   5374:
                   5375: NDV plain_vect_to_ndv_q(Q *vect,int col,UINT *s0vect)
                   5376: {
1.157     noro     5377:     int j,k,len;
                   5378:     UINT *p;
                   5379:     Q c;
                   5380:     NDV r;
                   5381:     NMV mr0,mr;
                   5382:
                   5383:     for ( j = 0, len = 0; j < col; j++ ) if ( vect[j] ) len++;
                   5384:     if ( !len ) return 0;
                   5385:     else {
                   5386:         mr0 = (NMV)GC_malloc(nmv_adv*len);
1.129     noro     5387: #if 0
1.157     noro     5388:         ndv_alloc += nmv_adv*len;
1.129     noro     5389: #endif
1.157     noro     5390:         mr = mr0;
                   5391:         p = s0vect;
                   5392:         for ( j = k = 0; j < col; j++, p += nd_wpd, k++ )
                   5393:             if ( c = vect[k] ) {
                   5394:                 if ( DN(c) )
                   5395:                     error("afo");
                   5396:                 ndl_copy(p,DL(mr)); CQ(mr) = c; NMV_ADV(mr);
                   5397:             }
                   5398:         MKNDV(nd_nvar,mr0,len,r);
                   5399:         return r;
                   5400:     }
1.129     noro     5401: }
                   5402:
1.133     noro     5403: int nd_sp_f4(int m,int trace,ND_pairs l,PGeoBucket bucket)
1.65      noro     5404: {
1.157     noro     5405:     ND_pairs t;
                   5406:     NODE sp0,sp;
                   5407:     int stat;
                   5408:     ND spol;
                   5409:
                   5410:     for ( t = l; t; t = NEXT(t) ) {
                   5411:         stat = nd_sp(m,trace,t,&spol);
                   5412:         if ( !stat ) return 0;
                   5413:         if ( spol ) {
                   5414:             add_pbucket_symbolic(bucket,spol);
                   5415:         }
                   5416:     }
                   5417:     return 1;
1.65      noro     5418: }
                   5419:
1.133     noro     5420: int nd_symbolic_preproc(PGeoBucket bucket,int trace,UINT **s0vect,NODE *r)
1.65      noro     5421: {
1.157     noro     5422:     NODE rp0,rp;
                   5423:     NM mul,head,s0,s;
                   5424:     int index,col,i,sugar;
                   5425:     RHist h;
                   5426:     UINT *s0v,*p;
                   5427:     NM_ind_pair pair;
                   5428:     ND red;
                   5429:     NDV *ps;
                   5430:
                   5431:     s0 = 0; rp0 = 0; col = 0;
                   5432:     ps = trace?nd_ps_trace:nd_ps;
                   5433:     while ( 1 ) {
                   5434:         head = remove_head_pbucket_symbolic(bucket);
                   5435:         if ( !head ) break;
                   5436:         if ( !s0 ) s0 = head;
                   5437:         else NEXT(s) = head;
                   5438:         s = head;
                   5439:         index = ndl_find_reducer(DL(head));
                   5440:         if ( index >= 0 ) {
                   5441:             h = nd_psh[index];
                   5442:             NEWNM(mul);
                   5443:             ndl_sub(DL(head),DL(h),DL(mul));
                   5444:             if ( ndl_check_bound2(index,DL(mul)) ) return 0;
                   5445:             sugar = TD(DL(mul))+SG(ps[index]);
                   5446:             MKNM_ind_pair(pair,mul,index,sugar);
                   5447:             red = ndv_mul_nm_symbolic(mul,ps[index]);
                   5448:             add_pbucket_symbolic(bucket,nd_remove_head(red));
                   5449:             NEXTNODE(rp0,rp); BDY(rp) = (pointer)pair;
                   5450:         }
                   5451:         col++;
                   5452:     }
                   5453:     if ( rp0 ) NEXT(rp) = 0;
                   5454:     NEXT(s) = 0;
                   5455:     s0v = (UINT *)MALLOC_ATOMIC(col*nd_wpd*sizeof(UINT));
                   5456:     for ( i = 0, p = s0v, s = s0; i < col;
                   5457:         i++, p += nd_wpd, s = NEXT(s) ) ndl_copy(DL(s),p);
                   5458:     *s0vect = s0v;
                   5459:     *r = rp0;
                   5460:     return col;
1.65      noro     5461: }
                   5462:
1.167     noro     5463: NODE nd_f4(int m,int **indp)
1.69      noro     5464: {
1.157     noro     5465:     int i,nh,stat,index;
                   5466:     NODE r,g;
                   5467:     ND_pairs d,l,t;
                   5468:     ND spol,red;
                   5469:     NDV nf,redv;
                   5470:     NM s0,s;
                   5471:     NODE rp0,srp0,nflist;
                   5472:     int nsp,nred,col,rank,len,k,j,a;
                   5473:     UINT c;
                   5474:     UINT **spmat;
                   5475:     UINT *s0vect,*svect,*p,*v;
                   5476:     int *colstat;
                   5477:     IndArray *imat;
                   5478:     int *rhead;
                   5479:     int spcol,sprow;
                   5480:     int sugar;
                   5481:     PGeoBucket bucket;
                   5482:     struct oEGT eg0,eg1,eg_f4;
1.69      noro     5483:
1.103     noro     5484: #if 0
1.157     noro     5485:     ndv_alloc = 0;
1.103     noro     5486: #endif
1.157     noro     5487:     g = 0; d = 0;
                   5488:     for ( i = 0; i < nd_psn; i++ ) {
1.168     noro     5489:         d = update_pairs(d,g,i,0);
1.157     noro     5490:         g = update_base(g,i);
                   5491:     }
                   5492:     while ( d ) {
                   5493:         get_eg(&eg0);
                   5494:         l = nd_minsugarp(d,&d);
                   5495:         sugar = SG(l);
                   5496:         bucket = create_pbucket();
                   5497:         stat = nd_sp_f4(m,0,l,bucket);
                   5498:         if ( !stat ) {
                   5499:             for ( t = l; NEXT(t); t = NEXT(t) );
                   5500:             NEXT(t) = d; d = l;
                   5501:             d = nd_reconstruct(0,d);
                   5502:             continue;
                   5503:         }
                   5504:         if ( bucket->m < 0 ) continue;
                   5505:         col = nd_symbolic_preproc(bucket,0,&s0vect,&rp0);
                   5506:         if ( !col ) {
                   5507:             for ( t = l; NEXT(t); t = NEXT(t) );
                   5508:             NEXT(t) = d; d = l;
                   5509:             d = nd_reconstruct(0,d);
                   5510:             continue;
                   5511:         }
                   5512:         get_eg(&eg1); init_eg(&eg_f4); add_eg(&eg_f4,&eg0,&eg1);
                   5513:         if ( DP_Print )
                   5514:             fprintf(asir_out,"sugar=%d,symb=%fsec,",
                   5515:                 sugar,eg_f4.exectime+eg_f4.gctime);
                   5516:         if ( 1 )
                   5517:             nflist = nd_f4_red(m,l,0,s0vect,col,rp0,0);
                   5518:         else
                   5519:             nflist = nd_f4_red_dist(m,l,s0vect,col,rp0,0);
                   5520:         /* adding new bases */
                   5521:         for ( r = nflist; r; r = NEXT(r) ) {
                   5522:             nf = (NDV)BDY(r);
                   5523:             ndv_removecont(m,nf);
                   5524:             if ( !m && nd_nalg ) {
                   5525:                 ND nf1;
                   5526:
                   5527:                 nf1 = ndvtond(m,nf);
                   5528:                 nd_monic(0,&nf1);
                   5529:                 nd_removecont(m,nf1);
                   5530:                 nf = ndtondv(m,nf1);
                   5531:             }
                   5532:             nh = ndv_newps(m,nf,0);
1.168     noro     5533:             d = update_pairs(d,g,nh,0);
1.157     noro     5534:             g = update_base(g,nh);
                   5535:         }
                   5536:     }
1.103     noro     5537: #if 0
1.157     noro     5538:     fprintf(asir_out,"ndv_alloc=%d\n",ndv_alloc);
1.103     noro     5539: #endif
1.167     noro     5540:        conv_ilist(0,0,g,indp);
1.157     noro     5541:     return g;
1.69      noro     5542: }
1.74      noro     5543:
1.167     noro     5544: NODE nd_f4_trace(int m,int **indp)
1.133     noro     5545: {
1.157     noro     5546:     int i,nh,stat,index;
                   5547:     NODE r,g;
                   5548:     ND_pairs d,l,l0,t;
                   5549:     ND spol,red;
                   5550:     NDV nf,redv,nfqv,nfv;
                   5551:     NM s0,s;
                   5552:     NODE rp0,srp0,nflist;
                   5553:     int nsp,nred,col,rank,len,k,j,a;
                   5554:     UINT c;
                   5555:     UINT **spmat;
                   5556:     UINT *s0vect,*svect,*p,*v;
                   5557:     int *colstat;
                   5558:     IndArray *imat;
                   5559:     int *rhead;
                   5560:     int spcol,sprow;
                   5561:     int sugar;
                   5562:     PGeoBucket bucket;
                   5563:     struct oEGT eg0,eg1,eg_f4;
                   5564:
                   5565:     g = 0; d = 0;
                   5566:     for ( i = 0; i < nd_psn; i++ ) {
1.168     noro     5567:         d = update_pairs(d,g,i,0);
1.157     noro     5568:         g = update_base(g,i);
                   5569:     }
                   5570:     while ( d ) {
                   5571:         get_eg(&eg0);
                   5572:         l = nd_minsugarp(d,&d);
                   5573:         sugar = SG(l);
                   5574:         bucket = create_pbucket();
                   5575:         stat = nd_sp_f4(m,0,l,bucket);
                   5576:         if ( !stat ) {
                   5577:             for ( t = l; NEXT(t); t = NEXT(t) );
                   5578:             NEXT(t) = d; d = l;
                   5579:             d = nd_reconstruct(1,d);
                   5580:             continue;
                   5581:         }
                   5582:         if ( bucket->m < 0 ) continue;
                   5583:         col = nd_symbolic_preproc(bucket,0,&s0vect,&rp0);
                   5584:         if ( !col ) {
                   5585:             for ( t = l; NEXT(t); t = NEXT(t) );
                   5586:             NEXT(t) = d; d = l;
                   5587:             d = nd_reconstruct(1,d);
                   5588:             continue;
                   5589:         }
                   5590:         get_eg(&eg1); init_eg(&eg_f4); add_eg(&eg_f4,&eg0,&eg1);
                   5591:         if ( DP_Print )
                   5592:             fprintf(asir_out,"sugar=%d,symb=%fsec,",
                   5593:                 sugar,eg_f4.exectime+eg_f4.gctime);
                   5594:         nflist = nd_f4_red(m,l,0,s0vect,col,rp0,&l0);
                   5595:         if ( !l0 ) continue;
                   5596:         l = l0;
                   5597:
                   5598:         /* over Q */
                   5599:         bucket = create_pbucket();
                   5600:         stat = nd_sp_f4(0,1,l,bucket);
                   5601:         if ( !stat ) {
                   5602:             for ( t = l; NEXT(t); t = NEXT(t) );
                   5603:             NEXT(t) = d; d = l;
                   5604:             d = nd_reconstruct(1,d);
                   5605:             continue;
                   5606:         }
                   5607:         if ( bucket->m < 0 ) continue;
                   5608:         col = nd_symbolic_preproc(bucket,1,&s0vect,&rp0);
                   5609:         if ( !col ) {
                   5610:             for ( t = l; NEXT(t); t = NEXT(t) );
                   5611:             NEXT(t) = d; d = l;
                   5612:             d = nd_reconstruct(1,d);
                   5613:             continue;
                   5614:         }
                   5615:         nflist = nd_f4_red(0,l,1,s0vect,col,rp0,0);
                   5616:         /* adding new bases */
                   5617:         for ( r = nflist; r; r = NEXT(r) ) {
                   5618:             nfqv = (NDV)BDY(r);
                   5619:             ndv_removecont(0,nfqv);
                   5620:             if ( !rem(NM(HCQ(nfqv)),m) ) return 0;
                   5621:             if ( nd_nalg ) {
                   5622:                 ND nf1;
                   5623:
                   5624:                 nf1 = ndvtond(m,nfqv);
                   5625:                 nd_monic(0,&nf1);
                   5626:                 nd_removecont(0,nf1);
                   5627:                 nfqv = ndtondv(0,nf1); nd_free(nf1);
                   5628:             }
                   5629:             nfv = ndv_dup(0,nfqv);
                   5630:             ndv_mod(m,nfv);
                   5631:             ndv_removecont(m,nfv);
                   5632:             nh = ndv_newps(0,nfv,nfqv);
1.168     noro     5633:             d = update_pairs(d,g,nh,0);
1.157     noro     5634:             g = update_base(g,nh);
                   5635:         }
                   5636:     }
1.133     noro     5637: #if 0
1.157     noro     5638:     fprintf(asir_out,"ndv_alloc=%d\n",ndv_alloc);
1.133     noro     5639: #endif
1.167     noro     5640:        conv_ilist(0,1,g,indp);
1.157     noro     5641:     return g;
1.133     noro     5642: }
                   5643:
1.176     noro     5644: NODE nd_f4_pseudo_trace(int m,int **indp)
                   5645: {
                   5646:     int i,nh,stat,index;
                   5647:     NODE r,g;
                   5648:     ND_pairs d,l,l0,t;
                   5649:     ND spol,red;
                   5650:     NDV nf,redv,nfqv,nfv;
                   5651:     NM s0,s;
                   5652:     NODE rp0,srp0,nflist;
                   5653:     int nsp,nred,col,rank,len,k,j,a;
                   5654:     UINT c;
                   5655:     UINT **spmat;
                   5656:     UINT *s0vect,*svect,*p,*v;
                   5657:     int *colstat;
                   5658:     IndArray *imat;
                   5659:     int *rhead;
                   5660:     int spcol,sprow;
                   5661:     int sugar;
                   5662:     PGeoBucket bucket;
                   5663:     struct oEGT eg0,eg1,eg_f4;
                   5664:
                   5665:     g = 0; d = 0;
                   5666:     for ( i = 0; i < nd_psn; i++ ) {
                   5667:         d = update_pairs(d,g,i,0);
                   5668:         g = update_base(g,i);
                   5669:     }
                   5670:     while ( d ) {
                   5671:         get_eg(&eg0);
                   5672:         l = nd_minsugarp(d,&d);
                   5673:         sugar = SG(l);
                   5674:         bucket = create_pbucket();
                   5675:         stat = nd_sp_f4(m,0,l,bucket);
                   5676:         if ( !stat ) {
                   5677:             for ( t = l; NEXT(t); t = NEXT(t) );
                   5678:             NEXT(t) = d; d = l;
                   5679:             d = nd_reconstruct(1,d);
                   5680:             continue;
                   5681:         }
                   5682:         if ( bucket->m < 0 ) continue;
                   5683:         col = nd_symbolic_preproc(bucket,0,&s0vect,&rp0);
                   5684:         if ( !col ) {
                   5685:             for ( t = l; NEXT(t); t = NEXT(t) );
                   5686:             NEXT(t) = d; d = l;
                   5687:             d = nd_reconstruct(1,d);
                   5688:             continue;
                   5689:         }
                   5690:         get_eg(&eg1); init_eg(&eg_f4); add_eg(&eg_f4,&eg0,&eg1);
                   5691:         if ( DP_Print )
                   5692:             fprintf(asir_out,"sugar=%d,symb=%fsec,",
                   5693:                 sugar,eg_f4.exectime+eg_f4.gctime);
                   5694:         nflist = nd_f4_red(m,l,0,s0vect,col,rp0,&l0);
                   5695:         if ( !l0 ) continue;
                   5696:         l = l0;
                   5697:
                   5698:         /* over Q */
                   5699:                while ( 1 ) {
                   5700:                bucket = create_pbucket();
                   5701:                stat = nd_sp_f4(0,1,l,bucket);
                   5702:                if ( !stat ) {
                   5703:                for ( t = l; NEXT(t); t = NEXT(t) );
                   5704:                NEXT(t) = d; d = l;
                   5705:                d = nd_reconstruct(1,d);
                   5706:                continue;
                   5707:                }
                   5708:                if ( bucket->m < 0 ) continue;
                   5709:                col = nd_symbolic_preproc(bucket,1,&s0vect,&rp0);
                   5710:                if ( !col ) {
                   5711:                for ( t = l; NEXT(t); t = NEXT(t) );
                   5712:                NEXT(t) = d; d = l;
                   5713:                d = nd_reconstruct(1,d);
                   5714:                continue;
                   5715:                }
                   5716:                nflist = nd_f4_red(0,l,1,s0vect,col,rp0,0);
                   5717:                }
                   5718:
                   5719:         /* adding new bases */
                   5720:         for ( r = nflist; r; r = NEXT(r) ) {
                   5721:             nfqv = (NDV)BDY(r);
                   5722:             ndv_removecont(0,nfqv);
                   5723:             if ( !rem(NM(HCQ(nfqv)),m) ) return 0;
                   5724:             if ( nd_nalg ) {
                   5725:                 ND nf1;
                   5726:
                   5727:                 nf1 = ndvtond(m,nfqv);
                   5728:                 nd_monic(0,&nf1);
                   5729:                 nd_removecont(0,nf1);
                   5730:                 nfqv = ndtondv(0,nf1); nd_free(nf1);
                   5731:             }
                   5732:             nfv = ndv_dup(0,nfqv);
                   5733:             ndv_mod(m,nfv);
                   5734:             ndv_removecont(m,nfv);
                   5735:             nh = ndv_newps(0,nfv,nfqv);
                   5736:             d = update_pairs(d,g,nh,0);
                   5737:             g = update_base(g,nh);
                   5738:         }
                   5739:     }
                   5740: #if 0
                   5741:     fprintf(asir_out,"ndv_alloc=%d\n",ndv_alloc);
                   5742: #endif
                   5743:        conv_ilist(0,1,g,indp);
                   5744:     return g;
                   5745: }
                   5746:
1.133     noro     5747: NODE nd_f4_red(int m,ND_pairs sp0,int trace,UINT *s0vect,int col,NODE rp0,ND_pairs *nz)
1.63      noro     5748: {
1.157     noro     5749:     IndArray *imat;
                   5750:     int nsp,nred,i;
                   5751:     int *rhead;
                   5752:     NODE r0,rp;
                   5753:     ND_pairs sp;
                   5754:     NM_ind_pair *rvect;
                   5755:
                   5756:     for ( sp = sp0, nsp = 0; sp; sp = NEXT(sp), nsp++ );
                   5757:     nred = length(rp0);
                   5758:     imat = (IndArray *)ALLOCA(nred*sizeof(IndArray));
                   5759:     rhead = (int *)ALLOCA(col*sizeof(int));
                   5760:     for ( i = 0; i < col; i++ ) rhead[i] = 0;
                   5761:
                   5762:     /* construction of index arrays */
                   5763:     rvect = (NM_ind_pair *)ALLOCA(nred*sizeof(NM_ind_pair));
                   5764:     for ( rp = rp0, i = 0; rp; i++, rp = NEXT(rp) ) {
                   5765:         rvect[i] = (NM_ind_pair)BDY(rp);
                   5766:         imat[i] = nm_ind_pair_to_vect_compress(m,s0vect,col,rvect[i]);
                   5767:         rhead[imat[i]->head] = 1;
                   5768:     }
                   5769:     if ( m )
                   5770:         r0 = nd_f4_red_main(m,sp0,nsp,s0vect,col,rvect,rhead,imat,nred,nz);
                   5771:     else
                   5772:         r0 = nd_f4_red_q_main(sp0,nsp,trace,s0vect,col,rvect,rhead,imat,nred);
                   5773:     return r0;
1.106     noro     5774: }
1.74      noro     5775:
1.106     noro     5776: NODE nd_f4_red_main(int m,ND_pairs sp0,int nsp,UINT *s0vect,int col,
1.133     noro     5777:         NM_ind_pair *rvect,int *rhead,IndArray *imat,int nred,ND_pairs *nz)
1.106     noro     5778: {
1.157     noro     5779:     int spcol,sprow,a;
                   5780:     int i,j,k,l,rank;
                   5781:     NODE r0,r;
                   5782:     ND_pairs sp;
                   5783:     ND spol;
                   5784:     int **spmat;
                   5785:     UINT *svect,*v;
                   5786:     int *colstat;
                   5787:     struct oEGT eg0,eg1,eg2,eg_f4,eg_f4_1,eg_f4_2;
                   5788:     int maxrs;
                   5789:     int *spsugar;
                   5790:     ND_pairs *spactive;
                   5791:
                   5792:     spcol = col-nred;
                   5793:     get_eg(&eg0);
                   5794:     /* elimination (1st step) */
                   5795:     spmat = (int **)ALLOCA(nsp*sizeof(UINT *));
                   5796:     svect = (UINT *)ALLOCA(col*sizeof(UINT));
                   5797:     spsugar = (int *)ALLOCA(nsp*sizeof(UINT));
                   5798:     spactive = !nz?0:(ND_pairs *)ALLOCA(nsp*sizeof(ND_pairs));
                   5799:     for ( a = sprow = 0, sp = sp0; a < nsp; a++, sp = NEXT(sp) ) {
                   5800:         nd_sp(m,0,sp,&spol);
                   5801:         if ( !spol ) continue;
                   5802:         nd_to_vect(m,s0vect,col,spol,svect);
                   5803:         if ( m == -1 )
                   5804:             maxrs = ndv_reduce_vect_sf(m,svect,col,imat,rvect,nred);
                   5805:         else
                   5806:             maxrs = ndv_reduce_vect(m,svect,col,imat,rvect,nred);
                   5807:         for ( i = 0; i < col; i++ ) if ( svect[i] ) break;
                   5808:         if ( i < col ) {
                   5809:             spmat[sprow] = v = (UINT *)MALLOC_ATOMIC(spcol*sizeof(UINT));
                   5810:             for ( j = k = 0; j < col; j++ )
                   5811:                 if ( !rhead[j] ) v[k++] = svect[j];
                   5812:             spsugar[sprow] = MAX(maxrs,SG(spol));
                   5813:             if ( nz )
                   5814:             spactive[sprow] = sp;
                   5815:             sprow++;
                   5816:         }
                   5817:         nd_free(spol);
                   5818:     }
                   5819:     get_eg(&eg1); init_eg(&eg_f4_1); add_eg(&eg_f4_1,&eg0,&eg1);
                   5820:     if ( DP_Print ) {
                   5821:         fprintf(asir_out,"elim1=%fsec,",eg_f4_1.exectime+eg_f4_1.gctime);
                   5822:         fflush(asir_out);
                   5823:     }
                   5824:     /* free index arrays */
                   5825:     for ( i = 0; i < nred; i++ ) GC_free(imat[i]->index.c);
                   5826:
                   5827:     /* elimination (2nd step) */
                   5828:     colstat = (int *)ALLOCA(spcol*sizeof(int));
                   5829:     if ( m == -1 )
                   5830:         rank = nd_gauss_elim_sf(spmat,spsugar,sprow,spcol,m,colstat);
                   5831:     else
                   5832:         rank = nd_gauss_elim_mod(spmat,spsugar,spactive,sprow,spcol,m,colstat);
                   5833:     r0 = 0;
                   5834:     for ( i = 0; i < rank; i++ ) {
                   5835:         NEXTNODE(r0,r); BDY(r) =
                   5836:             (pointer)vect_to_ndv(spmat[i],spcol,col,rhead,s0vect);
                   5837:         SG((NDV)BDY(r)) = spsugar[i];
                   5838:         GC_free(spmat[i]);
                   5839:     }
                   5840:     if ( r0 ) NEXT(r) = 0;
                   5841:
                   5842:     for ( ; i < sprow; i++ ) GC_free(spmat[i]);
                   5843:     get_eg(&eg2); init_eg(&eg_f4_2); add_eg(&eg_f4_2,&eg1,&eg2);
                   5844:     init_eg(&eg_f4); add_eg(&eg_f4,&eg0,&eg2);
                   5845:     if ( DP_Print ) {
                   5846:         fprintf(asir_out,"elim2=%fsec\n",eg_f4_2.exectime+eg_f4_2.gctime);
                   5847:         fprintf(asir_out,"nsp=%d,nred=%d,spmat=(%d,%d),rank=%d  ",
                   5848:             nsp,nred,sprow,spcol,rank);
                   5849:         fprintf(asir_out,"%fsec\n",eg_f4.exectime+eg_f4.gctime);
                   5850:     }
                   5851:     if ( nz ) {
                   5852:         for ( i = 0; i < rank-1; i++ ) NEXT(spactive[i]) = spactive[i+1];
                   5853:         if ( rank > 0 ) {
                   5854:             NEXT(spactive[rank-1]) = 0;
                   5855:             *nz = spactive[0];
                   5856:         } else
                   5857:             *nz = 0;
                   5858:     }
                   5859:     return r0;
1.74      noro     5860: }
                   5861:
1.133     noro     5862: #if 1
                   5863: NODE nd_f4_red_q_main(ND_pairs sp0,int nsp,int trace,UINT *s0vect,int col,
1.107     noro     5864:         NM_ind_pair *rvect,int *rhead,IndArray *imat,int nred)
                   5865: {
1.157     noro     5866:     int spcol,sprow,a;
                   5867:     int i,j,k,l,rank;
                   5868:     NODE r0,r;
                   5869:     ND_pairs sp;
                   5870:     ND spol;
                   5871:     Q **spmat;
                   5872:     Q *svect,*v;
                   5873:     int *colstat;
                   5874:     struct oEGT eg0,eg1,eg2,eg_f4,eg_f4_1,eg_f4_2;
                   5875:     int maxrs;
                   5876:     int *spsugar;
                   5877:     pointer *w;
                   5878:
                   5879:     spcol = col-nred;
                   5880:     get_eg(&eg0);
                   5881:     /* elimination (1st step) */
                   5882:     spmat = (Q **)ALLOCA(nsp*sizeof(Q *));
                   5883:     svect = (Q *)ALLOCA(col*sizeof(Q));
                   5884:     spsugar = (int *)ALLOCA(nsp*sizeof(Q));
                   5885:     for ( a = sprow = 0, sp = sp0; a < nsp; a++, sp = NEXT(sp) ) {
                   5886:         nd_sp(0,trace,sp,&spol);
                   5887:         if ( !spol ) continue;
                   5888:         nd_to_vect_q(s0vect,col,spol,svect);
                   5889:         maxrs = ndv_reduce_vect_q(svect,trace,col,imat,rvect,nred);
                   5890:         for ( i = 0; i < col; i++ ) if ( svect[i] ) break;
                   5891:         if ( i < col ) {
                   5892:             spmat[sprow] = v = (Q *)MALLOC(spcol*sizeof(Q));
                   5893:             for ( j = k = 0; j < col; j++ )
                   5894:                 if ( !rhead[j] ) v[k++] = svect[j];
                   5895:             spsugar[sprow] = MAX(maxrs,SG(spol));
                   5896:             sprow++;
                   5897:         }
                   5898: /*        nd_free(spol); */
                   5899:     }
                   5900:     get_eg(&eg1); init_eg(&eg_f4_1); add_eg(&eg_f4_1,&eg0,&eg1);
                   5901:     if ( DP_Print ) {
                   5902:         fprintf(asir_out,"elim1=%fsec,",eg_f4_1.exectime+eg_f4_1.gctime);
                   5903:         fflush(asir_out);
                   5904:     }
                   5905:     /* free index arrays */
                   5906: /*    for ( i = 0; i < nred; i++ ) GC_free(imat[i]->index.c); */
                   5907:
                   5908:     /* elimination (2nd step) */
                   5909:     colstat = (int *)ALLOCA(spcol*sizeof(int));
                   5910:     rank = nd_gauss_elim_q(spmat,spsugar,sprow,spcol,colstat);
                   5911:     w = (pointer *)ALLOCA(rank*sizeof(pointer));
                   5912:     for ( i = 0; i < rank; i++ ) {
                   5913:         w[rank-i-1] = (pointer)vect_to_ndv_q(spmat[i],spcol,col,rhead,s0vect);
                   5914:         SG((NDV)w[rank-i-1]) = spsugar[i];
                   5915: /*        GC_free(spmat[i]); */
                   5916:     }
1.138     noro     5917: #if 0
1.157     noro     5918:     qsort(w,rank,sizeof(NDV),
                   5919:         (int (*)(const void *,const void *))ndv_compare);
1.137     noro     5920: #endif
1.157     noro     5921:     r0 = 0;
                   5922:     for ( i = 0; i < rank; i++ ) {
                   5923:         NEXTNODE(r0,r); BDY(r) = w[i];
                   5924:     }
                   5925:     if ( r0 ) NEXT(r) = 0;
                   5926:
                   5927: /*    for ( ; i < sprow; i++ ) GC_free(spmat[i]); */
                   5928:     get_eg(&eg2); init_eg(&eg_f4_2); add_eg(&eg_f4_2,&eg1,&eg2);
                   5929:     init_eg(&eg_f4); add_eg(&eg_f4,&eg0,&eg2);
                   5930:     if ( DP_Print ) {
                   5931:         fprintf(asir_out,"elim2=%fsec\n",eg_f4_2.exectime+eg_f4_2.gctime);
                   5932:         fprintf(asir_out,"nsp=%d,nred=%d,spmat=(%d,%d),rank=%d  ",
                   5933:             nsp,nred,sprow,spcol,rank);
                   5934:         fprintf(asir_out,"%fsec\n",eg_f4.exectime+eg_f4.gctime);
                   5935:     }
                   5936:     return r0;
1.107     noro     5937: }
1.129     noro     5938: #else
                   5939: void printm(Q **mat,int row,int col)
                   5940: {
1.157     noro     5941:     int i,j;
                   5942:     printf("[");
                   5943:     for ( i = 0; i < row; i++ ) {
                   5944:         for ( j = 0; j < col; j++ ) {
                   5945:             printexpr(CO,mat[i][j]); printf(" ");
                   5946:         }
                   5947:         printf("]\n");
                   5948:     }
1.129     noro     5949: }
                   5950:
                   5951: NODE nd_f4_red_q_main(ND_pairs sp0,int nsp,UINT *s0vect,int col,
                   5952:         NM_ind_pair *rvect,int *rhead,IndArray *imat,int nred)
                   5953: {
1.157     noro     5954:     int row,a;
                   5955:     int i,j,rank;
                   5956:     NODE r0,r;
                   5957:     ND_pairs sp;
                   5958:     ND spol;
                   5959:     Q **mat;
                   5960:     int *colstat;
                   5961:     int *sugar;
                   5962:
                   5963:     row = nsp+nred;
                   5964:     /* make the matrix */
                   5965:     mat = (Q **)ALLOCA(row*sizeof(Q *));
                   5966:     sugar = (int *)ALLOCA(row*sizeof(int));
                   5967:     for ( row = a = 0, sp = sp0; a < nsp; a++, sp = NEXT(sp) ) {
                   5968:         nd_sp(0,0,sp,&spol);
                   5969:         if ( !spol ) continue;
                   5970:         mat[row] = (Q *)MALLOC(col*sizeof(Q));
                   5971:         nd_to_vect_q(s0vect,col,spol,mat[row]);
                   5972:         sugar[row] = SG(spol);
                   5973:         row++;
                   5974:     }
                   5975:     for ( i = 0; i < nred; i++, row++ ) {
                   5976:         mat[row] = nm_ind_pair_to_vect(0,s0vect,col,rvect[i]);
                   5977:         sugar[row] = rvect[i]->sugar;
                   5978:     }
                   5979:     /* elimination */
                   5980:     colstat = (int *)ALLOCA(col*sizeof(int));
                   5981:     rank = nd_gauss_elim_q(mat,sugar,row,col,colstat);
                   5982:     r0 = 0;
                   5983:     for ( i = 0; i < rank; i++ ) {
                   5984:         for ( j = 0; j < col; j++ ) if ( mat[i][j] ) break;
                   5985:         if ( j == col ) error("nd_f4_red_q_main : cannot happen");
                   5986:         if ( rhead[j] ) continue;
                   5987:         NEXTNODE(r0,r); BDY(r) =
                   5988:             (pointer)plain_vect_to_ndv_q(mat[i],col,s0vect);
                   5989:         SG((NDV)BDY(r)) = sugar[i];
                   5990:     }
                   5991:     if ( r0 ) NEXT(r) = 0;
                   5992:     printf("\n");
                   5993:     return r0;
1.129     noro     5994: }
                   5995: #endif
1.107     noro     5996:
1.74      noro     5997: FILE *nd_write,*nd_read;
                   5998:
                   5999: void nd_send_int(int a) {
1.157     noro     6000:     write_int(nd_write,&a);
1.74      noro     6001: }
                   6002:
                   6003: void nd_send_intarray(int *p,int len) {
1.157     noro     6004:     write_intarray(nd_write,p,len);
1.74      noro     6005: }
                   6006:
                   6007: int nd_recv_int() {
1.157     noro     6008:     int a;
1.74      noro     6009:
1.157     noro     6010:     read_int(nd_read,&a);
                   6011:     return a;
1.74      noro     6012: }
                   6013:
                   6014: void nd_recv_intarray(int *p,int len) {
1.157     noro     6015:     read_intarray(nd_read,p,len);
1.74      noro     6016: }
                   6017:
                   6018: void nd_send_ndv(NDV p) {
1.157     noro     6019:     int len,i;
                   6020:     NMV m;
1.74      noro     6021:
1.157     noro     6022:     if ( !p ) nd_send_int(0);
                   6023:     else {
                   6024:         len = LEN(p);
                   6025:         nd_send_int(len);
                   6026:         m = BDY(p);
                   6027:         for ( i = 0; i < len; i++, NMV_ADV(m) ) {
                   6028:             nd_send_int(CM(m));
                   6029:             nd_send_intarray(DL(m),nd_wpd);
                   6030:         }
                   6031:     }
1.74      noro     6032: }
                   6033:
                   6034: void nd_send_nd(ND p) {
1.157     noro     6035:     int len,i;
                   6036:     NM m;
1.74      noro     6037:
1.157     noro     6038:     if ( !p ) nd_send_int(0);
                   6039:     else {
                   6040:         len = LEN(p);
                   6041:         nd_send_int(len);
                   6042:         m = BDY(p);
                   6043:         for ( i = 0; i < len; i++, m = NEXT(m) ) {
                   6044:             nd_send_int(CM(m));
                   6045:             nd_send_intarray(DL(m),nd_wpd);
                   6046:         }
                   6047:     }
1.74      noro     6048: }
1.65      noro     6049:
1.74      noro     6050: NDV nd_recv_ndv()
                   6051: {
1.157     noro     6052:     int len,i;
                   6053:     NMV m,m0;
                   6054:     NDV r;
                   6055:
                   6056:     len = nd_recv_int();
                   6057:     if ( !len ) return 0;
                   6058:     else {
                   6059:         m0 = m = (NMV)GC_malloc_atomic_ignore_off_page(nmv_adv*len);
1.103     noro     6060: #if 0
1.157     noro     6061:         ndv_alloc += len*nmv_adv;
1.103     noro     6062: #endif
1.157     noro     6063:         for ( i = 0; i < len; i++, NMV_ADV(m) ) {
                   6064:             CM(m) = nd_recv_int();
                   6065:             nd_recv_intarray(DL(m),nd_wpd);
                   6066:         }
                   6067:         MKNDV(nd_nvar,m0,len,r);
                   6068:         return r;
                   6069:     }
1.74      noro     6070: }
1.65      noro     6071:
1.74      noro     6072: int ox_exec_f4_red(Q proc)
                   6073: {
1.157     noro     6074:     Obj obj;
                   6075:     STRING fname;
                   6076:     NODE arg;
                   6077:     int s;
                   6078:     extern int ox_need_conv,ox_file_io;
                   6079:
                   6080:     MKSTR(fname,"nd_exec_f4_red");
                   6081:     arg = mknode(2,proc,fname);
                   6082:     Pox_cmo_rpc(arg,&obj);
                   6083:     s = get_ox_server_id(QTOS(proc));
                   6084:     nd_write = iofp[s].out;
                   6085:     nd_read = iofp[s].in;
                   6086:     ox_need_conv = ox_file_io = 0;
                   6087:     return s;
1.74      noro     6088: }
                   6089:
1.133     noro     6090: NODE nd_f4_red_dist(int m,ND_pairs sp0,UINT *s0vect,int col,NODE rp0,ND_pairs *nz)
1.74      noro     6091: {
1.157     noro     6092:     int nsp,nred;
                   6093:     int i,rank,s;
                   6094:     NODE rp,r0,r;
                   6095:     ND_pairs sp;
                   6096:     NM_ind_pair pair;
                   6097:     NMV nmv;
                   6098:     NM nm;
                   6099:     NDV nf;
                   6100:     Obj proc,dmy;
                   6101:
                   6102:     ox_launch_main(0,0,&proc);
                   6103:     s = ox_exec_f4_red((Q)proc);
                   6104:
                   6105:     nd_send_int(m);
                   6106:     nd_send_int(nd_nvar);
                   6107:     nd_send_int(nd_bpe);
                   6108:     nd_send_int(nd_wpd);
                   6109:     nd_send_int(nmv_adv);
                   6110:
                   6111:     saveobj(nd_write,dp_current_spec->obj); fflush(nd_write);
                   6112:
                   6113:     nd_send_int(nd_psn);
                   6114:     for ( i = 0; i < nd_psn; i++ ) nd_send_ndv(nd_ps[i]);
                   6115:
                   6116:     for ( sp = sp0, nsp = 0; sp; sp = NEXT(sp), nsp++ );
                   6117:     nd_send_int(nsp);
                   6118:     for ( i = 0, sp = sp0; i < nsp; i++, sp = NEXT(sp) ) {
                   6119:         nd_send_int(sp->i1); nd_send_int(sp->i2);
                   6120:     }
                   6121:
                   6122:     nd_send_int(col); nd_send_intarray(s0vect,col*nd_wpd);
                   6123:
                   6124:     nred = length(rp0); nd_send_int(nred);
                   6125:     for ( i = 0, rp = rp0; i < nred; i++, rp = NEXT(rp) ) {
                   6126:         pair = (NM_ind_pair)BDY(rp);
                   6127:         nd_send_int(pair->index);
                   6128:         nd_send_intarray(pair->mul->dl,nd_wpd);
                   6129:     }
                   6130:     fflush(nd_write);
                   6131:     rank = nd_recv_int();
                   6132:     fprintf(asir_out,"rank=%d\n",rank);
                   6133:     r0 = 0;
                   6134:     for ( i = 0; i < rank; i++ ) {
                   6135:         nf = nd_recv_ndv();
                   6136:         NEXTNODE(r0,r); BDY(r) = (pointer)nf;
                   6137:     }
                   6138:     Pox_shutdown(mknode(1,proc),&dmy);
                   6139:     return r0;
1.74      noro     6140: }
                   6141:
                   6142: /* server side */
                   6143:
                   6144: void nd_exec_f4_red_dist()
                   6145: {
1.157     noro     6146:     int m,i,nsp,col,s0size,nred,spcol,j,k;
                   6147:     NM_ind_pair *rp0;
                   6148:     NDV nf;
                   6149:     UINT *s0vect;
                   6150:     IndArray *imat;
                   6151:     int *rhead;
                   6152:     int **spmat;
                   6153:     UINT *svect,*v;
                   6154:     ND_pairs *sp0;
                   6155:     int *colstat;
                   6156:     int a,sprow,rank;
                   6157:     struct order_spec *ord;
                   6158:     Obj ordspec;
                   6159:     ND spol;
                   6160:     int maxrs;
                   6161:     int *spsugar;
                   6162:
                   6163:     nd_read = iofp[0].in;
                   6164:     nd_write = iofp[0].out;
                   6165:     m = nd_recv_int();
                   6166:     nd_nvar = nd_recv_int();
                   6167:     nd_bpe = nd_recv_int();
                   6168:     nd_wpd = nd_recv_int();
                   6169:     nmv_adv = nd_recv_int();
                   6170:
                   6171:     loadobj(nd_read,&ordspec);
                   6172:     create_order_spec(0,ordspec,&ord);
                   6173:     nd_init_ord(ord);
                   6174:     nd_setup_parameters(nd_nvar,0);
                   6175:
                   6176:     nd_psn = nd_recv_int();
                   6177:     nd_ps = (NDV *)MALLOC(nd_psn*sizeof(NDV));
                   6178:     nd_bound = (UINT **)MALLOC(nd_psn*sizeof(UINT *));
                   6179:     for ( i = 0; i < nd_psn; i++ ) {
                   6180:         nd_ps[i] = nd_recv_ndv();
                   6181:         nd_bound[i] = ndv_compute_bound(nd_ps[i]);
                   6182:     }
                   6183:
                   6184:     nsp = nd_recv_int();
                   6185:     sp0 = (ND_pairs *)MALLOC(nsp*sizeof(ND_pairs));
                   6186:     for ( i = 0; i < nsp; i++ ) {
                   6187:         NEWND_pairs(sp0[i]);
                   6188:         sp0[i]->i1 = nd_recv_int(); sp0[i]->i2 = nd_recv_int();
                   6189:         ndl_lcm(HDL(nd_ps[sp0[i]->i1]),HDL(nd_ps[sp0[i]->i2]),LCM(sp0[i]));
                   6190:     }
                   6191:
                   6192:     col = nd_recv_int();
                   6193:     s0size = col*nd_wpd;
                   6194:     s0vect = (UINT *)MALLOC(s0size*sizeof(UINT));
                   6195:     nd_recv_intarray(s0vect,s0size);
                   6196:
                   6197:     nred = nd_recv_int();
                   6198:     rp0 = (NM_ind_pair *)MALLOC(nred*sizeof(NM_ind_pair));
                   6199:     for ( i = 0; i < nred; i++ ) {
                   6200:         rp0[i] = (NM_ind_pair)MALLOC(sizeof(struct oNM_ind_pair));
                   6201:         rp0[i]->index = nd_recv_int();
                   6202:         rp0[i]->mul = (NM)MALLOC(sizeof(struct oNM)+(nd_wpd-1)*sizeof(UINT));
                   6203:         nd_recv_intarray(rp0[i]->mul->dl,nd_wpd);
                   6204:     }
                   6205:
                   6206:     spcol = col-nred;
                   6207:     imat = (IndArray *)MALLOC(nred*sizeof(IndArray));
                   6208:     rhead = (int *)MALLOC(col*sizeof(int));
                   6209:     for ( i = 0; i < col; i++ ) rhead[i] = 0;
                   6210:
                   6211:     /* construction of index arrays */
                   6212:     for ( i = 0; i < nred; i++ ) {
                   6213:         imat[i] = nm_ind_pair_to_vect_compress(m,s0vect,col,rp0[i]);
                   6214:         rhead[imat[i]->head] = 1;
                   6215:     }
                   6216:
                   6217:     /* elimination (1st step) */
                   6218:     spmat = (int **)MALLOC(nsp*sizeof(UINT *));
                   6219:     svect = (UINT *)MALLOC(col*sizeof(UINT));
                   6220:     spsugar = (int *)ALLOCA(nsp*sizeof(UINT));
                   6221:     for ( a = sprow = 0; a < nsp; a++ ) {
                   6222:         nd_sp(m,0,sp0[a],&spol);
                   6223:         if ( !spol ) continue;
                   6224:         nd_to_vect(m,s0vect,col,spol,svect);
                   6225:         if ( m == -1 )
                   6226:             maxrs = ndv_reduce_vect_sf(m,svect,col,imat,rp0,nred);
                   6227:         else
                   6228:             maxrs = ndv_reduce_vect(m,svect,col,imat,rp0,nred);
                   6229:         for ( i = 0; i < col; i++ ) if ( svect[i] ) break;
                   6230:         if ( i < col ) {
                   6231:             spmat[sprow] = v = (UINT *)MALLOC(spcol*sizeof(UINT));
                   6232:             for ( j = k = 0; j < col; j++ )
                   6233:                 if ( !rhead[j] ) v[k++] = svect[j];
                   6234:             spsugar[sprow] = MAX(maxrs,SG(spol));
                   6235:             sprow++;
                   6236:         }
                   6237:         nd_free(spol);
                   6238:     }
                   6239:     /* elimination (2nd step) */
                   6240:     colstat = (int *)ALLOCA(spcol*sizeof(int));
                   6241:     if ( m == -1 )
                   6242:         rank = nd_gauss_elim_sf(spmat,spsugar,sprow,spcol,m,colstat);
                   6243:     else
                   6244:         rank = nd_gauss_elim_mod(spmat,spsugar,0,sprow,spcol,m,colstat);
                   6245:     nd_send_int(rank);
                   6246:     for ( i = 0; i < rank; i++ ) {
                   6247:         nf = vect_to_ndv(spmat[i],spcol,col,rhead,s0vect);
                   6248:         nd_send_ndv(nf);
                   6249:     }
                   6250:     fflush(nd_write);
1.107     noro     6251: }
                   6252:
1.113     noro     6253: int nd_gauss_elim_q(Q **mat0,int *sugar,int row,int col,int *colstat)
1.107     noro     6254: {
1.176     noro     6255:     int i,j,t,c,rank,inv;
1.157     noro     6256:     int *ci,*ri;
                   6257:     Q dn;
                   6258:     MAT m,nm;
                   6259:
                   6260:     NEWMAT(m); m->row = row; m->col = col; m->body = (pointer **)mat0;
                   6261:     rank = generic_gauss_elim(m,&nm,&dn,&ri,&ci);
                   6262:     for ( i = 0; i < row; i++ )
                   6263:         for ( j = 0; j < col; j++ )
                   6264:             mat0[i][j] = 0;
                   6265:     c = col-rank;
                   6266:     for ( i = 0; i < rank; i++ ) {
                   6267:         mat0[i][ri[i]] = dn;
                   6268:         for ( j = 0; j < c; j++ )
                   6269:             mat0[i][ci[j]] = (Q)BDY(nm)[i][j];
                   6270:     }
                   6271:     return rank;
1.76      noro     6272: }
                   6273:
1.133     noro     6274: int nd_gauss_elim_mod(int **mat0,int *sugar,ND_pairs *spactive,int row,int col,int md,int *colstat)
1.76      noro     6275: {
1.157     noro     6276:     int i,j,k,l,inv,a,rank,s;
                   6277:     unsigned int *t,*pivot,*pk;
                   6278:     unsigned int **mat;
                   6279:     ND_pairs pair;
                   6280:
                   6281:     mat = (unsigned int **)mat0;
                   6282:     for ( rank = 0, j = 0; j < col; j++ ) {
                   6283:         for ( i = rank; i < row; i++ )
                   6284:             mat[i][j] %= md;
                   6285:         for ( i = rank; i < row; i++ )
                   6286:             if ( mat[i][j] )
                   6287:                 break;
                   6288:         if ( i == row ) {
                   6289:             colstat[j] = 0;
                   6290:             continue;
                   6291:         } else
                   6292:             colstat[j] = 1;
                   6293:         if ( i != rank ) {
                   6294:             t = mat[i]; mat[i] = mat[rank]; mat[rank] = t;
                   6295:             s = sugar[i]; sugar[i] = sugar[rank]; sugar[rank] = s;
                   6296:             if ( spactive ) {
                   6297:                 pair = spactive[i]; spactive[i] = spactive[rank];
                   6298:                 spactive[rank] = pair;
                   6299:             }
                   6300:         }
                   6301:         pivot = mat[rank];
                   6302:         s = sugar[rank];
                   6303:         inv = invm(pivot[j],md);
                   6304:         for ( k = j, pk = pivot+k; k < col; k++, pk++ )
                   6305:             if ( *pk ) {
                   6306:                 if ( *pk >= (unsigned int)md )
                   6307:                     *pk %= md;
                   6308:                 DMAR(*pk,inv,0,md,*pk)
                   6309:             }
                   6310:         for ( i = rank+1; i < row; i++ ) {
                   6311:             t = mat[i];
                   6312:             if ( a = t[j] ) {
                   6313:                 sugar[i] = MAX(sugar[i],s);
                   6314:                 red_by_vect(md,t+j,pivot+j,md-a,col-j);
                   6315:             }
                   6316:         }
                   6317:         rank++;
                   6318:     }
                   6319:     for ( j = col-1, l = rank-1; j >= 0; j-- )
                   6320:         if ( colstat[j] ) {
                   6321:             pivot = mat[l];
                   6322:             s = sugar[l];
                   6323:             for ( i = 0; i < l; i++ ) {
                   6324:                 t = mat[i];
                   6325:                 t[j] %= md;
                   6326:                 if ( a = t[j] ) {
                   6327:                     sugar[i] = MAX(sugar[i],s);
                   6328:                     red_by_vect(md,t+j,pivot+j,md-a,col-j);
                   6329:                 }
                   6330:             }
                   6331:             l--;
                   6332:         }
                   6333:     for ( j = 0, l = 0; l < rank; j++ )
                   6334:         if ( colstat[j] ) {
                   6335:             t = mat[l];
                   6336:             for ( k = j; k < col; k++ )
                   6337:                 if ( t[k] >= (unsigned int)md )
                   6338:                     t[k] %= md;
                   6339:             l++;
                   6340:         }
                   6341:     return rank;
1.76      noro     6342: }
                   6343:
                   6344: int nd_gauss_elim_sf(int **mat0,int *sugar,int row,int col,int md,int *colstat)
                   6345: {
1.157     noro     6346:     int i,j,k,l,inv,a,rank,s;
                   6347:     unsigned int *t,*pivot,*pk;
                   6348:     unsigned int **mat;
                   6349:
                   6350:     mat = (unsigned int **)mat0;
                   6351:     for ( rank = 0, j = 0; j < col; j++ ) {
                   6352:         for ( i = rank; i < row; i++ )
                   6353:             if ( mat[i][j] )
                   6354:                 break;
                   6355:         if ( i == row ) {
                   6356:             colstat[j] = 0;
                   6357:             continue;
                   6358:         } else
                   6359:             colstat[j] = 1;
                   6360:         if ( i != rank ) {
                   6361:             t = mat[i]; mat[i] = mat[rank]; mat[rank] = t;
                   6362:             s = sugar[i]; sugar[i] = sugar[rank]; sugar[rank] = s;
                   6363:         }
                   6364:         pivot = mat[rank];
                   6365:         s = sugar[rank];
                   6366:         inv = _invsf(pivot[j]);
                   6367:         for ( k = j, pk = pivot+k; k < col; k++, pk++ )
                   6368:             if ( *pk )
                   6369:                 *pk = _mulsf(*pk,inv);
                   6370:         for ( i = rank+1; i < row; i++ ) {
                   6371:             t = mat[i];
                   6372:             if ( a = t[j] ) {
                   6373:                 sugar[i] = MAX(sugar[i],s);
                   6374:                 red_by_vect_sf(md,t+j,pivot+j,_chsgnsf(a),col-j);
                   6375:             }
                   6376:         }
                   6377:         rank++;
                   6378:     }
                   6379:     for ( j = col-1, l = rank-1; j >= 0; j-- )
                   6380:         if ( colstat[j] ) {
                   6381:             pivot = mat[l];
                   6382:             s = sugar[l];
                   6383:             for ( i = 0; i < l; i++ ) {
                   6384:                 t = mat[i];
                   6385:                 if ( a = t[j] ) {
                   6386:                     sugar[i] = MAX(sugar[i],s);
                   6387:                     red_by_vect_sf(md,t+j,pivot+j,_chsgnsf(a),col-j);
                   6388:                 }
                   6389:             }
                   6390:             l--;
                   6391:         }
                   6392:     return rank;
1.77      noro     6393: }
                   6394:
                   6395: int ndv_ishomo(NDV p)
                   6396: {
1.157     noro     6397:     NMV m;
                   6398:     int len,h;
1.77      noro     6399:
1.157     noro     6400:     if ( !p ) return 1;
                   6401:     len = LEN(p);
                   6402:     m = BDY(p);
                   6403:     h = TD(DL(m));
                   6404:     NMV_ADV(m);
                   6405:     for ( len--; len; len--, NMV_ADV(m) )
                   6406:         if ( TD(DL(m)) != h ) return 0;
                   6407:     return 1;
1.77      noro     6408: }
                   6409:
                   6410: void ndv_save(NDV p,int index)
                   6411: {
1.157     noro     6412:     FILE *s;
                   6413:     char name[BUFSIZ];
                   6414:     short id;
                   6415:     int nv,sugar,len,n,i,td,e,j;
                   6416:     NMV m;
                   6417:     unsigned int *dl;
                   6418:     int mpos;
                   6419:
                   6420:     sprintf(name,"%s/%d",Demand,index);
                   6421:     s = fopen(name,"w");
                   6422:     savevl(s,0);
                   6423:     if ( !p ) {
                   6424:         saveobj(s,0);
                   6425:         return;
                   6426:     }
                   6427:     id = O_DP;
                   6428:     nv = NV(p);
                   6429:     sugar = SG(p);
                   6430:     len = LEN(p);
                   6431:     write_short(s,&id); write_int(s,&nv); write_int(s,&sugar);
                   6432:     write_int(s,&len);
                   6433:
                   6434:     for ( m = BDY(p), i = 0; i < len; i++, NMV_ADV(m) ) {
                   6435:         saveobj(s,(Obj)CQ(m));
                   6436:         dl = DL(m);
                   6437:         td = TD(dl);
                   6438:         write_int(s,&td);
                   6439:         for ( j = 0; j < nv; j++ ) {
                   6440:             e = GET_EXP(dl,j);
                   6441:             write_int(s,&e);
                   6442:         }
                   6443:         if ( nd_module ) {
                   6444:             mpos = MPOS(dl); write_int(s,&mpos);
                   6445:         }
                   6446:     }
                   6447:     fclose(s);
1.77      noro     6448: }
                   6449:
                   6450: NDV ndv_load(int index)
                   6451: {
1.157     noro     6452:     FILE *s;
                   6453:     char name[BUFSIZ];
                   6454:     short id;
                   6455:     int nv,sugar,len,n,i,td,e,j;
                   6456:     NDV d;
                   6457:     NMV m0,m;
                   6458:     unsigned int *dl;
                   6459:     Obj obj;
                   6460:     int mpos;
                   6461:
                   6462:     sprintf(name,"%s/%d",Demand,index);
                   6463:     s = fopen(name,"r");
                   6464:     if ( !s ) return 0;
                   6465:
                   6466:     skipvl(s);
                   6467:     read_short(s,&id);
                   6468:     if ( !id ) return 0;
                   6469:     read_int(s,&nv);
                   6470:     read_int(s,&sugar);
                   6471:     read_int(s,&len);
                   6472:
                   6473:     m0 = m = MALLOC(len*nmv_adv);
                   6474:     for ( i = 0; i < len; i++, NMV_ADV(m) ) {
                   6475:         loadobj(s,&obj); CQ(m) = (Q)obj;
                   6476:         dl = DL(m);
                   6477:         ndl_zero(dl);
                   6478:         read_int(s,&td); TD(dl) = td;
                   6479:         for ( j = 0; j < nv; j++ ) {
                   6480:             read_int(s,&e);
                   6481:             PUT_EXP(dl,j,e);
                   6482:         }
                   6483:         if ( nd_module ) {
                   6484:             read_int(s,&mpos); MPOS(dl) = mpos;
                   6485:         }
                   6486:         if ( nd_blockmask ) ndl_weight_mask(dl);
                   6487:     }
                   6488:     fclose(s);
                   6489:     MKNDV(nv,m0,len,d);
                   6490:     SG(d) = sugar;
                   6491:     return d;
1.99      noro     6492: }
                   6493:
1.102     noro     6494: void nd_det(int mod,MAT f,P *rp)
1.99      noro     6495: {
1.157     noro     6496:     VL fv,tv;
                   6497:     int n,i,j,max,e,nvar,sgn,k0,l0,len0,len,k,l,a;
                   6498:     pointer **m;
                   6499:     Q mone;
                   6500:     P **w;
                   6501:     P mp,r;
                   6502:     NDV **dm;
                   6503:     NDV *t,*mi,*mj;
                   6504:     NDV d,s,mij,mjj;
                   6505:     ND u;
                   6506:     NMV nmv;
                   6507:     UINT *bound;
                   6508:     PGeoBucket bucket;
                   6509:     struct order_spec *ord;
                   6510:     Q dq,dt,ds;
                   6511:     N gn,qn,dn0,nm,dn;
                   6512:
                   6513:     create_order_spec(0,0,&ord);
                   6514:     nd_init_ord(ord);
                   6515:     get_vars((Obj)f,&fv);
                   6516:     if ( f->row != f->col )
                   6517:         error("nd_det : non-square matrix");
                   6518:     n = f->row;
                   6519:     m = f->body;
                   6520:     for ( nvar = 0, tv = fv; tv; tv = NEXT(tv), nvar++ );
                   6521:
                   6522:     if ( !nvar ) {
                   6523:         if ( !mod )
                   6524:             detp(CO,(P **)m,n,rp);
                   6525:         else {
                   6526:             w = (P **)almat_pointer(n,n);
                   6527:             for ( i = 0; i < n; i++ )
                   6528:                 for ( j = 0; j < n; j++ )
                   6529:                     ptomp(mod,(P)m[i][j],&w[i][j]);
                   6530:             detmp(CO,mod,w,n,&mp);
                   6531:             mptop(mp,rp);
                   6532:         }
                   6533:         return;
                   6534:     }
                   6535:
                   6536:     if ( !mod ) {
                   6537:         w = (P **)almat_pointer(n,n);
                   6538:         dq = ONE;
                   6539:         for ( i = 0; i < n; i++ ) {
                   6540:             dn0 = ONEN;
                   6541:             for ( j = 0; j < n; j++ ) {
                   6542:                 if ( !m[i][j] ) continue;
                   6543:                 lgp(m[i][j],&nm,&dn);
                   6544:                 gcdn(dn0,dn,&gn); divsn(dn0,gn,&qn); muln(qn,dn,&dn0);
                   6545:             }
                   6546:             if ( !UNIN(dn0) ) {
                   6547:                 NTOQ(dn0,1,ds);
                   6548:                 for ( j = 0; j < n; j++ )
                   6549:                     mulp(CO,(P)m[i][j],(P)ds,&w[i][j]);
                   6550:                 mulq(dq,ds,&dt); dq = dt;
                   6551:             } else
                   6552:                 for ( j = 0; j < n; j++ )
                   6553:                     w[i][j] = (P)m[i][j];
                   6554:         }
                   6555:         m = (pointer **)w;
                   6556:     }
                   6557:
1.178     noro     6558:     for ( i = 0, max = 1; i < n; i++ )
1.157     noro     6559:         for ( j = 0; j < n; j++ )
                   6560:             for ( tv = fv; tv; tv = NEXT(tv) ) {
                   6561:                 e = getdeg(tv->v,(P)m[i][j]);
                   6562:                 max = MAX(e,max);
                   6563:             }
                   6564:     nd_setup_parameters(nvar,max);
                   6565:     dm = (NDV **)almat_pointer(n,n);
1.178     noro     6566:     for ( i = 0, max = 1; i < n; i++ )
1.157     noro     6567:         for ( j = 0; j < n; j++ ) {
                   6568:             dm[i][j] = ptondv(CO,fv,m[i][j]);
                   6569:             if ( mod ) ndv_mod(mod,dm[i][j]);
                   6570:             if ( dm[i][j] && !LEN(dm[i][j]) ) dm[i][j] = 0;
                   6571:         }
                   6572:     d = ptondv(CO,fv,(P)ONE);
                   6573:     if ( mod ) ndv_mod(mod,d);
                   6574:     chsgnq(ONE,&mone);
                   6575:     for ( j = 0, sgn = 1; j < n; j++ ) {
                   6576:         if ( DP_Print ) fprintf(stderr,".",j);
                   6577:         for ( i = j; i < n && !dm[i][j]; i++ );
                   6578:         if ( i == n ) {
                   6579:             *rp = 0;
                   6580:             return;
                   6581:         }
                   6582:         k0 = i; l0 = j; len0 = LEN(dm[k0][l0]);
                   6583:         for ( k = j; k < n; k++ )
                   6584:             for ( l = j; l < n; l++ )
                   6585:                 if ( dm[k][l] && LEN(dm[k][l]) < len0 ) {
                   6586:                     k0 = k; l0 = l; len0 = LEN(dm[k][l]);
                   6587:                 }
                   6588:         if ( k0 != j ) {
                   6589:             t = dm[j]; dm[j] = dm[k0]; dm[k0] = t;
                   6590:             sgn = -sgn;
                   6591:         }
                   6592:         if ( l0 != j ) {
                   6593:             for ( k = j; k < n; k++ ) {
                   6594:                 s = dm[k][j]; dm[k][j] = dm[k][l0]; dm[k][l0] = s;
                   6595:             }
                   6596:             sgn = -sgn;
                   6597:         }
                   6598:         bound = nd_det_compute_bound(dm,n,j);
                   6599:         for ( k = 0; k < nd_nvar; k++ )
                   6600:             if ( bound[k]*2 > nd_mask0 ) break;
                   6601:         if ( k < nd_nvar )
                   6602:             nd_det_reconstruct(dm,n,j,d);
                   6603:
                   6604:         for ( i = j+1, mj = dm[j], mjj = mj[j]; i < n; i++ ) {
                   6605: /*            if ( DP_Print ) fprintf(stderr,"    i=%d\n        ",i); */
                   6606:             mi = dm[i]; mij = mi[j];
                   6607:             if ( mod )
                   6608:                 ndv_mul_c(mod,mij,mod-1);
                   6609:             else
                   6610:                 ndv_mul_c_q(mij,mone);
                   6611:             for ( k = j+1; k < n; k++ ) {
                   6612: /*                if ( DP_Print ) fprintf(stderr,"k=%d ",k); */
                   6613:                 bucket = create_pbucket();
                   6614:                 if ( mi[k] ) {
                   6615:                     nmv = BDY(mjj); len = LEN(mjj);
                   6616:                     for ( a = 0; a < len; a++, NMV_ADV(nmv) ) {
                   6617:                         u = ndv_mul_nmv_trunc(mod,nmv,mi[k],DL(BDY(d)));
                   6618:                         add_pbucket(mod,bucket,u);
                   6619:                     }
                   6620:                 }
                   6621:                 if ( mj[k] && mij ) {
                   6622:                     nmv = BDY(mij); len = LEN(mij);
                   6623:                     for ( a = 0; a < len; a++, NMV_ADV(nmv) ) {
                   6624:                         u = ndv_mul_nmv_trunc(mod,nmv,mj[k],DL(BDY(d)));
                   6625:                         add_pbucket(mod,bucket,u);
                   6626:                     }
                   6627:                 }
                   6628:                 u = nd_quo(mod,bucket,d);
                   6629:                 mi[k] = ndtondv(mod,u);
                   6630:             }
                   6631: /*            if ( DP_Print ) fprintf(stderr,"\n",k); */
                   6632:         }
                   6633:         d = mjj;
                   6634:     }
                   6635:     if ( DP_Print ) fprintf(stderr,"\n",k);
                   6636:     if ( sgn < 0 )
                   6637:         if ( mod )
                   6638:             ndv_mul_c(mod,d,mod-1);
                   6639:         else
                   6640:             ndv_mul_c_q(d,mone);
                   6641:     r = ndvtop(mod,CO,fv,d);
                   6642:     if ( !mod && !UNIQ(dq) )
                   6643:         divsp(CO,r,(P)dq,rp);
                   6644:     else
                   6645:         *rp = r;
1.99      noro     6646: }
                   6647:
1.102     noro     6648: ND ndv_mul_nmv_trunc(int mod,NMV m0,NDV p,UINT *d)
1.99      noro     6649: {
1.157     noro     6650:     NM mr,mr0;
                   6651:     NM tnm;
                   6652:     NMV m;
                   6653:     UINT *d0,*dt,*dm;
                   6654:     int c,n,td,i,c1,c2,len;
                   6655:     Q q;
                   6656:     ND r;
                   6657:
                   6658:     if ( !p ) return 0;
                   6659:     else {
                   6660:         n = NV(p); m = BDY(p); len = LEN(p);
                   6661:         d0 = DL(m0);
                   6662:         td = TD(d);
                   6663:         mr0 = 0;
                   6664:         NEWNM(tnm);
                   6665:         if ( mod ) {
                   6666:             c = CM(m0);
                   6667:             for ( i = 0; i < len; i++, NMV_ADV(m) ) {
                   6668:                 ndl_add(DL(m),d0,DL(tnm));
                   6669:                 if ( ndl_reducible(DL(tnm),d) ) {
                   6670:                     NEXTNM(mr0,mr);
                   6671:                     c1 = CM(m); DMAR(c1,c,0,mod,c2); CM(mr) = c2;
                   6672:                     ndl_copy(DL(tnm),DL(mr));
                   6673:                 }
                   6674:             }
                   6675:         } else {
                   6676:             q = CQ(m0);
                   6677:             for ( i = 0; i < len; i++, NMV_ADV(m) ) {
                   6678:                 ndl_add(DL(m),d0,DL(tnm));
                   6679:                 if ( ndl_reducible(DL(tnm),d) ) {
                   6680:                     NEXTNM(mr0,mr);
                   6681:                     mulq(CQ(m),q,&CQ(mr));
                   6682:                     ndl_copy(DL(tnm),DL(mr));
                   6683:                 }
                   6684:             }
                   6685:         }
                   6686:         if ( !mr0 )
                   6687:             return 0;
                   6688:         else {
                   6689:             NEXT(mr) = 0;
                   6690:             for ( len = 0, mr = mr0; mr; mr = NEXT(mr), len++ );
                   6691:             MKND(NV(p),mr0,len,r);
                   6692:             SG(r) = SG(p) + TD(d0);
                   6693:             return r;
                   6694:         }
                   6695:     }
1.114     noro     6696: }
                   6697:
                   6698: void nd_det_reconstruct(NDV **dm,int n,int j,NDV d)
                   6699: {
1.157     noro     6700:     int i,obpe,oadv,h,k,l;
                   6701:     static NM prev_nm_free_list;
                   6702:     EPOS oepos;
                   6703:
                   6704:     obpe = nd_bpe;
                   6705:     oadv = nmv_adv;
                   6706:     oepos = nd_epos;
                   6707:     if ( obpe < 2 ) nd_bpe = 2;
                   6708:     else if ( obpe < 3 ) nd_bpe = 3;
                   6709:     else if ( obpe < 4 ) nd_bpe = 4;
                   6710:     else if ( obpe < 5 ) nd_bpe = 5;
                   6711:     else if ( obpe < 6 ) nd_bpe = 6;
                   6712:     else if ( obpe < 8 ) nd_bpe = 8;
                   6713:     else if ( obpe < 10 ) nd_bpe = 10;
                   6714:     else if ( obpe < 16 ) nd_bpe = 16;
                   6715:     else if ( obpe < 32 ) nd_bpe = 32;
                   6716:     else error("nd_det_reconstruct : exponent too large");
                   6717:
                   6718:     nd_setup_parameters(nd_nvar,0);
                   6719:     prev_nm_free_list = _nm_free_list;
                   6720:     _nm_free_list = 0;
                   6721:     for ( k = j; k < n; k++ )
                   6722:         for (l = j; l < n; l++ )
                   6723:             ndv_realloc(dm[k][l],obpe,oadv,oepos);
                   6724:     ndv_realloc(d,obpe,oadv,oepos);
                   6725:     prev_nm_free_list = 0;
1.114     noro     6726: #if 0
1.157     noro     6727:     GC_gcollect();
1.114     noro     6728: #endif
                   6729: }
                   6730:
1.153     noro     6731: /* returns a UINT array containing degree bounds */
                   6732:
1.114     noro     6733: UINT *nd_det_compute_bound(NDV **dm,int n,int j)
                   6734: {
1.157     noro     6735:     UINT *d0,*d1,*d,*t,*r;
                   6736:     int k,l,i;
1.114     noro     6737:
1.157     noro     6738:     d0 = (UINT *)MALLOC(nd_nvar*sizeof(UINT));
                   6739:     for ( k = 0; k < nd_nvar; k++ ) d0[k] = 0;
                   6740:     for ( k = j; k < n; k++ )
                   6741:         for ( l = j; l < n; l++ )
                   6742:             if ( dm[k][l] ) {
                   6743:                 d = ndv_compute_bound(dm[k][l]);
                   6744:                 for ( i = 0; i < nd_nvar; i++ )
                   6745:                     d0[i] = MAX(d0[i],d[i]);
                   6746:             }
                   6747:     return d0;
1.117     noro     6748: }
                   6749:
                   6750: DL nd_separate_d(UINT *d,UINT *trans)
                   6751: {
1.157     noro     6752:     int n,td,i,e,j;
                   6753:     DL a;
1.117     noro     6754:
1.157     noro     6755:     ndl_zero(trans);
                   6756:     td = 0;
                   6757:     for ( i = 0; i < nd_ntrans; i++ ) {
                   6758:         e = GET_EXP(d,i);
                   6759:         PUT_EXP(trans,i,e);
                   6760:         td += MUL_WEIGHT(e,i);
                   6761:     }
                   6762:     if ( nd_ntrans+nd_nalg < nd_nvar ) {
                   6763:         /* homogenized */
                   6764:         i = nd_nvar-1;
                   6765:         e = GET_EXP(d,i);
                   6766:         PUT_EXP(trans,i,e);
                   6767:         td += MUL_WEIGHT(e,i);
                   6768:     }
                   6769:     TD(trans) = td;
                   6770:     if ( nd_blockmask) ndl_weight_mask(trans);
                   6771:     NEWDL(a,nd_nalg);
                   6772:     td = 0;
                   6773:     for ( i = 0; i < nd_nalg; i++ ) {
                   6774:         j = nd_ntrans+i;
                   6775:         e = GET_EXP(d,j);
                   6776:         a->d[i] = e;
                   6777:         td += e;
                   6778:     }
                   6779:     a->td = td;
                   6780:     return a;
1.117     noro     6781: }
                   6782:
1.118     noro     6783: int nd_monic(int mod,ND *p)
1.117     noro     6784: {
1.157     noro     6785:     UINT *trans,*t;
                   6786:     DL alg;
                   6787:     MP mp0,mp;
                   6788:     NM m,m0,m1,ma0,ma,mb,mr0,mr;
                   6789:     ND r;
                   6790:     DL dl;
                   6791:     DP nm;
                   6792:     NDV ndv;
                   6793:     DAlg inv,cd;
                   6794:     ND s,c;
                   6795:     Q l,mul;
                   6796:     N ln;
                   6797:     int n,ntrans,i,e,td,is_lc,len;
                   6798:     NumberField nf;
                   6799:     struct oEGT eg0,eg1;
                   6800:
                   6801:     if ( !(nf = get_numberfield()) )
                   6802:         error("nd_monic : current_numberfield is not set");
                   6803:
                   6804:     /* Q coef -> DAlg coef */
                   6805:     NEWNM(ma0); ma = ma0;
                   6806:     m = BDY(*p);
                   6807:     is_lc = 1;
                   6808:     while ( 1 ) {
                   6809:         NEWMP(mp0); mp = mp0;
                   6810:         mp->c = (P)CQ(m);
                   6811:         mp->dl = nd_separate_d(DL(m),DL(ma));
                   6812:         NEWNM(mb);
                   6813:         for ( m = NEXT(m); m; m = NEXT(m) ) {
                   6814:             alg = nd_separate_d(DL(m),DL(mb));
                   6815:             if ( !ndl_equal(DL(ma),DL(mb)) )
                   6816:                 break;
                   6817:             NEXTMP(mp0,mp); mp->c = (P)CQ(m); mp->dl = alg;
                   6818:         }
                   6819:         NEXT(mp) = 0;
                   6820:         MKDP(nd_nalg,mp0,nm);
                   6821:         MKDAlg(nm,ONE,cd);
                   6822:         if ( is_lc == 1 ) {
                   6823:             /* if the lc is a rational number, we have nothing to do */
                   6824:             if ( !mp0->dl->td )
                   6825:                 return 1;
                   6826:
                   6827:             get_eg(&eg0);
                   6828:             invdalg(cd,&inv);
                   6829:             get_eg(&eg1); add_eg(&eg_invdalg,&eg0,&eg1);
                   6830:             /* check the validity of inv */
                   6831:             if ( mod && !rem(NM(inv->dn),mod) )
                   6832:                 return 0;
                   6833:             CA(ma) = nf->one;
                   6834:             is_lc = 0;
                   6835:             ln = ONEN;
                   6836:         } else {
                   6837:             muldalg(cd,inv,&CA(ma));
                   6838:             lcmn(ln,NM(CA(ma)->dn),&ln);
                   6839:         }
                   6840:         if ( m ) {
                   6841:             NEXT(ma) = mb; ma = mb;
                   6842:         } else {
                   6843:             NEXT(ma) = 0;
                   6844:             break;
                   6845:         }
                   6846:     }
                   6847:     /* l = lcm(denoms) */
                   6848:     NTOQ(ln,1,l);
                   6849:     for ( mr0 = 0, m = ma0; m; m = NEXT(m) ) {
                   6850:         divq(l,CA(m)->dn,&mul);
                   6851:         for ( mp = BDY(CA(m)->nm); mp; mp = NEXT(mp) ) {
                   6852:             NEXTNM(mr0,mr);
                   6853:             mulq((Q)mp->c,mul,&CQ(mr));
                   6854:             dl = mp->dl;
                   6855:             td = TD(DL(m));
                   6856:             ndl_copy(DL(m),DL(mr));
                   6857:             for ( i = 0; i < nd_nalg; i++ ) {
                   6858:                 e = dl->d[i];
                   6859:                 PUT_EXP(DL(mr),i+nd_ntrans,e);
                   6860:                 td += MUL_WEIGHT(e,i+nd_ntrans);
                   6861:             }
1.163     noro     6862:             if ( nd_module ) MPOS(DL(mr)) = MPOS(DL(m));
1.157     noro     6863:             TD(DL(mr)) = td;
                   6864:             if ( nd_blockmask) ndl_weight_mask(DL(mr));
                   6865:         }
                   6866:     }
                   6867:     NEXT(mr) = 0;
                   6868:     for ( len = 0, mr = mr0; mr; mr = NEXT(mr), len++ );
                   6869:     MKND(NV(*p),mr0,len,r);
                   6870:     /* XXX */
                   6871:     SG(r) = SG(*p);
                   6872:     nd_free(*p);
                   6873:     *p = r;
                   6874:     return 1;
1.59      noro     6875: }
1.167     noro     6876:
                   6877: NODE reverse_node(NODE n)
                   6878: {
                   6879:     NODE t,t1;
                   6880:
                   6881:     for ( t = 0; n; n = NEXT(n) ) {
                   6882:         MKNODE(t1,BDY(n),t); t = t1;
                   6883:     }
                   6884:     return t;
                   6885: }
                   6886:
                   6887: P ndc_div(int mod,union oNDC a,union oNDC b)
                   6888: {
                   6889:     union oNDC c;
                   6890:     int inv,t;
                   6891:
                   6892:     if ( mod == -1 ) c.m = _mulsf(a.m,_invsf(b.m));
                   6893:     else if ( mod ) {
                   6894:         inv = invm(b.m,mod);
                   6895:         DMAR(a.m,inv,0,mod,t); c.m = t;
                   6896:     } else if ( nd_vc )
                   6897:        divsp(nd_vc,a.p,b.p,&c.p);
                   6898:     else
                   6899:        divq(a.z,b.z,&c.z);
                   6900:     return ndctop(mod,c);
                   6901: }
                   6902:
                   6903: P ndctop(int mod,union oNDC c)
                   6904: {
                   6905:     Q q;
                   6906:     int e;
                   6907:     GFS gfs;
                   6908:
                   6909:     if ( mod == -1 ) {
                   6910:         e = IFTOF(c.m); MKGFS(e,gfs); return (P)gfs;
                   6911:     } else if ( mod ) {
                   6912:         STOQ(c.m,q); return (P)q;
                   6913:     } else
                   6914:         return (P)c.p;
                   6915: }
                   6916:
                   6917: /* [0,0,0,cont] = p -> p/cont */
                   6918:
                   6919: void finalize_tracelist(int i,P cont)
                   6920: {
                   6921:         LIST l;
                   6922:         NODE node;
                   6923:      Q iq;
                   6924:
                   6925:         if ( !UNIQ(cont) ) {
                   6926:          node = mknode(4,0,0,0,cont);
                   6927:          MKLIST(l,node); MKNODE(node,l,nd_tracelist);
                   6928:                 nd_tracelist = node;
                   6929:         }
                   6930:      STOQ(i,iq);
                   6931:      nd_tracelist = reverse_node(nd_tracelist);
                   6932:      MKLIST(l,nd_tracelist);
                   6933:      node = mknode(2,iq,l); MKLIST(l,node);
                   6934:      MKNODE(node,l,nd_alltracelist); MKLIST(l,node);
                   6935:      nd_alltracelist = node; nd_tracelist = 0;
                   6936: }
                   6937:
                   6938: void conv_ilist(int demand,int trace,NODE g,int **indp)
                   6939: {
                   6940:     int n,i,j;
                   6941:        int *ind;
                   6942:        NODE t;
                   6943:
                   6944:     n = length(g);
                   6945:        ind = (int *)MALLOC(n*sizeof(int));
                   6946:        for ( i = 0, t = g; i < n; i++, t = NEXT(t) ) {
                   6947:                j = (long)BDY(t); ind[i] = j;
                   6948:                BDY(t) = (pointer)(demand?ndv_load(j):(trace?nd_ps_trace[j]:nd_ps[j]));
                   6949:        }
                   6950:        if ( indp ) *indp = ind;
                   6951: }
1.172     noro     6952:
                   6953: void parse_nd_option(NODE opt)
                   6954: {
1.187     noro     6955:     NODE t,p,u;
                   6956:        int i,s;
1.172     noro     6957:     char *key;
                   6958:     Obj value;
                   6959:
1.187     noro     6960:     nd_gentrace = 0; nd_gensyz = 0; nd_nora = 0; nd_gbblock = 0;
1.192     noro     6961:        nd_newelim = 0;
1.172     noro     6962:     for ( t = opt; t; t = NEXT(t) ) {
                   6963:         p = BDY((LIST)BDY(t));
                   6964:         key = BDY((STRING)BDY(p));
                   6965:         value = (Obj)BDY(NEXT(p));
                   6966:         if ( !strcmp(key,"gentrace") )
                   6967:             nd_gentrace = value?1:0;
                   6968:         else if ( !strcmp(key,"gensyz") )
                   6969:             nd_gensyz = value?1:0;
1.173     noro     6970:         else if ( !strcmp(key,"nora") )
                   6971:             nd_nora = value?1:0;
1.187     noro     6972:         else if ( !strcmp(key,"gbblock") ) {
                   6973:                        if ( !value || OID(value) != O_LIST )
                   6974:                                error("nd_* : invalid value for gbblock option");
                   6975:                        u = BDY((LIST)value);
1.189     noro     6976:             nd_gbblock = MALLOC((2*length(u)+1)*sizeof(int));
1.187     noro     6977:                        for ( i = 0; u; u = NEXT(u) ) {
                   6978:                                p = BDY((LIST)BDY(u));
                   6979:                                s = nd_gbblock[i++] = QTOS((Q)BDY(p));
                   6980:                                nd_gbblock[i++] = s+QTOS((Q)BDY(NEXT(p)))-1;
                   6981:                        }
                   6982:                        nd_gbblock[i] = -1;
1.192     noro     6983:                } else if ( !strcmp(key,"newelim") )
                   6984:             nd_newelim = value?1:0;
1.172     noro     6985:     }
                   6986: }

FreeBSD-CVSweb <freebsd-cvsweb@FreeBSD.org>