瀏覽代碼

Add 64-bit byte-swapping operations.

tags/v0.9.3
Michael Brown 18 年之前
父節點
當前提交
1ad72e0e79
共有 4 個文件被更改,包括 48 次插入7 次删除
  1. 38
    7
      src/arch/i386/include/bits/byteswap.h
  2. 4
    0
      src/include/big_bswap.h
  3. 2
    0
      src/include/byteswap.h
  4. 4
    0
      src/include/little_bswap.h

+ 38
- 7
src/arch/i386/include/bits/byteswap.h 查看文件

1
 #ifndef ETHERBOOT_BITS_BYTESWAP_H
1
 #ifndef ETHERBOOT_BITS_BYTESWAP_H
2
 #define ETHERBOOT_BITS_BYTESWAP_H
2
 #define ETHERBOOT_BITS_BYTESWAP_H
3
 
3
 
4
-static inline uint16_t __i386_bswap_16(uint16_t x)
4
+static inline __attribute__ ((always_inline)) uint16_t
5
+__i386_bswap_16(uint16_t x)
5
 {
6
 {
6
 	__asm__("xchgb %b0,%h0\n\t"
7
 	__asm__("xchgb %b0,%h0\n\t"
7
 		: "=q" (x)
8
 		: "=q" (x)
9
 	return x;
10
 	return x;
10
 }
11
 }
11
 
12
 
12
-static inline uint32_t __i386_bswap_32(uint32_t x)
13
+static inline __attribute__ ((always_inline)) uint32_t
14
+__i386_bswap_32(uint32_t x)
13
 {
15
 {
14
 	__asm__("xchgb %b0,%h0\n\t"
16
 	__asm__("xchgb %b0,%h0\n\t"
15
 		"rorl $16,%0\n\t"
17
 		"rorl $16,%0\n\t"
19
 	return x;
21
 	return x;
20
 }
22
 }
21
 
23
 
24
+static inline __attribute__ ((always_inline)) uint64_t
25
+__i386_bswap_64(uint64_t x)
26
+{
27
+	union {
28
+		uint64_t qword;
29
+		uint32_t dword[2]; 
30
+	} u;
31
+
32
+	u.qword = x;
33
+	u.dword[0] = __i386_bswap_32(u.dword[0]);
34
+	u.dword[1] = __i386_bswap_32(u.dword[1]);
35
+	__asm__("xchgl %0,%1"
36
+		: "=r" ( u.dword[0] ), "=r" ( u.dword[1] )
37
+		: "0" ( u.dword[0] ), "1" ( u.dword[1] ) );
38
+	return u.qword;
39
+}
22
 
40
 
23
 #define __bswap_constant_16(x) \
41
 #define __bswap_constant_16(x) \
24
 	((uint16_t)((((uint16_t)(x) & 0x00ff) << 8) | \
42
 	((uint16_t)((((uint16_t)(x) & 0x00ff) << 8) | \
25
-		(((uint16_t)(x) & 0xff00) >> 8)))
43
+		    (((uint16_t)(x) & 0xff00) >> 8)))
26
 
44
 
27
 #define __bswap_constant_32(x) \
45
 #define __bswap_constant_32(x) \
28
 	((uint32_t)((((uint32_t)(x) & 0x000000ffU) << 24) | \
46
 	((uint32_t)((((uint32_t)(x) & 0x000000ffU) << 24) | \
29
-		(((uint32_t)(x) & 0x0000ff00U) <<  8) | \
30
-		(((uint32_t)(x) & 0x00ff0000U) >>  8) | \
31
-		(((uint32_t)(x) & 0xff000000U) >> 24)))
47
+		    (((uint32_t)(x) & 0x0000ff00U) <<  8) | \
48
+		    (((uint32_t)(x) & 0x00ff0000U) >>  8) | \
49
+		    (((uint32_t)(x) & 0xff000000U) >> 24)))
50
+
51
+#define __bswap_constant_64(x) \
52
+	((uint64_t)((((uint64_t)(x) & 0x00000000000000ffULL) << 56) | \
53
+		    (((uint64_t)(x) & 0x000000000000ff00ULL) << 40) | \
54
+		    (((uint64_t)(x) & 0x0000000000ff0000ULL) << 24) | \
55
+		    (((uint64_t)(x) & 0x00000000ff000000ULL) <<  8) | \
56
+		    (((uint64_t)(x) & 0x000000ff00000000ULL) >>  8) | \
57
+		    (((uint64_t)(x) & 0x0000ff0000000000ULL) >> 24) | \
58
+		    (((uint64_t)(x) & 0x00ff000000000000ULL) >> 40) | \
59
+		    (((uint64_t)(x) & 0xff00000000000000ULL) >> 56)))
32
 
60
 
33
 #define __bswap_16(x) \
61
 #define __bswap_16(x) \
34
 	((uint16_t)(__builtin_constant_p(x) ? \
62
 	((uint16_t)(__builtin_constant_p(x) ? \
35
 	__bswap_constant_16(x) : \
63
 	__bswap_constant_16(x) : \
36
 	__i386_bswap_16(x)))
64
 	__i386_bswap_16(x)))
37
 
65
 
38
-
39
 #define __bswap_32(x) \
66
 #define __bswap_32(x) \
40
 	((uint32_t)(__builtin_constant_p(x) ? \
67
 	((uint32_t)(__builtin_constant_p(x) ? \
41
 	__bswap_constant_32(x) : \
68
 	__bswap_constant_32(x) : \
42
 	__i386_bswap_32(x)))
69
 	__i386_bswap_32(x)))
43
 
70
 
71
+#define __bswap_64(x) \
72
+	((uint64_t)(__builtin_constant_p(x) ? \
73
+	__bswap_constant_64(x) : \
74
+	__i386_bswap_64(x)))
44
 
75
 
45
 #endif /* ETHERBOOT_BITS_BYTESWAP_H */
76
 #endif /* ETHERBOOT_BITS_BYTESWAP_H */

+ 4
- 0
src/include/big_bswap.h 查看文件

5
 #define htonl(x) 	(x)
5
 #define htonl(x) 	(x)
6
 #define ntohs(x) 	(x)
6
 #define ntohs(x) 	(x)
7
 #define htons(x) 	(x)
7
 #define htons(x) 	(x)
8
+#define cpu_to_le64(x)	__bswap_64(x)
8
 #define cpu_to_le32(x)	__bswap_32(x)
9
 #define cpu_to_le32(x)	__bswap_32(x)
9
 #define cpu_to_le16(x)	__bswap_16(x)
10
 #define cpu_to_le16(x)	__bswap_16(x)
11
+#define cpu_to_be64(x)	(x)
10
 #define cpu_to_be32(x)	(x)
12
 #define cpu_to_be32(x)	(x)
11
 #define cpu_to_be16(x)	(x)
13
 #define cpu_to_be16(x)	(x)
14
+#define le64_to_cpu(x)	__bswap_64(x)
12
 #define le32_to_cpu(x)	__bswap_32(x)
15
 #define le32_to_cpu(x)	__bswap_32(x)
13
 #define le16_to_cpu(x)	__bswap_16(x)
16
 #define le16_to_cpu(x)	__bswap_16(x)
17
+#define be64_to_cpu(x)	(x)
14
 #define be32_to_cpu(x)	(x)
18
 #define be32_to_cpu(x)	(x)
15
 #define be16_to_cpu(x)	(x)
19
 #define be16_to_cpu(x)	(x)
16
 
20
 

+ 2
- 0
src/include/byteswap.h 查看文件

12
 #endif
12
 #endif
13
 
13
 
14
 /* Make routines available to all */
14
 /* Make routines available to all */
15
+#define swap64(x)	__bswap_64(x)
15
 #define swap32(x)	__bswap_32(x)
16
 #define swap32(x)	__bswap_32(x)
16
 #define swap16(x)	__bswap_16(x)
17
 #define swap16(x)	__bswap_16(x)
18
+#define bswap_64(x)	__bswap_64(x)
17
 #define bswap_32(x)	__bswap_32(x)
19
 #define bswap_32(x)	__bswap_32(x)
18
 #define bswap_16(x)	__bswap_16(x)
20
 #define bswap_16(x)	__bswap_16(x)
19
 	
21
 	

+ 4
- 0
src/include/little_bswap.h 查看文件

5
 #define htonl(x) 	__bswap_32(x)
5
 #define htonl(x) 	__bswap_32(x)
6
 #define ntohs(x) 	__bswap_16(x)
6
 #define ntohs(x) 	__bswap_16(x)
7
 #define htons(x) 	__bswap_16(x)
7
 #define htons(x) 	__bswap_16(x)
8
+#define cpu_to_le64(x)	(x)
8
 #define cpu_to_le32(x)	(x)
9
 #define cpu_to_le32(x)	(x)
9
 #define cpu_to_le16(x)	(x)
10
 #define cpu_to_le16(x)	(x)
11
+#define cpu_to_be64(x)	__bswap_64(x)
10
 #define cpu_to_be32(x)	__bswap_32(x)
12
 #define cpu_to_be32(x)	__bswap_32(x)
11
 #define cpu_to_be16(x)	__bswap_16(x)
13
 #define cpu_to_be16(x)	__bswap_16(x)
14
+#define le64_to_cpu(x)	(x)
12
 #define le32_to_cpu(x)	(x)
15
 #define le32_to_cpu(x)	(x)
13
 #define le16_to_cpu(x)	(x)
16
 #define le16_to_cpu(x)	(x)
17
+#define be64_to_cpu(x)	__bswap_64(x)
14
 #define be32_to_cpu(x)	__bswap_32(x)
18
 #define be32_to_cpu(x)	__bswap_32(x)
15
 #define be16_to_cpu(x)	__bswap_16(x)
19
 #define be16_to_cpu(x)	__bswap_16(x)
16
 
20
 

Loading…
取消
儲存