If we have native endianess, i.e. parsing modules for the running
system, assist the compiler to note that it is really much faster to
move a word/qword etc. instead of actually running through a loop.
Reduces library instructions on x86_64 by 1.4 % and binary instructions
by 3 % with default configuration.
Signed-off-by: Tobias Stoeckmann <tobias@stoeckmann.org>
Reviewed-by: Emil Velikov <emil.l.velikov@gmail.com>
Link: https://github.com/kmod-project/kmod/pull/187
Signed-off-by: Lucas De Marchi <lucas.de.marchi@gmail.com>
#include <assert.h>
#include <elf.h>
+#include <endian.h>
#include <errno.h>
#include <limits.h>
#include <stdlib.h>
{
const uint8_t *p;
uint64_t ret = 0;
- size_t i;
assert(size <= sizeof(uint64_t));
assert(offset + size <= elf->size);
}
p = elf->memory + offset;
+
if (elf->msb) {
- for (i = 0; i < size; i++)
- ret = (ret << 8) | p[i];
+ memcpy((char *)&ret + sizeof(ret) - size, p, size);
+ ret = be64toh(ret);
} else {
- for (i = 1; i <= size; i++)
- ret = (ret << 8) | p[size - i];
+ memcpy(&ret, p, size);
+ ret = le64toh(ret);
}
ELFDBG(elf, "size=%" PRIu16 " offset=%" PRIu64 " value=%" PRIu64 "\n", size,