aboutsummaryrefslogtreecommitdiff
path: root/src/byteorder.h
blob: f0dffa3ff84ee353b66e8f52d9fadfe61789457d (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
#ifndef __BYTEORDER_H
#define __BYTEORDER_H

#include "autoconf.h"
#include "types.h" // u32

#if CONFIG_X86
#define TARGET_LITTLE_ENDIAN
#elif CONFIG_PARISC
#define TARGET_BIG_ENDIAN
#else
#error "unknown endianess"
#endif

static inline u16 __swab16_constant(u16 val) {
    return (val<<8) | (val>>8);
}
static inline u32 __swab32_constant(u32 val) {
    return (val<<24) | ((val&0xff00)<<8) | ((val&0xff0000)>>8) | (val>>24);
}
static inline u64 __swab64_constant(u64 val) {
    return ((u64)__swab32_constant(val) << 32) | __swab32_constant(val>>32);
}
static inline u32 __swab32(u32 val) {
#if CONFIG_X86
    asm("bswapl %0" : "+r"(val));
#elif CONFIG_PARISC
    unsigned int temp;
    asm("shd %0, %0, 16, %1\n\t"	/* shift abcdabcd -> cdab */
        "dep %1, 15, 8, %1\n\t"		/* deposit cdab -> cbab */
        "shd %0, %1, 8, %0"		/* shift abcdcbab -> dcba */
        : "=r" (val), "=&r" (temp)
        : "0" (val));
#else
    #error "unknown arch"
#endif
    return val;
}
static inline u64 __swab64(u64 val) {
    union u64_u32_u i, o;
    i.val = val;
    o.lo = __swab32(i.hi);
    o.hi = __swab32(i.lo);
    return o.val;
}

#define swab16(x) __swab16_constant(x)
#define swab32(x) (__builtin_constant_p((u32)(x)) \
                   ? __swab32_constant(x) : __swab32(x))
#define swab64(x) (__builtin_constant_p((u64)(x)) \
                   ? __swab64_constant(x) : __swab64(x))

#if defined(TARGET_BIG_ENDIAN)
static inline u16 cpu_to_le16(u16 x) {
    return swab16(x);
}
static inline u32 cpu_to_le32(u32 x) {
    return swab32(x);
}
static inline u64 cpu_to_le64(u64 x) {
    return swab64(x);
}
static inline u16 le16_to_cpu(u16 x) {
    return swab16(x);
}
static inline u32 le32_to_cpu(u32 x) {
    return swab32(x);
}
static inline u64 le64_to_cpu(u64 x) {
    return swab64(x);
}

static inline u16 cpu_to_be16(u16 x) {
    return x;
}
static inline u32 cpu_to_be32(u32 x) {
    return x;
}
static inline u64 cpu_to_be64(u64 x) {
    return x;
}
static inline u16 be16_to_cpu(u16 x) {
    return x;
}
static inline u32 be32_to_cpu(u32 x) {
    return x;
}
static inline u64 be64_to_cpu(u64 x) {
    return x;
}

static inline void convert_to_le32(u32 *script, unsigned long bytes)
{
    while (bytes > 0) {
	*script = cpu_to_le32(*script);
	script++;
	bytes -= sizeof(u32);
    }
}

#else /* defined(TARGET_LITTLE_ENDIAN) */
static inline u16 cpu_to_le16(u16 x) {
    return x;
}
static inline u32 cpu_to_le32(u32 x) {
    return x;
}
static inline u64 cpu_to_le64(u64 x) {
    return x;
}
static inline u16 le16_to_cpu(u16 x) {
    return x;
}
static inline u32 le32_to_cpu(u32 x) {
    return x;
}
static inline u64 le64_to_cpu(u64 x) {
    return x;
}

static inline u16 cpu_to_be16(u16 x) {
    return swab16(x);
}
static inline u32 cpu_to_be32(u32 x) {
    return swab32(x);
}
static inline u64 cpu_to_be64(u64 x) {
    return swab64(x);
}
static inline u16 be16_to_cpu(u16 x) {
    return swab16(x);
}
static inline u32 be32_to_cpu(u32 x) {
    return swab32(x);
}
static inline u64 be64_to_cpu(u64 x) {
    return swab64(x);
}

static inline void convert_to_le32(u32 *script, unsigned long bytes)
{
}
#endif

#endif // byteorder.h