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

1.172   ! noro        1: /* $OpenXM: OpenXM_contrib2/asir2000/engine/nd.c,v 1.171 2009/02/11 06:30:21 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.172   ! noro       52: static int nd_gentrace,nd_gensyz;
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.157     noro     2195:     n = length(f);
                   2196:     ndv_setup(m,0,f,0,1);
1.167     noro     2197:        perm = (int *)MALLOC(n*sizeof(int));
1.172   ! noro     2198:        if ( nd_gentrace ) {
1.167     noro     2199:            for ( t = nd_tracelist, i = 0; i < n; i++, t = NEXT(t) )
                   2200:                    perm[i] = QTOS((Q)ARG1(BDY((LIST)BDY(t))));
                   2201:        }
1.157     noro     2202:     for ( i = 0; i < n; ) {
1.172   ! noro     2203:         if ( nd_gentrace ) {
1.167     noro     2204:             /* Trace = [1,index,1,1] */
                   2205:             STOQ(i,iq); node = mknode(4,ONE,iq,ONE,ONE);
                   2206:             MKLIST(l,node); MKNODE(nd_tracelist,l,0);
                   2207:         }
1.157     noro     2208:         g = ndvtond(m,nd_ps[i]);
                   2209:         g = nd_separate_head(g,&head);
                   2210:         stat = nd_nf(m,head,g,nd_ps,1,0,&nf);
                   2211:         if ( !stat )
                   2212:             nd_reconstruct(0,0);
                   2213:         else {
                   2214:             if ( DP_Print ) { printf("."); fflush(stdout); }
                   2215:             ndv_free(nd_ps[i]);
1.167     noro     2216:             hc = HCU(nf); nd_removecont(m,nf);
1.172   ! noro     2217:             if ( nd_gentrace ) {
1.167     noro     2218:                                for ( t = nd_tracelist; t; t = NEXT(t) ) {
                   2219:                     jq = ARG1(BDY((LIST)BDY(t))); j = QTOS(jq);
                   2220:                     STOQ(perm[j],jq); ARG1(BDY((LIST)BDY(t))) = jq;
                   2221:                 }
                   2222:                 cont = ndc_div(m,hc,HCU(nf));
                   2223:                 finalize_tracelist(perm[i],cont);
                   2224:             }
1.157     noro     2225:             nd_ps[i] = ndtondv(m,nf); nd_free(nf);
                   2226:             nd_bound[i] = ndv_compute_bound(nd_ps[i]);
                   2227:             i++;
                   2228:         }
                   2229:     }
                   2230:     if ( DP_Print ) { printf("\n"); }
                   2231:     for ( a0 = 0, i = 0; i < n; i++ ) {
                   2232:         NEXTNODE(a0,a);
1.172   ! noro     2233:                if ( !nd_gentrace ) BDY(a) = (pointer)nd_ps[i];
1.167     noro     2234:                else {
                   2235:                        for ( j = 0; j < n; j++ ) if ( perm[j] == i ) break;
                   2236:                        BDY(a) = (pointer)nd_ps[j];
                   2237:                }
1.157     noro     2238:     }
                   2239:     NEXT(a) = 0;
                   2240:     return a0;
1.23      noro     2241: }
                   2242:
1.168     noro     2243: ND_pairs update_pairs( ND_pairs d, NODE /* of index */ g, int t, int gensyz)
1.1       noro     2244: {
1.157     noro     2245:     ND_pairs d1,nd,cur,head,prev,remove;
1.1       noro     2246:
1.157     noro     2247:     if ( !g ) return d;
1.168     noro     2248:        /* for testing */
1.172   ! noro     2249:        if ( gensyz && nd_gensyz == 2 ) {
1.168     noro     2250:        d1 = nd_newpairs(g,t);
                   2251:        if ( !d )
                   2252:                 return d1;
                   2253:        else {
                   2254:                 nd = d;
                   2255:                 while ( NEXT(nd) ) nd = NEXT(nd);
                   2256:                 NEXT(nd) = d1;
                   2257:                 return d;
                   2258:        }
                   2259:        }
1.157     noro     2260:     d = crit_B(d,t);
                   2261:     d1 = nd_newpairs(g,t);
                   2262:     d1 = crit_M(d1);
                   2263:     d1 = crit_F(d1);
1.168     noro     2264:     if ( gensyz || do_weyl )
1.157     noro     2265:         head = d1;
                   2266:     else {
                   2267:         prev = 0; cur = head = d1;
                   2268:         while ( cur ) {
                   2269:             if ( crit_2( cur->i1,cur->i2 ) ) {
                   2270:                 remove = cur;
                   2271:                 if ( !prev ) head = cur = NEXT(cur);
                   2272:                 else cur = NEXT(prev) = NEXT(cur);
                   2273:                 FREENDP(remove);
                   2274:             } else {
                   2275:                 prev = cur; cur = NEXT(cur);
                   2276:             }
                   2277:         }
                   2278:     }
                   2279:     if ( !d )
                   2280:         return head;
                   2281:     else {
                   2282:         nd = d;
                   2283:         while ( NEXT(nd) ) nd = NEXT(nd);
                   2284:         NEXT(nd) = head;
                   2285:         return d;
                   2286:     }
1.1       noro     2287: }
                   2288:
1.157     noro     2289:
1.1       noro     2290: ND_pairs nd_newpairs( NODE g, int t )
                   2291: {
1.157     noro     2292:     NODE h;
                   2293:     UINT *dl;
                   2294:     int ts,s;
                   2295:     ND_pairs r,r0;
                   2296:
                   2297:     dl = DL(nd_psh[t]);
                   2298:     ts = SG(nd_psh[t]) - TD(dl);
                   2299:     for ( r0 = 0, h = g; h; h = NEXT(h) ) {
1.159     noro     2300:         if ( nd_module && (MPOS(DL(nd_psh[(long)BDY(h)])) != MPOS(dl)) )
1.157     noro     2301:                 continue;
                   2302:         NEXTND_pairs(r0,r);
1.159     noro     2303:         r->i1 = (long)BDY(h);
1.157     noro     2304:         r->i2 = t;
                   2305:         ndl_lcm(DL(nd_psh[r->i1]),dl,r->lcm);
                   2306:         s = SG(nd_psh[r->i1])-TD(DL(nd_psh[r->i1]));
                   2307:         SG(r) = MAX(s,ts) + TD(LCM(r));
                   2308:     }
                   2309:     if ( r0 ) NEXT(r) = 0;
                   2310:     return r0;
1.1       noro     2311: }
                   2312:
1.157     noro     2313: /* kokokara */
                   2314:
1.1       noro     2315: ND_pairs crit_B( ND_pairs d, int s )
                   2316: {
1.157     noro     2317:     ND_pairs cur,head,prev,remove;
                   2318:     UINT *t,*tl,*lcm;
                   2319:     int td,tdl;
                   2320:
                   2321:     if ( !d ) return 0;
                   2322:     t = DL(nd_psh[s]);
                   2323:     prev = 0;
                   2324:     head = cur = d;
                   2325:     lcm = (UINT *)ALLOCA(nd_wpd*sizeof(UINT));
                   2326:     while ( cur ) {
                   2327:         tl = cur->lcm;
1.163     noro     2328:         if ( ndl_reducible(tl,t) ) {
                   2329:             ndl_lcm(DL(nd_psh[cur->i1]),t,lcm);
1.167     noro     2330:             if ( !ndl_equal(lcm,tl) ) {
                   2331:                 ndl_lcm(DL(nd_psh[cur->i2]),t,lcm);
                   2332:                 if (!ndl_equal(lcm,tl)) {
                   2333:                     remove = cur;
                   2334:                     if ( !prev ) {
                   2335:                         head = cur = NEXT(cur);
                   2336:                     } else {
                   2337:                         cur = NEXT(prev) = NEXT(cur);
                   2338:                     }
                   2339:                     FREENDP(remove);
                   2340:                 } else {
                   2341:                     prev = cur; cur = NEXT(cur);
                   2342:                 }
                   2343:             } else {
                   2344:                     prev = cur; cur = NEXT(cur);
                   2345:             }
1.157     noro     2346:         } else {
                   2347:             prev = cur; cur = NEXT(cur);
                   2348:         }
                   2349:     }
                   2350:     return head;
1.1       noro     2351: }
                   2352:
                   2353: ND_pairs crit_M( ND_pairs d1 )
                   2354: {
1.157     noro     2355:     ND_pairs e,d2,d3,dd,p;
                   2356:     UINT *id,*jd;
1.1       noro     2357:
1.157     noro     2358:     if ( !d1 ) return d1;
                   2359:     for ( dd = 0, e = d1; e; e = d3 ) {
                   2360:         if ( !(d2 = NEXT(e)) ) {
                   2361:             NEXT(e) = dd;
                   2362:             return e;
                   2363:         }
                   2364:         id = LCM(e);
                   2365:         for ( d3 = 0; d2; d2 = p ) {
                   2366:             p = NEXT(d2);
                   2367:             jd = LCM(d2);
                   2368:             if ( ndl_equal(jd,id) )
                   2369:                 ;
                   2370:             else if ( TD(jd) > TD(id) )
                   2371:                 if ( ndl_reducible(jd,id) ) continue;
                   2372:                 else ;
                   2373:             else if ( ndl_reducible(id,jd) ) goto delit;
                   2374:             NEXT(d2) = d3;
                   2375:             d3 = d2;
                   2376:         }
                   2377:         NEXT(e) = dd;
                   2378:         dd = e;
                   2379:         continue;
                   2380:         /**/
                   2381:     delit:    NEXT(d2) = d3;
                   2382:         d3 = d2;
                   2383:         for ( ; p; p = d2 ) {
                   2384:             d2 = NEXT(p);
                   2385:             NEXT(p) = d3;
                   2386:             d3 = p;
                   2387:         }
                   2388:         FREENDP(e);
                   2389:     }
                   2390:     return dd;
1.1       noro     2391: }
                   2392:
                   2393: ND_pairs crit_F( ND_pairs d1 )
                   2394: {
1.157     noro     2395:     ND_pairs rest, head,remove;
                   2396:     ND_pairs last, p, r, w;
                   2397:     int s;
                   2398:
                   2399:     if ( !d1 ) return d1;
                   2400:     for ( head = last = 0, p = d1; NEXT(p); ) {
                   2401:         r = w = equivalent_pairs(p,&rest);
                   2402:         s = SG(r);
                   2403:         w = NEXT(w);
                   2404:         while ( w ) {
                   2405:             if ( crit_2(w->i1,w->i2) ) {
                   2406:                 r = w;
                   2407:                 w = NEXT(w);
                   2408:                 while ( w ) {
                   2409:                     remove = w;
                   2410:                     w = NEXT(w);
                   2411:                     FREENDP(remove);
                   2412:                 }
                   2413:                 break;
                   2414:             } else if ( SG(w) < s ) {
                   2415:                 FREENDP(r);
                   2416:                 r = w;
                   2417:                 s = SG(r);
                   2418:                 w = NEXT(w);
                   2419:             } else {
                   2420:                 remove = w;
                   2421:                 w = NEXT(w);
                   2422:                 FREENDP(remove);
                   2423:             }
                   2424:         }
                   2425:         if ( last ) NEXT(last) = r;
                   2426:         else head = r;
                   2427:         NEXT(last = r) = 0;
                   2428:         p = rest;
                   2429:         if ( !p ) return head;
                   2430:     }
                   2431:     if ( !last ) return p;
                   2432:     NEXT(last) = p;
                   2433:     return head;
1.1       noro     2434: }
                   2435:
                   2436: int crit_2( int dp1, int dp2 )
                   2437: {
1.157     noro     2438:     return ndl_disjoint(DL(nd_psh[dp1]),DL(nd_psh[dp2]));
1.1       noro     2439: }
                   2440:
1.40      noro     2441: ND_pairs equivalent_pairs( ND_pairs d1, ND_pairs *prest )
1.1       noro     2442: {
1.157     noro     2443:     ND_pairs w,p,r,s;
                   2444:     UINT *d;
1.1       noro     2445:
1.157     noro     2446:     w = d1;
                   2447:     d = LCM(w);
                   2448:     s = NEXT(w);
                   2449:     NEXT(w) = 0;
                   2450:     for ( r = 0; s; s = p ) {
                   2451:         p = NEXT(s);
                   2452:         if ( ndl_equal(d,LCM(s)) ) {
                   2453:             NEXT(s) = w; w = s;
                   2454:         } else {
                   2455:             NEXT(s) = r; r = s;
                   2456:         }
                   2457:     }
                   2458:     *prest = r;
                   2459:     return w;
1.1       noro     2460: }
                   2461:
                   2462: NODE update_base(NODE nd,int ndp)
                   2463: {
1.157     noro     2464:     UINT *dl, *dln;
                   2465:     NODE last, p, head;
1.1       noro     2466:
1.157     noro     2467:     dl = DL(nd_psh[ndp]);
                   2468:     for ( head = last = 0, p = nd; p; ) {
1.159     noro     2469:         dln = DL(nd_psh[(long)BDY(p)]);
1.157     noro     2470:         if ( ndl_reducible( dln, dl ) ) {
                   2471:             p = NEXT(p);
                   2472:             if ( last ) NEXT(last) = p;
                   2473:         } else {
                   2474:             if ( !last ) head = p;
                   2475:             p = NEXT(last = p);
                   2476:         }
                   2477:     }
                   2478:     head = append_one(head,ndp);
                   2479:     return head;
1.1       noro     2480: }
                   2481:
                   2482: ND_pairs nd_minp( ND_pairs d, ND_pairs *prest )
                   2483: {
1.157     noro     2484:     ND_pairs m,ml,p,l;
                   2485:     UINT *lcm;
                   2486:     int s,td,len,tlen,c,c1;
                   2487:
                   2488:     if ( !(p = NEXT(m = d)) ) {
                   2489:         *prest = p;
                   2490:         NEXT(m) = 0;
                   2491:         return m;
                   2492:     }
                   2493:     s = SG(m);
                   2494:     if ( !NoSugar ) {
                   2495:         for ( ml = 0, l = m; p; p = NEXT(l = p) )
                   2496:             if ( (SG(p) < s)
                   2497:                 || ((SG(p) == s) && (DL_COMPARE(LCM(p),LCM(m)) < 0)) ) {
                   2498:                 ml = l; m = p; s = SG(m);
                   2499:             }
                   2500:     } else {
                   2501:         for ( ml = 0, l = m; p; p = NEXT(l = p) )
                   2502:             if ( DL_COMPARE(LCM(p),LCM(m)) < 0 ) {
                   2503:                 ml = l; m = p; s = SG(m);
                   2504:             }
                   2505:     }
                   2506:     if ( !ml ) *prest = NEXT(m);
                   2507:     else {
                   2508:         NEXT(ml) = NEXT(m);
                   2509:         *prest = d;
                   2510:     }
                   2511:     NEXT(m) = 0;
                   2512:     return m;
1.1       noro     2513: }
                   2514:
1.63      noro     2515: ND_pairs nd_minsugarp( ND_pairs d, ND_pairs *prest )
                   2516: {
1.157     noro     2517:     int msugar,i;
                   2518:     ND_pairs t,dm0,dm,dr0,dr;
1.63      noro     2519:
1.157     noro     2520:     for ( msugar = SG(d), t = NEXT(d); t; t = NEXT(t) )
                   2521:         if ( SG(t) < msugar ) msugar = SG(t);
                   2522:     dm0 = 0; dr0 = 0;
                   2523:     for ( i = 0, t = d; t; t = NEXT(t) )
                   2524:         if ( i < nd_f4_nsp && SG(t) == msugar ) {
                   2525:             if ( dm0 ) NEXT(dm) = t;
                   2526:             else dm0 = t;
                   2527:             dm = t;
                   2528:             i++;
                   2529:         } else {
                   2530:             if ( dr0 ) NEXT(dr) = t;
                   2531:             else dr0 = t;
                   2532:             dr = t;
                   2533:         }
                   2534:     NEXT(dm) = 0;
                   2535:     if ( dr0 ) NEXT(dr) = 0;
                   2536:     *prest = dr0;
                   2537:     return dm0;
1.63      noro     2538: }
                   2539:
1.77      noro     2540: int ndv_newps(int m,NDV a,NDV aq)
1.1       noro     2541: {
1.157     noro     2542:     int len;
                   2543:     RHist r;
                   2544:     NDV b;
1.167     noro     2545:     NODE tn;
                   2546:     LIST l;
                   2547:     Q iq;
1.157     noro     2548:
                   2549:     if ( nd_psn == nd_pslen ) {
                   2550:         nd_pslen *= 2;
                   2551:         nd_ps = (NDV *)REALLOC((char *)nd_ps,nd_pslen*sizeof(NDV));
                   2552:         nd_ps_trace = (NDV *)REALLOC((char *)nd_ps_trace,nd_pslen*sizeof(NDV));
                   2553:         nd_psh = (RHist *)REALLOC((char *)nd_psh,nd_pslen*sizeof(RHist));
                   2554:         nd_bound = (UINT **)
                   2555:             REALLOC((char *)nd_bound,nd_pslen*sizeof(UINT *));
                   2556:     }
                   2557:     NEWRHist(r); nd_psh[nd_psn] = r;
                   2558:     nd_ps[nd_psn] = a;
                   2559:     if ( aq ) {
                   2560:         nd_ps_trace[nd_psn] = aq;
                   2561:         register_hcf(aq);
                   2562:         nd_bound[nd_psn] = ndv_compute_bound(aq);
                   2563:         SG(r) = SG(aq); ndl_copy(HDL(aq),DL(r));
                   2564:     } else {
                   2565:         if ( !m ) register_hcf(a);
                   2566:         nd_bound[nd_psn] = ndv_compute_bound(a);
                   2567:         SG(r) = SG(a); ndl_copy(HDL(a),DL(r));
                   2568:     }
                   2569:     if ( nd_demand ) {
                   2570:         if ( aq ) {
                   2571:             ndv_save(nd_ps_trace[nd_psn],nd_psn);
                   2572:             nd_ps_trace[nd_psn] = 0;
                   2573:         } else {
                   2574:             ndv_save(nd_ps[nd_psn],nd_psn);
                   2575:             nd_ps[nd_psn] = 0;
                   2576:         }
                   2577:     }
1.172   ! noro     2578:     if ( nd_gentrace ) {
1.167     noro     2579:         /* reverse the tracelist and append it to alltracelist */
                   2580:         nd_tracelist = reverse_node(nd_tracelist); MKLIST(l,nd_tracelist);
                   2581:         STOQ(nd_psn,iq); tn = mknode(2,iq,l); MKLIST(l,tn);
                   2582:         MKNODE(tn,l,nd_alltracelist); nd_alltracelist = tn; nd_tracelist = 0;
                   2583:     }
1.157     noro     2584:     return nd_psn++;
1.1       noro     2585: }
                   2586:
1.167     noro     2587: /* nd_tracelist = [[0,index,div],...,[nd_psn-1,index,div]] */
                   2588:
1.151     noro     2589: void ndv_setup(int mod,int trace,NODE f,int dont_sort,int dont_removecont)
1.1       noro     2590: {
1.157     noro     2591:     int i,j,td,len,max;
1.167     noro     2592:     NODE s,s0,f0,tn;
1.157     noro     2593:     UINT *d;
                   2594:     RHist r;
1.167     noro     2595:     NDVI w;
1.157     noro     2596:     NDV a,am;
1.167     noro     2597:     union oNDC hc;
                   2598:     NODE node;
                   2599:     P hcp;
                   2600:     Q iq,jq,hcq;
                   2601:     LIST l;
1.157     noro     2602:
                   2603:     nd_found = 0; nd_notfirst = 0; nd_create = 0;
1.167     noro     2604:     /* initialize the tracelist */
                   2605:     nd_tracelist = 0;
1.157     noro     2606:
                   2607:     for ( nd_psn = 0, s = f; s; s = NEXT(s) ) if ( BDY(s) ) nd_psn++;
1.167     noro     2608:     w = (NDVI)ALLOCA(nd_psn*sizeof(struct oNDVI));
                   2609:     for ( i = j = 0, s = f; s; s = NEXT(s), j++ )
                   2610:         if ( BDY(s) ) { w[i].p = BDY(s); w[i].i = j; i++; }
1.157     noro     2611:     if ( !dont_sort ) {
                   2612:         /* XXX heuristic */
                   2613:         if ( !nd_ord->id && (nd_ord->ord.simple<2) )
1.167     noro     2614:             qsort(w,nd_psn,sizeof(struct oNDVI),
                   2615:                 (int (*)(const void *,const void *))ndvi_compare_rev);
1.157     noro     2616:         else
1.167     noro     2617:             qsort(w,nd_psn,sizeof(struct oNDVI),
                   2618:                 (int (*)(const void *,const void *))ndvi_compare);
1.157     noro     2619:     }
                   2620:     nd_pslen = 2*nd_psn;
                   2621:     nd_ps = (NDV *)MALLOC(nd_pslen*sizeof(NDV));
                   2622:     nd_ps_trace = (NDV *)MALLOC(nd_pslen*sizeof(NDV));
                   2623:     nd_psh = (RHist *)MALLOC(nd_pslen*sizeof(RHist));
                   2624:     nd_bound = (UINT **)MALLOC(nd_pslen*sizeof(UINT *));
                   2625:     nd_hcf = 0;
                   2626:
                   2627:     if ( trace && nd_vc )
                   2628:         makesubst(nd_vc,&nd_subst);
                   2629:     else
                   2630:         nd_subst = 0;
                   2631:
                   2632:     if ( !nd_red )
                   2633:         nd_red = (RHist *)MALLOC(REDTAB_LEN*sizeof(RHist));
                   2634:     for ( i = 0; i < REDTAB_LEN; i++ ) nd_red[i] = 0;
                   2635:     for ( i = 0; i < nd_psn; i++ ) {
1.167     noro     2636:         hc = HCU(w[i].p);
1.157     noro     2637:         if ( trace ) {
1.167     noro     2638:             a = nd_ps_trace[i] = ndv_dup(0,w[i].p);
1.157     noro     2639:             if ( !dont_removecont) ndv_removecont(0,a);
                   2640:             register_hcf(a);
                   2641:             am = nd_ps[i] = ndv_dup(mod,a);
                   2642:             ndv_mod(mod,am);
                   2643:             ndv_removecont(mod,am);
                   2644:         } else {
1.167     noro     2645:             a = nd_ps[i] = ndv_dup(mod,w[i].p);
1.157     noro     2646:             if ( mod || !dont_removecont ) ndv_removecont(mod,a);
                   2647:             if ( !mod ) register_hcf(a);
                   2648:         }
1.172   ! noro     2649:         if ( nd_gentrace ) {
1.167     noro     2650:             STOQ(i,iq); STOQ(w[i].i,jq); node = mknode(3,iq,jq,ONE);
1.168     noro     2651:                        if ( !dont_removecont )
                   2652:                 ARG2(node) = (pointer)ndc_div(trace?0:mod,hc,HCU(a));
1.167     noro     2653:             MKLIST(l,node); NEXTNODE(nd_tracelist,tn); BDY(tn) = l;
                   2654:         }
1.157     noro     2655:         NEWRHist(r); SG(r) = HTD(a); ndl_copy(HDL(a),DL(r));
                   2656:         nd_bound[i] = ndv_compute_bound(a);
                   2657:         nd_psh[i] = r;
                   2658:         if ( nd_demand ) {
                   2659:             if ( trace ) {
                   2660:                 ndv_save(nd_ps_trace[i],i);
                   2661:                 nd_ps_trace[i] = 0;
                   2662:             } else {
                   2663:                 ndv_save(nd_ps[i],i);
                   2664:                 nd_ps[i] = 0;
                   2665:             }
                   2666:         }
                   2667:     }
1.172   ! noro     2668:     if ( nd_gentrace && nd_tracelist ) NEXT(tn) = 0;
1.20      noro     2669: }
                   2670:
1.119     noro     2671: struct order_spec *append_block(struct order_spec *spec,
                   2672:     int nv,int nalg,int ord);
                   2673:
1.121     noro     2674: extern VECT current_dl_weight_vector_obj;
                   2675: static VECT prev_weight_vector_obj;
                   2676:
1.120     noro     2677: void preprocess_algcoef(VL vv,VL av,struct order_spec *ord,LIST f,
1.157     noro     2678:     struct order_spec **ord1p,LIST *f1p,NODE *alistp)
1.120     noro     2679: {
1.157     noro     2680:     NODE alist,t,s,r0,r,arg;
                   2681:     VL tv;
                   2682:     P poly;
                   2683:     DP d;
                   2684:     Alg alpha,dp;
                   2685:     DAlg inv,da,hc;
                   2686:     MP m;
                   2687:     int i,nvar,nalg,n;
                   2688:     NumberField nf;
                   2689:     LIST f1,f2;
                   2690:     struct order_spec *current_spec;
                   2691:     VECT obj,obj0;
                   2692:     Obj tmp;
                   2693:
                   2694:     for ( nvar = 0, tv = vv; tv; tv = NEXT(tv), nvar++);
                   2695:     for ( nalg = 0, tv = av; tv; tv = NEXT(tv), nalg++);
                   2696:
                   2697:     for ( alist = 0, tv = av; tv; tv = NEXT(tv) ) {
                   2698:         NEXTNODE(alist,t); MKV(tv->v,poly);
                   2699:         MKAlg(poly,alpha); BDY(t) = (pointer)alpha;
                   2700:         tv->v = tv->v->priv;
                   2701:     }
                   2702:     NEXT(t) = 0;
                   2703:
1.167     noro     2704:     /* simplification, making polynomials monic */
1.157     noro     2705:     setfield_dalg(alist);
                   2706:     obj_algtodalg(f,&f1);
                   2707:     for ( t = BDY(f); t; t = NEXT(t) ) {
                   2708:         initd(ord); ptod(vv,vv,(P)BDY(t),&d);
                   2709:         hc = (DAlg)BDY(d)->c;
                   2710:         if ( NID(hc) == N_DA ) {
                   2711:             invdalg(hc,&inv);
                   2712:             for ( m = BDY(d); m; m = NEXT(m) ) {
                   2713:                 muldalg(inv,(DAlg)m->c,&da); m->c = (P)da;
                   2714:             }
                   2715:         }
                   2716:         initd(ord); dtop(vv,vv,d,&poly); BDY(f) = (pointer)poly;
                   2717:     }
                   2718:     obj_dalgtoalg(f1,&f);
                   2719:
                   2720:     /* append alg vars to the var list */
                   2721:     for ( tv = vv; NEXT(tv); tv = NEXT(tv) );
                   2722:     NEXT(tv) = av;
                   2723:
                   2724:     /* append a block to ord */
                   2725:     *ord1p = append_block(ord,nvar,nalg,2);
                   2726:
                   2727:     /* create generator list */
                   2728:     nf = get_numberfield();
                   2729:     for ( i = nalg-1, t = BDY(f); i >= 0; i-- ) {
                   2730:         MKAlg(nf->defpoly[i],dp);
                   2731:         MKNODE(s,dp,t); t = s;
                   2732:     }
                   2733:     MKLIST(f1,t);
                   2734:     *alistp = alist;
                   2735:     algobjtorat(f1,f1p);
                   2736:
                   2737:     /* creating a new weight vector */
                   2738:     prev_weight_vector_obj = obj0 = current_dl_weight_vector_obj;
                   2739:     n = nvar+nalg+1;
                   2740:     MKVECT(obj,n);
                   2741:     if ( obj0 && obj0->len == nvar )
                   2742:         for ( i = 0; i < nvar; i++ ) BDY(obj)[i] = BDY(obj0)[i];
                   2743:     else
                   2744:         for ( i = 0; i < nvar; i++ ) BDY(obj)[i] = (pointer)ONE;
                   2745:     for ( i = 0; i < nalg; i++ ) BDY(obj)[i+nvar] = 0;
                   2746:     BDY(obj)[n-1] = (pointer)ONE;
                   2747:     arg = mknode(1,obj);
                   2748:     Pdp_set_weight(arg,&tmp);
1.121     noro     2749: }
                   2750:
                   2751: NODE postprocess_algcoef(VL av,NODE alist,NODE r)
                   2752: {
1.157     noro     2753:     NODE s,t,u0,u;
                   2754:     P p;
                   2755:     VL tv;
                   2756:     Obj obj,tmp;
                   2757:     NODE arg;
                   2758:
                   2759:     u0 = 0;
                   2760:     for ( t = r; t; t = NEXT(t) ) {
                   2761:         p = (P)BDY(t);
                   2762:         for ( tv = av, s = alist; tv; tv = NEXT(tv), s = NEXT(s) ) {
                   2763:             substr(CO,0,(Obj)p,tv->v,(Obj)BDY(s),&obj); p = (P)obj;
                   2764:         }
                   2765:         if ( OID(p) == O_P || (OID(p) == O_N && NID((Num)p) != N_A) ) {
                   2766:             NEXTNODE(u0,u);
                   2767:             BDY(u) = (pointer)p;
                   2768:         }
                   2769:     }
                   2770:     arg = mknode(1,prev_weight_vector_obj);
                   2771:     Pdp_set_weight(arg,&tmp);
1.121     noro     2772:
1.157     noro     2773:     return u0;
1.120     noro     2774: }
                   2775:
