From 04ca98e013d942f3c9876f25738fc56e71574dd7 Mon Sep 17 00:00:00 2001 From: Karl Williamson Date: Mon, 1 Dec 2025 14:49:09 -0700 Subject: [PATCH] Reformat XDIGIT_VALUE() This makes it clearer what is going on, and may encourage compiler optimization --- handy.h | 12 +++++++----- 1 file changed, 7 insertions(+), 5 deletions(-) diff --git a/handy.h b/handy.h index 0ada57f2d2e3..193bf5b65e24 100644 --- a/handy.h +++ b/handy.h @@ -2554,16 +2554,18 @@ typedef U32 line_t; /* Converts a character KNOWN to represent a hexadecimal digit (0-9, A-F, or * a-f) to its numeric value without using any branches. The input is * validated only by an assert() in DEBUGGING builds. - * * It works by right shifting and isolating the bit that is 0 for the digits, * and 1 for at least the alphas A-F, a-f. The bit is shifted to the ones * position, and then to the eights position. Both are added together to form * 0 if the input is '0'-'9' and to form 9 if alpha. This is added to the * final four bits of the input to form the correct value. */ -#define XDIGIT_VALUE(c) (assert(isXDIGIT(c)), \ - ((NATIVE_TO_LATIN1(c) >> 6) & 1) /* 1 if alpha; 0 if not */ \ - + ((NATIVE_TO_LATIN1(c) >> 3) & 8) /* 8 if alpha; 0 if not */ \ - + ((c) & 0xF)) /* 0-9 if input valid hex digit */ + +#define XDIGIT_VALUE(c) \ + (assert(isXDIGIT(c)), \ + ( ( (NATIVE_TO_LATIN1(c) & 0x40) >> 3) /* 8 if alpha; 0 if not */ \ + | (NATIVE_TO_LATIN1(c) & 0x40) >> 6) /* 1 if alpha; 0 if not */ \ + /* After OR: 9 if alpha, 0 if not */ \ + + ((c) & 0xF)) /* 0-9 if input valid hex digit */ /* The argument is a string pointer, which is advanced. */ #define READ_XDIGIT(s) ((s)++, XDIGIT_VALUE(*((s) - 1)))