Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: mark psp::math functions as safe #134

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
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
136 changes: 72 additions & 64 deletions psp/src/math/mod.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
#[no_mangle]
pub unsafe extern "C" fn fminf(x: f32, y: f32) -> f32 {
pub extern "C" fn fminf(x: f32, y: f32) -> f32 {
let out: f32;
if x.is_nan() && !y.is_nan() {
out = y;
Expand All @@ -8,28 +8,30 @@ pub unsafe extern "C" fn fminf(x: f32, y: f32) -> f32 {
} else if x.is_nan() && y.is_nan() {
out = core::f32::NAN;
} else {
vfpu_asm! (
"mfc1 {tmp1}, {x}",
"mfc1 {tmp2}, {y}",
"mtv {tmp1}, S000",
"mtv {tmp2}, S001",
"vmin.s S000, S000, S001",
"mfv {tmp1}, S000",
"mtc1 {tmp1}, {out}",
"nop",
x = in(freg) x,
y = in(freg) y,
tmp1 = out(reg) _,
tmp2 = out(reg) _,
out = out(freg) out,
options(nostack, nomem),
);
unsafe {
vfpu_asm! (
"mfc1 {tmp1}, {x}",
"mfc1 {tmp2}, {y}",
"mtv {tmp1}, S000",
"mtv {tmp2}, S001",
"vmin.s S000, S000, S001",
"mfv {tmp1}, S000",
"mtc1 {tmp1}, {out}",
"nop",
x = in(freg) x,
y = in(freg) y,
tmp1 = out(reg) _,
tmp2 = out(reg) _,
out = out(freg) out,
options(nostack, nomem),
);
}
}
out
}

#[no_mangle]
pub unsafe extern "C" fn fmaxf(x: f32, y: f32) -> f32 {
pub extern "C" fn fmaxf(x: f32, y: f32) -> f32 {
let out: f32;
if x.is_nan() && !y.is_nan() {
out = y;
Expand All @@ -38,63 +40,69 @@ pub unsafe extern "C" fn fmaxf(x: f32, y: f32) -> f32 {
} else if x.is_nan() && y.is_nan() {
out = core::f32::NAN;
} else {
vfpu_asm! (
"mfc1 {tmp1}, {x}",
"mfc1 {tmp2}, {y}",
"mtv {tmp1}, S000",
"mtv {tmp2}, S001",
"vmax.s S000, S000, S001",
"mfv {tmp1}, S000",
"mtc1 {tmp1}, {out}",
"nop",
x = in(freg) x,
y = in(freg) y,
tmp1 = out(reg) _,
tmp2 = out(reg) _,
out = out(freg) out,
options(nostack, nomem),
);
unsafe {
vfpu_asm! (
"mfc1 {tmp1}, {x}",
"mfc1 {tmp2}, {y}",
"mtv {tmp1}, S000",
"mtv {tmp2}, S001",
"vmax.s S000, S000, S001",
"mfv {tmp1}, S000",
"mtc1 {tmp1}, {out}",
"nop",
x = in(freg) x,
y = in(freg) y,
tmp1 = out(reg) _,
tmp2 = out(reg) _,
out = out(freg) out,
options(nostack, nomem),
);
}
}
out
}

#[no_mangle]
pub unsafe extern "C" fn cosf(scalar: f32) -> f32 {
pub extern "C" fn cosf(scalar: f32) -> f32 {
let out: f32;
vfpu_asm! (
"mfc1 {tmp}, {scalar}",
"mtv {tmp}, S000",
"nop",
"vcst.s S001, VFPU_2_PI",
"vmul.s S000, S000, S001",
"vcos.s S000, S000",
"mfv {tmp}, S000",
"mtc1 {tmp}, {scalar}",
"nop",
scalar = inlateout(freg) scalar => out,
tmp = out(reg) _,
options(nostack, nomem),
);
unsafe {
vfpu_asm! (
"mfc1 {tmp}, {scalar}",
"mtv {tmp}, S000",
"nop",
"vcst.s S001, VFPU_2_PI",
"vmul.s S000, S000, S001",
"vcos.s S000, S000",
"mfv {tmp}, S000",
"mtc1 {tmp}, {scalar}",
"nop",
scalar = inlateout(freg) scalar => out,
tmp = out(reg) _,
options(nostack, nomem),
);
}
out
}

#[no_mangle]
pub unsafe extern "C" fn sinf(scalar: f32) -> f32 {
pub extern "C" fn sinf(scalar: f32) -> f32 {
let out: f32;
vfpu_asm! (
"mfc1 {tmp}, {scalar}",
"mtv {tmp}, S000",
"nop",
"vcst.s S001, VFPU_2_PI",
"vmul.s S000, S000, S001",
"vsin.s S000, S000",
"mfv {tmp}, S000",
"mtc1 {tmp}, {scalar}",
"nop",
scalar = inlateout(freg) scalar => out,
tmp = out(reg) _,
options(nostack, nomem),
);
unsafe {
vfpu_asm! (
"mfc1 {tmp}, {scalar}",
"mtv {tmp}, S000",
"nop",
"vcst.s S001, VFPU_2_PI",
"vmul.s S000, S000, S001",
"vsin.s S000, S000",
"mfv {tmp}, S000",
"mtc1 {tmp}, {scalar}",
"nop",
scalar = inlateout(freg) scalar => out,
tmp = out(reg) _,
options(nostack, nomem),
);
}
out
}
// borrowed from https://github.com/samcrow/cmsis_dsp.rs/blob/master/src/libm_c.rs
Expand Down