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

1.118   ! noro        1: /* $OpenXM: OpenXM_contrib2/asir2000/engine/nd.c,v 1.117 2004/12/03 08:57:30 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.118   ! noro       47: int nd_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.118   ! noro     1624:                        if ( !m && nd_nalg ) {
        !          1625:                                nd_monic(0,&nf);
        !          1626:                                nd_removecont(m,nf);
1.117     noro     1627:                        }
1.63      noro     1628:                        nfv = ndtondv(m,nf); nd_free(nf);
1.77      noro     1629:                        nh = ndv_newps(m,nfv,0);
1.1       noro     1630:                        d = update_pairs(d,g,nh);
                   1631:                        g = update_base(g,nh);
                   1632:                        FREENDP(l);
                   1633:                } else {
1.71      noro     1634:                        if ( DP_Print ) { printf("."); fflush(stdout); }
1.1       noro     1635:                        FREENDP(l);
                   1636:                }
                   1637:        }
1.77      noro     1638:        if ( nd_demand )
                   1639:                for ( t = g; t; t = NEXT(t) )
                   1640:                        BDY(t) = (pointer)ndv_load((int)BDY(t));
                   1641:        else
                   1642:                for ( t = g; t; t = NEXT(t) )
                   1643:                        BDY(t) = (pointer)nd_ps[(int)BDY(t)];
1.1       noro     1644:        return g;
                   1645: }
                   1646:
1.92      noro     1647: void do_diagonalize_trace(int sugar,int m)
1.91      noro     1648: {
                   1649:        int i,nh,stat;
                   1650:        NODE r,g,t;
                   1651:        ND h,nf,nfq,s,head;
                   1652:        NDV nfv,nfqv;
1.113     noro     1653:        Q q,den,num;
                   1654:        union oNDC dn;
1.91      noro     1655:
                   1656:        for ( i = nd_psn-1; i >= 0 && SG(nd_psh[i]) == sugar; i-- ) {
                   1657:                /* for nd_ps */
                   1658:                s = ndvtond(m,nd_ps[i]);
                   1659:                s = nd_separate_head(s,&head);
                   1660:                nd_nf_pbucket(m,s,nd_ps,1,&nf);
                   1661:                nf = nd_add(m,head,nf);
                   1662:                ndv_free(nd_ps[i]);
                   1663:                nd_ps[i] = ndtondv(m,nf);
                   1664:                nd_free(nf);
                   1665:
                   1666:                /* for nd_ps_trace */
                   1667:                if ( nd_demand )
                   1668:                        nfv = ndv_load(i);
                   1669:                else
                   1670:                        nfv = nd_ps_trace[i];
                   1671:                s = ndvtond(0,nfv);
                   1672:                s = nd_separate_head(s,&head);
                   1673:                nd_nf(0,s,nd_ps_trace,1,&dn,&nf);
1.113     noro     1674:                NTOQ(NM(dn.z),SGN(dn.z),num);
                   1675:                mulq(HCQ(head),num,&q); HCQ(head) = q;
                   1676:                if ( DN(dn.z) ) {
                   1677:                        NTOQ(DN(dn.z),1,den);
                   1678:                        nd_mul_c_q(nf,den);
                   1679:                }
1.91      noro     1680:                nf = nd_add(0,head,nf);
                   1681:                ndv_free(nfv);
                   1682:                nd_removecont(0,nf);
                   1683:                nfv = ndtondv(0,nf);
                   1684:                nd_free(nf);
                   1685:                nd_bound[i] = ndv_compute_bound(nfv);
                   1686:                if ( nd_demand ) {
                   1687:                        ndv_save(nfv,i);
                   1688:                        ndv_free(nfv);
                   1689:                } else
                   1690:                        nd_ps_trace[i] = nfv;
                   1691:        }
                   1692: }
                   1693:
1.118   ! noro     1694: static struct oEGT eg_invdalg;
        !          1695: struct oEGT eg_le;
        !          1696:
1.82      noro     1697: NODE nd_gb_trace(int m,int ishomo)
1.20      noro     1698: {
                   1699:        int i,nh,sugar,stat;
1.23      noro     1700:        NODE r,g,t;
1.20      noro     1701:        ND_pairs d;
                   1702:        ND_pairs l;
1.82      noro     1703:        ND h,nf,nfq,s,head;
1.63      noro     1704:        NDV nfv,nfqv;
1.113     noro     1705:        Q q,den,num;
                   1706:        union oNDC dn;
1.118   ! noro     1707:        struct oEGT eg_monic,egm0,egm1;
1.20      noro     1708:
1.118   ! noro     1709:        init_eg(&eg_monic);
        !          1710:        init_eg(&eg_invdalg);
        !          1711:        init_eg(&eg_le);
1.23      noro     1712:        g = 0; d = 0;
                   1713:        for ( i = 0; i < nd_psn; i++ ) {
1.20      noro     1714:                d = update_pairs(d,g,i);
                   1715:                g = update_base(g,i);
                   1716:        }
                   1717:        sugar = 0;
                   1718:        while ( d ) {
                   1719: again:
                   1720:                l = nd_minp(d,&d);
                   1721:                if ( SG(l) != sugar ) {
1.93      noro     1722:                        if ( ishomo ) do_diagonalize_trace(sugar,m);
1.20      noro     1723:                        sugar = SG(l);
1.71      noro     1724:                        if ( DP_Print ) fprintf(asir_out,"%d",sugar);
1.20      noro     1725:                }
1.53      noro     1726:                stat = nd_sp(m,0,l,&h);
1.20      noro     1727:                if ( !stat ) {
                   1728:                        NEXT(l) = d; d = l;
1.103     noro     1729:                        d = nd_reconstruct(1,d);
1.20      noro     1730:                        goto again;
                   1731:                }
1.41      noro     1732: #if USE_GEOBUCKET
1.53      noro     1733:                stat = nd_nf_pbucket(m,h,nd_ps,!Top,&nf);
1.41      noro     1734: #else
1.69      noro     1735:                stat = nd_nf(m,h,nd_ps,!Top,0,&nf);
1.41      noro     1736: #endif
1.20      noro     1737:                if ( !stat ) {
                   1738:                        NEXT(l) = d; d = l;
1.103     noro     1739:                        d = nd_reconstruct(1,d);
1.20      noro     1740:                        goto again;
                   1741:                } else if ( nf ) {
1.77      noro     1742:                        if ( nd_demand ) {
                   1743:                                nfqv = ndv_load(nd_psn);
                   1744:                                nfq = ndvtond(0,nfqv);
                   1745:                        } else
                   1746:                                nfq = 0;
                   1747:                        if ( !nfq ) {
                   1748:                                if ( !nd_sp(0,1,l,&h) || !nd_nf(0,h,nd_ps_trace,!Top,0,&nfq) ) {
                   1749:                                        NEXT(l) = d; d = l;
1.103     noro     1750:                                        d = nd_reconstruct(1,d);
1.77      noro     1751:                                        goto again;
                   1752:                                }
1.70      noro     1753:                        }
1.20      noro     1754:                        if ( nfq ) {
1.70      noro     1755:                                /* m|HC(nfq) => failure */
1.113     noro     1756:                                if ( !rem(NM(HCQ(nfq)),m) ) return 0;
1.63      noro     1757:
1.71      noro     1758:                                if ( DP_Print ) { printf("+"); fflush(stdout); }
1.118   ! noro     1759:                                if ( nd_nalg ) {
        !          1760:                                        /* m|DN(HC(nf)^(-1)) => failure */
        !          1761:                                        get_eg(&egm0);
        !          1762:                                        if ( !nd_monic(m,&nfq) ) return 0;
        !          1763:                                        get_eg(&egm1); add_eg(&eg_monic,&egm0,&egm1);
        !          1764:                                        nd_removecont(0,nfq); nfqv = ndtondv(0,nfq); nd_free(nfq);
        !          1765:                                        nfv = ndv_dup(0,nfqv); ndv_mod(m,nfv); nd_free(nf);
        !          1766:                                } else {
        !          1767:                                        nd_removecont(0,nfq); nfqv = ndtondv(0,nfq); nd_free(nfq);
        !          1768:                                        nd_removecont(m,nf); nfv = ndtondv(m,nf); nd_free(nf);
        !          1769:                                }
1.77      noro     1770:                                nh = ndv_newps(0,nfv,nfqv);
1.20      noro     1771:                                d = update_pairs(d,g,nh);
                   1772:                                g = update_base(g,nh);
                   1773:                        } else {
1.71      noro     1774:                                if ( DP_Print ) { printf("*"); fflush(stdout); }
1.20      noro     1775:                        }
                   1776:                } else {
1.71      noro     1777:                        if ( DP_Print ) { printf("."); fflush(stdout); }
1.20      noro     1778:                }
                   1779:                FREENDP(l);
                   1780:        }
1.77      noro     1781:        if ( nd_demand )
                   1782:                for ( t = g; t; t = NEXT(t) )
                   1783:                        BDY(t) = (pointer)ndv_load((int)BDY(t));
                   1784:        else
                   1785:                for ( t = g; t; t = NEXT(t) )
                   1786:                        BDY(t) = (pointer)nd_ps_trace[(int)BDY(t)];
1.118   ! noro     1787:        if ( nd_nalg ) {
        !          1788:                print_eg("monic",&eg_monic);
        !          1789:                print_eg("invdalg",&eg_invdalg);
        !          1790:                print_eg("le",&eg_le);
        !          1791:        }
1.20      noro     1792:        return g;
                   1793: }
                   1794:
1.23      noro     1795: int ndv_compare(NDV *p1,NDV *p2)
                   1796: {
1.34      noro     1797:        return DL_COMPARE(HDL(*p1),HDL(*p2));
1.23      noro     1798: }
                   1799:
                   1800: int ndv_compare_rev(NDV *p1,NDV *p2)
                   1801: {
1.34      noro     1802:        return -DL_COMPARE(HDL(*p1),HDL(*p2));
1.23      noro     1803: }
                   1804:
1.61      noro     1805: NODE ndv_reduceall(int m,NODE f)
1.23      noro     1806: {
1.69      noro     1807:        int i,n,stat;
                   1808:        ND nf,g,head;
1.23      noro     1809:        NODE t,a0,a;
1.113     noro     1810:        union oNDC dn;
1.69      noro     1811:        NDV *w;
1.113     noro     1812:        Q q,num,den;
1.23      noro     1813:
1.69      noro     1814:        n = length(f);
                   1815: #if 0
                   1816:        w = (NDV *)ALLOCA(n*sizeof(NDV));
                   1817:        for ( i = 0, t = f; i < n; i++, t = NEXT(t) ) w[i] = (NDV)BDY(t);
                   1818:        qsort(w,n,sizeof(NDV),
                   1819:                (int (*)(const void *,const void *))ndv_compare);
                   1820:        for ( t = f, i = 0; t; i++, t = NEXT(t) ) BDY(t) = (pointer)w[i];
                   1821: #endif
                   1822:        ndv_setup(m,0,f);
                   1823:        for ( i = 0; i < n; ) {
                   1824:                g = ndvtond(m,nd_ps[i]);
                   1825:                g = nd_separate_head(g,&head);
                   1826:                stat = nd_nf(m,g,nd_ps,1,&dn,&nf);
1.50      noro     1827:                if ( !stat )
1.103     noro     1828:                        nd_reconstruct(0,0);
1.69      noro     1829:                else {
1.71      noro     1830:                        if ( DP_Print ) { printf("."); fflush(stdout); }
1.85      noro     1831:                        if ( !m ) {
1.113     noro     1832:                                NTOQ(NM(dn.z),SGN(dn.z),num);
                   1833:                                mulq(HCQ(head),num,&q); HCQ(head) = q;
                   1834:                                if ( DN(dn.z) ) {
                   1835:                                        NTOQ(DN(dn.z),1,den);
                   1836:                                        nd_mul_c_q(nf,den);
                   1837:                                }
1.85      noro     1838:                        }
1.69      noro     1839:                        nf = nd_add(m,head,nf);
                   1840:                        ndv_free(nd_ps[i]);
1.24      noro     1841:                        nd_removecont(m,nf);
1.69      noro     1842:                        nd_ps[i] = ndtondv(m,nf); nd_free(nf);
                   1843:                        nd_bound[i] = ndv_compute_bound(nd_ps[i]);
1.50      noro     1844:                        i++;
1.23      noro     1845:                }
                   1846:        }
1.71      noro     1847:        if ( DP_Print ) { printf("\n"); }
1.23      noro     1848:        for ( a0 = 0, i = 0; i < n; i++ ) {
                   1849:                NEXTNODE(a0,a);
1.69      noro     1850:                BDY(a) = (pointer)nd_ps[i];
1.23      noro     1851:        }
                   1852:        NEXT(a) = 0;
                   1853:        return a0;
                   1854: }
                   1855:
1.1       noro     1856: ND_pairs update_pairs( ND_pairs d, NODE /* of index */ g, int t)
                   1857: {
                   1858:        ND_pairs d1,nd,cur,head,prev,remove;
                   1859:
                   1860:        if ( !g ) return d;
                   1861:        d = crit_B(d,t);
                   1862:        d1 = nd_newpairs(g,t);
                   1863:        d1 = crit_M(d1);
                   1864:        d1 = crit_F(d1);
1.55      noro     1865:        if ( do_weyl )
                   1866:                head = d1;
                   1867:        else {
                   1868:                prev = 0; cur = head = d1;
                   1869:                while ( cur ) {
                   1870:                        if ( crit_2( cur->i1,cur->i2 ) ) {
                   1871:                                remove = cur;
                   1872:                                if ( !prev ) head = cur = NEXT(cur);
                   1873:                                else cur = NEXT(prev) = NEXT(cur);
                   1874:                                FREENDP(remove);
                   1875:                        } else {
                   1876:                                prev = cur; cur = NEXT(cur);
                   1877:                        }
1.1       noro     1878:                }
                   1879:        }
                   1880:        if ( !d )
                   1881:                return head;
                   1882:        else {
                   1883:                nd = d;
1.34      noro     1884:                while ( NEXT(nd) ) nd = NEXT(nd);
1.1       noro     1885:                NEXT(nd) = head;
                   1886:                return d;
                   1887:        }
                   1888: }
                   1889:
                   1890: ND_pairs nd_newpairs( NODE g, int t )
                   1891: {
                   1892:        NODE h;
1.61      noro     1893:        UINT *dl;
1.34      noro     1894:        int ts,s;
1.1       noro     1895:        ND_pairs r,r0;
                   1896:
1.20      noro     1897:        dl = DL(nd_psh[t]);
1.34      noro     1898:        ts = SG(nd_psh[t]) - TD(dl);
1.1       noro     1899:        for ( r0 = 0, h = g; h; h = NEXT(h) ) {
                   1900:                NEXTND_pairs(r0,r);
                   1901:                r->i1 = (int)BDY(h);
                   1902:                r->i2 = t;
1.20      noro     1903:                ndl_lcm(DL(nd_psh[r->i1]),dl,r->lcm);
1.34      noro     1904:                s = SG(nd_psh[r->i1])-TD(DL(nd_psh[r->i1]));
                   1905:                SG(r) = MAX(s,ts) + TD(LCM(r));
1.1       noro     1906:        }
                   1907:        NEXT(r) = 0;
                   1908:        return r0;
                   1909: }
                   1910:
                   1911: ND_pairs crit_B( ND_pairs d, int s )
                   1912: {
                   1913:        ND_pairs cur,head,prev,remove;
1.61      noro     1914:        UINT *t,*tl,*lcm;
1.1       noro     1915:        int td,tdl;
                   1916:
                   1917:        if ( !d ) return 0;
1.20      noro     1918:        t = DL(nd_psh[s]);
1.1       noro     1919:        prev = 0;
                   1920:        head = cur = d;
1.61      noro     1921:        lcm = (UINT *)ALLOCA(nd_wpd*sizeof(UINT));
1.1       noro     1922:        while ( cur ) {
                   1923:                tl = cur->lcm;
                   1924:                if ( ndl_reducible(tl,t)
1.20      noro     1925:                        && (ndl_lcm(DL(nd_psh[cur->i1]),t,lcm),!ndl_equal(lcm,tl))
                   1926:                        && (ndl_lcm(DL(nd_psh[cur->i2]),t,lcm),!ndl_equal(lcm,tl)) ) {
1.1       noro     1927:                        remove = cur;
                   1928:                        if ( !prev ) {
                   1929:                                head = cur = NEXT(cur);
                   1930:                        } else {
                   1931:                                cur = NEXT(prev) = NEXT(cur);
                   1932:                        }
                   1933:                        FREENDP(remove);
                   1934:                } else {
1.34      noro     1935:                        prev = cur; cur = NEXT(cur);
1.1       noro     1936:                }
                   1937:        }
                   1938:        return head;
                   1939: }
                   1940:
                   1941: ND_pairs crit_M( ND_pairs d1 )
                   1942: {
                   1943:        ND_pairs e,d2,d3,dd,p;
1.61      noro     1944:        UINT *id,*jd;
1.1       noro     1945:
                   1946:        for ( dd = 0, e = d1; e; e = d3 ) {
                   1947:                if ( !(d2 = NEXT(e)) ) {
                   1948:                        NEXT(e) = dd;
                   1949:                        return e;
                   1950:                }
1.34      noro     1951:                id = LCM(e);
1.1       noro     1952:                for ( d3 = 0; d2; d2 = p ) {
1.34      noro     1953:                        p = NEXT(d2);
                   1954:                        jd = LCM(d2);
                   1955:                        if ( ndl_equal(jd,id) )
                   1956:                                ;
                   1957:                        else if ( TD(jd) > TD(id) )
1.1       noro     1958:                                if ( ndl_reducible(jd,id) ) continue;
                   1959:                                else ;
1.34      noro     1960:                        else if ( ndl_reducible(id,jd) ) goto delit;
1.1       noro     1961:                        NEXT(d2) = d3;
                   1962:                        d3 = d2;
                   1963:                }
                   1964:                NEXT(e) = dd;
                   1965:                dd = e;
                   1966:                continue;
                   1967:                /**/
                   1968:        delit:  NEXT(d2) = d3;
                   1969:                d3 = d2;
                   1970:                for ( ; p; p = d2 ) {
                   1971:                        d2 = NEXT(p);
                   1972:                        NEXT(p) = d3;
                   1973:                        d3 = p;
                   1974:                }
                   1975:                FREENDP(e);
                   1976:        }
                   1977:        return dd;
                   1978: }
                   1979:
                   1980: ND_pairs crit_F( ND_pairs d1 )
                   1981: {
                   1982:        ND_pairs rest, head,remove;
                   1983:        ND_pairs last, p, r, w;
                   1984:        int s;
                   1985:
                   1986:        for ( head = last = 0, p = d1; NEXT(p); ) {
                   1987:                r = w = equivalent_pairs(p,&rest);
1.14      noro     1988:                s = SG(r);
1.1       noro     1989:                w = NEXT(w);
                   1990:                while ( w ) {
                   1991:                        if ( crit_2(w->i1,w->i2) ) {
                   1992:                                r = w;
                   1993:                                w = NEXT(w);
                   1994:                                while ( w ) {
                   1995:                                        remove = w;
                   1996:                                        w = NEXT(w);
                   1997:                                        FREENDP(remove);
                   1998:                                }
                   1999:                                break;
1.14      noro     2000:                        } else if ( SG(w) < s ) {
1.1       noro     2001:                                FREENDP(r);
                   2002:                                r = w;
1.14      noro     2003:                                s = SG(r);
1.1       noro     2004:                                w = NEXT(w);
                   2005:                        } else {
                   2006:                                remove = w;
                   2007:                                w = NEXT(w);
                   2008:                                FREENDP(remove);
                   2009:                        }
                   2010:                }
                   2011:                if ( last ) NEXT(last) = r;
                   2012:                else head = r;
                   2013:                NEXT(last = r) = 0;
                   2014:                p = rest;
                   2015:                if ( !p ) return head;
                   2016:        }
                   2017:        if ( !last ) return p;
                   2018:        NEXT(last) = p;
                   2019:        return head;
                   2020: }
                   2021:
                   2022: int crit_2( int dp1, int dp2 )
                   2023: {
1.20      noro     2024:        return ndl_disjoint(DL(nd_psh[dp1]),DL(nd_psh[dp2]));
1.1       noro     2025: }
                   2026:
1.40      noro     2027: ND_pairs equivalent_pairs( ND_pairs d1, ND_pairs *prest )
1.1       noro     2028: {
                   2029:        ND_pairs w,p,r,s;
1.61      noro     2030:        UINT *d;
1.1       noro     2031:
                   2032:        w = d1;
1.34      noro     2033:        d = LCM(w);
1.1       noro     2034:        s = NEXT(w);
                   2035:        NEXT(w) = 0;
                   2036:        for ( r = 0; s; s = p ) {
                   2037:                p = NEXT(s);
1.34      noro     2038:                if ( ndl_equal(d,LCM(s)) ) {
1.39      noro     2039:                        NEXT(s) = w; w = s;
1.1       noro     2040:                } else {
1.39      noro     2041:                        NEXT(s) = r; r = s;
1.1       noro     2042:                }
                   2043:        }
                   2044:        *prest = r;
                   2045:        return w;
                   2046: }
                   2047:
                   2048: NODE update_base(NODE nd,int ndp)
                   2049: {
1.61      noro     2050:        UINT *dl, *dln;
1.1       noro     2051:        NODE last, p, head;
                   2052:
1.20      noro     2053:        dl = DL(nd_psh[ndp]);
1.1       noro     2054:        for ( head = last = 0, p = nd; p; ) {
1.20      noro     2055:                dln = DL(nd_psh[(int)BDY(p)]);
1.34      noro     2056:                if ( ndl_reducible( dln, dl ) ) {
1.1       noro     2057:                        p = NEXT(p);
                   2058:                        if ( last ) NEXT(last) = p;
                   2059:                } else {
                   2060:                        if ( !last ) head = p;
                   2061:                        p = NEXT(last = p);
                   2062:                }
                   2063:        }
                   2064:        head = append_one(head,ndp);
                   2065:        return head;
                   2066: }
                   2067:
                   2068: ND_pairs nd_minp( ND_pairs d, ND_pairs *prest )
                   2069: {
                   2070:        ND_pairs m,ml,p,l;
1.61      noro     2071:        UINT *lcm;
1.33      noro     2072:        int s,td,len,tlen,c,c1;
1.1       noro     2073:
                   2074:        if ( !(p = NEXT(m = d)) ) {
                   2075:                *prest = p;
                   2076:                NEXT(m) = 0;
                   2077:                return m;
                   2078:        }
1.14      noro     2079:        s = SG(m);
1.77      noro     2080:        if ( !NoSugar ) {
                   2081:                for ( ml = 0, l = m; p; p = NEXT(l = p) )
                   2082:                        if ( (SG(p) < s)
                   2083:                                || ((SG(p) == s) && (DL_COMPARE(LCM(p),LCM(m)) < 0)) ) {
                   2084:                                ml = l; m = p; s = SG(m);
                   2085:                        }
                   2086:        } else {
                   2087:                for ( ml = 0, l = m; p; p = NEXT(l = p) )
                   2088:                        if ( DL_COMPARE(LCM(p),LCM(m)) < 0 ) {
                   2089:                                ml = l; m = p; s = SG(m);
                   2090:                        }
                   2091:        }
1.1       noro     2092:        if ( !ml ) *prest = NEXT(m);
                   2093:        else {
                   2094:                NEXT(ml) = NEXT(m);
                   2095:                *prest = d;
                   2096:        }
                   2097:        NEXT(m) = 0;
                   2098:        return m;
                   2099: }
                   2100:
