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

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

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