ByteOrder.h revision 040dc2ee
1/*
2 * Copyright 2007, Haiku, Inc. All Rights Reserved.
3 * Distributed under the terms of the MIT License.
4 */
5#ifndef _BYTEORDER_H
6#define _BYTEORDER_H
7
8
9#include <BeBuild.h>
10#include <endian.h>
11#include <SupportDefs.h>
12#include <TypeConstants.h>
13	/* for convenience */
14
15
16/* swap directions */
17typedef enum {
18	B_SWAP_HOST_TO_LENDIAN,
19	B_SWAP_HOST_TO_BENDIAN,
20	B_SWAP_LENDIAN_TO_HOST,
21	B_SWAP_BENDIAN_TO_HOST,
22	B_SWAP_ALWAYS
23} swap_action;
24
25
26/* BSD/networking macros */
27#ifndef htonl
28#	define htonl(x) B_HOST_TO_BENDIAN_INT32(x)
29#	define ntohl(x) B_BENDIAN_TO_HOST_INT32(x)
30#	define htons(x) B_HOST_TO_BENDIAN_INT16(x)
31#	define ntohs(x) B_BENDIAN_TO_HOST_INT16(x)
32
33static inline uint16 __swap_int16(uint16 arg)
34{
35	return (arg >> 8) | (arg << 8);
36}
37#endif
38
39/* always swap macros */
40#define B_SWAP_DOUBLE(arg)   __swap_double(arg)
41#define B_SWAP_FLOAT(arg)    __swap_float(arg)
42#define B_SWAP_INT64(arg)    __swap_int64(arg)
43#define B_SWAP_INT32(arg)    __swap_int32(arg)
44#define B_SWAP_INT16(arg)    __swap_int16(arg)
45
46#if BYTE_ORDER == __LITTLE_ENDIAN
47/* Host is little endian */
48
49#define B_HOST_IS_LENDIAN 1
50#define B_HOST_IS_BENDIAN 0
51
52/* Host native to little endian */
53#define B_HOST_TO_LENDIAN_DOUBLE(arg)	(double)(arg)
54#define B_HOST_TO_LENDIAN_FLOAT(arg)	(float)(arg)
55#define B_HOST_TO_LENDIAN_INT64(arg)	(uint64)(arg)
56#define B_HOST_TO_LENDIAN_INT32(arg)	(uint32)(arg)
57#define B_HOST_TO_LENDIAN_INT16(arg)	(uint16)(arg)
58
59/* Little endian to host native */
60#define B_LENDIAN_TO_HOST_DOUBLE(arg)	(double)(arg)
61#define B_LENDIAN_TO_HOST_FLOAT(arg)	(float)(arg)
62#define B_LENDIAN_TO_HOST_INT64(arg)	(uint64)(arg)
63#define B_LENDIAN_TO_HOST_INT32(arg)	(uint32)(arg)
64#define B_LENDIAN_TO_HOST_INT16(arg)	(uint16)(arg)
65
66/* Host native to big endian */
67#define B_HOST_TO_BENDIAN_DOUBLE(arg)	__swap_double(arg)
68#define B_HOST_TO_BENDIAN_FLOAT(arg)	__swap_float(arg)
69#define B_HOST_TO_BENDIAN_INT64(arg)	__swap_int64(arg)
70#define B_HOST_TO_BENDIAN_INT32(arg)	__swap_int32(arg)
71#define B_HOST_TO_BENDIAN_INT16(arg)	__swap_int16(arg)
72
73/* Big endian to host native */
74#define B_BENDIAN_TO_HOST_DOUBLE(arg)	__swap_double(arg)
75#define B_BENDIAN_TO_HOST_FLOAT(arg)	__swap_float(arg)
76#define B_BENDIAN_TO_HOST_INT64(arg)	__swap_int64(arg)
77#define B_BENDIAN_TO_HOST_INT32(arg)	__swap_int32(arg)
78#define B_BENDIAN_TO_HOST_INT16(arg)	__swap_int16(arg)
79
80#else	/* BYTE_ORDER */
81/* Host is big endian */
82
83#define B_HOST_IS_LENDIAN 0
84#define B_HOST_IS_BENDIAN 1
85
86/* Host native to little endian */
87#define B_HOST_TO_LENDIAN_DOUBLE(arg)	__swap_double(arg)
88#define B_HOST_TO_LENDIAN_FLOAT(arg)	__swap_float(arg)
89#define B_HOST_TO_LENDIAN_INT64(arg)	__swap_int64(arg)
90#define B_HOST_TO_LENDIAN_INT32(arg)	__swap_int32(arg)
91#define B_HOST_TO_LENDIAN_INT16(arg)	__swap_int16(arg)
92
93/* Little endian to host native */
94#define B_LENDIAN_TO_HOST_DOUBLE(arg)	__swap_double(arg)
95#define B_LENDIAN_TO_HOST_FLOAT(arg)	__swap_float(arg)
96#define B_LENDIAN_TO_HOST_INT64(arg)	__swap_int64(arg)
97#define B_LENDIAN_TO_HOST_INT32(arg)	__swap_int32(arg)
98#define B_LENDIAN_TO_HOST_INT16(arg)	__swap_int16(arg)
99
100/* Host native to big endian */
101#define B_HOST_TO_BENDIAN_DOUBLE(arg)	(double)(arg)
102#define B_HOST_TO_BENDIAN_FLOAT(arg)	(float)(arg)
103#define B_HOST_TO_BENDIAN_INT64(arg)	(uint64)(arg)
104#define B_HOST_TO_BENDIAN_INT32(arg)	(uint32)(arg)
105#define B_HOST_TO_BENDIAN_INT16(arg)	(uint16)(arg)
106
107/* Big endian to host native */
108#define B_BENDIAN_TO_HOST_DOUBLE(arg)	(double)(arg)
109#define B_BENDIAN_TO_HOST_FLOAT(arg)	(float)(arg)
110#define B_BENDIAN_TO_HOST_INT64(arg)	(uint64)(arg)
111#define B_BENDIAN_TO_HOST_INT32(arg)	(uint32)(arg)
112#define B_BENDIAN_TO_HOST_INT16(arg)	(uint16)(arg)
113
114#endif	/* BYTE_ORDER */
115
116
117#ifdef __cplusplus
118extern "C" {
119#endif
120
121extern status_t swap_data(type_code type, void *data, size_t length,
122	swap_action action);
123extern bool is_type_swapped(type_code type);
124
125/* Private implementations */
126extern double __swap_double(double arg);
127extern float  __swap_float(float arg);
128#if __GNUC__ >= 4
129#define __swap_int64(arg)	__builtin_bswap64(arg)
130#define __swap_int32(arg)	__builtin_bswap32(arg)
131#else
132extern uint64 __swap_int64(uint64 arg);
133extern uint32 __swap_int32(uint32 arg);
134#endif
135
136#ifdef __cplusplus
137}
138#endif
139
140#endif	/* _BYTEORDER_H */
141