xref: /haiku/headers/os/support/ByteOrder.h (revision 29e8fa5922c9f9a5eb09a2fcc92e7fb321d4cc59)
1087882c2SAxel Dörfler /*
2*29e8fa59SJohn Scipione  * Copyright 2007 Haiku, Inc. All rights reserved.
3087882c2SAxel Dörfler  * Distributed under the terms of the MIT License.
4087882c2SAxel Dörfler  */
5*29e8fa59SJohn Scipione #ifndef _BYTE_ORDER_H
6*29e8fa59SJohn Scipione #define _BYTE_ORDER_H
752a38012Sejakowatz 
852a38012Sejakowatz 
952a38012Sejakowatz #include <BeBuild.h>
1052a38012Sejakowatz #include <endian.h>
1152a38012Sejakowatz #include <SupportDefs.h>
12087882c2SAxel Dörfler #include <TypeConstants.h>
13e1f3108aSStephan Aßmus 	/* for convenience */
1452a38012Sejakowatz 
1552a38012Sejakowatz 
16e1f3108aSStephan Aßmus /* swap directions */
1752a38012Sejakowatz typedef enum {
1852a38012Sejakowatz 	B_SWAP_HOST_TO_LENDIAN,
1952a38012Sejakowatz 	B_SWAP_HOST_TO_BENDIAN,
2052a38012Sejakowatz 	B_SWAP_LENDIAN_TO_HOST,
2152a38012Sejakowatz 	B_SWAP_BENDIAN_TO_HOST,
2252a38012Sejakowatz 	B_SWAP_ALWAYS
2352a38012Sejakowatz } swap_action;
2452a38012Sejakowatz 
2552a38012Sejakowatz 
26e1f3108aSStephan Aßmus /* BSD/networking macros */
27087882c2SAxel Dörfler #ifndef htonl
2852a38012Sejakowatz #	define htonl(x) B_HOST_TO_BENDIAN_INT32(x)
2952a38012Sejakowatz #	define ntohl(x) B_BENDIAN_TO_HOST_INT32(x)
3052a38012Sejakowatz #	define htons(x) B_HOST_TO_BENDIAN_INT16(x)
3152a38012Sejakowatz #	define ntohs(x) B_BENDIAN_TO_HOST_INT16(x)
327d9ef5afSbeveloper #endif
3352a38012Sejakowatz 
34e1f3108aSStephan Aßmus /* always swap macros */
35087882c2SAxel Dörfler #define B_SWAP_DOUBLE(arg)   __swap_double(arg)
36087882c2SAxel Dörfler #define B_SWAP_FLOAT(arg)    __swap_float(arg)
37087882c2SAxel Dörfler #define B_SWAP_INT64(arg)    __swap_int64(arg)
38087882c2SAxel Dörfler #define B_SWAP_INT32(arg)    __swap_int32(arg)
39087882c2SAxel Dörfler #define B_SWAP_INT16(arg)    __swap_int16(arg)
40087882c2SAxel Dörfler 
41087882c2SAxel Dörfler #if BYTE_ORDER == __LITTLE_ENDIAN
42e1f3108aSStephan Aßmus /* Host is little endian */
43087882c2SAxel Dörfler 
44087882c2SAxel Dörfler #define B_HOST_IS_LENDIAN 1
45087882c2SAxel Dörfler #define B_HOST_IS_BENDIAN 0
46087882c2SAxel Dörfler 
47e1f3108aSStephan Aßmus /* Host native to little endian */
48087882c2SAxel Dörfler #define B_HOST_TO_LENDIAN_DOUBLE(arg)	(double)(arg)
49087882c2SAxel Dörfler #define B_HOST_TO_LENDIAN_FLOAT(arg)	(float)(arg)
50087882c2SAxel Dörfler #define B_HOST_TO_LENDIAN_INT64(arg)	(uint64)(arg)
51087882c2SAxel Dörfler #define B_HOST_TO_LENDIAN_INT32(arg)	(uint32)(arg)
52087882c2SAxel Dörfler #define B_HOST_TO_LENDIAN_INT16(arg)	(uint16)(arg)
53087882c2SAxel Dörfler 
54e1f3108aSStephan Aßmus /* Little endian to host native */
55087882c2SAxel Dörfler #define B_LENDIAN_TO_HOST_DOUBLE(arg)	(double)(arg)
56087882c2SAxel Dörfler #define B_LENDIAN_TO_HOST_FLOAT(arg)	(float)(arg)
57087882c2SAxel Dörfler #define B_LENDIAN_TO_HOST_INT64(arg)	(uint64)(arg)
58087882c2SAxel Dörfler #define B_LENDIAN_TO_HOST_INT32(arg)	(uint32)(arg)
59087882c2SAxel Dörfler #define B_LENDIAN_TO_HOST_INT16(arg)	(uint16)(arg)
60087882c2SAxel Dörfler 
61e1f3108aSStephan Aßmus /* Host native to big endian */
62087882c2SAxel Dörfler #define B_HOST_TO_BENDIAN_DOUBLE(arg)	__swap_double(arg)
63087882c2SAxel Dörfler #define B_HOST_TO_BENDIAN_FLOAT(arg)	__swap_float(arg)
64087882c2SAxel Dörfler #define B_HOST_TO_BENDIAN_INT64(arg)	__swap_int64(arg)
65087882c2SAxel Dörfler #define B_HOST_TO_BENDIAN_INT32(arg)	__swap_int32(arg)
66087882c2SAxel Dörfler #define B_HOST_TO_BENDIAN_INT16(arg)	__swap_int16(arg)
67087882c2SAxel Dörfler 
68e1f3108aSStephan Aßmus /* Big endian to host native */
69087882c2SAxel Dörfler #define B_BENDIAN_TO_HOST_DOUBLE(arg)	__swap_double(arg)
70087882c2SAxel Dörfler #define B_BENDIAN_TO_HOST_FLOAT(arg)	__swap_float(arg)
71087882c2SAxel Dörfler #define B_BENDIAN_TO_HOST_INT64(arg)	__swap_int64(arg)
72087882c2SAxel Dörfler #define B_BENDIAN_TO_HOST_INT32(arg)	__swap_int32(arg)
73087882c2SAxel Dörfler #define B_BENDIAN_TO_HOST_INT16(arg)	__swap_int16(arg)
74087882c2SAxel Dörfler 
75e1f3108aSStephan Aßmus #else	/* BYTE_ORDER */
76e1f3108aSStephan Aßmus /* Host is big endian */
77087882c2SAxel Dörfler 
78087882c2SAxel Dörfler #define B_HOST_IS_LENDIAN 0
79087882c2SAxel Dörfler #define B_HOST_IS_BENDIAN 1
80087882c2SAxel Dörfler 
81e1f3108aSStephan Aßmus /* Host native to little endian */
82087882c2SAxel Dörfler #define B_HOST_TO_LENDIAN_DOUBLE(arg)	__swap_double(arg)
83087882c2SAxel Dörfler #define B_HOST_TO_LENDIAN_FLOAT(arg)	__swap_float(arg)
84087882c2SAxel Dörfler #define B_HOST_TO_LENDIAN_INT64(arg)	__swap_int64(arg)
85087882c2SAxel Dörfler #define B_HOST_TO_LENDIAN_INT32(arg)	__swap_int32(arg)
86087882c2SAxel Dörfler #define B_HOST_TO_LENDIAN_INT16(arg)	__swap_int16(arg)
87087882c2SAxel Dörfler 
88e1f3108aSStephan Aßmus /* Little endian to host native */
89087882c2SAxel Dörfler #define B_LENDIAN_TO_HOST_DOUBLE(arg)	__swap_double(arg)
90087882c2SAxel Dörfler #define B_LENDIAN_TO_HOST_FLOAT(arg)	__swap_float(arg)
91087882c2SAxel Dörfler #define B_LENDIAN_TO_HOST_INT64(arg)	__swap_int64(arg)
92087882c2SAxel Dörfler #define B_LENDIAN_TO_HOST_INT32(arg)	__swap_int32(arg)
93087882c2SAxel Dörfler #define B_LENDIAN_TO_HOST_INT16(arg)	__swap_int16(arg)
94087882c2SAxel Dörfler 
95e1f3108aSStephan Aßmus /* Host native to big endian */
96087882c2SAxel Dörfler #define B_HOST_TO_BENDIAN_DOUBLE(arg)	(double)(arg)
97087882c2SAxel Dörfler #define B_HOST_TO_BENDIAN_FLOAT(arg)	(float)(arg)
98087882c2SAxel Dörfler #define B_HOST_TO_BENDIAN_INT64(arg)	(uint64)(arg)
99087882c2SAxel Dörfler #define B_HOST_TO_BENDIAN_INT32(arg)	(uint32)(arg)
100087882c2SAxel Dörfler #define B_HOST_TO_BENDIAN_INT16(arg)	(uint16)(arg)
101087882c2SAxel Dörfler 
102e1f3108aSStephan Aßmus /* Big endian to host native */
103087882c2SAxel Dörfler #define B_BENDIAN_TO_HOST_DOUBLE(arg)	(double)(arg)
104087882c2SAxel Dörfler #define B_BENDIAN_TO_HOST_FLOAT(arg)	(float)(arg)
105087882c2SAxel Dörfler #define B_BENDIAN_TO_HOST_INT64(arg)	(uint64)(arg)
106087882c2SAxel Dörfler #define B_BENDIAN_TO_HOST_INT32(arg)	(uint32)(arg)
107087882c2SAxel Dörfler #define B_BENDIAN_TO_HOST_INT16(arg)	(uint16)(arg)
108087882c2SAxel Dörfler 
109e1f3108aSStephan Aßmus #endif	/* BYTE_ORDER */
110087882c2SAxel Dörfler 
111087882c2SAxel Dörfler 
112087882c2SAxel Dörfler #ifdef __cplusplus
113087882c2SAxel Dörfler extern "C" {
114*29e8fa59SJohn Scipione #endif	/* __cplusplus */
115087882c2SAxel Dörfler 
116087882c2SAxel Dörfler extern status_t swap_data(type_code type, void *data, size_t length,
117087882c2SAxel Dörfler 	swap_action action);
118087882c2SAxel Dörfler extern bool is_type_swapped(type_code type);
119087882c2SAxel Dörfler 
120e1f3108aSStephan Aßmus /* Private implementations */
121087882c2SAxel Dörfler extern double __swap_double(double arg);
122087882c2SAxel Dörfler extern float  __swap_float(float arg);
12344ec21c3SJessica Hamilton #if __GNUC__ >= 4
1244ffdf2edSJessica Hamilton #define __swap_int64(arg)	__builtin_bswap64(arg)
1254ffdf2edSJessica Hamilton #define __swap_int32(arg)	__builtin_bswap32(arg)
126fad4fc59SJessica Hamilton #define __swap_int16(arg)	__builtin_bswap16(arg)
1274ffdf2edSJessica Hamilton #else
128087882c2SAxel Dörfler extern uint64 __swap_int64(uint64 arg);
129087882c2SAxel Dörfler extern uint32 __swap_int32(uint32 arg);
130fad4fc59SJessica Hamilton extern uint16 __swap_int16(uint16 arg);
1314ffdf2edSJessica Hamilton #endif
132087882c2SAxel Dörfler 
13352a38012Sejakowatz #ifdef __cplusplus
13452a38012Sejakowatz }
135*29e8fa59SJohn Scipione #endif	/*  __cplusplus */
13652a38012Sejakowatz 
137*29e8fa59SJohn Scipione #endif	/* _BYTE_ORDER_H */
138