Skip to content

Commit

Permalink
Some fixes for undefined behaviour (edited by TU)
Browse files Browse the repository at this point in the history
This is extracted from the commit 29854a5 to synchronize and make it
easy to merge the "diagrams" branch in future. Most of them are for
removing "left shift of negative value" (e.g., -100 << 4). Note that,
on x86-64, both "a <<= 1" and "a *= 2" are naturally optimized as
"a = a + a" (at least with gcc 7.2, clang 5.0.0 and icc 17).
  • Loading branch information
vermaseren authored and tueda committed Oct 26, 2017
1 parent 338da9f commit 364f6d5
Show file tree
Hide file tree
Showing 12 changed files with 58 additions and 58 deletions.
2 changes: 1 addition & 1 deletion sources/argument.c
Original file line number Diff line number Diff line change
Expand Up @@ -528,7 +528,7 @@ ScaledVariety:;
r4 = AddRHS(AT.ebufnum,1);
while ( (r4+j+12) > CC->Top ) r4 = DoubleCbuffer(AT.ebufnum,r4,3);
*r4++ = j+1;
i = (j-1)>>1;
i = (j-1)/2; /* was (j-1)*2 ????? 17-oct-2017 */
for ( k = 0; k < i; k++ ) *r4++ = r3[i+k];
for ( k = 0; k < i; k++ ) *r4++ = r3[k];
if ( ( type == TYPENORM3 ) || ( type == TYPENORM4 ) ) *r4++ = j*sign;
Expand Down
2 changes: 1 addition & 1 deletion sources/compcomm.c
Original file line number Diff line number Diff line change
Expand Up @@ -4229,7 +4229,7 @@ int CoIf(UBYTE *inp)
}
}
u[1] = WORDDIF(w,u);
u[2] = (u[1] - 3)>>1;
u[2] = (u[1] - 3)/2;
if ( level ) u[2] = -u[2];
gotexp = 1;
}
Expand Down
7 changes: 5 additions & 2 deletions sources/declare.h
Original file line number Diff line number Diff line change
Expand Up @@ -41,12 +41,15 @@
#define MiN(x,y) ((x) < (y) ? (x): (y))
#define ABS(x) ( (x) < 0 ? -(x): (x) )
#define SGN(x) ( (x) > 0 ? 1 : (x) < 0 ? -1 : 0 )
#define REDLENG(x) ((((x)<0)?((x)+1):((x)-1))>>1)
#define INCLENG(x) (((x)<0)?(((x)<<1)-1):(((x)<<1)+1))
#define REDLENG(x) ((((x)<0)?((x)+1):((x)-1))/2)
#define INCLENG(x) (((x)<0)?(((x)*2)-1):(((x)*2)+1))
#define GETCOEF(x,y) x += *x;y = x[-1];x -= ABS(y);y=REDLENG(y)
#define GETSTOP(x,y) y=x+(*x)-1;y -= ABS(*y)-1
#define StuffAdd(x,y) (((x)<0?-1:1)*(y)+((y)<0?-1:1)*(x))

#define EXCHN(t1,t2,n) { WORD a,i; for(i=0;i<n;i++){a=t1[i];t1[i]=t2[i];t2[i]=a;} }
#define EXCH(x,y) { WORD a = (x); (x) = (y); (y) = a; }

#define TOKENTOLINE(x,y) if ( AC.OutputSpaces == NOSPACEFORMAT ) { \
TokenToLine((UBYTE *)(y)); } else { TokenToLine((UBYTE *)(x)); }

