ADS
Algorithms and Data Structures
Loading...
Searching...
No Matches
RowFingerprint.h File Reference

The fingerprints of the rows of doubles, beyond the book: a hash of 64 bits of a dense row \( ( x_0 , \dots , x_{n-1} ) \) or of a sparse one, its indices and values \( ( j_k , v_k ) \) in their order, for finding the rows already seen, e.g., a cut generated twice, by a table of the fingerprints; sameRow() then tells whether two rows with the same fingerprint are really equal. More...

#include "ADS/Contract.h"
#include "ADS/dictionary/FastHash.h"
#include <bit>
#include <cstddef>
#include <cstdint>
#include <cstring>
#include <span>
#include <type_traits>
#include "ADS/dictionary/impl/RowFingerprint.tpp"

Go to the source code of this file.

Namespaces

namespace  ADS

Functions

std::uint64_t ADS::canonicalWord (double x)
 the canonical word of \( x \): 0 for \( +0 \) and \( -0 \), one quiet NaN for every NaN, the bits of \( x \) otherwise
std::uint64_t ADS::rowFingerprint (std::span< const double > x, std::uint64_t seed=0)
 the fingerprint of the dense row \( x \) under the seed
template<class I>
std::uint64_t ADS::rowFingerprint (std::size_t n, const I *ind, const double *val, std::uint64_t seed=0)
 the fingerprint of the sparse row of the \( n \) entries \( ( \mathit{ind}[ k ] , \mathit{val}[ k ] ) \) under the seed: the values hashed as for a dense row, from the hash of the bytes of the indices
bool ADS::sameRow (std::span< const double > a, std::span< const double > b)
 true if the dense rows \( a \) and \( b \) have the same length and equal values, \( +0 \) equal to \( -0 \) and every NaN equal to every NaN
template<class I>
bool ADS::sameRow (std::size_t na, const I *ia, const double *va, std::size_t nb, const I *ib, const double *vb)
 true if the sparse rows of \( n_a \) and \( n_b \) entries have the same indices in the same order and equal values, in the sense above

Detailed description

The fingerprints of the rows of doubles, beyond the book: a hash of 64 bits of a dense row \( ( x_0 , \dots , x_{n-1} ) \) or of a sparse one, its indices and values \( ( j_k , v_k ) \) in their order, for finding the rows already seen, e.g., a cut generated twice, by a table of the fingerprints; sameRow() then tells whether two rows with the same fingerprint are really equal.

The values are compared as numbers, not as bit patterns: \( +0 \) and \( -0 \) are equal, and so are all the NaNs (whatever their sign and payload), which the comparison of the doubles does not do. A value is hence mapped to a canonical word before it is hashed: 0 for both zeros, the quiet NaN \( 7FF8 \ldots 0_{16} \) for every NaN, its own bits otherwise; two rows equal in this sense have the same words, hence the same fingerprint. The words are canonical a block of 64 at a time, by comparisons and blends that the compiler turns into vector operations, and each block of 512 bytes goes through hashBytes() [see FastHash.h], the hash of the previous block as its seed, so that a row costs about what its bytes cost to hash; a sparse row hashes its indices, as bytes, then its values.

Two rows with different words collide with probability about \( 2^{-64} \) for a hash that behaves at random, so that a table of \( k \) fingerprints holds a false match with probability about \( k^2 2^{-65} \) [the birthday bound]; but hashBytes() is not cryptographic, so a match is confirmed by sameRow(), in time linear in the row: first a comparison of the bytes of the two rows, which the library does by vectors and which settles the common case of two copies of the same row, then, if some byte differs, the comparison of the values as numbers. The fingerprint of a dense row differs from that of the same row as a sparse one, and the order of the indices of a sparse row counts.

The contracts [see Contract.h]: a sparse row has pointers that are not nil when it has entries. The functions are defined in RowFingerprint.tpp.

See also
M. O. Rabin, Fingerprinting by random polynomials, Technical Report TR-15-81, Center for Research in Computing Technology, Harvard University, 1981 (the identity of objects by short fingerprints)
IEEE Standard for Floating-Point Arithmetic, IEEE Std 754-2019, 2019, doi:10.1109/IEEESTD.2019.8766229 (the zeros and the NaNs)
Author
Donato Meoli
Dipartimento di Informatica
Università di Pisa