Skip to content

Commit

Permalink
Merge pull request #77 from jedbrown/jed/no-std
Browse files Browse the repository at this point in the history
restore no_std after diman_lib was split out
  • Loading branch information
Tehforsch authored Sep 1, 2024
2 parents 90bd201 + d5d973d commit 7db1f67
Show file tree
Hide file tree
Showing 9 changed files with 40 additions and 25 deletions.
6 changes: 5 additions & 1 deletion .github/workflows/rust.yml
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,8 @@ jobs:
uses: actions/checkout@v4
- name: Install latest nightly
uses: dtolnay/rust-toolchain@nightly
with:
target: nvptx64-nvidia-cuda
- name: Install hdf5 libraries
run: |
sudo apt-get update
Expand All @@ -30,10 +32,12 @@ jobs:
sudo apt-get install mpich
- name: Build
run: cargo build --verbose --all-targets
- name: Build with no_std on nvptx64-nvidia-cuda
run: cargo build --target nvptx64-nvidia-cuda --no-default-features --features f32,f64
- name: Run tests (default features)
run: cargo test --tests --workspace
- name: Run tests (all features, no rational dimensions)
run: cargo test --tests --features glam,glam-vec2,glam-dvec2,glam-vec3,glam-dvec3,f32,f64,gen-vec-names,si,mpi,hdf5,rand,serde --workspace
run: cargo test --tests --features glam,glam-vec2,glam-dvec2,glam-vec3,glam-dvec3,f32,f64,si,mpi,hdf5,rand,serde --workspace
- name: Run tests (all features)
run: cargo test --tests --all-features --workspace
- name: Run tests (no std, no libm)
Expand Down
6 changes: 2 additions & 4 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -14,16 +14,15 @@ readme = "README.md"
members = ["crates/*"]

[features]
num-traits-libm = ["num-traits/libm", "diman_unit_system/num-traits-libm"]
num-traits-libm = ["diman_unit_system/num-traits-libm", "diman_lib/num-traits-libm"]
glam = ["dep:glam", "diman_unit_system/glam"]
glam-vec2 = ["glam", "f32", "diman_unit_system/glam-vec2"]
glam-dvec2 = ["glam", "f64", "diman_unit_system/glam-dvec2"]
glam-vec3 = ["glam", "f32", "diman_unit_system/glam-vec3"]
glam-dvec3 = ["glam", "f64", "diman_unit_system/glam-dvec3"]
f32 = ["diman_unit_system/f32"]
f64 = ["diman_unit_system/f64"]
gen-vec-names = ["diman_unit_system/gen-vec-names"]
std = ["diman_unit_system/std"]
std = ["diman_unit_system/std", "diman_lib/std"]
si = []
rational-dimensions = ["diman_unit_system/rational-dimensions"]

Expand All @@ -42,7 +41,6 @@ serde = { version = "1.0.193", features = ["derive"], optional = true }
hdf5 = { package = "hdf5-metno", version = "0.9.0", optional = true}
mpi = { version = "0.7", default-features = false, features = ["derive"], optional = true }
once_cell = { version = "1.18.0", optional = true }
num-traits = { version = "0.2.17", default-features = false }

diman_unit_system = { path = "crates/diman_unit_system", version = "0.5", default-features = false }
diman_lib = { path = "crates/diman_lib", version = "0.5" }
Expand Down
5 changes: 5 additions & 0 deletions crates/diman_lib/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -9,4 +9,9 @@ description = "Commonly used types for internal use in diman."
license = "MIT OR Apache-2.0"
repository = "https://github.com/tehforsch/diman"

[features]
std = []
num-traits-libm = ["num-traits/libm"]

[dependencies]
num-traits = { version = "0.2.17", default-features = false }
7 changes: 7 additions & 0 deletions crates/diman_lib/src/dimension_exponent.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,13 @@ use core::ops::{AddAssign, Mul, Neg};

use crate::magnitude::Magnitude;

// For some reason, this use statement is never recognized as used
// even when I build the crates with no std, where removing the use
// statement means that powi below cannot be used.
#[allow(unused)]
#[cfg(not(any(feature = "std")))]
use num_traits::float::FloatCore;