Expand Down
4 changes: 2 additions & 2 deletions sources/minos.h
Original file line number Diff line number Diff line change
Expand Up @@ -140,8 +140,8 @@ typedef struct compile {
*/
#define TODISK 0
#define FROMDISK 1
#define MCLEANFLAG -2L
#define DIRTYFLAG 1
#define MDIRTYFLAG 1
#define MCLEANFLAG (~MDIRTYFLAG)
#define INANDOUT 0
#define INPUTONLY 1
#define OUTPUTONLY 2
Expand Down
13 changes: 5 additions & 8 deletions sources/normal.c
Original file line number Diff line number Diff line change
Expand Up @@ -714,7 +714,7 @@ NextSymbol:;
k = -t[1]-NMIN4SHIFT;
k = ExtraSymbol(k,1,nsym,ppsym,&ncoef);
nsym += k;
ppsym += (k << 1);
ppsym += (k * 2);
}
else if ( t[1] == 0 ) goto NormZero;
else {
Expand Down Expand Up @@ -1742,7 +1742,7 @@ gcdcalc: if ( GcdLong(BHEAD (UWORD *)num1,size1,(UWORD *)num2,size2
for ( iii = 2; iii < t1[1]; iii += 2 ) {
change = ExtraSymbol(t1[iii],t1[iii+1],nsym,ppsym,&ncoef);
nsym += change;
ppsym += change << 1;
ppsym += change * 2;
}
}
t1 = gcd + *gcd;
Expand Down Expand Up @@ -2571,7 +2571,7 @@ TryAgain:;
t += FUNHEAD+1;
change = ExtraSymbol(*t,-1,nsym,ppsym,&ncoef);
nsym += change;
ppsym += change << 1;
ppsym += change * 2;
goto DropDen;
}
else if ( t[FUNHEAD] == -SNUMBER ) {
Expand Down Expand Up @@ -2621,7 +2621,7 @@ TryAgain:;
WORD change;
change = ExtraSymbol(*t,-t[1],nsym,ppsym,&ncoef);
nsym += change;
ppsym += change << 1;
ppsym += change * 2;
t += 2;
}
}
Expand Down Expand Up @@ -2694,7 +2694,7 @@ WithFix: shortnum = k;
WORD change;
change = ExtraSymbol((WORD)(-k),(WORD)1,nsym,ppsym,&ncoef);
nsym += change;
ppsym += change << 1;
ppsym += change * 2;
}
t[1] = pdel[ndel-1];
t[0] = pdel[ndel-2];
Expand Down Expand Up @@ -5103,9 +5103,6 @@ int TestFunFlag(PHEAD WORD *tfun)
#[ BracketNormalize :
*/

#define EXCHN(t1,t2,n) { WORD a,i; for(i=0;i<n;i++){x=t1[i];t1[i]=t2[i];t2[i]=a;}
#define EXCH(x,y) { WORD a = (x); (x) = (y); (y) = a; }

