Skip to content

Commit

Permalink
fix
Browse files Browse the repository at this point in the history
  • Loading branch information
funnsam committed Jul 3, 2024
1 parent d7d9f7f commit 886017d
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 4 deletions.
2 changes: 1 addition & 1 deletion as.sh
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
#!/bin/sh

riscv64-unknown-elf-as $1 -march=rv64ima -o program.o
riscv64-unknown-elf-as $1 -march=rv64g -o program.o
riscv64-unknown-elf-objcopy program.o -O binary program.bin
15 changes: 12 additions & 3 deletions emu/src/cpu/float.rs
Original file line number Diff line number Diff line change
Expand Up @@ -45,25 +45,31 @@ impl<'a> Cpu<'a> {
}
}

#[inline(always)]
pub(crate) fn check_fpu(&mut self) {
core::hint::black_box(Self::_check_fpu)(self);
}

fn _check_fpu(&mut self) {
unsafe {
let f = (fenv::fetestexcept(fenv::FE_INVALID as _) != 0) as u64 * NV
| (fenv::fetestexcept(fenv::FE_DIVBYZERO as _) != 0) as u64 * DZ
| (fenv::fetestexcept(fenv::FE_INEXACT as _) != 0) as u64 * NX
| (fenv::fetestexcept(fenv::FE_OVERFLOW as _) != 0) as u64 * OF
| (fenv::fetestexcept(fenv::FE_UNDERFLOW as _) != 0) as u64 * UF;
self.float_set_flags(f);
println!("{f:02x}");
}
}

pub(crate) fn float_do_op<T, F: Fn(&mut Self) -> T>(&mut self, f: F) -> T {
unsafe { fenv::feclearexcept(fenv::FE_ALL_EXCEPT as _); }
core::hint::black_box(|| unsafe { fenv::feclearexcept(fenv::FE_ALL_EXCEPT as _); })();
let v = f(self);
v
}

pub(crate) fn float_do_op_f32<F: Fn() -> f32>(&mut self, f: F) -> f32 {
unsafe { fenv::feclearexcept(fenv::FE_ALL_EXCEPT as _); }
core::hint::black_box(|| unsafe { fenv::feclearexcept(fenv::FE_ALL_EXCEPT as _); })();
let v = f();
self.check_fpu();
if v.is_nan() {
Expand All @@ -74,7 +80,7 @@ impl<'a> Cpu<'a> {
}

pub(crate) fn float_do_op_f64<F: Fn() -> f64>(&mut self, f: F) -> f64 {
unsafe { fenv::feclearexcept(fenv::FE_ALL_EXCEPT as _); }
core::hint::black_box(|| unsafe { fenv::feclearexcept(fenv::FE_ALL_EXCEPT as _); })();
let v = f();
self.check_fpu();
if v.is_nan() {
Expand Down Expand Up @@ -117,6 +123,9 @@ macro_rules! cast {
($s: tt $v: tt $f: tt $t: tt s) => {{
let v = if $v.is_nan() { $t::MAX } else { $v as $t };
$s.check_fpu();
if v as $f != $v.trunc() {
$s.float_set_flags($crate::cpu::float::NV);
}
v
}};
($s: tt $v: tt $f: tt $t: tt u) => {{
Expand Down

0 comments on commit 886017d

Please sign in to comment.