Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 11 additions & 0 deletions src/compile.ml
Original file line number Diff line number Diff line change
Expand Up @@ -1440,6 +1440,12 @@ module Prim = struct
let prim_shiftToWordN b =
prim_intToWord32 ^^
UnboxedSmallWord.shift_leftWordNtoI32 b
let prim_hashInt env =
let (set_n, get_n) = new_local64 env "n" in
set_n ^^
get_n ^^ get_n ^^ compile_const_64 32L ^^ G.i (Binary (Wasm.Values.I64 I64Op.ShrU)) ^^
G.i (Binary (Wasm.Values.I64 I64Op.Xor)) ^^
prim_intToWord32
end (* Prim *)

module Object = struct
Expand Down Expand Up @@ -3782,6 +3788,11 @@ and compile_exp (env : E.t) exp =
compile_unboxed_const 8l ^^
G.i (Binary (Wasm.Values.I32 I32Op.Shl))

| "Int~hash" ->
SR.UnboxedWord32,
compile_exp_as env SR.UnboxedInt64 e ^^
Prim.prim_hashInt env

| "popcnt" ->
SR.UnboxedWord32,
compile_exp_as env SR.UnboxedWord32 e ^^
Expand Down
8 changes: 8 additions & 0 deletions src/prelude.ml
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,10 @@ class revrange(x : Nat, y : Nat) {
func printInt(x : Int) { (prim "printInt" : Int -> ()) x };
func print(x : Text) { (prim "print" : Text -> ()) x };

// Hashing
func hashInt(n : Int) : Word32 = (prim "Int~hash" : Int -> Word32) n;


// Conversions
func natToWord8(n : Nat) : Word8 = (prim "Nat->Word8" : Nat -> Word8) n;
func word8ToNat(n : Word8) : Nat = (prim "Word8->Nat" : Word8 -> Nat) n;
Expand Down Expand Up @@ -138,6 +142,10 @@ end (* Conv *)
let prim = function
| "abs" -> fun v k -> k (Int (Nat.abs (as_int v)))

| "Int~hash" -> fun v k ->
let i = Word64.of_int_s (Big_int.int_of_big_int (as_int v)) in
let j = Word64.(and_ 0xFFFFFFFFL (xor (shr_u i 32L) i))
in k (Word32 (Word32.of_int_u (Int64.to_int j)))
| "Nat->Word8" -> fun v k ->
let i = Big_int.int_of_big_int (as_int v)
in k (Word8 (Word8.of_int_u i))
Expand Down
16 changes: 8 additions & 8 deletions test/run-dfinity/ok/counter-class.wasm.stderr.ok
Original file line number Diff line number Diff line change
Expand Up @@ -18,31 +18,31 @@ non-closed actor: (ActorE
(FuncE
read
(shared 1 -> 0)
(params $68)
(params $69)
()
(BlockE
(LetD
(TupP (VarP $66))
(TupE (CallE ( 1 -> 1) (PrimE @deserialize) (VarE $68)))
(TupP (VarP $67))
(TupE (CallE ( 1 -> 1) (PrimE @deserialize) (VarE $69)))
)
(CallE
( 1 -> 0)
(FuncE
$lambda
( 1 -> 0)
(params $65)
(params $66)
()
(CallE ( 1 -> 0) (VarE $65) (VarE c))
(CallE ( 1 -> 0) (VarE $66) (VarE c))
)
(FuncE
$lambda
( 1 -> 0)
(params $67)
(params $68)
()
(CallE
(shared 1 -> 0)
(VarE $66)
(CallE ( 1 -> 1) (PrimE @serialize) (VarE $67))
(VarE $67)
(CallE ( 1 -> 1) (PrimE @serialize) (VarE $68))
)
)
)
Expand Down
8 changes: 8 additions & 0 deletions test/run/hashes.as
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@

assert (hashInt (10**7) == (10000000 : Word32));
assert (hashInt 0 == (0 : Word32));
assert (hashInt (10**18) == (2_860_824_243 : Word32));

assert (hashInt (-1) == (0 : Word32));
assert (hashInt (-387) == (386 : Word32));
assert (hashInt (-3876548352991) == (2_487_851_096 : Word32));