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

1.112   ! noro        1: /* $OpenXM: OpenXM_contrib2/asir2000/engine/nd.c,v 1.111 2004/09/21 07:19:01 noro Exp $ */
1.2       noro        2:
1.94      noro        3: #include "nd.h"
1.63      noro        4:
1.61      noro        5: int (*ndl_compare_function)(UINT *a1,UINT *a2);
1.94      noro        6: int nd_dcomp;
                      7: NM _nm_free_list;
                      8: ND _nd_free_list;
                      9: ND_pairs _ndp_free_list;
1.32      noro       10:
1.103     noro       11: #if 0
1.74      noro       12: static int ndv_alloc;
1.103     noro       13: #endif
1.87      noro       14: #if 1
1.69      noro       15: static int nd_f4_nsp=0x7fffffff;
1.87      noro       16: #else
                     17: static int nd_f4_nsp=50;
                     18: #endif
1.42      noro       19: static double nd_scale=2;
1.61      noro       20: static UINT **nd_bound;
1.42      noro       21: static struct order_spec *nd_ord;
                     22: static EPOS nd_epos;
1.43      noro       23: static BlockMask nd_blockmask;
1.42      noro       24: static int nd_nvar;
                     25: static int nd_isrlex;
                     26: static int nd_epw,nd_bpe,nd_wpd,nd_exporigin;
1.61      noro       27: static UINT nd_mask[32];
                     28: static UINT nd_mask0,nd_mask1;
1.42      noro       29:
1.20      noro       30: static NDV *nd_ps;
1.53      noro       31: static NDV *nd_ps_trace;
1.42      noro       32: static RHist *nd_psh;
                     33: static int nd_psn,nd_pslen;
                     34: static RHist *nd_red;
1.96      noro       35: static int *nd_work_vector;
                     36: static int **nd_matrix;
                     37: static int nd_matrix_len;
1.97      noro       38: static struct weight_or_block *nd_worb;
                     39: static int nd_worb_len;
1.42      noro       40: static int nd_found,nd_create,nd_notfirst;
                     41: static int nmv_adv;
1.77      noro       42: static int nd_demand;
1.1       noro       43:
                     44: void nd_free_private_storage()
                     45: {
                     46:        _nm_free_list = 0;
1.5       noro       47:        _ndp_free_list = 0;
1.71      noro       48: #if 0
1.1       noro       49:        GC_gcollect();
1.71      noro       50: #endif
1.1       noro       51: }
                     52:
                     53: void _NM_alloc()
                     54: {
                     55:        NM p;
                     56:        int i;
                     57:
1.11      noro       58:        for ( i = 0; i < 1024; i++ ) {
1.61      noro       59:                p = (NM)GC_malloc(sizeof(struct oNM)+(nd_wpd-1)*sizeof(UINT));
1.1       noro       60:                p->next = _nm_free_list; _nm_free_list = p;
                     61:        }
                     62: }
                     63:
                     64: void _ND_alloc()
                     65: {
                     66:        ND p;
                     67:        int i;
                     68:
                     69:        for ( i = 0; i < 1024; i++ ) {
                     70:                p = (ND)GC_malloc(sizeof(struct oND));
                     71:                p->body = (NM)_nd_free_list; _nd_free_list = p;
                     72:        }
                     73: }
                     74:
                     75: void _NDP_alloc()
                     76: {
                     77:        ND_pairs p;
                     78:        int i;
                     79:
1.11      noro       80:        for ( i = 0; i < 1024; i++ ) {
1.1       noro       81:                p = (ND_pairs)GC_malloc(sizeof(struct oND_pairs)
1.61      noro       82:                        +(nd_wpd-1)*sizeof(UINT));
1.1       noro       83:                p->next = _ndp_free_list; _ndp_free_list = p;
                     84:        }
                     85: }
                     86:
1.30      noro       87: INLINE int nd_length(ND p)
1.1       noro       88: {
                     89:        NM m;
                     90:        int i;
                     91:
                     92:        if ( !p )
                     93:                return 0;
                     94:        else {
                     95:                for ( i = 0, m = BDY(p); m; m = NEXT(m), i++ );
                     96:                return i;
                     97:        }
                     98: }
                     99:
1.61      noro      100: INLINE int ndl_reducible(UINT *d1,UINT *d2)
1.1       noro      101: {
1.61      noro      102:        UINT u1,u2;
1.1       noro      103:        int i,j;
                    104:
1.34      noro      105:        if ( TD(d1) < TD(d2) ) return 0;
1.65      noro      106: #if USE_UNROLL
1.1       noro      107:        switch ( nd_bpe ) {
1.62      noro      108:                case 3:
                    109:                        for ( i = nd_exporigin; i < nd_wpd; i++ ) {
                    110:                                u1 = d1[i]; u2 = d2[i];
                    111:                                if ( (u1&0x38000000) < (u2&0x38000000) ) return 0;
                    112:                                if ( (u1& 0x7000000) < (u2& 0x7000000) ) return 0;
                    113:                                if ( (u1&  0xe00000) < (u2&  0xe00000) ) return 0;
                    114:                                if ( (u1&  0x1c0000) < (u2&  0x1c0000) ) return 0;
                    115:                                if ( (u1&   0x38000) < (u2&   0x38000) ) return 0;
                    116:                                if ( (u1&    0x7000) < (u2&    0x7000) ) return 0;
                    117:                                if ( (u1&     0xe00) < (u2&     0xe00) ) return 0;
                    118:                                if ( (u1&     0x1c0) < (u2&     0x1c0) ) return 0;
                    119:                                if ( (u1&      0x38) < (u2&      0x38) ) return 0;
                    120:                                if ( (u1&       0x7) < (u2&       0x7) ) return 0;
                    121:                        }
                    122:                        return 1;
                    123:                        break;
1.1       noro      124:                case 4:
1.41      noro      125:                        for ( i = nd_exporigin; i < nd_wpd; i++ ) {
1.1       noro      126:                                u1 = d1[i]; u2 = d2[i];
                    127:                                if ( (u1&0xf0000000) < (u2&0xf0000000) ) return 0;
1.62      noro      128:                                if ( (u1& 0xf000000) < (u2& 0xf000000) ) return 0;
                    129:                                if ( (u1&  0xf00000) < (u2&  0xf00000) ) return 0;
                    130:                                if ( (u1&   0xf0000) < (u2&   0xf0000) ) return 0;
                    131:                                if ( (u1&    0xf000) < (u2&    0xf000) ) return 0;
                    132:                                if ( (u1&     0xf00) < (u2&     0xf00) ) return 0;
                    133:                                if ( (u1&      0xf0) < (u2&      0xf0) ) return 0;
                    134:                                if ( (u1&       0xf) < (u2&       0xf) ) return 0;
1.1       noro      135:                        }
                    136:                        return 1;
                    137:                        break;
                    138:                case 6:
1.41      noro      139:                        for ( i = nd_exporigin; i < nd_wpd; i++ ) {
1.1       noro      140:                                u1 = d1[i]; u2 = d2[i];
                    141:                                if ( (u1&0x3f000000) < (u2&0x3f000000) ) return 0;
1.62      noro      142:                                if ( (u1&  0xfc0000) < (u2&  0xfc0000) ) return 0;
                    143:                                if ( (u1&   0x3f000) < (u2&   0x3f000) ) return 0;
                    144:                                if ( (u1&     0xfc0) < (u2&     0xfc0) ) return 0;
                    145:                                if ( (u1&      0x3f) < (u2&      0x3f) ) return 0;
1.1       noro      146:                        }
                    147:                        return 1;
                    148:                        break;
                    149:                case 8:
1.41      noro      150:                        for ( i = nd_exporigin; i < nd_wpd; i++ ) {
1.1       noro      151:                                u1 = d1[i]; u2 = d2[i];
                    152:                                if ( (u1&0xff000000) < (u2&0xff000000) ) return 0;
1.62      noro      153:                                if ( (u1&  0xff0000) < (u2&  0xff0000) ) return 0;
                    154:                                if ( (u1&    0xff00) < (u2&    0xff00) ) return 0;
                    155:                                if ( (u1&      0xff) < (u2&      0xff) ) return 0;
1.1       noro      156:                        }
                    157:                        return 1;
                    158:                        break;
                    159:                case 16:
1.41      noro      160:                        for ( i = nd_exporigin; i < nd_wpd; i++ ) {
1.1       noro      161:                                u1 = d1[i]; u2 = d2[i];
                    162:                                if ( (u1&0xffff0000) < (u2&0xffff0000) ) return 0;
1.62      noro      163:                                if ( (u1&    0xffff) < (u2&    0xffff) ) return 0;
1.1       noro      164:                        }
                    165:                        return 1;
                    166:                        break;
                    167:                case 32:
1.41      noro      168:                        for ( i = nd_exporigin; i < nd_wpd; i++ )
1.1       noro      169:                                if ( d1[i] < d2[i] ) return 0;
                    170:                        return 1;
                    171:                        break;
                    172:                default:
1.41      noro      173:                        for ( i = nd_exporigin; i < nd_wpd; i++ ) {
1.1       noro      174:                                u1 = d1[i]; u2 = d2[i];
                    175:                                for ( j = 0; j < nd_epw; j++ )
                    176:                                        if ( (u1&nd_mask[j]) < (u2&nd_mask[j]) ) return 0;
                    177:                        }
                    178:                        return 1;
                    179:        }
1.65      noro      180: #else
                    181:        for ( i = nd_exporigin; i < nd_wpd; i++ ) {
                    182:                u1 = d1[i]; u2 = d2[i];
                    183:                for ( j = 0; j < nd_epw; j++ )
                    184:                        if ( (u1&nd_mask[j]) < (u2&nd_mask[j]) ) return 0;
                    185:        }
                    186:        return 1;
                    187: #endif
1.1       noro      188: }
                    189:
1.61      noro      190: /*
                    191:  * If the current order is a block order,
                    192:  * then the last block is length 1 and contains
                    193:  * the homo variable. Otherwise, the original
                    194:  * order is either 0 or 2.
                    195:  */
                    196:
                    197: void ndl_homogenize(UINT *d,UINT *r,int obpe,EPOS oepos,int weight)
1.23      noro      198: {
1.61      noro      199:        int w,i,e,n,omask0;
                    200:
                    201:        omask0 = (1<<obpe)-1;
                    202:        n = nd_nvar-1;
                    203:        ndl_zero(r);
                    204:        for ( i = 0; i < n; i++ ) {
                    205:                e = GET_EXP_OLD(d,i);
                    206:                PUT_EXP(r,i,e);
                    207:        }
                    208:        w = TD(d);
                    209:        PUT_EXP(r,nd_nvar-1,weight-w);
                    210:        TD(r) = weight;
                    211:        if ( nd_blockmask ) ndl_weight_mask(r);
                    212: }
                    213:
                    214: void ndl_dehomogenize(UINT *d)
                    215: {
                    216:        UINT mask;
                    217:        UINT h;
1.31      noro      218:        int i,bits;
1.23      noro      219:
1.44      noro      220:        if ( nd_blockmask ) {
                    221:                h = GET_EXP(d,nd_nvar-1);
1.45      noro      222:                XOR_EXP(d,nd_nvar-1,h);
1.44      noro      223:                TD(d) -= h;
                    224:                d[nd_exporigin-1] -= h;
                    225:        } else {
                    226:                if ( nd_isrlex ) {
                    227:                        if ( nd_bpe == 32 ) {
                    228:                                h = d[nd_exporigin];
                    229:                                for ( i = nd_exporigin+1; i < nd_wpd; i++ )
                    230:                                        d[i-1] = d[i];
                    231:                                d[i-1] = 0;
                    232:                                TD(d) -= h;
                    233:                        } else {
                    234:                                bits = nd_epw*nd_bpe;
                    235:                                mask = bits==32?0xffffffff:((1<<(nd_epw*nd_bpe))-1);
                    236:                                h = (d[nd_exporigin]>>((nd_epw-1)*nd_bpe))&nd_mask0;
                    237:                                for ( i = nd_exporigin; i < nd_wpd; i++ )
                    238:                                        d[i] = ((d[i]<<nd_bpe)&mask)
                    239:                                                |(i+1<nd_wpd?((d[i+1]>>((nd_epw-1)*nd_bpe))&nd_mask0):0);
                    240:                                TD(d) -= h;
                    241:                        }
1.45      noro      242:                } else {
                    243:                        h = GET_EXP(d,nd_nvar-1);
                    244:                        XOR_EXP(d,nd_nvar-1,h);
                    245:                        TD(d) -= h;
                    246:                }
1.44      noro      247:        }
1.23      noro      248: }
                    249:
1.61      noro      250: void ndl_lcm(UINT *d1,unsigned *d2,UINT *d)
1.1       noro      251: {
1.61      noro      252:        UINT t1,t2,u,u1,u2;
1.43      noro      253:        int i,j,l;
1.1       noro      254:
1.65      noro      255: #if USE_UNROLL
1.1       noro      256:        switch ( nd_bpe ) {
1.62      noro      257:                case 3:
                    258:                        for ( i = nd_exporigin; i < nd_wpd; i++ ) {
                    259:                                u1 = d1[i]; u2 = d2[i];
                    260:                                t1 = (u1&0x38000000); t2 = (u2&0x38000000); u = t1>t2?t1:t2;
                    261:                                t1 = (u1& 0x7000000); t2 = (u2& 0x7000000); u |= t1>t2?t1:t2;
                    262:                                t1 = (u1&  0xe00000); t2 = (u2&  0xe00000); u |= t1>t2?t1:t2;
                    263:                                t1 = (u1&  0x1c0000); t2 = (u2&  0x1c0000); u |= t1>t2?t1:t2;
                    264:                                t1 = (u1&   0x38000); t2 = (u2&   0x38000); u |= t1>t2?t1:t2;
                    265:                                t1 = (u1&    0x7000); t2 = (u2&    0x7000); u |= t1>t2?t1:t2;
                    266:                                t1 = (u1&     0xe00); t2 = (u2&     0xe00); u |= t1>t2?t1:t2;
                    267:                                t1 = (u1&     0x1c0); t2 = (u2&     0x1c0); u |= t1>t2?t1:t2;
                    268:                                t1 = (u1&      0x38); t2 = (u2&      0x38); u |= t1>t2?t1:t2;
                    269:                                t1 = (u1&       0x7); t2 = (u2&       0x7); u |= t1>t2?t1:t2;
                    270:                                d[i] = u;
                    271:                        }
                    272:                        break;
1.1       noro      273:                case 4:
1.41      noro      274:                        for ( i = nd_exporigin; i < nd_wpd; i++ ) {
1.1       noro      275:                                u1 = d1[i]; u2 = d2[i];
                    276:                                t1 = (u1&0xf0000000); t2 = (u2&0xf0000000); u = t1>t2?t1:t2;
1.62      noro      277:                                t1 = (u1& 0xf000000); t2 = (u2& 0xf000000); u |= t1>t2?t1:t2;
                    278:                                t1 = (u1&  0xf00000); t2 = (u2&  0xf00000); u |= t1>t2?t1:t2;
                    279:                                t1 = (u1&   0xf0000); t2 = (u2&   0xf0000); u |= t1>t2?t1:t2;
                    280:                                t1 = (u1&    0xf000); t2 = (u2&    0xf000); u |= t1>t2?t1:t2;
                    281:                                t1 = (u1&     0xf00); t2 = (u2&     0xf00); u |= t1>t2?t1:t2;
                    282:                                t1 = (u1&      0xf0); t2 = (u2&      0xf0); u |= t1>t2?t1:t2;
                    283:                                t1 = (u1&       0xf); t2 = (u2&       0xf); u |= t1>t2?t1:t2;
1.1       noro      284:                                d[i] = u;
                    285:                        }
                    286:                        break;
                    287:                case 6:
1.41      noro      288:                        for ( i = nd_exporigin; i < nd_wpd; i++ ) {
1.1       noro      289:                                u1 = d1[i]; u2 = d2[i];
                    290:                                t1 = (u1&0x3f000000); t2 = (u2&0x3f000000); u = t1>t2?t1:t2;
1.62      noro      291:                                t1 = (u1&  0xfc0000); t2 = (u2&  0xfc0000); u |= t1>t2?t1:t2;
                    292:                                t1 = (u1&   0x3f000); t2 = (u2&   0x3f000); u |= t1>t2?t1:t2;
                    293:                                t1 = (u1&     0xfc0); t2 = (u2&     0xfc0); u |= t1>t2?t1:t2;
                    294:                                t1 = (u1&      0x3f); t2 = (u2&      0x3f); u |= t1>t2?t1:t2;
1.1       noro      295:                                d[i] = u;
                    296:                        }
                    297:                        break;
                    298:                case 8:
1.41      noro      299:                        for ( i = nd_exporigin; i < nd_wpd; i++ ) {
1.1       noro      300:                                u1 = d1[i]; u2 = d2[i];
                    301:                                t1 = (u1&0xff000000); t2 = (u2&0xff000000); u = t1>t2?t1:t2;
1.62      noro      302:                                t1 = (u1&  0xff0000); t2 = (u2&  0xff0000); u |= t1>t2?t1:t2;
                    303:                                t1 = (u1&    0xff00); t2 = (u2&    0xff00); u |= t1>t2?t1:t2;
                    304:                                t1 = (u1&      0xff); t2 = (u2&      0xff); u |= t1>t2?t1:t2;
1.1       noro      305:                                d[i] = u;
                    306:                        }
                    307:                        break;
                    308:                case 16:
1.41      noro      309:                        for ( i = nd_exporigin; i < nd_wpd; i++ ) {
1.1       noro      310:                                u1 = d1[i]; u2 = d2[i];
                    311:                                t1 = (u1&0xffff0000); t2 = (u2&0xffff0000); u = t1>t2?t1:t2;
1.62      noro      312:                                t1 = (u1&    0xffff); t2 = (u2&    0xffff); u |= t1>t2?t1:t2;
1.1       noro      313:                                d[i] = u;
                    314:                        }
                    315:                        break;
                    316:                case 32:
1.41      noro      317:                        for ( i = nd_exporigin; i < nd_wpd; i++ ) {
1.1       noro      318:                                u1 = d1[i]; u2 = d2[i];
                    319:                                d[i] = u1>u2?u1:u2;
                    320:                        }
                    321:                        break;
                    322:                default:
1.41      noro      323:                        for ( i = nd_exporigin; i < nd_wpd; i++ ) {
1.1       noro      324:                                u1 = d1[i]; u2 = d2[i];
                    325:                                for ( j = 0, u = 0; j < nd_epw; j++ ) {
                    326:                                        t1 = (u1&nd_mask[j]); t2 = (u2&nd_mask[j]); u |= t1>t2?t1:t2;
                    327:                                }
                    328:                                d[i] = u;
                    329:                        }
                    330:                        break;
                    331:        }
1.65      noro      332: #else
                    333:        for ( i = nd_exporigin; i < nd_wpd; i++ ) {
                    334:                u1 = d1[i]; u2 = d2[i];
                    335:                for ( j = 0, u = 0; j < nd_epw; j++ ) {
                    336:                        t1 = (u1&nd_mask[j]); t2 = (u2&nd_mask[j]); u |= t1>t2?t1:t2;
                    337:                }
                    338:                d[i] = u;
                    339:        }
                    340: #endif
1.39      noro      341:        TD(d) = ndl_weight(d);
1.61      noro      342:        if ( nd_blockmask ) ndl_weight_mask(d);
1.57      noro      343: }
                    344:
1.61      noro      345: int ndl_weight(UINT *d)
1.1       noro      346: {
1.61      noro      347:        UINT t,u;
1.1       noro      348:        int i,j;
                    349:
1.60      noro      350:        if ( current_dl_weight_vector )
                    351:                for ( i = 0, t = 0; i < nd_nvar; i++ ) {
                    352:                        u = GET_EXP(d,i);
                    353:                        t += MUL_WEIGHT(u,i);
                    354:                }
                    355:        else
                    356:                for ( t = 0, i = nd_exporigin; i < nd_wpd; i++ ) {
                    357:                        u = d[i];
                    358:                        for ( j = 0; j < nd_epw; j++, u>>=nd_bpe )
                    359:                                t += (u&nd_mask0);
                    360:                }
1.1       noro      361:        return t;
                    362: }
                    363:
1.61      noro      364: void ndl_weight_mask(UINT *d)
1.43      noro      365: {
1.61      noro      366:        UINT t,u;
                    367:        UINT *mask;
                    368:        int i,j,k,l;
1.43      noro      369:
1.61      noro      370:        l = nd_blockmask->n;
                    371:        for ( k = 0; k < l; k++ ) {
                    372:                mask = nd_blockmask->mask[k];
                    373:                if ( current_dl_weight_vector )
                    374:                        for ( i = 0, t = 0; i < nd_nvar; i++ ) {
                    375:                                u = GET_EXP_MASK(d,i,mask);
                    376:                                t += MUL_WEIGHT(u,i);
                    377:                        }
                    378:                else
                    379:                        for ( t = 0, i = nd_exporigin; i < nd_wpd; i++ ) {
                    380:                                u = d[i]&mask[i];
                    381:                                for ( j = 0; j < nd_epw; j++, u>>=nd_bpe )
                    382:                                        t += (u&nd_mask0);
                    383:                        }
                    384:                d[k+1] = t;
                    385:        }
1.43      noro      386: }
                    387:
1.61      noro      388: int ndl_lex_compare(UINT *d1,UINT *d2)
1.1       noro      389: {
                    390:        int i;
                    391:
1.41      noro      392:        d1 += nd_exporigin;
                    393:        d2 += nd_exporigin;
                    394:        for ( i = nd_exporigin; i < nd_wpd; i++, d1++, d2++ )
1.1       noro      395:                if ( *d1 > *d2 )
1.32      noro      396:                        return nd_isrlex ? -1 : 1;
1.1       noro      397:                else if ( *d1 < *d2 )
1.32      noro      398:                        return nd_isrlex ? 1 : -1;
1.1       noro      399:        return 0;
                    400: }
                    401:
1.61      noro      402: int ndl_block_compare(UINT *d1,UINT *d2)
1.43      noro      403: {
                    404:        int i,l,j,ord_o,ord_l;
                    405:        struct order_pair *op;
1.61      noro      406:        UINT t1,t2,m;
                    407:        UINT *mask;
1.43      noro      408:
                    409:        l = nd_blockmask->n;
                    410:        op = nd_blockmask->order_pair;
                    411:        for ( j = 0; j < l; j++ ) {
                    412:                mask = nd_blockmask->mask[j];
                    413:                ord_o = op[j].order;
                    414:                if ( ord_o < 2 )
1.44      noro      415:                        if ( (t1=d1[j+1]) > (t2=d2[j+1]) ) return 1;
                    416:                        else if ( t1 < t2 ) return -1;
1.43      noro      417:                for ( i = nd_exporigin; i < nd_wpd; i++ ) {
1.44      noro      418:                        m = mask[i];
                    419:                        t1 = d1[i]&m;
                    420:                        t2 = d2[i]&m;
1.43      noro      421:                        if ( t1 > t2 )
                    422:                                return !ord_o ? -1 : 1;
                    423:                        else if ( t1 < t2 )
                    424:                                return !ord_o ? 1 : -1;
                    425:                }
                    426:        }
                    427:        return 0;
                    428: }
                    429:
1.96      noro      430: int ndl_matrix_compare(UINT *d1,UINT *d2)
                    431: {
                    432:        int i,j,s;
                    433:        int *v;
                    434:
                    435:        for ( j = 0; j < nd_nvar; j++ )
                    436:                nd_work_vector[j] = GET_EXP(d1,j)-GET_EXP(d2,j);
                    437:        for ( i = 0; i < nd_matrix_len; i++ ) {
                    438:                v = nd_matrix[i];
                    439:                for ( j = 0, s = 0; j < nd_nvar; j++ )
                    440:                        s += v[j]*nd_work_vector[j];
                    441:                if ( s > 0 ) return 1;
                    442:                else if ( s < 0 ) return -1;
                    443:        }
                    444:        return 0;
                    445: }
                    446:
1.97      noro      447: int ndl_composite_compare(UINT *d1,UINT *d2)
                    448: {
                    449:        int i,j,s,start,end,len,o;
                    450:        int *v;
                    451:        struct sparse_weight *sw;
                    452:
                    453:        for ( j = 0; j < nd_nvar; j++ )
                    454:                nd_work_vector[j] = GET_EXP(d1,j)-GET_EXP(d2,j);
                    455:        for ( i = 0; i < nd_worb_len; i++ ) {
                    456:                len = nd_worb[i].length;
                    457:                switch ( nd_worb[i].type ) {
                    458:                        case IS_DENSE_WEIGHT:
                    459:                                v = nd_worb[i].body.dense_weight;
                    460:                                for ( j = 0, s = 0; j < len; j++ )
                    461:                                        s += v[j]*nd_work_vector[j];
                    462:                                if ( s > 0 ) return 1;
                    463:                                else if ( s < 0 ) return -1;
                    464:                                break;
                    465:                        case IS_SPARSE_WEIGHT:
                    466:                                sw = nd_worb[i].body.sparse_weight;
                    467:                                for ( j = 0, s = 0; j < len; j++ )
                    468:                                        s += sw[j].value*nd_work_vector[sw[j].pos];
                    469:                                if ( s > 0 ) return 1;
                    470:                                else if ( s < 0 ) return -1;
                    471:                                break;
                    472:                        case IS_BLOCK:
                    473:                                o = nd_worb[i].body.block.order;
                    474:                                start = nd_worb[i].body.block.start;
                    475:                                switch ( o ) {
                    476:                                        case 0:
                    477:                                                end = start+len;
                    478:                                                for ( j = start, s = 0; j < end; j++ )
                    479:                                                        s += MUL_WEIGHT(nd_work_vector[j],j);
                    480:                                                if ( s > 0 ) return 1;
                    481:                                                else if ( s < 0 ) return -1;
                    482:                                                for ( j = end-1; j >= start; j-- )
                    483:                                                        if ( nd_work_vector[j] < 0 ) return 1;
                    484:                                                        else if ( nd_work_vector[j] > 0 ) return -1;
                    485:                                                break;
                    486:                                        case 1:
                    487:                                                end = start+len;
                    488:                                                for ( j = start, s = 0; j < end; j++ )
                    489:                                                        s += MUL_WEIGHT(nd_work_vector[j],j);
                    490:                                                if ( s > 0 ) return 1;
                    491:                                                else if ( s < 0 ) return -1;
                    492:                                                for ( j = start; j < end; j++ )
                    493:                                                        if ( nd_work_vector[j] > 0 ) return 1;
                    494:                                                        else if ( nd_work_vector[j] < 0 ) return -1;
                    495:                                                break;
                    496:                                        case 2:
                    497:                                                for ( j = start; j < end; j++ )
                    498:                                                        if ( nd_work_vector[j] > 0 ) return 1;
                    499:                                                        else if ( nd_work_vector[j] < 0 ) return -1;
                    500:                                                break;
                    501:                                }
                    502:                                break;
                    503:                }
                    504:        }
                    505:        return 0;
                    506: }
                    507:
1.58      noro      508: /* TDH -> WW -> TD-> RL */
                    509:
1.61      noro      510: int ndl_ww_lex_compare(UINT *d1,UINT *d2)
1.58      noro      511: {
                    512:        int i,m,e1,e2;
                    513:
                    514:        if ( TD(d1) > TD(d2) ) return 1;
                    515:        else if ( TD(d1) < TD(d2) ) return -1;
                    516:        m = nd_nvar>>1;
                    517:        for ( i = 0, e1 = e2 = 0; i < m; i++ ) {
                    518:                e1 += current_weyl_weight_vector[i]*(GET_EXP(d1,m+i)-GET_EXP(d1,i));
                    519:                e2 += current_weyl_weight_vector[i]*(GET_EXP(d2,m+i)-GET_EXP(d2,i));
                    520:        }
                    521:        if ( e1 > e2 ) return 1;
                    522:        else if ( e1 < e2 ) return -1;
                    523:        return ndl_lex_compare(d1,d2);
                    524: }
                    525:
1.61      noro      526: INLINE int ndl_equal(UINT *d1,UINT *d2)
1.1       noro      527: {
                    528:        int i;
                    529:
1.81      noro      530:        switch ( nd_wpd ) {
                    531:                case 2:
                    532:                        if ( TD(d2) != TD(d1) ) return 0;
                    533:                        if ( d2[1] != d1[1] ) return 0;
                    534:                        return 1;
                    535:                        break;
                    536:                case 3:
                    537:                        if ( TD(d2) != TD(d1) ) return 0;
                    538:                        if ( d2[1] != d1[1] ) return 0;
                    539:                        if ( d2[2] != d1[2] ) return 0;
                    540:                        return 1;
                    541:                        break;
                    542:                default:
                    543:                        for ( i = 0; i < nd_wpd; i++ )
                    544:                                if ( *d1++ != *d2++ ) return 0;
                    545:                        return 1;
                    546:                        break;
                    547:        }
1.1       noro      548: }
                    549:
1.61      noro      550: INLINE void ndl_copy(UINT *d1,UINT *d2)
1.6       noro      551: {
                    552:        int i;
                    553:
                    554:        switch ( nd_wpd ) {
1.41      noro      555:                case 2:
1.34      noro      556:                        TD(d2) = TD(d1);
                    557:                        d2[1] = d1[1];
1.6       noro      558:                        break;
1.41      noro      559:                case 3:
1.34      noro      560:                        TD(d2) = TD(d1);
1.6       noro      561:                        d2[1] = d1[1];
1.34      noro      562:                        d2[2] = d1[2];
1.6       noro      563:                        break;
                    564:                default:
1.41      noro      565:                        for ( i = 0; i < nd_wpd; i++ )
1.6       noro      566:                                d2[i] = d1[i];
                    567:                        break;
                    568:        }
                    569: }
                    570:
1.61      noro      571: INLINE void ndl_zero(UINT *d)
                    572: {
                    573:        int i;
                    574:        for ( i = 0; i < nd_wpd; i++ ) d[i] = 0;
                    575: }
                    576:
                    577: INLINE void ndl_add(UINT *d1,UINT *d2,UINT *d)
1.1       noro      578: {
                    579:        int i;
                    580:
1.43      noro      581: #if 1
1.6       noro      582:        switch ( nd_wpd ) {
1.41      noro      583:                case 2:
                    584:                        TD(d) = TD(d1)+TD(d2);
1.34      noro      585:                        d[1] = d1[1]+d2[1];
1.6       noro      586:                        break;
1.41      noro      587:                case 3:
                    588:                        TD(d) = TD(d1)+TD(d2);
1.6       noro      589:                        d[1] = d1[1]+d2[1];
1.34      noro      590:                        d[2] = d1[2]+d2[2];
1.6       noro      591:                        break;
                    592:                default:
1.43      noro      593:                        for ( i = 0; i < nd_wpd; i++ ) d[i] = d1[i]+d2[i];
1.6       noro      594:                        break;
                    595:        }
1.43      noro      596: #else
                    597:        for ( i = 0; i < nd_wpd; i++ ) d[i] = d1[i]+d2[i];
                    598: #endif
1.6       noro      599: }
                    600:
1.55      noro      601: /* d1 += d2 */
1.61      noro      602: INLINE void ndl_addto(UINT *d1,UINT *d2)
1.55      noro      603: {
                    604:        int i;
                    605:
                    606: #if 1
                    607:        switch ( nd_wpd ) {
                    608:                case 2:
                    609:                        TD(d1) += TD(d2);
                    610:                        d1[1] += d2[1];
                    611:                        break;
                    612:                case 3:
                    613:                        TD(d1) += TD(d2);
                    614:                        d1[1] += d2[1];
                    615:                        d1[2] += d2[2];
                    616:                        break;
                    617:                default:
                    618:                        for ( i = 0; i < nd_wpd; i++ ) d1[i] += d2[i];
                    619:                        break;
                    620:        }
                    621: #else
                    622:        for ( i = 0; i < nd_wpd; i++ ) d1[i] += d2[i];
                    623: #endif
                    624: }
                    625:
1.61      noro      626: INLINE void ndl_sub(UINT *d1,UINT *d2,UINT *d)
1.6       noro      627: {
                    628:        int i;
                    629:
1.43      noro      630:        for ( i = 0; i < nd_wpd; i++ ) d[i] = d1[i]-d2[i];
1.1       noro      631: }
                    632:
