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

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

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