Files
openfoam/src/OpenFOAM/containers/HashTables/StaticHashTable/StaticHashTableCore.C
Mark Olesen 710f23aa35 COMP: hash-table size overflow with 64-bit labels (fixes #498)
- Requires (1L << N) instead of (1 << N), otherwise it overflows
  and the result is zero.
2017-06-14 12:54:38 +02:00

87 lines
2.6 KiB
C

/*---------------------------------------------------------------------------*\
========= |
\\ / F ield | OpenFOAM: The Open Source CFD Toolbox
\\ / O peration |
\\ / A nd | Copyright (C) 2011-2012 OpenFOAM Foundation
\\/ 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 <http://www.gnu.org/licenses/>.
\*---------------------------------------------------------------------------*/
#include "StaticHashTable.H"
#include "uLabel.H"
// * * * * * * * * * * * * * * Static Data Members * * * * * * * * * * * * * //
namespace Foam
{
defineTypeNameAndDebug(StaticHashTableCore, 0);
}
// Approximately labelMax/4
static const Foam::label maxTableSize(1L << (sizeof(Foam::label)*8-3));
// * * * * * * * * * * * * * * Member Functions * * * * * * * * * * * * * * //
Foam::label Foam::StaticHashTableCore::canonicalSize(const label requested_size)
{
if (requested_size < 1)
{
return 0;
}
else if (requested_size >= maxTableSize)
{
return maxTableSize;
}
// Enforce power of two - makes for a very fast modulus.
// Use unsigned for these calculations.
//
// - The lower limit (8) is somewhat arbitrary, but if the hash table
// is too small, there will be many direct table collisions.
// - The upper limit (approx. labelMax/4) must be a power of two,
// need not be extremely large for hashing.
uLabel powerOfTwo = 8u; // lower-limit
const uLabel size = requested_size;
if (size <= powerOfTwo)
{
return powerOfTwo;
}
else if (size & (size-1)) // <- Modulus of i^2
{
// Determine power-of-two. Brute-force is fast enough.
while (powerOfTwo < size)
{
powerOfTwo <<= 1;
}
return powerOfTwo;
}
else
{
return size;
}
}
// ************************************************************************* //