Hashtron inference: Difference between revisions

Content added Content deleted
m (→‎{{header|Phix}}: debug arg)
m (→‎{{header|Phix}}: added unsigned mask comment)
Line 318: Line 318:
=={{header|Phix}}==
=={{header|Phix}}==
{{trans|Wren}}
{{trans|Wren}}
This sort of thing is generally quite painful in Phix, since it will insist on things like zero minus one being -1
rather than some huge positive number... you certainly have to take some care it always uses unsigned masking.

<syntaxhighlight lang="phix">
<syntaxhighlight lang="phix">
requires(64)
requires(64)
constant U32 = #FFFFFFFF

function hashtron(integer n, s, mx)
function hashtron(integer n, s, mx)
// Mixing stage, mix input with salt using subtraction
// Mixing stage, mix input with salt using subtraction
atom m = and_bitsu(n - s,#FFFFFFFF)
atom m = and_bitsu(n-s,U32)
// Hashing stage, use xor shift with prime coefficients
// Hashing stage, use xor shift with prime coefficients
for p in {-2,-3,+5,+7,-11,-13,+17,-19} do
for p in {-2,-3,+5,+7,-11,-13,+17,-19} do
m = xor_bitsu(m,and_bitsu(shift_bits(m,p),#FFFFFFFF))
m = xor_bitsu(m,and_bitsu(shift_bits(m,p),U32))
end for
end for


// Mixing stage 2, mix input with salt using addition
// Mixing stage 2, mix input with salt using addition
m = and_bitsu(m+s,#FFFFFFFF)
m = and_bitsu(m+s,U32)


// Modular stage using Lemire's fast alternative to modulo reduction
// Modular stage using Lemire's fast alternative to modulo reduction