github.com/256dpi/max-go@v0.7.0/lib/max/ext_byteorder.h (about)

     1  #ifndef _BYTEORDER_H_
     2  #define _BYTEORDER_H_
     3  
     4  #include "ext_prefix.h"
     5  #include "ext_common.h"
     6  
     7  #ifdef MAC_VERSION
     8  #if TARGET_RT_LITTLE_ENDIAN
     9  #define C74_LITTLE_ENDIAN 1
    10  #define C74_BIG_ENDIAN 0
    11  #else
    12  #define C74_LITTLE_ENDIAN 0
    13  #define C74_BIG_ENDIAN 1
    14  #endif
    15  
    16  #elif defined (__BYTE_ORDER__)
    17  
    18  #define C74_LITTLE_ENDIAN (__BYTE_ORDER__ == __ORDER_LITTLE_ENDIAN__)
    19  #define C74_BIG_ENDIAN (__BYTE_ORDER__ == __ORDER_BIG_ENDIAN__)
    20  
    21  #else // WIN_VERSION
    22  
    23  /**
    24  	A macro that indicates whether or not the current architecture uses Litte-endian byte ordering
    25  	(such as is used on an i386 processor).
    26  	Note that this macro is always defined; it will be either a 0 or a 1.
    27  	@ingroup byteorder
    28  */
    29  #define C74_LITTLE_ENDIAN 1
    30  
    31  /**
    32  	A macro that indicates whether or not the current architecture uses Big-endian byte ordering
    33  	(such as is used on a PPC processor).
    34  	Note that this macro is always defined; it will be either a 0 or a 1.
    35  	@ingroup byteorder
    36  */
    37  #define C74_BIG_ENDIAN 0
    38  
    39  #endif // WIN_VERSION
    40  
    41  
    42  /**
    43  	Switch the byte ordering of a short integer.
    44  	@ingroup	byteorder
    45  	@param	x	A short integer.
    46  	@return		A short integer with the byte-ordering swapped.
    47  */
    48  #define BYTEORDER_SWAPW16(x) ((t_int16)(((((t_uint16)(x))>>8)&0x00ff)+ \
    49  										((((t_uint16)(x))<<8)&0xff00)))
    50  
    51  
    52  /**
    53  	Switch the byte ordering of an integer.
    54  	@ingroup	byteorder
    55  	@param	x	An integer.
    56  	@return		An integer with the byte-ordering swapped.
    57  */
    58  #define BYTEORDER_SWAPW32(x) ((t_int32)(((((t_uint32)(x))>>24L)&0x000000ff)+ \
    59  										((((t_uint32)(x))>> 8L)&0x0000ff00)+ \
    60  										((((t_uint32)(x))<< 8L)&0x00ff0000)+ \
    61  										((((t_uint32)(x))<<24L)&0xff000000)))
    62  
    63  
    64  /**
    65  	 Switch the byte ordering of an integer.
    66  	 @ingroup	byteorder
    67  	 @param	x	An integer.
    68  	 @return	An integer with the byte-ordering swapped.
    69  */
    70  #define BYTEORDER_SWAPW64(x) ((t_int64)(((((t_uint64)(x))>>56L)&0x00000000000000ff)+ \
    71  										((((t_uint64)(x))>>40L)&0x000000000000ff00)+ \
    72  										((((t_uint64)(x))>>24L)&0x0000000000ff0000)+ \
    73  										((((t_uint64)(x))>> 8L)&0x00000000ff000000)+ \
    74  										((((t_uint64)(x))<< 8L)&0x000000ff00000000)+ \
    75  										((((t_uint64)(x))<<24L)&0x0000ff0000000000)+ \
    76  										((((t_uint64)(x))<<40L)&0x00ff000000000000)+ \
    77  										((((t_uint64)(x))<<56L)&0xff00000000000000)))
    78  
    79  
    80  /**
    81  	Switch the byte ordering of a float.
    82  	@ingroup	byteorder
    83  	@param	x	A float.
    84  	@return		A float with the byte-ordering swapped.
    85  */
    86  #define BYTEORDER_SWAPF32 byteorder_swapf32
    87  
    88  /**
    89  	Switch the byte ordering of a double.
    90  	@ingroup	byteorder
    91  	@param	x	A double.
    92  	@return		A double.
    93  */
    94  #define BYTEORDER_SWAPF64 byteorder_swapf64
    95  
    96  // To be documented
    97  #define BYTEORDER_SWAPF32_PTR(x) 			byteorder_swap_pointer_32((unsigned char*)(x))
    98  #define BYTEORDER_SWAPF64_PTR(x) 			byteorder_swap_pointer_64((unsigned char*)(x))
    99  #define BYTEORDER_SWAPF32_PTR_TO_PTR(x, y) 	byteorder_swap_pointer_32_copy((unsigned char*)(x), (unsigned char*)(y))
   100  #define BYTEORDER_SWAPF64_PTR_TO_PTR(x, y) 	byteorder_swap_pointer_64_copy((unsigned char*)(x), (unsigned char*)(y))
   101  #define BYTEORDER_SWAPF32_FROM_PTR(x) 		byteorder_swap_pointer_32_to_float32((unsigned char*)(x))
   102  #define BYTEORDER_SWAPF64_FROM_PTR(x) 		byteorder_swap_pointer_64_to_float64((unsigned char*)(x))
   103  
   104  
   105  BEGIN_USING_C_LINKAGE
   106  
   107  // Undocumented, use the above macros.
   108  // OBSOLETE. don't use. not safe to pass byteswapped floats in registers
   109  C74_DEPRECATED( float byteorder_swapf32(float f) );
   110  C74_DEPRECATED( double byteorder_swapf64(double f) );
   111  C74_DEPRECATED( float swapf32(float f) );
   112  C74_DEPRECATED( double swapf64(double f) );
   113  
   114  // these functions are inplace
   115  void byteorder_swap_pointer_16(unsigned char *p);
   116  void byteorder_swap_pointer_32(unsigned char *p);
   117  void byteorder_swap_pointer_64(unsigned char *p);
   118  // these function copy from one pointer to another (src!=dst)
   119  void byteorder_swap_pointer_16_copy(unsigned char *src, unsigned char *dst);
   120  void byteorder_swap_pointer_32_copy(unsigned char *src, unsigned char *dst);
   121  void byteorder_swap_pointer_64_copy(unsigned char *src, unsigned char *dst);
   122  // these functions return floating point values from a byteswapped pointer
   123  float byteorder_swap_pointer_32_to_float32(unsigned char *p);
   124  double byteorder_swap_pointer_64_to_float64(unsigned char *p);
   125  
   126  END_USING_C_LINKAGE
   127  
   128  
   129  #if C74_BIG_ENDIAN
   130  #define BYTEORDER_LSBW16(x) 				BYTEORDER_SWAPW16(x)
   131  #define BYTEORDER_LSBW32(x) 				BYTEORDER_SWAPW32(x)
   132  #define BYTEORDER_LSBF32(x) 				BYTEORDER_SWAPF32(x)
   133  #define BYTEORDER_LSBF64(x) 				BYTEORDER_SWAPF64(x)
   134  #define BYTEORDER_LSBF32_PTR(x) 			BYTEORDER_SWAPF32_PTR((unsigned char *)(x))
   135  #define BYTEORDER_LSBF64_PTR(x) 			BYTEORDER_SWAPF64_PTR((unsigned char *)(x))
   136  #define BYTEORDER_LSBF32_FROM_PTR(x) 		BYTEORDER_SWAPF32_FROM_PTR((unsigned char *)(x))
   137  #define BYTEORDER_LSBF64_FROM_PTR(x) 		BYTEORDER_SWAPF64_FROM_PTR((unsigned char *)(x))
   138  #define BYTEORDER_LSBF32_PTR_TO_PTR(x,y) 	BYTEORDER_SWAPF32_PTR_TO_PTR((unsigned char *)(x),(unsigned char *)(y))
   139  #define BYTEORDER_LSBF64_PTR_TO_PTR(x,y) 	BYTEORDER_SWAPF64_PTR_TO_PTR((unsigned char *)(x),(unsigned char *)(y))
   140  #define BYTEORDER_MSBW16(x) 				(x)
   141  #define BYTEORDER_MSBW32(x) 				(x)
   142  #define BYTEORDER_MSBW64(x)					(x)
   143  #define BYTEORDER_MSBF32(x)					(x)
   144  #define BYTEORDER_MSBF64(x) 				(x)
   145  #define BYTEORDER_MSBF32_PTR(x) 		
   146  #define BYTEORDER_MSBF64_PTR(x) 		
   147  #define BYTEORDER_MSBF32_FROM_PTR(x) 		(*(float *)(x))
   148  #define BYTEORDER_MSBF64_FROM_PTR(x) 		(*(double *)(x))
   149  #define BYTEORDER_MSBF32_PTR_TO_PTR(x,y) 	((*(float *)(y))=(*(float *)(x)))
   150  #define BYTEORDER_MSBF64_PTR_TO_PTR(x,y) 	((*(double *)(y))=(*(double *)(x)))
   151  #else
   152  /**
   153  	Switch the byte ordering of a short integer from the native swapping to Little-endian (Least Significant Byte).
   154  	If the current environment is already Little-endian, then the returned value is not byteswapped.
   155  	
   156  	@ingroup	byteorder
   157  	@param	x	A short integer.
   158  	@return		A short integer with the byte-ordering swapped if neccessary.
   159  */
   160  #define BYTEORDER_LSBW16(x) 		(x)
   161  
   162  /**
   163  	Switch the byte ordering of an integer from the native swapping to Little-endian (Least Significant Byte).
   164  	If the current environment is already Little-endian, then the returned value is not byteswapped.
   165  	
   166  	@ingroup	byteorder
   167  	@param	x	An integer.
   168  	@return		An integer with the byte-ordering swapped if neccessary.
   169  */
   170  #define BYTEORDER_LSBW32(x) 		(x)
   171  
   172  /**
   173  	Switch the byte ordering of an integer from the native swapping to Little-endian (Least Significant Byte).
   174  	If the current environment is already Little-endian, then the returned value is not byteswapped.
   175   
   176  	@ingroup	byteorder
   177  	@param	x	An integer.
   178  	@return		An integer with the byte-ordering swapped if neccessary.
   179  */
   180  #define BYTEORDER_LSBW64(x) 		(x)
   181  
   182  /**
   183  	Switch the byte ordering of a float from the native swapping to Little-endian (Least Significant Byte).
   184  	If the current environment is already Little-endian, then the returned value is not byteswapped.
   185  	
   186  	@ingroup	byteorder
   187  	@param	x	A float.
   188  	@return		A float with the byte-ordering swapped if neccessary.
   189  */
   190  #define BYTEORDER_LSBF32(x) 		(x)
   191  
   192  /**
   193  	Switch the byte ordering of a double from the native swapping to Little-endian (Least Significant Byte).
   194  	If the current environment is already Little-endian, then the returned value is not byteswapped.
   195  	
   196  	@ingroup	byteorder
   197  	@param	x	A double.
   198  	@return		A double with the byte-ordering swapped if neccessary.
   199  */
   200  #define BYTEORDER_LSBF64(x) 		(x)
   201  
   202  /**
   203  	Switch the byte ordering of a short integer from the native swapping to Big-endian (Most Significant Byte).
   204  	If the current environment is already Big-endian, then the returned value is not byteswapped.
   205  	
   206  	@ingroup	byteorder
   207  	@param	x	A short integer.
   208  	@return		A short integer with the byte-ordering swapped if neccessary.
   209  */
   210  #define BYTEORDER_MSBW16(x) 		BYTEORDER_SWAPW16(x)
   211  
   212  /**
   213  	Switch the byte ordering of an integer from the native swapping to Big-endian (Most Significant Byte).
   214  	If the current environment is already Big-endian, then the returned value is not byteswapped.
   215  	
   216  	@ingroup	byteorder
   217  	@param	x	An integer.
   218  	@return		An integer with the byte-ordering swapped if neccessary.
   219  */
   220  #define BYTEORDER_MSBW32(x) 		BYTEORDER_SWAPW32(x)
   221  
   222  /**
   223  	Switch the byte ordering of an integer from the native swapping to Big-endian (Most Significant Byte).
   224  	If the current environment is already Big-endian, then the returned value is not byteswapped.
   225   
   226  	@ingroup	byteorder
   227  	@param	x	An integer.
   228  	@return		An integer with the byte-ordering swapped if neccessary.
   229  */
   230  #define BYTEORDER_MSBW64(x) 		BYTEORDER_SWAPW64(x)
   231  
   232  /**
   233  	Switch the byte ordering of a float from the native swapping to Big-endian (Most Significant Byte).
   234  	If the current environment is already Big-endian, then the returned value is not byteswapped.
   235  	
   236  	@ingroup	byteorder
   237  	@param	x	A float.
   238  	@return		A float with the byte-ordering swapped if neccessary.
   239  */
   240  #define BYTEORDER_MSBF32(x) 		BYTEORDER_SWAPF32(x)
   241  
   242  /**
   243  	Switch the byte ordering of a double from the native swapping to Big-endian (Most Significant Byte).
   244  	If the current environment is already Big-endian, then the returned value is not byteswapped.
   245  	
   246  	@ingroup	byteorder
   247  	@param	x	A double.
   248  	@return		A double with the byte-ordering swapped if neccessary.
   249  */
   250  #define BYTEORDER_MSBF64(x) 		BYTEORDER_SWAPF64(x)
   251  
   252  
   253  // To be documented.
   254  #define BYTEORDER_LSBF32_PTR(x) 		
   255  #define BYTEORDER_LSBF64_PTR(x) 		
   256  #define BYTEORDER_LSBF32_FROM_PTR(x) 		(*(float *)(x))
   257  #define BYTEORDER_LSBF64_FROM_PTR(x) 		(*(double *)(x))
   258  #define BYTEORDER_LSBF32_PTR_TO_PTR(x,y) 	((*(float *)(y))=(*(float *)(x)))
   259  #define BYTEORDER_LSBF64_PTR_TO_PTR(x,y) 	((*(double *)(y))=(*(double *)(x)))
   260  #define BYTEORDER_MSBF32_PTR(x) 			BYTEORDER_SWAPF32_PTR((unsigned char *)(x))
   261  #define BYTEORDER_MSBF64_PTR(x) 			BYTEORDER_SWAPF64_PTR((unsigned char *)(x))
   262  #define BYTEORDER_MSBF32_FROM_PTR(x) 		BYTEORDER_SWAPF32_FROM_PTR((unsigned char *)(x))
   263  #define BYTEORDER_MSBF64_FROM_PTR(x) 		BYTEORDER_SWAPF64_FROM_PTR((unsigned char *)(x))
   264  #define BYTEORDER_MSBF32_PTR_TO_PTR(x,y) 	BYTEORDER_SWAPF32_PTR_TO_PTR((unsigned char *)(x),(unsigned char *)(y))
   265  #define BYTEORDER_MSBF64_PTR_TO_PTR(x,y) 	BYTEORDER_SWAPF64_PTR_TO_PTR((unsigned char *)(x),(unsigned char *)(y))
   266  
   267  #endif
   268  
   269  
   270  #if C74_LITTLE_ENDIAN
   271  
   272  #define C74_FOUR_CHAR_CODE(x)		(x)
   273  
   274  
   275  #define STR_TO_FOURCC(x)	((x) = \
   276  							(((t_uint32) ((x) & 0x000000FF)) << 24) | \
   277  							(((t_uint32) ((x) & 0x0000FF00)) << 8)  | \
   278  							(((t_uint32) ((x) & 0x00FF0000)) >> 8)  | \
   279  							(((t_uint32) ((x) & 0xFF000000)) >> 24))\
   280  							
   281  #else
   282  
   283  #define C74_FOUR_CHAR_CODE(x)   (((t_uint32) ((x) & 0x000000FF)) << 24) \
   284  								| (((t_uint32) ((x) & 0x0000FF00)) << 8) \
   285  								| (((t_uint32) ((x) & 0x00FF0000)) >> 8) \
   286  								| (((t_uint32) ((x) & 0xFF000000)) >> 24)
   287  
   288  /*
   289  	Swap the byte ordering, if neccessary, for a Four Character Code.
   290  	The bytes are swapped in-place, thus there is no return value.
   291  	
   292  	@ingroup byteorder
   293  	@param	x	An integer (32bit) containing the four char code.  
   294  				If byte-swapping is needed, it will be byte-swapped in-place.
   295  */
   296  #define STR_TO_FOURCC(x)	(x)
   297  #endif // C74_LITTLE_ENDIAN
   298  
   299  #ifndef FOUR_CHAR_CODE
   300  #define FOUR_CHAR_CODE(x)		C74_FOUR_CHAR_CODE(x)
   301  #endif
   302  
   303  #endif // _BYTEORDER_H