1.63      noro     2776: void nd_gr(LIST f,LIST v,int m,int f4,struct order_spec *ord,LIST *rp)
1.1       noro     2777: {
1.157     noro     2778:     VL tv,fv,vv,vc,av;
                   2779:     NODE fd,fd0,r,r0,t,x,s,xx,alist;
                   2780:     int e,max,nvar,i;
                   2781:     NDV b;
                   2782:     int ishomo,nalg,mrank,trank;
                   2783:     Alg alpha,dp;
                   2784:     P p,zp;
                   2785:     Q dmy;
                   2786:     LIST f1,f2,zpl;
                   2787:     Obj obj;
                   2788:     NumberField nf;
                   2789:     struct order_spec *ord1;
1.170     noro     2790:     NODE tr,tl1,tl2,tl3,tl4;
                   2791:     LIST l1,l2,l3,l4,l5;
1.167     noro     2792:        int j;
                   2793:        Q jq;
                   2794:     int *perm;
1.170     noro     2795:     EPOS oepos;
                   2796:     int obpe,oadv,ompos;
1.1       noro     2797:
1.172   ! noro     2798:     nd_module;
1.157     noro     2799:     if ( !m && Demand ) nd_demand = 1;
                   2800:     else nd_demand = 0;
1.172   ! noro     2801:     parse_nd_option(current_option);
1.78      noro     2802:
1.157     noro     2803:     if ( DP_Multiple )
                   2804:         nd_scale = ((double)DP_Multiple)/(double)(Denominator?Denominator:1);
1.103     noro     2805: #if 0
1.157     noro     2806:     ndv_alloc = 0;
1.103     noro     2807: #endif
1.157     noro     2808:     get_vars((Obj)f,&fv); pltovl(v,&vv); vlminus(fv,vv,&nd_vc);
                   2809:     for ( nvar = 0, tv = vv; tv; tv = NEXT(tv), nvar++ );
                   2810:     switch ( ord->id ) {
                   2811:         case 1:
                   2812:             if ( ord->nv != nvar )
                   2813:                 error("nd_{gr,f4} : invalid order specification");
                   2814:             break;
                   2815:         default:
                   2816:             break;
                   2817:     }
                   2818:     nd_nalg = 0;
                   2819:     av = 0;
                   2820:     if ( !m ) {
                   2821:         get_algtree((Obj)f,&av);
                   2822:         for ( nalg = 0, tv = av; tv; tv = NEXT(tv), nalg++ );
                   2823:         nd_ntrans = nvar;
                   2824:         nd_nalg = nalg;
                   2825:         /* #i -> t#i */
                   2826:         if ( nalg ) {
                   2827:             preprocess_algcoef(vv,av,ord,f,&ord1,&f1,&alist);
                   2828:             ord = ord1;
                   2829:             f = f1;
                   2830:         }
                   2831:         nvar += nalg;
                   2832:     }
                   2833:     nd_init_ord(ord);
                   2834:     mrank = 0;
                   2835:     for ( t = BDY(f), max = 0; t; t = NEXT(t) )
                   2836:         for ( tv = vv; tv; tv = NEXT(tv) ) {
                   2837:             if ( nd_module ) {
                   2838:                 s = BDY((LIST)BDY(t));
                   2839:                 trank = length(s);
                   2840:                 mrank = MAX(mrank,trank);
                   2841:                 for ( ; s; s = NEXT(s) ) {
                   2842:                     e = getdeg(tv->v,(P)BDY(s));
                   2843:                     max = MAX(e,max);
                   2844:                 }
                   2845:             } else {
                   2846:                 e = getdeg(tv->v,(P)BDY(t));
                   2847:                 max = MAX(e,max);
                   2848:             }
                   2849:         }
                   2850:     nd_setup_parameters(nvar,max);
1.170     noro     2851:     obpe = nd_bpe; oadv = nmv_adv; oepos = nd_epos; ompos = nd_mpos;
1.157     noro     2852:     ishomo = 1;
                   2853:     for ( fd0 = 0, t = BDY(f); t; t = NEXT(t) ) {
1.167     noro     2854:         if ( nd_module ) {
1.172   ! noro     2855:                        if ( !m && !nd_gentrace ) pltozpl((LIST)BDY(t),&dmy,&zpl);
1.167     noro     2856:                        else zpl = (LIST)BDY(t);
1.157     noro     2857:             b = (pointer)pltondv(CO,vv,zpl);
                   2858:         } else {
1.172   ! noro     2859:                        if ( !m && !nd_gentrace ) ptozp((P)BDY(t),1,&dmy,&zp);
1.167     noro     2860:                        else zp = (P)BDY(t);
1.157     noro     2861:             b = (pointer)ptondv(CO,vv,zp);
1.167     noro     2862:         }
1.157     noro     2863:         if ( ishomo )
                   2864:             ishomo = ishomo && ndv_ishomo(b);
                   2865:         if ( m ) ndv_mod(m,b);
                   2866:         if ( b ) { NEXTNODE(fd0,fd); BDY(fd) = (pointer)b; }
                   2867:     }
                   2868:     if ( fd0 ) NEXT(fd) = 0;
                   2869:     ndv_setup(m,0,fd0,0,0);
1.172   ! noro     2870:     if ( nd_gentrace ) {
1.167     noro     2871:         MKLIST(l1,nd_tracelist); MKNODE(nd_alltracelist,l1,0);
                   2872:     }
1.168     noro     2873:     x = f4?nd_f4(m,&perm):nd_gb(m,ishomo,0,0,&perm);
1.157     noro     2874:     nd_demand = 0;
1.167     noro     2875:     x = ndv_reducebase(x,perm);
1.172   ! noro     2876:     if ( nd_gentrace ) { tl1 = nd_alltracelist; nd_alltracelist = 0; }
1.157     noro     2877:     x = ndv_reduceall(m,x);
1.172   ! noro     2878:     if ( nd_gentrace ) {
1.170     noro     2879:         tl2 = nd_alltracelist; nd_alltracelist = 0;
                   2880:         ndv_check_membership(m,fd0,obpe,oadv,oepos,x);
1.172   ! noro     2881:         if ( nd_gentrace ) {
1.170     noro     2882:             tl3 = nd_alltracelist; nd_alltracelist = 0;
                   2883:         } else tl3 = 0;
1.172   ! noro     2884:         nd_gb(m,0,1,nd_gensyz?1:0,0)!=0;
        !          2885:         if ( nd_gentrace && nd_gensyz ) {
1.170     noro     2886:             tl4 = nd_alltracelist; nd_alltracelist = 0;
                   2887:         } else tl4 = 0;
                   2888:     }
1.157     noro     2889:     for ( r0 = 0, t = x; t; t = NEXT(t) ) {
                   2890:         NEXTNODE(r0,r);
1.167     noro     2891:     if ( nd_module ) BDY(r) = ndvtopl(m,CO,vv,BDY(t),mrank);
1.157     noro     2892:         else BDY(r) = ndvtop(m,CO,vv,BDY(t));
                   2893:     }
                   2894:     if ( r0 ) NEXT(r) = 0;
                   2895:     if ( nalg )
                   2896:         r0 = postprocess_algcoef(av,alist,r0);
                   2897:     MKLIST(*rp,r0);
1.172   ! noro     2898:     if ( nd_gentrace ) {
1.167     noro     2899:         tl1 = reverse_node(tl1); tl2 = reverse_node(tl2);
1.170     noro     2900:        tl3 = reverse_node(tl3);
1.167     noro     2901:                /* tl2 = [[i,[[*,j,*,*],...]],...] */
                   2902:         for ( t = tl2; t; t = NEXT(t) ) {
                   2903:                        /* s = [i,[*,j,*,*],...] */
                   2904:             s = BDY((LIST)BDY(t));
                   2905:             j = perm[QTOS((Q)ARG0(s))]; STOQ(j,jq); ARG0(s) = (pointer)jq;
                   2906:                        for ( s = BDY((LIST)ARG1(s)); s; s = NEXT(s) ) {
                   2907:                 j = perm[QTOS((Q)ARG1(BDY((LIST)BDY(s))))]; STOQ(j,jq);
                   2908:                                ARG1(BDY((LIST)BDY(s))) = (pointer)jq;
                   2909:             }
                   2910:                }
                   2911:                for ( j = length(x)-1, t = 0; j >= 0; j-- ) {
                   2912:                    STOQ(perm[j],jq); MKNODE(s,jq,t); t = s;
                   2913:                }
1.170     noro     2914:        MKLIST(l1,tl1); MKLIST(l2,tl2); MKLIST(l3,t); MKLIST(l4,tl3);
                   2915:        MKLIST(l5,tl4);
                   2916:       tr = mknode(7,*rp,0,l1,l2,l3,l4,l5); MKLIST(*rp,tr);
1.167     noro     2917:     }
1.103     noro     2918: #if 0
1.157     noro     2919:     fprintf(asir_out,"ndv_alloc=%d\n",ndv_alloc);
1.103     noro     2920: #endif
1.127     noro     2921: }
                   2922:
                   2923: void nd_gr_postproc(LIST f,LIST v,int m,struct order_spec *ord,int do_check,LIST *rp)
                   2924: {
1.157     noro     2925:     VL tv,fv,vv,vc,av;
                   2926:     NODE fd,fd0,r,r0,t,x,s,xx,alist;
                   2927:     int e,max,nvar,i;
                   2928:     NDV b;
                   2929:     int ishomo,nalg;
                   2930:     Alg alpha,dp;
                   2931:     P p,zp;
                   2932:     Q dmy;
                   2933:     LIST f1,f2;
                   2934:     Obj obj;
                   2935:     NumberField nf;
                   2936:     struct order_spec *ord1;
1.167     noro     2937:     int *perm;
1.157     noro     2938:
                   2939:     get_vars((Obj)f,&fv); pltovl(v,&vv); vlminus(fv,vv,&nd_vc);
                   2940:     for ( nvar = 0, tv = vv; tv; tv = NEXT(tv), nvar++ );
                   2941:     switch ( ord->id ) {
                   2942:         case 1:
                   2943:             if ( ord->nv != nvar )
                   2944:                 error("nd_check : invalid order specification");
                   2945:             break;
                   2946:         default:
                   2947:             break;
                   2948:     }
                   2949:     nd_nalg = 0;
                   2950:     av = 0;
                   2951:     if ( !m ) {
                   2952:         get_algtree((Obj)f,&av);
                   2953:         for ( nalg = 0, tv = av; tv; tv = NEXT(tv), nalg++ );
                   2954:         nd_ntrans = nvar;
                   2955:         nd_nalg = nalg;
                   2956:         /* #i -> t#i */
                   2957:         if ( nalg ) {
                   2958:             preprocess_algcoef(vv,av,ord,f,&ord1,&f1,&alist);
                   2959:             ord = ord1;
                   2960:             f = f1;
                   2961:         }
                   2962:         nvar += nalg;
                   2963:     }
                   2964:     nd_init_ord(ord);
                   2965:     for ( t = BDY(f), max = 0; t; t = NEXT(t) )
                   2966:         for ( tv = vv; tv; tv = NEXT(tv) ) {
                   2967:             e = getdeg(tv->v,(P)BDY(t));
                   2968:             max = MAX(e,max);
                   2969:         }
                   2970:     nd_setup_parameters(nvar,max);
                   2971:     ishomo = 1;
                   2972:     for ( fd0 = 0, t = BDY(f); t; t = NEXT(t) ) {
                   2973:         ptozp((P)BDY(t),1,&dmy,&zp);
                   2974:         b = (pointer)ptondv(CO,vv,zp);
                   2975:         if ( ishomo )
                   2976:             ishomo = ishomo && ndv_ishomo(b);
                   2977:         if ( m ) ndv_mod(m,b);
                   2978:         if ( b ) { NEXTNODE(fd0,fd); BDY(fd) = (pointer)b; }
                   2979:     }
                   2980:     if ( fd0 ) NEXT(fd) = 0;
                   2981:     ndv_setup(m,0,fd0,0,1);
                   2982:     for ( x = 0, i = 0; i < nd_psn; i++ )
                   2983:         x = update_base(x,i);
                   2984:     if ( do_check ) {
1.168     noro     2985:         x = nd_gb(m,ishomo,1,0,&perm);
1.157     noro     2986:         if ( !x ) {
                   2987:             *rp = 0;
                   2988:             return;
                   2989:         }
                   2990:     } else {
                   2991:         for ( t = x; t; t = NEXT(t) )
1.159     noro     2992:             BDY(t) = (pointer)nd_ps[(long)BDY(t)];
1.157     noro     2993:     }
1.167     noro     2994:     x = ndv_reducebase(x,perm);
1.157     noro     2995:     x = ndv_reduceall(m,x);
                   2996:     for ( r0 = 0, t = x; t; t = NEXT(t) ) {
                   2997:         NEXTNODE(r0,r);
                   2998:         BDY(r) = ndvtop(m,CO,vv,BDY(t));
                   2999:     }
                   3000:     if ( r0 ) NEXT(r) = 0;
                   3001:     if ( nalg )
                   3002:         r0 = postprocess_algcoef(av,alist,r0);
                   3003:     MKLIST(*rp,r0);
1.20      noro     3004: }
                   3005:
1.133     noro     3006: void nd_gr_trace(LIST f,LIST v,int trace,int homo,int f4,struct order_spec *ord,LIST *rp)
1.20      noro     3007: {
1.157     noro     3008:     VL tv,fv,vv,vc,av;
                   3009:     NODE fd,fd0,in0,in,r,r0,t,s,cand,alist;
                   3010:     int m,nocheck,nvar,mindex,e,max;
                   3011:     NDV c;
                   3012:     NMV a;
                   3013:     P p,zp;
                   3014:     Q dmy;
                   3015:     EPOS oepos;
1.164     noro     3016:     int obpe,oadv,wmax,i,len,cbpe,ishomo,nalg,mrank,trank,ompos;
1.157     noro     3017:     Alg alpha,dp;
                   3018:     P poly;
1.158     noro     3019:     LIST f1,f2,zpl;
1.157     noro     3020:     Obj obj;
                   3021:     NumberField nf;
                   3022:     struct order_spec *ord1;
                   3023:     struct oEGT eg_check,eg0,eg1;
1.168     noro     3024:     NODE tr,tl1,tl2,tl3,tl4;
                   3025:     LIST l1,l2,l3,l4,l5;
1.167     noro     3026:     int *perm;
1.168     noro     3027:     int j,ret;
1.167     noro     3028:     Q jq;
1.157     noro     3029:
1.167     noro     3030:     nd_module = 0;
1.172   ! noro     3031:     parse_nd_option(current_option);
1.157     noro     3032:     if ( DP_Multiple )
                   3033:         nd_scale = ((double)DP_Multiple)/(double)(Denominator?Denominator:1);
                   3034:
                   3035:     get_vars((Obj)f,&fv); pltovl(v,&vv); vlminus(fv,vv,&nd_vc);
                   3036:     for ( nvar = 0, tv = vv; tv; tv = NEXT(tv), nvar++ );
                   3037:     switch ( ord->id ) {
                   3038:         case 1:
                   3039:             if ( ord->nv != nvar )
                   3040:                 error("nd_gr_trace : invalid order specification");
                   3041:             break;
                   3042:         default:
                   3043:             break;
                   3044:     }
                   3045:
                   3046:     get_algtree((Obj)f,&av);
                   3047:     for ( nalg = 0, tv = av; tv; tv = NEXT(tv), nalg++ );
                   3048:     nd_ntrans = nvar;
                   3049:     nd_nalg = nalg;
                   3050:     /* #i -> t#i */
                   3051:     if ( nalg ) {
                   3052:         preprocess_algcoef(vv,av,ord,f,&ord1,&f1,&alist);
                   3053:         ord = ord1;
                   3054:         f = f1;
                   3055:     }
                   3056:     nvar += nalg;
                   3057:
                   3058:     nocheck = 0;
                   3059:     mindex = 0;
                   3060:
                   3061:     if ( Demand ) nd_demand = 1;
                   3062:     else nd_demand = 0;
                   3063:
                   3064:     /* setup modulus */
                   3065:     if ( trace < 0 ) {
                   3066:         trace = -trace;
                   3067:         nocheck = 1;
                   3068:     }
                   3069:     m = trace > 1 ? trace : get_lprime(mindex);
1.158     noro     3070:     nd_init_ord(ord);
                   3071:     mrank = 0;
1.157     noro     3072:     for ( t = BDY(f), max = 0; t; t = NEXT(t) )
                   3073:         for ( tv = vv; tv; tv = NEXT(tv) ) {
1.158     noro     3074:             if ( nd_module ) {
                   3075:                 s = BDY((LIST)BDY(t));
                   3076:                 trank = length(s);
                   3077:                 mrank = MAX(mrank,trank);
                   3078:                 for ( ; s; s = NEXT(s) ) {
                   3079:                     e = getdeg(tv->v,(P)BDY(s));
                   3080:                     max = MAX(e,max);
                   3081:                 }
                   3082:             } else {
                   3083:                 e = getdeg(tv->v,(P)BDY(t));
                   3084:                 max = MAX(e,max);
                   3085:             }
1.157     noro     3086:         }
                   3087:     nd_setup_parameters(nvar,max);
1.164     noro     3088:     obpe = nd_bpe; oadv = nmv_adv; oepos = nd_epos; ompos = nd_mpos;
1.157     noro     3089:     ishomo = 1;
                   3090:     for ( in0 = 0, fd0 = 0, t = BDY(f); t; t = NEXT(t) ) {
1.167     noro     3091:         if ( nd_module ) {
1.172   ! noro     3092:                        if ( !nd_gentrace ) pltozpl((LIST)BDY(t),&dmy,&zpl);
1.167     noro     3093:                        else zpl = (LIST)BDY(t);
1.158     noro     3094:             c = (pointer)pltondv(CO,vv,zpl);
                   3095:         } else {
1.172   ! noro     3096:                        if ( !nd_gentrace ) ptozp((P)BDY(t),1,&dmy,&zp);
1.167     noro     3097:                        else zp = (P)BDY(t);
1.158     noro     3098:             c = (pointer)ptondv(CO,vv,zp);
1.167     noro     3099:         }
1.157     noro     3100:         if ( ishomo )
                   3101:             ishomo = ishomo && ndv_ishomo(c);
                   3102:         if ( c ) {
                   3103:             NEXTNODE(in0,in); BDY(in) = (pointer)c;
                   3104:             NEXTNODE(fd0,fd); BDY(fd) = (pointer)ndv_dup(0,c);
                   3105:         }
                   3106:     }
                   3107:     if ( in0 ) NEXT(in) = 0;
                   3108:     if ( fd0 ) NEXT(fd) = 0;
                   3109:     if ( !ishomo && homo ) {
                   3110:         for ( t = in0, wmax = max; t; t = NEXT(t) ) {
                   3111:             c = (NDV)BDY(t); len = LEN(c);
                   3112:             for ( a = BDY(c), i = 0; i < len; i++, NMV_ADV(a) )
                   3113:                 wmax = MAX(TD(DL(a)),wmax);
                   3114:         }
                   3115:         homogenize_order(ord,nvar,&ord1);
                   3116:         nd_init_ord(ord1);
                   3117:         nd_setup_parameters(nvar+1,wmax);
                   3118:         for ( t = fd0; t; t = NEXT(t) )
1.164     noro     3119:             ndv_homogenize((NDV)BDY(t),obpe,oadv,oepos,ompos);
1.157     noro     3120:     }
                   3121:     while ( 1 ) {
                   3122:         if ( Demand )
                   3123:             nd_demand = 1;
                   3124:         ndv_setup(m,1,fd0,0,0);
1.172   ! noro     3125:         if ( nd_gentrace ) {
1.167     noro     3126:             MKLIST(l1,nd_tracelist); MKNODE(nd_alltracelist,l1,0);
                   3127:         }
                   3128:         cand = f4?nd_f4_trace(m,&perm):nd_gb_trace(m,ishomo || homo,&perm);
1.157     noro     3129:         if ( !cand ) {
                   3130:             /* failure */
                   3131:             if ( trace > 1 ) { *rp = 0; return; }
                   3132:             else m = get_lprime(++mindex);
                   3133:             continue;
                   3134:         }
                   3135:         if ( !ishomo && homo ) {
                   3136:             /* dehomogenization */
                   3137:             for ( t = cand; t; t = NEXT(t) ) ndv_dehomogenize((NDV)BDY(t),ord);
                   3138:             nd_init_ord(ord);
                   3139:             nd_setup_parameters(nvar,0);
                   3140:         }
                   3141:         nd_demand = 0;
1.167     noro     3142:         cand = ndv_reducebase(cand,perm);
1.172   ! noro     3143:         if ( nd_gentrace ) { tl1 = nd_alltracelist; nd_alltracelist = 0; }
1.157     noro     3144:         cand = ndv_reduceall(0,cand);
                   3145:         cbpe = nd_bpe;
1.172   ! noro     3146:         if ( nd_gentrace ) { tl2 = nd_alltracelist; nd_alltracelist = 0; }
1.157     noro     3147:         if ( nocheck )
                   3148:             break;
                   3149:         get_eg(&eg0);
1.170     noro     3150:         if ( ret = ndv_check_membership(0,in0,obpe,oadv,oepos,cand) ) {
1.172   ! noro     3151:             if ( nd_gentrace ) {
1.168     noro     3152:                            tl3 = nd_alltracelist; nd_alltracelist = 0;
                   3153:                    } else tl3 = 0;
                   3154:             /* gbcheck : cand is a GB of Id(cand) ? */
1.172   ! noro     3155:             ret = nd_gb(0,0,1,nd_gensyz?1:0,0)!=0;
        !          3156:             if ( nd_gentrace && nd_gensyz ) {
1.168     noro     3157:                            tl4 = nd_alltracelist; nd_alltracelist = 0;
                   3158:                    } else tl4 = 0;
                   3159:                }
                   3160:                if ( ret ) break;
1.157     noro     3161:         else if ( trace > 1 ) {
                   3162:             /* failure */
                   3163:             *rp = 0; return;
                   3164:         } else {
                   3165:             /* try the next modulus */
                   3166:             m = get_lprime(++mindex);
                   3167:             /* reset the parameters */
                   3168:             if ( !ishomo && homo ) {
                   3169:                 nd_init_ord(ord1);
                   3170:                 nd_setup_parameters(nvar+1,wmax);
                   3171:             } else {
                   3172:                 nd_init_ord(ord);
                   3173:                 nd_setup_parameters(nvar,max);
                   3174:             }
                   3175:         }
                   3176:     }
                   3177:     get_eg(&eg1); init_eg(&eg_check); add_eg(&eg_check,&eg0,&eg1);
                   3178:     if ( DP_Print )
                   3179:         fprintf(asir_out,"check=%fsec\n",eg_check.exectime+eg_check.gctime);
                   3180:     /* dp->p */
                   3181:     nd_bpe = cbpe;
                   3182:     nd_setup_parameters(nd_nvar,0);
1.158     noro     3183:     for ( r = cand; r; r = NEXT(r) ) {
1.167     noro     3184:     if ( nd_module ) BDY(r) = ndvtopl(0,CO,vv,BDY(r),mrank);
1.158     noro     3185:         else BDY(r) = (pointer)ndvtop(0,CO,vv,BDY(r));
                   3186:     }
1.157     noro     3187:     if ( nalg )
                   3188:         cand = postprocess_algcoef(av,alist,cand);
                   3189:     MKLIST(*rp,cand);
1.172   ! noro     3190:     if ( nd_gentrace ) {
1.167     noro     3191:         tl1 = reverse_node(tl1); tl2 = reverse_node(tl2);
1.168     noro     3192:                tl3 = reverse_node(tl3);
1.167     noro     3193:                /* tl2 = [[i,[[*,j,*,*],...]],...] */
                   3194:         for ( t = tl2; t; t = NEXT(t) ) {
                   3195:                        /* s = [i,[*,j,*,*],...] */
                   3196:             s = BDY((LIST)BDY(t));
                   3197:             j = perm[QTOS((Q)ARG0(s))]; STOQ(j,jq); ARG0(s) = (pointer)jq;
                   3198:                        for ( s = BDY((LIST)ARG1(s)); s; s = NEXT(s) ) {
                   3199:                 j = perm[QTOS((Q)ARG1(BDY((LIST)BDY(s))))]; STOQ(j,jq);
                   3200:                                ARG1(BDY((LIST)BDY(s))) = (pointer)jq;
                   3201:             }
                   3202:                }
                   3203:                for ( j = length(cand)-1, t = 0; j >= 0; j-- ) {
                   3204:                    STOQ(perm[j],jq); MKNODE(s,jq,t); t = s;
                   3205:                }
1.168     noro     3206:         MKLIST(l1,tl1); MKLIST(l2,tl2); MKLIST(l3,t); MKLIST(l4,tl3);
                   3207:                MKLIST(l5,tl4);
1.169     noro     3208:         tr = mknode(7,*rp,(!ishomo&&homo)?ONE:0,l1,l2,l3,l4,l5); MKLIST(*rp,tr);
1.167     noro     3209:     }
1.157     noro     3210: }
1.52      noro     3211:
1.157     noro     3212: /* XXX : module element is not considered  */
1.1       noro     3213:
1.61      noro     3214: void dltondl(int n,DL dl,UINT *r)
1.1       noro     3215: {
1.157     noro     3216:     UINT *d;
                   3217:     int i,j,l,s,ord_l;
                   3218:     struct order_pair *op;
                   3219:
                   3220:     d = dl->d;
                   3221:     for ( i = 0; i < nd_wpd; i++ ) r[i] = 0;
                   3222:     if ( nd_blockmask ) {
                   3223:         l = nd_blockmask->n;
                   3224:         op = nd_blockmask->order_pair;
                   3225:         for ( j = 0, s = 0; j < l; j++ ) {
                   3226:             ord_l = op[j].length;
                   3227:             for ( i = 0; i < ord_l; i++, s++ ) PUT_EXP(r,s,d[s]);
                   3228:         }
                   3229:         TD(r) = ndl_weight(r);
                   3230:         ndl_weight_mask(r);
                   3231:     } else {
                   3232:         for ( i = 0; i < n; i++ ) PUT_EXP(r,i,d[i]);
                   3233:         TD(r) = ndl_weight(r);
                   3234:     }
1.1       noro     3235: }
                   3236:
1.61      noro     3237: DL ndltodl(int n,UINT *ndl)
1.1       noro     3238: {
1.157     noro     3239:     DL dl;
                   3240:     int *d;
                   3241:     int i,j,l,s,ord_l;
                   3242:     struct order_pair *op;
                   3243:
                   3244:     NEWDL(dl,n);
                   3245:     dl->td = TD(ndl);
                   3246:     d = dl->d;
                   3247:     if ( nd_blockmask ) {
                   3248:         l = nd_blockmask->n;
                   3249:         op = nd_blockmask->order_pair;
                   3250:         for ( j = 0, s = 0; j < l; j++ ) {
                   3251:             ord_l = op[j].length;
                   3252:             for ( i = 0; i < ord_l; i++, s++ ) d[s] = GET_EXP(ndl,s);
                   3253:         }
                   3254:     } else {
                   3255:         for ( i = 0; i < n; i++ ) d[i] = GET_EXP(ndl,i);
                   3256:     }
                   3257:     return dl;
1.1       noro     3258: }
                   3259:
1.167     noro     3260: void nmtodp(int mod,NM m,DP *r)
                   3261: {
                   3262:     DP dp;
                   3263:     MP mr;
                   3264:
                   3265:     NEWMP(mr);
                   3266:     mr->dl = ndltodl(nd_nvar,DL(m));
                   3267:     mr->c = ndctop(mod,m->c);
                   3268:     NEXT(mr) = 0; MKDP(nd_nvar,mr,dp); dp->sugar = mr->dl->td;
                   3269:     *r = dp;
                   3270: }
                   3271:
1.61      noro     3272: void ndl_print(UINT *dl)
1.1       noro     3273: {
1.157     noro     3274:     int n;
                   3275:     int i,j,l,ord_l,s,s0;
                   3276:     struct order_pair *op;
                   3277:
                   3278:     n = nd_nvar;
                   3279:     printf("<<");
                   3280:     if ( nd_blockmask ) {
                   3281:         l = nd_blockmask->n;
                   3282:         op = nd_blockmask->order_pair;
                   3283:         for ( j = 0, s = s0 = 0; j < l; j++ ) {
                   3284:             ord_l = op[j].length;
                   3285:             for ( i = 0; i < ord_l; i++, s++ )
                   3286:                 printf(s==n-1?"%d":"%d,",GET_EXP(dl,s));
                   3287:         }
                   3288:     } else {
                   3289:         for ( i = 0; i < n; i++ ) printf(i==n-1?"%d":"%d,",GET_EXP(dl,i));
                   3290:     }
                   3291:     printf(">>");
                   3292:     if ( MPOS(dl) )
                   3293:         printf("*e%d",MPOS(dl));
1.1       noro     3294: }
                   3295:
                   3296: void nd_print(ND p)
                   3297: {
1.157     noro     3298:     NM m;
1.1       noro     3299:
1.157     noro     3300:     if ( !p )
                   3301:         printf("0\n");
                   3302:     else {
                   3303:         for ( m = BDY(p); m; m = NEXT(m) ) {
                   3304:             if ( CM(m) & 0x80000000 ) printf("+@_%d*",IFTOF(CM(m)));
                   3305:             else printf("+%d*",CM(m));
                   3306:             ndl_print(DL(m));
                   3307:         }
                   3308:         printf("\n");
                   3309:     }
1.1       noro     3310: }
                   3311:
1.113     noro     3312: void nd_print_q(ND p)
1.16      noro     3313: {
1.157     noro     3314:     NM m;
1.16      noro     3315:
1.157     noro     3316:     if ( !p )
                   3317:         printf("0\n");
                   3318:     else {
                   3319:         for ( m = BDY(p); m; m = NEXT(m) ) {
                   3320:             printf("+");
                   3321:             printexpr(CO,(Obj)CQ(m));
                   3322:             printf("*");
                   3323:             ndl_print(DL(m));
                   3324:         }
                   3325:         printf("\n");
                   3326:     }
1.16      noro     3327: }
                   3328:
1.1       noro     3329: void ndp_print(ND_pairs d)
                   3330: {
1.157     noro     3331:     ND_pairs t;
1.1       noro     3332:
1.157     noro     3333:     for ( t = d; t; t = NEXT(t) ) printf("%d,%d ",t->i1,t->i2);
                   3334:     printf("\n");
1.1       noro     3335: }
                   3336:
1.20      noro     3337: void nd_removecont(int mod,ND p)
1.16      noro     3338: {
1.157     noro     3339:     int i,n;
                   3340:     Q *w;
                   3341:     Q dvr,t;
                   3342:     NM m;
                   3343:     struct oVECT v;
                   3344:     N q,r;
                   3345:
                   3346:     if ( mod == -1 ) nd_mul_c(mod,p,_invsf(HCM(p)));
                   3347:     else if ( mod ) nd_mul_c(mod,p,invm(HCM(p),mod));
                   3348:     else {
                   3349:         for ( m = BDY(p), n = 0; m; m = NEXT(m), n++ );
                   3350:         w = (Q *)ALLOCA(n*sizeof(Q));
                   3351:         v.len = n;
                   3352:         v.body = (pointer *)w;
                   3353:         for ( m = BDY(p), i = 0; i < n; m = NEXT(m), i++ ) w[i] = CQ(m);
                   3354:         removecont_array((P *)w,n,1);
                   3355:         for ( m = BDY(p), i = 0; i < n; m = NEXT(m), i++ ) CQ(m) = w[i];
                   3356:     }
1.16      noro     3357: }
                   3358:
1.21      noro     3359: void nd_removecont2(ND p1,ND p2)
                   3360: {
1.157     noro     3361:     int i,n1,n2,n;
                   3362:     Q *w;
                   3363:     Q dvr,t;
                   3364:     NM m;
                   3365:     struct oVECT v;
                   3366:     N q,r;
                   3367:
                   3368:     n1 = nd_length(p1);
                   3369:     n2 = nd_length(p2);
                   3370:     n = n1+n2;
                   3371:     w = (Q *)ALLOCA(n*sizeof(Q));
                   3372:     v.len = n;
                   3373:     v.body = (pointer *)w;
                   3374:     i = 0;
                   3375:     if ( p1 )
                   3376:         for ( m = BDY(p1); i < n1; m = NEXT(m), i++ ) w[i] = CQ(m);
                   3377:     if ( p2 )
                   3378:         for ( m = BDY(p2); i < n; m = NEXT(m), i++ ) w[i] = CQ(m);
                   3379:     removecont_array((P *)w,n,1);
                   3380:     i = 0;
                   3381:     if ( p1 )
                   3382:         for ( m = BDY(p1); i < n1; m = NEXT(m), i++ ) CQ(m) = w[i];
                   3383:     if ( p2 )
                   3384:         for ( m = BDY(p2); i < n; m = NEXT(m), i++ ) CQ(m) = w[i];
1.21      noro     3385: }
                   3386:
1.20      noro     3387: void ndv_removecont(int mod,NDV p)
1.16      noro     3388: {
1.157     noro     3389:     int i,len,all_p;
                   3390:     Q *c;
                   3391:     P *w;
                   3392:     Q dvr,t;
                   3393:     P g,cont,tp;
                   3394:     NMV m;
                   3395:
                   3396:     if ( mod == -1 )
                   3397:         ndv_mul_c(mod,p,_invsf(HCM(p)));
                   3398:     else if ( mod )
                   3399:         ndv_mul_c(mod,p,invm(HCM(p),mod));
                   3400:     else {
                   3401:         len = p->len;
                   3402:         w = (P *)ALLOCA(len*sizeof(P));
                   3403:         c = (Q *)ALLOCA(len*sizeof(Q));
                   3404:         for ( m = BDY(p), all_p = 1, i = 0; i < len; NMV_ADV(m), i++ ) {
                   3405:             ptozp(CP(m),1,&c[i],&w[i]);
                   3406:             all_p = all_p && !NUM(w[i]);
                   3407:         }
                   3408:         if ( all_p ) {
                   3409:             qltozl(c,len,&dvr); nd_heu_nezgcdnpz(nd_vc,w,len,1,&g);
                   3410:             mulp(nd_vc,(P)dvr,g,&cont);
                   3411:             for ( m = BDY(p), i = 0; i < len; NMV_ADV(m), i++ ) {
                   3412:                 divsp(nd_vc,CP(m),cont,&tp); CP(m) = tp;
                   3413:             }
                   3414:         } else {
                   3415:             sortbynm((Q *)c,len);
                   3416:             qltozl((Q *)c,len,&dvr);
                   3417:             for ( m = BDY(p), i = 0; i < len; NMV_ADV(m), i++ ) {
                   3418:                 divsp(nd_vc,CP(m),(P)dvr,&tp); CP(m) = tp;
                   3419:             }
                   3420:         }
                   3421:     }
1.21      noro     3422: }
                   3423:
1.157     noro     3424: /* koko */
                   3425:
1.164     noro     3426: void ndv_homogenize(NDV p,int obpe,int oadv,EPOS oepos,int ompos)
1.61      noro     3427: {
1.157     noro     3428:     int len,i,max;
                   3429:     NMV m,mr0,mr,t;
1.61      noro     3430:
1.157     noro     3431:     len = p->len;
                   3432:     for ( m = BDY(p), i = 0, max = 0; i < len; NMV_OADV(m), i++ )
                   3433:         max = MAX(max,TD(DL(m)));
                   3434:     mr0 = nmv_adv>oadv?(NMV)REALLOC(BDY(p),len*nmv_adv):BDY(p);
                   3435:     m = (NMV)((char *)mr0+(len-1)*oadv);
                   3436:     mr = (NMV)((char *)mr0+(len-1)*nmv_adv);
                   3437:     t = (NMV)ALLOCA(nmv_adv);
                   3438:     for ( i = 0; i < len; i++, NMV_OPREV(m), NMV_PREV(mr) ) {
1.164     noro     3439:         ndl_homogenize(DL(m),DL(t),obpe,oepos,ompos,max);
1.157     noro     3440:         CQ(mr) = CQ(m);
                   3441:         ndl_copy(DL(t),DL(mr));
                   3442:     }
                   3443:     NV(p)++;
                   3444:     BDY(p) = mr0;
1.61      noro     3445: }
                   3446:
1.45      noro     3447: void ndv_dehomogenize(NDV p,struct order_spec *ord)
1.23      noro     3448: {
1.164     noro     3449:     int i,j,adj,len,newnvar,newwpd,newadv,newexporigin,newmpos;
1.167     noro     3450:     int pos;
1.157     noro     3451:     Q *w;
                   3452:     Q dvr,t;
                   3453:     NMV m,r;
                   3454:
                   3455:     len = p->len;
                   3456:     newnvar = nd_nvar-1;
                   3457:     newexporigin = nd_get_exporigin(ord);
1.167     noro     3458:     if ( nd_module ) newmpos = newexporigin-1;
1.157     noro     3459:     newwpd = newnvar/nd_epw+(newnvar%nd_epw?1:0)+newexporigin;
                   3460:     for ( m = BDY(p), i = 0; i < len; NMV_ADV(m), i++ )
                   3461:         ndl_dehomogenize(DL(m));
                   3462:     if ( newwpd != nd_wpd ) {
                   3463:         newadv = ROUND_FOR_ALIGN(sizeof(struct oNMV)+(newwpd-1)*sizeof(UINT));
                   3464:         for ( m = r = BDY(p), i = 0; i < len; NMV_ADV(m), NDV_NADV(r), i++ ) {
                   3465:             CQ(r) = CQ(m);
1.167     noro     3466:             if ( nd_module ) pos = MPOS(DL(m));
1.157     noro     3467:             for ( j = 0; j < newexporigin; j++ ) DL(r)[j] = DL(m)[j];
                   3468:             adj = nd_exporigin-newexporigin;
                   3469:             for ( ; j < newwpd; j++ ) DL(r)[j] = DL(m)[j+adj];
1.167     noro     3470:             if ( nd_module ) {
                   3471:                 DL(r)[newmpos] = pos;
                   3472:             }
1.157     noro     3473:         }
                   3474:     }
                   3475:     NV(p)--;
1.23      noro     3476: }
                   3477:
1.150     noro     3478: void nd_heu_nezgcdnpz(VL vl,P *pl,int m,int full,P *pr)
                   3479: {
1.157     noro     3480:     int i;
                   3481:     P *tpl,*tpl1;
                   3482:     NODE l;
                   3483:     P h,gcd,t;
                   3484:
                   3485:     tpl = (P *)ALLOCA(m*sizeof(P));
                   3486:     tpl1 = (P *)ALLOCA(m*sizeof(P));
                   3487:     bcopy(pl,tpl,m*sizeof(P));
                   3488:     gcd = (P)ONE;
                   3489:     for ( l = nd_hcf; l; l = NEXT(l) ) {
                   3490:         h = (P)BDY(l);
                   3491:         while ( 1 ) {
                   3492:             for ( i = 0; i < m; i++ )
                   3493:                 if ( !divtpz(vl,tpl[i],h,&tpl1[i]) )
                   3494:                     break;
                   3495:             if ( i == m ) {
                   3496:                 bcopy(tpl1,tpl,m*sizeof(P));
                   3497:                 mulp(vl,gcd,h,&t); gcd = t;
                   3498:             } else
                   3499:                 break;
                   3500:         }
                   3501:     }
                   3502:     if ( DP_Print > 2 ){fprintf(asir_out,"[%d]",nmonop(gcd)); fflush(asir_out);}
                   3503:     if ( full ) {
                   3504:         heu_nezgcdnpz(vl,tpl,m,&t);
                   3505:         mulp(vl,gcd,t,pr);
                   3506:     } else
                   3507:         *pr = gcd;
1.150     noro     3508: }
                   3509:
                   3510: void removecont_array(P *p,int n,int full)
1.146     noro     3511: {
1.157     noro     3512:     int all_p,all_q,i;
                   3513:     Q *c;
                   3514:     P *w;
                   3515:     P t,s;
                   3516:
                   3517:     for ( all_q = 1, i = 0; i < n; i++ )
                   3518:         all_q = all_q && NUM(p[i]);
                   3519:     if ( all_q ) {
                   3520:         removecont_array_q((Q *)p,n);
                   3521:     } else {
                   3522:         c = (Q *)ALLOCA(n*sizeof(Q));
                   3523:         w = (P *)ALLOCA(n*sizeof(P));
                   3524:         for ( i = 0; i < n; i++ ) {
                   3525:             ptozp(p[i],1,&c[i],&w[i]);
                   3526:         }
                   3527:         removecont_array_q(c,n);
                   3528:         nd_heu_nezgcdnpz(nd_vc,w,n,full,&t);
                   3529:         for ( i = 0; i < n; i++ ) {
                   3530:             divsp(nd_vc,w[i],t,&s); mulp(nd_vc,s,(P)c[i],&p[i]);
                   3531:         }
                   3532:     }
1.146     noro     3533: }
                   3534:
                   3535: void removecont_array_q(Q *c,int n)
1.21      noro     3536: {
1.157     noro     3537:     struct oVECT v;
                   3538:     Q d0,d1,a,u,u1,gcd;
                   3539:     int i,j;
                   3540:     N qn,rn,gn;
                   3541:     Q *q,*r;
                   3542:
                   3543:     q = (Q *)ALLOCA(n*sizeof(Q));
                   3544:     r = (Q *)ALLOCA(n*sizeof(Q));
                   3545:     v.id = O_VECT; v.len = n; v.body = (pointer *)c;
                   3546:     igcdv_estimate(&v,&d0);
                   3547:     for ( i = 0; i < n; i++ ) {
                   3548:         divn(NM(c[i]),NM(d0),&qn,&rn);
                   3549:         NTOQ(qn,SGN(c[i])*SGN(d0),q[i]);
                   3550:         NTOQ(rn,SGN(c[i]),r[i]);
                   3551:     }
                   3552:     for ( i = 0; i < n; i++ ) if ( r[i] ) break;
                   3553:     if ( i < n ) {
                   3554:         v.id = O_VECT; v.len = n; v.body = (pointer *)r;
                   3555:         igcdv(&v,&d1);
                   3556:         gcdn(NM(d0),NM(d1),&gn); NTOQ(gn,1,gcd);
                   3557:         divsn(NM(d0),gn,&qn); NTOQ(qn,1,a);
                   3558:         for ( i = 0; i < n; i++ ) {
                   3559:             mulq(a,q[i],&u);
                   3560:             if ( r[i] ) {
                   3561:                 divsn(NM(r[i]),gn,&qn); NTOQ(qn,SGN(r[i]),u1);
                   3562:                 addq(u,u1,&q[i]);
                   3563:             } else
                   3564:                 q[i] = u;
                   3565:         }
                   3566:     }
                   3567:     for ( i = 0; i < n; i++ ) c[i] = q[i];
1.16      noro     3568: }
                   3569:
1.19      noro     3570: void nd_mul_c(int mod,ND p,int mul)
1.1       noro     3571: {
1.157     noro     3572:     NM m;
                   3573:     int c,c1;
1.1       noro     3574:
1.157     noro     3575:     if ( !p ) return;
                   3576:     if ( mul == 1 ) return;
                   3577:     if ( mod == -1 )
                   3578:         for ( m = BDY(p); m; m = NEXT(m) )
                   3579:             CM(m) = _mulsf(CM(m),mul);
                   3580:     else
                   3581:         for ( m = BDY(p); m; m = NEXT(m) ) {
                   3582:             c1 = CM(m); DMAR(c1,mul,0,mod,c); CM(m) = c;
                   3583:         }
1.1       noro     3584: }
                   3585:
1.146     noro     3586: void nd_mul_c_q(ND p,P mul)
1.16      noro     3587: {
1.157     noro     3588:     NM m;
                   3589:     P c;
1.16      noro     3590:
1.157     noro     3591:     if ( !p ) return;
                   3592:     if ( UNIQ(mul) ) return;
                   3593:     for ( m = BDY(p); m; m = NEXT(m) ) {
                   3594:         mulp(nd_vc,CP(m),mul,&c); CP(m) = c;
                   3595:     }
1.16      noro     3596: }
                   3597:
1.61      noro     3598: void nd_mul_c_p(VL vl,ND p,P mul)
                   3599: {
1.157     noro     3600:     NM m;
                   3601:     P c;
1.61      noro     3602:
1.157     noro     3603:     if ( !p ) return;
                   3604:     for ( m = BDY(p); m; m = NEXT(m) ) {
                   3605:         mulp(vl,CP(m),mul,&c); CP(m) = c;
                   3606:     }
1.61      noro     3607: }
                   3608:
1.1       noro     3609: void nd_free(ND p)
                   3610: {
1.157     noro     3611:     NM t,s;
1.1       noro     3612:
1.157     noro     3613:     if ( !p ) return;
                   3614:     t = BDY(p);
                   3615:     while ( t ) {
                   3616:         s = NEXT(t);
                   3617:         FREENM(t);
                   3618:         t = s;
                   3619:     }
                   3620:     FREEND(p);
1.1       noro     3621: }
                   3622:
1.23      noro     3623: void ndv_free(NDV p)
                   3624: {
1.157     noro     3625:     GC_free(BDY(p));
1.23      noro     3626: }
                   3627:
1.61      noro     3628: void nd_append_red(UINT *d,int i)
1.1       noro     3629: {
1.157     noro     3630:     RHist m,m0;
                   3631:     int h;
1.1       noro     3632:
1.157     noro     3633:     NEWRHist(m);
                   3634:     h = ndl_hash_value(d);
                   3635:     m->index = i;
                   3636:     ndl_copy(d,DL(m));
                   3637:     NEXT(m) = nd_red[h];
                   3638:     nd_red[h] = m;
1.1       noro     3639: }
                   3640:
1.61      noro     3641: UINT *ndv_compute_bound(NDV p)
1.1       noro     3642: {
1.157     noro     3643:     UINT *d1,*d2,*t;
                   3644:     UINT u;
                   3645:     int i,j,k,l,len,ind;
                   3646:     NMV m;
                   3647:
                   3648:     if ( !p )
                   3649:         return 0;
                   3650:     d1 = (UINT *)ALLOCA(nd_wpd*sizeof(UINT));
                   3651:     d2 = (UINT *)ALLOCA(nd_wpd*sizeof(UINT));
                   3652:     len = LEN(p);
                   3653:     m = BDY(p); ndl_copy(DL(m),d1); NMV_ADV(m);
                   3654:     for ( i = 1; i < len; i++, NMV_ADV(m) ) {
1.159     noro     3655:         ndl_max(DL(m),d1,d2);
1.157     noro     3656:         t = d1; d1 = d2; d2 = t;
                   3657:     }
                   3658:     l = nd_nvar+31;
                   3659:     t = (UINT *)MALLOC_ATOMIC(l*sizeof(UINT));
                   3660:     for ( i = nd_exporigin, ind = 0; i < nd_wpd; i++ ) {
                   3661:         u = d1[i];
                   3662:         k = (nd_epw-1)*nd_bpe;
                   3663:         for ( j = 0; j < nd_epw; j++, k -= nd_bpe, ind++ )
                   3664:             t[ind] = (u>>k)&nd_mask0;
                   3665:     }
                   3666:     for ( ; ind < l; ind++ ) t[ind] = 0;
                   3667:     return t;
1.1       noro     3668: }
                   3669:
1.99      noro     3670: UINT *nd_compute_bound(ND p)
                   3671: {
1.157     noro     3672:     UINT *d1,*d2,*t;
                   3673:     UINT u;
                   3674:     int i,j,k,l,len,ind;
                   3675:     NM m;
                   3676:
                   3677:     if ( !p )
                   3678:         return 0;
                   3679:     d1 = (UINT *)ALLOCA(nd_wpd*sizeof(UINT));
                   3680:     d2 = (UINT *)ALLOCA(nd_wpd*sizeof(UINT));
                   3681:     len = LEN(p);
                   3682:     m = BDY(p); ndl_copy(DL(m),d1); m = NEXT(m);
                   3683:     for ( m = NEXT(m); m; m = NEXT(m) ) {
                   3684:         ndl_lcm(DL(m),d1,d2);
                   3685:         t = d1; d1 = d2; d2 = t;
                   3686:     }
                   3687:     l = nd_nvar+31;
                   3688:     t = (UINT *)MALLOC_ATOMIC(l*sizeof(UINT));
                   3689:     for ( i = nd_exporigin, ind = 0; i < nd_wpd; i++ ) {
                   3690:         u = d1[i];
                   3691:         k = (nd_epw-1)*nd_bpe;
                   3692:         for ( j = 0; j < nd_epw; j++, k -= nd_bpe, ind++ )
                   3693:             t[ind] = (u>>k)&nd_mask0;
                   3694:     }
                   3695:     for ( ; ind < l; ind++ ) t[ind] = 0;
                   3696:     return t;
1.99      noro     3697: }
                   3698:
1.157     noro     3699: /* if nd_module == 1 then d[nd_exporigin-1] indicates the position */
                   3700: /* of a term. In this case we need additional 1 word. */
                   3701:
1.48      noro     3702: int nd_get_exporigin(struct order_spec *ord)
                   3703: {
1.157     noro     3704:     switch ( ord->id ) {
                   3705:         case 0: case 2: case 256: case 258:
                   3706:             return 1+nd_module;
                   3707:         case 1: case 257:
                   3708:             /* block order */
                   3709:             /* poly ring d[0]:weight d[1]:w0,...,d[nd_exporigin-1]:w(n-1) */
                   3710:             /* module d[0]:weight d[1]:w0,...,d[nd_exporigin-2]:w(n-1) */
                   3711:             return ord->ord.block.length+1+nd_module;
                   3712:         case 3: case 259:
                   3713:             error("nd_get_exporigin : composite order is not supported yet.");
                   3714:     }
1.48      noro     3715: }
                   3716:
1.61      noro     3717: void nd_setup_parameters(int nvar,int max) {
1.157     noro     3718:     int i,j,n,elen,ord_o,ord_l,l,s,wpd;
                   3719:     struct order_pair *op;
1.48      noro     3720:
1.157     noro     3721:     nd_nvar = nvar;
                   3722:     if ( max ) {
                   3723:         /* XXX */
                   3724:         if ( do_weyl ) nd_bpe = 32;
                   3725:         else if ( max < 2 ) nd_bpe = 1;
                   3726:         else if ( max < 4 ) nd_bpe = 2;
                   3727:         else if ( max < 8 ) nd_bpe = 3;
                   3728:         else if ( max < 16 ) nd_bpe = 4;
                   3729:         else if ( max < 32 ) nd_bpe = 5;
                   3730:         else if ( max < 64 ) nd_bpe = 6;
                   3731:         else if ( max < 256 ) nd_bpe = 8;
                   3732:         else if ( max < 1024 ) nd_bpe = 10;
                   3733:         else if ( max < 65536 ) nd_bpe = 16;
                   3734:         else nd_bpe = 32;
                   3735:     }
                   3736:     nd_epw = (sizeof(UINT)*8)/nd_bpe;
                   3737:     elen = nd_nvar/nd_epw+(nd_nvar%nd_epw?1:0);
                   3738:     nd_exporigin = nd_get_exporigin(nd_ord);
                   3739:     wpd = nd_exporigin+elen;
                   3740:     if ( nd_module )
                   3741:         nd_mpos = nd_exporigin-1;
                   3742:     else
                   3743:         nd_mpos = -1;
                   3744:     if ( wpd != nd_wpd ) {
                   3745:         nd_free_private_storage();
                   3746:         nd_wpd = wpd;
                   3747:     }
                   3748:     if ( nd_bpe < 32 ) {
                   3749:         nd_mask0 = (1<<nd_bpe)-1;
                   3750:     } else {
                   3751:         nd_mask0 = 0xffffffff;
                   3752:     }
                   3753:     bzero(nd_mask,sizeof(nd_mask));
                   3754:     nd_mask1 = 0;
                   3755:     for ( i = 0; i < nd_epw; i++ ) {
                   3756:         nd_mask[nd_epw-i-1] = (nd_mask0<<(i*nd_bpe));
                   3757:         nd_mask1 |= (1<<(nd_bpe-1))<<(i*nd_bpe);
                   3758:     }
                   3759:     nmv_adv = ROUND_FOR_ALIGN(sizeof(struct oNMV)+(nd_wpd-1)*sizeof(UINT));
                   3760:     nd_epos = nd_create_epos(nd_ord);
                   3761:     nd_blockmask = nd_create_blockmask(nd_ord);
                   3762:     nd_work_vector = (int *)REALLOC(nd_work_vector,nd_nvar*sizeof(int));
1.1       noro     3763: }
                   3764:
1.103     noro     3765: ND_pairs nd_reconstruct(int trace,ND_pairs d)
1.1       noro     3766: {
1.157     noro     3767:     int i,obpe,oadv,h;
                   3768:     static NM prev_nm_free_list;
                   3769:     static ND_pairs prev_ndp_free_list;
                   3770:     RHist mr0,mr;
                   3771:     RHist r;
                   3772:     RHist *old_red;
                   3773:     ND_pairs s0,s,t;
                   3774:     EPOS oepos;
                   3775:
                   3776:     obpe = nd_bpe;
                   3777:     oadv = nmv_adv;
                   3778:     oepos = nd_epos;
                   3779:     if ( obpe < 2 ) nd_bpe = 2;
                   3780:     else if ( obpe < 3 ) nd_bpe = 3;
                   3781:     else if ( obpe < 4 ) nd_bpe = 4;
                   3782:     else if ( obpe < 5 ) nd_bpe = 5;
                   3783:     else if ( obpe < 6 ) nd_bpe = 6;
                   3784:     else if ( obpe < 8 ) nd_bpe = 8;
                   3785:     else if ( obpe < 10 ) nd_bpe = 10;
                   3786:     else if ( obpe < 16 ) nd_bpe = 16;
                   3787:     else if ( obpe < 32 ) nd_bpe = 32;
                   3788:     else error("nd_reconstruct : exponent too large");
                   3789:
                   3790:     nd_setup_parameters(nd_nvar,0);
                   3791:     prev_nm_free_list = _nm_free_list;
                   3792:     prev_ndp_free_list = _ndp_free_list;
                   3793:     _nm_free_list = 0;
                   3794:     _ndp_free_list = 0;
                   3795:     for ( i = nd_psn-1; i >= 0; i-- ) ndv_realloc(nd_ps[i],obpe,oadv,oepos);
                   3796:     if ( trace )
                   3797:         for ( i = nd_psn-1; i >= 0; i-- )
                   3798:             ndv_realloc(nd_ps_trace[i],obpe,oadv,oepos);
                   3799:     s0 = 0;
                   3800:     for ( t = d; t; t = NEXT(t) ) {
                   3801:         NEXTND_pairs(s0,s);
                   3802:         s->i1 = t->i1;
                   3803:         s->i2 = t->i2;
                   3804:         SG(s) = SG(t);
                   3805:         ndl_reconstruct(LCM(t),LCM(s),obpe,oepos);
                   3806:     }
                   3807:
                   3808:     old_red = (RHist *)ALLOCA(REDTAB_LEN*sizeof(RHist));
                   3809:     for ( i = 0; i < REDTAB_LEN; i++ ) {
                   3810:         old_red[i] = nd_red[i];
                   3811:         nd_red[i] = 0;
                   3812:     }
                   3813:     for ( i = 0; i < REDTAB_LEN; i++ )
                   3814:         for ( r = old_red[i]; r; r = NEXT(r) ) {
                   3815:             NEWRHist(mr);
                   3816:             mr->index = r->index;
                   3817:             SG(mr) = SG(r);
                   3818:             ndl_reconstruct(DL(r),DL(mr),obpe,oepos);
                   3819:             h = ndl_hash_value(DL(mr));
                   3820:             NEXT(mr) = nd_red[h];
                   3821:             nd_red[h] = mr;
                   3822:         }
                   3823:     for ( i = 0; i < REDTAB_LEN; i++ ) old_red[i] = 0;
                   3824:     old_red = 0;
                   3825:     for ( i = 0; i < nd_psn; i++ ) {
                   3826:         NEWRHist(r); SG(r) = SG(nd_psh[i]);
                   3827:         ndl_reconstruct(DL(nd_psh[i]),DL(r),obpe,oepos);
                   3828:         nd_psh[i] = r;
                   3829:     }
                   3830:     if ( s0 ) NEXT(s) = 0;
                   3831:     prev_nm_free_list = 0;
                   3832:     prev_ndp_free_list = 0;
1.71      noro     3833: #if 0
1.157     noro     3834:     GC_gcollect();
1.71      noro     3835: #endif
1.157     noro     3836:     return s0;
1.1       noro     3837: }
                   3838:
