00001 /* 00002 ============================================================================== 00003 00004 This file is part of the JUCE library - "Jules' Utility Class Extensions" 00005 Copyright 2004-7 by Raw Material Software ltd. 00006 00007 ------------------------------------------------------------------------------ 00008 00009 JUCE can be redistributed and/or modified under the terms of the 00010 GNU General Public License, as published by the Free Software Foundation; 00011 either version 2 of the License, or (at your option) any later version. 00012 00013 JUCE is distributed in the hope that it will be useful, 00014 but WITHOUT ANY WARRANTY; without even the implied warranty of 00015 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 00016 GNU General Public License for more details. 00017 00018 You should have received a copy of the GNU General Public License 00019 along with JUCE; if not, visit www.gnu.org/licenses or write to the 00020 Free Software Foundation, Inc., 59 Temple Place, Suite 330, 00021 Boston, MA 02111-1307 USA 00022 00023 ------------------------------------------------------------------------------ 00024 00025 If you'd like to release a closed-source product which uses JUCE, commercial 00026 licenses are also available: visit www.rawmaterialsoftware.com/juce for 00027 more information. 00028 00029 ============================================================================== 00030 */ 00031 00032 #ifndef __JUCE_DATACONVERSIONS_JUCEHEADER__ 00033 #define __JUCE_DATACONVERSIONS_JUCEHEADER__ 00034 00035 #include "juce_PlatformDefs.h" 00036 00037 #if JUCE_USE_INTRINSICS 00038 #pragma intrinsic (_byteswap_ulong) 00039 #endif 00040 00041 //============================================================================== 00042 // Endianness conversions.. 00043 00045 forcedinline uint32 swapByteOrder (uint32 n) throw() 00046 { 00047 #if JUCE_MAC 00048 // Mac version 00049 return CFSwapInt32 (n); 00050 #elif JUCE_GCC 00051 // Inpenetrable GCC version.. 00052 asm("bswap %%eax" : "=a"(n) : "a"(n)); 00053 return n; 00054 #elif JUCE_USE_INTRINSICS 00055 // Win32 intrinsics version.. 00056 return _byteswap_ulong (n); 00057 #else 00058 // Win32 version.. 00059 __asm { 00060 mov eax, n 00061 bswap eax 00062 mov n, eax 00063 } 00064 return n; 00065 #endif 00066 } 00067 00069 inline uint16 swapByteOrder (const uint16 n) throw() 00070 { 00071 #if JUCE_USE_INTRINSICSxxx // agh - the MS compiler has an internal error when you try to use this intrinsic! 00072 // Win32 intrinsics version.. 00073 return (uint16) _byteswap_ushort (n); 00074 #else 00075 return (uint16) ((n << 8) | (n >> 8)); 00076 #endif 00077 } 00078 00079 inline uint64 swapByteOrder (const uint64 value) throw() 00080 { 00081 #if JUCE_MAC 00082 return CFSwapInt64 (value); 00083 #elif JUCE_USE_INTRINSICS 00084 return _byteswap_uint64 (value); 00085 #else 00086 return (((int64) swapByteOrder ((uint32) value)) << 32) 00087 | swapByteOrder ((uint32) (value >> 32)); 00088 #endif 00089 } 00090 00091 #if JUCE_LITTLE_ENDIAN 00092 00093 inline uint16 swapIfBigEndian (const uint16 v) throw() { return v; } 00095 inline uint32 swapIfBigEndian (const uint32 v) throw() { return v; } 00097 inline uint64 swapIfBigEndian (const uint64 v) throw() { return v; } 00098 00100 inline uint16 swapIfLittleEndian (const uint16 v) throw() { return swapByteOrder (v); } 00102 inline uint32 swapIfLittleEndian (const uint32 v) throw() { return swapByteOrder (v); } 00104 inline uint64 swapIfLittleEndian (const uint64 v) throw() { return swapByteOrder (v); } 00105 00107 inline uint32 littleEndianInt (const char* const bytes) throw() { return *(uint32*) bytes; } 00109 inline uint16 littleEndianShort (const char* const bytes) throw() { return *(uint16*) bytes; } 00110 00112 inline uint32 bigEndianInt (const char* const bytes) throw() { return swapByteOrder (*(uint32*) bytes); } 00114 inline uint16 bigEndianShort (const char* const bytes) throw() { return swapByteOrder (*(uint16*) bytes); } 00115 00116 #else 00117 00118 inline uint16 swapIfBigEndian (const uint16 v) throw() { return swapByteOrder (v); } 00120 inline uint32 swapIfBigEndian (const uint32 v) throw() { return swapByteOrder (v); } 00122 inline uint64 swapIfBigEndian (const uint64 v) throw() { return swapByteOrder (v); } 00123 00125 inline uint16 swapIfLittleEndian (const uint16 v) throw() { return v; } 00127 inline uint32 swapIfLittleEndian (const uint32 v) throw() { return v; } 00129 inline uint64 swapIfLittleEndian (const uint64 v) throw() { return v; } 00130 00132 inline uint32 littleEndianInt (const char* const bytes) throw() { return swapByteOrder (*(uint32*) bytes); } 00134 inline uint16 littleEndianShort (const char* const bytes) throw() { return swapByteOrder (*(uint16*) bytes); } 00135 00137 inline uint32 bigEndianInt (const char* const bytes) throw() { return *(uint32*) bytes; } 00139 inline uint16 bigEndianShort (const char* const bytes) throw() { return *(uint16*) bytes; } 00140 #endif 00141 00143 inline int littleEndian24Bit (const char* const bytes) throw() { return (((int) bytes[2]) << 16) | (((uint32) (uint8) bytes[1]) << 8) | ((uint32) (uint8) bytes[0]); } 00145 inline int bigEndian24Bit (const char* const bytes) throw() { return (((int) bytes[0]) << 16) | (((uint32) (uint8) bytes[1]) << 8) | ((uint32) (uint8) bytes[2]); } 00146 00148 inline void littleEndian24BitToChars (const int value, char* const destBytes) throw() { destBytes[0] = (char)(value & 0xff); destBytes[1] = (char)((value >> 8) & 0xff); destBytes[2] = (char)((value >> 16) & 0xff); } 00150 inline void bigEndian24BitToChars (const int value, char* const destBytes) throw() { destBytes[0] = (char)((value >> 16) & 0xff); destBytes[1] = (char)((value >> 8) & 0xff); destBytes[2] = (char)(value & 0xff); } 00151 00152 00153 //============================================================================== 00165 inline int roundDoubleToInt (const double value) throw() 00166 { 00167 union { int asInt[2]; double asDouble; } n; 00168 n.asDouble = value + 6755399441055744.0; 00169 00170 #if JUCE_BIG_ENDIAN 00171 return n.asInt [1]; 00172 #else 00173 return n.asInt [0]; 00174 #endif 00175 } 00176 00182 inline int roundDoubleToIntAccurate (const double value) throw() 00183 { 00184 return roundDoubleToInt (value + 1.5e-8); 00185 } 00186 00197 inline int roundFloatToInt (const float value) throw() 00198 { 00199 union { int asInt[2]; double asDouble; } n; 00200 n.asDouble = value + 6755399441055744.0; 00201 00202 #if JUCE_BIG_ENDIAN 00203 return n.asInt [1]; 00204 #else 00205 return n.asInt [0]; 00206 #endif 00207 } 00208 00209 00210 #endif // __JUCE_DATACONVERSIONS_JUCEHEADER__