1/*
2 * Copyright 2007, Haiku, Inc. All rights reserved.
3 * Distributed under the terms of the MIT License.
4 */
5#ifndef _BYTE_ORDER_H
6#define _BYTE_ORDER_H
7
8
9#include <BeBuild.h>
10#include <endian.h>
11#include <SupportDefs.h>
12#include <TypeConstants.h>
13	/* for convenience */
14
15
16/* swap directions */
17typedef enum {
18	B_SWAP_HOST_TO_LENDIAN,
19	B_SWAP_HOST_TO_BENDIAN,
20	B_SWAP_LENDIAN_TO_HOST,
21	B_SWAP_BENDIAN_TO_HOST,
22	B_SWAP_ALWAYS
23} swap_action;
24
25
26/* always swap macros */
27#define B_SWAP_DOUBLE(arg)   __swap_double(arg)
28#define B_SWAP_FLOAT(arg)    __swap_float(arg)
29#define B_SWAP_INT64(arg)    __swap_int64(arg)
30#define B_SWAP_INT32(arg)    __swap_int32(arg)
31#define B_SWAP_INT16(arg)    __swap_int16(arg)
32
33#if BYTE_ORDER == __LITTLE_ENDIAN
34/* Host is little endian */
35
36#define B_HOST_IS_LENDIAN 1
37#define B_HOST_IS_BENDIAN 0
38
39/* Host native to little endian */
40#define B_HOST_TO_LENDIAN_DOUBLE(arg)	(double)(arg)
41#define B_HOST_TO_LENDIAN_FLOAT(arg)	(float)(arg)
42#define B_HOST_TO_LENDIAN_INT64(arg)	(uint64)(arg)
43#define B_HOST_TO_LENDIAN_INT32(arg)	(uint32)(arg)
44#define B_HOST_TO_LENDIAN_INT16(arg)	(uint16)(arg)
45
46/* Little endian to host native */
47#define B_LENDIAN_TO_HOST_DOUBLE(arg)	(double)(arg)
48#define B_LENDIAN_TO_HOST_FLOAT(arg)	(float)(arg)
49#define B_LENDIAN_TO_HOST_INT64(arg)	(uint64)(arg)
50#define B_LENDIAN_TO_HOST_INT32(arg)	(uint32)(arg)
51#define B_LENDIAN_TO_HOST_INT16(arg)	(uint16)(arg)
52
53/* Host native to big endian */
54#define B_HOST_TO_BENDIAN_DOUBLE(arg)	__swap_double(arg)
55#define B_HOST_TO_BENDIAN_FLOAT(arg)	__swap_float(arg)
56#define B_HOST_TO_BENDIAN_INT64(arg)	__swap_int64(arg)
57#define B_HOST_TO_BENDIAN_INT32(arg)	__swap_int32(arg)
58#define B_HOST_TO_BENDIAN_INT16(arg)	__swap_int16(arg)
59
60/* Big endian to host native */
61#define B_BENDIAN_TO_HOST_DOUBLE(arg)	__swap_double(arg)
62#define B_BENDIAN_TO_HOST_FLOAT(arg)	__swap_float(arg)
63#define B_BENDIAN_TO_HOST_INT64(arg)	__swap_int64(arg)
64#define B_BENDIAN_TO_HOST_INT32(arg)	__swap_int32(arg)
65#define B_BENDIAN_TO_HOST_INT16(arg)	__swap_int16(arg)
66
67#else	/* BYTE_ORDER */
68/* Host is big endian */
69
70#define B_HOST_IS_LENDIAN 0
71#define B_HOST_IS_BENDIAN 1
72
73/* Host native to little endian */
74#define B_HOST_TO_LENDIAN_DOUBLE(arg)	__swap_double(arg)
75#define B_HOST_TO_LENDIAN_FLOAT(arg)	__swap_float(arg)
76#define B_HOST_TO_LENDIAN_INT64(arg)	__swap_int64(arg)
77#define B_HOST_TO_LENDIAN_INT32(arg)	__swap_int32(arg)
78#define B_HOST_TO_LENDIAN_INT16(arg)	__swap_int16(arg)
79
80/* Little endian to host native */
81#define B_LENDIAN_TO_HOST_DOUBLE(arg)	__swap_double(arg)
82#define B_LENDIAN_TO_HOST_FLOAT(arg)	__swap_float(arg)
83#define B_LENDIAN_TO_HOST_INT64(arg)	__swap_int64(arg)
84#define B_LENDIAN_TO_HOST_INT32(arg)	__swap_int32(arg)
85#define B_LENDIAN_TO_HOST_INT16(arg)	__swap_int16(arg)
86
87/* Host native to big endian */
88#define B_HOST_TO_BENDIAN_DOUBLE(arg)	(double)(arg)
89#define B_HOST_TO_BENDIAN_FLOAT(arg)	(float)(arg)
90#define B_HOST_TO_BENDIAN_INT64(arg)	(uint64)(arg)
91#define B_HOST_TO_BENDIAN_INT32(arg)	(uint32)(arg)
92#define B_HOST_TO_BENDIAN_INT16(arg)	(uint16)(arg)
93
94/* Big endian to host native */
95#define B_BENDIAN_TO_HOST_DOUBLE(arg)	(double)(arg)
96#define B_BENDIAN_TO_HOST_FLOAT(arg)	(float)(arg)
97#define B_BENDIAN_TO_HOST_INT64(arg)	(uint64)(arg)
98#define B_BENDIAN_TO_HOST_INT32(arg)	(uint32)(arg)
99#define B_BENDIAN_TO_HOST_INT16(arg)	(uint16)(arg)
100
101#endif	/* BYTE_ORDER */
102
103
104#ifdef __cplusplus
105extern "C" {
106#endif	/* __cplusplus */
107
108extern status_t swap_data(type_code type, void *data, size_t length,
109	swap_action action);
110extern bool is_type_swapped(type_code type);
111
112/* Private implementations */
113extern double __swap_double(double arg);
114extern float  __swap_float(float arg);
115#if __GNUC__ >= 4
116#define __swap_int64(arg)	(uint64)__builtin_bswap64(arg)
117#define __swap_int32(arg)	(uint32)__builtin_bswap32(arg)
118#define __swap_int16(arg)	(uint16)__builtin_bswap16(arg)
119#else
120extern uint64 __swap_int64(uint64 arg);
121extern uint32 __swap_int32(uint32 arg);
122extern uint16 __swap_int16(uint16 arg);
123#endif
124
125#ifdef __cplusplus
126}
127#endif	/*  __cplusplus */
128
129#endif	/* _BYTE_ORDER_H */
130