summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorruki <[email protected]>2025-10-02 22:50:18 +0800
committerruki <[email protected]>2025-10-02 22:50:18 +0800
commit6377f4494967e341150f0e2bb042d3c7d8bd15c9 (patch)
treea0e6791c22c70b58bad39d12a9c2d91d12154cad
parent0938e19d1ff2711224c6e8675366fed845b28c0d (diff)
improve rand32
-rw-r--r--core/src/xmake/hash/prefix.h8
-rw-r--r--core/src/xmake/hash/rand32.c11
-rw-r--r--core/src/xmake/hash/xxhash.c36
-rw-r--r--xmake/core/base/hash.lua18
4 files changed, 40 insertions, 33 deletions
diff --git a/core/src/xmake/hash/prefix.h b/core/src/xmake/hash/prefix.h
index 59a3941d7..8a321e1eb 100644
--- a/core/src/xmake/hash/prefix.h
+++ b/core/src/xmake/hash/prefix.h
@@ -30,14 +30,6 @@
* helper implementation
*/
-static __tb_inline__ tb_uint32_t xm_hash_xorshift32(tb_uint32_t x)
-{
- x ^= x << 13;
- x ^= x >> 7;
- x ^= x << 17;
- return x;
-}
-
static __tb_inline__ tb_uint64_t xm_hash_xorshift64(tb_uint64_t x)
{
x ^= x << 13;
diff --git a/core/src/xmake/hash/rand32.c b/core/src/xmake/hash/rand32.c
index c14c6288e..bbf565acd 100644
--- a/core/src/xmake/hash/rand32.c
+++ b/core/src/xmake/hash/rand32.c
@@ -38,13 +38,14 @@ tb_int_t xm_hash_rand32(lua_State* lua)
// check
tb_assert_and_check_return_val(lua, 0);
- static union { tb_byte_t b[4]; tb_uint32_t word; } s_seed = {0};
- if (!s_seed.word)
- s_seed.word = (tb_uint32_t)tb_uclock();
- s_seed.word = xm_hash_xorshift32(s_seed.word);
+ static tb_uint64_t s_seed = 0;
+ if (!s_seed)
+ s_seed = (tb_uint64_t)tb_uclock();
+ s_seed = xm_hash_xorshift64(s_seed);
tb_char_t s[256];
- tb_size_t n = xm_hash_make_cstr(s, s_seed.b, 4);
+ tb_uint32_t word = (s_seed >> 32) ^ (s_seed & 0xffffffff);
+ tb_size_t n = xm_hash_make_cstr(s, (tb_byte_t const*)&word, 4);
lua_pushlstring(lua, s, n);
return 1;
diff --git a/core/src/xmake/hash/xxhash.c b/core/src/xmake/hash/xxhash.c
index 0c9dbdad7..cf4f67e37 100644
--- a/core/src/xmake/hash/xxhash.c
+++ b/core/src/xmake/hash/xxhash.c
@@ -44,7 +44,7 @@ tb_int_t xm_hash_xxhash(lua_State* lua)
// get mode
tb_size_t mode = (tb_size_t)lua_tointeger(lua, 1);
- if (mode != 64 && mode != 128)
+ if (mode != 32 && mode != 64 && mode != 128)
{
lua_pushnil(lua);
lua_pushfstring(lua, "invalid mode(%d)!", (tb_int_t)mode);
@@ -66,17 +66,24 @@ tb_int_t xm_hash_xxhash(lua_State* lua)
// compuate hash
tb_byte_t const* buffer = tb_null;
+ tb_uint32_t value32;
XXH64_hash_t value64;
XXH128_hash_t value128;
- if (mode == 64)
+ if (mode == 128)
+ {
+ value128 = XM_XXH3_128bits(data, size);
+ buffer = (tb_byte_t const*)&value128;
+ }
+ else if (mode == 64)
{
value64 = XM_XXH3_64bits(data, size);
buffer = (tb_byte_t const*)&value64;
}
- else if (mode == 128)
+ else if (mode == 32)
{
- value128 = XM_XXH3_128bits(data, size);
- buffer = (tb_byte_t const*)&value128;
+ value64 = XM_XXH3_64bits(data, size);
+ value32 = (value64 >> 32) ^ (value64 & 0xffffffff);
+ buffer = (tb_byte_t const*)&value32;
}
if (!buffer)
{
@@ -109,7 +116,7 @@ tb_int_t xm_hash_xxhash(lua_State* lua)
if (tb_stream_open(stream) && state)
{
// reset xxhash
- if (mode == 64) XM_XXH3_64bits_reset(state);
+ if (mode == 32 || mode == 64) XM_XXH3_64bits_reset(state);
else XM_XXH3_128bits_reset(state);
// read data and update xxhash
@@ -122,7 +129,7 @@ tb_int_t xm_hash_xxhash(lua_State* lua)
// ok?
if (real > 0)
{
- if (mode == 64) XM_XXH3_64bits_update(state, data, real);
+ if (mode == 32 || mode == 64) XM_XXH3_64bits_update(state, data, real);
else XM_XXH3_128bits_update(state, data, real);
}
// no data? continue it
@@ -141,17 +148,24 @@ tb_int_t xm_hash_xxhash(lua_State* lua)
// compuate hash
tb_byte_t const* buffer;
+ tb_uint32_t value32;
XXH64_hash_t value64;
XXH128_hash_t value128;
- if (mode == 64)
+ if (mode == 128)
+ {
+ value128 = XM_XXH3_128bits_digest(state);
+ buffer = (tb_byte_t const*)&value128;
+ }
+ else if (mode == 64)
{
value64 = XM_XXH3_64bits_digest(state);
buffer = (tb_byte_t const*)&value64;
}
- else
+ else if (mode == 32)
{
- value128 = XM_XXH3_128bits_digest(state);
- buffer = (tb_byte_t const*)&value128;
+ value64 = XM_XXH3_64bits_digest(state);
+ value32 = (value64 >> 32) ^ (value64 & 0xffffffff);
+ buffer = (tb_byte_t const*)&value32;
}
// make xxhash string
diff --git a/xmake/core/base/hash.lua b/xmake/core/base/hash.lua
index f08c562aa..e846ba302 100644
--- a/xmake/core/base/hash.lua
+++ b/xmake/core/base/hash.lua
@@ -81,11 +81,15 @@ end
-- generate xxhash32 from the given file or data
function hash.xxhash32(file_or_data)
- local result, errors = hash.xxhash64(file_or_data)
- if result then
- result = result:sub(1, 8)
+ local hashstr, errors
+ if bytes.instance_of(file_or_data) then
+ local datasize = file_or_data:size()
+ local dataaddr = file_or_data:caddr()
+ hashstr, errors = hash._xxhash(32, dataaddr, datasize)
+ else
+ hashstr, errors = hash._xxhash(32, file_or_data)
end
- return result, errors
+ return hashstr, errors
end
-- generate xxhash64 from the given file or data
@@ -118,11 +122,7 @@ end
function hash.strhash32(str)
local data = libc.ptraddr(libc.dataptr(str))
local size = #str
- local result, errors = hash._xxhash(64, data, size)
- if result then
- result = result:sub(1, 8)
- end
- return result, errors
+ return hash._xxhash(32, data, size)
end
-- generate hash64 from string, e.g. "91e8ecf191e8ecf1"