Re: [PATCH 1/2] lib: hexdump: use a look-up table to do hex_to_bin

From: Andy Shevchenko
Date: Wed Jun 29 2016 - 12:23:09 EST


On Thu, 2016-06-30 at 00:15 +0800, zengzhaoxiu@xxxxxxx wrote:
> From: Zhaoxiu Zeng <zhaoxiu.zeng@xxxxxxxxx>
>

No way.

At least commit message.
And prerequisite is the performance / memory foot print tests.

> Signed-off-by: Zhaoxiu Zeng <zhaoxiu.zeng@xxxxxxxxx>
> ---
> Âinclude/linux/kernel.h | 15 ++++++++++++++-
> Âlib/hexdump.cÂÂÂÂÂÂÂÂÂÂ| 36 +++++++++++++++++++-----------------
> Â2 files changed, 33 insertions(+), 18 deletions(-)
>
> diff --git a/include/linux/kernel.h b/include/linux/kernel.h
> index 94aa10f..72a0432 100644
> --- a/include/linux/kernel.h
> +++ b/include/linux/kernel.h
> @@ -532,7 +532,20 @@ static inline char *hex_byte_pack_upper(char
> *buf, u8 byte)
> Â return buf;
> Â}
> Â
> -extern int hex_to_bin(char ch);
> +extern const int8_t h2b_lut[];
> +
> +/**
> + * hex_to_bin - convert a hex digit to its real value
> + * @ch: ascii character represents hex digit
> + *
> + * hex_to_bin() converts one hex digit to its actual value or -1 in
> case of bad
> + * input.
> + */
> +static inline int hex_to_bin(char ch)
> +{
> + return h2b_lut[(unsigned char)ch];
> +}
> +
> Âextern int __must_check hex2bin(u8 *dst, const char *src, size_t
> count);
> Âextern char *bin2hex(char *dst, const void *src, size_t count);
> Â
> diff --git a/lib/hexdump.c b/lib/hexdump.c
> index 992457b..878697f 100644
> --- a/lib/hexdump.c
> +++ b/lib/hexdump.c
> @@ -18,23 +18,25 @@ EXPORT_SYMBOL(hex_asc);
> Âconst char hex_asc_upper[] = "0123456789ABCDEF";
> ÂEXPORT_SYMBOL(hex_asc_upper);
> Â
> -/**
> - * hex_to_bin - convert a hex digit to its real value
> - * @ch: ascii character represents hex digit
> - *
> - * hex_to_bin() converts one hex digit to its actual value or -1 in
> case of bad
> - * input.
> - */
> -int hex_to_bin(char ch)
> -{
> - if ((ch >= '0') && (ch <= '9'))
> - return ch - '0';
> - ch = tolower(ch);
> - if ((ch >= 'a') && (ch <= 'f'))
> - return ch - 'a' + 10;
> - return -1;
> -}
> -EXPORT_SYMBOL(hex_to_bin);
> +const int8_t h2b_lut[] = {
> + -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
> -1,
> + -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
> -1,
> + -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
> -1,
> + Â0,ÂÂ1,ÂÂ2,ÂÂ3,ÂÂ4,ÂÂ5,ÂÂ6,ÂÂ7,ÂÂ8,ÂÂ9, -1, -1, -1, -1, -1,
> -1,
> + -1, 10, 11, 12, 13, 14, 15, -1, -1, -1, -1, -1, -1, -1, -1,
> -1,
> + -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
> -1,
> + -1, 10, 11, 12, 13, 14, 15, -1, -1, -1, -1, -1, -1, -1, -1,
> -1,
> + -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
> -1,
> + -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
> -1,
> + -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
> -1,
> + -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
> -1,
> + -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
> -1,
> + -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
> -1,
> + -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
> -1,
> + -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
> -1,
> + -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
> -1,
> +};
> +EXPORT_SYMBOL(h2b_lut);
> Â
> Â/**
> Â * hex2bin - convert an ascii hexadecimal string to its binary
> representation

--

Andy Shevchenko <andriy.shevchenko@xxxxxxxxxxxxxxx>
Intel Finland Oy