xref: /haiku/headers/os/support/ByteOrder.h (revision f2b4344867e97c3f4e742a1b4a15e6879644601a)
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 */
17 typedef 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 #endif
33 
34 /* always swap macros */
35 #define B_SWAP_DOUBLE(arg)   __swap_double(arg)
36 #define B_SWAP_FLOAT(arg)    __swap_float(arg)
37 #define B_SWAP_INT64(arg)    __swap_int64(arg)
38 #define B_SWAP_INT32(arg)    __swap_int32(arg)
39 #define B_SWAP_INT16(arg)    __swap_int16(arg)
40 
41 #if BYTE_ORDER == __LITTLE_ENDIAN
42 /* Host is little endian */
43 
44 #define B_HOST_IS_LENDIAN 1
45 #define B_HOST_IS_BENDIAN 0
46 
47 /* Host native to little endian */
48 #define B_HOST_TO_LENDIAN_DOUBLE(arg)	(double)(arg)
49 #define B_HOST_TO_LENDIAN_FLOAT(arg)	(float)(arg)
50 #define B_HOST_TO_LENDIAN_INT64(arg)	(uint64)(arg)
51 #define B_HOST_TO_LENDIAN_INT32(arg)	(uint32)(arg)
52 #define B_HOST_TO_LENDIAN_INT16(arg)	(uint16)(arg)
53 
54 /* Little endian to host native */
55 #define B_LENDIAN_TO_HOST_DOUBLE(arg)	(double)(arg)
56 #define B_LENDIAN_TO_HOST_FLOAT(arg)	(float)(arg)
57 #define B_LENDIAN_TO_HOST_INT64(arg)	(uint64)(arg)
58 #define B_LENDIAN_TO_HOST_INT32(arg)	(uint32)(arg)
59 #define B_LENDIAN_TO_HOST_INT16(arg)	(uint16)(arg)
60 
61 /* Host native to big endian */
62 #define B_HOST_TO_BENDIAN_DOUBLE(arg)	__swap_double(arg)
63 #define B_HOST_TO_BENDIAN_FLOAT(arg)	__swap_float(arg)
64 #define B_HOST_TO_BENDIAN_INT64(arg)	__swap_int64(arg)
65 #define B_HOST_TO_BENDIAN_INT32(arg)	__swap_int32(arg)
66 #define B_HOST_TO_BENDIAN_INT16(arg)	__swap_int16(arg)
67 
68 /* Big endian to host native */
69 #define B_BENDIAN_TO_HOST_DOUBLE(arg)	__swap_double(arg)
70 #define B_BENDIAN_TO_HOST_FLOAT(arg)	__swap_float(arg)
71 #define B_BENDIAN_TO_HOST_INT64(arg)	__swap_int64(arg)
72 #define B_BENDIAN_TO_HOST_INT32(arg)	__swap_int32(arg)
73 #define B_BENDIAN_TO_HOST_INT16(arg)	__swap_int16(arg)
74 
75 #else	/* BYTE_ORDER */
76 /* Host is big endian */
77 
78 #define B_HOST_IS_LENDIAN 0
79 #define B_HOST_IS_BENDIAN 1
80 
81 /* Host native to little endian */
82 #define B_HOST_TO_LENDIAN_DOUBLE(arg)	__swap_double(arg)
83 #define B_HOST_TO_LENDIAN_FLOAT(arg)	__swap_float(arg)
84 #define B_HOST_TO_LENDIAN_INT64(arg)	__swap_int64(arg)
85 #define B_HOST_TO_LENDIAN_INT32(arg)	__swap_int32(arg)
86 #define B_HOST_TO_LENDIAN_INT16(arg)	__swap_int16(arg)
87 
88 /* Little endian to host native */
89 #define B_LENDIAN_TO_HOST_DOUBLE(arg)	__swap_double(arg)
90 #define B_LENDIAN_TO_HOST_FLOAT(arg)	__swap_float(arg)
91 #define B_LENDIAN_TO_HOST_INT64(arg)	__swap_int64(arg)
92 #define B_LENDIAN_TO_HOST_INT32(arg)	__swap_int32(arg)
93 #define B_LENDIAN_TO_HOST_INT16(arg)	__swap_int16(arg)
94 
95 /* Host native to big endian */
96 #define B_HOST_TO_BENDIAN_DOUBLE(arg)	(double)(arg)
97 #define B_HOST_TO_BENDIAN_FLOAT(arg)	(float)(arg)
98 #define B_HOST_TO_BENDIAN_INT64(arg)	(uint64)(arg)
99 #define B_HOST_TO_BENDIAN_INT32(arg)	(uint32)(arg)
100 #define B_HOST_TO_BENDIAN_INT16(arg)	(uint16)(arg)
101 
102 /* Big endian to host native */
103 #define B_BENDIAN_TO_HOST_DOUBLE(arg)	(double)(arg)
104 #define B_BENDIAN_TO_HOST_FLOAT(arg)	(float)(arg)
105 #define B_BENDIAN_TO_HOST_INT64(arg)	(uint64)(arg)
106 #define B_BENDIAN_TO_HOST_INT32(arg)	(uint32)(arg)
107 #define B_BENDIAN_TO_HOST_INT16(arg)	(uint16)(arg)
108 
109 #endif	/* BYTE_ORDER */
110 
111 
112 #ifdef __cplusplus
113 extern "C" {
114 #endif
115 
116 extern status_t swap_data(type_code type, void *data, size_t length,
117 	swap_action action);
118 extern bool is_type_swapped(type_code type);
119 
120 
121 /* Private implementations */
122 extern double __swap_double(double arg);
123 extern float  __swap_float(float arg);
124 extern uint64 __swap_int64(uint64 arg);
125 extern uint32 __swap_int32(uint32 arg);
126 extern uint16 __swap_int16(uint16 arg);
127 
128 #ifdef __cplusplus
129 }
130 #endif
131 
132 #endif	/* _BYTEORDER_H */
133