1.61      noro      633: int ndl_disjoint(UINT *d1,UINT *d2)
1.1       noro      634: {
1.61      noro      635:        UINT t1,t2,u,u1,u2;
1.1       noro      636:        int i,j;
                    637:
1.65      noro      638: #if USE_UNROLL
1.1       noro      639:        switch ( nd_bpe ) {
1.62      noro      640:                case 3:
                    641:                        for ( i = nd_exporigin; i < nd_wpd; i++ ) {
                    642:                                u1 = d1[i]; u2 = d2[i];
                    643:                                t1 = u1&0x38000000; t2 = u2&0x38000000; if ( t1&&t2 ) return 0;
                    644:                                t1 = u1& 0x7000000; t2 = u2& 0x7000000; if ( t1&&t2 ) return 0;
                    645:                                t1 = u1&  0xe00000; t2 = u2&  0xe00000; if ( t1&&t2 ) return 0;
                    646:                                t1 = u1&  0x1c0000; t2 = u2&  0x1c0000; if ( t1&&t2 ) return 0;
                    647:                                t1 = u1&   0x38000; t2 = u2&   0x38000; if ( t1&&t2 ) return 0;
                    648:                                t1 = u1&    0x7000; t2 = u2&    0x7000; if ( t1&&t2 ) return 0;
                    649:                                t1 = u1&     0xe00; t2 = u2&     0xe00; if ( t1&&t2 ) return 0;
                    650:                                t1 = u1&     0x1c0; t2 = u2&     0x1c0; if ( t1&&t2 ) return 0;
                    651:                                t1 = u1&      0x38; t2 = u2&      0x38; if ( t1&&t2 ) return 0;
                    652:                                t1 = u1&       0x7; t2 = u2&       0x7; if ( t1&&t2 ) return 0;
                    653:                        }
                    654:                        return 1;
                    655:                        break;
1.1       noro      656:                case 4:
1.41      noro      657:                        for ( i = nd_exporigin; i < nd_wpd; i++ ) {
1.1       noro      658:                                u1 = d1[i]; u2 = d2[i];
                    659:                                t1 = u1&0xf0000000; t2 = u2&0xf0000000; if ( t1&&t2 ) return 0;
1.62      noro      660:                                t1 = u1& 0xf000000; t2 = u2& 0xf000000; if ( t1&&t2 ) return 0;
                    661:                                t1 = u1&  0xf00000; t2 = u2&  0xf00000; if ( t1&&t2 ) return 0;
                    662:                                t1 = u1&   0xf0000; t2 = u2&   0xf0000; if ( t1&&t2 ) return 0;
                    663:                                t1 = u1&    0xf000; t2 = u2&    0xf000; if ( t1&&t2 ) return 0;
                    664:                                t1 = u1&     0xf00; t2 = u2&     0xf00; if ( t1&&t2 ) return 0;
                    665:                                t1 = u1&      0xf0; t2 = u2&      0xf0; if ( t1&&t2 ) return 0;
                    666:                                t1 = u1&       0xf; t2 = u2&       0xf; if ( t1&&t2 ) return 0;
1.1       noro      667:                        }
                    668:                        return 1;
                    669:                        break;
                    670:                case 6:
1.41      noro      671:                        for ( i = nd_exporigin; i < nd_wpd; i++ ) {
1.1       noro      672:                                u1 = d1[i]; u2 = d2[i];
                    673:                                t1 = u1&0x3f000000; t2 = u2&0x3f000000; if ( t1&&t2 ) return 0;
1.62      noro      674:                                t1 = u1&  0xfc0000; t2 = u2&  0xfc0000; if ( t1&&t2 ) return 0;
                    675:                                t1 = u1&   0x3f000; t2 = u2&   0x3f000; if ( t1&&t2 ) return 0;
                    676:                                t1 = u1&     0xfc0; t2 = u2&     0xfc0; if ( t1&&t2 ) return 0;
                    677:                                t1 = u1&      0x3f; t2 = u2&      0x3f; if ( t1&&t2 ) return 0;
1.1       noro      678:                        }
                    679:                        return 1;
                    680:                        break;
                    681:                case 8:
1.41      noro      682:                        for ( i = nd_exporigin; i < nd_wpd; i++ ) {
1.1       noro      683:                                u1 = d1[i]; u2 = d2[i];
                    684:                                t1 = u1&0xff000000; t2 = u2&0xff000000; if ( t1&&t2 ) return 0;
1.62      noro      685:                                t1 = u1&  0xff0000; t2 = u2&  0xff0000; if ( t1&&t2 ) return 0;
                    686:                                t1 = u1&    0xff00; t2 = u2&    0xff00; if ( t1&&t2 ) return 0;
                    687:                                t1 = u1&      0xff; t2 = u2&      0xff; if ( t1&&t2 ) return 0;
1.1       noro      688:                        }
                    689:                        return 1;
                    690:                        break;
                    691:                case 16:
1.41      noro      692:                        for ( i = nd_exporigin; i < nd_wpd; i++ ) {
1.1       noro      693:                                u1 = d1[i]; u2 = d2[i];
                    694:                                t1 = u1&0xffff0000; t2 = u2&0xffff0000; if ( t1&&t2 ) return 0;
1.62      noro      695:                                t1 = u1&    0xffff; t2 = u2&    0xffff; if ( t1&&t2 ) return 0;
1.1       noro      696:                        }
                    697:                        return 1;
                    698:                        break;
                    699:                case 32:
1.41      noro      700:                        for ( i = nd_exporigin; i < nd_wpd; i++ )
1.1       noro      701:                                if ( d1[i] && d2[i] ) return 0;
                    702:                        return 1;
                    703:                        break;
                    704:                default:
1.41      noro      705:                        for ( i = nd_exporigin; i < nd_wpd; i++ ) {
1.1       noro      706:                                u1 = d1[i]; u2 = d2[i];
                    707:                                for ( j = 0; j < nd_epw; j++ ) {
                    708:                                        if ( (u1&nd_mask0) && (u2&nd_mask0) ) return 0;
                    709:                                        u1 >>= nd_bpe; u2 >>= nd_bpe;
                    710:                                }
                    711:                        }
                    712:                        return 1;
                    713:                        break;
                    714:        }
1.65      noro      715: #else
                    716:        for ( i = nd_exporigin; i < nd_wpd; i++ ) {
                    717:                u1 = d1[i]; u2 = d2[i];
                    718:                for ( j = 0; j < nd_epw; j++ ) {
                    719:                        if ( (u1&nd_mask0) && (u2&nd_mask0) ) return 0;
                    720:                        u1 >>= nd_bpe; u2 >>= nd_bpe;
                    721:                }
                    722:        }
                    723:        return 1;
                    724: #endif
1.1       noro      725: }
                    726:
1.61      noro      727: int ndl_check_bound2(int index,UINT *d2)
1.1       noro      728: {
1.61      noro      729:        UINT u2;
                    730:        UINT *d1;
1.5       noro      731:        int i,j,ind,k;
1.1       noro      732:
1.5       noro      733:        d1 = nd_bound[index];
                    734:        ind = 0;
1.65      noro      735: #if USE_UNROLL
1.5       noro      736:        switch ( nd_bpe ) {
1.62      noro      737:                case 3:
                    738:                        for ( i = nd_exporigin; i < nd_wpd; i++ ) {
                    739:                                u2 = d2[i];
                    740:                                if ( d1[ind++]+((u2>>27)&0x7) >= 0x8 ) return 1;
                    741:                                if ( d1[ind++]+((u2>>24)&0x7) >= 0x8 ) return 1;
                    742:                                if ( d1[ind++]+((u2>>21)&0x7) >= 0x8 ) return 1;
                    743:                                if ( d1[ind++]+((u2>>18)&0x7) >= 0x8 ) return 1;
                    744:                                if ( d1[ind++]+((u2>>15)&0x7) >= 0x8 ) return 1;
                    745:                                if ( d1[ind++]+((u2>>12)&0x7) >= 0x8 ) return 1;
                    746:                                if ( d1[ind++]+((u2>>9)&0x7) >= 0x8 ) return 1;
                    747:                                if ( d1[ind++]+((u2>>6)&0x7) >= 0x8 ) return 1;
                    748:                                if ( d1[ind++]+((u2>>3)&0x7) >= 0x8 ) return 1;
                    749:                                if ( d1[ind++]+(u2&0x7) >= 0x8 ) return 1;
                    750:                        }
                    751:                        return 0;
                    752:                        break;
1.5       noro      753:                case 4:
1.41      noro      754:                        for ( i = nd_exporigin; i < nd_wpd; i++ ) {
1.5       noro      755:                                u2 = d2[i];
                    756:                                if ( d1[ind++]+((u2>>28)&0xf) >= 0x10 ) return 1;
                    757:                                if ( d1[ind++]+((u2>>24)&0xf) >= 0x10 ) return 1;
                    758:                                if ( d1[ind++]+((u2>>20)&0xf) >= 0x10 ) return 1;
                    759:                                if ( d1[ind++]+((u2>>16)&0xf) >= 0x10 ) return 1;
                    760:                                if ( d1[ind++]+((u2>>12)&0xf) >= 0x10 ) return 1;
                    761:                                if ( d1[ind++]+((u2>>8)&0xf) >= 0x10 ) return 1;
                    762:                                if ( d1[ind++]+((u2>>4)&0xf) >= 0x10 ) return 1;
                    763:                                if ( d1[ind++]+(u2&0xf) >= 0x10 ) return 1;
                    764:                        }
                    765:                        return 0;
                    766:                        break;
                    767:                case 6:
1.41      noro      768:                        for ( i = nd_exporigin; i < nd_wpd; i++ ) {
1.5       noro      769:                                u2 = d2[i];
                    770:                                if ( d1[ind++]+((u2>>24)&0x3f) >= 0x40 ) return 1;
                    771:                                if ( d1[ind++]+((u2>>18)&0x3f) >= 0x40 ) return 1;
                    772:                                if ( d1[ind++]+((u2>>12)&0x3f) >= 0x40 ) return 1;
                    773:                                if ( d1[ind++]+((u2>>6)&0x3f) >= 0x40 ) return 1;
                    774:                                if ( d1[ind++]+(u2&0x3f) >= 0x40 ) return 1;
                    775:                        }
                    776:                        return 0;
                    777:                        break;
                    778:                case 8:
1.41      noro      779:                        for ( i = nd_exporigin; i < nd_wpd; i++ ) {
1.5       noro      780:                                u2 = d2[i];
                    781:                                if ( d1[ind++]+((u2>>24)&0xff) >= 0x100 ) return 1;
                    782:                                if ( d1[ind++]+((u2>>16)&0xff) >= 0x100 ) return 1;
                    783:                                if ( d1[ind++]+((u2>>8)&0xff) >= 0x100 ) return 1;
                    784:                                if ( d1[ind++]+(u2&0xff) >= 0x100 ) return 1;
                    785:                        }
                    786:                        return 0;
                    787:                        break;
                    788:                case 16:
1.41      noro      789:                        for ( i = nd_exporigin; i < nd_wpd; i++ ) {
1.5       noro      790:                                u2 = d2[i];
                    791:                                if ( d1[ind++]+((u2>>16)&0xffff) > 0x10000 ) return 1;
                    792:                                if ( d1[ind++]+(u2&0xffff) > 0x10000 ) return 1;
                    793:                        }
                    794:                        return 0;
                    795:                        break;
                    796:                case 32:
1.41      noro      797:                        for ( i = nd_exporigin; i < nd_wpd; i++ )
1.5       noro      798:                                if ( d1[i]+d2[i]<d1[i] ) return 1;
                    799:                        return 0;
                    800:                        break;
                    801:                default:
1.41      noro      802:                        for ( i = nd_exporigin; i < nd_wpd; i++ ) {
1.5       noro      803:                                u2 = d2[i];
                    804:                                k = (nd_epw-1)*nd_bpe;
                    805:                                for ( j = 0; j < nd_epw; j++, k -= nd_bpe )
                    806:                                        if ( d1[ind++]+((u2>>k)&nd_mask0) > nd_mask0 ) return 1;
                    807:                        }
                    808:                        return 0;
                    809:                        break;
                    810:        }
1.65      noro      811: #else
                    812:        for ( i = nd_exporigin; i < nd_wpd; i++ ) {
                    813:                u2 = d2[i];
                    814:                k = (nd_epw-1)*nd_bpe;
                    815:                for ( j = 0; j < nd_epw; j++, k -= nd_bpe )
                    816:                        if ( d1[ind++]+((u2>>k)&nd_mask0) > nd_mask0 ) return 1;
                    817:        }
                    818:        return 0;
                    819: #endif
1.1       noro      820: }
                    821:
1.61      noro      822: INLINE int ndl_hash_value(UINT *d)
1.1       noro      823: {
                    824:        int i;
                    825:        int r;
                    826:
1.34      noro      827:        r = 0;
1.41      noro      828:        for ( i = 0; i < nd_wpd; i++ )
1.1       noro      829:                r = ((r<<16)+d[i])%REDTAB_LEN;
                    830:        return r;
                    831: }
                    832:
1.63      noro      833: INLINE int ndl_find_reducer(UINT *dg)
1.1       noro      834: {
1.13      noro      835:        RHist r;
1.6       noro      836:        int d,k,i;
1.1       noro      837:
1.63      noro      838:        d = ndl_hash_value(dg);
1.13      noro      839:        for ( r = nd_red[d], k = 0; r; r = NEXT(r), k++ ) {
1.34      noro      840:                if ( ndl_equal(dg,DL(r)) ) {
1.1       noro      841:                        if ( k > 0 ) nd_notfirst++;
                    842:                        nd_found++;
1.13      noro      843:                        return r->index;
1.1       noro      844:                }
                    845:        }
1.13      noro      846:        if ( Reverse )
                    847:                for ( i = nd_psn-1; i >= 0; i-- ) {
                    848:                        r = nd_psh[i];
1.34      noro      849:                        if ( ndl_reducible(dg,DL(r)) ) {
1.13      noro      850:                                nd_create++;
1.34      noro      851:                                nd_append_red(dg,i);
1.13      noro      852:                                return i;
                    853:                        }
                    854:                }
                    855:        else
                    856:                for ( i = 0; i < nd_psn; i++ ) {
                    857:                        r = nd_psh[i];
1.34      noro      858:                        if ( ndl_reducible(dg,DL(r)) ) {
1.13      noro      859:                                nd_create++;
1.34      noro      860:                                nd_append_red(dg,i);
1.13      noro      861:                                return i;
                    862:                        }
1.1       noro      863:                }
1.6       noro      864:        return -1;
1.1       noro      865: }
                    866:
1.63      noro      867: ND nd_merge(ND p1,ND p2)
                    868: {
                    869:        int n,c;
                    870:        int t,can,td1,td2;
                    871:        ND r;
                    872:        NM m1,m2,mr0,mr,s;
                    873:
                    874:        if ( !p1 ) return p2;
                    875:        else if ( !p2 ) return p1;
                    876:        else {
                    877:                can = 0;
                    878:                for ( n = NV(p1), m1 = BDY(p1), m2 = BDY(p2), mr0 = 0; m1 && m2; ) {
                    879:                        c = DL_COMPARE(DL(m1),DL(m2));
                    880:                        switch ( c ) {
                    881:                                case 0:
                    882:                                        s = m1; m1 = NEXT(m1);
                    883:                                        can++; NEXTNM2(mr0,mr,s);
                    884:                                        s = m2; m2 = NEXT(m2); FREENM(s);
                    885:                                        break;
                    886:                                case 1:
                    887:                                        s = m1; m1 = NEXT(m1); NEXTNM2(mr0,mr,s);
                    888:                                        break;
                    889:                                case -1:
                    890:                                        s = m2; m2 = NEXT(m2); NEXTNM2(mr0,mr,s);
                    891:                                        break;
                    892:                        }
                    893:                }
                    894:                if ( !mr0 )
                    895:                        if ( m1 ) mr0 = m1;
                    896:                        else if ( m2 ) mr0 = m2;
                    897:                        else return 0;
                    898:                else if ( m1 ) NEXT(mr) = m1;
                    899:                else if ( m2 ) NEXT(mr) = m2;
                    900:                else NEXT(mr) = 0;
                    901:                BDY(p1) = mr0;
                    902:                SG(p1) = MAX(SG(p1),SG(p2));
                    903:                LEN(p1) = LEN(p1)+LEN(p2)-can;
                    904:                FREEND(p2);
                    905:                return p1;
                    906:        }
                    907: }
                    908:
1.31      noro      909: ND nd_add(int mod,ND p1,ND p2)
1.1       noro      910: {
                    911:        int n,c;
1.34      noro      912:        int t,can,td1,td2;
1.1       noro      913:        ND r;
                    914:        NM m1,m2,mr0,mr,s;
                    915:
1.34      noro      916:        if ( !p1 ) return p2;
                    917:        else if ( !p2 ) return p1;
1.71      noro      918:        else if ( mod == -1 ) return nd_add_sf(p1,p2);
1.112   ! noro      919:        else if ( !mod ) return nd_add_z(p1,p2);
1.1       noro      920:        else {
1.30      noro      921:                can = 0;
1.1       noro      922:                for ( n = NV(p1), m1 = BDY(p1), m2 = BDY(p2), mr0 = 0; m1 && m2; ) {
1.34      noro      923:                        c = DL_COMPARE(DL(m1),DL(m2));
1.1       noro      924:                        switch ( c ) {
                    925:                                case 0:
1.19      noro      926:                                        t = ((CM(m1))+(CM(m2))) - mod;
1.34      noro      927:                                        if ( t < 0 ) t += mod;
1.1       noro      928:                                        s = m1; m1 = NEXT(m1);
                    929:                                        if ( t ) {
1.34      noro      930:                                                can++; NEXTNM2(mr0,mr,s); CM(mr) = (t);
1.17      noro      931:                                        } else {
1.34      noro      932:                                                can += 2; FREENM(s);
1.17      noro      933:                                        }
                    934:                                        s = m2; m2 = NEXT(m2); FREENM(s);
1.95      noro      935:                                        break;
                    936:                                case 1:
                    937:                                        s = m1; m1 = NEXT(m1); NEXTNM2(mr0,mr,s);
                    938:                                        break;
                    939:                                case -1:
                    940:                                        s = m2; m2 = NEXT(m2); NEXTNM2(mr0,mr,s);
                    941:                                        break;
                    942:                        }
                    943:                }
                    944:                if ( !mr0 )
                    945:                        if ( m1 ) mr0 = m1;
                    946:                        else if ( m2 ) mr0 = m2;
                    947:                        else return 0;
                    948:                else if ( m1 ) NEXT(mr) = m1;
                    949:                else if ( m2 ) NEXT(mr) = m2;
                    950:                else NEXT(mr) = 0;
                    951:                BDY(p1) = mr0;
                    952:                SG(p1) = MAX(SG(p1),SG(p2));
                    953:                LEN(p1) = LEN(p1)+LEN(p2)-can;
                    954:                FREEND(p2);
                    955:                return p1;
                    956:        }
                    957: }
                    958:
                    959: /* XXX on opteron, the inlined manipulation of destructive additon of
                    960:  * two NM seems to make gcc optimizer get confused, so the part is
                    961:  * done in a function.
                    962:  */
                    963:
1.112   ! noro      964: int nm_destructive_add_z(NM *m1,NM *m2,NM *mr0,NM *mr)
1.95      noro      965: {
                    966:        NM s;
1.112   ! noro      967:        Z t;
1.95      noro      968:        int can;
                    969:
1.112   ! noro      970:        t = addz(CZ(*m1),CZ(*m2));
1.95      noro      971:        s = *m1; *m1 = NEXT(*m1);
                    972:        if ( t ) {
1.112   ! noro      973:                can = 1; NEXTNM2(*mr0,*mr,s); CZ(*mr) = (t);
1.95      noro      974:        } else {
                    975:                can = 2; FREENM(s);
                    976:        }
                    977:        s = *m2; *m2 = NEXT(*m2); FREENM(s);
                    978:        return can;
                    979: }
                    980:
1.112   ! noro      981: ND nd_add_z(ND p1,ND p2)
1.95      noro      982: {
                    983:        int n,c,can;
                    984:        ND r;
                    985:        NM m1,m2,mr0,mr,s;
1.112   ! noro      986:        Z t;
1.95      noro      987:
                    988:        if ( !p1 ) return p2;
                    989:        else if ( !p2 ) return p1;
                    990:        else {
                    991:                can = 0;
                    992:                for ( n = NV(p1), m1 = BDY(p1), m2 = BDY(p2), mr0 = 0; m1 && m2; ) {
                    993:                        c = DL_COMPARE(DL(m1),DL(m2));
                    994:                        switch ( c ) {
                    995:                                case 0:
                    996: #if defined(__x86_64__)
1.112   ! noro      997:                                        can += nm_destructive_add_z(&m1,&m2,&mr0,&mr);
1.95      noro      998: #else
1.112   ! noro      999:                                        t = addz(CZ(m1),CZ(m2));
1.95      noro     1000:                                        s = m1; m1 = NEXT(m1);
                   1001:                                        if ( t ) {
1.112   ! noro     1002:                                                can++; NEXTNM2(mr0,mr,s); CZ(mr) = (t);
1.95      noro     1003:                                        } else {
                   1004:                                                can += 2; FREENM(s);
                   1005:                                        }
                   1006:                                        s = m2; m2 = NEXT(m2); FREENM(s);
                   1007: #endif
1.17      noro     1008:                                        break;
                   1009:                                case 1:
                   1010:                                        s = m1; m1 = NEXT(m1); NEXTNM2(mr0,mr,s);
                   1011:                                        break;
                   1012:                                case -1:
                   1013:                                        s = m2; m2 = NEXT(m2); NEXTNM2(mr0,mr,s);
                   1014:                                        break;
                   1015:                        }
                   1016:                }
                   1017:                if ( !mr0 )
1.34      noro     1018:                        if ( m1 ) mr0 = m1;
                   1019:                        else if ( m2 ) mr0 = m2;
                   1020:                        else return 0;
                   1021:                else if ( m1 ) NEXT(mr) = m1;
                   1022:                else if ( m2 ) NEXT(mr) = m2;
                   1023:                else NEXT(mr) = 0;
1.17      noro     1024:                BDY(p1) = mr0;
                   1025:                SG(p1) = MAX(SG(p1),SG(p2));
1.31      noro     1026:                LEN(p1) = LEN(p1)+LEN(p2)-can;
1.17      noro     1027:                FREEND(p2);
                   1028:                return p1;
                   1029:        }
                   1030: }
                   1031:
1.71      noro     1032: ND nd_add_sf(ND p1,ND p2)
                   1033: {
                   1034:        int n,c,can;
                   1035:        ND r;
                   1036:        NM m1,m2,mr0,mr,s;
                   1037:        int t;
                   1038:
                   1039:        if ( !p1 ) return p2;
                   1040:        else if ( !p2 ) return p1;
                   1041:        else {
                   1042:                can = 0;
                   1043:                for ( n = NV(p1), m1 = BDY(p1), m2 = BDY(p2), mr0 = 0; m1 && m2; ) {
                   1044:                        c = DL_COMPARE(DL(m1),DL(m2));
                   1045:                        switch ( c ) {
                   1046:                                case 0:
                   1047:                                        t = _addsf(CM(m1),CM(m2));
                   1048:                                        s = m1; m1 = NEXT(m1);
                   1049:                                        if ( t ) {
                   1050:                                                can++; NEXTNM2(mr0,mr,s); CM(mr) = (t);
                   1051:                                        } else {
                   1052:                                                can += 2; FREENM(s);
                   1053:                                        }
                   1054:                                        s = m2; m2 = NEXT(m2); FREENM(s);
                   1055:                                        break;
                   1056:                                case 1:
                   1057:                                        s = m1; m1 = NEXT(m1); NEXTNM2(mr0,mr,s);
                   1058:                                        break;
                   1059:                                case -1:
                   1060:                                        s = m2; m2 = NEXT(m2); NEXTNM2(mr0,mr,s);
                   1061:                                        break;
                   1062:                        }
                   1063:                }
                   1064:                if ( !mr0 )
                   1065:                        if ( m1 ) mr0 = m1;
                   1066:                        else if ( m2 ) mr0 = m2;
                   1067:                        else return 0;
                   1068:                else if ( m1 ) NEXT(mr) = m1;
                   1069:                else if ( m2 ) NEXT(mr) = m2;
                   1070:                else NEXT(mr) = 0;
                   1071:                BDY(p1) = mr0;
                   1072:                SG(p1) = MAX(SG(p1),SG(p2));
                   1073:                LEN(p1) = LEN(p1)+LEN(p2)-can;
                   1074:                FREEND(p2);
                   1075:                return p1;
                   1076:        }
                   1077: }
                   1078:
1.1       noro     1079: /* ret=1 : success, ret=0 : overflow */
1.112   ! noro     1080: int nd_nf(int mod,ND g,NDV *ps,int full,Q *dn,ND *rp)
1.1       noro     1081: {
1.11      noro     1082:        ND d;
1.1       noro     1083:        NM m,mrd,tail;
1.7       noro     1084:        NM mul;
1.10      noro     1085:        int n,sugar,psugar,sugar0,stat,index;
1.30      noro     1086:        int c,c1,c2,dummy;
1.17      noro     1087:        RHist h;
1.11      noro     1088:        NDV p,red;
1.112   ! noro     1089:        Z cg,cred,gcd;
        !          1090:        Q tq,qq;
1.21      noro     1091:        double hmag;
1.1       noro     1092:
1.112   ! noro     1093:        if ( dn && !mod )
        !          1094:                *dn = ONE;
1.1       noro     1095:        if ( !g ) {
                   1096:                *rp = 0;
                   1097:                return 1;
                   1098:        }
1.112   ! noro     1099:        if ( !mod ) hmag = ((double)z_mag(HCZ(g)))*nd_scale;
1.21      noro     1100:
1.14      noro     1101:        sugar0 = sugar = SG(g);
1.1       noro     1102:        n = NV(g);
1.61      noro     1103:        mul = (NM)ALLOCA(sizeof(struct oNM)+(nd_wpd-1)*sizeof(UINT));
1.1       noro     1104:        for ( d = 0; g; ) {
1.63      noro     1105:                index = ndl_find_reducer(HDL(g));
1.6       noro     1106:                if ( index >= 0 ) {
1.17      noro     1107:                        h = nd_psh[index];
                   1108:                        ndl_sub(HDL(g),DL(h),DL(mul));
1.14      noro     1109:                        if ( ndl_check_bound2(index,DL(mul)) ) {
1.6       noro     1110:                                nd_free(g); nd_free(d);
                   1111:                                return 0;
                   1112:                        }
1.77      noro     1113:                        if ( nd_demand )
                   1114:                                p = ndv_load(index);
                   1115:                        else
                   1116:                                p = ps[index];
1.71      noro     1117:                        if ( mod == -1 )
                   1118:                                CM(mul) = _mulsf(_invsf(HCM(p)),_chsgnsf(HCM(g)));
                   1119:                        else if ( mod ) {
1.19      noro     1120:                                c1 = invm(HCM(p),mod); c2 = mod-HCM(g);
                   1121:                                DMAR(c1,c2,0,mod,c); CM(mul) = c;
1.16      noro     1122:                        } else {
1.112   ! noro     1123:                                gcd = gcdz_cofactor(HCZ(g),HCZ(p),&cg,&cred);
        !          1124:                                CZ(mul) = chsgnz(cg);
        !          1125:                                nd_mul_c_z(d,cred);
        !          1126:                                nd_mul_c_z(g,cred);
1.69      noro     1127:                                if ( dn ) {
1.112   ! noro     1128:                                        mulq(*dn,ztoq(cred),&tq); *dn = tq;
1.69      noro     1129:                                }
1.16      noro     1130:                        }
1.55      noro     1131:                        g = nd_add(mod,g,ndv_mul_nm(mod,mul,p));
1.112   ! noro     1132:
1.34      noro     1133:                        sugar = MAX(sugar,SG(p)+TD(DL(mul)));
1.112   ! noro     1134:                        if ( !mod && hmag && g && ((double)(z_mag(HCZ(g))) > hmag) ) {
        !          1135:                                tq = ztoq(HCZ(g));
1.21      noro     1136:                                nd_removecont2(d,g);
1.69      noro     1137:                                if ( dn ) {
1.112   ! noro     1138:                                        divq(tq,ztoq(HCZ(g)),&qq); divq(*dn,qq,&tq); *dn = tq;
1.69      noro     1139:                                }
1.112   ! noro     1140:                                hmag = ((double)z_mag((P)HCZ(g)))*nd_scale;
1.21      noro     1141:                        }
1.1       noro     1142:                } else if ( !full ) {
                   1143:                        *rp = g;
                   1144:                        return 1;
                   1145:                } else {
                   1146:                        m = BDY(g);
                   1147:                        if ( NEXT(m) ) {
1.34      noro     1148:                                BDY(g) = NEXT(m); NEXT(m) = 0; LEN(g)--;
1.1       noro     1149:                        } else {
                   1150:                                FREEND(g); g = 0;
                   1151:                        }
                   1152:                        if ( d ) {
1.34      noro     1153:                                NEXT(tail)=m; tail=m; LEN(d)++;
1.1       noro     1154:                        } else {
1.34      noro     1155:                                MKND(n,m,1,d); tail = BDY(d);
1.1       noro     1156:                        }
                   1157:                }
                   1158:        }
1.34      noro     1159:        if ( d ) SG(d) = sugar;
1.1       noro     1160:        *rp = d;
                   1161:        return 1;
                   1162: }
1.28      noro     1163:
1.53      noro     1164: int nd_nf_pbucket(int mod,ND g,NDV *ps,int full,ND *rp)
1.25      noro     1165: {
                   1166:        int hindex,index;
                   1167:        NDV p;
                   1168:        ND u,d,red;
                   1169:        NODE l;
1.31      noro     1170:        NM mul,m,mrd,tail;
1.25      noro     1171:        int sugar,psugar,n,h_reducible;
                   1172:        PGeoBucket bucket;
                   1173:        int c,c1,c2;
1.112   ! noro     1174:        Z cg,cred,gcd,zzz;
1.25      noro     1175:        RHist h;
1.28      noro     1176:        double hmag,gmag;
1.77      noro     1177:        int count = 0;
                   1178:        int hcount = 0;
1.25      noro     1179:
                   1180:        if ( !g ) {
                   1181:                *rp = 0;
                   1182:                return 1;
                   1183:        }
                   1184:        sugar = SG(g);
                   1185:        n = NV(g);
1.112   ! noro     1186:        if ( !mod ) hmag = ((double)z_mag(HCZ(g)))*nd_scale;
1.25      noro     1187:        bucket = create_pbucket();
1.31      noro     1188:        add_pbucket(mod,bucket,g);
1.25      noro     1189:        d = 0;
1.61      noro     1190:        mul = (NM)ALLOCA(sizeof(struct oNM)+(nd_wpd-1)*sizeof(UINT));
1.25      noro     1191:        while ( 1 ) {
1.112   ! noro     1192:                hindex = mod?head_pbucket(mod,bucket):head_pbucket_z(bucket);
1.25      noro     1193:                if ( hindex < 0 ) {
1.77      noro     1194:                        if ( DP_Print > 3 ) printf("(%d %d)",count,hcount);
1.34      noro     1195:                        if ( d ) SG(d) = sugar;
1.25      noro     1196:                        *rp = d;
                   1197:                        return 1;
                   1198:                }
                   1199:                g = bucket->body[hindex];
1.63      noro     1200:                index = ndl_find_reducer(HDL(g));
1.25      noro     1201:                if ( index >= 0 ) {
1.77      noro     1202:                        count++;
                   1203:                        if ( !d ) hcount++;
1.25      noro     1204:                        h = nd_psh[index];
                   1205:                        ndl_sub(HDL(g),DL(h),DL(mul));
                   1206:                        if ( ndl_check_bound2(index,DL(mul)) ) {
1.26      noro     1207:                                nd_free(d);
1.25      noro     1208:                                free_pbucket(bucket);
                   1209:                                *rp = 0;
                   1210:                                return 0;
                   1211:                        }
1.53      noro     1212:                        p = ps[index];
1.71      noro     1213:                        if ( mod == -1 )
                   1214:                                CM(mul) = _mulsf(_invsf(HCM(p)),_chsgnsf(HCM(g)));
                   1215:                        else if ( mod ) {
1.25      noro     1216:                                c1 = invm(HCM(p),mod); c2 = mod-HCM(g);
                   1217:                                DMAR(c1,c2,0,mod,c); CM(mul) = c;
                   1218:                        } else {
1.112   ! noro     1219:                                gcd = gcdz_cofactor(HCZ(g),HCZ(p),&cg,&cred);
        !          1220:                                CZ(mul) = chsgnz(cg);
        !          1221:                                nd_mul_c_z(d,cred);
        !          1222:                                mulz_pbucket(bucket,cred);
1.26      noro     1223:                                g = bucket->body[hindex];
1.112   ! noro     1224:                                gmag = (double)z_mag(HCZ(g));
1.25      noro     1225:                        }
1.55      noro     1226:                        red = ndv_mul_nm(mod,mul,p);
1.25      noro     1227:                        bucket->body[hindex] = nd_remove_head(g);
                   1228:                        red = nd_remove_head(red);
1.31      noro     1229:                        add_pbucket(mod,bucket,red);
1.34      noro     1230:                        psugar = SG(p)+TD(DL(mul));
                   1231:                        sugar = MAX(sugar,psugar);
1.28      noro     1232:                        if ( !mod && hmag && (gmag > hmag) ) {
                   1233:                                g = normalize_pbucket(mod,bucket);
                   1234:                                if ( !g ) {
1.34      noro     1235:                                        if ( d ) SG(d) = sugar;
1.28      noro     1236:                                        *rp = d;
                   1237:                                        return 1;
                   1238:                                }
                   1239:                                nd_removecont2(d,g);
1.112   ! noro     1240:                                hmag = ((double)z_mag(HCZ(g)))*nd_scale;
1.31      noro     1241:                                add_pbucket(mod,bucket,g);
1.28      noro     1242:                        }
1.25      noro     1243:                } else if ( !full ) {
                   1244:                        g = normalize_pbucket(mod,bucket);
1.34      noro     1245:                        if ( g ) SG(g) = sugar;
1.25      noro     1246:                        *rp = g;
                   1247:                        return 1;
                   1248:                } else {
                   1249:                        m = BDY(g);
                   1250:                        if ( NEXT(m) ) {
1.34      noro     1251:                                BDY(g) = NEXT(m); NEXT(m) = 0; LEN(g)--;
1.25      noro     1252:                        } else {
                   1253:                                FREEND(g); g = 0;
                   1254:                        }
                   1255:                        bucket->body[hindex] = g;
                   1256:                        NEXT(m) = 0;
                   1257:                        if ( d ) {
1.34      noro     1258:                                NEXT(tail)=m; tail=m; LEN(d)++;
1.25      noro     1259:                        } else {
1.34      noro     1260:                                MKND(n,m,1,d); tail = BDY(d);
1.25      noro     1261:                        }
                   1262:                }
                   1263:        }
                   1264: }
