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