1.63      noro     2101: ND_pairs nd_minsugarp( ND_pairs d, ND_pairs *prest )
                   2102: {
1.69      noro     2103:        int msugar,i;
1.63      noro     2104:        ND_pairs t,dm0,dm,dr0,dr;
                   2105:
                   2106:        for ( msugar = SG(d), t = NEXT(d); t; t = NEXT(t) )
                   2107:                if ( SG(t) < msugar ) msugar = SG(t);
                   2108:        dm0 = 0; dr0 = 0;
1.69      noro     2109:        for ( i = 0, t = d; t; t = NEXT(t) )
                   2110:                if ( i < nd_f4_nsp && SG(t) == msugar ) {
1.63      noro     2111:                        if ( dm0 ) NEXT(dm) = t;
                   2112:                        else dm0 = t;
                   2113:                        dm = t;
1.69      noro     2114:                        i++;
1.63      noro     2115:                } else {
                   2116:                        if ( dr0 ) NEXT(dr) = t;
                   2117:                        else dr0 = t;
                   2118:                        dr = t;
                   2119:                }
                   2120:        NEXT(dm) = 0;
                   2121:        if ( dr0 ) NEXT(dr) = 0;
                   2122:        *prest = dr0;
                   2123:        return dm0;
                   2124: }
                   2125:
1.77      noro     2126: int ndv_newps(int m,NDV a,NDV aq)
1.1       noro     2127: {
1.3       noro     2128:        int len;
1.13      noro     2129:        RHist r;
1.20      noro     2130:        NDV b;
1.3       noro     2131:
1.1       noro     2132:        if ( nd_psn == nd_pslen ) {
                   2133:                nd_pslen *= 2;
1.11      noro     2134:                nd_ps = (NDV *)REALLOC((char *)nd_ps,nd_pslen*sizeof(NDV));
1.53      noro     2135:                nd_ps_trace = (NDV *)REALLOC((char *)nd_ps_trace,nd_pslen*sizeof(NDV));
1.13      noro     2136:                nd_psh = (RHist *)REALLOC((char *)nd_psh,nd_pslen*sizeof(RHist));
1.61      noro     2137:                nd_bound = (UINT **)
                   2138:                        REALLOC((char *)nd_bound,nd_pslen*sizeof(UINT *));
1.1       noro     2139:        }
1.39      noro     2140:        NEWRHist(r); nd_psh[nd_psn] = r;
1.63      noro     2141:        nd_ps[nd_psn] = a;
1.39      noro     2142:        if ( aq ) {
1.63      noro     2143:                nd_ps_trace[nd_psn] = aq;
                   2144:                nd_bound[nd_psn] = ndv_compute_bound(aq);
1.39      noro     2145:                SG(r) = SG(aq); ndl_copy(HDL(aq),DL(r));
1.53      noro     2146:        } else {
1.63      noro     2147:                nd_bound[nd_psn] = ndv_compute_bound(a);
1.53      noro     2148:                SG(r) = SG(a); ndl_copy(HDL(a),DL(r));
1.39      noro     2149:        }
1.77      noro     2150:        if ( nd_demand ) {
                   2151:                if ( aq ) {
                   2152:                        ndv_save(nd_ps_trace[nd_psn],nd_psn);
                   2153:                        nd_ps_trace[nd_psn] = 0;
                   2154:                } else {
                   2155:                        ndv_save(nd_ps[nd_psn],nd_psn);
                   2156:                        nd_ps[nd_psn] = 0;
                   2157:                }
                   2158:        }
1.1       noro     2159:        return nd_psn++;
                   2160: }
                   2161:
1.61      noro     2162: void ndv_setup(int mod,int trace,NODE f)
1.1       noro     2163: {
1.5       noro     2164:        int i,j,td,len,max;
1.1       noro     2165:        NODE s,s0,f0;
1.61      noro     2166:        UINT *d;
1.13      noro     2167:        RHist r;
1.69      noro     2168:        NDV *w;
1.61      noro     2169:        NDV a,am;
1.11      noro     2170:
                   2171:        nd_found = 0; nd_notfirst = 0; nd_create = 0;
1.1       noro     2172:
1.69      noro     2173:        for ( nd_psn = 0, s = f; s; s = NEXT(s) ) if ( BDY(s) ) nd_psn++;
                   2174:        w = (NDV *)ALLOCA(nd_psn*sizeof(NDV));
                   2175:        for ( i = 0, s = f; s; s = NEXT(s) ) if ( BDY(s) ) w[i++] = BDY(s);
                   2176:        qsort(w,nd_psn,sizeof(NDV),
                   2177:                (int (*)(const void *,const void *))ndv_compare);
                   2178:        nd_pslen = 2*nd_psn;
1.11      noro     2179:        nd_ps = (NDV *)MALLOC(nd_pslen*sizeof(NDV));
1.53      noro     2180:        nd_ps_trace = (NDV *)MALLOC(nd_pslen*sizeof(NDV));
1.13      noro     2181:        nd_psh = (RHist *)MALLOC(nd_pslen*sizeof(RHist));
1.61      noro     2182:        nd_bound = (UINT **)MALLOC(nd_pslen*sizeof(UINT *));
1.57      noro     2183:
1.11      noro     2184:        if ( !nd_red )
1.13      noro     2185:                nd_red = (RHist *)MALLOC(REDTAB_LEN*sizeof(RHist));
1.71      noro     2186:        for ( i = 0; i < REDTAB_LEN; i++ ) nd_red[i] = 0;
1.69      noro     2187:        for ( i = 0; i < nd_psn; i++ ) {
1.39      noro     2188:                if ( trace ) {
1.69      noro     2189:                        a = nd_ps_trace[i] = ndv_dup(0,w[i]);
1.61      noro     2190:                        ndv_removecont(0,a);
                   2191:                        am = nd_ps[i] = ndv_dup(mod,a);
                   2192:                        ndv_mod(mod,am);
                   2193:                        ndv_removecont(mod,am);
                   2194:                } else {
1.69      noro     2195:                        a = nd_ps[i] = ndv_dup(mod,w[i]);
1.61      noro     2196:                        ndv_removecont(mod,a);
1.39      noro     2197:                }
1.61      noro     2198:                NEWRHist(r); SG(r) = HTD(a); ndl_copy(HDL(a),DL(r));
1.57      noro     2199:                nd_bound[i] = ndv_compute_bound(a);
1.20      noro     2200:                nd_psh[i] = r;
1.77      noro     2201:                if ( nd_demand ) {
                   2202:                        if ( trace ) {
                   2203:                                ndv_save(nd_ps_trace[i],i);
                   2204:                                nd_ps_trace[i] = 0;
                   2205:                        } else {
                   2206:                                ndv_save(nd_ps[i],i);
                   2207:                                nd_ps[i] = 0;
                   2208:                        }
                   2209:                }
1.20      noro     2210:        }
                   2211: }
                   2212:
1.63      noro     2213: void nd_gr(LIST f,LIST v,int m,int f4,struct order_spec *ord,LIST *rp)
1.1       noro     2214: {
1.61      noro     2215:        VL tv,fv,vv,vc;
1.1       noro     2216:        NODE fd,fd0,r,r0,t,x,s,xx;
1.61      noro     2217:        int e,max,nvar;
1.69      noro     2218:        NDV b;
1.77      noro     2219:        int ishomo;
1.1       noro     2220:
1.78      noro     2221:        if ( !m && Demand ) nd_demand = 1;
                   2222:        else nd_demand = 0;
                   2223:
1.103     noro     2224: #if 0
1.74      noro     2225:        ndv_alloc = 0;
1.103     noro     2226: #endif
1.1       noro     2227:        get_vars((Obj)f,&fv); pltovl(v,&vv);
1.74      noro     2228:        for ( nvar = 0, tv = vv; tv; tv = NEXT(tv), nvar++ );
1.88      noro     2229:        switch ( ord->id ) {
                   2230:                case 1:
                   2231:                        if ( ord->nv != nvar )
                   2232:                                error("nd_{gr,f4} : invalid order specification");
                   2233:                        break;
                   2234:                default:
                   2235:                        break;
                   2236:        }
1.32      noro     2237:        nd_init_ord(ord);
1.61      noro     2238:        for ( t = BDY(f), max = 0; t; t = NEXT(t) )
                   2239:                for ( tv = vv; tv; tv = NEXT(tv) ) {
                   2240:                        e = getdeg(tv->v,(P)BDY(t));
                   2241:                        max = MAX(e,max);
                   2242:                }
                   2243:        nd_setup_parameters(nvar,max);
1.77      noro     2244:        ishomo = 1;
1.1       noro     2245:        for ( fd0 = 0, t = BDY(f); t; t = NEXT(t) ) {
1.61      noro     2246:                b = (pointer)ptondv(CO,vv,(P)BDY(t));
1.77      noro     2247:                if ( ishomo )
                   2248:                        ishomo = ishomo && ndv_ishomo(b);
1.69      noro     2249:                if ( m ) ndv_mod(m,b);
1.61      noro     2250:                if ( b ) { NEXTNODE(fd0,fd); BDY(fd) = (pointer)b; }
1.1       noro     2251:        }
                   2252:        if ( fd0 ) NEXT(fd) = 0;
1.61      noro     2253:        ndv_setup(m,0,fd0);
1.77      noro     2254:        x = f4?nd_f4(m):nd_gb(m,ishomo,0);
1.82      noro     2255:        nd_demand = 0;
1.61      noro     2256:        x = ndv_reducebase(x);
                   2257:        x = ndv_reduceall(m,x);
1.23      noro     2258:        for ( r0 = 0, t = x; t; t = NEXT(t) ) {
1.1       noro     2259:                NEXTNODE(r0,r);
1.61      noro     2260:                BDY(r) = ndvtop(m,CO,vv,BDY(t));
1.20      noro     2261:        }
                   2262:        if ( r0 ) NEXT(r) = 0;
                   2263:        MKLIST(*rp,r0);
1.103     noro     2264: #if 0
1.74      noro     2265:        fprintf(asir_out,"ndv_alloc=%d\n",ndv_alloc);
1.103     noro     2266: #endif
1.20      noro     2267: }
                   2268:
1.52      noro     2269: void nd_gr_trace(LIST f,LIST v,int trace,int homo,struct order_spec *ord,LIST *rp)
1.20      noro     2270: {
1.89      noro     2271:        struct order_spec *ord1;
1.61      noro     2272:        VL tv,fv,vv,vc;
1.27      noro     2273:        NODE fd,fd0,in0,in,r,r0,t,s,cand;
1.61      noro     2274:        int m,nocheck,nvar,mindex,e,max;
                   2275:        NDV c;
                   2276:        NMV a;
1.27      noro     2277:        P p;
1.61      noro     2278:        EPOS oepos;
1.82      noro     2279:        int obpe,oadv,wmax,i,len,cbpe,ishomo;
1.20      noro     2280:
                   2281:        get_vars((Obj)f,&fv); pltovl(v,&vv);
1.74      noro     2282:        for ( nvar = 0, tv = vv; tv; tv = NEXT(tv), nvar++ );
1.88      noro     2283:        switch ( ord->id ) {
                   2284:                case 1:
                   2285:                        if ( ord->nv != nvar )
                   2286:                                error("nd_gr_trace : invalid order specification");
                   2287:                        break;
                   2288:                default:
                   2289:                        break;
                   2290:        }
1.52      noro     2291:        nocheck = 0;
                   2292:        mindex = 0;
                   2293:
1.78      noro     2294:        if ( Demand ) nd_demand = 1;
                   2295:        else nd_demand = 0;
                   2296:
1.52      noro     2297:        /* setup modulus */
                   2298:        if ( trace < 0 ) {
                   2299:                trace = -trace;
                   2300:                nocheck = 1;
                   2301:        }
                   2302:        m = trace > 1 ? trace : get_lprime(mindex);
1.61      noro     2303:        for ( t = BDY(f), max = 0; t; t = NEXT(t) )
                   2304:                for ( tv = vv; tv; tv = NEXT(tv) ) {
                   2305:                        e = getdeg(tv->v,(P)BDY(t));
                   2306:                        max = MAX(e,max);
1.23      noro     2307:                }
1.61      noro     2308:        nd_init_ord(ord);
                   2309:        nd_setup_parameters(nvar,max);
                   2310:        obpe = nd_bpe; oadv = nmv_adv; oepos = nd_epos;
1.82      noro     2311:        ishomo = 1;
1.61      noro     2312:        for ( in0 = 0, fd0 = 0, t = BDY(f); t; t = NEXT(t) ) {
                   2313:                c = ptondv(CO,vv,(P)BDY(t));
1.82      noro     2314:                if ( ishomo )
                   2315:                        ishomo = ishomo && ndv_ishomo(c);
1.61      noro     2316:                if ( c ) {
                   2317:                        NEXTNODE(in0,in); BDY(in) = (pointer)c;
                   2318:                        NEXTNODE(fd0,fd); BDY(fd) = (pointer)ndv_dup(0,c);
1.23      noro     2319:                }
1.61      noro     2320:        }
                   2321:        if ( in0 ) NEXT(in) = 0;
                   2322:        if ( fd0 ) NEXT(fd) = 0;
1.82      noro     2323:        if ( !ishomo && homo ) {
1.61      noro     2324:                for ( t = in0, wmax = 0; t; t = NEXT(t) ) {
                   2325:                        c = (NDV)BDY(t); len = LEN(c);
                   2326:                        for ( a = BDY(c), i = 0; i < len; i++, NMV_ADV(a) )
                   2327:                                wmax = MAX(TD(DL(a)),wmax);
                   2328:                }
                   2329:                homogenize_order(ord,nvar,&ord1);
1.89      noro     2330:                nd_init_ord(ord1);
1.61      noro     2331:                nd_setup_parameters(nvar+1,wmax);
                   2332:                for ( t = fd0; t; t = NEXT(t) )
                   2333:                        ndv_homogenize((NDV)BDY(t),obpe,oadv,oepos);
1.27      noro     2334:        }
1.52      noro     2335:        while ( 1 ) {
1.77      noro     2336:                if ( Demand )
                   2337:                        nd_demand = 1;
1.61      noro     2338:                ndv_setup(m,1,fd0);
1.82      noro     2339:                cand = nd_gb_trace(m,ishomo || homo);
1.52      noro     2340:                if ( !cand ) {
                   2341:                        /* failure */
1.61      noro     2342:                        if ( trace > 1 ) { *rp = 0; return; }
                   2343:                        else m = get_lprime(++mindex);
1.52      noro     2344:                        continue;
                   2345:                }
1.82      noro     2346:                if ( !ishomo && homo ) {
1.27      noro     2347:                        /* dehomogenization */
1.61      noro     2348:                        for ( t = cand; t; t = NEXT(t) ) ndv_dehomogenize((NDV)BDY(t),ord);
1.45      noro     2349:                        nd_init_ord(ord);
1.61      noro     2350:                        nd_setup_parameters(nvar,0);
1.27      noro     2351:                }
1.77      noro     2352:                nd_demand = 0;
1.61      noro     2353:                cand = ndv_reducebase(cand);
                   2354:                cand = ndv_reduceall(0,cand);
1.78      noro     2355:                cbpe = nd_bpe;
1.61      noro     2356:                if ( nocheck )
                   2357:                        break;
                   2358:                if ( ndv_check_candidate(in0,obpe,oadv,oepos,cand) )
1.52      noro     2359:                        /* success */
                   2360:                        break;
                   2361:                else if ( trace > 1 ) {
                   2362:                        /* failure */
                   2363:                        *rp = 0; return;
1.61      noro     2364:                } else {
1.52      noro     2365:                        /* try the next modulus */
                   2366:                        m = get_lprime(++mindex);
1.61      noro     2367:                        /* reset the parameters */
1.82      noro     2368:                        if ( !ishomo && homo ) {
1.89      noro     2369:                                nd_init_ord(ord1);
1.61      noro     2370:                                nd_setup_parameters(nvar+1,wmax);
                   2371:                        } else {
                   2372:                                nd_init_ord(ord);
                   2373:                                nd_setup_parameters(nvar,max);
                   2374:                        }
                   2375:                }
1.52      noro     2376:        }
1.27      noro     2377:        /* dp->p */
1.61      noro     2378:        nd_bpe = cbpe;
1.73      noro     2379:        nd_setup_parameters(nd_nvar,0);
1.61      noro     2380:        for ( r = cand; r; r = NEXT(r) ) BDY(r) = (pointer)ndvtop(0,CO,vv,BDY(r));
1.27      noro     2381:        MKLIST(*rp,cand);
1.1       noro     2382: }
                   2383:
1.61      noro     2384: void dltondl(int n,DL dl,UINT *r)
1.1       noro     2385: {
1.61      noro     2386:        UINT *d;
1.57      noro     2387:        int i,j,l,s,ord_l;
1.43      noro     2388:        struct order_pair *op;
1.1       noro     2389:
                   2390:        d = dl->d;
1.41      noro     2391:        for ( i = 0; i < nd_wpd; i++ ) r[i] = 0;
1.43      noro     2392:        if ( nd_blockmask ) {
                   2393:                l = nd_blockmask->n;
                   2394:                op = nd_blockmask->order_pair;
                   2395:                for ( j = 0, s = 0; j < l; j++ ) {
                   2396:                        ord_l = op[j].length;
1.57      noro     2397:                        for ( i = 0; i < ord_l; i++, s++ ) PUT_EXP(r,s,d[s]);
1.43      noro     2398:                }
                   2399:                TD(r) = ndl_weight(r);
1.61      noro     2400:                ndl_weight_mask(r);
1.43      noro     2401:        } else {
1.56      noro     2402:                for ( i = 0; i < n; i++ ) PUT_EXP(r,i,d[i]);
1.43      noro     2403:                TD(r) = ndl_weight(r);
                   2404:        }
1.1       noro     2405: }
                   2406:
1.61      noro     2407: DL ndltodl(int n,UINT *ndl)
1.1       noro     2408: {
                   2409:        DL dl;
                   2410:        int *d;
1.57      noro     2411:        int i,j,l,s,ord_l;
1.43      noro     2412:        struct order_pair *op;
1.1       noro     2413:
                   2414:        NEWDL(dl,n);
1.34      noro     2415:        dl->td = TD(ndl);
1.1       noro     2416:        d = dl->d;
1.43      noro     2417:        if ( nd_blockmask ) {
                   2418:                l = nd_blockmask->n;
                   2419:                op = nd_blockmask->order_pair;
                   2420:                for ( j = 0, s = 0; j < l; j++ ) {
                   2421:                        ord_l = op[j].length;
1.57      noro     2422:                        for ( i = 0; i < ord_l; i++, s++ ) d[s] = GET_EXP(ndl,s);
1.43      noro     2423:                }
                   2424:        } else {
1.56      noro     2425:                for ( i = 0; i < n; i++ ) d[i] = GET_EXP(ndl,i);
1.43      noro     2426:        }
1.1       noro     2427:        return dl;
                   2428: }
                   2429:
1.61      noro     2430: void ndl_print(UINT *dl)
1.1       noro     2431: {
                   2432:        int n;
1.57      noro     2433:        int i,j,l,ord_l,s,s0;
1.43      noro     2434:        struct order_pair *op;
1.1       noro     2435:
                   2436:        n = nd_nvar;
                   2437:        printf("<<");
1.43      noro     2438:        if ( nd_blockmask ) {
                   2439:                l = nd_blockmask->n;
                   2440:                op = nd_blockmask->order_pair;
                   2441:                for ( j = 0, s = s0 = 0; j < l; j++ ) {
                   2442:                        ord_l = op[j].length;
1.57      noro     2443:                        for ( i = 0; i < ord_l; i++, s++ )
                   2444:                                printf(s==n-1?"%d":"%d,",GET_EXP(dl,s));
1.43      noro     2445:                }
                   2446:        } else {
1.56      noro     2447:                for ( i = 0; i < n; i++ ) printf(i==n-1?"%d":"%d,",GET_EXP(dl,i));
1.43      noro     2448:        }
1.1       noro     2449:        printf(">>");
                   2450: }
                   2451:
                   2452: void nd_print(ND p)
                   2453: {
                   2454:        NM m;
                   2455:
                   2456:        if ( !p )
                   2457:                printf("0\n");
                   2458:        else {
                   2459:                for ( m = BDY(p); m; m = NEXT(m) ) {
1.71      noro     2460:                        if ( CM(m) & 0x80000000 ) printf("+@_%d*",IFTOF(CM(m)));
                   2461:                        else printf("+%d*",CM(m));
1.14      noro     2462:                        ndl_print(DL(m));
1.1       noro     2463:                }
                   2464:                printf("\n");
                   2465:        }
                   2466: }
                   2467:
1.113     noro     2468: void nd_print_q(ND p)
1.16      noro     2469: {
                   2470:        NM m;
                   2471:
                   2472:        if ( !p )
                   2473:                printf("0\n");
                   2474:        else {
                   2475:                for ( m = BDY(p); m; m = NEXT(m) ) {
                   2476:                        printf("+");
1.113     noro     2477:                        printexpr(CO,(Obj)CQ(m));
1.16      noro     2478:                        printf("*");
                   2479:                        ndl_print(DL(m));
                   2480:                }
                   2481:                printf("\n");
                   2482:        }
                   2483: }
                   2484:
1.1       noro     2485: void ndp_print(ND_pairs d)
                   2486: {
                   2487:        ND_pairs t;
                   2488:
1.34      noro     2489:        for ( t = d; t; t = NEXT(t) ) printf("%d,%d ",t->i1,t->i2);
1.1       noro     2490:        printf("\n");
                   2491: }
                   2492:
1.20      noro     2493: void nd_removecont(int mod,ND p)
1.16      noro     2494: {
                   2495:        int i,n;
1.113     noro     2496:        Q *w;
                   2497:        Q dvr,t;
1.16      noro     2498:        NM m;
1.21      noro     2499:        struct oVECT v;
1.113     noro     2500:        N q,r;
1.16      noro     2501:
1.71      noro     2502:        if ( mod == -1 ) nd_mul_c(mod,p,_invsf(HCM(p)));
                   2503:        else if ( mod ) nd_mul_c(mod,p,invm(HCM(p),mod));
1.20      noro     2504:        else {
                   2505:                for ( m = BDY(p), n = 0; m; m = NEXT(m), n++ );
1.113     noro     2506:                w = (Q *)ALLOCA(n*sizeof(Q));
1.21      noro     2507:                v.len = n;
                   2508:                v.body = (pointer *)w;
1.113     noro     2509:                for ( m = BDY(p), i = 0; i < n; m = NEXT(m), i++ ) w[i] = CQ(m);
1.21      noro     2510:                removecont_array(w,n);
1.113     noro     2511:                for ( m = BDY(p), i = 0; i < n; m = NEXT(m), i++ ) CQ(m) = w[i];
1.16      noro     2512:        }
                   2513: }
                   2514:
1.21      noro     2515: void nd_removecont2(ND p1,ND p2)
                   2516: {
                   2517:        int i,n1,n2,n;
1.113     noro     2518:        Q *w;
                   2519:        Q dvr,t;
1.21      noro     2520:        NM m;
                   2521:        struct oVECT v;
1.113     noro     2522:        N q,r;
1.21      noro     2523:
                   2524:        if ( !p1 ) {
                   2525:                nd_removecont(0,p2); return;
                   2526:        } else if ( !p2 ) {
                   2527:                nd_removecont(0,p1); return;
                   2528:        }
                   2529:        n1 = nd_length(p1);
                   2530:        n2 = nd_length(p2);
                   2531:        n = n1+n2;
1.113     noro     2532:        w = (Q *)ALLOCA(n*sizeof(Q));
1.21      noro     2533:        v.len = n;
                   2534:        v.body = (pointer *)w;
1.113     noro     2535:        for ( m = BDY(p1), i = 0; i < n1; m = NEXT(m), i++ ) w[i] = CQ(m);
                   2536:        for ( m = BDY(p2); i < n; m = NEXT(m), i++ ) w[i] = CQ(m);
1.21      noro     2537:        removecont_array(w,n);
1.113     noro     2538:        for ( m = BDY(p1), i = 0; i < n1; m = NEXT(m), i++ ) CQ(m) = w[i];
                   2539:        for ( m = BDY(p2); i < n; m = NEXT(m), i++ ) CQ(m) = w[i];
1.21      noro     2540: }
                   2541:
