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

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

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