From e0560a2db9b7a1a29a47e7ebe4de668763e36b2d Mon Sep 17 00:00:00 2001 From: Sebastien Bourdeauducq Date: Tue, 21 Jul 2020 13:50:33 +0800 Subject: [PATCH] expose libm functions to kernel --- default.nix | 2 +- src/Cargo.lock | 7 +++++++ src/runtime/Cargo.toml | 1 + src/runtime/src/kernel/api.rs | 29 +++++++++++++++++++++++++++++ 4 files changed, 38 insertions(+), 1 deletion(-) diff --git a/default.nix b/default.nix index 67ba91fe..52d46ce8 100644 --- a/default.nix +++ b/default.nix @@ -14,7 +14,7 @@ let version = "0.1.0"; src = ./src; - cargoSha256 = "11kd1vmm4i4plqwxbsgrxia1ihs2szw1r7af8j645zib71f0qx87"; + cargoSha256 = "04asmk07xc7asbpl15d9pgm31dd77lj9hkfgf76kcq6ds7l04k12"; nativeBuildInputs = [ pkgs.gnumake diff --git a/src/Cargo.lock b/src/Cargo.lock index bd5c453e..eb1b03a1 100644 --- a/src/Cargo.lock +++ b/src/Cargo.lock @@ -242,6 +242,12 @@ dependencies = [ "libregister", ] +[[package]] +name = "libm" +version = "0.2.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "c7d73b3f436185384286bd8098d17ec07c9a7d2388a6599f824d8502b529702a" + [[package]] name = "libregister" version = "0.0.0" @@ -412,6 +418,7 @@ dependencies = [ "libboard_zynq", "libc", "libcortex_a9", + "libm", "libregister", "libsupport_zynq", "log", diff --git a/src/runtime/Cargo.toml b/src/runtime/Cargo.toml index 91173f01..e8dc899e 100644 --- a/src/runtime/Cargo.toml +++ b/src/runtime/Cargo.toml @@ -23,6 +23,7 @@ futures = { version = "0.3", default-features = false, features = ["async-await" async-recursion = "0.3" fatfs = { version = "0.3", features = ["core_io"], default-features = false } log_buffer = { version = "1.2" } +libm = { version = "0.2", features = ["unstable"] } libboard_zynq = { git = "https://git.m-labs.hk/M-Labs/zc706.git" } libsupport_zynq = { default-features = false, git = "https://git.m-labs.hk/M-Labs/zc706.git" } diff --git a/src/runtime/src/kernel/api.rs b/src/runtime/src/kernel/api.rs index aba60193..1a63a608 100644 --- a/src/runtime/src/kernel/api.rs +++ b/src/runtime/src/kernel/api.rs @@ -1,3 +1,5 @@ +use libm; + use crate::eh_artiq; use crate::rtio; use super::rpc::{rpc_send, rpc_send_async, rpc_recv}; @@ -16,6 +18,15 @@ macro_rules! api { } } +macro_rules! api_libm_f64f64 { + ($i:ident) => ({ + extern fn $i(x: f64) -> f64 { + libm::$i(x) + } + api!($i = $i) + }) +} + pub fn resolve(required: &[u8]) -> Option { let api = &[ // timing @@ -117,14 +128,32 @@ pub fn resolve(required: &[u8]) -> Option { api!(__aeabi_memclr8), api!(__aeabi_memclr4), api!(__aeabi_memclr), + // libc api!(memcmp, extern { fn memcmp(a: *const u8, b: *mut u8, size: usize); }), + // exceptions api!(_Unwind_Resume = unwind::_Unwind_Resume), api!(__artiq_personality = eh_artiq::artiq_personality), api!(__artiq_raise = eh_artiq::raise), api!(__artiq_reraise = eh_artiq::reraise), + // libm + api_libm_f64f64!(log), + api_libm_f64f64!(log10), + api_libm_f64f64!(exp), + { + extern fn pow(x: f64, y: f64) -> f64 { + libm::pow(x, y) + } + api!(pow = pow) + }, + api_libm_f64f64!(sin), + api_libm_f64f64!(cos), + api_libm_f64f64!(tan), + api_libm_f64f64!(asin), + api_libm_f64f64!(acos), + api_libm_f64f64!(atan), ]; api.iter() .find(|&&(exported, _)| exported.as_bytes() == required)