Last active
August 17, 2017 07:28
-
-
Save niXman/e12d185894e6c09d0d0df46eabebbf27 to your computer and use it in GitHub Desktop.
fnv1a
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include <iostream> | |
#include <cstdlib> | |
/***********************************************************************************/ | |
constexpr std::uint32_t fnv1a_r(const char *s, std::uint32_t h = 0x811c9dc5) { | |
return (*s == 0) ? h : fnv1a_r(s+1, ((h ^ (*s)) * 0x01000193)); | |
} | |
constexpr std::uint32_t fnv1a_i(const char *s) { | |
std::uint32_t seed = 0x811c9dc5; | |
for ( ; *s; ++s ) { | |
seed = ((seed ^ (*s)) * 0x01000193); | |
} | |
return seed; | |
} | |
/***********************************************************************************/ | |
int main(int argc, char **argv) { | |
constexpr char s0[] = "1234567890"; | |
enum { v0 = fnv1a_r(s0), v1 = fnv1a_i(s0) }; | |
std::cout << v0 << std::endl; | |
std::cout << v1 << std::endl; | |
std::cout << (v0 == v1 && v1 == fnv1a_i(argv[1])) << std::endl; | |
} | |
/***********************************************************************************/ |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment