mirror of
https://codeberg.org/libreboot/lbmk.git
synced 2026-03-25 21:39:03 +02:00
util/nvmutil: optimise hextonum
there is 0x20 of different between a and A so we can just or 0x20 and compare only lowercase. we can also cast char (which may me signed on some systems) to unsigned, and then only check whether it's lower than 10. this code results in far less branching (in C), but a good optimising compiler probably wouldn't have cared about the old version anyway. it's just nicer C code. this also means we no longer need to check for X, only x. Signed-off-by: Leah Rowe <leah@libreboot.org>
This commit is contained in:
@@ -422,16 +422,17 @@ set_mac_nib(int mac_pos, int nib)
|
||||
static uint8_t
|
||||
hextonum(char ch)
|
||||
{
|
||||
if (ch >= '0' && ch <= '9')
|
||||
if ((unsigned)(ch - '0') <= 9)
|
||||
return ch - '0';
|
||||
else if (ch >= 'A' && ch <= 'F')
|
||||
return ch - 'A' + 10;
|
||||
else if (ch >= 'a' && ch <= 'f')
|
||||
|
||||
ch |= 0x20;
|
||||
|
||||
if ((unsigned)(ch - 'a') <= 5)
|
||||
return ch - 'a' + 10;
|
||||
else if (ch == '?' || ch == 'x' || ch == 'X')
|
||||
return rhex(); /* random hex value */
|
||||
else if (ch == '?' || ch == 'x')
|
||||
return rhex(); /* random character */
|
||||
else
|
||||
return 16; /* error: invalid character */
|
||||
return 16; /* invalid character */
|
||||
}
|
||||
|
||||
static uint8_t
|
||||
|
||||
Reference in New Issue
Block a user