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