From af75e701c43fc4e754f516c81190b77fa4006bb8 Mon Sep 17 00:00:00 2001 From: Mattias Andrée Date: Thu, 7 May 2026 21:13:34 +0200 Subject: Optimise conversion to hexadecimal MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: Mattias Andrée --- LICENSE | 2 +- util/libkeccak_behex_lower.c | 15 +++++++++++++-- util/libkeccak_behex_upper.c | 15 +++++++++++++-- 3 files changed, 27 insertions(+), 5 deletions(-) diff --git a/LICENSE b/LICENSE index 6b568f5..a2cc1c2 100644 --- a/LICENSE +++ b/LICENSE @@ -1,6 +1,6 @@ ISC License -© 2014, 2015, 2017, 2019, 2021, 2022, 2024 Mattias Andrée +© 2014, 2015, 2017, 2019, 2021, 2022, 2024, 2026 Mattias Andrée Permission to use, copy, modify, and/or distribute this software for any purpose with or without fee is hereby granted, provided that the above diff --git a/util/libkeccak_behex_lower.c b/util/libkeccak_behex_lower.c index 07593e8..70f5c01 100644 --- a/util/libkeccak_behex_lower.c +++ b/util/libkeccak_behex_lower.c @@ -12,11 +12,22 @@ void libkeccak_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]; } } diff --git a/util/libkeccak_behex_upper.c b/util/libkeccak_behex_upper.c index 5186a28..c5f9691 100644 --- a/util/libkeccak_behex_upper.c +++ b/util/libkeccak_behex_upper.c @@ -12,11 +12,22 @@ void libkeccak_behex_upper(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]; } } -- cgit v1.2.3-70-g09d2