1.20      noro     2542: void ndv_removecont(int mod,NDV p)
1.16      noro     2543: {
                   2544:        int i,len;
1.113     noro     2545:        Q *w;
                   2546:        Q dvr,t;
1.16      noro     2547:        NMV m;
                   2548:
1.71      noro     2549:        if ( mod == -1 )
                   2550:                ndv_mul_c(mod,p,_invsf(HCM(p)));
                   2551:        else if ( mod )
1.20      noro     2552:                ndv_mul_c(mod,p,invm(HCM(p),mod));
                   2553:        else {
                   2554:                len = p->len;
1.113     noro     2555:                w = (Q *)ALLOCA(len*sizeof(Q));
                   2556:                for ( m = BDY(p), i = 0; i < len; NMV_ADV(m), i++ ) w[i] = CQ(m);
                   2557:                sortbynm(w,len);
                   2558:                qltozl(w,len,&dvr);
                   2559:                for ( m = BDY(p), i = 0; i < len; NMV_ADV(m), i++ ) {
                   2560:                        divq(CQ(m),dvr,&t); CQ(m) = t;
                   2561:                }
1.16      noro     2562:        }
1.21      noro     2563: }
                   2564:
1.61      noro     2565: void ndv_homogenize(NDV p,int obpe,int oadv,EPOS oepos)
                   2566: {
                   2567:        int len,i,max;
                   2568:        NMV m,mr0,mr,t;
                   2569:
                   2570:        len = p->len;
                   2571:        for ( m = BDY(p), i = 0, max = 0; i < len; NMV_OADV(m), i++ )
                   2572:                max = MAX(max,TD(DL(m)));
                   2573:        mr0 = nmv_adv>oadv?(NMV)REALLOC(BDY(p),len*nmv_adv):BDY(p);
                   2574:        m = (NMV)((char *)mr0+(len-1)*oadv);
                   2575:        mr = (NMV)((char *)mr0+(len-1)*nmv_adv);
                   2576:        t = (NMV)ALLOCA(nmv_adv);
                   2577:        for ( i = 0; i < len; i++, NMV_OPREV(m), NMV_PREV(mr) ) {
                   2578:                ndl_homogenize(DL(m),DL(t),obpe,oepos,max);
1.113     noro     2579:                CQ(mr) = CQ(m);
1.61      noro     2580:                ndl_copy(DL(t),DL(mr));
                   2581:        }
                   2582:        NV(p)++;
                   2583:        BDY(p) = mr0;
                   2584: }
                   2585:
1.45      noro     2586: void ndv_dehomogenize(NDV p,struct order_spec *ord)
1.23      noro     2587: {
1.45      noro     2588:        int i,j,adj,len,newnvar,newwpd,newadv,newexporigin;
1.113     noro     2589:        Q *w;
                   2590:        Q dvr,t;
1.23      noro     2591:        NMV m,r;
                   2592:
                   2593:        len = p->len;
                   2594:        newnvar = nd_nvar-1;
1.48      noro     2595:        newexporigin = nd_get_exporigin(ord);
1.45      noro     2596:        newwpd = newnvar/nd_epw+(newnvar%nd_epw?1:0)+newexporigin;
1.23      noro     2597:        for ( m = BDY(p), i = 0; i < len; NMV_ADV(m), i++ )
1.34      noro     2598:                ndl_dehomogenize(DL(m));
1.23      noro     2599:        if ( newwpd != nd_wpd ) {
1.90      noro     2600:                newadv = ROUND_FOR_ALIGN(sizeof(struct oNMV)+(newwpd-1)*sizeof(UINT));
1.61      noro     2601:                for ( m = r = BDY(p), i = 0; i < len; NMV_ADV(m), NDV_NADV(r), i++ ) {
1.113     noro     2602:                        CQ(r) = CQ(m);
1.45      noro     2603:                        for ( j = 0; j < newexporigin; j++ ) DL(r)[j] = DL(m)[j];
                   2604:                        adj = nd_exporigin-newexporigin;
                   2605:                        for ( ; j < newwpd; j++ ) DL(r)[j] = DL(m)[j+adj];
1.23      noro     2606:                }
                   2607:        }
                   2608:        NV(p)--;
                   2609: }
                   2610:
1.113     noro     2611: void removecont_array(Q *c,int n)
1.21      noro     2612: {
                   2613:        struct oVECT v;
1.113     noro     2614:        Q d0,d1,a,u,u1,gcd;
1.110     noro     2615:        int i,j;
1.113     noro     2616:        N qn,rn,gn;
                   2617:        Q *q,*r;
1.21      noro     2618:
1.113     noro     2619:        q = (Q *)ALLOCA(n*sizeof(Q));
                   2620:        r = (Q *)ALLOCA(n*sizeof(Q));
1.21      noro     2621:        v.id = O_VECT; v.len = n; v.body = (pointer *)c;
1.113     noro     2622:        igcdv_estimate(&v,&d0);
                   2623:        for ( i = 0; i < n; i++ ) {
                   2624:                divn(NM(c[i]),NM(d0),&qn,&rn);
                   2625:                NTOQ(qn,SGN(c[i])*SGN(d0),q[i]);
                   2626:                NTOQ(rn,SGN(c[i]),r[i]);
                   2627:        }
1.34      noro     2628:        for ( i = 0; i < n; i++ ) if ( r[i] ) break;
1.21      noro     2629:        if ( i < n ) {
                   2630:                v.id = O_VECT; v.len = n; v.body = (pointer *)r;
1.113     noro     2631:                igcdv(&v,&d1);
                   2632:                gcdn(NM(d0),NM(d1),&gn); NTOQ(gn,1,gcd);
                   2633:                divsn(NM(d0),gn,&qn); NTOQ(qn,1,a);
1.21      noro     2634:                for ( i = 0; i < n; i++ ) {
1.113     noro     2635:                        mulq(a,q[i],&u);
                   2636:                        if ( r[i] ) {
                   2637:                                divsn(NM(r[i]),gn,&qn); NTOQ(qn,SGN(r[i]),u1);
                   2638:                                addq(u,u1,&q[i]);
                   2639:                        } else
                   2640:                                q[i] = u;
1.21      noro     2641:                }
                   2642:        }
1.34      noro     2643:        for ( i = 0; i < n; i++ ) c[i] = q[i];
1.16      noro     2644: }
                   2645:
1.19      noro     2646: void nd_mul_c(int mod,ND p,int mul)
1.1       noro     2647: {
                   2648:        NM m;
                   2649:        int c,c1;
                   2650:
1.34      noro     2651:        if ( !p ) return;
1.115     noro     2652:        if ( mul == 1 ) return;
1.71      noro     2653:        if ( mod == -1 )
                   2654:                for ( m = BDY(p); m; m = NEXT(m) )
                   2655:                        CM(m) = _mulsf(CM(m),mul);
                   2656:        else
                   2657:                for ( m = BDY(p); m; m = NEXT(m) ) {
                   2658:                        c1 = CM(m); DMAR(c1,mul,0,mod,c); CM(m) = c;
                   2659:                }
1.1       noro     2660: }
                   2661:
1.113     noro     2662: void nd_mul_c_q(ND p,Q mul)
1.16      noro     2663: {
                   2664:        NM m;
1.113     noro     2665:        Q c;
1.16      noro     2666:
1.113     noro     2667:        if ( !p ) return;
1.115     noro     2668:        if ( UNIQ(mul) ) return;
1.16      noro     2669:        for ( m = BDY(p); m; m = NEXT(m) ) {
1.113     noro     2670:                mulq(CQ(m),mul,&c); CQ(m) = c;
1.16      noro     2671:        }
                   2672: }
                   2673:
1.61      noro     2674: void nd_mul_c_p(VL vl,ND p,P mul)
                   2675: {
                   2676:        NM m;
                   2677:        P c;
                   2678:
                   2679:        if ( !p ) return;
                   2680:        for ( m = BDY(p); m; m = NEXT(m) ) {
                   2681:                mulp(vl,CP(m),mul,&c); CP(m) = c;
                   2682:        }
                   2683: }
                   2684:
1.1       noro     2685: void nd_free(ND p)
                   2686: {
                   2687:        NM t,s;
                   2688:
1.34      noro     2689:        if ( !p ) return;
1.1       noro     2690:        t = BDY(p);
                   2691:        while ( t ) {
                   2692:                s = NEXT(t);
                   2693:                FREENM(t);
                   2694:                t = s;
                   2695:        }
                   2696:        FREEND(p);
                   2697: }
                   2698:
1.23      noro     2699: void ndv_free(NDV p)
                   2700: {
                   2701:        GC_free(BDY(p));
                   2702: }
                   2703:
1.61      noro     2704: void nd_append_red(UINT *d,int i)
1.1       noro     2705: {
1.13      noro     2706:        RHist m,m0;
1.1       noro     2707:        int h;
                   2708:
1.13      noro     2709:        NEWRHist(m);
1.34      noro     2710:        h = ndl_hash_value(d);
1.13      noro     2711:        m->index = i;
1.14      noro     2712:        ndl_copy(d,DL(m));
1.1       noro     2713:        NEXT(m) = nd_red[h];
                   2714:        nd_red[h] = m;
                   2715: }
                   2716:
1.61      noro     2717: UINT *ndv_compute_bound(NDV p)
1.1       noro     2718: {
1.61      noro     2719:        UINT *d1,*d2,*t;
                   2720:        UINT u;
1.57      noro     2721:        int i,j,k,l,len,ind;
1.45      noro     2722:        NMV m;
1.1       noro     2723:
                   2724:        if ( !p )
                   2725:                return 0;
1.61      noro     2726:        d1 = (UINT *)ALLOCA(nd_wpd*sizeof(UINT));
                   2727:        d2 = (UINT *)ALLOCA(nd_wpd*sizeof(UINT));
1.45      noro     2728:        len = LEN(p);
                   2729:        m = BDY(p); ndl_copy(DL(m),d1); NMV_ADV(m);
                   2730:        for ( i = 1; i < len; i++, NMV_ADV(m) ) {
1.14      noro     2731:                ndl_lcm(DL(m),d1,d2);
1.1       noro     2732:                t = d1; d1 = d2; d2 = t;
                   2733:        }
1.12      noro     2734:        l = nd_nvar+31;
1.61      noro     2735:        t = (UINT *)MALLOC_ATOMIC(l*sizeof(UINT));
1.57      noro     2736:        for ( i = nd_exporigin, ind = 0; i < nd_wpd; i++ ) {
                   2737:                u = d1[i];
                   2738:                k = (nd_epw-1)*nd_bpe;
                   2739:                for ( j = 0; j < nd_epw; j++, k -= nd_bpe, ind++ )
                   2740:                        t[ind] = (u>>k)&nd_mask0;
                   2741:        }
                   2742:        for ( ; ind < l; ind++ ) t[ind] = 0;
1.1       noro     2743:        return t;
                   2744: }
                   2745:
1.99      noro     2746: UINT *nd_compute_bound(ND p)
                   2747: {
                   2748:        UINT *d1,*d2,*t;
                   2749:        UINT u;
                   2750:        int i,j,k,l,len,ind;
                   2751:        NM m;
                   2752:
                   2753:        if ( !p )
                   2754:                return 0;
                   2755:        d1 = (UINT *)ALLOCA(nd_wpd*sizeof(UINT));
                   2756:        d2 = (UINT *)ALLOCA(nd_wpd*sizeof(UINT));
                   2757:        len = LEN(p);
                   2758:        m = BDY(p); ndl_copy(DL(m),d1); m = NEXT(m);
                   2759:        for ( m = NEXT(m); m; m = NEXT(m) ) {
                   2760:                ndl_lcm(DL(m),d1,d2);
                   2761:                t = d1; d1 = d2; d2 = t;
                   2762:        }
                   2763:        l = nd_nvar+31;
                   2764:        t = (UINT *)MALLOC_ATOMIC(l*sizeof(UINT));
                   2765:        for ( i = nd_exporigin, ind = 0; i < nd_wpd; i++ ) {
                   2766:                u = d1[i];
                   2767:                k = (nd_epw-1)*nd_bpe;
                   2768:                for ( j = 0; j < nd_epw; j++, k -= nd_bpe, ind++ )
                   2769:                        t[ind] = (u>>k)&nd_mask0;
                   2770:        }
                   2771:        for ( ; ind < l; ind++ ) t[ind] = 0;
                   2772:        return t;
                   2773: }
                   2774:
1.48      noro     2775: int nd_get_exporigin(struct order_spec *ord)
                   2776: {
1.51      noro     2777:        switch ( ord->id ) {
1.96      noro     2778:                case 0: case 2:
1.48      noro     2779:                        return 1;
1.41      noro     2780:                case 1:
                   2781:                        /* block order */
1.43      noro     2782:                        /* d[0]:weight d[1]:w0,...,d[nd_exporigin-1]:w(n-1) */
1.48      noro     2783:                        return ord->ord.block.length+1;
1.96      noro     2784:                case 3:
                   2785:                        error("nd_get_exporigin : composite order is not supported yet.");
1.41      noro     2786:        }
1.48      noro     2787: }
                   2788:
1.61      noro     2789: void nd_setup_parameters(int nvar,int max) {
1.79      noro     2790:        int i,j,n,elen,ord_o,ord_l,l,s,wpd;
1.57      noro     2791:        struct order_pair *op;
1.48      noro     2792:
1.73      noro     2793:        nd_nvar = nvar;
1.79      noro     2794:        if ( max ) {
1.83      noro     2795:                /* XXX */
                   2796:                if ( do_weyl ) nd_bpe = 32;
                   2797:                else if ( max < 2 ) nd_bpe = 1;
1.79      noro     2798:                else if ( max < 4 ) nd_bpe = 2;
                   2799:                else if ( max < 8 ) nd_bpe = 3;
                   2800:                else if ( max < 16 ) nd_bpe = 4;
                   2801:                else if ( max < 32 ) nd_bpe = 5;
                   2802:                else if ( max < 64 ) nd_bpe = 6;
                   2803:                else if ( max < 256 ) nd_bpe = 8;
                   2804:                else if ( max < 1024 ) nd_bpe = 10;
                   2805:                else if ( max < 65536 ) nd_bpe = 16;
                   2806:                else nd_bpe = 32;
                   2807:        }
1.61      noro     2808:        nd_epw = (sizeof(UINT)*8)/nd_bpe;
1.48      noro     2809:        elen = nd_nvar/nd_epw+(nd_nvar%nd_epw?1:0);
                   2810:        nd_exporigin = nd_get_exporigin(nd_ord);
1.79      noro     2811:        wpd = nd_exporigin+elen;
                   2812:        if ( wpd != nd_wpd ) {
                   2813:                nd_free_private_storage();
                   2814:                nd_wpd = wpd;
                   2815:        }
1.1       noro     2816:        if ( nd_bpe < 32 ) {
                   2817:                nd_mask0 = (1<<nd_bpe)-1;
                   2818:        } else {
                   2819:                nd_mask0 = 0xffffffff;
                   2820:        }
                   2821:        bzero(nd_mask,sizeof(nd_mask));
                   2822:        nd_mask1 = 0;
                   2823:        for ( i = 0; i < nd_epw; i++ ) {
                   2824:                nd_mask[nd_epw-i-1] = (nd_mask0<<(i*nd_bpe));
                   2825:                nd_mask1 |= (1<<(nd_bpe-1))<<(i*nd_bpe);
                   2826:        }
1.90      noro     2827:        nmv_adv = ROUND_FOR_ALIGN(sizeof(struct oNMV)+(nd_wpd-1)*sizeof(UINT));
1.57      noro     2828:        nd_epos = nd_create_epos(nd_ord);
1.43      noro     2829:        nd_blockmask = nd_create_blockmask(nd_ord);
1.96      noro     2830:        nd_work_vector = (int *)REALLOC(nd_work_vector,nd_nvar*sizeof(int));
1.1       noro     2831: }
                   2832:
1.103     noro     2833: ND_pairs nd_reconstruct(int trace,ND_pairs d)
1.1       noro     2834: {
1.37      noro     2835:        int i,obpe,oadv,h;
1.71      noro     2836:        static NM prev_nm_free_list;
                   2837:        static ND_pairs prev_ndp_free_list;
1.13      noro     2838:        RHist mr0,mr;
                   2839:        RHist r;
1.37      noro     2840:        RHist *old_red;
1.71      noro     2841:        ND_pairs s0,s,t;
1.43      noro     2842:        EPOS oepos;
1.15      noro     2843:
1.1       noro     2844:        obpe = nd_bpe;
1.11      noro     2845:        oadv = nmv_adv;
1.43      noro     2846:        oepos = nd_epos;
1.63      noro     2847:        if ( obpe < 2 ) nd_bpe = 2;
                   2848:        else if ( obpe < 3 ) nd_bpe = 3;
1.62      noro     2849:        else if ( obpe < 4 ) nd_bpe = 4;
1.63      noro     2850:        else if ( obpe < 5 ) nd_bpe = 5;
1.34      noro     2851:        else if ( obpe < 6 ) nd_bpe = 6;
                   2852:        else if ( obpe < 8 ) nd_bpe = 8;
1.63      noro     2853:        else if ( obpe < 10 ) nd_bpe = 10;
1.34      noro     2854:        else if ( obpe < 16 ) nd_bpe = 16;
                   2855:        else if ( obpe < 32 ) nd_bpe = 32;
                   2856:        else error("nd_reconstruct : exponent too large");
1.5       noro     2857:
1.73      noro     2858:        nd_setup_parameters(nd_nvar,0);
1.1       noro     2859:        prev_nm_free_list = _nm_free_list;
                   2860:        prev_ndp_free_list = _ndp_free_list;
                   2861:        _nm_free_list = 0;
                   2862:        _ndp_free_list = 0;
1.53      noro     2863:        for ( i = nd_psn-1; i >= 0; i-- ) ndv_realloc(nd_ps[i],obpe,oadv,oepos);
                   2864:        if ( trace )
                   2865:                for ( i = nd_psn-1; i >= 0; i-- )
                   2866:                        ndv_realloc(nd_ps_trace[i],obpe,oadv,oepos);
1.1       noro     2867:        s0 = 0;
                   2868:        for ( t = d; t; t = NEXT(t) ) {
                   2869:                NEXTND_pairs(s0,s);
                   2870:                s->i1 = t->i1;
                   2871:                s->i2 = t->i2;
1.14      noro     2872:                SG(s) = SG(t);
1.61      noro     2873:                ndl_reconstruct(LCM(t),LCM(s),obpe,oepos);
1.1       noro     2874:        }
1.37      noro     2875:
                   2876:        old_red = (RHist *)ALLOCA(REDTAB_LEN*sizeof(RHist));
1.6       noro     2877:        for ( i = 0; i < REDTAB_LEN; i++ ) {
1.37      noro     2878:                old_red[i] = nd_red[i];
                   2879:                nd_red[i] = 0;
                   2880:        }
                   2881:        for ( i = 0; i < REDTAB_LEN; i++ )
                   2882:                for ( r = old_red[i]; r; r = NEXT(r) ) {
                   2883:                        NEWRHist(mr);
1.13      noro     2884:                        mr->index = r->index;
1.20      noro     2885:                        SG(mr) = SG(r);
1.61      noro     2886:                        ndl_reconstruct(DL(r),DL(mr),obpe,oepos);
1.37      noro     2887:                        h = ndl_hash_value(DL(mr));
                   2888:                        NEXT(mr) = nd_red[h];
                   2889:                        nd_red[h] = mr;
1.6       noro     2890:                }
1.37      noro     2891:        for ( i = 0; i < REDTAB_LEN; i++ ) old_red[i] = 0;
                   2892:        old_red = 0;
1.11      noro     2893:        for ( i = 0; i < nd_psn; i++ ) {
1.20      noro     2894:                NEWRHist(r); SG(r) = SG(nd_psh[i]);
1.61      noro     2895:                ndl_reconstruct(DL(nd_psh[i]),DL(r),obpe,oepos);
1.13      noro     2896:                nd_psh[i] = r;
1.11      noro     2897:        }
1.1       noro     2898:        if ( s0 ) NEXT(s) = 0;
                   2899:        prev_nm_free_list = 0;
                   2900:        prev_ndp_free_list = 0;
1.71      noro     2901: #if 0
1.1       noro     2902:        GC_gcollect();
1.71      noro     2903: #endif
1.1       noro     2904:        return s0;
                   2905: }
                   2906:
1.61      noro     2907: void ndl_reconstruct(UINT *d,UINT *r,int obpe,EPOS oepos)
1.1       noro     2908: {
1.57      noro     2909:        int n,i,ei,oepw,omask0,j,s,ord_l,l;
1.43      noro     2910:        struct order_pair *op;
1.1       noro     2911:
                   2912:        n = nd_nvar;
1.61      noro     2913:        oepw = (sizeof(UINT)*8)/obpe;
1.43      noro     2914:        omask0 = (1<<obpe)-1;
1.34      noro     2915:        TD(r) = TD(d);
1.41      noro     2916:        for ( i = nd_exporigin; i < nd_wpd; i++ ) r[i] = 0;
1.43      noro     2917:        if ( nd_blockmask ) {
                   2918:                l = nd_blockmask->n;
                   2919:                op = nd_blockmask->order_pair;
                   2920:                for ( i = 1; i < nd_exporigin; i++ )
                   2921:                        r[i] = d[i];
                   2922:                for ( j = 0, s = 0; j < l; j++ ) {
                   2923:                        ord_l = op[j].length;
1.57      noro     2924:                        for ( i = 0; i < ord_l; i++, s++ ) {
                   2925:                                ei =  GET_EXP_OLD(d,s);
                   2926:                                PUT_EXP(r,s,ei);
                   2927:                        }
1.1       noro     2928:                }
1.43      noro     2929:        } else {
1.56      noro     2930:                for ( i = 0; i < n; i++ ) {
                   2931:                        ei = GET_EXP_OLD(d,i);
                   2932:                        PUT_EXP(r,i,ei);
                   2933:                }
1.1       noro     2934:        }
                   2935: }
1.3       noro     2936:
1.6       noro     2937: ND nd_copy(ND p)
                   2938: {
                   2939:        NM m,mr,mr0;
1.41      noro     2940:        int c,n;
1.6       noro     2941:        ND r;
                   2942:
                   2943:        if ( !p )
                   2944:                return 0;
                   2945:        else {
                   2946:                for ( mr0 = 0, m = BDY(p); m; m = NEXT(m) ) {
                   2947:                        NEXTNM(mr0,mr);
1.14      noro     2948:                        CM(mr) = CM(m);
                   2949:                        ndl_copy(DL(m),DL(mr));
1.6       noro     2950:                }
                   2951:                NEXT(mr) = 0;
1.31      noro     2952:                MKND(NV(p),mr0,LEN(p),r);
1.14      noro     2953:                SG(r) = SG(p);
1.6       noro     2954:                return r;
                   2955:        }
                   2956: }
                   2957:
