From cb27eebd32af3dfebf46f5eb3778fe29175004b5 Mon Sep 17 00:00:00 2001 From: Karl Williamson Date: Tue, 31 Dec 2013 22:35:46 -0700 Subject: [PATCH] handy.h: Add two macros handy.h contains a macro that reads a hex digit and returns its value, with fewer branches than a naive implementation would use. This commit just copies and modifies it to create two macros for 1) just converting the hex value, without advancing the input; and 2) doing the same for an octal value. --- handy.h | 17 +++++++++++++---- 1 file changed, 13 insertions(+), 4 deletions(-) diff --git a/handy.h b/handy.h index c4a162d..6b74c87 100644 --- a/handy.h +++ b/handy.h @@ -1564,14 +1564,23 @@ typedef U32 line_t; } \ return a; -/* Converts a hex digit in a string to its numeric value, advancing the - * pointer. The input must be known to be 0-9, A-F, or a-f. In both ASCII and - * EBCDIC the last 4 bits of the digits are 0-9; and the last 4 bits of A-F and - * a-f are 1-6, so adding 9 yields 10-15 */ +/* Converts a character known to represent a hexadecimal digit (0-9, A-F, or + * a-f) to its numeric value. READ_XDIGIT's argument is a string pointer, + * which is advanced. The input is validated only by an assert() in DEBUGGING + * builds. In both ASCII and EBCDIC the last 4 bits of the digits are 0-9; and + * the last 4 bits of A-F and a-f are 1-6, so adding 9 yields 10-15 */ +#define XDIGIT_VALUE(c) (__ASSERT_(isXDIGIT(c)) (0xf & (isDIGIT(c) \ + ? (c) \ + : ((c) + 9)))) #define READ_XDIGIT(s) (__ASSERT_(isXDIGIT(*s)) (0xf & (isDIGIT(*(s)) \ ? (*(s)++) \ : (*(s)++ + 9)))) +/* Converts a character known to represent an octal digit (0-7) to its numeric + * value. The input is validated only by an assert() in DEBUGGING builds. In + * both ASCII and EBCDIC the last 3 bits of the octal digits range from 0-7. */ +#define OCTAL_VALUE(c) (__ASSERT_(isOCTAL(c)) (7 & (c))) + /* =head1 Memory Management -- 2.7.4