From 599f6443286a558873241de98ddbe8a52dcb5625 Mon Sep 17 00:00:00 2001 From: Daniel Gibson Date: Sun, 17 Apr 2016 23:35:04 +0200 Subject: [PATCH] Resolved some 64bit issues, marked/commented some others introduced PLATFORM_32BIT and PLATFORM_64BIT macros, so you can do #ifdef PLATFORM_64BIT if you need to. I needed that for CDrawPort::GetID() to properly CRC a pointer. Also added a sanity check in Engine/Base/Types.h that makes sure that uintprt_t and size_t have the same size, as the code uses size_t to store pointers (or cast from pointer to int) all over the place. Made some "tags" from Engine/Templates/BSP_internal.h size_t instead of ULONG - they're used to store pointers to identify vertices and such, so they'd better be big enough to actually store a pointer. Some more are still missing. --- Sources/Engine/Base/CRC.h | 12 ++++++++++ Sources/Engine/Base/Shell.cpp | 1 + Sources/Engine/Base/Shell.h | 2 +- Sources/Engine/Base/Types.h | 31 +++++++++++++++++++++++++ Sources/Engine/Brushes/BrushPolygon.cpp | 4 ++-- Sources/Engine/Graphics/DrawPort.cpp | 5 +++- Sources/Engine/Graphics/Texture.cpp | 7 ++++-- Sources/Engine/Math/Object3D_CSG.cpp | 9 +++---- Sources/Engine/Math/ObjectSector.cpp | 9 +++---- Sources/Engine/Models/Model.cpp | 12 +++++----- Sources/Engine/Network/Network.cpp | 2 ++ Sources/Engine/Network/SessionState.h | 2 +- Sources/Engine/Templates/BSP.cpp | 2 ++ Sources/Engine/Templates/BSP_internal.h | 8 +++---- 14 files changed, 77 insertions(+), 29 deletions(-) diff --git a/Sources/Engine/Base/CRC.h b/Sources/Engine/Base/CRC.h index 8fab7ac..3d56397 100644 --- a/Sources/Engine/Base/CRC.h +++ b/Sources/Engine/Base/CRC.h @@ -44,6 +44,18 @@ inline void CRC_AddLONG( ULONG &ulCRC, ULONG ul) CRC_AddBYTE(ulCRC, UBYTE(ul>> 0)); }; +inline void CRC_AddLONGLONG( ULONG &ulCRC, __uint64 x) +{ + CRC_AddBYTE(ulCRC, UBYTE(x>>56)); + CRC_AddBYTE(ulCRC, UBYTE(x>>48)); + CRC_AddBYTE(ulCRC, UBYTE(x>>40)); + CRC_AddBYTE(ulCRC, UBYTE(x>>32)); + CRC_AddBYTE(ulCRC, UBYTE(x>>24)); + CRC_AddBYTE(ulCRC, UBYTE(x>>16)); + CRC_AddBYTE(ulCRC, UBYTE(x>> 8)); + CRC_AddBYTE(ulCRC, UBYTE(x>> 0)); +} + inline void CRC_AddFLOAT(ULONG &ulCRC, FLOAT f) { CRC_AddLONG(ulCRC, *(ULONG*)&f); diff --git a/Sources/Engine/Base/Shell.cpp b/Sources/Engine/Base/Shell.cpp index 07e4336..396db23 100644 --- a/Sources/Engine/Base/Shell.cpp +++ b/Sources/Engine/Base/Shell.cpp @@ -704,6 +704,7 @@ INDEX CShell::GetINDEX(const CTString &strName) return -666; } // get it + STUBBED("64-bit issue"); // the return value is used as a pointer a lot! return *(INDEX*)pss->ss_pvValue; } diff --git a/Sources/Engine/Base/Shell.h b/Sources/Engine/Base/Shell.h index 759ae83..035cb06 100644 --- a/Sources/Engine/Base/Shell.h +++ b/Sources/Engine/Base/Shell.h @@ -59,7 +59,7 @@ public: // get/set symbols FLOAT GetFLOAT(const CTString &strName); void SetFLOAT(const CTString &strName, FLOAT fValue); - INDEX GetINDEX(const CTString &strName); + INDEX GetINDEX(const CTString &strName); // FIXME DG: maybe this should return size_t or uintptr_t? return value is casted to ptr all the time! void SetINDEX(const CTString &strName, INDEX iValue); CTString GetString(const CTString &strName); void SetString(const CTString &strName, const CTString &strValue); diff --git a/Sources/Engine/Base/Types.h b/Sources/Engine/Base/Types.h index 056ebf3..1cf1903 100644 --- a/Sources/Engine/Base/Types.h +++ b/Sources/Engine/Base/Types.h @@ -61,6 +61,37 @@ typedef unsigned int UINT; #define PLATFORM_LITTLEENDIAN 1 #endif +#ifdef PLATFORM_WIN32 + #ifdef _WIN64 + #define PLATFORM_64BIT 1 + #else + #define PLATFORM_32BIT 1 + #endif +#else + // AFAIK there were versions of MSVC where UINTPTR_MAX was incorrect, + // so I use different code for Windows above + #include // UINTPTR_MAX + #ifdef UINTPTR_MAX + #if UINTPTR_MAX == 0xffffffffuL + #define PLATFORM_32BIT 1 + #elif UINTPTR_MAX == 0xffffffffffffffffuLL + #define PLATFORM_64BIT 1 + #else + #error WTF, your system seems to be neither 32bit nor 64bit?! + #endif + #else + #error Your system does not provide UINTPRT_MAX, find another way! + #endif + + #if UINTPTR_MAX != SIZE_MAX + // the code uses size_t to store pointers all over the place, so if size_t and uintptr_t + // don't have the same size on your system, you're in real trouble. + // (before panicking however make sure your headers don't just contain bullshit values for UINTPTR_MAX or SIZE_MAX) + #error Seems like on your system sizeof(size_t) != sizeof(uintptr_t) - that is *very* bad. + #endif + +#endif + // Mac symbols have an underscore prepended... #if PLATFORM_MACOSX #define ASMSYM(x) "_" #x diff --git a/Sources/Engine/Brushes/BrushPolygon.cpp b/Sources/Engine/Brushes/BrushPolygon.cpp index 192476e..42f4a4b 100644 --- a/Sources/Engine/Brushes/BrushPolygon.cpp +++ b/Sources/Engine/Brushes/BrushPolygon.cpp @@ -84,7 +84,7 @@ void CBrushPolygon::CreateBSPPolygon(BSPPolygon &bspo) // set the plane of the bsp polygon ((DOUBLEplane3D &)bspo) = *brpo.bpo_pbplPlane->bpl_ppldPreciseAbsolute; - bspo.bpo_ulPlaneTag = (ULONG)brpo.bpo_pbscSector->bsc_abplPlanes.Index(brpo.bpo_pbplPlane); + bspo.bpo_ulPlaneTag = (size_t)brpo.bpo_pbscSector->bsc_abplPlanes.Index(brpo.bpo_pbplPlane); // create the array of edges in the bsp polygon INDEX ctEdges = brpo.bpo_abpePolygonEdges.Count(); @@ -109,7 +109,7 @@ void CBrushPolygon::CreateBSPPolygonNonPrecise(BSPPolygon &bspo) // set the plane of the bsp polygon ((DOUBLEplane3D &)bspo) = FLOATtoDOUBLE(brpo.bpo_pbplPlane->bpl_plAbsolute); - bspo.bpo_ulPlaneTag = (ULONG)brpo.bpo_pbscSector->bsc_abplPlanes.Index(brpo.bpo_pbplPlane); + bspo.bpo_ulPlaneTag = (size_t)brpo.bpo_pbscSector->bsc_abplPlanes.Index(brpo.bpo_pbplPlane); // calculate offset for points DOUBLE3D vOffset = FLOATtoDOUBLE(((FLOAT3D&)brpo.bpo_pbplPlane->bpl_plAbsolute))*-fOffset; // offset the plane diff --git a/Sources/Engine/Graphics/DrawPort.cpp b/Sources/Engine/Graphics/DrawPort.cpp index 26f714a..0722ce3 100755 --- a/Sources/Engine/Graphics/DrawPort.cpp +++ b/Sources/Engine/Graphics/DrawPort.cpp @@ -221,8 +221,11 @@ ULONG CDrawPort::GetID(void) { ULONG ulCRC; CRC_Start( ulCRC); - STUBBED("64-bit issue"); +#ifdef PLATFORM_64BIT + CRC_AddLONGLONG( ulCRC, (__uint64)(size_t)dp_Raster); +#else CRC_AddLONG( ulCRC, (ULONG)(size_t)dp_Raster); +#endif CRC_AddLONG( ulCRC, (ULONG)dp_MinI); CRC_AddLONG( ulCRC, (ULONG)dp_MinJ); CRC_AddLONG( ulCRC, (ULONG)dp_MaxI); diff --git a/Sources/Engine/Graphics/Texture.cpp b/Sources/Engine/Graphics/Texture.cpp index 41046dc..9f3f03d 100644 --- a/Sources/Engine/Graphics/Texture.cpp +++ b/Sources/Engine/Graphics/Texture.cpp @@ -1367,7 +1367,8 @@ void CTextureData::SetAsCurrent( INDEX iFrameNo/*=0*/, BOOL bForceUpload/*=FALSE for( INDEX iFr=0; iFr1) ulTexObject = ((ULONG*)td_ulObject)[iFrameNo]; // animation if( bUseProbe) { // set probe if burst value doesn't allow real texture diff --git a/Sources/Engine/Math/Object3D_CSG.cpp b/Sources/Engine/Math/Object3D_CSG.cpp index 48dfc9a..98bc4a3 100644 --- a/Sources/Engine/Math/Object3D_CSG.cpp +++ b/Sources/Engine/Math/Object3D_CSG.cpp @@ -323,16 +323,14 @@ void CObjectCSG::PolygonEdgesToBSPEdges( // if it is reversed if (ope.ope_Backward) { // add bsp edge with reverse vertices - STUBBED("64-bit issue"); // we're casting ope.ope_Edge to a 32-bit value. Code really only cares if this is zero or !zero, but one could totally have a non-NULL 64-bit pointer that truncates down to zero! abed[iEdge] = DOUBLEbspedge3D(*ope.ope_Edge->oed_Vertex1, - *ope.ope_Edge->oed_Vertex0, (ULONG)(size_t)ope.ope_Edge); + *ope.ope_Edge->oed_Vertex0, (size_t)ope.ope_Edge); // if it is not reversed } else{ // add bsp edge with normal vertices - STUBBED("64-bit issue"); // we're casting ope.ope_Edge to a 32-bit value. Code really only cares if this is zero or !zero, but one could totally have a non-NULL 64-bit pointer that truncates down to zero! abed[iEdge] = DOUBLEbspedge3D(*ope.ope_Edge->oed_Vertex0, - *ope.ope_Edge->oed_Vertex1, (ULONG)(size_t)ope.ope_Edge); + *ope.ope_Edge->oed_Vertex1, (size_t)ope.ope_Edge); } } @@ -544,8 +542,7 @@ void CObjectCSG::DoCSGSplitting( oc_popoPortalB1B2 = NULL; // create a bsp polygon from first temporary array - STUBBED("64-bit issue"); - DOUBLEbsppolygon3D bpoA(*itopoA->opo_Plane, abedRemaining, (ULONG)(size_t)itopoA->opo_Plane); + DOUBLEbsppolygon3D bpoA(*itopoA->opo_Plane, abedRemaining, (size_t)itopoA->opo_Plane); // create a BSP cutter for B's sector BSP and A's polygon DOUBLEbspcutter3D bcCutter(bpoA, *itoscB->osc_BSPTree.bt_pbnRoot); diff --git a/Sources/Engine/Math/ObjectSector.cpp b/Sources/Engine/Math/ObjectSector.cpp index 2bb402f..9b69542 100644 --- a/Sources/Engine/Math/ObjectSector.cpp +++ b/Sources/Engine/Math/ObjectSector.cpp @@ -1844,8 +1844,7 @@ void CObjectSector::CreateBSP(void) // copy the plane (DOUBLEplane3D &)bpo = *opo.opo_Plane; - STUBBED("64-bit issue"); - bpo.bpo_ulPlaneTag = (ULONG)(size_t)opo.opo_Plane; + bpo.bpo_ulPlaneTag = (size_t)opo.opo_Plane; // get count of edges in this polygon const INDEX ctEdges = opo.opo_PolygonEdges.Count(); @@ -1860,13 +1859,11 @@ void CObjectSector::CreateBSP(void) // if the edge is reversed if(ope.ope_Backward) { // add bsp edge with reversed vertices - STUBBED("64-bit issue"); - pbed[iEdge] = DOUBLEbspedge3D(*oed.oed_Vertex1, *oed.oed_Vertex0, (ULONG)(size_t)&oed); + pbed[iEdge] = DOUBLEbspedge3D(*oed.oed_Vertex1, *oed.oed_Vertex0, (size_t)&oed); // otherwise } else { // add normal bsp edge - STUBBED("64-bit issue"); - pbed[iEdge] = DOUBLEbspedge3D(*oed.oed_Vertex0, *oed.oed_Vertex1, (ULONG)(size_t)&oed); + pbed[iEdge] = DOUBLEbspedge3D(*oed.oed_Vertex0, *oed.oed_Vertex1, (size_t)&oed); } } opo.opo_PolygonEdges.Unlock(); diff --git a/Sources/Engine/Models/Model.cpp b/Sources/Engine/Models/Model.cpp index b28a533..76591a8 100644 --- a/Sources/Engine/Models/Model.cpp +++ b/Sources/Engine/Models/Model.cpp @@ -481,7 +481,7 @@ ModelTextureVertex::ModelTextureVertex(void) //------------------------------------------ WRITE void ModelPolygonVertex::Write_t( CTStream *pFile) // throw char * { - STUBBED("64-bit issue"); + STUBBED("64-bit issue"); // DG: probably ok, because PtrToIndices() should have been called before this (*pFile) << (INDEX) (size_t) mpv_ptvTransformedVertex; (*pFile) << (INDEX) (size_t) mpv_ptvTextureVertex; } @@ -490,7 +490,7 @@ void ModelPolygonVertex::Read_t( CTStream *pFile) // throw char * { INDEX itmp; - STUBBED("64-bit issue"); + STUBBED("64-bit issue"); // DG: probably ok, because IndicesToPtrs() should be called afterwards (*pFile) >> itmp; mpv_ptvTransformedVertex = (struct TransformedVertexData *) (size_t) itmp; (*pFile) >> itmp; @@ -1040,14 +1040,14 @@ void CModelData::PtrsToIndices() if( it2.Current().mpv_ptvTransformedVertex == &md_TransformedVertices[ j]) break; } - it2.Current().mpv_ptvTransformedVertex = (struct TransformedVertexData *) j; + it2.Current().mpv_ptvTransformedVertex = (struct TransformedVertexData *)(size_t)j; for( j=0; jMute(); + STUBBED("64-bit issue"); // GetINDEX() returns int32! CWorld *pwo = (CWorld*)_pShell->GetINDEX("pwoCurrentWorld"); if( pwo!=NULL) { pwo->wo_baBrushes.CacheAllShadowmaps(); @@ -522,6 +523,7 @@ static void StockInfo(void) INDEX ctEntities=0, ctShadowLayers=0, ctPolys=0, ctPlanes=0, ctEdges=0, ctVertices=0, ctSectors=0; SLONG slEntBytes=0, slLyrBytes=0, slPlyBytes=0, slPlnBytes=0, slEdgBytes=0, slVtxBytes=0, slSecBytes=0; SLONG slCgrBytes=0; + STUBBED("64-bit issue"); // GetINDEX() returns int32! CWorld *pwo = (CWorld*)_pShell->GetINDEX("pwoCurrentWorld"); if( pwo!=NULL) diff --git a/Sources/Engine/Network/SessionState.h b/Sources/Engine/Network/SessionState.h index db23a96..717feaf 100644 --- a/Sources/Engine/Network/SessionState.h +++ b/Sources/Engine/Network/SessionState.h @@ -60,7 +60,7 @@ public: TIME pe_tmTick; ULONG pe_ulEntityID; ULONG pe_ulTypeID; - ULONG pe_ulEventID; + ULONG pe_ulEventID; // FIXME: make this void* or uintptr_t/size_t CPredictedEvent(void); void Clear(void) {}; diff --git a/Sources/Engine/Templates/BSP.cpp b/Sources/Engine/Templates/BSP.cpp index dffaf1f..ab942cc 100644 --- a/Sources/Engine/Templates/BSP.cpp +++ b/Sources/Engine/Templates/BSP.cpp @@ -1139,11 +1139,13 @@ void BSPTree::MoveSubTreeToArray(BSPNode * if (pbnSubtree->bn_pbnFront==NULL) { bnInArray.bn_pbnFront = NULL; } else { + STUBBED("64-bit issue"); // bn_ulPlaneTag is uint32! bnInArray.bn_pbnFront = (BSPNode*)pbnSubtree->bn_pbnFront->bn_ulPlaneTag; } if (pbnSubtree->bn_pbnBack==NULL) { bnInArray.bn_pbnBack = NULL; } else { + STUBBED("64-bit issue"); // basically the same as above but for back! bnInArray.bn_pbnBack = (BSPNode*)pbnSubtree->bn_pbnBack->bn_ulPlaneTag; } } diff --git a/Sources/Engine/Templates/BSP_internal.h b/Sources/Engine/Templates/BSP_internal.h index f812e7b..afc5761 100644 --- a/Sources/Engine/Templates/BSP_internal.h +++ b/Sources/Engine/Templates/BSP_internal.h @@ -87,12 +87,12 @@ class BSPEdge { public: Vector bed_vVertex0; // edge vertices Vector bed_vVertex1; - ULONG bed_ulEdgeTag; // tags for BSPs with tagged edges/planes + size_t bed_ulEdgeTag; // tags for BSPs with tagged edges/planes - FIXME DG: or uintprt_t? /* Default constructor. */ inline BSPEdge(void) {}; /* Constructor with two vectors. */ - inline BSPEdge(const Vector &vVertex0, const Vector &vVertex1, ULONG ulTag) + inline BSPEdge(const Vector &vVertex0, const Vector &vVertex1, size_t ulTag) : bed_vVertex0(vVertex0), bed_vVertex1(vVertex1), bed_ulEdgeTag(ulTag) {} /* Clear the object. */ @@ -110,7 +110,7 @@ template class BSPPolygon : public Plane { public: CDynamicArray > bpo_abedPolygonEdges; // array of edges in the polygon - ULONG bpo_ulPlaneTag; // tags for BSPs with tagged planes (-1 for no tag) + size_t bpo_ulPlaneTag; // tags for BSPs with tagged planes (-1 for no tag) /* Add an edge to the polygon. */ inline void AddEdge(const Vector &vPoint0, const Vector &vPoint1, ULONG ulTag); @@ -119,7 +119,7 @@ public: inline BSPPolygon(void) : bpo_ulPlaneTag(-1) {}; /* Constructor with array of edges and plane. */ inline BSPPolygon( - Plane &plPlane, CDynamicArray > abedPolygonEdges, ULONG ulPlaneTag) + Plane &plPlane, CDynamicArray > abedPolygonEdges, size_t ulPlaneTag) : Plane(plPlane) , bpo_abedPolygonEdges(abedPolygonEdges) , bpo_ulPlaneTag(ulPlaneTag)