1.53      noro     2958: int nd_sp(int mod,int trace,ND_pairs p,ND *rp)
1.11      noro     2959: {
                   2960:        NM m;
                   2961:        NDV p1,p2;
                   2962:        ND t1,t2;
1.61      noro     2963:        UINT *lcm;
1.31      noro     2964:        int td;
1.11      noro     2965:
1.77      noro     2966:        if ( !mod && nd_demand ) {
                   2967:                p1 = ndv_load(p->i1); p2 = ndv_load(p->i2);
1.53      noro     2968:        } else {
1.77      noro     2969:                if ( trace ) {
                   2970:                        p1 = nd_ps_trace[p->i1]; p2 = nd_ps_trace[p->i2];
                   2971:                } else {
                   2972:                        p1 = nd_ps[p->i1]; p2 = nd_ps[p->i2];
                   2973:                }
1.20      noro     2974:        }
1.34      noro     2975:        lcm = LCM(p);
1.11      noro     2976:        NEWNM(m);
1.113     noro     2977:        CQ(m) = HCQ(p2);
1.34      noro     2978:        ndl_sub(lcm,HDL(p1),DL(m));
1.56      noro     2979:        if ( ndl_check_bound2(p->i1,DL(m)) )
                   2980:                return 0;
1.55      noro     2981:        t1 = ndv_mul_nm(mod,m,p1);
1.71      noro     2982:        if ( mod == -1 ) CM(m) = _chsgnsf(HCM(p1));
                   2983:        else if ( mod ) CM(m) = mod-HCM(p1);
1.113     noro     2984:        else chsgnq(HCQ(p1),&CQ(m));
1.34      noro     2985:        ndl_sub(lcm,HDL(p2),DL(m));
1.14      noro     2986:        if ( ndl_check_bound2(p->i2,DL(m)) ) {
1.11      noro     2987:                nd_free(t1);
                   2988:                return 0;
                   2989:        }
1.55      noro     2990:        t2 = ndv_mul_nm(mod,m,p2);
1.31      noro     2991:        *rp = nd_add(mod,t1,t2);
1.11      noro     2992:        FREENM(m);
                   2993:        return 1;
                   2994: }
                   2995:
1.19      noro     2996: void ndv_mul_c(int mod,NDV p,int mul)
1.11      noro     2997: {
                   2998:        NMV m;
                   2999:        int c,c1,len,i;
                   3000:
1.34      noro     3001:        if ( !p ) return;
1.14      noro     3002:        len = LEN(p);
1.71      noro     3003:        if ( mod == -1 )
                   3004:                for ( m = BDY(p), i = 0; i < len; i++, NMV_ADV(m) )
                   3005:                        CM(m) = _mulsf(CM(m),mul);
                   3006:        else
                   3007:                for ( m = BDY(p), i = 0; i < len; i++, NMV_ADV(m) ) {
                   3008:                        c1 = CM(m); DMAR(c1,mul,0,mod,c); CM(m) = c;
                   3009:                }
1.11      noro     3010: }
                   3011:
1.113     noro     3012: void ndv_mul_c_q(NDV p,Q mul)
1.16      noro     3013: {
                   3014:        NMV m;
1.113     noro     3015:        Q c;
1.16      noro     3016:        int len,i;
                   3017:
1.34      noro     3018:        if ( !p ) return;
1.16      noro     3019:        len = LEN(p);
                   3020:        for ( m = BDY(p), i = 0; i < len; i++, NMV_ADV(m) ) {
1.113     noro     3021:                mulq(CQ(m),mul,&c); CQ(m) = c;
1.16      noro     3022:        }
                   3023: }
                   3024:
1.55      noro     3025: ND weyl_ndv_mul_nm(int mod,NM m0,NDV p) {
                   3026:        int n2,i,j,l,n,tlen;
1.61      noro     3027:        UINT *d0;
1.55      noro     3028:        NM *tab,*psum;
                   3029:        ND s,r;
                   3030:        NM t;
                   3031:        NMV m1;
                   3032:
                   3033:        if ( !p ) return 0;
                   3034:        n = NV(p); n2 = n>>1;
                   3035:        d0 = DL(m0);
                   3036:        l = LEN(p);
                   3037:        for ( i = 0, tlen = 1; i < n2; i++ ) tlen *= (GET_EXP(d0,n2+i)+1);
                   3038:        tab = (NM *)ALLOCA(tlen*sizeof(NM));
                   3039:        psum = (NM *)ALLOCA(tlen*sizeof(NM));
                   3040:        for ( i = 0; i < tlen; i++ ) psum[i] = 0;
1.56      noro     3041:        m1 = (NMV)(((char *)BDY(p))+nmv_adv*(l-1));
                   3042:        for ( i = l-1; i >= 0; i--, NMV_PREV(m1) ) {
1.55      noro     3043:                /* m0(NM) * m1(NMV) => tab(NM) */
1.56      noro     3044:                weyl_mul_nm_nmv(n,mod,m0,m1,tab,tlen);
1.55      noro     3045:                for ( j = 0; j < tlen; j++ ) {
                   3046:                        if ( tab[j] ) {
                   3047:                                NEXT(tab[j]) = psum[j]; psum[j] = tab[j];
                   3048:                        }
                   3049:                }
                   3050:        }
                   3051:        for ( i = tlen-1, r = 0; i >= 0; i-- )
                   3052:                if ( psum[i] ) {
                   3053:                        for ( j = 0, t = psum[i]; t; t = NEXT(t), j++ );
                   3054:                        MKND(n,psum[i],j,s);
                   3055:                        r = nd_add(mod,r,s);
                   3056:                }
1.56      noro     3057:        if ( r ) SG(r) = SG(p)+TD(d0);
                   3058:        return r;
1.55      noro     3059: }
                   3060:
1.56      noro     3061: /* product of monomials */
                   3062: /* XXX block order is not handled correctly */
                   3063:
1.55      noro     3064: void weyl_mul_nm_nmv(int n,int mod,NM m0,NMV m1,NM *tab,int tlen)
                   3065: {
1.56      noro     3066:        int i,n2,j,s,curlen,homo,h,a,b,k,l,u,min;
1.61      noro     3067:        UINT *d0,*d1,*d,*dt,*ctab;
1.113     noro     3068:        Q *ctab_q;
                   3069:        Q q,q1;
1.61      noro     3070:        UINT c0,c1,c;
1.55      noro     3071:        NM *p;
                   3072:        NM m,t;
                   3073:
                   3074:        for ( i = 0; i < tlen; i++ ) tab[i] = 0;
                   3075:        if ( !m0 || !m1 ) return;
                   3076:        d0 = DL(m0); d1 = DL(m1); n2 = n>>1;
                   3077:        NEWNM(m); d = DL(m);
1.56      noro     3078:        if ( mod ) {
                   3079:                c0 = CM(m0); c1 = CM(m1); DMAR(c0,c1,0,mod,c); CM(m) = c;
                   3080:        } else
1.113     noro     3081:                mulq(CQ(m0),CQ(m1),&CQ(m));
1.55      noro     3082:        for ( i = 0; i < nd_wpd; i++ ) d[i] = 0;
                   3083:        homo = n&1 ? 1 : 0;
                   3084:        if ( homo ) {
                   3085:                /* offset of h-degree */
                   3086:                h = GET_EXP(d0,n-1)+GET_EXP(d1,n-1);
                   3087:                PUT_EXP(DL(m),n-1,h);
                   3088:                TD(DL(m)) = h;
1.61      noro     3089:                if ( nd_blockmask ) ndl_weight_mask(DL(m));
1.55      noro     3090:        }
                   3091:        tab[0] = m;
                   3092:        NEWNM(m); d = DL(m);
1.57      noro     3093:        for ( i = 0, curlen = 1; i < n2; i++ ) {
1.55      noro     3094:                a = GET_EXP(d0,i); b = GET_EXP(d1,n2+i);
                   3095:                k = GET_EXP(d0,n2+i); l = GET_EXP(d1,i);
                   3096:                /* xi^a*(Di^k*xi^l)*Di^b */
                   3097:                a += l; b += k;
1.56      noro     3098:                s = MUL_WEIGHT(a,i)+MUL_WEIGHT(b,n2+i);
1.55      noro     3099:                if ( !k || !l ) {
                   3100:                        for ( j = 0; j < curlen; j++ )
1.56      noro     3101:                                if ( t = tab[j] ) {
                   3102:                                        dt = DL(t);
                   3103:                                        PUT_EXP(dt,i,a); PUT_EXP(dt,n2+i,b); TD(dt) += s;
1.61      noro     3104:                                        if ( nd_blockmask ) ndl_weight_mask(dt);
1.55      noro     3105:                                }
                   3106:                        curlen *= k+1;
                   3107:                        continue;
                   3108:                }
                   3109:                min = MIN(k,l);
1.56      noro     3110:                if ( mod ) {
1.61      noro     3111:                        ctab = (UINT *)ALLOCA((min+1)*sizeof(UINT));
1.56      noro     3112:                        mkwcm(k,l,mod,ctab);
                   3113:                } else {
1.113     noro     3114:                        ctab_q = (Q *)ALLOCA((min+1)*sizeof(Q));
                   3115:                        mkwc(k,l,ctab_q);
1.56      noro     3116:                }
1.57      noro     3117:                for ( j = min; j >= 0; j-- ) {
1.56      noro     3118:                        for ( u = 0; u < nd_wpd; u++ ) d[u] = 0;
1.55      noro     3119:                        PUT_EXP(d,i,a-j); PUT_EXP(d,n2+i,b-j);
1.56      noro     3120:                        h = MUL_WEIGHT(a-j,i)+MUL_WEIGHT(b-j,n2+i);
1.55      noro     3121:                        if ( homo ) {
                   3122:                                TD(d) = s;
1.56      noro     3123:                                PUT_EXP(d,n-1,s-h);
1.55      noro     3124:                        } else TD(d) = h;
1.61      noro     3125:                        if ( nd_blockmask ) ndl_weight_mask(d);
1.56      noro     3126:                        if ( mod ) c = ctab[j];
1.113     noro     3127:                        else q = ctab_q[j];
1.57      noro     3128:                        p = tab+curlen*j;
                   3129:                        if ( j == 0 ) {
                   3130:                                for ( u = 0; u < curlen; u++, p++ ) {
                   3131:                                        if ( tab[u] ) {
                   3132:                                                ndl_addto(DL(tab[u]),d);
                   3133:                                                if ( mod ) {
                   3134:                                                        c0 = CM(tab[u]); DMAR(c0,c,0,mod,c1); CM(tab[u]) = c1;
                   3135:                                                } else {
1.113     noro     3136:                                                        mulq(CQ(tab[u]),q,&q1); CQ(tab[u]) = q1;
1.57      noro     3137:                                                }
                   3138:                                        }
1.56      noro     3139:                                }
1.57      noro     3140:                        } else {
                   3141:                                for ( u = 0; u < curlen; u++, p++ ) {
                   3142:                                        if ( tab[u] ) {
                   3143:                                                NEWNM(t);
                   3144:                                                ndl_add(DL(tab[u]),d,DL(t));
                   3145:                                                if ( mod ) {
                   3146:                                                        c0 = CM(tab[u]); DMAR(c0,c,0,mod,c1); CM(t) = c1;
                   3147:                                                } else
1.113     noro     3148:                                                        mulq(CQ(tab[u]),q,&CQ(t));
1.57      noro     3149:                                                *p = t;
                   3150:                                        }
1.55      noro     3151:                                }
                   3152:                        }
                   3153:                }
                   3154:                curlen *= k+1;
                   3155:        }
                   3156:        FREENM(m);
                   3157: }
                   3158:
1.63      noro     3159: ND ndv_mul_nm_symbolic(NM m0,NDV p)
                   3160: {
                   3161:        NM mr,mr0;
                   3162:        NMV m;
                   3163:        UINT *d,*dt,*dm;
                   3164:        int c,n,td,i,c1,c2,len;
1.113     noro     3165:        Q q;
1.63      noro     3166:        ND r;
                   3167:
                   3168:        if ( !p ) return 0;
                   3169:        else {
                   3170:                n = NV(p); m = BDY(p);
                   3171:                d = DL(m0);
                   3172:                len = LEN(p);
                   3173:                mr0 = 0;
                   3174:                td = TD(d);
                   3175:                c = CM(m0);
                   3176:                for ( i = 0; i < len; i++, NMV_ADV(m) ) {
                   3177:                        NEXTNM(mr0,mr);
                   3178:                        CM(mr) = 1;
                   3179:                        ndl_add(DL(m),d,DL(mr));
                   3180:                }
                   3181:                NEXT(mr) = 0;
                   3182:                MKND(NV(p),mr0,len,r);
                   3183:                SG(r) = SG(p) + TD(d);
                   3184:                return r;
                   3185:        }
                   3186: }
                   3187:
1.55      noro     3188: ND ndv_mul_nm(int mod,NM m0,NDV p)
1.9       noro     3189: {
                   3190:        NM mr,mr0;
                   3191:        NMV m;
1.61      noro     3192:        UINT *d,*dt,*dm;
1.9       noro     3193:        int c,n,td,i,c1,c2,len;
1.113     noro     3194:        Q q;
1.9       noro     3195:        ND r;
                   3196:
1.34      noro     3197:        if ( !p ) return 0;
1.55      noro     3198:        else if ( do_weyl )
1.71      noro     3199:                if ( mod == -1 )
                   3200:                        error("ndv_mul_nm : not implemented (weyl)");
                   3201:                else
                   3202:                        return weyl_ndv_mul_nm(mod,m0,p);
1.9       noro     3203:        else {
                   3204:                n = NV(p); m = BDY(p);
1.34      noro     3205:                d = DL(m0);
1.14      noro     3206:                len = LEN(p);
1.9       noro     3207:                mr0 = 0;
1.34      noro     3208:                td = TD(d);
1.71      noro     3209:                if ( mod == -1 ) {
                   3210:                        c = CM(m0);
                   3211:                        for ( i = 0; i < len; i++, NMV_ADV(m) ) {
                   3212:                                NEXTNM(mr0,mr);
                   3213:                                CM(mr) = _mulsf(CM(m),c);
                   3214:                                ndl_add(DL(m),d,DL(mr));
                   3215:                        }
                   3216:                } else if ( mod ) {
1.16      noro     3217:                        c = CM(m0);
                   3218:                        for ( i = 0; i < len; i++, NMV_ADV(m) ) {
                   3219:                                NEXTNM(mr0,mr);
                   3220:                                c1 = CM(m);
1.19      noro     3221:                                DMAR(c1,c,0,mod,c2);
1.16      noro     3222:                                CM(mr) = c2;
                   3223:                                ndl_add(DL(m),d,DL(mr));
                   3224:                        }
                   3225:                } else {
1.113     noro     3226:                        q = CQ(m0);
1.16      noro     3227:                        for ( i = 0; i < len; i++, NMV_ADV(m) ) {
                   3228:                                NEXTNM(mr0,mr);
1.113     noro     3229:                                mulq(CQ(m),q,&CQ(mr));
1.16      noro     3230:                                ndl_add(DL(m),d,DL(mr));
                   3231:                        }
1.4       noro     3232:                }
1.9       noro     3233:                NEXT(mr) = 0;
1.31      noro     3234:                MKND(NV(p),mr0,len,r);
1.34      noro     3235:                SG(r) = SG(p) + TD(d);
1.9       noro     3236:                return r;
1.4       noro     3237:        }
                   3238: }
                   3239:
1.104     noro     3240: ND nd_quo(int mod,PGeoBucket bucket,NDV d)
1.99      noro     3241: {
                   3242:        NM mq0,mq;
1.102     noro     3243:        NMV tm;
1.113     noro     3244:        Q q;
1.104     noro     3245:        int i,nv,sg,c,c1,c2,hindex;
                   3246:        ND p,t,r;
                   3247:        N tnm;
                   3248:
1.99      noro     3249:        if ( !p ) return 0;
                   3250:        else {
1.104     noro     3251:                nv = NV(d);
1.99      noro     3252:                mq0 = 0;
1.102     noro     3253:                tm = (NMV)ALLOCA(nmv_adv);
1.104     noro     3254:                while ( 1 ) {
1.113     noro     3255:                        hindex = mod?head_pbucket(mod,bucket):head_pbucket_q(bucket);
1.104     noro     3256:                        if ( hindex < 0 ) break;
                   3257:                        p = bucket->body[hindex];
1.99      noro     3258:                        NEXTNM(mq0,mq);
                   3259:                        ndl_sub(HDL(p),HDL(d),DL(mq));
1.102     noro     3260:                        ndl_copy(DL(mq),DL(tm));
                   3261:                        if ( mod ) {
                   3262:                                c1 = invm(HCM(d),mod); c2 = HCM(p);
                   3263:                                DMAR(c1,c2,0,mod,c); CM(mq) = c;
                   3264:                                CM(tm) = mod-c;
                   3265:                        } else {
1.113     noro     3266:                                divsn(NM(HCQ(p)),NM(HCQ(d)),&tnm);
                   3267:                                NTOQ(tnm,SGN(HCQ(p))*SGN(HCQ(d)),CQ(mq));
                   3268:                                chsgnq(CQ(mq),&CQ(tm));
1.102     noro     3269:                        }
                   3270:                        t = ndv_mul_nmv_trunc(mod,tm,d,HDL(d));
1.104     noro     3271:                        bucket->body[hindex] = nd_remove_head(p);
                   3272:                        t = nd_remove_head(t);
                   3273:                        add_pbucket(mod,bucket,t);
                   3274:                }
                   3275:                if ( !mq0 )
                   3276:                        r = 0;
                   3277:                else {
                   3278:                        NEXT(mq) = 0;
                   3279:                        for ( i = 0, mq = mq0; mq; mq = NEXT(mq), i++ );
                   3280:                        MKND(nv,mq0,i,r);
                   3281:                        /* XXX */
                   3282:                        SG(r) = HTD(r);
1.99      noro     3283:                }
                   3284:                return r;
                   3285:        }
                   3286: }
                   3287:
1.43      noro     3288: void ndv_realloc(NDV p,int obpe,int oadv,EPOS oepos)
1.11      noro     3289: {
1.13      noro     3290:        NMV m,mr,mr0,t;
                   3291:        int len,i,k;
1.11      noro     3292:
1.61      noro     3293:        if ( !p ) return;
                   3294:        m = BDY(p); len = LEN(p);
                   3295:        mr0 = nmv_adv>oadv?(NMV)REALLOC(BDY(p),len*nmv_adv):BDY(p);
                   3296:        m = (NMV)((char *)mr0+(len-1)*oadv);
                   3297:        mr = (NMV)((char *)mr0+(len-1)*nmv_adv);
                   3298:        t = (NMV)ALLOCA(nmv_adv);
                   3299:        for ( i = 0; i < len; i++, NMV_OPREV(m), NMV_PREV(mr) ) {
1.113     noro     3300:                CQ(t) = CQ(m);
1.61      noro     3301:                for ( k = 0; k < nd_wpd; k++ ) DL(t)[k] = 0;
                   3302:                ndl_reconstruct(DL(m),DL(t),obpe,oepos);
1.113     noro     3303:                CQ(mr) = CQ(t);
1.61      noro     3304:                ndl_copy(DL(t),DL(mr));
                   3305:        }
                   3306:        BDY(p) = mr0;
                   3307: }
                   3308:
                   3309: NDV ndv_dup_realloc(NDV p,int obpe,int oadv,EPOS oepos)
                   3310: {
                   3311:        NMV m,mr,mr0;
                   3312:        int len,i;
                   3313:        NDV r;
1.11      noro     3314:
1.61      noro     3315:        if ( !p ) return 0;
                   3316:        m = BDY(p); len = LEN(p);
                   3317:        mr0 = mr = (NMV)MALLOC(len*nmv_adv);
                   3318:        for ( i = 0; i < len; i++, NMV_OADV(m), NMV_ADV(mr) ) {
                   3319:                ndl_zero(DL(mr));
                   3320:                ndl_reconstruct(DL(m),DL(mr),obpe,oepos);
1.113     noro     3321:                CQ(mr) = CQ(m);
1.11      noro     3322:        }
1.61      noro     3323:        MKNDV(NV(p),mr0,len,r);
                   3324:        SG(r) = SG(p);
                   3325:        return r;
1.11      noro     3326: }
                   3327:
1.61      noro     3328: /* duplicate p */
                   3329:
                   3330: NDV ndv_dup(int mod,NDV p)
1.3       noro     3331: {
                   3332:        NDV d;
1.61      noro     3333:        NMV t,m,m0;
1.3       noro     3334:        int i,len;
                   3335:
1.34      noro     3336:        if ( !p ) return 0;
1.31      noro     3337:        len = LEN(p);
1.34      noro     3338:        m0 = m = (NMV)(mod?MALLOC_ATOMIC(len*nmv_adv):MALLOC(len*nmv_adv));
1.61      noro     3339:        for ( t = BDY(p), i = 0; i < len; i++, NMV_ADV(t), NMV_ADV(m) ) {
1.14      noro     3340:                ndl_copy(DL(t),DL(m));
1.113     noro     3341:                CQ(m) = CQ(t);
1.3       noro     3342:        }
                   3343:        MKNDV(NV(p),m0,len,d);
1.23      noro     3344:        SG(d) = SG(p);
                   3345:        return d;
                   3346: }
                   3347:
1.63      noro     3348: ND nd_dup(ND p)
                   3349: {
                   3350:        ND d;
                   3351:        NM t,m,m0;
                   3352:
                   3353:        if ( !p ) return 0;
                   3354:        for ( m0 = 0, t = BDY(p); t; t = NEXT(t) ) {
                   3355:                NEXTNM(m0,m);
                   3356:                ndl_copy(DL(t),DL(m));
1.113     noro     3357:                CQ(m) = CQ(t);
1.63      noro     3358:        }
                   3359:        if ( m0 ) NEXT(m) = 0;
                   3360:        MKND(NV(p),m0,LEN(p),d);
                   3361:        SG(d) = SG(p);
                   3362:        return d;
                   3363: }
                   3364:
1.61      noro     3365: /* XXX if p->len == 0 then it represents 0 */
                   3366:
                   3367: void ndv_mod(int mod,NDV p)
                   3368: {
                   3369:        NMV t,d;
                   3370:        int r;
                   3371:        int i,len,dlen;
1.71      noro     3372:        Obj gfs;
1.61      noro     3373:
                   3374:        if ( !p ) return;
                   3375:        len = LEN(p);
                   3376:        dlen = 0;
1.71      noro     3377:        if ( mod == -1 )
                   3378:                for ( t = d = BDY(p), i = 0; i < len; i++, NMV_ADV(t) ) {
                   3379:                        simp_ff((Obj)CP(t),&gfs);
                   3380:                        r = FTOIF(CONT((GFS)gfs));
1.61      noro     3381:                        CM(d) = r;
                   3382:                        ndl_copy(DL(t),DL(d));
                   3383:                        NMV_ADV(d);
                   3384:                        dlen++;
                   3385:                }
1.71      noro     3386:        else
                   3387:                for ( t = d = BDY(p), i = 0; i < len; i++, NMV_ADV(t) ) {
1.113     noro     3388:                        r = rem(NM(CQ(t)),mod);
1.71      noro     3389:                        if ( r ) {
1.113     noro     3390:                                if ( SGN(CQ(t)) < 0 )
                   3391:                                        r = mod-r;
1.71      noro     3392:                                CM(d) = r;
                   3393:                                ndl_copy(DL(t),DL(d));
                   3394:                                NMV_ADV(d);
                   3395:                                dlen++;
                   3396:                        }
                   3397:                }
1.61      noro     3398:        LEN(p) = dlen;
                   3399: }
                   3400:
                   3401: NDV ptondv(VL vl,VL dvl,P p)
                   3402: {
                   3403:        ND nd;
                   3404:
                   3405:        nd = ptond(vl,dvl,p);
                   3406:        return ndtondv(0,nd);
                   3407: }
                   3408:
                   3409: ND ptond(VL vl,VL dvl,P p)
