[Date Prev][Date Next][Thread Prev][Thread Next][Thread Index]

[XaraXtreme-commits] Commit Complete



Commit by  : alex
Repository : xara
Revision   : 1695
Date       : Sat Aug 12 20:08:11 BST 2006

Changed paths:
   M /Trunk/XaraLX/Kernel/cxfile.cpp
   M /Trunk/XaraLX/Kernel/cxfrec.cpp
   M /Trunk/XaraLX/wxOil/hardwaremanager.h

Attempt to fix (blind) ppc loading problems. Passing floats & doubles
in non-native form as parameters is a bad plan because the register representation may not support them, even if the memory representation does (i.e. you can byte swap something in memory, but expecting a byteswapped double to be passed successfully as a double is going to lead to disappointment, because the registers may not accept malformed or extended precision numbers). Use a Union instead.


Diff:
Index: Trunk/XaraLX/Kernel/cxfile.cpp
===================================================================
--- Trunk/XaraLX/Kernel/cxfile.cpp	(revision 1694)
+++ Trunk/XaraLX/Kernel/cxfile.cpp	(revision 1695)
@@ -889,15 +889,17 @@
 
 BOOL CXaraFile::Read(FLOAT* pf)
 {
-	BOOL ok = (Read((BYTE*)pf,sizeof(FLOAT)));
-	*pf = LEtoNative(*pf);
+	FloatUnion f;
+	BOOL ok = (Read((BYTE*)&(f.u_INT32),sizeof(f.u_INT32)));
+	*pf = LEtoNative(f);
 	return ok;
 }
 
 BOOL CXaraFile::Read(double* pd)
 {
-	BOOL ok = (Read((BYTE*)pd,sizeof(double)));
-	*pd = LEtoNative(*pd);
+	DoubleUnion f;
+	BOOL ok = (Read((BYTE*)&(f.u_INT64),sizeof(f.u_INT64)));
+	*pd = LEtoNative(f);
 	return ok;
 }
 
@@ -1088,12 +1090,14 @@
 
 BOOL CXaraFile::Write(FLOAT f)
 {
-	return (Write((BYTE*)&f,sizeof(FLOAT)));
+	FloatUnion u=NativetoLEU(f);
+	return (Write((BYTE*)&(u.u_INT32),sizeof(u.u_INT32)));
 }
 
 BOOL CXaraFile::Write(double d)
 {
-	return (Write((BYTE*)&d,sizeof(double)));
+	DoubleUnion u=NativetoLEU(d);
+	return (Write((BYTE*)&(u.u_INT64),sizeof(u.u_INT64)));
 }
 
 BOOL CXaraFile::WriteWCHAR(WCHAR w)
Index: Trunk/XaraLX/Kernel/cxfrec.cpp
===================================================================
--- Trunk/XaraLX/Kernel/cxfrec.cpp	(revision 1694)
+++ Trunk/XaraLX/Kernel/cxfrec.cpp	(revision 1695)
@@ -624,7 +624,8 @@
 BOOL CXaraFileRecord::WriteFLOAT(FLOAT f)
 {
 	ENTERWRITEFUNCTION(FTT_FLOAT);
-	BOOL ok = WriteBuffer((BYTE*)&f,sizeof(FLOAT));
+	FloatUnion u=NativetoLEU(f);
+	BOOL ok = WriteBuffer((BYTE*)&(u.u_INT32),sizeof(u.u_INT32));
 	LEAVEWRITEFUNCTION;
 	return (ok);
 }	
@@ -668,7 +669,8 @@
 BOOL CXaraFileRecord::WriteDOUBLE(double d)
 {
 	ENTERWRITEFUNCTION(FTT_DOUBLE);
-	BOOL ok = WriteBuffer((BYTE*)&d,sizeof(double));
+	DoubleUnion u=NativetoLEU(d);
+	BOOL ok = WriteBuffer((BYTE*)&(u.u_INT64),sizeof(u.u_INT64));
 	LEAVEWRITEFUNCTION;
 	return (ok);
 }	
