From 8461dcf423ec73074fcfc1a8638d8e7ccfe74f80 Mon Sep 17 00:00:00 2001 From: Aryadev Chavali Date: Sun, 28 Apr 2024 20:53:44 +0530 Subject: [PATCH] Little endian check for convert_*_to_* We don't need to perform the for loop if not necessary --- lib/base.c | 28 ++++++++++++++++++++-------- 1 file changed, 20 insertions(+), 8 deletions(-) diff --git a/lib/base.c b/lib/base.c index 5fbe097..64039ea 100644 --- a/lib/base.c +++ b/lib/base.c @@ -73,27 +73,39 @@ word_t word_bctoh(word_t w) hword_t convert_bytes_to_hword(const byte_t *bytes) { hword_t h = 0; - for (size_t i = 0; i < HWORD_SIZE; ++i) - h |= ((hword_t)(bytes[i]) << (8 * i)); + if (LITTLE_ENDIAN) + memcpy(&h, bytes, HWORD_SIZE); + else + for (size_t i = 0; i < HWORD_SIZE; ++i) + h |= ((hword_t)(bytes[i]) << (8 * i)); return h; } word_t convert_bytes_to_word(const byte_t *bytes) { word_t h = 0; - for (size_t i = 0; i < WORD_SIZE; ++i) - h |= ((word_t)(bytes[i]) << (8 * i)); + if (LITTLE_ENDIAN) + memcpy(&h, bytes, WORD_SIZE); + else + for (size_t i = 0; i < WORD_SIZE; ++i) + h |= ((word_t)(bytes[i]) << (8 * i)); return h; } void convert_hword_to_bytes(hword_t w, byte_t *bytes) { - hword_t be_h = hword_htobc(w); - memcpy(bytes, &be_h, HWORD_SIZE); + if (LITTLE_ENDIAN) + memcpy(bytes, &w, HWORD_SIZE); + else + for (size_t i = 0; i < HWORD_SIZE; ++i) + bytes[i] = WORD_NTH_BYTE(w, i); } void convert_word_to_bytes(word_t w, byte_t *bytes) { - word_t be_w = word_htobc(w); - memcpy(bytes, &be_w, WORD_SIZE); + if (LITTLE_ENDIAN) + memcpy(bytes, &w, WORD_SIZE); + else + for (size_t i = 0; i < WORD_SIZE; ++i) + bytes[i] = WORD_NTH_BYTE(w, i); }