commit b5cd66c29256d3ab4ed670ecb321ac93a67d6902
parent 6b5863463084e563b626da50f4ea7a9aff0edb95
Author: Christian Ermann <christianermann@gmail.com>
Date: Wed, 4 Dec 2024 11:04:38 -0800
Refactor 'hash'
Diffstat:
1 file changed, 12 insertions(+), 16 deletions(-)
diff --git a/src/forth.s b/src/forth.s
@@ -1398,26 +1398,22 @@ defword "create", create, 0x26BB595D, flags=flag_immediate
jal l_bracket
exit
-defcode "hash", hash, 0xEDBF0FE3
- mv x, w
- pop w
- push_ret ra
- jal hash_impl
- pop_ret ra
- next
-
-hash_impl: # 32-bit fnv1a
+defcode "hash", hash, 0xEDBF0FE3 # 32-bit fnv1a
+ # ( addr len -- hash )
+ pop x
li t0, 2166136261 # hash
li t1, 16777619 # prime
-_hash_char:
- lb t2, 0(w)
- addi w, w, 1
- addi x, x, -1
- xor t0, t0, t2
+ blez w, _hash_done
+_hash_loop:
+ lb y, 0(x)
+ addi x, x, 1
+ addi w, w, -1
+ xor t0, t0, y
mul t0, t0, t1
- bgtz x, _hash_char
+ bgtz w, _hash_loop
+_hash_done:
mv w, t0
- ret
+ next
defword "[", l_bracket, 0xDE0C1FBA, flags=flag_immediate
# set state