From a2dc0cc9ea4062dfde69423ddb009617fc891c95 Mon Sep 17 00:00:00 2001 From: Gilles Peskine Date: Fri, 30 Jul 2021 12:57:22 +0200 Subject: [PATCH] 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 --- library/base64.c | 8 +++++--- 1 file changed, 5 insertions(+), 3 deletions(-) diff --git a/library/base64.c b/library/base64.c index d33429645..6ddd501a4 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.