version 1.1, 2003/07/23 01:22:51 |
version 1.6, 2003/07/25 04:19:54 |
|
|
|
/* $OpenXM: OpenXM_contrib2/asir2000/engine/nd.c,v 1.5 2003/07/24 03:45:41 noro Exp $ */ |
|
|
#include "ca.h" |
#include "ca.h" |
#include "inline.h" |
#include "inline.h" |
|
|
|
#define USE_NDV 1 |
|
|
#if defined(__GNUC__) |
#if defined(__GNUC__) |
#define INLINE inline |
#define INLINE inline |
#elif defined(VISUAL) |
#elif defined(VISUAL) |
Line 22 typedef struct oND { |
|
Line 26 typedef struct oND { |
|
int sugar; |
int sugar; |
} *ND; |
} *ND; |
|
|
|
typedef struct oNDV { |
|
struct oNMV *body; |
|
int nv; |
|
int sugar; |
|
int len; |
|
} *NDV; |
|
|
typedef struct oNM { |
typedef struct oNM { |
struct oNM *next; |
struct oNM *next; |
int td; |
int td; |
Line 29 typedef struct oNM { |
|
Line 40 typedef struct oNM { |
|
unsigned int dl[1]; |
unsigned int dl[1]; |
} *NM; |
} *NM; |
|
|
|
typedef struct oNMV { |
|
int td; |
|
int c; |
|
unsigned int dl[1]; |
|
} *NMV; |
|
|
typedef struct oND_pairs { |
typedef struct oND_pairs { |
struct oND_pairs *next; |
struct oND_pairs *next; |
int i1,i2; |
int i1,i2; |
Line 37 typedef struct oND_pairs { |
|
Line 54 typedef struct oND_pairs { |
|
} *ND_pairs; |
} *ND_pairs; |
|
|
static ND *nd_ps; |
static ND *nd_ps; |
|
static NDV *nd_psv; |
static unsigned int **nd_bound; |
static unsigned int **nd_bound; |
int nd_mod,nd_nvar; |
int nd_mod,nd_nvar; |
int is_rlex; |
int is_rlex; |
|
|
extern int Top,Reverse; |
extern int Top,Reverse; |
int nd_psn,nd_pslen; |
int nd_psn,nd_pslen; |
int nd_found,nd_create,nd_notfirst; |
int nd_found,nd_create,nd_notfirst; |
|
int *nd_psl; |
|
|
void GC_gcollect(); |
void GC_gcollect(); |
NODE append_one(NODE,int); |
NODE append_one(NODE,int); |
Line 94 void _ND_alloc(); |
|
Line 113 void _ND_alloc(); |
|
int ndl_td(unsigned int *d); |
int ndl_td(unsigned int *d); |
ND nd_add(ND p1,ND p2); |
ND nd_add(ND p1,ND p2); |
ND nd_mul_nm(ND p,NM m0); |
ND nd_mul_nm(ND p,NM m0); |
|
ND nd_mul_ind_nm(int index,NM m0); |
ND nd_mul_term(ND p,int td,unsigned int *d); |
ND nd_mul_term(ND p,int td,unsigned int *d); |
int nd_sp(ND_pairs p,ND *nf); |
int nd_sp(ND_pairs p,ND *nf); |
int nd_find_reducer(ND g,ND *red); |
int nd_find_reducer(ND g); |
int nd_nf(ND g,int full,ND *nf); |
int nd_nf(ND g,int full,ND *nf); |
ND nd_reduce(ND p1,ND p2); |
ND nd_reduce(ND p1,ND p2); |
ND nd_reduce_special(ND p1,ND p2); |
ND nd_reduce_special(ND p1,ND p2); |
Line 110 void nd_mul_c(ND p,int mul); |
|
Line 130 void nd_mul_c(ND p,int mul); |
|
void nd_free_redlist(); |
void nd_free_redlist(); |
void nd_append_red(unsigned int *d,int td,int i); |
void nd_append_red(unsigned int *d,int td,int i); |
unsigned int *nd_compute_bound(ND p); |
unsigned int *nd_compute_bound(ND p); |
|
unsigned int *dp_compute_bound(DP p); |
ND_pairs nd_reconstruct(ND_pairs); |
ND_pairs nd_reconstruct(ND_pairs); |
void nd_setup_parameters(); |
void nd_setup_parameters(); |
ND nd_dup(ND p,int obpe); |
ND nd_dup(ND p,int obpe); |
|
ND nd_copy(ND p); |
void ndl_dup(int obpe,unsigned int *d,unsigned int *r); |
void ndl_dup(int obpe,unsigned int *d,unsigned int *r); |
|
|
|
#if USE_NDV |
|
#define NMV_ADV(m) (m = (NMV)(((char *)m)+nmv_adv)) |
|
#define NEWNDV(d) ((d)=(NDV)MALLOC(sizeof(struct oNDV))) |
|
#define MKNDV(n,m,l,d) NEWNDV(d); (d)->nv=(n); BDY(d)=(m); (d)->len = l; |
|
|
|
int nmv_adv; |
|
int nmv_len; |
|
NDV ndv_red; |
|
|
|
ND ndv_add(ND p1,NDV p2); |
|
NDV ndtondv(ND p); |
|
void ndv_mul_nm(NDV pv,NM m,NDV r); |
|
#endif |
|
|
void nd_free_private_storage() |
void nd_free_private_storage() |
{ |
{ |
_nd_free_list = 0; |
_nd_free_list = 0; |
_nm_free_list = 0; |
_nm_free_list = 0; |
|
_ndp_free_list = 0; |
nd_red = 0; |
nd_red = 0; |
GC_gcollect(); |
GC_gcollect(); |
} |
} |
Line 336 INLINE int ndl_equal(unsigned int *d1,unsigned int *d2 |
|
Line 373 INLINE int ndl_equal(unsigned int *d1,unsigned int *d2 |
|
return 1; |
return 1; |
} |
} |
|
|
|
INLINE void ndl_copy(unsigned int *d1,unsigned int *d2) |
|
{ |
|
int i; |
|
|
|
switch ( nd_wpd ) { |
|
case 1: |
|
d2[0] = d1[0]; |
|
break; |
|
case 2: |
|
d2[0] = d1[0]; |
|
d2[1] = d1[1]; |
|
break; |
|
default: |
|
for ( i = 0; i < nd_wpd; i++ ) |
|
d2[i] = d1[i]; |
|
break; |
|
} |
|
} |
|
|
INLINE void ndl_add(unsigned int *d1,unsigned int *d2,unsigned int *d) |
INLINE void ndl_add(unsigned int *d1,unsigned int *d2,unsigned int *d) |
{ |
{ |
int i; |
int i; |
|
|
for ( i = 0; i < nd_wpd; i++ ) { |
switch ( nd_wpd ) { |
d[i] = d1[i]+d2[i]; |
case 1: |
|
d[0] = d1[0]+d2[0]; |
|
break; |
|
case 2: |
|
d[0] = d1[0]+d2[0]; |
|
d[1] = d1[1]+d2[1]; |
|
break; |
|
default: |
|
for ( i = 0; i < nd_wpd; i++ ) |
|
d[i] = d1[i]+d2[i]; |
|
break; |
} |
} |
} |
} |
|
|
|
INLINE void ndl_add2(unsigned int *d1,unsigned int *d2) |
|
{ |
|
int i; |
|
|
|
switch ( nd_wpd ) { |
|
case 1: |
|
d2[0] += d1[0]; |
|
break; |
|
case 2: |
|
d2[0] += d1[0]; |
|
d2[1] += d1[1]; |
|
break; |
|
default: |
|
for ( i = 0; i < nd_wpd; i++ ) |
|
d2[i] += d1[i]; |
|
break; |
|
} |
|
} |
|
|
void ndl_sub(unsigned int *d1,unsigned int *d2,unsigned int *d) |
void ndl_sub(unsigned int *d1,unsigned int *d2,unsigned int *d) |
{ |
{ |
int i; |
int i; |
Line 619 ND nd_reduce_special(ND p1,ND p2) |
|
Line 704 ND nd_reduce_special(ND p1,ND p2) |
|
} |
} |
} |
} |
|
|
INLINE int ndl_check_bound(unsigned int *d) |
int ndl_check_bound2(int index,unsigned int *d2) |
{ |
{ |
int i; |
unsigned int u2; |
|
unsigned int *d1; |
|
int i,j,ind,k; |
|
|
for ( i = 0; i < nd_wpd; i++ ) |
d1 = nd_bound[index]; |
if ( d[i] & nd_mask1 ) |
ind = 0; |
return 1; |
switch ( nd_bpe ) { |
return 0; |
case 4: |
|
for ( i = 0; i < nd_wpd; i++ ) { |
|
u2 = d2[i]; |
|
if ( d1[ind++]+((u2>>28)&0xf) >= 0x10 ) return 1; |
|
if ( d1[ind++]+((u2>>24)&0xf) >= 0x10 ) return 1; |
|
if ( d1[ind++]+((u2>>20)&0xf) >= 0x10 ) return 1; |
|
if ( d1[ind++]+((u2>>16)&0xf) >= 0x10 ) return 1; |
|
if ( d1[ind++]+((u2>>12)&0xf) >= 0x10 ) return 1; |
|
if ( d1[ind++]+((u2>>8)&0xf) >= 0x10 ) return 1; |
|
if ( d1[ind++]+((u2>>4)&0xf) >= 0x10 ) return 1; |
|
if ( d1[ind++]+(u2&0xf) >= 0x10 ) return 1; |
|
} |
|
return 0; |
|
break; |
|
case 6: |
|
for ( i = 0; i < nd_wpd; i++ ) { |
|
u2 = d2[i]; |
|
if ( d1[ind++]+((u2>>24)&0x3f) >= 0x40 ) return 1; |
|
if ( d1[ind++]+((u2>>18)&0x3f) >= 0x40 ) return 1; |
|
if ( d1[ind++]+((u2>>12)&0x3f) >= 0x40 ) return 1; |
|
if ( d1[ind++]+((u2>>6)&0x3f) >= 0x40 ) return 1; |
|
if ( d1[ind++]+(u2&0x3f) >= 0x40 ) return 1; |
|
} |
|
return 0; |
|
break; |
|
case 8: |
|
for ( i = 0; i < nd_wpd; i++ ) { |
|
u2 = d2[i]; |
|
if ( d1[ind++]+((u2>>24)&0xff) >= 0x100 ) return 1; |
|
if ( d1[ind++]+((u2>>16)&0xff) >= 0x100 ) return 1; |
|
if ( d1[ind++]+((u2>>8)&0xff) >= 0x100 ) return 1; |
|
if ( d1[ind++]+(u2&0xff) >= 0x100 ) return 1; |
|
} |
|
return 0; |
|
break; |
|
case 16: |
|
for ( i = 0; i < nd_wpd; i++ ) { |
|
u2 = d2[i]; |
|
if ( d1[ind++]+((u2>>16)&0xffff) > 0x10000 ) return 1; |
|
if ( d1[ind++]+(u2&0xffff) > 0x10000 ) return 1; |
|
} |
|
return 0; |
|
break; |
|
case 32: |
|
for ( i = 0; i < nd_wpd; i++ ) |
|
if ( d1[i]+d2[i]<d1[i] ) return 1; |
|
return 0; |
|
break; |
|
default: |
|
for ( i = 0; i < nd_wpd; i++ ) { |
|
u2 = d2[i]; |
|
k = (nd_epw-1)*nd_bpe; |
|
for ( j = 0; j < nd_epw; j++, k -= nd_bpe ) |
|
if ( d1[ind++]+((u2>>k)&nd_mask0) > nd_mask0 ) return 1; |
|
} |
|
return 0; |
|
break; |
|
} |
} |
} |
|
|
int nd_sp(ND_pairs p,ND *rp) |
int nd_sp(ND_pairs p,ND *rp) |
{ |
{ |
NM m; |
NM m; |
ND p1,p2,t1,t2; |
ND p1,p2,t1,t2; |
unsigned int *lcm,*check; |
unsigned int *lcm; |
int td; |
int td; |
|
|
check = (unsigned int *)ALLOCA(nd_wpd*sizeof(unsigned int)); |
|
p1 = nd_ps[p->i1]; |
p1 = nd_ps[p->i1]; |
p2 = nd_ps[p->i2]; |
p2 = nd_ps[p->i2]; |
lcm = p->lcm; |
lcm = p->lcm; |
td = p->td; |
td = p->td; |
NEWNM(m); |
NEWNM(m); |
C(m) = HC(p2); m->td = td-HTD(p1); ndl_sub(lcm,HDL(p1),m->dl); NEXT(m) = 0; |
C(m) = HC(p2); m->td = td-HTD(p1); ndl_sub(lcm,HDL(p1),m->dl); NEXT(m) = 0; |
ndl_add(nd_bound[p->i1],m->dl,check); |
if ( ndl_check_bound2(p->i1,m->dl) ) |
if ( ndl_check_bound(check) ) |
|
return 0; |
return 0; |
t1 = nd_mul_nm(p1,m); |
t1 = nd_mul_nm(p1,m); |
C(m) = nd_mod-HC(p1); m->td = td-HTD(p2); ndl_sub(lcm,HDL(p2),m->dl); |
C(m) = nd_mod-HC(p1); m->td = td-HTD(p2); ndl_sub(lcm,HDL(p2),m->dl); |
ndl_add(nd_bound[p->i2],m->dl,check); |
if ( ndl_check_bound2(p->i2,m->dl) ) { |
if ( ndl_check_bound(check) ) { |
|
nd_free(t1); |
nd_free(t1); |
return 0; |
return 0; |
} |
} |
Line 659 int nd_sp(ND_pairs p,ND *rp) |
|
Line 800 int nd_sp(ND_pairs p,ND *rp) |
|
return 1; |
return 1; |
} |
} |
|
|
int ndl_hash_value(int td,unsigned int *d) |
INLINE int ndl_hash_value(int td,unsigned int *d) |
{ |
{ |
int i; |
int i; |
int r; |
int r; |
Line 670 int ndl_hash_value(int td,unsigned int *d) |
|
Line 811 int ndl_hash_value(int td,unsigned int *d) |
|
return r; |
return r; |
} |
} |
|
|
int nd_find_reducer(ND g, ND *rp) |
INLINE int nd_find_reducer(ND g) |
{ |
{ |
NM m; |
NM m; |
ND r,p; |
ND p; |
int i,c1,c2,c; |
int d,k,i; |
int d,k,append,index; |
|
unsigned int *check; |
|
NM t; |
|
|
|
d = ndl_hash_value(HTD(g),HDL(g)); |
d = ndl_hash_value(HTD(g),HDL(g)); |
for ( m = nd_red[d], k = 0; m; m = NEXT(m), k++ ) { |
for ( m = nd_red[d], k = 0; m; m = NEXT(m), k++ ) { |
if ( HTD(g) == m->td && ndl_equal(HDL(g),m->dl) ) { |
if ( HTD(g) == m->td && ndl_equal(HDL(g),m->dl) ) { |
if ( k > 0 ) nd_notfirst++; |
if ( k > 0 ) nd_notfirst++; |
index = m->c; |
|
append = 0; |
|
nd_found++; |
nd_found++; |
goto found; |
return m->c; |
} |
} |
} |
} |
|
|
for ( i = 0; i < nd_psn; i++ ) { |
for ( i = 0; i < nd_psn; i++ ) { |
p = nd_ps[i]; |
p = nd_ps[i]; |
if ( HTD(g) >= HTD(p) && ndl_reducible(HDL(g),HDL(p)) ) { |
if ( HTD(g) >= HTD(p) && ndl_reducible(HDL(g),HDL(p)) ) { |
index = i; |
|
append = 1; |
|
nd_create++; |
nd_create++; |
goto found; |
nd_append_red(HDL(g),HTD(g),i); |
|
return i; |
} |
} |
} |
} |
return 0; |
return -1; |
|
|
found: |
|
NEWNM(m); |
|
p = nd_ps[index]; |
|
ndl_sub(HDL(g),HDL(p),m->dl); |
|
|
|
check = (unsigned int *)ALLOCA(nd_wpd*sizeof(unsigned int)); |
|
ndl_add(nd_bound[index],m->dl,check); |
|
if ( ndl_check_bound(check) ) { |
|
FREENM(m); |
|
return -1; |
|
} |
|
|
|
c1 = invm(HC(p),nd_mod); |
|
c2 = nd_mod-HC(g); |
|
DMAR(c1,c2,0,nd_mod,c); |
|
C(m) = c; |
|
m->td = HTD(g)-HTD(p); |
|
NEXT(m) = 0; |
|
*rp = r = nd_mul_nm(p,m); |
|
FREENM(m); |
|
|
|
if ( append ) nd_append_red(HDL(g),HTD(g),i); |
|
return 1; |
|
} |
} |
|
|
ND nd_find_monic_reducer(ND g) |
ND nd_find_monic_reducer(ND g) |
Line 833 ND nd_mul_nm(ND p,NM m0) |
|
Line 944 ND nd_mul_nm(ND p,NM m0) |
|
} |
} |
} |
} |
|
|
|
ND nd_mul_ind_nm(int index,NM m0) |
|
{ |
|
register int c1,c2,c; |
|
register NM m,new,prev; |
|
NM mr0; |
|
unsigned int *d; |
|
int n,td,i,len,d0,d1; |
|
ND p,r; |
|
|
|
p = nd_ps[index]; |
|
len = nd_psl[index]; |
|
n = NV(p); m = BDY(p); |
|
d = m0->dl; td = m0->td; c = C(m0); |
|
|
|
NEWNM(mr0); |
|
c1 = C(m); DMAR(c1,c,0,nd_mod,c2); C(mr0) = c2; |
|
mr0->td = m->td+td; ndl_add(m->dl,d,mr0->dl); |
|
prev = mr0; m = NEXT(m); |
|
len--; |
|
|
|
switch ( nd_wpd ) { |
|
case 1: |
|
d0 = d[0]; |
|
while ( len-- ) { |
|
c1 = C(m); DMAR(c1,c,0,nd_mod,c2); |
|
NEWNM(new); C(new) = c2; |
|
new->td = m->td+td; new->dl[0] = m->dl[0]+d0; |
|
m = NEXT(m); NEXT(prev) = new; prev = new; |
|
} |
|
break; |
|
case 2: |
|
d0 = d[0]; d1 = d[1]; |
|
while ( len-- ) { |
|
c1 = C(m); DMAR(c1,c,0,nd_mod,c2); |
|
NEWNM(new); C(new) = c2; |
|
new->td = m->td+td; |
|
new->dl[0] = m->dl[0]+d0; |
|
new->dl[1] = m->dl[1]+d1; |
|
m = NEXT(m); NEXT(prev) = new; prev = new; |
|
} |
|
break; |
|
default: |
|
while ( len-- ) { |
|
c1 = C(m); DMAR(c1,c,0,nd_mod,c2); |
|
NEWNM(new); C(new) = c2; |
|
new->td = m->td+td; ndl_add(m->dl,d,new->dl); |
|
m = NEXT(m); NEXT(prev) = new; prev = new; |
|
} |
|
break; |
|
} |
|
|
|
NEXT(prev) = 0; |
|
MKND(NV(p),mr0,r); |
|
r->sugar = p->sugar + td; |
|
return r; |
|
} |
|
|
ND nd_mul_term(ND p,int td,unsigned int *d) |
ND nd_mul_term(ND p,int td,unsigned int *d) |
{ |
{ |
NM m,mr,mr0; |
NM m,mr,mr0; |
Line 860 ND nd_mul_term(ND p,int td,unsigned int *d) |
|
Line 1028 ND nd_mul_term(ND p,int td,unsigned int *d) |
|
/* ret=1 : success, ret=0 : overflow */ |
/* ret=1 : success, ret=0 : overflow */ |
int nd_nf(ND g,int full,ND *rp) |
int nd_nf(ND g,int full,ND *rp) |
{ |
{ |
ND p,d,red; |
ND p,d; |
NM m,mrd,tail; |
NM m,mrd,tail; |
int n,sugar,psugar,stat; |
struct oNM mul; |
|
int n,sugar,psugar,stat,index; |
|
int c,c1,c2; |
|
#if USE_NDV |
|
NDV red; |
|
#else |
|
ND red; |
|
#endif |
|
|
if ( !g ) { |
if ( !g ) { |
*rp = 0; |
*rp = 0; |
Line 871 int nd_nf(ND g,int full,ND *rp) |
|
Line 1046 int nd_nf(ND g,int full,ND *rp) |
|
sugar = g->sugar; |
sugar = g->sugar; |
n = NV(g); |
n = NV(g); |
for ( d = 0; g; ) { |
for ( d = 0; g; ) { |
/* stat=1 : found, stat=0 : not found, stat=-1 : overflow */ |
index = nd_find_reducer(g); |
stat = nd_find_reducer(g,&red); |
if ( index >= 0 ) { |
if ( stat == 1 ) { |
p = nd_ps[index]; |
#if 1 |
ndl_sub(HDL(g),HDL(p),mul.dl); |
|
mul.td = HTD(g)-HTD(p); |
|
if ( ndl_check_bound2(index,mul.dl) ) { |
|
nd_free(g); nd_free(d); |
|
return 0; |
|
} |
|
c1 = invm(HC(p),nd_mod); c2 = nd_mod-HC(g); |
|
DMAR(c1,c2,0,nd_mod,c); mul.c = c; |
|
#if USE_NDV |
|
ndv_mul_nm(nd_psv[index],&mul,ndv_red); |
|
g = ndv_add(g,ndv_red); |
|
sugar = MAX(sugar,ndv_red->sugar); |
|
#else |
|
red = nd_mul_ind_nm(index,&mul); |
g = nd_add(g,red); |
g = nd_add(g,red); |
sugar = MAX(sugar,red->sugar); |
sugar = MAX(sugar,red->sugar); |
#else |
|
psugar = (HTD(g)-HTD(red))+red->sugar; |
|
g = nd_reduce(g,red); |
|
sugar = MAX(sugar,psugar); |
|
#endif |
#endif |
} else if ( stat == -1 ) { |
|
nd_free(g); |
|
nd_free(d); |
|
return 0; |
|
} else if ( !full ) { |
} else if ( !full ) { |
*rp = g; |
*rp = g; |
return 1; |
return 1; |
Line 1361 ND_pairs nd_minp( ND_pairs d, ND_pairs *prest ) |
|
Line 1541 ND_pairs nd_minp( ND_pairs d, ND_pairs *prest ) |
|
lcm = m->lcm; |
lcm = m->lcm; |
s = m->sugar; |
s = m->sugar; |
td = m->td; |
td = m->td; |
len = nd_length(nd_ps[m->i1])+nd_length(nd_ps[m->i2]); |
len = nd_psl[m->i1]+nd_psl[m->i2]; |
for ( ml = 0, l = m; p; p = NEXT(l = p) ) { |
for ( ml = 0, l = m; p; p = NEXT(l = p) ) { |
if (p->sugar < s) |
if (p->sugar < s) |
goto find; |
goto find; |
Line 1373 ND_pairs nd_minp( ND_pairs d, ND_pairs *prest ) |
|
Line 1553 ND_pairs nd_minp( ND_pairs d, ND_pairs *prest ) |
|
if ( c < 0 ) |
if ( c < 0 ) |
goto find; |
goto find; |
else if ( c == 0 ) { |
else if ( c == 0 ) { |
tlen = nd_length(nd_ps[p->i1])+nd_length(nd_ps[p->i2]); |
tlen = nd_psl[p->i1]+nd_psl[p->i2]; |
if ( tlen < len ) |
if ( tlen < len ) |
goto find; |
goto find; |
} |
} |
|
|
|
|
int nd_newps(ND a) |
int nd_newps(ND a) |
{ |
{ |
|
int len; |
|
|
if ( nd_psn == nd_pslen ) { |
if ( nd_psn == nd_pslen ) { |
nd_pslen *= 2; |
nd_pslen *= 2; |
nd_ps = (ND *)REALLOC((char *)nd_ps,nd_pslen*sizeof(ND)); |
nd_ps = (ND *)REALLOC((char *)nd_ps,nd_pslen*sizeof(ND)); |
|
nd_psl = (int *)REALLOC((char *)nd_psl,nd_pslen*sizeof(int)); |
|
#if USE_NDV |
|
nd_psv = (NDV *)REALLOC((char *)nd_psv,nd_pslen*sizeof(NDV)); |
|
#endif |
nd_bound = (unsigned int **) |
nd_bound = (unsigned int **) |
REALLOC((char *)nd_bound,nd_pslen*sizeof(unsigned int *)); |
REALLOC((char *)nd_bound,nd_pslen*sizeof(unsigned int *)); |
} |
} |
nd_monic(a); |
nd_monic(a); |
nd_ps[nd_psn] = a; |
nd_ps[nd_psn] = a; |
|
nd_psl[nd_psn] = nd_length(a); |
nd_bound[nd_psn] = nd_compute_bound(a); |
nd_bound[nd_psn] = nd_compute_bound(a); |
|
#if USE_NDV |
|
nd_psv[nd_psn] = ndtondv(a); |
|
len = nd_psv[nd_psn]->len; |
|
if ( len > nmv_len ) { |
|
nmv_len = 2*len; |
|
BDY(ndv_red) = (NMV)REALLOC(BDY(ndv_red),nmv_len*nmv_adv); |
|
} |
|
#endif |
return nd_psn++; |
return nd_psn++; |
} |
} |
|
|
|
|
|
|
NODE nd_setup(NODE f) |
NODE nd_setup(NODE f) |
{ |
{ |
int i,td; |
int i,j,td,len,max; |
NODE s,s0,f0; |
NODE s,s0,f0; |
|
unsigned int *d; |
|
|
nd_found = 0; |
nd_found = 0; |
nd_notfirst = 0; |
nd_notfirst = 0; |
Line 1428 NODE nd_setup(NODE f) |
|
Line 1624 NODE nd_setup(NODE f) |
|
#endif |
#endif |
nd_psn = length(f); nd_pslen = 2*nd_psn; |
nd_psn = length(f); nd_pslen = 2*nd_psn; |
nd_ps = (ND *)MALLOC(nd_pslen*sizeof(ND)); |
nd_ps = (ND *)MALLOC(nd_pslen*sizeof(ND)); |
|
nd_psl = (int *)MALLOC(nd_pslen*sizeof(int)); |
|
#if USE_NDV |
|
nd_psv = (NDV *)MALLOC(nd_pslen*sizeof(NDV)); |
|
#endif |
nd_bound = (unsigned int **)MALLOC(nd_pslen*sizeof(unsigned int *)); |
nd_bound = (unsigned int **)MALLOC(nd_pslen*sizeof(unsigned int *)); |
nd_bpe = 4; |
for ( max = 0, i = 0, s = f; i < nd_psn; i++, s = NEXT(s) ) { |
|
nd_bound[i] = d = dp_compute_bound((DP)BDY(s)); |
|
for ( j = 0; j < nd_nvar; j++ ) |
|
max = MAX(d[j],max); |
|
} |
|
|
|
if ( max < 2 ) |
|
nd_bpe = 2; |
|
else if ( max < 4 ) |
|
nd_bpe = 4; |
|
else if ( max < 64 ) |
|
nd_bpe = 6; |
|
else if ( max < 256 ) |
|
nd_bpe = 8; |
|
else if ( max < 65536 ) |
|
nd_bpe = 16; |
|
else |
|
nd_bpe = 32; |
|
|
nd_setup_parameters(); |
nd_setup_parameters(); |
nd_free_private_storage(); |
nd_free_private_storage(); |
|
len = 0; |
for ( i = 0; i < nd_psn; i++, f = NEXT(f) ) { |
for ( i = 0; i < nd_psn; i++, f = NEXT(f) ) { |
nd_ps[i] = dptond((DP)BDY(f)); |
nd_ps[i] = dptond((DP)BDY(f)); |
nd_monic(nd_ps[i]); |
nd_monic(nd_ps[i]); |
nd_bound[i] = nd_compute_bound(nd_ps[i]); |
nd_psl[i] = nd_length(nd_ps[i]); |
|
#if USE_NDV |
|
nd_psv[i] = ndtondv(nd_ps[i]); |
|
len = MAX(len,nd_psv[i]->len); |
|
#endif |
} |
} |
nd_red = (NM *)MALLOC(REDTAB_LEN*sizeof(NM)); |
nd_red = (NM *)MALLOC(REDTAB_LEN*sizeof(NM)); |
|
#if USE_NDV |
|
nmv_len = 16*len; |
|
NEWNDV(ndv_red); |
|
BDY(ndv_red) = (NMV)MALLOC_ATOMIC(nmv_len*nmv_adv); |
|
#endif |
for ( s0 = 0, i = 0; i < nd_psn; i++ ) { |
for ( s0 = 0, i = 0; i < nd_psn; i++ ) { |
NEXTNODE(s0,s); BDY(s) = (pointer)i; |
NEXTNODE(s0,s); BDY(s) = (pointer)i; |
} |
} |
Line 1669 void nd_append_red(unsigned int *d,int td,int i) |
|
Line 1897 void nd_append_red(unsigned int *d,int td,int i) |
|
nd_red[h] = m; |
nd_red[h] = m; |
} |
} |
|
|
|
unsigned int *dp_compute_bound(DP p) |
|
{ |
|
unsigned int *d,*d1,*d2,*t; |
|
MP m; |
|
int i; |
|
|
|
if ( !p ) |
|
return 0; |
|
d1 = (unsigned int *)ALLOCA(nd_nvar*sizeof(unsigned int)); |
|
d2 = (unsigned int *)ALLOCA(nd_nvar*sizeof(unsigned int)); |
|
m = BDY(p); |
|
bcopy(m->dl->d,d1,nd_nvar*sizeof(unsigned int)); |
|
for ( m = NEXT(BDY(p)); m; m = NEXT(m) ) { |
|
d = m->dl->d; |
|
for ( i = 0; i < nd_nvar; i++ ) |
|
d2[i] = d[i] > d1[i] ? d[i] : d1[i]; |
|
t = d1; d1 = d2; d2 = t; |
|
} |
|
t = (unsigned int *)MALLOC_ATOMIC(nd_nvar*sizeof(unsigned int)); |
|
bcopy(d1,t,nd_nvar*sizeof(unsigned int)); |
|
return t; |
|
} |
|
|
unsigned int *nd_compute_bound(ND p) |
unsigned int *nd_compute_bound(ND p) |
{ |
{ |
unsigned int *d1,*d2,*t; |
unsigned int *d1,*d2,*t; |
|
int i; |
NM m; |
NM m; |
|
|
if ( !p ) |
if ( !p ) |
Line 1683 unsigned int *nd_compute_bound(ND p) |
|
Line 1935 unsigned int *nd_compute_bound(ND p) |
|
ndl_lcm(m->dl,d1,d2); |
ndl_lcm(m->dl,d1,d2); |
t = d1; d1 = d2; d2 = t; |
t = d1; d1 = d2; d2 = t; |
} |
} |
t = (unsigned int *)MALLOC_ATOMIC(nd_wpd*sizeof(unsigned int)); |
t = (unsigned int *)MALLOC_ATOMIC(nd_nvar*sizeof(unsigned int)); |
bcopy(d1,t,nd_wpd*sizeof(unsigned int)); |
bzero(t,nd_nvar*sizeof(unsigned int)); |
|
for ( i = 0; i < nd_nvar; i++ ) |
|
t[i] = (d1[i/nd_epw]>>((nd_epw-(i%nd_epw)-1)*nd_bpe))&nd_mask0; |
return t; |
return t; |
} |
} |
|
|
Line 1704 void nd_setup_parameters() { |
|
Line 1958 void nd_setup_parameters() { |
|
nd_mask[nd_epw-i-1] = (nd_mask0<<(i*nd_bpe)); |
nd_mask[nd_epw-i-1] = (nd_mask0<<(i*nd_bpe)); |
nd_mask1 |= (1<<(nd_bpe-1))<<(i*nd_bpe); |
nd_mask1 |= (1<<(nd_bpe-1))<<(i*nd_bpe); |
} |
} |
|
#if USE_NDV |
|
nmv_adv = sizeof(struct oNMV)+(nd_wpd-1)*sizeof(unsigned int); |
|
#endif |
} |
} |
|
|
ND_pairs nd_reconstruct(ND_pairs d) |
ND_pairs nd_reconstruct(ND_pairs d) |
{ |
{ |
int i,obpe; |
int i,obpe; |
NM prev_nm_free_list; |
NM prev_nm_free_list,mr0,mr,m; |
ND_pairs s0,s,t,prev_ndp_free_list; |
ND_pairs s0,s,t,prev_ndp_free_list; |
|
|
obpe = nd_bpe; |
obpe = nd_bpe; |
switch ( nd_bpe ) { |
if ( obpe < 4 ) |
case 4: nd_bpe = 6; break; |
nd_bpe = 4; |
case 6: nd_bpe = 8; break; |
else if ( obpe < 6 ) |
case 8: nd_bpe = 16; break; |
nd_bpe = 6; |
case 16: nd_bpe = 32; break; |
else if ( obpe < 8 ) |
} |
nd_bpe = 8; |
|
else if ( obpe < 16 ) |
|
nd_bpe = 16; |
|
else if ( obpe < 32 ) |
|
nd_bpe = 32; |
|
else |
|
error("nd_reconstruct : exponent too large"); |
|
|
nd_setup_parameters(); |
nd_setup_parameters(); |
prev_nm_free_list = _nm_free_list; |
prev_nm_free_list = _nm_free_list; |
prev_ndp_free_list = _ndp_free_list; |
prev_ndp_free_list = _ndp_free_list; |
Line 1726 ND_pairs nd_reconstruct(ND_pairs d) |
|
Line 1990 ND_pairs nd_reconstruct(ND_pairs d) |
|
_ndp_free_list = 0; |
_ndp_free_list = 0; |
for ( i = 0; i < nd_psn; i++ ) { |
for ( i = 0; i < nd_psn; i++ ) { |
nd_ps[i] = nd_dup(nd_ps[i],obpe); |
nd_ps[i] = nd_dup(nd_ps[i],obpe); |
nd_bound[i] = nd_compute_bound(nd_ps[i]); |
#if USE_NDV |
|
nd_psv[i] = ndtondv(nd_ps[i]); |
|
#endif |
} |
} |
s0 = 0; |
s0 = 0; |
for ( t = d; t; t = NEXT(t) ) { |
for ( t = d; t; t = NEXT(t) ) { |
Line 1737 ND_pairs nd_reconstruct(ND_pairs d) |
|
Line 2003 ND_pairs nd_reconstruct(ND_pairs d) |
|
s->sugar = t->sugar; |
s->sugar = t->sugar; |
ndl_dup(obpe,t->lcm,s->lcm); |
ndl_dup(obpe,t->lcm,s->lcm); |
} |
} |
|
for ( i = 0; i < REDTAB_LEN; i++ ) { |
|
for ( mr0 = 0, m = nd_red[i]; m; m = NEXT(m) ) { |
|
NEXTNM(mr0,mr); |
|
mr->c = m->c; |
|
mr->td = m->td; |
|
ndl_dup(obpe,m->dl,mr->dl); |
|
} |
|
if ( mr0 ) NEXT(mr) = 0; |
|
nd_red[i] = mr0; |
|
} |
if ( s0 ) NEXT(s) = 0; |
if ( s0 ) NEXT(s) = 0; |
prev_nm_free_list = 0; |
prev_nm_free_list = 0; |
prev_ndp_free_list = 0; |
prev_ndp_free_list = 0; |
|
#if USE_NDV |
|
BDY(ndv_red) = (NMV)REALLOC(BDY(ndv_red),nmv_len*nmv_adv); |
|
#endif |
GC_gcollect(); |
GC_gcollect(); |
return s0; |
return s0; |
} |
} |
Line 1788 ND nd_dup(ND p,int obpe) |
|
Line 2067 ND nd_dup(ND p,int obpe) |
|
return r; |
return r; |
} |
} |
} |
} |
|
|
|
ND nd_copy(ND p) |
|
{ |
|
NM m,mr,mr0; |
|
int c,n,s; |
|
ND r; |
|
|
|
if ( !p ) |
|
return 0; |
|
else { |
|
s = sizeof(struct oNM)+(nd_wpd-1)*sizeof(unsigned int); |
|
for ( mr0 = 0, m = BDY(p); m; m = NEXT(m) ) { |
|
NEXTNM(mr0,mr); |
|
C(mr) = C(m); |
|
mr->td = m->td; |
|
ndl_copy(m->dl,mr->dl); |
|
} |
|
NEXT(mr) = 0; |
|
MKND(NV(p),mr0,r); |
|
r->sugar = p->sugar; |
|
return r; |
|
} |
|
} |
|
|
|
#if USE_NDV |
|
void ndv_mul_nm(NDV p,NM m0,NDV r) |
|
{ |
|
NMV m,mr,mr0; |
|
unsigned int *d,*dt,*dm; |
|
int c,n,td,i,c1,c2,len; |
|
|
|
if ( !p ) |
|
/* XXX */ |
|
r->len = 0; |
|
else { |
|
n = NV(p); m = BDY(p); len = p->len; |
|
d = m0->dl; td = m0->td; c = C(m0); |
|
mr = BDY(r); |
|
for ( i = 0; i < len; i++, NMV_ADV(m), NMV_ADV(mr) ) { |
|
c1 = C(m); |
|
DMAR(c1,c,0,nd_mod,c2); |
|
C(mr) = c2; |
|
mr->td = m->td+td; |
|
ndl_add(m->dl,d,mr->dl); |
|
} |
|
NV(r) = NV(p); |
|
r->len = p->len; |
|
r->sugar = p->sugar + td; |
|
} |
|
} |
|
|
|
ND ndv_add(ND p1,NDV p2) |
|
{ |
|
int c,c1,c2,t,td,td2,mul,len,i; |
|
NM prev,head,cur,new; |
|
unsigned int *d; |
|
NMV m2; |
|
|
|
if ( !p1 ) |
|
return 0; |
|
else { |
|
prev = 0; head = cur = BDY(p1); |
|
NEWNM(new); |
|
len = p2->len; |
|
for ( m2 = BDY(p2), i = 0; i < len; ) { |
|
td2 = new->td = m2->td; |
|
if ( !cur ) { |
|
C(new) = C(m2); |
|
ndl_copy(m2->dl,new->dl); |
|
if ( !prev ) { |
|
prev = new; |
|
NEXT(prev) = 0; |
|
head = prev; |
|
} else { |
|
NEXT(prev) = new; |
|
NEXT(new) = 0; |
|
prev = new; |
|
} |
|
NMV_ADV(m2); i++; |
|
NEWNM(new); |
|
continue; |
|
} |
|
if ( cur->td > td2 ) |
|
c = 1; |
|
else if ( cur->td < td2 ) |
|
c = -1; |
|
else |
|
c = ndl_compare(cur->dl,m2->dl); |
|
switch ( c ) { |
|
case 0: |
|
t = C(m2)+C(cur)-nd_mod; |
|
if ( t < 0 ) |
|
t += nd_mod; |
|
if ( t ) |
|
C(cur) = t; |
|
else if ( !prev ) { |
|
head = NEXT(cur); |
|
FREENM(cur); |
|
cur = head; |
|
} else { |
|
NEXT(prev) = NEXT(cur); |
|
FREENM(cur); |
|
cur = NEXT(prev); |
|
} |
|
NMV_ADV(m2); i++; |
|
break; |
|
case 1: |
|
prev = cur; |
|
cur = NEXT(cur); |
|
break; |
|
case -1: |
|
ndl_copy(m2->dl,new->dl); |
|
if ( !prev ) { |
|
/* cur = head */ |
|
prev = new; |
|
C(prev) = C(m2); |
|
NEXT(prev) = head; |
|
head = prev; |
|
} else { |
|
C(new) = C(m2); |
|
NEXT(prev) = new; |
|
NEXT(new) = cur; |
|
prev = new; |
|
} |
|
NEWNM(new); |
|
NMV_ADV(m2); i++; |
|
break; |
|
} |
|
} |
|
FREENM(new); |
|
if ( head ) { |
|
BDY(p1) = head; |
|
p1->sugar = MAX(p1->sugar,p2->sugar+td); |
|
return p1; |
|
} else { |
|
FREEND(p1); |
|
return 0; |
|
} |
|
|
|
} |
|
} |
|
|
|
NDV ndtondv(ND p) |
|
{ |
|
NDV d; |
|
NMV m,m0; |
|
NM t; |
|
int i,len; |
|
|
|
if ( !p ) |
|
return 0; |
|
len = nd_length(p); |
|
m0 = m = (NMV)MALLOC_ATOMIC(len*nmv_adv); |
|
for ( t = BDY(p), i = 0; t; t = NEXT(t), i++, NMV_ADV(m) ) { |
|
m->td = t->td; |
|
bcopy(t->dl,m->dl,nd_wpd*sizeof(unsigned int)); |
|
m->c = t->c; |
|
} |
|
MKNDV(NV(p),m0,len,d); |
|
d->sugar = p->sugar; |
|
return d; |
|
} |
|
|
|
void ndv_print(NDV p) |
|
{ |
|
NMV m; |
|
int i,len; |
|
|
|
if ( !p ) |
|
printf("0\n"); |
|
else { |
|
len = p->len; |
|
for ( m = BDY(p), i = 0; i < len; i++, NMV_ADV(m) ) { |
|
printf("+%d*",m->c); |
|
ndl_print(m->dl); |
|
} |
|
printf("\n"); |
|
} |
|
} |
|
#endif |