diff --git a/autotest/cpp/test_cpl.cpp b/autotest/cpp/test_cpl.cpp index 5a60bb6bc0e8..732d73f93b11 100644 --- a/autotest/cpp/test_cpl.cpp +++ b/autotest/cpp/test_cpl.cpp @@ -1640,36 +1640,36 @@ TEST_F(test_cpl, CPLSM_unsigned) { } - ASSERT_EQ((CPLSM(static_cast(2) * 1000 * 1000 * 1000) + - CPLSM(static_cast(3) * 1000 * 1000 * 1000)) + ASSERT_EQ((CPLSM(static_cast(2) * 1000 * 1000 * 1000) + + CPLSM(static_cast(3) * 1000 * 1000 * 1000)) .v(), - static_cast(5) * 1000 * 1000 * 1000); - ASSERT_EQ((CPLSM(std::numeric_limits::max() - 1) + - CPLSM(static_cast(1))) + static_cast(5) * 1000 * 1000 * 1000); + ASSERT_EQ((CPLSM(std::numeric_limits::max() - 1) + + CPLSM(static_cast(1))) .v(), - std::numeric_limits::max()); + std::numeric_limits::max()); try { - (CPLSM(std::numeric_limits::max()) + - CPLSM(static_cast(1))); + (CPLSM(std::numeric_limits::max()) + + CPLSM(static_cast(1))); } catch (...) { } - ASSERT_EQ((CPLSM(static_cast(2) * 1000 * 1000 * 1000) * - CPLSM(static_cast(3) * 1000 * 1000 * 1000)) + ASSERT_EQ((CPLSM(static_cast(2) * 1000 * 1000 * 1000) * + CPLSM(static_cast(3) * 1000 * 1000 * 1000)) .v(), - static_cast(6) * 1000 * 1000 * 1000 * 1000 * 1000 * + static_cast(6) * 1000 * 1000 * 1000 * 1000 * 1000 * 1000); - ASSERT_EQ((CPLSM(std::numeric_limits::max()) * - CPLSM(static_cast(1))) + ASSERT_EQ((CPLSM(std::numeric_limits::max()) * + CPLSM(static_cast(1))) .v(), - std::numeric_limits::max()); + std::numeric_limits::max()); try { - (CPLSM(std::numeric_limits::max()) * - CPLSM(static_cast(2))); + (CPLSM(std::numeric_limits::max()) * + CPLSM(static_cast(2))); } catch (...) { diff --git a/frmts/pds/pdsdataset.cpp b/frmts/pds/pdsdataset.cpp index 74dde9c539fa..a0d8f65bf3dd 100644 --- a/frmts/pds/pdsdataset.cpp +++ b/frmts/pds/pdsdataset.cpp @@ -1201,7 +1201,7 @@ int PDSDataset::ParseImage(const CPLString &osPrefix, int nPixelOffset; vsi_l_offset nBandOffset; - const auto CPLSM64 = [](int x) { return CPLSM(static_cast(x)); }; + const auto CPLSM64 = [](int x) { return CPLSM(static_cast(x)); }; try { diff --git a/frmts/pds/vicardataset.cpp b/frmts/pds/vicardataset.cpp index baec7c00e7c6..9fb10c7d340a 100644 --- a/frmts/pds/vicardataset.cpp +++ b/frmts/pds/vicardataset.cpp @@ -2630,15 +2630,15 @@ GDALDataset *VICARDataset::Open(GDALOpenInfo *poOpenInfo) /* Compute the line offsets. */ /* -------------------------------------------------------------------- */ - GUInt64 nPixelOffset; - GUInt64 nLineOffset; - GUInt64 nBandOffset; - GUInt64 nImageOffsetWithoutNBB; - GUInt64 nNBB; - GUInt64 nImageSize; + uint64_t nPixelOffset; + uint64_t nLineOffset; + uint64_t nBandOffset; + uint64_t nImageOffsetWithoutNBB; + uint64_t nNBB; + uint64_t nImageSize; if (!GetSpacings(poDS->oKeywords, nPixelOffset, nLineOffset, nBandOffset, nImageOffsetWithoutNBB, nNBB, nImageSize) || - nImageOffsetWithoutNBB > std::numeric_limits::max() - + nImageOffsetWithoutNBB > std::numeric_limits::max() - (nNBB + nBandOffset * (nBands - 1))) { CPLDebug("VICAR", "Invalid spacings found"); @@ -3076,22 +3076,22 @@ GDALDataType VICARDataset::GetDataTypeFromFormat(const char *pszFormat) /************************************************************************/ bool VICARDataset::GetSpacings(const VICARKeywordHandler &keywords, - GUInt64 &nPixelOffset, GUInt64 &nLineOffset, - GUInt64 &nBandOffset, - GUInt64 &nImageOffsetWithoutNBB, GUInt64 &nNBB, - GUInt64 &nImageSize) + uint64_t &nPixelOffset, uint64_t &nLineOffset, + uint64_t &nBandOffset, + uint64_t &nImageOffsetWithoutNBB, uint64_t &nNBB, + uint64_t &nImageSize) { const GDALDataType eDataType = GetDataTypeFromFormat(keywords.GetKeyword("FORMAT", "")); if (eDataType == GDT_Unknown) return false; - const GUInt64 nItemSize = GDALGetDataTypeSizeBytes(eDataType); + const uint64_t nItemSize = GDALGetDataTypeSizeBytes(eDataType); const char *value = keywords.GetKeyword("ORG", "BSQ"); // number of bytes of binary prefix before each record nNBB = atoi(keywords.GetKeyword("NBB", "")); - const GUInt64 nCols64 = atoi(keywords.GetKeyword("NS", "")); - const GUInt64 nRows64 = atoi(keywords.GetKeyword("NL", "")); - const GUInt64 nBands64 = atoi(keywords.GetKeyword("NB", "")); + const uint64_t nCols64 = atoi(keywords.GetKeyword("NS", "")); + const uint64_t nRows64 = atoi(keywords.GetKeyword("NL", "")); + const uint64_t nBands64 = atoi(keywords.GetKeyword("NB", "")); try { if (EQUAL(value, "BIP")) @@ -3130,9 +3130,9 @@ bool VICARDataset::GetSpacings(const VICARKeywordHandler &keywords, return false; } - const GUInt64 nLabelSize = atoi(keywords.GetKeyword("LBLSIZE", "")); - const GUInt64 nRecordSize = atoi(keywords.GetKeyword("RECSIZE", "")); - const GUInt64 nNLB = atoi(keywords.GetKeyword("NLB", "")); + const uint64_t nLabelSize = atoi(keywords.GetKeyword("LBLSIZE", "")); + const uint64_t nRecordSize = atoi(keywords.GetKeyword("RECSIZE", "")); + const uint64_t nNLB = atoi(keywords.GetKeyword("NLB", "")); try { nImageOffsetWithoutNBB = diff --git a/frmts/pds/vicardataset.h b/frmts/pds/vicardataset.h index b152266fda3f..e969c63a594a 100644 --- a/frmts/pds/vicardataset.h +++ b/frmts/pds/vicardataset.h @@ -132,10 +132,10 @@ class VICARDataset final : public RawDataset static GDALDataType GetDataTypeFromFormat(const char *pszFormat); static bool GetSpacings(const VICARKeywordHandler &keywords, - GUInt64 &nPixelOffset, GUInt64 &nLineOffset, - GUInt64 &nBandOffset, - GUInt64 &nImageOffsetWithoutNBB, GUInt64 &nNBB, - GUInt64 &nImageSize); + uint64_t &nPixelOffset, uint64_t &nLineOffset, + uint64_t &nBandOffset, + uint64_t &nImageOffsetWithoutNBB, uint64_t &nNBB, + uint64_t &nImageSize); }; #endif // VICARDATASET_H diff --git a/frmts/pds/vicarkeywordhandler.cpp b/frmts/pds/vicarkeywordhandler.cpp index af4468f53cd8..f90ac7f2d7c5 100644 --- a/frmts/pds/vicarkeywordhandler.cpp +++ b/frmts/pds/vicarkeywordhandler.cpp @@ -109,12 +109,12 @@ bool VICARKeywordHandler::Ingest(VSILFILE *fp, const GByte *pabyHeader) /* There is a EOL! e.G. h4231_0000.nd4.06 */ /* -------------------------------------------------------------------- */ - GUInt64 nPixelOffset; - GUInt64 nLineOffset; - GUInt64 nBandOffset; - GUInt64 nImageOffsetWithoutNBB; - GUInt64 nNBB; - GUInt64 nImageSize; + uint64_t nPixelOffset; + uint64_t nLineOffset; + uint64_t nBandOffset; + uint64_t nImageOffsetWithoutNBB; + uint64_t nNBB; + uint64_t nImageSize; if (!VICARDataset::GetSpacings(*this, nPixelOffset, nLineOffset, nBandOffset, nImageOffsetWithoutNBB, nNBB, nImageSize)) @@ -128,7 +128,7 @@ bool VICARKeywordHandler::Ingest(VSILFILE *fp, const GByte *pabyHeader) const vsi_l_offset nEOCI = (nEOCI2 << 32) | nEOCI1; if (nImageOffsetWithoutNBB > - std::numeric_limits::max() - nImageSize) + std::numeric_limits::max() - nImageSize) { CPLError(CE_Failure, CPLE_AppDefined, "Invalid label values"); return false; diff --git a/frmts/rmf/rmfdataset.cpp b/frmts/rmf/rmfdataset.cpp index ffdeeebb7c36..c35f225bba19 100644 --- a/frmts/rmf/rmfdataset.cpp +++ b/frmts/rmf/rmfdataset.cpp @@ -1418,15 +1418,15 @@ RMFDataset *RMFDataset::Open(GDALOpenInfo *poOpenInfo, RMFDataset *poParentDS, bool bInvalidTileSize; try { - GUInt64 nMaxTileBits = - (CPLSM(static_cast(2)) * - CPLSM(static_cast(poDS->sHeader.nTileWidth)) * - CPLSM(static_cast(poDS->sHeader.nTileHeight)) * - CPLSM(static_cast(poDS->sHeader.nBitDepth))) + uint64_t nMaxTileBits = + (CPLSM(static_cast(2)) * + CPLSM(static_cast(poDS->sHeader.nTileWidth)) * + CPLSM(static_cast(poDS->sHeader.nTileHeight)) * + CPLSM(static_cast(poDS->sHeader.nBitDepth))) .v(); bInvalidTileSize = (nMaxTileBits > - static_cast(std::numeric_limits::max())); + static_cast(std::numeric_limits::max())); } catch (...) { diff --git a/gcore/gdaljp2box.cpp b/gcore/gdaljp2box.cpp index d4495cce3746..09ba5952bd47 100644 --- a/gcore/gdaljp2box.cpp +++ b/gcore/gdaljp2box.cpp @@ -155,21 +155,9 @@ int GDALJP2Box::ReadBox() if (VSIFReadL(abyXLBox, 8, 1, fpVSIL) != 1) return FALSE; -#ifdef CPL_HAS_GINT64 CPL_MSBPTR64(abyXLBox); memcpy(&nBoxLength, abyXLBox, 8); -#else - // In case we lack a 64 bit integer type - if (abyXLBox[0] != 0 || abyXLBox[1] != 0 || abyXLBox[2] != 0 || - abyXLBox[3] != 0) - { - CPLError(CE_Failure, CPLE_AppDefined, - "Box size requires a 64 bit integer type"); - return FALSE; - } - CPL_MSBPTR32(abyXLBox + 4); - memcpy(&nBoxLength, abyXLBox + 4, 4); -#endif + if (nBoxLength < 0) { CPLDebug("GDALJP2", "Invalid length for box %s", szBoxType); diff --git a/gcore/gdalmultidim.cpp b/gcore/gdalmultidim.cpp index c47f215896bf..44764a6b2c7e 100644 --- a/gcore/gdalmultidim.cpp +++ b/gcore/gdalmultidim.cpp @@ -1887,8 +1887,8 @@ bool GDALAbstractMDArray::CheckReadWriteParams( bool bOK; try { - newStride = (CPLSM(static_cast(stride)) * - CPLSM(static_cast(count[i]))) + newStride = (CPLSM(static_cast(stride)) * + CPLSM(static_cast(count[i]))) .v(); bOK = static_cast(newStride) == newStride && newStride < std::numeric_limits::max() / 2; @@ -1934,9 +1934,9 @@ bool GDALAbstractMDArray::CheckReadWriteParams( { try { - bOverflow = (CPLSM(static_cast(arrayStartIdx[i])) + - CPLSM(static_cast(count[i] - 1)) * - CPLSM(static_cast(arrayStep[i]))) + bOverflow = (CPLSM(static_cast(arrayStartIdx[i])) + + CPLSM(static_cast(count[i] - 1)) * + CPLSM(static_cast(arrayStep[i]))) .v() >= dims[i]->GetSize(); } catch (...) @@ -1960,10 +1960,10 @@ bool GDALAbstractMDArray::CheckReadWriteParams( { bOverflow = arrayStartIdx[i] < - (CPLSM(static_cast(count[i] - 1)) * + (CPLSM(static_cast(count[i] - 1)) * CPLSM(arrayStep[i] == std::numeric_limits::min() - ? (static_cast(1) << 63) - : static_cast(-arrayStep[i]))) + ? (static_cast(1) << 63) + : static_cast(-arrayStep[i]))) .v(); } catch (...) @@ -2002,10 +2002,10 @@ bool GDALAbstractMDArray::CheckReadWriteParams( { try { - nOffset = (CPLSM(static_cast(nOffset)) + - CPLSM(static_cast(bufferStride[i])) * - CPLSM(static_cast(count[i] - 1)) * - CPLSM(static_cast(elementSize))) + nOffset = (CPLSM(static_cast(nOffset)) + + CPLSM(static_cast(bufferStride[i])) * + CPLSM(static_cast(count[i] - 1)) * + CPLSM(static_cast(elementSize))) .v(); } catch (...) @@ -2272,8 +2272,8 @@ GUInt64 GDALAbstractMDArray::GetTotalElementsCount() const { try { - nElts = (CPLSM(static_cast(nElts)) * - CPLSM(static_cast(dim->GetSize()))) + nElts = (CPLSM(static_cast(nElts)) * + CPLSM(static_cast(dim->GetSize()))) .v(); } catch (...) diff --git a/gcore/gdalrasterband.cpp b/gcore/gdalrasterband.cpp index a4665cba021c..187f5dddb872 100644 --- a/gcore/gdalrasterband.cpp +++ b/gcore/gdalrasterband.cpp @@ -4987,8 +4987,6 @@ CPLErr CPL_STDCALL GDALGetRasterStatistics(GDALRasterBandH hBand, int bApproxOK, pdfStdDev); } -#ifdef CPL_HAS_GINT64 - /************************************************************************/ /* GDALUInt128 */ /************************************************************************/ @@ -6021,8 +6019,6 @@ struct ComputeStatisticsInternal // (defined(__x86_64__) || defined(_M_X64)) && (defined(__GNUC__) || // defined(_MSC_VER)) -#endif // CPL_HAS_GINT64 - /************************************************************************/ /* GetPixelValue() */ /************************************************************************/ @@ -6431,7 +6427,6 @@ CPLErr GDALRasterBand::ComputeStatistics(int bApproxOK, double *pdfMin, if (nSampleRate == 1) bApproxOK = false; -#ifdef CPL_HAS_GINT64 // Particular case for GDT_Byte that only use integral types for all // intermediate computations. Only possible if the number of pixels // explored is lower than GUINTBIG_MAX / (255*255), so that nSumSquare @@ -6580,7 +6575,6 @@ CPLErr GDALRasterBand::ComputeStatistics(int bApproxOK, double *pdfMin, "in sampling."); return CE_Failure; } -#endif GByte *pabyMaskData = nullptr; if (poMaskBand) diff --git a/gcore/rasterio.cpp b/gcore/rasterio.cpp index 65f2a4718d17..fed171e9c8e1 100644 --- a/gcore/rasterio.cpp +++ b/gcore/rasterio.cpp @@ -2067,7 +2067,6 @@ void CPL_STDCALL GDALSwapWords(void *pData, int nWordSize, int nWordCount, case 8: CPLAssert(nWordSkip >= 8 || nWordCount == 1); -#ifdef CPL_HAS_GINT64 if (CPL_IS_ALIGNED(pabyData, 8) && (nWordSkip % 8) == 0) { for (int i = 0; i < nWordCount; i++) @@ -2078,7 +2077,6 @@ void CPL_STDCALL GDALSwapWords(void *pData, int nWordSize, int nWordCount, } } else -#endif { for (int i = 0; i < nWordCount; i++) { diff --git a/gcore/rawdataset.cpp b/gcore/rawdataset.cpp index 841609137720..22bc2693fbf5 100644 --- a/gcore/rawdataset.cpp +++ b/gcore/rawdataset.cpp @@ -1760,17 +1760,17 @@ bool RAWDatasetCheckMemoryUsage(int nXSize, int nYSize, int nBands, int nDTSize, try { nExpectedFileSize = - (CPLSM(static_cast(nHeaderSize)) + - CPLSM(static_cast(nBandOffset)) * - CPLSM(static_cast(nBands - 1)) + + (CPLSM(static_cast(nHeaderSize)) + + CPLSM(static_cast(nBandOffset)) * + CPLSM(static_cast(nBands - 1)) + (nLineOffset >= 0 - ? CPLSM(static_cast(nYSize - 1)) * - CPLSM(static_cast(nLineOffset)) - : CPLSM(static_cast(0))) + + ? CPLSM(static_cast(nYSize - 1)) * + CPLSM(static_cast(nLineOffset)) + : CPLSM(static_cast(0))) + (nPixelOffset >= 0 - ? CPLSM(static_cast(nXSize - 1)) * - CPLSM(static_cast(nPixelOffset)) - : CPLSM(static_cast(0)))) + ? CPLSM(static_cast(nXSize - 1)) * + CPLSM(static_cast(nPixelOffset)) + : CPLSM(static_cast(0)))) .v(); } catch (...) diff --git a/ogr/ogr_swq.h b/ogr/ogr_swq.h index 71f4f45e6b6b..c777d480cbdf 100644 --- a/ogr/ogr_swq.h +++ b/ogr/ogr_swq.h @@ -183,7 +183,7 @@ class CPL_UNSTABLE_API swq_expr_node /* only for SNT_CONSTANT */ int is_null = false; - GIntBig int_value = 0; + int64_t int_value = 0; double float_value = 0.0; OGRGeometry *geometry_value = nullptr; diff --git a/ogr/ogrsf_frmts/cad/libopencad/cadheader.cpp b/ogr/ogrsf_frmts/cad/libopencad/cadheader.cpp index e4f25c04e099..6bbbb1a3091e 100644 --- a/ogr/ogrsf_frmts/cad/libopencad/cadheader.cpp +++ b/ogr/ogrsf_frmts/cad/libopencad/cadheader.cpp @@ -180,23 +180,23 @@ long CADHandle::getAsLong( const CADHandle& ref_handle ) const { case 0x06: { - return static_cast((CPLSM(static_cast(getAsLong(ref_handle.handleOrOffset))) + - CPLSM(static_cast(1))).v()); + return static_cast((CPLSM(static_cast(getAsLong(ref_handle.handleOrOffset))) + + CPLSM(static_cast(1))).v()); } case 0x08: { - return static_cast((CPLSM(static_cast(getAsLong(ref_handle.handleOrOffset))) - - CPLSM(static_cast(1))).v()); + return static_cast((CPLSM(static_cast(getAsLong(ref_handle.handleOrOffset))) - + CPLSM(static_cast(1))).v()); } case 0x0A: { - return static_cast((CPLSM(static_cast(getAsLong(ref_handle.handleOrOffset))) + - CPLSM(static_cast(getAsLong(handleOrOffset)))).v()); + return static_cast((CPLSM(static_cast(getAsLong(ref_handle.handleOrOffset))) + + CPLSM(static_cast(getAsLong(handleOrOffset)))).v()); } case 0x0C: { - return static_cast((CPLSM(static_cast(getAsLong(ref_handle.handleOrOffset))) - - CPLSM(static_cast(getAsLong(handleOrOffset)))).v()); + return static_cast((CPLSM(static_cast(getAsLong(ref_handle.handleOrOffset))) - + CPLSM(static_cast(getAsLong(handleOrOffset)))).v()); } } } diff --git a/ogr/ogrsf_frmts/generic/ogr_gensql.cpp b/ogr/ogrsf_frmts/generic/ogr_gensql.cpp index 726415bf29b9..df85675f1176 100644 --- a/ogr/ogrsf_frmts/generic/ogr_gensql.cpp +++ b/ogr/ogrsf_frmts/generic/ogr_gensql.cpp @@ -1653,7 +1653,8 @@ std::unique_ptr OGRGenSQLResultsLayer::TranslateFeature( case SWQ_BOOLEAN: case SWQ_INTEGER: case SWQ_INTEGER64: - poDstFeat->SetField(iRegularField++, poResult->int_value); + poDstFeat->SetField(iRegularField++, + static_cast(poResult->int_value)); break; case SWQ_FLOAT: diff --git a/ogr/ogrsf_frmts/gmlutils/ogrwfsfilter.cpp b/ogr/ogrsf_frmts/gmlutils/ogrwfsfilter.cpp index a162fcd2c02c..32c84a3ed4dd 100644 --- a/ogr/ogrsf_frmts/gmlutils/ogrwfsfilter.cpp +++ b/ogr/ogrsf_frmts/gmlutils/ogrwfsfilter.cpp @@ -13,6 +13,8 @@ #include "ogrwfsfilter.h" #include "ogr_p.h" +#include + typedef struct { int nVersion; @@ -53,7 +55,7 @@ static bool WFS_ExprDumpGmlObjectIdFilter(CPLString &osFilter, poExpr->papoSubExpr[1]->field_type == SWQ_INTEGER64) { osFilter += - CPLSPrintf(CPL_FRMT_GIB, poExpr->papoSubExpr[1]->int_value); + CPLSPrintf("%" PRId64, poExpr->papoSubExpr[1]->int_value); } else if (poExpr->papoSubExpr[1]->field_type == SWQ_STRING) { @@ -92,7 +94,7 @@ static bool WFS_ExprDumpRawLitteral(CPLString &osFilter, { if (poExpr->field_type == SWQ_INTEGER || poExpr->field_type == SWQ_INTEGER64) - osFilter += CPLSPrintf(CPL_FRMT_GIB, poExpr->int_value); + osFilter += CPLSPrintf("%" PRId64, poExpr->int_value); else if (poExpr->field_type == SWQ_FLOAT) osFilter += CPLSPrintf("%.16g", poExpr->float_value); else if (poExpr->field_type == SWQ_STRING) diff --git a/ogr/swq_expr_node.cpp b/ogr/swq_expr_node.cpp index 3cbedff6785e..9fd19c4d31cb 100644 --- a/ogr/swq_expr_node.cpp +++ b/ogr/swq_expr_node.cpp @@ -19,6 +19,7 @@ #include #include +#include #include #include #include @@ -427,7 +428,7 @@ void swq_expr_node::Dump(FILE *fp, int depth) { if (field_type == SWQ_INTEGER || field_type == SWQ_INTEGER64 || field_type == SWQ_BOOLEAN) - fprintf(fp, "%s " CPL_FRMT_GIB "\n", spaces, int_value); + fprintf(fp, "%s %" PRId64 "\n", spaces, int_value); else if (field_type == SWQ_FLOAT) fprintf(fp, "%s %.15g\n", spaces, float_value); else if (field_type == SWQ_GEOMETRY) @@ -538,7 +539,7 @@ char *swq_expr_node::Unparse(swq_field_list *field_list, char chColumnQuote) if (field_type == SWQ_INTEGER || field_type == SWQ_INTEGER64 || field_type == SWQ_BOOLEAN) - osExpr.Printf(CPL_FRMT_GIB, int_value); + osExpr.Printf("%" PRId64, int_value); else if (field_type == SWQ_FLOAT) { osExpr.Printf("%.15g", float_value); diff --git a/port/cpl_safemaths.hpp b/port/cpl_safemaths.hpp index b88ae16b6bc8..565bfe183e15 100644 --- a/port/cpl_safemaths.hpp +++ b/port/cpl_safemaths.hpp @@ -17,6 +17,8 @@ #include #include +#include + #ifndef __has_builtin #define __has_builtin(x) 0 #endif @@ -29,23 +31,11 @@ #include "safeint.h" -#elif defined(GDAL_COMPILATION) -#include "cpl_port.h" - -#elif !defined(DISABLE_GINT64) && !defined(CPL_HAS_GINT64) -#if defined(WIN32) && defined(_MSC_VER) -typedef _int64 GInt64; -typedef unsigned _int64 GUInt64; -#else -typedef long long GInt64; -typedef unsigned long long GUInt64; -#endif -#define CPL_HAS_GINT64 #endif template struct CPLSafeInt { - T val; + const T val; inline explicit CPLSafeInt(T valIn) : val(valIn) { @@ -91,27 +81,20 @@ inline CPLSafeInt CPLSM_TO_UNSIGNED(int x) return CPLSafeInt(static_cast(x)); } -// Unimplemented for now -inline CPLSafeInt CPLSM_TO_UNSIGNED(unsigned x); -inline CPLSafeInt CPLSM(long x); -inline CPLSafeInt CPLSM_TO_UNSIGNED(long x); -inline CPLSafeInt CPLSM(unsigned long x); -inline CPLSafeInt CPLSM_TO_UNSIGNED(unsigned long x); -#if defined(GDAL_COMPILATION) -inline CPLSafeInt CPLSM(GInt64 x) +inline CPLSafeInt CPLSM(int64_t x) { - return CPLSafeInt(x); + return CPLSafeInt(x); } -inline CPLSafeInt CPLSM_TO_UNSIGNED(GInt64 x); - -inline CPLSafeInt CPLSM(GUInt64 x) +inline CPLSafeInt CPLSM(uint64_t x) { - return CPLSafeInt(x); + return CPLSafeInt(x); } -inline CPLSafeInt CPLSM_TO_UNSIGNED(GUInt64 x); -#endif +// Unimplemented for now +inline CPLSafeInt CPLSM_TO_UNSIGNED(unsigned x); +inline CPLSafeInt CPLSM_TO_UNSIGNED(int64_t x); +inline CPLSafeInt CPLSM_TO_UNSIGNED(uint64_t x); #if !defined(BUILTIN_OVERFLOW_CHECK_AVAILABLE) && defined(_MSC_VER) class CPLMSVCSafeIntException : public msl::utilities::SafeIntException @@ -154,34 +137,32 @@ inline CPLSafeInt operator+(const CPLSafeInt &A, msl::utilities::SafeInt A2(A.v()); msl::utilities::SafeInt B2(B.v()); return CPLSM(static_cast(A2 + B2)); -#elif defined(CPL_HAS_GINT64) +#else const int a = A.v(); const int b = B.v(); - const GInt64 res = static_cast(a) + b; + const int64_t res = static_cast(a) + b; if (res < std::numeric_limits::min() || res > std::numeric_limits::max()) { throw CPLSafeIntOverflow(); } return CPLSM(static_cast(res)); -#else - return SafeAddSigned(A, B); #endif } #if defined(GDAL_COMPILATION) -inline CPLSafeInt operator+(const CPLSafeInt &A, - const CPLSafeInt &B) +inline CPLSafeInt operator+(const CPLSafeInt &A, + const CPLSafeInt &B) { #ifdef BUILTIN_OVERFLOW_CHECK_AVAILABLE - GInt64 res; + long long res; if (__builtin_saddll_overflow(A.v(), B.v(), &res)) throw CPLSafeIntOverflow(); - return CPLSM(res); + return CPLSM(static_cast(res)); #elif defined(_MSC_VER) - msl::utilities::SafeInt A2(A.v()); - msl::utilities::SafeInt B2(B.v()); - return CPLSM(static_cast(A2 + B2)); + msl::utilities::SafeInt A2(A.v()); + msl::utilities::SafeInt B2(B.v()); + return CPLSM(static_cast(A2 + B2)); #else return SafeAddSigned(A, B); #endif @@ -210,22 +191,22 @@ inline CPLSafeInt operator+(const CPLSafeInt &A, } #if defined(GDAL_COMPILATION) -inline CPLSafeInt operator+(const CPLSafeInt &A, - const CPLSafeInt &B) +inline CPLSafeInt operator+(const CPLSafeInt &A, + const CPLSafeInt &B) { #ifdef BUILTIN_OVERFLOW_CHECK_AVAILABLE - GUInt64 res; + unsigned long long res; if (__builtin_uaddll_overflow(A.v(), B.v(), &res)) throw CPLSafeIntOverflow(); - return CPLSM(res); + return CPLSM(static_cast(res)); #elif defined(_MSC_VER) - msl::utilities::SafeInt A2(A.v()); - msl::utilities::SafeInt B2(B.v()); - return CPLSM(static_cast(A2 + B2)); + msl::utilities::SafeInt A2(A.v()); + msl::utilities::SafeInt B2(B.v()); + return CPLSM(static_cast(A2 + B2)); #else - const GUInt64 a = A.v(); - const GUInt64 b = B.v(); - if (a > std::numeric_limits::max() - b) + const uint64_t a = A.v(); + const uint64_t b = B.v(); + if (a > std::numeric_limits::max() - b) throw CPLSafeIntOverflow(); return CPLSM(a + b); #endif @@ -258,39 +239,35 @@ inline CPLSafeInt operator-(const CPLSafeInt &A, msl::utilities::SafeInt A2(A.v()); msl::utilities::SafeInt B2(B.v()); return CPLSM(static_cast(A2 - B2)); -#elif defined(CPL_HAS_GINT64) +#else const int a = A.v(); const int b = B.v(); - const GInt64 res = static_cast(a) - b; + const int64_t res = static_cast(a) - b; if (res < std::numeric_limits::min() || res > std::numeric_limits::max()) { throw CPLSafeIntOverflow(); } return CPLSM(static_cast(res)); -#else - return SafeSubSigned(A, B); #endif } -#if defined(GDAL_COMPILATION) -inline CPLSafeInt operator-(const CPLSafeInt &A, - const CPLSafeInt &B) +inline CPLSafeInt operator-(const CPLSafeInt &A, + const CPLSafeInt &B) { #ifdef BUILTIN_OVERFLOW_CHECK_AVAILABLE - GInt64 res; + long long res; if (__builtin_ssubll_overflow(A.v(), B.v(), &res)) throw CPLSafeIntOverflow(); - return CPLSM(res); + return CPLSM(static_cast(res)); #elif defined(_MSC_VER) - msl::utilities::SafeInt A2(A.v()); - msl::utilities::SafeInt B2(B.v()); - return CPLSM(static_cast(A2 - B2)); + msl::utilities::SafeInt A2(A.v()); + msl::utilities::SafeInt B2(B.v()); + return CPLSM(static_cast(A2 - B2)); #else return SafeSubSigned(A, B); #endif } -#endif // GDAL_COMPILATION inline CPLSafeInt operator-(const CPLSafeInt &A, const CPLSafeInt &B) @@ -353,39 +330,35 @@ inline CPLSafeInt operator*(const CPLSafeInt &A, msl::utilities::SafeInt A2(A.v()); msl::utilities::SafeInt B2(B.v()); return CPLSM(static_cast(A2 * B2)); -#elif defined(CPL_HAS_GINT64) +#else const int a = A.v(); const int b = B.v(); - const GInt64 res = static_cast(a) * b; + const int64_t res = static_cast(a) * b; if (res < std::numeric_limits::min() || res > std::numeric_limits::max()) { throw CPLSafeIntOverflow(); } return CPLSM(static_cast(res)); -#else - return SafeMulSigned(A, B); #endif } -#if defined(GDAL_COMPILATION) -inline CPLSafeInt operator*(const CPLSafeInt &A, - const CPLSafeInt &B) +inline CPLSafeInt operator*(const CPLSafeInt &A, + const CPLSafeInt &B) { #if defined(BUILTIN_OVERFLOW_CHECK_AVAILABLE) && defined(__x86_64__) - GInt64 res; + long long res; if (__builtin_smulll_overflow(A.v(), B.v(), &res)) throw CPLSafeIntOverflow(); - return CPLSM(res); + return CPLSM(static_cast(res)); #elif defined(_MSC_VER) - msl::utilities::SafeInt A2(A.v()); - msl::utilities::SafeInt B2(B.v()); - return CPLSM(static_cast(A2 * B2)); + msl::utilities::SafeInt A2(A.v()); + msl::utilities::SafeInt B2(B.v()); + return CPLSM(static_cast(A2 * B2)); #else return SafeMulSigned(A, B); #endif } -#endif // GDAL_COMPILATION inline CPLSafeInt operator*(const CPLSafeInt &A, const CPLSafeInt &B) @@ -399,46 +372,38 @@ inline CPLSafeInt operator*(const CPLSafeInt &A, msl::utilities::SafeInt A2(A.v()); msl::utilities::SafeInt B2(B.v()); return CPLSM(static_cast(A2 * B2)); -#elif defined(CPL_HAS_GINT64) +#else const unsigned a = A.v(); const unsigned b = B.v(); - const GUInt64 res = static_cast(a) * b; + const uint64_t res = static_cast(a) * b; if (res > std::numeric_limits::max()) { throw CPLSafeIntOverflow(); } return CPLSM(static_cast(res)); -#else - const unsigned a = A.v(); - const unsigned b = B.v(); - if (b > 0 && a > std::numeric_limits::max() / b) - throw CPLSafeIntOverflow(); - return CPLSM(a * b); #endif } -#if defined(GDAL_COMPILATION) -inline CPLSafeInt operator*(const CPLSafeInt &A, - const CPLSafeInt &B) +inline CPLSafeInt operator*(const CPLSafeInt &A, + const CPLSafeInt &B) { #ifdef BUILTIN_OVERFLOW_CHECK_AVAILABLE - GUInt64 res; + unsigned long long res; if (__builtin_umulll_overflow(A.v(), B.v(), &res)) throw CPLSafeIntOverflow(); - return CPLSM(res); + return CPLSM(static_cast(res)); #elif defined(_MSC_VER) - msl::utilities::SafeInt A2(A.v()); - msl::utilities::SafeInt B2(B.v()); - return CPLSM(static_cast(A2 * B2)); + msl::utilities::SafeInt A2(A.v()); + msl::utilities::SafeInt B2(B.v()); + return CPLSM(static_cast(A2 * B2)); #else - const GUInt64 a = A.v(); - const GUInt64 b = B.v(); - if (b > 0 && a > std::numeric_limits::max() / b) + const uint64_t a = A.v(); + const uint64_t b = B.v(); + if (b > 0 && a > std::numeric_limits::max() / b) throw CPLSafeIntOverflow(); return CPLSM(a * b); #endif } -#endif // GDAL_COMPILATION template inline CPLSafeInt SafeDivSigned(const CPLSafeInt &A, @@ -459,13 +424,11 @@ inline CPLSafeInt operator/(const CPLSafeInt &A, return SafeDivSigned(A, B); } -#if defined(GDAL_COMPILATION) -inline CPLSafeInt operator/(const CPLSafeInt &A, - const CPLSafeInt &B) +inline CPLSafeInt operator/(const CPLSafeInt &A, + const CPLSafeInt &B) { return SafeDivSigned(A, B); } -#endif // GDAL_COMPILATION inline CPLSafeInt operator/(const CPLSafeInt &A, const CPLSafeInt &B)