1.27      noro     1265:
1.61      noro     1266: /* input : list of NDV, cand : list of NDV */
1.28      noro     1267:
1.61      noro     1268: int ndv_check_candidate(NODE input,int obpe,int oadv,EPOS oepos,NODE cand)
1.28      noro     1269: {
                   1270:        int n,i,stat;
                   1271:        ND nf,d;
1.61      noro     1272:        NDV r;
1.45      noro     1273:        NODE t,s;
                   1274:
1.61      noro     1275:        ndv_setup(0,0,cand);
1.31      noro     1276:        n = length(cand);
1.28      noro     1277:
                   1278:        /* membercheck : list is a subset of Id(cand) ? */
                   1279:        for ( t = input; t; t = NEXT(t) ) {
1.45      noro     1280: again:
1.61      noro     1281:                if ( nd_bpe > obpe )
                   1282:                        r = ndv_dup_realloc((NDV)BDY(t),obpe,oadv,oepos);
                   1283:                else
                   1284:                        r = (NDV)BDY(t);
                   1285:                d = ndvtond(0,r);
1.69      noro     1286:                stat = nd_nf(0,d,nd_ps,0,0,&nf);
1.45      noro     1287:                if ( !stat ) {
1.103     noro     1288:                        nd_reconstruct(0,0);
1.45      noro     1289:                        goto again;
                   1290:                } else if ( nf ) return 0;
1.71      noro     1291:                if ( DP_Print ) { printf("."); fflush(stdout); }
1.28      noro     1292:        }
1.71      noro     1293:        if ( DP_Print ) { printf("\n"); }
1.28      noro     1294:        /* gbcheck : cand is a GB of Id(cand) ? */
1.77      noro     1295:        if ( !nd_gb(0,0,1) ) return 0;
1.28      noro     1296:        /* XXX */
1.23      noro     1297:        return 1;
                   1298: }
1.1       noro     1299:
                   1300: ND nd_remove_head(ND p)
                   1301: {
                   1302:        NM m;
                   1303:
                   1304:        m = BDY(p);
                   1305:        if ( !NEXT(m) ) {
1.34      noro     1306:                FREEND(p); p = 0;
1.31      noro     1307:        } else {
1.34      noro     1308:                BDY(p) = NEXT(m); LEN(p)--;
1.31      noro     1309:        }
1.1       noro     1310:        FREENM(m);
                   1311:        return p;
                   1312: }
                   1313:
1.69      noro     1314: ND nd_separate_head(ND p,ND *head)
                   1315: {
                   1316:        NM m,m0;
                   1317:        ND r;
                   1318:
                   1319:        m = BDY(p);
                   1320:        if ( !NEXT(m) ) {
                   1321:                *head = p; p = 0;
                   1322:        } else {
                   1323:                m0 = m;
                   1324:                BDY(p) = NEXT(m); LEN(p)--;
                   1325:                NEXT(m0) = 0;
                   1326:                MKND(NV(p),m0,1,r);
                   1327:                *head = r;
                   1328:        }
                   1329:        return p;
                   1330: }
                   1331:
1.1       noro     1332: PGeoBucket create_pbucket()
                   1333: {
                   1334:     PGeoBucket g;
                   1335:
                   1336:        g = CALLOC(1,sizeof(struct oPGeoBucket));
                   1337:        g->m = -1;
                   1338:        return g;
                   1339: }
                   1340:
1.25      noro     1341: void free_pbucket(PGeoBucket b) {
                   1342:        int i;
                   1343:
1.26      noro     1344:        for ( i = 0; i <= b->m; i++ )
1.25      noro     1345:                if ( b->body[i] ) {
                   1346:                        nd_free(b->body[i]);
                   1347:                        b->body[i] = 0;
                   1348:                }
                   1349:        GC_free(b);
                   1350: }
                   1351:
1.63      noro     1352: void add_pbucket_symbolic(PGeoBucket g,ND d)
                   1353: {
                   1354:        int l,i,k,m;
                   1355:
                   1356:        if ( !d )
                   1357:                return;
                   1358:        l = LEN(d);
                   1359:        for ( k = 0, m = 1; l > m; k++, m <<= 1 );
                   1360:        /* 2^(k-1) < l <= 2^k (=m) */
                   1361:        d = nd_merge(g->body[k],d);
                   1362:        for ( ; d && LEN(d) > m; k++, m <<= 1 ) {
                   1363:                g->body[k] = 0;
                   1364:                d = nd_merge(g->body[k+1],d);
                   1365:        }
                   1366:        g->body[k] = d;
                   1367:        g->m = MAX(g->m,k);
                   1368: }
                   1369:
1.31      noro     1370: void add_pbucket(int mod,PGeoBucket g,ND d)
1.1       noro     1371: {
1.31      noro     1372:        int l,i,k,m;
1.1       noro     1373:
1.31      noro     1374:        if ( !d )
                   1375:                return;
                   1376:        l = LEN(d);
1.29      noro     1377:        for ( k = 0, m = 1; l > m; k++, m <<= 1 );
                   1378:        /* 2^(k-1) < l <= 2^k (=m) */
1.31      noro     1379:        d = nd_add(mod,g->body[k],d);
                   1380:        for ( ; d && LEN(d) > m; k++, m <<= 1 ) {
1.1       noro     1381:                g->body[k] = 0;
1.31      noro     1382:                d = nd_add(mod,g->body[k+1],d);
1.1       noro     1383:        }
                   1384:        g->body[k] = d;
                   1385:        g->m = MAX(g->m,k);
                   1386: }
                   1387:
1.112   ! noro     1388: void mulz_pbucket(PGeoBucket g,Z c)
1.26      noro     1389: {
                   1390:        int k;
                   1391:
                   1392:        for ( k = 0; k <= g->m; k++ )
1.112   ! noro     1393:                nd_mul_c_z(g->body[k],c);
1.26      noro     1394: }
                   1395:
1.63      noro     1396: NM remove_head_pbucket_symbolic(PGeoBucket g)
                   1397: {
                   1398:        int j,i,k,c;
                   1399:        NM head;
                   1400:
                   1401:        k = g->m;
                   1402:        j = -1;
                   1403:        for ( i = 0; i <= k; i++ ) {
                   1404:                if ( !g->body[i] ) continue;
                   1405:                if ( j < 0 ) j = i;
                   1406:                else {
                   1407:                        c = DL_COMPARE(HDL(g->body[i]),HDL(g->body[j]));
                   1408:                        if ( c > 0 )
                   1409:                                j = i;
                   1410:                        else if ( c == 0 )
                   1411:                                g->body[i] = nd_remove_head(g->body[i]);
                   1412:                }
                   1413:        }
                   1414:        if ( j < 0 ) return 0;
                   1415:        else {
                   1416:                head = BDY(g->body[j]);
                   1417:                if ( !NEXT(head) ) {
                   1418:                        FREEND(g->body[j]);
                   1419:                        g->body[j] = 0;
                   1420:                } else {
                   1421:                        BDY(g->body[j]) = NEXT(head);
                   1422:                        LEN(g->body[j])--;
                   1423:                }
                   1424:                return head;
                   1425:        }
                   1426: }
                   1427:
1.19      noro     1428: int head_pbucket(int mod,PGeoBucket g)
1.1       noro     1429: {
                   1430:        int j,i,c,k,nv,sum;
1.61      noro     1431:        UINT *di,*dj;
1.1       noro     1432:        ND gi,gj;
                   1433:
                   1434:        k = g->m;
                   1435:        while ( 1 ) {
                   1436:                j = -1;
                   1437:                for ( i = 0; i <= k; i++ ) {
                   1438:                        if ( !(gi = g->body[i]) )
                   1439:                                continue;
                   1440:                        if ( j < 0 ) {
                   1441:                                j = i;
                   1442:                                gj = g->body[j];
                   1443:                                dj = HDL(gj);
1.14      noro     1444:                                sum = HCM(gj);
1.1       noro     1445:                        } else {
1.34      noro     1446:                                c = DL_COMPARE(HDL(gi),dj);
1.1       noro     1447:                                if ( c > 0 ) {
1.34      noro     1448:                                        if ( sum ) HCM(gj) = sum;
                   1449:                                        else g->body[j] = nd_remove_head(gj);
1.1       noro     1450:                                        j = i;
                   1451:                                        gj = g->body[j];
                   1452:                                        dj = HDL(gj);
1.14      noro     1453:                                        sum = HCM(gj);
1.1       noro     1454:                                } else if ( c == 0 ) {
1.71      noro     1455:                                        if ( mod == -1 )
                   1456:                                                sum = _addsf(sum,HCM(gi));
                   1457:                                        else {
                   1458:                                                sum = sum+HCM(gi)-mod;
                   1459:                                                if ( sum < 0 ) sum += mod;
                   1460:                                        }
1.1       noro     1461:                                        g->body[i] = nd_remove_head(gi);
                   1462:                                }
                   1463:                        }
                   1464:                }
1.34      noro     1465:                if ( j < 0 ) return -1;
1.1       noro     1466:                else if ( sum ) {
1.14      noro     1467:                        HCM(gj) = sum;
1.26      noro     1468:                        return j;
1.31      noro     1469:                } else
1.26      noro     1470:                        g->body[j] = nd_remove_head(gj);
                   1471:        }
                   1472: }
                   1473:
1.112   ! noro     1474: int head_pbucket_z(PGeoBucket g)
1.26      noro     1475: {
                   1476:        int j,i,c,k,nv;
1.112   ! noro     1477:        Z sum,t;
1.26      noro     1478:        ND gi,gj;
                   1479:
                   1480:        k = g->m;
                   1481:        while ( 1 ) {
                   1482:                j = -1;
                   1483:                for ( i = 0; i <= k; i++ ) {
1.34      noro     1484:                        if ( !(gi = g->body[i]) ) continue;
1.26      noro     1485:                        if ( j < 0 ) {
                   1486:                                j = i;
                   1487:                                gj = g->body[j];
1.112   ! noro     1488:                                sum = HCZ(gj);
1.26      noro     1489:                        } else {
                   1490:                                nv = NV(gi);
1.34      noro     1491:                                c = DL_COMPARE(HDL(gi),HDL(gj));
1.26      noro     1492:                                if ( c > 0 ) {
1.112   ! noro     1493:                                        if ( sum ) HCZ(gj) = sum;
1.34      noro     1494:                                        else g->body[j] = nd_remove_head(gj);
1.26      noro     1495:                                        j = i;
                   1496:                                        gj = g->body[j];
1.112   ! noro     1497:                                        sum = HCZ(gj);
1.26      noro     1498:                                } else if ( c == 0 ) {
1.112   ! noro     1499:                                        sum = addz(sum,HCZ(gi));
1.26      noro     1500:                                        g->body[i] = nd_remove_head(gi);
                   1501:                                }
                   1502:                        }
                   1503:                }
1.34      noro     1504:                if ( j < 0 ) return -1;
1.26      noro     1505:                else if ( sum ) {
1.112   ! noro     1506:                        HCZ(gj) = sum;
1.1       noro     1507:                        return j;
1.31      noro     1508:                } else
1.1       noro     1509:                        g->body[j] = nd_remove_head(gj);
                   1510:        }
                   1511: }
                   1512:
1.25      noro     1513: ND normalize_pbucket(int mod,PGeoBucket g)
1.1       noro     1514: {
1.31      noro     1515:        int i;
1.1       noro     1516:        ND r,t;
                   1517:
                   1518:        r = 0;
1.28      noro     1519:        for ( i = 0; i <= g->m; i++ ) {
1.31      noro     1520:                r = nd_add(mod,r,g->body[i]);
1.28      noro     1521:                g->body[i] = 0;
                   1522:        }
                   1523:        g->m = -1;
1.1       noro     1524:        return r;
                   1525: }
                   1526:
1.92      noro     1527: void do_diagonalize(int sugar,int m)
                   1528: {
                   1529:        int i,nh,stat;
                   1530:        NODE r,g,t;
                   1531:        ND h,nf,s,head;
                   1532:        NDV nfv;
1.112   ! noro     1533:        Z q,num,den;
        !          1534:        Q dn;
1.92      noro     1535:
                   1536:        for ( i = nd_psn-1; i >= 0 && SG(nd_psh[i]) == sugar; i-- ) {
                   1537:                if ( nd_demand )
                   1538:                        nfv = ndv_load(i);
                   1539:                else
                   1540:                        nfv = nd_ps[i];
                   1541:                s = ndvtond(m,nfv);
                   1542:                s = nd_separate_head(s,&head);
                   1543:                nd_nf(m,s,nd_ps,1,&dn,&nf);
                   1544:                if ( !m ) {
1.112   ! noro     1545:                        HCZ(head) = mulz(HCZ(head),simpz((Z)NM(dn)));
        !          1546:                        if ( SGN(dn) < 0 ) HCZ(head) = chsgnz(HCZ(head));
        !          1547:                        if ( DN(dn) )
        !          1548:                                nd_mul_c_z(nf,simpz((Z)DN(dn)));
1.92      noro     1549:                }
                   1550:                nf = nd_add(m,head,nf);
                   1551:                ndv_free(nfv);
                   1552:                nd_removecont(m,nf);
                   1553:                nfv = ndtondv(m,nf);
                   1554:                nd_free(nf);
                   1555:                nd_bound[i] = ndv_compute_bound(nfv);
                   1556:                if ( nd_demand ) {
                   1557:                        ndv_save(nfv,i);
                   1558:                        ndv_free(nfv);
                   1559:                } else
                   1560:                        nd_ps[i] = nfv;
                   1561:        }
                   1562: }
                   1563:
1.27      noro     1564: /* return value = 0 => input is not a GB */
                   1565:
1.77      noro     1566: NODE nd_gb(int m,int ishomo,int checkonly)
1.1       noro     1567: {
                   1568:        int i,nh,sugar,stat;
1.23      noro     1569:        NODE r,g,t;
1.1       noro     1570:        ND_pairs d;
                   1571:        ND_pairs l;
1.77      noro     1572:        ND h,nf,s,head;
1.63      noro     1573:        NDV nfv;
1.1       noro     1574:
1.23      noro     1575:        g = 0; d = 0;
                   1576:        for ( i = 0; i < nd_psn; i++ ) {
1.1       noro     1577:                d = update_pairs(d,g,i);
                   1578:                g = update_base(g,i);
                   1579:        }
                   1580:        sugar = 0;
                   1581:        while ( d ) {
                   1582: again:
                   1583:                l = nd_minp(d,&d);
1.14      noro     1584:                if ( SG(l) != sugar ) {
1.92      noro     1585:                        if ( ishomo ) do_diagonalize(sugar,m);
                   1586:
1.14      noro     1587:                        sugar = SG(l);
1.71      noro     1588:                        if ( DP_Print ) fprintf(asir_out,"%d",sugar);
1.1       noro     1589:                }
1.53      noro     1590:                stat = nd_sp(m,0,l,&h);
1.1       noro     1591:                if ( !stat ) {
                   1592:                        NEXT(l) = d; d = l;
1.103     noro     1593:                        d = nd_reconstruct(0,d);
1.1       noro     1594:                        goto again;
                   1595:                }
1.41      noro     1596: #if USE_GEOBUCKET
1.69      noro     1597:                stat = m?nd_nf_pbucket(m,h,nd_ps,!Top,&nf):nd_nf(m,h,nd_ps,!Top,0,&nf);
1.41      noro     1598: #else
1.69      noro     1599:                stat = nd_nf(m,h,nd_ps,!Top,0,&nf);
1.41      noro     1600: #endif
1.1       noro     1601:                if ( !stat ) {
                   1602:                        NEXT(l) = d; d = l;
1.103     noro     1603:                        d = nd_reconstruct(0,d);
1.1       noro     1604:                        goto again;
                   1605:                } else if ( nf ) {
1.27      noro     1606:                        if ( checkonly ) return 0;
1.71      noro     1607:                        if ( DP_Print ) { printf("+"); fflush(stdout); }
1.63      noro     1608:                        nd_removecont(m,nf);
                   1609:                        nfv = ndtondv(m,nf); nd_free(nf);
1.77      noro     1610:                        nh = ndv_newps(m,nfv,0);
1.1       noro     1611:                        d = update_pairs(d,g,nh);
                   1612:                        g = update_base(g,nh);
                   1613:                        FREENDP(l);
                   1614:                } else {
1.71      noro     1615:                        if ( DP_Print ) { printf("."); fflush(stdout); }
1.1       noro     1616:                        FREENDP(l);
                   1617:                }
                   1618:        }
1.77      noro     1619:        if ( nd_demand )
                   1620:                for ( t = g; t; t = NEXT(t) )
                   1621:                        BDY(t) = (pointer)ndv_load((int)BDY(t));
                   1622:        else
                   1623:                for ( t = g; t; t = NEXT(t) )
                   1624:                        BDY(t) = (pointer)nd_ps[(int)BDY(t)];
1.1       noro     1625:        return g;
                   1626: }
                   1627:
1.92      noro     1628: void do_diagonalize_trace(int sugar,int m)
1.91      noro     1629: {
                   1630:        int i,nh,stat;
                   1631:        NODE r,g,t;
                   1632:        ND h,nf,nfq,s,head;
                   1633:        NDV nfv,nfqv;
1.112   ! noro     1634:        Q dn;
1.91      noro     1635:
                   1636:        for ( i = nd_psn-1; i >= 0 && SG(nd_psh[i]) == sugar; i-- ) {
                   1637:                /* for nd_ps */
                   1638:                s = ndvtond(m,nd_ps[i]);
                   1639:                s = nd_separate_head(s,&head);
                   1640:                nd_nf_pbucket(m,s,nd_ps,1,&nf);
                   1641:                nf = nd_add(m,head,nf);
                   1642:                ndv_free(nd_ps[i]);
                   1643:                nd_ps[i] = ndtondv(m,nf);
                   1644:                nd_free(nf);
                   1645:
                   1646:                /* for nd_ps_trace */
                   1647:                if ( nd_demand )
                   1648:                        nfv = ndv_load(i);
                   1649:                else
                   1650:                        nfv = nd_ps_trace[i];
                   1651:                s = ndvtond(0,nfv);
                   1652:                s = nd_separate_head(s,&head);
                   1653:                nd_nf(0,s,nd_ps_trace,1,&dn,&nf);
1.112   ! noro     1654:                HCZ(head) = mulz(HCZ(head),(Z)NM(dn));
        !          1655:                if ( SGN(dn) < 0 ) HCZ(head) = chsgnz(HCZ(head));
        !          1656:                if ( DN(dn) )
        !          1657:                        nd_mul_c_z(nf,(Z)DN(dn));
1.91      noro     1658:                nf = nd_add(0,head,nf);
                   1659:                ndv_free(nfv);
                   1660:                nd_removecont(0,nf);
                   1661:                nfv = ndtondv(0,nf);
                   1662:                nd_free(nf);
                   1663:                nd_bound[i] = ndv_compute_bound(nfv);
                   1664:                if ( nd_demand ) {
                   1665:                        ndv_save(nfv,i);
                   1666:                        ndv_free(nfv);
                   1667:                } else
                   1668:                        nd_ps_trace[i] = nfv;
                   1669:        }
                   1670: }
                   1671:
1.82      noro     1672: NODE nd_gb_trace(int m,int ishomo)
1.20      noro     1673: {
                   1674:        int i,nh,sugar,stat;
1.23      noro     1675:        NODE r,g,t;
1.20      noro     1676:        ND_pairs d;
                   1677:        ND_pairs l;
1.82      noro     1678:        ND h,nf,nfq,s,head;
1.63      noro     1679:        NDV nfv,nfqv;
1.20      noro     1680:
1.23      noro     1681:        g = 0; d = 0;
                   1682:        for ( i = 0; i < nd_psn; i++ ) {
1.20      noro     1683:                d = update_pairs(d,g,i);
                   1684:                g = update_base(g,i);
                   1685:        }
                   1686:        sugar = 0;
                   1687:        while ( d ) {
                   1688: again:
                   1689:                l = nd_minp(d,&d);
                   1690:                if ( SG(l) != sugar ) {
1.93      noro     1691:                        if ( ishomo ) do_diagonalize_trace(sugar,m);
1.20      noro     1692:                        sugar = SG(l);
1.71      noro     1693:                        if ( DP_Print ) fprintf(asir_out,"%d",sugar);
1.20      noro     1694:                }
1.53      noro     1695:                stat = nd_sp(m,0,l,&h);
1.20      noro     1696:                if ( !stat ) {
                   1697:                        NEXT(l) = d; d = l;
1.103     noro     1698:                        d = nd_reconstruct(1,d);
1.20      noro     1699:                        goto again;
                   1700:                }
1.41      noro     1701: #if USE_GEOBUCKET
1.53      noro     1702:                stat = nd_nf_pbucket(m,h,nd_ps,!Top,&nf);
1.41      noro     1703: #else
1.69      noro     1704:                stat = nd_nf(m,h,nd_ps,!Top,0,&nf);
1.41      noro     1705: #endif
1.20      noro     1706:                if ( !stat ) {
                   1707:                        NEXT(l) = d; d = l;
1.103     noro     1708:                        d = nd_reconstruct(1,d);
1.20      noro     1709:                        goto again;
                   1710:                } else if ( nf ) {
1.77      noro     1711:                        if ( nd_demand ) {
                   1712:                                nfqv = ndv_load(nd_psn);
                   1713:                                nfq = ndvtond(0,nfqv);
                   1714:                        } else
                   1715:                                nfq = 0;
                   1716:                        if ( !nfq ) {
                   1717:                                if ( !nd_sp(0,1,l,&h) || !nd_nf(0,h,nd_ps_trace,!Top,0,&nfq) ) {
                   1718:                                        NEXT(l) = d; d = l;
1.103     noro     1719:                                        d = nd_reconstruct(1,d);
1.77      noro     1720:                                        goto again;
                   1721:                                }
1.70      noro     1722:                        }
1.20      noro     1723:                        if ( nfq ) {
1.70      noro     1724:                                /* m|HC(nfq) => failure */
1.112   ! noro     1725:                                if ( !remzi(HCZ(nfq),m) ) return 0;
1.63      noro     1726:
1.71      noro     1727:                                if ( DP_Print ) { printf("+"); fflush(stdout); }
1.63      noro     1728:                                nd_removecont(m,nf); nfv = ndtondv(m,nf); nd_free(nf);
                   1729:                                nd_removecont(0,nfq); nfqv = ndtondv(0,nfq); nd_free(nfq);
1.77      noro     1730:                                nh = ndv_newps(0,nfv,nfqv);
1.20      noro     1731:                                d = update_pairs(d,g,nh);
                   1732:                                g = update_base(g,nh);
                   1733:                        } else {
1.71      noro     1734:                                if ( DP_Print ) { printf("*"); fflush(stdout); }
1.20      noro     1735:                        }
                   1736:                } else {
1.71      noro     1737:                        if ( DP_Print ) { printf("."); fflush(stdout); }
1.20      noro     1738:                }
                   1739:                FREENDP(l);
                   1740:        }
1.77      noro     1741:        if ( nd_demand )
                   1742:                for ( t = g; t; t = NEXT(t) )
                   1743:                        BDY(t) = (pointer)ndv_load((int)BDY(t));
                   1744:        else
                   1745:                for ( t = g; t; t = NEXT(t) )
                   1746:                        BDY(t) = (pointer)nd_ps_trace[(int)BDY(t)];
1.20      noro     1747:        return g;
                   1748: }
                   1749:
1.23      noro     1750: int ndv_compare(NDV *p1,NDV *p2)
                   1751: {
1.34      noro     1752:        return DL_COMPARE(HDL(*p1),HDL(*p2));
1.23      noro     1753: }
                   1754:
                   1755: int ndv_compare_rev(NDV *p1,NDV *p2)
                   1756: {
1.34      noro     1757:        return -DL_COMPARE(HDL(*p1),HDL(*p2));
1.23      noro     1758: }
                   1759:
1.61      noro     1760: NODE ndv_reduceall(int m,NODE f)
1.23      noro     1761: {
1.69      noro     1762:        int i,n,stat;
                   1763:        ND nf,g,head;
1.23      noro     1764:        NODE t,a0,a;
1.112   ! noro     1765:        Q dn;
1.69      noro     1766:        NDV *w;
1.23      noro     1767:
1.69      noro     1768:        n = length(f);
                   1769: #if 0
                   1770:        w = (NDV *)ALLOCA(n*sizeof(NDV));
                   1771:        for ( i = 0, t = f; i < n; i++, t = NEXT(t) ) w[i] = (NDV)BDY(t);
                   1772:        qsort(w,n,sizeof(NDV),
                   1773:                (int (*)(const void *,const void *))ndv_compare);
                   1774:        for ( t = f, i = 0; t; i++, t = NEXT(t) ) BDY(t) = (pointer)w[i];
                   1775: #endif
                   1776:        ndv_setup(m,0,f);
                   1777:        for ( i = 0; i < n; ) {
                   1778:                g = ndvtond(m,nd_ps[i]);
                   1779:                g = nd_separate_head(g,&head);
                   1780:                stat = nd_nf(m,g,nd_ps,1,&dn,&nf);
1.50      noro     1781:                if ( !stat )
1.103     noro     1782:                        nd_reconstruct(0,0);
1.69      noro     1783:                else {
1.71      noro     1784:                        if ( DP_Print ) { printf("."); fflush(stdout); }
1.85      noro     1785:                        if ( !m ) {
1.112   ! noro     1786:                                HCZ(head) = mulz(HCZ(head),simpz((Z)NM(dn)));
        !          1787:                                if ( SGN(dn) < 0 ) HCZ(head) = chsgnz(HCZ(head));
        !          1788:                                if ( DN(dn) )
        !          1789:                                        nd_mul_c_z(nf,simpz((Z)DN(dn)));
1.85      noro     1790:                        }
1.69      noro     1791:                        nf = nd_add(m,head,nf);
                   1792:                        ndv_free(nd_ps[i]);
1.24      noro     1793:                        nd_removecont(m,nf);
1.69      noro     1794:                        nd_ps[i] = ndtondv(m,nf); nd_free(nf);
                   1795:                        nd_bound[i] = ndv_compute_bound(nd_ps[i]);
1.50      noro     1796:                        i++;
1.23      noro     1797:                }
                   1798:        }
1.71      noro     1799:        if ( DP_Print ) { printf("\n"); }
1.23      noro     1800:        for ( a0 = 0, i = 0; i < n; i++ ) {
                   1801:                NEXTNODE(a0,a);
1.69      noro     1802:                BDY(a) = (pointer)nd_ps[i];
1.23      noro     1803:        }
                   1804:        NEXT(a) = 0;
                   1805:        return a0;
                   1806: }
                   1807:
1.1       noro     1808: ND_pairs update_pairs( ND_pairs d, NODE /* of index */ g, int t)
                   1809: {
                   1810:        ND_pairs d1,nd,cur,head,prev,remove;
                   1811:
                   1812:        if ( !g ) return d;
                   1813:        d = crit_B(d,t);
                   1814:        d1 = nd_newpairs(g,t);
                   1815:        d1 = crit_M(d1);
                   1816:        d1 = crit_F(d1);
1.55      noro     1817:        if ( do_weyl )
                   1818:                head = d1;
                   1819:        else {
                   1820:                prev = 0; cur = head = d1;
                   1821:                while ( cur ) {
                   1822:                        if ( crit_2( cur->i1,cur->i2 ) ) {
                   1823:                                remove = cur;
                   1824:                                if ( !prev ) head = cur = NEXT(cur);
                   1825:                                else cur = NEXT(prev) = NEXT(cur);
                   1826:                                FREENDP(remove);
                   1827:                        } else {
                   1828:                                prev = cur; cur = NEXT(cur);
                   1829:                        }
1.1       noro     1830:                }
                   1831:        }
                   1832:        if ( !d )
                   1833:                return head;
                   1834:        else {
                   1835:                nd = d;
1.34      noro     1836:                while ( NEXT(nd) ) nd = NEXT(nd);
1.1       noro     1837:                NEXT(nd) = head;
                   1838:                return d;
                   1839:        }
                   1840: }
                   1841:
                   1842: ND_pairs nd_newpairs( NODE g, int t )
                   1843: {
                   1844:        NODE h;
1.61      noro     1845:        UINT *dl;
1.34      noro     1846:        int ts,s;
1.1       noro     1847:        ND_pairs r,r0;
                   1848:
1.20      noro     1849:        dl = DL(nd_psh[t]);
1.34      noro     1850:        ts = SG(nd_psh[t]) - TD(dl);
1.1       noro     1851:        for ( r0 = 0, h = g; h; h = NEXT(h) ) {
                   1852:                NEXTND_pairs(r0,r);
                   1853:                r->i1 = (int)BDY(h);
                   1854:                r->i2 = t;
1.20      noro     1855:                ndl_lcm(DL(nd_psh[r->i1]),dl,r->lcm);
1.34      noro     1856:                s = SG(nd_psh[r->i1])-TD(DL(nd_psh[r->i1]));
                   1857:                SG(r) = MAX(s,ts) + TD(LCM(r));
1.1       noro     1858:        }
                   1859:        NEXT(r) = 0;
                   1860:        return r0;
                   1861: }
                   1862:
                   1863: ND_pairs crit_B( ND_pairs d, int s )
                   1864: {
                   1865:        ND_pairs cur,head,prev,remove;
1.61      noro     1866:        UINT *t,*tl,*lcm;
1.1       noro     1867:        int td,tdl;
                   1868:
                   1869:        if ( !d ) return 0;
1.20      noro     1870:        t = DL(nd_psh[s]);
1.1       noro     1871:        prev = 0;
                   1872:        head = cur = d;
1.61      noro     1873:        lcm = (UINT *)ALLOCA(nd_wpd*sizeof(UINT));
1.1       noro     1874:        while ( cur ) {
                   1875:                tl = cur->lcm;
                   1876:                if ( ndl_reducible(tl,t)
1.20      noro     1877:                        && (ndl_lcm(DL(nd_psh[cur->i1]),t,lcm),!ndl_equal(lcm,tl))
                   1878:                        && (ndl_lcm(DL(nd_psh[cur->i2]),t,lcm),!ndl_equal(lcm,tl)) ) {
1.1       noro     1879:                        remove = cur;
                   1880:                        if ( !prev ) {
                   1881:                                head = cur = NEXT(cur);
                   1882:                        } else {
                   1883:                                cur = NEXT(prev) = NEXT(cur);
                   1884:                        }
                   1885:                        FREENDP(remove);
                   1886:                } else {
1.34      noro     1887:                        prev = cur; cur = NEXT(cur);
1.1       noro     1888:                }
                   1889:        }
                   1890:        return head;
                   1891: }
                   1892:
                   1893: ND_pairs crit_M( ND_pairs d1 )
                   1894: {
                   1895:        ND_pairs e,d2,d3,dd,p;
1.61      noro     1896:        UINT *id,*jd;
1.1       noro     1897:
                   1898:        for ( dd = 0, e = d1; e; e = d3 ) {
                   1899:                if ( !(d2 = NEXT(e)) ) {
                   1900:                        NEXT(e) = dd;
                   1901:                        return e;
                   1902:                }
1.34      noro     1903:                id = LCM(e);
1.1       noro     1904:                for ( d3 = 0; d2; d2 = p ) {
1.34      noro     1905:                        p = NEXT(d2);
                   1906:                        jd = LCM(d2);
                   1907:                        if ( ndl_equal(jd,id) )
                   1908:                                ;
                   1909:                        else if ( TD(jd) > TD(id) )
1.1       noro     1910:                                if ( ndl_reducible(jd,id) ) continue;
                   1911:                                else ;
1.34      noro     1912:                        else if ( ndl_reducible(id,jd) ) goto delit;
1.1       noro     1913:                        NEXT(d2) = d3;
                   1914:                        d3 = d2;
                   1915:                }
                   1916:                NEXT(e) = dd;
                   1917:                dd = e;
                   1918:                continue;
                   1919:                /**/
                   1920:        delit:  NEXT(d2) = d3;
                   1921:                d3 = d2;
                   1922:                for ( ; p; p = d2 ) {
                   1923:                        d2 = NEXT(p);
                   1924:                        NEXT(p) = d3;
                   1925:                        d3 = p;
                   1926:                }
                   1927:                FREENDP(e);
                   1928:        }
                   1929:        return dd;
                   1930: }
                   1931:
                   1932: ND_pairs crit_F( ND_pairs d1 )
                   1933: {
                   1934:        ND_pairs rest, head,remove;
                   1935:        ND_pairs last, p, r, w;
                   1936:        int s;
                   1937:
                   1938:        for ( head = last = 0, p = d1; NEXT(p); ) {
                   1939:                r = w = equivalent_pairs(p,&rest);
1.14      noro     1940:                s = SG(r);
1.1       noro     1941:                w = NEXT(w);
                   1942:                while ( w ) {
                   1943:                        if ( crit_2(w->i1,w->i2) ) {
                   1944:                                r = w;
                   1945:                                w = NEXT(w);
                   1946:                                while ( w ) {
                   1947:                                        remove = w;
                   1948:                                        w = NEXT(w);
                   1949:                                        FREENDP(remove);
                   1950:                                }
                   1951:                                break;
1.14      noro     1952:                        } else if ( SG(w) < s ) {
1.1       noro     1953:                                FREENDP(r);
                   1954:                                r = w;
1.14      noro     1955:                                s = SG(r);
1.1       noro     1956:                                w = NEXT(w);
                   1957:                        } else {
                   1958:                                remove = w;
                   1959:                                w = NEXT(w);
                   1960:                                FREENDP(remove);
                   1961:                        }
                   1962:                }
                   1963:                if ( last ) NEXT(last) = r;
                   1964:                else head = r;
                   1965:                NEXT(last = r) = 0;
                   1966:                p = rest;
                   1967:                if ( !p ) return head;
                   1968:        }
                   1969:        if ( !last ) return p;
                   1970:        NEXT(last) = p;
                   1971:        return head;
                   1972: }
                   1973:
                   1974: int crit_2( int dp1, int dp2 )
                   1975: {
1.20      noro     1976:        return ndl_disjoint(DL(nd_psh[dp1]),DL(nd_psh[dp2]));
1.1       noro     1977: }
                   1978:
