From d4bfc2cd764d36dd6e38cc74914bb682f01e3d3a Mon Sep 17 00:00:00 2001 From: Amjad Alsharafi <26300843+Amjad50@users.noreply.github.com> Date: Sun, 18 Feb 2024 22:04:35 +0800 Subject: [PATCH 1/2] Lib: Added `libm` to `emerald_std` This adds the missing functions `rust/std` require that are normally available in `libc` We will update `rust` as well to use these new functions. We have modified `libm` in the PR: https://github.com/rust-lang/libm/pull/290 to enable building `std`. --- Cargo.lock | 12 +++++++++++- libraries/emerald_std/Cargo.toml | 7 +++++-- libraries/emerald_std/src/graphics.rs | 2 +- libraries/emerald_std/src/lib.rs | 1 + libraries/emerald_std/src/math.rs | 2 ++ 5 files changed, 20 insertions(+), 4 deletions(-) create mode 100644 libraries/emerald_std/src/math.rs diff --git a/Cargo.lock b/Cargo.lock index 994c90c1..a70b8622 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -69,11 +69,12 @@ dependencies = [ [[package]] name = "emerald_std" -version = "0.2.6" +version = "0.2.7" dependencies = [ "compiler_builtins", "emerald_kernel_user_link", "increasing_heap_allocator", + "libm", "rustc-std-workspace-alloc", "rustc-std-workspace-core", ] @@ -124,6 +125,15 @@ version = "1.4.0" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "e2abad23fbc42b3700f2f279844dc832adb2b2eb069b2df918f455c4e18cc646" +[[package]] +name = "libm" +version = "0.2.8" +source = "git+https://github.com/Amjad50/libm?branch=compiling_libm_for_std#9ded10304060e82d5cb71fcf8ae99a9538802c65" +dependencies = [ + "compiler_builtins", + "rustc-std-workspace-core", +] + [[package]] name = "micromath" version = "2.1.0" diff --git a/libraries/emerald_std/Cargo.toml b/libraries/emerald_std/Cargo.toml index e60f0bc2..870e8204 100644 --- a/libraries/emerald_std/Cargo.toml +++ b/libraries/emerald_std/Cargo.toml @@ -1,6 +1,6 @@ [package] name = "emerald_std" -version = "0.2.6" +version = "0.2.7" edition = "2021" readme = "README.md" authors = ["Amjad Alsharafi"] @@ -15,6 +15,8 @@ categories = ["os"] [dependencies] increasing_heap_allocator = { version="0.1.3", path = "../increasing_heap_allocator" } kernel_user_link = { version="0.2.4", path = "../kernel_user_link", package = "emerald_kernel_user_link" } +# for now, waiting for the PR https://github.com/rust-lang/libm/pull/290 to be merged +libm = { version = "0.2.8", git = "https://github.com/Amjad50/libm", branch = "compiling_libm_for_std" } core = { version = "1.0.0", optional = true, package = "rustc-std-workspace-core" } compiler_builtins = { version = "0.1.2", optional = true } @@ -27,5 +29,6 @@ rustc-dep-of-std = [ "alloc", "compiler_builtins/mem", "kernel_user_link/rustc-dep-of-std", - "increasing_heap_allocator/rustc-dep-of-std" + "increasing_heap_allocator/rustc-dep-of-std", + "libm/rustc-dep-of-std" ] diff --git a/libraries/emerald_std/src/graphics.rs b/libraries/emerald_std/src/graphics.rs index a6c44532..ee4ec79f 100644 --- a/libraries/emerald_std/src/graphics.rs +++ b/libraries/emerald_std/src/graphics.rs @@ -77,7 +77,7 @@ pub fn get_framebuffer_info() -> Result { unsafe { Ok(info.assume_init()) } } -pub fn blit(command: &BlitCommand) -> Result<(), SyscallError> { +pub fn blit(command: &BlitCommand<'_>) -> Result<(), SyscallError> { if !command.is_buffer_valid() { return Err(SyscallError::InvalidGraphicsBuffer); } diff --git a/libraries/emerald_std/src/lib.rs b/libraries/emerald_std/src/lib.rs index 290586d0..ec9cede9 100644 --- a/libraries/emerald_std/src/lib.rs +++ b/libraries/emerald_std/src/lib.rs @@ -4,6 +4,7 @@ pub mod alloc; pub mod clock; pub mod graphics; pub mod io; +pub mod math; pub mod process; mod sync; diff --git a/libraries/emerald_std/src/math.rs b/libraries/emerald_std/src/math.rs new file mode 100644 index 00000000..d301a7eb --- /dev/null +++ b/libraries/emerald_std/src/math.rs @@ -0,0 +1,2 @@ +//! Re-export of the `libm` crate. +pub use libm::*; From bb76d3aeac03cfc836db4bd2fd3c0a5854e73045 Mon Sep 17 00:00:00 2001 From: Amjad Alsharafi <26300843+Amjad50@users.noreply.github.com> Date: Fri, 23 Feb 2024 20:10:46 +0800 Subject: [PATCH 2/2] Lib+Extern: (temp) Added `weak` linking to math libraries This is clunky, as in `std`, we are using a relative path to `emerald_std`, and I don't really like that. But just to get this done, since the PRs are very slow. Here, we are using unstable feature to add all the math symbols, but with `weak` linking, so that the linker won't panic when it finds two of them defined (as some of them are already defined). --- extern/rust | 2 +- libraries/emerald_std/src/lib.rs | 5 ++ libraries/emerald_std/src/math.rs | 101 +++++++++++++++++++++++++++++- 3 files changed, 105 insertions(+), 3 deletions(-) diff --git a/extern/rust b/extern/rust index 06643913..942910c8 160000 --- a/extern/rust +++ b/extern/rust @@ -1 +1 @@ -Subproject commit 06643913cbbf6ab1a7cb5e74db826fdc8995d27b +Subproject commit 942910c851386a1b319494b5511281c253732ff9 diff --git a/libraries/emerald_std/src/lib.rs b/libraries/emerald_std/src/lib.rs index ec9cede9..bb72e6e4 100644 --- a/libraries/emerald_std/src/lib.rs +++ b/libraries/emerald_std/src/lib.rs @@ -1,4 +1,9 @@ #![no_std] +// used to weak link libm functions +// this is temporary, waiting for PRs to be merged +// into `compiler-builtins` +// https://github.com/rust-lang/compiler-builtins/pull/577 +#![feature(linkage)] pub mod alloc; pub mod clock; diff --git a/libraries/emerald_std/src/math.rs b/libraries/emerald_std/src/math.rs index d301a7eb..2c69b696 100644 --- a/libraries/emerald_std/src/math.rs +++ b/libraries/emerald_std/src/math.rs @@ -1,2 +1,99 @@ -//! Re-export of the `libm` crate. -pub use libm::*; +#[allow(unused_macros)] +macro_rules! no_mangle { + ($(fn $fun:ident($($iid:ident : $ity:ty),+) -> $oty:ty;)+) => { + $( + #[linkage="weak"] + #[no_mangle] + pub unsafe extern "C" fn $fun($($iid: $ity),+) -> $oty { + libm::$fun($($iid),+) + } + )+ + } +} + +no_mangle! { + fn acos(n: f64) -> f64; + fn acosf(n: f32) -> f32; + fn asin(n: f64) -> f64; + fn asinf(n: f32) -> f32; + fn atan(n: f64) -> f64; + fn atan2(a: f64, b: f64) -> f64; + fn atan2f(a: f32, b: f32) -> f32; + fn atanf(n: f32) -> f32; + fn cbrt(n: f64) -> f64; + fn cbrtf(n: f32) -> f32; + fn cosh(n: f64) -> f64; + fn coshf(n: f32) -> f32; + fn expm1(n: f64) -> f64; + fn expm1f(n: f32) -> f32; + fn fdim(a: f64, b: f64) -> f64; + fn fdimf(a: f32, b: f32) -> f32; + fn hypot(x: f64, y: f64) -> f64; + fn hypotf(x: f32, y: f32) -> f32; + fn log1p(n: f64) -> f64; + fn log1pf(n: f32) -> f32; + fn sinh(n: f64) -> f64; + fn sinhf(n: f32) -> f32; + fn tan(n: f64) -> f64; + fn tanf(n: f32) -> f32; + fn tanh(n: f64) -> f64; + fn tanhf(n: f32) -> f32; + fn tgamma(n: f64) -> f64; + fn tgammaf(n: f32) -> f32; + // fn lgamma_r(n: f64, s: &mut i32) -> f64; + // fn lgammaf_r(n: f32, s: &mut i32) -> f32; + fn cos(x: f64) -> f64; + fn expf(x: f32) -> f32; + fn log2(x: f64) -> f64; + fn log2f(x: f32) -> f32; + fn log10(x: f64) -> f64; + fn log10f(x: f32) -> f32; + fn log(x: f64) -> f64; + fn logf(x: f32) -> f32; + fn fmin(x: f64, y: f64) -> f64; + fn fminf(x: f32, y: f32) -> f32; + fn fmax(x: f64, y: f64) -> f64; + fn fmaxf(x: f32, y: f32) -> f32; + fn round(x: f64) -> f64; + fn roundf(x: f32) -> f32; + fn rint(x: f64) -> f64; + fn rintf(x: f32) -> f32; + fn sin(x: f64) -> f64; + fn pow(x: f64, y: f64) -> f64; + fn powf(x: f32, y: f32) -> f32; + fn fmod(x: f64, y: f64) -> f64; + fn fmodf(x: f32, y: f32) -> f32; + fn ldexp(f: f64, n: i32) -> f64; + fn ldexpf(f: f32, n: i32) -> f32; + fn cosf(x: f32) -> f32; + fn exp(x: f64) -> f64; + fn sinf(x: f32) -> f32; + fn exp2(x: f64) -> f64; + fn exp2f(x: f32) -> f32; + fn fma(x: f64, y: f64, z: f64) -> f64; + fn fmaf(x: f32, y: f32, z: f32) -> f32; + fn sqrtf(x: f32) -> f32; + fn sqrt(x: f64) -> f64; + fn ceil(x: f64) -> f64; + fn ceilf(x: f32) -> f32; + fn floor(x: f64) -> f64; + fn floorf(x: f32) -> f32; + fn trunc(x: f64) -> f64; + fn truncf(x: f32) -> f32; +} + +#[linkage = "weak"] +#[no_mangle] +pub unsafe extern "C" fn lgamma_r(x: f64, s: &mut i32) -> f64 { + let r = libm::lgamma_r(x); + *s = r.1; + r.0 +} + +#[linkage = "weak"] +#[no_mangle] +pub unsafe extern "C" fn lgammaf_r(x: f32, s: &mut i32) -> f32 { + let r = libm::lgammaf_r(x); + *s = r.1; + r.0 +}