2007-01-26 19:36:29 +03:00
|
|
|
/*
|
2014-06-25 03:25:27 +04:00
|
|
|
* Copyright 2007 Haiku, Inc. All rights reserved.
|
2007-01-26 19:36:29 +03:00
|
|
|
* Distributed under the terms of the MIT License.
|
|
|
|
*/
|
2014-06-25 03:25:27 +04:00
|
|
|
#ifndef _BYTE_ORDER_H
|
|
|
|
#define _BYTE_ORDER_H
|
2002-07-09 16:24:59 +04:00
|
|
|
|
|
|
|
|
|
|
|
#include <BeBuild.h>
|
|
|
|
#include <endian.h>
|
|
|
|
#include <SupportDefs.h>
|
2007-01-26 19:36:29 +03:00
|
|
|
#include <TypeConstants.h>
|
2008-11-11 02:50:42 +03:00
|
|
|
/* for convenience */
|
2002-07-09 16:24:59 +04:00
|
|
|
|
|
|
|
|
2008-11-11 02:50:42 +03:00
|
|
|
/* swap directions */
|
2002-07-09 16:24:59 +04:00
|
|
|
typedef enum {
|
|
|
|
B_SWAP_HOST_TO_LENDIAN,
|
|
|
|
B_SWAP_HOST_TO_BENDIAN,
|
|
|
|
B_SWAP_LENDIAN_TO_HOST,
|
|
|
|
B_SWAP_BENDIAN_TO_HOST,
|
|
|
|
B_SWAP_ALWAYS
|
|
|
|
} swap_action;
|
|
|
|
|
|
|
|
|
2008-11-11 02:50:42 +03:00
|
|
|
/* BSD/networking macros */
|
2007-01-26 19:36:29 +03:00
|
|
|
#ifndef htonl
|
|
|
|
# define htonl(x) B_HOST_TO_BENDIAN_INT32(x)
|
|
|
|
# define ntohl(x) B_BENDIAN_TO_HOST_INT32(x)
|
|
|
|
# define htons(x) B_HOST_TO_BENDIAN_INT16(x)
|
|
|
|
# define ntohs(x) B_BENDIAN_TO_HOST_INT16(x)
|
|
|
|
#endif
|
2002-07-09 16:24:59 +04:00
|
|
|
|
2008-11-11 02:50:42 +03:00
|
|
|
/* always swap macros */
|
2007-01-26 19:36:29 +03:00
|
|
|
#define B_SWAP_DOUBLE(arg) __swap_double(arg)
|
|
|
|
#define B_SWAP_FLOAT(arg) __swap_float(arg)
|
|
|
|
#define B_SWAP_INT64(arg) __swap_int64(arg)
|
|
|
|
#define B_SWAP_INT32(arg) __swap_int32(arg)
|
|
|
|
#define B_SWAP_INT16(arg) __swap_int16(arg)
|
2002-07-09 16:24:59 +04:00
|
|
|
|
|
|
|
#if BYTE_ORDER == __LITTLE_ENDIAN
|
2008-11-11 02:50:42 +03:00
|
|
|
/* Host is little endian */
|
2007-01-26 19:36:29 +03:00
|
|
|
|
2002-07-09 16:24:59 +04:00
|
|
|
#define B_HOST_IS_LENDIAN 1
|
|
|
|
#define B_HOST_IS_BENDIAN 0
|
|
|
|
|
2008-11-11 02:50:42 +03:00
|
|
|
/* Host native to little endian */
|
2002-07-09 16:24:59 +04:00
|
|
|
#define B_HOST_TO_LENDIAN_DOUBLE(arg) (double)(arg)
|
|
|
|
#define B_HOST_TO_LENDIAN_FLOAT(arg) (float)(arg)
|
|
|
|
#define B_HOST_TO_LENDIAN_INT64(arg) (uint64)(arg)
|
|
|
|
#define B_HOST_TO_LENDIAN_INT32(arg) (uint32)(arg)
|
|
|
|
#define B_HOST_TO_LENDIAN_INT16(arg) (uint16)(arg)
|
|
|
|
|
2008-11-11 02:50:42 +03:00
|
|
|
/* Little endian to host native */
|
2002-07-09 16:24:59 +04:00
|
|
|
#define B_LENDIAN_TO_HOST_DOUBLE(arg) (double)(arg)
|
|
|
|
#define B_LENDIAN_TO_HOST_FLOAT(arg) (float)(arg)
|
|
|
|
#define B_LENDIAN_TO_HOST_INT64(arg) (uint64)(arg)
|
|
|
|
#define B_LENDIAN_TO_HOST_INT32(arg) (uint32)(arg)
|
|
|
|
#define B_LENDIAN_TO_HOST_INT16(arg) (uint16)(arg)
|
|
|
|
|
2008-11-11 02:50:42 +03:00
|
|
|
/* Host native to big endian */
|
2007-01-26 19:36:29 +03:00
|
|
|
#define B_HOST_TO_BENDIAN_DOUBLE(arg) __swap_double(arg)
|
|
|
|
#define B_HOST_TO_BENDIAN_FLOAT(arg) __swap_float(arg)
|
|
|
|
#define B_HOST_TO_BENDIAN_INT64(arg) __swap_int64(arg)
|
|
|
|
#define B_HOST_TO_BENDIAN_INT32(arg) __swap_int32(arg)
|
|
|
|
#define B_HOST_TO_BENDIAN_INT16(arg) __swap_int16(arg)
|
|
|
|
|
2008-11-11 02:50:42 +03:00
|
|
|
/* Big endian to host native */
|
2002-07-09 16:24:59 +04:00
|
|
|
#define B_BENDIAN_TO_HOST_DOUBLE(arg) __swap_double(arg)
|
|
|
|
#define B_BENDIAN_TO_HOST_FLOAT(arg) __swap_float(arg)
|
|
|
|
#define B_BENDIAN_TO_HOST_INT64(arg) __swap_int64(arg)
|
|
|
|
#define B_BENDIAN_TO_HOST_INT32(arg) __swap_int32(arg)
|
|
|
|
#define B_BENDIAN_TO_HOST_INT16(arg) __swap_int16(arg)
|
|
|
|
|
2008-11-11 02:50:42 +03:00
|
|
|
#else /* BYTE_ORDER */
|
|
|
|
/* Host is big endian */
|
2002-07-09 16:24:59 +04:00
|
|
|
|
|
|
|
#define B_HOST_IS_LENDIAN 0
|
|
|
|
#define B_HOST_IS_BENDIAN 1
|
|
|
|
|
2008-11-11 02:50:42 +03:00
|
|
|
/* Host native to little endian */
|
2002-07-09 16:24:59 +04:00
|
|
|
#define B_HOST_TO_LENDIAN_DOUBLE(arg) __swap_double(arg)
|
|
|
|
#define B_HOST_TO_LENDIAN_FLOAT(arg) __swap_float(arg)
|
|
|
|
#define B_HOST_TO_LENDIAN_INT64(arg) __swap_int64(arg)
|
|
|
|
#define B_HOST_TO_LENDIAN_INT32(arg) __swap_int32(arg)
|
|
|
|
#define B_HOST_TO_LENDIAN_INT16(arg) __swap_int16(arg)
|
|
|
|
|
2008-11-11 02:50:42 +03:00
|
|
|
/* Little endian to host native */
|
2002-07-09 16:24:59 +04:00
|
|
|
#define B_LENDIAN_TO_HOST_DOUBLE(arg) __swap_double(arg)
|
|
|
|
#define B_LENDIAN_TO_HOST_FLOAT(arg) __swap_float(arg)
|
|
|
|
#define B_LENDIAN_TO_HOST_INT64(arg) __swap_int64(arg)
|
|
|
|
#define B_LENDIAN_TO_HOST_INT32(arg) __swap_int32(arg)
|
|
|
|
#define B_LENDIAN_TO_HOST_INT16(arg) __swap_int16(arg)
|
|
|
|
|
2008-11-11 02:50:42 +03:00
|
|
|
/* Host native to big endian */
|
2007-01-26 19:36:29 +03:00
|
|
|
#define B_HOST_TO_BENDIAN_DOUBLE(arg) (double)(arg)
|
|
|
|
#define B_HOST_TO_BENDIAN_FLOAT(arg) (float)(arg)
|
|
|
|
#define B_HOST_TO_BENDIAN_INT64(arg) (uint64)(arg)
|
|
|
|
#define B_HOST_TO_BENDIAN_INT32(arg) (uint32)(arg)
|
|
|
|
#define B_HOST_TO_BENDIAN_INT16(arg) (uint16)(arg)
|
|
|
|
|
2008-11-11 02:50:42 +03:00
|
|
|
/* Big endian to host native */
|
2002-07-09 16:24:59 +04:00
|
|
|
#define B_BENDIAN_TO_HOST_DOUBLE(arg) (double)(arg)
|
|
|
|
#define B_BENDIAN_TO_HOST_FLOAT(arg) (float)(arg)
|
|
|
|
#define B_BENDIAN_TO_HOST_INT64(arg) (uint64)(arg)
|
|
|
|
#define B_BENDIAN_TO_HOST_INT32(arg) (uint32)(arg)
|
|
|
|
#define B_BENDIAN_TO_HOST_INT16(arg) (uint16)(arg)
|
|
|
|
|
2008-11-11 02:50:42 +03:00
|
|
|
#endif /* BYTE_ORDER */
|
2002-07-09 16:24:59 +04:00
|
|
|
|
|
|
|
|
2007-01-26 19:36:29 +03:00
|
|
|
#ifdef __cplusplus
|
|
|
|
extern "C" {
|
2014-06-25 03:25:27 +04:00
|
|
|
#endif /* __cplusplus */
|
2007-01-26 19:36:29 +03:00
|
|
|
|
|
|
|
extern status_t swap_data(type_code type, void *data, size_t length,
|
|
|
|
swap_action action);
|
|
|
|
extern bool is_type_swapped(type_code type);
|
2002-07-09 16:24:59 +04:00
|
|
|
|
2008-11-11 02:50:42 +03:00
|
|
|
/* Private implementations */
|
2007-01-26 19:36:29 +03:00
|
|
|
extern double __swap_double(double arg);
|
|
|
|
extern float __swap_float(float arg);
|
2014-05-29 16:12:45 +04:00
|
|
|
#if __GNUC__ >= 4
|
2014-05-04 04:03:11 +04:00
|
|
|
#define __swap_int64(arg) __builtin_bswap64(arg)
|
|
|
|
#define __swap_int32(arg) __builtin_bswap32(arg)
|
2014-05-30 11:56:34 +04:00
|
|
|
#define __swap_int16(arg) __builtin_bswap16(arg)
|
2014-05-04 04:03:11 +04:00
|
|
|
#else
|
2007-01-26 19:36:29 +03:00
|
|
|
extern uint64 __swap_int64(uint64 arg);
|
|
|
|
extern uint32 __swap_int32(uint32 arg);
|
2014-05-30 11:56:34 +04:00
|
|
|
extern uint16 __swap_int16(uint16 arg);
|
2014-05-04 04:03:11 +04:00
|
|
|
#endif
|
2002-07-09 16:24:59 +04:00
|
|
|
|
|
|
|
#ifdef __cplusplus
|
|
|
|
}
|
2014-06-25 03:25:27 +04:00
|
|
|
#endif /* __cplusplus */
|
2002-07-09 16:24:59 +04:00
|
|
|
|
2014-06-25 03:25:27 +04:00
|
|
|
#endif /* _BYTE_ORDER_H */
|