1.40      noro     1979: ND_pairs equivalent_pairs( ND_pairs d1, ND_pairs *prest )
1.1       noro     1980: {
                   1981:        ND_pairs w,p,r,s;
1.61      noro     1982:        UINT *d;
1.1       noro     1983:
                   1984:        w = d1;
1.34      noro     1985:        d = LCM(w);
1.1       noro     1986:        s = NEXT(w);
                   1987:        NEXT(w) = 0;
                   1988:        for ( r = 0; s; s = p ) {
                   1989:                p = NEXT(s);
1.34      noro     1990:                if ( ndl_equal(d,LCM(s)) ) {
1.39      noro     1991:                        NEXT(s) = w; w = s;
1.1       noro     1992:                } else {
1.39      noro     1993:                        NEXT(s) = r; r = s;
1.1       noro     1994:                }
                   1995:        }
                   1996:        *prest = r;
                   1997:        return w;
                   1998: }
                   1999:
                   2000: NODE update_base(NODE nd,int ndp)
                   2001: {
1.61      noro     2002:        UINT *dl, *dln;
1.1       noro     2003:        NODE last, p, head;
                   2004:
1.20      noro     2005:        dl = DL(nd_psh[ndp]);
1.1       noro     2006:        for ( head = last = 0, p = nd; p; ) {
1.20      noro     2007:                dln = DL(nd_psh[(int)BDY(p)]);
1.34      noro     2008:                if ( ndl_reducible( dln, dl ) ) {
1.1       noro     2009:                        p = NEXT(p);
                   2010:                        if ( last ) NEXT(last) = p;
                   2011:                } else {
                   2012:                        if ( !last ) head = p;
                   2013:                        p = NEXT(last = p);
                   2014:                }
                   2015:        }
                   2016:        head = append_one(head,ndp);
                   2017:        return head;
                   2018: }
                   2019:
                   2020: ND_pairs nd_minp( ND_pairs d, ND_pairs *prest )
                   2021: {
                   2022:        ND_pairs m,ml,p,l;
1.61      noro     2023:        UINT *lcm;
1.33      noro     2024:        int s,td,len,tlen,c,c1;
1.1       noro     2025:
                   2026:        if ( !(p = NEXT(m = d)) ) {
                   2027:                *prest = p;
                   2028:                NEXT(m) = 0;
                   2029:                return m;
                   2030:        }
1.14      noro     2031:        s = SG(m);
1.77      noro     2032:        if ( !NoSugar ) {
                   2033:                for ( ml = 0, l = m; p; p = NEXT(l = p) )
                   2034:                        if ( (SG(p) < s)
                   2035:                                || ((SG(p) == s) && (DL_COMPARE(LCM(p),LCM(m)) < 0)) ) {
                   2036:                                ml = l; m = p; s = SG(m);
                   2037:                        }
                   2038:        } else {
                   2039:                for ( ml = 0, l = m; p; p = NEXT(l = p) )
                   2040:                        if ( DL_COMPARE(LCM(p),LCM(m)) < 0 ) {
                   2041:                                ml = l; m = p; s = SG(m);
                   2042:                        }
                   2043:        }
1.1       noro     2044:        if ( !ml ) *prest = NEXT(m);
                   2045:        else {
                   2046:                NEXT(ml) = NEXT(m);
                   2047:                *prest = d;
                   2048:        }
                   2049:        NEXT(m) = 0;
                   2050:        return m;
                   2051: }
                   2052:
1.63      noro     2053: ND_pairs nd_minsugarp( ND_pairs d, ND_pairs *prest )
                   2054: {
1.69      noro     2055:        int msugar,i;
1.63      noro     2056:        ND_pairs t,dm0,dm,dr0,dr;
                   2057:
                   2058:        for ( msugar = SG(d), t = NEXT(d); t; t = NEXT(t) )
                   2059:                if ( SG(t) < msugar ) msugar = SG(t);
                   2060:        dm0 = 0; dr0 = 0;
1.69      noro     2061:        for ( i = 0, t = d; t; t = NEXT(t) )
                   2062:                if ( i < nd_f4_nsp && SG(t) == msugar ) {
1.63      noro     2063:                        if ( dm0 ) NEXT(dm) = t;
                   2064:                        else dm0 = t;
                   2065:                        dm = t;
1.69      noro     2066:                        i++;
1.63      noro     2067:                } else {
                   2068:                        if ( dr0 ) NEXT(dr) = t;
                   2069:                        else dr0 = t;
                   2070:                        dr = t;
                   2071:                }
                   2072:        NEXT(dm) = 0;
                   2073:        if ( dr0 ) NEXT(dr) = 0;
                   2074:        *prest = dr0;
                   2075:        return dm0;
                   2076: }
                   2077:
1.77      noro     2078: int ndv_newps(int m,NDV a,NDV aq)
1.1       noro     2079: {
1.3       noro     2080:        int len;
1.13      noro     2081:        RHist r;
1.20      noro     2082:        NDV b;
1.3       noro     2083:
1.1       noro     2084:        if ( nd_psn == nd_pslen ) {
                   2085:                nd_pslen *= 2;
1.11      noro     2086:                nd_ps = (NDV *)REALLOC((char *)nd_ps,nd_pslen*sizeof(NDV));
1.53      noro     2087:                nd_ps_trace = (NDV *)REALLOC((char *)nd_ps_trace,nd_pslen*sizeof(NDV));
1.13      noro     2088:                nd_psh = (RHist *)REALLOC((char *)nd_psh,nd_pslen*sizeof(RHist));
1.61      noro     2089:                nd_bound = (UINT **)
                   2090:                        REALLOC((char *)nd_bound,nd_pslen*sizeof(UINT *));
1.1       noro     2091:        }
1.39      noro     2092:        NEWRHist(r); nd_psh[nd_psn] = r;
1.63      noro     2093:        nd_ps[nd_psn] = a;
1.39      noro     2094:        if ( aq ) {
1.63      noro     2095:                nd_ps_trace[nd_psn] = aq;
                   2096:                nd_bound[nd_psn] = ndv_compute_bound(aq);
1.39      noro     2097:                SG(r) = SG(aq); ndl_copy(HDL(aq),DL(r));
1.53      noro     2098:        } else {
1.63      noro     2099:                nd_bound[nd_psn] = ndv_compute_bound(a);
1.53      noro     2100:                SG(r) = SG(a); ndl_copy(HDL(a),DL(r));
1.39      noro     2101:        }
1.77      noro     2102:        if ( nd_demand ) {
                   2103:                if ( aq ) {
                   2104:                        ndv_save(nd_ps_trace[nd_psn],nd_psn);
                   2105:                        nd_ps_trace[nd_psn] = 0;
                   2106:                } else {
                   2107:                        ndv_save(nd_ps[nd_psn],nd_psn);
                   2108:                        nd_ps[nd_psn] = 0;
                   2109:                }
                   2110:        }
1.1       noro     2111:        return nd_psn++;
                   2112: }
                   2113:
1.61      noro     2114: void ndv_setup(int mod,int trace,NODE f)
1.1       noro     2115: {
1.5       noro     2116:        int i,j,td,len,max;
1.1       noro     2117:        NODE s,s0,f0;
1.61      noro     2118:        UINT *d;
1.13      noro     2119:        RHist r;
1.69      noro     2120:        NDV *w;
1.61      noro     2121:        NDV a,am;
1.11      noro     2122:
                   2123:        nd_found = 0; nd_notfirst = 0; nd_create = 0;
1.1       noro     2124:
1.69      noro     2125:        for ( nd_psn = 0, s = f; s; s = NEXT(s) ) if ( BDY(s) ) nd_psn++;
                   2126:        w = (NDV *)ALLOCA(nd_psn*sizeof(NDV));
                   2127:        for ( i = 0, s = f; s; s = NEXT(s) ) if ( BDY(s) ) w[i++] = BDY(s);
                   2128:        qsort(w,nd_psn,sizeof(NDV),
                   2129:                (int (*)(const void *,const void *))ndv_compare);
                   2130:        nd_pslen = 2*nd_psn;
1.11      noro     2131:        nd_ps = (NDV *)MALLOC(nd_pslen*sizeof(NDV));
1.53      noro     2132:        nd_ps_trace = (NDV *)MALLOC(nd_pslen*sizeof(NDV));
1.13      noro     2133:        nd_psh = (RHist *)MALLOC(nd_pslen*sizeof(RHist));
1.61      noro     2134:        nd_bound = (UINT **)MALLOC(nd_pslen*sizeof(UINT *));
1.57      noro     2135:
1.11      noro     2136:        if ( !nd_red )
1.13      noro     2137:                nd_red = (RHist *)MALLOC(REDTAB_LEN*sizeof(RHist));
1.71      noro     2138:        for ( i = 0; i < REDTAB_LEN; i++ ) nd_red[i] = 0;
1.69      noro     2139:        for ( i = 0; i < nd_psn; i++ ) {
1.39      noro     2140:                if ( trace ) {
1.69      noro     2141:                        a = nd_ps_trace[i] = ndv_dup(0,w[i]);
1.61      noro     2142:                        ndv_removecont(0,a);
                   2143:                        am = nd_ps[i] = ndv_dup(mod,a);
                   2144:                        ndv_mod(mod,am);
                   2145:                        ndv_removecont(mod,am);
                   2146:                } else {
1.69      noro     2147:                        a = nd_ps[i] = ndv_dup(mod,w[i]);
1.61      noro     2148:                        ndv_removecont(mod,a);
1.39      noro     2149:                }
1.61      noro     2150:                NEWRHist(r); SG(r) = HTD(a); ndl_copy(HDL(a),DL(r));
1.57      noro     2151:                nd_bound[i] = ndv_compute_bound(a);
1.20      noro     2152:                nd_psh[i] = r;
1.77      noro     2153:                if ( nd_demand ) {
                   2154:                        if ( trace ) {
                   2155:                                ndv_save(nd_ps_trace[i],i);
                   2156:                                nd_ps_trace[i] = 0;
                   2157:                        } else {
                   2158:                                ndv_save(nd_ps[i],i);
                   2159:                                nd_ps[i] = 0;
                   2160:                        }
                   2161:                }
1.20      noro     2162:        }
                   2163: }
                   2164:
1.63      noro     2165: void nd_gr(LIST f,LIST v,int m,int f4,struct order_spec *ord,LIST *rp)
1.1       noro     2166: {
1.61      noro     2167:        VL tv,fv,vv,vc;
1.1       noro     2168:        NODE fd,fd0,r,r0,t,x,s,xx;
1.61      noro     2169:        int e,max,nvar;
1.69      noro     2170:        NDV b;
1.77      noro     2171:        int ishomo;
1.1       noro     2172:
1.78      noro     2173:        if ( !m && Demand ) nd_demand = 1;
                   2174:        else nd_demand = 0;
                   2175:
1.103     noro     2176: #if 0
1.74      noro     2177:        ndv_alloc = 0;
1.103     noro     2178: #endif
1.1       noro     2179:        get_vars((Obj)f,&fv); pltovl(v,&vv);
1.74      noro     2180:        for ( nvar = 0, tv = vv; tv; tv = NEXT(tv), nvar++ );
1.88      noro     2181:        switch ( ord->id ) {
                   2182:                case 1:
                   2183:                        if ( ord->nv != nvar )
                   2184:                                error("nd_{gr,f4} : invalid order specification");
                   2185:                        break;
                   2186:                default:
                   2187:                        break;
                   2188:        }
1.32      noro     2189:        nd_init_ord(ord);
1.61      noro     2190:        for ( t = BDY(f), max = 0; t; t = NEXT(t) )
                   2191:                for ( tv = vv; tv; tv = NEXT(tv) ) {
                   2192:                        e = getdeg(tv->v,(P)BDY(t));
                   2193:                        max = MAX(e,max);
                   2194:                }
                   2195:        nd_setup_parameters(nvar,max);
1.77      noro     2196:        ishomo = 1;
1.1       noro     2197:        for ( fd0 = 0, t = BDY(f); t; t = NEXT(t) ) {
1.61      noro     2198:                b = (pointer)ptondv(CO,vv,(P)BDY(t));
1.77      noro     2199:                if ( ishomo )
                   2200:                        ishomo = ishomo && ndv_ishomo(b);
1.69      noro     2201:                if ( m ) ndv_mod(m,b);
1.61      noro     2202:                if ( b ) { NEXTNODE(fd0,fd); BDY(fd) = (pointer)b; }
1.1       noro     2203:        }
                   2204:        if ( fd0 ) NEXT(fd) = 0;
1.61      noro     2205:        ndv_setup(m,0,fd0);
1.77      noro     2206:        x = f4?nd_f4(m):nd_gb(m,ishomo,0);
1.82      noro     2207:        nd_demand = 0;
1.61      noro     2208:        x = ndv_reducebase(x);
                   2209:        x = ndv_reduceall(m,x);
1.23      noro     2210:        for ( r0 = 0, t = x; t; t = NEXT(t) ) {
1.1       noro     2211:                NEXTNODE(r0,r);
1.61      noro     2212:                BDY(r) = ndvtop(m,CO,vv,BDY(t));
1.20      noro     2213:        }
                   2214:        if ( r0 ) NEXT(r) = 0;
                   2215:        MKLIST(*rp,r0);
1.103     noro     2216: #if 0
1.74      noro     2217:        fprintf(asir_out,"ndv_alloc=%d\n",ndv_alloc);
1.103     noro     2218: #endif
1.20      noro     2219: }
                   2220:
1.52      noro     2221: void nd_gr_trace(LIST f,LIST v,int trace,int homo,struct order_spec *ord,LIST *rp)
1.20      noro     2222: {
1.89      noro     2223:        struct order_spec *ord1;
1.61      noro     2224:        VL tv,fv,vv,vc;
1.27      noro     2225:        NODE fd,fd0,in0,in,r,r0,t,s,cand;
1.61      noro     2226:        int m,nocheck,nvar,mindex,e,max;
                   2227:        NDV c;
                   2228:        NMV a;
1.27      noro     2229:        P p;
1.61      noro     2230:        EPOS oepos;
1.82      noro     2231:        int obpe,oadv,wmax,i,len,cbpe,ishomo;
1.20      noro     2232:
                   2233:        get_vars((Obj)f,&fv); pltovl(v,&vv);
1.74      noro     2234:        for ( nvar = 0, tv = vv; tv; tv = NEXT(tv), nvar++ );
1.88      noro     2235:        switch ( ord->id ) {
                   2236:                case 1:
                   2237:                        if ( ord->nv != nvar )
                   2238:                                error("nd_gr_trace : invalid order specification");
                   2239:                        break;
                   2240:                default:
                   2241:                        break;
                   2242:        }
1.52      noro     2243:        nocheck = 0;
                   2244:        mindex = 0;
                   2245:
1.78      noro     2246:        if ( Demand ) nd_demand = 1;
                   2247:        else nd_demand = 0;
                   2248:
1.52      noro     2249:        /* setup modulus */
                   2250:        if ( trace < 0 ) {
                   2251:                trace = -trace;
                   2252:                nocheck = 1;
                   2253:        }
                   2254:        m = trace > 1 ? trace : get_lprime(mindex);
1.61      noro     2255:        for ( t = BDY(f), max = 0; t; t = NEXT(t) )
                   2256:                for ( tv = vv; tv; tv = NEXT(tv) ) {
                   2257:                        e = getdeg(tv->v,(P)BDY(t));
                   2258:                        max = MAX(e,max);
1.23      noro     2259:                }
1.61      noro     2260:        nd_init_ord(ord);
                   2261:        nd_setup_parameters(nvar,max);
                   2262:        obpe = nd_bpe; oadv = nmv_adv; oepos = nd_epos;
1.82      noro     2263:        ishomo = 1;
1.61      noro     2264:        for ( in0 = 0, fd0 = 0, t = BDY(f); t; t = NEXT(t) ) {
                   2265:                c = ptondv(CO,vv,(P)BDY(t));
1.82      noro     2266:                if ( ishomo )
                   2267:                        ishomo = ishomo && ndv_ishomo(c);
1.61      noro     2268:                if ( c ) {
                   2269:                        NEXTNODE(in0,in); BDY(in) = (pointer)c;
                   2270:                        NEXTNODE(fd0,fd); BDY(fd) = (pointer)ndv_dup(0,c);
1.23      noro     2271:                }
1.61      noro     2272:        }
                   2273:        if ( in0 ) NEXT(in) = 0;
                   2274:        if ( fd0 ) NEXT(fd) = 0;
1.82      noro     2275:        if ( !ishomo && homo ) {
1.61      noro     2276:                for ( t = in0, wmax = 0; t; t = NEXT(t) ) {
                   2277:                        c = (NDV)BDY(t); len = LEN(c);
                   2278:                        for ( a = BDY(c), i = 0; i < len; i++, NMV_ADV(a) )
                   2279:                                wmax = MAX(TD(DL(a)),wmax);
                   2280:                }
                   2281:                homogenize_order(ord,nvar,&ord1);
1.89      noro     2282:                nd_init_ord(ord1);
1.61      noro     2283:                nd_setup_parameters(nvar+1,wmax);
                   2284:                for ( t = fd0; t; t = NEXT(t) )
                   2285:                        ndv_homogenize((NDV)BDY(t),obpe,oadv,oepos);
1.27      noro     2286:        }
1.52      noro     2287:        while ( 1 ) {
1.77      noro     2288:                if ( Demand )
                   2289:                        nd_demand = 1;
1.61      noro     2290:                ndv_setup(m,1,fd0);
1.82      noro     2291:                cand = nd_gb_trace(m,ishomo || homo);
1.52      noro     2292:                if ( !cand ) {
                   2293:                        /* failure */
1.61      noro     2294:                        if ( trace > 1 ) { *rp = 0; return; }
                   2295:                        else m = get_lprime(++mindex);
1.52      noro     2296:                        continue;
                   2297:                }
1.82      noro     2298:                if ( !ishomo && homo ) {
1.27      noro     2299:                        /* dehomogenization */
1.61      noro     2300:                        for ( t = cand; t; t = NEXT(t) ) ndv_dehomogenize((NDV)BDY(t),ord);
1.45      noro     2301:                        nd_init_ord(ord);
1.61      noro     2302:                        nd_setup_parameters(nvar,0);
1.27      noro     2303:                }
1.77      noro     2304:                nd_demand = 0;
1.61      noro     2305:                cand = ndv_reducebase(cand);
                   2306:                cand = ndv_reduceall(0,cand);
1.78      noro     2307:                cbpe = nd_bpe;
1.61      noro     2308:                if ( nocheck )
                   2309:                        break;
                   2310:                if ( ndv_check_candidate(in0,obpe,oadv,oepos,cand) )
1.52      noro     2311:                        /* success */
                   2312:                        break;
                   2313:                else if ( trace > 1 ) {
                   2314:                        /* failure */
                   2315:                        *rp = 0; return;
1.61      noro     2316:                } else {
1.52      noro     2317:                        /* try the next modulus */
                   2318:                        m = get_lprime(++mindex);
1.61      noro     2319:                        /* reset the parameters */
1.82      noro     2320:                        if ( !ishomo && homo ) {
1.89      noro     2321:                                nd_init_ord(ord1);
1.61      noro     2322:                                nd_setup_parameters(nvar+1,wmax);
                   2323:                        } else {
                   2324:                                nd_init_ord(ord);
                   2325:                                nd_setup_parameters(nvar,max);
                   2326:                        }
                   2327:                }
1.52      noro     2328:        }
1.27      noro     2329:        /* dp->p */
1.61      noro     2330:        nd_bpe = cbpe;
1.73      noro     2331:        nd_setup_parameters(nd_nvar,0);
1.61      noro     2332:        for ( r = cand; r; r = NEXT(r) ) BDY(r) = (pointer)ndvtop(0,CO,vv,BDY(r));
1.27      noro     2333:        MKLIST(*rp,cand);
1.1       noro     2334: }
                   2335:
1.61      noro     2336: void dltondl(int n,DL dl,UINT *r)
1.1       noro     2337: {
1.61      noro     2338:        UINT *d;
1.57      noro     2339:        int i,j,l,s,ord_l;
1.43      noro     2340:        struct order_pair *op;
1.1       noro     2341:
                   2342:        d = dl->d;
1.41      noro     2343:        for ( i = 0; i < nd_wpd; i++ ) r[i] = 0;
1.43      noro     2344:        if ( nd_blockmask ) {
                   2345:                l = nd_blockmask->n;
                   2346:                op = nd_blockmask->order_pair;
                   2347:                for ( j = 0, s = 0; j < l; j++ ) {
                   2348:                        ord_l = op[j].length;
1.57      noro     2349:                        for ( i = 0; i < ord_l; i++, s++ ) PUT_EXP(r,s,d[s]);
1.43      noro     2350:                }
                   2351:                TD(r) = ndl_weight(r);
1.61      noro     2352:                ndl_weight_mask(r);
1.43      noro     2353:        } else {
1.56      noro     2354:                for ( i = 0; i < n; i++ ) PUT_EXP(r,i,d[i]);
1.43      noro     2355:                TD(r) = ndl_weight(r);
                   2356:        }
1.1       noro     2357: }
                   2358:
1.61      noro     2359: DL ndltodl(int n,UINT *ndl)
1.1       noro     2360: {
                   2361:        DL dl;
                   2362:        int *d;
1.57      noro     2363:        int i,j,l,s,ord_l;
1.43      noro     2364:        struct order_pair *op;
1.1       noro     2365:
                   2366:        NEWDL(dl,n);
1.34      noro     2367:        dl->td = TD(ndl);
1.1       noro     2368:        d = dl->d;
1.43      noro     2369:        if ( nd_blockmask ) {
                   2370:                l = nd_blockmask->n;
                   2371:                op = nd_blockmask->order_pair;
                   2372:                for ( j = 0, s = 0; j < l; j++ ) {
                   2373:                        ord_l = op[j].length;
1.57      noro     2374:                        for ( i = 0; i < ord_l; i++, s++ ) d[s] = GET_EXP(ndl,s);
1.43      noro     2375:                }
                   2376:        } else {
1.56      noro     2377:                for ( i = 0; i < n; i++ ) d[i] = GET_EXP(ndl,i);
1.43      noro     2378:        }
1.1       noro     2379:        return dl;
                   2380: }
                   2381:
1.61      noro     2382: void ndl_print(UINT *dl)
1.1       noro     2383: {
                   2384:        int n;
1.57      noro     2385:        int i,j,l,ord_l,s,s0;
1.43      noro     2386:        struct order_pair *op;
1.1       noro     2387:
                   2388:        n = nd_nvar;
                   2389:        printf("<<");
1.43      noro     2390:        if ( nd_blockmask ) {
                   2391:                l = nd_blockmask->n;
                   2392:                op = nd_blockmask->order_pair;
                   2393:                for ( j = 0, s = s0 = 0; j < l; j++ ) {
                   2394:                        ord_l = op[j].length;
1.57      noro     2395:                        for ( i = 0; i < ord_l; i++, s++ )
                   2396:                                printf(s==n-1?"%d":"%d,",GET_EXP(dl,s));
1.43      noro     2397:                }
                   2398:        } else {
1.56      noro     2399:                for ( i = 0; i < n; i++ ) printf(i==n-1?"%d":"%d,",GET_EXP(dl,i));
1.43      noro     2400:        }
1.1       noro     2401:        printf(">>");
                   2402: }
                   2403:
                   2404: void nd_print(ND p)
                   2405: {
                   2406:        NM m;
                   2407:
                   2408:        if ( !p )
                   2409:                printf("0\n");
                   2410:        else {
                   2411:                for ( m = BDY(p); m; m = NEXT(m) ) {
1.71      noro     2412:                        if ( CM(m) & 0x80000000 ) printf("+@_%d*",IFTOF(CM(m)));
                   2413:                        else printf("+%d*",CM(m));
1.14      noro     2414:                        ndl_print(DL(m));
1.1       noro     2415:                }
                   2416:                printf("\n");
                   2417:        }
                   2418: }
                   2419:
1.112   ! noro     2420: void nd_print_z(ND p)
1.16      noro     2421: {
                   2422:        NM m;
                   2423:
                   2424:        if ( !p )
                   2425:                printf("0\n");
                   2426:        else {
                   2427:                for ( m = BDY(p); m; m = NEXT(m) ) {
                   2428:                        printf("+");
1.112   ! noro     2429:                        printz(CZ(m));
1.16      noro     2430:                        printf("*");
                   2431:                        ndl_print(DL(m));
                   2432:                }
                   2433:                printf("\n");
                   2434:        }
                   2435: }
                   2436:
1.1       noro     2437: void ndp_print(ND_pairs d)
                   2438: {
                   2439:        ND_pairs t;
                   2440:
1.34      noro     2441:        for ( t = d; t; t = NEXT(t) ) printf("%d,%d ",t->i1,t->i2);
1.1       noro     2442:        printf("\n");
                   2443: }
                   2444:
1.20      noro     2445: void nd_removecont(int mod,ND p)
1.16      noro     2446: {
                   2447:        int i,n;
1.112   ! noro     2448:        Z *w;
        !          2449:        Z dvr,t;
1.16      noro     2450:        NM m;
1.21      noro     2451:        struct oVECT v;
1.16      noro     2452:
1.71      noro     2453:        if ( mod == -1 ) nd_mul_c(mod,p,_invsf(HCM(p)));
                   2454:        else if ( mod ) nd_mul_c(mod,p,invm(HCM(p),mod));
1.20      noro     2455:        else {
                   2456:                for ( m = BDY(p), n = 0; m; m = NEXT(m), n++ );
1.112   ! noro     2457:                w = (Z *)ALLOCA(n*sizeof(Z));
1.21      noro     2458:                v.len = n;
                   2459:                v.body = (pointer *)w;
1.112   ! noro     2460:                for ( m = BDY(p), i = 0; i < n; m = NEXT(m), i++ ) w[i] = CZ(m);
1.21      noro     2461:                removecont_array(w,n);
1.112   ! noro     2462:                for ( m = BDY(p), i = 0; i < n; m = NEXT(m), i++ ) CZ(m) = w[i];
1.16      noro     2463:        }
                   2464: }
                   2465:
1.21      noro     2466: void nd_removecont2(ND p1,ND p2)
                   2467: {
                   2468:        int i,n1,n2,n;
1.112   ! noro     2469:        Z *w;
1.21      noro     2470:        NM m;
                   2471:        struct oVECT v;
                   2472:
                   2473:        if ( !p1 ) {
                   2474:                nd_removecont(0,p2); return;
                   2475:        } else if ( !p2 ) {
                   2476:                nd_removecont(0,p1); return;
                   2477:        }
                   2478:        n1 = nd_length(p1);
                   2479:        n2 = nd_length(p2);
                   2480:        n = n1+n2;
1.112   ! noro     2481:        w = (Z *)ALLOCA(n*sizeof(Z));
1.21      noro     2482:        v.len = n;
                   2483:        v.body = (pointer *)w;
1.112   ! noro     2484:        for ( m = BDY(p1), i = 0; i < n1; m = NEXT(m), i++ ) w[i] = CZ(m);
        !          2485:        for ( m = BDY(p2); i < n; m = NEXT(m), i++ ) w[i] = CZ(m);
1.21      noro     2486:        removecont_array(w,n);
1.112   ! noro     2487:        for ( m = BDY(p1), i = 0; i < n1; m = NEXT(m), i++ ) CZ(m) = w[i];
        !          2488:        for ( m = BDY(p2); i < n; m = NEXT(m), i++ ) CZ(m) = w[i];
1.21      noro     2489: }
                   2490:
1.20      noro     2491: void ndv_removecont(int mod,NDV p)
1.16      noro     2492: {
                   2493:        int i,len;
1.112   ! noro     2494:        Z *w;
        !          2495:        Z dvr,t;
1.16      noro     2496:        NMV m;
                   2497:
1.71      noro     2498:        if ( mod == -1 )
                   2499:                ndv_mul_c(mod,p,_invsf(HCM(p)));
                   2500:        else if ( mod )
1.20      noro     2501:                ndv_mul_c(mod,p,invm(HCM(p),mod));
                   2502:        else {
                   2503:                len = p->len;
1.112   ! noro     2504:                w = (Z *)ALLOCA(len*sizeof(Z));
        !          2505:                for ( m = BDY(p), i = 0; i < len; NMV_ADV(m), i++ ) w[i] = CZ(m);
        !          2506:                removecont_array(w,len);
        !          2507:                for ( m = BDY(p), i = 0; i < len; NMV_ADV(m), i++ ) CZ(m) = w[i];
1.16      noro     2508:        }
1.21      noro     2509: }
                   2510:
1.61      noro     2511: void ndv_homogenize(NDV p,int obpe,int oadv,EPOS oepos)
                   2512: {
                   2513:        int len,i,max;
                   2514:        NMV m,mr0,mr,t;
                   2515:
                   2516:        len = p->len;
                   2517:        for ( m = BDY(p), i = 0, max = 0; i < len; NMV_OADV(m), i++ )
                   2518:                max = MAX(max,TD(DL(m)));
                   2519:        mr0 = nmv_adv>oadv?(NMV)REALLOC(BDY(p),len*nmv_adv):BDY(p);
                   2520:        m = (NMV)((char *)mr0+(len-1)*oadv);
                   2521:        mr = (NMV)((char *)mr0+(len-1)*nmv_adv);
                   2522:        t = (NMV)ALLOCA(nmv_adv);
                   2523:        for ( i = 0; i < len; i++, NMV_OPREV(m), NMV_PREV(mr) ) {
                   2524:                ndl_homogenize(DL(m),DL(t),obpe,oepos,max);
1.112   ! noro     2525:                CZ(mr) = CZ(m);
1.61      noro     2526:                ndl_copy(DL(t),DL(mr));
                   2527:        }
                   2528:        NV(p)++;
                   2529:        BDY(p) = mr0;
                   2530: }
                   2531:
1.45      noro     2532: void ndv_dehomogenize(NDV p,struct order_spec *ord)
1.23      noro     2533: {
1.45      noro     2534:        int i,j,adj,len,newnvar,newwpd,newadv,newexporigin;
1.112   ! noro     2535:        Z *w;
        !          2536:        Z dvr,t;
1.23      noro     2537:        NMV m,r;
                   2538:
                   2539:        len = p->len;
                   2540:        newnvar = nd_nvar-1;
1.48      noro     2541:        newexporigin = nd_get_exporigin(ord);
1.45      noro     2542:        newwpd = newnvar/nd_epw+(newnvar%nd_epw?1:0)+newexporigin;
1.23      noro     2543:        for ( m = BDY(p), i = 0; i < len; NMV_ADV(m), i++ )
1.34      noro     2544:                ndl_dehomogenize(DL(m));
1.23      noro     2545:        if ( newwpd != nd_wpd ) {
1.90      noro     2546:                newadv = ROUND_FOR_ALIGN(sizeof(struct oNMV)+(newwpd-1)*sizeof(UINT));
1.61      noro     2547:                for ( m = r = BDY(p), i = 0; i < len; NMV_ADV(m), NDV_NADV(r), i++ ) {
1.112   ! noro     2548:                        CZ(r) = CZ(m);
1.45      noro     2549:                        for ( j = 0; j < newexporigin; j++ ) DL(r)[j] = DL(m)[j];
                   2550:                        adj = nd_exporigin-newexporigin;
                   2551:                        for ( ; j < newwpd; j++ ) DL(r)[j] = DL(m)[j+adj];
1.23      noro     2552:                }
                   2553:        }
                   2554:        NV(p)--;
                   2555: }
                   2556:
1.112   ! noro     2557: int comp_z(Z *a,Z *b)
        !          2558: {
        !          2559:        return cmpz(*a,*b);
        !          2560: }
        !          2561:
        !          2562: Z zgcdv(VECT a)
        !          2563: {
        !          2564:        int i,j,n,nz;
        !          2565:        Z g,gt,q,r;
        !          2566:        Z *c;
        !          2567:
        !          2568:        n = a->len;
        !          2569:        c = (Z *)ALLOCA(n*sizeof(Z));
        !          2570:        for ( i = 0; i < n; i++ )
        !          2571:                c[i] = (Z)a->body[i];
        !          2572:        qsort(c,n,sizeof(Z),(int (*) (const void *,const void *))comp_z);
        !          2573:        for ( ; n && ! *c; n--, c++ );
        !          2574:
        !          2575:        if ( !n )
        !          2576:                return 0;
        !          2577:        else if ( n == 1 )
        !          2578:                return absz(c[0]);
        !          2579:
        !          2580:        g = gcdz(c[0],c[1]);
        !          2581:        for ( i = 2; i < n; i++ ) {
        !          2582:                q = divz(c[i],g,&r);
        !          2583:                gt = gcdz(g,r);
        !          2584:                if ( !cmpz(g,gt) ) {
        !          2585:                        for ( j = i+1; j < n; j++ ) {
        !          2586:                                q = divz(c[j],g,&r); c[j] = r;
        !          2587:                        }
        !          2588:                } else
        !          2589:                        g = gt;
        !          2590:        }
        !          2591:        return absz(g);
        !          2592: }
        !          2593:
        !          2594: Z zgcdv_estimate(VECT a)
        !          2595: {
        !          2596:        int n,i,m;
        !          2597:        Z s,t,u,g;
        !          2598:        Z *q;
        !          2599:
        !          2600:        n = a->len; q = (Z *)a->body;
        !          2601:        if ( n == 1 )
        !          2602:                return absz(q[0]);
        !          2603:
        !          2604:        m = n/2;
        !          2605:        for ( i = 0 , s = 0; i < m; i++ )
        !          2606:                s = sgnz(q[i])>=0 ? addz(s,q[i]) : subz(s,q[i]);
        !          2607:        for ( t = 0; i < n; i++ )
        !          2608:                t = sgnz(q[i])>=0 ? addz(t,q[i]) : subz(t,q[i]);
        !          2609:        g = gcdz(s,t);
        !          2610:        return absz(g);
        !          2611: }
        !          2612:
        !          2613: void removecont_array(Z *c,int n)
1.21      noro     2614: {
                   2615:        struct oVECT v;
1.112   ! noro     2616:        Z d0,d1,a,u,u1,gcd;
1.110     noro     2617:        int i,j;
1.112   ! noro     2618:        Z q0,q1;
        !          2619:        Z *q,*r;
1.21      noro     2620:
1.112   ! noro     2621:        q = (Z *)ALLOCA(n*sizeof(Z));
        !          2622:        r = (Z *)ALLOCA(n*sizeof(Z));
1.21      noro     2623:        v.id = O_VECT; v.len = n; v.body = (pointer *)c;
1.112   ! noro     2624:        d0 = zgcdv_estimate(&v);
        !          2625:        /* c[i] = d0*q[i]+r[i] */
        !          2626:        for ( i = 0; i < n; i++ )
        !          2627:                q[i] = divz(c[i],d0,&r[i]);
1.34      noro     2628:        for ( i = 0; i < n; i++ ) if ( r[i] ) break;
1.21      noro     2629:        if ( i < n ) {
                   2630:                v.id = O_VECT; v.len = n; v.body = (pointer *)r;
1.112   ! noro     2631:                /* d1 = GCD(r[0],...,r[n-1]) */
        !          2632:                d1 = zgcdv(&v);
        !          2633:                /* q0 = d0/GCD(d0,d1) */
        !          2634:                gcd = gcdz_cofactor(d0,d1,&q0,&q1);
1.21      noro     2635:                for ( i = 0; i < n; i++ ) {
1.112   ! noro     2636:                        /* q[i] = d0/gcd*q[i]+r[i]/gcd */
        !          2637:                        q[i] = addz(mulz(q0,q[i]),divsz(r[i],gcd));
1.21      noro     2638:                }
                   2639:        }
1.34      noro     2640:        for ( i = 0; i < n; i++ ) c[i] = q[i];
1.16      noro     2641: }
                   2642:
1.19      noro     2643: void nd_mul_c(int mod,ND p,int mul)
1.1       noro     2644: {
                   2645:        NM m;
                   2646:        int c,c1;
                   2647:
1.34      noro     2648:        if ( !p ) return;
1.71      noro     2649:        if ( mod == -1 )
                   2650:                for ( m = BDY(p); m; m = NEXT(m) )
                   2651:                        CM(m) = _mulsf(CM(m),mul);
                   2652:        else
                   2653:                for ( m = BDY(p); m; m = NEXT(m) ) {
                   2654:                        c1 = CM(m); DMAR(c1,mul,0,mod,c); CM(m) = c;
                   2655:                }
1.1       noro     2656: }
                   2657:
1.112   ! noro     2658: void nd_mul_c_z(ND p,Z mul)
1.16      noro     2659: {
                   2660:        NM m;
1.112   ! noro     2661:        Z c;
1.16      noro     2662:
1.112   ! noro     2663:        if ( !p || uniz(mul) ) return;
1.16      noro     2664:        for ( m = BDY(p); m; m = NEXT(m) ) {
1.112   ! noro     2665:                CZ(m) = mulz(CZ(m),mul);
1.16      noro     2666:        }
                   2667: }
                   2668:
1.61      noro     2669: void nd_mul_c_p(VL vl,ND p,P mul)
                   2670: {
                   2671:        NM m;
                   2672:        P c;
                   2673:
                   2674:        if ( !p ) return;
                   2675:        for ( m = BDY(p); m; m = NEXT(m) ) {
                   2676:                mulp(vl,CP(m),mul,&c); CP(m) = c;
                   2677:        }
                   2678: }
                   2679:
1.1       noro     2680: void nd_free(ND p)
                   2681: {
                   2682:        NM t,s;
                   2683:
1.34      noro     2684:        if ( !p ) return;
1.1       noro     2685:        t = BDY(p);
                   2686:        while ( t ) {
                   2687:                s = NEXT(t);
                   2688:                FREENM(t);
                   2689:                t = s;
                   2690:        }
                   2691:        FREEND(p);
                   2692: }
                   2693:
1.23      noro     2694: void ndv_free(NDV p)
                   2695: {
                   2696:        GC_free(BDY(p));
                   2697: }
                   2698:
1.61      noro     2699: void nd_append_red(UINT *d,int i)
1.1       noro     2700: {
1.13      noro     2701:        RHist m,m0;
1.1       noro     2702:        int h;
                   2703:
1.13      noro     2704:        NEWRHist(m);
1.34      noro     2705:        h = ndl_hash_value(d);
1.13      noro     2706:        m->index = i;
1.14      noro     2707:        ndl_copy(d,DL(m));
1.1       noro     2708:        NEXT(m) = nd_red[h];
                   2709:        nd_red[h] = m;
                   2710: }
                   2711:
1.61      noro     2712: UINT *ndv_compute_bound(NDV p)
1.1       noro     2713: {
1.61      noro     2714:        UINT *d1,*d2,*t;
                   2715:        UINT u;
1.57      noro     2716:        int i,j,k,l,len,ind;
1.45      noro     2717:        NMV m;
1.1       noro     2718:
                   2719:        if ( !p )
                   2720:                return 0;
1.61      noro     2721:        d1 = (UINT *)ALLOCA(nd_wpd*sizeof(UINT));
                   2722:        d2 = (UINT *)ALLOCA(nd_wpd*sizeof(UINT));
1.45      noro     2723:        len = LEN(p);
                   2724:        m = BDY(p); ndl_copy(DL(m),d1); NMV_ADV(m);
                   2725:        for ( i = 1; i < len; i++, NMV_ADV(m) ) {
1.14      noro     2726:                ndl_lcm(DL(m),d1,d2);
1.1       noro     2727:                t = d1; d1 = d2; d2 = t;
                   2728:        }
1.12      noro     2729:        l = nd_nvar+31;
1.61      noro     2730:        t = (UINT *)MALLOC_ATOMIC(l*sizeof(UINT));
1.57      noro     2731:        for ( i = nd_exporigin, ind = 0; i < nd_wpd; i++ ) {
                   2732:                u = d1[i];
                   2733:                k = (nd_epw-1)*nd_bpe;
                   2734:                for ( j = 0; j < nd_epw; j++, k -= nd_bpe, ind++ )
                   2735:                        t[ind] = (u>>k)&nd_mask0;
                   2736:        }
                   2737:        for ( ; ind < l; ind++ ) t[ind] = 0;
1.1       noro     2738:        return t;
                   2739: }
                   2740:
1.99      noro     2741: UINT *nd_compute_bound(ND p)
                   2742: {
                   2743:        UINT *d1,*d2,*t;
                   2744:        UINT u;
                   2745:        int i,j,k,l,len,ind;
                   2746:        NM m;
                   2747:
                   2748:        if ( !p )
                   2749:                return 0;
                   2750:        d1 = (UINT *)ALLOCA(nd_wpd*sizeof(UINT));
                   2751:        d2 = (UINT *)ALLOCA(nd_wpd*sizeof(UINT));
                   2752:        len = LEN(p);
                   2753:        m = BDY(p); ndl_copy(DL(m),d1); m = NEXT(m);
                   2754:        for ( m = NEXT(m); m; m = NEXT(m) ) {
                   2755:                ndl_lcm(DL(m),d1,d2);
                   2756:                t = d1; d1 = d2; d2 = t;
                   2757:        }
                   2758:        l = nd_nvar+31;
                   2759:        t = (UINT *)MALLOC_ATOMIC(l*sizeof(UINT));
                   2760:        for ( i = nd_exporigin, ind = 0; i < nd_wpd; i++ ) {
                   2761:                u = d1[i];
                   2762:                k = (nd_epw-1)*nd_bpe;
                   2763:                for ( j = 0; j < nd_epw; j++, k -= nd_bpe, ind++ )
                   2764:                        t[ind] = (u>>k)&nd_mask0;
                   2765:        }
                   2766:        for ( ; ind < l; ind++ ) t[ind] = 0;
                   2767:        return t;
                   2768: }
                   2769:
1.48      noro     2770: int nd_get_exporigin(struct order_spec *ord)
                   2771: {
1.51      noro     2772:        switch ( ord->id ) {
1.96      noro     2773:                case 0: case 2:
1.48      noro     2774:                        return 1;
1.41      noro     2775:                case 1:
                   2776:                        /* block order */
1.43      noro     2777:                        /* d[0]:weight d[1]:w0,...,d[nd_exporigin-1]:w(n-1) */
1.48      noro     2778:                        return ord->ord.block.length+1;
1.96      noro     2779:                case 3:
                   2780:                        error("nd_get_exporigin : composite order is not supported yet.");
1.41      noro     2781:        }
1.48      noro     2782: }
                   2783:
1.61      noro     2784: void nd_setup_parameters(int nvar,int max) {
1.79      noro     2785:        int i,j,n,elen,ord_o,ord_l,l,s,wpd;
1.57      noro     2786:        struct order_pair *op;
1.48      noro     2787:
1.73      noro     2788:        nd_nvar = nvar;
1.79      noro     2789:        if ( max ) {
1.83      noro     2790:                /* XXX */
                   2791:                if ( do_weyl ) nd_bpe = 32;
                   2792:                else if ( max < 2 ) nd_bpe = 1;
1.79      noro     2793:                else if ( max < 4 ) nd_bpe = 2;
                   2794:                else if ( max < 8 ) nd_bpe = 3;
                   2795:                else if ( max < 16 ) nd_bpe = 4;
                   2796:                else if ( max < 32 ) nd_bpe = 5;
                   2797:                else if ( max < 64 ) nd_bpe = 6;
                   2798:                else if ( max < 256 ) nd_bpe = 8;
                   2799:                else if ( max < 1024 ) nd_bpe = 10;
                   2800:                else if ( max < 65536 ) nd_bpe = 16;
                   2801:                else nd_bpe = 32;
                   2802:        }
1.61      noro     2803:        nd_epw = (sizeof(UINT)*8)/nd_bpe;
1.48      noro     2804:        elen = nd_nvar/nd_epw+(nd_nvar%nd_epw?1:0);
                   2805:        nd_exporigin = nd_get_exporigin(nd_ord);
1.79      noro     2806:        wpd = nd_exporigin+elen;
                   2807:        if ( wpd != nd_wpd ) {
                   2808:                nd_free_private_storage();
                   2809:                nd_wpd = wpd;
                   2810:        }
1.1       noro     2811:        if ( nd_bpe < 32 ) {
                   2812:                nd_mask0 = (1<<nd_bpe)-1;
                   2813:        } else {
                   2814:                nd_mask0 = 0xffffffff;
                   2815:        }
                   2816:        bzero(nd_mask,sizeof(nd_mask));
                   2817:        nd_mask1 = 0;
                   2818:        for ( i = 0; i < nd_epw; i++ ) {
                   2819:                nd_mask[nd_epw-i-1] = (nd_mask0<<(i*nd_bpe));
                   2820:                nd_mask1 |= (1<<(nd_bpe-1))<<(i*nd_bpe);
                   2821:        }
1.90      noro     2822:        nmv_adv = ROUND_FOR_ALIGN(sizeof(struct oNMV)+(nd_wpd-1)*sizeof(UINT));
1.57      noro     2823:        nd_epos = nd_create_epos(nd_ord);
1.43      noro     2824:        nd_blockmask = nd_create_blockmask(nd_ord);
1.96      noro     2825:        nd_work_vector = (int *)REALLOC(nd_work_vector,nd_nvar*sizeof(int));
1.1       noro     2826: }
                   2827:
1.103     noro     2828: ND_pairs nd_reconstruct(int trace,ND_pairs d)
1.1       noro     2829: {
1.37      noro     2830:        int i,obpe,oadv,h;
1.71      noro     2831:        static NM prev_nm_free_list;
                   2832:        static ND_pairs prev_ndp_free_list;
1.13      noro     2833:        RHist mr0,mr;
                   2834:        RHist r;
1.37      noro     2835:        RHist *old_red;
1.71      noro     2836:        ND_pairs s0,s,t;
1.43      noro     2837:        EPOS oepos;
1.15      noro     2838:
1.1       noro     2839:        obpe = nd_bpe;
1.11      noro     2840:        oadv = nmv_adv;
1.43      noro     2841:        oepos = nd_epos;
1.63      noro     2842:        if ( obpe < 2 ) nd_bpe = 2;
                   2843:        else if ( obpe < 3 ) nd_bpe = 3;
1.62      noro     2844:        else if ( obpe < 4 ) nd_bpe = 4;
1.63      noro     2845:        else if ( obpe < 5 ) nd_bpe = 5;
1.34      noro     2846:        else if ( obpe < 6 ) nd_bpe = 6;
                   2847:        else if ( obpe < 8 ) nd_bpe = 8;
1.63      noro     2848:        else if ( obpe < 10 ) nd_bpe = 10;
1.34      noro     2849:        else if ( obpe < 16 ) nd_bpe = 16;
                   2850:        else if ( obpe < 32 ) nd_bpe = 32;
                   2851:        else error("nd_reconstruct : exponent too large");
1.5       noro     2852:
1.73      noro     2853:        nd_setup_parameters(nd_nvar,0);
1.1       noro     2854:        prev_nm_free_list = _nm_free_list;
                   2855:        prev_ndp_free_list = _ndp_free_list;
                   2856:        _nm_free_list = 0;
                   2857:        _ndp_free_list = 0;
1.53      noro     2858:        for ( i = nd_psn-1; i >= 0; i-- ) ndv_realloc(nd_ps[i],obpe,oadv,oepos);
                   2859:        if ( trace )
                   2860:                for ( i = nd_psn-1; i >= 0; i-- )
                   2861:                        ndv_realloc(nd_ps_trace[i],obpe,oadv,oepos);
1.1       noro     2862:        s0 = 0;
                   2863:        for ( t = d; t; t = NEXT(t) ) {
                   2864:                NEXTND_pairs(s0,s);
                   2865:                s->i1 = t->i1;
                   2866:                s->i2 = t->i2;
1.14      noro     2867:                SG(s) = SG(t);
1.61      noro     2868:                ndl_reconstruct(LCM(t),LCM(s),obpe,oepos);
1.1       noro     2869:        }
1.37      noro     2870:
                   2871:        old_red = (RHist *)ALLOCA(REDTAB_LEN*sizeof(RHist));
1.6       noro     2872:        for ( i = 0; i < REDTAB_LEN; i++ ) {
1.37      noro     2873:                old_red[i] = nd_red[i];
                   2874:                nd_red[i] = 0;
                   2875:        }
                   2876:        for ( i = 0; i < REDTAB_LEN; i++ )
                   2877:                for ( r = old_red[i]; r; r = NEXT(r) ) {
                   2878:                        NEWRHist(mr);
1.13      noro     2879:                        mr->index = r->index;
1.20      noro     2880:                        SG(mr) = SG(r);
1.61      noro     2881:                        ndl_reconstruct(DL(r),DL(mr),obpe,oepos);
1.37      noro     2882:                        h = ndl_hash_value(DL(mr));
                   2883:                        NEXT(mr) = nd_red[h];
                   2884:                        nd_red[h] = mr;
1.6       noro     2885:                }
1.37      noro     2886:        for ( i = 0; i < REDTAB_LEN; i++ ) old_red[i] = 0;
                   2887:        old_red = 0;
1.11      noro     2888:        for ( i = 0; i < nd_psn; i++ ) {
1.20      noro     2889:                NEWRHist(r); SG(r) = SG(nd_psh[i]);
1.61      noro     2890:                ndl_reconstruct(DL(nd_psh[i]),DL(r),obpe,oepos);
1.13      noro     2891:                nd_psh[i] = r;
1.11      noro     2892:        }
1.1       noro     2893:        if ( s0 ) NEXT(s) = 0;
                   2894:        prev_nm_free_list = 0;
                   2895:        prev_ndp_free_list = 0;
1.71      noro     2896: #if 0
1.1       noro     2897:        GC_gcollect();
1.71      noro     2898: #endif
1.1       noro     2899:        return s0;
                   2900: }
                   2901:
1.61      noro     2902: void ndl_reconstruct(UINT *d,UINT *r,int obpe,EPOS oepos)
1.1       noro     2903: {
1.57      noro     2904:        int n,i,ei,oepw,omask0,j,s,ord_l,l;
1.43      noro     2905:        struct order_pair *op;
1.1       noro     2906:
                   2907:        n = nd_nvar;
1.61      noro     2908:        oepw = (sizeof(UINT)*8)/obpe;
1.43      noro     2909:        omask0 = (1<<obpe)-1;
1.34      noro     2910:        TD(r) = TD(d);
1.41      noro     2911:        for ( i = nd_exporigin; i < nd_wpd; i++ ) r[i] = 0;
1.43      noro     2912:        if ( nd_blockmask ) {
                   2913:                l = nd_blockmask->n;
                   2914:                op = nd_blockmask->order_pair;
                   2915:                for ( i = 1; i < nd_exporigin; i++ )
                   2916:                        r[i] = d[i];
                   2917:                for ( j = 0, s = 0; j < l; j++ ) {
                   2918:                        ord_l = op[j].length;
1.57      noro     2919:                        for ( i = 0; i < ord_l; i++, s++ ) {
                   2920:                                ei =  GET_EXP_OLD(d,s);
                   2921:                                PUT_EXP(r,s,ei);
                   2922:                        }
1.1       noro     2923:                }
1.43      noro     2924:        } else {
1.56      noro     2925:                for ( i = 0; i < n; i++ ) {
                   2926:                        ei = GET_EXP_OLD(d,i);
                   2927:                        PUT_EXP(r,i,ei);
                   2928:                }
1.1       noro     2929:        }
                   2930: }
1.3       noro     2931:
1.6       noro     2932: ND nd_copy(ND p)
                   2933: {
                   2934:        NM m,mr,mr0;
1.41      noro     2935:        int c,n;
1.6       noro     2936:        ND r;
                   2937:
                   2938:        if ( !p )
                   2939:                return 0;
                   2940:        else {
                   2941:                for ( mr0 = 0, m = BDY(p); m; m = NEXT(m) ) {
                   2942:                        NEXTNM(mr0,mr);
1.14      noro     2943:                        CM(mr) = CM(m);
                   2944:                        ndl_copy(DL(m),DL(mr));
1.6       noro     2945:                }
                   2946:                NEXT(mr) = 0;
1.31      noro     2947:                MKND(NV(p),mr0,LEN(p),r);
1.14      noro     2948:                SG(r) = SG(p);
1.6       noro     2949:                return r;
                   2950:        }
                   2951: }
                   2952:
1.53      noro     2953: int nd_sp(int mod,int trace,ND_pairs p,ND *rp)
1.11      noro     2954: {
                   2955:        NM m;
                   2956:        NDV p1,p2;
                   2957:        ND t1,t2;
1.61      noro     2958:        UINT *lcm;
1.31      noro     2959:        int td;
1.11      noro     2960:
1.77      noro     2961:        if ( !mod && nd_demand ) {
                   2962:                p1 = ndv_load(p->i1); p2 = ndv_load(p->i2);
1.53      noro     2963:        } else {
1.77      noro     2964:                if ( trace ) {
                   2965:                        p1 = nd_ps_trace[p->i1]; p2 = nd_ps_trace[p->i2];
                   2966:                } else {
                   2967:                        p1 = nd_ps[p->i1]; p2 = nd_ps[p->i2];
                   2968:                }
1.20      noro     2969:        }
1.34      noro     2970:        lcm = LCM(p);
1.11      noro     2971:        NEWNM(m);
1.112   ! noro     2972:        CZ(m) = HCZ(p2);
1.34      noro     2973:        ndl_sub(lcm,HDL(p1),DL(m));
1.56      noro     2974:        if ( ndl_check_bound2(p->i1,DL(m)) )
                   2975:                return 0;
1.55      noro     2976:        t1 = ndv_mul_nm(mod,m,p1);
1.71      noro     2977:        if ( mod == -1 ) CM(m) = _chsgnsf(HCM(p1));
                   2978:        else if ( mod ) CM(m) = mod-HCM(p1);
1.112   ! noro     2979:        else CZ(m) = chsgnz(HCZ(p1));
1.34      noro     2980:        ndl_sub(lcm,HDL(p2),DL(m));
1.14      noro     2981:        if ( ndl_check_bound2(p->i2,DL(m)) ) {
1.11      noro     2982:                nd_free(t1);
                   2983:                return 0;
                   2984:        }
1.55      noro     2985:        t2 = ndv_mul_nm(mod,m,p2);
1.31      noro     2986:        *rp = nd_add(mod,t1,t2);
1.11      noro     2987:        FREENM(m);
                   2988:        return 1;
                   2989: }
                   2990:
1.19      noro     2991: void ndv_mul_c(int mod,NDV p,int mul)
1.11      noro     2992: {
                   2993:        NMV m;
                   2994:        int c,c1,len,i;
                   2995:
1.34      noro     2996:        if ( !p ) return;
1.14      noro     2997:        len = LEN(p);
1.71      noro     2998:        if ( mod == -1 )
                   2999:                for ( m = BDY(p), i = 0; i < len; i++, NMV_ADV(m) )
                   3000:                        CM(m) = _mulsf(CM(m),mul);
                   3001:        else
                   3002:                for ( m = BDY(p), i = 0; i < len; i++, NMV_ADV(m) ) {
                   3003:                        c1 = CM(m); DMAR(c1,mul,0,mod,c); CM(m) = c;
                   3004:                }
1.11      noro     3005: }
                   3006:
1.112   ! noro     3007: void ndv_mul_c_z(NDV p,Z mul)
1.16      noro     3008: {
                   3009:        NMV m;
1.112   ! noro     3010:        Z c;
1.16      noro     3011:        int len,i;
                   3012:
1.34      noro     3013:        if ( !p ) return;
1.16      noro     3014:        len = LEN(p);
                   3015:        for ( m = BDY(p), i = 0; i < len; i++, NMV_ADV(m) ) {
1.112   ! noro     3016:                CZ(m) = mulz(CZ(m),mul);
1.16      noro     3017:        }
                   3018: }
                   3019:
1.55      noro     3020: ND weyl_ndv_mul_nm(int mod,NM m0,NDV p) {
                   3021:        int n2,i,j,l,n,tlen;
1.61      noro     3022:        UINT *d0;
1.55      noro     3023:        NM *tab,*psum;
                   3024:        ND s,r;
                   3025:        NM t;
                   3026:        NMV m1;
                   3027:
                   3028:        if ( !p ) return 0;
                   3029:        n = NV(p); n2 = n>>1;
                   3030:        d0 = DL(m0);
                   3031:        l = LEN(p);
                   3032:        for ( i = 0, tlen = 1; i < n2; i++ ) tlen *= (GET_EXP(d0,n2+i)+1);
                   3033:        tab = (NM *)ALLOCA(tlen*sizeof(NM));
                   3034:        psum = (NM *)ALLOCA(tlen*sizeof(NM));
                   3035:        for ( i = 0; i < tlen; i++ ) psum[i] = 0;
1.56      noro     3036:        m1 = (NMV)(((char *)BDY(p))+nmv_adv*(l-1));
                   3037:        for ( i = l-1; i >= 0; i--, NMV_PREV(m1) ) {
1.55      noro     3038:                /* m0(NM) * m1(NMV) => tab(NM) */
1.56      noro     3039:                weyl_mul_nm_nmv(n,mod,m0,m1,tab,tlen);
1.55      noro     3040:                for ( j = 0; j < tlen; j++ ) {
                   3041:                        if ( tab[j] ) {
                   3042:                                NEXT(tab[j]) = psum[j]; psum[j] = tab[j];
                   3043:                        }
                   3044:                }
                   3045:        }
                   3046:        for ( i = tlen-1, r = 0; i >= 0; i-- )
                   3047:                if ( psum[i] ) {
                   3048:                        for ( j = 0, t = psum[i]; t; t = NEXT(t), j++ );
                   3049:                        MKND(n,psum[i],j,s);
                   3050:                        r = nd_add(mod,r,s);
                   3051:                }
1.56      noro     3052:        if ( r ) SG(r) = SG(p)+TD(d0);
                   3053:        return r;
1.55      noro     3054: }
                   3055:
1.56      noro     3056: /* product of monomials */
                   3057: /* XXX block order is not handled correctly */
                   3058:
1.55      noro     3059: void weyl_mul_nm_nmv(int n,int mod,NM m0,NMV m1,NM *tab,int tlen)
                   3060: {
1.56      noro     3061:        int i,n2,j,s,curlen,homo,h,a,b,k,l,u,min;
1.61      noro     3062:        UINT *d0,*d1,*d,*dt,*ctab;
1.112   ! noro     3063:        Z *ctab_z;
        !          3064:        Z q,q1;
1.61      noro     3065:        UINT c0,c1,c;
1.55      noro     3066:        NM *p;
                   3067:        NM m,t;
                   3068:
                   3069:        for ( i = 0; i < tlen; i++ ) tab[i] = 0;
                   3070:        if ( !m0 || !m1 ) return;
                   3071:        d0 = DL(m0); d1 = DL(m1); n2 = n>>1;
                   3072:        NEWNM(m); d = DL(m);
1.56      noro     3073:        if ( mod ) {
                   3074:                c0 = CM(m0); c1 = CM(m1); DMAR(c0,c1,0,mod,c); CM(m) = c;
                   3075:        } else
1.112   ! noro     3076:                CZ(m) = mulz(CZ(m0),CZ(m1));
1.55      noro     3077:        for ( i = 0; i < nd_wpd; i++ ) d[i] = 0;
                   3078:        homo = n&1 ? 1 : 0;
                   3079:        if ( homo ) {
                   3080:                /* offset of h-degree */
                   3081:                h = GET_EXP(d0,n-1)+GET_EXP(d1,n-1);
                   3082:                PUT_EXP(DL(m),n-1,h);
                   3083:                TD(DL(m)) = h;
1.61      noro     3084:                if ( nd_blockmask ) ndl_weight_mask(DL(m));
1.55      noro     3085:        }
                   3086:        tab[0] = m;
                   3087:        NEWNM(m); d = DL(m);
1.57      noro     3088:        for ( i = 0, curlen = 1; i < n2; i++ ) {
1.55      noro     3089:                a = GET_EXP(d0,i); b = GET_EXP(d1,n2+i);
                   3090:                k = GET_EXP(d0,n2+i); l = GET_EXP(d1,i);
                   3091:                /* xi^a*(Di^k*xi^l)*Di^b */
                   3092:                a += l; b += k;
1.56      noro     3093:                s = MUL_WEIGHT(a,i)+MUL_WEIGHT(b,n2+i);
1.55      noro     3094:                if ( !k || !l ) {
                   3095:                        for ( j = 0; j < curlen; j++ )
1.56      noro     3096:                                if ( t = tab[j] ) {
                   3097:                                        dt = DL(t);
                   3098:                                        PUT_EXP(dt,i,a); PUT_EXP(dt,n2+i,b); TD(dt) += s;
1.61      noro     3099:                                        if ( nd_blockmask ) ndl_weight_mask(dt);
1.55      noro     3100:                                }
                   3101:                        curlen *= k+1;
                   3102:                        continue;
                   3103:                }
                   3104:                min = MIN(k,l);
1.56      noro     3105:                if ( mod ) {
1.61      noro     3106:                        ctab = (UINT *)ALLOCA((min+1)*sizeof(UINT));
1.56      noro     3107:                        mkwcm(k,l,mod,ctab);
                   3108:                } else {
1.112   ! noro     3109:                        ctab_z = (Z *)ALLOCA((min+1)*sizeof(Z));
        !          3110:                        mkwcz(k,l,ctab_z);
1.56      noro     3111:                }
1.57      noro     3112:                for ( j = min; j >= 0; j-- ) {
1.56      noro     3113:                        for ( u = 0; u < nd_wpd; u++ ) d[u] = 0;
1.55      noro     3114:                        PUT_EXP(d,i,a-j); PUT_EXP(d,n2+i,b-j);
1.56      noro     3115:                        h = MUL_WEIGHT(a-j,i)+MUL_WEIGHT(b-j,n2+i);
1.55      noro     3116:                        if ( homo ) {
                   3117:                                TD(d) = s;
1.56      noro     3118:                                PUT_EXP(d,n-1,s-h);
1.55      noro     3119:                        } else TD(d) = h;
1.61      noro     3120:                        if ( nd_blockmask ) ndl_weight_mask(d);
1.56      noro     3121:                        if ( mod ) c = ctab[j];
1.112   ! noro     3122:                        else q = ctab_z[j];
1.57      noro     3123:                        p = tab+curlen*j;
                   3124:                        if ( j == 0 ) {
                   3125:                                for ( u = 0; u < curlen; u++, p++ ) {
                   3126:                                        if ( tab[u] ) {
                   3127:                                                ndl_addto(DL(tab[u]),d);
                   3128:                                                if ( mod ) {
                   3129:                                                        c0 = CM(tab[u]); DMAR(c0,c,0,mod,c1); CM(tab[u]) = c1;
                   3130:                                                } else {
1.112   ! noro     3131:                                                        CZ(tab[u]) = mulz(CZ(tab[u]),q);
1.57      noro     3132:                                                }
                   3133:                                        }
1.56      noro     3134:                                }
1.57      noro     3135:                        } else {
                   3136:                                for ( u = 0; u < curlen; u++, p++ ) {
                   3137:                                        if ( tab[u] ) {
                   3138:                                                NEWNM(t);
                   3139:                                                ndl_add(DL(tab[u]),d,DL(t));
                   3140:                                                if ( mod ) {
                   3141:                                                        c0 = CM(tab[u]); DMAR(c0,c,0,mod,c1); CM(t) = c1;
                   3142:                                                } else
1.112   ! noro     3143:                                                        CZ(t) = mulz(CZ(tab[u]),q);
1.57      noro     3144:                                                *p = t;
                   3145:                                        }
1.55      noro     3146:                                }
                   3147:                        }
                   3148:                }
                   3149:                curlen *= k+1;
                   3150:        }
                   3151:        FREENM(m);
                   3152: }
                   3153:
1.63      noro     3154: ND ndv_mul_nm_symbolic(NM m0,NDV p)
                   3155: {
                   3156:        NM mr,mr0;
                   3157:        NMV m;
                   3158:        UINT *d,*dt,*dm;
                   3159:        int c,n,td,i,c1,c2,len;
                   3160:        ND r;
                   3161:
                   3162:        if ( !p ) return 0;
                   3163:        else {
                   3164:                n = NV(p); m = BDY(p);
                   3165:                d = DL(m0);
                   3166:                len = LEN(p);
                   3167:                mr0 = 0;
                   3168:                td = TD(d);
                   3169:                c = CM(m0);
                   3170:                for ( i = 0; i < len; i++, NMV_ADV(m) ) {
                   3171:                        NEXTNM(mr0,mr);
                   3172:                        CM(mr) = 1;
                   3173:                        ndl_add(DL(m),d,DL(mr));
                   3174:                }
                   3175:                NEXT(mr) = 0;
                   3176:                MKND(NV(p),mr0,len,r);
                   3177:                SG(r) = SG(p) + TD(d);
                   3178:                return r;
                   3179:        }
                   3180: }
                   3181:
1.55      noro     3182: ND ndv_mul_nm(int mod,NM m0,NDV p)
1.9       noro     3183: {
                   3184:        NM mr,mr0;
                   3185:        NMV m;
1.61      noro     3186:        UINT *d,*dt,*dm;
1.9       noro     3187:        int c,n,td,i,c1,c2,len;
1.112   ! noro     3188:        Z q;
1.9       noro     3189:        ND r;
                   3190:
1.34      noro     3191:        if ( !p ) return 0;
1.55      noro     3192:        else if ( do_weyl )
1.71      noro     3193:                if ( mod == -1 )
                   3194:                        error("ndv_mul_nm : not implemented (weyl)");
                   3195:                else
                   3196:                        return weyl_ndv_mul_nm(mod,m0,p);
1.9       noro     3197:        else {
                   3198:                n = NV(p); m = BDY(p);
1.34      noro     3199:                d = DL(m0);
1.14      noro     3200:                len = LEN(p);
1.9       noro     3201:                mr0 = 0;
1.34      noro     3202:                td = TD(d);
1.71      noro     3203:                if ( mod == -1 ) {
                   3204:                        c = CM(m0);
                   3205:                        for ( i = 0; i < len; i++, NMV_ADV(m) ) {
                   3206:                                NEXTNM(mr0,mr);
                   3207:                                CM(mr) = _mulsf(CM(m),c);
                   3208:                                ndl_add(DL(m),d,DL(mr));
                   3209:                        }
                   3210:                } else if ( mod ) {
1.16      noro     3211:                        c = CM(m0);
                   3212:                        for ( i = 0; i < len; i++, NMV_ADV(m) ) {
                   3213:                                NEXTNM(mr0,mr);
                   3214:                                c1 = CM(m);
1.19      noro     3215:                                DMAR(c1,c,0,mod,c2);
1.16      noro     3216:                                CM(mr) = c2;
                   3217:                                ndl_add(DL(m),d,DL(mr));
                   3218:                        }
                   3219:                } else {
1.112   ! noro     3220:                        q = CZ(m0);
1.16      noro     3221:                        for ( i = 0; i < len; i++, NMV_ADV(m) ) {
                   3222:                                NEXTNM(mr0,mr);
1.112   ! noro     3223:                                CZ(mr) = mulz(CZ(m),q);
1.16      noro     3224:                                ndl_add(DL(m),d,DL(mr));
                   3225:                        }
1.4       noro     3226:                }
1.9       noro     3227:                NEXT(mr) = 0;
1.31      noro     3228:                MKND(NV(p),mr0,len,r);
1.34      noro     3229:                SG(r) = SG(p) + TD(d);
1.9       noro     3230:                return r;
1.4       noro     3231:        }
                   3232: }
                   3233:
1.104     noro     3234: ND nd_quo(int mod,PGeoBucket bucket,NDV d)
1.99      noro     3235: {
                   3236:        NM mq0,mq;
1.102     noro     3237:        NMV tm;
1.104     noro     3238:        int i,nv,sg,c,c1,c2,hindex;
                   3239:        ND p,t,r;
                   3240:        N tnm;
                   3241:
1.99      noro     3242:        if ( !p ) return 0;
                   3243:        else {
1.104     noro     3244:                nv = NV(d);
1.99      noro     3245:                mq0 = 0;
1.102     noro     3246:                tm = (NMV)ALLOCA(nmv_adv);
1.104     noro     3247:                while ( 1 ) {
1.112   ! noro     3248:                        hindex = mod?head_pbucket(mod,bucket):head_pbucket_z(bucket);
1.104     noro     3249:                        if ( hindex < 0 ) break;
                   3250:                        p = bucket->body[hindex];
1.99      noro     3251:                        NEXTNM(mq0,mq);
                   3252:                        ndl_sub(HDL(p),HDL(d),DL(mq));
1.102     noro     3253:                        ndl_copy(DL(mq),DL(tm));
                   3254:                        if ( mod ) {
                   3255:                                c1 = invm(HCM(d),mod); c2 = HCM(p);
                   3256:                                DMAR(c1,c2,0,mod,c); CM(mq) = c;
                   3257:                                CM(tm) = mod-c;
                   3258:                        } else {
1.112   ! noro     3259:                                CZ(mq) = divsz(HCZ(p),HCZ(d));
        !          3260:                                CZ(tm) = chsgnz(CZ(mq));
1.102     noro     3261:                        }
                   3262:                        t = ndv_mul_nmv_trunc(mod,tm,d,HDL(d));
1.104     noro     3263:                        bucket->body[hindex] = nd_remove_head(p);
                   3264:                        t = nd_remove_head(t);
                   3265:                        add_pbucket(mod,bucket,t);
                   3266:                }
                   3267:                if ( !mq0 )
                   3268:                        r = 0;
                   3269:                else {
                   3270:                        NEXT(mq) = 0;
                   3271:                        for ( i = 0, mq = mq0; mq; mq = NEXT(mq), i++ );
                   3272:                        MKND(nv,mq0,i,r);
                   3273:                        /* XXX */
                   3274:                        SG(r) = HTD(r);
1.99      noro     3275:                }
                   3276:                return r;
                   3277:        }
                   3278: }
                   3279:
1.43      noro     3280: void ndv_realloc(NDV p,int obpe,int oadv,EPOS oepos)
1.11      noro     3281: {
1.13      noro     3282:        NMV m,mr,mr0,t;
                   3283:        int len,i,k;
1.11      noro     3284:
1.61      noro     3285:        if ( !p ) return;
                   3286:        m = BDY(p); len = LEN(p);
                   3287:        mr0 = nmv_adv>oadv?(NMV)REALLOC(BDY(p),len*nmv_adv):BDY(p);
                   3288:        m = (NMV)((char *)mr0+(len-1)*oadv);
                   3289:        mr = (NMV)((char *)mr0+(len-1)*nmv_adv);
                   3290:        t = (NMV)ALLOCA(nmv_adv);
                   3291:        for ( i = 0; i < len; i++, NMV_OPREV(m), NMV_PREV(mr) ) {
1.112   ! noro     3292:                CZ(t) = CZ(m);
1.61      noro     3293:                for ( k = 0; k < nd_wpd; k++ ) DL(t)[k] = 0;
                   3294:                ndl_reconstruct(DL(m),DL(t),obpe,oepos);
1.112   ! noro     3295:                CZ(mr) = CZ(t);
1.61      noro     3296:                ndl_copy(DL(t),DL(mr));
                   3297:        }
                   3298:        BDY(p) = mr0;
                   3299: }
                   3300:
                   3301: NDV ndv_dup_realloc(NDV p,int obpe,int oadv,EPOS oepos)
                   3302: {
                   3303:        NMV m,mr,mr0;
                   3304:        int len,i;
                   3305:        NDV r;
1.11      noro     3306:
1.61      noro     3307:        if ( !p ) return 0;
                   3308:        m = BDY(p); len = LEN(p);
                   3309:        mr0 = mr = (NMV)MALLOC(len*nmv_adv);
                   3310:        for ( i = 0; i < len; i++, NMV_OADV(m), NMV_ADV(mr) ) {
                   3311:                ndl_zero(DL(mr));
                   3312:                ndl_reconstruct(DL(m),DL(mr),obpe,oepos);
1.112   ! noro     3313:                CZ(mr) = CZ(m);
1.11      noro     3314:        }
1.61      noro     3315:        MKNDV(NV(p),mr0,len,r);
                   3316:        SG(r) = SG(p);
                   3317:        return r;
1.11      noro     3318: }
                   3319:
1.61      noro     3320: /* duplicate p */
                   3321:
                   3322: NDV ndv_dup(int mod,NDV p)
1.3       noro     3323: {
                   3324:        NDV d;
1.61      noro     3325:        NMV t,m,m0;
1.3       noro     3326:        int i,len;
                   3327:
1.34      noro     3328:        if ( !p ) return 0;
1.31      noro     3329:        len = LEN(p);
1.34      noro     3330:        m0 = m = (NMV)(mod?MALLOC_ATOMIC(len*nmv_adv):MALLOC(len*nmv_adv));
1.61      noro     3331:        for ( t = BDY(p), i = 0; i < len; i++, NMV_ADV(t), NMV_ADV(m) ) {
1.14      noro     3332:                ndl_copy(DL(t),DL(m));
1.112   ! noro     3333:                CZ(m) = CZ(t);
1.3       noro     3334:        }
                   3335:        MKNDV(NV(p),m0,len,d);
1.23      noro     3336:        SG(d) = SG(p);
                   3337:        return d;
                   3338: }
                   3339:
1.63      noro     3340: ND nd_dup(ND p)
                   3341: {
                   3342:        ND d;
                   3343:        NM t,m,m0;
                   3344:
                   3345:        if ( !p ) return 0;
                   3346:        for ( m0 = 0, t = BDY(p); t; t = NEXT(t) ) {
                   3347:                NEXTNM(m0,m);
                   3348:                ndl_copy(DL(t),DL(m));
1.112   ! noro     3349:                CZ(m) = CZ(t);
1.63      noro     3350:        }
                   3351:        if ( m0 ) NEXT(m) = 0;
                   3352:        MKND(NV(p),m0,LEN(p),d);
                   3353:        SG(d) = SG(p);
                   3354:        return d;
                   3355: }
                   3356:
1.61      noro     3357: /* XXX if p->len == 0 then it represents 0 */
                   3358:
                   3359: void ndv_mod(int mod,NDV p)
                   3360: {
                   3361:        NMV t,d;
                   3362:        int r;
                   3363:        int i,len,dlen;
1.71      noro     3364:        Obj gfs;
1.61      noro     3365:
                   3366:        if ( !p ) return;
                   3367:        len = LEN(p);
                   3368:        dlen = 0;
1.71      noro     3369:        if ( mod == -1 )
                   3370:                for ( t = d = BDY(p), i = 0; i < len; i++, NMV_ADV(t) ) {
                   3371:                        simp_ff((Obj)CP(t),&gfs);
                   3372:                        r = FTOIF(CONT((GFS)gfs));
1.61      noro     3373:                        CM(d) = r;
                   3374:                        ndl_copy(DL(t),DL(d));
                   3375:                        NMV_ADV(d);
                   3376:                        dlen++;
                   3377:                }
1.71      noro     3378:        else
                   3379:                for ( t = d = BDY(p), i = 0; i < len; i++, NMV_ADV(t) ) {
1.112   ! noro     3380:                        r = remzi(CZ(t),mod);
1.71      noro     3381:                        if ( r ) {
                   3382:                                CM(d) = r;
                   3383:                                ndl_copy(DL(t),DL(d));
                   3384:                                NMV_ADV(d);
                   3385:                                dlen++;
                   3386:                        }
                   3387:                }
1.61      noro     3388:        LEN(p) = dlen;
                   3389: }
                   3390:
                   3391: NDV ptondv(VL vl,VL dvl,P p)
                   3392: {
                   3393:        ND nd;
                   3394:
                   3395:        nd = ptond(vl,dvl,p);
                   3396:        return ndtondv(0,nd);
                   3397: }
                   3398:
                   3399: ND ptond(VL vl,VL dvl,P p)
1.23      noro     3400: {
1.61      noro     3401:        int n,i,j,k,e;
                   3402:        VL tvl;
                   3403:        V v;
                   3404:        DCP dc;
                   3405:        DCP *w;
                   3406:        ND r,s,t,u;
                   3407:        P x;
                   3408:        int c;
                   3409:        UINT *d;
1.23      noro     3410:        NM m,m0;
1.61      noro     3411:
                   3412:        if ( !p )
                   3413:                return 0;
                   3414:        else if ( NUM(p) ) {
                   3415:                NEWNM(m);
                   3416:                ndl_zero(DL(m));
1.112   ! noro     3417:                CZ(m) = qtoz((Q)p);
1.61      noro     3418:                NEXT(m) = 0;
                   3419:                MKND(nd_nvar,m,1,r);
                   3420:                SG(r) = 0;
                   3421:                return r;
                   3422:        } else {
                   3423:                for ( dc = DC(p), k = 0; dc; dc = NEXT(dc), k++ );
                   3424:                w = (DCP *)ALLOCA(k*sizeof(DCP));
                   3425:                for ( dc = DC(p), j = 0; j < k; dc = NEXT(dc), j++ ) w[j] = dc;
                   3426:                for ( i = 0, tvl = dvl, v = VR(p);
1.98      noro     3427:                        tvl && tvl->v != v; tvl = NEXT(tvl), i++ );
1.61      noro     3428:                if ( !tvl ) {
                   3429:                        for ( j = k-1, s = 0, MKV(v,x); j >= 0; j-- ) {
                   3430:                                t = ptond(vl,dvl,COEF(w[j]));
                   3431:                                pwrp(vl,x,DEG(w[j]),&p);
                   3432:                                nd_mul_c_p(CO,t,p); s = nd_add(0,s,t);
                   3433:                        }
                   3434:                        return s;
                   3435:                } else {
                   3436:                        NEWNM(m0); d = DL(m0);
                   3437:                        for ( j = k-1, s = 0; j >= 0; j-- ) {
                   3438:                                ndl_zero(d); e = QTOS(DEG(w[j])); PUT_EXP(d,i,e);
                   3439:                                TD(d) = MUL_WEIGHT(e,i);
                   3440:                                if ( nd_blockmask) ndl_weight_mask(d);
                   3441:                                t = ptond(vl,dvl,COEF(w[j]));
                   3442:                                for ( m = BDY(t); m; m = NEXT(m) )
                   3443:                                        ndl_addto(DL(m),d);
                   3444:                                SG(t) += TD(d);
                   3445:                                s = nd_add(0,s,t);
                   3446:                        }
                   3447:                        FREENM(m0);
                   3448:                        return s;
                   3449:                }
                   3450:        }
                   3451: }
                   3452:
                   3453: P ndvtop(int mod,VL vl,VL dvl,NDV p)
                   3454: {
                   3455:        VL tvl;
                   3456:        int len,n,j,i,e;
                   3457:        NMV m;
                   3458:        Q q;
                   3459:        P c;
                   3460:        UINT *d;
                   3461:        P s,r,u,t,w;
1.71      noro     3462:        GFS gfs;
1.23      noro     3463:
1.34      noro     3464:        if ( !p ) return 0;
1.61      noro     3465:        else {
                   3466:                len = LEN(p);
                   3467:                n = NV(p);
                   3468:                m = (NMV)(((char *)BDY(p))+nmv_adv*(len-1));
                   3469:                for ( j = len-1, s = 0; j >= 0; j--, NMV_PREV(m) ) {
1.71      noro     3470:                        if ( mod == -1 ) {
                   3471:                                e = IFTOF(CM(m)); MKGFS(e,gfs); c = (P)gfs;
                   3472:                        } else if ( mod ) {
1.61      noro     3473:                                STOQ(CM(m),q); c = (P)q;
                   3474:                        } else
1.112   ! noro     3475:                                c = (P)ztoq(CZ(m));
1.61      noro     3476:                        d = DL(m);
                   3477:                        for ( i = 0, t = c, tvl = dvl; i < n; tvl = NEXT(tvl), i++ ) {
                   3478:                                MKV(tvl->v,r); e = GET_EXP(d,i); STOQ(e,q);
                   3479:                                pwrp(vl,r,q,&u); mulp(vl,t,u,&w); t = w;
                   3480:                        }
                   3481:                        addp(vl,s,t,&u); s = u;
                   3482:                }
                   3483:                return s;
1.23      noro     3484:        }
1.3       noro     3485: }
                   3486:
1.61      noro     3487: NDV ndtondv(int mod,ND p)
1.11      noro     3488: {
                   3489:        NDV d;
1.61      noro     3490:        NMV m,m0;
                   3491:        NM t;
                   3492:        int i,len;
1.11      noro     3493:
1.34      noro     3494:        if ( !p ) return 0;
1.61      noro     3495:        len = LEN(p);
1.74      noro     3496:        if ( mod )
                   3497:                m0 = m = (NMV)GC_malloc_atomic_ignore_off_page(len*nmv_adv);
                   3498:        else
                   3499:                m0 = m = MALLOC(len*nmv_adv);
1.103     noro     3500: #if 0
1.74      noro     3501:        ndv_alloc += nmv_adv*len;
1.103     noro     3502: #endif
1.61      noro     3503:        for ( t = BDY(p), i = 0; t; t = NEXT(t), i++, NMV_ADV(m) ) {
                   3504:                ndl_copy(DL(t),DL(m));
1.112   ! noro     3505:                CZ(m) = CZ(t);
1.11      noro     3506:        }
1.61      noro     3507:        MKNDV(NV(p),m0,len,d);
1.14      noro     3508:        SG(d) = SG(p);
1.11      noro     3509:        return d;
                   3510: }
                   3511:
1.61      noro     3512: ND ndvtond(int mod,NDV p)
1.11      noro     3513: {
1.61      noro     3514:        ND d;
                   3515:        NM m,m0;
1.11      noro     3516:        NMV t;
1.61      noro     3517:        int i,len;
1.11      noro     3518:
1.34      noro     3519:        if ( !p ) return 0;
1.11      noro     3520:        m0 = 0;
1.61      noro     3521:        len = p->len;
                   3522:        for ( t = BDY(p), i = 0; i < len; NMV_ADV(t), i++ ) {
                   3523:                NEXTNM(m0,m);
                   3524:                ndl_copy(DL(t),DL(m));
1.112   ! noro     3525:                CZ(m) = CZ(t);
1.11      noro     3526:        }
                   3527:        NEXT(m) = 0;
1.61      noro     3528:        MKND(NV(p),m0,len,d);
1.14      noro     3529:        SG(d) = SG(p);
1.11      noro     3530:        return d;
                   3531: }
                   3532:
1.3       noro     3533: void ndv_print(NDV p)
                   3534: {
                   3535:        NMV m;
                   3536:        int i,len;
                   3537:
1.34      noro     3538:        if ( !p ) printf("0\n");
1.3       noro     3539:        else {
1.14      noro     3540:                len = LEN(p);
1.3       noro     3541:                for ( m = BDY(p), i = 0; i < len; i++, NMV_ADV(m) ) {
1.71      noro     3542:                        if ( CM(m) & 0x80000000 ) printf("+@_%d*",IFTOF(CM(m)));
                   3543:                        else printf("+%d*",CM(m));
1.16      noro     3544:                        ndl_print(DL(m));
                   3545:                }
                   3546:                printf("\n");
                   3547:        }
                   3548: }
                   3549:
1.112   ! noro     3550: void ndv_print_z(NDV p)
1.16      noro     3551: {
                   3552:        NMV m;
                   3553:        int i,len;
                   3554:
1.34      noro     3555:        if ( !p ) printf("0\n");
1.16      noro     3556:        else {
                   3557:                len = LEN(p);
                   3558:                for ( m = BDY(p), i = 0; i < len; i++, NMV_ADV(m) ) {
                   3559:                        printf("+");
1.112   ! noro     3560:                        printz(CZ(m));
1.16      noro     3561:                        printf("*");
1.14      noro     3562:                        ndl_print(DL(m));
1.3       noro     3563:                }
                   3564:                printf("\n");
                   3565:        }
1.25      noro     3566: }
                   3567:
1.61      noro     3568: NODE ndv_reducebase(NODE x)
1.27      noro     3569: {
                   3570:        int len,i,j;
                   3571:        NDV *w;
                   3572:        NODE t,t0;
                   3573:
                   3574:        len = length(x);
                   3575:        w = (NDV *)ALLOCA(len*sizeof(NDV));
                   3576:        for ( i = 0, t = x; i < len; i++, t = NEXT(t) ) w[i] = BDY(t);
                   3577:        for ( i = 0; i < len; i++ ) {
                   3578:                for ( j = 0; j < i; j++ ) {
                   3579:                        if ( w[i] && w[j] )
                   3580:                                if ( ndl_reducible(HDL(w[i]),HDL(w[j])) ) w[i] = 0;
                   3581:                                else if ( ndl_reducible(HDL(w[j]),HDL(w[i])) ) w[j] = 0;
                   3582:                }
                   3583:        }
                   3584:        for ( i = len-1, t0 = 0; i >= 0; i-- ) {
                   3585:                if ( w[i] ) { NEXTNODE(t0,t); BDY(t) = (pointer)w[i]; }
                   3586:        }
                   3587:        NEXT(t) = 0; x = t0;
                   3588:        return x;
1.11      noro     3589: }
1.32      noro     3590:
1.43      noro     3591: /* XXX incomplete */
                   3592:
1.32      noro     3593: void nd_init_ord(struct order_spec *ord)
                   3594: {
1.43      noro     3595:        switch ( ord->id ) {
1.32      noro     3596:                case 0:
1.43      noro     3597:                        switch ( ord->ord.simple ) {
                   3598:                                case 0:
                   3599:                                        nd_dcomp = 1;
                   3600:                                        nd_isrlex = 1;
                   3601:                                        break;
                   3602:                                case 1:
                   3603:                                        nd_dcomp = 1;
                   3604:                                        nd_isrlex = 0;
                   3605:                                        break;
                   3606:                                case 2:
                   3607:                                        nd_dcomp = 0;
                   3608:                                        nd_isrlex = 0;
1.45      noro     3609:                                        ndl_compare_function = ndl_lex_compare;
1.58      noro     3610:                                        break;
                   3611:                                case 11:
                   3612:                                        /* XXX */
                   3613:                                        nd_dcomp = 0;
                   3614:                                        nd_isrlex = 1;
                   3615:                                        ndl_compare_function = ndl_ww_lex_compare;
1.43      noro     3616:                                        break;
                   3617:                                default:
                   3618:                                        error("nd_gr : unsupported order");
                   3619:                        }
1.32      noro     3620:                        break;
                   3621:                case 1:
1.96      noro     3622:                        /* block order */
1.43      noro     3623:                        /* XXX */
                   3624:                        nd_dcomp = -1;
1.32      noro     3625:                        nd_isrlex = 0;
1.45      noro     3626:                        ndl_compare_function = ndl_block_compare;
1.34      noro     3627:                        break;
1.43      noro     3628:                case 2:
1.96      noro     3629:                        /* matrix order */
                   3630:                        /* XXX */
                   3631:                        nd_dcomp = -1;
                   3632:                        nd_isrlex = 0;
                   3633:                        nd_matrix_len = ord->ord.matrix.row;
                   3634:                        nd_matrix = ord->ord.matrix.matrix;
                   3635:                        ndl_compare_function = ndl_matrix_compare;
                   3636:                        break;
                   3637:                case 3:
1.97      noro     3638:                        /* composite order */
                   3639:                        nd_dcomp = -1;
                   3640:                        nd_isrlex = 0;
                   3641:                        nd_worb_len = ord->ord.composite.length;
                   3642:                        nd_worb = ord->ord.composite.w_or_b;
                   3643:                        ndl_compare_function = ndl_composite_compare;
1.32      noro     3644:                        break;
                   3645:        }
1.41      noro     3646:        nd_ord = ord;
1.32      noro     3647: }
                   3648:
1.43      noro     3649: BlockMask nd_create_blockmask(struct order_spec *ord)
                   3650: {
                   3651:        int n,i,j,s,l;
1.61      noro     3652:        UINT *t;
1.43      noro     3653:        BlockMask bm;
                   3654:
1.96      noro     3655:        /* we only create mask table for block order */
                   3656:        if ( ord->id != 1 )
1.43      noro     3657:                return 0;
                   3658:        n = ord->ord.block.length;
                   3659:        bm = (BlockMask)MALLOC(sizeof(struct oBlockMask));
                   3660:        bm->n = n;
                   3661:        bm->order_pair = ord->ord.block.order_pair;
1.61      noro     3662:        bm->mask = (UINT **)MALLOC(n*sizeof(UINT *));
1.43      noro     3663:        for ( i = 0, s = 0; i < n; i++ ) {
1.61      noro     3664:                bm->mask[i] = t = (UINT *)MALLOC_ATOMIC(nd_wpd*sizeof(UINT));
1.43      noro     3665:                for ( j = 0; j < nd_wpd; j++ ) t[j] = 0;
                   3666:                l = bm->order_pair[i].length;
                   3667:                for ( j = 0; j < l; j++, s++ ) PUT_EXP(t,s,nd_mask0);
                   3668:        }
                   3669:        return bm;
1.57      noro     3670: }
                   3671:
                   3672: EPOS nd_create_epos(struct order_spec *ord)
                   3673: {
                   3674:        int i,j,l,s,ord_l,ord_o;
                   3675:        EPOS epos;
                   3676:        struct order_pair *op;
                   3677:
                   3678:        epos = (EPOS)MALLOC_ATOMIC(nd_nvar*sizeof(struct oEPOS));
                   3679:        switch ( ord->id ) {
                   3680:                case 0:
                   3681:                        if ( nd_isrlex ) {
                   3682:                                for ( i = 0; i < nd_nvar; i++ ) {
                   3683:                                        epos[i].i = nd_exporigin + (nd_nvar-1-i)/nd_epw;
                   3684:                                        epos[i].s = (nd_epw-((nd_nvar-1-i)%nd_epw)-1)*nd_bpe;
                   3685:                                }
                   3686:                        } else {
                   3687:                                for ( i = 0; i < nd_nvar; i++ ) {
                   3688:                                        epos[i].i = nd_exporigin + i/nd_epw;
                   3689:                                        epos[i].s = (nd_epw-(i%nd_epw)-1)*nd_bpe;
                   3690:                                }
                   3691:                        }
                   3692:                        break;
                   3693:                case 1:
                   3694:                        /* block order */
                   3695:                        l = ord->ord.block.length;
                   3696:                        op = ord->ord.block.order_pair;
                   3697:                        for ( j = 0, s = 0; j < l; j++ ) {
                   3698:                                ord_o = op[j].order;
                   3699:                                ord_l = op[j].length;
                   3700:                                if ( !ord_o )
                   3701:                                        for ( i = 0; i < ord_l; i++ ) {
                   3702:                                                epos[s+i].i = nd_exporigin + (s+ord_l-i-1)/nd_epw;
                   3703:                                                epos[s+i].s = (nd_epw-((s+ord_l-i-1)%nd_epw)-1)*nd_bpe;
                   3704:                                        }
                   3705:                                else
                   3706:                                        for ( i = 0; i < ord_l; i++ ) {
                   3707:                                                epos[s+i].i = nd_exporigin + (s+i)/nd_epw;
                   3708:                                                epos[s+i].s = (nd_epw-((s+i)%nd_epw)-1)*nd_bpe;
                   3709:                                        }
                   3710:                                s += ord_l;
                   3711:                        }
                   3712:                        break;
                   3713:                case 2:
1.96      noro     3714:                        /* matrix order */
                   3715:                case 3:
                   3716:                        /* composite order */
                   3717:                        for ( i = 0; i < nd_nvar; i++ ) {
                   3718:                                epos[i].i = nd_exporigin + i/nd_epw;
                   3719:                                epos[i].s = (nd_epw-(i%nd_epw)-1)*nd_bpe;
                   3720:                        }
                   3721:                        break;
1.57      noro     3722:        }
                   3723:        return epos;
1.43      noro     3724: }
1.59      noro     3725:
                   3726: /* external interface */
                   3727:
                   3728: void nd_nf_p(P f,LIST g,LIST v,int m,struct order_spec *ord,P *rp)
                   3729: {
1.61      noro     3730:        NODE t,in0,in;
1.59      noro     3731:        ND nd,nf;
1.61      noro     3732:        NDV ndv;
                   3733:        VL vv,tv;
                   3734:        int stat,nvar,max,e;
1.59      noro     3735:
                   3736:        pltovl(v,&vv);
1.74      noro     3737:        for ( nvar = 0, tv = vv; tv; tv = NEXT(tv), nvar++ );
1.61      noro     3738:
                   3739:        /* get the degree bound */
                   3740:        for ( t = BDY(g), max = 0; t; t = NEXT(t) )
                   3741:                for ( tv = vv; tv; tv = NEXT(tv) ) {
                   3742:                        e = getdeg(tv->v,(P)BDY(t));
                   3743:                        max = MAX(e,max);
                   3744:                }
                   3745:        for ( tv = vv; tv; tv = NEXT(tv) ) {
                   3746:                e = getdeg(tv->v,f);
                   3747:                max = MAX(e,max);
                   3748:        }
                   3749:
1.59      noro     3750:        nd_init_ord(ord);
1.61      noro     3751:        nd_setup_parameters(nvar,max);
                   3752:
                   3753:        /* conversion to ndv */
                   3754:        for ( in0 = 0, t = BDY(g); t; t = NEXT(t) ) {
                   3755:                NEXTNODE(in0,in);
                   3756:                BDY(in) = (pointer)ptondv(CO,vv,(P)BDY(t));
1.69      noro     3757:                if ( m ) ndv_mod(m,(NDV)BDY(in));
1.61      noro     3758:        }
                   3759:        NEXTNODE(in0,in);
                   3760:        BDY(in) = (pointer)ptondv(CO,vv,f);
1.69      noro     3761:        if ( m ) ndv_mod(m,(NDV)BDY(in));
1.61      noro     3762:        NEXT(in) = 0;
                   3763:
                   3764:        ndv_setup(m,0,in0);
1.59      noro     3765:        nd_psn--;
                   3766:        nd_scale=2;
                   3767:        while ( 1 ) {
                   3768:                nd = (pointer)ndvtond(m,nd_ps[nd_psn]);
1.69      noro     3769:                stat = nd_nf(m,nd,nd_ps,1,0,&nf);
1.59      noro     3770:                if ( !stat ) {
                   3771:                        nd_psn++;
1.103     noro     3772:                        nd_reconstruct(0,0);
1.59      noro     3773:                        nd_psn--;
                   3774:                } else
                   3775:                        break;
                   3776:        }
1.61      noro     3777:        *rp = ndvtop(m,CO,vv,ndtondv(m,nf));
1.63      noro     3778: }
                   3779:
                   3780: int nd_to_vect(int mod,UINT *s0,int n,ND d,UINT *r)
                   3781: {
                   3782:        NM m;
                   3783:        UINT *t,*s;
                   3784:        int i;
                   3785:
                   3786:        for ( i = 0; i < n; i++ ) r[i] = 0;
                   3787:        for ( i = 0, s = s0, m = BDY(d); m; m = NEXT(m) ) {
                   3788:                t = DL(m);
                   3789:                for ( ; !ndl_equal(t,s); s += nd_wpd, i++ );
                   3790:                r[i] = CM(m);
                   3791:        }
                   3792:        for ( i = 0; !r[i]; i++ );
                   3793:        return i;
                   3794: }
                   3795:
1.112   ! noro     3796: int nd_to_vect_z(UINT *s0,int n,ND d,Z *r)
1.74      noro     3797: {
1.107     noro     3798:        NM m;
1.74      noro     3799:        UINT *t,*s;
1.107     noro     3800:        int i;
1.74      noro     3801:
                   3802:        for ( i = 0; i < n; i++ ) r[i] = 0;
1.107     noro     3803:        for ( i = 0, s = s0, m = BDY(d); m; m = NEXT(m) ) {
1.74      noro     3804:                t = DL(m);
                   3805:                for ( ; !ndl_equal(t,s); s += nd_wpd, i++ );
1.112   ! noro     3806:                r[i] = CZ(m);
1.74      noro     3807:        }
                   3808:        for ( i = 0; !r[i]; i++ );
                   3809:        return i;
                   3810: }
                   3811:
