diff --git a/applications/test/bitops/Make/files b/applications/test/bitops/Make/files new file mode 100644 index 0000000000..c3d8c066b2 --- /dev/null +++ b/applications/test/bitops/Make/files @@ -0,0 +1,3 @@ +Test-bitops.C + +EXE = $(FOAM_USER_APPBIN)/Test-bitops diff --git a/applications/test/bitops/Make/options b/applications/test/bitops/Make/options new file mode 100644 index 0000000000..6a9e9810b3 --- /dev/null +++ b/applications/test/bitops/Make/options @@ -0,0 +1,2 @@ +/* EXE_INC = -I$(LIB_SRC)/cfdTools/include */ +/* EXE_LIBS = -lfiniteVolume */ diff --git a/applications/test/bitops/Test-bitops.C b/applications/test/bitops/Test-bitops.C new file mode 100644 index 0000000000..dd3c2b03ac --- /dev/null +++ b/applications/test/bitops/Test-bitops.C @@ -0,0 +1,124 @@ +/*---------------------------------------------------------------------------*\ + ========= | + \\ / F ield | OpenFOAM: The Open Source CFD Toolbox + \\ / O peration | + \\ / A nd | Copyright (C) 2018 OpenCFD Ltd. + \\/ M anipulation | +------------------------------------------------------------------------------- +License + This file is part of OpenFOAM. + + OpenFOAM is free software: you can redistribute it and/or modify it + under the terms of the GNU General Public License as published by + the Free Software Foundation, either version 3 of the License, or + (at your option) any later version. + + OpenFOAM is distributed in the hope that it will be useful, but WITHOUT + ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or + FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License + for more details. + + You should have received a copy of the GNU General Public License + along with OpenFOAM. If not, see . + +Description + Test some bit-operations. + +\*---------------------------------------------------------------------------*/ + +#include "bool.H" +#include "IOstreams.H" + +#include +#include +#include + +template +struct set_lower_bits +: + public std::integral_constant + < + UIntType, + v | (v >> n) | set_lower_bits> n), (n >> 1)>::value + > +{}; + + +template +struct set_lower_bits +: + public std::integral_constant> 1)> +{}; + + +template +struct pow2ceil +: + public std::integral_constant + < + UIntType, + set_lower_bits + < + UIntType, + v - 1, + (std::numeric_limits::digits >> 1) + >::value + 1 + > +{}; + + +// No quite +template +struct topbit : std::integral_constant> 1)>{} + 1> {}; + +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'; +} + + +// * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * // + +int main(int argc, char *argv[]) +{ + Info<<"pow2ceil<10>: " + << pow2ceil::value << nl; + + Info<<"pow2ceil<16>: " + << pow2ceil::value << nl; + + Info<<"pow2ceil<369>: " + << pow2ceil::value << nl; + + printTopbit<0>(); + printTopbit<1>(); + printTopbit<2>(); + printTopbit<3>(); + printTopbit<4>(); + printTopbit<5>(); + printTopbit<6>(); + printTopbit<7>(); + printTopbit<8>(); + printTopbit<9>(); + printTopbit<10>(); + printTopbit<11>(); + printTopbit<15>(); + printTopbit<16>(); + printTopbit<17>(); + printTopbit<18>(); + + Info << "---\nEnd\n" << endl; + + return 0; +} + + +// ************************************************************************* // diff --git a/src/OpenFOAM/containers/Lists/PackedList/PackedBoolList.H b/src/OpenFOAM/containers/Lists/PackedList/PackedBoolList.H index 05836769ba..28209b6ea5 100644 --- a/src/OpenFOAM/containers/Lists/PackedList/PackedBoolList.H +++ b/src/OpenFOAM/containers/Lists/PackedList/PackedBoolList.H @@ -124,11 +124,11 @@ public: explicit inline PackedBoolList(const UList& lst); //- Construct from a list of labels - // using the labels as indices to indicate which bits are set + //- using the labels as indices to indicate which bits are set explicit inline PackedBoolList(const labelUList& indices); //- Construct from a list of labels - // using the labels as indices to indicate which bits are set + //- using the labels as indices to indicate which bits are set explicit inline PackedBoolList(const labelUIndList& indices); //- Clone @@ -183,11 +183,11 @@ public: // Edit //- Transfer the contents of the argument list into this list - // and annul the argument list. + //- and annul the argument list. inline void transfer(PackedBoolList& lst); //- Transfer the contents of the argument list into this list - // and annul the argument list. + //- and annul the argument list. inline void transfer(PackedList<1>& lst); //- Transfer contents to the Xfer container @@ -209,11 +209,11 @@ public: void operator=(const UList& lst); //- Assignment operator, - // using the labels as indices to indicate which bits are set + //- using the labels as indices to indicate which bits are set inline void operator=(const labelUList& indices); //- Assignment operator, - // using the labels as indices to indicate which bits are set + //- using the labels as indices to indicate which bits are set inline void operator=(const labelUIndList& indices); //- Complement operator @@ -223,11 +223,11 @@ public: inline PackedBoolList& operator&=(const PackedList<1>& lst); //- And operator (lists may be dissimilar sizes) - // using the labels as indices to indicate which bits are set + //- using the labels as indices to indicate which bits are set inline PackedBoolList& operator&=(const labelUList& indices); //- And operator (lists may be dissimilar sizes) - // using the labels as indices to indicate which bits are set + //- using the labels as indices to indicate which bits are set inline PackedBoolList& operator&=(const labelUIndList& indices); //- Xor operator (lists may be dissimilar sizes) @@ -238,11 +238,11 @@ public: inline PackedBoolList& operator|=(const PackedList<1>& lst); //- Or operator (lists may be dissimilar sizes), - // using the labels as indices to indicate which bits are set + //- using the labels as indices to indicate which bits are set inline PackedBoolList& operator|=(const labelUList& indices); //- Or operator (lists may be dissimilar sizes), - // using the labels as indices to indicate which bits are set + //- using the labels as indices to indicate which bits are set inline PackedBoolList& operator|=(const labelUIndList& indices); diff --git a/src/OpenFOAM/containers/Lists/PackedList/PackedList.H b/src/OpenFOAM/containers/Lists/PackedList/PackedList.H index 1d41bd1424..2ae542aeaa 100644 --- a/src/OpenFOAM/containers/Lists/PackedList/PackedList.H +++ b/src/OpenFOAM/containers/Lists/PackedList/PackedList.H @@ -158,7 +158,7 @@ protected: // Protected Member Functions //- Calculate the list length when packed - inline static label packedLength(const label nElem); + inline static constexpr label packedLength(const label nElem); //- Read a list entry (allows for specialization) inline static unsigned int readValue(Istream& is); @@ -211,7 +211,7 @@ public: inline static constexpr unsigned int packing(); //- Masking for all bits below the offset - inline static unsigned int maskLower(unsigned offset); + inline static constexpr unsigned int maskLower(unsigned offset); // Forward declaration of iterators diff --git a/src/OpenFOAM/containers/Lists/PackedList/PackedListI.H b/src/OpenFOAM/containers/Lists/PackedList/PackedListI.H index 00d1d54b6e..94f91cee4b 100644 --- a/src/OpenFOAM/containers/Lists/PackedList/PackedListI.H +++ b/src/OpenFOAM/containers/Lists/PackedList/PackedListI.H @@ -49,17 +49,23 @@ inline constexpr unsigned int Foam::PackedList::packing() template -inline unsigned int Foam::PackedList::maskLower(unsigned offset) +inline constexpr unsigned int Foam::PackedList::maskLower +( + unsigned offset +) { // Return (1u << (nBits * offset)) - 1; // The next one works more reliably with overflows // eg, when compiled without optimization - return (~0u >> ( sizeof(StorageType)*CHAR_BIT - nBits * offset)); + return (~0u >> (sizeof(StorageType)*CHAR_BIT - nBits * offset)); } template -inline Foam::label Foam::PackedList::packedLength(const label nElem) +inline constexpr Foam::label Foam::PackedList::packedLength +( + const label nElem +) { return (nElem + packing() - 1) / packing(); } @@ -826,7 +832,6 @@ inline void Foam::PackedList::setSize } - template inline Foam::label Foam::PackedList::capacity() const { @@ -912,6 +917,7 @@ inline void Foam::PackedList::shrink() } } + template inline Foam::List& Foam::PackedList::storage() {