aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorMattias Andrée <m@maandree.se>2026-05-07 21:07:10 +0200
committerMattias Andrée <m@maandree.se>2026-05-07 21:07:17 +0200
commite18ea49a8ff16034cd34235fcf5691732159306b (patch)
treeb937e7cc5ddf18e3031a9a9e040292cfc38de33b
parentFix config-armv8.mk (diff)
downloadlibsha1-e18ea49a8ff16034cd34235fcf5691732159306b.tar.gz
libsha1-e18ea49a8ff16034cd34235fcf5691732159306b.tar.bz2
libsha1-e18ea49a8ff16034cd34235fcf5691732159306b.tar.xz
Optimise conversion to hexadecimal
Signed-off-by: Mattias Andrée <m@maandree.se>
Diffstat (limited to '')
-rw-r--r--behex_lower.c15
-rw-r--r--behex_upper.c15
2 files changed, 26 insertions, 4 deletions
diff --git a/behex_lower.c b/behex_lower.c
index 301a8ca..cca2ddd 100644
--- a/behex_lower.c
+++ b/behex_lower.c
@@ -5,10 +5,21 @@
void
libsha1_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/behex_upper.c b/behex_upper.c
index 877b38f..5002fdb 100644
--- a/behex_upper.c
+++ b/behex_upper.c
@@ -5,10 +5,21 @@
void
libsha1_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];
}
}