summaryrefslogtreecommitdiff
path: root/com32/include/byteswap.h
blob: 5ff2cd6935a3b7a4104f26296d2eabb68b67d43b (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
#ifndef _BYTESWAP_H
#define _BYTESWAP_H

#include <stdint.h>
#include <klibc/endian.h>
#include <klibc/compiler.h>

/* This assumes an i386 platform */

#define __bswap_16_macro(v) ((uint16_t)		  			\
			     (((uint16_t)(v) << 8) | 			\
			      ((uint16_t)(v) >> 8)))

static inline __constfunc uint16_t __bswap_16(uint16_t v)
{
    return __bswap_16_macro(v);
}

#define bswap_16(x) (__builtin_constant_p(x) ?				\
			__bswap_16_macro(x) : __bswap_16(x))

#define __bswap_32_macro(v) ((uint32_t)					\
			     ((((uint32_t)(v) & 0x000000ff) << 24) |	\
			      (((uint32_t)(v) & 0x0000ff00) << 8)  |	\
			     (((uint32_t)(v) & 0x00ff0000) >> 8)  |	\
			      (((uint32_t)(v) & 0xff000000) >> 24)))

static inline __constfunc uint32_t __bswap_32(uint32_t v)
{
#if defined(__x86_64__)
    asm("bswap %0" : "+r" (v));
#elif defined(__i386__)
    asm("xchgb %h0,%b0 ; roll $16,%0 ; xchgb %h0,%b0"
	: "+q" (v));
#else
    v = __bswap_32_macro(v);
#endif
    return v;
}

#define bswap_32(x) (__builtin_constant_p(x) ?				\
			 __bswap_32_macro(x) : __bswap_32(x))


#define __bswap_64_macro(v) ((uint64_t)					\
    (((uint64_t)__bswap_32_macro((uint32_t)(v)) << 32) |		\
     (__bswap_32_macro((uint32_t)((uint64_t)(v) >> 32)))))

static inline __constfunc uint64_t __bswap_64(uint64_t v)
{
#if defined(__x86_64__)
    asm("bswap %0" : "+r" (v));
#else
    v = ((uint64_t)__bswap_32(v) << 32) | __bswap_32(v >> 32);
#endif
    return v;
}

#define bswap_64(x) (__builtin_constant_p(x) ? 				\
			__bswap_64_macro(x) :  __bswap_64(x))

/* This is generic */

#if __BYTE_ORDER == __LITTLE_ENDIAN

#define be16_to_cpu(x) bswap_16(x)
#define cpu_to_be16(x) bswap_16(x)
#define be32_to_cpu(x) bswap_32(x)
#define cpu_to_be32(x) bswap_32(x)
#define be64_to_cpu(x) bswap_64(x)
#define cpu_to_be64(x) bswap_64(x)

#define le16_to_cpu(x) (x)
#define cpu_to_le16(x) (x)
#define le32_to_cpu(x) (x)
#define cpu_to_le32(x) (x)
#define le64_to_cpu(x) (x)
#define cpu_to_le64(x) (x)

#elif __BYTE_ORDER == __BIG_ENDIAN

#define le16_to_cpu(x) bswap_16(x)
#define cpu_to_le16(x) bswap_16(x)
#define le32_to_cpu(x) bswap_32(x)
#define cpu_to_le32(x) bswap_32(x)
#define le64_to_cpu(x) bswap_64(x)
#define cpu_to_le64(x) bswap_64(x)

#define be16_to_cpu(x) (x)
#define cpu_to_be16(x) (x)
#define be32_to_cpu(x) (x)
#define cpu_to_be32(x) (x)
#define be64_to_cpu(x) (x)
#define cpu_to_be64(x) (x)

#else

#error "Unknown byte order!"

#endif

typedef struct { uint16_t x; } __attribute__((packed)) __ua_uint16_t;
typedef struct { uint32_t x; } __attribute__((packed)) __ua_uint32_t;
typedef struct { uint64_t x; } __attribute__((packed)) __ua_uint64_t;

/* These are guaranteed to support unaligned accesses */
static inline uint16_t get_le16(const uint16_t *p)
{
    const __ua_uint16_t *up = (const __ua_uint16_t *)p;
    return le16_to_cpu(up->x);
}

static inline uint32_t get_le32(const uint32_t *p)
{
    const __ua_uint32_t *up = (const __ua_uint32_t *)p;
    return le32_to_cpu(up->x);
}    

static inline uint64_t get_le64(const uint64_t *p)
{
    const __ua_uint64_t *up = (const __ua_uint64_t *)p;
    return le64_to_cpu(up->x);
}

static inline uint16_t get_be16(const uint16_t *p)
{
    const __ua_uint16_t *up = (const __ua_uint16_t *)p;
    return be16_to_cpu(up->x);
}

static inline uint32_t get_be32(const uint32_t *p)
{
    const __ua_uint32_t *up = (const __ua_uint32_t *)p;
    return be32_to_cpu(up->x);
}    

static inline uint64_t get_be64(const uint64_t *p)
{
    const __ua_uint64_t *up = (const __ua_uint64_t *)p;
    return be64_to_cpu(up->x);
}

static inline void put_le16(uint16_t v, uint16_t *p)
{
    __ua_uint16_t *up = (__ua_uint16_t *)p;
    up->x = cpu_to_le16(v);
}

static inline void put_le32(uint32_t v, uint32_t *p)
{
    __ua_uint32_t *up = (__ua_uint32_t *)p;
    up->x = cpu_to_le32(v);
}

static inline void put_le64(uint64_t v, uint64_t *p)
{
    __ua_uint64_t *up = (__ua_uint64_t *)p;
    up->x = cpu_to_le64(v);
}

static inline void put_be16(uint16_t v, uint16_t *p)
{
    __ua_uint16_t *up = (__ua_uint16_t *)p;
    up->x = cpu_to_be16(v);
}

static inline void put_be32(uint32_t v, uint32_t *p)
{
    __ua_uint32_t *up = (__ua_uint32_t *)p;
    up->x = cpu_to_be32(v);
}

static inline void put_be64(uint64_t v, uint64_t *p)
{
    __ua_uint64_t *up = (__ua_uint64_t *)p;
    up->x = cpu_to_be64(v);
}

#endif /* _BYTESWAP_H */