aboutsummaryrefslogtreecommitdiffstats
path: root/com32/include/byteswap.h
blob: b7eeeb4039c3b38799479b2b0fbc3e87590793dc (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
#ifndef _BYTESWAP_H
#define _BYTESWAP_H

/* COM32 will be running on an i386 platform */

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

#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 __SIZEOF_POINTER__ == 4
    asm("xchgb %h0,%b0 ; roll $16,%0 ; xchgb %h0,%b0"
	: "+q" (v));
#elif __SIZEOF_POINTER__ == 8
    asm("bswap	%0"
	: "=r" (v)
	: "0" (v));
#else
#error "unable to build for architecture"
#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)
{
    return ((uint64_t)__bswap_32(v) << 32) | __bswap_32(v >> 32);
}

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

#endif /* byteswap.h */