1.61      noro     3839: void ndl_reconstruct(UINT *d,UINT *r,int obpe,EPOS oepos)
1.1       noro     3840: {
1.157     noro     3841:     int n,i,ei,oepw,omask0,j,s,ord_l,l;
                   3842:     struct order_pair *op;
1.1       noro     3843:
1.157     noro     3844:     n = nd_nvar;
                   3845:     oepw = (sizeof(UINT)*8)/obpe;
                   3846:     omask0 = (1<<obpe)-1;
                   3847:     TD(r) = TD(d);
                   3848:     for ( i = nd_exporigin; i < nd_wpd; i++ ) r[i] = 0;
                   3849:     if ( nd_blockmask ) {
                   3850:         l = nd_blockmask->n;
                   3851:         op = nd_blockmask->order_pair;
                   3852:         for ( i = 1; i < nd_exporigin; i++ )
                   3853:             r[i] = d[i];
                   3854:         for ( j = 0, s = 0; j < l; j++ ) {
                   3855:             ord_l = op[j].length;
                   3856:             for ( i = 0; i < ord_l; i++, s++ ) {
                   3857:                 ei =  GET_EXP_OLD(d,s);
                   3858:                 PUT_EXP(r,s,ei);
                   3859:             }
                   3860:         }
                   3861:     } else {
                   3862:         for ( i = 0; i < n; i++ ) {
                   3863:             ei = GET_EXP_OLD(d,i);
                   3864:             PUT_EXP(r,i,ei);
                   3865:         }
                   3866:     }
                   3867:     if ( nd_module ) MPOS(r) = MPOS(d);
1.1       noro     3868: }
1.3       noro     3869:
1.6       noro     3870: ND nd_copy(ND p)
                   3871: {
1.157     noro     3872:     NM m,mr,mr0;
                   3873:     int c,n;
                   3874:     ND r;
                   3875:
                   3876:     if ( !p )
                   3877:         return 0;
                   3878:     else {
                   3879:         for ( mr0 = 0, m = BDY(p); m; m = NEXT(m) ) {
                   3880:             NEXTNM(mr0,mr);
                   3881:             CM(mr) = CM(m);
                   3882:             ndl_copy(DL(m),DL(mr));
                   3883:         }
                   3884:         NEXT(mr) = 0;
                   3885:         MKND(NV(p),mr0,LEN(p),r);
                   3886:         SG(r) = SG(p);
                   3887:         return r;
                   3888:     }
1.6       noro     3889: }
                   3890:
1.53      noro     3891: int nd_sp(int mod,int trace,ND_pairs p,ND *rp)
1.11      noro     3892: {
1.157     noro     3893:     NM m1,m2;
                   3894:     NDV p1,p2;
                   3895:     ND t1,t2;
                   3896:     UINT *lcm;
                   3897:     P gp,tp;
1.167     noro     3898:     Q g,t,iq;
1.157     noro     3899:     int td;
1.167     noro     3900:     LIST hist;
                   3901:     NODE node;
                   3902:     DP d;
1.157     noro     3903:
                   3904:     if ( !mod && nd_demand ) {
                   3905:         p1 = ndv_load(p->i1); p2 = ndv_load(p->i2);
                   3906:     } else {
                   3907:         if ( trace ) {
                   3908:             p1 = nd_ps_trace[p->i1]; p2 = nd_ps_trace[p->i2];
                   3909:         } else {
                   3910:             p1 = nd_ps[p->i1]; p2 = nd_ps[p->i2];
                   3911:         }
                   3912:     }
                   3913:     lcm = LCM(p);
                   3914:     NEWNM(m1); ndl_sub(lcm,HDL(p1),DL(m1));
                   3915:     if ( ndl_check_bound2(p->i1,DL(m1)) ) {
                   3916:         FREENM(m1); return 0;
                   3917:     }
                   3918:     NEWNM(m2); ndl_sub(lcm,HDL(p2),DL(m2));
                   3919:     if ( ndl_check_bound2(p->i2,DL(m2)) ) {
                   3920:         FREENM(m1); FREENM(m2); return 0;
                   3921:     }
                   3922:
                   3923:     if ( mod == -1 ) {
                   3924:         CM(m1) = HCM(p2); CM(m2) = _chsgnsf(HCM(p1));
                   3925:     } else if ( mod ) {
                   3926:         CM(m1) = HCM(p2); CM(m2) = mod-HCM(p1);
                   3927:     } else if ( nd_vc ) {
                   3928:         ezgcdpz(nd_vc,HCP(p1),HCP(p2),&gp);
                   3929:         divsp(nd_vc,HCP(p2),gp,&CP(m1));
                   3930:         divsp(nd_vc,HCP(p1),gp,&tp); chsgnp(tp,&CP(m2));
                   3931:     } else {
                   3932:         igcd_cofactor(HCQ(p1),HCQ(p2),&g,&t,&CQ(m1)); chsgnq(t,&CQ(m2));
                   3933:     }
                   3934:     t1 = ndv_mul_nm(mod,m1,p1); t2 = ndv_mul_nm(mod,m2,p2);
                   3935:     *rp = nd_add(mod,t1,t2);
1.172   ! noro     3936:     if ( nd_gentrace ) {
1.167     noro     3937:         /* nd_tracelist is initialized */
                   3938:         STOQ(p->i1,iq); nmtodp(mod,m1,&d); node = mknode(4,ONE,iq,d,ONE);
                   3939:         MKLIST(hist,node); MKNODE(nd_tracelist,hist,0);
                   3940:         STOQ(p->i2,iq); nmtodp(mod,m2,&d); node = mknode(4,ONE,iq,d,ONE);
                   3941:         MKLIST(hist,node); MKNODE(node,hist,nd_tracelist);
                   3942:         nd_tracelist = node;
                   3943:     }
1.157     noro     3944:     FREENM(m1); FREENM(m2);
                   3945:     return 1;
1.11      noro     3946: }
                   3947:
1.19      noro     3948: void ndv_mul_c(int mod,NDV p,int mul)
1.11      noro     3949: {
1.157     noro     3950:     NMV m;
                   3951:     int c,c1,len,i;
1.11      noro     3952:
1.157     noro     3953:     if ( !p ) return;
                   3954:     len = LEN(p);
                   3955:     if ( mod == -1 )
                   3956:         for ( m = BDY(p), i = 0; i < len; i++, NMV_ADV(m) )
                   3957:             CM(m) = _mulsf(CM(m),mul);
                   3958:     else
                   3959:         for ( m = BDY(p), i = 0; i < len; i++, NMV_ADV(m) ) {
                   3960:             c1 = CM(m); DMAR(c1,mul,0,mod,c); CM(m) = c;
                   3961:         }
1.11      noro     3962: }
                   3963:
1.113     noro     3964: void ndv_mul_c_q(NDV p,Q mul)
1.16      noro     3965: {
1.157     noro     3966:     NMV m;
                   3967:     Q c;
                   3968:     int len,i;
                   3969:
                   3970:     if ( !p ) return;
                   3971:     len = LEN(p);
                   3972:     for ( m = BDY(p), i = 0; i < len; i++, NMV_ADV(m) ) {
                   3973:         mulq(CQ(m),mul,&c); CQ(m) = c;
                   3974:     }
1.16      noro     3975: }
                   3976:
1.55      noro     3977: ND weyl_ndv_mul_nm(int mod,NM m0,NDV p) {
1.157     noro     3978:     int n2,i,j,l,n,tlen;
                   3979:     UINT *d0;
                   3980:     NM *tab,*psum;
                   3981:     ND s,r;
                   3982:     NM t;
                   3983:     NMV m1;
                   3984:
                   3985:     if ( !p ) return 0;
                   3986:     n = NV(p); n2 = n>>1;
                   3987:     d0 = DL(m0);
                   3988:     l = LEN(p);
                   3989:     for ( i = 0, tlen = 1; i < n2; i++ ) tlen *= (GET_EXP(d0,n2+i)+1);
                   3990:     tab = (NM *)ALLOCA(tlen*sizeof(NM));
                   3991:     psum = (NM *)ALLOCA(tlen*sizeof(NM));
                   3992:     for ( i = 0; i < tlen; i++ ) psum[i] = 0;
                   3993:     m1 = (NMV)(((char *)BDY(p))+nmv_adv*(l-1));
                   3994:     for ( i = l-1; i >= 0; i--, NMV_PREV(m1) ) {
                   3995:         /* m0(NM) * m1(NMV) => tab(NM) */
                   3996:         weyl_mul_nm_nmv(n,mod,m0,m1,tab,tlen);
                   3997:         for ( j = 0; j < tlen; j++ ) {
                   3998:             if ( tab[j] ) {
                   3999:                 NEXT(tab[j]) = psum[j];    psum[j] = tab[j];
                   4000:             }
                   4001:         }
                   4002:     }
                   4003:     for ( i = tlen-1, r = 0; i >= 0; i-- )
                   4004:         if ( psum[i] ) {
                   4005:             for ( j = 0, t = psum[i]; t; t = NEXT(t), j++ );
                   4006:             MKND(n,psum[i],j,s);
                   4007:             r = nd_add(mod,r,s);
                   4008:         }
                   4009:     if ( r ) SG(r) = SG(p)+TD(d0);
                   4010:     return r;
1.55      noro     4011: }
                   4012:
1.56      noro     4013: /* product of monomials */
                   4014: /* XXX block order is not handled correctly */
                   4015:
1.55      noro     4016: void weyl_mul_nm_nmv(int n,int mod,NM m0,NMV m1,NM *tab,int tlen)
                   4017: {
1.157     noro     4018:     int i,n2,j,s,curlen,homo,h,a,b,k,l,u,min;
                   4019:     UINT *d0,*d1,*d,*dt,*ctab;
                   4020:     Q *ctab_q;
                   4021:     Q q,q1;
                   4022:     UINT c0,c1,c;
                   4023:     NM *p;
                   4024:     NM m,t;
                   4025:     int mpos;
                   4026:
                   4027:     for ( i = 0; i < tlen; i++ ) tab[i] = 0;
                   4028:     if ( !m0 || !m1 ) return;
                   4029:     d0 = DL(m0); d1 = DL(m1); n2 = n>>1;
1.166     noro     4030:     if ( nd_module )
                   4031:         if ( MPOS(d0) ) error("weyl_mul_nm_nmv : invalid operation");
                   4032:
1.157     noro     4033:     NEWNM(m); d = DL(m);
                   4034:     if ( mod ) {
                   4035:         c0 = CM(m0); c1 = CM(m1); DMAR(c0,c1,0,mod,c); CM(m) = c;
                   4036:     } else
                   4037:         mulq(CQ(m0),CQ(m1),&CQ(m));
                   4038:     for ( i = 0; i < nd_wpd; i++ ) d[i] = 0;
                   4039:     homo = n&1 ? 1 : 0;
                   4040:     if ( homo ) {
                   4041:         /* offset of h-degree */
                   4042:         h = GET_EXP(d0,n-1)+GET_EXP(d1,n-1);
                   4043:         PUT_EXP(DL(m),n-1,h);
                   4044:         TD(DL(m)) = h;
                   4045:         if ( nd_blockmask ) ndl_weight_mask(DL(m));
                   4046:     }
                   4047:     tab[0] = m;
                   4048:     NEWNM(m); d = DL(m);
                   4049:     for ( i = 0, curlen = 1; i < n2; i++ ) {
                   4050:         a = GET_EXP(d0,i); b = GET_EXP(d1,n2+i);
                   4051:         k = GET_EXP(d0,n2+i); l = GET_EXP(d1,i);
                   4052:         /* xi^a*(Di^k*xi^l)*Di^b */
                   4053:         a += l; b += k;
                   4054:         s = MUL_WEIGHT(a,i)+MUL_WEIGHT(b,n2+i);
                   4055:         if ( !k || !l ) {
                   4056:             for ( j = 0; j < curlen; j++ )
                   4057:                 if ( t = tab[j] ) {
                   4058:                     dt = DL(t);
                   4059:                     PUT_EXP(dt,i,a); PUT_EXP(dt,n2+i,b); TD(dt) += s;
                   4060:                     if ( nd_blockmask ) ndl_weight_mask(dt);
                   4061:                 }
                   4062:             curlen *= k+1;
                   4063:             continue;
                   4064:         }
                   4065:         min = MIN(k,l);
                   4066:         if ( mod ) {
                   4067:             ctab = (UINT *)ALLOCA((min+1)*sizeof(UINT));
                   4068:             mkwcm(k,l,mod,ctab);
                   4069:         } else {
                   4070:             ctab_q = (Q *)ALLOCA((min+1)*sizeof(Q));
                   4071:             mkwc(k,l,ctab_q);
                   4072:         }
                   4073:         for ( j = min; j >= 0; j-- ) {
                   4074:             for ( u = 0; u < nd_wpd; u++ ) d[u] = 0;
                   4075:             PUT_EXP(d,i,a-j); PUT_EXP(d,n2+i,b-j);
                   4076:             h = MUL_WEIGHT(a-j,i)+MUL_WEIGHT(b-j,n2+i);
                   4077:             if ( homo ) {
                   4078:                 TD(d) = s;
                   4079:                 PUT_EXP(d,n-1,s-h);
                   4080:             } else TD(d) = h;
                   4081:             if ( nd_blockmask ) ndl_weight_mask(d);
                   4082:             if ( mod ) c = ctab[j];
                   4083:             else q = ctab_q[j];
                   4084:             p = tab+curlen*j;
                   4085:             if ( j == 0 ) {
                   4086:                 for ( u = 0; u < curlen; u++, p++ ) {
                   4087:                     if ( tab[u] ) {
                   4088:                         ndl_addto(DL(tab[u]),d);
                   4089:                         if ( mod ) {
                   4090:                             c0 = CM(tab[u]); DMAR(c0,c,0,mod,c1); CM(tab[u]) = c1;
                   4091:                         } else {
                   4092:                             mulq(CQ(tab[u]),q,&q1); CQ(tab[u]) = q1;
                   4093:                         }
                   4094:                     }
                   4095:                 }
                   4096:             } else {
                   4097:                 for ( u = 0; u < curlen; u++, p++ ) {
                   4098:                     if ( tab[u] ) {
                   4099:                         NEWNM(t);
                   4100:                         ndl_add(DL(tab[u]),d,DL(t));
                   4101:                         if ( mod ) {
                   4102:                             c0 = CM(tab[u]); DMAR(c0,c,0,mod,c1); CM(t) = c1;
                   4103:                         } else
                   4104:                             mulq(CQ(tab[u]),q,&CQ(t));
                   4105:                         *p = t;
                   4106:                     }
                   4107:                 }
                   4108:             }
                   4109:         }
                   4110:         curlen *= k+1;
                   4111:     }
                   4112:     FREENM(m);
1.167     noro     4113:     if ( nd_module ) {
1.166     noro     4114:         mpos = MPOS(d1);
1.167     noro     4115:         for ( i = 0; i < tlen; i++ )
                   4116:             if ( tab[i] ) {
                   4117:                 d = DL(tab[i]);
                   4118:                 MPOS(d) = mpos;
                   4119:                 TD(d) = ndl_weight(d);
                   4120:             }
                   4121:     }
1.55      noro     4122: }
                   4123:
1.63      noro     4124: ND ndv_mul_nm_symbolic(NM m0,NDV p)
                   4125: {
1.157     noro     4126:     NM mr,mr0;
                   4127:     NMV m;
                   4128:     UINT *d,*dt,*dm;
                   4129:     int c,n,td,i,c1,c2,len;
                   4130:     Q q;
                   4131:     ND r;
                   4132:
                   4133:     if ( !p ) return 0;
                   4134:     else {
                   4135:         n = NV(p); m = BDY(p);
                   4136:         d = DL(m0);
                   4137:         len = LEN(p);
                   4138:         mr0 = 0;
                   4139:         td = TD(d);
                   4140:         c = CM(m0);
                   4141:         for ( i = 0; i < len; i++, NMV_ADV(m) ) {
                   4142:             NEXTNM(mr0,mr);
                   4143:             CM(mr) = 1;
                   4144:             ndl_add(DL(m),d,DL(mr));
                   4145:         }
                   4146:         NEXT(mr) = 0;
                   4147:         MKND(NV(p),mr0,len,r);
                   4148:         SG(r) = SG(p) + TD(d);
                   4149:         return r;
                   4150:     }
1.63      noro     4151: }
                   4152:
1.55      noro     4153: ND ndv_mul_nm(int mod,NM m0,NDV p)
1.9       noro     4154: {
1.157     noro     4155:     NM mr,mr0;
                   4156:     NMV m;
                   4157:     UINT *d,*dt,*dm;
                   4158:     int c,n,td,i,c1,c2,len;
                   4159:     P q;
                   4160:     ND r;
                   4161:
                   4162:     if ( !p ) return 0;
                   4163:     else if ( do_weyl )
                   4164:         if ( mod == -1 )
                   4165:             error("ndv_mul_nm : not implemented (weyl)");
                   4166:         else
                   4167:             return weyl_ndv_mul_nm(mod,m0,p);
                   4168:     else {
                   4169:         n = NV(p); m = BDY(p);
                   4170:         d = DL(m0);
                   4171:         len = LEN(p);
                   4172:         mr0 = 0;
                   4173:         td = TD(d);
                   4174:         if ( mod == -1 ) {
                   4175:             c = CM(m0);
                   4176:             for ( i = 0; i < len; i++, NMV_ADV(m) ) {
                   4177:                 NEXTNM(mr0,mr);
                   4178:                 CM(mr) = _mulsf(CM(m),c);
                   4179:                 ndl_add(DL(m),d,DL(mr));
                   4180:             }
                   4181:         } else if ( mod ) {
                   4182:             c = CM(m0);
                   4183:             for ( i = 0; i < len; i++, NMV_ADV(m) ) {
                   4184:                 NEXTNM(mr0,mr);
                   4185:                 c1 = CM(m);
                   4186:                 DMAR(c1,c,0,mod,c2);
                   4187:                 CM(mr) = c2;
                   4188:                 ndl_add(DL(m),d,DL(mr));
                   4189:             }
                   4190:         } else {
                   4191:             q = CP(m0);
                   4192:             for ( i = 0; i < len; i++, NMV_ADV(m) ) {
                   4193:                 NEXTNM(mr0,mr);
                   4194:                 mulp(nd_vc,CP(m),q,&CP(mr));
                   4195:                 ndl_add(DL(m),d,DL(mr));
                   4196:             }
                   4197:         }
                   4198:         NEXT(mr) = 0;
                   4199:         MKND(NV(p),mr0,len,r);
                   4200:         SG(r) = SG(p) + TD(d);
                   4201:         return r;
                   4202:     }
1.4       noro     4203: }
                   4204:
1.104     noro     4205: ND nd_quo(int mod,PGeoBucket bucket,NDV d)
1.99      noro     4206: {
1.157     noro     4207:     NM mq0,mq;
                   4208:     NMV tm;
                   4209:     Q q;
                   4210:     int i,nv,sg,c,c1,c2,hindex;
                   4211:     ND p,t,r;
                   4212:     N tnm;
                   4213:
                   4214:     if ( bucket->m < 0 ) return 0;
                   4215:     else {
                   4216:         nv = NV(d);
                   4217:         mq0 = 0;
                   4218:         tm = (NMV)ALLOCA(nmv_adv);
                   4219:         while ( 1 ) {
                   4220:             hindex = mod?head_pbucket(mod,bucket):head_pbucket_q(bucket);
                   4221:             if ( hindex < 0 ) break;
                   4222:             p = bucket->body[hindex];
                   4223:             NEXTNM(mq0,mq);
                   4224:             ndl_sub(HDL(p),HDL(d),DL(mq));
                   4225:             ndl_copy(DL(mq),DL(tm));
                   4226:             if ( mod ) {
                   4227:                 c1 = invm(HCM(d),mod); c2 = HCM(p);
                   4228:                 DMAR(c1,c2,0,mod,c); CM(mq) = c;
                   4229:                 CM(tm) = mod-c;
                   4230:             } else {
                   4231:                 divsn(NM(HCQ(p)),NM(HCQ(d)),&tnm);
                   4232:                 NTOQ(tnm,SGN(HCQ(p))*SGN(HCQ(d)),CQ(mq));
                   4233:                 chsgnq(CQ(mq),&CQ(tm));
                   4234:             }
                   4235:             t = ndv_mul_nmv_trunc(mod,tm,d,HDL(d));
                   4236:             bucket->body[hindex] = nd_remove_head(p);
                   4237:             t = nd_remove_head(t);
                   4238:             add_pbucket(mod,bucket,t);
                   4239:         }
                   4240:         if ( !mq0 )
                   4241:             r = 0;
                   4242:         else {
                   4243:             NEXT(mq) = 0;
                   4244:             for ( i = 0, mq = mq0; mq; mq = NEXT(mq), i++ );
                   4245:             MKND(nv,mq0,i,r);
                   4246:             /* XXX */
                   4247:             SG(r) = HTD(r);
                   4248:         }
                   4249:         return r;
                   4250:     }
1.99      noro     4251: }
                   4252:
1.43      noro     4253: void ndv_realloc(NDV p,int obpe,int oadv,EPOS oepos)
1.11      noro     4254: {
1.157     noro     4255:     NMV m,mr,mr0,t;
                   4256:     int len,i,k;
1.11      noro     4257:
1.157     noro     4258:     if ( !p ) return;
                   4259:     m = BDY(p); len = LEN(p);
                   4260:     mr0 = nmv_adv>oadv?(NMV)REALLOC(BDY(p),len*nmv_adv):BDY(p);
                   4261:     m = (NMV)((char *)mr0+(len-1)*oadv);
                   4262:     mr = (NMV)((char *)mr0+(len-1)*nmv_adv);
                   4263:     t = (NMV)ALLOCA(nmv_adv);
                   4264:     for ( i = 0; i < len; i++, NMV_OPREV(m), NMV_PREV(mr) ) {
                   4265:         CQ(t) = CQ(m);
                   4266:         for ( k = 0; k < nd_wpd; k++ ) DL(t)[k] = 0;
                   4267:         ndl_reconstruct(DL(m),DL(t),obpe,oepos);
                   4268:         CQ(mr) = CQ(t);
                   4269:         ndl_copy(DL(t),DL(mr));
                   4270:     }
                   4271:     BDY(p) = mr0;
1.61      noro     4272: }
                   4273:
                   4274: NDV ndv_dup_realloc(NDV p,int obpe,int oadv,EPOS oepos)
                   4275: {
1.157     noro     4276:     NMV m,mr,mr0;
                   4277:     int len,i;
                   4278:     NDV r;
                   4279:
                   4280:     if ( !p ) return 0;
                   4281:     m = BDY(p); len = LEN(p);
                   4282:     mr0 = mr = (NMV)MALLOC(len*nmv_adv);
                   4283:     for ( i = 0; i < len; i++, NMV_OADV(m), NMV_ADV(mr) ) {
                   4284:         ndl_zero(DL(mr));
                   4285:         ndl_reconstruct(DL(m),DL(mr),obpe,oepos);
                   4286:         CQ(mr) = CQ(m);
                   4287:     }
                   4288:     MKNDV(NV(p),mr0,len,r);
                   4289:     SG(r) = SG(p);
                   4290:     return r;
1.11      noro     4291: }
                   4292:
1.61      noro     4293: /* duplicate p */
                   4294:
                   4295: NDV ndv_dup(int mod,NDV p)
1.3       noro     4296: {
1.157     noro     4297:     NDV d;
                   4298:     NMV t,m,m0;
                   4299:     int i,len;
                   4300:
                   4301:     if ( !p ) return 0;
                   4302:     len = LEN(p);
                   4303:     m0 = m = (NMV)(mod?MALLOC_ATOMIC(len*nmv_adv):MALLOC(len*nmv_adv));
                   4304:     for ( t = BDY(p), i = 0; i < len; i++, NMV_ADV(t), NMV_ADV(m) ) {
                   4305:         ndl_copy(DL(t),DL(m));
                   4306:         CQ(m) = CQ(t);
                   4307:     }
                   4308:     MKNDV(NV(p),m0,len,d);
                   4309:     SG(d) = SG(p);
                   4310:     return d;
1.23      noro     4311: }
                   4312:
1.63      noro     4313: ND nd_dup(ND p)
                   4314: {
1.157     noro     4315:     ND d;
                   4316:     NM t,m,m0;
1.63      noro     4317:
1.157     noro     4318:     if ( !p ) return 0;
                   4319:     for ( m0 = 0, t = BDY(p); t; t = NEXT(t) ) {
                   4320:         NEXTNM(m0,m);
                   4321:         ndl_copy(DL(t),DL(m));
                   4322:         CQ(m) = CQ(t);
                   4323:     }
                   4324:     if ( m0 ) NEXT(m) = 0;
                   4325:     MKND(NV(p),m0,LEN(p),d);
                   4326:     SG(d) = SG(p);
                   4327:     return d;
1.63      noro     4328: }
                   4329:
1.61      noro     4330: /* XXX if p->len == 0 then it represents 0 */
                   4331:
                   4332: void ndv_mod(int mod,NDV p)
                   4333: {
1.157     noro     4334:     NMV t,d;
                   4335:     int r,s,u;
                   4336:     int i,len,dlen;
                   4337:     P cp;
                   4338:     Q c;
                   4339:     Obj gfs;
                   4340:
                   4341:     if ( !p ) return;
                   4342:     len = LEN(p);
                   4343:     dlen = 0;
                   4344:     if ( mod == -1 )
                   4345:         for ( t = d = BDY(p), i = 0; i < len; i++, NMV_ADV(t) ) {
                   4346:             simp_ff((Obj)CP(t),&gfs);
                   4347:             r = FTOIF(CONT((GFS)gfs));
                   4348:             CM(d) = r;
                   4349:             ndl_copy(DL(t),DL(d));
                   4350:             NMV_ADV(d);
                   4351:             dlen++;
                   4352:         }
                   4353:     else
                   4354:         for ( t = d = BDY(p), i = 0; i < len; i++, NMV_ADV(t) ) {
                   4355:             if ( nd_vc ) {
                   4356:                 nd_subst_vector(nd_vc,CP(t),nd_subst,&cp);
                   4357:                 c = (Q)cp;
                   4358:             } else
                   4359:                 c = CQ(t);
                   4360:             r = rem(NM(c),mod);
                   4361:             if ( r ) {
                   4362:                 if ( SGN(c) < 0 )
                   4363:                     r = mod-r;
                   4364:                 if ( DN(c) ) {
                   4365:                     s = rem(DN(c),mod);
                   4366:                     if ( !s )
                   4367:                         error("ndv_mod : division by 0");
                   4368:                     s = invm(s,mod);
                   4369:                     DMAR(r,s,0,mod,u); r = u;
                   4370:                 }
                   4371:                 CM(d) = r;
                   4372:                 ndl_copy(DL(t),DL(d));
                   4373:                 NMV_ADV(d);
                   4374:                 dlen++;
                   4375:             }
                   4376:         }
                   4377:     LEN(p) = dlen;
1.61      noro     4378: }
                   4379:
                   4380: NDV ptondv(VL vl,VL dvl,P p)
                   4381: {
1.157     noro     4382:     ND nd;
                   4383:
                   4384:     nd = ptond(vl,dvl,p);
                   4385:     return ndtondv(0,nd);
                   4386: }
1.61      noro     4387:
1.157     noro     4388: void pltozpl(LIST l,Q *cont,LIST *pp)
                   4389: {
                   4390:     NODE nd,nd1;
                   4391:     int n;
                   4392:     P *pl;
                   4393:     Q *cl;
                   4394:     int i;
                   4395:     P dmy;
                   4396:     Q dvr;
                   4397:     LIST r;
                   4398:
                   4399:     nd = BDY(l); n = length(nd);
                   4400:     pl = (P *)ALLOCA(n*sizeof(P));
                   4401:     cl = (Q *)ALLOCA(n*sizeof(P));
                   4402:     for ( i = 0; i < n; i++, nd = NEXT(nd) )
                   4403:         ptozp((P)BDY(nd),1,&cl[i],&dmy);
                   4404:     qltozl(cl,n,&dvr);
                   4405:     nd = BDY(l);
                   4406:     for ( i = 0; i < n; i++, nd = NEXT(nd) ) {
                   4407:         divsp(CO,(P)BDY(nd),(P)dvr,&pl[i]);
                   4408:     }
                   4409:     nd = 0;
                   4410:     for ( i = n-1; i >= 0; i-- ) {
                   4411:         MKNODE(nd1,pl[i],nd); nd = nd1;
                   4412:     }
                   4413:     MKLIST(r,nd);
                   4414:     *pp = r;
                   4415: }
                   4416:
                   4417: /* (a1,a2,...,an) -> a1*e(1)+...+an*e(n) */
                   4418:
                   4419: NDV pltondv(VL vl,VL dvl,LIST p)
                   4420: {
                   4421:     int i;
                   4422:     NODE t;
                   4423:     ND r,ri;
                   4424:     NM m;
                   4425:
                   4426:     if ( !nd_module ) error("pltond : module order must be set");
                   4427:     r = 0;
                   4428:     for ( i = 1, t = BDY(p); t; t = NEXT(t), i++ ) {
                   4429:         ri = ptond(vl,dvl,(P)BDY(t));
1.163     noro     4430:         if ( ri )
                   4431:             for ( m = BDY(ri); m; m = NEXT(m) ) {
1.167     noro     4432:                 MPOS(DL(m)) = i;
                   4433:                 TD(DL(m)) = ndl_weight(DL(m));
1.163     noro     4434:                 if ( nd_blockmask ) ndl_weight_mask(DL(m));
                   4435:             }
1.157     noro     4436:         r = nd_add(0,r,ri);
                   4437:     }
                   4438:     return ndtondv(0,r);
1.61      noro     4439: }
                   4440:
                   4441: ND ptond(VL vl,VL dvl,P p)
