fix random

implement hardware_random_u32
implement math.max, alter math.min
remove unneeded dependencies from fb_driver
This commit is contained in:
koniifer 2024-08-30 16:22:13 +01:00
parent 58bc6facbc
commit 12ee3a9b87
5 changed files with 40 additions and 7 deletions

View file

@ -226,6 +226,33 @@ pub fn hardware_random_u64() -> u64 {
} }
} }
pub fn hardware_random_u32() -> u32 {
use {log::trace, rdrand::RdRand};
let gen = RdRand::new();
match gen {
Ok(gen) => {
let ret = gen.try_next_u32().unwrap();
trace!("Random {}", ret);
return ret;
}
Err(err) => {
warn!("RDRand not supported.");
// Try rdseed
let gen = RdSeed::new();
match gen {
Ok(gen) => {
let ret = gen.try_next_u32().unwrap();
trace!("Random {}", ret);
return ret;
}
Err(err) => {
panic!("Neither RDRand or RDSeed are supported")
}
}
}
}
}
pub fn get_edid() {} pub fn get_edid() {}
pub fn register_dump() { pub fn register_dump() {

View file

@ -133,7 +133,7 @@ pub fn handler(vm: &mut Vm) {
} }
// source of rng // source of rng
4 => { 4 => {
vm.registers[1] = hbvm::value::Value(crate::arch::hardware_random_u64()); vm.registers[1] = hbvm::value::Value(crate::arch::hardware_random_u32() as u64);
} }
buffer_id => { buffer_id => {
let mut buffs = IPC_BUFFERS.lock(); let mut buffs = IPC_BUFFERS.lock();

View file

@ -1,7 +1,14 @@
SIZEOF_INT := 31
abs := fn(x: int): int { abs := fn(x: int): int {
mask := x >> 31 mask := x >> SIZEOF_INT
return (x ^ mask) - mask return (x ^ mask) - mask
} }
min := fn(a: int, b: int): int { min := fn(a: int, b: int): int {
return b + (a - b & a - b >> 31) c := a - b
return b + (c & c >> SIZEOF_INT)
}
max := fn(a: int, b: int): int {
c := a - b
return a - (c & c >> SIZEOF_INT)
} }

View file

@ -1,5 +1,5 @@
uint_64 := fn(min: uint, max: uint): uint { integer := fn(min: int, max: int): int {
rng := @eca(uint, 3, 4) rng := @eca(int, 3, 4)
if min != 0 | max != 0 { if min != 0 | max != 0 {
return rng % (max - min + 1) + min return rng % (max - min + 1) + min

View file

@ -1,5 +1,4 @@
.{log, memory, string, buffer} := @use("../../../libraries/stn/src/lib.hb"); .{log, memory, string, buffer} := @use("../../../libraries/stn/src/lib.hb")
.{front_buffer_ptr, screenidx, ColorBGRA, Point} := @use("./lib.hb")
main := fn(): int { main := fn(): int {
buffer_id := buffer.create("XGraphics\0") buffer_id := buffer.create("XGraphics\0")