2 * Copyright 2007, Haiku, Inc. All Rights Reserved.
3 * Distributed under the terms of the MIT License.
5 #ifndef _FSSH_BYTEORDER_H
6 #define _FSSH_BYTEORDER_H
12 #include "fssh_defs.h"
17 FSSH_B_SWAP_HOST_TO_LENDIAN
,
18 FSSH_B_SWAP_HOST_TO_BENDIAN
,
19 FSSH_B_SWAP_LENDIAN_TO_HOST
,
20 FSSH_B_SWAP_BENDIAN_TO_HOST
,
25 // BSD/networking macros
27 # define fssh_htonl(x) FSSH_B_HOST_TO_BENDIAN_INT32(x)
28 # define fssh_ntohl(x) FSSH_B_BENDIAN_TO_HOST_INT32(x)
29 # define fssh_htons(x) FSSH_B_HOST_TO_BENDIAN_INT16(x)
30 # define fssh_ntohs(x) FSSH_B_BENDIAN_TO_HOST_INT16(x)
34 #define FSSH_B_SWAP_DOUBLE(arg) __fssh_swap_double(arg)
35 #define FSSH_B_SWAP_FLOAT(arg) __fssh_swap_float(arg)
36 #define FSSH_B_SWAP_INT64(arg) __fssh_swap_int64(arg)
37 #define FSSH_B_SWAP_INT32(arg) __fssh_swap_int32(arg)
38 #define FSSH_B_SWAP_INT16(arg) __fssh_swap_int16(arg)
40 #if BYTE_ORDER == __LITTLE_ENDIAN
41 // Host is little endian
43 #define FSSH_B_HOST_IS_LENDIAN 1
44 #define FSSH_B_HOST_IS_BENDIAN 0
46 // Host native to little endian
47 #define FSSH_B_HOST_TO_LENDIAN_DOUBLE(arg) (double)(arg)
48 #define FSSH_B_HOST_TO_LENDIAN_FLOAT(arg) (float)(arg)
49 #define FSSH_B_HOST_TO_LENDIAN_INT64(arg) (uint64_t)(arg)
50 #define FSSH_B_HOST_TO_LENDIAN_INT32(arg) (uint32_t)(arg)
51 #define FSSH_B_HOST_TO_LENDIAN_INT16(arg) (uint16_t)(arg)
53 // Little endian to host native
54 #define FSSH_B_LENDIAN_TO_HOST_DOUBLE(arg) (double)(arg)
55 #define FSSH_B_LENDIAN_TO_HOST_FLOAT(arg) (float)(arg)
56 #define FSSH_B_LENDIAN_TO_HOST_INT64(arg) (uint64_t)(arg)
57 #define FSSH_B_LENDIAN_TO_HOST_INT32(arg) (uint32_t)(arg)
58 #define FSSH_B_LENDIAN_TO_HOST_INT16(arg) (uint16_t)(arg)
60 // Host native to big endian
61 #define FSSH_B_HOST_TO_BENDIAN_DOUBLE(arg) __fssh_swap_double(arg)
62 #define FSSH_B_HOST_TO_BENDIAN_FLOAT(arg) __fssh_swap_float(arg)
63 #define FSSH_B_HOST_TO_BENDIAN_INT64(arg) __fssh_swap_int64(arg)
64 #define FSSH_B_HOST_TO_BENDIAN_INT32(arg) __fssh_swap_int32(arg)
65 #define FSSH_B_HOST_TO_BENDIAN_INT16(arg) __fssh_swap_int16(arg)
67 // Big endian to host native
68 #define FSSH_B_BENDIAN_TO_HOST_DOUBLE(arg) __fssh_swap_double(arg)
69 #define FSSH_B_BENDIAN_TO_HOST_FLOAT(arg) __fssh_swap_float(arg)
70 #define FSSH_B_BENDIAN_TO_HOST_INT64(arg) __fssh_swap_int64(arg)
71 #define FSSH_B_BENDIAN_TO_HOST_INT32(arg) __fssh_swap_int32(arg)
72 #define FSSH_B_BENDIAN_TO_HOST_INT16(arg) __fssh_swap_int16(arg)
77 #define FSSH_B_HOST_IS_LENDIAN 0
78 #define FSSH_B_HOST_IS_BENDIAN 1
80 // Host native to little endian
81 #define FSSH_B_HOST_TO_LENDIAN_DOUBLE(arg) __fssh_swap_double(arg)
82 #define FSSH_B_HOST_TO_LENDIAN_FLOAT(arg) __fssh_swap_float(arg)
83 #define FSSH_B_HOST_TO_LENDIAN_INT64(arg) __fssh_swap_int64(arg)
84 #define FSSH_B_HOST_TO_LENDIAN_INT32(arg) __fssh_swap_int32(arg)
85 #define FSSH_B_HOST_TO_LENDIAN_INT16(arg) __fssh_swap_int16(arg)
87 // Little endian to host native
88 #define FSSH_B_LENDIAN_TO_HOST_DOUBLE(arg) __fssh_swap_double(arg)
89 #define FSSH_B_LENDIAN_TO_HOST_FLOAT(arg) __fssh_swap_float(arg)
90 #define FSSH_B_LENDIAN_TO_HOST_INT64(arg) __fssh_swap_int64(arg)
91 #define FSSH_B_LENDIAN_TO_HOST_INT32(arg) __fssh_swap_int32(arg)
92 #define FSSH_B_LENDIAN_TO_HOST_INT16(arg) __fssh_swap_int16(arg)
94 // Host native to big endian
95 #define FSSH_B_HOST_TO_BENDIAN_DOUBLE(arg) (double)(arg)
96 #define FSSH_B_HOST_TO_BENDIAN_FLOAT(arg) (float)(arg)
97 #define FSSH_B_HOST_TO_BENDIAN_INT64(arg) (uint64_t)(arg)
98 #define FSSH_B_HOST_TO_BENDIAN_INT32(arg) (uint32_t)(arg)
99 #define FSSH_B_HOST_TO_BENDIAN_INT16(arg) (uint16_t)(arg)
101 // Big endian to host native
102 #define FSSH_B_BENDIAN_TO_HOST_DOUBLE(arg) (double)(arg)
103 #define FSSH_B_BENDIAN_TO_HOST_FLOAT(arg) (float)(arg)
104 #define FSSH_B_BENDIAN_TO_HOST_INT64(arg) (uint64_t)(arg)
105 #define FSSH_B_BENDIAN_TO_HOST_INT32(arg) (uint32_t)(arg)
106 #define FSSH_B_BENDIAN_TO_HOST_INT16(arg) (uint16_t)(arg)
115 extern fssh_status_t
fssh_swap_data(fssh_type_code type
, void *data
,
116 fssh_size_t length
, fssh_swap_action action
);
117 extern bool fssh_is_type_swapped(fssh_type_code type
);
120 // Private implementations
121 extern double __fssh_swap_double(double arg
);
122 extern float __fssh_swap_float(float arg
);
123 extern uint64_t __fssh_swap_int64(uint64_t arg
);
124 extern uint32_t __fssh_swap_int32(uint32_t arg
);
125 extern uint16_t __fssh_swap_int16(uint16_t arg
);
131 #endif // _FSSH_BYTEORDER_H