Skip to content

Commit

Permalink
add runtime target-features test crate (following #409)
Browse files Browse the repository at this point in the history
  • Loading branch information
boozook committed Aug 11, 2024
1 parent 23b3e44 commit bb92f8f
Show file tree
Hide file tree
Showing 4 changed files with 247 additions and 1 deletion.
27 changes: 27 additions & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
@@ -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/**/*"]

Expand Down
43 changes: 43 additions & 0 deletions examples/rt-target-test/Cargo.toml
Original file line number Diff line number Diff line change
@@ -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"
176 changes: 176 additions & 0 deletions examples/rt-target-test/src/lib.rs
Original file line number Diff line number Diff line change
@@ -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<PlaydateAPI>, 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, 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::<f32>(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!();

0 comments on commit bb92f8f

Please sign in to comment.