From 088f9a6c32cf48f87ad31d0a7de2fa9b41d21993 Mon Sep 17 00:00:00 2001 From: Aleksei Nikiforov Date: Fri, 10 Jan 2025 11:16:41 +0100 Subject: [PATCH] Get rid of additional memcpy calls --- ggml/src/ggml.c | 15 +++------------ ggml/src/gguf.cpp | 15 +++------------ 2 files changed, 6 insertions(+), 24 deletions(-) diff --git a/ggml/src/ggml.c b/ggml/src/ggml.c index 383246a72..553ac5926 100644 --- a/ggml/src/ggml.c +++ b/ggml/src/ggml.c @@ -51,24 +51,15 @@ #define convert_from_le64(x) #elif __BYTE_ORDER__ == __ORDER_BIG_ENDIAN__ static inline void convert_from_le16(void * value) { - uint16_t temp; - memcpy(&temp, value, sizeof(uint16_t)); - temp = le16toh(temp); - memcpy(value, &temp, sizeof(uint16_t)); + *((uint16_t*)value) = le16toh(*((uint16_t*)value)); } static inline void convert_from_le32(void * value) { - uint32_t temp; - memcpy(&temp, value, sizeof(uint32_t)); - temp = le32toh(temp); - memcpy(value, &temp, sizeof(uint32_t)); + *((uint32_t*)value) = le32toh(*((uint32_t*)value)); } static inline void convert_from_le64(void * value) { - uint64_t temp; - memcpy(&temp, value, sizeof(uint64_t)); - temp = le64toh(temp); - memcpy(value, &temp, sizeof(uint64_t)); + *((uint64_t*)value) = le64toh(*((uint64_t*)value)); } #else #error Unexpected or undefined __BYTE_ORDER__ diff --git a/ggml/src/gguf.cpp b/ggml/src/gguf.cpp index bc47dd6cc..55de3b765 100644 --- a/ggml/src/gguf.cpp +++ b/ggml/src/gguf.cpp @@ -36,26 +36,17 @@ static inline void convert_from_le(T * /*value*/) template = 0> static inline void convert_from_le(T * value) { - uint16_t temp; - memcpy(&temp, value, sizeof(uint16_t)); - temp = le16toh(temp); - memcpy(value, &temp, sizeof(uint16_t)); + *((uint16_t*)value) = le16toh(*((uint16_t*)value)); } template = 0> static inline void convert_from_le(T * value) { - uint32_t temp; - memcpy(&temp, value, sizeof(uint32_t)); - temp = le32toh(temp); - memcpy(value, &temp, sizeof(uint32_t)); + *((uint32_t*)value) = le32toh(*((uint32_t*)value)); } template = 0> static inline void convert_from_le(T * value) { - uint64_t temp; - memcpy(&temp, value, sizeof(uint64_t)); - temp = le64toh(temp); - memcpy(value, &temp, sizeof(uint64_t)); + *((uint64_t*)value) = le64toh(*((uint64_t*)value)); } #else #error Unexpected or undefined __BYTE_ORDER__