@@ -1323,22 +1325,25 @@
 
 BOOL CXaraFileRecord::ReadFLOAT(FLOAT* pf)
 {
-	BOOL ok = ReadBuffer((BYTE*)pf,sizeof(FLOAT));
-	*pf = LEtoNative(*pf);
+	FloatUnion f;
+	BOOL ok = ReadBuffer((BYTE*)&(f.u_INT32),sizeof(f.u_INT32));
+	*pf = LEtoNative(f);
 	return ok;
 }
 
 BOOL CXaraFileRecord::ReadDOUBLE(double* pd)
 {
-	BOOL ok = ReadBuffer((BYTE*)pd,sizeof(double));
-	*pd = LEtoNative(*pd);
+	DoubleUnion f;
+	BOOL ok = ReadBuffer((BYTE*)&(f.u_INT64),sizeof(f.u_INT64));
+	*pd = LEtoNative(f);
 	return ok;
 }
 
 BOOL CXaraFileRecord::ReadDOUBLEnoError(double* pd)
 {
-	BOOL ok = ReadBuffernoError((BYTE*)pd,sizeof(double));
-	*pd = LEtoNative(*pd);
+	DoubleUnion f;
+	BOOL ok = ReadBuffernoError((BYTE*)&(f.u_INT64),sizeof(f.u_INT64));
+	*pd = LEtoNative(f);
 	return ok;
 }
 
