Cthulhu  0.2.10
Cthulhu compiler collection
endian.h
Go to the documentation of this file.
1 // SPDX-License-Identifier: LGPL-3.0-only
2 
3 #pragma once
4 
5 #include <ctu_endian_api.h>
6 
7 #include "core/analyze.h"
8 #include "core/compiler.h"
9 
10 #include <stdint.h>
11 
15 
16 #if CT_OS_WINDOWS
17 # include "core/win32.h" // IWYU pragma: keep
18 # define CT_BIG_ENDIAN REG_DWORD_BIG_ENDIAN
19 # define CT_LITTLE_ENDIAN REG_DWORD_LITTLE_ENDIAN
20 # define CT_BYTE_ORDER REG_DWORD
21 #else
22 # if CT_OS_LINUX
23 # include <endian.h>
24 # elif CT_OS_APPLE
25 # include <machine/endian.h>
26 # endif
27 # define CT_BIG_ENDIAN __ORDER_BIG_ENDIAN__
28 # define CT_LITTLE_ENDIAN __ORDER_LITTLE_ENDIAN__
29 # define CT_BYTE_ORDER __BYTE_ORDER__
30 #endif
31 
38 
40 
42 typedef enum endian_t
43 {
44 #define ENDIAN(id, name, v) id = (v),
45 #include "endian.inc"
46 
49 
56 CT_ENDIAN_API uint16_t endian_swap16(uint16_t value);
57 
64 CT_ENDIAN_API uint32_t endian_swap32(uint32_t value);
65 
72 CT_ENDIAN_API uint64_t endian_swap64(uint64_t value);
73 
81 CT_ENDIAN_API uint16_t native_order16(uint16_t value, IN_DOMAIN(<, eEndianCount) endian_t order);
82 
90 CT_ENDIAN_API uint32_t native_order32(uint32_t value, IN_DOMAIN(<, eEndianCount) endian_t order);
91 
99 CT_ENDIAN_API uint64_t native_order64(uint64_t value, IN_DOMAIN(<, eEndianCount) endian_t order);
100 
102 
#define CT_NODISCARD
mark a function as returning a value that must be used
#define IN_DOMAIN(cmp, it)
annotate a parameter as being bounded by the expression of cmp and it
#define CT_CONSTFN
mark a function as const, has no side effects and always returns the same value for the same argument...
Definition: analyze.h:227
#define CT_BEGIN_API
Definition: compiler.h:129
#define CT_END_API
Definition: compiler.h:130
endian_t
endianess enum
Definition: endian.h:43
CT_NODISCARD CT_CONSTFN CT_ENDIAN_API uint16_t endian_swap16(uint16_t value)
swap the endianess of a 16-bit value
Definition: endian.c:13
CT_NODISCARD CT_CONSTFN CT_ENDIAN_API uint64_t endian_swap64(uint64_t value)
swap the endianess of a 64-bit value
Definition: endian.c:25
CT_NODISCARD CT_CONSTFN CT_ENDIAN_API uint64_t native_order64(uint64_t value, endian_t order)
convert a 64-bit value of a given endianess to the native endianess
Definition: endian.c:45
CT_NODISCARD CT_CONSTFN CT_ENDIAN_API uint32_t endian_swap32(uint32_t value)
swap the endianess of a 32-bit value
Definition: endian.c:19
CT_NODISCARD CT_CONSTFN CT_ENDIAN_API uint16_t native_order16(uint16_t value, endian_t order)
convert a 16-bit value of a given endianess to the native endianess
Definition: endian.c:31
CT_NODISCARD CT_CONSTFN CT_ENDIAN_API uint32_t native_order32(uint32_t value, endian_t order)
convert a 32-bit value of a given endianess to the native endianess
Definition: endian.c:38
@ eEndianCount
Definition: endian.h:47