| 1 |
/* SPDX-License-Identifier: LGPL-2.1-or-later */ |
| 2 |
#pragma once |
| 3 |
|
| 4 |
#include <endian.h> |
| 5 |
|
| 6 |
#include "basic-forward.h" |
| 7 |
|
| 8 |
#include "../fundamental/unaligned.h" /* IWYU pragma: export */ |
| 9 |
|
| 10 |
/* BE */ |
| 11 |
|
| 12 |
static inline uint16_t unaligned_read_be16(const void *_u) { |
| 13 |
const struct __attribute__((__packed__, __may_alias__)) { uint16_t x; } *u = _u; |
| 14 |
|
| 15 |
return be16toh(u->x); |
| 16 |
} |
| 17 |
|
| 18 |
static inline uint32_t unaligned_read_be32(const void *_u) { |
| 19 |
const struct __attribute__((__packed__, __may_alias__)) { uint32_t x; } *u = _u; |
| 20 |
|
| 21 |
return be32toh(u->x); |
| 22 |
} |
| 23 |
|
| 24 |
static inline uint64_t unaligned_read_be64(const void *_u) { |
| 25 |
const struct __attribute__((__packed__, __may_alias__)) { uint64_t x; } *u = _u; |
| 26 |
|
| 27 |
return be64toh(u->x); |
| 28 |
} |
| 29 |
|
| 30 |
static inline void unaligned_write_be16(void *_u, uint16_t a) { |
| 31 |
struct __attribute__((__packed__, __may_alias__)) { uint16_t x; } *u = _u; |
| 32 |
|
| 33 |
u->x = be16toh(a); |
| 34 |
} |
| 35 |
|
| 36 |
static inline void unaligned_write_be32(void *_u, uint32_t a) { |
| 37 |
struct __attribute__((__packed__, __may_alias__)) { uint32_t x; } *u = _u; |
| 38 |
|
| 39 |
u->x = be32toh(a); |
| 40 |
} |
| 41 |
|
| 42 |
static inline void unaligned_write_be64(void *_u, uint64_t a) { |
| 43 |
struct __attribute__((__packed__, __may_alias__)) { uint64_t x; } *u = _u; |
| 44 |
|
| 45 |
u->x = be64toh(a); |
| 46 |
} |
| 47 |
|
| 48 |
/* LE */ |
| 49 |
|
| 50 |
static inline uint16_t unaligned_read_le16(const void *_u) { |
| 51 |
const struct __attribute__((__packed__, __may_alias__)) { uint16_t x; } *u = _u; |
| 52 |
|
| 53 |
return le16toh(u->x); |
| 54 |
} |
| 55 |
|
| 56 |
static inline uint32_t unaligned_read_le32(const void *_u) { |
| 57 |
const struct __attribute__((__packed__, __may_alias__)) { uint32_t x; } *u = _u; |
| 58 |
|
| 59 |
return le32toh(u->x); |
| 60 |
} |
| 61 |
|
| 62 |
static inline uint64_t unaligned_read_le64(const void *_u) { |
| 63 |
const struct __attribute__((__packed__, __may_alias__)) { uint64_t x; } *u = _u; |
| 64 |
|
| 65 |
return le64toh(u->x); |
| 66 |
} |
| 67 |
|
| 68 |
static inline void unaligned_write_le16(void *_u, uint16_t a) { |
| 69 |
struct __attribute__((__packed__, __may_alias__)) { uint16_t x; } *u = _u; |
| 70 |
|
| 71 |
u->x = le16toh(a); |
| 72 |
} |
| 73 |
|
| 74 |
static inline void unaligned_write_le32(void *_u, uint32_t a) { |
| 75 |
struct __attribute__((__packed__, __may_alias__)) { uint32_t x; } *u = _u; |
| 76 |
|
| 77 |
u->x = le32toh(a); |
| 78 |
} |
| 79 |
|
| 80 |
static inline void unaligned_write_le64(void *_u, uint64_t a) { |
| 81 |
struct __attribute__((__packed__, __may_alias__)) { uint64_t x; } *u = _u; |
| 82 |
|
| 83 |
u->x = le64toh(a); |
| 84 |
} |