mirror of
https://develop.openfoam.com/Development/openfoam.git
synced 2025-11-28 03:28:01 +00:00
ENH: add low-level readRawLabels, readRawScalars (#1378)
- these use the additional byte-size checks in IOstream to handle native vs non-native sizes
This commit is contained in:
committed by
Andrew Heather
parent
b0ffdbcfb1
commit
ef9bb4ae16
@ -249,7 +249,7 @@ public:
|
||||
}
|
||||
|
||||
|
||||
// Stream State Functions
|
||||
// Element sizes (precision)
|
||||
|
||||
//- The label byte-size associated with the stream
|
||||
unsigned labelByteSize() const
|
||||
@ -275,6 +275,28 @@ public:
|
||||
scalarByteSize_ = nbytes;
|
||||
}
|
||||
|
||||
|
||||
//- Check if the label byte-size associated with the stream
|
||||
//- is the same as the given type
|
||||
template<class T = label>
|
||||
typename std::enable_if<std::is_integral<T>::value, bool>::type
|
||||
checkLabelSize() const
|
||||
{
|
||||
return labelByteSize_ == sizeof(T);
|
||||
}
|
||||
|
||||
//- Check if the scalar byte-size associated with the stream
|
||||
//- is the same as the given type
|
||||
template<class T = scalar>
|
||||
typename std::enable_if<std::is_floating_point<T>::value, bool>::type
|
||||
checkScalarSize() const
|
||||
{
|
||||
return scalarByteSize_ == sizeof(T);
|
||||
}
|
||||
|
||||
|
||||
// Stream State Functions
|
||||
|
||||
//- Const access to the current stream line number
|
||||
label lineNumber() const
|
||||
{
|
||||
|
||||
@ -2,7 +2,7 @@
|
||||
========= |
|
||||
\\ / F ield | OpenFOAM: The Open Source CFD Toolbox
|
||||
\\ / O peration |
|
||||
\\ / A nd | Copyright (C) 2016-2018 OpenCFD Ltd.
|
||||
\\ / A nd | Copyright (C) 2016-2019 OpenCFD Ltd.
|
||||
\\/ M anipulation |
|
||||
-------------------------------------------------------------------------------
|
||||
| Copyright (C) 2011-2016 OpenFOAM Foundation
|
||||
@ -123,7 +123,7 @@ bool ScalarRead(const char* buf, Scalar& val)
|
||||
|
||||
Scalar ScalarRead(Istream& is)
|
||||
{
|
||||
Scalar val;
|
||||
Scalar val(0);
|
||||
is >> val;
|
||||
|
||||
return val;
|
||||
|
||||
@ -51,8 +51,7 @@ namespace Foam
|
||||
typedef double doubleScalar;
|
||||
|
||||
// Largest and smallest scalar values allowed in certain parts of the code.
|
||||
// (15 is the number of significant figures in an
|
||||
// IEEE double precision number. See limits.h or float.h)
|
||||
// See std::numeric_limits max(), min(), epsilon()
|
||||
constexpr doubleScalar doubleScalarGREAT = 1.0e+15;
|
||||
constexpr doubleScalar doubleScalarVGREAT = 1.0e+300;
|
||||
constexpr doubleScalar doubleScalarROOTVGREAT = 1.0e+150;
|
||||
|
||||
@ -51,8 +51,7 @@ namespace Foam
|
||||
typedef float floatScalar;
|
||||
|
||||
// Largest and smallest scalar values allowed in certain parts of the code.
|
||||
// (6 is the number of significant figures in an
|
||||
// IEEE single precision number. See limits.h or float.h)
|
||||
// See std::numeric_limits max(), min(), epsilon()
|
||||
constexpr floatScalar floatScalarGREAT = 1.0e+6;
|
||||
constexpr floatScalar floatScalarVGREAT = 1.0e+37;
|
||||
constexpr floatScalar floatScalarROOTVGREAT = 1.0e+18;
|
||||
|
||||
@ -2,10 +2,8 @@
|
||||
========= |
|
||||
\\ / F ield | OpenFOAM: The Open Source CFD Toolbox
|
||||
\\ / O peration |
|
||||
\\ / A nd |
|
||||
\\ / A nd | Copyright (C) 2004-2019 OpenCFD Ltd.
|
||||
\\/ M anipulation |
|
||||
-------------------------------------------------------------------------------
|
||||
| Copyright (C) 2011 OpenFOAM Foundation
|
||||
-------------------------------------------------------------------------------
|
||||
License
|
||||
This file is part of OpenFOAM.
|
||||
@ -32,11 +30,93 @@ License
|
||||
|
||||
Foam::scalar Foam::readScalar(Istream& is)
|
||||
{
|
||||
scalar val;
|
||||
scalar val(0);
|
||||
is >> val;
|
||||
|
||||
return val;
|
||||
}
|
||||
|
||||
|
||||
Foam::scalar Foam::readRawScalar(Istream& is)
|
||||
{
|
||||
scalar val(0);
|
||||
readRawScalars(is, &val, 1);
|
||||
return val;
|
||||
}
|
||||
|
||||
|
||||
void Foam::readRawScalars(Istream& is, scalar* data, size_t nElem)
|
||||
{
|
||||
// No check for binary vs ascii, the caller knows what they are doing
|
||||
|
||||
#if defined(WM_SP) || defined(WM_SPDP)
|
||||
|
||||
// Defined scalar as a float, non-native type is double
|
||||
// Handle type narrowing limits
|
||||
|
||||
typedef double nonNative;
|
||||
|
||||
if (is.checkScalarSize<nonNative>())
|
||||
{
|
||||
nonNative other;
|
||||
|
||||
for (const scalar* endData = data + nElem; data != endData; ++data)
|
||||
{
|
||||
is.readRaw(reinterpret_cast<char*>(&other), sizeof(nonNative));
|
||||
|
||||
// Type narrowing
|
||||
// Overflow: silently fix, or raise error?
|
||||
|
||||
if (other < -VGREAT)
|
||||
{
|
||||
*data = -VGREAT;
|
||||
}
|
||||
else if (other > VGREAT)
|
||||
{
|
||||
*data = VGREAT;
|
||||
}
|
||||
else if (other > -VSMALL && other < VSMALL)
|
||||
{
|
||||
// Underflow: round to zero
|
||||
*data = 0;
|
||||
}
|
||||
else
|
||||
{
|
||||
*data = scalar(other);
|
||||
}
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
// Read with native size
|
||||
is.readRaw(reinterpret_cast<char*>(data), nElem*sizeof(scalar));
|
||||
}
|
||||
|
||||
#elif defined(WM_DP)
|
||||
|
||||
// Defined scalar as a double, non-native type is float
|
||||
|
||||
typedef float nonNative;
|
||||
|
||||
if (is.checkScalarSize<nonNative>())
|
||||
{
|
||||
nonNative other;
|
||||
|
||||
for (const scalar* endData = data + nElem; data != endData; ++data)
|
||||
{
|
||||
is.readRaw(reinterpret_cast<char*>(&other), sizeof(nonNative));
|
||||
|
||||
*data = scalar(other);
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
// Read with native size
|
||||
is.readRaw(reinterpret_cast<char*>(data), nElem*sizeof(scalar));
|
||||
}
|
||||
|
||||
#endif
|
||||
}
|
||||
|
||||
|
||||
// ************************************************************************* //
|
||||
|
||||
@ -2,7 +2,7 @@
|
||||
========= |
|
||||
\\ / F ield | OpenFOAM: The Open Source CFD Toolbox
|
||||
\\ / O peration |
|
||||
\\ / A nd | Copyright (C) 2017 OpenCFD Ltd.
|
||||
\\ / A nd | Copyright (C) 2017-2019 OpenCFD Ltd.
|
||||
\\/ M anipulation |
|
||||
-------------------------------------------------------------------------------
|
||||
| Copyright (C) 2011-2016 OpenFOAM Foundation
|
||||
@ -64,8 +64,6 @@ namespace Foam
|
||||
constexpr scalar VSMALL = floatScalarVSMALL;
|
||||
constexpr scalar ROOTVSMALL = floatScalarROOTVSMALL;
|
||||
|
||||
scalar readScalar(Istream& is);
|
||||
|
||||
inline scalar readScalar(const char* buf)
|
||||
{
|
||||
return readFloat(buf);
|
||||
@ -85,6 +83,19 @@ namespace Foam
|
||||
{
|
||||
return readFloat(str, val);
|
||||
}
|
||||
|
||||
//- Read scalar from stream.
|
||||
scalar readScalar(Istream& is);
|
||||
|
||||
//- Read raw scalar from binary stream.
|
||||
// \note No internal check for binary vs ascii,
|
||||
// the caller knows what they are doing
|
||||
scalar readRawScalar(Istream& is);
|
||||
|
||||
//- Read raw scalars from binary stream.
|
||||
// \note No internal check for binary vs ascii,
|
||||
// the caller knows what they are doing
|
||||
void readRawScalars(Istream& is, scalar* data, size_t nElem);
|
||||
}
|
||||
|
||||
#elif defined(WM_DP)
|
||||
@ -104,8 +115,6 @@ namespace Foam
|
||||
constexpr scalar VSMALL = doubleScalarVSMALL;
|
||||
constexpr scalar ROOTVSMALL = doubleScalarROOTVSMALL;
|
||||
|
||||
scalar readScalar(Istream& is);
|
||||
|
||||
inline scalar readScalar(const char* buf)
|
||||
{
|
||||
return readDouble(buf);
|
||||
@ -125,6 +134,20 @@ namespace Foam
|
||||
{
|
||||
return readDouble(str, val);
|
||||
}
|
||||
|
||||
|
||||
//- Read scalar from stream.
|
||||
scalar readScalar(Istream& is);
|
||||
|
||||
//- Read raw scalar from binary stream.
|
||||
// \note No internal check for binary vs ascii,
|
||||
// the caller knows what they are doing
|
||||
scalar readRawScalar(Istream& is);
|
||||
|
||||
//- Read raw scalars from binary stream.
|
||||
// \note No internal check for binary vs ascii,
|
||||
// the caller knows what they are doing
|
||||
void readRawScalars(Istream& is, scalar* data, size_t nElem);
|
||||
}
|
||||
|
||||
#endif
|
||||
|
||||
@ -2,7 +2,7 @@
|
||||
========= |
|
||||
\\ / F ield | OpenFOAM: The Open Source CFD Toolbox
|
||||
\\ / O peration |
|
||||
\\ / A nd |
|
||||
\\ / A nd | Copyright (C) 2019 OpenCFD Ltd.
|
||||
\\/ M anipulation |
|
||||
-------------------------------------------------------------------------------
|
||||
| Copyright (C) 2011-2015 OpenFOAM Foundation
|
||||
@ -27,16 +27,95 @@ License
|
||||
|
||||
#include "error.H"
|
||||
#include "label.H"
|
||||
#include "Istream.H"
|
||||
|
||||
// * * * * * * * * * * * * * * Static Data Members * * * * * * * * * * * * * //
|
||||
|
||||
#if WM_LABEL_SIZE == 32
|
||||
const char* const Foam::pTraits<int32_t>::typeName = "label";
|
||||
const char* const Foam::pTraits<int64_t>::typeName = "int64";
|
||||
#elif WM_LABEL_SIZE == 64
|
||||
const char* const Foam::pTraits<int32_t>::typeName = "int32";
|
||||
const char* const Foam::pTraits<int64_t>::typeName = "label";
|
||||
#endif
|
||||
|
||||
|
||||
// * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
|
||||
|
||||
#if WM_LABEL_SIZE == 32
|
||||
const char* const Foam::pTraits<int64_t>::typeName = "int64";
|
||||
const char* const Foam::pTraits<int32_t>::typeName = "label";
|
||||
#elif WM_LABEL_SIZE == 64
|
||||
const char* const Foam::pTraits<int64_t>::typeName = "label";
|
||||
const char* const Foam::pTraits<int32_t>::typeName = "int32";
|
||||
#endif
|
||||
Foam::label Foam::readRawLabel(Istream& is)
|
||||
{
|
||||
label val(0);
|
||||
readRawLabels(is, &val, 1);
|
||||
return val;
|
||||
}
|
||||
|
||||
|
||||
void Foam::readRawLabels(Istream& is, label* data, size_t nElem)
|
||||
{
|
||||
// No check for binary vs ascii, the caller knows what they are doing
|
||||
|
||||
#if WM_LABEL_SIZE == 32
|
||||
|
||||
// Defined label as int32, non-native type is int64
|
||||
// Handle type narrowing limits
|
||||
|
||||
typedef int64_t nonNative;
|
||||
|
||||
if (is.checkLabelSize<nonNative>())
|
||||
{
|
||||
nonNative parsed;
|
||||
|
||||
for (const label* endData = data + nElem; data != endData; ++data)
|
||||
{
|
||||
is.readRaw(reinterpret_cast<char*>(&parsed), sizeof(nonNative));
|
||||
|
||||
// Type narrowing
|
||||
// Overflow: silently fix, or raise error?
|
||||
if (parsed < labelMin)
|
||||
{
|
||||
*data = labelMin;
|
||||
}
|
||||
else if (parsed > labelMax)
|
||||
{
|
||||
*data = labelMax;
|
||||
}
|
||||
else
|
||||
{
|
||||
*data = label(parsed);
|
||||
}
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
// Read with native size
|
||||
is.readRaw(reinterpret_cast<char*>(data), nElem*sizeof(label));
|
||||
}
|
||||
|
||||
#elif WM_LABEL_SIZE == 64
|
||||
|
||||
// Defined label as int64, non-native type is int32
|
||||
|
||||
typedef int32_t nonNative;
|
||||
|
||||
if (is.checkLabelSize<nonNative>())
|
||||
{
|
||||
nonNative parsed;
|
||||
|
||||
for (const label* endData = data + nElem; data != endData; ++data)
|
||||
{
|
||||
is.readRaw(reinterpret_cast<char*>(&parsed), sizeof(nonNative));
|
||||
|
||||
*data = label(parsed);
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
// Read with native size
|
||||
is.readRaw(reinterpret_cast<char*>(data), nElem*sizeof(label));
|
||||
}
|
||||
|
||||
#endif
|
||||
}
|
||||
|
||||
|
||||
// * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
|
||||
|
||||
@ -2,7 +2,7 @@
|
||||
========= |
|
||||
\\ / F ield | OpenFOAM: The Open Source CFD Toolbox
|
||||
\\ / O peration |
|
||||
\\ / A nd | Copyright (C) 2018 OpenCFD Ltd.
|
||||
\\ / A nd | Copyright (C) 2018-2019 OpenCFD Ltd.
|
||||
\\/ M anipulation |
|
||||
-------------------------------------------------------------------------------
|
||||
| Copyright (C) 2011-2014 OpenFOAM Foundation
|
||||
@ -63,13 +63,6 @@ typedef INT_SIZE(int, _t) label;
|
||||
constexpr label labelMin = INT_SIZE(INT, _MIN);
|
||||
constexpr label labelMax = INT_SIZE(INT, _MAX);
|
||||
|
||||
//- Read label from stream.
|
||||
// Uses readInt32 or readInt64 according to WM_LABEL_SIZE
|
||||
inline label readLabel(Istream& is)
|
||||
{
|
||||
return INT_SIZE(readInt,) (is);
|
||||
}
|
||||
|
||||
//- Parse entire buffer as a label, skipping leading/trailing whitespace.
|
||||
// Uses readInt32 or readInt64 according to WM_LABEL_SIZE
|
||||
// \return Parsed value or FatalIOError on any problem
|
||||
@ -103,6 +96,24 @@ inline bool readLabel(const std::string& str, label& val)
|
||||
}
|
||||
|
||||
|
||||
//- Read label from stream.
|
||||
// Uses readInt32 or readInt64 according to WM_LABEL_SIZE
|
||||
inline label readLabel(Istream& is)
|
||||
{
|
||||
return INT_SIZE(readInt,) (is);
|
||||
}
|
||||
|
||||
//- Read raw label from binary stream.
|
||||
// \note No internal check for binary vs ascii,
|
||||
// the caller knows what they are doing
|
||||
label readRawLabel(Istream& is);
|
||||
|
||||
//- Read raw labels from binary stream.
|
||||
// \note No internal check for binary vs ascii,
|
||||
// the caller knows what they are doing
|
||||
void readRawLabels(Istream& is, label* data, size_t nElem);
|
||||
|
||||
|
||||
// * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
|
||||
|
||||
//- Raise one label to the power of another
|
||||
|
||||
Reference in New Issue
Block a user