diff --git a/src/OpenFOAM/primitives/Scalar/scalar/scalar.H b/src/OpenFOAM/primitives/Scalar/scalar/scalar.H index 2535989316..d87bfca1c8 100644 --- a/src/OpenFOAM/primitives/Scalar/scalar/scalar.H +++ b/src/OpenFOAM/primitives/Scalar/scalar/scalar.H @@ -6,7 +6,7 @@ \\/ M anipulation | ------------------------------------------------------------------------------- Copyright (C) 2011-2016 OpenFOAM Foundation - Copyright (C) 2017-2019 OpenCFD Ltd. + Copyright (C) 2017-2020 OpenCFD Ltd. ------------------------------------------------------------------------------- License This file is part of OpenFOAM. @@ -153,6 +153,31 @@ namespace Foam #endif +// * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * // + +// Type conversions (narrowing) + +namespace Foam +{ + +//- Type narrowing from double to float +// Overflow: silently fix, or raise error? +inline float narrowFloat(const double val) +{ + // Single statement - future constexpr? + return + ( + (val <= -floatScalarVGREAT) ? -floatScalarVGREAT + : (val >= floatScalarVGREAT) ? floatScalarVGREAT + : (val > -floatScalarVSMALL && val < floatScalarVSMALL) // underflow + ? 0 + : static_cast(val) + ); +} + +} // End namespace Foam + + // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * // // Additional transcendental functions and specialisations diff --git a/src/OpenFOAM/primitives/ints/label/label.H b/src/OpenFOAM/primitives/ints/label/label.H index 8990a351dc..36c5906fc6 100644 --- a/src/OpenFOAM/primitives/ints/label/label.H +++ b/src/OpenFOAM/primitives/ints/label/label.H @@ -6,7 +6,7 @@ \\/ M anipulation | ------------------------------------------------------------------------------- Copyright (C) 2011-2014 OpenFOAM Foundation - Copyright (C) 2018-2019 OpenCFD Ltd. + Copyright (C) 2018-2020 OpenCFD Ltd. ------------------------------------------------------------------------------- License This file is part of OpenFOAM. @@ -169,6 +169,24 @@ struct labelOp }; +// * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * // + +// Type conversions (narrowing) + +//- Type narrowing from int64_t to int32_t +// Overflow: silently fix, or raise error? +inline int32_t narrowInt32(const int64_t val) +{ + // Single statement - future constexpr? + return + ( + (val < INT32_MIN) ? INT32_MIN + : (val > INT32_MAX) ? INT32_MAX + : static_cast(val) + ); +} + + // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * // } // End namespace Foam