pub trait DimensionExponent: Clone + PartialEq + Copy + Mul + AddAssign + Neg {
fn float_pow(mag: Magnitude, exponent: Self) -> Magnitude;
fn one() -> Self;
Expand Down
9 changes: 9 additions & 0 deletions crates/diman_lib/src/lib.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,16 @@
#![cfg_attr(not(feature = "std"), no_std)]
#![allow(incomplete_features)]
#![feature(generic_const_exprs, adt_const_params)]

pub mod dimension_exponent;
pub mod magnitude;
#[cfg(any(feature = "std", feature = "num-traits-libm"))]
pub mod ratio;
pub mod runtime_unit_storage;

pub mod num_traits_reexport {
#[cfg(feature = "num-traits-libm")]
pub use num_traits::float::Float;
#[cfg(not(feature = "num-traits-libm"))]
pub use num_traits::float::FloatCore;
}
7 changes: 7 additions & 0 deletions crates/diman_lib/src/magnitude.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,12 @@ use core::{
ops::{Div, Mul},
};

#[cfg(feature = "num-traits-libm")]
pub use num_traits::float::Float;

#[cfg(not(feature = "num-traits-libm"))]
pub use num_traits::float::FloatCore;

pub const MAX_NUM_FACTORS: usize = 10;

#[derive(Copy, Clone, PartialEq, Eq, Debug, ConstParamTy)]
Expand Down Expand Up @@ -45,6 +51,7 @@ impl Magnitude {
self.into_f64() as f32
}

#[cfg(any(feature = "std", feature = "num-traits-libm"))]
pub fn pow_rational(&self, num: i64, denom: i64) -> Magnitude {
Self::from_f64(self.into_f64().powf(num as f64 / denom as f64))
}
Expand Down
5 changes: 2 additions & 3 deletions crates/diman_unit_system/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -21,10 +21,9 @@ mpi = []
serde = []
rand = []
hdf5 = []
gen-vec-names = []
rational-dimensions = []
num-traits-libm = []
std = []
num-traits-libm = ["diman_lib/num-traits-libm"]
std = ["diman_lib/std"]

default = ["f32", "f64"]

Expand Down
12 changes: 1 addition & 11 deletions crates/diman_unit_system/src/codegen/float_methods.rs
Original file line number Diff line number Diff line change
Expand Up @@ -111,17 +111,7 @@ impl Codegen {
}
};
#[cfg(all(not(feature = "std"), not(feature = "num-traits-libm")))]
let roots = quote! {
pub fn sqrt(&self) -> #quantity_type<#float_type, { D.div_2() }>
{
#quantity_type::<#float_type, { D.div_2() }>(self.0.sqrt())
}

pub fn cbrt(&self) -> #quantity_type<#float_type, { D.div_3() }>
{
#quantity_type::<#float_type, { D.div_3() }>(self.0.cbrt())
}
};
let roots = quote! {};

quote! {
impl<const D: #dimension_type> #quantity_type<#float_type, D> {
Expand Down
8 changes: 2 additions & 6 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -99,9 +99,11 @@
//! let length = 2.0f64 * meters;
//! let area = length.squared();
//! assert_eq!(area, 4.0 * square_meters);
//! # #[cfg(any(feature = "std", feature = "num-traits-libm"))]
//! assert_eq!(area.sqrt(), length);
//! let vol = length.cubed();
//! assert_eq!(vol, 8.0 * cubic_meters);
//! # #[cfg(any(feature = "std", feature = "num-traits-libm"))]
//! assert_eq!(vol.cbrt(), length);
//! let foo = length.powi::<4>();
//! ```
Expand Down Expand Up @@ -535,10 +537,4 @@ pub type Quotient<Q1, Q2> = <Q1 as core::ops::Div<Q2>>::Output;

pub mod internal {
pub use diman_lib::*;
pub mod num_traits_reexport {
#[cfg(feature = "num-traits-libm")]
pub use num_traits::float::Float;
#[cfg(not(feature = "num-traits-libm"))]
pub use num_traits::float::FloatCore;
}
}

0 comments on commit 7db1f67

Please sign in to comment.