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

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

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