endian.h
Engine/source/core/util/endian.h
Public Functions
endianSwap(const F32 in_swap)
endianSwap(const F64 in_swap)
endianSwap(const S16 in_swap)
endianSwap(const S32 in_swap)
endianSwap(const S64 in_swap)
endianSwap(const S8 in_swap)
endianSwap(const U16 in_swap)
Convert the byte ordering on the U16 to and from big/little endian format.
endianSwap(const U32 in_swap)
Convert the byte ordering on the U32 to and from big/little endian format.
endianSwap(const U64 in_swap)
endianSwap(const U8 in_swap)
Detailed Description
Public Functions
endianSwap(const F32 in_swap)
endianSwap(const F64 in_swap)
endianSwap(const S16 in_swap)
endianSwap(const S32 in_swap)
endianSwap(const S64 in_swap)
endianSwap(const S8 in_swap)
endianSwap(const U16 in_swap)
Convert the byte ordering on the U16 to and from big/little endian format.
Parameters:
in_swap | Any U16 |
return:
swapped U16.
endianSwap(const U32 in_swap)
Convert the byte ordering on the U32 to and from big/little endian format.
Parameters:
in_swap | Any U32 |
return:
swapped U32.
endianSwap(const U64 in_swap)
endianSwap(const U8 in_swap)
1 2//----------------------------------------------------------------------------- 3// Copyright (c) 2012 GarageGames, LLC 4// 5// Permission is hereby granted, free of charge, to any person obtaining a copy 6// of this software and associated documentation files (the "Software"), to 7// deal in the Software without restriction, including without limitation the 8// rights to use, copy, modify, merge, publish, distribute, sublicense, and/or 9// sell copies of the Software, and to permit persons to whom the Software is 10// furnished to do so, subject to the following conditions: 11// 12// The above copyright notice and this permission notice shall be included in 13// all copies or substantial portions of the Software. 14// 15// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 16// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 17// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 18// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 19// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING 20// FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS 21// IN THE SOFTWARE. 22//----------------------------------------------------------------------------- 23 24#ifndef _ENDIAN_H_ 25#define _ENDIAN_H_ 26 27#ifndef _TORQUE_TYPES_H_ 28#include "platform/types.h" 29#endif 30 31//------------------------------------------------------------------------------ 32// Endian conversions 33 34inline U8 endianSwap(const U8 in_swap) 35{ 36 return in_swap; 37} 38 39inline S8 endianSwap(const S8 in_swap) 40{ 41 return in_swap; 42} 43 44/** 45Convert the byte ordering on the U16 to and from big/little endian format. 46@param in_swap Any U16 47@returns swapped U16. 48*/ 49 50inline U16 endianSwap(const U16 in_swap) 51{ 52 return U16(((in_swap >> 8) & 0x00ff) | 53 ((in_swap << 8) & 0xff00)); 54} 55 56inline S16 endianSwap(const S16 in_swap) 57{ 58 return S16(endianSwap(U16(in_swap))); 59} 60 61/** 62Convert the byte ordering on the U32 to and from big/little endian format. 63@param in_swap Any U32 64@returns swapped U32. 65*/ 66inline U32 endianSwap(const U32 in_swap) 67{ 68 return U32(((in_swap >> 24) & 0x000000ff) | 69 ((in_swap >> 8) & 0x0000ff00) | 70 ((in_swap << 8) & 0x00ff0000) | 71 ((in_swap << 24) & 0xff000000)); 72} 73 74inline S32 endianSwap(const S32 in_swap) 75{ 76 return S32(endianSwap(U32(in_swap))); 77} 78 79inline U64 endianSwap(const U64 in_swap) 80{ 81 U32 *inp = (U32 *) &in_swap; 82 U64 ret; 83 U32 *outp = (U32 *) &ret; 84 outp[0] = endianSwap(inp[1]); 85 outp[1] = endianSwap(inp[0]); 86 return ret; 87} 88 89inline S64 endianSwap(const S64 in_swap) 90{ 91 return S64(endianSwap(U64(in_swap))); 92} 93 94inline F32 endianSwap(const F32 in_swap) 95{ 96 U32 result = endianSwap(* ((U32 *) &in_swap) ); 97 return * ((F32 *) &result); 98} 99 100inline F64 endianSwap(const F64 in_swap) 101{ 102 U64 result = endianSwap(* ((U64 *) &in_swap) ); 103 return * ((F64 *) &result); 104} 105 106//------------------------------------------------------------------------------ 107// Endian conversions 108 109#ifdef TORQUE_LITTLE_ENDIAN 110 111#define TORQUE_DECLARE_TEMPLATIZED_ENDIAN_CONV(type) \ 112 inline type convertHostToLEndian(type i) { return i; } \ 113 inline type convertLEndianToHost(type i) { return i; } \ 114 inline type convertHostToBEndian(type i) { return endianSwap(i); } \ 115 inline type convertBEndianToHost(type i) { return endianSwap(i); } 116 117#elif defined(TORQUE_BIG_ENDIAN) 118 119#define TORQUE_DECLARE_TEMPLATIZED_ENDIAN_CONV(type) \ 120 inline type convertHostToLEndian(type i) { return endianSwap(i); } \ 121 inline type convertLEndianToHost(type i) { return endianSwap(i); } \ 122 inline type convertHostToBEndian(type i) { return i; } \ 123 inline type convertBEndianToHost(type i) { return i; } 124 125#else 126#error "Endian define not set!" 127#endif 128 129 130TORQUE_DECLARE_TEMPLATIZED_ENDIAN_CONV(U8) 131TORQUE_DECLARE_TEMPLATIZED_ENDIAN_CONV(S8) 132TORQUE_DECLARE_TEMPLATIZED_ENDIAN_CONV(U16) 133TORQUE_DECLARE_TEMPLATIZED_ENDIAN_CONV(S16) 134TORQUE_DECLARE_TEMPLATIZED_ENDIAN_CONV(U32) 135TORQUE_DECLARE_TEMPLATIZED_ENDIAN_CONV(S32) 136TORQUE_DECLARE_TEMPLATIZED_ENDIAN_CONV(U64) 137TORQUE_DECLARE_TEMPLATIZED_ENDIAN_CONV(S64) 138TORQUE_DECLARE_TEMPLATIZED_ENDIAN_CONV(F32) 139TORQUE_DECLARE_TEMPLATIZED_ENDIAN_CONV(F64) 140 141#endif 142 143