Vault
4.1
|
00001 /* 00002 Copyright c1997-2014 Trygve Isaacson. All rights reserved. 00003 This file is part of the Code Vault version 4.1 00004 http://www.bombaydigital.com/ 00005 License: MIT. See LICENSE.md in the Vault top level directory. 00006 */ 00007 00010 #include "vtypes.h" 00011 #include "vtypes_internal.h" 00012 #include "vstring.h" 00013 #include "vexception.h" 00014 00015 #include <iostream> // for namespace std 00016 #include <assert.h> 00017 00018 // Still to be determined is what constant value/type should be used here when 00019 // performing 64-bit VC++ compilation. Until then, the optional "/Wp64" option 00020 // in the 32-bit VC++ compiler ("Detect 64-bit Portability Issues") will emit 00021 // a warning #4312 so we want to disable that for this line of code. The warning 00022 // is because we are using a 32-bit constant 0xFEEEFEEE. Simply making it a 00023 // 64-bit constant is not portable either because that overflows in 32 bits. 00024 #ifdef VCOMPILER_MSVC 00025 #pragma warning(disable: 6001) // VS2010 static analysis is confused by our byte swapping functions. 00026 #pragma warning(disable: 4312) 00027 #endif 00028 const void* const VCPP_DEBUG_BAD_POINTER_VALUE = reinterpret_cast<const void*>(0xFEEEFEEE); 00029 #ifdef VCOMPILER_MSVC 00030 #pragma warning(default: 4312) 00031 #endif 00032 00033 Vu16 vault::VbyteSwap16(Vu16 a16BitValue) { 00034 Vu16 original = a16BitValue; 00035 Vu16 swapped; 00036 Vu8* originalBytes = reinterpret_cast<Vu8*>(&original); 00037 Vu8* swappedBytes = reinterpret_cast<Vu8*>(&swapped); 00038 00039 swappedBytes[0] = originalBytes[1]; 00040 swappedBytes[1] = originalBytes[0]; 00041 00042 return swapped; 00043 } 00044 00045 Vu32 vault::VbyteSwap32(Vu32 a32BitValue) { 00046 Vu32 original = a32BitValue; 00047 Vu32 swapped; 00048 Vu8* originalBytes = reinterpret_cast<Vu8*>(&original); 00049 Vu8* swappedBytes = reinterpret_cast<Vu8*>(&swapped); 00050 00051 swappedBytes[0] = originalBytes[3]; 00052 swappedBytes[1] = originalBytes[2]; 00053 swappedBytes[2] = originalBytes[1]; 00054 swappedBytes[3] = originalBytes[0]; 00055 00056 return swapped; 00057 } 00058 00059 Vu64 vault::VbyteSwap64(Vu64 a64BitValue) { 00060 Vu64 original = a64BitValue; 00061 Vu64 swapped; 00062 Vu8* originalBytes = reinterpret_cast<Vu8*>(&original); 00063 Vu8* swappedBytes = reinterpret_cast<Vu8*>(&swapped); 00064 00065 swappedBytes[0] = originalBytes[7]; 00066 swappedBytes[1] = originalBytes[6]; 00067 swappedBytes[2] = originalBytes[5]; 00068 swappedBytes[3] = originalBytes[4]; 00069 swappedBytes[4] = originalBytes[3]; 00070 swappedBytes[5] = originalBytes[2]; 00071 swappedBytes[6] = originalBytes[1]; 00072 swappedBytes[7] = originalBytes[0]; 00073 00074 return swapped; 00075 } 00076 00077 VFloat vault::VbyteSwapFloat(VFloat a32BitValue) { 00078 /* 00079 The key here is avoid allowing the compiler to do any 00080 conversion of the float to int, which would cause truncation 00081 of the fractional value. That's what happens if you use 00082 VbyteSwap32 because the float gets converted to an int. 00083 */ 00084 VFloat original = a32BitValue; 00085 VFloat swapped; 00086 Vu8* originalBytes = reinterpret_cast<Vu8*>(&original); 00087 Vu8* swappedBytes = reinterpret_cast<Vu8*>(&swapped); 00088 00089 swappedBytes[0] = originalBytes[3]; 00090 swappedBytes[1] = originalBytes[2]; 00091 swappedBytes[2] = originalBytes[1]; 00092 swappedBytes[3] = originalBytes[0]; 00093 00094 return swapped; 00095 } 00096 00097 VDouble vault::VbyteSwapDouble(VDouble a64BitValue) { 00098 /* 00099 The key here is avoid allowing the compiler to do any 00100 conversion of the double to Vs64, which would cause truncation 00101 of the fractional value. That's what happens if you use 00102 VbyteSwap64 because the double gets converted to a Vs64. 00103 */ 00104 VDouble original = a64BitValue; 00105 VDouble swapped; 00106 Vu8* originalBytes = reinterpret_cast<Vu8*>(&original); 00107 Vu8* swappedBytes = reinterpret_cast<Vu8*>(&swapped); 00108 00109 swappedBytes[0] = originalBytes[7]; 00110 swappedBytes[1] = originalBytes[6]; 00111 swappedBytes[2] = originalBytes[5]; 00112 swappedBytes[3] = originalBytes[4]; 00113 swappedBytes[4] = originalBytes[3]; 00114 swappedBytes[5] = originalBytes[2]; 00115 swappedBytes[6] = originalBytes[1]; 00116 swappedBytes[7] = originalBytes[0]; 00117 00118 return swapped; 00119 } 00120 00121 /* 00122 We don't conditionally compile this according to V_DEBUG_STATIC_INITIALIZATION_TRACE; 00123 rather, we always compile it, so that you have the option of turning it on per-file 00124 instead of all-or-nothing. We let the linker decide whether anyone calls it. 00125 */ 00126 int Vtrace(const char* fileName, int lineNumber) { 00127 std::cout << "Static Initialization @ " << fileName << ":" << lineNumber << std::endl; 00128 return 0; 00129 } 00130