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

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

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