diff options
author | Kevin O'Connor <kevin@koconnor.net> | 2012-08-14 21:20:10 -0400 |
---|---|---|
committer | Kevin O'Connor <kevin@koconnor.net> | 2012-09-02 16:00:03 -0400 |
commit | b306459d2d9804e249cdded2f0967a74b685c684 (patch) | |
tree | 6f11498b5a7498d5c2c6a7c493ef69bd2de68730 /src/byteorder.h | |
parent | 51755c3b5ed9dcdfdef8cee56631d68642bde416 (diff) | |
download | seabios-b306459d2d9804e249cdded2f0967a74b685c684.tar.gz |
Use cpu_to_be32() (and related) instead of htonl (and related).
Unify the syntax for byte swab calls.
This also fixes a bug in coreboot due to the lack of a be64_to_cpu()
call.
Signed-off-by: Kevin O'Connor <kevin@koconnor.net>
Diffstat (limited to 'src/byteorder.h')
-rw-r--r-- | src/byteorder.h | 69 |
1 files changed, 69 insertions, 0 deletions
diff --git a/src/byteorder.h b/src/byteorder.h new file mode 100644 index 00000000..94e3a3bc --- /dev/null +++ b/src/byteorder.h @@ -0,0 +1,69 @@ +#ifndef __BYTEORDER_H +#define __BYTEORDER_H + +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) { + asm("bswapl %0" : "+r"(val)); + return val; +} +static inline u64 __swab64(u64 val) { + union u64_u32_u i, o; + i.val = val; + o.hi = __swab32(i.lo); + o.lo = __swab32(i.hi); + 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)) + +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 u32 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 u32 be64_to_cpu(u64 x) { + return swab64(x); +} + +#endif // byteorder.h |