ENH: define stdFoam::min(), stdFoam::max() as constexpr as per C++14
This commit is contained in:
parent
991a15c4ad
commit
0a5e4cf1b0
@ -28,36 +28,57 @@ Description
|
|||||||
|
|
||||||
#include "bool.H"
|
#include "bool.H"
|
||||||
#include "IOstreams.H"
|
#include "IOstreams.H"
|
||||||
|
#include "stdFoam.H"
|
||||||
|
|
||||||
#include <algorithm>
|
#include <algorithm>
|
||||||
#include <type_traits>
|
#include <type_traits>
|
||||||
#include <limits>
|
#include <limits>
|
||||||
|
|
||||||
|
|
||||||
|
namespace Foam
|
||||||
|
{
|
||||||
|
namespace Detail
|
||||||
|
{
|
||||||
|
|
||||||
template<typename UIntType, UIntType v, unsigned int n>
|
template<typename UIntType, UIntType v, unsigned int n>
|
||||||
struct set_lower_bits
|
struct bitops_setlower
|
||||||
:
|
:
|
||||||
public std::integral_constant
|
std::integral_constant
|
||||||
<
|
<
|
||||||
UIntType,
|
UIntType,
|
||||||
v | (v >> n) | set_lower_bits<UIntType, v | (v >> n), (n >> 1)>::value
|
v | (v >> n) | bitops_setlower<UIntType, v | (v >> n), (n >> 1)>::value
|
||||||
>
|
>
|
||||||
{};
|
{};
|
||||||
|
|
||||||
|
|
||||||
template<typename UIntType, UIntType v>
|
template<typename UIntType, UIntType v>
|
||||||
struct set_lower_bits<UIntType, v, 1>
|
struct bitops_setlower<UIntType, v, 1>
|
||||||
:
|
:
|
||||||
public std::integral_constant<UIntType, v | (v >> 1)>
|
std::integral_constant<UIntType, v | (v >> 1)>
|
||||||
{};
|
{};
|
||||||
|
|
||||||
|
|
||||||
|
template<size_t N>
|
||||||
|
struct bitops_topbit
|
||||||
|
:
|
||||||
|
std::integral_constant
|
||||||
|
<
|
||||||
|
size_t,
|
||||||
|
bitops_topbit<(N >> 1)>{} + 1
|
||||||
|
>
|
||||||
|
{};
|
||||||
|
|
||||||
|
template<> struct bitops_topbit<2> : std::integral_constant<size_t,1> {};
|
||||||
|
template<> struct bitops_topbit<1> : std::integral_constant<size_t,1> {};
|
||||||
|
template<> struct bitops_topbit<0> : std::integral_constant<size_t,0> {};
|
||||||
|
|
||||||
|
|
||||||
template<typename UIntType, UIntType v>
|
template<typename UIntType, UIntType v>
|
||||||
struct pow2ceil
|
struct pow2ceil
|
||||||
:
|
:
|
||||||
public std::integral_constant
|
public std::integral_constant
|
||||||
<
|
<
|
||||||
UIntType,
|
UIntType,
|
||||||
set_lower_bits
|
bitops_setlower
|
||||||
<
|
<
|
||||||
UIntType,
|
UIntType,
|
||||||
v - 1,
|
v - 1,
|
||||||
@ -66,22 +87,53 @@ struct pow2ceil
|
|||||||
>
|
>
|
||||||
{};
|
{};
|
||||||
|
|
||||||
|
|
||||||
// No quite
|
|
||||||
template<size_t N>
|
template<size_t N>
|
||||||
struct topbit : std::integral_constant<size_t, topbit<(N >> 1)>{} + 1> {};
|
struct pow2ceil_shift
|
||||||
|
:
|
||||||
|
std::integral_constant
|
||||||
|
<
|
||||||
|
size_t,
|
||||||
|
bitops_topbit<pow2ceil<size_t, N>::value>::value
|
||||||
|
>
|
||||||
|
{};
|
||||||
|
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
template<typename UIntType, UIntType v>
|
||||||
|
struct pow2ceil
|
||||||
|
:
|
||||||
|
Foam::Detail::pow2ceil<UIntType,v>
|
||||||
|
{};
|
||||||
|
|
||||||
|
|
||||||
|
template<size_t N>
|
||||||
|
struct pow2topbit
|
||||||
|
:
|
||||||
|
Foam::Detail::pow2ceil_shift<N>
|
||||||
|
{};
|
||||||
|
|
||||||
template<> struct topbit<1> : std::integral_constant<size_t,1> {};
|
|
||||||
template<> struct topbit<0> : std::integral_constant<size_t,0> {};
|
|
||||||
|
|
||||||
using namespace Foam;
|
using namespace Foam;
|
||||||
|
|
||||||
|
|
||||||
template<size_t N>
|
template<size_t N>
|
||||||
void printTopbit()
|
void printTopbit()
|
||||||
{
|
{
|
||||||
std::cout
|
std::cout
|
||||||
<<"topbit<" << N << "> : " << topbit<N>::value << '\n';
|
<< "pow2ceil<" << N << "> = "
|
||||||
|
<< pow2ceil<size_t, N>::value
|
||||||
|
<< " shift = " << pow2topbit<N>::value << '\n';
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
template<class T, int Offset = 19>
|
||||||
|
void printOffset()
|
||||||
|
{
|
||||||
|
std::cout
|
||||||
|
<< "pow2ceil of " << typeid(T).name() << " <" << sizeof(T) << "> = "
|
||||||
|
<< pow2ceil<size_t, sizeof(T)>::value
|
||||||
|
<< " shift = " << pow2topbit<sizeof(T)>::value << '\n';
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
@ -114,6 +166,16 @@ int main(int argc, char *argv[])
|
|||||||
printTopbit<16>();
|
printTopbit<16>();
|
||||||
printTopbit<17>();
|
printTopbit<17>();
|
||||||
printTopbit<18>();
|
printTopbit<18>();
|
||||||
|
printTopbit<29>();
|
||||||
|
printTopbit<30>();
|
||||||
|
printTopbit<31>();
|
||||||
|
printTopbit<32>();
|
||||||
|
printTopbit<33>();
|
||||||
|
printTopbit<4095>();
|
||||||
|
printTopbit<4096>();
|
||||||
|
printTopbit<4097>();
|
||||||
|
|
||||||
|
printOffset<double>();
|
||||||
|
|
||||||
Info << "---\nEnd\n" << endl;
|
Info << "---\nEnd\n" << endl;
|
||||||
|
|
||||||
|
@ -2,7 +2,7 @@
|
|||||||
========= |
|
========= |
|
||||||
\\ / F ield | OpenFOAM: The Open Source CFD Toolbox
|
\\ / F ield | OpenFOAM: The Open Source CFD Toolbox
|
||||||
\\ / O peration |
|
\\ / O peration |
|
||||||
\\ / A nd | Copyright (C) 2017 OpenCFD Ltd.
|
\\ / A nd | Copyright (C) 2017-2018 OpenCFD Ltd.
|
||||||
\\/ M anipulation |
|
\\/ M anipulation |
|
||||||
-------------------------------------------------------------------------------
|
-------------------------------------------------------------------------------
|
||||||
License
|
License
|
||||||
@ -155,6 +155,21 @@ constexpr auto crend(const C& c) -> decltype(c.rend())
|
|||||||
return c.rend();
|
return c.rend();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
//- Return the lesser of the parameters.
|
||||||
|
// Definition as per std::min C++14
|
||||||
|
template<class T>
|
||||||
|
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<class T>
|
||||||
|
constexpr inline const T& max(const T& a, const T& b)
|
||||||
|
{
|
||||||
|
return (a < b) ? b : a;
|
||||||
|
}
|
||||||
|
|
||||||
} // End namespace stdFoam
|
} // End namespace stdFoam
|
||||||
|
|
||||||
|
Loading…
Reference in New Issue
Block a user