ByteOrder.h

Go to the documentation of this file.
00001 /*
00002  * Copyright (c) 2005 Palmsource, Inc.
00003  * 
00004  * This software is licensed as described in the file LICENSE, which
00005  * you should have received as part of this distribution. The terms
00006  * are also available at http://www.openbinder.org/license.html.
00007  * 
00008  * This software consists of voluntary contributions made by many
00009  * individuals. For the exact contribution history, see the revision
00010  * history and logs, available at http://www.openbinder.org
00011  */
00012 
00013 #ifndef _SUPPORT_BYTEORDER_H
00014 #define _SUPPORT_BYTEORDER_H
00015 #define _BYTEORDER_H
00016 
00022 #include <support/SupportDefs.h>
00023 #include <support/TypeConstants.h>  /* For convenience */
00024 
00029 #ifdef __cplusplus
00030 extern "C" {
00031 #endif 
00032 
00035 
00036 /*----------------------------------------------------------------------*/
00037 /*------- Swap-direction constants, and swapping functions -------------*/
00038 
00039 typedef enum {
00040     B_SWAP_HOST_TO_LENDIAN,
00041     B_SWAP_HOST_TO_BENDIAN,
00042     B_SWAP_LENDIAN_TO_HOST,
00043     B_SWAP_BENDIAN_TO_HOST,
00044     B_SWAP_ALWAYS
00045 } swap_action;
00046 
00047 extern _IMPEXP_SUPPORT status_t swap_data(  type_code type,
00048                             void *data,
00049                             size_t length,
00050                             swap_action action);
00051 
00052 extern _IMPEXP_SUPPORT int is_type_swapped(type_code type);
00053  
00054  
00055 /*-----------------------------------------------------------------------*/
00056 /*----- Private implementations -----------------------------------------*/
00057 
00058 double _swap_double(double arg);
00059 float  _swap_float(float arg);
00060 uint64_t _swap_int64(uint64_t uarg);
00061 uint32_t _swap_int32(uint32_t uarg);
00062 
00063 /* This one is now always inlined */
00064 INLINE_FNC uint16_t _swap_int16(uint16_t uarg)
00065 {
00066     return (uarg>>8)|((uarg&0xFF)<<8);
00067 }
00068 
00069 /*-------------------------------------------------------------*/
00070 /*--------- Enforced Swapping  --------------------------------*/
00071 
00072 #define B_BYTE_SWAP_DOUBLE(arg)         _swap_double(arg)
00073 #define B_BYTE_SWAP_FLOAT(arg)          _swap_float(arg)
00074 #define B_BYTE_SWAP_INT64(arg)          _swap_int64(arg)
00075 #define B_BYTE_SWAP_INT32(arg)          _swap_int32(arg)
00076 #define B_BYTE_SWAP_INT16(arg)          _swap_int16(arg)
00077 
00078 /*-------------------------------------------------------------*/
00079 /*--------- Host is Little  -----------------------------------*/
00080 
00081 #if BYTE_ORDER == LITTLE_ENDIAN
00082 #define B_HOST_IS_LENDIAN 1
00083 #define B_HOST_IS_BENDIAN 0
00084 
00085 /*--------- Host Native -> Little  --------------------*/
00086 
00087 #define B_HOST_TO_LENDIAN_DOUBLE(arg)   (double)(arg)
00088 #define B_HOST_TO_LENDIAN_FLOAT(arg)    (float)(arg)
00089 #define B_HOST_TO_LENDIAN_INT64(arg)    (uint64_t)(arg)
00090 #define B_HOST_TO_LENDIAN_INT32(arg)    (uint32_t)(arg)
00091 #define B_HOST_TO_LENDIAN_INT16(arg)    (uint16_t)(arg)
00092 
00093 /*--------- Host Native -> Big  ------------------------*/
00094 #define B_HOST_TO_BENDIAN_DOUBLE(arg)   _swap_double(arg)
00095 #define B_HOST_TO_BENDIAN_FLOAT(arg)    _swap_float(arg)
00096 #define B_HOST_TO_BENDIAN_INT64(arg)    _swap_int64(arg)
00097 #define B_HOST_TO_BENDIAN_INT32(arg)    _swap_int32(arg)
00098 #define B_HOST_TO_BENDIAN_INT16(arg)    _swap_int16(arg)
00099 
00100 /*--------- Little -> Host Native ---------------------*/
00101 #define B_LENDIAN_TO_HOST_DOUBLE(arg)   (double)(arg)
00102 #define B_LENDIAN_TO_HOST_FLOAT(arg)    (float)(arg)
00103 #define B_LENDIAN_TO_HOST_INT64(arg)    (uint64_t)(arg)
00104 #define B_LENDIAN_TO_HOST_INT32(arg)    (uint32_t)(arg)
00105 #define B_LENDIAN_TO_HOST_INT16(arg)    (uint16_t)(arg)
00106 
00107 /*--------- Big -> Host Native ------------------------*/
00108 #define B_BENDIAN_TO_HOST_DOUBLE(arg)   _swap_double(arg)
00109 #define B_BENDIAN_TO_HOST_FLOAT(arg)    _swap_float(arg)
00110 #define B_BENDIAN_TO_HOST_INT64(arg)    _swap_int64(arg)
00111 #define B_BENDIAN_TO_HOST_INT32(arg)    _swap_int32(arg)
00112 #define B_BENDIAN_TO_HOST_INT16(arg)    _swap_int16(arg)
00113 
00114 #else /* __LITTLE_ENDIAN */
00115 
00116 
00117 /*-------------------------------------------------------------*/
00118 /*--------- Host is Big  --------------------------------------*/
00119 
00120 #define B_HOST_IS_LENDIAN 0
00121 #define B_HOST_IS_BENDIAN 1
00122 
00123 /*--------- Host Native -> Little  -------------------*/
00124 #define B_HOST_TO_LENDIAN_DOUBLE(arg)   _swap_double(arg)
00125 #define B_HOST_TO_LENDIAN_FLOAT(arg)    _swap_float(arg)
00126 #define B_HOST_TO_LENDIAN_INT64(arg)    _swap_int64(arg)
00127 #define B_HOST_TO_LENDIAN_INT32(arg)    _swap_int32(arg)
00128 #define B_HOST_TO_LENDIAN_INT16(arg)    _swap_int16(arg)
00129 
00130 /*--------- Host Native -> Big  ------------------------*/
00131 #define B_HOST_TO_BENDIAN_DOUBLE(arg)   (double)(arg)
00132 #define B_HOST_TO_BENDIAN_FLOAT(arg)    (float)(arg)
00133 #define B_HOST_TO_BENDIAN_INT64(arg)    (uint64_t)(arg)
00134 #define B_HOST_TO_BENDIAN_INT32(arg)    (uint32_t)(arg)
00135 #define B_HOST_TO_BENDIAN_INT16(arg)    (uint16_t)(arg)
00136 
00137 /*--------- Little -> Host Native ----------------------*/
00138 #define B_LENDIAN_TO_HOST_DOUBLE(arg)   _swap_double(arg)
00139 #define B_LENDIAN_TO_HOST_FLOAT(arg)    _swap_float(arg)
00140 #define B_LENDIAN_TO_HOST_INT64(arg)    _swap_int64(arg)
00141 #define B_LENDIAN_TO_HOST_INT32(arg)    _swap_int32(arg)
00142 #define B_LENDIAN_TO_HOST_INT16(arg)    _swap_int16(arg)
00143 
00144 /*--------- Big -> Host Native -------------------------*/
00145 #define B_BENDIAN_TO_HOST_DOUBLE(arg)   (double)(arg)
00146 #define B_BENDIAN_TO_HOST_FLOAT(arg)    (float)(arg)
00147 #define B_BENDIAN_TO_HOST_INT64(arg)    (uint64_t)(arg)
00148 #define B_BENDIAN_TO_HOST_INT32(arg)    (uint32_t)(arg)
00149 #define B_BENDIAN_TO_HOST_INT16(arg)    (uint16_t)(arg)
00150 
00151 #endif /* __LITTLE_ENDIAN */
00152 
00153 
00154 /*-------------------------------------------------------------*/
00155 /*--------- Just-do-it macros ---------------------------------*/
00156 #define B_SWAP_DOUBLE(arg)   _swap_double(arg)
00157 #define B_SWAP_FLOAT(arg)    _swap_float(arg)
00158 #define B_SWAP_INT64(arg)    _swap_int64(arg)
00159 #define B_SWAP_INT32(arg)    _swap_int32(arg)
00160 #define B_SWAP_INT16(arg)    _swap_int16(arg)
00161 
00163 
00164 #ifdef __cplusplus
00165 }
00166 #endif /* __cplusplus */
00167 
00170 /*-------------------------------------------------------------*/
00171 /*-------------------------------------------------------------*/
00172 
00173 #endif /* _BYTEORDER_H */