WORD BracketNormalize(PHEAD WORD *term)
{
WORD *stop = term+*term-3, *t, *tt, *tstart, *r;
Expand Down
14 changes: 7 additions & 7 deletions sources/opera.c
Original file line number Diff line number Diff line change
Expand Up @@ -136,7 +136,7 @@ WORD EpfFind(PHEAD WORD *term, WORD *params)
mstop = c2;
fac = t[1] - FUNHEAD;
m = AT.TMout;
*m++ = 3 + (min<<1); /* The full length */
*m++ = 3 + (min*2); /* The full length */
*m++ = CONTRACT;
if ( number < 0 ) *m++ = 1;
else *m++ = 0;
Expand Down Expand Up @@ -183,9 +183,9 @@ WORD EpfFind(PHEAD WORD *term, WORD *params)
MUNLOCK(ErrorMessageLock);
SETERROR(-1)
}
tstop += (ABS(ncoef))<<1;
tstop += (ABS(ncoef))*2;
if ( sgn < 0 ) ncoef = -ncoef;
ncoef <<= 1;
ncoef *= 2;
*tstop++ = (ncoef<0)?(ncoef-1):(ncoef+1);
*term = WORDDIF(tstop,term);
return(1);
Expand Down Expand Up @@ -284,7 +284,7 @@ WORD EpfGen(WORD number, WORD *inlist, WORD *kron, WORD *perm, WORD sgn)
WORD i, *in2, k, a;
if ( !sgn ) {
in2 = inlist + number;
number <<= 1;
number *= 2;
for ( i = 1; i < number; i += 2 ) {
*perm++ = i;
*perm++ = i;
Expand All @@ -294,7 +294,7 @@ WORD EpfGen(WORD number, WORD *inlist, WORD *kron, WORD *perm, WORD sgn)
if ( number <= 0 ) return(0);
else return(1);
}
number <<= 1;
number *= 2;
i = number - 1;
while ( ( i -= 2 ) >= 0 ) {
if ( ( k = perm[i] ) != i ) {
Expand Down Expand Up @@ -433,7 +433,7 @@ WORD Trace4no(WORD number, WORD *kron, TRACES *t)
t->sgn = 1;
{
WORD nhalf = number >> 1;
WORD ndouble = number << 1;
WORD ndouble = number * 2;
p = t->eers;
t->eers = p; p += nhalf;
t->mepf = p; p += nhalf;
Expand Down Expand Up @@ -727,7 +727,7 @@ WORD Trace4(PHEAD WORD *term, WORD *params, WORD num, WORD level)
}
t->inlist = AT.WorkPointer;
t->accup = t->accu = t->inlist + number;
t->perm = t->accu + (number<<1);
t->perm = t->accu + (number*2);
t->eers = t->perm + number;
if ( ( AT.WorkPointer += 19 * number ) >= AT.WorkTop ) {
MLOCK(ErrorMessageLock);
Expand Down
2 changes: 1 addition & 1 deletion sources/parallel.c
Original file line number Diff line number Diff line change
Expand Up @@ -810,7 +810,7 @@ static int PF_GetLoser(NODE *n)
/*
keep the left term and get the loser of right subtree
*/
newnlen <<= 1;
newnlen *= 2;
newclen = ( newnlen > 0 ) ? ( newnlen + 1 ) : ( newnlen - 1 );
if ( newnlen < 0 ) newnlen = -newnlen;
PF_newclen[n->lloser] = newclen;
Expand Down
8 changes: 4 additions & 4 deletions sources/proces.c
Original file line number Diff line number Diff line change
Expand Up @@ -2580,7 +2580,7 @@ ComAct: if ( t < u ) do { *m++ = *t++; } while ( t < u );
if ( MulRat(BHEAD (UWORD *)u,REDLENG(l1),(UWORD *)r,REDLENG(l2),
(UWORD *)m,&l1) ) goto InsCall;
l2 = l1;
l2 <<= 1;
l2 *= 2;
if ( l2 < 0 ) {
m -= l2;
*m++ = l2-1;
Expand Down Expand Up @@ -2804,7 +2804,7 @@ WORD *PasteTerm(PHEAD WORD number, WORD *accum, WORD *position, WORD times, WORD
return(0);
}
x = l1;
x <<= 1;
x *= 2;
if ( x < 0 ) { accum -= x; *accum++ = x - 1; }
else { accum += x; *accum++ = x + 1; }
*u = WORDDIF(accum,u);
Expand Down Expand Up @@ -2976,7 +2976,7 @@ Nextr:;
}

i = ABS(l2);
i <<= 1;
i *= 2;
i++;
l2 = ( l2 >= 0 ) ? i: -i;
r = coef;
Expand Down Expand Up @@ -4897,7 +4897,7 @@ WORD PrepPoly(PHEAD WORD *term,WORD par)
while ( t < vv ) *m++ = *t++;
if ( MulRat(BHEAD (UWORD *)vv,ncoef,(UWORD *)tstop,jcoef,
(UWORD *)m,&ncoef) ) Terminate(-1);
ncoef <<= 1;
ncoef *= 2;
m += ABS(ncoef);
if ( ncoef < 0 ) ncoef--;
else ncoef++;
Expand Down
2 changes: 1 addition & 1 deletion sources/ratio.c
Original file line number Diff line number Diff line change
Expand Up @@ -489,7 +489,7 @@ WORD Glue(PHEAD WORD *term1, WORD *term2, WORD *sub, WORD insert)
TermFree(coef,"Glue");
SETERROR(-1)
}
i = (ABS(nc3))<<1;
i = (ABS(nc3))*2;
t += i++;
*t++ = (nc3 >= 0)?i:-i;
*term1 = WORDDIF(t,term1);
Expand Down
34 changes: 17 additions & 17 deletions sources/reken.c
Original file line number Diff line number Diff line change
Expand Up @@ -216,15 +216,15 @@ WORD AddRat(PHEAD UWORD *a, WORD na, UWORD *b, WORD nb, UWORD *c, WORD *nc)
WORD i;
*nc = nb;
if ( nb < 0 ) nb = -nb;
nb <<= 1;
nb *= 2;
for ( i = 0; i < nb; i++ ) *c++ = *b++;
return(0);
}
else if ( !nb ) {
WORD i;
*nc = na;
if ( na < 0 ) na = -na;
na <<= 1;
na *= 2;
for ( i = 0; i < na; i++ ) *c++ = *a++;
return(0);
}
Expand Down Expand Up @@ -382,27 +382,27 @@ WORD MulRat(PHEAD UWORD *a, WORD na, UWORD *b, WORD nb, UWORD *c, WORD *nc)
if ( *b == 1 && b[1] == 1 ) {
if ( nb == 1 ) {
*nc = na;
i = ABS(na); i <<= 1;
i = ABS(na); i *= 2;
while ( --i >= 0 ) *c++ = *a++;
return(0);
}
else if ( nb == -1 ) {
*nc = - na;
i = ABS(na); i <<= 1;
i = ABS(na); i *= 2;
while ( --i >= 0 ) *c++ = *a++;
return(0);
}
}
if ( *a == 1 && a[1] == 1 ) {
if ( na == 1 ) {
*nc = nb;
i = ABS(nb); i <<= 1;
i = ABS(nb); i *= 2;
while ( --i >= 0 ) *c++ = *b++;
return(0);
}
else if ( na == -1 ) {
*nc = - nb;
i = ABS(nb); i <<= 1;
i = ABS(nb); i *= 2;
while ( --i >= 0 ) *c++ = *b++;
return(0);
}
Expand Down Expand Up @@ -1232,12 +1232,12 @@ WORD RaisPow(PHEAD UWORD *a, WORD *na, UWORD b)
c = b;
for ( i = 0; i < BITSINWORD; i++ ) {
if ( !c ) break;
c >>= 1;
c /= 2;
}
i--;
c = 1 << i;
while ( --i >= 0 ) {
c >>= 1;
c /= 2;
if(MulLong(is,ns,is,ns,it,&nt)) goto RaisOvl;
if ( b & c ) {
if ( MulLong(it,nt,a,*na,is,&ns) ) goto RaisOvl;
Expand Down Expand Up @@ -1375,7 +1375,7 @@ WORD RaisPowMod (WORD x, WORD n, WORD m) {
while (n) {
if (n&1) { y*=z; y%=m; }
z*=z; z%=m;
n>>=1;
n /= 2;
}
return (WORD)y;
}
Expand Down Expand Up @@ -2311,15 +2311,15 @@ WORD GcdLong(PHEAD UWORD *a, WORD na, UWORD *b, WORD nb, UWORD *c, WORD *nc)
while ( a[0] == 0 ) { a++; ana--; tcounta1++; }
for ( ii = 0; ii < ana; ii++ ) { *uw++ = *a++; }
if ( ( ana & 1 ) != 0 ) { *uw = 0; ana++; }
ana >>= 1;
ana /= 2;

u2 = uw = NumberMalloc("GcdLong");
upb = (mp_limb_t *)u2;
anb = nb; tcountb1 = 0;
while ( b[0] == 0 ) { b++; anb--; tcountb1++; }
for ( ii = 0; ii < anb; ii++ ) { *uw++ = *b++; }
if ( ( anb & 1 ) != 0 ) { *uw = 0; anb++; }
anb >>= 1;
anb /= 2;

xx = upa[0]; tcounta = 0;
while ( ( xx & 15 ) == 0 ) { tcounta += 4; xx >>= 4; }
Expand Down Expand Up @@ -3106,7 +3106,7 @@ WORD Modulus(WORD *term)
return(0);
}
else if ( n1 > 0 ) {
n1 <<= 1;
n1 *= 2;
t += n1; /* Note that n1 >= 0 */
n1++;
}
Expand Down Expand Up @@ -3322,7 +3322,7 @@ WORD TakeNormalModulus (UWORD *a, WORD *na, UWORD *c, WORD nc, WORD par)
WCOPY(halfc,c,nc);

for (n=0; n<nhalfc; n++) {
halfc[n] >>= 1;
halfc[n] /= 2;
if (n+1<nc) halfc[n] |= c[n+1] << (BITSINWORD-1);
}

Expand Down Expand Up @@ -3393,11 +3393,11 @@ WORD MakeModTable()
UWORD *MMscrat7 = NumberMalloc("MakeModTable"), *MMscratC = NumberMalloc("MakeModTable");
*MMscratC = 1;
nScrat = 1;
j = size << 1;
j = size * 2;
for ( i = 0; i < j; i+=2 ) { AC.modpowers[i] = 0; AC.modpowers[i+1] = 0; }
for ( i = 0; i < size; i++ ) {
j = *MMscratC + (((LONG)MMscratC[1])<<BITSINWORD);
j <<= 1;
j *= 2;
AC.modpowers[j] = (WORD)(i & WORDMASK);
AC.modpowers[j+1] = (WORD)(i >> BITSINWORD);
MulLong((UWORD *)MMscratC,nScrat,(UWORD *)AC.powmod,
Expand All @@ -3406,7 +3406,7 @@ WORD MakeModTable()
*MMscratC = *MMscrat7; MMscratC[1] = MMscrat7[1]; nScrat = n2;
}
NumberFree(MMscrat7,"MakeModTable"); NumberFree(MMscratC,"MakeModTable");
j = size << 1;
j = size * 2;
for ( i = 4; i < j; i+=2 ) {
if ( AC.modpowers[i] == 0 && AC.modpowers[i+1] == 0 ) {
MLOCK(ErrorMessageLock);
Expand Down Expand Up @@ -3761,7 +3761,7 @@ void iniwranf(PHEAD0)
pow = offset; accu = 1;
while ( i ) {
if ( ( i & 1 ) != 0 ) accu *= pow;
i >>= 1; pow = pow*pow;
i /= 2; pow = pow*pow;
}
offset = accu;
}
Expand Down

0 comments on commit 364f6d5

Please sign in to comment.