xref: /haiku/headers/build/os/support/ByteOrder.h (revision d3d8b26997fac34a84981e6d2b649521de2cc45a)
1 // Modified BeOS header. Just here to be able to compile and test BResources.
2 // To be replaced by the OpenBeOS version to be provided by the IK Team.
3 
4 #ifndef _sk_byte_order_h_
5 #define _sk_byte_order_h_
6 
7 
8 #include <BeBuild.h>
9 #include <SupportDefs.h>
10 #include <TypeConstants.h>	/* For convenience */
11 
12 #ifndef HAIKU_HOST_PLATFORM_FREEBSD
13 #	include <endian.h>
14 #endif
15 
16 
17 #ifdef __cplusplus
18 extern "C" {
19 #endif
20 
21 /*----------------------------------------------------------------------*/
22 /*------- Swap-direction constants, and swapping functions -------------*/
23 
24 typedef enum {
25 	B_SWAP_HOST_TO_LENDIAN,
26 	B_SWAP_HOST_TO_BENDIAN,
27 	B_SWAP_LENDIAN_TO_HOST,
28 	B_SWAP_BENDIAN_TO_HOST,
29 	B_SWAP_ALWAYS
30 } swap_action;
31 
32 extern status_t swap_data(type_code type, void *data, size_t length,
33 						  swap_action action);
34 
35 extern bool is_type_swapped(type_code type);
36 
37 
38 /*-----------------------------------------------------------------------*/
39 /*----- Private implementations -----------------------------------------*/
40 extern double __swap_double(double arg);
41 extern float  __swap_float(float arg);
42 extern uint64 __swap_int64(uint64 uarg);
43 extern uint32 __swap_int32(uint32 uarg);
44 extern uint16 __swap_int16(uint16 uarg);
45 /*-------------------------------------------------------------*/
46 
47 
48 /*-------------------------------------------------------------*/
49 /*--------- Host is Little  --------------------------------------*/
50 
51 #if BYTE_ORDER == __LITTLE_ENDIAN
52 #define B_HOST_IS_LENDIAN 1
53 #define B_HOST_IS_BENDIAN 0
54 
55 /*--------- Host Native -> Little  --------------------*/
56 
57 #define B_HOST_TO_LENDIAN_DOUBLE(arg)	(double)(arg)
58 #define B_HOST_TO_LENDIAN_FLOAT(arg)	(float)(arg)
59 #define B_HOST_TO_LENDIAN_INT64(arg)	(uint64)(arg)
60 #define B_HOST_TO_LENDIAN_INT32(arg)	(uint32)(arg)
61 #define B_HOST_TO_LENDIAN_INT16(arg)	(uint16)(arg)
62 
63 /*--------- Host Native -> Big  ------------------------*/
64 #define B_HOST_TO_BENDIAN_DOUBLE(arg)	__swap_double(arg)
65 #define B_HOST_TO_BENDIAN_FLOAT(arg)	__swap_float(arg)
66 #define B_HOST_TO_BENDIAN_INT64(arg)	__swap_int64(arg)
67 #define B_HOST_TO_BENDIAN_INT32(arg)	__swap_int32(arg)
68 #define B_HOST_TO_BENDIAN_INT16(arg)	__swap_int16(arg)
69 
70 /*--------- Little -> Host Native ---------------------*/
71 #define B_LENDIAN_TO_HOST_DOUBLE(arg)	(double)(arg)
72 #define B_LENDIAN_TO_HOST_FLOAT(arg)	(float)(arg)
73 #define B_LENDIAN_TO_HOST_INT64(arg)	(uint64)(arg)
74 #define B_LENDIAN_TO_HOST_INT32(arg)	(uint32)(arg)
75 #define B_LENDIAN_TO_HOST_INT16(arg)	(uint16)(arg)
76 
77 /*--------- Big -> Host Native ------------------------*/
78 #define B_BENDIAN_TO_HOST_DOUBLE(arg)	__swap_double(arg)
79 #define B_BENDIAN_TO_HOST_FLOAT(arg)	__swap_float(arg)
80 #define B_BENDIAN_TO_HOST_INT64(arg)	__swap_int64(arg)
81 #define B_BENDIAN_TO_HOST_INT32(arg)	__swap_int32(arg)
82 #define B_BENDIAN_TO_HOST_INT16(arg)	__swap_int16(arg)
83 
84 #else /* __LITTLE_ENDIAN */
85 
86 
87 /*-------------------------------------------------------------*/
88 /*--------- Host is Big  --------------------------------------*/
89 
90 #define B_HOST_IS_LENDIAN 0
91 #define B_HOST_IS_BENDIAN 1
92 
93 /*--------- Host Native -> Little  -------------------*/
94 #define B_HOST_TO_LENDIAN_DOUBLE(arg)	__swap_double(arg)
95 #define B_HOST_TO_LENDIAN_FLOAT(arg)	__swap_float(arg)
96 #define B_HOST_TO_LENDIAN_INT64(arg)	__swap_int64(arg)
97 #define B_HOST_TO_LENDIAN_INT32(arg)	__swap_int32(arg)
98 #define B_HOST_TO_LENDIAN_INT16(arg)	__swap_int16(arg)
99 
100 /*--------- Host Native -> Big  ------------------------*/
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 /*--------- Little -> Host Native ----------------------*/
108 #define B_LENDIAN_TO_HOST_DOUBLE(arg)	__swap_double(arg)
109 #define B_LENDIAN_TO_HOST_FLOAT(arg)	__swap_float(arg)
110 #define B_LENDIAN_TO_HOST_INT64(arg)	__swap_int64(arg)
111 #define B_LENDIAN_TO_HOST_INT32(arg)	__swap_int32(arg)
112 #define B_LENDIAN_TO_HOST_INT16(arg)	__swap_int16(arg)
113 
114 /*--------- Big -> Host Native -------------------------*/
115 #define B_BENDIAN_TO_HOST_DOUBLE(arg)	(double)(arg)
116 #define B_BENDIAN_TO_HOST_FLOAT(arg)	(float)(arg)
117 #define B_BENDIAN_TO_HOST_INT64(arg)	(uint64)(arg)
118 #define B_BENDIAN_TO_HOST_INT32(arg)	(uint32)(arg)
119 #define B_BENDIAN_TO_HOST_INT16(arg)	(uint16)(arg)
120 
121 #endif /* __LITTLE_ENDIAN */
122 
123 
124 /*-------------------------------------------------------------*/
125 /*--------- Just-do-it macros ---------------------------------*/
126 #define B_SWAP_DOUBLE(arg)   __swap_double(arg)
127 #define B_SWAP_FLOAT(arg)    __swap_float(arg)
128 #define B_SWAP_INT64(arg)    __swap_int64(arg)
129 #define B_SWAP_INT32(arg)    __swap_int32(arg)
130 #define B_SWAP_INT16(arg)    __swap_int16(arg)
131 
132 
133 /*-------------------------------------------------------------*/
134 /*---------Berkeley macros -----------------------------------*/
135 #ifndef htonl /* avoid collision with <netinet/in.h> */
136   #define htonl(x) B_HOST_TO_BENDIAN_INT32(x)
137   #define ntohl(x) B_BENDIAN_TO_HOST_INT32(x)
138   #define htons(x) B_HOST_TO_BENDIAN_INT16(x)
139   #define ntohs(x) B_BENDIAN_TO_HOST_INT16(x)
140 #endif
141 
142 #ifdef __cplusplus
143 }
144 #endif /* __cplusplus */
145 
146 /*-------------------------------------------------------------*/
147 /*-------------------------------------------------------------*/
148 
149 #endif	// _sk_byte_order_h_
150