Loading...
Searching...
No Matches
HashTableCore.C
Go to the documentation of this file.
1/*---------------------------------------------------------------------------*\
2 ========= |
3 \\ / F ield | OpenFOAM: The Open Source CFD Toolbox
4 \\ / O peration |
5 \\ / A nd | www.openfoam.com
6 \\/ M anipulation |
7-------------------------------------------------------------------------------
8 Copyright (C) 2011-2012 OpenFOAM Foundation
9 Copyright (C) 2017-2025 OpenCFD Ltd.
10-------------------------------------------------------------------------------
11License
12 This file is part of OpenFOAM.
13
14 OpenFOAM is free software: you can redistribute it and/or modify it
15 under the terms of the GNU General Public License as published by
16 the Free Software Foundation, either version 3 of the License, or
17 (at your option) any later version.
18
19 OpenFOAM is distributed in the hope that it will be useful, but WITHOUT
20 ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
21 FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
22 for more details.
23
24 You should have received a copy of the GNU General Public License
25 along with OpenFOAM. If not, see <http://www.gnu.org/licenses/>.
26
27\*---------------------------------------------------------------------------*/
29#include "HashTableCore.H"
30
31// * * * * * * * * * * * * * * Static Data Members * * * * * * * * * * * * * //
32
33namespace Foam
35 defineTypeNameAndDebug(HashTableCore, 0);
36}
37
38
39// file-scope:
40// Minimum internal table size (must be a power of two!)
41constexpr int32_t minTableSize = 8;
42
43
44// * * * * * * * * * * * * * * Member Functions * * * * * * * * * * * * * * //
45
46Foam::label Foam::HashTableCore::canonicalSize(const label size) noexcept
47{
48 // Enforce power of two for fast modulus in hash index calculations.
49 // Use unsigned for these calculations.
50 //
51 // - The lower limit (8) is somewhat arbitrary, but if the hash table
52 // is too small, there will be many direct table collisions.
53 // - The upper limit (approx. INT32_MAX/4) must be a power of two,
54 // need not be extremely large for hashing.
55
56 if (size <= minTableSize)
57 {
58 return (size < 1 ? 0 : minTableSize);
59 }
60 else if (size > maxTableSize/2)
61 {
62 return maxTableSize;
63 }
64
65 // Determine power-of-two with glibc (may or may not be faster):
66 //
67 // return (1 << (32-__builtin_clz(int32_t(size-1))));
68
69 if (!(size & (size-1)))
70 {
71 // Already a power-of-two...
72 return size;
73 }
74
75 // Non-branching for 32-bit
76 // [https://graphics.stanford.edu/~seander/bithacks.html]
77 {
78 uint32_t n(size);
79 --n;
80 n |= n >> 1;
81 n |= n >> 2;
82 n |= n >> 4;
83 n |= n >> 8;
84 n |= n >> 16;
85 ++n;
86 return n;
87 }
88
89 // OLD:
90 // Brute-force method
91 //
92 // uint32_t n(minTableSize);
93 // while (n < uint32_t(size))
94 // {
95 // n <<= 1;
96 // }
97 // return n;
98}
99
100
101// ************************************************************************* //
constexpr int32_t minTableSize
label n
#define defineTypeNameAndDebug(Type, DebugSwitch)
Define the typeName and debug information.
Definition className.H:142
Namespace for OpenFOAM.
Bits that are independent of HashTable template parameters.
static constexpr int32_t maxTableSize
Maximum allowable internal table size (must be a power of two!).
static label canonicalSize(const label size) noexcept
Return a canonical (power-of-two) of the requested size.