1.23      noro     3410: {
1.61      noro     3411:        int n,i,j,k,e;
                   3412:        VL tvl;
                   3413:        V v;
                   3414:        DCP dc;
                   3415:        DCP *w;
                   3416:        ND r,s,t,u;
                   3417:        P x;
                   3418:        int c;
                   3419:        UINT *d;
1.23      noro     3420:        NM m,m0;
1.61      noro     3421:
                   3422:        if ( !p )
                   3423:                return 0;
                   3424:        else if ( NUM(p) ) {
                   3425:                NEWNM(m);
                   3426:                ndl_zero(DL(m));
1.113     noro     3427:                CQ(m) = (Q)p;
1.61      noro     3428:                NEXT(m) = 0;
                   3429:                MKND(nd_nvar,m,1,r);
                   3430:                SG(r) = 0;
                   3431:                return r;
                   3432:        } else {
                   3433:                for ( dc = DC(p), k = 0; dc; dc = NEXT(dc), k++ );
                   3434:                w = (DCP *)ALLOCA(k*sizeof(DCP));
                   3435:                for ( dc = DC(p), j = 0; j < k; dc = NEXT(dc), j++ ) w[j] = dc;
                   3436:                for ( i = 0, tvl = dvl, v = VR(p);
1.98      noro     3437:                        tvl && tvl->v != v; tvl = NEXT(tvl), i++ );
1.61      noro     3438:                if ( !tvl ) {
                   3439:                        for ( j = k-1, s = 0, MKV(v,x); j >= 0; j-- ) {
                   3440:                                t = ptond(vl,dvl,COEF(w[j]));
                   3441:                                pwrp(vl,x,DEG(w[j]),&p);
                   3442:                                nd_mul_c_p(CO,t,p); s = nd_add(0,s,t);
                   3443:                        }
                   3444:                        return s;
                   3445:                } else {
                   3446:                        NEWNM(m0); d = DL(m0);
                   3447:                        for ( j = k-1, s = 0; j >= 0; j-- ) {
                   3448:                                ndl_zero(d); e = QTOS(DEG(w[j])); PUT_EXP(d,i,e);
                   3449:                                TD(d) = MUL_WEIGHT(e,i);
                   3450:                                if ( nd_blockmask) ndl_weight_mask(d);
                   3451:                                t = ptond(vl,dvl,COEF(w[j]));
                   3452:                                for ( m = BDY(t); m; m = NEXT(m) )
                   3453:                                        ndl_addto(DL(m),d);
                   3454:                                SG(t) += TD(d);
                   3455:                                s = nd_add(0,s,t);
                   3456:                        }
                   3457:                        FREENM(m0);
                   3458:                        return s;
                   3459:                }
                   3460:        }
                   3461: }
                   3462:
                   3463: P ndvtop(int mod,VL vl,VL dvl,NDV p)
                   3464: {
                   3465:        VL tvl;
                   3466:        int len,n,j,i,e;
                   3467:        NMV m;
                   3468:        Q q;
                   3469:        P c;
                   3470:        UINT *d;
                   3471:        P s,r,u,t,w;
1.71      noro     3472:        GFS gfs;
1.23      noro     3473:
1.34      noro     3474:        if ( !p ) return 0;
1.61      noro     3475:        else {
                   3476:                len = LEN(p);
                   3477:                n = NV(p);
                   3478:                m = (NMV)(((char *)BDY(p))+nmv_adv*(len-1));
                   3479:                for ( j = len-1, s = 0; j >= 0; j--, NMV_PREV(m) ) {
1.71      noro     3480:                        if ( mod == -1 ) {
                   3481:                                e = IFTOF(CM(m)); MKGFS(e,gfs); c = (P)gfs;
                   3482:                        } else if ( mod ) {
1.61      noro     3483:                                STOQ(CM(m),q); c = (P)q;
                   3484:                        } else
1.113     noro     3485:                                c = CP(m);
1.61      noro     3486:                        d = DL(m);
                   3487:                        for ( i = 0, t = c, tvl = dvl; i < n; tvl = NEXT(tvl), i++ ) {
                   3488:                                MKV(tvl->v,r); e = GET_EXP(d,i); STOQ(e,q);
                   3489:                                pwrp(vl,r,q,&u); mulp(vl,t,u,&w); t = w;
                   3490:                        }
                   3491:                        addp(vl,s,t,&u); s = u;
                   3492:                }
                   3493:                return s;
1.23      noro     3494:        }
1.3       noro     3495: }
                   3496:
1.61      noro     3497: NDV ndtondv(int mod,ND p)
1.11      noro     3498: {
                   3499:        NDV d;
1.61      noro     3500:        NMV m,m0;
                   3501:        NM t;
                   3502:        int i,len;
1.11      noro     3503:
1.34      noro     3504:        if ( !p ) return 0;
1.61      noro     3505:        len = LEN(p);
1.74      noro     3506:        if ( mod )
                   3507:                m0 = m = (NMV)GC_malloc_atomic_ignore_off_page(len*nmv_adv);
                   3508:        else
                   3509:                m0 = m = MALLOC(len*nmv_adv);
1.103     noro     3510: #if 0
1.74      noro     3511:        ndv_alloc += nmv_adv*len;
1.103     noro     3512: #endif
1.61      noro     3513:        for ( t = BDY(p), i = 0; t; t = NEXT(t), i++, NMV_ADV(m) ) {
                   3514:                ndl_copy(DL(t),DL(m));
1.113     noro     3515:                CQ(m) = CQ(t);
1.11      noro     3516:        }
1.61      noro     3517:        MKNDV(NV(p),m0,len,d);
1.14      noro     3518:        SG(d) = SG(p);
1.11      noro     3519:        return d;
                   3520: }
                   3521:
1.61      noro     3522: ND ndvtond(int mod,NDV p)
1.11      noro     3523: {
1.61      noro     3524:        ND d;
                   3525:        NM m,m0;
1.11      noro     3526:        NMV t;
1.61      noro     3527:        int i,len;
1.11      noro     3528:
1.34      noro     3529:        if ( !p ) return 0;
1.11      noro     3530:        m0 = 0;
1.61      noro     3531:        len = p->len;
                   3532:        for ( t = BDY(p), i = 0; i < len; NMV_ADV(t), i++ ) {
                   3533:                NEXTNM(m0,m);
                   3534:                ndl_copy(DL(t),DL(m));
1.113     noro     3535:                CQ(m) = CQ(t);
1.11      noro     3536:        }
                   3537:        NEXT(m) = 0;
1.61      noro     3538:        MKND(NV(p),m0,len,d);
1.14      noro     3539:        SG(d) = SG(p);
1.11      noro     3540:        return d;
                   3541: }
                   3542:
1.3       noro     3543: void ndv_print(NDV p)
                   3544: {
                   3545:        NMV m;
                   3546:        int i,len;
                   3547:
1.34      noro     3548:        if ( !p ) printf("0\n");
1.3       noro     3549:        else {
1.14      noro     3550:                len = LEN(p);
1.3       noro     3551:                for ( m = BDY(p), i = 0; i < len; i++, NMV_ADV(m) ) {
1.71      noro     3552:                        if ( CM(m) & 0x80000000 ) printf("+@_%d*",IFTOF(CM(m)));
                   3553:                        else printf("+%d*",CM(m));
1.16      noro     3554:                        ndl_print(DL(m));
                   3555:                }
                   3556:                printf("\n");
                   3557:        }
                   3558: }
                   3559:
1.113     noro     3560: void ndv_print_q(NDV p)
1.16      noro     3561: {
                   3562:        NMV m;
                   3563:        int i,len;
                   3564:
1.34      noro     3565:        if ( !p ) printf("0\n");
1.16      noro     3566:        else {
                   3567:                len = LEN(p);
                   3568:                for ( m = BDY(p), i = 0; i < len; i++, NMV_ADV(m) ) {
                   3569:                        printf("+");
1.113     noro     3570:                        printexpr(CO,(Obj)CQ(m));
1.16      noro     3571:                        printf("*");
1.14      noro     3572:                        ndl_print(DL(m));
1.3       noro     3573:                }
                   3574:                printf("\n");
                   3575:        }
1.25      noro     3576: }
                   3577:
1.61      noro     3578: NODE ndv_reducebase(NODE x)
1.27      noro     3579: {
                   3580:        int len,i,j;
                   3581:        NDV *w;
                   3582:        NODE t,t0;
                   3583:
                   3584:        len = length(x);
                   3585:        w = (NDV *)ALLOCA(len*sizeof(NDV));
                   3586:        for ( i = 0, t = x; i < len; i++, t = NEXT(t) ) w[i] = BDY(t);
                   3587:        for ( i = 0; i < len; i++ ) {
                   3588:                for ( j = 0; j < i; j++ ) {
                   3589:                        if ( w[i] && w[j] )
                   3590:                                if ( ndl_reducible(HDL(w[i]),HDL(w[j])) ) w[i] = 0;
                   3591:                                else if ( ndl_reducible(HDL(w[j]),HDL(w[i])) ) w[j] = 0;
                   3592:                }
                   3593:        }
                   3594:        for ( i = len-1, t0 = 0; i >= 0; i-- ) {
                   3595:                if ( w[i] ) { NEXTNODE(t0,t); BDY(t) = (pointer)w[i]; }
                   3596:        }
                   3597:        NEXT(t) = 0; x = t0;
                   3598:        return x;
1.11      noro     3599: }
1.32      noro     3600:
1.43      noro     3601: /* XXX incomplete */
                   3602:
1.32      noro     3603: void nd_init_ord(struct order_spec *ord)
                   3604: {
1.43      noro     3605:        switch ( ord->id ) {
1.32      noro     3606:                case 0:
1.43      noro     3607:                        switch ( ord->ord.simple ) {
                   3608:                                case 0:
                   3609:                                        nd_dcomp = 1;
                   3610:                                        nd_isrlex = 1;
                   3611:                                        break;
                   3612:                                case 1:
                   3613:                                        nd_dcomp = 1;
                   3614:                                        nd_isrlex = 0;
                   3615:                                        break;
                   3616:                                case 2:
                   3617:                                        nd_dcomp = 0;
                   3618:                                        nd_isrlex = 0;
1.45      noro     3619:                                        ndl_compare_function = ndl_lex_compare;
1.58      noro     3620:                                        break;
                   3621:                                case 11:
                   3622:                                        /* XXX */
                   3623:                                        nd_dcomp = 0;
                   3624:                                        nd_isrlex = 1;
                   3625:                                        ndl_compare_function = ndl_ww_lex_compare;
1.43      noro     3626:                                        break;
                   3627:                                default:
                   3628:                                        error("nd_gr : unsupported order");
                   3629:                        }
1.32      noro     3630:                        break;
                   3631:                case 1:
1.96      noro     3632:                        /* block order */
1.43      noro     3633:                        /* XXX */
                   3634:                        nd_dcomp = -1;
1.32      noro     3635:                        nd_isrlex = 0;
1.45      noro     3636:                        ndl_compare_function = ndl_block_compare;
1.34      noro     3637:                        break;
1.43      noro     3638:                case 2:
1.96      noro     3639:                        /* matrix order */
                   3640:                        /* XXX */
                   3641:                        nd_dcomp = -1;
                   3642:                        nd_isrlex = 0;
                   3643:                        nd_matrix_len = ord->ord.matrix.row;
                   3644:                        nd_matrix = ord->ord.matrix.matrix;
                   3645:                        ndl_compare_function = ndl_matrix_compare;
                   3646:                        break;
                   3647:                case 3:
1.97      noro     3648:                        /* composite order */
                   3649:                        nd_dcomp = -1;
                   3650:                        nd_isrlex = 0;
                   3651:                        nd_worb_len = ord->ord.composite.length;
                   3652:                        nd_worb = ord->ord.composite.w_or_b;
                   3653:                        ndl_compare_function = ndl_composite_compare;
1.32      noro     3654:                        break;
                   3655:        }
1.41      noro     3656:        nd_ord = ord;
1.32      noro     3657: }
                   3658:
1.43      noro     3659: BlockMask nd_create_blockmask(struct order_spec *ord)
                   3660: {
                   3661:        int n,i,j,s,l;
1.61      noro     3662:        UINT *t;
1.43      noro     3663:        BlockMask bm;
                   3664:
1.96      noro     3665:        /* we only create mask table for block order */
                   3666:        if ( ord->id != 1 )
1.43      noro     3667:                return 0;
                   3668:        n = ord->ord.block.length;
                   3669:        bm = (BlockMask)MALLOC(sizeof(struct oBlockMask));
                   3670:        bm->n = n;
                   3671:        bm->order_pair = ord->ord.block.order_pair;
1.61      noro     3672:        bm->mask = (UINT **)MALLOC(n*sizeof(UINT *));
1.43      noro     3673:        for ( i = 0, s = 0; i < n; i++ ) {
1.61      noro     3674:                bm->mask[i] = t = (UINT *)MALLOC_ATOMIC(nd_wpd*sizeof(UINT));
1.43      noro     3675:                for ( j = 0; j < nd_wpd; j++ ) t[j] = 0;
                   3676:                l = bm->order_pair[i].length;
                   3677:                for ( j = 0; j < l; j++, s++ ) PUT_EXP(t,s,nd_mask0);
                   3678:        }
                   3679:        return bm;
1.57      noro     3680: }
                   3681:
                   3682: EPOS nd_create_epos(struct order_spec *ord)
                   3683: {
                   3684:        int i,j,l,s,ord_l,ord_o;
                   3685:        EPOS epos;
                   3686:        struct order_pair *op;
                   3687:
                   3688:        epos = (EPOS)MALLOC_ATOMIC(nd_nvar*sizeof(struct oEPOS));
                   3689:        switch ( ord->id ) {
                   3690:                case 0:
                   3691:                        if ( nd_isrlex ) {
                   3692:                                for ( i = 0; i < nd_nvar; i++ ) {
                   3693:                                        epos[i].i = nd_exporigin + (nd_nvar-1-i)/nd_epw;
                   3694:                                        epos[i].s = (nd_epw-((nd_nvar-1-i)%nd_epw)-1)*nd_bpe;
                   3695:                                }
                   3696:                        } else {
                   3697:                                for ( i = 0; i < nd_nvar; i++ ) {
                   3698:                                        epos[i].i = nd_exporigin + i/nd_epw;
                   3699:                                        epos[i].s = (nd_epw-(i%nd_epw)-1)*nd_bpe;
                   3700:                                }
                   3701:                        }
                   3702:                        break;
                   3703:                case 1:
                   3704:                        /* block order */
                   3705:                        l = ord->ord.block.length;
                   3706:                        op = ord->ord.block.order_pair;
                   3707:                        for ( j = 0, s = 0; j < l; j++ ) {
                   3708:                                ord_o = op[j].order;
                   3709:                                ord_l = op[j].length;
                   3710:                                if ( !ord_o )
                   3711:                                        for ( i = 0; i < ord_l; i++ ) {
                   3712:                                                epos[s+i].i = nd_exporigin + (s+ord_l-i-1)/nd_epw;
                   3713:                                                epos[s+i].s = (nd_epw-((s+ord_l-i-1)%nd_epw)-1)*nd_bpe;
                   3714:                                        }
                   3715:                                else
                   3716:                                        for ( i = 0; i < ord_l; i++ ) {
                   3717:                                                epos[s+i].i = nd_exporigin + (s+i)/nd_epw;
                   3718:                                                epos[s+i].s = (nd_epw-((s+i)%nd_epw)-1)*nd_bpe;
                   3719:                                        }
                   3720:                                s += ord_l;
                   3721:                        }
                   3722:                        break;
                   3723:                case 2:
1.96      noro     3724:                        /* matrix order */
                   3725:                case 3:
                   3726:                        /* composite order */
                   3727:                        for ( i = 0; i < nd_nvar; i++ ) {
                   3728:                                epos[i].i = nd_exporigin + i/nd_epw;
                   3729:                                epos[i].s = (nd_epw-(i%nd_epw)-1)*nd_bpe;
                   3730:                        }
                   3731:                        break;
1.57      noro     3732:        }
                   3733:        return epos;
1.43      noro     3734: }
1.59      noro     3735:
                   3736: /* external interface */
                   3737:
                   3738: void nd_nf_p(P f,LIST g,LIST v,int m,struct order_spec *ord,P *rp)
                   3739: {
1.61      noro     3740:        NODE t,in0,in;
1.59      noro     3741:        ND nd,nf;
1.61      noro     3742:        NDV ndv;
                   3743:        VL vv,tv;
                   3744:        int stat,nvar,max,e;
1.113     noro     3745:        union oNDC dn;
1.59      noro     3746:
1.116     noro     3747:        if ( !f ) {
                   3748:                *rp = 0;
                   3749:                return;
                   3750:        }
1.59      noro     3751:        pltovl(v,&vv);
1.74      noro     3752:        for ( nvar = 0, tv = vv; tv; tv = NEXT(tv), nvar++ );
1.61      noro     3753:
                   3754:        /* get the degree bound */
                   3755:        for ( t = BDY(g), max = 0; t; t = NEXT(t) )
                   3756:                for ( tv = vv; tv; tv = NEXT(tv) ) {
                   3757:                        e = getdeg(tv->v,(P)BDY(t));
                   3758:                        max = MAX(e,max);
                   3759:                }
                   3760:        for ( tv = vv; tv; tv = NEXT(tv) ) {
                   3761:                e = getdeg(tv->v,f);
                   3762:                max = MAX(e,max);
                   3763:        }
                   3764:
1.59      noro     3765:        nd_init_ord(ord);
1.61      noro     3766:        nd_setup_parameters(nvar,max);
                   3767:
                   3768:        /* conversion to ndv */
                   3769:        for ( in0 = 0, t = BDY(g); t; t = NEXT(t) ) {
                   3770:                NEXTNODE(in0,in);
                   3771:                BDY(in) = (pointer)ptondv(CO,vv,(P)BDY(t));
1.69      noro     3772:                if ( m ) ndv_mod(m,(NDV)BDY(in));
1.61      noro     3773:        }
                   3774:        NEXTNODE(in0,in);
                   3775:        BDY(in) = (pointer)ptondv(CO,vv,f);
1.69      noro     3776:        if ( m ) ndv_mod(m,(NDV)BDY(in));
1.61      noro     3777:        NEXT(in) = 0;
                   3778:
                   3779:        ndv_setup(m,0,in0);
1.59      noro     3780:        nd_psn--;
                   3781:        nd_scale=2;
                   3782:        while ( 1 ) {
                   3783:                nd = (pointer)ndvtond(m,nd_ps[nd_psn]);
1.69      noro     3784:                stat = nd_nf(m,nd,nd_ps,1,0,&nf);
1.59      noro     3785:                if ( !stat ) {
                   3786:                        nd_psn++;
1.103     noro     3787:                        nd_reconstruct(0,0);
1.59      noro     3788:                        nd_psn--;
                   3789:                } else
                   3790:                        break;
                   3791:        }
1.61      noro     3792:        *rp = ndvtop(m,CO,vv,ndtondv(m,nf));
1.63      noro     3793: }
                   3794:
                   3795: int nd_to_vect(int mod,UINT *s0,int n,ND d,UINT *r)
                   3796: {
                   3797:        NM m;
                   3798:        UINT *t,*s;
                   3799:        int i;
                   3800:
                   3801:        for ( i = 0; i < n; i++ ) r[i] = 0;
                   3802:        for ( i = 0, s = s0, m = BDY(d); m; m = NEXT(m) ) {
                   3803:                t = DL(m);
                   3804:                for ( ; !ndl_equal(t,s); s += nd_wpd, i++ );
                   3805:                r[i] = CM(m);
                   3806:        }
                   3807:        for ( i = 0; !r[i]; i++ );
                   3808:        return i;
                   3809: }
                   3810:
1.113     noro     3811: int nd_to_vect_q(UINT *s0,int n,ND d,Q *r)
1.74      noro     3812: {
1.107     noro     3813:        NM m;
1.74      noro     3814:        UINT *t,*s;
1.107     noro     3815:        int i;
1.74      noro     3816:
                   3817:        for ( i = 0; i < n; i++ ) r[i] = 0;
1.107     noro     3818:        for ( i = 0, s = s0, m = BDY(d); m; m = NEXT(m) ) {
1.74      noro     3819:                t = DL(m);
                   3820:                for ( ; !ndl_equal(t,s); s += nd_wpd, i++ );
1.113     noro     3821:                r[i] = CQ(m);
1.74      noro     3822:        }
                   3823:        for ( i = 0; !r[i]; i++ );
                   3824:        return i;
                   3825: }
                   3826:
1.67      noro     3827: IndArray nm_ind_pair_to_vect_compress(int mod,UINT *s0,int n,NM_ind_pair pair)
1.64      noro     3828: {
                   3829:        NM m;
                   3830:        NMV mr;
                   3831:        UINT *d,*t,*s;
                   3832:        NDV p;
1.67      noro     3833:        unsigned char *ivc;
                   3834:        unsigned short *ivs;
1.81      noro     3835:        UINT *v,*ivi,*s0v;
1.67      noro     3836:        int i,j,len,prev,diff,cdiff;
                   3837:        IndArray r;
1.64      noro     3838:
                   3839:        m = pair->mul;
                   3840:        d = DL(m);
                   3841:        p = nd_ps[pair->index];
                   3842:        len = LEN(p);
                   3843:        t = (UINT *)ALLOCA(nd_wpd*sizeof(UINT));
1.67      noro     3844:        v = (unsigned int *)ALLOCA(len*sizeof(unsigned int));
1.64      noro     3845:        for ( i = j = 0, s = s0, mr = BDY(p); j < len; j++, NMV_ADV(mr) ) {
                   3846:                ndl_add(d,DL(mr),t);
                   3847:                for ( ; !ndl_equal(t,s); s += nd_wpd, i++ );
1.67      noro     3848:                v[j] = i;
                   3849:        }
1.81      noro     3850:        r = (IndArray)MALLOC(sizeof(struct oIndArray));
1.67      noro     3851:        r->head = v[0];
                   3852:        diff = 0;
                   3853:        for ( i = 1; i < len; i++ ) {
                   3854:                cdiff = v[i]-v[i-1]; diff = MAX(cdiff,diff);
                   3855:        }
                   3856:        if ( diff < 256 ) {
                   3857:                r->width = 1;
                   3858:                ivc = (unsigned char *)MALLOC_ATOMIC(len*sizeof(unsigned char));
                   3859:                r->index.c = ivc;
                   3860:                for ( i = 1, ivc[0] = 0; i < len; i++ ) ivc[i] = v[i]-v[i-1];
                   3861:        } else if ( diff < 65536 ) {
                   3862:                r->width = 2;
                   3863:                ivs = (unsigned short *)MALLOC_ATOMIC(len*sizeof(unsigned short));
                   3864:                r->index.s = ivs;
                   3865:                for ( i = 1, ivs[0] = 0; i < len; i++ ) ivs[i] = v[i]-v[i-1];
                   3866:        } else {
                   3867:                r->width = 4;
                   3868:                ivi = (unsigned int *)MALLOC_ATOMIC(len*sizeof(unsigned int));
                   3869:                r->index.i = ivi;
                   3870:                for ( i = 1, ivi[0] = 0; i < len; i++ ) ivi[i] = v[i]-v[i-1];
1.64      noro     3871:        }
1.67      noro     3872:        return r;
1.64      noro     3873: }
                   3874:
