| 1 | /* |
| 2 | * Copyright (c) 2016 Michael J. Roberts. All Rights Reserved. |
| 3 | * |
| 4 | * Please see the accompanying license file, LICENSE.TXT, for information |
| 5 | * on using and copying this software. |
| 6 | */ |
| 7 | /* |
| 8 | Name |
| 9 | h_le_c11.h - hardware definitions for little-endian CPUs. |
| 10 | |
| 11 | Function |
| 12 | These definitions are for little-endian CPUs and suitable for all flat |
| 13 | memory addressing widths (so it's suitable for both 32-bit as well as |
| 14 | 64-bit systems.) |
| 15 | |
| 16 | The definitions provided here will never perform unaligned memory access. |
| 17 | |
| 18 | A C11 compiler is required to use this file with TADS 2, and a C++11 |
| 19 | compiler is required for TADS 3. |
| 20 | |
| 21 | Notes |
| 22 | This file is especially useful for CPUs where unaligned memory access is |
| 23 | either not allowed or is problematic (like some of the ARM CPU models). It |
| 24 | should however be just as fast on x86 and x86-64 as the h_ix86.h and |
| 25 | h_ix86_64.h definitions due to compiler optimizations. So if you have |
| 26 | access to a C11 (and C++11) compiler, you can use this file on all |
| 27 | little-endian architectures. |
| 28 | |
| 29 | Note that even though the routines below use memcpy(), virtually all |
| 30 | compilers will optimize away the memcpy() and produce inlined data |
| 31 | movement machine instructions. |
| 32 | |
| 33 | Modified |
| 34 | 03/23/2016 Nikos Chantziaras - Creation |
| 35 | */ |
| 36 | |
| 37 | #ifndef H_LE_C11_H |
| 38 | #define H_LE_C11_H |
| 39 | |
| 40 | #include <stdint.h> |
| 41 | #include <string.h> |
| 42 | #ifndef __cplusplus |
| 43 | #include <stdalign.h> |
| 44 | #endif |
| 45 | |
| 46 | /* Round a size up to worst-case alignment boundary. */ |
| 47 | static inline size_t osrndsz(const size_t siz) |
| 48 | { |
| 49 | return (siz + alignof(intmax_t) - 1) & ~(alignof(intmax_t) - 1); |
| 50 | } |
| 51 | |
| 52 | /* Round a pointer up to worst-case alignment boundary. */ |
| 53 | static inline const void* osrndpt(const void* p) |
| 54 | { |
| 55 | return (void*)(((uintptr_t)p + alignof(void*) - 1) |
| 56 | & ~(alignof(void*) - 1)); |
| 57 | } |
| 58 | |
| 59 | /* Read an unaligned portable unsigned 2-byte value, returning int. */ |
| 60 | static inline int osrp2(const void* p) |
| 61 | { |
| 62 | uint16_t tmp; |
| 63 | memcpy(&tmp, p, 2); |
| 64 | return tmp; |
| 65 | } |
| 66 | |
| 67 | /* Read an unaligned portable signed 2-byte value, returning int. */ |
| 68 | static inline int osrp2s(const void* p) |
| 69 | { |
| 70 | int16_t tmp; |
| 71 | memcpy(&tmp, p, 2); |
| 72 | return tmp; |
| 73 | } |
| 74 | |
| 75 | /* Write unsigned int to unaligned portable 2-byte value. */ |
| 76 | static inline void oswp2(void* p, const unsigned i) |
| 77 | { |
| 78 | const uint16_t tmp = i; |
| 79 | memcpy(p, &tmp, 2); |
| 80 | } |
| 81 | |
| 82 | /* Write signed int to unaligned portable 2-byte value. */ |
| 83 | static inline void oswp2s(void* p, const int i) |
| 84 | { |
| 85 | const int16_t tmp = i; |
| 86 | memcpy(p, &tmp, 2); |
| 87 | } |
| 88 | |
| 89 | /* Read an unaligned unsigned portable 4-byte value, returning long. */ |
| 90 | static inline unsigned long osrp4(const void* p) |
| 91 | { |
| 92 | uint32_t tmp; |
| 93 | memcpy(&tmp, p, 4); |
| 94 | return tmp; |
| 95 | } |
| 96 | |
| 97 | /* Read an unaligned signed portable 4-byte value, returning long. */ |
| 98 | static inline long osrp4s(const void *p) |
| 99 | { |
| 100 | int32_t tmp; |
| 101 | memcpy(&tmp, p, 4); |
| 102 | return tmp; |
| 103 | } |
| 104 | |
| 105 | /* Write an unsigned long to an unaligned portable 4-byte value. */ |
| 106 | static inline void oswp4(void* p, const unsigned long l) |
| 107 | { |
| 108 | const uint32_t tmp = l; |
| 109 | memcpy(p, &tmp, 4); |
| 110 | } |
| 111 | |
| 112 | /* Write a signed long to an unaligned portable 4-byte value. */ |
| 113 | static inline void oswp4s(void* p, const long l) |
| 114 | { |
| 115 | const int32_t tmp = l; |
| 116 | memcpy(p, &tmp, 4); |
| 117 | } |
| 118 | |
| 119 | #endif |