[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.173

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

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