Skip to content

Instantly share code, notes, and snippets.

@ruby0x1
Last active May 30, 2025 19:22
Show Gist options
  • Select an option

  • Save ruby0x1/81308642d0325fd386237cfa3b44785c to your computer and use it in GitHub Desktop.

Select an option

Save ruby0x1/81308642d0325fd386237cfa3b44785c to your computer and use it in GitHub Desktop.

Revisions

  1. ruby0x1 revised this gist Aug 10, 2023. 1 changed file with 2 additions and 2 deletions.
    4 changes: 2 additions & 2 deletions hash_fnv1a_constexpr.h
    Original file line number Diff line number Diff line change
    @@ -13,9 +13,9 @@
    constexpr uint64_t prime_64_const = 0x100000001b3;

    inline constexpr uint32_t hash_32_fnv1a_const(const char* const str, const uint32_t value = val_32_const) noexcept {
    return (str[0] == '\0') ? value : hash_32_fnv1a_const(&str[1], (value ^ uint32_t(str[0])) * prime_32_const);
    return (str[0] == '\0') ? value : hash_32_fnv1a_const(&str[1], (value ^ uint32_t((uint8_t)str[0])) * prime_32_const);
    }

    inline constexpr uint64_t hash_64_fnv1a_const(const char* const str, const uint64_t value = val_64_const) noexcept {
    return (str[0] == '\0') ? value : hash_64_fnv1a_const(&str[1], (value ^ uint64_t(str[0])) * prime_64_const);
    return (str[0] == '\0') ? value : hash_64_fnv1a_const(&str[1], (value ^ uint64_t((uint8_t)str[0])) * prime_64_const);
    }
  2. ruby0x1 revised this gist Oct 19, 2016. 2 changed files with 2 additions and 0 deletions.
    1 change: 1 addition & 0 deletions hash_fnv1a.h
    Original file line number Diff line number Diff line change
    @@ -4,6 +4,7 @@
    //fnv1a 32 and 64 bit hash functions
    // key is the data to hash, len is the size of the data (or how much of it to hash against)
    // code license: public domain or equivalent
    // post: https://notes.underscorediscovery.com/constexpr-fnv1a/

    inline const uint32_t hash_32_fnv1a(const void* key, const uint32_t len) {

    1 change: 1 addition & 0 deletions hash_fnv1a_constexpr.h
    Original file line number Diff line number Diff line change
    @@ -5,6 +5,7 @@
    // str should be a null terminated string literal, value should be left out
    // e.g hash_32_fnv1a_const("example")
    // code license: public domain or equivalent
    // post: https://notes.underscorediscovery.com/constexpr-fnv1a/

    constexpr uint32_t val_32_const = 0x811c9dc5;
    constexpr uint32_t prime_32_const = 0x1000193;
  3. ruby0x1 revised this gist Oct 19, 2016. 2 changed files with 2 additions and 0 deletions.
    1 change: 1 addition & 0 deletions hash_fnv1a.h
    Original file line number Diff line number Diff line change
    @@ -1,3 +1,4 @@
    #pragma once
    #include <stdint.h>

    //fnv1a 32 and 64 bit hash functions
    1 change: 1 addition & 0 deletions hash_fnv1a_constexpr.h
    Original file line number Diff line number Diff line change
    @@ -1,3 +1,4 @@
    #pragma once
    #include <stdint.h>

    // FNV1a c++11 constexpr compile time hash functions, 32 and 64 bit
  4. ruby0x1 revised this gist Oct 19, 2016. 2 changed files with 0 additions and 0 deletions.
    File renamed without changes.
    File renamed without changes.
  5. ruby0x1 renamed this gist Oct 19, 2016. 1 changed file with 0 additions and 0 deletions.
    File renamed without changes.
  6. ruby0x1 revised this gist Oct 19, 2016. 2 changed files with 2 additions and 2 deletions.
    2 changes: 1 addition & 1 deletion hash_const_fnv1a.h.cpp → hash_constexpr_fnv1a.h.cpp
    Original file line number Diff line number Diff line change
    @@ -5,7 +5,7 @@
    // e.g hash_32_fnv1a_const("example")
    // code license: public domain or equivalent

    constexpr uint32_t val_32_const = 0x811C9DC5;
    constexpr uint32_t val_32_const = 0x811c9dc5;
    constexpr uint32_t prime_32_const = 0x1000193;
    constexpr uint64_t val_64_const = 0xcbf29ce484222325;
    constexpr uint64_t prime_64_const = 0x100000001b3;
    2 changes: 1 addition & 1 deletion hash_fnv1a.h.cpp
    Original file line number Diff line number Diff line change
    @@ -7,7 +7,7 @@
    inline const uint32_t hash_32_fnv1a(const void* key, const uint32_t len) {

    const char* data = (char*)key;
    uint32_t hash = 0x811C9DC5;
    uint32_t hash = 0x811c9dc5;
    uint32_t prime = 0x1000193;

    for(int i = 0; i < len; ++i) {
  7. ruby0x1 revised this gist Oct 19, 2016. 2 changed files with 4 additions and 4 deletions.
    4 changes: 2 additions & 2 deletions hash_const_fnv1a.h.cpp
    Original file line number Diff line number Diff line change
    @@ -5,8 +5,8 @@
    // e.g hash_32_fnv1a_const("example")
    // code license: public domain or equivalent

    constexpr uint32_t val_32_const = 2166136261;
    constexpr uint32_t prime_32_const = 16777619;
    constexpr uint32_t val_32_const = 0x811C9DC5;
    constexpr uint32_t prime_32_const = 0x1000193;
    constexpr uint64_t val_64_const = 0xcbf29ce484222325;
    constexpr uint64_t prime_64_const = 0x100000001b3;

    4 changes: 2 additions & 2 deletions hash_fnv1a.h.cpp
    Original file line number Diff line number Diff line change
    @@ -7,8 +7,8 @@
    inline const uint32_t hash_32_fnv1a(const void* key, const uint32_t len) {

    const char* data = (char*)key;
    uint32_t hash = 2166136261;
    uint32_t prime = 16777619;
    uint32_t hash = 0x811C9DC5;
    uint32_t prime = 0x1000193;

    for(int i = 0; i < len; ++i) {
    uint8_t value = data[i];
  8. ruby0x1 revised this gist Oct 19, 2016. 2 changed files with 2 additions and 2 deletions.
    2 changes: 1 addition & 1 deletion hash_const_fnv1a.h.cpp
    Original file line number Diff line number Diff line change
    @@ -3,7 +3,7 @@
    // FNV1a c++11 constexpr compile time hash functions, 32 and 64 bit
    // str should be a null terminated string literal, value should be left out
    // e.g hash_32_fnv1a_const("example")
    // license: public domain or equivalent
    // code license: public domain or equivalent

    constexpr uint32_t val_32_const = 2166136261;
    constexpr uint32_t prime_32_const = 16777619;
    2 changes: 1 addition & 1 deletion hash_fnv1a.h.cpp
    Original file line number Diff line number Diff line change
    @@ -2,7 +2,7 @@

    //fnv1a 32 and 64 bit hash functions
    // key is the data to hash, len is the size of the data (or how much of it to hash against)
    // license: public domain or equivalent
    // code license: public domain or equivalent

    inline const uint32_t hash_32_fnv1a(const void* key, const uint32_t len) {

  9. ruby0x1 revised this gist Oct 19, 2016. 2 changed files with 5 additions and 0 deletions.
    1 change: 1 addition & 0 deletions hash_const_fnv1a.h.cpp
    Original file line number Diff line number Diff line change
    @@ -3,6 +3,7 @@
    // FNV1a c++11 constexpr compile time hash functions, 32 and 64 bit
    // str should be a null terminated string literal, value should be left out
    // e.g hash_32_fnv1a_const("example")
    // license: public domain or equivalent

    constexpr uint32_t val_32_const = 2166136261;
    constexpr uint32_t prime_32_const = 16777619;
    4 changes: 4 additions & 0 deletions hash_fnv1a.h.cpp
    Original file line number Diff line number Diff line change
    @@ -1,5 +1,9 @@
    #include <stdint.h>

    //fnv1a 32 and 64 bit hash functions
    // key is the data to hash, len is the size of the data (or how much of it to hash against)
    // license: public domain or equivalent

    inline const uint32_t hash_32_fnv1a(const void* key, const uint32_t len) {

    const char* data = (char*)key;
  10. ruby0x1 created this gist Oct 19, 2016.
    18 changes: 18 additions & 0 deletions hash_const_fnv1a.h.cpp
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,18 @@
    #include <stdint.h>

    // FNV1a c++11 constexpr compile time hash functions, 32 and 64 bit
    // str should be a null terminated string literal, value should be left out
    // e.g hash_32_fnv1a_const("example")

    constexpr uint32_t val_32_const = 2166136261;
    constexpr uint32_t prime_32_const = 16777619;
    constexpr uint64_t val_64_const = 0xcbf29ce484222325;
    constexpr uint64_t prime_64_const = 0x100000001b3;

    inline constexpr uint32_t hash_32_fnv1a_const(const char* const str, const uint32_t value = val_32_const) noexcept {
    return (str[0] == '\0') ? value : hash_32_fnv1a_const(&str[1], (value ^ uint32_t(str[0])) * prime_32_const);
    }

    inline constexpr uint64_t hash_64_fnv1a_const(const char* const str, const uint64_t value = val_64_const) noexcept {
    return (str[0] == '\0') ? value : hash_64_fnv1a_const(&str[1], (value ^ uint64_t(str[0])) * prime_64_const);
    }
    33 changes: 33 additions & 0 deletions hash_fnv1a.h.cpp
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,33 @@
    #include <stdint.h>

    inline const uint32_t hash_32_fnv1a(const void* key, const uint32_t len) {

    const char* data = (char*)key;
    uint32_t hash = 2166136261;
    uint32_t prime = 16777619;

    for(int i = 0; i < len; ++i) {
    uint8_t value = data[i];
    hash = hash ^ value;
    hash *= prime;
    }

    return hash;

    } //hash_32_fnv1a

    inline const uint64_t hash_64_fnv1a(const void* key, const uint64_t len) {

    const char* data = (char*)key;
    uint64_t hash = 0xcbf29ce484222325;
    uint64_t prime = 0x100000001b3;

    for(int i = 0; i < len; ++i) {
    uint8_t value = data[i];
    hash = hash ^ value;
    hash *= prime;
    }

    return hash;

    } //hash_64_fnv1a