1.23      noro     4442: {
1.157     noro     4443:     int n,i,j,k,e;
                   4444:     VL tvl;
                   4445:     V v;
                   4446:     DCP dc;
                   4447:     DCP *w;
                   4448:     ND r,s,t,u;
                   4449:     P x;
                   4450:     int c;
                   4451:     UINT *d;
                   4452:     NM m,m0;
                   4453:
                   4454:     if ( !p )
                   4455:         return 0;
                   4456:     else if ( NUM(p) ) {
                   4457:         NEWNM(m);
                   4458:         ndl_zero(DL(m));
                   4459:         CQ(m) = (Q)p;
                   4460:         NEXT(m) = 0;
                   4461:         MKND(nd_nvar,m,1,r);
                   4462:         SG(r) = 0;
                   4463:         return r;
                   4464:     } else {
                   4465:         for ( dc = DC(p), k = 0; dc; dc = NEXT(dc), k++ );
                   4466:         w = (DCP *)ALLOCA(k*sizeof(DCP));
                   4467:         for ( dc = DC(p), j = 0; j < k; dc = NEXT(dc), j++ ) w[j] = dc;
                   4468:         for ( i = 0, tvl = dvl, v = VR(p);
                   4469:             tvl && tvl->v != v; tvl = NEXT(tvl), i++ );
                   4470:         if ( !tvl ) {
                   4471:             for ( j = k-1, s = 0, MKV(v,x); j >= 0; j-- ) {
                   4472:                 t = ptond(vl,dvl,COEF(w[j]));
                   4473:                 pwrp(vl,x,DEG(w[j]),&p);
                   4474:                 nd_mul_c_p(CO,t,p); s = nd_add(0,s,t);
                   4475:             }
                   4476:             return s;
                   4477:         } else {
                   4478:             NEWNM(m0); d = DL(m0);
                   4479:             for ( j = k-1, s = 0; j >= 0; j-- ) {
                   4480:                 ndl_zero(d); e = QTOS(DEG(w[j])); PUT_EXP(d,i,e);
                   4481:                 TD(d) = MUL_WEIGHT(e,i);
                   4482:                 if ( nd_blockmask) ndl_weight_mask(d);
                   4483:                 if ( nd_module ) MPOS(d) = 0;
                   4484:                 t = ptond(vl,dvl,COEF(w[j]));
                   4485:                 for ( m = BDY(t); m; m = NEXT(m) )
                   4486:                     ndl_addto(DL(m),d);
                   4487:                 SG(t) += TD(d);
                   4488:                 s = nd_add(0,s,t);
                   4489:             }
                   4490:             FREENM(m0);
                   4491:             return s;
                   4492:         }
                   4493:     }
1.61      noro     4494: }
                   4495:
                   4496: P ndvtop(int mod,VL vl,VL dvl,NDV p)
                   4497: {
1.157     noro     4498:     VL tvl;
                   4499:     int len,n,j,i,e;
                   4500:     NMV m;
                   4501:     Q q;
                   4502:     P c;
                   4503:     UINT *d;
                   4504:     P s,r,u,t,w;
                   4505:     GFS gfs;
                   4506:
                   4507:     if ( !p ) return 0;
                   4508:     else {
                   4509:         len = LEN(p);
                   4510:         n = NV(p);
                   4511:         m = (NMV)(((char *)BDY(p))+nmv_adv*(len-1));
                   4512:         for ( j = len-1, s = 0; j >= 0; j--, NMV_PREV(m) ) {
                   4513:             if ( mod == -1 ) {
                   4514:                 e = IFTOF(CM(m)); MKGFS(e,gfs); c = (P)gfs;
                   4515:             } else if ( mod ) {
                   4516:                 STOQ(CM(m),q); c = (P)q;
                   4517:             } else
                   4518:                 c = CP(m);
                   4519:             d = DL(m);
                   4520:             for ( i = 0, t = c, tvl = dvl; i < n; tvl = NEXT(tvl), i++ ) {
                   4521:                 MKV(tvl->v,r); e = GET_EXP(d,i); STOQ(e,q);
                   4522:                 pwrp(vl,r,q,&u); mulp(vl,t,u,&w); t = w;
                   4523:             }
                   4524:             addp(vl,s,t,&u); s = u;
                   4525:         }
                   4526:         return s;
                   4527:     }
                   4528: }
                   4529:
                   4530: LIST ndvtopl(int mod,VL vl,VL dvl,NDV p,int rank)
                   4531: {
                   4532:     VL tvl;
                   4533:     int len,n,j,i,e;
                   4534:     NMV m;
                   4535:     Q q;
                   4536:     P c;
                   4537:     UINT *d;
                   4538:     P s,r,u,t,w;
                   4539:     GFS gfs;
                   4540:     P *a;
                   4541:     LIST l;
                   4542:     NODE nd,nd1;
                   4543:
                   4544:     if ( !p ) return 0;
                   4545:     else {
                   4546:         a = (P *)ALLOCA((rank+1)*sizeof(P));
                   4547:         for ( i = 0; i <= rank; i++ ) a[i] = 0;
                   4548:         len = LEN(p);
                   4549:         n = NV(p);
                   4550:         m = (NMV)(((char *)BDY(p))+nmv_adv*(len-1));
                   4551:         for ( j = len-1; j >= 0; j--, NMV_PREV(m) ) {
                   4552:             if ( mod == -1 ) {
                   4553:                 e = IFTOF(CM(m)); MKGFS(e,gfs); c = (P)gfs;
                   4554:             } else if ( mod ) {
                   4555:                 STOQ(CM(m),q); c = (P)q;
                   4556:             } else
                   4557:                 c = CP(m);
                   4558:             d = DL(m);
                   4559:             for ( i = 0, t = c, tvl = dvl; i < n; tvl = NEXT(tvl), i++ ) {
                   4560:                 MKV(tvl->v,r); e = GET_EXP(d,i); STOQ(e,q);
                   4561:                 pwrp(vl,r,q,&u); mulp(vl,t,u,&w); t = w;
                   4562:             }
                   4563:             addp(vl,a[MPOS(d)],t,&u); a[MPOS(d)] = u;
                   4564:         }
                   4565:         nd = 0;
                   4566:         for ( i = rank; i > 0; i-- ) {
                   4567:             MKNODE(nd1,a[i],nd); nd = nd1;
                   4568:         }
                   4569:         MKLIST(l,nd);
                   4570:         return l;
                   4571:     }
1.3       noro     4572: }
                   4573:
1.61      noro     4574: NDV ndtondv(int mod,ND p)
1.11      noro     4575: {
1.157     noro     4576:     NDV d;
                   4577:     NMV m,m0;
                   4578:     NM t;
                   4579:     int i,len;
                   4580:
                   4581:     if ( !p ) return 0;
                   4582:     len = LEN(p);
                   4583:     if ( mod )
                   4584:         m0 = m = (NMV)GC_malloc_atomic_ignore_off_page(len*nmv_adv);
                   4585:     else
                   4586:         m0 = m = MALLOC(len*nmv_adv);
1.103     noro     4587: #if 0
1.157     noro     4588:     ndv_alloc += nmv_adv*len;
1.103     noro     4589: #endif
1.157     noro     4590:     for ( t = BDY(p), i = 0; t; t = NEXT(t), i++, NMV_ADV(m) ) {
                   4591:         ndl_copy(DL(t),DL(m));
                   4592:         CQ(m) = CQ(t);
                   4593:     }
                   4594:     MKNDV(NV(p),m0,len,d);
                   4595:     SG(d) = SG(p);
                   4596:     return d;
1.11      noro     4597: }
                   4598:
1.61      noro     4599: ND ndvtond(int mod,NDV p)
1.11      noro     4600: {
1.157     noro     4601:     ND d;
                   4602:     NM m,m0;
                   4603:     NMV t;
                   4604:     int i,len;
                   4605:
                   4606:     if ( !p ) return 0;
                   4607:     m0 = 0;
                   4608:     len = p->len;
                   4609:     for ( t = BDY(p), i = 0; i < len; NMV_ADV(t), i++ ) {
                   4610:         NEXTNM(m0,m);
                   4611:         ndl_copy(DL(t),DL(m));
                   4612:         CQ(m) = CQ(t);
                   4613:     }
                   4614:     NEXT(m) = 0;
                   4615:     MKND(NV(p),m0,len,d);
                   4616:     SG(d) = SG(p);
                   4617:     return d;
1.11      noro     4618: }
                   4619:
1.3       noro     4620: void ndv_print(NDV p)
                   4621: {
1.157     noro     4622:     NMV m;
                   4623:     int i,len;
1.3       noro     4624:
1.157     noro     4625:     if ( !p ) printf("0\n");
                   4626:     else {
                   4627:         len = LEN(p);
                   4628:         for ( m = BDY(p), i = 0; i < len; i++, NMV_ADV(m) ) {
                   4629:             if ( CM(m) & 0x80000000 ) printf("+@_%d*",IFTOF(CM(m)));
                   4630:             else printf("+%d*",CM(m));
                   4631:             ndl_print(DL(m));
                   4632:         }
                   4633:         printf("\n");
                   4634:     }
1.16      noro     4635: }
                   4636:
1.113     noro     4637: void ndv_print_q(NDV p)
1.16      noro     4638: {
1.157     noro     4639:     NMV m;
                   4640:     int i,len;
1.16      noro     4641:
1.157     noro     4642:     if ( !p ) printf("0\n");
                   4643:     else {
                   4644:         len = LEN(p);
                   4645:         for ( m = BDY(p), i = 0; i < len; i++, NMV_ADV(m) ) {
                   4646:             printf("+");
                   4647:             printexpr(CO,(Obj)CQ(m));
                   4648:             printf("*");
                   4649:             ndl_print(DL(m));
                   4650:         }
                   4651:         printf("\n");
                   4652:     }
1.25      noro     4653: }
                   4654:
1.167     noro     4655: NODE ndv_reducebase(NODE x,int *perm)
1.27      noro     4656: {
1.157     noro     4657:     int len,i,j;
1.167     noro     4658:     NDVI w;
1.157     noro     4659:     NODE t,t0;
                   4660:
                   4661:     len = length(x);
1.167     noro     4662:     w = (NDVI)ALLOCA(len*sizeof(struct oNDVI));
                   4663:     for ( i = 0, t = x; i < len; i++, t = NEXT(t) ) {
                   4664:         w[i].p = BDY(t); w[i].i = perm[i];
                   4665:     }
1.157     noro     4666:     for ( i = 0; i < len; i++ ) {
                   4667:         for ( j = 0; j < i; j++ ) {
1.167     noro     4668:             if ( w[i].p && w[j].p )
                   4669:                 if ( ndl_reducible(HDL(w[i].p),HDL(w[j].p)) ) w[i].p = 0;
                   4670:                 else if ( ndl_reducible(HDL(w[j].p),HDL(w[i].p)) ) w[j].p = 0;
1.157     noro     4671:         }
                   4672:     }
1.167     noro     4673:     for ( i = j = 0, t0 = 0; i < len; i++ ) {
                   4674:         if ( w[i].p ) {
                   4675:             NEXTNODE(t0,t); BDY(t) = (pointer)w[i].p;
                   4676:             perm[j++] = w[i].i;
                   4677:         }
1.157     noro     4678:     }
                   4679:     NEXT(t) = 0; x = t0;
                   4680:     return x;
1.11      noro     4681: }
1.32      noro     4682:
1.43      noro     4683: /* XXX incomplete */
                   4684:
1.32      noro     4685: void nd_init_ord(struct order_spec *ord)
                   4686: {
1.157     noro     4687:     nd_module = (ord->id >= 256);
                   4688:     switch ( ord->id ) {
                   4689:         case 0:
                   4690:             switch ( ord->ord.simple ) {
                   4691:                 case 0:
                   4692:                     nd_dcomp = 1;
                   4693:                     nd_isrlex = 1;
                   4694:                     break;
                   4695:                 case 1:
                   4696:                     nd_dcomp = 1;
                   4697:                     nd_isrlex = 0;
                   4698:                     break;
                   4699:                 case 2:
                   4700:                     nd_dcomp = 0;
                   4701:                     nd_isrlex = 0;
                   4702:                     ndl_compare_function = ndl_lex_compare;
                   4703:                     break;
                   4704:                 case 11:
                   4705:                     /* XXX */
                   4706:                     nd_dcomp = 0;
                   4707:                     nd_isrlex = 1;
                   4708:                     ndl_compare_function = ndl_ww_lex_compare;
                   4709:                     break;
                   4710:                 default:
                   4711:                     error("nd_gr : unsupported order");
                   4712:             }
                   4713:             break;
                   4714:         case 1:
                   4715:             /* block order */
                   4716:             /* XXX */
                   4717:             nd_dcomp = -1;
                   4718:             nd_isrlex = 0;
                   4719:             ndl_compare_function = ndl_block_compare;
                   4720:             break;
                   4721:         case 2:
                   4722:             /* matrix order */
                   4723:             /* XXX */
                   4724:             nd_dcomp = -1;
                   4725:             nd_isrlex = 0;
                   4726:             nd_matrix_len = ord->ord.matrix.row;
                   4727:             nd_matrix = ord->ord.matrix.matrix;
                   4728:             ndl_compare_function = ndl_matrix_compare;
                   4729:             break;
                   4730:         case 3:
                   4731:             /* composite order */
                   4732:             nd_dcomp = -1;
                   4733:             nd_isrlex = 0;
                   4734:             nd_worb_len = ord->ord.composite.length;
                   4735:             nd_worb = ord->ord.composite.w_or_b;
                   4736:             ndl_compare_function = ndl_composite_compare;
                   4737:             break;
                   4738:
                   4739:         /* module order */
                   4740:         case 256:
1.160     noro     4741:             nd_ispot = ord->ispot;
1.157     noro     4742:             nd_dcomp = -1;
                   4743:             switch ( ord->ord.simple ) {
                   4744:                 case 0:
1.167     noro     4745:                     nd_isrlex = 1;
1.157     noro     4746:                     ndl_compare_function = ndl_module_grlex_compare;
                   4747:                     break;
                   4748:                 case 1:
1.167     noro     4749:                     nd_isrlex = 0;
1.157     noro     4750:                     ndl_compare_function = ndl_module_glex_compare;
                   4751:                     break;
                   4752:                 case 2:
1.167     noro     4753:                     nd_isrlex = 0;
1.157     noro     4754:                     ndl_compare_function = ndl_module_lex_compare;
                   4755:                     break;
                   4756:                 default:
                   4757:                     error("nd_gr : unsupported order");
                   4758:             }
                   4759:             break;
                   4760:         case 257:
                   4761:             /* block order */
                   4762:             ndl_compare_function = ndl_module_block_compare;
                   4763:             break;
                   4764:         case 258:
                   4765:             /* matrix order */
                   4766:             nd_matrix_len = ord->ord.matrix.row;
                   4767:             nd_matrix = ord->ord.matrix.matrix;
                   4768:             ndl_compare_function = ndl_module_matrix_compare;
                   4769:             break;
                   4770:         case 259:
                   4771:             /* composite order */
                   4772:             nd_worb_len = ord->ord.composite.length;
                   4773:             nd_worb = ord->ord.composite.w_or_b;
                   4774:             ndl_compare_function = ndl_module_composite_compare;
                   4775:             break;
                   4776:     }
                   4777:     nd_ord = ord;
1.32      noro     4778: }
                   4779:
1.43      noro     4780: BlockMask nd_create_blockmask(struct order_spec *ord)
                   4781: {
1.157     noro     4782:     int n,i,j,s,l;
                   4783:     UINT *t;
                   4784:     BlockMask bm;
                   4785:
                   4786:     /* we only create mask table for block order */
1.164     noro     4787:     if ( ord->id != 1 && ord->id != 257 )
1.157     noro     4788:         return 0;
                   4789:     n = ord->ord.block.length;
                   4790:     bm = (BlockMask)MALLOC(sizeof(struct oBlockMask));
                   4791:     bm->n = n;
                   4792:     bm->order_pair = ord->ord.block.order_pair;
                   4793:     bm->mask = (UINT **)MALLOC(n*sizeof(UINT *));
                   4794:     for ( i = 0, s = 0; i < n; i++ ) {
                   4795:         bm->mask[i] = t = (UINT *)MALLOC_ATOMIC(nd_wpd*sizeof(UINT));
                   4796:         for ( j = 0; j < nd_wpd; j++ ) t[j] = 0;
                   4797:         l = bm->order_pair[i].length;
                   4798:         for ( j = 0; j < l; j++, s++ ) PUT_EXP(t,s,nd_mask0);
                   4799:     }
                   4800:     return bm;
1.57      noro     4801: }
                   4802:
                   4803: EPOS nd_create_epos(struct order_spec *ord)
                   4804: {
1.157     noro     4805:     int i,j,l,s,ord_l,ord_o;
                   4806:     EPOS epos;
                   4807:     struct order_pair *op;
                   4808:
                   4809:     epos = (EPOS)MALLOC_ATOMIC(nd_nvar*sizeof(struct oEPOS));
                   4810:     switch ( ord->id ) {
1.164     noro     4811:         case 0: case 256:
1.157     noro     4812:             if ( nd_isrlex ) {
                   4813:                 for ( i = 0; i < nd_nvar; i++ ) {
                   4814:                     epos[i].i = nd_exporigin + (nd_nvar-1-i)/nd_epw;
                   4815:                     epos[i].s = (nd_epw-((nd_nvar-1-i)%nd_epw)-1)*nd_bpe;
                   4816:                 }
                   4817:             } else {
                   4818:                 for ( i = 0; i < nd_nvar; i++ ) {
                   4819:                     epos[i].i = nd_exporigin + i/nd_epw;
                   4820:                     epos[i].s = (nd_epw-(i%nd_epw)-1)*nd_bpe;
                   4821:                 }
                   4822:             }
                   4823:             break;
1.164     noro     4824:         case 1: case 257:
1.157     noro     4825:             /* block order */
                   4826:             l = ord->ord.block.length;
                   4827:             op = ord->ord.block.order_pair;
                   4828:             for ( j = 0, s = 0; j < l; j++ ) {
                   4829:                 ord_o = op[j].order;
                   4830:                 ord_l = op[j].length;
                   4831:                 if ( !ord_o )
                   4832:                     for ( i = 0; i < ord_l; i++ ) {
                   4833:                         epos[s+i].i = nd_exporigin + (s+ord_l-i-1)/nd_epw;
                   4834:                         epos[s+i].s = (nd_epw-((s+ord_l-i-1)%nd_epw)-1)*nd_bpe;
                   4835:                     }
                   4836:                 else
                   4837:                     for ( i = 0; i < ord_l; i++ ) {
                   4838:                         epos[s+i].i = nd_exporigin + (s+i)/nd_epw;
                   4839:                         epos[s+i].s = (nd_epw-((s+i)%nd_epw)-1)*nd_bpe;
                   4840:                     }
                   4841:                 s += ord_l;
                   4842:             }
                   4843:             break;
                   4844:         case 2:
                   4845:             /* matrix order */
                   4846:         case 3:
                   4847:             /* composite order */
1.167     noro     4848:         default:
1.157     noro     4849:             for ( i = 0; i < nd_nvar; i++ ) {
                   4850:                 epos[i].i = nd_exporigin + i/nd_epw;
                   4851:                 epos[i].s = (nd_epw-(i%nd_epw)-1)*nd_bpe;
                   4852:             }
                   4853:             break;
                   4854:     }
                   4855:     return epos;
1.43      noro     4856: }
1.59      noro     4857:
                   4858: /* external interface */
                   4859:
                   4860: void nd_nf_p(P f,LIST g,LIST v,int m,struct order_spec *ord,P *rp)
                   4861: {
1.157     noro     4862:     NODE t,in0,in;
                   4863:     ND nd,nf;
                   4864:     NDV ndv;
                   4865:     VL vv,tv;
                   4866:     int stat,nvar,max,e;
                   4867:     union oNDC dn;
                   4868:     Q cont;
                   4869:     P pp;
                   4870:
                   4871:     if ( !f ) {
                   4872:         *rp = 0;
                   4873:         return;
                   4874:     }
                   4875:     pltovl(v,&vv);
                   4876:     for ( nvar = 0, tv = vv; tv; tv = NEXT(tv), nvar++ );
                   4877:
                   4878:     /* get the degree bound */
                   4879:     for ( t = BDY(g), max = 0; t; t = NEXT(t) )
                   4880:         for ( tv = vv; tv; tv = NEXT(tv) ) {
                   4881:             e = getdeg(tv->v,(P)BDY(t));
                   4882:             max = MAX(e,max);
                   4883:         }
                   4884:     for ( tv = vv; tv; tv = NEXT(tv) ) {
                   4885:         e = getdeg(tv->v,f);
                   4886:         max = MAX(e,max);
                   4887:     }
                   4888:
                   4889:     nd_init_ord(ord);
                   4890:     nd_setup_parameters(nvar,max);
                   4891:
                   4892:     /* conversion to ndv */
                   4893:     for ( in0 = 0, t = BDY(g); t; t = NEXT(t) ) {
                   4894:         NEXTNODE(in0,in);
                   4895:         ptozp((P)BDY(t),1,&cont,&pp);
                   4896:         BDY(in) = (pointer)ptondv(CO,vv,pp);
                   4897:         if ( m ) ndv_mod(m,(NDV)BDY(in));
                   4898:     }
                   4899:     NEXTNODE(in0,in);
                   4900:     BDY(in) = (pointer)ptondv(CO,vv,f);
                   4901:     if ( m ) ndv_mod(m,(NDV)BDY(in));
                   4902:     NEXT(in) = 0;
                   4903:
                   4904:     /* dont sort, dont removecont */
                   4905:     ndv_setup(m,0,in0,1,1);
                   4906:     nd_psn--;
                   4907:     nd_scale=2;
                   4908:     while ( 1 ) {
                   4909:         nd = (pointer)ndvtond(m,nd_ps[nd_psn]);
                   4910:         stat = nd_nf(m,0,nd,nd_ps,1,0,&nf);
                   4911:         if ( !stat ) {
                   4912:             nd_psn++;
                   4913:             nd_reconstruct(0,0);
                   4914:             nd_psn--;
                   4915:         } else
                   4916:             break;
                   4917:     }
                   4918:     *rp = ndvtop(m,CO,vv,ndtondv(m,nf));
1.63      noro     4919: }
                   4920:
                   4921: int nd_to_vect(int mod,UINT *s0,int n,ND d,UINT *r)
                   4922: {
1.157     noro     4923:     NM m;
                   4924:     UINT *t,*s;
                   4925:     int i;
                   4926:
                   4927:     for ( i = 0; i < n; i++ ) r[i] = 0;
                   4928:     for ( i = 0, s = s0, m = BDY(d); m; m = NEXT(m) ) {
                   4929:         t = DL(m);
                   4930:         for ( ; !ndl_equal(t,s); s += nd_wpd, i++ );
                   4931:         r[i] = CM(m);
                   4932:     }
                   4933:     for ( i = 0; !r[i]; i++ );
                   4934:     return i;
1.63      noro     4935: }
                   4936:
1.113     noro     4937: int nd_to_vect_q(UINT *s0,int n,ND d,Q *r)
1.74      noro     4938: {
1.157     noro     4939:     NM m;
                   4940:     UINT *t,*s;
                   4941:     int i;
                   4942:
                   4943:     for ( i = 0; i < n; i++ ) r[i] = 0;
                   4944:     for ( i = 0, s = s0, m = BDY(d); m; m = NEXT(m) ) {
                   4945:         t = DL(m);
                   4946:         for ( ; !ndl_equal(t,s); s += nd_wpd, i++ );
                   4947:         r[i] = CQ(m);
                   4948:     }
                   4949:     for ( i = 0; !r[i]; i++ );
                   4950:     return i;
1.74      noro     4951: }
                   4952:
1.129     noro     4953: Q *nm_ind_pair_to_vect(int mod,UINT *s0,int n,NM_ind_pair pair)
                   4954: {
1.157     noro     4955:     NM m;
                   4956:     NMV mr;
                   4957:     UINT *d,*t,*s;
                   4958:     NDV p;
                   4959:     int i,j,len;
                   4960:     Q *r;
                   4961:
                   4962:     m = pair->mul;
                   4963:     d = DL(m);
                   4964:     p = nd_ps[pair->index];
                   4965:     len = LEN(p);
                   4966:     r = (Q *)CALLOC(n,sizeof(Q));
                   4967:     t = (UINT *)ALLOCA(nd_wpd*sizeof(UINT));
                   4968:     for ( i = j = 0, s = s0, mr = BDY(p); j < len; j++, NMV_ADV(mr) ) {
                   4969:         ndl_add(d,DL(mr),t);
                   4970:         for ( ; !ndl_equal(t,s); s += nd_wpd, i++ );
                   4971:         r[i] = CQ(mr);
                   4972:     }
                   4973:     return r;
1.129     noro     4974: }
                   4975:
1.67      noro     4976: IndArray nm_ind_pair_to_vect_compress(int mod,UINT *s0,int n,NM_ind_pair pair)
1.64      noro     4977: {
1.157     noro     4978:     NM m;
                   4979:     NMV mr;
                   4980:     UINT *d,*t,*s;
                   4981:     NDV p;
                   4982:     unsigned char *ivc;
                   4983:     unsigned short *ivs;
                   4984:     UINT *v,*ivi,*s0v;
                   4985:     int i,j,len,prev,diff,cdiff;
                   4986:     IndArray r;
                   4987:
                   4988:     m = pair->mul;
                   4989:     d = DL(m);
                   4990:     p = nd_ps[pair->index];
                   4991:     len = LEN(p);
                   4992:     t = (UINT *)ALLOCA(nd_wpd*sizeof(UINT));
                   4993:     v = (unsigned int *)ALLOCA(len*sizeof(unsigned int));
                   4994:     for ( i = j = 0, s = s0, mr = BDY(p); j < len; j++, NMV_ADV(mr) ) {
                   4995:         ndl_add(d,DL(mr),t);
                   4996:         for ( ; !ndl_equal(t,s); s += nd_wpd, i++ );
                   4997:         v[j] = i;
                   4998:     }
                   4999:     r = (IndArray)MALLOC(sizeof(struct oIndArray));
                   5000:     r->head = v[0];
                   5001:     diff = 0;
                   5002:     for ( i = 1; i < len; i++ ) {
                   5003:         cdiff = v[i]-v[i-1]; diff = MAX(cdiff,diff);
                   5004:     }
                   5005:     if ( diff < 256 ) {
                   5006:         r->width = 1;
                   5007:         ivc = (unsigned char *)MALLOC_ATOMIC(len*sizeof(unsigned char));
                   5008:         r->index.c = ivc;
                   5009:         for ( i = 1, ivc[0] = 0; i < len; i++ ) ivc[i] = v[i]-v[i-1];
                   5010:     } else if ( diff < 65536 ) {
                   5011:         r->width = 2;
                   5012:         ivs = (unsigned short *)MALLOC_ATOMIC(len*sizeof(unsigned short));
                   5013:         r->index.s = ivs;
                   5014:         for ( i = 1, ivs[0] = 0; i < len; i++ ) ivs[i] = v[i]-v[i-1];
                   5015:     } else {
                   5016:         r->width = 4;
                   5017:         ivi = (unsigned int *)MALLOC_ATOMIC(len*sizeof(unsigned int));
                   5018:         r->index.i = ivi;
                   5019:         for ( i = 1, ivi[0] = 0; i < len; i++ ) ivi[i] = v[i]-v[i-1];
                   5020:     }
                   5021:     return r;
1.64      noro     5022: }
                   5023:
1.135     noro     5024: int compress_array(Q *svect,Q *cvect,int n)
                   5025: {
1.157     noro     5026:     int i,j;
1.135     noro     5027:
1.157     noro     5028:     for ( i = j = 0; i < n; i++ )
                   5029:         if ( svect[i] ) cvect[j++] = svect[i];
                   5030:     return j;
1.135     noro     5031: }
                   5032:
                   5033: void expand_array(Q *svect,Q *cvect,int n)
                   5034: {
1.157     noro     5035:     int i,j;
1.135     noro     5036:
1.157     noro     5037:     for ( i = j = 0; j < n;  i++  )
                   5038:         if ( svect[i] ) svect[i] = cvect[j++];
1.135     noro     5039: }
                   5040:
