Skip to main content

inlean_runtime/
misc.rs

1//! Primitives that belong to no data type.
2
3use crate::object::{box_, Obj};
4
5/// The number of bits in a word of the target, as a `Nat`. Takes Lean's `Unit`.
6pub fn system_platform_nbits(_unit: Obj) -> Obj {
7    box_(usize::BITS as usize)
8}
9
10/// Lean's `panic!`. Lean's own runtime prints the message and continues with a default
11/// value; here the Rust program panics, so that a violated assumption cannot go unnoticed.
12pub unsafe fn panic_fn(_default: Obj, message: Obj) -> Obj {
13    panic!("{}", crate::string::text(message))
14}
15
16/// [`panic_fn`] for a default value the caller keeps.
17pub unsafe fn panic_fn_borrowed(default: Obj, message: Obj) -> Obj {
18    panic_fn(default, message)
19}
20
21/// The constructor index of a value, as a `Nat`.
22pub unsafe fn obj_tag_nat(o: Obj) -> Obj {
23    box_(crate::object::obj_tag(o) as usize)
24}
25
26/// Combines two hash values.
27pub fn uint64_mix_hash(mut h: u64, mut k: u64) -> u64 {
28    const M: u64 = 0xc6a4a7935bd1e995;
29    const R: u32 = 47;
30    k = k.wrapping_mul(M);
31    k ^= k >> R;
32    k ^= M;
33    h ^= k;
34    h = h.wrapping_mul(M);
35    h
36}
37
38/// The functions of this module that generated code may call, named as in `lean.h` without
39/// the `lean_` prefix.
40pub(crate) const OPS: &[&str] = &["system_platform_nbits", "panic_fn", "panic_fn_borrowed", "uint64_mix_hash", "obj_tag_nat"];