1087882c2SAxel Dörfler /* 2*9cb15d72SAugustin Cavalier * Copyright 2007, Haiku, Inc. All rights reserved. 3087882c2SAxel Dörfler * Distributed under the terms of the MIT License. 4087882c2SAxel Dörfler */ 529e8fa59SJohn Scipione #ifndef _BYTE_ORDER_H 629e8fa59SJohn 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 /* always swap macros */ 27087882c2SAxel Dörfler #define B_SWAP_DOUBLE(arg) __swap_double(arg) 28087882c2SAxel Dörfler #define B_SWAP_FLOAT(arg) __swap_float(arg) 29087882c2SAxel Dörfler #define B_SWAP_INT64(arg) __swap_int64(arg) 30087882c2SAxel Dörfler #define B_SWAP_INT32(arg) __swap_int32(arg) 31087882c2SAxel Dörfler #define B_SWAP_INT16(arg) __swap_int16(arg) 32087882c2SAxel Dörfler 33087882c2SAxel Dörfler #if BYTE_ORDER == __LITTLE_ENDIAN 34e1f3108aSStephan Aßmus /* Host is little endian */ 35087882c2SAxel Dörfler 36087882c2SAxel Dörfler #define B_HOST_IS_LENDIAN 1 37087882c2SAxel Dörfler #define B_HOST_IS_BENDIAN 0 38087882c2SAxel Dörfler 39e1f3108aSStephan Aßmus /* Host native to little endian */ 40087882c2SAxel Dörfler #define B_HOST_TO_LENDIAN_DOUBLE(arg) (double)(arg) 41087882c2SAxel Dörfler #define B_HOST_TO_LENDIAN_FLOAT(arg) (float)(arg) 42087882c2SAxel Dörfler #define B_HOST_TO_LENDIAN_INT64(arg) (uint64)(arg) 43087882c2SAxel Dörfler #define B_HOST_TO_LENDIAN_INT32(arg) (uint32)(arg) 44087882c2SAxel Dörfler #define B_HOST_TO_LENDIAN_INT16(arg) (uint16)(arg) 45087882c2SAxel Dörfler 46e1f3108aSStephan Aßmus /* Little endian to host native */ 47087882c2SAxel Dörfler #define B_LENDIAN_TO_HOST_DOUBLE(arg) (double)(arg) 48087882c2SAxel Dörfler #define B_LENDIAN_TO_HOST_FLOAT(arg) (float)(arg) 49087882c2SAxel Dörfler #define B_LENDIAN_TO_HOST_INT64(arg) (uint64)(arg) 50087882c2SAxel Dörfler #define B_LENDIAN_TO_HOST_INT32(arg) (uint32)(arg) 51087882c2SAxel Dörfler #define B_LENDIAN_TO_HOST_INT16(arg) (uint16)(arg) 52087882c2SAxel Dörfler 53e1f3108aSStephan Aßmus /* Host native to big endian */ 54087882c2SAxel Dörfler #define B_HOST_TO_BENDIAN_DOUBLE(arg) __swap_double(arg) 55087882c2SAxel Dörfler #define B_HOST_TO_BENDIAN_FLOAT(arg) __swap_float(arg) 56087882c2SAxel Dörfler #define B_HOST_TO_BENDIAN_INT64(arg) __swap_int64(arg) 57087882c2SAxel Dörfler #define B_HOST_TO_BENDIAN_INT32(arg) __swap_int32(arg) 58087882c2SAxel Dörfler #define B_HOST_TO_BENDIAN_INT16(arg) __swap_int16(arg) 59087882c2SAxel Dörfler 60e1f3108aSStephan Aßmus /* Big endian to host native */ 61087882c2SAxel Dörfler #define B_BENDIAN_TO_HOST_DOUBLE(arg) __swap_double(arg) 62087882c2SAxel Dörfler #define B_BENDIAN_TO_HOST_FLOAT(arg) __swap_float(arg) 63087882c2SAxel Dörfler #define B_BENDIAN_TO_HOST_INT64(arg) __swap_int64(arg) 64087882c2SAxel Dörfler #define B_BENDIAN_TO_HOST_INT32(arg) __swap_int32(arg) 65087882c2SAxel Dörfler #define B_BENDIAN_TO_HOST_INT16(arg) __swap_int16(arg) 66087882c2SAxel Dörfler 67e1f3108aSStephan Aßmus #else /* BYTE_ORDER */ 68e1f3108aSStephan Aßmus /* Host is big endian */ 69087882c2SAxel Dörfler 70087882c2SAxel Dörfler #define B_HOST_IS_LENDIAN 0 71087882c2SAxel Dörfler #define B_HOST_IS_BENDIAN 1 72087882c2SAxel Dörfler 73e1f3108aSStephan Aßmus /* Host native to little endian */ 74087882c2SAxel Dörfler #define B_HOST_TO_LENDIAN_DOUBLE(arg) __swap_double(arg) 75087882c2SAxel Dörfler #define B_HOST_TO_LENDIAN_FLOAT(arg) __swap_float(arg) 76087882c2SAxel Dörfler #define B_HOST_TO_LENDIAN_INT64(arg) __swap_int64(arg) 77087882c2SAxel Dörfler #define B_HOST_TO_LENDIAN_INT32(arg) __swap_int32(arg) 78087882c2SAxel Dörfler #define B_HOST_TO_LENDIAN_INT16(arg) __swap_int16(arg) 79087882c2SAxel Dörfler 80e1f3108aSStephan Aßmus /* Little endian to host native */ 81087882c2SAxel Dörfler #define B_LENDIAN_TO_HOST_DOUBLE(arg) __swap_double(arg) 82087882c2SAxel Dörfler #define B_LENDIAN_TO_HOST_FLOAT(arg) __swap_float(arg) 83087882c2SAxel Dörfler #define B_LENDIAN_TO_HOST_INT64(arg) __swap_int64(arg) 84087882c2SAxel Dörfler #define B_LENDIAN_TO_HOST_INT32(arg) __swap_int32(arg) 85087882c2SAxel Dörfler #define B_LENDIAN_TO_HOST_INT16(arg) __swap_int16(arg) 86087882c2SAxel Dörfler 87e1f3108aSStephan Aßmus /* Host native to big endian */ 88087882c2SAxel Dörfler #define B_HOST_TO_BENDIAN_DOUBLE(arg) (double)(arg) 89087882c2SAxel Dörfler #define B_HOST_TO_BENDIAN_FLOAT(arg) (float)(arg) 90087882c2SAxel Dörfler #define B_HOST_TO_BENDIAN_INT64(arg) (uint64)(arg) 91087882c2SAxel Dörfler #define B_HOST_TO_BENDIAN_INT32(arg) (uint32)(arg) 92087882c2SAxel Dörfler #define B_HOST_TO_BENDIAN_INT16(arg) (uint16)(arg) 93087882c2SAxel Dörfler 94e1f3108aSStephan Aßmus /* Big endian to host native */ 95087882c2SAxel Dörfler #define B_BENDIAN_TO_HOST_DOUBLE(arg) (double)(arg) 96087882c2SAxel Dörfler #define B_BENDIAN_TO_HOST_FLOAT(arg) (float)(arg) 97087882c2SAxel Dörfler #define B_BENDIAN_TO_HOST_INT64(arg) (uint64)(arg) 98087882c2SAxel Dörfler #define B_BENDIAN_TO_HOST_INT32(arg) (uint32)(arg) 99087882c2SAxel Dörfler #define B_BENDIAN_TO_HOST_INT16(arg) (uint16)(arg) 100087882c2SAxel Dörfler 101e1f3108aSStephan Aßmus #endif /* BYTE_ORDER */ 102087882c2SAxel Dörfler 103087882c2SAxel Dörfler 104087882c2SAxel Dörfler #ifdef __cplusplus 105087882c2SAxel Dörfler extern "C" { 10629e8fa59SJohn Scipione #endif /* __cplusplus */ 107087882c2SAxel Dörfler 108087882c2SAxel Dörfler extern status_t swap_data(type_code type, void *data, size_t length, 109087882c2SAxel Dörfler swap_action action); 110087882c2SAxel Dörfler extern bool is_type_swapped(type_code type); 111087882c2SAxel Dörfler 112e1f3108aSStephan Aßmus /* Private implementations */ 113087882c2SAxel Dörfler extern double __swap_double(double arg); 114087882c2SAxel Dörfler extern float __swap_float(float arg); 11544ec21c3SJessica Hamilton #if __GNUC__ >= 4 1167dd10738SFrançois Revol #define __swap_int64(arg) (uint64)__builtin_bswap64(arg) 1177dd10738SFrançois Revol #define __swap_int32(arg) (uint32)__builtin_bswap32(arg) 1187dd10738SFrançois Revol #define __swap_int16(arg) (uint16)__builtin_bswap16(arg) 1194ffdf2edSJessica Hamilton #else 120087882c2SAxel Dörfler extern uint64 __swap_int64(uint64 arg); 121087882c2SAxel Dörfler extern uint32 __swap_int32(uint32 arg); 122fad4fc59SJessica Hamilton extern uint16 __swap_int16(uint16 arg); 1234ffdf2edSJessica Hamilton #endif 124087882c2SAxel Dörfler 12552a38012Sejakowatz #ifdef __cplusplus 12652a38012Sejakowatz } 12729e8fa59SJohn Scipione #endif /* __cplusplus */ 12852a38012Sejakowatz 12929e8fa59SJohn Scipione #endif /* _BYTE_ORDER_H */ 130