From 9e4d3eb47dd946cee9c71f66966b0db2f99a0e52 Mon Sep 17 00:00:00 2001 From: Rafael Espindola Date: Mon, 9 Nov 2015 21:34:45 +0000 Subject: [PATCH] Add templated read/write to support::endian. Expose read and write functions that take endianess as a template parameter. git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@252517 91177308-0d34-0410-b5e6-96231b3b80d8 --- include/llvm/Support/Endian.h | 54 ++++++++++++++++++++++++++--------- 1 file changed, 41 insertions(+), 13 deletions(-) diff --git a/include/llvm/Support/Endian.h b/include/llvm/Support/Endian.h index b98ff5ede77..bc93c9a66ee 100644 --- a/include/llvm/Support/Endian.h +++ b/include/llvm/Support/Endian.h @@ -297,19 +297,47 @@ typedef detail::packed_endian_specific_integral unaligned_int64_t; namespace endian { -inline uint16_t read16le(const void *p) { return *(const ulittle16_t *)p; } -inline uint32_t read32le(const void *p) { return *(const ulittle32_t *)p; } -inline uint64_t read64le(const void *p) { return *(const ulittle64_t *)p; } -inline uint16_t read16be(const void *p) { return *(const ubig16_t *)p; } -inline uint32_t read32be(const void *p) { return *(const ubig32_t *)p; } -inline uint64_t read64be(const void *p) { return *(const ubig64_t *)p; } - -inline void write16le(void *p, uint16_t v) { *(ulittle16_t *)p = v; } -inline void write32le(void *p, uint32_t v) { *(ulittle32_t *)p = v; } -inline void write64le(void *p, uint64_t v) { *(ulittle64_t *)p = v; } -inline void write16be(void *p, uint16_t v) { *(ubig16_t *)p = v; } -inline void write32be(void *p, uint32_t v) { *(ubig32_t *)p = v; } -inline void write64be(void *p, uint64_t v) { *(ubig64_t *)p = v; } +template inline T read(const void *P) { + return *(const detail::packed_endian_specific_integral *)P; +} + +template inline uint16_t read16(const void *P) { + return read(P); +} +template inline uint32_t read32(const void *P) { + return read(P); +} +template inline uint64_t read64(const void *P) { + return read(P); +} + +inline uint16_t read16le(const void *P) { return read16(P); } +inline uint32_t read32le(const void *P) { return read32(P); } +inline uint64_t read64le(const void *P) { return read64(P); } +inline uint16_t read16be(const void *P) { return read16(P); } +inline uint32_t read32be(const void *P) { return read32(P); } +inline uint64_t read64be(const void *P) { return read64(P); } + +template inline void write(void *P, T V) { + *(detail::packed_endian_specific_integral *)P = V; +} + +template inline void write16(void *P, uint16_t V) { + write(P, V); +} +template inline void write32(void *P, uint32_t V) { + write(P, V); +} +template inline void write64(void *P, uint64_t V) { + write(P, V); +} + +inline void write16le(void *P, uint16_t V) { write16(P, V); } +inline void write32le(void *P, uint32_t V) { write32(P, V); } +inline void write64le(void *P, uint64_t V) { write64(P, V); } +inline void write16be(void *P, uint16_t V) { write16(P, V); } +inline void write32be(void *P, uint32_t V) { write32(P, V); } +inline void write64be(void *P, uint64_t V) { write64(P, V); } } // end namespace endian } // end namespace support } // end namespace llvm -- 2.34.1