1.65      noro     3875:
1.113     noro     3876: int ndv_reduce_vect_q(Q *svect,int col,IndArray *imat,NM_ind_pair *rp0,int nred)
1.107     noro     3877: {
                   3878:        int i,j,k,len,pos,prev;
1.113     noro     3879:        Q cs,mcs,c1,c2,cr,gcd,t;
1.107     noro     3880:        IndArray ivect;
                   3881:        unsigned char *ivc;
                   3882:        unsigned short *ivs;
                   3883:        unsigned int *ivi;
                   3884:        NDV redv;
                   3885:        NMV mr;
                   3886:        NODE rp;
                   3887:        int maxrs;
                   3888:
                   3889:        maxrs = 0;
                   3890:        for ( i = 0; i < nred; i++ ) {
                   3891:                ivect = imat[i];
                   3892:                k = ivect->head;
                   3893:                if ( svect[k] ) {
                   3894:                        maxrs = MAX(maxrs,rp0[i]->sugar);
                   3895:                        redv = nd_ps[rp0[i]->index];
                   3896:                        len = LEN(redv); mr = BDY(redv);
1.113     noro     3897:                        igcd_cofactor(svect[k],CQ(mr),&gcd,&cs,&cr);
                   3898:                        chsgnq(cs,&mcs);
                   3899:                        if ( !UNIQ(cr) ) {
                   3900:                                for ( j = 0; j < col; j++ ) {
                   3901:                                        mulq(svect[j],cr,&c1); svect[j] = c1;
                   3902:                                }
                   3903:                        }
1.107     noro     3904:                        svect[k] = 0; prev = k;
                   3905:                        switch ( ivect->width ) {
                   3906:                                case 1:
                   3907:                                        ivc = ivect->index.c;
                   3908:                                        for ( j = 1, NMV_ADV(mr); j < len; j++, NMV_ADV(mr) ) {
                   3909:                                                pos = prev+ivc[j]; prev = pos;
1.113     noro     3910:                                                mulq(CQ(mr),mcs,&c2); addq(svect[pos],c2,&t); svect[pos] = t;
1.107     noro     3911:                                        }
                   3912:                                        break;
                   3913:                                case 2:
                   3914:                                        ivs = ivect->index.s;
                   3915:                                        for ( j = 1, NMV_ADV(mr); j < len; j++, NMV_ADV(mr) ) {
                   3916:                                                pos = prev+ivs[j]; prev = pos;
1.113     noro     3917:                                                mulq(CQ(mr),mcs,&c2); addq(svect[pos],c2,&t); svect[pos] = t;
1.107     noro     3918:                                        }
                   3919:                                        break;
                   3920:                                case 4:
                   3921:                                        ivi = ivect->index.i;
                   3922:                                        for ( j = 1, NMV_ADV(mr); j < len; j++, NMV_ADV(mr) ) {
                   3923:                                                pos = prev+ivi[j]; prev = pos;
1.113     noro     3924:                                                mulq(CQ(mr),mcs,&c2); addq(svect[pos],c2,&t); svect[pos] = t;
1.107     noro     3925:                                        }
                   3926:                                        break;
                   3927:                        }
                   3928:                }
                   3929:        }
                   3930:        return maxrs;
                   3931: }
                   3932:
1.76      noro     3933: int ndv_reduce_vect(int m,UINT *svect,int col,IndArray *imat,NM_ind_pair *rp0,int nred)
1.65      noro     3934: {
1.67      noro     3935:        int i,j,k,len,pos,prev;
1.66      noro     3936:        UINT c,c1,c2,c3,up,lo,dmy;
1.67      noro     3937:        IndArray ivect;
                   3938:        unsigned char *ivc;
                   3939:        unsigned short *ivs;
                   3940:        unsigned int *ivi;
1.65      noro     3941:        NDV redv;
1.67      noro     3942:        NMV mr;
1.65      noro     3943:        NODE rp;
1.76      noro     3944:        int maxrs;
1.65      noro     3945:
1.76      noro     3946:        maxrs = 0;
1.74      noro     3947:        for ( i = 0; i < nred; i++ ) {
1.65      noro     3948:                ivect = imat[i];
1.67      noro     3949:                k = ivect->head; svect[k] %= m;
1.65      noro     3950:                if ( c = svect[k] ) {
1.76      noro     3951:                        maxrs = MAX(maxrs,rp0[i]->sugar);
1.74      noro     3952:                        c = m-c; redv = nd_ps[rp0[i]->index];
1.67      noro     3953:                        len = LEN(redv); mr = BDY(redv);
                   3954:                        svect[k] = 0; prev = k;
                   3955:                        switch ( ivect->width ) {
                   3956:                                case 1:
                   3957:                                        ivc = ivect->index.c;
                   3958:                                        for ( j = 1, NMV_ADV(mr); j < len; j++, NMV_ADV(mr) ) {
                   3959:                                                pos = prev+ivc[j]; c1 = CM(mr); c2 = svect[pos];
                   3960:                                                prev = pos;
                   3961:                                                DMA(c1,c,c2,up,lo);
                   3962:                                                if ( up ) { DSAB(m,up,lo,dmy,c3); svect[pos] = c3;
                   3963:                                                } else svect[pos] = lo;
                   3964:                                        }
                   3965:                                        break;
                   3966:                                case 2:
                   3967:                                        ivs = ivect->index.s;
                   3968:                                        for ( j = 1, NMV_ADV(mr); j < len; j++, NMV_ADV(mr) ) {
                   3969:                                                pos = prev+ivs[j]; c1 = CM(mr); c2 = svect[pos];
                   3970:                                                prev = pos;
                   3971:                                                DMA(c1,c,c2,up,lo);
                   3972:                                                if ( up ) { DSAB(m,up,lo,dmy,c3); svect[pos] = c3;
                   3973:                                                } else svect[pos] = lo;
                   3974:                                        }
                   3975:                                        break;
                   3976:                                case 4:
                   3977:                                        ivi = ivect->index.i;
                   3978:                                        for ( j = 1, NMV_ADV(mr); j < len; j++, NMV_ADV(mr) ) {
                   3979:                                                pos = prev+ivi[j]; c1 = CM(mr); c2 = svect[pos];
                   3980:                                                prev = pos;
                   3981:                                                DMA(c1,c,c2,up,lo);
                   3982:                                                if ( up ) { DSAB(m,up,lo,dmy,c3); svect[pos] = c3;
                   3983:                                                } else svect[pos] = lo;
                   3984:                                        }
                   3985:                                        break;
1.65      noro     3986:                        }
                   3987:                }
                   3988:        }
1.66      noro     3989:        for ( i = 0; i < col; i++ )
                   3990:                if ( svect[i] >= (UINT)m ) svect[i] %= m;
1.76      noro     3991:        return maxrs;
1.65      noro     3992: }
                   3993:
1.76      noro     3994: int ndv_reduce_vect_sf(int m,UINT *svect,int col,IndArray *imat,NM_ind_pair *rp0,int nred)
1.72      noro     3995: {
                   3996:        int i,j,k,len,pos,prev;
                   3997:        UINT c,c1,c2,c3,up,lo,dmy;
                   3998:        IndArray ivect;
                   3999:        unsigned char *ivc;
                   4000:        unsigned short *ivs;
                   4001:        unsigned int *ivi;
                   4002:        NDV redv;
                   4003:        NMV mr;
                   4004:        NODE rp;
1.76      noro     4005:        int maxrs;
1.72      noro     4006:
1.76      noro     4007:        maxrs = 0;
1.74      noro     4008:        for ( i = 0; i < nred; i++ ) {
1.72      noro     4009:                ivect = imat[i];
                   4010:                k = ivect->head; svect[k] %= m;
                   4011:                if ( c = svect[k] ) {
1.76      noro     4012:                        maxrs = MAX(maxrs,rp0[i]->sugar);
1.74      noro     4013:                        c = _chsgnsf(c); redv = nd_ps[rp0[i]->index];
1.72      noro     4014:                        len = LEN(redv); mr = BDY(redv);
                   4015:                        svect[k] = 0; prev = k;
                   4016:                        switch ( ivect->width ) {
                   4017:                                case 1:
                   4018:                                        ivc = ivect->index.c;
                   4019:                                        for ( j = 1, NMV_ADV(mr); j < len; j++, NMV_ADV(mr) ) {
                   4020:                                                pos = prev+ivc[j]; prev = pos;
                   4021:                                                svect[pos] = _addsf(_mulsf(CM(mr),c),svect[pos]);
                   4022:                                        }
                   4023:                                        break;
                   4024:                                case 2:
                   4025:                                        ivs = ivect->index.s;
                   4026:                                        for ( j = 1, NMV_ADV(mr); j < len; j++, NMV_ADV(mr) ) {
                   4027:                                                pos = prev+ivs[j]; prev = pos;
                   4028:                                                svect[pos] = _addsf(_mulsf(CM(mr),c),svect[pos]);
                   4029:                                        }
                   4030:                                        break;
                   4031:                                case 4:
                   4032:                                        ivi = ivect->index.i;
                   4033:                                        for ( j = 1, NMV_ADV(mr); j < len; j++, NMV_ADV(mr) ) {
                   4034:                                                pos = prev+ivi[j]; prev = pos;
                   4035:                                                svect[pos] = _addsf(_mulsf(CM(mr),c),svect[pos]);
                   4036:                                        }
                   4037:                                        break;
                   4038:                        }
                   4039:                }
                   4040:        }
1.76      noro     4041:        return maxrs;
1.72      noro     4042: }
                   4043:
1.65      noro     4044: NDV vect_to_ndv(UINT *vect,int spcol,int col,int *rhead,UINT *s0vect)
                   4045: {
                   4046:        int j,k,len;
                   4047:        UINT *p;
                   4048:        UINT c;
                   4049:        NDV r;
                   4050:        NMV mr0,mr;
                   4051:
                   4052:        for ( j = 0, len = 0; j < spcol; j++ ) if ( vect[j] ) len++;
                   4053:        if ( !len ) return 0;
                   4054:        else {
1.74      noro     4055:                mr0 = (NMV)GC_malloc_atomic_ignore_off_page(nmv_adv*len);
1.103     noro     4056: #if 0
1.74      noro     4057:                ndv_alloc += nmv_adv*len;
1.103     noro     4058: #endif
1.65      noro     4059:                mr = mr0;
                   4060:                p = s0vect;
                   4061:                for ( j = k = 0; j < col; j++, p += nd_wpd )
                   4062:                        if ( !rhead[j] ) {
                   4063:                                if ( c = vect[k++] ) {
                   4064:                                        ndl_copy(p,DL(mr)); CM(mr) = c; NMV_ADV(mr);
                   4065:                                }
                   4066:                        }
                   4067:                MKNDV(nd_nvar,mr0,len,r);
                   4068:                return r;
                   4069:        }
                   4070: }
                   4071:
1.113     noro     4072: NDV vect_to_ndv_q(Q *vect,int spcol,int col,int *rhead,UINT *s0vect)
1.107     noro     4073: {
                   4074:        int j,k,len;
                   4075:        UINT *p;
1.113     noro     4076:        Q c;
1.107     noro     4077:        NDV r;
                   4078:        NMV mr0,mr;
                   4079:
                   4080:        for ( j = 0, len = 0; j < spcol; j++ ) if ( vect[j] ) len++;
                   4081:        if ( !len ) return 0;
                   4082:        else {
1.109     noro     4083:                mr0 = (NMV)GC_malloc(nmv_adv*len);
1.107     noro     4084: #if 0
                   4085:                ndv_alloc += nmv_adv*len;
                   4086: #endif
                   4087:                mr = mr0;
                   4088:                p = s0vect;
                   4089:                for ( j = k = 0; j < col; j++, p += nd_wpd )
                   4090:                        if ( !rhead[j] ) {
                   4091:                                if ( c = vect[k++] ) {
1.113     noro     4092:                                        if ( DN(c) )
                   4093:                                                error("afo");
                   4094:                                        ndl_copy(p,DL(mr)); CQ(mr) = c; NMV_ADV(mr);
1.107     noro     4095:                                }
                   4096:                        }
                   4097:                MKNDV(nd_nvar,mr0,len,r);
                   4098:                return r;
                   4099:        }
                   4100: }
                   4101:
1.74      noro     4102: int nd_sp_f4(int m,ND_pairs l,PGeoBucket bucket)
1.65      noro     4103: {
                   4104:        ND_pairs t;
                   4105:        NODE sp0,sp;
                   4106:        int stat;
                   4107:        ND spol;
                   4108:
                   4109:        for ( t = l; t; t = NEXT(t) ) {
                   4110:                stat = nd_sp(m,0,t,&spol);
                   4111:                if ( !stat ) return 0;
                   4112:                if ( spol ) {
                   4113:                        add_pbucket_symbolic(bucket,spol);
                   4114:                }
                   4115:        }
1.68      noro     4116:        return 1;
1.65      noro     4117: }
                   4118:
                   4119: int nd_symbolic_preproc(PGeoBucket bucket,UINT **s0vect,NODE *r)
                   4120: {
                   4121:        NODE rp0,rp;
                   4122:        NM mul,head,s0,s;
1.76      noro     4123:        int index,col,i,sugar;
1.65      noro     4124:        RHist h;
                   4125:        UINT *s0v,*p;
                   4126:        NM_ind_pair pair;
                   4127:        ND red;
                   4128:
                   4129:        s0 = 0; rp0 = 0; col = 0;
                   4130:        while ( 1 ) {
                   4131:                head = remove_head_pbucket_symbolic(bucket);
                   4132:                if ( !head ) break;
                   4133:                if ( !s0 ) s0 = head;
                   4134:                else NEXT(s) = head;
                   4135:                s = head;
                   4136:                index = ndl_find_reducer(DL(head));
                   4137:                if ( index >= 0 ) {
                   4138:                        h = nd_psh[index];
                   4139:                        NEWNM(mul);
                   4140:                        ndl_sub(DL(head),DL(h),DL(mul));
                   4141:                        if ( ndl_check_bound2(index,DL(mul)) ) return 0;
1.76      noro     4142:                        sugar = TD(DL(mul))+SG(nd_ps[index]);
                   4143:                        MKNM_ind_pair(pair,mul,index,sugar);
1.65      noro     4144:                        red = ndv_mul_nm_symbolic(mul,nd_ps[index]);
                   4145:                        add_pbucket_symbolic(bucket,nd_remove_head(red));
                   4146:                        NEXTNODE(rp0,rp); BDY(rp) = (pointer)pair;
                   4147:                }
                   4148:                col++;
                   4149:        }
1.72      noro     4150:        if ( rp0 ) NEXT(rp) = 0;
                   4151:        NEXT(s) = 0;
1.65      noro     4152:        s0v = (UINT *)MALLOC_ATOMIC(col*nd_wpd*sizeof(UINT));
                   4153:        for ( i = 0, p = s0v, s = s0; i < col;
                   4154:                i++, p += nd_wpd, s = NEXT(s) ) ndl_copy(DL(s),p);
                   4155:        *s0vect = s0v;
                   4156:        *r = rp0;
                   4157:        return col;
                   4158: }
                   4159:
1.69      noro     4160: NODE nd_f4(int m)
                   4161: {
                   4162:        int i,nh,stat,index;
                   4163:        NODE r,g;
                   4164:        ND_pairs d,l,t;
                   4165:        ND spol,red;
                   4166:        NDV nf,redv;
                   4167:        NM s0,s;
1.84      noro     4168:        NODE rp0,srp0,nflist;
1.69      noro     4169:        int nsp,nred,col,rank,len,k,j,a;
                   4170:        UINT c;
1.74      noro     4171:        UINT **spmat;
1.69      noro     4172:        UINT *s0vect,*svect,*p,*v;
                   4173:        int *colstat;
                   4174:        IndArray *imat;
                   4175:        int *rhead;
                   4176:        int spcol,sprow;
                   4177:        int sugar;
                   4178:        PGeoBucket bucket;
                   4179:        struct oEGT eg0,eg1,eg_f4;
                   4180:
1.103     noro     4181: #if 0
1.74      noro     4182:        ndv_alloc = 0;
1.103     noro     4183: #endif
1.69      noro     4184:        g = 0; d = 0;
                   4185:        for ( i = 0; i < nd_psn; i++ ) {
                   4186:                d = update_pairs(d,g,i);
                   4187:                g = update_base(g,i);
                   4188:        }
                   4189:        while ( d ) {
                   4190:                get_eg(&eg0);
                   4191:                l = nd_minsugarp(d,&d);
                   4192:                sugar = SG(l);
                   4193:                bucket = create_pbucket();
1.74      noro     4194:                stat = nd_sp_f4(m,l,bucket);
1.69      noro     4195:                if ( !stat ) {
                   4196:                        for ( t = l; NEXT(t); t = NEXT(t) );
                   4197:                        NEXT(t) = d; d = l;
1.103     noro     4198:                        d = nd_reconstruct(0,d);
1.69      noro     4199:                        continue;
                   4200:                }
1.86      noro     4201:                if ( bucket->m < 0 ) continue;
1.69      noro     4202:                col = nd_symbolic_preproc(bucket,&s0vect,&rp0);
                   4203:                if ( !col ) {
                   4204:                        for ( t = l; NEXT(t); t = NEXT(t) );
                   4205:                        NEXT(t) = d; d = l;
1.103     noro     4206:                        d = nd_reconstruct(0,d);
1.69      noro     4207:                        continue;
                   4208:                }
                   4209:                get_eg(&eg1); init_eg(&eg_f4); add_eg(&eg_f4,&eg0,&eg1);
1.74      noro     4210:                if ( DP_Print )
                   4211:                        fprintf(asir_out,"sugar=%d,symb=%fsec,",
                   4212:                                sugar,eg_f4.exectime+eg_f4.gctime);
                   4213:                if ( 1 )
                   4214:                        nflist = nd_f4_red(m,l,s0vect,col,rp0);
                   4215:                else
                   4216:                        nflist = nd_f4_red_dist(m,l,s0vect,col,rp0);
1.69      noro     4217:                /* adding new bases */
1.74      noro     4218:                for ( r = nflist; r; r = NEXT(r) ) {
                   4219:                        nf = (NDV)BDY(r);
1.69      noro     4220:                        ndv_removecont(m,nf);
1.77      noro     4221:                        nh = ndv_newps(m,nf,0);
1.69      noro     4222:                        d = update_pairs(d,g,nh);
                   4223:                        g = update_base(g,nh);
                   4224:                }
                   4225:        }
                   4226:        for ( r = g; r; r = NEXT(r) ) BDY(r) = (pointer)nd_ps[(int)BDY(r)];
1.103     noro     4227: #if 0
1.74      noro     4228:        fprintf(asir_out,"ndv_alloc=%d\n",ndv_alloc);
1.103     noro     4229: #endif
1.69      noro     4230:        return g;
                   4231: }
1.74      noro     4232:
                   4233: NODE nd_f4_red(int m,ND_pairs sp0,UINT *s0vect,int col,NODE rp0)
1.63      noro     4234: {
1.67      noro     4235:        IndArray *imat;
1.106     noro     4236:        int nsp,nred,i;
1.65      noro     4237:        int *rhead;
1.106     noro     4238:        NODE r0,rp;
1.74      noro     4239:        ND_pairs sp;
                   4240:        NM_ind_pair *rvect;
1.63      noro     4241:
1.74      noro     4242:        for ( sp = sp0, nsp = 0; sp; sp = NEXT(sp), nsp++ );
1.106     noro     4243:        nred = length(rp0);
1.74      noro     4244:        imat = (IndArray *)ALLOCA(nred*sizeof(IndArray));
                   4245:        rhead = (int *)ALLOCA(col*sizeof(int));
                   4246:        for ( i = 0; i < col; i++ ) rhead[i] = 0;
1.63      noro     4247:
1.74      noro     4248:        /* construction of index arrays */
                   4249:        rvect = (NM_ind_pair *)ALLOCA(nred*sizeof(NM_ind_pair));
                   4250:        for ( rp = rp0, i = 0; rp; i++, rp = NEXT(rp) ) {
                   4251:                rvect[i] = (NM_ind_pair)BDY(rp);
                   4252:                imat[i] = nm_ind_pair_to_vect_compress(m,s0vect,col,rvect[i]);
                   4253:                rhead[imat[i]->head] = 1;
                   4254:        }
1.107     noro     4255:        if ( m )
                   4256:                r0 = nd_f4_red_main(m,sp0,nsp,s0vect,col,rvect,rhead,imat,nred);
                   4257:        else
1.113     noro     4258:                r0 = nd_f4_red_q_main(sp0,nsp,s0vect,col,rvect,rhead,imat,nred);
1.106     noro     4259:        return r0;
                   4260: }
1.74      noro     4261:
1.106     noro     4262: NODE nd_f4_red_main(int m,ND_pairs sp0,int nsp,UINT *s0vect,int col,
                   4263:         NM_ind_pair *rvect,int *rhead,IndArray *imat,int nred)
                   4264: {
                   4265:        int spcol,sprow,a;
                   4266:        int i,j,k,l,rank;
                   4267:        NODE r0,r;
                   4268:        ND_pairs sp;
                   4269:        ND spol;
                   4270:        int **spmat;
                   4271:        UINT *svect,*v;
                   4272:        int *colstat;
                   4273:        struct oEGT eg0,eg1,eg2,eg_f4,eg_f4_1,eg_f4_2;
                   4274:        int maxrs;
                   4275:        int *spsugar;
                   4276:
                   4277:        spcol = col-nred;
                   4278:        get_eg(&eg0);
1.74      noro     4279:        /* elimination (1st step) */
                   4280:        spmat = (int **)ALLOCA(nsp*sizeof(UINT *));
                   4281:        svect = (UINT *)ALLOCA(col*sizeof(UINT));
1.76      noro     4282:        spsugar = (int *)ALLOCA(nsp*sizeof(UINT));
1.74      noro     4283:        for ( a = sprow = 0, sp = sp0; a < nsp; a++, sp = NEXT(sp) ) {
                   4284:                nd_sp(m,0,sp,&spol);
1.75      noro     4285:                if ( !spol ) continue;
1.74      noro     4286:                nd_to_vect(m,s0vect,col,spol,svect);
1.76      noro     4287:                if ( m == -1 )
                   4288:                        maxrs = ndv_reduce_vect_sf(m,svect,col,imat,rvect,nred);
                   4289:                else
                   4290:                        maxrs = ndv_reduce_vect(m,svect,col,imat,rvect,nred);
1.74      noro     4291:                for ( i = 0; i < col; i++ ) if ( svect[i] ) break;
                   4292:                if ( i < col ) {
                   4293:                        spmat[sprow] = v = (UINT *)MALLOC_ATOMIC(spcol*sizeof(UINT));
                   4294:                        for ( j = k = 0; j < col; j++ )
                   4295:                                if ( !rhead[j] ) v[k++] = svect[j];
1.76      noro     4296:                        spsugar[sprow] = MAX(maxrs,SG(spol));
1.74      noro     4297:                        sprow++;
                   4298:                }
1.76      noro     4299:                nd_free(spol);
1.74      noro     4300:        }
1.80      noro     4301:        get_eg(&eg1); init_eg(&eg_f4_1); add_eg(&eg_f4_1,&eg0,&eg1);
                   4302:        if ( DP_Print ) {
                   4303:                fprintf(asir_out,"elim1=%fsec,",eg_f4_1.exectime+eg_f4_1.gctime);
                   4304:                fflush(asir_out);
                   4305:        }
1.74      noro     4306:        /* free index arrays */
                   4307:        for ( i = 0; i < nred; i++ ) GC_free(imat[i]->index.c);
                   4308:
                   4309:        /* elimination (2nd step) */
                   4310:        colstat = (int *)ALLOCA(spcol*sizeof(int));
                   4311:        if ( m == -1 )
1.76      noro     4312:                rank = nd_gauss_elim_sf(spmat,spsugar,sprow,spcol,m,colstat);
1.74      noro     4313:        else
1.76      noro     4314:                rank = nd_gauss_elim_mod(spmat,spsugar,sprow,spcol,m,colstat);
1.74      noro     4315:        r0 = 0;
                   4316:        for ( i = 0; i < rank; i++ ) {
                   4317:                NEXTNODE(r0,r); BDY(r) =
                   4318:                        (pointer)vect_to_ndv(spmat[i],spcol,col,rhead,s0vect);
1.76      noro     4319:                SG((NDV)BDY(r)) = spsugar[i];
1.74      noro     4320:                GC_free(spmat[i]);
                   4321:        }
1.109     noro     4322:        if ( r0 ) NEXT(r) = 0;
1.74      noro     4323:        for ( ; i < sprow; i++ ) GC_free(spmat[i]);
1.80      noro     4324:        get_eg(&eg2); init_eg(&eg_f4_2); add_eg(&eg_f4_2,&eg1,&eg2);
                   4325:        init_eg(&eg_f4); add_eg(&eg_f4,&eg0,&eg2);
1.74      noro     4326:        if ( DP_Print ) {
1.80      noro     4327:                fprintf(asir_out,"elim2=%fsec\n",eg_f4_2.exectime+eg_f4_2.gctime);
1.74      noro     4328:                fprintf(asir_out,"nsp=%d,nred=%d,spmat=(%d,%d),rank=%d  ",
                   4329:                        nsp,nred,sprow,spcol,rank);
                   4330:                fprintf(asir_out,"%fsec\n",eg_f4.exectime+eg_f4.gctime);
1.63      noro     4331:        }
1.74      noro     4332:        return r0;
                   4333: }
                   4334:
1.113     noro     4335: NODE nd_f4_red_q_main(ND_pairs sp0,int nsp,UINT *s0vect,int col,
1.107     noro     4336:         NM_ind_pair *rvect,int *rhead,IndArray *imat,int nred)
                   4337: {
                   4338:        int spcol,sprow,a;
                   4339:        int i,j,k,l,rank;
                   4340:        NODE r0,r;
                   4341:        ND_pairs sp;
                   4342:        ND spol;
1.113     noro     4343:        Q **spmat;
                   4344:        Q *svect,*v;
1.107     noro     4345:        int *colstat;
                   4346:        struct oEGT eg0,eg1,eg2,eg_f4,eg_f4_1,eg_f4_2;
                   4347:        int maxrs;
                   4348:        int *spsugar;
                   4349:
                   4350:        spcol = col-nred;
                   4351:        get_eg(&eg0);
                   4352:        /* elimination (1st step) */
1.113     noro     4353:        spmat = (Q **)ALLOCA(nsp*sizeof(Q *));
                   4354:        svect = (Q *)ALLOCA(col*sizeof(Q));
                   4355:        spsugar = (int *)ALLOCA(nsp*sizeof(Q));
1.107     noro     4356:        for ( a = sprow = 0, sp = sp0; a < nsp; a++, sp = NEXT(sp) ) {
                   4357:                nd_sp(0,0,sp,&spol);
                   4358:                if ( !spol ) continue;
1.113     noro     4359:                nd_to_vect_q(s0vect,col,spol,svect);
                   4360:                maxrs = ndv_reduce_vect_q(svect,col,imat,rvect,nred);
1.107     noro     4361:                for ( i = 0; i < col; i++ ) if ( svect[i] ) break;
                   4362:                if ( i < col ) {
1.113     noro     4363:                        spmat[sprow] = v = (Q *)MALLOC(spcol*sizeof(Q));
1.107     noro     4364:                        for ( j = k = 0; j < col; j++ )
                   4365:                                if ( !rhead[j] ) v[k++] = svect[j];
                   4366:                        spsugar[sprow] = MAX(maxrs,SG(spol));
                   4367:                        sprow++;
                   4368:                }
1.109     noro     4369: /*             nd_free(spol); */
1.107     noro     4370:        }
                   4371:        get_eg(&eg1); init_eg(&eg_f4_1); add_eg(&eg_f4_1,&eg0,&eg1);
                   4372:        if ( DP_Print ) {
                   4373:                fprintf(asir_out,"elim1=%fsec,",eg_f4_1.exectime+eg_f4_1.gctime);
                   4374:                fflush(asir_out);
                   4375:        }
                   4376:        /* free index arrays */
1.109     noro     4377: /*     for ( i = 0; i < nred; i++ ) GC_free(imat[i]->index.c); */
1.107     noro     4378:
                   4379:        /* elimination (2nd step) */
                   4380:        colstat = (int *)ALLOCA(spcol*sizeof(int));
1.113     noro     4381:        rank = nd_gauss_elim_q(spmat,spsugar,sprow,spcol,colstat);
1.107     noro     4382:        r0 = 0;
                   4383:        for ( i = 0; i < rank; i++ ) {
                   4384:                NEXTNODE(r0,r); BDY(r) =
1.113     noro     4385:                        (pointer)vect_to_ndv_q(spmat[i],spcol,col,rhead,s0vect);
1.107     noro     4386:                SG((NDV)BDY(r)) = spsugar[i];
1.109     noro     4387: /*             GC_free(spmat[i]); */
1.107     noro     4388:        }
1.109     noro     4389:        if ( r0 ) NEXT(r) = 0;
                   4390:
                   4391: /*     for ( ; i < sprow; i++ ) GC_free(spmat[i]); */
1.107     noro     4392:        get_eg(&eg2); init_eg(&eg_f4_2); add_eg(&eg_f4_2,&eg1,&eg2);
                   4393:        init_eg(&eg_f4); add_eg(&eg_f4,&eg0,&eg2);
                   4394:        if ( DP_Print ) {
                   4395:                fprintf(asir_out,"elim2=%fsec\n",eg_f4_2.exectime+eg_f4_2.gctime);
                   4396:                fprintf(asir_out,"nsp=%d,nred=%d,spmat=(%d,%d),rank=%d  ",
                   4397:                        nsp,nred,sprow,spcol,rank);
                   4398:                fprintf(asir_out,"%fsec\n",eg_f4.exectime+eg_f4.gctime);
                   4399:        }
                   4400:        return r0;
                   4401: }
                   4402:
1.74      noro     4403: FILE *nd_write,*nd_read;
                   4404:
                   4405: void nd_send_int(int a) {
                   4406:        write_int(nd_write,&a);
                   4407: }
                   4408:
                   4409: void nd_send_intarray(int *p,int len) {
                   4410:        write_intarray(nd_write,p,len);
                   4411: }
                   4412:
                   4413: int nd_recv_int() {
                   4414:        int a;
                   4415:
                   4416:        read_int(nd_read,&a);
                   4417:        return a;
                   4418: }
                   4419:
                   4420: void nd_recv_intarray(int *p,int len) {
                   4421:        read_intarray(nd_read,p,len);
                   4422: }
                   4423:
                   4424: void nd_send_ndv(NDV p) {
                   4425:        int len,i;
                   4426:        NMV 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++, NMV_ADV(m) ) {
                   4434:                        nd_send_int(CM(m));
                   4435:                        nd_send_intarray(DL(m),nd_wpd);
1.68      noro     4436:                }
1.74      noro     4437:        }
                   4438: }
                   4439:
                   4440: void nd_send_nd(ND p) {
                   4441:        int len,i;
                   4442:        NM m;
                   4443:
                   4444:        if ( !p ) nd_send_int(0);
                   4445:        else {
                   4446:                len = LEN(p);
                   4447:                nd_send_int(len);
                   4448:                m = BDY(p);
                   4449:                for ( i = 0; i < len; i++, m = NEXT(m) ) {
                   4450:                        nd_send_int(CM(m));
                   4451:                        nd_send_intarray(DL(m),nd_wpd);
1.65      noro     4452:                }
1.74      noro     4453:        }
                   4454: }
1.65      noro     4455:
1.74      noro     4456: NDV nd_recv_ndv()
                   4457: {
                   4458:        int len,i;
                   4459:        NMV m,m0;
                   4460:        NDV r;
1.65      noro     4461:
1.74      noro     4462:        len = nd_recv_int();
                   4463:        if ( !len ) return 0;
                   4464:        else {
                   4465:                m0 = m = (NMV)GC_malloc_atomic_ignore_off_page(nmv_adv*len);
1.103     noro     4466: #if 0
1.74      noro     4467:                ndv_alloc += len*nmv_adv;
1.103     noro     4468: #endif
1.74      noro     4469:                for ( i = 0; i < len; i++, NMV_ADV(m) ) {
                   4470:                        CM(m) = nd_recv_int();
                   4471:                        nd_recv_intarray(DL(m),nd_wpd);
1.71      noro     4472:                }
1.74      noro     4473:                MKNDV(nd_nvar,m0,len,r);
                   4474:                return r;
                   4475:        }
                   4476: }
1.65      noro     4477:
1.74      noro     4478: int ox_exec_f4_red(Q proc)
                   4479: {
                   4480:        Obj obj;
                   4481:        STRING fname;
                   4482:        NODE arg;
                   4483:        int s;
                   4484:        extern int ox_need_conv,ox_file_io;
                   4485:
                   4486:        MKSTR(fname,"nd_exec_f4_red");
                   4487:        arg = mknode(2,proc,fname);
                   4488:        Pox_cmo_rpc(arg,&obj);
                   4489:        s = get_ox_server_id(QTOS(proc));
                   4490:        nd_write = iofp[s].out;
                   4491:        nd_read = iofp[s].in;
                   4492:        ox_need_conv = ox_file_io = 0;
                   4493:        return s;
                   4494: }
                   4495:
                   4496: NODE nd_f4_red_dist(int m,ND_pairs sp0,UINT *s0vect,int col,NODE rp0)
                   4497: {
                   4498:        int nsp,nred;
                   4499:        int i,rank,s;
                   4500:        NODE rp,r0,r;
                   4501:        ND_pairs sp;
                   4502:        NM_ind_pair pair;
                   4503:        NMV nmv;
                   4504:        NM nm;
                   4505:        NDV nf;
                   4506:        Obj proc,dmy;
                   4507:
                   4508:        ox_launch_main(0,0,&proc);
                   4509:        s = ox_exec_f4_red((Q)proc);
                   4510:
                   4511:        nd_send_int(m);
                   4512:        nd_send_int(nd_nvar);
                   4513:        nd_send_int(nd_bpe);
                   4514:        nd_send_int(nd_wpd);
                   4515:        nd_send_int(nmv_adv);
                   4516:
1.89      noro     4517:        saveobj(nd_write,dp_current_spec->obj); fflush(nd_write);
1.74      noro     4518:
                   4519:        nd_send_int(nd_psn);
                   4520:        for ( i = 0; i < nd_psn; i++ ) nd_send_ndv(nd_ps[i]);
                   4521:
                   4522:        for ( sp = sp0, nsp = 0; sp; sp = NEXT(sp), nsp++ );
                   4523:        nd_send_int(nsp);
                   4524:        for ( i = 0, sp = sp0; i < nsp; i++, sp = NEXT(sp) ) {
                   4525:                nd_send_int(sp->i1); nd_send_int(sp->i2);
                   4526:        }
                   4527:
                   4528:        nd_send_int(col); nd_send_intarray(s0vect,col*nd_wpd);
                   4529:
                   4530:        nred = length(rp0); nd_send_int(nred);
                   4531:        for ( i = 0, rp = rp0; i < nred; i++, rp = NEXT(rp) ) {
                   4532:                pair = (NM_ind_pair)BDY(rp);
                   4533:                nd_send_int(pair->index);
                   4534:                nd_send_intarray(pair->mul->dl,nd_wpd);
                   4535:        }
                   4536:        fflush(nd_write);
                   4537:        rank = nd_recv_int();
                   4538:        fprintf(asir_out,"rank=%d\n",rank);
                   4539:        r0 = 0;
                   4540:        for ( i = 0; i < rank; i++ ) {
                   4541:                nf = nd_recv_ndv();
                   4542:                NEXTNODE(r0,r); BDY(r) = (pointer)nf;
                   4543:        }
                   4544:        Pox_shutdown(mknode(1,proc),&dmy);
                   4545:        return r0;
                   4546: }
                   4547:
                   4548: /* server side */
                   4549:
                   4550: void nd_exec_f4_red_dist()
                   4551: {
                   4552:        int m,i,nsp,col,s0size,nred,spcol,j,k;
                   4553:        NM_ind_pair *rp0;
                   4554:        NDV nf;
                   4555:        UINT *s0vect;
                   4556:        IndArray *imat;
                   4557:        int *rhead;
                   4558:        int **spmat;
                   4559:        UINT *svect,*v;
                   4560:        ND_pairs *sp0;
                   4561:        int *colstat;
                   4562:        int a,sprow,rank;
1.89      noro     4563:        struct order_spec *ord;
1.74      noro     4564:        Obj ordspec;
                   4565:        ND spol;
1.76      noro     4566:        int maxrs;
                   4567:        int *spsugar;
1.74      noro     4568:
                   4569:        nd_read = iofp[0].in;
                   4570:        nd_write = iofp[0].out;
                   4571:        m = nd_recv_int();
                   4572:        nd_nvar = nd_recv_int();
                   4573:        nd_bpe = nd_recv_int();
                   4574:        nd_wpd = nd_recv_int();
                   4575:        nmv_adv = nd_recv_int();
                   4576:
                   4577:        loadobj(nd_read,&ordspec);
1.89      noro     4578:        create_order_spec(0,ordspec,&ord);
                   4579:        nd_init_ord(ord);
1.74      noro     4580:        nd_setup_parameters(nd_nvar,0);
                   4581:
                   4582:        nd_psn = nd_recv_int();
                   4583:        nd_ps = (NDV *)MALLOC(nd_psn*sizeof(NDV));
                   4584:        nd_bound = (UINT **)MALLOC(nd_psn*sizeof(UINT *));
                   4585:        for ( i = 0; i < nd_psn; i++ ) {
                   4586:                nd_ps[i] = nd_recv_ndv();
                   4587:                nd_bound[i] = ndv_compute_bound(nd_ps[i]);
                   4588:        }
                   4589:
                   4590:        nsp = nd_recv_int();
                   4591:        sp0 = (ND_pairs *)MALLOC(nsp*sizeof(ND_pairs));
                   4592:        for ( i = 0; i < nsp; i++ ) {
                   4593:                NEWND_pairs(sp0[i]);
                   4594:                sp0[i]->i1 = nd_recv_int(); sp0[i]->i2 = nd_recv_int();
                   4595:                ndl_lcm(HDL(nd_ps[sp0[i]->i1]),HDL(nd_ps[sp0[i]->i2]),LCM(sp0[i]));
                   4596:        }
                   4597:
                   4598:        col = nd_recv_int();
                   4599:        s0size = col*nd_wpd;
                   4600:        s0vect = (UINT *)MALLOC(s0size*sizeof(UINT));
                   4601:        nd_recv_intarray(s0vect,s0size);
                   4602:
                   4603:        nred = nd_recv_int();
                   4604:        rp0 = (NM_ind_pair *)MALLOC(nred*sizeof(NM_ind_pair));
                   4605:        for ( i = 0; i < nred; i++ ) {
                   4606:                rp0[i] = (NM_ind_pair)MALLOC(sizeof(struct oNM_ind_pair));
                   4607:                rp0[i]->index = nd_recv_int();
                   4608:                rp0[i]->mul = (NM)MALLOC(sizeof(struct oNM)+(nd_wpd-1)*sizeof(UINT));
                   4609:                nd_recv_intarray(rp0[i]->mul->dl,nd_wpd);
                   4610:        }
                   4611:
                   4612:        spcol = col-nred;
                   4613:        imat = (IndArray *)MALLOC(nred*sizeof(IndArray));
                   4614:        rhead = (int *)MALLOC(col*sizeof(int));
                   4615:        for ( i = 0; i < col; i++ ) rhead[i] = 0;
                   4616:
                   4617:        /* construction of index arrays */
                   4618:        for ( i = 0; i < nred; i++ ) {
                   4619:                imat[i] = nm_ind_pair_to_vect_compress(m,s0vect,col,rp0[i]);
                   4620:                rhead[imat[i]->head] = 1;
                   4621:        }
                   4622:
                   4623:        /* elimination (1st step) */
                   4624:        spmat = (int **)MALLOC(nsp*sizeof(UINT *));
                   4625:        svect = (UINT *)MALLOC(col*sizeof(UINT));
1.76      noro     4626:        spsugar = (int *)ALLOCA(nsp*sizeof(UINT));
1.74      noro     4627:        for ( a = sprow = 0; a < nsp; a++ ) {
                   4628:                nd_sp(m,0,sp0[a],&spol);
1.75      noro     4629:                if ( !spol ) continue;
1.74      noro     4630:                nd_to_vect(m,s0vect,col,spol,svect);
1.76      noro     4631:                if ( m == -1 )
                   4632:                        maxrs = ndv_reduce_vect_sf(m,svect,col,imat,rp0,nred);
                   4633:                else
                   4634:                        maxrs = ndv_reduce_vect(m,svect,col,imat,rp0,nred);
1.74      noro     4635:                for ( i = 0; i < col; i++ ) if ( svect[i] ) break;
                   4636:                if ( i < col ) {
                   4637:                        spmat[sprow] = v = (UINT *)MALLOC(spcol*sizeof(UINT));
                   4638:                        for ( j = k = 0; j < col; j++ )
                   4639:                                if ( !rhead[j] ) v[k++] = svect[j];
1.76      noro     4640:                        spsugar[sprow] = MAX(maxrs,SG(spol));
1.74      noro     4641:                        sprow++;
                   4642:                }
1.76      noro     4643:                nd_free(spol);
1.74      noro     4644:        }
                   4645:        /* elimination (2nd step) */
                   4646:        colstat = (int *)ALLOCA(spcol*sizeof(int));
                   4647:        if ( m == -1 )
1.76      noro     4648:                rank = nd_gauss_elim_sf(spmat,spsugar,sprow,spcol,m,colstat);
1.74      noro     4649:        else
1.76      noro     4650:                rank = nd_gauss_elim_mod(spmat,spsugar,sprow,spcol,m,colstat);
1.74      noro     4651:        nd_send_int(rank);
                   4652:        for ( i = 0; i < rank; i++ ) {
                   4653:                nf = vect_to_ndv(spmat[i],spcol,col,rhead,s0vect);
                   4654:                nd_send_ndv(nf);
1.63      noro     4655:        }
1.74      noro     4656:        fflush(nd_write);
1.107     noro     4657: }
                   4658:
1.113     noro     4659: int nd_gauss_elim_q(Q **mat0,int *sugar,int row,int col,int *colstat)
1.107     noro     4660: {
1.109     noro     4661:        int mod,i,j,t,c,rank,rank0,inv;
                   4662:        int *ci,*ri;
1.113     noro     4663:        Q dn;
1.109     noro     4664:        MAT m,nm;
1.108     noro     4665:        int **wmat;
                   4666:
                   4667:        /* XXX */
                   4668:        mod = 99999989;
                   4669:        wmat = (int **)ALLOCA(row*sizeof(int *));
                   4670:        for ( i = 0; i < row; i++ ) {
                   4671:                wmat[i] = (int *)ALLOCA(col*sizeof(int));
                   4672:                for ( j = 0; j < col; j++ ) {
1.113     noro     4673:                        if ( mat0[i][j] ) {
                   4674:                                t = rem(NM(mat0[i][j]),mod);
                   4675:                                if ( SGN(mat0[i][j]) < 0 ) t = mod-t;
                   4676:                                wmat[i][j] = t;
                   4677:                        } else
1.108     noro     4678:                                wmat[i][j] = 0;
                   4679:                }
                   4680:        }
1.109     noro     4681:        rank0 = nd_gauss_elim_mod(wmat,sugar,row,col,mod,colstat);
                   4682:        NEWMAT(m); m->row = row; m->col = col; m->body = (pointer **)mat0;
1.113     noro     4683:        rank = generic_gauss_elim(m,&nm,&dn,&ri,&ci);
1.109     noro     4684:        if ( rank != rank0 )
                   4685:                error("afo");
                   4686:        for ( i = 0; i < row; i++ )
                   4687:                for ( j = 0; j < col; j++ )
                   4688:                        mat0[i][j] = 0;
                   4689:        c = col-rank;
                   4690:        for ( i = 0; i < rank; i++ ) {
1.113     noro     4691:                mat0[i][ri[i]] = dn;
1.109     noro     4692:                for ( j = 0; j < c; j++ )
1.113     noro     4693:                        mat0[i][ci[j]] = (Q)BDY(nm)[i][j];
1.109     noro     4694:        }
1.113     noro     4695:        inv = invm(rem(NM(dn),mod),mod);
                   4696:        if ( SGN(dn) < 0 ) inv = mod-inv;
1.109     noro     4697:        for ( i = 0; i < row; i++ )
                   4698:                for ( j = 0; j < col; j++ ) {
1.113     noro     4699:                        if ( mat0[i][j] ) {
                   4700:                                t = rem(NM(mat0[i][j]),mod);
                   4701:                                if ( SGN(mat0[i][j]) < 0 ) t = mod-t;
                   4702:                        } else
1.109     noro     4703:                                t = 0;
                   4704:                        c = dmar(t,inv,0,mod);
                   4705:                        if ( wmat[i][j] != c )
                   4706:                                error("afo");
                   4707:                }
                   4708:        return rank;
1.76      noro     4709: }
                   4710:
                   4711: int nd_gauss_elim_mod(int **mat0,int *sugar,int row,int col,int md,int *colstat)
                   4712: {
                   4713:        int i,j,k,l,inv,a,rank,s;
                   4714:        unsigned int *t,*pivot,*pk;
                   4715:        unsigned int **mat;
                   4716:
                   4717:        mat = (unsigned int **)mat0;
                   4718:        for ( rank = 0, j = 0; j < col; j++ ) {
                   4719:                for ( i = rank; i < row; i++ )
                   4720:                        mat[i][j] %= md;
                   4721:                for ( i = rank; i < row; i++ )
                   4722:                        if ( mat[i][j] )
                   4723:                                break;
                   4724:                if ( i == row ) {
                   4725:                        colstat[j] = 0;
                   4726:                        continue;
                   4727:                } else
                   4728:                        colstat[j] = 1;
                   4729:                if ( i != rank ) {
                   4730:                        t = mat[i]; mat[i] = mat[rank]; mat[rank] = t;
                   4731:                        s = sugar[i]; sugar[i] = sugar[rank]; sugar[rank] = s;
                   4732:                }
                   4733:                pivot = mat[rank];
                   4734:                s = sugar[rank];
                   4735:                inv = invm(pivot[j],md);
                   4736:                for ( k = j, pk = pivot+k; k < col; k++, pk++ )
                   4737:                        if ( *pk ) {
                   4738:                                if ( *pk >= (unsigned int)md )
                   4739:                                        *pk %= md;
                   4740:                                DMAR(*pk,inv,0,md,*pk)
                   4741:                        }
                   4742:                for ( i = rank+1; i < row; i++ ) {
                   4743:                        t = mat[i];
                   4744:                        if ( a = t[j] ) {
                   4745:                                sugar[i] = MAX(sugar[i],s);
                   4746:                                red_by_vect(md,t+j,pivot+j,md-a,col-j);
                   4747:                        }
                   4748:                }
                   4749:                rank++;
                   4750:        }
                   4751:        for ( j = col-1, l = rank-1; j >= 0; j-- )
                   4752:                if ( colstat[j] ) {
                   4753:                        pivot = mat[l];
                   4754:                        s = sugar[l];
                   4755:                        for ( i = 0; i < l; i++ ) {
                   4756:                                t = mat[i];
                   4757:                                t[j] %= md;
                   4758:                                if ( a = t[j] ) {
                   4759:                                        sugar[i] = MAX(sugar[i],s);
                   4760:                                        red_by_vect(md,t+j,pivot+j,md-a,col-j);
                   4761:                                }
                   4762:                        }
                   4763:                        l--;
                   4764:                }
                   4765:        for ( j = 0, l = 0; l < rank; j++ )
                   4766:                if ( colstat[j] ) {
                   4767:                        t = mat[l];
                   4768:                        for ( k = j; k < col; k++ )
                   4769:                                if ( t[k] >= (unsigned int)md )
                   4770:                                        t[k] %= md;
                   4771:                        l++;
                   4772:                }
                   4773:        return rank;
                   4774: }
                   4775:
                   4776: int nd_gauss_elim_sf(int **mat0,int *sugar,int row,int col,int md,int *colstat)
                   4777: {
                   4778:        int i,j,k,l,inv,a,rank,s;
                   4779:        unsigned int *t,*pivot,*pk;
                   4780:        unsigned int **mat;
                   4781:
                   4782:        mat = (unsigned int **)mat0;
                   4783:        for ( rank = 0, j = 0; j < col; j++ ) {
                   4784:                for ( i = rank; i < row; i++ )
                   4785:                        if ( mat[i][j] )
                   4786:                                break;
                   4787:                if ( i == row ) {
                   4788:                        colstat[j] = 0;
                   4789:                        continue;
                   4790:                } else
                   4791:                        colstat[j] = 1;
                   4792:                if ( i != rank ) {
                   4793:                        t = mat[i]; mat[i] = mat[rank]; mat[rank] = t;
                   4794:                        s = sugar[i]; sugar[i] = sugar[rank]; sugar[rank] = s;
                   4795:                }
                   4796:                pivot = mat[rank];
                   4797:                s = sugar[rank];
                   4798:                inv = _invsf(pivot[j]);
                   4799:                for ( k = j, pk = pivot+k; k < col; k++, pk++ )
                   4800:                        if ( *pk )
                   4801:                                *pk = _mulsf(*pk,inv);
                   4802:                for ( i = rank+1; i < row; i++ ) {
                   4803:                        t = mat[i];
                   4804:                        if ( a = t[j] ) {
                   4805:                                sugar[i] = MAX(sugar[i],s);
                   4806:                                red_by_vect_sf(md,t+j,pivot+j,_chsgnsf(a),col-j);
                   4807:                        }
                   4808:                }
                   4809:                rank++;
                   4810:        }
                   4811:        for ( j = col-1, l = rank-1; j >= 0; j-- )
                   4812:                if ( colstat[j] ) {
                   4813:                        pivot = mat[l];
                   4814:                        s = sugar[l];
                   4815:                        for ( i = 0; i < l; i++ ) {
                   4816:                                t = mat[i];
                   4817:                                if ( a = t[j] ) {
                   4818:                                        sugar[i] = MAX(sugar[i],s);
                   4819:                                        red_by_vect_sf(md,t+j,pivot+j,_chsgnsf(a),col-j);
                   4820:                                }
                   4821:                        }
                   4822:                        l--;
                   4823:                }
                   4824:        return rank;
1.77      noro     4825: }
                   4826:
                   4827: int ndv_ishomo(NDV p)
                   4828: {
                   4829:        NMV m;
                   4830:        int len,h;
                   4831:
                   4832:        if ( !p ) return 1;
                   4833:        len = LEN(p);
                   4834:        m = BDY(p);
                   4835:        h = TD(DL(m));
                   4836:        NMV_ADV(m);
                   4837:        for ( len--; len; len--, NMV_ADV(m) )
                   4838:                if ( TD(DL(m)) != h ) return 0;
                   4839:        return 1;
                   4840: }
                   4841:
                   4842: void ndv_save(NDV p,int index)
                   4843: {
                   4844:        FILE *s;
                   4845:        char name[BUFSIZ];
                   4846:        short id;
                   4847:        int nv,sugar,len,n,i,td,e,j;
                   4848:        NMV m;
                   4849:        unsigned int *dl;
                   4850:
                   4851:        sprintf(name,"%s/%d",Demand,index);
                   4852:        s = fopen(name,"w");
                   4853:        savevl(s,0);
                   4854:        if ( !p ) {
                   4855:                saveobj(s,0);
                   4856:                return;
                   4857:        }
                   4858:        id = O_DP;
                   4859:        nv = NV(p);
                   4860:        sugar = SG(p);
                   4861:        len = LEN(p);
                   4862:        write_short(s,&id); write_int(s,&nv); write_int(s,&sugar);
                   4863:        write_int(s,&len);
                   4864:
                   4865:        for ( m = BDY(p), i = 0; i < len; i++, NMV_ADV(m) ) {
1.113     noro     4866:                saveobj(s,(Obj)CQ(m));
1.77      noro     4867:                dl = DL(m);
                   4868:                td = TD(dl);
                   4869:                write_int(s,&td);
                   4870:                for ( j = 0; j < nv; j++ ) {
                   4871:                        e = GET_EXP(dl,j);
                   4872:                        write_int(s,&e);
                   4873:                }
                   4874:        }
                   4875:        fclose(s);
                   4876: }
                   4877:
                   4878: NDV ndv_load(int index)
                   4879: {
                   4880:        FILE *s;
                   4881:        char name[BUFSIZ];
                   4882:        short id;
                   4883:        int nv,sugar,len,n,i,td,e,j;
                   4884:        NDV d;
                   4885:        NMV m0,m;
                   4886:        unsigned int *dl;
                   4887:        Obj obj;
                   4888:
                   4889:        sprintf(name,"%s/%d",Demand,index);
                   4890:        s = fopen(name,"r");
                   4891:        if ( !s ) return 0;
                   4892:
                   4893:        skipvl(s);
                   4894:        read_short(s,&id);
                   4895:        if ( !id ) return 0;
                   4896:        read_int(s,&nv);
                   4897:        read_int(s,&sugar);
                   4898:        read_int(s,&len);
                   4899:
                   4900:        m0 = m = MALLOC(len*nmv_adv);
                   4901:        for ( i = 0; i < len; i++, NMV_ADV(m) ) {
1.113     noro     4902:                loadobj(s,&obj); CQ(m) = (Q)obj;
1.77      noro     4903:                dl = DL(m);
                   4904:                ndl_zero(dl);
                   4905:                read_int(s,&td); TD(dl) = td;
                   4906:                for ( j = 0; j < nv; j++ ) {
                   4907:                        read_int(s,&e);
                   4908:                        PUT_EXP(dl,j,e);
                   4909:                }
                   4910:                if ( nd_blockmask ) ndl_weight_mask(dl);
                   4911:        }
                   4912:        fclose(s);
                   4913:        MKNDV(nv,m0,len,d);
                   4914:        SG(d) = sugar;
                   4915:        return d;
1.99      noro     4916: }
                   4917:
1.102     noro     4918: void nd_det(int mod,MAT f,P *rp)
1.99      noro     4919: {
                   4920:        VL fv,tv;
1.102     noro     4921:        int n,i,j,max,e,nvar,sgn,k0,l0,len0,len,k,l,a;
1.99      noro     4922:        pointer **m;
1.113     noro     4923:        Q mone;
1.102     noro     4924:        NDV **dm;
                   4925:        NDV *t,*mi,*mj;
                   4926:        NDV d,s,mij,mjj;
                   4927:        ND u;
                   4928:        NMV nmv;
1.114     noro     4929:        UINT *bound;
1.99      noro     4930:        PGeoBucket bucket;
                   4931:        struct order_spec *ord;
                   4932:
                   4933:        create_order_spec(0,0,&ord);
                   4934:        nd_init_ord(ord);
                   4935:        get_vars((Obj)f,&fv);
                   4936:        if ( f->row != f->col )
                   4937:                error("nd_det : non-square matrix");
                   4938:        n = f->row;
                   4939:        for ( nvar = 0, tv = fv; tv; tv = NEXT(tv), nvar++ );
                   4940:        m = f->body;
                   4941:        for ( i = 0, max = 0; i < n; i++ )
                   4942:                for ( j = 0; j < n; j++ )
                   4943:                        for ( tv = fv; tv; tv = NEXT(tv) ) {
                   4944:                                e = getdeg(tv->v,(P)m[i][j]);
                   4945:                                max = MAX(e,max);
                   4946:                        }
1.114     noro     4947:        nd_setup_parameters(nvar,max);
1.102     noro     4948:        dm = (NDV **)almat_pointer(n,n);
1.99      noro     4949:        for ( i = 0, max = 0; i < n; i++ )
1.102     noro     4950:                for ( j = 0; j < n; j++ ) {
                   4951:                        dm[i][j] = ptondv(CO,fv,m[i][j]);
                   4952:                        if ( mod ) ndv_mod(mod,dm[i][j]);
                   4953:                        if ( dm[i][j] && !LEN(dm[i][j]) ) dm[i][j] = 0;
                   4954:                }
                   4955:        d = ptondv(CO,fv,(P)ONE);
                   4956:        if ( mod ) ndv_mod(mod,d);
1.113     noro     4957:        chsgnq(ONE,&mone);
1.99      noro     4958:        for ( j = 0, sgn = 1; j < n; j++ ) {
1.104     noro     4959:                if ( DP_Print ) fprintf(stderr,"j=%d\n",j);
1.99      noro     4960:                for ( i = j; i < n && !dm[i][j]; i++ );
                   4961:                if ( i == n ) {
                   4962:                        *rp = 0;
                   4963:                        return;
                   4964:                }
1.102     noro     4965:                k0 = i; l0 = j; len0 = LEN(dm[k0][l0]);
1.99      noro     4966:                for ( k = j; k < n; k++ )
                   4967:                        for ( l = j; l < n; l++ )
1.102     noro     4968:                                if ( dm[k][l] && LEN(dm[k][l]) < len0 ) {
                   4969:                                        k0 = k; l0 = l; len0 = LEN(dm[k][l]);
1.99      noro     4970:                                }
                   4971:                if ( k0 != j ) {
                   4972:                        t = dm[j]; dm[j] = dm[k0]; dm[k0] = t;
                   4973:                        sgn = -sgn;
                   4974:                }
                   4975:                if ( l0 != j ) {
                   4976:                        for ( k = j; k < n; k++ ) {
                   4977:                                s = dm[k][j]; dm[k][j] = dm[k][l0]; dm[k][l0] = s;
                   4978:                        }
                   4979:                        sgn = -sgn;
                   4980:                }
1.114     noro     4981:                bound = nd_det_compute_bound(dm,n,j);
                   4982:                if ( ndl_check_bound(bound,bound) )
                   4983:                        nd_det_reconstruct(dm,n,j,d);
                   4984:
1.99      noro     4985:                for ( i = j+1, mj = dm[j], mjj = mj[j]; i < n; i++ ) {
1.114     noro     4986: /*                     if ( DP_Print ) fprintf(stderr,"        i=%d\n          ",i); */
1.99      noro     4987:                        mi = dm[i]; mij = mi[j];
1.102     noro     4988:                        if ( mod )
                   4989:                                ndv_mul_c(mod,mij,mod-1);
                   4990:                        else
1.113     noro     4991:                                ndv_mul_c_q(mij,mone);
1.99      noro     4992:                        for ( k = j+1; k < n; k++ ) {
1.114     noro     4993: /*                             if ( DP_Print ) fprintf(stderr,"k=%d ",k); */
1.99      noro     4994:                                bucket = create_pbucket();
1.104     noro     4995:                                if ( mi[k] ) {
1.102     noro     4996:                                        nmv = BDY(mjj); len = LEN(mjj);
                   4997:                                        for ( a = 0; a < len; a++, NMV_ADV(nmv) ) {
                   4998:                                                u = ndv_mul_nmv_trunc(mod,nmv,mi[k],DL(BDY(d)));
                   4999:                                                add_pbucket(mod,bucket,u);
1.99      noro     5000:                                        }
1.104     noro     5001:                                }
1.99      noro     5002:                                if ( mj[k] && mij ) {
1.102     noro     5003:                                        nmv = BDY(mij); len = LEN(mij);
                   5004:                                        for ( a = 0; a < len; a++, NMV_ADV(nmv) ) {
                   5005:                                                u = ndv_mul_nmv_trunc(mod,nmv,mj[k],DL(BDY(d)));
                   5006:                                                add_pbucket(mod,bucket,u);
1.99      noro     5007:                                        }
                   5008:                                }
1.104     noro     5009:                                u = nd_quo(mod,bucket,d);
1.102     noro     5010:                                mi[k] = ndtondv(mod,u);
1.99      noro     5011:                        }
1.114     noro     5012: /*                     if ( DP_Print ) fprintf(stderr,"\n",k); */
1.99      noro     5013:                }
                   5014:                d = mjj;
                   5015:        }
1.100     noro     5016:        if ( sgn < 0 )
1.102     noro     5017:                if ( mod )
                   5018:                        ndv_mul_c(mod,d,mod-1);
                   5019:                else
1.113     noro     5020:                        ndv_mul_c_q(d,mone);
1.102     noro     5021:        *rp = ndvtop(mod,CO,fv,d);
1.99      noro     5022: }
                   5023:
1.102     noro     5024: ND ndv_mul_nmv_trunc(int mod,NMV m0,NDV p,UINT *d)
1.99      noro     5025: {
                   5026:        NM mr,mr0;
1.102     noro     5027:        NM tnm;
                   5028:        NMV m;
1.99      noro     5029:        UINT *d0,*dt,*dm;
                   5030:        int c,n,td,i,c1,c2,len;
1.113     noro     5031:        Q q;
1.99      noro     5032:        ND r;
                   5033:
                   5034:        if ( !p ) return 0;
                   5035:        else {
1.102     noro     5036:                n = NV(p); m = BDY(p); len = LEN(p);
1.99      noro     5037:                d0 = DL(m0);
1.102     noro     5038:                td = TD(d);
1.99      noro     5039:                mr0 = 0;
                   5040:                NEWNM(tnm);
1.102     noro     5041:                if ( mod ) {
                   5042:                        c = CM(m0);
                   5043:                        for ( i = 0; i < len; i++, NMV_ADV(m) ) {
                   5044:                                ndl_add(DL(m),d0,DL(tnm));
                   5045:                                if ( ndl_reducible(DL(tnm),d) ) {
                   5046:                                        NEXTNM(mr0,mr);
                   5047:                                        c1 = CM(m); DMAR(c1,c,0,mod,c2); CM(mr) = c2;
1.104     noro     5048:                                        ndl_copy(DL(tnm),DL(mr));
1.102     noro     5049:                                }
                   5050:                        }
                   5051:                } else {
1.113     noro     5052:                        q = CQ(m0);
1.102     noro     5053:                        for ( i = 0; i < len; i++, NMV_ADV(m) ) {
                   5054:                                ndl_add(DL(m),d0,DL(tnm));
                   5055:                                if ( ndl_reducible(DL(tnm),d) ) {
                   5056:                                        NEXTNM(mr0,mr);
1.113     noro     5057:                                        mulq(CQ(m),q,&CQ(mr));
1.104     noro     5058:                                        ndl_copy(DL(tnm),DL(mr));
1.102     noro     5059:                                }
1.99      noro     5060:                        }
                   5061:                }
                   5062:                if ( !mr0 )
                   5063:                        return 0;
                   5064:                else {
                   5065:                        NEXT(mr) = 0;
1.105     noro     5066:                        for ( len = 0, mr = mr0; mr; mr = NEXT(mr), len++ );
1.99      noro     5067:                        MKND(NV(p),mr0,len,r);
                   5068:                        SG(r) = SG(p) + TD(d0);
                   5069:                        return r;
                   5070:                }
                   5071:        }
1.114     noro     5072: }
                   5073:
                   5074: void nd_det_reconstruct(NDV **dm,int n,int j,NDV d)
                   5075: {
                   5076:        int i,obpe,oadv,h,k,l;
                   5077:        static NM prev_nm_free_list;
                   5078:        EPOS oepos;
                   5079:
                   5080:        obpe = nd_bpe;
                   5081:        oadv = nmv_adv;
                   5082:        oepos = nd_epos;
                   5083:        if ( obpe < 2 ) nd_bpe = 2;
                   5084:        else if ( obpe < 3 ) nd_bpe = 3;
                   5085:        else if ( obpe < 4 ) nd_bpe = 4;
                   5086:        else if ( obpe < 5 ) nd_bpe = 5;
                   5087:        else if ( obpe < 6 ) nd_bpe = 6;
                   5088:        else if ( obpe < 8 ) nd_bpe = 8;
                   5089:        else if ( obpe < 10 ) nd_bpe = 10;
                   5090:        else if ( obpe < 16 ) nd_bpe = 16;
                   5091:        else if ( obpe < 32 ) nd_bpe = 32;
                   5092:        else error("nd_det_reconstruct : exponent too large");
                   5093:
                   5094:        nd_setup_parameters(nd_nvar,0);
                   5095:        prev_nm_free_list = _nm_free_list;
                   5096:        _nm_free_list = 0;
                   5097:        for ( k = j; k < n; k++ )
                   5098:                for (l = j; l < n; l++ )
                   5099:                        ndv_realloc(dm[k][l],obpe,oadv,oepos);
                   5100:        ndv_realloc(d,obpe,oadv,oepos);
                   5101:        prev_nm_free_list = 0;
                   5102: #if 0
                   5103:        GC_gcollect();
                   5104: #endif
                   5105: }
                   5106:
                   5107: UINT *nd_det_compute_bound(NDV **dm,int n,int j)
                   5108: {
                   5109:        UINT *d0,*d1,*d,*t,*r;
                   5110:        int k,l;
                   5111:
                   5112:        d0 = (UINT *)ALLOCA(nd_wpd*sizeof(UINT));
                   5113:        d1 = (UINT *)ALLOCA(nd_wpd*sizeof(UINT));
                   5114:        for ( k = 0; k < nd_wpd; k++ ) d0[k] = 0;
                   5115:        for ( k = j; k < n; k++ )
                   5116:                for ( l = j; l < n; l++ )
                   5117:                        if ( dm[k][l] ) {
                   5118:                                d = ndv_compute_bound(dm[k][l]);
                   5119:                                ndl_lcm(d,d0,d1);
                   5120:                                t = d1; d1 = d0; d0 = t;
                   5121:                        }
                   5122:        r = (UINT *)ALLOCA(nd_wpd*sizeof(UINT));
                   5123:        for ( k = 0; k < nd_wpd; k++ ) r[k] = d0[k];
                   5124:        return r;
1.117     noro     5125: }
                   5126:
                   5127: DL nd_separate_d(UINT *d,UINT *trans)
                   5128: {
                   5129:        int n,ntrans,td,i,e;
                   5130:        DL a;
                   5131:
                   5132:        n = nd_nvar; ntrans = n-nd_nalg;
                   5133:        ndl_zero(trans);
                   5134:        td = 0;
                   5135:        for ( i = 0; i < ntrans; i++ ) {
                   5136:                e = GET_EXP(d,i);
                   5137:                PUT_EXP(trans,i,e);
                   5138:                td += MUL_WEIGHT(e,i);
                   5139:        }
                   5140:        TD(trans) = td;
                   5141:        if ( nd_blockmask) ndl_weight_mask(trans);
                   5142:        NEWDL(a,nd_nalg);
                   5143:        td = 0;
                   5144:        for ( ; i < n; i++ ) {
                   5145:                e = GET_EXP(d,i);
                   5146:                a->d[i-ntrans] = e;
                   5147:                td += e;
                   5148:        }
                   5149:        a->td = td;
                   5150:        return a;
                   5151: }
                   5152:
1.118   ! noro     5153: NumberField get_numberfield();
        !          5154:
        !          5155: int nd_monic(int mod,ND *p)
1.117     noro     5156: {
                   5157:        UINT *trans,*t;
                   5158:        DL alg;
                   5159:        MP mp0,mp;
1.118   ! noro     5160:        NM m,m0,m1,ma0,ma,mb,mr0,mr;
        !          5161:        ND r;
1.117     noro     5162:        DL dl;
                   5163:        DP nm;
                   5164:        NDV ndv;
1.118   ! noro     5165:        DAlg inv,cd;
1.117     noro     5166:        ND s,c;
1.118   ! noro     5167:        Q l,mul;
        !          5168:        N ln;
        !          5169:        int n,ntrans,i,e,td,is_lc,len;
        !          5170:        NumberField nf;
        !          5171:        struct oEGT eg0,eg1;
        !          5172:
        !          5173:        if ( !(nf = get_numberfield()) )
        !          5174:                error("nd_monic : current_numberfield is not set");
1.117     noro     5175:
                   5176:        n = nd_nvar; ntrans = n-nd_nalg;
1.118   ! noro     5177:        /* Q coef -> DAlg coef */
        !          5178:        NEWNM(ma0); ma = ma0;
        !          5179:        m = BDY(*p);
        !          5180:        is_lc = 1;
        !          5181:        while ( 1 ) {
        !          5182:                NEWMP(mp0); mp = mp0;
        !          5183:                mp->c = (P)CQ(m);
        !          5184:                mp->dl = nd_separate_d(DL(m),DL(ma));
        !          5185:                NEWNM(mb);
        !          5186:                for ( m = NEXT(m); m; m = NEXT(m) ) {
        !          5187:                        alg = nd_separate_d(DL(m),DL(mb));
        !          5188:                        if ( !ndl_equal(DL(ma),DL(mb)) )
        !          5189:                                break;
        !          5190:                        NEXTMP(mp0,mp); mp->c = (P)CQ(m); mp->dl = alg;
        !          5191:                }
        !          5192:                NEXT(mp) = 0;
        !          5193:                MKDP(nd_nalg,mp0,nm);
        !          5194:                MKDAlg(nm,ONE,cd);
        !          5195:                if ( is_lc == 1 ) {
        !          5196:                        /* if the lc is a rational number, we have nothing to do */
        !          5197:                        if ( !mp0->dl->td )
        !          5198:                                return 1;
        !          5199:
        !          5200:                        get_eg(&eg0);
        !          5201:                        invdalg(cd,&inv);
        !          5202:                        get_eg(&eg1); add_eg(&eg_invdalg,&eg0,&eg1);
        !          5203:                        /* check the validity of inv */
        !          5204:                        if ( mod && !rem(NM(inv->dn),mod) )
        !          5205:                                return 0;
        !          5206:                        CA(ma) = nf->one;
        !          5207:                        is_lc = 0;
        !          5208:                        ln = ONEN;
        !          5209:                } else {
        !          5210:                        muldalg(cd,inv,&CA(ma));
        !          5211:                        lcmn(ln,NM(CA(ma)->dn),&ln);
        !          5212:                }
        !          5213:                if ( m ) {
        !          5214:                        NEXT(ma) = mb; ma = mb;
        !          5215:                } else {
        !          5216:                        NEXT(ma) = 0;
        !          5217:                        break;
        !          5218:                }
        !          5219:        }
        !          5220:        /* l = lcm(denoms) */
        !          5221:        NTOQ(ln,1,l);
        !          5222:        for ( mr0 = 0, m = ma0; m; m = NEXT(m) ) {
        !          5223:                divq(l,CA(m)->dn,&mul);
        !          5224:                for ( mp = BDY(CA(m)->nm); mp; mp = NEXT(mp) ) {
        !          5225:                        NEXTNM(mr0,mr);
        !          5226:                        mulq((Q)mp->c,mul,&CQ(mr));
        !          5227:                        dl = mp->dl;
        !          5228:                        td = TD(DL(m));
        !          5229:                        ndl_copy(DL(m),DL(mr));
        !          5230:                        for ( i = ntrans; i < n; i++ ) {
        !          5231:                                e = dl->d[i-ntrans];
        !          5232:                                PUT_EXP(DL(mr),i,e);
        !          5233:                                td += MUL_WEIGHT(e,i);
        !          5234:                        }
        !          5235:                        TD(DL(mr)) = td;
        !          5236:                        if ( nd_blockmask) ndl_weight_mask(DL(mr));
        !          5237:                }
1.117     noro     5238:        }
1.118   ! noro     5239:        NEXT(mr) = 0;
        !          5240:        for ( len = 0, mr = mr0; mr; mr = NEXT(mr), len++ );
        !          5241:        MKND(NV(*p),mr0,len,r);
        !          5242:        /* XXX */
        !          5243:        SG(r) = SG(*p);
        !          5244:        nd_free(*p);
        !          5245:        *p = r;
        !          5246:        return 1;
        !          5247: }
        !          5248:
        !          5249: void nd_set_nalg(int nalg)
        !          5250: {
        !          5251:        nd_nalg = nalg;
1.59      noro     5252: }

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