1.67      noro     3812: IndArray nm_ind_pair_to_vect_compress(int mod,UINT *s0,int n,NM_ind_pair pair)
1.64      noro     3813: {
                   3814:        NM m;
                   3815:        NMV mr;
                   3816:        UINT *d,*t,*s;
                   3817:        NDV p;
1.67      noro     3818:        unsigned char *ivc;
                   3819:        unsigned short *ivs;
1.81      noro     3820:        UINT *v,*ivi,*s0v;
1.67      noro     3821:        int i,j,len,prev,diff,cdiff;
                   3822:        IndArray r;
1.64      noro     3823:
                   3824:        m = pair->mul;
                   3825:        d = DL(m);
                   3826:        p = nd_ps[pair->index];
                   3827:        len = LEN(p);
                   3828:        t = (UINT *)ALLOCA(nd_wpd*sizeof(UINT));
1.67      noro     3829:        v = (unsigned int *)ALLOCA(len*sizeof(unsigned int));
1.64      noro     3830:        for ( i = j = 0, s = s0, mr = BDY(p); j < len; j++, NMV_ADV(mr) ) {
                   3831:                ndl_add(d,DL(mr),t);
                   3832:                for ( ; !ndl_equal(t,s); s += nd_wpd, i++ );
1.67      noro     3833:                v[j] = i;
                   3834:        }
1.81      noro     3835:        r = (IndArray)MALLOC(sizeof(struct oIndArray));
1.67      noro     3836:        r->head = v[0];
                   3837:        diff = 0;
                   3838:        for ( i = 1; i < len; i++ ) {
                   3839:                cdiff = v[i]-v[i-1]; diff = MAX(cdiff,diff);
                   3840:        }
                   3841:        if ( diff < 256 ) {
                   3842:                r->width = 1;
                   3843:                ivc = (unsigned char *)MALLOC_ATOMIC(len*sizeof(unsigned char));
                   3844:                r->index.c = ivc;
                   3845:                for ( i = 1, ivc[0] = 0; i < len; i++ ) ivc[i] = v[i]-v[i-1];
                   3846:        } else if ( diff < 65536 ) {
                   3847:                r->width = 2;
                   3848:                ivs = (unsigned short *)MALLOC_ATOMIC(len*sizeof(unsigned short));
                   3849:                r->index.s = ivs;
                   3850:                for ( i = 1, ivs[0] = 0; i < len; i++ ) ivs[i] = v[i]-v[i-1];
                   3851:        } else {
                   3852:                r->width = 4;
                   3853:                ivi = (unsigned int *)MALLOC_ATOMIC(len*sizeof(unsigned int));
                   3854:                r->index.i = ivi;
                   3855:                for ( i = 1, ivi[0] = 0; i < len; i++ ) ivi[i] = v[i]-v[i-1];
1.64      noro     3856:        }
1.67      noro     3857:        return r;
1.64      noro     3858: }
                   3859:
1.65      noro     3860:
1.112   ! noro     3861: int ndv_reduce_vect_z(Z *svect,int col,IndArray *imat,NM_ind_pair *rp0,int nred)
1.107     noro     3862: {
                   3863:        int i,j,k,len,pos,prev;
1.112   ! noro     3864:        Z cs,mcs,c1,c2,cr,gcd,t;
1.107     noro     3865:        IndArray ivect;
                   3866:        unsigned char *ivc;
                   3867:        unsigned short *ivs;
                   3868:        unsigned int *ivi;
                   3869:        NDV redv;
                   3870:        NMV mr;
                   3871:        NODE rp;
                   3872:        int maxrs;
                   3873:
                   3874:        maxrs = 0;
                   3875:        for ( i = 0; i < nred; i++ ) {
                   3876:                ivect = imat[i];
                   3877:                k = ivect->head;
                   3878:                if ( svect[k] ) {
                   3879:                        maxrs = MAX(maxrs,rp0[i]->sugar);
                   3880:                        redv = nd_ps[rp0[i]->index];
                   3881:                        len = LEN(redv); mr = BDY(redv);
1.112   ! noro     3882:                        gcd = gcdz_cofactor(svect[k],CZ(mr),&cs,&cr);
        !          3883:                        mcs = chsgnz(cs);
        !          3884:                        if ( !uniz(cr) )
        !          3885:                                for ( j = 0; j < col; j++ )
        !          3886:                                        svect[j] = mulz(svect[j],cr);
1.107     noro     3887:                        svect[k] = 0; prev = k;
                   3888:                        switch ( ivect->width ) {
                   3889:                                case 1:
                   3890:                                        ivc = ivect->index.c;
                   3891:                                        for ( j = 1, NMV_ADV(mr); j < len; j++, NMV_ADV(mr) ) {
                   3892:                                                pos = prev+ivc[j]; prev = pos;
1.112   ! noro     3893:                                                svect[pos] = addz(svect[pos],mulz(CZ(mr),mcs));
1.107     noro     3894:                                        }
                   3895:                                        break;
                   3896:                                case 2:
                   3897:                                        ivs = ivect->index.s;
                   3898:                                        for ( j = 1, NMV_ADV(mr); j < len; j++, NMV_ADV(mr) ) {
                   3899:                                                pos = prev+ivs[j]; prev = pos;
1.112   ! noro     3900:                                                svect[pos] = addz(svect[pos],mulz(CZ(mr),mcs));
1.107     noro     3901:                                        }
                   3902:                                        break;
                   3903:                                case 4:
                   3904:                                        ivi = ivect->index.i;
                   3905:                                        for ( j = 1, NMV_ADV(mr); j < len; j++, NMV_ADV(mr) ) {
                   3906:                                                pos = prev+ivi[j]; prev = pos;
1.112   ! noro     3907:                                                svect[pos] = addz(svect[pos],mulz(CZ(mr),mcs));
1.107     noro     3908:                                        }
                   3909:                                        break;
                   3910:                        }
                   3911:                }
                   3912:        }
                   3913:        return maxrs;
                   3914: }
                   3915:
1.76      noro     3916: int ndv_reduce_vect(int m,UINT *svect,int col,IndArray *imat,NM_ind_pair *rp0,int nred)
1.65      noro     3917: {
1.67      noro     3918:        int i,j,k,len,pos,prev;
1.66      noro     3919:        UINT c,c1,c2,c3,up,lo,dmy;
1.67      noro     3920:        IndArray ivect;
                   3921:        unsigned char *ivc;
                   3922:        unsigned short *ivs;
                   3923:        unsigned int *ivi;
1.65      noro     3924:        NDV redv;
1.67      noro     3925:        NMV mr;
1.65      noro     3926:        NODE rp;
1.76      noro     3927:        int maxrs;
1.65      noro     3928:
1.76      noro     3929:        maxrs = 0;
1.74      noro     3930:        for ( i = 0; i < nred; i++ ) {
1.65      noro     3931:                ivect = imat[i];
1.67      noro     3932:                k = ivect->head; svect[k] %= m;
1.65      noro     3933:                if ( c = svect[k] ) {
1.76      noro     3934:                        maxrs = MAX(maxrs,rp0[i]->sugar);
1.74      noro     3935:                        c = m-c; redv = nd_ps[rp0[i]->index];
1.67      noro     3936:                        len = LEN(redv); mr = BDY(redv);
                   3937:                        svect[k] = 0; prev = k;
                   3938:                        switch ( ivect->width ) {
                   3939:                                case 1:
                   3940:                                        ivc = ivect->index.c;
                   3941:                                        for ( j = 1, NMV_ADV(mr); j < len; j++, NMV_ADV(mr) ) {
                   3942:                                                pos = prev+ivc[j]; c1 = CM(mr); c2 = svect[pos];
                   3943:                                                prev = pos;
                   3944:                                                DMA(c1,c,c2,up,lo);
                   3945:                                                if ( up ) { DSAB(m,up,lo,dmy,c3); svect[pos] = c3;
                   3946:                                                } else svect[pos] = lo;
                   3947:                                        }
                   3948:                                        break;
                   3949:                                case 2:
                   3950:                                        ivs = ivect->index.s;
                   3951:                                        for ( j = 1, NMV_ADV(mr); j < len; j++, NMV_ADV(mr) ) {
                   3952:                                                pos = prev+ivs[j]; c1 = CM(mr); c2 = svect[pos];
                   3953:                                                prev = pos;
                   3954:                                                DMA(c1,c,c2,up,lo);
                   3955:                                                if ( up ) { DSAB(m,up,lo,dmy,c3); svect[pos] = c3;
                   3956:                                                } else svect[pos] = lo;
                   3957:                                        }
                   3958:                                        break;
                   3959:                                case 4:
                   3960:                                        ivi = ivect->index.i;
                   3961:                                        for ( j = 1, NMV_ADV(mr); j < len; j++, NMV_ADV(mr) ) {
                   3962:                                                pos = prev+ivi[j]; c1 = CM(mr); c2 = svect[pos];
                   3963:                                                prev = pos;
                   3964:                                                DMA(c1,c,c2,up,lo);
                   3965:                                                if ( up ) { DSAB(m,up,lo,dmy,c3); svect[pos] = c3;
                   3966:                                                } else svect[pos] = lo;
                   3967:                                        }
                   3968:                                        break;
1.65      noro     3969:                        }
                   3970:                }
                   3971:        }
1.66      noro     3972:        for ( i = 0; i < col; i++ )
                   3973:                if ( svect[i] >= (UINT)m ) svect[i] %= m;
1.76      noro     3974:        return maxrs;
1.65      noro     3975: }
                   3976:
1.76      noro     3977: int ndv_reduce_vect_sf(int m,UINT *svect,int col,IndArray *imat,NM_ind_pair *rp0,int nred)
1.72      noro     3978: {
                   3979:        int i,j,k,len,pos,prev;
                   3980:        UINT c,c1,c2,c3,up,lo,dmy;
                   3981:        IndArray ivect;
                   3982:        unsigned char *ivc;
                   3983:        unsigned short *ivs;
                   3984:        unsigned int *ivi;
                   3985:        NDV redv;
                   3986:        NMV mr;
                   3987:        NODE rp;
1.76      noro     3988:        int maxrs;
1.72      noro     3989:
1.76      noro     3990:        maxrs = 0;
1.74      noro     3991:        for ( i = 0; i < nred; i++ ) {
1.72      noro     3992:                ivect = imat[i];
                   3993:                k = ivect->head; svect[k] %= m;
                   3994:                if ( c = svect[k] ) {
1.76      noro     3995:                        maxrs = MAX(maxrs,rp0[i]->sugar);
1.74      noro     3996:                        c = _chsgnsf(c); redv = nd_ps[rp0[i]->index];
1.72      noro     3997:                        len = LEN(redv); mr = BDY(redv);
                   3998:                        svect[k] = 0; prev = k;
                   3999:                        switch ( ivect->width ) {
                   4000:                                case 1:
                   4001:                                        ivc = ivect->index.c;
                   4002:                                        for ( j = 1, NMV_ADV(mr); j < len; j++, NMV_ADV(mr) ) {
                   4003:                                                pos = prev+ivc[j]; prev = pos;
                   4004:                                                svect[pos] = _addsf(_mulsf(CM(mr),c),svect[pos]);
                   4005:                                        }
                   4006:                                        break;
                   4007:                                case 2:
                   4008:                                        ivs = ivect->index.s;
                   4009:                                        for ( j = 1, NMV_ADV(mr); j < len; j++, NMV_ADV(mr) ) {
                   4010:                                                pos = prev+ivs[j]; prev = pos;
                   4011:                                                svect[pos] = _addsf(_mulsf(CM(mr),c),svect[pos]);
                   4012:                                        }
                   4013:                                        break;
                   4014:                                case 4:
                   4015:                                        ivi = ivect->index.i;
                   4016:                                        for ( j = 1, NMV_ADV(mr); j < len; j++, NMV_ADV(mr) ) {
                   4017:                                                pos = prev+ivi[j]; prev = pos;
                   4018:                                                svect[pos] = _addsf(_mulsf(CM(mr),c),svect[pos]);
                   4019:                                        }
                   4020:                                        break;
                   4021:                        }
                   4022:                }
                   4023:        }
1.76      noro     4024:        return maxrs;
1.72      noro     4025: }
                   4026:
1.65      noro     4027: NDV vect_to_ndv(UINT *vect,int spcol,int col,int *rhead,UINT *s0vect)
                   4028: {
                   4029:        int j,k,len;
                   4030:        UINT *p;
                   4031:        UINT c;
                   4032:        NDV r;
                   4033:        NMV mr0,mr;
                   4034:
                   4035:        for ( j = 0, len = 0; j < spcol; j++ ) if ( vect[j] ) len++;
                   4036:        if ( !len ) return 0;
                   4037:        else {
1.74      noro     4038:                mr0 = (NMV)GC_malloc_atomic_ignore_off_page(nmv_adv*len);
1.103     noro     4039: #if 0
1.74      noro     4040:                ndv_alloc += nmv_adv*len;
1.103     noro     4041: #endif
1.65      noro     4042:                mr = mr0;
                   4043:                p = s0vect;
                   4044:                for ( j = k = 0; j < col; j++, p += nd_wpd )
                   4045:                        if ( !rhead[j] ) {
                   4046:                                if ( c = vect[k++] ) {
                   4047:                                        ndl_copy(p,DL(mr)); CM(mr) = c; NMV_ADV(mr);
                   4048:                                }
                   4049:                        }
                   4050:                MKNDV(nd_nvar,mr0,len,r);
                   4051:                return r;
                   4052:        }
                   4053: }
                   4054:
1.112   ! noro     4055: NDV vect_to_ndv_z(Z *vect,int spcol,int col,int *rhead,UINT *s0vect)
1.107     noro     4056: {
                   4057:        int j,k,len;
                   4058:        UINT *p;
1.112   ! noro     4059:        Z c;
1.107     noro     4060:        NDV r;
                   4061:        NMV mr0,mr;
                   4062:
                   4063:        for ( j = 0, len = 0; j < spcol; j++ ) if ( vect[j] ) len++;
                   4064:        if ( !len ) return 0;
                   4065:        else {
1.109     noro     4066:                mr0 = (NMV)GC_malloc(nmv_adv*len);
1.107     noro     4067: #if 0
                   4068:                ndv_alloc += nmv_adv*len;
                   4069: #endif
                   4070:                mr = mr0;
                   4071:                p = s0vect;
                   4072:                for ( j = k = 0; j < col; j++, p += nd_wpd )
                   4073:                        if ( !rhead[j] ) {
                   4074:                                if ( c = vect[k++] ) {
1.112   ! noro     4075:                                        ndl_copy(p,DL(mr)); CZ(mr) = c; NMV_ADV(mr);
1.107     noro     4076:                                }
                   4077:                        }
                   4078:                MKNDV(nd_nvar,mr0,len,r);
                   4079:                return r;
                   4080:        }
                   4081: }
                   4082:
1.74      noro     4083: int nd_sp_f4(int m,ND_pairs l,PGeoBucket bucket)
1.65      noro     4084: {
                   4085:        ND_pairs t;
                   4086:        NODE sp0,sp;
                   4087:        int stat;
                   4088:        ND spol;
                   4089:
                   4090:        for ( t = l; t; t = NEXT(t) ) {
                   4091:                stat = nd_sp(m,0,t,&spol);
                   4092:                if ( !stat ) return 0;
                   4093:                if ( spol ) {
                   4094:                        add_pbucket_symbolic(bucket,spol);
                   4095:                }
                   4096:        }
1.68      noro     4097:        return 1;
1.65      noro     4098: }
                   4099:
                   4100: int nd_symbolic_preproc(PGeoBucket bucket,UINT **s0vect,NODE *r)
                   4101: {
                   4102:        NODE rp0,rp;
                   4103:        NM mul,head,s0,s;
1.76      noro     4104:        int index,col,i,sugar;
1.65      noro     4105:        RHist h;
                   4106:        UINT *s0v,*p;
                   4107:        NM_ind_pair pair;
                   4108:        ND red;
                   4109:
                   4110:        s0 = 0; rp0 = 0; col = 0;
                   4111:        while ( 1 ) {
                   4112:                head = remove_head_pbucket_symbolic(bucket);
                   4113:                if ( !head ) break;
                   4114:                if ( !s0 ) s0 = head;
                   4115:                else NEXT(s) = head;
                   4116:                s = head;
                   4117:                index = ndl_find_reducer(DL(head));
                   4118:                if ( index >= 0 ) {
                   4119:                        h = nd_psh[index];
                   4120:                        NEWNM(mul);
                   4121:                        ndl_sub(DL(head),DL(h),DL(mul));
                   4122:                        if ( ndl_check_bound2(index,DL(mul)) ) return 0;
1.76      noro     4123:                        sugar = TD(DL(mul))+SG(nd_ps[index]);
                   4124:                        MKNM_ind_pair(pair,mul,index,sugar);
1.65      noro     4125:                        red = ndv_mul_nm_symbolic(mul,nd_ps[index]);
                   4126:                        add_pbucket_symbolic(bucket,nd_remove_head(red));
                   4127:                        NEXTNODE(rp0,rp); BDY(rp) = (pointer)pair;
                   4128:                }
                   4129:                col++;
                   4130:        }
1.72      noro     4131:        if ( rp0 ) NEXT(rp) = 0;
                   4132:        NEXT(s) = 0;
1.65      noro     4133:        s0v = (UINT *)MALLOC_ATOMIC(col*nd_wpd*sizeof(UINT));
                   4134:        for ( i = 0, p = s0v, s = s0; i < col;
                   4135:                i++, p += nd_wpd, s = NEXT(s) ) ndl_copy(DL(s),p);
                   4136:        *s0vect = s0v;
                   4137:        *r = rp0;
                   4138:        return col;
                   4139: }
                   4140:
1.69      noro     4141: NODE nd_f4(int m)
                   4142: {
                   4143:        int i,nh,stat,index;
                   4144:        NODE r,g;
                   4145:        ND_pairs d,l,t;
                   4146:        ND spol,red;
                   4147:        NDV nf,redv;
                   4148:        NM s0,s;
1.84      noro     4149:        NODE rp0,srp0,nflist;
1.69      noro     4150:        int nsp,nred,col,rank,len,k,j,a;
                   4151:        UINT c;
1.74      noro     4152:        UINT **spmat;
1.69      noro     4153:        UINT *s0vect,*svect,*p,*v;
                   4154:        int *colstat;
                   4155:        IndArray *imat;
                   4156:        int *rhead;
                   4157:        int spcol,sprow;
                   4158:        int sugar;
                   4159:        PGeoBucket bucket;
                   4160:        struct oEGT eg0,eg1,eg_f4;
                   4161:
1.103     noro     4162: #if 0
1.74      noro     4163:        ndv_alloc = 0;
1.103     noro     4164: #endif
1.69      noro     4165:        g = 0; d = 0;
                   4166:        for ( i = 0; i < nd_psn; i++ ) {
                   4167:                d = update_pairs(d,g,i);
                   4168:                g = update_base(g,i);
                   4169:        }
                   4170:        while ( d ) {
                   4171:                get_eg(&eg0);
                   4172:                l = nd_minsugarp(d,&d);
                   4173:                sugar = SG(l);
                   4174:                bucket = create_pbucket();
1.74      noro     4175:                stat = nd_sp_f4(m,l,bucket);
1.69      noro     4176:                if ( !stat ) {
                   4177:                        for ( t = l; NEXT(t); t = NEXT(t) );
                   4178:                        NEXT(t) = d; d = l;
1.103     noro     4179:                        d = nd_reconstruct(0,d);
1.69      noro     4180:                        continue;
                   4181:                }
1.86      noro     4182:                if ( bucket->m < 0 ) continue;
1.69      noro     4183:                col = nd_symbolic_preproc(bucket,&s0vect,&rp0);
                   4184:                if ( !col ) {
                   4185:                        for ( t = l; NEXT(t); t = NEXT(t) );
                   4186:                        NEXT(t) = d; d = l;
1.103     noro     4187:                        d = nd_reconstruct(0,d);
1.69      noro     4188:                        continue;
                   4189:                }
                   4190:                get_eg(&eg1); init_eg(&eg_f4); add_eg(&eg_f4,&eg0,&eg1);
1.74      noro     4191:                if ( DP_Print )
                   4192:                        fprintf(asir_out,"sugar=%d,symb=%fsec,",
                   4193:                                sugar,eg_f4.exectime+eg_f4.gctime);
                   4194:                if ( 1 )
                   4195:                        nflist = nd_f4_red(m,l,s0vect,col,rp0);
                   4196:                else
                   4197:                        nflist = nd_f4_red_dist(m,l,s0vect,col,rp0);
1.69      noro     4198:                /* adding new bases */
1.74      noro     4199:                for ( r = nflist; r; r = NEXT(r) ) {
                   4200:                        nf = (NDV)BDY(r);
1.69      noro     4201:                        ndv_removecont(m,nf);
1.77      noro     4202:                        nh = ndv_newps(m,nf,0);
1.69      noro     4203:                        d = update_pairs(d,g,nh);
                   4204:                        g = update_base(g,nh);
                   4205:                }
                   4206:        }
                   4207:        for ( r = g; r; r = NEXT(r) ) BDY(r) = (pointer)nd_ps[(int)BDY(r)];
1.103     noro     4208: #if 0
1.74      noro     4209:        fprintf(asir_out,"ndv_alloc=%d\n",ndv_alloc);
1.103     noro     4210: #endif
1.69      noro     4211:        return g;
                   4212: }
1.74      noro     4213:
                   4214: NODE nd_f4_red(int m,ND_pairs sp0,UINT *s0vect,int col,NODE rp0)
1.63      noro     4215: {
1.67      noro     4216:        IndArray *imat;
1.106     noro     4217:        int nsp,nred,i;
1.65      noro     4218:        int *rhead;
1.106     noro     4219:        NODE r0,rp;
1.74      noro     4220:        ND_pairs sp;
                   4221:        NM_ind_pair *rvect;
1.63      noro     4222:
1.74      noro     4223:        for ( sp = sp0, nsp = 0; sp; sp = NEXT(sp), nsp++ );
1.106     noro     4224:        nred = length(rp0);
1.74      noro     4225:        imat = (IndArray *)ALLOCA(nred*sizeof(IndArray));
                   4226:        rhead = (int *)ALLOCA(col*sizeof(int));
                   4227:        for ( i = 0; i < col; i++ ) rhead[i] = 0;
1.63      noro     4228:
1.74      noro     4229:        /* construction of index arrays */
                   4230:        rvect = (NM_ind_pair *)ALLOCA(nred*sizeof(NM_ind_pair));
                   4231:        for ( rp = rp0, i = 0; rp; i++, rp = NEXT(rp) ) {
                   4232:                rvect[i] = (NM_ind_pair)BDY(rp);
                   4233:                imat[i] = nm_ind_pair_to_vect_compress(m,s0vect,col,rvect[i]);
                   4234:                rhead[imat[i]->head] = 1;
                   4235:        }
1.107     noro     4236:        if ( m )
                   4237:                r0 = nd_f4_red_main(m,sp0,nsp,s0vect,col,rvect,rhead,imat,nred);
                   4238:        else
1.112   ! noro     4239:                r0 = nd_f4_red_z_main(sp0,nsp,s0vect,col,rvect,rhead,imat,nred);
1.106     noro     4240:        return r0;
                   4241: }
1.74      noro     4242:
1.106     noro     4243: NODE nd_f4_red_main(int m,ND_pairs sp0,int nsp,UINT *s0vect,int col,
                   4244:         NM_ind_pair *rvect,int *rhead,IndArray *imat,int nred)
                   4245: {
                   4246:        int spcol,sprow,a;
                   4247:        int i,j,k,l,rank;
                   4248:        NODE r0,r;
                   4249:        ND_pairs sp;
                   4250:        ND spol;
                   4251:        int **spmat;
                   4252:        UINT *svect,*v;
                   4253:        int *colstat;
                   4254:        struct oEGT eg0,eg1,eg2,eg_f4,eg_f4_1,eg_f4_2;
                   4255:        int maxrs;
                   4256:        int *spsugar;
                   4257:
                   4258:        spcol = col-nred;
                   4259:        get_eg(&eg0);
1.74      noro     4260:        /* elimination (1st step) */
                   4261:        spmat = (int **)ALLOCA(nsp*sizeof(UINT *));
                   4262:        svect = (UINT *)ALLOCA(col*sizeof(UINT));
1.76      noro     4263:        spsugar = (int *)ALLOCA(nsp*sizeof(UINT));
1.74      noro     4264:        for ( a = sprow = 0, sp = sp0; a < nsp; a++, sp = NEXT(sp) ) {
                   4265:                nd_sp(m,0,sp,&spol);
1.75      noro     4266:                if ( !spol ) continue;
1.74      noro     4267:                nd_to_vect(m,s0vect,col,spol,svect);
1.76      noro     4268:                if ( m == -1 )
                   4269:                        maxrs = ndv_reduce_vect_sf(m,svect,col,imat,rvect,nred);
                   4270:                else
                   4271:                        maxrs = ndv_reduce_vect(m,svect,col,imat,rvect,nred);
1.74      noro     4272:                for ( i = 0; i < col; i++ ) if ( svect[i] ) break;
                   4273:                if ( i < col ) {
                   4274:                        spmat[sprow] = v = (UINT *)MALLOC_ATOMIC(spcol*sizeof(UINT));
                   4275:                        for ( j = k = 0; j < col; j++ )
                   4276:                                if ( !rhead[j] ) v[k++] = svect[j];
1.76      noro     4277:                        spsugar[sprow] = MAX(maxrs,SG(spol));
1.74      noro     4278:                        sprow++;
                   4279:                }
1.76      noro     4280:                nd_free(spol);
1.74      noro     4281:        }
1.80      noro     4282:        get_eg(&eg1); init_eg(&eg_f4_1); add_eg(&eg_f4_1,&eg0,&eg1);
                   4283:        if ( DP_Print ) {
                   4284:                fprintf(asir_out,"elim1=%fsec,",eg_f4_1.exectime+eg_f4_1.gctime);
                   4285:                fflush(asir_out);
                   4286:        }
1.74      noro     4287:        /* free index arrays */
                   4288:        for ( i = 0; i < nred; i++ ) GC_free(imat[i]->index.c);
                   4289:
                   4290:        /* elimination (2nd step) */
                   4291:        colstat = (int *)ALLOCA(spcol*sizeof(int));
                   4292:        if ( m == -1 )
1.76      noro     4293:                rank = nd_gauss_elim_sf(spmat,spsugar,sprow,spcol,m,colstat);
1.74      noro     4294:        else
1.76      noro     4295:                rank = nd_gauss_elim_mod(spmat,spsugar,sprow,spcol,m,colstat);
1.74      noro     4296:        r0 = 0;
                   4297:        for ( i = 0; i < rank; i++ ) {
                   4298:                NEXTNODE(r0,r); BDY(r) =
                   4299:                        (pointer)vect_to_ndv(spmat[i],spcol,col,rhead,s0vect);
1.76      noro     4300:                SG((NDV)BDY(r)) = spsugar[i];
1.74      noro     4301:                GC_free(spmat[i]);
                   4302:        }
1.109     noro     4303:        if ( r0 ) NEXT(r) = 0;
1.74      noro     4304:        for ( ; i < sprow; i++ ) GC_free(spmat[i]);
1.80      noro     4305:        get_eg(&eg2); init_eg(&eg_f4_2); add_eg(&eg_f4_2,&eg1,&eg2);
                   4306:        init_eg(&eg_f4); add_eg(&eg_f4,&eg0,&eg2);
1.74      noro     4307:        if ( DP_Print ) {
1.80      noro     4308:                fprintf(asir_out,"elim2=%fsec\n",eg_f4_2.exectime+eg_f4_2.gctime);
1.74      noro     4309:                fprintf(asir_out,"nsp=%d,nred=%d,spmat=(%d,%d),rank=%d  ",
                   4310:                        nsp,nred,sprow,spcol,rank);
                   4311:                fprintf(asir_out,"%fsec\n",eg_f4.exectime+eg_f4.gctime);
1.63      noro     4312:        }
1.74      noro     4313:        return r0;
                   4314: }
                   4315:
1.112   ! noro     4316: NODE nd_f4_red_z_main(ND_pairs sp0,int nsp,UINT *s0vect,int col,
1.107     noro     4317:         NM_ind_pair *rvect,int *rhead,IndArray *imat,int nred)
                   4318: {
                   4319:        int spcol,sprow,a;
                   4320:        int i,j,k,l,rank;
                   4321:        NODE r0,r;
                   4322:        ND_pairs sp;
                   4323:        ND spol;
1.112   ! noro     4324:        Z **spmat;
        !          4325:        Z *svect,*v;
1.107     noro     4326:        int *colstat;
                   4327:        struct oEGT eg0,eg1,eg2,eg_f4,eg_f4_1,eg_f4_2;
                   4328:        int maxrs;
                   4329:        int *spsugar;
                   4330:
                   4331:        spcol = col-nred;
                   4332:        get_eg(&eg0);
                   4333:        /* elimination (1st step) */
1.112   ! noro     4334:        spmat = (Z **)ALLOCA(nsp*sizeof(Z *));
        !          4335:        svect = (Z *)ALLOCA(col*sizeof(Z));
        !          4336:        spsugar = (int *)ALLOCA(nsp*sizeof(Z));
1.107     noro     4337:        for ( a = sprow = 0, sp = sp0; a < nsp; a++, sp = NEXT(sp) ) {
                   4338:                nd_sp(0,0,sp,&spol);
                   4339:                if ( !spol ) continue;
1.112   ! noro     4340:                nd_to_vect_z(s0vect,col,spol,svect);
        !          4341:                maxrs = ndv_reduce_vect_z(svect,col,imat,rvect,nred);
1.107     noro     4342:                for ( i = 0; i < col; i++ ) if ( svect[i] ) break;
                   4343:                if ( i < col ) {
1.112   ! noro     4344:                        spmat[sprow] = v = (Z *)MALLOC(spcol*sizeof(Z));
1.107     noro     4345:                        for ( j = k = 0; j < col; j++ )
                   4346:                                if ( !rhead[j] ) v[k++] = svect[j];
                   4347:                        spsugar[sprow] = MAX(maxrs,SG(spol));
                   4348:                        sprow++;
                   4349:                }
1.109     noro     4350: /*             nd_free(spol); */
1.107     noro     4351:        }
                   4352:        get_eg(&eg1); init_eg(&eg_f4_1); add_eg(&eg_f4_1,&eg0,&eg1);
                   4353:        if ( DP_Print ) {
                   4354:                fprintf(asir_out,"elim1=%fsec,",eg_f4_1.exectime+eg_f4_1.gctime);
                   4355:                fflush(asir_out);
                   4356:        }
                   4357:        /* free index arrays */
1.109     noro     4358: /*     for ( i = 0; i < nred; i++ ) GC_free(imat[i]->index.c); */
1.107     noro     4359:
                   4360:        /* elimination (2nd step) */
                   4361:        colstat = (int *)ALLOCA(spcol*sizeof(int));
1.112   ! noro     4362:        rank = nd_gauss_elim_z(spmat,spsugar,sprow,spcol,colstat);
1.107     noro     4363:        r0 = 0;
                   4364:        for ( i = 0; i < rank; i++ ) {
                   4365:                NEXTNODE(r0,r); BDY(r) =
1.112   ! noro     4366:                        (pointer)vect_to_ndv_z(spmat[i],spcol,col,rhead,s0vect);
1.107     noro     4367:                SG((NDV)BDY(r)) = spsugar[i];
1.109     noro     4368: /*             GC_free(spmat[i]); */
1.107     noro     4369:        }
1.109     noro     4370:        if ( r0 ) NEXT(r) = 0;
                   4371:
                   4372: /*     for ( ; i < sprow; i++ ) GC_free(spmat[i]); */
1.107     noro     4373:        get_eg(&eg2); init_eg(&eg_f4_2); add_eg(&eg_f4_2,&eg1,&eg2);
                   4374:        init_eg(&eg_f4); add_eg(&eg_f4,&eg0,&eg2);
                   4375:        if ( DP_Print ) {
                   4376:                fprintf(asir_out,"elim2=%fsec\n",eg_f4_2.exectime+eg_f4_2.gctime);
                   4377:                fprintf(asir_out,"nsp=%d,nred=%d,spmat=(%d,%d),rank=%d  ",
                   4378:                        nsp,nred,sprow,spcol,rank);
                   4379:                fprintf(asir_out,"%fsec\n",eg_f4.exectime+eg_f4.gctime);
                   4380:        }
                   4381:        return r0;
                   4382: }
                   4383:
