From 401a8229c643a2b39c69d9dd91b09351e7af2dc7 Mon Sep 17 00:00:00 2001 From: Alexander Koz Date: Sun, 11 Aug 2024 22:52:12 +0400 Subject: [PATCH] add runtime target-features test crate, bump `cargo-playdate` (following #409) --- Cargo.lock | 29 ++++- Cargo.toml | 2 +- cargo/Cargo.toml | 2 +- examples/rt-target-test/Cargo.toml | 43 +++++++ examples/rt-target-test/src/lib.rs | 176 +++++++++++++++++++++++++++++ 5 files changed, 249 insertions(+), 3 deletions(-) create mode 100644 examples/rt-target-test/Cargo.toml create mode 100644 examples/rt-target-test/src/lib.rs diff --git a/Cargo.lock b/Cargo.lock index babc13bb..e65bf34b 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -729,7 +729,7 @@ dependencies = [ [[package]] name = "cargo-playdate" -version = "0.5.3" +version = "0.5.4" dependencies = [ "anstyle", "anyhow", @@ -1613,6 +1613,21 @@ version = "1.3.0" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "42703706b716c37f96a77aea830392ad231f44c9e9a67872fa5548707e11b11c" +[[package]] +name = "function_name" +version = "0.3.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "b1ab577a896d09940b5fe12ec5ae71f9d8211fff62c919c03a3750a9901e98a7" +dependencies = [ + "function_name-proc-macro", +] + +[[package]] +name = "function_name-proc-macro" +version = "0.3.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "673464e1e314dd67a0fd9544abc99e8eb28d0c7e3b69b033bcff9b2d00b87333" + [[package]] name = "futf" version = "0.1.5" @@ -4614,6 +4629,18 @@ dependencies = [ "subtle", ] +[[package]] +name = "rt-target-test" +version = "0.0.0" +dependencies = [ + "function_name", + "libm", + "num-traits", + "playdate-display", + "playdate-sys", + "playdate-system", +] + [[package]] name = "rusqlite" version = "0.31.0" diff --git a/Cargo.toml b/Cargo.toml index d372b816..d1c46103 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -1,6 +1,6 @@ [workspace] resolver = "2" -members = ["cargo", "api/*", "support/*", "components/*"] +members = ["cargo", "api/*", "support/*", "components/*", "examples/*"] default-members = ["cargo", "support/tool", "support/bindgen"] exclude = ["cargo/tests/crates/**/*"] diff --git a/cargo/Cargo.toml b/cargo/Cargo.toml index 614b913c..acb02af5 100644 --- a/cargo/Cargo.toml +++ b/cargo/Cargo.toml @@ -1,6 +1,6 @@ [package] name = "cargo-playdate" -version = "0.5.3" +version = "0.5.4" readme = "README.md" description = "Build tool for neat yellow console." keywords = ["playdate", "build", "cargo", "plugin", "cargo-subcommand"] diff --git a/examples/rt-target-test/Cargo.toml b/examples/rt-target-test/Cargo.toml new file mode 100644 index 00000000..0cf03c97 --- /dev/null +++ b/examples/rt-target-test/Cargo.toml @@ -0,0 +1,43 @@ +[package] +name = "rt-target-test" +version = "0.0.0" +publish = false +description = "Runtime test target features - FPU/VFP, SIMD, etc.." +edition.workspace = true +license.workspace = true +authors.workspace = true +homepage.workspace = true +repository.workspace = true + + +[lib] +crate-type = ["dylib", "staticlib"] + + +[dependencies] +libm = "0.2" +function_name = "0.3" + +[dependencies.num-traits] +version = "0.2" +features = ["libm"] +default-features = false + + +[dependencies.display] +workspace = true +default-features = false + +[dependencies.system] +workspace = true +features = ["try-trait-v2"] +default-features = false + +[dependencies.sys] +workspace = true +features = ["lang-items", "entry-point"] +default-features = false + + +[package.metadata.playdate] +bundle-id = "rs.playdate.rt-target-test" diff --git a/examples/rt-target-test/src/lib.rs b/examples/rt-target-test/src/lib.rs new file mode 100644 index 00000000..5de9a6c8 --- /dev/null +++ b/examples/rt-target-test/src/lib.rs @@ -0,0 +1,176 @@ +#![no_std] +#![feature(repr_simd)] +#![feature(core_intrinsics)] +#![allow(internal_features)] + +extern crate alloc; + +#[macro_use] +extern crate function_name; + +#[macro_use] +extern crate sys; + +use core::ptr::NonNull; +use display::Display; +use sys::EventLoopCtrl; +use sys::ffi::PlaydateAPI; +use system::prelude::*; + + +#[no_mangle] +fn event_handler(_: NonNull, event: SystemEvent, _: u32) -> EventLoopCtrl { + if matches!(event, SystemEvent::Init) { + Display::Default().set_refresh_rate(1.); + System::Default().set_update_callback_static(Some(on_update), Default::default()); + } + + EventLoopCtrl::Continue +} + + +fn on_update(frame: &mut usize) -> UpdateCtrl { + match *frame { + 0 => dummy_test(), + + 1 => simd::i32(), + 2 => simd::f32(), + + 3 => fp32::core(), + 4 => fp32::libm(), + 5 => fp32::num_traits(), + + 6 => fp64::core(), + 7 => fp64::libm(), + 8 => fp64::num_traits(), + + 9 => tests_complete(), + _ => System::Default().set_update_callback_static(None, ()), + } + + *frame += 1; + UpdateCtrl::Continue +} + + +fn dummy_test() { println!("init: OK") } +fn tests_complete() { println!("all tests: OK") } + +macro_rules! print_test_name { + () => { + println!("test {}", concat!(module_path!(), "::", function_name!())); + }; + ($msg:literal) => { + println!("test {}: {}", + concat!(module_path!(), "::", function_name!()), + $msg); + }; +} + + +pub mod fp32 { + const F: f32 = 1.4; + + #[named] + pub fn core() { + print_test_name!(); + let sin = unsafe { core::intrinsics::sinf32(F) }; + let cos = unsafe { core::intrinsics::cosf32(F) }; + println!("cos: {cos}"); + println!("sin: {sin}"); + print_test_name!("OK"); + } + + #[named] + pub fn libm() { + print_test_name!(); + let (sin, cos) = libm::sincosf(F); + println!("cos: {cos}"); + println!("sin: {sin}"); + print_test_name!("OK"); + } + + #[named] + pub fn num_traits() { + print_test_name!(); + let (sin, cos) = num_traits::Float::sin_cos(F); + println!("cos: {cos}"); + println!("sin: {sin}"); + print_test_name!("OK"); + } +} + +pub mod fp64 { + const F: f64 = 1.4; + + #[named] + pub fn core() { + print_test_name!(); + println!("cos: {}", unsafe { core::intrinsics::cosf64(F) }); + println!("sin: {}", unsafe { core::intrinsics::sinf64(F) }); + print_test_name!("OK"); + } + + #[named] + pub fn libm() { + print_test_name!(); + let (sin, cos) = libm::sincos(F); + println!("cos: {cos}"); + println!("sin: {sin}"); + print_test_name!("OK"); + } + + #[named] + pub fn num_traits() { + print_test_name!(); + let (sin, cos) = num_traits::real::Real::sin_cos(F); + println!("cos: {cos}"); + println!("sin: {sin}"); + print_test_name!("OK"); + } +} + + +pub mod simd { + use core::intrinsics::simd::simd_add; + + #[repr(simd)] + #[derive(Clone, Copy, Debug)] + struct Simd4(T, T, T, T); + + + #[named] + pub fn i32() { + print_test_name!(); + unsafe { + let a = Simd4(10, 10, 10, 10); + let b = Simd4(1, 2, 3, 4); + let mut res = Simd4(0, 0, 0, 0); + + for _ in 0..101 { + res = simd_add(a, simd_add(b, res)); + } + println!("res: {res:?}"); + } + print_test_name!("OK"); + } + + #[named] + pub fn f32() { + print_test_name!(); + unsafe { + let a = Simd4::(10.1, 10.1, 10.1, 10.1); + let b = Simd4(1.1, 2.1, 3.1, 4.1); + let mut res = Simd4(0.0, 0.0, 0.0, 0.0); + + for _ in 0..101 { + res = simd_add(a, simd_add(b, res)); + } + println!("res: {res:?}"); + } + print_test_name!("OK"); + } +} + + +ll_symbols!();