llvm.org GIT mirror llvm / 9e4d3eb
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 Rafael Espindola 4 years ago
1 changed file(s) with 41 addition(s) and 13 deletion(s). Raw diff Collapse all Expand all
296296 unaligned_int64_t;
297297
298298 namespace endian {
299 inline uint16_t read16le(const void *p) { return *(const ulittle16_t *)p; }
300 inline uint32_t read32le(const void *p) { return *(const ulittle32_t *)p; }
301 inline uint64_t read64le(const void *p) { return *(const ulittle64_t *)p; }
302 inline uint16_t read16be(const void *p) { return *(const ubig16_t *)p; }
303 inline uint32_t read32be(const void *p) { return *(const ubig32_t *)p; }
304 inline uint64_t read64be(const void *p) { return *(const ubig64_t *)p; }
305
306 inline void write16le(void *p, uint16_t v) { *(ulittle16_t *)p = v; }
307 inline void write32le(void *p, uint32_t v) { *(ulittle32_t *)p = v; }
308 inline void write64le(void *p, uint64_t v) { *(ulittle64_t *)p = v; }
309 inline void write16be(void *p, uint16_t v) { *(ubig16_t *)p = v; }
310 inline void write32be(void *p, uint32_t v) { *(ubig32_t *)p = v; }
311 inline void write64be(void *p, uint64_t v) { *(ubig64_t *)p = v; }
299 template inline T read(const void *P) {
300 return *(const detail::packed_endian_specific_integral *)P;
301 }
302
303 template inline uint16_t read16(const void *P) {
304 return read(P);
305 }
306 template inline uint32_t read32(const void *P) {
307 return read(P);
308 }
309 template inline uint64_t read64(const void *P) {
310 return read(P);
311 }
312
313 inline uint16_t read16le(const void *P) { return read16(P); }
314 inline uint32_t read32le(const void *P) { return read32(P); }
315 inline uint64_t read64le(const void *P) { return read64(P); }
316 inline uint16_t read16be(const void *P) { return read16(P); }
317 inline uint32_t read32be(const void *P) { return read32(P); }
318 inline uint64_t read64be(const void *P) { return read64(P); }
319
320 template inline void write(void *P, T V) {
321 *(detail::packed_endian_specific_integral *)P = V;
322 }
323
324 template inline void write16(void *P, uint16_t V) {
325 write(P, V);
326 }
327 template inline void write32(void *P, uint32_t V) {
328 write(P, V);
329 }
330 template inline void write64(void *P, uint64_t V) {
331 write(P, V);
332 }
333
334 inline void write16le(void *P, uint16_t V) { write16(P, V); }
335 inline void write32le(void *P, uint32_t V) { write32(P, V); }
336 inline void write64le(void *P, uint64_t V) { write64(P, V); }
337 inline void write16be(void *P, uint16_t V) { write16(P, V); }
338 inline void write32be(void *P, uint32_t V) { write32(P, V); }
339 inline void write64be(void *P, uint64_t V) { write64(P, V); }
312340 } // end namespace endian
313341 } // end namespace support
314342 } // end namespace llvm