1.74      noro     4384: FILE *nd_write,*nd_read;
                   4385:
                   4386: void nd_send_int(int a) {
                   4387:        write_int(nd_write,&a);
                   4388: }
                   4389:
                   4390: void nd_send_intarray(int *p,int len) {
                   4391:        write_intarray(nd_write,p,len);
                   4392: }
                   4393:
                   4394: int nd_recv_int() {
                   4395:        int a;
                   4396:
                   4397:        read_int(nd_read,&a);
                   4398:        return a;
                   4399: }
                   4400:
                   4401: void nd_recv_intarray(int *p,int len) {
                   4402:        read_intarray(nd_read,p,len);
                   4403: }
                   4404:
                   4405: void nd_send_ndv(NDV p) {
                   4406:        int len,i;
                   4407:        NMV m;
                   4408:
                   4409:        if ( !p ) nd_send_int(0);
                   4410:        else {
                   4411:                len = LEN(p);
                   4412:                nd_send_int(len);
                   4413:                m = BDY(p);
                   4414:                for ( i = 0; i < len; i++, NMV_ADV(m) ) {
                   4415:                        nd_send_int(CM(m));
                   4416:                        nd_send_intarray(DL(m),nd_wpd);
1.68      noro     4417:                }
1.74      noro     4418:        }
                   4419: }
                   4420:
                   4421: void nd_send_nd(ND p) {
                   4422:        int len,i;
                   4423:        NM m;
                   4424:
                   4425:        if ( !p ) nd_send_int(0);
                   4426:        else {
                   4427:                len = LEN(p);
                   4428:                nd_send_int(len);
                   4429:                m = BDY(p);
                   4430:                for ( i = 0; i < len; i++, m = NEXT(m) ) {
                   4431:                        nd_send_int(CM(m));
                   4432:                        nd_send_intarray(DL(m),nd_wpd);
1.65      noro     4433:                }
1.74      noro     4434:        }
                   4435: }
1.65      noro     4436:
1.74      noro     4437: NDV nd_recv_ndv()
                   4438: {
                   4439:        int len,i;
                   4440:        NMV m,m0;
                   4441:        NDV r;
1.65      noro     4442:
1.74      noro     4443:        len = nd_recv_int();
                   4444:        if ( !len ) return 0;
                   4445:        else {
                   4446:                m0 = m = (NMV)GC_malloc_atomic_ignore_off_page(nmv_adv*len);
1.103     noro     4447: #if 0
1.74      noro     4448:                ndv_alloc += len*nmv_adv;
1.103     noro     4449: #endif
1.74      noro     4450:                for ( i = 0; i < len; i++, NMV_ADV(m) ) {
                   4451:                        CM(m) = nd_recv_int();
                   4452:                        nd_recv_intarray(DL(m),nd_wpd);
1.71      noro     4453:                }
1.74      noro     4454:                MKNDV(nd_nvar,m0,len,r);
                   4455:                return r;
                   4456:        }
                   4457: }
1.65      noro     4458:
1.74      noro     4459: int ox_exec_f4_red(Q proc)
                   4460: {
                   4461:        Obj obj;
                   4462:        STRING fname;
                   4463:        NODE arg;
                   4464:        int s;
                   4465:        extern int ox_need_conv,ox_file_io;
                   4466:
                   4467:        MKSTR(fname,"nd_exec_f4_red");
                   4468:        arg = mknode(2,proc,fname);
                   4469:        Pox_cmo_rpc(arg,&obj);
                   4470:        s = get_ox_server_id(QTOS(proc));
                   4471:        nd_write = iofp[s].out;
                   4472:        nd_read = iofp[s].in;
                   4473:        ox_need_conv = ox_file_io = 0;
                   4474:        return s;
                   4475: }
                   4476:
                   4477: NODE nd_f4_red_dist(int m,ND_pairs sp0,UINT *s0vect,int col,NODE rp0)
                   4478: {
                   4479:        int nsp,nred;
                   4480:        int i,rank,s;
                   4481:        NODE rp,r0,r;
                   4482:        ND_pairs sp;
                   4483:        NM_ind_pair pair;
                   4484:        NMV nmv;
                   4485:        NM nm;
                   4486:        NDV nf;
                   4487:        Obj proc,dmy;
                   4488:
                   4489:        ox_launch_main(0,0,&proc);
                   4490:        s = ox_exec_f4_red((Q)proc);
                   4491:
                   4492:        nd_send_int(m);
                   4493:        nd_send_int(nd_nvar);
                   4494:        nd_send_int(nd_bpe);
                   4495:        nd_send_int(nd_wpd);
                   4496:        nd_send_int(nmv_adv);
                   4497:
1.89      noro     4498:        saveobj(nd_write,dp_current_spec->obj); fflush(nd_write);
1.74      noro     4499:
                   4500:        nd_send_int(nd_psn);
                   4501:        for ( i = 0; i < nd_psn; i++ ) nd_send_ndv(nd_ps[i]);
                   4502:
                   4503:        for ( sp = sp0, nsp = 0; sp; sp = NEXT(sp), nsp++ );
                   4504:        nd_send_int(nsp);
                   4505:        for ( i = 0, sp = sp0; i < nsp; i++, sp = NEXT(sp) ) {
                   4506:                nd_send_int(sp->i1); nd_send_int(sp->i2);
                   4507:        }
                   4508:
                   4509:        nd_send_int(col); nd_send_intarray(s0vect,col*nd_wpd);
                   4510:
                   4511:        nred = length(rp0); nd_send_int(nred);
                   4512:        for ( i = 0, rp = rp0; i < nred; i++, rp = NEXT(rp) ) {
                   4513:                pair = (NM_ind_pair)BDY(rp);
                   4514:                nd_send_int(pair->index);
                   4515:                nd_send_intarray(pair->mul->dl,nd_wpd);
                   4516:        }
                   4517:        fflush(nd_write);
                   4518:        rank = nd_recv_int();
                   4519:        fprintf(asir_out,"rank=%d\n",rank);
                   4520:        r0 = 0;
                   4521:        for ( i = 0; i < rank; i++ ) {
                   4522:                nf = nd_recv_ndv();
                   4523:                NEXTNODE(r0,r); BDY(r) = (pointer)nf;
                   4524:        }
                   4525:        Pox_shutdown(mknode(1,proc),&dmy);
                   4526:        return r0;
                   4527: }
                   4528:
                   4529: /* server side */
                   4530:
                   4531: void nd_exec_f4_red_dist()
                   4532: {
                   4533:        int m,i,nsp,col,s0size,nred,spcol,j,k;
                   4534:        NM_ind_pair *rp0;
                   4535:        NDV nf;
                   4536:        UINT *s0vect;
                   4537:        IndArray *imat;
                   4538:        int *rhead;
                   4539:        int **spmat;
                   4540:        UINT *svect,*v;
                   4541:        ND_pairs *sp0;
                   4542:        int *colstat;
                   4543:        int a,sprow,rank;
1.89      noro     4544:        struct order_spec *ord;
1.74      noro     4545:        Obj ordspec;
                   4546:        ND spol;
1.76      noro     4547:        int maxrs;
                   4548:        int *spsugar;
1.74      noro     4549:
                   4550:        nd_read = iofp[0].in;
                   4551:        nd_write = iofp[0].out;
                   4552:        m = nd_recv_int();
                   4553:        nd_nvar = nd_recv_int();
                   4554:        nd_bpe = nd_recv_int();
                   4555:        nd_wpd = nd_recv_int();
                   4556:        nmv_adv = nd_recv_int();
                   4557:
                   4558:        loadobj(nd_read,&ordspec);
1.89      noro     4559:        create_order_spec(0,ordspec,&ord);
                   4560:        nd_init_ord(ord);
1.74      noro     4561:        nd_setup_parameters(nd_nvar,0);
                   4562:
                   4563:        nd_psn = nd_recv_int();
                   4564:        nd_ps = (NDV *)MALLOC(nd_psn*sizeof(NDV));
                   4565:        nd_bound = (UINT **)MALLOC(nd_psn*sizeof(UINT *));
                   4566:        for ( i = 0; i < nd_psn; i++ ) {
                   4567:                nd_ps[i] = nd_recv_ndv();
                   4568:                nd_bound[i] = ndv_compute_bound(nd_ps[i]);
                   4569:        }
                   4570:
                   4571:        nsp = nd_recv_int();
                   4572:        sp0 = (ND_pairs *)MALLOC(nsp*sizeof(ND_pairs));
                   4573:        for ( i = 0; i < nsp; i++ ) {
                   4574:                NEWND_pairs(sp0[i]);
                   4575:                sp0[i]->i1 = nd_recv_int(); sp0[i]->i2 = nd_recv_int();
                   4576:                ndl_lcm(HDL(nd_ps[sp0[i]->i1]),HDL(nd_ps[sp0[i]->i2]),LCM(sp0[i]));
                   4577:        }
                   4578:
                   4579:        col = nd_recv_int();
                   4580:        s0size = col*nd_wpd;
                   4581:        s0vect = (UINT *)MALLOC(s0size*sizeof(UINT));
                   4582:        nd_recv_intarray(s0vect,s0size);
                   4583:
                   4584:        nred = nd_recv_int();
                   4585:        rp0 = (NM_ind_pair *)MALLOC(nred*sizeof(NM_ind_pair));
                   4586:        for ( i = 0; i < nred; i++ ) {
                   4587:                rp0[i] = (NM_ind_pair)MALLOC(sizeof(struct oNM_ind_pair));
                   4588:                rp0[i]->index = nd_recv_int();
                   4589:                rp0[i]->mul = (NM)MALLOC(sizeof(struct oNM)+(nd_wpd-1)*sizeof(UINT));
                   4590:                nd_recv_intarray(rp0[i]->mul->dl,nd_wpd);
                   4591:        }
                   4592:
                   4593:        spcol = col-nred;
                   4594:        imat = (IndArray *)MALLOC(nred*sizeof(IndArray));
                   4595:        rhead = (int *)MALLOC(col*sizeof(int));
                   4596:        for ( i = 0; i < col; i++ ) rhead[i] = 0;
                   4597:
                   4598:        /* construction of index arrays */
                   4599:        for ( i = 0; i < nred; i++ ) {
                   4600:                imat[i] = nm_ind_pair_to_vect_compress(m,s0vect,col,rp0[i]);
                   4601:                rhead[imat[i]->head] = 1;
                   4602:        }
                   4603:
                   4604:        /* elimination (1st step) */
                   4605:        spmat = (int **)MALLOC(nsp*sizeof(UINT *));
                   4606:        svect = (UINT *)MALLOC(col*sizeof(UINT));
1.76      noro     4607:        spsugar = (int *)ALLOCA(nsp*sizeof(UINT));
1.74      noro     4608:        for ( a = sprow = 0; a < nsp; a++ ) {
                   4609:                nd_sp(m,0,sp0[a],&spol);
1.75      noro     4610:                if ( !spol ) continue;
1.74      noro     4611:                nd_to_vect(m,s0vect,col,spol,svect);
1.76      noro     4612:                if ( m == -1 )
                   4613:                        maxrs = ndv_reduce_vect_sf(m,svect,col,imat,rp0,nred);
                   4614:                else
                   4615:                        maxrs = ndv_reduce_vect(m,svect,col,imat,rp0,nred);
1.74      noro     4616:                for ( i = 0; i < col; i++ ) if ( svect[i] ) break;
                   4617:                if ( i < col ) {
                   4618:                        spmat[sprow] = v = (UINT *)MALLOC(spcol*sizeof(UINT));
                   4619:                        for ( j = k = 0; j < col; j++ )
                   4620:                                if ( !rhead[j] ) v[k++] = svect[j];
1.76      noro     4621:                        spsugar[sprow] = MAX(maxrs,SG(spol));
1.74      noro     4622:                        sprow++;
                   4623:                }
1.76      noro     4624:                nd_free(spol);
1.74      noro     4625:        }
                   4626:        /* elimination (2nd step) */
                   4627:        colstat = (int *)ALLOCA(spcol*sizeof(int));
                   4628:        if ( m == -1 )
1.76      noro     4629:                rank = nd_gauss_elim_sf(spmat,spsugar,sprow,spcol,m,colstat);
1.74      noro     4630:        else
1.76      noro     4631:                rank = nd_gauss_elim_mod(spmat,spsugar,sprow,spcol,m,colstat);
1.74      noro     4632:        nd_send_int(rank);
                   4633:        for ( i = 0; i < rank; i++ ) {
                   4634:                nf = vect_to_ndv(spmat[i],spcol,col,rhead,s0vect);
                   4635:                nd_send_ndv(nf);
1.63      noro     4636:        }
1.74      noro     4637:        fflush(nd_write);
1.107     noro     4638: }
                   4639:
1.112   ! noro     4640: /* XXX */
        !          4641: int generic_gauss_elim_z(MAT m,MAT *nm,Z *dn,int **ri,int **ci)
        !          4642: {}
        !          4643:
        !          4644: int nd_gauss_elim_z(Z **mat0,int *sugar,int row,int col,int *colstat)
1.107     noro     4645: {
1.109     noro     4646:        int mod,i,j,t,c,rank,rank0,inv;
                   4647:        int *ci,*ri;
1.112   ! noro     4648:        Z dn;
1.109     noro     4649:        MAT m,nm;
1.108     noro     4650:        int **wmat;
                   4651:
                   4652:        /* XXX */
                   4653:        mod = 99999989;
                   4654:        wmat = (int **)ALLOCA(row*sizeof(int *));
                   4655:        for ( i = 0; i < row; i++ ) {
                   4656:                wmat[i] = (int *)ALLOCA(col*sizeof(int));
                   4657:                for ( j = 0; j < col; j++ ) {
1.112   ! noro     4658:                        if ( mat0[i][j] )
        !          4659:                                wmat[i][j] = remzi(mat0[i][j],mod);
        !          4660:                        else
1.108     noro     4661:                                wmat[i][j] = 0;
                   4662:                }
                   4663:        }
1.109     noro     4664:        rank0 = nd_gauss_elim_mod(wmat,sugar,row,col,mod,colstat);
                   4665:        NEWMAT(m); m->row = row; m->col = col; m->body = (pointer **)mat0;
1.112   ! noro     4666:        rank = generic_gauss_elim_z(m,&nm,&dn,&ri,&ci);
1.109     noro     4667:        if ( rank != rank0 )
                   4668:                error("afo");
                   4669:        for ( i = 0; i < row; i++ )
                   4670:                for ( j = 0; j < col; j++ )
                   4671:                        mat0[i][j] = 0;
                   4672:        c = col-rank;
                   4673:        for ( i = 0; i < rank; i++ ) {
1.112   ! noro     4674:                mat0[i][ri[i]] = dn;
1.109     noro     4675:                for ( j = 0; j < c; j++ )
1.112   ! noro     4676:                        mat0[i][ci[j]] = (Z)BDY(nm)[i][j];
1.109     noro     4677:        }
1.112   ! noro     4678:        inv = invm(remzi(dn,mod),mod);
1.109     noro     4679:        for ( i = 0; i < row; i++ )
                   4680:                for ( j = 0; j < col; j++ ) {
1.112   ! noro     4681:                        if ( mat0[i][j] )
        !          4682:                                t = remzi(mat0[i][j],mod);
        !          4683:                        else
1.109     noro     4684:                                t = 0;
                   4685:                        c = dmar(t,inv,0,mod);
                   4686:                        if ( wmat[i][j] != c )
                   4687:                                error("afo");
                   4688:                }
                   4689:        return rank;
1.76      noro     4690: }
                   4691:
                   4692: int nd_gauss_elim_mod(int **mat0,int *sugar,int row,int col,int md,int *colstat)
                   4693: {
                   4694:        int i,j,k,l,inv,a,rank,s;
                   4695:        unsigned int *t,*pivot,*pk;
                   4696:        unsigned int **mat;
                   4697:
                   4698:        mat = (unsigned int **)mat0;
                   4699:        for ( rank = 0, j = 0; j < col; j++ ) {
                   4700:                for ( i = rank; i < row; i++ )
                   4701:                        mat[i][j] %= md;
                   4702:                for ( i = rank; i < row; i++ )
                   4703:                        if ( mat[i][j] )
                   4704:                                break;
                   4705:                if ( i == row ) {
                   4706:                        colstat[j] = 0;
                   4707:                        continue;
                   4708:                } else
                   4709:                        colstat[j] = 1;
                   4710:                if ( i != rank ) {
                   4711:                        t = mat[i]; mat[i] = mat[rank]; mat[rank] = t;
                   4712:                        s = sugar[i]; sugar[i] = sugar[rank]; sugar[rank] = s;
                   4713:                }
                   4714:                pivot = mat[rank];
                   4715:                s = sugar[rank];
                   4716:                inv = invm(pivot[j],md);
                   4717:                for ( k = j, pk = pivot+k; k < col; k++, pk++ )
                   4718:                        if ( *pk ) {
                   4719:                                if ( *pk >= (unsigned int)md )
                   4720:                                        *pk %= md;
                   4721:                                DMAR(*pk,inv,0,md,*pk)
                   4722:                        }
                   4723:                for ( i = rank+1; i < row; i++ ) {
                   4724:                        t = mat[i];
                   4725:                        if ( a = t[j] ) {
                   4726:                                sugar[i] = MAX(sugar[i],s);
                   4727:                                red_by_vect(md,t+j,pivot+j,md-a,col-j);
                   4728:                        }
                   4729:                }
                   4730:                rank++;
                   4731:        }
                   4732:        for ( j = col-1, l = rank-1; j >= 0; j-- )
                   4733:                if ( colstat[j] ) {
                   4734:                        pivot = mat[l];
                   4735:                        s = sugar[l];
                   4736:                        for ( i = 0; i < l; i++ ) {
                   4737:                                t = mat[i];
                   4738:                                t[j] %= md;
                   4739:                                if ( a = t[j] ) {
                   4740:                                        sugar[i] = MAX(sugar[i],s);
                   4741:                                        red_by_vect(md,t+j,pivot+j,md-a,col-j);
                   4742:                                }
                   4743:                        }
                   4744:                        l--;
                   4745:                }
                   4746:        for ( j = 0, l = 0; l < rank; j++ )
                   4747:                if ( colstat[j] ) {
                   4748:                        t = mat[l];
                   4749:                        for ( k = j; k < col; k++ )
                   4750:                                if ( t[k] >= (unsigned int)md )
                   4751:                                        t[k] %= md;
                   4752:                        l++;
                   4753:                }
                   4754:        return rank;
                   4755: }
                   4756:
                   4757: int nd_gauss_elim_sf(int **mat0,int *sugar,int row,int col,int md,int *colstat)
                   4758: {
                   4759:        int i,j,k,l,inv,a,rank,s;
                   4760:        unsigned int *t,*pivot,*pk;
                   4761:        unsigned int **mat;
                   4762:
                   4763:        mat = (unsigned int **)mat0;
                   4764:        for ( rank = 0, j = 0; j < col; j++ ) {
                   4765:                for ( i = rank; i < row; i++ )
                   4766:                        if ( mat[i][j] )
                   4767:                                break;
                   4768:                if ( i == row ) {
                   4769:                        colstat[j] = 0;
                   4770:                        continue;
                   4771:                } else
                   4772:                        colstat[j] = 1;
                   4773:                if ( i != rank ) {
                   4774:                        t = mat[i]; mat[i] = mat[rank]; mat[rank] = t;
                   4775:                        s = sugar[i]; sugar[i] = sugar[rank]; sugar[rank] = s;
                   4776:                }
                   4777:                pivot = mat[rank];
                   4778:                s = sugar[rank];
                   4779:                inv = _invsf(pivot[j]);
                   4780:                for ( k = j, pk = pivot+k; k < col; k++, pk++ )
                   4781:                        if ( *pk )
                   4782:                                *pk = _mulsf(*pk,inv);
                   4783:                for ( i = rank+1; i < row; i++ ) {
                   4784:                        t = mat[i];
                   4785:                        if ( a = t[j] ) {
                   4786:                                sugar[i] = MAX(sugar[i],s);
                   4787:                                red_by_vect_sf(md,t+j,pivot+j,_chsgnsf(a),col-j);
                   4788:                        }
                   4789:                }
                   4790:                rank++;
                   4791:        }
                   4792:        for ( j = col-1, l = rank-1; j >= 0; j-- )
                   4793:                if ( colstat[j] ) {
                   4794:                        pivot = mat[l];
                   4795:                        s = sugar[l];
                   4796:                        for ( i = 0; i < l; i++ ) {
                   4797:                                t = mat[i];
                   4798:                                if ( a = t[j] ) {
                   4799:                                        sugar[i] = MAX(sugar[i],s);
                   4800:                                        red_by_vect_sf(md,t+j,pivot+j,_chsgnsf(a),col-j);
                   4801:                                }
                   4802:                        }
                   4803:                        l--;
                   4804:                }
                   4805:        return rank;
1.77      noro     4806: }
                   4807:
                   4808: int ndv_ishomo(NDV p)
                   4809: {
                   4810:        NMV m;
                   4811:        int len,h;
                   4812:
                   4813:        if ( !p ) return 1;
                   4814:        len = LEN(p);
                   4815:        m = BDY(p);
                   4816:        h = TD(DL(m));
                   4817:        NMV_ADV(m);
                   4818:        for ( len--; len; len--, NMV_ADV(m) )
                   4819:                if ( TD(DL(m)) != h ) return 0;
                   4820:        return 1;
                   4821: }
                   4822:
                   4823: void ndv_save(NDV p,int index)
                   4824: {
                   4825:        FILE *s;
                   4826:        char name[BUFSIZ];
                   4827:        short id;
                   4828:        int nv,sugar,len,n,i,td,e,j;
                   4829:        NMV m;
                   4830:        unsigned int *dl;
                   4831:
                   4832:        sprintf(name,"%s/%d",Demand,index);
                   4833:        s = fopen(name,"w");
                   4834:        savevl(s,0);
                   4835:        if ( !p ) {
                   4836:                saveobj(s,0);
                   4837:                return;
                   4838:        }
                   4839:        id = O_DP;
                   4840:        nv = NV(p);
                   4841:        sugar = SG(p);
                   4842:        len = LEN(p);
                   4843:        write_short(s,&id); write_int(s,&nv); write_int(s,&sugar);
                   4844:        write_int(s,&len);
                   4845:
                   4846:        for ( m = BDY(p), i = 0; i < len; i++, NMV_ADV(m) ) {
1.112   ! noro     4847:                saveobj(s,(Obj)ztoq(CZ(m)));
1.77      noro     4848:                dl = DL(m);
                   4849:                td = TD(dl);
                   4850:                write_int(s,&td);
                   4851:                for ( j = 0; j < nv; j++ ) {
                   4852:                        e = GET_EXP(dl,j);
                   4853:                        write_int(s,&e);
                   4854:                }
                   4855:        }
                   4856:        fclose(s);
                   4857: }
                   4858:
                   4859: NDV ndv_load(int index)
                   4860: {
                   4861:        FILE *s;
                   4862:        char name[BUFSIZ];
                   4863:        short id;
                   4864:        int nv,sugar,len,n,i,td,e,j;
                   4865:        NDV d;
                   4866:        NMV m0,m;
                   4867:        unsigned int *dl;
                   4868:        Obj obj;
                   4869:
                   4870:        sprintf(name,"%s/%d",Demand,index);
                   4871:        s = fopen(name,"r");
                   4872:        if ( !s ) return 0;
                   4873:
                   4874:        skipvl(s);
                   4875:        read_short(s,&id);
                   4876:        if ( !id ) return 0;
                   4877:        read_int(s,&nv);
                   4878:        read_int(s,&sugar);
                   4879:        read_int(s,&len);
                   4880:
                   4881:        m0 = m = MALLOC(len*nmv_adv);
                   4882:        for ( i = 0; i < len; i++, NMV_ADV(m) ) {
1.112   ! noro     4883:                loadobj(s,&obj); CZ(m) = qtoz((Q)obj);
1.77      noro     4884:                dl = DL(m);
                   4885:                ndl_zero(dl);
                   4886:                read_int(s,&td); TD(dl) = td;
                   4887:                for ( j = 0; j < nv; j++ ) {
                   4888:                        read_int(s,&e);
                   4889:                        PUT_EXP(dl,j,e);
                   4890:                }
                   4891:                if ( nd_blockmask ) ndl_weight_mask(dl);
                   4892:        }
                   4893:        fclose(s);
                   4894:        MKNDV(nv,m0,len,d);
                   4895:        SG(d) = sugar;
                   4896:        return d;
1.99      noro     4897: }
                   4898:
1.102     noro     4899: void nd_det(int mod,MAT f,P *rp)
1.99      noro     4900: {
                   4901:        VL fv,tv;
1.102     noro     4902:        int n,i,j,max,e,nvar,sgn,k0,l0,len0,len,k,l,a;
1.99      noro     4903:        pointer **m;
1.112   ! noro     4904:        Z mone;
1.102     noro     4905:        NDV **dm;
                   4906:        NDV *t,*mi,*mj;
                   4907:        NDV d,s,mij,mjj;
                   4908:        ND u;
                   4909:        NMV nmv;
1.99      noro     4910:        PGeoBucket bucket;
                   4911:        struct order_spec *ord;
                   4912:
                   4913:        create_order_spec(0,0,&ord);
                   4914:        nd_init_ord(ord);
                   4915:        get_vars((Obj)f,&fv);
                   4916:        if ( f->row != f->col )
                   4917:                error("nd_det : non-square matrix");
                   4918:        n = f->row;
                   4919:        for ( nvar = 0, tv = fv; tv; tv = NEXT(tv), nvar++ );
                   4920:        m = f->body;
                   4921:        for ( i = 0, max = 0; i < n; i++ )
                   4922:                for ( j = 0; j < n; j++ )
                   4923:                        for ( tv = fv; tv; tv = NEXT(tv) ) {
                   4924:                                e = getdeg(tv->v,(P)m[i][j]);
                   4925:                                max = MAX(e,max);
                   4926:                        }
                   4927:        nd_setup_parameters(nvar,1024);
1.102     noro     4928:        dm = (NDV **)almat_pointer(n,n);
1.99      noro     4929:        for ( i = 0, max = 0; i < n; i++ )
1.102     noro     4930:                for ( j = 0; j < n; j++ ) {
                   4931:                        dm[i][j] = ptondv(CO,fv,m[i][j]);
                   4932:                        if ( mod ) ndv_mod(mod,dm[i][j]);
                   4933:                        if ( dm[i][j] && !LEN(dm[i][j]) ) dm[i][j] = 0;
                   4934:                }
                   4935:        d = ptondv(CO,fv,(P)ONE);
                   4936:        if ( mod ) ndv_mod(mod,d);
1.112   ! noro     4937:        mone = chsgnz(qtoz(ONE));
1.99      noro     4938:        for ( j = 0, sgn = 1; j < n; j++ ) {
1.104     noro     4939:                if ( DP_Print ) fprintf(stderr,"j=%d\n",j);
1.99      noro     4940:                for ( i = j; i < n && !dm[i][j]; i++ );
                   4941:                if ( i == n ) {
                   4942:                        *rp = 0;
                   4943:                        return;
                   4944:                }
1.102     noro     4945:                k0 = i; l0 = j; len0 = LEN(dm[k0][l0]);
1.99      noro     4946:                for ( k = j; k < n; k++ )
                   4947:                        for ( l = j; l < n; l++ )
1.102     noro     4948:                                if ( dm[k][l] && LEN(dm[k][l]) < len0 ) {
                   4949:                                        k0 = k; l0 = l; len0 = LEN(dm[k][l]);
1.99      noro     4950:                                }
                   4951:                if ( k0 != j ) {
                   4952:                        t = dm[j]; dm[j] = dm[k0]; dm[k0] = t;
                   4953:                        sgn = -sgn;
                   4954:                }
                   4955:                if ( l0 != j ) {
                   4956:                        for ( k = j; k < n; k++ ) {
                   4957:                                s = dm[k][j]; dm[k][j] = dm[k][l0]; dm[k][l0] = s;
                   4958:                        }
                   4959:                        sgn = -sgn;
                   4960:                }
                   4961:                for ( i = j+1, mj = dm[j], mjj = mj[j]; i < n; i++ ) {
1.104     noro     4962:                        if ( DP_Print ) fprintf(stderr,"        i=%d\n          ",i);
1.99      noro     4963:                        mi = dm[i]; mij = mi[j];
1.102     noro     4964:                        if ( mod )
                   4965:                                ndv_mul_c(mod,mij,mod-1);
                   4966:                        else
1.112   ! noro     4967:                                ndv_mul_c_z(mij,mone);
1.99      noro     4968:                        for ( k = j+1; k < n; k++ ) {
1.104     noro     4969:                                if ( DP_Print ) fprintf(stderr,"k=%d ",k);
1.99      noro     4970:                                bucket = create_pbucket();
1.104     noro     4971:                                if ( mi[k] ) {
1.102     noro     4972:                                        nmv = BDY(mjj); len = LEN(mjj);
                   4973:                                        for ( a = 0; a < len; a++, NMV_ADV(nmv) ) {
                   4974:                                                u = ndv_mul_nmv_trunc(mod,nmv,mi[k],DL(BDY(d)));
                   4975:                                                add_pbucket(mod,bucket,u);
1.99      noro     4976:                                        }
1.104     noro     4977:                                }
1.99      noro     4978:                                if ( mj[k] && mij ) {
1.102     noro     4979:                                        nmv = BDY(mij); len = LEN(mij);
                   4980:                                        for ( a = 0; a < len; a++, NMV_ADV(nmv) ) {
                   4981:                                                u = ndv_mul_nmv_trunc(mod,nmv,mj[k],DL(BDY(d)));
                   4982:                                                add_pbucket(mod,bucket,u);
1.99      noro     4983:                                        }
                   4984:                                }
1.104     noro     4985:                                u = nd_quo(mod,bucket,d);
1.102     noro     4986:                                mi[k] = ndtondv(mod,u);
1.99      noro     4987:                        }
1.104     noro     4988:                        if ( DP_Print ) fprintf(stderr,"\n",k);
1.99      noro     4989:                }
                   4990:                d = mjj;
                   4991:        }
1.100     noro     4992:        if ( sgn < 0 )
1.102     noro     4993:                if ( mod )
                   4994:                        ndv_mul_c(mod,d,mod-1);
                   4995:                else
1.112   ! noro     4996:                        ndv_mul_c_z(d,mone);
1.102     noro     4997:        *rp = ndvtop(mod,CO,fv,d);
1.99      noro     4998: }
                   4999:
1.102     noro     5000: ND ndv_mul_nmv_trunc(int mod,NMV m0,NDV p,UINT *d)
1.99      noro     5001: {
                   5002:        NM mr,mr0;
1.102     noro     5003:        NM tnm;
                   5004:        NMV m;
1.99      noro     5005:        UINT *d0,*dt,*dm;
                   5006:        int c,n,td,i,c1,c2,len;
1.112   ! noro     5007:        Z q;
1.99      noro     5008:        ND r;
                   5009:
                   5010:        if ( !p ) return 0;
                   5011:        else {
1.102     noro     5012:                n = NV(p); m = BDY(p); len = LEN(p);
1.99      noro     5013:                d0 = DL(m0);
1.102     noro     5014:                td = TD(d);
1.99      noro     5015:                mr0 = 0;
                   5016:                NEWNM(tnm);
1.102     noro     5017:                if ( mod ) {
                   5018:                        c = CM(m0);
                   5019:                        for ( i = 0; i < len; i++, NMV_ADV(m) ) {
                   5020:                                ndl_add(DL(m),d0,DL(tnm));
                   5021:                                if ( ndl_reducible(DL(tnm),d) ) {
                   5022:                                        NEXTNM(mr0,mr);
                   5023:                                        c1 = CM(m); DMAR(c1,c,0,mod,c2); CM(mr) = c2;
1.104     noro     5024:                                        ndl_copy(DL(tnm),DL(mr));
1.102     noro     5025:                                }
                   5026:                        }
                   5027:                } else {
1.112   ! noro     5028:                        q = CZ(m0);
1.102     noro     5029:                        for ( i = 0; i < len; i++, NMV_ADV(m) ) {
                   5030:                                ndl_add(DL(m),d0,DL(tnm));
                   5031:                                if ( ndl_reducible(DL(tnm),d) ) {
                   5032:                                        NEXTNM(mr0,mr);
1.112   ! noro     5033:                                        CZ(mr) = mulz(CZ(m),q);
1.104     noro     5034:                                        ndl_copy(DL(tnm),DL(mr));
1.102     noro     5035:                                }
1.99      noro     5036:                        }
                   5037:                }
                   5038:                if ( !mr0 )
                   5039:                        return 0;
                   5040:                else {
                   5041:                        NEXT(mr) = 0;
1.105     noro     5042:                        for ( len = 0, mr = mr0; mr; mr = NEXT(mr), len++ );
1.99      noro     5043:                        MKND(NV(p),mr0,len,r);
                   5044:                        SG(r) = SG(p) + TD(d0);
                   5045:                        return r;
                   5046:                }
                   5047:        }
1.59      noro     5048: }

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