1.133     noro     5041: int ndv_reduce_vect_q(Q *svect,int trace,int col,IndArray *imat,NM_ind_pair *rp0,int nred)
1.107     noro     5042: {
1.157     noro     5043:     int i,j,k,len,pos,prev,nz;
                   5044:     Q cs,mcs,c1,c2,cr,gcd,t;
                   5045:     IndArray ivect;
                   5046:     unsigned char *ivc;
                   5047:     unsigned short *ivs;
                   5048:     unsigned int *ivi;
                   5049:     NDV redv;
                   5050:     NMV mr;
                   5051:     NODE rp;
                   5052:     int maxrs;
                   5053:     double hmag;
                   5054:     Q *cvect;
                   5055:
                   5056:     maxrs = 0;
                   5057:     for ( i = 0; i < col && !svect[i]; i++ );
                   5058:     if ( i == col ) return maxrs;
                   5059:     hmag = p_mag((P)svect[i])*nd_scale;
                   5060:     cvect = (Q *)ALLOCA(col*sizeof(Q));
                   5061:     for ( i = 0; i < nred; i++ ) {
                   5062:         ivect = imat[i];
                   5063:         k = ivect->head;
                   5064:         if ( svect[k] ) {
                   5065:             maxrs = MAX(maxrs,rp0[i]->sugar);
                   5066:             redv = trace?nd_ps_trace[rp0[i]->index]:nd_ps[rp0[i]->index];
                   5067:             len = LEN(redv); mr = BDY(redv);
                   5068:             igcd_cofactor(svect[k],CQ(mr),&gcd,&cs,&cr);
                   5069:             chsgnq(cs,&mcs);
                   5070:             if ( !UNIQ(cr) ) {
                   5071:                 for ( j = 0; j < col; j++ ) {
                   5072:                     mulq(svect[j],cr,&c1); svect[j] = c1;
                   5073:                 }
                   5074:             }
                   5075:             svect[k] = 0; prev = k;
                   5076:             switch ( ivect->width ) {
                   5077:                 case 1:
                   5078:                     ivc = ivect->index.c;
                   5079:                     for ( j = 1, NMV_ADV(mr); j < len; j++, NMV_ADV(mr) ) {
                   5080:                         pos = prev+ivc[j]; prev = pos;
                   5081:                         mulq(CQ(mr),mcs,&c2); addq(svect[pos],c2,&t); svect[pos] = t;
                   5082:                     }
                   5083:                     break;
                   5084:                 case 2:
                   5085:                     ivs = ivect->index.s;
                   5086:                     for ( j = 1, NMV_ADV(mr); j < len; j++, NMV_ADV(mr) ) {
                   5087:                         pos = prev+ivs[j]; prev = pos;
                   5088:                         mulq(CQ(mr),mcs,&c2); addq(svect[pos],c2,&t); svect[pos] = t;
                   5089:                     }
                   5090:                     break;
                   5091:                 case 4:
                   5092:                     ivi = ivect->index.i;
                   5093:                     for ( j = 1, NMV_ADV(mr); j < len; j++, NMV_ADV(mr) ) {
                   5094:                         pos = prev+ivi[j]; prev = pos;
                   5095:                         mulq(CQ(mr),mcs,&c2); addq(svect[pos],c2,&t); svect[pos] = t;
                   5096:                     }
                   5097:                     break;
                   5098:             }
                   5099:             for ( j = k+1; j < col && !svect[j]; j++ );
                   5100:             if ( j == col ) break;
                   5101:             if ( hmag && ((double)p_mag((P)svect[j]) > hmag) ) {
                   5102:                 nz = compress_array(svect,cvect,col);
                   5103:                 removecont_array((P *)cvect,nz,1);
                   5104:                 expand_array(svect,cvect,nz);
                   5105:                 hmag = ((double)p_mag((P)svect[j]))*nd_scale;
                   5106:             }
                   5107:         }
                   5108:     }
                   5109:     nz = compress_array(svect,cvect,col);
                   5110:     removecont_array((P *)cvect,nz,1);
                   5111:     expand_array(svect,cvect,nz);
                   5112:     if ( DP_Print ) {
                   5113:         fprintf(asir_out,"-"); fflush(asir_out);
                   5114:     }
                   5115:     return maxrs;
1.107     noro     5116: }
                   5117:
1.76      noro     5118: int ndv_reduce_vect(int m,UINT *svect,int col,IndArray *imat,NM_ind_pair *rp0,int nred)
1.65      noro     5119: {
1.157     noro     5120:     int i,j,k,len,pos,prev;
                   5121:     UINT c,c1,c2,c3,up,lo,dmy;
                   5122:     IndArray ivect;
                   5123:     unsigned char *ivc;
                   5124:     unsigned short *ivs;
                   5125:     unsigned int *ivi;
                   5126:     NDV redv;
                   5127:     NMV mr;
                   5128:     NODE rp;
                   5129:     int maxrs;
                   5130:
                   5131:     maxrs = 0;
                   5132:     for ( i = 0; i < nred; i++ ) {
                   5133:         ivect = imat[i];
                   5134:         k = ivect->head; svect[k] %= m;
                   5135:         if ( c = svect[k] ) {
                   5136:             maxrs = MAX(maxrs,rp0[i]->sugar);
                   5137:             c = m-c; redv = nd_ps[rp0[i]->index];
                   5138:             len = LEN(redv); mr = BDY(redv);
                   5139:             svect[k] = 0; prev = k;
                   5140:             switch ( ivect->width ) {
                   5141:                 case 1:
                   5142:                     ivc = ivect->index.c;
                   5143:                     for ( j = 1, NMV_ADV(mr); j < len; j++, NMV_ADV(mr) ) {
                   5144:                         pos = prev+ivc[j]; c1 = CM(mr); c2 = svect[pos];
                   5145:                         prev = pos;
                   5146:                         DMA(c1,c,c2,up,lo);
                   5147:                         if ( up ) { DSAB(m,up,lo,dmy,c3); svect[pos] = c3;
                   5148:                         } else svect[pos] = lo;
                   5149:                     }
                   5150:                     break;
                   5151:                 case 2:
                   5152:                     ivs = ivect->index.s;
                   5153:                     for ( j = 1, NMV_ADV(mr); j < len; j++, NMV_ADV(mr) ) {
                   5154:                         pos = prev+ivs[j]; c1 = CM(mr); c2 = svect[pos];
                   5155:                         prev = pos;
                   5156:                         DMA(c1,c,c2,up,lo);
                   5157:                         if ( up ) { DSAB(m,up,lo,dmy,c3); svect[pos] = c3;
                   5158:                         } else svect[pos] = lo;
                   5159:                     }
                   5160:                     break;
                   5161:                 case 4:
                   5162:                     ivi = ivect->index.i;
                   5163:                     for ( j = 1, NMV_ADV(mr); j < len; j++, NMV_ADV(mr) ) {
                   5164:                         pos = prev+ivi[j]; c1 = CM(mr); c2 = svect[pos];
                   5165:                         prev = pos;
                   5166:                         DMA(c1,c,c2,up,lo);
                   5167:                         if ( up ) { DSAB(m,up,lo,dmy,c3); svect[pos] = c3;
                   5168:                         } else svect[pos] = lo;
                   5169:                     }
                   5170:                     break;
                   5171:             }
                   5172:         }
                   5173:     }
                   5174:     for ( i = 0; i < col; i++ )
                   5175:         if ( svect[i] >= (UINT)m ) svect[i] %= m;
                   5176:     return maxrs;
1.65      noro     5177: }
                   5178:
1.76      noro     5179: int ndv_reduce_vect_sf(int m,UINT *svect,int col,IndArray *imat,NM_ind_pair *rp0,int nred)
1.72      noro     5180: {
1.157     noro     5181:     int i,j,k,len,pos,prev;
                   5182:     UINT c,c1,c2,c3,up,lo,dmy;
                   5183:     IndArray ivect;
                   5184:     unsigned char *ivc;
                   5185:     unsigned short *ivs;
                   5186:     unsigned int *ivi;
                   5187:     NDV redv;
                   5188:     NMV mr;
                   5189:     NODE rp;
                   5190:     int maxrs;
                   5191:
                   5192:     maxrs = 0;
                   5193:     for ( i = 0; i < nred; i++ ) {
                   5194:         ivect = imat[i];
                   5195:         k = ivect->head; svect[k] %= m;
                   5196:         if ( c = svect[k] ) {
                   5197:             maxrs = MAX(maxrs,rp0[i]->sugar);
                   5198:             c = _chsgnsf(c); redv = nd_ps[rp0[i]->index];
                   5199:             len = LEN(redv); mr = BDY(redv);
                   5200:             svect[k] = 0; prev = k;
                   5201:             switch ( ivect->width ) {
                   5202:                 case 1:
                   5203:                     ivc = ivect->index.c;
                   5204:                     for ( j = 1, NMV_ADV(mr); j < len; j++, NMV_ADV(mr) ) {
                   5205:                         pos = prev+ivc[j]; prev = pos;
                   5206:                         svect[pos] = _addsf(_mulsf(CM(mr),c),svect[pos]);
                   5207:                     }
                   5208:                     break;
                   5209:                 case 2:
                   5210:                     ivs = ivect->index.s;
                   5211:                     for ( j = 1, NMV_ADV(mr); j < len; j++, NMV_ADV(mr) ) {
                   5212:                         pos = prev+ivs[j]; prev = pos;
                   5213:                         svect[pos] = _addsf(_mulsf(CM(mr),c),svect[pos]);
                   5214:                     }
                   5215:                     break;
                   5216:                 case 4:
                   5217:                     ivi = ivect->index.i;
                   5218:                     for ( j = 1, NMV_ADV(mr); j < len; j++, NMV_ADV(mr) ) {
                   5219:                         pos = prev+ivi[j]; prev = pos;
                   5220:                         svect[pos] = _addsf(_mulsf(CM(mr),c),svect[pos]);
                   5221:                     }
                   5222:                     break;
                   5223:             }
                   5224:         }
                   5225:     }
                   5226:     return maxrs;
1.72      noro     5227: }
                   5228:
1.65      noro     5229: NDV vect_to_ndv(UINT *vect,int spcol,int col,int *rhead,UINT *s0vect)
                   5230: {
1.157     noro     5231:     int j,k,len;
                   5232:     UINT *p;
                   5233:     UINT c;
                   5234:     NDV r;
                   5235:     NMV mr0,mr;
                   5236:
                   5237:     for ( j = 0, len = 0; j < spcol; j++ ) if ( vect[j] ) len++;
                   5238:     if ( !len ) return 0;
                   5239:     else {
                   5240:         mr0 = (NMV)GC_malloc_atomic_ignore_off_page(nmv_adv*len);
1.103     noro     5241: #if 0
1.157     noro     5242:         ndv_alloc += nmv_adv*len;
1.103     noro     5243: #endif
1.157     noro     5244:         mr = mr0;
                   5245:         p = s0vect;
                   5246:         for ( j = k = 0; j < col; j++, p += nd_wpd )
                   5247:             if ( !rhead[j] ) {
                   5248:                 if ( c = vect[k++] ) {
                   5249:                     ndl_copy(p,DL(mr)); CM(mr) = c; NMV_ADV(mr);
                   5250:                 }
                   5251:             }
                   5252:         MKNDV(nd_nvar,mr0,len,r);
                   5253:         return r;
                   5254:     }
1.65      noro     5255: }
                   5256:
1.129     noro     5257: /* for preprocessed vector */
                   5258:
1.113     noro     5259: NDV vect_to_ndv_q(Q *vect,int spcol,int col,int *rhead,UINT *s0vect)
1.107     noro     5260: {
1.157     noro     5261:     int j,k,len;
                   5262:     UINT *p;
                   5263:     Q c;
                   5264:     NDV r;
                   5265:     NMV mr0,mr;
                   5266:
                   5267:     for ( j = 0, len = 0; j < spcol; j++ ) if ( vect[j] ) len++;
                   5268:     if ( !len ) return 0;
                   5269:     else {
                   5270:         mr0 = (NMV)GC_malloc(nmv_adv*len);
1.107     noro     5271: #if 0
1.157     noro     5272:         ndv_alloc += nmv_adv*len;
1.107     noro     5273: #endif
1.157     noro     5274:         mr = mr0;
                   5275:         p = s0vect;
                   5276:         for ( j = k = 0; j < col; j++, p += nd_wpd )
                   5277:             if ( !rhead[j] ) {
                   5278:                 if ( c = vect[k++] ) {
                   5279:                     if ( DN(c) )
                   5280:                         error("afo");
                   5281:                     ndl_copy(p,DL(mr)); CQ(mr) = c; NMV_ADV(mr);
                   5282:                 }
                   5283:             }
                   5284:         MKNDV(nd_nvar,mr0,len,r);
                   5285:         return r;
                   5286:     }
1.107     noro     5287: }
                   5288:
1.129     noro     5289: /* for plain vector */
                   5290:
                   5291: NDV plain_vect_to_ndv_q(Q *vect,int col,UINT *s0vect)
                   5292: {
1.157     noro     5293:     int j,k,len;
                   5294:     UINT *p;
                   5295:     Q c;
                   5296:     NDV r;
                   5297:     NMV mr0,mr;
                   5298:
                   5299:     for ( j = 0, len = 0; j < col; j++ ) if ( vect[j] ) len++;
                   5300:     if ( !len ) return 0;
                   5301:     else {
                   5302:         mr0 = (NMV)GC_malloc(nmv_adv*len);
1.129     noro     5303: #if 0
1.157     noro     5304:         ndv_alloc += nmv_adv*len;
1.129     noro     5305: #endif
1.157     noro     5306:         mr = mr0;
                   5307:         p = s0vect;
                   5308:         for ( j = k = 0; j < col; j++, p += nd_wpd, k++ )
                   5309:             if ( c = vect[k] ) {
                   5310:                 if ( DN(c) )
                   5311:                     error("afo");
                   5312:                 ndl_copy(p,DL(mr)); CQ(mr) = c; NMV_ADV(mr);
                   5313:             }
                   5314:         MKNDV(nd_nvar,mr0,len,r);
                   5315:         return r;
                   5316:     }
1.129     noro     5317: }
                   5318:
1.133     noro     5319: int nd_sp_f4(int m,int trace,ND_pairs l,PGeoBucket bucket)
1.65      noro     5320: {
1.157     noro     5321:     ND_pairs t;
                   5322:     NODE sp0,sp;
                   5323:     int stat;
                   5324:     ND spol;
                   5325:
                   5326:     for ( t = l; t; t = NEXT(t) ) {
                   5327:         stat = nd_sp(m,trace,t,&spol);
                   5328:         if ( !stat ) return 0;
                   5329:         if ( spol ) {
                   5330:             add_pbucket_symbolic(bucket,spol);
                   5331:         }
                   5332:     }
                   5333:     return 1;
1.65      noro     5334: }
                   5335:
1.133     noro     5336: int nd_symbolic_preproc(PGeoBucket bucket,int trace,UINT **s0vect,NODE *r)
1.65      noro     5337: {
1.157     noro     5338:     NODE rp0,rp;
                   5339:     NM mul,head,s0,s;
                   5340:     int index,col,i,sugar;
                   5341:     RHist h;
                   5342:     UINT *s0v,*p;
                   5343:     NM_ind_pair pair;
                   5344:     ND red;
                   5345:     NDV *ps;
                   5346:
                   5347:     s0 = 0; rp0 = 0; col = 0;
                   5348:     ps = trace?nd_ps_trace:nd_ps;
                   5349:     while ( 1 ) {
                   5350:         head = remove_head_pbucket_symbolic(bucket);
                   5351:         if ( !head ) break;
                   5352:         if ( !s0 ) s0 = head;
                   5353:         else NEXT(s) = head;
                   5354:         s = head;
                   5355:         index = ndl_find_reducer(DL(head));
                   5356:         if ( index >= 0 ) {
                   5357:             h = nd_psh[index];
                   5358:             NEWNM(mul);
                   5359:             ndl_sub(DL(head),DL(h),DL(mul));
                   5360:             if ( ndl_check_bound2(index,DL(mul)) ) return 0;
                   5361:             sugar = TD(DL(mul))+SG(ps[index]);
                   5362:             MKNM_ind_pair(pair,mul,index,sugar);
                   5363:             red = ndv_mul_nm_symbolic(mul,ps[index]);
                   5364:             add_pbucket_symbolic(bucket,nd_remove_head(red));
                   5365:             NEXTNODE(rp0,rp); BDY(rp) = (pointer)pair;
                   5366:         }
                   5367:         col++;
                   5368:     }
                   5369:     if ( rp0 ) NEXT(rp) = 0;
                   5370:     NEXT(s) = 0;
                   5371:     s0v = (UINT *)MALLOC_ATOMIC(col*nd_wpd*sizeof(UINT));
                   5372:     for ( i = 0, p = s0v, s = s0; i < col;
                   5373:         i++, p += nd_wpd, s = NEXT(s) ) ndl_copy(DL(s),p);
                   5374:     *s0vect = s0v;
                   5375:     *r = rp0;
                   5376:     return col;
1.65      noro     5377: }
                   5378:
1.167     noro     5379: NODE nd_f4(int m,int **indp)
1.69      noro     5380: {
1.157     noro     5381:     int i,nh,stat,index;
                   5382:     NODE r,g;
                   5383:     ND_pairs d,l,t;
                   5384:     ND spol,red;
                   5385:     NDV nf,redv;
                   5386:     NM s0,s;
                   5387:     NODE rp0,srp0,nflist;
                   5388:     int nsp,nred,col,rank,len,k,j,a;
                   5389:     UINT c;
                   5390:     UINT **spmat;
                   5391:     UINT *s0vect,*svect,*p,*v;
                   5392:     int *colstat;
                   5393:     IndArray *imat;
                   5394:     int *rhead;
                   5395:     int spcol,sprow;
                   5396:     int sugar;
                   5397:     PGeoBucket bucket;
                   5398:     struct oEGT eg0,eg1,eg_f4;
1.69      noro     5399:
1.103     noro     5400: #if 0
1.157     noro     5401:     ndv_alloc = 0;
1.103     noro     5402: #endif
1.157     noro     5403:     g = 0; d = 0;
                   5404:     for ( i = 0; i < nd_psn; i++ ) {
1.168     noro     5405:         d = update_pairs(d,g,i,0);
1.157     noro     5406:         g = update_base(g,i);
                   5407:     }
                   5408:     while ( d ) {
                   5409:         get_eg(&eg0);
                   5410:         l = nd_minsugarp(d,&d);
                   5411:         sugar = SG(l);
                   5412:         bucket = create_pbucket();
                   5413:         stat = nd_sp_f4(m,0,l,bucket);
                   5414:         if ( !stat ) {
                   5415:             for ( t = l; NEXT(t); t = NEXT(t) );
                   5416:             NEXT(t) = d; d = l;
                   5417:             d = nd_reconstruct(0,d);
                   5418:             continue;
                   5419:         }
                   5420:         if ( bucket->m < 0 ) continue;
                   5421:         col = nd_symbolic_preproc(bucket,0,&s0vect,&rp0);
                   5422:         if ( !col ) {
                   5423:             for ( t = l; NEXT(t); t = NEXT(t) );
                   5424:             NEXT(t) = d; d = l;
                   5425:             d = nd_reconstruct(0,d);
                   5426:             continue;
                   5427:         }
                   5428:         get_eg(&eg1); init_eg(&eg_f4); add_eg(&eg_f4,&eg0,&eg1);
                   5429:         if ( DP_Print )
                   5430:             fprintf(asir_out,"sugar=%d,symb=%fsec,",
                   5431:                 sugar,eg_f4.exectime+eg_f4.gctime);
                   5432:         if ( 1 )
                   5433:             nflist = nd_f4_red(m,l,0,s0vect,col,rp0,0);
                   5434:         else
                   5435:             nflist = nd_f4_red_dist(m,l,s0vect,col,rp0,0);
                   5436:         /* adding new bases */
                   5437:         for ( r = nflist; r; r = NEXT(r) ) {
                   5438:             nf = (NDV)BDY(r);
                   5439:             ndv_removecont(m,nf);
                   5440:             if ( !m && nd_nalg ) {
                   5441:                 ND nf1;
                   5442:
                   5443:                 nf1 = ndvtond(m,nf);
                   5444:                 nd_monic(0,&nf1);
                   5445:                 nd_removecont(m,nf1);
                   5446:                 nf = ndtondv(m,nf1);
                   5447:             }
                   5448:             nh = ndv_newps(m,nf,0);
1.168     noro     5449:             d = update_pairs(d,g,nh,0);
1.157     noro     5450:             g = update_base(g,nh);
                   5451:         }
                   5452:     }
1.103     noro     5453: #if 0
1.157     noro     5454:     fprintf(asir_out,"ndv_alloc=%d\n",ndv_alloc);
1.103     noro     5455: #endif
1.167     noro     5456:        conv_ilist(0,0,g,indp);
1.157     noro     5457:     return g;
1.69      noro     5458: }
1.74      noro     5459:
1.167     noro     5460: NODE nd_f4_trace(int m,int **indp)
1.133     noro     5461: {
1.157     noro     5462:     int i,nh,stat,index;
                   5463:     NODE r,g;
                   5464:     ND_pairs d,l,l0,t;
                   5465:     ND spol,red;
                   5466:     NDV nf,redv,nfqv,nfv;
                   5467:     NM s0,s;
                   5468:     NODE rp0,srp0,nflist;
                   5469:     int nsp,nred,col,rank,len,k,j,a;
                   5470:     UINT c;
                   5471:     UINT **spmat;
                   5472:     UINT *s0vect,*svect,*p,*v;
                   5473:     int *colstat;
                   5474:     IndArray *imat;
                   5475:     int *rhead;
                   5476:     int spcol,sprow;
                   5477:     int sugar;
                   5478:     PGeoBucket bucket;
                   5479:     struct oEGT eg0,eg1,eg_f4;
                   5480:
                   5481:     g = 0; d = 0;
                   5482:     for ( i = 0; i < nd_psn; i++ ) {
1.168     noro     5483:         d = update_pairs(d,g,i,0);
1.157     noro     5484:         g = update_base(g,i);
                   5485:     }
                   5486:     while ( d ) {
                   5487:         get_eg(&eg0);
                   5488:         l = nd_minsugarp(d,&d);
                   5489:         sugar = SG(l);
                   5490:         bucket = create_pbucket();
                   5491:         stat = nd_sp_f4(m,0,l,bucket);
                   5492:         if ( !stat ) {
                   5493:             for ( t = l; NEXT(t); t = NEXT(t) );
                   5494:             NEXT(t) = d; d = l;
                   5495:             d = nd_reconstruct(1,d);
                   5496:             continue;
                   5497:         }
                   5498:         if ( bucket->m < 0 ) continue;
                   5499:         col = nd_symbolic_preproc(bucket,0,&s0vect,&rp0);
                   5500:         if ( !col ) {
                   5501:             for ( t = l; NEXT(t); t = NEXT(t) );
                   5502:             NEXT(t) = d; d = l;
                   5503:             d = nd_reconstruct(1,d);
                   5504:             continue;
                   5505:         }
                   5506:         get_eg(&eg1); init_eg(&eg_f4); add_eg(&eg_f4,&eg0,&eg1);
                   5507:         if ( DP_Print )
                   5508:             fprintf(asir_out,"sugar=%d,symb=%fsec,",
                   5509:                 sugar,eg_f4.exectime+eg_f4.gctime);
                   5510:         nflist = nd_f4_red(m,l,0,s0vect,col,rp0,&l0);
                   5511:         if ( !l0 ) continue;
                   5512:         l = l0;
                   5513:
                   5514:         /* over Q */
                   5515:         bucket = create_pbucket();
                   5516:         stat = nd_sp_f4(0,1,l,bucket);
                   5517:         if ( !stat ) {
                   5518:             for ( t = l; NEXT(t); t = NEXT(t) );
                   5519:             NEXT(t) = d; d = l;
                   5520:             d = nd_reconstruct(1,d);
                   5521:             continue;
                   5522:         }
                   5523:         if ( bucket->m < 0 ) continue;
                   5524:         col = nd_symbolic_preproc(bucket,1,&s0vect,&rp0);
                   5525:         if ( !col ) {
                   5526:             for ( t = l; NEXT(t); t = NEXT(t) );
                   5527:             NEXT(t) = d; d = l;
                   5528:             d = nd_reconstruct(1,d);
                   5529:             continue;
                   5530:         }
                   5531:         nflist = nd_f4_red(0,l,1,s0vect,col,rp0,0);
                   5532:         /* adding new bases */
                   5533:         for ( r = nflist; r; r = NEXT(r) ) {
                   5534:             nfqv = (NDV)BDY(r);
                   5535:             ndv_removecont(0,nfqv);
                   5536:             if ( !rem(NM(HCQ(nfqv)),m) ) return 0;
                   5537:             if ( nd_nalg ) {
                   5538:                 ND nf1;
                   5539:
                   5540:                 nf1 = ndvtond(m,nfqv);
                   5541:                 nd_monic(0,&nf1);
                   5542:                 nd_removecont(0,nf1);
                   5543:                 nfqv = ndtondv(0,nf1); nd_free(nf1);
                   5544:             }
                   5545:             nfv = ndv_dup(0,nfqv);
                   5546:             ndv_mod(m,nfv);
                   5547:             ndv_removecont(m,nfv);
                   5548:             nh = ndv_newps(0,nfv,nfqv);
1.168     noro     5549:             d = update_pairs(d,g,nh,0);
1.157     noro     5550:             g = update_base(g,nh);
                   5551:         }
                   5552:     }
1.133     noro     5553: #if 0
1.157     noro     5554:     fprintf(asir_out,"ndv_alloc=%d\n",ndv_alloc);
1.133     noro     5555: #endif
1.167     noro     5556:        conv_ilist(0,1,g,indp);
1.157     noro     5557:     return g;
1.133     noro     5558: }
                   5559:
                   5560: NODE nd_f4_red(int m,ND_pairs sp0,int trace,UINT *s0vect,int col,NODE rp0,ND_pairs *nz)
1.63      noro     5561: {
1.157     noro     5562:     IndArray *imat;
                   5563:     int nsp,nred,i;
                   5564:     int *rhead;
                   5565:     NODE r0,rp;
                   5566:     ND_pairs sp;
                   5567:     NM_ind_pair *rvect;
                   5568:
                   5569:     for ( sp = sp0, nsp = 0; sp; sp = NEXT(sp), nsp++ );
                   5570:     nred = length(rp0);
                   5571:     imat = (IndArray *)ALLOCA(nred*sizeof(IndArray));
                   5572:     rhead = (int *)ALLOCA(col*sizeof(int));
                   5573:     for ( i = 0; i < col; i++ ) rhead[i] = 0;
                   5574:
                   5575:     /* construction of index arrays */
                   5576:     rvect = (NM_ind_pair *)ALLOCA(nred*sizeof(NM_ind_pair));
                   5577:     for ( rp = rp0, i = 0; rp; i++, rp = NEXT(rp) ) {
                   5578:         rvect[i] = (NM_ind_pair)BDY(rp);
                   5579:         imat[i] = nm_ind_pair_to_vect_compress(m,s0vect,col,rvect[i]);
                   5580:         rhead[imat[i]->head] = 1;
                   5581:     }
                   5582:     if ( m )
                   5583:         r0 = nd_f4_red_main(m,sp0,nsp,s0vect,col,rvect,rhead,imat,nred,nz);
                   5584:     else
                   5585:         r0 = nd_f4_red_q_main(sp0,nsp,trace,s0vect,col,rvect,rhead,imat,nred);
                   5586:     return r0;
1.106     noro     5587: }
1.74      noro     5588:
1.106     noro     5589: NODE nd_f4_red_main(int m,ND_pairs sp0,int nsp,UINT *s0vect,int col,
1.133     noro     5590:         NM_ind_pair *rvect,int *rhead,IndArray *imat,int nred,ND_pairs *nz)
1.106     noro     5591: {
1.157     noro     5592:     int spcol,sprow,a;
                   5593:     int i,j,k,l,rank;
                   5594:     NODE r0,r;
                   5595:     ND_pairs sp;
                   5596:     ND spol;
                   5597:     int **spmat;
                   5598:     UINT *svect,*v;
                   5599:     int *colstat;
                   5600:     struct oEGT eg0,eg1,eg2,eg_f4,eg_f4_1,eg_f4_2;
                   5601:     int maxrs;
                   5602:     int *spsugar;
                   5603:     ND_pairs *spactive;
                   5604:
                   5605:     spcol = col-nred;
                   5606:     get_eg(&eg0);
                   5607:     /* elimination (1st step) */
                   5608:     spmat = (int **)ALLOCA(nsp*sizeof(UINT *));
                   5609:     svect = (UINT *)ALLOCA(col*sizeof(UINT));
                   5610:     spsugar = (int *)ALLOCA(nsp*sizeof(UINT));
                   5611:     spactive = !nz?0:(ND_pairs *)ALLOCA(nsp*sizeof(ND_pairs));
                   5612:     for ( a = sprow = 0, sp = sp0; a < nsp; a++, sp = NEXT(sp) ) {
                   5613:         nd_sp(m,0,sp,&spol);
                   5614:         if ( !spol ) continue;
                   5615:         nd_to_vect(m,s0vect,col,spol,svect);
                   5616:         if ( m == -1 )
                   5617:             maxrs = ndv_reduce_vect_sf(m,svect,col,imat,rvect,nred);
                   5618:         else
                   5619:             maxrs = ndv_reduce_vect(m,svect,col,imat,rvect,nred);
                   5620:         for ( i = 0; i < col; i++ ) if ( svect[i] ) break;
                   5621:         if ( i < col ) {
                   5622:             spmat[sprow] = v = (UINT *)MALLOC_ATOMIC(spcol*sizeof(UINT));
                   5623:             for ( j = k = 0; j < col; j++ )
                   5624:                 if ( !rhead[j] ) v[k++] = svect[j];
                   5625:             spsugar[sprow] = MAX(maxrs,SG(spol));
                   5626:             if ( nz )
                   5627:             spactive[sprow] = sp;
                   5628:             sprow++;
                   5629:         }
                   5630:         nd_free(spol);
                   5631:     }
                   5632:     get_eg(&eg1); init_eg(&eg_f4_1); add_eg(&eg_f4_1,&eg0,&eg1);
                   5633:     if ( DP_Print ) {
                   5634:         fprintf(asir_out,"elim1=%fsec,",eg_f4_1.exectime+eg_f4_1.gctime);
                   5635:         fflush(asir_out);
                   5636:     }
                   5637:     /* free index arrays */
                   5638:     for ( i = 0; i < nred; i++ ) GC_free(imat[i]->index.c);
                   5639:
                   5640:     /* elimination (2nd step) */
                   5641:     colstat = (int *)ALLOCA(spcol*sizeof(int));
                   5642:     if ( m == -1 )
                   5643:         rank = nd_gauss_elim_sf(spmat,spsugar,sprow,spcol,m,colstat);
                   5644:     else
                   5645:         rank = nd_gauss_elim_mod(spmat,spsugar,spactive,sprow,spcol,m,colstat);
                   5646:     r0 = 0;
                   5647:     for ( i = 0; i < rank; i++ ) {
                   5648:         NEXTNODE(r0,r); BDY(r) =
                   5649:             (pointer)vect_to_ndv(spmat[i],spcol,col,rhead,s0vect);
                   5650:         SG((NDV)BDY(r)) = spsugar[i];
                   5651:         GC_free(spmat[i]);
                   5652:     }
                   5653:     if ( r0 ) NEXT(r) = 0;
                   5654:
                   5655:     for ( ; i < sprow; i++ ) GC_free(spmat[i]);
                   5656:     get_eg(&eg2); init_eg(&eg_f4_2); add_eg(&eg_f4_2,&eg1,&eg2);
                   5657:     init_eg(&eg_f4); add_eg(&eg_f4,&eg0,&eg2);
                   5658:     if ( DP_Print ) {
                   5659:         fprintf(asir_out,"elim2=%fsec\n",eg_f4_2.exectime+eg_f4_2.gctime);
                   5660:         fprintf(asir_out,"nsp=%d,nred=%d,spmat=(%d,%d),rank=%d  ",
                   5661:             nsp,nred,sprow,spcol,rank);
                   5662:         fprintf(asir_out,"%fsec\n",eg_f4.exectime+eg_f4.gctime);
                   5663:     }
                   5664:     if ( nz ) {
                   5665:         for ( i = 0; i < rank-1; i++ ) NEXT(spactive[i]) = spactive[i+1];
                   5666:         if ( rank > 0 ) {
                   5667:             NEXT(spactive[rank-1]) = 0;
                   5668:             *nz = spactive[0];
                   5669:         } else
                   5670:             *nz = 0;
                   5671:     }
                   5672:     return r0;
1.74      noro     5673: }
                   5674:
