aboutsummaryrefslogtreecommitdiffstats
path: root/vespalib/src/vespa/vespalib/util/string_hash.cpp
blob: b3a6fc519955d773ddc66a96af4a85bedd33e700 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
// Copyright Yahoo. Licensed under the terms of the Apache 2.0 license. See LICENSE in the project root.

#include "string_hash.h"
#include <vespa/vespalib/stllike/hash_fun.h>

namespace vespalib {

double hash2d(const char *str, size_t len) {
    size_t h = hashValue(str, len);
    if ((h & 0x7ff0000000000000ul) == 0x7ff0000000000000ul) {
        // Avoid nan and inf
        h = h & 0xffeffffffffffffful;
    }
    double d = 0;
    static_assert(sizeof(d) == sizeof(h));
    memcpy(&d, &h, sizeof(d));
    return d;
}

double hash2d(vespalib::stringref str) {
    return hash2d(str.data(), str.size());
}

} // namespace vespalib