Update of /cvsroot/opentnl/tnl/tnl
In directory sc8-pr-cvs1.sourceforge.net:/tmp/cvs-serv6106/tnl
Modified Files:
bitStream.cpp journal.cpp platform.cpp tnlBitStream.h
tnlEndian.h tnlJournal.h udp.cpp
Log Message:
Cleaned up endian file and added swaps for types > 4 bytes
Fully implemented journal recording and playback
Index: platform.cpp
===================================================================
RCS file: /cvsroot/opentnl/tnl/tnl/platform.cpp,v
retrieving revision 1.2
retrieving revision 1.3
diff -C2 -d -r1.2 -r1.3
*** platform.cpp 25 Apr 2004 19:08:24 -0000 1.2
--- platform.cpp 11 May 2004 02:45:26 -0000 1.3
***************
*** 27,30 ****
--- 27,31 ----
#include "tnlTypes.h"
#include "tnl.h"
+ #include "tnlJournal.h"
#include <string.h>
***************
*** 73,77 ****
U32 Platform::getRealMilliseconds()
{
! return GetTickCount();
}
--- 74,91 ----
U32 Platform::getRealMilliseconds()
{
! U32 tickCount;
! TNL_JOURNAL_READ_BLOCK
! (
! TNL_JOURNAL_READ( (&tickCount) );
! return tickCount;
! )
!
! tickCount = GetTickCount();
!
! TNL_JOURNAL_WRITE_BLOCK
! (
! TNL_JOURNAL_WRITE(tickCount);
! )
! return tickCount;
}
***************
*** 127,131 ****
U32 Platform::getRealMilliseconds()
{
! return GetTickCount();
}
--- 141,158 ----
U32 Platform::getRealMilliseconds()
{
! U32 tickCount;
! TNL_JOURNAL_READ_BLOCK
! (
! TNL_JOURNAL_READ( (&tickCount) );
! return tickCount;
! )
!
! tickCount = GetTickCount();
!
! TNL_JOURNAL_WRITE_BLOCK
! (
! TNL_JOURNAL_WRITE( (tickCount) );
! )
! return tickCount;
}
***************
*** 168,177 ****
S64 Platform::getHighPrecisionTimerValue()
{
! return gTimer.getCurrentTime();
}
F64 Platform::getHighPrecisionMilliseconds(S64 timerDelta)
{
! return gTimer.convertToMS(timerDelta);
}
--- 195,232 ----
S64 Platform::getHighPrecisionTimerValue()
{
! S64 currentTime;
! TNL_JOURNAL_READ_BLOCK
! (
! TNL_JOURNAL_READ( (¤tTime) );
! return currentTime;
! )
!
! currentTime = gTimer.getCurrentTime();
!
! TNL_JOURNAL_WRITE_BLOCK
! (
! TNL_JOURNAL_WRITE( (currentTime) );
! )
!
! return currentTime;
}
F64 Platform::getHighPrecisionMilliseconds(S64 timerDelta)
{
! F64 timerValue;
! TNL_JOURNAL_READ_BLOCK
! (
! TNL_JOURNAL_READ( (&timerValue) );
! return timerValue;
! )
!
! timerValue = gTimer.convertToMS(timerDelta);
!
! TNL_JOURNAL_WRITE_BLOCK
! (
! TNL_JOURNAL_WRITE( (timerValue) );
! )
!
! return timerValue;
}
Index: tnlJournal.h
===================================================================
RCS file: /cvsroot/opentnl/tnl/tnl/tnlJournal.h,v
retrieving revision 1.1
retrieving revision 1.2
diff -C2 -d -r1.1 -r1.2
*** tnlJournal.h 10 May 2004 22:34:23 -0000 1.1
--- tnlJournal.h 11 May 2004 02:45:26 -0000 1.2
***************
*** 43,50 ****
class Journal : public Object
{
! FILE *journalFile;
! BitStream readStream;
! BitStream writeStream;
public:
void record(const char *fileName);
void load(const char *fileName);
--- 43,63 ----
class Journal : public Object
{
! static FILE *mJournalFile;
! static BitStream mReadStream;
! static BitStream mWriteStream;
! static Journal *mJournal;
! static U32 mWritePosition;
public:
+ enum Mode
+ {
+ Inactive,
+ Record,
+ Playback,
+ };
+ protected:
+ static Mode mCurrentMode;
+ static bool mInsideEntrypoint;
+ public:
+ Journal();
void record(const char *fileName);
void load(const char *fileName);
***************
*** 52,55 ****
--- 65,76 ----
void callEntry(const char *funcName, MarshalledCall *theCall);
void processNextJournalEntry();
+
+ static Mode getCurrentMode() { return mCurrentMode; }
+ static Journal *get() { return mJournal; }
+ static void checkReadPosition();
+ static void syncWriteStream();
+ static BitStream *getReadStream() { return &mReadStream; }
+ static BitStream *getWriteStream() { return &mWriteStream; }
+ static bool isInEntrypoint() { return mInsideEntrypoint; }
};
***************
*** 57,66 ****
{
const char *mFunctionName;
JournalEntryRecord *mNext;
static JournalEntryRecord *mList;
! JournalEntryRecord(const char *functionName)
{
mFunctionName = functionName;
mNext = mList;
mList = this;
--- 78,89 ----
{
const char *mFunctionName;
+ MethodArgList *mMethodArgList;
JournalEntryRecord *mNext;
static JournalEntryRecord *mList;
! JournalEntryRecord(const char *functionName, MethodArgList *methodArgList)
{
mFunctionName = functionName;
+ mMethodArgList = methodArgList;
mNext = mList;
mList = this;
***************
*** 83,88 ****
if(sizeof(fptr) > sizeof(U32)) m.v2 = *(((U32 *) &fptr) + 1); \
}; \
! Journal_##className##_##func##_er(const char *name) : JournalEntryRecord(name) {} \
! } gJournal_##className##_##func##_er(#func); \
void FN_CDECL className::func args { \
SAVE_PARAMS \
--- 106,111 ----
if(sizeof(fptr) > sizeof(U32)) m.v2 = *(((U32 *) &fptr) + 1); \
}; \
! Journal_##className##_##func##_er(const char *name, MethodArgList *methodArgList) : JournalEntryRecord(name, methodArgList) {} \
! } gJournal_##className##_##func##_er(#func, &Journal_##className##_##func); \
void FN_CDECL className::func args { \
SAVE_PARAMS \
***************
*** 93,96 ****
--- 116,154 ----
void FN_CDECL className::func##_body args
+ #define TNL_JOURNAL_WRITE_BLOCK(x) \
+ { \
+ if(TNL::Journal::getCurrentMode() == TNL::Journal::Record && TNL::Journal::isInEntrypoint()) \
+ { \
+ x \
+ } \
+ TNL::Journal::syncWriteStream(); \
+ }
+
+ class JournalReadChecker
+ {
+ public:
+ ~JournalReadChecker()
+ {
+ TNL::Journal::checkReadPosition();
+ }
+ };
+
+ #define TNL_JOURNAL_READ_BLOCK(x) \
+ { \
+ if(TNL::Journal::isInEntrypoint()) { \
+ TNL::JournalReadChecker dummy; \
+ if(TNL::Journal::getCurrentMode() == TNL::Journal::Playback) \
+ { \
+ x \
+ } \
+ } \
+ }
+
+ #define TNL_JOURNAL_READ(x) \
+ TNL::Journal::getReadStream()->read x
+
+ #define TNL_JOURNAL_WRITE(x) \
+ TNL::Journal::getWriteStream()->write x
+
#else
#define TNL_DECLARE_JOURNAL_ENTRYPOINT(func, args) \
***************
*** 100,103 ****
--- 158,164 ----
void className::func args
+ #define TNL_JOURNAL_WRITE_BLOCK(x)
+ #define TNL_JOURNAL_READ_BLOCK(x)
+
#endif
};
Index: udp.cpp
===================================================================
RCS file: /cvsroot/opentnl/tnl/tnl/udp.cpp,v
retrieving revision 1.2
retrieving revision 1.3
diff -C2 -d -r1.2 -r1.3
*** udp.cpp 20 Apr 2004 22:18:37 -0000 1.2
--- udp.cpp 11 May 2004 02:45:26 -0000 1.3
***************
*** 26,29 ****
--- 26,30 ----
#include "tnl.h"
+ #include "tnlJournal.h"
#if defined ( TNL_OS_XBOX )
***************
*** 190,193 ****
--- 191,198 ----
Socket::Socket(const Address &bindAddress, U32 sendBufferSize, U32 recvBufferSize, bool acceptsBroadcast, bool nonblockingIO)
{
+ TNL_JOURNAL_READ_BLOCK(
+ TNL_JOURNAL_READ( (&mPlatformSocket) );
+ return;
+ )
init();
mPlatformSocket = INVALID_SOCKET;
***************
*** 266,273 ****
--- 271,285 ----
}
}
+ TNL_JOURNAL_WRITE_BLOCK(
+ TNL_JOURNAL_WRITE( (mPlatformSocket) );
+ )
}
Socket::~Socket()
{
+ TNL_JOURNAL_READ_BLOCK(
+ return;
+ )
+
if(mPlatformSocket != INVALID_SOCKET)
closesocket(mPlatformSocket);
***************
*** 277,280 ****
--- 289,296 ----
NetError Socket::sendto(const Address &address, const U8 *buffer, S32 bufferSize)
{
+ TNL_JOURNAL_READ_BLOCK(
+ return NoError;
+ )
+
if(address.transport != mTransportProtocol)
return InvalidPacketProtocol;
***************
*** 293,296 ****
--- 309,329 ----
NetError Socket::recvfrom(Address *address, U8 *buffer, S32 bufferSize, S32 *outSize)
{
+ TNL_JOURNAL_READ_BLOCK(
+ bool wouldBlock;
+ TNL_JOURNAL_READ( (&wouldBlock) );
+ if(wouldBlock)
+ return WouldBlock;
+
+ TNL_JOURNAL_READ( (&address->transport) );
+ TNL_JOURNAL_READ( (&address->port) );
+ TNL_JOURNAL_READ( (&address->netNum[0]) );
+ TNL_JOURNAL_READ( (&address->netNum[1]) );
+ TNL_JOURNAL_READ( (&address->netNum[2]) );
+ TNL_JOURNAL_READ( (&address->netNum[3]) );
+ TNL_JOURNAL_READ( (outSize) );
+ TNL_JOURNAL_READ( (*outSize, buffer) );
+ return NoError;
+ )
+
SOCKADDR sa;
socklen_t addrLen = sizeof(sa);
***************
*** 299,307 ****
--- 332,357 ----
bytesRead = ::recvfrom(mPlatformSocket, (char *) buffer, bufferSize, 0, &sa, &addrLen);
if(bytesRead == SOCKET_ERROR)
+ {
+ TNL_JOURNAL_WRITE_BLOCK(
+ TNL_JOURNAL_WRITE ( (true) );
+ )
return WouldBlock;
+ }
SocketToTNLAddress(&sa, address);
*outSize = bytesRead;
+
+ TNL_JOURNAL_WRITE_BLOCK(
+ TNL_JOURNAL_WRITE( (false) );
+ TNL_JOURNAL_WRITE( (address->transport) );
+ TNL_JOURNAL_WRITE( (address->port) );
+ TNL_JOURNAL_WRITE( (address->netNum[0]) );
+ TNL_JOURNAL_WRITE( (address->netNum[1]) );
+ TNL_JOURNAL_WRITE( (address->netNum[2]) );
+ TNL_JOURNAL_WRITE( (address->netNum[3]) );
+ TNL_JOURNAL_WRITE( (*outSize) );
+ TNL_JOURNAL_WRITE( (*outSize, buffer) );
+ )
return NoError;
}
Index: journal.cpp
===================================================================
RCS file: /cvsroot/opentnl/tnl/tnl/journal.cpp,v
retrieving revision 1.1
retrieving revision 1.2
diff -C2 -d -r1.1 -r1.2
*** journal.cpp 10 May 2004 22:34:23 -0000 1.1
--- journal.cpp 11 May 2004 02:45:26 -0000 1.2
***************
*** 26,29 ****
--- 26,30 ----
#include "tnlJournal.h"
+ #include "tnlEndian.h"
namespace TNL
***************
*** 32,37 ****
--- 33,134 ----
JournalEntryRecord *JournalEntryRecord::mList = NULL;
+ bool Journal::mInsideEntrypoint = false;
+ Journal::Mode Journal::mCurrentMode = Journal::Inactive;
+
+ FILE *Journal::mJournalFile = NULL;
+ BitStream Journal::mWriteStream;
+ BitStream Journal::mReadStream;
+ Journal *Journal::mJournal = NULL;
+ U32 Journal::mWritePosition = 0;
+
+ Journal::Journal()
+ {
+ TNLAssert(mJournal == NULL, "Cannot construct more than one Journal instance.");
+ mJournal = this;
+ }
+
+ // the journal stream is written as a single continuous bit stream.
+ // the first four bytes of the stream are the total number of bits in
+ // the stream. As a block is written, the bits in the write stream are
+ // all erased except for those in the last byte, which are moved to the first.
+ void Journal::syncWriteStream()
+ {
+ if(mWriteStream.getBytePosition() == 0)
+ return;
+
+ U32 totalBits = (mWritePosition << 3) + mWriteStream.getBitPosition();
+
+ // seek back to the beginning
+ fseek(mJournalFile, 0, SEEK_SET);
+
+ // write the new total bits
+ U32 writeBits = convertHostToLEndian(totalBits);
+ fwrite(&writeBits, 1, sizeof(U32), mJournalFile);
+
+ // seek to the writing position
+ fseek(mJournalFile, mWritePosition, SEEK_SET);
+
+ U32 bytesToWrite = mWriteStream.getBytePosition();
+ // write the bytes to the file
+ fwrite(mWriteStream.getBuffer(), 1, bytesToWrite, mJournalFile);
+
+ // adjust the write stream
+ if(totalBits & 7)
+ {
+ U8 *buffer = mWriteStream.getBuffer();
+ buffer[0] = buffer[bytesToWrite - 1];
+ mWriteStream.setBitPosition(totalBits & 7);
+ mWritePosition += bytesToWrite - 1;
+ }
+ else
+ {
+ mWritePosition += bytesToWrite;
+ mWriteStream.setBitPosition(0);
+ }
+ }
+
+ void Journal::record(const char *fileName)
+ {
+ mJournalFile = fopen(fileName, "wb");
+ if(mJournalFile)
+ {
+ mCurrentMode = Record;
+ mWritePosition = sizeof(U32);
+ }
+ }
+
+ void Journal::load(const char *fileName)
+ {
+ FILE *theJournal = fopen(fileName, "rb");
+ if(!theJournal)
+ return;
+
+ fseek(theJournal, 0, SEEK_END);
+ U32 fileSize = ftell(theJournal);
+ fseek(theJournal, 0, SEEK_SET);
+
+ mReadStream.resize(fileSize);
+ U32 bitCount;
+ fread(mReadStream.getBuffer(), 1, fileSize, theJournal);
+ mReadStream.read(&bitCount);
+ mReadStream.setMaxBitSizes(bitCount);
+ fclose(theJournal);
+ mCurrentMode = Playback;
+ }
+
void Journal::callEntry(const char *funcName, MarshalledCall *theCall)
{
+ if(mCurrentMode == Playback)
+ return;
+
+ TNLAssert(mInsideEntrypoint == false, "Journal entries cannot be reentrant!");
+ mInsideEntrypoint = true;
+ if(mCurrentMode == Record)
+ {
+ mWriteStream.writeString(funcName);
+ mWriteStream.writeBits(theCall->marshalledData.getBitPosition(), theCall->marshalledData.getBuffer());
+ syncWriteStream();
+ }
+
BitStream unmarshallData(theCall->marshalledData.getBuffer(), theCall->marshalledData.getBytePosition());
theCall->unmarshall(&unmarshallData);
***************
*** 43,49 ****
--- 140,182 ----
walk->getFuncPtr(p);
theCall->dispatch((void *) this, &p);
+ mInsideEntrypoint = false;
return;
}
}
+ TNLAssert(0, "No entry point found!");
+ }
+
+ void Journal::checkReadPosition()
+ {
+ if(!mReadStream.isValid() || mReadStream.getBitPosition() == mReadStream.getMaxReadBitPosition())
+ Platform::debugBreak();
+ }
+
+ void Journal::processNextJournalEntry()
+ {
+ if(mCurrentMode != Playback)
+ return;
+
+ char funcName[256];
+ mReadStream.readString(funcName);
+ JournalEntryRecord *theEntry;
+ for(theEntry = JournalEntryRecord::mList; theEntry; theEntry = theEntry->mNext)
+ if(!strcmp(theEntry->mFunctionName, funcName))
+ break;
+
+ // check for errors...
+ if(!theEntry)
+ {
+ TNLAssert(0, "blech!");
+ }
+ checkReadPosition();
+
+ MethodPointer p;
+ theEntry->getFuncPtr(p);
+ MarshalledCall theCall(theEntry->mMethodArgList);
+ theCall.unmarshall(&mReadStream);
+ mInsideEntrypoint = true;
+ theCall.dispatch((void *) this, &p);
+ mInsideEntrypoint = false;
}
Index: bitStream.cpp
===================================================================
RCS file: /cvsroot/opentnl/tnl/tnl/bitStream.cpp,v
retrieving revision 1.1
retrieving revision 1.2
diff -C2 -d -r1.1 -r1.2
*** bitStream.cpp 20 Apr 2004 04:27:18 -0000 1.1
--- bitStream.cpp 11 May 2004 02:45:26 -0000 1.2
***************
*** 42,45 ****
--- 42,51 ----
}
+ void BitStream::setMaxBitSizes(U32 maxReadSize, U32 maxWriteSize)
+ {
+ maxReadBitNum = maxReadSize;
+ maxWriteBitNum = maxWriteSize;
+ }
+
void BitStream::reset()
{
Index: tnlBitStream.h
===================================================================
RCS file: /cvsroot/opentnl/tnl/tnl/tnlBitStream.h,v
retrieving revision 1.2
retrieving revision 1.3
diff -C2 -d -r1.2 -r1.3
*** tnlBitStream.h 22 Apr 2004 20:42:25 -0000 1.2
--- tnlBitStream.h 11 May 2004 02:45:26 -0000 1.3
***************
*** 97,100 ****
--- 97,103 ----
void setMaxSizes(U32 maxReadSize, U32 maxWriteSize = 0);
+ /// Sets the maximum read and write bit sizes for the BitStream.
+ void setMaxBitSizes(U32 maxReadBitSize, U32 maxWriteBitSize = 0);
+
/// resets the read/write position to 0 and clears any error state.
void reset();
***************
*** 114,117 ****
--- 117,123 ----
void advanceBitPosition(const S32 numBits);
+ /// Returns the maximum readable bit position
+ U32 getMaxReadBitPosition() const { return maxReadBitNum; }
+
/// Pads the bits up to the next byte boundary with 0's.
void zeroToByteBoundary();
***************
*** 208,211 ****
--- 214,220 ----
bool readFlag();
+ bool write(bool value) { writeFlag(value); return !error; }
+ bool read(bool *value) { *value = readFlag(); return !error; }
+
/// Writes a huffman compressed string into the stream.
void writeString(const char *stringBuf, U8 maxLen=255);
***************
*** 228,231 ****
--- 237,242 ----
DeclareTemplatizedReadWrite(U32);
DeclareTemplatizedReadWrite(S32);
+ DeclareTemplatizedReadWrite(S64);
+ DeclareTemplatizedReadWrite(U64);
DeclareTemplatizedReadWrite(F32);
DeclareTemplatizedReadWrite(F64);
Index: tnlEndian.h
===================================================================
RCS file: /cvsroot/opentnl/tnl/tnl/tnlEndian.h,v
retrieving revision 1.2
retrieving revision 1.3
diff -C2 -d -r1.2 -r1.3
*** tnlEndian.h 7 May 2004 02:21:45 -0000 1.2
--- tnlEndian.h 11 May 2004 02:45:26 -0000 1.3
***************
*** 30,33 ****
--- 30,43 ----
namespace TNL {
+ inline U8 endianSwap(const U8 in_swap)
+ {
+ return in_swap;
+ }
+
+ inline S8 endianSwap(const S8 in_swap)
+ {
+ return in_swap;
+ }
+
/**
Convert the byte ordering on the U16 to and from big/little endian format.
***************
*** 35,38 ****
--- 45,49 ----
@returns swapped U16.
*/
+
inline U16 endianSwap(const U16 in_swap)
{
***************
*** 41,44 ****
--- 52,60 ----
}
+ inline S16 endianSwap(const S16 in_swap)
+ {
+ return S16(endianSwap(U16(in_swap)));
+ }
+
/**
Convert the byte ordering on the U32 to and from big/little endian format.
***************
*** 54,211 ****
}
! //------------------------------------------------------------------------------
! // Endian conversions
! #ifdef TNL_LITTLE_ENDIAN
!
! inline U8 convertHostToLEndian(U8 i) { return i; }
! inline U8 convertLEndianToHost(U8 i) { return i; }
! inline U16 convertHostToLEndian(U16 i) { return i; }
! inline U16 convertLEndianToHost(U16 i) { return i; }
! inline U32 convertHostToLEndian(U32 i) { return i; }
! inline U32 convertLEndianToHost(U32 i) { return i; }
! inline S8 convertHostToLEndian(S8 i) { return i; }
! inline S8 convertLEndianToHost(S8 i) { return i; }
! inline S16 convertHostToLEndian(S16 i) { return i; }
! inline S16 convertLEndianToHost(S16 i) { return i; }
! inline S32 convertHostToLEndian(S32 i) { return i; }
! inline S32 convertLEndianToHost(S32 i) { return i; }
!
! inline F32 convertHostToLEndian(F32 i) { return i; }
! inline F32 convertLEndianToHost(F32 i) { return i; }
! inline F64 convertHostToLEndian(F64 i) { return i; }
! inline F64 convertLEndianToHost(F64 i) { return i; }
!
! inline U8 convertHostToBEndian(U8 i) { return i; }
! inline U8 convertBEndianToHost(U8 i) { return i; }
! inline S8 convertHostToBEndian(S8 i) { return i; }
! inline S8 convertBEndianToHost(S8 i) { return i; }
!
! inline U16 convertHostToBEndian(U16 i)
! {
! return U16((i << 8) | (i >> 8));
! }
!
! inline U16 convertBEndianToHost(U16 i)
! {
! return U16((i << 8) | (i >> 8));
! }
!
! inline S16 convertHostToBEndian(S16 i)
{
! return S16(convertHostToBEndian(U16(i)));
}
! inline S16 convertBEndianToHost(S16 i)
{
! return S16(convertBEndianToHost(U16(i)));
}
! inline U32 convertHostToBEndian(U32 i)
{
! return ((i << 24) & 0xff000000) |
! ((i << 8) & 0x00ff0000) |
! ((i >> 8) & 0x0000ff00) |
! ((i >> 24) & 0x000000ff);
}
! inline U32 convertBEndianToHost(U32 i)
{
! return ((i << 24) & 0xff000000) |
! ((i << 8) & 0x00ff0000) |
! ((i >> 8) & 0x0000ff00) |
! ((i >> 24) & 0x000000ff);
}
! inline S32 convertHostToBEndian(S32 i)
{
! return S32(convertHostToBEndian(U32(i)));
}
! inline S32 convertBEndianToHost(S32 i)
! {
! return S32(convertBEndianToHost(S32(i)));
! }
#elif defined(TNL_BIG_ENDIAN)
! inline U8 convertHostToBEndian(U8 i) { return i; }
! inline U8 convertBEndianToHost(U8 i) { return i; }
! inline S8 convertHostToBEndian(S8 i) { return i; }
! inline S8 convertBEndianToHost(S8 i) { return i; }
! inline U16 convertHostToBEndian(U16 i) { return i; }
! inline U16 convertBEndianToHost(U16 i) { return i; }
! inline U32 convertHostToBEndian(U32 i) { return i; }
! inline U32 convertBEndianToHost(U32 i) { return i; }
! inline S16 convertHostToBEndian(S16 i) { return i; }
! inline S16 convertBEndianToHost(S16 i) { return i; }
! inline S32 convertHostToBEndian(S32 i) { return i; }
! inline S32 convertBEndianToHost(S32 i) { return i; }
!
! inline U8 convertHostToLEndian(U8 i) { return i; }
! inline U8 convertLEndianToHost(U8 i) { return i; }
! inline S8 convertHostToLEndian(S8 i) { return i; }
! inline S8 convertLEndianToHost(S8 i) { return i; }
!
! inline U16 convertHostToLEndian(U16 i)
! {
! return (i << 8) | (i >> 8);
! }
! inline U16 convertLEndianToHost(U16 i)
! {
! return (i << 8) | (i >> 8);
! }
! inline U32 convertHostToLEndian(U32 i)
! {
! return ((i << 24) & 0xff000000) |
! ((i << 8) & 0x00ff0000) |
! ((i >> 8) & 0x0000ff00) |
! ((i >> 24) & 0x000000ff);
! }
! inline U32 convertLEndianToHost(U32 i)
! {
! return ((i << 24) & 0xff000000) |
! ((i << 8) & 0x00ff0000) |
! ((i >> 8) & 0x0000ff00) |
! ((i >> 24) & 0x000000ff);
! }
!
!
! inline F32 convertHostToLEndian(F32 i)
! {
! U32 result = convertHostToLEndian( *reinterpret_cast<U32*>(&i) );
! return *reinterpret_cast<F32*>(&result);
! }
!
! inline F32 convertLEndianToHost(F32 i)
! {
! U32 result = convertLEndianToHost( *reinterpret_cast<U32*>(&i) );
! return *reinterpret_cast<F32*>(&result);
! }
!
! inline F64 convertHostToLEndian(F64 i)
! {
! F64 ret;
! reinterpret_cast<U32 *>(&ret)[0] =
! convertLEndianToHost( reinterpret_cast<U32*>(&i)[1] );
! reinterpret_cast<U32 *>(&ret)[1] =
! convertLEndianToHost( reinterpret_cast<U32*>(&i)[0] );
! return ret;
! }
!
! inline F64 convertLEndianToHost(F64 i)
! {
! F64 ret;
! reinterpret_cast<U32 *>(&ret)[0] =
! convertLEndianToHost( reinterpret_cast<U32*>(&i)[1] );
! reinterpret_cast<U32 *>(&ret)[1] =
! convertLEndianToHost( reinterpret_cast<U32*>(&i)[0] );
! return ret;
! }
!
! inline S16 convertHostToLEndian(S16 i) { return S16(convertHostToLEndian(U16(i))); }
! inline S16 convertLEndianToHost(S16 i) { return S16(convertLEndianToHost(U16(i))); }
! inline S32 convertHostToLEndian(S32 i) { return S32(convertHostToLEndian(U32(i))); }
! inline S32 convertLEndianToHost(S32 i) { return S32(convertLEndianToHost(U32(i))); }
#else
--- 70,122 ----
}
! inline S32 endianSwap(const S32 in_swap)
{
! return S32(endianSwap(U32(in_swap)));
}
! inline U64 endianSwap(const U64 in_swap)
{
! U32 *inp = (U32 *) &in_swap;
! U64 ret;
! U32 *outp = (U32 *) &ret;
! outp[0] = endianSwap(inp[1]);
! outp[1] = endianSwap(inp[0]);
! return ret;
}
! inline S64 endianSwap(const S64 in_swap)
{
! return S64(endianSwap(U64(in_swap)));
}
! inline F32 endianSwap(const F32 in_swap)
{
! U32 result = endianSwap(* ((U32 *) &in_swap) );
! return * ((F32 *) &result);
}
! inline F64 endianSwap(const F64 in_swap)
{
! U64 result = endianSwap(* ((U64 *) &in_swap) );
! return * ((F64 *) &result);
}
! //------------------------------------------------------------------------------
! // Endian conversions
! #ifdef TNL_LITTLE_ENDIAN
+ #define TNL_DECLARE_TEMPLATIZED_ENDIAN_CONV(type) \
+ inline type convertHostToLEndian(type i) { return i; } \
+ inline type convertLEndianToHost(type i) { return i; } \
+ inline type convertHostToBEndian(type i) { return endianSwap(i); } \
+ inline type convertBEndianToHost(type i) { return endianSwap(i); }
#elif defined(TNL_BIG_ENDIAN)
! #define TNL_DECLARE_TEMPLATIZED_ENDIAN_CONV(type) \
! inline type convertHostToLEndian(type i) { return endianSwap(i); } \
! inline type convertLEndianToHost(type i) { return endianSwap(i); } \
! inline type convertHostToBEndian(type i) { return i; } \
! inline type convertBEndianToHost(type i) { return i; }
#else
***************
*** 214,217 ****
--- 125,139 ----
+ TNL_DECLARE_TEMPLATIZED_ENDIAN_CONV(U8)
+ TNL_DECLARE_TEMPLATIZED_ENDIAN_CONV(S8)
+ TNL_DECLARE_TEMPLATIZED_ENDIAN_CONV(U16)
+ TNL_DECLARE_TEMPLATIZED_ENDIAN_CONV(S16)
+ TNL_DECLARE_TEMPLATIZED_ENDIAN_CONV(U32)
+ TNL_DECLARE_TEMPLATIZED_ENDIAN_CONV(S32)
+ TNL_DECLARE_TEMPLATIZED_ENDIAN_CONV(U64)
+ TNL_DECLARE_TEMPLATIZED_ENDIAN_CONV(S64)
+ TNL_DECLARE_TEMPLATIZED_ENDIAN_CONV(F32)
+ TNL_DECLARE_TEMPLATIZED_ENDIAN_CONV(F64)
+
};
|