1.133     noro     5675: #if 1
                   5676: NODE nd_f4_red_q_main(ND_pairs sp0,int nsp,int trace,UINT *s0vect,int col,
1.107     noro     5677:         NM_ind_pair *rvect,int *rhead,IndArray *imat,int nred)
                   5678: {
1.157     noro     5679:     int spcol,sprow,a;
                   5680:     int i,j,k,l,rank;
                   5681:     NODE r0,r;
                   5682:     ND_pairs sp;
                   5683:     ND spol;
                   5684:     Q **spmat;
                   5685:     Q *svect,*v;
                   5686:     int *colstat;
                   5687:     struct oEGT eg0,eg1,eg2,eg_f4,eg_f4_1,eg_f4_2;
                   5688:     int maxrs;
                   5689:     int *spsugar;
                   5690:     pointer *w;
                   5691:
                   5692:     spcol = col-nred;
                   5693:     get_eg(&eg0);
                   5694:     /* elimination (1st step) */
                   5695:     spmat = (Q **)ALLOCA(nsp*sizeof(Q *));
                   5696:     svect = (Q *)ALLOCA(col*sizeof(Q));
                   5697:     spsugar = (int *)ALLOCA(nsp*sizeof(Q));
                   5698:     for ( a = sprow = 0, sp = sp0; a < nsp; a++, sp = NEXT(sp) ) {
                   5699:         nd_sp(0,trace,sp,&spol);
                   5700:         if ( !spol ) continue;
                   5701:         nd_to_vect_q(s0vect,col,spol,svect);
                   5702:         maxrs = ndv_reduce_vect_q(svect,trace,col,imat,rvect,nred);
                   5703:         for ( i = 0; i < col; i++ ) if ( svect[i] ) break;
                   5704:         if ( i < col ) {
                   5705:             spmat[sprow] = v = (Q *)MALLOC(spcol*sizeof(Q));
                   5706:             for ( j = k = 0; j < col; j++ )
                   5707:                 if ( !rhead[j] ) v[k++] = svect[j];
                   5708:             spsugar[sprow] = MAX(maxrs,SG(spol));
                   5709:             sprow++;
                   5710:         }
                   5711: /*        nd_free(spol); */
                   5712:     }
                   5713:     get_eg(&eg1); init_eg(&eg_f4_1); add_eg(&eg_f4_1,&eg0,&eg1);
                   5714:     if ( DP_Print ) {
                   5715:         fprintf(asir_out,"elim1=%fsec,",eg_f4_1.exectime+eg_f4_1.gctime);
                   5716:         fflush(asir_out);
                   5717:     }
                   5718:     /* free index arrays */
                   5719: /*    for ( i = 0; i < nred; i++ ) GC_free(imat[i]->index.c); */
                   5720:
                   5721:     /* elimination (2nd step) */
                   5722:     colstat = (int *)ALLOCA(spcol*sizeof(int));
                   5723:     rank = nd_gauss_elim_q(spmat,spsugar,sprow,spcol,colstat);
                   5724:     w = (pointer *)ALLOCA(rank*sizeof(pointer));
                   5725:     for ( i = 0; i < rank; i++ ) {
                   5726:         w[rank-i-1] = (pointer)vect_to_ndv_q(spmat[i],spcol,col,rhead,s0vect);
                   5727:         SG((NDV)w[rank-i-1]) = spsugar[i];
                   5728: /*        GC_free(spmat[i]); */
                   5729:     }
1.138     noro     5730: #if 0
1.157     noro     5731:     qsort(w,rank,sizeof(NDV),
                   5732:         (int (*)(const void *,const void *))ndv_compare);
1.137     noro     5733: #endif
1.157     noro     5734:     r0 = 0;
                   5735:     for ( i = 0; i < rank; i++ ) {
                   5736:         NEXTNODE(r0,r); BDY(r) = w[i];
                   5737:     }
                   5738:     if ( r0 ) NEXT(r) = 0;
                   5739:
                   5740: /*    for ( ; i < sprow; i++ ) GC_free(spmat[i]); */
                   5741:     get_eg(&eg2); init_eg(&eg_f4_2); add_eg(&eg_f4_2,&eg1,&eg2);
                   5742:     init_eg(&eg_f4); add_eg(&eg_f4,&eg0,&eg2);
                   5743:     if ( DP_Print ) {
                   5744:         fprintf(asir_out,"elim2=%fsec\n",eg_f4_2.exectime+eg_f4_2.gctime);
                   5745:         fprintf(asir_out,"nsp=%d,nred=%d,spmat=(%d,%d),rank=%d  ",
                   5746:             nsp,nred,sprow,spcol,rank);
                   5747:         fprintf(asir_out,"%fsec\n",eg_f4.exectime+eg_f4.gctime);
                   5748:     }
                   5749:     return r0;
1.107     noro     5750: }
1.129     noro     5751: #else
                   5752: void printm(Q **mat,int row,int col)
                   5753: {
1.157     noro     5754:     int i,j;
                   5755:     printf("[");
                   5756:     for ( i = 0; i < row; i++ ) {
                   5757:         for ( j = 0; j < col; j++ ) {
                   5758:             printexpr(CO,mat[i][j]); printf(" ");
                   5759:         }
                   5760:         printf("]\n");
                   5761:     }
1.129     noro     5762: }
                   5763:
                   5764: NODE nd_f4_red_q_main(ND_pairs sp0,int nsp,UINT *s0vect,int col,
                   5765:         NM_ind_pair *rvect,int *rhead,IndArray *imat,int nred)
                   5766: {
1.157     noro     5767:     int row,a;
                   5768:     int i,j,rank;
                   5769:     NODE r0,r;
                   5770:     ND_pairs sp;
                   5771:     ND spol;
                   5772:     Q **mat;
                   5773:     int *colstat;
                   5774:     int *sugar;
                   5775:
                   5776:     row = nsp+nred;
                   5777:     /* make the matrix */
                   5778:     mat = (Q **)ALLOCA(row*sizeof(Q *));
                   5779:     sugar = (int *)ALLOCA(row*sizeof(int));
                   5780:     for ( row = a = 0, sp = sp0; a < nsp; a++, sp = NEXT(sp) ) {
                   5781:         nd_sp(0,0,sp,&spol);
                   5782:         if ( !spol ) continue;
                   5783:         mat[row] = (Q *)MALLOC(col*sizeof(Q));
                   5784:         nd_to_vect_q(s0vect,col,spol,mat[row]);
                   5785:         sugar[row] = SG(spol);
                   5786:         row++;
                   5787:     }
                   5788:     for ( i = 0; i < nred; i++, row++ ) {
                   5789:         mat[row] = nm_ind_pair_to_vect(0,s0vect,col,rvect[i]);
                   5790:         sugar[row] = rvect[i]->sugar;
                   5791:     }
                   5792:     /* elimination */
                   5793:     colstat = (int *)ALLOCA(col*sizeof(int));
                   5794:     rank = nd_gauss_elim_q(mat,sugar,row,col,colstat);
                   5795:     r0 = 0;
                   5796:     for ( i = 0; i < rank; i++ ) {
                   5797:         for ( j = 0; j < col; j++ ) if ( mat[i][j] ) break;
                   5798:         if ( j == col ) error("nd_f4_red_q_main : cannot happen");
                   5799:         if ( rhead[j] ) continue;
                   5800:         NEXTNODE(r0,r); BDY(r) =
                   5801:             (pointer)plain_vect_to_ndv_q(mat[i],col,s0vect);
                   5802:         SG((NDV)BDY(r)) = sugar[i];
                   5803:     }
                   5804:     if ( r0 ) NEXT(r) = 0;
                   5805:     printf("\n");
                   5806:     return r0;
1.129     noro     5807: }
                   5808: #endif
1.107     noro     5809:
1.74      noro     5810: FILE *nd_write,*nd_read;
                   5811:
                   5812: void nd_send_int(int a) {
1.157     noro     5813:     write_int(nd_write,&a);
1.74      noro     5814: }
                   5815:
                   5816: void nd_send_intarray(int *p,int len) {
1.157     noro     5817:     write_intarray(nd_write,p,len);
1.74      noro     5818: }
                   5819:
                   5820: int nd_recv_int() {
1.157     noro     5821:     int a;
1.74      noro     5822:
1.157     noro     5823:     read_int(nd_read,&a);
                   5824:     return a;
1.74      noro     5825: }
                   5826:
                   5827: void nd_recv_intarray(int *p,int len) {
1.157     noro     5828:     read_intarray(nd_read,p,len);
1.74      noro     5829: }
                   5830:
                   5831: void nd_send_ndv(NDV p) {
1.157     noro     5832:     int len,i;
                   5833:     NMV m;
1.74      noro     5834:
1.157     noro     5835:     if ( !p ) nd_send_int(0);
                   5836:     else {
                   5837:         len = LEN(p);
                   5838:         nd_send_int(len);
                   5839:         m = BDY(p);
                   5840:         for ( i = 0; i < len; i++, NMV_ADV(m) ) {
                   5841:             nd_send_int(CM(m));
                   5842:             nd_send_intarray(DL(m),nd_wpd);
                   5843:         }
                   5844:     }
1.74      noro     5845: }
                   5846:
                   5847: void nd_send_nd(ND p) {
1.157     noro     5848:     int len,i;
                   5849:     NM m;
1.74      noro     5850:
1.157     noro     5851:     if ( !p ) nd_send_int(0);
                   5852:     else {
                   5853:         len = LEN(p);
                   5854:         nd_send_int(len);
                   5855:         m = BDY(p);
                   5856:         for ( i = 0; i < len; i++, m = NEXT(m) ) {
                   5857:             nd_send_int(CM(m));
                   5858:             nd_send_intarray(DL(m),nd_wpd);
                   5859:         }
                   5860:     }
1.74      noro     5861: }
1.65      noro     5862:
1.74      noro     5863: NDV nd_recv_ndv()
                   5864: {
1.157     noro     5865:     int len,i;
                   5866:     NMV m,m0;
                   5867:     NDV r;
                   5868:
                   5869:     len = nd_recv_int();
                   5870:     if ( !len ) return 0;
                   5871:     else {
                   5872:         m0 = m = (NMV)GC_malloc_atomic_ignore_off_page(nmv_adv*len);
1.103     noro     5873: #if 0
1.157     noro     5874:         ndv_alloc += len*nmv_adv;
1.103     noro     5875: #endif
1.157     noro     5876:         for ( i = 0; i < len; i++, NMV_ADV(m) ) {
                   5877:             CM(m) = nd_recv_int();
                   5878:             nd_recv_intarray(DL(m),nd_wpd);
                   5879:         }
                   5880:         MKNDV(nd_nvar,m0,len,r);
                   5881:         return r;
                   5882:     }
1.74      noro     5883: }
1.65      noro     5884:
1.74      noro     5885: int ox_exec_f4_red(Q proc)
                   5886: {
1.157     noro     5887:     Obj obj;
                   5888:     STRING fname;
                   5889:     NODE arg;
                   5890:     int s;
                   5891:     extern int ox_need_conv,ox_file_io;
                   5892:
                   5893:     MKSTR(fname,"nd_exec_f4_red");
                   5894:     arg = mknode(2,proc,fname);
                   5895:     Pox_cmo_rpc(arg,&obj);
                   5896:     s = get_ox_server_id(QTOS(proc));
                   5897:     nd_write = iofp[s].out;
                   5898:     nd_read = iofp[s].in;
                   5899:     ox_need_conv = ox_file_io = 0;
                   5900:     return s;
1.74      noro     5901: }
                   5902:
1.133     noro     5903: NODE nd_f4_red_dist(int m,ND_pairs sp0,UINT *s0vect,int col,NODE rp0,ND_pairs *nz)
1.74      noro     5904: {
1.157     noro     5905:     int nsp,nred;
                   5906:     int i,rank,s;
                   5907:     NODE rp,r0,r;
                   5908:     ND_pairs sp;
                   5909:     NM_ind_pair pair;
                   5910:     NMV nmv;
                   5911:     NM nm;
                   5912:     NDV nf;
                   5913:     Obj proc,dmy;
                   5914:
                   5915:     ox_launch_main(0,0,&proc);
                   5916:     s = ox_exec_f4_red((Q)proc);
                   5917:
                   5918:     nd_send_int(m);
                   5919:     nd_send_int(nd_nvar);
                   5920:     nd_send_int(nd_bpe);
                   5921:     nd_send_int(nd_wpd);
                   5922:     nd_send_int(nmv_adv);
                   5923:
                   5924:     saveobj(nd_write,dp_current_spec->obj); fflush(nd_write);
                   5925:
                   5926:     nd_send_int(nd_psn);
                   5927:     for ( i = 0; i < nd_psn; i++ ) nd_send_ndv(nd_ps[i]);
                   5928:
                   5929:     for ( sp = sp0, nsp = 0; sp; sp = NEXT(sp), nsp++ );
                   5930:     nd_send_int(nsp);
                   5931:     for ( i = 0, sp = sp0; i < nsp; i++, sp = NEXT(sp) ) {
                   5932:         nd_send_int(sp->i1); nd_send_int(sp->i2);
                   5933:     }
                   5934:
                   5935:     nd_send_int(col); nd_send_intarray(s0vect,col*nd_wpd);
                   5936:
                   5937:     nred = length(rp0); nd_send_int(nred);
                   5938:     for ( i = 0, rp = rp0; i < nred; i++, rp = NEXT(rp) ) {
                   5939:         pair = (NM_ind_pair)BDY(rp);
                   5940:         nd_send_int(pair->index);
                   5941:         nd_send_intarray(pair->mul->dl,nd_wpd);
                   5942:     }
                   5943:     fflush(nd_write);
                   5944:     rank = nd_recv_int();
                   5945:     fprintf(asir_out,"rank=%d\n",rank);
                   5946:     r0 = 0;
                   5947:     for ( i = 0; i < rank; i++ ) {
                   5948:         nf = nd_recv_ndv();
                   5949:         NEXTNODE(r0,r); BDY(r) = (pointer)nf;
                   5950:     }
                   5951:     Pox_shutdown(mknode(1,proc),&dmy);
                   5952:     return r0;
1.74      noro     5953: }
                   5954:
                   5955: /* server side */
                   5956:
                   5957: void nd_exec_f4_red_dist()
                   5958: {
1.157     noro     5959:     int m,i,nsp,col,s0size,nred,spcol,j,k;
                   5960:     NM_ind_pair *rp0;
                   5961:     NDV nf;
                   5962:     UINT *s0vect;
                   5963:     IndArray *imat;
                   5964:     int *rhead;
                   5965:     int **spmat;
                   5966:     UINT *svect,*v;
                   5967:     ND_pairs *sp0;
                   5968:     int *colstat;
                   5969:     int a,sprow,rank;
                   5970:     struct order_spec *ord;
                   5971:     Obj ordspec;
                   5972:     ND spol;
                   5973:     int maxrs;
                   5974:     int *spsugar;
                   5975:
                   5976:     nd_read = iofp[0].in;
                   5977:     nd_write = iofp[0].out;
                   5978:     m = nd_recv_int();
                   5979:     nd_nvar = nd_recv_int();
                   5980:     nd_bpe = nd_recv_int();
                   5981:     nd_wpd = nd_recv_int();
                   5982:     nmv_adv = nd_recv_int();
                   5983:
                   5984:     loadobj(nd_read,&ordspec);
                   5985:     create_order_spec(0,ordspec,&ord);
                   5986:     nd_init_ord(ord);
                   5987:     nd_setup_parameters(nd_nvar,0);
                   5988:
                   5989:     nd_psn = nd_recv_int();
                   5990:     nd_ps = (NDV *)MALLOC(nd_psn*sizeof(NDV));
                   5991:     nd_bound = (UINT **)MALLOC(nd_psn*sizeof(UINT *));
                   5992:     for ( i = 0; i < nd_psn; i++ ) {
                   5993:         nd_ps[i] = nd_recv_ndv();
                   5994:         nd_bound[i] = ndv_compute_bound(nd_ps[i]);
                   5995:     }
                   5996:
                   5997:     nsp = nd_recv_int();
                   5998:     sp0 = (ND_pairs *)MALLOC(nsp*sizeof(ND_pairs));
                   5999:     for ( i = 0; i < nsp; i++ ) {
                   6000:         NEWND_pairs(sp0[i]);
                   6001:         sp0[i]->i1 = nd_recv_int(); sp0[i]->i2 = nd_recv_int();
                   6002:         ndl_lcm(HDL(nd_ps[sp0[i]->i1]),HDL(nd_ps[sp0[i]->i2]),LCM(sp0[i]));
                   6003:     }
                   6004:
                   6005:     col = nd_recv_int();
                   6006:     s0size = col*nd_wpd;
                   6007:     s0vect = (UINT *)MALLOC(s0size*sizeof(UINT));
                   6008:     nd_recv_intarray(s0vect,s0size);
                   6009:
                   6010:     nred = nd_recv_int();
                   6011:     rp0 = (NM_ind_pair *)MALLOC(nred*sizeof(NM_ind_pair));
                   6012:     for ( i = 0; i < nred; i++ ) {
                   6013:         rp0[i] = (NM_ind_pair)MALLOC(sizeof(struct oNM_ind_pair));
                   6014:         rp0[i]->index = nd_recv_int();
                   6015:         rp0[i]->mul = (NM)MALLOC(sizeof(struct oNM)+(nd_wpd-1)*sizeof(UINT));
                   6016:         nd_recv_intarray(rp0[i]->mul->dl,nd_wpd);
                   6017:     }
                   6018:
                   6019:     spcol = col-nred;
                   6020:     imat = (IndArray *)MALLOC(nred*sizeof(IndArray));
                   6021:     rhead = (int *)MALLOC(col*sizeof(int));
                   6022:     for ( i = 0; i < col; i++ ) rhead[i] = 0;
                   6023:
                   6024:     /* construction of index arrays */
                   6025:     for ( i = 0; i < nred; i++ ) {
                   6026:         imat[i] = nm_ind_pair_to_vect_compress(m,s0vect,col,rp0[i]);
                   6027:         rhead[imat[i]->head] = 1;
                   6028:     }
                   6029:
                   6030:     /* elimination (1st step) */
                   6031:     spmat = (int **)MALLOC(nsp*sizeof(UINT *));
                   6032:     svect = (UINT *)MALLOC(col*sizeof(UINT));
                   6033:     spsugar = (int *)ALLOCA(nsp*sizeof(UINT));
                   6034:     for ( a = sprow = 0; a < nsp; a++ ) {
                   6035:         nd_sp(m,0,sp0[a],&spol);
                   6036:         if ( !spol ) continue;
                   6037:         nd_to_vect(m,s0vect,col,spol,svect);
                   6038:         if ( m == -1 )
                   6039:             maxrs = ndv_reduce_vect_sf(m,svect,col,imat,rp0,nred);
                   6040:         else
                   6041:             maxrs = ndv_reduce_vect(m,svect,col,imat,rp0,nred);
                   6042:         for ( i = 0; i < col; i++ ) if ( svect[i] ) break;
                   6043:         if ( i < col ) {
                   6044:             spmat[sprow] = v = (UINT *)MALLOC(spcol*sizeof(UINT));
                   6045:             for ( j = k = 0; j < col; j++ )
                   6046:                 if ( !rhead[j] ) v[k++] = svect[j];
                   6047:             spsugar[sprow] = MAX(maxrs,SG(spol));
                   6048:             sprow++;
                   6049:         }
                   6050:         nd_free(spol);
                   6051:     }
                   6052:     /* elimination (2nd step) */
                   6053:     colstat = (int *)ALLOCA(spcol*sizeof(int));
                   6054:     if ( m == -1 )
                   6055:         rank = nd_gauss_elim_sf(spmat,spsugar,sprow,spcol,m,colstat);
                   6056:     else
                   6057:         rank = nd_gauss_elim_mod(spmat,spsugar,0,sprow,spcol,m,colstat);
                   6058:     nd_send_int(rank);
                   6059:     for ( i = 0; i < rank; i++ ) {
                   6060:         nf = vect_to_ndv(spmat[i],spcol,col,rhead,s0vect);
                   6061:         nd_send_ndv(nf);
                   6062:     }
                   6063:     fflush(nd_write);
1.107     noro     6064: }
                   6065:
1.113     noro     6066: int nd_gauss_elim_q(Q **mat0,int *sugar,int row,int col,int *colstat)
1.107     noro     6067: {
1.157     noro     6068:     int mod,i,j,t,c,rank,rank0,inv;
                   6069:     int *ci,*ri;
                   6070:     Q dn;
                   6071:     MAT m,nm;
                   6072:     int **wmat;
                   6073:
                   6074:     /* XXX */
                   6075:     mod = 99999989;
                   6076:     wmat = (int **)ALLOCA(row*sizeof(int *));
                   6077:     for ( i = 0; i < row; i++ ) {
                   6078:         wmat[i] = (int *)ALLOCA(col*sizeof(int));
                   6079:         for ( j = 0; j < col; j++ ) {
                   6080:             if ( mat0[i][j] ) {
                   6081:                 t = rem(NM(mat0[i][j]),mod);
                   6082:                 if ( SGN(mat0[i][j]) < 0 ) t = mod-t;
                   6083:                 wmat[i][j] = t;
                   6084:             } else
                   6085:                 wmat[i][j] = 0;
                   6086:         }
                   6087:     }
                   6088:     rank0 = nd_gauss_elim_mod(wmat,sugar,0,row,col,mod,colstat);
                   6089:     NEWMAT(m); m->row = row; m->col = col; m->body = (pointer **)mat0;
                   6090:     rank = generic_gauss_elim(m,&nm,&dn,&ri,&ci);
                   6091:     if ( rank != rank0 )
                   6092:         error("afo");
                   6093:     for ( i = 0; i < row; i++ )
                   6094:         for ( j = 0; j < col; j++ )
                   6095:             mat0[i][j] = 0;
                   6096:     c = col-rank;
                   6097:     for ( i = 0; i < rank; i++ ) {
                   6098:         mat0[i][ri[i]] = dn;
                   6099:         for ( j = 0; j < c; j++ )
                   6100:             mat0[i][ci[j]] = (Q)BDY(nm)[i][j];
                   6101:     }
                   6102:     inv = invm(rem(NM(dn),mod),mod);
                   6103:     if ( SGN(dn) < 0 ) inv = mod-inv;
                   6104:     for ( i = 0; i < row; i++ )
                   6105:         for ( j = 0; j < col; j++ ) {
                   6106:             if ( mat0[i][j] ) {
                   6107:                 t = rem(NM(mat0[i][j]),mod);
                   6108:                 if ( SGN(mat0[i][j]) < 0 ) t = mod-t;
                   6109:             } else
                   6110:                 t = 0;
                   6111:             c = dmar(t,inv,0,mod);
                   6112:             if ( wmat[i][j] != c )
                   6113:                 error("afo");
                   6114:         }
                   6115:     return rank;
1.76      noro     6116: }
                   6117:
