#![no_std]
#![forbid(unsafe_code)]
#![warn(missing_docs)]
#[cfg(test)]
extern crate alloc;
#[cfg(any(feature = "std", test))]
extern crate std;
pub mod bf16;
#[derive(Clone, Copy, Debug, Default, Eq, PartialEq)]
#[non_exhaustive]
pub enum Execution {
#[default]
Upgrade,
Caller,
}
#[derive(Clone, Copy, Debug, Default, Eq, PartialEq)]
#[non_exhaustive]
pub enum Portability {
#[default]
BitExact,
BackendDependent,
}
#[derive(Clone, Copy, Debug, Default, Eq, PartialEq)]
#[non_exhaustive]
pub enum Subnormals {
Reject,
#[default]
FlushOrClamp,
Normalize,
}
#[derive(Clone, Copy, Debug, Default, Eq, PartialEq)]
#[non_exhaustive]
pub enum Validation {
#[default]
Checked,
ClampOutOfBounds,
AssumeValid,
ClampFinite,
}
#[derive(Clone, Copy, Debug, Eq, PartialEq)]
pub struct SliceError<K> {
pub kind: K,
pub index: usize,
}
impl<K: core::fmt::Debug> core::fmt::Display for SliceError<K> {
fn fmt(&self, f: &mut core::fmt::Formatter<'_>) -> core::fmt::Result {
write!(f, "{:?} at index {}", self.kind, self.index)
}
}
#[cfg(feature = "std")]
impl<K: core::fmt::Debug> std::error::Error for SliceError<K> {}
mod private {
pub trait Sealed: Copy {
fn eval_powf_const<const CFG: u128>(
self,
exponent: f32,
) -> Result<Self, crate::powf::PowfError>;
fn eval_powf_const_exp<const CFG: u128>(self) -> Result<Self, crate::powf::PowfError>;
fn eval_powf_runtime(
self,
exponent: f32,
options: crate::powf::PowfOptions,
) -> Result<Self, crate::powf::PowfError>;
fn eval_powf_runtime_const_exp(
self,
options: crate::powf::PowfOptions,
) -> Result<Self, crate::powf::PowfError>;
fn eval_expf_const<const CFG: u128, const OP: u8>(
self,
) -> Result<Self, crate::expf::ExpfError>;
fn eval_expf_runtime<const OP: u8>(
self,
options: crate::expf::ExpfOptions,
) -> Result<Self, crate::expf::ExpfError>;
fn eval_logf_const<const CFG: u128, const OP: u8>(
self,
) -> Result<Self, crate::logf::LogfError>;
fn eval_logf_runtime<const OP: u8>(
self,
options: crate::logf::LogfOptions,
) -> Result<Self, crate::logf::LogfError>;
fn eval_log1pf_const<const CFG: u128>(self) -> Result<Self, crate::log1pf::Log1pfError>;
fn eval_log1pf_runtime(
self,
options: crate::log1pf::Log1pfOptions,
) -> Result<Self, crate::log1pf::Log1pfError>;
fn eval_cbrtf_const<const CFG: u128>(self) -> Result<Self, crate::cbrtf::CbrtfError>;
fn eval_cbrtf_runtime(
self,
options: crate::cbrtf::CbrtfOptions,
) -> Result<Self, crate::cbrtf::CbrtfError>;
}
}
pub trait SimdVector: private::Sealed {}
pub trait SimdMath: SimdVector {
#[inline(always)]
fn try_powf(self, exponent: f32) -> Result<Self, powf::PowfError> {
self.try_powf_with::<{ powf::PowfOptions::new().bits() }>(exponent)
}
#[inline(always)]
fn try_powf_with<const CFG: u128>(self, exponent: f32) -> Result<Self, powf::PowfError> {
const {
let opts = powf::PowfOptions::decode_const(CFG);
assert!(
!opts.has_const_exponent(),
"CFG sets `.const_exponent(...)`; call `try_powf_const::<CFG>()` so the constant exponent is not ignored"
);
}
self.eval_powf_const::<CFG>(exponent)
}
#[inline(always)]
fn try_powf_const<const CFG: u128>(self) -> Result<Self, powf::PowfError> {
const {
let opts = powf::PowfOptions::decode_const(CFG);
assert!(
opts.has_const_exponent(),
"SimdMath::try_powf_const::<CFG> requires `.const_exponent(n)` on PowfOptions"
);
}
self.eval_powf_const_exp::<CFG>()
}
#[inline(always)]
fn powf<const CFG: u128>(self, exponent: f32) -> Self {
const {
let opts = powf::PowfOptions::decode_const(CFG);
opts.validate();
assert!(
!opts.has_const_exponent(),
"CFG sets `.const_exponent(...)`; call `powf_const::<CFG>()` so the constant exponent is not ignored"
);
assert!(
opts.is_infallible(),
"SimdMath::powf::<CFG> requires `.clamp_out_of_bounds()`, `.assume_in_bounds()` / `.unchecked()`, or `.admit_all_floats().highp()`; use `try_powf_with::<CFG>` for checked options"
);
}
match self.eval_powf_const::<CFG>(exponent) {
Ok(v) => v,
Err(_) => unreachable!("infallible configuration returned Err"),
}
}
#[inline(always)]
fn powf_const<const CFG: u128>(self) -> Self {
const {
let opts = powf::PowfOptions::decode_const(CFG);
opts.validate();
assert!(
opts.has_const_exponent(),
"SimdMath::powf_const::<CFG> requires `.const_exponent(n)` on PowfOptions"
);
assert!(
opts.is_infallible(),
"SimdMath::powf_const::<CFG> requires `.clamp_out_of_bounds()`, `.assume_in_bounds()` / `.unchecked()`, or `.admit_all_floats().highp()`; use `try_powf_const::<CFG>` for checked options"
);
}
match self.eval_powf_const_exp::<CFG>() {
Ok(v) => v,
Err(_) => unreachable!("infallible configuration returned Err"),
}
}
#[inline(always)]
fn try_exp2(self) -> Result<Self, expf::ExpfError> {
self.try_exp2_with::<{ expf::ExpfOptions::new().bits() }>()
}
#[inline(always)]
fn try_exp2_with<const CFG: u128>(self) -> Result<Self, expf::ExpfError> {
self.eval_expf_const::<CFG, 0>()
}
#[inline(always)]
fn exp2<const CFG: u128>(self) -> Self {
const {
let opts = expf::ExpfOptions::decode_const(CFG);
opts.validate();
assert!(
opts.is_infallible(),
"SimdMath::exp2::<CFG> requires `.clamp_out_of_bounds()`, `.assume_in_bounds()` / `.unchecked()`, or `.admit_all_floats()`; use `try_exp2_with::<CFG>` for checked options"
);
}
match self.eval_expf_const::<CFG, 0>() {
Ok(v) => v,
Err(_) => unreachable!("infallible configuration returned Err"),
}
}
#[inline(always)]
fn try_exp(self) -> Result<Self, expf::ExpfError> {
self.try_exp_with::<{ expf::ExpfOptions::new().bits() }>()
}
#[inline(always)]
fn try_exp_with<const CFG: u128>(self) -> Result<Self, expf::ExpfError> {
self.eval_expf_const::<CFG, 1>()
}
#[inline(always)]
fn exp<const CFG: u128>(self) -> Self {
const {
let opts = expf::ExpfOptions::decode_const(CFG);
opts.validate();
assert!(
opts.is_infallible(),
"SimdMath::exp::<CFG> requires `.clamp_out_of_bounds()`, `.assume_in_bounds()` / `.unchecked()`, or `.admit_all_floats()`; use `try_exp_with::<CFG>` for checked options"
);
}
match self.eval_expf_const::<CFG, 1>() {
Ok(v) => v,
Err(_) => unreachable!("infallible configuration returned Err"),
}
}
#[inline(always)]
fn try_log2(self) -> Result<Self, logf::LogfError> {
self.try_log2_with::<{ logf::LogfOptions::new().bits() }>()
}
#[inline(always)]
fn try_log2_with<const CFG: u128>(self) -> Result<Self, logf::LogfError> {
self.eval_logf_const::<CFG, 0>()
}
#[inline(always)]
fn log2<const CFG: u128>(self) -> Self {
const {
let opts = logf::LogfOptions::decode_const(CFG);
opts.validate();
assert!(
opts.is_infallible(),
"SimdMath::log2::<CFG> requires `.clamp_out_of_bounds()`, `.assume_in_bounds()` / `.unchecked()`, or `.admit_all_floats()`; use `try_log2_with::<CFG>` for checked options"
);
}
match self.eval_logf_const::<CFG, 0>() {
Ok(v) => v,
Err(_) => unreachable!("infallible configuration returned Err"),
}
}
#[inline(always)]
fn try_ln(self) -> Result<Self, logf::LogfError> {
self.try_ln_with::<{ logf::LogfOptions::new().bits() }>()
}
#[inline(always)]
fn try_ln_with<const CFG: u128>(self) -> Result<Self, logf::LogfError> {
self.eval_logf_const::<CFG, 1>()
}
#[inline(always)]
fn ln<const CFG: u128>(self) -> Self {
const {
let opts = logf::LogfOptions::decode_const(CFG);
opts.validate();
assert!(
opts.is_infallible(),
"SimdMath::ln::<CFG> requires `.clamp_out_of_bounds()`, `.assume_in_bounds()` / `.unchecked()`, or `.admit_all_floats()`; use `try_ln_with::<CFG>` for checked options"
);
}
match self.eval_logf_const::<CFG, 1>() {
Ok(v) => v,
Err(_) => unreachable!("infallible configuration returned Err"),
}
}
#[inline(always)]
fn try_log10(self) -> Result<Self, logf::LogfError> {
self.try_log10_with::<{ logf::LogfOptions::new().bits() }>()
}
#[inline(always)]
fn try_log10_with<const CFG: u128>(self) -> Result<Self, logf::LogfError> {
self.eval_logf_const::<CFG, 2>()
}
#[inline(always)]
fn log10<const CFG: u128>(self) -> Self {
const {
let opts = logf::LogfOptions::decode_const(CFG);
opts.validate();
assert!(
opts.is_infallible(),
"SimdMath::log10::<CFG> requires `.clamp_out_of_bounds()`, `.assume_in_bounds()` / `.unchecked()`, or `.admit_all_floats()`; use `try_log10_with::<CFG>` for checked options"
);
}
match self.eval_logf_const::<CFG, 2>() {
Ok(v) => v,
Err(_) => unreachable!("infallible configuration returned Err"),
}
}
#[inline(always)]
fn try_log1p(self) -> Result<Self, log1pf::Log1pfError> {
self.try_log1p_with::<{ log1pf::Log1pfOptions::new().bits() }>()
}
#[inline(always)]
fn try_log1p_with<const CFG: u128>(self) -> Result<Self, log1pf::Log1pfError> {
self.eval_log1pf_const::<CFG>()
}
#[inline(always)]
fn log1p<const CFG: u128>(self) -> Self {
const {
let opts = log1pf::Log1pfOptions::decode_const(CFG);
opts.validate();
assert!(
opts.is_infallible(),
"SimdMath::log1p::<CFG> requires `.clamp_out_of_bounds()`, `.assume_in_bounds()` / `.unchecked()`, or `.admit_all_floats()`; use `try_log1p_with::<CFG>` for checked options"
);
}
match self.eval_log1pf_const::<CFG>() {
Ok(v) => v,
Err(_) => unreachable!("infallible configuration returned Err"),
}
}
#[inline(always)]
fn try_cbrt(self) -> Result<Self, cbrtf::CbrtfError> {
self.try_cbrt_with::<{ cbrtf::CbrtfOptions::new().bits() }>()
}
#[inline(always)]
fn try_cbrt_with<const CFG: u128>(self) -> Result<Self, cbrtf::CbrtfError> {
self.eval_cbrtf_const::<CFG>()
}
#[inline(always)]
fn cbrt<const CFG: u128>(self) -> Self {
const {
let opts = cbrtf::CbrtfOptions::decode_const(CFG);
opts.validate();
assert!(
opts.is_infallible(),
"SimdMath::cbrt::<CFG> requires `.clamp_out_of_bounds()`, `.assume_in_bounds()` / `.unchecked()`, or `.admit_all_floats()`; use `try_cbrt_with::<CFG>` for checked options"
);
}
match self.eval_cbrtf_const::<CFG>() {
Ok(v) => v,
Err(_) => unreachable!("infallible configuration returned Err"),
}
}
}
macro_rules! impl_simd_vector {
($vec:ident, $bound:ident, $run_powf:ident, $run_powf_const:ident, $run_expf:ident, $run_logf:ident, $run_log1pf:ident, $run_cbrtf:ident, $lanes:literal) => {
impl<T: magetypes::simd::backends::$bound> private::Sealed
for magetypes::simd::generic::$vec<T>
{
#[inline(always)]
fn eval_powf_const<const CFG: u128>(
self,
exponent: f32,
) -> Result<Self, powf::PowfError> {
powf::const_api::$run_powf::<CFG, T>(self.token(), self, exponent)
}
#[inline(always)]
fn eval_powf_const_exp<const CFG: u128>(self) -> Result<Self, powf::PowfError> {
powf::const_api::$run_powf_const::<CFG, T>(self.token(), self)
}
#[inline(always)]
fn eval_powf_runtime(
self,
exponent: f32,
options: powf::PowfOptions,
) -> Result<Self, powf::PowfError> {
let token = self.token();
powf::const_api::runtime::<T, $lanes>(token, self.to_array(), exponent, options)
.map(|arr| magetypes::simd::generic::$vec::from_array_t(token, arr))
}
#[inline(always)]
fn eval_powf_runtime_const_exp(
self,
options: powf::PowfOptions,
) -> Result<Self, powf::PowfError> {
let token = self.token();
powf::const_api::runtime_const_exp::<T, $lanes>(token, self.to_array(), options)
.map(|arr| magetypes::simd::generic::$vec::from_array_t(token, arr))
}
#[inline(always)]
fn eval_expf_const<const CFG: u128, const OP: u8>(
self,
) -> Result<Self, expf::ExpfError> {
expf::const_api::$run_expf::<CFG, OP, T>(self.token(), self)
}
#[inline(always)]
fn eval_expf_runtime<const OP: u8>(
self,
options: expf::ExpfOptions,
) -> Result<Self, expf::ExpfError> {
let token = self.token();
expf::const_api::runtime_token::<T, OP, $lanes>(token, self.to_array(), options)
.map(|arr| magetypes::simd::generic::$vec::from_array_t(token, arr))
}
#[inline(always)]
fn eval_logf_const<const CFG: u128, const OP: u8>(
self,
) -> Result<Self, logf::LogfError> {
logf::const_api::$run_logf::<CFG, OP, T>(self.token(), self)
}
#[inline(always)]
fn eval_logf_runtime<const OP: u8>(
self,
options: logf::LogfOptions,
) -> Result<Self, logf::LogfError> {
let token = self.token();
logf::const_api::runtime_token::<T, OP, $lanes>(token, self.to_array(), options)
.map(|arr| magetypes::simd::generic::$vec::from_array_t(token, arr))
}
#[inline(always)]
fn eval_log1pf_const<const CFG: u128>(self) -> Result<Self, log1pf::Log1pfError> {
log1pf::const_api::$run_log1pf::<CFG, T>(self.token(), self)
}
#[inline(always)]
fn eval_log1pf_runtime(
self,
options: log1pf::Log1pfOptions,
) -> Result<Self, log1pf::Log1pfError> {
let token = self.token();
log1pf::const_api::runtime_token::<T, $lanes>(token, self.to_array(), options)
.map(|arr| magetypes::simd::generic::$vec::from_array_t(token, arr))
}
#[inline(always)]
fn eval_cbrtf_const<const CFG: u128>(self) -> Result<Self, cbrtf::CbrtfError> {
cbrtf::const_api::$run_cbrtf::<CFG, T>(self.token(), self)
}
#[inline(always)]
fn eval_cbrtf_runtime(
self,
options: cbrtf::CbrtfOptions,
) -> Result<Self, cbrtf::CbrtfError> {
let token = self.token();
cbrtf::const_api::runtime_token::<T, $lanes>(token, self.to_array(), options)
.map(|arr| magetypes::simd::generic::$vec::from_array_t(token, arr))
}
}
impl<T: magetypes::simd::backends::$bound> SimdVector
for magetypes::simd::generic::$vec<T>
{
}
impl<T: magetypes::simd::backends::$bound> SimdMath for magetypes::simd::generic::$vec<T> {}
};
}
impl_simd_vector!(
f32x4,
F32x4Convert,
run4,
run4_const,
run4,
run4,
run4,
run4,
4
);
impl_simd_vector!(
f32x8,
F32x8Convert,
run8,
run8_const,
run8,
run8,
run8,
run8,
8
);
#[cfg(feature = "w512")]
impl_simd_vector!(
f32x16,
F32x16Convert,
run16,
run16_const,
run16,
run16,
run16,
run16,
16
);
pub mod cbrtf;
pub mod expf;
pub mod log1pf;
pub mod logf;
pub mod powf;
mod scalar1;
mod soft_fma;
#[cfg(target_arch = "wasm32")]
#[allow(dead_code)] mod wasm_fma;