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.
This commit is contained in:
Mark Olesen
2017-06-14 12:54:38 +02:00
parent 6d649ddf1f
commit 710f23aa35
3 changed files with 10 additions and 11 deletions

View File

@ -92,14 +92,14 @@ int main()
std::cout<< "max = " << pTraits<label>::max << nl; std::cout<< "max = " << pTraits<label>::max << nl;
std::cout<< "umax = " << pTraits<uLabel>::max << nl; std::cout<< "umax = " << pTraits<uLabel>::max << nl;
std::cout<< "max_2 = " << pTraits<label>::max/2 << " == " std::cout<< "max_2 = " << pTraits<label>::max/2 << " <=> "
<< (1 << (sizeof(label)*8-2)) << nl; << (1L << (sizeof(label)*8-2)) << nl;
std::cout<< "max_4 = " << pTraits<label>::max/4 << " == " std::cout<< "max_4 = " << pTraits<label>::max/4 << " <=> "
<< (1 << (sizeof(label)*8-3)) << nl; << (1L << (sizeof(label)*8-3)) << nl;
std::cout<< "max_8 = " << pTraits<label>::max/8 << " == " std::cout<< "max_8 = " << pTraits<label>::max/8 << " <=> "
<< (1 << (sizeof(label)*8-4)) << nl; << (1L << (sizeof(label)*8-4)) << nl;
Info<< "End\n" << endl; Info<< "End\n" << endl;

View File

@ -33,9 +33,8 @@ namespace Foam
defineTypeNameAndDebug(HashTableCore, 0); defineTypeNameAndDebug(HashTableCore, 0);
} }
// Approximately labelMax/4 // Approximately labelMax/4
const Foam::label Foam::HashTableCore::maxTableSize(1 << (sizeof(label)*8-3)); const Foam::label Foam::HashTableCore::maxTableSize(1L << (sizeof(label)*8-3));
// * * * * * * * * * * * * * * Member Functions * * * * * * * * * * * * * * // // * * * * * * * * * * * * * * Member Functions * * * * * * * * * * * * * * //
@ -59,7 +58,7 @@ Foam::label Foam::HashTableCore::canonicalSize(const label requested_size)
// - The upper limit (approx. labelMax/4) must be a power of two, // - The upper limit (approx. labelMax/4) must be a power of two,
// need not be extremely large for hashing. // need not be extremely large for hashing.
uLabel powerOfTwo = 8; // lower-limit uLabel powerOfTwo = 8u; // lower-limit
const uLabel size = requested_size; const uLabel size = requested_size;
if (size <= powerOfTwo) if (size <= powerOfTwo)

View File

@ -35,7 +35,7 @@ defineTypeNameAndDebug(StaticHashTableCore, 0);
// Approximately labelMax/4 // Approximately labelMax/4
static const Foam::label maxTableSize(1 << (sizeof(Foam::label)*8-3)); static const Foam::label maxTableSize(1L << (sizeof(Foam::label)*8-3));
// * * * * * * * * * * * * * * Member Functions * * * * * * * * * * * * * * // // * * * * * * * * * * * * * * Member Functions * * * * * * * * * * * * * * //
@ -59,7 +59,7 @@ Foam::label Foam::StaticHashTableCore::canonicalSize(const label requested_size)
// - The upper limit (approx. labelMax/4) must be a power of two, // - The upper limit (approx. labelMax/4) must be a power of two,
// need not be extremely large for hashing. // need not be extremely large for hashing.
uLabel powerOfTwo = 8; // lower-limit uLabel powerOfTwo = 8u; // lower-limit
const uLabel size = requested_size; const uLabel size = requested_size;
if (size <= powerOfTwo) if (size <= powerOfTwo)