SyterKit 0.4.0.x
SyterKit is a bare-metal framework
Loading...
Searching...
No Matches
byteorder.h
Go to the documentation of this file.
1#ifndef __BYTEORDER_H__
2#define __BYTEORDER_H__
3
4#ifdef __cplusplus
5extern "C" {
6#endif
7
8#include <types.h>
9#include <endian.h>
10
11static inline u16_t __swab16(u16_t x) {
12 return ((x << 8) | (x >> 8));
13}
14
15static inline u32_t __swab32(u32_t x) {
16 return ((x << 24) | (x >> 24) | ((x & (u32_t) 0x0000ff00UL) << 8) | ((x & (u32_t) 0x00ff0000UL) >> 8));
17}
18
19static inline u64_t __swab64(u64_t x) {
20 return ((x << 56) | (x >> 56) | ((x & (u64_t) 0x000000000000ff00ULL) << 40) | ((x & (u64_t) 0x0000000000ff0000ULL) << 24) | ((x & (u64_t) 0x00000000ff000000ULL) << 8) |
21 ((x & (u64_t) 0x000000ff00000000ULL) >> 8) | ((x & (u64_t) 0x0000ff0000000000ULL) >> 24) | ((x & (u64_t) 0x00ff000000000000ULL) >> 40));
22}
23
24/*
25 * swap bytes bizarrely.
26 * swahw32 - swap 16-bit half-words in a 32-bit word
27 */
28static inline u32_t __swahw32(u32_t x) {
29 return (((x & (u32_t) 0x0000ffffUL) << 16) | ((x & (u32_t) 0xffff0000UL) >> 16));
30}
31
32/*
33 * swap bytes bizarrely.
34 * swahb32 - swap 8-bit halves of each 16-bit half-word in a 32-bit word
35 */
36static inline u32_t __swahb32(u32_t x) {
37 return (((x & (u32_t) 0x00ff00ffUL) << 8) | ((x & (u32_t) 0xff00ff00UL) >> 8));
38}
39
40#if (BYTE_ORDER == BIG_ENDIAN)
41#define cpu_to_le64(x) (__swab64((u64_t) (x)))
42#define le64_to_cpu(x) (__swab64((u64_t) (x)))
43#define cpu_to_le32(x) (__swab32((u32_t) (x)))
44#define le32_to_cpu(x) (__swab32((u32_t) (x)))
45#define cpu_to_le16(x) (__swab16((u16_t) (x)))
46#define le16_to_cpu(x) (__swab16((u16_t) (x)))
47#define cpu_to_be64(x) ((u64_t) (x))
48#define be64_to_cpu(x) ((u64_t) (x))
49#define cpu_to_be32(x) ((u32_t) (x))
50#define be32_to_cpu(x) ((u32_t) (x))
51#define cpu_to_be16(x) ((u16_t) (x))
52#define be16_to_cpu(x) ((u16_t) (x))
53#else
54#define cpu_to_le64(x) ((u64_t) (x))
55#define le64_to_cpu(x) ((u64_t) (x))
56#define cpu_to_le32(x) ((u32_t) (x))
57#define le32_to_cpu(x) ((u32_t) (x))
58#define cpu_to_le16(x) ((u16_t) (x))
59#define le16_to_cpu(x) ((u16_t) (x))
60#define cpu_to_be64(x) (__swab64((u64_t) (x)))
61#define be64_to_cpu(x) (__swab64((u64_t) (x)))
62#define cpu_to_be32(x) (__swab32((u32_t) (x)))
63#define be32_to_cpu(x) (__swab32((u32_t) (x)))
64#define cpu_to_be16(x) (__swab16((u16_t) (x)))
65#define be16_to_cpu(x) (__swab16((u16_t) (x)))
66#endif
67
68#ifdef __cplusplus
69}
70#endif
71
72#endif /* __BYTEORDER_H__ */
static u16_t __swab16(u16_t x)
Definition byteorder.h:6
static u64_t __swab64(u64_t x)
Definition byteorder.h:14
static u32_t __swahb32(u32_t x)
Definition byteorder.h:31
static u32_t __swab32(u32_t x)
Definition byteorder.h:10
static u32_t __swahw32(u32_t x)
Definition byteorder.h:23
unsigned int u32_t
Definition types.h:11
unsigned long long u64_t
Definition types.h:14
unsigned short u16_t
Definition types.h:8