mask_of_range: simplify high comparison
To test c <= high, instead of testing the sign of (high + 1) - c, negate the
sign of high - c (as we're doing for c - low). This is a little easier to
read and shaves 2 instructions off the arm thumb build with
arm-none-eabi-gcc 7.3.1.
Signed-off-by: Gilles Peskine <Gilles.Peskine@arm.com>
diff --git a/library/base64.c b/library/base64.c
index d334296..6ddd501 100644
--- a/library/base64.c
+++ b/library/base64.c
@@ -44,9 +44,11 @@
static unsigned char mask_of_range( unsigned char low, unsigned char high,
unsigned char c )
{
- unsigned low_mask = ( c - low ) >> 8;
- unsigned high_mask = ( c - high - 1 ) >> 8;
- return( ~low_mask & high_mask & 0xff );
+ /* low_mask is: 0 if low <= c, 0x...ff if low > c */
+ unsigned low_mask = ( (unsigned) c - low ) >> 8;
+ /* high_mask is: 0 if c <= high, 0x...ff if high > c */
+ unsigned high_mask = ( (unsigned) high - c ) >> 8;
+ return( ~( low_mask | high_mask ) & 0xff );
}
/* Given a value in the range 0..63, return the corresponding Base64 digit.