Skip to contentSkip to conversation

add endian support to mini-os

PATCH In review Matthew Fioravante 2 participants 4 messages Sep 17, 2012 – Sep 18, 2012

lore
Share & tools
lore
#0 lore raw

PATCH 3/8 add endian support to mini-os

This patch addes byte swapping macros and endian support to mini-os

Signed off by: Matthew Fioravante [email protected]

(quoted diff) +0 -0

b/extras/mini-os/include/byteorder.h

extras/mini-os/include/byteorder.h new file +36 -0
@@ -0,0 +1,36 @@
+#ifndef MINIOS_BYTEORDER_H
+#define MINIOS_BYTEORDER_H
+
+#include <mini-os/byteswap.h>
+#include <mini-os/endian.h>
+
+#if __BYTE_ORDER == __LITTLE_ENDIAN
+#define be16_to_cpu(v) bswap_16(v)
+#define be32_to_cpu(v) bswap_32(v)
+#define be64_to_cpu(v) bswap_64(v)
+
+#define le16_to_cpu(v) (v)
+#define le32_to_cpu(v) (v)
+#define le64_to_cpu(v) (v)
+
+#else /*__BIG_ENDIAN*/
+#define be16_to_cpu(v) (v)
+#define be32_to_cpu(v) (v)
+#define be64_to_cpu(v) (v)
+
+#define le16_to_cpu(v) bswap_16(v)
+#define le32_to_cpu(v) bswap_32(v)
+#define le64_to_cpu(v) bswap_64(v)
+
+#endif
+
+#define cpu_to_be16(v) be16_to_cpu(v)
+#define cpu_to_be32(v) be32_to_cpu(v)
+#define cpu_to_be64(v) be64_to_cpu(v)
+
+#define cpu_to_le16(v) le16_to_cpu(v)
+#define cpu_to_le32(v) le32_to_cpu(v)
+#define cpu_to_le64(v) le64_to_cpu(v)
+
+
+#endif
(quoted diff) +0 -0

b/extras/mini-os/include/byteswap.h

extras/mini-os/include/byteswap.h +3 -25
@@ -4,30 +4,36 @@
 /* Unfortunately not provided by newlib.  */
 
 #include <mini-os/types.h>
-static inline uint16_t bswap_16(uint16_t x)
-{
-    return
-    ((((x) & 0xff00) >> 8) | (((x) & 0xff) << 8));
-}
-
-static inline uint32_t bswap_32(uint32_t x)
-{
-    return
-    ((((x) & 0xff000000) >> 24) | (((x) & 0x00ff0000) >>  8) |
-     (((x) & 0x0000ff00) <<  8) | (((x) & 0x000000ff) << 24));
-}
-
-static inline uint64_t bswap_64(uint64_t x)
-{
-    return
-    ((((x) & 0xff00000000000000ULL) >> 56) |
-     (((x) & 0x00ff000000000000ULL) >> 40) |
-     (((x) & 0x0000ff0000000000ULL) >> 24) |
-     (((x) & 0x000000ff00000000ULL) >>  8) |
-     (((x) & 0x00000000ff000000ULL) <<  8) |
-     (((x) & 0x0000000000ff0000ULL) << 24) |
-     (((x) & 0x000000000000ff00ULL) << 40) |
-     (((x) & 0x00000000000000ffULL) << 56));
-}
+
+#define bswap_16(x) ((uint16_t)(                         \
+             (((uint16_t)(x) & (uint16_t)0x00ffU) << 8)

| \
+ (((uint16_t)(x) & (uint16_t)0xff00U) >> 8)))
+
+/* Use gcc optimized versions if they exist */ +#if __GNUC__ > 4 || (__GNUC__ == 4 && __GNUC_MINOR__ >= 3) +#define bswap_32(v) __builtin_bswap32(v)
+#define bswap_64(v) __builtin_bswap64(v)
+#else
+
+#define bswap_32(x) ((uint32_t)( \
+ (((uint32_t)(x) & (uint32_t)0x000000ffUL) << 24)
| \
+ (((uint32_t)(x) & (uint32_t)0x0000ff00UL) << 8)
| \
+ (((uint32_t)(x) & (uint32_t)0x00ff0000UL) >> 8)
| \
+ (((uint32_t)(x) & (uint32_t)0xff000000UL) >> 24)))
+
+#define bswap_64(x) ((uint64_t)( \
+ (((uint64_t)(x) & (uint64_t)0x00000000000000ffULL) << 56)
| \
+ (((uint64_t)(x) & (uint64_t)0x000000000000ff00ULL) << 40)
| \
+ (((uint64_t)(x) & (uint64_t)0x0000000000ff0000ULL) << 24)
| \
+ (((uint64_t)(x) & (uint64_t)0x00000000ff000000ULL) << 8)
| \
+ (((uint64_t)(x) & (uint64_t)0x000000ff00000000ULL) >> 8)
| \
+ (((uint64_t)(x) & (uint64_t)0x0000ff0000000000ULL) >> 24)
| \
+ (((uint64_t)(x) & (uint64_t)0x00ff000000000000ULL) >> 40)
| \
+ (((uint64_t)(x) & (uint64_t)0xff00000000000000ULL) >> 56)))
+
+#endif
+
+
+