Index: Trunk/XaraLX/wxOil/hardwaremanager.h
===================================================================
--- Trunk/XaraLX/wxOil/hardwaremanager.h	(revision 1694)
+++ Trunk/XaraLX/wxOil/hardwaremanager.h	(revision 1695)
@@ -115,6 +115,19 @@
 
 namespace oilHardwareManager
 {
+	// Avoid aliasing problems
+	union DoubleUnion
+	{
+		double u_double;
+		INT64 u_INT64;
+	};
+
+	union FloatUnion
+	{
+		float u_float;
+		INT32 u_INT32;
+	};
+
 // Byte ordering functions
 #if defined(WORDS_BIGENDIAN)
 	// __BIG_ENDIAN__
@@ -158,16 +171,28 @@
 	static inline FIXED16 NativetoBE(FIXED16 n) {return n;}
 	static inline FIXED16 NativetoLE(FIXED16 n) {return FIXED16::FromRawLong(wxINT32_SWAP_ALWAYS(n.GetRawLong()));}
 
-	static inline float BEtoNative(float n) {return n;}
-	static inline float LEtoNative(float n) {return n;} //{return reinterpret_cast<float>(wxINT32_SWAP_ALWAYS(reinterpret_cast<INT32>(n)));}
-	static inline float NativetoBE(float n) {return n;}
-	static inline float NativetoLE(float n) {return n;} //{return reinterpret_cast<float>(wxINT32_SWAP_ALWAYS(reinterpret_cast<INT32>(n)));}
+	// AMB is not sure these are are a great idea as they rely on floats/doubles being set up with invalid data
+//	static inline float BEtoNative(float n) {return n;}
+//	static inline float LEtoNative(float n) {return n;} //{return reinterpret_cast<float>(wxINT32_SWAP_ALWAYS(reinterpret_cast<INT32>(n)));}
+//	static inline float NativetoBE(float n) {return n;}
+//	static inline float NativetoLE(float n) {return n;} //{return reinterpret_cast<float>(wxINT32_SWAP_ALWAYS(reinterpret_cast<INT32>(n)));}
 
-	static inline double BEtoNative(double n) {return n;}
-	static inline double LEtoNative(double n) {return n;} //{return reinterpret_cast<double>(wxINT64_SWAP_ALWAYS(reinterpret_cast<INT64>(n)));}
-	static inline double NativetoBE(double n) {return n;}
-	static inline double NativetoLE(double n) {return n;} //{return reinterpret_cast<double>(wxINT64_SWAP_ALWAYS(reinterpret_cast<INT64>(n)));}
+	// AMB is not sure these are are a great idea as they rely on floats/doubles being set up with invalid data
+//	static inline double BEtoNative(double n) {return n;}
+//	static inline double LEtoNative(double n) {return n;} //{return reinterpret_cast<double>(wxINT64_SWAP_ALWAYS(reinterpret_cast<INT64>(n)));}
+//	static inline double NativetoBE(double n) {return n;}
+//	static inline double NativetoLE(double n) {return n;} //{return reinterpret_cast<double>(wxINT64_SWAP_ALWAYS(reinterpret_cast<INT64>(n)));}
 
+	static inline float BEtoNative(FloatUnion n) {return n.u_float;}
+	static inline float LEtoNative(FloatUnion n) {n.u_INT32 = wxINT32_SWAP_ALWAYS(n.u_INT32); return n.u_float;}
+	static inline FloatUnion NativetoBEU(float n) {FloatUnion f; f.u_float=n; return f;}
+	static inline FloatUnion NativetoLEU(float n) {FloatUnion f; f.u_float=n; f.u_INT32 = wxINT32_SWAP_ALWAYS(f.u_INT32); return f;}
+
+	static inline double BEtoNative(DoubleUnion n) {return n.u_double;}
+	static inline double LEtoNative(DoubleUnion n) {n.u_INT64 = wxINT64_SWAP_ALWAYS(n.u_INT64); return n.u_double;}
+	static inline DoubleUnion NativetoBEU(double n) {DoubleUnion f; f.u_double=n; return f;}
+	static inline DoubleUnion NativetoLEU(double n) {DoubleUnion f; f.u_double=n; f.u_INT64 = wxINT64_SWAP_ALWAYS(f.u_INT64); return f;}
+
 #else
 	// __LITTLE_ENDIAN__
 	// Little-Endian, signed
@@ -210,16 +235,27 @@
 	static inline FIXED16 NativetoBE(FIXED16 n) {return FIXED16::FromRawLong(wxINT32_SWAP_ALWAYS(n.GetRawLong()));}
 	static inline FIXED16 NativetoLE(FIXED16 n) {return n;}
 
-	static inline float BEtoNative(float n) {return n;} //{return reinterpret_cast<float>(wxINT32_SWAP_ALWAYS(reinterpret_cast<INT32>(n)));}
-	static inline float LEtoNative(float n) {return n;}
-	static inline float NativetoBE(float n) {return n;} //{return reinterpret_cast<float>(wxINT32_SWAP_ALWAYS(reinterpret_cast<INT32>(n)));}
-	static inline float NativetoLE(float n) {return n;}
+	// AMB is not sure these are are a great idea as they rely on floats/doubles being set up with invalid data
+//	static inline float BEtoNative(float n) {return n;} //{return reinterpret_cast<float>(wxINT32_SWAP_ALWAYS(reinterpret_cast<INT32>(n)));}
+//	static inline float LEtoNative(float n) {return n;}
+//	static inline float NativetoBE(float n) {return n;} //{return reinterpret_cast<float>(wxINT32_SWAP_ALWAYS(reinterpret_cast<INT32>(n)));}
+//	static inline float NativetoLE(float n) {return n;}
 
-	static inline double BEtoNative(double n) {return n;} //{return reinterpret_cast<double>(wxINT64_SWAP_ALWAYS(reinterpret_cast<INT64>(n)));}
-	static inline double LEtoNative(double n) {return n;}
-	static inline double NativetoBE(double n) {return n;} //{return reinterpret_cast<double>(wxINT64_SWAP_ALWAYS(reinterpret_cast<INT64>(n)));}
-	static inline double NativetoLE(double n) {return n;}
+	// AMB is not sure these are are a great idea as they rely on floats/doubles being set up with invalid data
+//	static inline double BEtoNative(double n) {return n;} //{return reinterpret_cast<double>(wxINT64_SWAP_ALWAYS(reinterpret_cast<INT64>(n)));}
+//	static inline double LEtoNative(double n) {return n;}
+//	static inline double NativetoBE(double n) {return n;} //{return reinterpret_cast<double>(wxINT64_SWAP_ALWAYS(reinterpret_cast<INT64>(n)));}
+//	static inline double NativetoLE(double n) {return n;}
 
+	static inline float BEtoNative(FloatUnion n) {n.u_INT32 = wxINT32_SWAP_ALWAYS(n.u_INT32); return n.u_float;}
+	static inline float LEtoNative(FloatUnion n) {return n.u_float;}
+	static inline FloatUnion NativetoBEU(float n) {FloatUnion f; f.u_float=n; f.u_INT32 = wxINT32_SWAP_ALWAYS(f.u_INT32); return f;}
+	static inline FloatUnion NativetoLEU(float n) {FloatUnion f; f.u_float=n; return f;}
+
+	static inline double BEtoNative(DoubleUnion n) {n.u_INT64 = wxINT64_SWAP_ALWAYS(n.u_INT64); return n.u_double;}
+	static inline double LEtoNative(DoubleUnion n) {return n.u_double;}
+	static inline DoubleUnion NativetoBEU(double n) {DoubleUnion f; f.u_double=n; f.u_INT64 = wxINT64_SWAP_ALWAYS(f.u_INT64); return f;}
+	static inline DoubleUnion NativetoLEU(double n) {DoubleUnion f; f.u_double=n; return f;}
 #endif
 
 // -------------------------------------------------------------------------------


Xara