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

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

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