summaryrefslogtreecommitdiff
path: root/util.c
diff options
context:
space:
mode:
authorYves Orton <demerphq@gmail.com>2023-03-23 11:50:54 +0100
committerYves Orton <demerphq@gmail.com>2023-03-29 20:54:49 +0800
commit55dc0196bb04b67e8268bba2bf900f129e4545d1 (patch)
tree682a3ae37e252e748de60c208bc0e67c176b8a35 /util.c
parent3bce2dad3897a6791eb6d124f4b4a368a93d4cd5 (diff)
downloadperl-55dc0196bb04b67e8268bba2bf900f129e4545d1.tar.gz
util.c - avoid warning about truncated pointer value
This code is part of the fallback rng initialization, it should only be used in odd circumstances, and it is intended to be "random"ish, so the logic change should not be a problem. This more or less reverts commit 4b4c4ab35e70f005167c863eae6d2003492c393c, although the S_ptr_hash() now lives in util.c instead of hv.c. Turns out having a decent pointer hash /is/ useful.
Diffstat (limited to 'util.c')
-rw-r--r--util.c36
1 files changed, 35 insertions, 1 deletions
diff --git a/util.c b/util.c
index cedde45120..b5721173e1 100644
--- a/util.c
+++ b/util.c
@@ -4592,6 +4592,39 @@ Perl_parse_unicode_opts(pTHX_ const char **popt)
# include <starlet.h>
#endif
+/* hash a pointer and return a U32
+ *
+ * this code was derived from Sereal, which was derived from autobox.
+ */
+
+PERL_STATIC_INLINE U32 S_ptr_hash(PTRV u) {
+#if PTRSIZE == 8
+ /*
+ * This is one of Thomas Wang's hash functions for 64-bit integers from:
+ * http://www.concentric.net/~Ttwang/tech/inthash.htm
+ */
+ u = (~u) + (u << 18);
+ u = u ^ (u >> 31);
+ u = u * 21;
+ u = u ^ (u >> 11);
+ u = u + (u << 6);
+ u = u ^ (u >> 22);
+#else
+ /*
+ * This is one of Bob Jenkins' hash functions for 32-bit integers
+ * from: http://burtleburtle.net/bob/hash/integer.html
+ */
+ u = (u + 0x7ed55d16) + (u << 12);
+ u = (u ^ 0xc761c23c) ^ (u >> 19);
+ u = (u + 0x165667b1) + (u << 5);
+ u = (u + 0xd3a2646c) ^ (u << 9);
+ u = (u + 0xfd7046c5) + (u << 3);
+ u = (u ^ 0xb55a4f09) ^ (u >> 16);
+#endif
+ return (U32)u;
+}
+
+
U32
Perl_seed(pTHX)
{
@@ -4660,7 +4693,8 @@ Perl_seed(pTHX)
u += SEED_C3 * (U32)PerlProc_getpid();
u += SEED_C4 * (U32)PTR2UV(PL_stack_sp);
#ifndef PLAN9 /* XXX Plan9 assembler chokes on this; fix needed */
- u += SEED_C5 * (U32)PTR2UV(&when);
+ UV ptruv = PTR2UV(&when);
+ u += SEED_C5 * ptr_hash(ptruv);
#endif
return u;
}