diff --git a/sources/declare.h b/sources/declare.h index 802e171e..47e5315c 100644 --- a/sources/declare.h +++ b/sources/declare.h @@ -68,12 +68,18 @@ ParseNumber(x,s) if ( sgn ) x = -x; } /* (n) is necessary here, since the macro is sometimes passed dereferenced pointers for n */ -#define NCOPY(s,t,n) { while ( (n)-- > 0 ) { *s++ = *t++; } } -/*#define NCOPY(s,t,n) { memcpy(s,t,n*sizeof(WORD)); s+=n; t+=n; n = -1; }*/ +/*#define NCOPY(s,t,n) { while ( (n)-- > 0 ) { *s++ = *t++; } } #define NCOPYI(s,t,n) { while ( (n)-- > 0 ) { *s++ = *t++; } } #define NCOPYB(s,t,n) { while ( (n)-- > 0 ) { *s++ = *t++; } } #define NCOPYI32(s,t,n) { while ( (n)-- > 0 ) { *s++ = *t++; } } -#define WCOPY(s,t,n) { int nn=n; WORD *ss=(WORD *)s, *tt=(WORD *)t; while ( (nn)-- > 0 ) { *ss++ = *tt++; } } +#define WCOPY(s,t,n) { int nn=n; WORD *ss=(WORD *)s, *tt=(WORD *)t; while ( (nn)-- > 0 ) { *ss++ = *tt++; } }*/ + +/* Use memmove: we can't guarantee that s and t do not overlap in uses of the macros */ +#define NCOPY(s,t,n) { memmove(s,t,(n)*sizeof(*s)); s+=n; t+=n; n=0; } +#define NCOPYI(s,t,n) { NCOPY(s,t,n); } +#define NCOPYB(s,t,n) { NCOPY(s,t,n); } +#define NCOPYI32(s,t,n) { NCOPY(s,t,n); } +#define WCOPY(s,t,n) { int nn=n; WORD *ss=(WORD *)s, *tt=(WORD *)t; NCOPY(ss,tt,nn); } #define NeedNumber(x,s,err) { int sgn = 1; \ while ( *s == ' ' || *s == '\t' || *s == '-' || *s == '+' ) { \ diff --git a/sources/execute.c b/sources/execute.c index 2514370c..6279cb91 100644 --- a/sources/execute.c +++ b/sources/execute.c @@ -1601,7 +1601,9 @@ NextSymbol2:; if ( AR.BracketOn < 0 ) { s1 = t1; t1 = t2; t2 = s1; } - do { *t2++ = *t++; } while ( t < (WORD *)tStopa ); + /* Old code copied 1 element if copy == 0. This doesn't happen in the test suite. */ + LONG copy = tStopa - t; + NCOPY(t2, t, copy); t = AT.WorkPointer; i = WORDDIF(t1,term1); *t++ = 4 + i + WORDDIF(t2,term2); diff --git a/sources/if.c b/sources/if.c index 0e0f2485..3490da24 100644 --- a/sources/if.c +++ b/sources/if.c @@ -816,7 +816,7 @@ donemul: if ( i ) { ncoef2 = 1; coef2 = Spac2; coef2[0] = coef2[1] = 1; } } coef1 = Spac1; i = 2*ABS(ncoef2); - for ( j = 0; j < i; j++ ) coef1[j] = coef2[j]; + WCOPY(coef1, coef2, i); ncoef1 = ncoef2; SkipCond: ifp += ifp[1]; diff --git a/sources/proces.c b/sources/proces.c index 93ac5c80..45683d53 100644 --- a/sources/proces.c +++ b/sources/proces.c @@ -2174,7 +2174,8 @@ int InFunction(PHEAD WORD *term, WORD *termout) if ( AR.TePos ) { if ( ( term + AR.TePos ) == t ) { m = termout; - while ( from < t ) *m++ = *from++; + LONG copy = t - from; + NCOPY(m, from, copy); *m++ = DENOMINATOR; *m++ = t[1] + 4 + FUNHEAD + ARGHEAD; *m++ = DIRTYFLAG; @@ -2185,7 +2186,8 @@ int InFunction(PHEAD WORD *term, WORD *termout) *m++ = t[1] + 4; t[3] = -t[3]; v = t + t[1]; - while ( t < v ) *m++ = *t++; + copy = v - t; + NCOPY(m, t, copy); from[3] = -from[3]; *m++ = 1; *m++ = 1; @@ -2220,11 +2222,10 @@ int InFunction(PHEAD WORD *term, WORD *termout) AR.DeferFlag = 0; v = t + *t; t += ARGHEAD; /* First term */ - w = 0; /* to appease the compilers warning devices */ - while ( from < t ) { - if ( from == u ) w = m; - *m++ = *from++; - } + LONG copy = t - from; + const LONG size = t - u; + NCOPY(m, from, copy); + w = m - size; to = m; NewSort(BHEAD0); if ( *u == AR.PolyFun && AR.PolyFunType == 2 ) { @@ -2314,11 +2315,10 @@ int InFunction(PHEAD WORD *term, WORD *termout) && ( *u != AR.PolyFun ) ) { AN.ncmod = 0; } AR.DeferFlag = 0; v = t + 2; - w = 0; /* to appease the compilers warning devices */ - while ( from < t ) { - if ( from == u ) w = m; - *m++ = *from++; - } + LONG copy = t - from; + const LONG size = t - u; + NCOPY(m, from, copy); + w = m - size; to = m; switch ( d->type ) { case DOLINDEX: @@ -2481,11 +2481,10 @@ int InFunction(PHEAD WORD *term, WORD *termout) u points at the start of the function t points at the start of the argument */ - w = 0; - while ( from < t ) { - if ( from == u ) w = m; - *m++ = *from++; - } + LONG copy = t - from; + const LONG size = t - u; + NCOPY(m, from, copy); + w = m - size; if ( ( numterms & MAXPOSITIVE ) == numterms ) { *m++ = -SNUMBER; *m++ = numterms & MAXPOSITIVE; w[1] += 1; @@ -2505,7 +2504,8 @@ int InFunction(PHEAD WORD *term, WORD *termout) } from++; /* Skip our function */ r = term + *term; - while ( from < r ) *m++ = *from++; + copy = r - from; + NCOPY(m, from, copy); if ( (m-termout) > (LONG)(AM.MaxTer/sizeof(WORD)) ) { MLOCK(ErrorMessageLock); MesPrint("Output term too large (%d words) (MaxTermSize: %d words)", m-termout, AM.MaxTer/sizeof(WORD)); @@ -2548,11 +2548,11 @@ int InFunction(PHEAD WORD *term, WORD *termout) if ( ( AN.ncmod != 0 ) && ( ( AC.modmode & ALSOFUNARGS ) == 0 ) && ( *u != AR.PolyFun ) ) { AN.ncmod = 0; } - m = termout; w = 0; - while ( from < t ) { - if ( from == u ) w = m; - *m++ = *from++; - } + m = termout; + LONG copy = t - from; + const LONG size = t - u; + NCOPY(m, from, copy); + w = m - size; to = m; switch ( d->type ) { case DOLINDEX: @@ -2650,7 +2650,8 @@ wrongtype:; w[1] = w[1] - 2 + (m-to); from += 2; term += *term; - while ( from < term ) *m++ = *from++; + copy = term - from; + NCOPY(m, from, copy); if ( sign < 0 ) m[-1] = -m[-1]; if ( (m-termout) > (LONG)(AM.MaxTer/sizeof(WORD)) ) { MLOCK(ErrorMessageLock); @@ -3413,10 +3414,10 @@ SkipCount: level++; are involved in a match(). Hence we have to make a private copy here!!!! */ - WORD ic, jc, *ifcode, *jfcode; + WORD jc, *ifcode, *jfcode; jfcode = C->lhs[level]; jc = jfcode[1]; ifcode = AT.WorkPointer; AT.WorkPointer += jc; - for ( ic = 0; ic < jc; ic++ ) ifcode[ic] = jfcode[ic]; + WCOPY(ifcode, jfcode, jc); while ( !DoIfStatement(BHEAD ifcode,term) ) { level = C->lhs[level][2]; if ( C->lhs[level][0] != TYPEELIF ) break; @@ -5253,11 +5254,14 @@ int PrepPoly(PHEAD WORD *term,WORD par) */ m = term + *term; if ( poly + poly[1] < tstop ) { - for ( i = 0; i < poly[1]; i++ ) m[i] = poly[i]; + LONG copy = poly[1]; + WCOPY(m, poly, copy); t = poly; v = poly + poly[1]; - while ( v < tstop ) *t++ = *v++; + copy = tstop - v; + NCOPY(t, v, copy); poly = t; - for ( i = 0; i < m[1]; i++ ) t[i] = m[i]; + copy = m[1]; + WCOPY(t, m, copy); t += m[1]; } AT.PolyAct = WORDDIF(poly,term); diff --git a/sources/sort.c b/sources/sort.c index 1005e17c..c93e2aa1 100644 --- a/sources/sort.c +++ b/sources/sort.c @@ -4624,9 +4624,7 @@ int StoreTerm(PHEAD WORD *term) while ( ( t = *ss++ ) != 0 ) { j = *t; if ( j < 0 ) j = t[1] + 2; - while ( --j >= 0 ){ - *lfill++ = *t++; - } + NCOPY(lfill, t, j); } } *lfill++ = 0; @@ -4636,7 +4634,7 @@ int StoreTerm(PHEAD WORD *term) *(S->PoinFill) = S->sFill = S->sBuffer; } j = *term; - while ( --j >= 0 ) *S->sFill++ = *term++; + NCOPY(S->sFill, term, j); S->sTerms++; S->GenTerms++; S->TermsLeft++; diff --git a/sources/threads.c b/sources/threads.c index 9176e4c6..0a311a09 100644 --- a/sources/threads.c +++ b/sources/threads.c @@ -3543,9 +3543,9 @@ int PutToMaster(PHEAD WORD *term) fill = AT.SB.MasterFill[i]; /* Where we are filling */ top = AT.SB.MasterStop[i]; /* End of the block */ while ( j > 0 ) { - while ( j > 0 && fill < top ) { - *fill++ = *t++; j--; - } + LONG copy = MiN(top - fill, j); + j -= copy; + NCOPY(fill, t, copy); if ( j > 0 ) { /* We reached the end of the block.