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