From 0a5e4cf1b05dcba55188a2c5b9ee09afa7ba8bbe Mon Sep 17 00:00:00 2001 From: Mark Olesen Date: Fri, 19 Jan 2018 17:13:03 +0100 Subject: [PATCH] ENH: define stdFoam::min(), stdFoam::max() as constexpr as per C++14 --- applications/test/bitops/Test-bitops.C | 90 ++++++++++++++++++++++---- src/OpenFOAM/include/stdFoam.H | 17 ++++- 2 files changed, 92 insertions(+), 15 deletions(-) diff --git a/applications/test/bitops/Test-bitops.C b/applications/test/bitops/Test-bitops.C index dd3c2b03ac..fa0dfb63bd 100644 --- a/applications/test/bitops/Test-bitops.C +++ b/applications/test/bitops/Test-bitops.C @@ -28,36 +28,57 @@ Description #include "bool.H" #include "IOstreams.H" +#include "stdFoam.H" #include #include #include + +namespace Foam +{ +namespace Detail +{ + template -struct set_lower_bits +struct bitops_setlower : - public std::integral_constant + std::integral_constant < UIntType, - v | (v >> n) | set_lower_bits> n), (n >> 1)>::value + v | (v >> n) | bitops_setlower> n), (n >> 1)>::value > {}; - template -struct set_lower_bits +struct bitops_setlower : - public std::integral_constant> 1)> + std::integral_constant> 1)> {}; +template +struct bitops_topbit +: + std::integral_constant + < + size_t, + bitops_topbit<(N >> 1)>{} + 1 + > +{}; + +template<> struct bitops_topbit<2> : std::integral_constant {}; +template<> struct bitops_topbit<1> : std::integral_constant {}; +template<> struct bitops_topbit<0> : std::integral_constant {}; + + template struct pow2ceil : public std::integral_constant < UIntType, - set_lower_bits + bitops_setlower < UIntType, v - 1, @@ -66,22 +87,53 @@ struct pow2ceil > {}; - -// No quite template -struct topbit : std::integral_constant> 1)>{} + 1> {}; +struct pow2ceil_shift +: + std::integral_constant + < + size_t, + bitops_topbit::value>::value + > +{}; + +} +} + + +template +struct pow2ceil +: + Foam::Detail::pow2ceil +{}; + + +template +struct pow2topbit +: + Foam::Detail::pow2ceil_shift +{}; -template<> struct topbit<1> : std::integral_constant {}; -template<> struct topbit<0> : std::integral_constant {}; using namespace Foam; - template void printTopbit() { std::cout - <<"topbit<" << N << "> : " << topbit::value << '\n'; + << "pow2ceil<" << N << "> = " + << pow2ceil::value + << " shift = " << pow2topbit::value << '\n'; +} + + +template +void printOffset() +{ + std::cout + << "pow2ceil of " << typeid(T).name() << " <" << sizeof(T) << "> = " + << pow2ceil::value + << " shift = " << pow2topbit::value << '\n'; } @@ -114,6 +166,16 @@ int main(int argc, char *argv[]) printTopbit<16>(); printTopbit<17>(); printTopbit<18>(); + printTopbit<29>(); + printTopbit<30>(); + printTopbit<31>(); + printTopbit<32>(); + printTopbit<33>(); + printTopbit<4095>(); + printTopbit<4096>(); + printTopbit<4097>(); + + printOffset(); Info << "---\nEnd\n" << endl; diff --git a/src/OpenFOAM/include/stdFoam.H b/src/OpenFOAM/include/stdFoam.H index ac6bd6c342..916d8baddf 100644 --- a/src/OpenFOAM/include/stdFoam.H +++ b/src/OpenFOAM/include/stdFoam.H @@ -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-2018 OpenCFD Ltd. \\/ M anipulation | ------------------------------------------------------------------------------- License @@ -155,6 +155,21 @@ constexpr auto crend(const C& c) -> decltype(c.rend()) return c.rend(); } +//- Return the lesser of the parameters. +// Definition as per std::min C++14 +template +constexpr inline const T& min(const T& a, const T& b) +{ + return (b < a) ? b : a; +} + +//- Return the greater of the parameters. +// Definition as per std::max C++14 +template +constexpr inline const T& max(const T& a, const T& b) +{ + return (a < b) ? b : a; +} } // End namespace stdFoam