#endif /* _BYTESWAP_H */

(quoted diff) +0 -0

b/extras/mini-os/include/endian.h

extras/mini-os/include/endian.h new file +15 -0
@@ -0,0 +1,15 @@
+#ifndef    _ENDIAN_H_
+#define    _ENDIAN_H_
+
+#define    __LITTLE_ENDIAN    1234
+#define    __BIG_ENDIAN    4321
+#define    __PDP_ENDIAN    3412
+
+#define ARCH_ENDIAN_H
+/* This will define __BYTE_ORDER for the current arch */
+#include <arch_endian.h>
+#undef ARCH_ENDIAN_H
+
+#include <arch_wordsize.h>
+
+#endif    /* endian.h */
(quoted diff) +0 -0

b/extras/mini-os/include/ia64/arch_endian.h

extras/mini-os/include/ia64/arch_endian.h new file +7 -0
@@ -0,0 +1,7 @@
+#ifndef    ARCH_ENDIAN_H
+#error "Do not include arch_endian by itself, include endian.h"
+#else
+
+#define __BYTE_ORDER __LITTLE_ENDIAN
+
+#endif
(quoted diff) +0 -0

b/extras/mini-os/include/ia64/arch_wordsize.h

extras/mini-os/include/ia64/arch_wordsize.h new file +1 -0
@@ -0,0 +1 @@
+#define __WORDSIZE 64
(quoted diff) +0 -0

b/extras/mini-os/include/x86/arch_endian.h

extras/mini-os/include/x86/arch_endian.h new file +7 -0
@@ -0,0 +1,7 @@
+#ifndef    ARCH_ENDIAN_H
+#error "Do not include arch_endian by itself, include endian.h"
+#else
+
+#define __BYTE_ORDER __LITTLE_ENDIAN
+
+#endif
(quoted diff) +0 -0

b/extras/mini-os/include/x86/x86_32/arch_wordsize.h

extras/mini-os/include/x86/x86_32/arch_wordsize.h new file +1 -0
@@ -0,0 +1 @@
+#define __WORDSIZE 32
(quoted diff) +0 -0

b/extras/mini-os/include/x86/x86_64/arch_wordsize.h

extras/mini-os/include/x86/x86_64/arch_wordsize.h new file +2 -0
@@ -0,0 +1,2 @@
+#define __WORDSIZE 64
+#define __WORDSIZE_COMPAT32 1

_______________________________________________
Xen-devel mailing list
[email protected]
http://lists.xen.org/xen-devel

#1 lore raw

extras/mini-os/include/byteorder.h

Quoted 4 lines · Matthew Fioravante, le Mon 17 Sep 2012 17:57:11 -0400, a écrit :
extras/mini-os/include/byteorder.h new file +0 -0

Ok.

Quoted 10 lines
extras/mini-os/include/byteorder.h new file +2 -6
-static inline uint16_t bswap_16(uint16_t x)
-{
-    return
-    ((((x) & 0xff00) >> 8) | (((x) & 0xff) << 8));
-}
-
+#define bswap_16(x) ((uint16_t)(                         \
+             (((uint16_t)(x) & (uint16_t)0x00ffU) << 8)

| \
+ (((uint16_t)(x) & (uint16_t)0xff00U) >> 8)))

Why replacing the inline with a macro? AIUI, inlines provide the same efficiency while providing some minimal type checking (and safety against things like bswap_16(x++).

Review

Quoted 6 lines
(quoted diff) +0 -0
(quoted diff) +0 -0
(quoted diff) +0 -0
(quoted diff) +0 -0
(quoted diff) +0 -0
(quoted diff) +0 -0

Ok.

Samuel

#2 lore raw
Quoted 7 lines · Samuel Thibault

extras/mini-os/include/byteorder.h

Quoted 4 lines · Matthew Fioravante, le Mon 17 Sep 2012 17:57:11 -0400, a écrit :
extras/mini-os/include/byteorder.h new file +0 -0

Ok.

Quoted 13 lines
Quoted 10 lines
extras/mini-os/include/byteorder.h new file +2 -6
-static inline uint16_t bswap_16(uint16_t x)
-{
-    return
-    ((((x) & 0xff00) >> 8) | (((x) & 0xff) << 8));
-}
-
+#define bswap_16(x) ((uint16_t)(                         \
+             (((uint16_t)(x) & (uint16_t)0x00ffU) << 8)

| \
+ (((uint16_t)(x) & (uint16_t)0xff00U) >> 8)))

Why replacing the inline with a macro? AIUI, inlines provide the same efficiency while providing some minimal type checking (and safety against things like bswap_16(x++).

With a macro you can do this:

const uint16_t foo = bswap_16(4000);

Not possible with inline. From what I've seen in other system heads (linux in particular), they tend to be implemented as macros or compiler builtins.

Quoted 10 lines

Review

Quoted 7 lines
(quoted diff) +0 -0
(quoted diff) +0 -0
(quoted diff) +0 -0
(quoted diff) +0 -0
(quoted diff) +0 -0
(quoted diff) +0 -0

Ok.

Samuel

_______________________________________________
Xen-devel mailing list
[email protected]
http://lists.xen.org/xen-devel