progs/..., symm/...: Fix 32-bit right-shift idiom.
authorMark Wooding <mdw@distorted.org.uk>
Tue, 30 Oct 2018 22:05:18 +0000 (22:05 +0000)
committerMark Wooding <mdw@distorted.org.uk>
Sat, 24 Nov 2018 20:12:17 +0000 (20:12 +0000)
commite91d142c49f0ff129a087f2b66380bd7c5da0617
tree0d8eea6036b91894f4b07bd24122c3f2463e5805
parent2921991916ba2362d054111a0d041ff170c899c1
progs/..., symm/...: Fix 32-bit right-shift idiom.

This one has a long and troubled history.  Writing

x >> 32

is undefined behaviour if x is only 32 bits wide.  On the other hand, if
it's /not/, then this is necessary to get hold of the upper bits.

The obvious escape plan is to write

(x >> 16) >> 16

(the parentheses are unfortunately necessary), but some Microsoft
compilers managed do bungle compiling this: they merged the two shifts
together and then decided that a shift by 32 places was a no-op.

So I wrote

((x&~MASK32) >> 16) >> 16

which stood for many years.  Unfortunately this is really wrong too: if
x is wider than 32 bits, that's nice, but MASK32 /isn't/ necessarily, so
~MASK32 is all-bits zero and the high bits of x are just lost.

Fix this by casting MASK32 to the-type-of-x before inverting it.

Ugh.
18 files changed:
progs/cookie.c
progs/dsig.c
symm/blkc.h
symm/has160.c
symm/hash.h
symm/mars-mktab.c
symm/md4.c
symm/md5.c
symm/rmd128.c
symm/rmd160.c
symm/rmd256.c
symm/rmd320.c
symm/sha.c
symm/sha256.c
symm/sha3.c
symm/sha512.c
symm/tiger.c
symm/whirlpool.c