rapidhash 0.2.0.0 → 1.0.0.0
raw patch · 9 files changed
+571/−557 lines, 9 filesdep −aesonPVP ok
version bump matches the API change (PVP)
Dependencies removed: aeson
API changes (from Hackage documentation)
- Data.Hash.RapidHash.Aeson: instance Data.Aeson.Types.FromJSON.FromJSON Data.Hash.RapidHash.Types.RapidHash
- Data.Hash.RapidHash.Aeson: instance Data.Aeson.Types.FromJSON.FromJSON Data.Hash.RapidHash.Types.RapidHashMicro
- Data.Hash.RapidHash.Aeson: instance Data.Aeson.Types.FromJSON.FromJSONKey Data.Hash.RapidHash.Types.RapidHash
- Data.Hash.RapidHash.Aeson: instance Data.Aeson.Types.FromJSON.FromJSONKey Data.Hash.RapidHash.Types.RapidHashMicro
- Data.Hash.RapidHash.Aeson: instance Data.Aeson.Types.ToJSON.ToJSON Data.Hash.RapidHash.Types.RapidHash
- Data.Hash.RapidHash.Aeson: instance Data.Aeson.Types.ToJSON.ToJSON Data.Hash.RapidHash.Types.RapidHashMicro
- Data.Hash.RapidHash.Aeson: instance Data.Aeson.Types.ToJSON.ToJSONKey Data.Hash.RapidHash.Types.RapidHash
- Data.Hash.RapidHash.Aeson: instance Data.Aeson.Types.ToJSON.ToJSONKey Data.Hash.RapidHash.Types.RapidHashMicro
Files
- CHANGELOG.md +10/−0
- README.md +2/−18
- cbits/rapidhash.h +504/−396
- rapidhash.cabal +3/−28
- src/Data/Hash/RapidHash.hs +2/−10
- src/Data/Hash/RapidHash/Aeson.hs +0/−57
- src/Data/Hash/RapidHash/FFI.hs +21/−21
- src/Data/Hash/RapidHash/Types.hs +17/−17
- test/pin/pin.c +12/−10
CHANGELOG.md view
@@ -2,6 +2,16 @@ ## Unreleased +## 1.0.0.0 - 2026-11-22++### Changed++- **Breaking**: This module no longer provides Aeson instances. They have been+ moved to `rapidhash-aeson`, which provides them as orphans. This should+ improve composeability with downstream libraries.+- Changed some Haddock references, which should fix a few broken links in the+ docs on Hackage.+ ## 0.2.0.0 - 2026-09-12 ### Added
README.md view
@@ -83,24 +83,8 @@ ### Aeson -Efficient `Aeson.ToJSON`/`FromJSON` and `Aeson.ToJSONKey`/`FromJSONKey`-instances are provided behind flag `aeson`; map keys use the same tagged-hex form as values. To use these, add:--cabal.project:--```cabal-package rapidhash- flags: +aeson-```--stack.yaml:--```yaml-flags:- rapidhash:- aeson: true-```+`ToJSON`/`FromJSON` and `ToJSONKey`/`FromJSONKey` instances are provided by+[`rapidhash-aeson`](https://hackage.haskell.org/package/rapidhash-aeson). ## Supported input types
cbits/rapidhash.h view
@@ -1,11 +1,13 @@ /* * NOTE: This file is pulled from rapidhash v3. - * permalink: https://raw.githubusercontent.com/Nicoshev/rapidhash/5c62c4b3352c7d4c87ed3429d2329ddaf9b74df9/rapidhash.h + * permalink: + * https://raw.githubusercontent.com/Nicoshev/rapidhash/5c62c4b3352c7d4c87ed3429d2329ddaf9b74df9/rapidhash.h * - * rapidhash V3 - Very fast, high quality, platform-independent hashing algorithm. + * rapidhash V3 - Very fast, high quality, platform-independent hashing + * algorithm. * * Based on 'wyhash', by Wang Yi <godspeed_china@yeah.net> - * + * * Copyright (C) 2025 Nicolas De Carli * * Permission is hereby granted, free of charge, to any person obtaining a copy @@ -15,8 +17,8 @@ * copies of the Software, and to permit persons to whom the Software is * furnished to do so, subject to the following conditions: * - * The above copyright notice and this permission notice shall be included in all - * copies or substantial portions of the Software. + * The above copyright notice and this permission notice shall be included in + * all copies or substantial portions of the Software. * * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, @@ -30,239 +32,283 @@ * - rapidhash source repository: https://github.com/Nicoshev/rapidhash */ - #pragma once - +#pragma once + /* * Includes. */ - #include <stdint.h> - #include <string.h> - #if defined(_MSC_VER) - # include <intrin.h> - # if defined(_M_X64) && !defined(_M_ARM64EC) - # pragma intrinsic(_umul128) - # endif - #endif - - /* - * C/C++ macros. - */ - - #ifdef _MSC_VER - # define RAPIDHASH_ALWAYS_INLINE __forceinline - #elif defined(__GNUC__) - # define RAPIDHASH_ALWAYS_INLINE inline __attribute__((__always_inline__)) - #else - # define RAPIDHASH_ALWAYS_INLINE inline - #endif - - #ifdef __cplusplus - # define RAPIDHASH_NOEXCEPT noexcept - # define RAPIDHASH_CONSTEXPR constexpr - # ifndef RAPIDHASH_INLINE - # define RAPIDHASH_INLINE RAPIDHASH_ALWAYS_INLINE - # endif - # if __cplusplus >= 201402L && !defined(_MSC_VER) - # define RAPIDHASH_INLINE_CONSTEXPR RAPIDHASH_ALWAYS_INLINE constexpr - # else - # define RAPIDHASH_INLINE_CONSTEXPR RAPIDHASH_ALWAYS_INLINE - # endif - #else - # define RAPIDHASH_NOEXCEPT - # define RAPIDHASH_CONSTEXPR static const - # ifndef RAPIDHASH_INLINE - # define RAPIDHASH_INLINE static RAPIDHASH_ALWAYS_INLINE - # endif - # define RAPIDHASH_INLINE_CONSTEXPR RAPIDHASH_INLINE - #endif +#include <stdint.h> +#include <string.h> +#if defined(_MSC_VER) +#include <intrin.h> +#if defined(_M_X64) && !defined(_M_ARM64EC) +#pragma intrinsic(_umul128) +#endif +#endif - /* - * Unrolled macro. - * Improves large input speed, but increases code size and worsens small input speed. - * - * RAPIDHASH_COMPACT: Normal behavior. - * RAPIDHASH_UNROLLED: - * - */ - #ifndef RAPIDHASH_UNROLLED - # define RAPIDHASH_COMPACT - #elif defined(RAPIDHASH_COMPACT) - # error "cannot define RAPIDHASH_COMPACT and RAPIDHASH_UNROLLED simultaneously." - #endif - - /* - * Protection macro, alters behaviour of rapid_mum multiplication function. - * - * RAPIDHASH_FAST: Normal behavior, max speed. - * RAPIDHASH_PROTECTED: Extra protection against entropy loss. - */ - #ifndef RAPIDHASH_PROTECTED - # define RAPIDHASH_FAST - #elif defined(RAPIDHASH_FAST) - # error "cannot define RAPIDHASH_PROTECTED and RAPIDHASH_FAST simultaneously." - #endif - - /* - * Likely and unlikely macros. - */ - #if defined(__GNUC__) || defined(__INTEL_COMPILER) || defined(__clang__) - # define _likely_(x) __builtin_expect(x,1) - # define _unlikely_(x) __builtin_expect(x,0) - #else - # define _likely_(x) (x) - # define _unlikely_(x) (x) - #endif - - /* - * Endianness macros. - */ - #ifndef RAPIDHASH_LITTLE_ENDIAN - # if defined(_WIN32) || defined(__LITTLE_ENDIAN__) || (defined(__BYTE_ORDER__) && __BYTE_ORDER__ == __ORDER_LITTLE_ENDIAN__) - # define RAPIDHASH_LITTLE_ENDIAN - # elif defined(__BIG_ENDIAN__) || (defined(__BYTE_ORDER__) && __BYTE_ORDER__ == __ORDER_BIG_ENDIAN__) - # define RAPIDHASH_BIG_ENDIAN - # else - # warning "could not determine endianness! Falling back to little endian." - # define RAPIDHASH_LITTLE_ENDIAN - # endif - #endif - - /* - * Default secret parameters. - */ - RAPIDHASH_CONSTEXPR uint64_t rapid_secret[8] = { - 0x2d358dccaa6c78a5ull, - 0x8bb84b93962eacc9ull, - 0x4b33a62ed433d4a3ull, - 0x4d5a2da51de1aa47ull, - 0xa0761d6478bd642full, - 0xe7037ed1a0b428dbull, - 0x90ed1765281c388cull, - 0xaaaaaaaaaaaaaaaaull}; - - /* - * 64*64 -> 128bit multiply function. - * - * @param A Address of 64-bit number. - * @param B Address of 64-bit number. - * - * Calculates 128-bit C = *A * *B. - * - * When RAPIDHASH_FAST is defined: - * Overwrites A contents with C's low 64 bits. - * Overwrites B contents with C's high 64 bits. - * - * When RAPIDHASH_PROTECTED is defined: - * Xors and overwrites A contents with C's low 64 bits. - * Xors and overwrites B contents with C's high 64 bits. - */ - RAPIDHASH_INLINE_CONSTEXPR void rapid_mum(uint64_t *A, uint64_t *B) RAPIDHASH_NOEXCEPT { - #if defined(__SIZEOF_INT128__) - __uint128_t r=*A; r*=*B; - #ifdef RAPIDHASH_PROTECTED - *A^=(uint64_t)r; *B^=(uint64_t)(r>>64); - #else - *A=(uint64_t)r; *B=(uint64_t)(r>>64); - #endif - #elif defined(_MSC_VER) && (defined(_WIN64) || defined(_M_HYBRID_CHPE_ARM64)) - #if defined(_M_X64) - #ifdef RAPIDHASH_PROTECTED - uint64_t a, b; - a=_umul128(*A,*B,&b); - *A^=a; *B^=b; - #else - *A=_umul128(*A,*B,B); - #endif - #else - #ifdef RAPIDHASH_PROTECTED - uint64_t a, b; - b = __umulh(*A, *B); - a = *A * *B; - *A^=a; *B^=b; - #else - uint64_t c = __umulh(*A, *B); - *A = *A * *B; - *B = c; - #endif - #endif - #else - uint64_t ha=*A>>32, hb=*B>>32, la=(uint32_t)*A, lb=(uint32_t)*B; - uint64_t rh=ha*hb, rm0=ha*lb, rm1=hb*la, rl=la*lb, t=rl+(rm0<<32), c=t<rl; - uint64_t lo=t+(rm1<<32); - c+=lo<t; - uint64_t hi=rh+(rm0>>32)+(rm1>>32)+c; - #ifdef RAPIDHASH_PROTECTED - *A^=lo; *B^=hi; - #else - *A=lo; *B=hi; - #endif - #endif - } - - /* - * Multiply and xor mix function. - * - * @param A 64-bit number. - * @param B 64-bit number. - * - * Calculates 128-bit C = A * B. - * Returns 64-bit xor between high and low 64 bits of C. - */ - RAPIDHASH_INLINE_CONSTEXPR uint64_t rapid_mix(uint64_t A, uint64_t B) RAPIDHASH_NOEXCEPT { rapid_mum(&A,&B); return A^B; } - - /* - * Read functions. - */ - #ifdef RAPIDHASH_LITTLE_ENDIAN - RAPIDHASH_INLINE uint64_t rapid_read64(const uint8_t *p) RAPIDHASH_NOEXCEPT { uint64_t v; memcpy(&v, p, sizeof(uint64_t)); return v;} - RAPIDHASH_INLINE uint64_t rapid_read32(const uint8_t *p) RAPIDHASH_NOEXCEPT { uint32_t v; memcpy(&v, p, sizeof(uint32_t)); return v;} - #elif defined(__GNUC__) || defined(__INTEL_COMPILER) || defined(__clang__) - RAPIDHASH_INLINE uint64_t rapid_read64(const uint8_t *p) RAPIDHASH_NOEXCEPT { uint64_t v; memcpy(&v, p, sizeof(uint64_t)); return __builtin_bswap64(v);} - RAPIDHASH_INLINE uint64_t rapid_read32(const uint8_t *p) RAPIDHASH_NOEXCEPT { uint32_t v; memcpy(&v, p, sizeof(uint32_t)); return __builtin_bswap32(v);} - #elif defined(_MSC_VER) - RAPIDHASH_INLINE uint64_t rapid_read64(const uint8_t *p) RAPIDHASH_NOEXCEPT { uint64_t v; memcpy(&v, p, sizeof(uint64_t)); return _byteswap_uint64(v);} - RAPIDHASH_INLINE uint64_t rapid_read32(const uint8_t *p) RAPIDHASH_NOEXCEPT { uint32_t v; memcpy(&v, p, sizeof(uint32_t)); return _byteswap_ulong(v);} - #else - RAPIDHASH_INLINE uint64_t rapid_read64(const uint8_t *p) RAPIDHASH_NOEXCEPT { - uint64_t v; memcpy(&v, p, 8); - return (((v >> 56) & 0xff)| ((v >> 40) & 0xff00)| ((v >> 24) & 0xff0000)| ((v >> 8) & 0xff000000)| ((v << 8) & 0xff00000000)| ((v << 24) & 0xff0000000000)| ((v << 40) & 0xff000000000000)| ((v << 56) & 0xff00000000000000)); - } - RAPIDHASH_INLINE uint64_t rapid_read32(const uint8_t *p) RAPIDHASH_NOEXCEPT { - uint32_t v; memcpy(&v, p, 4); - return (((v >> 24) & 0xff)| ((v >> 8) & 0xff00)| ((v << 8) & 0xff0000)| ((v << 24) & 0xff000000)); - } - #endif - - /* - * rapidhash main function. - * - * @param key Buffer to be hashed. - * @param len @key length, in bytes. - * @param seed 64-bit seed used to alter the hash result predictably. - * @param secret Triplet of 64-bit secrets used to alter hash result predictably. - * - * Returns a 64-bit hash. - */ -RAPIDHASH_INLINE_CONSTEXPR uint64_t rapidhash_internal(const void *key, size_t len, uint64_t seed, const uint64_t* secret) RAPIDHASH_NOEXCEPT { - const uint8_t *p=(const uint8_t *)key; +/* + * C/C++ macros. + */ + +#ifdef _MSC_VER +#define RAPIDHASH_ALWAYS_INLINE __forceinline +#elif defined(__GNUC__) +#define RAPIDHASH_ALWAYS_INLINE inline __attribute__((__always_inline__)) +#else +#define RAPIDHASH_ALWAYS_INLINE inline +#endif + +#ifdef __cplusplus +#define RAPIDHASH_NOEXCEPT noexcept +#define RAPIDHASH_CONSTEXPR constexpr +#ifndef RAPIDHASH_INLINE +#define RAPIDHASH_INLINE RAPIDHASH_ALWAYS_INLINE +#endif +#if __cplusplus >= 201402L && !defined(_MSC_VER) +#define RAPIDHASH_INLINE_CONSTEXPR RAPIDHASH_ALWAYS_INLINE constexpr +#else +#define RAPIDHASH_INLINE_CONSTEXPR RAPIDHASH_ALWAYS_INLINE +#endif +#else +#define RAPIDHASH_NOEXCEPT +#define RAPIDHASH_CONSTEXPR static const +#ifndef RAPIDHASH_INLINE +#define RAPIDHASH_INLINE static RAPIDHASH_ALWAYS_INLINE +#endif +#define RAPIDHASH_INLINE_CONSTEXPR RAPIDHASH_INLINE +#endif + +/* + * Unrolled macro. + * Improves large input speed, but increases code size and worsens small input + * speed. + * + * RAPIDHASH_COMPACT: Normal behavior. + * RAPIDHASH_UNROLLED: + * + */ +#ifndef RAPIDHASH_UNROLLED +#define RAPIDHASH_COMPACT +#elif defined(RAPIDHASH_COMPACT) +#error "cannot define RAPIDHASH_COMPACT and RAPIDHASH_UNROLLED simultaneously." +#endif + +/* + * Protection macro, alters behaviour of rapid_mum multiplication function. + * + * RAPIDHASH_FAST: Normal behavior, max speed. + * RAPIDHASH_PROTECTED: Extra protection against entropy loss. + */ +#ifndef RAPIDHASH_PROTECTED +#define RAPIDHASH_FAST +#elif defined(RAPIDHASH_FAST) +#error "cannot define RAPIDHASH_PROTECTED and RAPIDHASH_FAST simultaneously." +#endif + +/* + * Likely and unlikely macros. + */ +#if defined(__GNUC__) || defined(__INTEL_COMPILER) || defined(__clang__) +#define _likely_(x) __builtin_expect(x, 1) +#define _unlikely_(x) __builtin_expect(x, 0) +#else +#define _likely_(x) (x) +#define _unlikely_(x) (x) +#endif + +/* + * Endianness macros. + */ +#ifndef RAPIDHASH_LITTLE_ENDIAN +#if defined(_WIN32) || defined(__LITTLE_ENDIAN__) || \ + (defined(__BYTE_ORDER__) && __BYTE_ORDER__ == __ORDER_LITTLE_ENDIAN__) +#define RAPIDHASH_LITTLE_ENDIAN +#elif defined(__BIG_ENDIAN__) || \ + (defined(__BYTE_ORDER__) && __BYTE_ORDER__ == __ORDER_BIG_ENDIAN__) +#define RAPIDHASH_BIG_ENDIAN +#else +#warning "could not determine endianness! Falling back to little endian." +#define RAPIDHASH_LITTLE_ENDIAN +#endif +#endif + +/* + * Default secret parameters. + */ +RAPIDHASH_CONSTEXPR uint64_t rapid_secret[8] = { + 0x2d358dccaa6c78a5ull, 0x8bb84b93962eacc9ull, 0x4b33a62ed433d4a3ull, + 0x4d5a2da51de1aa47ull, 0xa0761d6478bd642full, 0xe7037ed1a0b428dbull, + 0x90ed1765281c388cull, 0xaaaaaaaaaaaaaaaaull}; + +/* + * 64*64 -> 128bit multiply function. + * + * @param A Address of 64-bit number. + * @param B Address of 64-bit number. + * + * Calculates 128-bit C = *A * *B. + * + * When RAPIDHASH_FAST is defined: + * Overwrites A contents with C's low 64 bits. + * Overwrites B contents with C's high 64 bits. + * + * When RAPIDHASH_PROTECTED is defined: + * Xors and overwrites A contents with C's low 64 bits. + * Xors and overwrites B contents with C's high 64 bits. + */ +RAPIDHASH_INLINE_CONSTEXPR void rapid_mum(uint64_t *A, + uint64_t *B) RAPIDHASH_NOEXCEPT { +#if defined(__SIZEOF_INT128__) + __uint128_t r = *A; + r *= *B; +#ifdef RAPIDHASH_PROTECTED + *A ^= (uint64_t)r; + *B ^= (uint64_t)(r >> 64); +#else + *A = (uint64_t)r; + *B = (uint64_t)(r >> 64); +#endif +#elif defined(_MSC_VER) && (defined(_WIN64) || defined(_M_HYBRID_CHPE_ARM64)) +#if defined(_M_X64) +#ifdef RAPIDHASH_PROTECTED + uint64_t a, b; + a = _umul128(*A, *B, &b); + *A ^= a; + *B ^= b; +#else + *A = _umul128(*A, *B, B); +#endif +#else +#ifdef RAPIDHASH_PROTECTED + uint64_t a, b; + b = __umulh(*A, *B); + a = *A * *B; + *A ^= a; + *B ^= b; +#else + uint64_t c = __umulh(*A, *B); + *A = *A * *B; + *B = c; +#endif +#endif +#else + uint64_t ha = *A >> 32, hb = *B >> 32, la = (uint32_t)*A, lb = (uint32_t)*B; + uint64_t rh = ha * hb, rm0 = ha * lb, rm1 = hb * la, rl = la * lb, + t = rl + (rm0 << 32), c = t < rl; + uint64_t lo = t + (rm1 << 32); + c += lo < t; + uint64_t hi = rh + (rm0 >> 32) + (rm1 >> 32) + c; +#ifdef RAPIDHASH_PROTECTED + *A ^= lo; + *B ^= hi; +#else + *A = lo; + *B = hi; +#endif +#endif +} + +/* + * Multiply and xor mix function. + * + * @param A 64-bit number. + * @param B 64-bit number. + * + * Calculates 128-bit C = A * B. + * Returns 64-bit xor between high and low 64 bits of C. + */ +RAPIDHASH_INLINE_CONSTEXPR uint64_t rapid_mix(uint64_t A, + uint64_t B) RAPIDHASH_NOEXCEPT { + rapid_mum(&A, &B); + return A ^ B; +} + +/* + * Read functions. + */ +#ifdef RAPIDHASH_LITTLE_ENDIAN +RAPIDHASH_INLINE uint64_t rapid_read64(const uint8_t *p) RAPIDHASH_NOEXCEPT { + uint64_t v; + memcpy(&v, p, sizeof(uint64_t)); + return v; +} +RAPIDHASH_INLINE uint64_t rapid_read32(const uint8_t *p) RAPIDHASH_NOEXCEPT { + uint32_t v; + memcpy(&v, p, sizeof(uint32_t)); + return v; +} +#elif defined(__GNUC__) || defined(__INTEL_COMPILER) || defined(__clang__) +RAPIDHASH_INLINE uint64_t rapid_read64(const uint8_t *p) RAPIDHASH_NOEXCEPT { + uint64_t v; + memcpy(&v, p, sizeof(uint64_t)); + return __builtin_bswap64(v); +} +RAPIDHASH_INLINE uint64_t rapid_read32(const uint8_t *p) RAPIDHASH_NOEXCEPT { + uint32_t v; + memcpy(&v, p, sizeof(uint32_t)); + return __builtin_bswap32(v); +} +#elif defined(_MSC_VER) +RAPIDHASH_INLINE uint64_t rapid_read64(const uint8_t *p) RAPIDHASH_NOEXCEPT { + uint64_t v; + memcpy(&v, p, sizeof(uint64_t)); + return _byteswap_uint64(v); +} +RAPIDHASH_INLINE uint64_t rapid_read32(const uint8_t *p) RAPIDHASH_NOEXCEPT { + uint32_t v; + memcpy(&v, p, sizeof(uint32_t)); + return _byteswap_ulong(v); +} +#else +RAPIDHASH_INLINE uint64_t rapid_read64(const uint8_t *p) RAPIDHASH_NOEXCEPT { + uint64_t v; + memcpy(&v, p, 8); + return (((v >> 56) & 0xff) | ((v >> 40) & 0xff00) | ((v >> 24) & 0xff0000) | + ((v >> 8) & 0xff000000) | ((v << 8) & 0xff00000000) | + ((v << 24) & 0xff0000000000) | ((v << 40) & 0xff000000000000) | + ((v << 56) & 0xff00000000000000)); +} +RAPIDHASH_INLINE uint64_t rapid_read32(const uint8_t *p) RAPIDHASH_NOEXCEPT { + uint32_t v; + memcpy(&v, p, 4); + return (((v >> 24) & 0xff) | ((v >> 8) & 0xff00) | ((v << 8) & 0xff0000) | + ((v << 24) & 0xff000000)); +} +#endif + +/* + * rapidhash main function. + * + * @param key Buffer to be hashed. + * @param len @key length, in bytes. + * @param seed 64-bit seed used to alter the hash result predictably. + * @param secret Triplet of 64-bit secrets used to alter hash result + * predictably. + * + * Returns a 64-bit hash. + */ +RAPIDHASH_INLINE_CONSTEXPR uint64_t +rapidhash_internal(const void *key, size_t len, uint64_t seed, + const uint64_t *secret) RAPIDHASH_NOEXCEPT { + const uint8_t *p = (const uint8_t *)key; seed ^= rapid_mix(seed ^ secret[2], secret[1]); - uint64_t a=0, b=0; + uint64_t a = 0, b = 0; size_t i = len; if (_likely_(len <= 16)) { if (len >= 4) { seed ^= len; if (len >= 8) { - const uint8_t* plast = p + len - 8; + const uint8_t *plast = p + len - 8; a = rapid_read64(p); b = rapid_read64(plast); } else { - const uint8_t* plast = p + len - 4; + const uint8_t *plast = p + len - 4; a = rapid_read32(p); b = rapid_read32(plast); } } else if (len > 0) { - a = (((uint64_t)p[0])<<45)|p[len-1]; - b = p[len>>1]; + a = (((uint64_t)p[0]) << 45) | p[len - 1]; + b = p[len >> 1]; } else a = b = 0; } else { @@ -272,43 +318,71 @@ uint64_t see5 = seed, see6 = seed; #ifdef RAPIDHASH_COMPACT do { - seed = rapid_mix(rapid_read64(p) ^ secret[0], rapid_read64(p + 8) ^ seed); - see1 = rapid_mix(rapid_read64(p + 16) ^ secret[1], rapid_read64(p + 24) ^ see1); - see2 = rapid_mix(rapid_read64(p + 32) ^ secret[2], rapid_read64(p + 40) ^ see2); - see3 = rapid_mix(rapid_read64(p + 48) ^ secret[3], rapid_read64(p + 56) ^ see3); - see4 = rapid_mix(rapid_read64(p + 64) ^ secret[4], rapid_read64(p + 72) ^ see4); - see5 = rapid_mix(rapid_read64(p + 80) ^ secret[5], rapid_read64(p + 88) ^ see5); - see6 = rapid_mix(rapid_read64(p + 96) ^ secret[6], rapid_read64(p + 104) ^ see6); + seed = + rapid_mix(rapid_read64(p) ^ secret[0], rapid_read64(p + 8) ^ seed); + see1 = rapid_mix(rapid_read64(p + 16) ^ secret[1], + rapid_read64(p + 24) ^ see1); + see2 = rapid_mix(rapid_read64(p + 32) ^ secret[2], + rapid_read64(p + 40) ^ see2); + see3 = rapid_mix(rapid_read64(p + 48) ^ secret[3], + rapid_read64(p + 56) ^ see3); + see4 = rapid_mix(rapid_read64(p + 64) ^ secret[4], + rapid_read64(p + 72) ^ see4); + see5 = rapid_mix(rapid_read64(p + 80) ^ secret[5], + rapid_read64(p + 88) ^ see5); + see6 = rapid_mix(rapid_read64(p + 96) ^ secret[6], + rapid_read64(p + 104) ^ see6); p += 112; i -= 112; - } while(i > 112); + } while (i > 112); #else while (i > 224) { - seed = rapid_mix(rapid_read64(p) ^ secret[0], rapid_read64(p + 8) ^ seed); - see1 = rapid_mix(rapid_read64(p + 16) ^ secret[1], rapid_read64(p + 24) ^ see1); - see2 = rapid_mix(rapid_read64(p + 32) ^ secret[2], rapid_read64(p + 40) ^ see2); - see3 = rapid_mix(rapid_read64(p + 48) ^ secret[3], rapid_read64(p + 56) ^ see3); - see4 = rapid_mix(rapid_read64(p + 64) ^ secret[4], rapid_read64(p + 72) ^ see4); - see5 = rapid_mix(rapid_read64(p + 80) ^ secret[5], rapid_read64(p + 88) ^ see5); - see6 = rapid_mix(rapid_read64(p + 96) ^ secret[6], rapid_read64(p + 104) ^ see6); - seed = rapid_mix(rapid_read64(p + 112) ^ secret[0], rapid_read64(p + 120) ^ seed); - see1 = rapid_mix(rapid_read64(p + 128) ^ secret[1], rapid_read64(p + 136) ^ see1); - see2 = rapid_mix(rapid_read64(p + 144) ^ secret[2], rapid_read64(p + 152) ^ see2); - see3 = rapid_mix(rapid_read64(p + 160) ^ secret[3], rapid_read64(p + 168) ^ see3); - see4 = rapid_mix(rapid_read64(p + 176) ^ secret[4], rapid_read64(p + 184) ^ see4); - see5 = rapid_mix(rapid_read64(p + 192) ^ secret[5], rapid_read64(p + 200) ^ see5); - see6 = rapid_mix(rapid_read64(p + 208) ^ secret[6], rapid_read64(p + 216) ^ see6); + seed = + rapid_mix(rapid_read64(p) ^ secret[0], rapid_read64(p + 8) ^ seed); + see1 = rapid_mix(rapid_read64(p + 16) ^ secret[1], + rapid_read64(p + 24) ^ see1); + see2 = rapid_mix(rapid_read64(p + 32) ^ secret[2], + rapid_read64(p + 40) ^ see2); + see3 = rapid_mix(rapid_read64(p + 48) ^ secret[3], + rapid_read64(p + 56) ^ see3); + see4 = rapid_mix(rapid_read64(p + 64) ^ secret[4], + rapid_read64(p + 72) ^ see4); + see5 = rapid_mix(rapid_read64(p + 80) ^ secret[5], + rapid_read64(p + 88) ^ see5); + see6 = rapid_mix(rapid_read64(p + 96) ^ secret[6], + rapid_read64(p + 104) ^ see6); + seed = rapid_mix(rapid_read64(p + 112) ^ secret[0], + rapid_read64(p + 120) ^ seed); + see1 = rapid_mix(rapid_read64(p + 128) ^ secret[1], + rapid_read64(p + 136) ^ see1); + see2 = rapid_mix(rapid_read64(p + 144) ^ secret[2], + rapid_read64(p + 152) ^ see2); + see3 = rapid_mix(rapid_read64(p + 160) ^ secret[3], + rapid_read64(p + 168) ^ see3); + see4 = rapid_mix(rapid_read64(p + 176) ^ secret[4], + rapid_read64(p + 184) ^ see4); + see5 = rapid_mix(rapid_read64(p + 192) ^ secret[5], + rapid_read64(p + 200) ^ see5); + see6 = rapid_mix(rapid_read64(p + 208) ^ secret[6], + rapid_read64(p + 216) ^ see6); p += 224; i -= 224; } if (i > 112) { - seed = rapid_mix(rapid_read64(p) ^ secret[0], rapid_read64(p + 8) ^ seed); - see1 = rapid_mix(rapid_read64(p + 16) ^ secret[1], rapid_read64(p + 24) ^ see1); - see2 = rapid_mix(rapid_read64(p + 32) ^ secret[2], rapid_read64(p + 40) ^ see2); - see3 = rapid_mix(rapid_read64(p + 48) ^ secret[3], rapid_read64(p + 56) ^ see3); - see4 = rapid_mix(rapid_read64(p + 64) ^ secret[4], rapid_read64(p + 72) ^ see4); - see5 = rapid_mix(rapid_read64(p + 80) ^ secret[5], rapid_read64(p + 88) ^ see5); - see6 = rapid_mix(rapid_read64(p + 96) ^ secret[6], rapid_read64(p + 104) ^ see6); + seed = + rapid_mix(rapid_read64(p) ^ secret[0], rapid_read64(p + 8) ^ seed); + see1 = rapid_mix(rapid_read64(p + 16) ^ secret[1], + rapid_read64(p + 24) ^ see1); + see2 = rapid_mix(rapid_read64(p + 32) ^ secret[2], + rapid_read64(p + 40) ^ see2); + see3 = rapid_mix(rapid_read64(p + 48) ^ secret[3], + rapid_read64(p + 56) ^ see3); + see4 = rapid_mix(rapid_read64(p + 64) ^ secret[4], + rapid_read64(p + 72) ^ see4); + see5 = rapid_mix(rapid_read64(p + 80) ^ secret[5], + rapid_read64(p + 88) ^ see5); + see6 = rapid_mix(rapid_read64(p + 96) ^ secret[6], + rapid_read64(p + 104) ^ see6); p += 112; i -= 112; } @@ -323,22 +397,28 @@ if (i > 16) { seed = rapid_mix(rapid_read64(p) ^ secret[2], rapid_read64(p + 8) ^ seed); if (i > 32) { - seed = rapid_mix(rapid_read64(p + 16) ^ secret[2], rapid_read64(p + 24) ^ seed); - if (i > 48) { - seed = rapid_mix(rapid_read64(p + 32) ^ secret[1], rapid_read64(p + 40) ^ seed); - if (i > 64) { - seed = rapid_mix(rapid_read64(p + 48) ^ secret[1], rapid_read64(p + 56) ^ seed); - if (i > 80) { - seed = rapid_mix(rapid_read64(p + 64) ^ secret[2], rapid_read64(p + 72) ^ seed); - if (i > 96) { - seed = rapid_mix(rapid_read64(p + 80) ^ secret[1], rapid_read64(p + 88) ^ seed); - } - } + seed = rapid_mix(rapid_read64(p + 16) ^ secret[2], + rapid_read64(p + 24) ^ seed); + if (i > 48) { + seed = rapid_mix(rapid_read64(p + 32) ^ secret[1], + rapid_read64(p + 40) ^ seed); + if (i > 64) { + seed = rapid_mix(rapid_read64(p + 48) ^ secret[1], + rapid_read64(p + 56) ^ seed); + if (i > 80) { + seed = rapid_mix(rapid_read64(p + 64) ^ secret[2], + rapid_read64(p + 72) ^ seed); + if (i > 96) { + seed = rapid_mix(rapid_read64(p + 80) ^ secret[1], + rapid_read64(p + 88) ^ seed); } + } } + } } } - a=rapid_read64(p+i-16) ^ i; b=rapid_read64(p+i-8); + a = rapid_read64(p + i - 16) ^ i; + b = rapid_read64(p + i - 8); } a ^= secret[1]; b ^= seed; @@ -346,135 +426,155 @@ return rapid_mix(a ^ secret[7], b ^ secret[1] ^ i); } - /* - * rapidhashMicro main function. - * - * @param key Buffer to be hashed. - * @param len @key length, in bytes. - * @param seed 64-bit seed used to alter the hash result predictably. - * @param secret Triplet of 64-bit secrets used to alter hash result predictably. - * - * Returns a 64-bit hash. - */ - RAPIDHASH_INLINE_CONSTEXPR uint64_t rapidhashMicro_internal(const void *key, size_t len, uint64_t seed, const uint64_t* secret) RAPIDHASH_NOEXCEPT { - const uint8_t *p=(const uint8_t *)key; - seed ^= rapid_mix(seed ^ secret[2], secret[1]); - uint64_t a=0, b=0; - size_t i = len; - if (_likely_(len <= 16)) { - if (len >= 4) { - seed ^= len; - if (len >= 8) { - const uint8_t* plast = p + len - 8; - a = rapid_read64(p); - b = rapid_read64(plast); - } else { - const uint8_t* plast = p + len - 4; - a = rapid_read32(p); - b = rapid_read32(plast); - } - } else if (len > 0) { - a = (((uint64_t)p[0])<<45)|p[len-1]; - b = p[len>>1]; - } else - a = b = 0; - } else { - if (i > 80) { - uint64_t see1 = seed, see2 = seed; - uint64_t see3 = seed, see4 = seed; - do { - seed = rapid_mix(rapid_read64(p) ^ secret[0], rapid_read64(p + 8) ^ seed); - see1 = rapid_mix(rapid_read64(p + 16) ^ secret[1], rapid_read64(p + 24) ^ see1); - see2 = rapid_mix(rapid_read64(p + 32) ^ secret[2], rapid_read64(p + 40) ^ see2); - see3 = rapid_mix(rapid_read64(p + 48) ^ secret[3], rapid_read64(p + 56) ^ see3); - see4 = rapid_mix(rapid_read64(p + 64) ^ secret[4], rapid_read64(p + 72) ^ see4); - p += 80; - i -= 80; - } while(i > 80); - seed ^= see1; - see2 ^= see3; - seed ^= see4; - seed ^= see2; +/* + * rapidhashMicro main function. + * + * @param key Buffer to be hashed. + * @param len @key length, in bytes. + * @param seed 64-bit seed used to alter the hash result predictably. + * @param secret Triplet of 64-bit secrets used to alter hash result + * predictably. + * + * Returns a 64-bit hash. + */ +RAPIDHASH_INLINE_CONSTEXPR uint64_t +rapidhashMicro_internal(const void *key, size_t len, uint64_t seed, + const uint64_t *secret) RAPIDHASH_NOEXCEPT { + const uint8_t *p = (const uint8_t *)key; + seed ^= rapid_mix(seed ^ secret[2], secret[1]); + uint64_t a = 0, b = 0; + size_t i = len; + if (_likely_(len <= 16)) { + if (len >= 4) { + seed ^= len; + if (len >= 8) { + const uint8_t *plast = p + len - 8; + a = rapid_read64(p); + b = rapid_read64(plast); + } else { + const uint8_t *plast = p + len - 4; + a = rapid_read32(p); + b = rapid_read32(plast); } - if (i > 16) { - seed = rapid_mix(rapid_read64(p) ^ secret[2], rapid_read64(p + 8) ^ seed); - if (i > 32) { - seed = rapid_mix(rapid_read64(p + 16) ^ secret[2], rapid_read64(p + 24) ^ seed); - if (i > 48) { - seed = rapid_mix(rapid_read64(p + 32) ^ secret[1], rapid_read64(p + 40) ^ seed); - if (i > 64) { - seed = rapid_mix(rapid_read64(p + 48) ^ secret[1], rapid_read64(p + 56) ^ seed); - } - } + } else if (len > 0) { + a = (((uint64_t)p[0]) << 45) | p[len - 1]; + b = p[len >> 1]; + } else + a = b = 0; + } else { + if (i > 80) { + uint64_t see1 = seed, see2 = seed; + uint64_t see3 = seed, see4 = seed; + do { + seed = + rapid_mix(rapid_read64(p) ^ secret[0], rapid_read64(p + 8) ^ seed); + see1 = rapid_mix(rapid_read64(p + 16) ^ secret[1], + rapid_read64(p + 24) ^ see1); + see2 = rapid_mix(rapid_read64(p + 32) ^ secret[2], + rapid_read64(p + 40) ^ see2); + see3 = rapid_mix(rapid_read64(p + 48) ^ secret[3], + rapid_read64(p + 56) ^ see3); + see4 = rapid_mix(rapid_read64(p + 64) ^ secret[4], + rapid_read64(p + 72) ^ see4); + p += 80; + i -= 80; + } while (i > 80); + seed ^= see1; + see2 ^= see3; + seed ^= see4; + seed ^= see2; + } + if (i > 16) { + seed = rapid_mix(rapid_read64(p) ^ secret[2], rapid_read64(p + 8) ^ seed); + if (i > 32) { + seed = rapid_mix(rapid_read64(p + 16) ^ secret[2], + rapid_read64(p + 24) ^ seed); + if (i > 48) { + seed = rapid_mix(rapid_read64(p + 32) ^ secret[1], + rapid_read64(p + 40) ^ seed); + if (i > 64) { + seed = rapid_mix(rapid_read64(p + 48) ^ secret[1], + rapid_read64(p + 56) ^ seed); + } } } - a=rapid_read64(p+i-16) ^ i; b=rapid_read64(p+i-8); } - a ^= secret[1]; - b ^= seed; - rapid_mum(&a, &b); - return rapid_mix(a ^ secret[7], b ^ secret[1] ^ i); + a = rapid_read64(p + i - 16) ^ i; + b = rapid_read64(p + i - 8); } + a ^= secret[1]; + b ^= seed; + rapid_mum(&a, &b); + return rapid_mix(a ^ secret[7], b ^ secret[1] ^ i); +} - /* - * rapidhashNano main function. - * - * @param key Buffer to be hashed. - * @param len @key length, in bytes. - * @param seed 64-bit seed used to alter the hash result predictably. - * @param secret Triplet of 64-bit secrets used to alter hash result predictably. - * - * Returns a 64-bit hash. - */ - RAPIDHASH_INLINE_CONSTEXPR uint64_t rapidhashNano_internal(const void *key, size_t len, uint64_t seed, const uint64_t* secret) RAPIDHASH_NOEXCEPT { - const uint8_t *p=(const uint8_t *)key; - seed ^= rapid_mix(seed ^ secret[2], secret[1]); - uint64_t a=0, b=0; - size_t i = len; - if (_likely_(len <= 16)) { - if (len >= 4) { - seed ^= len; - if (len >= 8) { - const uint8_t* plast = p + len - 8; - a = rapid_read64(p); - b = rapid_read64(plast); - } else { - const uint8_t* plast = p + len - 4; - a = rapid_read32(p); - b = rapid_read32(plast); - } - } else if (len > 0) { - a = (((uint64_t)p[0])<<45)|p[len-1]; - b = p[len>>1]; - } else - a = b = 0; - } else { - if (i > 48) { - uint64_t see1 = seed, see2 = seed; - do { - seed = rapid_mix(rapid_read64(p) ^ secret[0], rapid_read64(p + 8) ^ seed); - see1 = rapid_mix(rapid_read64(p + 16) ^ secret[1], rapid_read64(p + 24) ^ see1); - see2 = rapid_mix(rapid_read64(p + 32) ^ secret[2], rapid_read64(p + 40) ^ see2); - p += 48; - i -= 48; - } while(i > 48); - seed ^= see1; - seed ^= see2; +/* + * rapidhashNano main function. + * + * @param key Buffer to be hashed. + * @param len @key length, in bytes. + * @param seed 64-bit seed used to alter the hash result predictably. + * @param secret Triplet of 64-bit secrets used to alter hash result + * predictably. + * + * Returns a 64-bit hash. + */ +RAPIDHASH_INLINE_CONSTEXPR uint64_t +rapidhashNano_internal(const void *key, size_t len, uint64_t seed, + const uint64_t *secret) RAPIDHASH_NOEXCEPT { + const uint8_t *p = (const uint8_t *)key; + seed ^= rapid_mix(seed ^ secret[2], secret[1]); + uint64_t a = 0, b = 0; + size_t i = len; + if (_likely_(len <= 16)) { + if (len >= 4) { + seed ^= len; + if (len >= 8) { + const uint8_t *plast = p + len - 8; + a = rapid_read64(p); + b = rapid_read64(plast); + } else { + const uint8_t *plast = p + len - 4; + a = rapid_read32(p); + b = rapid_read32(plast); } - if (i > 16) { - seed = rapid_mix(rapid_read64(p) ^ secret[2], rapid_read64(p + 8) ^ seed); - if (i > 32) { - seed = rapid_mix(rapid_read64(p + 16) ^ secret[2], rapid_read64(p + 24) ^ seed); - } + } else if (len > 0) { + a = (((uint64_t)p[0]) << 45) | p[len - 1]; + b = p[len >> 1]; + } else + a = b = 0; + } else { + if (i > 48) { + uint64_t see1 = seed, see2 = seed; + do { + seed = + rapid_mix(rapid_read64(p) ^ secret[0], rapid_read64(p + 8) ^ seed); + see1 = rapid_mix(rapid_read64(p + 16) ^ secret[1], + rapid_read64(p + 24) ^ see1); + see2 = rapid_mix(rapid_read64(p + 32) ^ secret[2], + rapid_read64(p + 40) ^ see2); + p += 48; + i -= 48; + } while (i > 48); + seed ^= see1; + seed ^= see2; + } + if (i > 16) { + seed = rapid_mix(rapid_read64(p) ^ secret[2], rapid_read64(p + 8) ^ seed); + if (i > 32) { + seed = rapid_mix(rapid_read64(p + 16) ^ secret[2], + rapid_read64(p + 24) ^ seed); } - a=rapid_read64(p+i-16) ^ i; b=rapid_read64(p+i-8); } - a ^= secret[1]; - b ^= seed; - rapid_mum(&a, &b); - return rapid_mix(a ^ secret[7], b ^ secret[1] ^ i); + a = rapid_read64(p + i - 16) ^ i; + b = rapid_read64(p + i - 8); } - + a ^= secret[1]; + b ^= seed; + rapid_mum(&a, &b); + return rapid_mix(a ^ secret[7], b ^ secret[1] ^ i); +} + /* * rapidhash seeded hash function. * @@ -486,10 +586,11 @@ * * Returns a 64-bit hash. */ -RAPIDHASH_INLINE_CONSTEXPR uint64_t rapidhash_withSeed(const void *key, size_t len, uint64_t seed) RAPIDHASH_NOEXCEPT { +RAPIDHASH_INLINE_CONSTEXPR uint64_t rapidhash_withSeed( + const void *key, size_t len, uint64_t seed) RAPIDHASH_NOEXCEPT { return rapidhash_internal(key, len, seed, rapid_secret); } - + /* * rapidhash general purpose hash function. * @@ -500,16 +601,18 @@ * * Returns a 64-bit hash. */ -RAPIDHASH_INLINE_CONSTEXPR uint64_t rapidhash(const void *key, size_t len) RAPIDHASH_NOEXCEPT { +RAPIDHASH_INLINE_CONSTEXPR uint64_t rapidhash(const void *key, + size_t len) RAPIDHASH_NOEXCEPT { return rapidhash_withSeed(key, len, 0); } /* * rapidhashMicro seeded hash function. * - * Designed for HPC and server applications, where cache misses make a noticeable performance detriment. - * Clang-18+ compiles it to ~140 instructions without stack usage, both on x86-64 and aarch64. - * Faster for sizes up to 512 bytes, just 15%-20% slower for inputs above 1kb. + * Designed for HPC and server applications, where cache misses make a + * noticeable performance detriment. Clang-18+ compiles it to ~140 instructions + * without stack usage, both on x86-64 and aarch64. Faster for sizes up to 512 + * bytes, just 15%-20% slower for inputs above 1kb. * * @param key Buffer to be hashed. * @param len @key length, in bytes. @@ -519,10 +622,11 @@ * * Returns a 64-bit hash. */ - RAPIDHASH_INLINE_CONSTEXPR uint64_t rapidhashMicro_withSeed(const void *key, size_t len, uint64_t seed) RAPIDHASH_NOEXCEPT { +RAPIDHASH_INLINE_CONSTEXPR uint64_t rapidhashMicro_withSeed( + const void *key, size_t len, uint64_t seed) RAPIDHASH_NOEXCEPT { return rapidhashMicro_internal(key, len, seed, rapid_secret); } - + /* * rapidhashMicro hash function. * @@ -533,7 +637,8 @@ * * Returns a 64-bit hash. */ -RAPIDHASH_INLINE_CONSTEXPR uint64_t rapidhashMicro(const void *key, size_t len) RAPIDHASH_NOEXCEPT { +RAPIDHASH_INLINE_CONSTEXPR uint64_t rapidhashMicro(const void *key, size_t len) + RAPIDHASH_NOEXCEPT { return rapidhashMicro_withSeed(key, len, 0); } @@ -548,16 +653,18 @@ * * Returns a 64-bit hash. */ - RAPIDHASH_INLINE_CONSTEXPR uint64_t rapidhashNano_withSeed(const void *key, size_t len, uint64_t seed) RAPIDHASH_NOEXCEPT { +RAPIDHASH_INLINE_CONSTEXPR uint64_t rapidhashNano_withSeed( + const void *key, size_t len, uint64_t seed) RAPIDHASH_NOEXCEPT { return rapidhashNano_internal(key, len, seed, rapid_secret); } - + /* * rapidhashNano hash function. * - * Designed for Mobile and embedded applications, where keeping a small code size is a top priority. - * Clang-18+ compiles it to less than 100 instructions without stack usage, both on x86-64 and aarch64. - * The fastest for sizes up to 48 bytes, but may be considerably slower for larger inputs. + * Designed for Mobile and embedded applications, where keeping a small code + * size is a top priority. Clang-18+ compiles it to less than 100 instructions + * without stack usage, both on x86-64 and aarch64. The fastest for sizes up to + * 48 bytes, but may be considerably slower for larger inputs. * * @param key Buffer to be hashed. * @param len @key length, in bytes. @@ -566,6 +673,7 @@ * * Returns a 64-bit hash. */ -RAPIDHASH_INLINE_CONSTEXPR uint64_t rapidhashNano(const void *key, size_t len) RAPIDHASH_NOEXCEPT { +RAPIDHASH_INLINE_CONSTEXPR uint64_t rapidhashNano(const void *key, size_t len) + RAPIDHASH_NOEXCEPT { return rapidhashNano_withSeed(key, len, 0); }
rapidhash.cabal view
@@ -5,23 +5,9 @@ -- see: https://github.com/sol/hpack name: rapidhash-version: 0.2.0.0-synopsis: rapidhash v3 - very fast, high-quality, non-cryptographic hashing-description: Zero-copy FFI bindings to rapidhash v3, a very fast, high-quality,- non-cryptographic hash. The upstream single-header C implementation is- vendored and ships with this package, so there is no system dependency.- .- The rapidhash algorithm itself is platform-independent. This package's- byte-oriented instances (Text, ByteString, ShortByteString, ByteArray)- produce stable, portable digests you can persist and compare across- machines; the element-typed Vector and PrimArray instances hash raw memory- as a fast path for large in-memory working sets and are not portable across- word width or endianness.- .- This is not a cryptographically secure hash. Use something else if you need cryptographic- security.- .- For more information, see the upstream repository: https://github.com/Nicoshev/rapidhash+version: 1.0.0.0+synopsis: rapidhash v3 - very fast, zero copy Hashable alternative+description: Rapidhash v3 is a very fast, high-quality, non-cryptographic hash. category: Data homepage: https://github.com/jtnuttall/lithon#readme bug-reports: https://github.com/jtnuttall/lithon/issues@@ -48,13 +34,7 @@ source-repository head type: git location: https://github.com/jtnuttall/lithon- subdir: rapidhash -flag aeson- description: Include ToJSON/FromJSON and ToJSONKey/FromJSONKey instances.- manual: True- default: False- flag llvm-bench description: Builds the benchmarks with GHC's LLVM backend so its effect can be measured on your hardware. Requires an LLVM toolchain matching your GHC's supported@@ -73,7 +53,6 @@ library exposed-modules: Data.Hash.RapidHash- Data.Hash.RapidHash.Aeson Data.Hash.RapidHash.Class Data.Hash.RapidHash.FFI Data.Hash.RapidHash.Types@@ -112,10 +91,6 @@ if flag(optimize) ghc-options: -O2 cc-options: -O3- if flag(aeson)- cpp-options: -DWANT_AESON- build-depends:- aeson >=2.2 && <2.4 test-suite rapidhash-doctest type: exitcode-stdio-1.0
src/Data/Hash/RapidHash.hs view
@@ -1,5 +1,3 @@-{-# LANGUAGE CPP #-}- -- | -- [rapidhash](https://github.com/Nicoshev/rapidhash) is a very fast, high-quality, -- non-cryptographic and platform-independent hash.@@ -13,9 +11,8 @@ -- -- This module can be imported unqualified. ----- 'Data.Aeson.ToJSON'\/'Data.Aeson.FromJSON' and--- 'Data.Aeson.ToJSONKey'\/'Data.Aeson.FromJSONKey' are provided behind the--- @aeson@ Cabal flag.+-- @ToJSON@\/@FromJSON@ and @ToJSONKey@\/@FromJSONKey@ instances are provided by+-- the companion package @rapidhash-aeson@ (import @Data.Hash.RapidHash.Aeson ()@). -- -- === __Security and HashDoS__ --@@ -132,8 +129,3 @@ showRapidHashMicroText, showRapidHashText, )--#ifdef WANT_AESON--- Orphans only-import Data.Hash.RapidHash.Aeson ()-#endif
− src/Data/Hash/RapidHash/Aeson.hs
@@ -1,57 +0,0 @@-{-# LANGUAGE CPP #-}-{-# OPTIONS_GHC -Wno-orphans #-}---- | Orphan aeson instances for 'Data.Hash.RapidHash.Types.RapidHash' and--- 'Data.Hash.RapidHash.Types.RapidHashMicro', encoding to and from the tagged--- hex form (e.g. @"rhv3:0123456789abcdef"@, @"rhmv3:0123456789abcdef"@).------ Opt-in with the @aeson@ flag; without it this module is empty and the--- instances do not exist.-module Data.Hash.RapidHash.Aeson () where--#ifdef WANT_AESON-import Data.Aeson qualified as A-import Data.Aeson.Types qualified as A--import Data.Hash.RapidHash.Types--instance A.ToJSON RapidHash where- toJSON = A.String . showRapidHashText- {-# INLINEABLE toJSON #-}--instance A.FromJSON RapidHash where- parseJSON = A.withText "rapidhashv3" \t -> case parseRapidHashText t of- Right h -> pure h- Left err -> fail err- {-# INLINEABLE parseJSON #-}---- | Keys use the same tagged hex form as the value instances, so a map--- keyed by @RapidHash@ serializes to a JSON object with @rhv3:@-prefixed keys.-instance A.ToJSONKey RapidHash where- toJSONKey = A.toJSONKeyText showRapidHashText- {-# INLINEABLE toJSONKey #-}--instance A.FromJSONKey RapidHash where- fromJSONKey = A.FromJSONKeyTextParser \t -> either fail pure (parseRapidHashText t)- {-# INLINEABLE fromJSONKey #-}--instance A.ToJSON RapidHashMicro where- toJSON = A.String . showRapidHashMicroText- {-# INLINEABLE toJSON #-}--instance A.FromJSON RapidHashMicro where- parseJSON = A.withText "rapidhashMicrov3" \t -> case parseRapidHashMicroText t of- Right h -> pure h- Left err -> fail err- {-# INLINEABLE parseJSON #-}---- | Keys use the same tagged hex form as the value instances, so a map--- keyed by @RapidHashMicro@ serializes to a JSON object with @rhmv3:@-prefixed keys.-instance A.ToJSONKey RapidHashMicro where- toJSONKey = A.toJSONKeyText showRapidHashMicroText- {-# INLINEABLE toJSONKey #-}--instance A.FromJSONKey RapidHashMicro where- fromJSONKey = A.FromJSONKeyTextParser \t -> either fail pure (parseRapidHashMicroText t)- {-# INLINEABLE fromJSONKey #-}-#endif
src/Data/Hash/RapidHash/FFI.hs view
@@ -66,12 +66,12 @@ ) where import Data.Array.Byte (ByteArray (ByteArray))-import Data.ByteString qualified as BS-import Data.ByteString.Short qualified as SBS+import Data.ByteString (ByteString)+import Data.ByteString.Short (ShortByteString (ShortByteString)) import Data.ByteString.Unsafe qualified as BSUnsafe import Data.Primitive (Prim (sizeOf#)) import Data.Primitive.PrimArray (PrimArray (PrimArray))-import Data.Text.Internal qualified as TI+import Data.Text.Internal (Text (Text)) import Data.Vector.Primitive qualified as PrimitiveVector import Data.Vector.Storable qualified as StorableVector import Data.Void (Void)@@ -145,12 +145,12 @@ ---------------------------------------------------------------------------------------------------- -- |--- Lifted 'rapidhashOffsetWithSeed_ByteArray#' for 'T.Text'.+-- Lifted 'rapidhashOffsetWithSeed_ByteArray#' for t'Text'. ----- This function reaches into 'T.Text'\'s internals to grab the offset and length into the underlying+-- This function reaches into t'Text'\'s internals to grab the offset and length into the underlying -- 'ByteArray#', which lets us do a zero-copy hash.-rapidhashWithSeed_Text :: CSeed -> TI.Text -> Word64-rapidhashWithSeed_Text seed (TI.Text (ByteArray ba#) off len) =+rapidhashWithSeed_Text :: CSeed -> Text -> Word64+rapidhashWithSeed_Text seed (Text (ByteArray ba#) off len) = rapidhashOffsetWithSeed_ByteArray# seed ba# (int2CSize off) (int2CSize len) {-# INLINE rapidhashWithSeed_Text #-} @@ -159,14 +159,14 @@ ---------------------------------------------------------------------------------------------------- -- |--- Applied 'rapidhashWithSeed_ByteArray' for 'SBS.ShortByteString'.-rapidhashWithSeed_ShortByteString :: CSeed -> SBS.ShortByteString -> Word64-rapidhashWithSeed_ShortByteString seed (SBS.ShortByteString ba) = rapidhashWithSeed_ByteArray seed ba+-- Applied 'rapidhashWithSeed_ByteArray' for t'ShortByteString'.+rapidhashWithSeed_ShortByteString :: CSeed -> ShortByteString -> Word64+rapidhashWithSeed_ShortByteString seed (ShortByteString ba) = rapidhashWithSeed_ByteArray seed ba {-# INLINE rapidhashWithSeed_ShortByteString #-} -- |--- Applied 'rapidhashWithSeedFFI_Ptr' for 'BS.ByteString'-rapidhashWithSeed_ByteString :: CSeed -> BS.ByteString -> Word64+-- Applied 'rapidhashWithSeedFFI_Ptr' for 'ByteString'+rapidhashWithSeed_ByteString :: CSeed -> ByteString -> Word64 rapidhashWithSeed_ByteString seed bs = unsafeDupablePerformIO $ BSUnsafe.unsafeUseAsCStringLen bs \(cstr, len) -> rapidhashWithSeedFFI_Ptr (castPtr cstr) (int2CSize len) seed@@ -262,12 +262,12 @@ ---------------------------------------------------------------------------------------------------- -- |--- Lifted 'rapidhashMicroOffsetWithSeed_ByteArray#' for 'T.Text'.+-- Lifted 'rapidhashMicroOffsetWithSeed_ByteArray#' for t'Text'. ----- This function reaches into 'T.Text'\'s internals to grab the offset and length into the underlying+-- This function reaches into t'Text'\'s internals to grab the offset and length into the underlying -- 'ByteArray#', which lets us do a zero-copy hash.-rapidhashMicroWithSeed_Text :: CSeed -> TI.Text -> Word64-rapidhashMicroWithSeed_Text seed (TI.Text (ByteArray ba#) off len) =+rapidhashMicroWithSeed_Text :: CSeed -> Text -> Word64+rapidhashMicroWithSeed_Text seed (Text (ByteArray ba#) off len) = rapidhashMicroOffsetWithSeed_ByteArray# seed ba#@@ -280,14 +280,14 @@ ---------------------------------------------------------------------------------------------------- -- |--- Applied 'rapidhashMicroWithSeed_ByteArray' for 'SBS.ShortByteString'.-rapidhashMicroWithSeed_ShortByteString :: CSeed -> SBS.ShortByteString -> Word64-rapidhashMicroWithSeed_ShortByteString seed (SBS.ShortByteString ba) = rapidhashMicroWithSeed_ByteArray seed ba+-- Applied 'rapidhashMicroWithSeed_ByteArray' for t'ShortByteString'.+rapidhashMicroWithSeed_ShortByteString :: CSeed -> ShortByteString -> Word64+rapidhashMicroWithSeed_ShortByteString seed (ShortByteString ba) = rapidhashMicroWithSeed_ByteArray seed ba {-# INLINE rapidhashMicroWithSeed_ShortByteString #-} -- |--- Applied 'rapidhashMicroWithSeedFFI_Ptr' for 'BS.ByteString'-rapidhashMicroWithSeed_ByteString :: CSeed -> BS.ByteString -> Word64+-- Applied 'rapidhashMicroWithSeedFFI_Ptr' for 'ByteString'+rapidhashMicroWithSeed_ByteString :: CSeed -> ByteString -> Word64 rapidhashMicroWithSeed_ByteString seed bs = unsafeDupablePerformIO $ BSUnsafe.unsafeUseAsCStringLen bs \(cstr, len) -> rapidhashMicroWithSeedFFI_Ptr (castPtr cstr) (int2CSize len) seed
src/Data/Hash/RapidHash/Types.hs view
@@ -61,9 +61,9 @@ import Data.Text (Text) import Data.Text qualified as T import Data.Text.Array qualified as A-import Data.Text.Builder.Linear qualified as TB+import Data.Text.Builder.Linear (Builder (Builder), runBuilder, runBuilderBS) import Data.Text.Builder.Linear.Buffer qualified as TBuff-import Data.Text.Builder.Linear.Core qualified as TB+import Data.Text.Builder.Linear.Core (appendBounded) import Data.Text.Read qualified as T import Data.Word (Word64, Word8) import GHC.Exts (Addr#, Int (I#), (>#))@@ -182,7 +182,7 @@ {-# INLINE get #-} -- |--- Efficiently render a 'RapidHash' as a 'TB.Builder'+-- Efficiently render a 'RapidHash' as a t'Builder' -- -- === __Examples__ --@@ -199,7 +199,7 @@ -- -- >>> RAPIDHASH_V3_PREFIX `T.isPrefixOf` showRapidHashText (RapidHash 0x0) -- True-rapidHashTextBuilder :: RapidHash -> TB.Builder+rapidHashTextBuilder :: RapidHash -> Builder rapidHashTextBuilder (RapidHash h) = hashTextBuilder RAPIDHASH_V3_PREFIX_ADDR h {-# INLINE rapidHashTextBuilder #-} @@ -213,9 +213,9 @@ -- | -- Efficiently render a 'RapidHash' as 'ByteString'. If your text blob is utf-8, it may be worth--- composing using 'TB.Builder' and extracting with 'TB.runBuilderBS' yourself.+-- composing using t'Builder' and extracting with 'runBuilderBS' yourself. ----- The resulting 'ByteString' is utf-8 encoded, using 'TB.runBuilderBS'+-- The resulting 'ByteString' is utf-8 encoded, using 'runBuilderBS' -- -- See 'rapidHashTextBuilder' showRapidHashBS :: RapidHash -> ByteString@@ -365,7 +365,7 @@ {-# INLINE get #-} -- |--- Efficiently render a 'RapidHashMicro' as a 'TB.Builder'+-- Efficiently render a 'RapidHashMicro' as a t'Builder' -- -- === __Examples__ --@@ -382,7 +382,7 @@ -- -- >>> RAPIDHASH_MICRO_V3_PREFIX `T.isPrefixOf` showRapidHashMicroText (RapidHashMicro 0x0) -- True-rapidhashMicroTextBuilder :: RapidHashMicro -> TB.Builder+rapidhashMicroTextBuilder :: RapidHashMicro -> Builder rapidhashMicroTextBuilder (RapidHashMicro h) = hashTextBuilder RAPIDHASH_MICRO_V3_PREFIX_ADDR h {-# INLINE rapidhashMicroTextBuilder #-} @@ -396,9 +396,9 @@ -- | -- Efficiently render a 'RapidHashMicro' as 'ByteString'. If your text blob is utf-8, it may be worth--- composing using 'TB.Builder' and extracting with 'TB.runBuilderBS' yourself.+-- composing using t'Builder' and extracting with 'runBuilderBS' yourself. ----- The resulting 'ByteString' is utf-8 encoded, using 'TB.runBuilderBS'+-- The resulting 'ByteString' is utf-8 encoded, using 'runBuilderBS' -- -- See 'rapidhashMicroTextBuilder' showRapidHashMicroBS :: RapidHashMicro -> ByteString@@ -486,22 +486,22 @@ -- Hash-agnostic ---------------------------------------------------------------------------------------------------- --- | Efficiently render a hash as a 'TB.Builder'-hashTextBuilder :: Addr# -> Word64 -> TB.Builder-hashTextBuilder prefix h = TB.Builder \b -> appendPaddedHashHexBuf (b TBuff.|># prefix) h+-- | Efficiently render a hash as a t'Builder'+hashTextBuilder :: Addr# -> Word64 -> Builder+hashTextBuilder prefix h = Builder \b -> appendPaddedHashHexBuf (b TBuff.|># prefix) h {-# INLINEABLE hashTextBuilder #-} -- | Efficiently render a hash as 'Text'. showHashText :: Addr# -> Word64 -> Text-showHashText prefix h = TB.runBuilder (hashTextBuilder prefix h)+showHashText prefix h = runBuilder (hashTextBuilder prefix h) {-# INLINEABLE showHashText #-} -- | -- Efficiently render a hash as 'ByteString'. ----- The resulting 'ByteString' is utf-8 encoded, using 'TB.runBuilderBS'+-- The resulting 'ByteString' is utf-8 encoded, using 'runBuilderBS' showHashBS :: Addr# -> Word64 -> ByteString-showHashBS prefix h = TB.runBuilderBS (hashTextBuilder prefix h)+showHashBS prefix h = runBuilderBS (hashTextBuilder prefix h) {-# INLINEABLE showHashBS #-} -- |@@ -518,7 +518,7 @@ appendPaddedHashHexBuf buffer h = -- appendBounded function preallocates a given length, and takes a callback that -- mutates the array and returns how many characters it wrote.- TB.appendBounded+ appendBounded RAPIDHASH_V3_HEX_LENGTH ( \dst off -> do endOff <- unsafeAppendHexW64 dst (off + RAPIDHASH_V3_HEX_LENGTH - 1) h
test/pin/pin.c view
@@ -22,10 +22,10 @@ /* Lengths bracketing rapidhash v3's internal branch boundaries. */ static const size_t lengths[] = {- 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11,- 12, 13, 14, 15, 16, 17, 31, 32, 33, 47, 48, 49,- 63, 64, 65, 79, 80, 81, 111, 112, 113, 127, 128, 129,- 191, 192, 193, 223, 224, 225, 255, 256, 257, 447, 448, 449,+ 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11,+ 12, 13, 14, 15, 16, 17, 31, 32, 33, 47, 48, 49,+ 63, 64, 65, 79, 80, 81, 111, 112, 113, 127, 128, 129,+ 191, 192, 193, 223, 224, 225, 255, 256, 257, 447, 448, 449, 511, 512, 1023, 1024, 4096, 65535, 65536, 300007}; static const uint64_t seeds[] = {0ULL, 1ULL, 0xDEADBEEFULL,@@ -40,11 +40,14 @@ int main(void) { size_t max_len = 0; for (size_t i = 0; i < COUNT(lengths); i++)- if (lengths[i] > max_len) max_len = lengths[i];+ if (lengths[i] > max_len)+ max_len = lengths[i]; uint8_t *buf = malloc(max_len);- if (!buf) return 1;- for (size_t i = 0; i < max_len; i++) buf[i] = byte_at(i);+ if (!buf)+ return 1;+ for (size_t i = 0; i < max_len; i++)+ buf[i] = byte_at(i); for (size_t li = 0; li < COUNT(lengths); li++) printf("default %zu %016" PRIx64 "\n", lengths[li],@@ -77,9 +80,8 @@ for (size_t si = 0; si < COUNT(offset_seeds); si++) printf("microOffset %zu %zu %016" PRIx64 " %016" PRIx64 "\n", offsets[oi], offset_lengths[li], offset_seeds[si],- rapidhashMicro_offset_withSeed(buf, offsets[oi],- offset_lengths[li],- offset_seeds[si]));+ rapidhashMicro_offset_withSeed(+ buf, offsets[oi], offset_lengths[li], offset_seeds[si])); free(buf); return 0;