firmware
IEM Firmware Documentation
Loading...
Searching...
No Matches
can_codec_utils.h
Go to the documentation of this file.
1
8
9#ifndef CAN_CODEC_UTILS_H
10#define CAN_CODEC_UTILS_H
11
12#include <stdint.h>
13#include <stdbool.h>
14#include <stddef.h>
15
16// This macro is defined by GCC but not clang
17#ifndef __always_inline
18 #define __always_inline __inline__ __attribute__((__always_inline__))
19#endif
20
21// NOLINTBEGIN(cppcoreguidelines-avoid-magic-numbers, readability-magic-numbers, readability-identifier-length)
22
29static __always_inline uint8_t swap_byte_order_8(uint8_t x) {
30 return x;
31}
32
39static __always_inline uint16_t swap_byte_order_16(uint16_t x) {
40 return (x & 0xFF00) >> 8 | (x & 0x00FF) << 8;
41}
42
49static __always_inline uint32_t swap_byte_order_32(uint32_t x) {
50 x = (x & 0xFFFF0000) >> 16 | (x & 0x0000FFFF) << 16;
51 return (x & 0xFF00FF00) >> 8 | (x & 0x00FF00FF) << 8;
52}
53
60static __always_inline uint32_t swap_byte_order_ftou(float x) {
61 uint32_t ret = *((uint32_t*)&x);
62 ret = (ret & 0xFFFF0000) >> 16 | (ret & 0x0000FFFF) << 16;
63 return (ret & 0xFF00FF00) >> 8 | (ret & 0x00FF00FF) << 8;
64}
65
72static __always_inline float swap_byte_order_utof(uint32_t x) {
73 x = (x & 0xFFFF0000) >> 16 | (x & 0x0000FFFF) << 16;
74 x = (x & 0xFF00FF00) >> 8 | (x & 0x00FF00FF) << 8;
75 return *((float*)&x);
76}
77
84static __always_inline uint64_t swap_byte_order_64(uint64_t x) {
85 x = (x & 0xFFFFFFFF00000000ULL) >> 32 | (x & 0x00000000FFFFFFFFULL) << 32;
86 x = (x & 0xFFFF0000FFFF0000ULL) >> 16 | (x & 0x0000FFFF0000FFFFULL) << 16;
87 return (x & 0xFF00FF00FF00FF00ULL) >> 8 | (x & 0x00FF00FF00FF00FFULL) << 8;
88}
89
96static __always_inline uint64_t swap_byte_order_dtou(double x) {
97 uint64_t ret = *((uint64_t*)&x);
98 ret = (ret & 0xFFFFFFFF00000000ULL) >> 32 | (ret & 0x00000000FFFFFFFFULL) << 32;
99 ret = (ret & 0xFFFF0000FFFF0000ULL) >> 16 | (ret & 0x0000FFFF0000FFFFULL) << 16;
100 return (ret & 0xFF00FF00FF00FF00ULL) >> 8 | (ret & 0x00FF00FF00FF00FFULL) << 8;
101}
102
109static __always_inline double swap_byte_order_utod(uint64_t x) {
110 x = (x & 0xFFFFFFFF00000000ULL) >> 32 | (x & 0x00000000FFFFFFFFULL) << 32;
111 x = (x & 0xFFFF0000FFFF0000ULL) >> 16 | (x & 0x0000FFFF0000FFFFULL) << 16;
112 x = (x & 0xFF00FF00FF00FF00ULL) >> 8 | (x & 0x00FF00FF00FF00FFULL) << 8;
113 return *((double*)&x);
114}
115
116
117// NOLINTEND(cppcoreguidelines-avoid-magic-numbers, readability-magic-numbers, readability-identifier-length)
118
119#endif /* CAN_CODEC_UTILS_H */
static __always_inline uint16_t swap_byte_order_16(uint16_t x)
Swaps the byte order of a uint16_t.
Definition can_codec_utils.h:39
static __always_inline uint32_t swap_byte_order_32(uint32_t x)
Swaps the byte order of a uint32_t.
Definition can_codec_utils.h:49
static __always_inline uint64_t swap_byte_order_dtou(double x)
Swaps the byte order of a double and returns its big-endian representation as a uint64_t.
Definition can_codec_utils.h:96
static __always_inline uint64_t swap_byte_order_64(uint64_t x)
Swaps the byte order of a uint64_t.
Definition can_codec_utils.h:84
#define __always_inline
Definition can_codec_utils.h:18
static __always_inline float swap_byte_order_utof(uint32_t x)
Swaps the byte order of a big-endian float represented as a uint32_t.
Definition can_codec_utils.h:72
static __always_inline uint8_t swap_byte_order_8(uint8_t x)
Swaps the byte order of a uint8_t.
Definition can_codec_utils.h:29
static __always_inline double swap_byte_order_utod(uint64_t x)
Swaps the byte order of a big-endian double represented as a uint64_t.
Definition can_codec_utils.h:109
static __always_inline uint32_t swap_byte_order_ftou(float x)
Swaps the byte order of a float and returns its big-endian representation as a uint32_t.
Definition can_codec_utils.h:60