diff options
| author | ruki <[email protected]> | 2025-10-01 00:47:56 +0800 |
|---|---|---|
| committer | ruki <[email protected]> | 2025-10-01 00:47:56 +0800 |
| commit | 9dc8bb99439f4d31e3563a5865764798c2094e8e (patch) | |
| tree | 911e222e99d1df86804f1380940f829045fbe97b /core | |
| parent | 672470e6e4ea7fd150354c131ba5fb60f7331643 (diff) | |
improve random hash
Diffstat (limited to 'core')
| -rw-r--r-- | core/src/xmake/engine.c | 16 | ||||
| -rw-r--r-- | core/src/xmake/hash/prefix.h | 27 | ||||
| -rw-r--r-- | core/src/xmake/hash/rand128.c | 55 | ||||
| -rw-r--r-- | core/src/xmake/hash/rand32.c | 51 | ||||
| -rw-r--r-- | core/src/xmake/hash/rand64.c | 51 |
5 files changed, 195 insertions, 5 deletions
diff --git a/core/src/xmake/engine.c b/core/src/xmake/engine.c index 203fc1d49..c3f8e9f5c 100644 --- a/core/src/xmake/engine.c +++ b/core/src/xmake/engine.c @@ -225,6 +225,9 @@ tb_int_t xm_hash_uuid4(lua_State* lua); tb_int_t xm_hash_sha(lua_State* lua); tb_int_t xm_hash_md5(lua_State* lua); tb_int_t xm_hash_xxhash(lua_State* lua); +tb_int_t xm_hash_rand32(lua_State* lua); +tb_int_t xm_hash_rand64(lua_State* lua); +tb_int_t xm_hash_rand128(lua_State* lua); // the base64 functions tb_int_t xm_base64_encode(lua_State* lua); @@ -518,11 +521,14 @@ static luaL_Reg const g_path_functions[] = // the hash functions static luaL_Reg const g_hash_functions[] = { - { "uuid4", xm_hash_uuid4 } -, { "sha", xm_hash_sha } -, { "md5", xm_hash_md5 } -, { "xxhash", xm_hash_xxhash } -, { tb_null, tb_null } + { "uuid4", xm_hash_uuid4 } +, { "sha", xm_hash_sha } +, { "md5", xm_hash_md5 } +, { "xxhash", xm_hash_xxhash } +, { "rand32", xm_hash_rand32 } +, { "rand64", xm_hash_rand64 } +, { "rand128", xm_hash_rand128 } +, { tb_null, tb_null } }; // the base64 functions diff --git a/core/src/xmake/hash/prefix.h b/core/src/xmake/hash/prefix.h index cf4cb082d..59a3941d7 100644 --- a/core/src/xmake/hash/prefix.h +++ b/core/src/xmake/hash/prefix.h @@ -30,6 +30,33 @@ * 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; + x ^= x >> 7; + x ^= x << 17; + return x; +} + +// http://xorshift.di.unimi.it/xorshift128plus.c +static __tb_inline__ tb_uint64_t xm_hash_xorshift128(tb_uint64_t* s) +{ + tb_uint64_t s1 = s[0]; + tb_uint64_t const s0 = s[1]; + s[0] = s0; + s1 ^= s1 << 23; + s[1] = s1 ^ s0 ^ (s1 >> 18) ^ (s0 >> 5); + return s[1] + s0; +} + static __tb_inline__ tb_size_t xm_hash_make_cstr(tb_char_t hash[256], tb_byte_t const* data, tb_size_t size) { static tb_char_t const* digits_table = "0123456789abcdef"; diff --git a/core/src/xmake/hash/rand128.c b/core/src/xmake/hash/rand128.c new file mode 100644 index 000000000..946d396fd --- /dev/null +++ b/core/src/xmake/hash/rand128.c @@ -0,0 +1,55 @@ +/*!A cross-platform build utility based on Lua + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + * + * Copyright (C) 2015-present, Xmake Open Source Community. + * + * @author ruki + * @file rand128.c + * + */ + +/* ////////////////////////////////////////////////////////////////////////////////////// + * trace + */ +#define TB_TRACE_MODULE_NAME "rand128" +#define TB_TRACE_MODULE_DEBUG (0) + +/* ////////////////////////////////////////////////////////////////////////////////////// + * includes + */ +#include "prefix.h" + +/* ////////////////////////////////////////////////////////////////////////////////////// + * implementation + */ +tb_int_t xm_hash_rand128(lua_State* lua) +{ + // check + tb_assert_and_check_return_val(lua, 0); + + static union { tb_byte_t b[16]; tb_uint64_t word[2]; } s_seed = {0}; + if (!s_seed.word[0] && !s_seed.word[1]) + { + s_seed.word[0] = (tb_uint64_t)tb_mclock(); + s_seed.word[1] = (tb_uint64_t)tb_mclock(); + } + s_seed.word[0] = xm_hash_xorshift128(s_seed.word); + s_seed.word[1] = xm_hash_xorshift128(s_seed.word); + + tb_char_t s[256]; + tb_size_t n = xm_hash_make_cstr(s, s_seed.b, 16); + + lua_pushlstring(lua, s, n); + return 1; +} diff --git a/core/src/xmake/hash/rand32.c b/core/src/xmake/hash/rand32.c new file mode 100644 index 000000000..b76b0ae15 --- /dev/null +++ b/core/src/xmake/hash/rand32.c @@ -0,0 +1,51 @@ +/*!A cross-platform build utility based on Lua + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + * + * Copyright (C) 2015-present, Xmake Open Source Community. + * + * @author ruki + * @file rand32.c + * + */ + +/* ////////////////////////////////////////////////////////////////////////////////////// + * trace + */ +#define TB_TRACE_MODULE_NAME "rand32" +#define TB_TRACE_MODULE_DEBUG (0) + +/* ////////////////////////////////////////////////////////////////////////////////////// + * includes + */ +#include "prefix.h" + +/* ////////////////////////////////////////////////////////////////////////////////////// + * implementation + */ +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_mclock(); + s_seed.word = xm_hash_xorshift32(s_seed.word); + + tb_char_t s[64]; + tb_size_t n = xm_hash_make_cstr(s, s_seed.b, 4); + + lua_pushlstring(lua, s, n); + return 1; +} diff --git a/core/src/xmake/hash/rand64.c b/core/src/xmake/hash/rand64.c new file mode 100644 index 000000000..a42d0eedb --- /dev/null +++ b/core/src/xmake/hash/rand64.c @@ -0,0 +1,51 @@ +/*!A cross-platform build utility based on Lua + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + * + * Copyright (C) 2015-present, Xmake Open Source Community. + * + * @author ruki + * @file rand64.c + * + */ + +/* ////////////////////////////////////////////////////////////////////////////////////// + * trace + */ +#define TB_TRACE_MODULE_NAME "rand64" +#define TB_TRACE_MODULE_DEBUG (0) + +/* ////////////////////////////////////////////////////////////////////////////////////// + * includes + */ +#include "prefix.h" + +/* ////////////////////////////////////////////////////////////////////////////////////// + * implementation + */ +tb_int_t xm_hash_rand64(lua_State* lua) +{ + // check + tb_assert_and_check_return_val(lua, 0); + + static union { tb_byte_t b[8]; tb_uint64_t word; } s_seed = {0}; + if (!s_seed.word) + s_seed.word = (tb_uint64_t)tb_mclock(); + s_seed.word = xm_hash_xorshift64(s_seed.word); + + tb_char_t s[256]; + tb_size_t n = xm_hash_make_cstr(s, s_seed.b, 8); + + lua_pushlstring(lua, s, n); + return 1; +} |
