Skip to content
← Back

src/ir/llvm/llvm_scalar_hash.ghul

1
namespace IR.LLVMTarget is
2
use LLVM.BLOCK
3
use LLVM.BinaryOperator
4
use LLVM.Conversion
5
use LLVM.FastMath
6
use LLVM.FloatPredicate
7
use LLVM.IntegerFlags
8
use LLVM.Types
9
10
/// The hash code of a scalar held as the LLVM target holds it, the one
11
/// the wasm target gives the same value: a 32-bit kind is its own hash,
12
/// a 64-bit one folds its halves together, and a floating value hashes
13
/// its bits once zero of either sign and every NaN are made alike, so
14
/// values `=~` reports equal hash alike.
15
class LLVM_SCALAR_HASH is
16
/// The instructions in `block` computing the hash of `value`, an
17
/// `i1`, a small integer, an `i32`, an `i64`, a `float` or a `double`.
18
///
19
/// - block: where the instructions go
20
/// - value: the scalar, as the target holds it
21
hash(block: BLOCK, value: LLVM.Value) -> LLVM.Value static is
22
let type = value.type
23
24
if type =~ LLVM.Type.FLOAT then
25
return block.convert(Conversion.BIT_CAST, _canonical(block, value), Types.i32)
26
elif type =~ LLVM.Type.DOUBLE then
27
return _fold(block, block.convert(Conversion.BIT_CAST, _canonical(block, value), Types.i64))
28
elif LLVM_TYPES.width(type) == 64 then
29
return _fold(block, value)
30
elif LLVM_TYPES.width(type) < 32 then
31
return block.convert(Conversion.ZERO_EXTEND, value, Types.i32)
32
fi
33
34
return value
35
si
36
37
// Zero where `value` is a NaN, and otherwise `value` plus zero, which
38
// turns -0 into +0.
39
_canonical(block: BLOCK, value: LLVM.Value) -> LLVM.Value static is
40
let zero = LLVM_TYPES.zero(value.type)
41
let is_number = block.compare(FloatPredicate.ORD, value, value)
42
43
return block.select(is_number, block.floating(BinaryOperator.FADD, FastMath.NONE, value, zero), zero)
44
si
45
46
// The low half of `value` exclusive-ored with its high half.
47
_fold(block: BLOCK, value: LLVM.Value) -> LLVM.Value static is
48
let high = block.binary(BinaryOperator.LSHR, IntegerFlags.NONE, value, LLVM.Values.i64(32L))
49
50
return block.convert(Conversion.TRUNCATE, block.binary(BinaryOperator.XOR, IntegerFlags.NONE, value, high), Types.i32)
51
si
52
si
53
si