diff options
| author | Mattias Andrée <m@maandree.se> | 2026-05-07 21:07:48 +0200 |
|---|---|---|
| committer | Mattias Andrée <m@maandree.se> | 2026-05-07 21:07:48 +0200 |
| commit | 44f7c4a6db7b4896227de2d07478e4739839dfd5 (patch) | |
| tree | 9a3e9d8ff700150a75c955ceb00141a63fa5c5ae /behex_lower.c | |
| parent | Fix config-armv8.mk (diff) | |
| download | libsha2-44f7c4a6db7b4896227de2d07478e4739839dfd5.tar.gz libsha2-44f7c4a6db7b4896227de2d07478e4739839dfd5.tar.bz2 libsha2-44f7c4a6db7b4896227de2d07478e4739839dfd5.tar.xz | |
Optimise conversion to hexadecimal
Signed-off-by: Mattias Andrée <m@maandree.se>
Diffstat (limited to '')
| -rw-r--r-- | behex_lower.c | 15 |
1 files changed, 13 insertions, 2 deletions
diff --git a/behex_lower.c b/behex_lower.c index 575ff39..93969ad 100644 --- a/behex_lower.c +++ b/behex_lower.c @@ -5,10 +5,21 @@ void libsha2_behex_lower(char *restrict output, const void *restrict hashsum_, size_t n) { +#define S(X)\ + X"0", X"1", X"2", X"3", X"4", X"5", X"6", X"7",\ + X"8", X"9", X"a", X"b", X"c", X"d", X"e", X"f" +#if defined(__GNUC__) + __attribute__((__nonstring__)) +#endif + static const char lut[256][2] = { + S("0"), S("1"), S("2"), S("3"), S("4"), S("5"), S("6"), S("7"), + S("8"), S("9"), S("a"), S("b"), S("c"), S("d"), S("e"), S("f") + }; + const unsigned char *restrict hashsum = hashsum_; output[2 * n] = '\0'; while (n--) { - output[2 * n + 0] = "0123456789abcdef"[(hashsum[n] >> 4) & 15]; - output[2 * n + 1] = "0123456789abcdef"[(hashsum[n] >> 0) & 15]; + output[2 * n + 0] = lut[hashsum[n]][0]; + output[2 * n + 1] = lut[hashsum[n]][1]; } } |