1.133     noro     6118: int nd_gauss_elim_mod(int **mat0,int *sugar,ND_pairs *spactive,int row,int col,int md,int *colstat)
1.76      noro     6119: {
1.157     noro     6120:     int i,j,k,l,inv,a,rank,s;
                   6121:     unsigned int *t,*pivot,*pk;
                   6122:     unsigned int **mat;
                   6123:     ND_pairs pair;
                   6124:
                   6125:     mat = (unsigned int **)mat0;
                   6126:     for ( rank = 0, j = 0; j < col; j++ ) {
                   6127:         for ( i = rank; i < row; i++ )
                   6128:             mat[i][j] %= md;
                   6129:         for ( i = rank; i < row; i++ )
                   6130:             if ( mat[i][j] )
                   6131:                 break;
                   6132:         if ( i == row ) {
                   6133:             colstat[j] = 0;
                   6134:             continue;
                   6135:         } else
                   6136:             colstat[j] = 1;
                   6137:         if ( i != rank ) {
                   6138:             t = mat[i]; mat[i] = mat[rank]; mat[rank] = t;
                   6139:             s = sugar[i]; sugar[i] = sugar[rank]; sugar[rank] = s;
                   6140:             if ( spactive ) {
                   6141:                 pair = spactive[i]; spactive[i] = spactive[rank];
                   6142:                 spactive[rank] = pair;
                   6143:             }
                   6144:         }
                   6145:         pivot = mat[rank];
                   6146:         s = sugar[rank];
                   6147:         inv = invm(pivot[j],md);
                   6148:         for ( k = j, pk = pivot+k; k < col; k++, pk++ )
                   6149:             if ( *pk ) {
                   6150:                 if ( *pk >= (unsigned int)md )
                   6151:                     *pk %= md;
                   6152:                 DMAR(*pk,inv,0,md,*pk)
                   6153:             }
                   6154:         for ( i = rank+1; i < row; i++ ) {
                   6155:             t = mat[i];
                   6156:             if ( a = t[j] ) {
                   6157:                 sugar[i] = MAX(sugar[i],s);
                   6158:                 red_by_vect(md,t+j,pivot+j,md-a,col-j);
                   6159:             }
                   6160:         }
                   6161:         rank++;
                   6162:     }
                   6163:     for ( j = col-1, l = rank-1; j >= 0; j-- )
                   6164:         if ( colstat[j] ) {
                   6165:             pivot = mat[l];
                   6166:             s = sugar[l];
                   6167:             for ( i = 0; i < l; i++ ) {
                   6168:                 t = mat[i];
                   6169:                 t[j] %= md;
                   6170:                 if ( a = t[j] ) {
                   6171:                     sugar[i] = MAX(sugar[i],s);
                   6172:                     red_by_vect(md,t+j,pivot+j,md-a,col-j);
                   6173:                 }
                   6174:             }
                   6175:             l--;
                   6176:         }
                   6177:     for ( j = 0, l = 0; l < rank; j++ )
                   6178:         if ( colstat[j] ) {
                   6179:             t = mat[l];
                   6180:             for ( k = j; k < col; k++ )
                   6181:                 if ( t[k] >= (unsigned int)md )
                   6182:                     t[k] %= md;
                   6183:             l++;
                   6184:         }
                   6185:     return rank;
1.76      noro     6186: }
                   6187:
                   6188: int nd_gauss_elim_sf(int **mat0,int *sugar,int row,int col,int md,int *colstat)
                   6189: {
1.157     noro     6190:     int i,j,k,l,inv,a,rank,s;
                   6191:     unsigned int *t,*pivot,*pk;
                   6192:     unsigned int **mat;
                   6193:
                   6194:     mat = (unsigned int **)mat0;
                   6195:     for ( rank = 0, j = 0; j < col; j++ ) {
                   6196:         for ( i = rank; i < row; i++ )
                   6197:             if ( mat[i][j] )
                   6198:                 break;
                   6199:         if ( i == row ) {
                   6200:             colstat[j] = 0;
                   6201:             continue;
                   6202:         } else
                   6203:             colstat[j] = 1;
                   6204:         if ( i != rank ) {
                   6205:             t = mat[i]; mat[i] = mat[rank]; mat[rank] = t;
                   6206:             s = sugar[i]; sugar[i] = sugar[rank]; sugar[rank] = s;
                   6207:         }
                   6208:         pivot = mat[rank];
                   6209:         s = sugar[rank];
                   6210:         inv = _invsf(pivot[j]);
                   6211:         for ( k = j, pk = pivot+k; k < col; k++, pk++ )
                   6212:             if ( *pk )
                   6213:                 *pk = _mulsf(*pk,inv);
                   6214:         for ( i = rank+1; i < row; i++ ) {
                   6215:             t = mat[i];
                   6216:             if ( a = t[j] ) {
                   6217:                 sugar[i] = MAX(sugar[i],s);
                   6218:                 red_by_vect_sf(md,t+j,pivot+j,_chsgnsf(a),col-j);
                   6219:             }
                   6220:         }
                   6221:         rank++;
                   6222:     }
                   6223:     for ( j = col-1, l = rank-1; j >= 0; j-- )
                   6224:         if ( colstat[j] ) {
                   6225:             pivot = mat[l];
                   6226:             s = sugar[l];
                   6227:             for ( i = 0; i < l; i++ ) {
                   6228:                 t = mat[i];
                   6229:                 if ( a = t[j] ) {
                   6230:                     sugar[i] = MAX(sugar[i],s);
                   6231:                     red_by_vect_sf(md,t+j,pivot+j,_chsgnsf(a),col-j);
                   6232:                 }
                   6233:             }
                   6234:             l--;
                   6235:         }
                   6236:     return rank;
1.77      noro     6237: }
                   6238:
                   6239: int ndv_ishomo(NDV p)
                   6240: {
1.157     noro     6241:     NMV m;
                   6242:     int len,h;
1.77      noro     6243:
1.157     noro     6244:     if ( !p ) return 1;
                   6245:     len = LEN(p);
                   6246:     m = BDY(p);
                   6247:     h = TD(DL(m));
                   6248:     NMV_ADV(m);
                   6249:     for ( len--; len; len--, NMV_ADV(m) )
                   6250:         if ( TD(DL(m)) != h ) return 0;
                   6251:     return 1;
1.77      noro     6252: }
                   6253:
                   6254: void ndv_save(NDV p,int index)
                   6255: {
1.157     noro     6256:     FILE *s;
                   6257:     char name[BUFSIZ];
                   6258:     short id;
                   6259:     int nv,sugar,len,n,i,td,e,j;
                   6260:     NMV m;
                   6261:     unsigned int *dl;
                   6262:     int mpos;
                   6263:
                   6264:     sprintf(name,"%s/%d",Demand,index);
                   6265:     s = fopen(name,"w");
                   6266:     savevl(s,0);
                   6267:     if ( !p ) {
                   6268:         saveobj(s,0);
                   6269:         return;
                   6270:     }
                   6271:     id = O_DP;
                   6272:     nv = NV(p);
                   6273:     sugar = SG(p);
                   6274:     len = LEN(p);
                   6275:     write_short(s,&id); write_int(s,&nv); write_int(s,&sugar);
                   6276:     write_int(s,&len);
                   6277:
                   6278:     for ( m = BDY(p), i = 0; i < len; i++, NMV_ADV(m) ) {
                   6279:         saveobj(s,(Obj)CQ(m));
                   6280:         dl = DL(m);
                   6281:         td = TD(dl);
                   6282:         write_int(s,&td);
                   6283:         for ( j = 0; j < nv; j++ ) {
                   6284:             e = GET_EXP(dl,j);
                   6285:             write_int(s,&e);
                   6286:         }
                   6287:         if ( nd_module ) {
                   6288:             mpos = MPOS(dl); write_int(s,&mpos);
                   6289:         }
                   6290:     }
                   6291:     fclose(s);
1.77      noro     6292: }
                   6293:
                   6294: NDV ndv_load(int index)
                   6295: {
1.157     noro     6296:     FILE *s;
                   6297:     char name[BUFSIZ];
                   6298:     short id;
                   6299:     int nv,sugar,len,n,i,td,e,j;
                   6300:     NDV d;
                   6301:     NMV m0,m;
                   6302:     unsigned int *dl;
                   6303:     Obj obj;
                   6304:     int mpos;
                   6305:
                   6306:     sprintf(name,"%s/%d",Demand,index);
                   6307:     s = fopen(name,"r");
                   6308:     if ( !s ) return 0;
                   6309:
                   6310:     skipvl(s);
                   6311:     read_short(s,&id);
                   6312:     if ( !id ) return 0;
                   6313:     read_int(s,&nv);
                   6314:     read_int(s,&sugar);
                   6315:     read_int(s,&len);
                   6316:
                   6317:     m0 = m = MALLOC(len*nmv_adv);
                   6318:     for ( i = 0; i < len; i++, NMV_ADV(m) ) {
                   6319:         loadobj(s,&obj); CQ(m) = (Q)obj;
                   6320:         dl = DL(m);
                   6321:         ndl_zero(dl);
                   6322:         read_int(s,&td); TD(dl) = td;
                   6323:         for ( j = 0; j < nv; j++ ) {
                   6324:             read_int(s,&e);
                   6325:             PUT_EXP(dl,j,e);
                   6326:         }
                   6327:         if ( nd_module ) {
                   6328:             read_int(s,&mpos); MPOS(dl) = mpos;
                   6329:         }
                   6330:         if ( nd_blockmask ) ndl_weight_mask(dl);
                   6331:     }
                   6332:     fclose(s);
                   6333:     MKNDV(nv,m0,len,d);
                   6334:     SG(d) = sugar;
                   6335:     return d;
1.99      noro     6336: }
                   6337:
1.102     noro     6338: void nd_det(int mod,MAT f,P *rp)
1.99      noro     6339: {
1.157     noro     6340:     VL fv,tv;
                   6341:     int n,i,j,max,e,nvar,sgn,k0,l0,len0,len,k,l,a;
                   6342:     pointer **m;
                   6343:     Q mone;
                   6344:     P **w;
                   6345:     P mp,r;
                   6346:     NDV **dm;
                   6347:     NDV *t,*mi,*mj;
                   6348:     NDV d,s,mij,mjj;
                   6349:     ND u;
                   6350:     NMV nmv;
                   6351:     UINT *bound;
                   6352:     PGeoBucket bucket;
                   6353:     struct order_spec *ord;
                   6354:     Q dq,dt,ds;
                   6355:     N gn,qn,dn0,nm,dn;
                   6356:
                   6357:     create_order_spec(0,0,&ord);
                   6358:     nd_init_ord(ord);
                   6359:     get_vars((Obj)f,&fv);
                   6360:     if ( f->row != f->col )
                   6361:         error("nd_det : non-square matrix");
                   6362:     n = f->row;
                   6363:     m = f->body;
                   6364:     for ( nvar = 0, tv = fv; tv; tv = NEXT(tv), nvar++ );
                   6365:
                   6366:     if ( !nvar ) {
                   6367:         if ( !mod )
                   6368:             detp(CO,(P **)m,n,rp);
                   6369:         else {
                   6370:             w = (P **)almat_pointer(n,n);
                   6371:             for ( i = 0; i < n; i++ )
                   6372:                 for ( j = 0; j < n; j++ )
                   6373:                     ptomp(mod,(P)m[i][j],&w[i][j]);
                   6374:             detmp(CO,mod,w,n,&mp);
                   6375:             mptop(mp,rp);
                   6376:         }
                   6377:         return;
                   6378:     }
                   6379:
                   6380:     if ( !mod ) {
                   6381:         w = (P **)almat_pointer(n,n);
                   6382:         dq = ONE;
                   6383:         for ( i = 0; i < n; i++ ) {
                   6384:             dn0 = ONEN;
                   6385:             for ( j = 0; j < n; j++ ) {
                   6386:                 if ( !m[i][j] ) continue;
                   6387:                 lgp(m[i][j],&nm,&dn);
                   6388:                 gcdn(dn0,dn,&gn); divsn(dn0,gn,&qn); muln(qn,dn,&dn0);
                   6389:             }
                   6390:             if ( !UNIN(dn0) ) {
                   6391:                 NTOQ(dn0,1,ds);
                   6392:                 for ( j = 0; j < n; j++ )
                   6393:                     mulp(CO,(P)m[i][j],(P)ds,&w[i][j]);
                   6394:                 mulq(dq,ds,&dt); dq = dt;
                   6395:             } else
                   6396:                 for ( j = 0; j < n; j++ )
                   6397:                     w[i][j] = (P)m[i][j];
                   6398:         }
                   6399:         m = (pointer **)w;
                   6400:     }
                   6401:
                   6402:     for ( i = 0, max = 0; i < n; i++ )
                   6403:         for ( j = 0; j < n; j++ )
                   6404:             for ( tv = fv; tv; tv = NEXT(tv) ) {
                   6405:                 e = getdeg(tv->v,(P)m[i][j]);
                   6406:                 max = MAX(e,max);
                   6407:             }
                   6408:     nd_setup_parameters(nvar,max);
                   6409:     dm = (NDV **)almat_pointer(n,n);
                   6410:     for ( i = 0, max = 0; i < n; i++ )
                   6411:         for ( j = 0; j < n; j++ ) {
                   6412:             dm[i][j] = ptondv(CO,fv,m[i][j]);
                   6413:             if ( mod ) ndv_mod(mod,dm[i][j]);
                   6414:             if ( dm[i][j] && !LEN(dm[i][j]) ) dm[i][j] = 0;
                   6415:         }
                   6416:     d = ptondv(CO,fv,(P)ONE);
                   6417:     if ( mod ) ndv_mod(mod,d);
                   6418:     chsgnq(ONE,&mone);
                   6419:     for ( j = 0, sgn = 1; j < n; j++ ) {
                   6420:         if ( DP_Print ) fprintf(stderr,".",j);
                   6421:         for ( i = j; i < n && !dm[i][j]; i++ );
                   6422:         if ( i == n ) {
                   6423:             *rp = 0;
                   6424:             return;
                   6425:         }
                   6426:         k0 = i; l0 = j; len0 = LEN(dm[k0][l0]);
                   6427:         for ( k = j; k < n; k++ )
                   6428:             for ( l = j; l < n; l++ )
                   6429:                 if ( dm[k][l] && LEN(dm[k][l]) < len0 ) {
                   6430:                     k0 = k; l0 = l; len0 = LEN(dm[k][l]);
                   6431:                 }
                   6432:         if ( k0 != j ) {
                   6433:             t = dm[j]; dm[j] = dm[k0]; dm[k0] = t;
                   6434:             sgn = -sgn;
                   6435:         }
                   6436:         if ( l0 != j ) {
                   6437:             for ( k = j; k < n; k++ ) {
                   6438:                 s = dm[k][j]; dm[k][j] = dm[k][l0]; dm[k][l0] = s;
                   6439:             }
                   6440:             sgn = -sgn;
                   6441:         }
                   6442:         bound = nd_det_compute_bound(dm,n,j);
                   6443:         for ( k = 0; k < nd_nvar; k++ )
                   6444:             if ( bound[k]*2 > nd_mask0 ) break;
                   6445:         if ( k < nd_nvar )
                   6446:             nd_det_reconstruct(dm,n,j,d);
                   6447:
                   6448:         for ( i = j+1, mj = dm[j], mjj = mj[j]; i < n; i++ ) {
                   6449: /*            if ( DP_Print ) fprintf(stderr,"    i=%d\n        ",i); */
                   6450:             mi = dm[i]; mij = mi[j];
                   6451:             if ( mod )
                   6452:                 ndv_mul_c(mod,mij,mod-1);
                   6453:             else
                   6454:                 ndv_mul_c_q(mij,mone);
                   6455:             for ( k = j+1; k < n; k++ ) {
                   6456: /*                if ( DP_Print ) fprintf(stderr,"k=%d ",k); */
                   6457:                 bucket = create_pbucket();
                   6458:                 if ( mi[k] ) {
                   6459:                     nmv = BDY(mjj); len = LEN(mjj);
                   6460:                     for ( a = 0; a < len; a++, NMV_ADV(nmv) ) {
                   6461:                         u = ndv_mul_nmv_trunc(mod,nmv,mi[k],DL(BDY(d)));
                   6462:                         add_pbucket(mod,bucket,u);
                   6463:                     }
                   6464:                 }
                   6465:                 if ( mj[k] && mij ) {
                   6466:                     nmv = BDY(mij); len = LEN(mij);
                   6467:                     for ( a = 0; a < len; a++, NMV_ADV(nmv) ) {
                   6468:                         u = ndv_mul_nmv_trunc(mod,nmv,mj[k],DL(BDY(d)));
                   6469:                         add_pbucket(mod,bucket,u);
                   6470:                     }
                   6471:                 }
                   6472:                 u = nd_quo(mod,bucket,d);
                   6473:                 mi[k] = ndtondv(mod,u);
                   6474:             }
                   6475: /*            if ( DP_Print ) fprintf(stderr,"\n",k); */
                   6476:         }
                   6477:         d = mjj;
                   6478:     }
                   6479:     if ( DP_Print ) fprintf(stderr,"\n",k);
                   6480:     if ( sgn < 0 )
                   6481:         if ( mod )
                   6482:             ndv_mul_c(mod,d,mod-1);
                   6483:         else
                   6484:             ndv_mul_c_q(d,mone);
                   6485:     r = ndvtop(mod,CO,fv,d);
                   6486:     if ( !mod && !UNIQ(dq) )
                   6487:         divsp(CO,r,(P)dq,rp);
                   6488:     else
                   6489:         *rp = r;
1.99      noro     6490: }
                   6491:
1.102     noro     6492: ND ndv_mul_nmv_trunc(int mod,NMV m0,NDV p,UINT *d)
1.99      noro     6493: {
1.157     noro     6494:     NM mr,mr0;
                   6495:     NM tnm;
                   6496:     NMV m;
                   6497:     UINT *d0,*dt,*dm;
                   6498:     int c,n,td,i,c1,c2,len;
                   6499:     Q q;
                   6500:     ND r;
                   6501:
                   6502:     if ( !p ) return 0;
                   6503:     else {
                   6504:         n = NV(p); m = BDY(p); len = LEN(p);
                   6505:         d0 = DL(m0);
                   6506:         td = TD(d);
                   6507:         mr0 = 0;
                   6508:         NEWNM(tnm);
                   6509:         if ( mod ) {
                   6510:             c = CM(m0);
                   6511:             for ( i = 0; i < len; i++, NMV_ADV(m) ) {
                   6512:                 ndl_add(DL(m),d0,DL(tnm));
                   6513:                 if ( ndl_reducible(DL(tnm),d) ) {
                   6514:                     NEXTNM(mr0,mr);
                   6515:                     c1 = CM(m); DMAR(c1,c,0,mod,c2); CM(mr) = c2;
                   6516:                     ndl_copy(DL(tnm),DL(mr));
                   6517:                 }
                   6518:             }
                   6519:         } else {
                   6520:             q = CQ(m0);
                   6521:             for ( i = 0; i < len; i++, NMV_ADV(m) ) {
                   6522:                 ndl_add(DL(m),d0,DL(tnm));
                   6523:                 if ( ndl_reducible(DL(tnm),d) ) {
                   6524:                     NEXTNM(mr0,mr);
                   6525:                     mulq(CQ(m),q,&CQ(mr));
                   6526:                     ndl_copy(DL(tnm),DL(mr));
                   6527:                 }
                   6528:             }
                   6529:         }
                   6530:         if ( !mr0 )
                   6531:             return 0;
                   6532:         else {
                   6533:             NEXT(mr) = 0;
                   6534:             for ( len = 0, mr = mr0; mr; mr = NEXT(mr), len++ );
                   6535:             MKND(NV(p),mr0,len,r);
                   6536:             SG(r) = SG(p) + TD(d0);
                   6537:             return r;
                   6538:         }
                   6539:     }
1.114     noro     6540: }
                   6541:
                   6542: void nd_det_reconstruct(NDV **dm,int n,int j,NDV d)
                   6543: {
1.157     noro     6544:     int i,obpe,oadv,h,k,l;
                   6545:     static NM prev_nm_free_list;
                   6546:     EPOS oepos;
                   6547:
                   6548:     obpe = nd_bpe;
                   6549:     oadv = nmv_adv;
                   6550:     oepos = nd_epos;
                   6551:     if ( obpe < 2 ) nd_bpe = 2;
                   6552:     else if ( obpe < 3 ) nd_bpe = 3;
                   6553:     else if ( obpe < 4 ) nd_bpe = 4;
                   6554:     else if ( obpe < 5 ) nd_bpe = 5;
                   6555:     else if ( obpe < 6 ) nd_bpe = 6;
                   6556:     else if ( obpe < 8 ) nd_bpe = 8;
                   6557:     else if ( obpe < 10 ) nd_bpe = 10;
                   6558:     else if ( obpe < 16 ) nd_bpe = 16;
                   6559:     else if ( obpe < 32 ) nd_bpe = 32;
                   6560:     else error("nd_det_reconstruct : exponent too large");
                   6561:
                   6562:     nd_setup_parameters(nd_nvar,0);
                   6563:     prev_nm_free_list = _nm_free_list;
                   6564:     _nm_free_list = 0;
                   6565:     for ( k = j; k < n; k++ )
                   6566:         for (l = j; l < n; l++ )
                   6567:             ndv_realloc(dm[k][l],obpe,oadv,oepos);
                   6568:     ndv_realloc(d,obpe,oadv,oepos);
                   6569:     prev_nm_free_list = 0;
1.114     noro     6570: #if 0
1.157     noro     6571:     GC_gcollect();
1.114     noro     6572: #endif
                   6573: }
                   6574:
1.153     noro     6575: /* returns a UINT array containing degree bounds */
                   6576:
1.114     noro     6577: UINT *nd_det_compute_bound(NDV **dm,int n,int j)
                   6578: {
1.157     noro     6579:     UINT *d0,*d1,*d,*t,*r;
                   6580:     int k,l,i;
1.114     noro     6581:
1.157     noro     6582:     d0 = (UINT *)MALLOC(nd_nvar*sizeof(UINT));
                   6583:     for ( k = 0; k < nd_nvar; k++ ) d0[k] = 0;
                   6584:     for ( k = j; k < n; k++ )
                   6585:         for ( l = j; l < n; l++ )
                   6586:             if ( dm[k][l] ) {
                   6587:                 d = ndv_compute_bound(dm[k][l]);
                   6588:                 for ( i = 0; i < nd_nvar; i++ )
                   6589:                     d0[i] = MAX(d0[i],d[i]);
                   6590:             }
                   6591:     return d0;
1.117     noro     6592: }
                   6593:
                   6594: DL nd_separate_d(UINT *d,UINT *trans)
                   6595: {
1.157     noro     6596:     int n,td,i,e,j;
                   6597:     DL a;
1.117     noro     6598:
1.157     noro     6599:     ndl_zero(trans);
                   6600:     td = 0;
                   6601:     for ( i = 0; i < nd_ntrans; i++ ) {
                   6602:         e = GET_EXP(d,i);
                   6603:         PUT_EXP(trans,i,e);
                   6604:         td += MUL_WEIGHT(e,i);
                   6605:     }
                   6606:     if ( nd_ntrans+nd_nalg < nd_nvar ) {
                   6607:         /* homogenized */
                   6608:         i = nd_nvar-1;
                   6609:         e = GET_EXP(d,i);
                   6610:         PUT_EXP(trans,i,e);
                   6611:         td += MUL_WEIGHT(e,i);
                   6612:     }
                   6613:     TD(trans) = td;
                   6614:     if ( nd_blockmask) ndl_weight_mask(trans);
                   6615:     NEWDL(a,nd_nalg);
                   6616:     td = 0;
                   6617:     for ( i = 0; i < nd_nalg; i++ ) {
                   6618:         j = nd_ntrans+i;
                   6619:         e = GET_EXP(d,j);
                   6620:         a->d[i] = e;
                   6621:         td += e;
                   6622:     }
                   6623:     a->td = td;
                   6624:     return a;
1.117     noro     6625: }
                   6626:
1.118     noro     6627: int nd_monic(int mod,ND *p)
1.117     noro     6628: {
1.157     noro     6629:     UINT *trans,*t;
                   6630:     DL alg;
                   6631:     MP mp0,mp;
                   6632:     NM m,m0,m1,ma0,ma,mb,mr0,mr;
                   6633:     ND r;
                   6634:     DL dl;
                   6635:     DP nm;
                   6636:     NDV ndv;
                   6637:     DAlg inv,cd;
                   6638:     ND s,c;
                   6639:     Q l,mul;
                   6640:     N ln;
                   6641:     int n,ntrans,i,e,td,is_lc,len;
                   6642:     NumberField nf;
                   6643:     struct oEGT eg0,eg1;
                   6644:
                   6645:     if ( !(nf = get_numberfield()) )
                   6646:         error("nd_monic : current_numberfield is not set");
                   6647:
                   6648:     /* Q coef -> DAlg coef */
                   6649:     NEWNM(ma0); ma = ma0;
                   6650:     m = BDY(*p);
                   6651:     is_lc = 1;
                   6652:     while ( 1 ) {
                   6653:         NEWMP(mp0); mp = mp0;
                   6654:         mp->c = (P)CQ(m);
                   6655:         mp->dl = nd_separate_d(DL(m),DL(ma));
                   6656:         NEWNM(mb);
                   6657:         for ( m = NEXT(m); m; m = NEXT(m) ) {
                   6658:             alg = nd_separate_d(DL(m),DL(mb));
                   6659:             if ( !ndl_equal(DL(ma),DL(mb)) )
                   6660:                 break;
                   6661:             NEXTMP(mp0,mp); mp->c = (P)CQ(m); mp->dl = alg;
                   6662:         }
                   6663:         NEXT(mp) = 0;
                   6664:         MKDP(nd_nalg,mp0,nm);
                   6665:         MKDAlg(nm,ONE,cd);
                   6666:         if ( is_lc == 1 ) {
                   6667:             /* if the lc is a rational number, we have nothing to do */
                   6668:             if ( !mp0->dl->td )
                   6669:                 return 1;
                   6670:
                   6671:             get_eg(&eg0);
                   6672:             invdalg(cd,&inv);
                   6673:             get_eg(&eg1); add_eg(&eg_invdalg,&eg0,&eg1);
                   6674:             /* check the validity of inv */
                   6675:             if ( mod && !rem(NM(inv->dn),mod) )
                   6676:                 return 0;
                   6677:             CA(ma) = nf->one;
                   6678:             is_lc = 0;
                   6679:             ln = ONEN;
                   6680:         } else {
                   6681:             muldalg(cd,inv,&CA(ma));
                   6682:             lcmn(ln,NM(CA(ma)->dn),&ln);
                   6683:         }
                   6684:         if ( m ) {
                   6685:             NEXT(ma) = mb; ma = mb;
                   6686:         } else {
                   6687:             NEXT(ma) = 0;
                   6688:             break;
                   6689:         }
                   6690:     }
                   6691:     /* l = lcm(denoms) */
                   6692:     NTOQ(ln,1,l);
                   6693:     for ( mr0 = 0, m = ma0; m; m = NEXT(m) ) {
                   6694:         divq(l,CA(m)->dn,&mul);
                   6695:         for ( mp = BDY(CA(m)->nm); mp; mp = NEXT(mp) ) {
                   6696:             NEXTNM(mr0,mr);
                   6697:             mulq((Q)mp->c,mul,&CQ(mr));
                   6698:             dl = mp->dl;
                   6699:             td = TD(DL(m));
                   6700:             ndl_copy(DL(m),DL(mr));
                   6701:             for ( i = 0; i < nd_nalg; i++ ) {
                   6702:                 e = dl->d[i];
                   6703:                 PUT_EXP(DL(mr),i+nd_ntrans,e);
                   6704:                 td += MUL_WEIGHT(e,i+nd_ntrans);
                   6705:             }
1.163     noro     6706:             if ( nd_module ) MPOS(DL(mr)) = MPOS(DL(m));
1.157     noro     6707:             TD(DL(mr)) = td;
                   6708:             if ( nd_blockmask) ndl_weight_mask(DL(mr));
                   6709:         }
                   6710:     }
                   6711:     NEXT(mr) = 0;
                   6712:     for ( len = 0, mr = mr0; mr; mr = NEXT(mr), len++ );
                   6713:     MKND(NV(*p),mr0,len,r);
                   6714:     /* XXX */
                   6715:     SG(r) = SG(*p);
                   6716:     nd_free(*p);
                   6717:     *p = r;
                   6718:     return 1;
1.59      noro     6719: }
1.167     noro     6720:
                   6721: NODE reverse_node(NODE n)
                   6722: {
                   6723:     NODE t,t1;
                   6724:
                   6725:     for ( t = 0; n; n = NEXT(n) ) {
                   6726:         MKNODE(t1,BDY(n),t); t = t1;
                   6727:     }
                   6728:     return t;
                   6729: }
                   6730:
                   6731: P ndc_div(int mod,union oNDC a,union oNDC b)
                   6732: {
                   6733:     union oNDC c;
                   6734:     int inv,t;
                   6735:
                   6736:     if ( mod == -1 ) c.m = _mulsf(a.m,_invsf(b.m));
                   6737:     else if ( mod ) {
                   6738:         inv = invm(b.m,mod);
                   6739:         DMAR(a.m,inv,0,mod,t); c.m = t;
                   6740:     } else if ( nd_vc )
                   6741:        divsp(nd_vc,a.p,b.p,&c.p);
                   6742:     else
                   6743:        divq(a.z,b.z,&c.z);
                   6744:     return ndctop(mod,c);
                   6745: }
                   6746:
                   6747: P ndctop(int mod,union oNDC c)
                   6748: {
                   6749:     Q q;
                   6750:     int e;
                   6751:     GFS gfs;
                   6752:
                   6753:     if ( mod == -1 ) {
                   6754:         e = IFTOF(c.m); MKGFS(e,gfs); return (P)gfs;
                   6755:     } else if ( mod ) {
                   6756:         STOQ(c.m,q); return (P)q;
                   6757:     } else
                   6758:         return (P)c.p;
                   6759: }
                   6760:
                   6761: /* [0,0,0,cont] = p -> p/cont */
                   6762:
                   6763: void finalize_tracelist(int i,P cont)
                   6764: {
                   6765:         LIST l;
                   6766:         NODE node;
                   6767:      Q iq;
                   6768:
                   6769:         if ( !UNIQ(cont) ) {
                   6770:          node = mknode(4,0,0,0,cont);
                   6771:          MKLIST(l,node); MKNODE(node,l,nd_tracelist);
                   6772:                 nd_tracelist = node;
                   6773:         }
                   6774:      STOQ(i,iq);
                   6775:      nd_tracelist = reverse_node(nd_tracelist);
                   6776:      MKLIST(l,nd_tracelist);
                   6777:      node = mknode(2,iq,l); MKLIST(l,node);
                   6778:      MKNODE(node,l,nd_alltracelist); MKLIST(l,node);
                   6779:      nd_alltracelist = node; nd_tracelist = 0;
                   6780: }
                   6781:
                   6782: void conv_ilist(int demand,int trace,NODE g,int **indp)
                   6783: {
                   6784:     int n,i,j;
                   6785:        int *ind;
                   6786:        NODE t;
                   6787:
                   6788:     n = length(g);
                   6789:        ind = (int *)MALLOC(n*sizeof(int));
                   6790:        for ( i = 0, t = g; i < n; i++, t = NEXT(t) ) {
                   6791:                j = (long)BDY(t); ind[i] = j;
                   6792:                BDY(t) = (pointer)(demand?ndv_load(j):(trace?nd_ps_trace[j]:nd_ps[j]));
                   6793:        }
                   6794:        if ( indp ) *indp = ind;
                   6795: }
1.172   ! noro     6796:
        !          6797: void parse_nd_option(NODE opt)
        !          6798: {
        !          6799:     NODE t,p;
        !          6800:     char *key;
        !          6801:     Obj value;
        !          6802:
        !          6803:     nd_gentrace = 0; nd_gensyz = 0;
        !          6804:     for ( t = opt; t; t = NEXT(t) ) {
        !          6805:         p = BDY((LIST)BDY(t));
        !          6806:         key = BDY((STRING)BDY(p));
        !          6807:         value = (Obj)BDY(NEXT(p));
        !          6808:         if ( !strcmp(key,"gentrace") )
        !          6809:             nd_gentrace = value?1:0;
        !          6810:         else if ( !strcmp(key,"gensyz") )
        !          6811:             nd_gensyz = value?1:0;
        !          6812:     }
        !          6813: }

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