use crate::{
Execution, Portability, Validation,
bf16::{MAX_BF16, ceil_bf16, decode_bf16_finite, expand_bf16, floor_bf16},
};
const MAGIC: u128 = 0xB;
const DOMAIN_SHIFT: u32 = 4;
const VALIDATION_SHIFT: u32 = 9;
const PREC_SHIFT: u32 = 12;
const PORTABILITY_SHIFT: u32 = 15;
const EXECUTION_SHIFT: u32 = 17;
const HAS_CUSTOM_RANGE: u128 = 1 << 19;
const RESERVED_LO_MASK: u128 = ((1_u128 << 44) - 1) << 20;
const IN_LO_SHIFT: u32 = 64;
const IN_HI_SHIFT: u32 = 80;
const RESERVED_HI_MASK: u128 = 0xffff_ffff_u128 << 96;
const fn abs(x: f32) -> f32 {
if x < 0.0 { -x } else { x }
}
#[derive(Clone, Copy, Debug, Default, Eq, PartialEq)]
#[non_exhaustive]
pub enum ExpfDomain {
#[default]
Finite,
NormalOutput,
NonPositive,
SignedUnit,
SingleOctave,
PositiveOctave,
AllFloats,
}
#[derive(Clone, Copy, Debug, Default, Eq, PartialEq)]
#[non_exhaustive]
pub enum ExpfPrecision {
#[default]
Midp,
Lowp,
}
#[derive(Clone, Copy, Debug, Eq, PartialEq)]
pub struct ExpfOptions {
domain: ExpfDomain,
custom_range: Option<(u16, u16)>,
precision: ExpfPrecision,
portability: Portability,
validation: Validation,
execution: Execution,
}
impl Default for ExpfOptions {
#[inline]
fn default() -> Self {
Self::new()
}
}
impl ExpfOptions {
pub const fn new() -> Self {
Self {
domain: ExpfDomain::Finite,
custom_range: None,
precision: ExpfPrecision::Midp,
portability: Portability::BitExact,
validation: Validation::Checked,
execution: Execution::Upgrade,
}
}
pub const fn domain(mut self, domain: ExpfDomain) -> Self {
self.domain = domain;
self.custom_range = None;
self
}
pub const fn input_range(mut self, lo: f32, hi: f32) -> Self {
assert!(
lo <= hi && abs(lo) <= MAX_BF16 && abs(hi) <= MAX_BF16,
"input range must be finite within ±MAX_BF16 (0x7f7f_0000) with lo <= hi"
);
self.custom_range = Some((floor_bf16(lo), ceil_bf16(hi)));
self
}
pub const fn small_exp2_range(self) -> Self {
self.domain(ExpfDomain::SingleOctave)
}
pub const fn small_exp_range(self) -> Self {
self.domain(ExpfDomain::SingleOctave)
}
pub const fn unclamped_exp2_range(self) -> Self {
self.domain(ExpfDomain::NormalOutput)
}
pub const fn unclamped_exp_range(self) -> Self {
self.domain(ExpfDomain::NormalOutput)
}
pub const fn admit_all_floats(self) -> Self {
self.domain(ExpfDomain::AllFloats)
}
pub const fn full_domain(self) -> Self {
self.admit_all_floats()
}
pub const fn precision(mut self, value: ExpfPrecision) -> Self {
self.precision = value;
self
}
pub const fn lowp(self) -> Self {
self.precision(ExpfPrecision::Lowp)
}
pub const fn midp(self) -> Self {
self.precision(ExpfPrecision::Midp)
}
pub const fn portability(mut self, value: Portability) -> Self {
self.portability = value;
self
}
pub const fn bit_exact(self) -> Self {
self.portability(Portability::BitExact)
}
pub const fn backend_dependent(self) -> Self {
self.portability(Portability::BackendDependent)
}
pub const fn validation(mut self, value: Validation) -> Self {
self.validation = value;
self
}
pub const fn error_on_out_of_bounds(self) -> Self {
self.validation(Validation::Checked)
}
pub const fn checked(self) -> Self {
self.error_on_out_of_bounds()
}
pub const fn clamp_out_of_bounds(self) -> Self {
self.validation(Validation::ClampOutOfBounds)
}
pub const fn clamp_finite(self) -> Self {
self.validation(Validation::ClampFinite)
}
pub const fn assume_in_bounds(self) -> Self {
self.validation(Validation::AssumeValid)
}
pub const fn unchecked(self) -> Self {
self.assume_in_bounds()
}
pub const fn execution(mut self, value: Execution) -> Self {
self.execution = value;
self
}
pub const fn upgrade(self) -> Self {
self.execution(Execution::Upgrade)
}
pub const fn caller(self) -> Self {
self.execution(Execution::Caller)
}
pub const fn selected_domain(&self) -> ExpfDomain {
self.domain
}
pub const fn is_valid(&self) -> bool {
!(self.is_full() && self.custom_range.is_some())
}
#[doc(hidden)]
pub const fn validate(&self) {
assert!(
!(self.is_full() && self.custom_range.is_some()),
"ExpfDomain::AllFloats / full_domain cannot be combined with input_range"
);
}
pub(crate) const fn exp2_bounds(&self) -> Option<(f32, f32)> {
if self.is_full() {
return None;
}
if let Some((lo, hi)) = self.custom_range {
return Some((expand_bf16(lo), expand_bf16(hi)));
}
match self.domain {
ExpfDomain::Finite | ExpfDomain::AllFloats => None,
ExpfDomain::NormalOutput => Some((-126.0, 127.0)),
ExpfDomain::NonPositive => Some((-f32::MAX, 0.0)),
ExpfDomain::SignedUnit => Some((-1.0, 1.0)),
ExpfDomain::SingleOctave => Some((-0.5, 0.5)),
ExpfDomain::PositiveOctave => Some((0.0, 0.999_999_94)),
}
}
pub(crate) const fn exp_bounds(&self) -> Option<(f32, f32)> {
if self.is_full() {
return None;
}
if let Some((lo, hi)) = self.custom_range {
return Some((expand_bf16(lo), expand_bf16(hi)));
}
match self.domain {
ExpfDomain::Finite | ExpfDomain::AllFloats => None,
ExpfDomain::NormalOutput => Some((-87.0, 88.0)),
ExpfDomain::NonPositive => Some((-f32::MAX, 0.0)),
ExpfDomain::SignedUnit => Some((-1.0, 1.0)),
ExpfDomain::SingleOctave => Some((-0.346_573_56, 0.346_573_56)),
ExpfDomain::PositiveOctave => Some((0.0, f32::from_bits(0x3f31_7217))),
}
}
#[doc(hidden)]
pub const fn elides_clamp(&self) -> bool {
let Some((lo, hi)) = self.exp2_bounds() else {
return false;
};
if matches!(self.precision, ExpfPrecision::Lowp) {
lo >= -126.0 && hi <= 127.99
} else {
lo >= -126.0 && hi < 127.5
}
}
#[doc(hidden)]
pub const fn elides_exp_clamp(&self) -> bool {
let Some((lo, hi)) = self.exp_bounds() else {
return false;
};
let s_lo = lo * core::f32::consts::LOG2_E;
let s_hi = hi * core::f32::consts::LOG2_E;
if matches!(self.precision, ExpfPrecision::Lowp) {
s_lo >= -126.0 && s_hi <= 127.99
} else {
s_lo >= -126.0 && s_hi < 127.5
}
}
pub(crate) const fn elides_exp2_overflow(&self) -> bool {
let Some((_, hi)) = self.exp2_bounds() else {
return false;
};
if matches!(self.precision, ExpfPrecision::Lowp) {
hi <= 127.99
} else {
hi < 127.5
}
}
pub(crate) const fn elides_exp_overflow(&self) -> bool {
let Some((_, hi)) = self.exp_bounds() else {
return false;
};
let s_hi = hi * core::f32::consts::LOG2_E;
if matches!(self.precision, ExpfPrecision::Lowp) {
s_hi <= 127.99
} else {
s_hi < 127.5
}
}
#[doc(hidden)]
pub const fn elides_reduction(&self) -> bool {
let Some((lo, hi)) = self.exp2_bounds() else {
return false;
};
if matches!(self.precision, ExpfPrecision::Lowp) {
lo >= 0.0 && hi < 1.0
} else {
lo >= -0.5 && hi <= 0.5
}
}
#[doc(hidden)]
pub const fn elides_exp_reduction(&self) -> bool {
let Some((lo, hi)) = self.exp_bounds() else {
return false;
};
let s_lo = lo * core::f32::consts::LOG2_E;
let s_hi = hi * core::f32::consts::LOG2_E;
if matches!(self.precision, ExpfPrecision::Lowp) {
s_lo >= 0.0 && s_hi < 1.0
} else {
s_lo >= -0.5 && s_hi <= 0.5
}
}
pub const fn is_infallible(&self) -> bool {
self.is_valid() && (self.assume_valid() || self.clamp_oob() || self.is_full())
}
pub const fn bits(&self) -> u128 {
let dom: u128 = match self.domain {
ExpfDomain::Finite => 0,
ExpfDomain::NormalOutput => 1,
ExpfDomain::NonPositive => 2,
ExpfDomain::SignedUnit => 3,
ExpfDomain::SingleOctave => 4,
ExpfDomain::PositiveOctave => 5,
ExpfDomain::AllFloats => 6,
};
let val: u128 = match self.validation {
Validation::Checked => 0,
Validation::ClampOutOfBounds => 1,
Validation::AssumeValid => 2,
Validation::ClampFinite => 3,
};
let prec: u128 = match self.precision {
ExpfPrecision::Midp => 0,
ExpfPrecision::Lowp => 1,
};
let port: u128 = match self.portability {
Portability::BitExact => 0,
Portability::BackendDependent => 1,
};
let exec: u128 = match self.execution {
Execution::Upgrade => 0,
Execution::Caller => 1,
};
let mut out = MAGIC
| (dom << DOMAIN_SHIFT)
| (val << VALIDATION_SHIFT)
| (prec << PREC_SHIFT)
| (port << PORTABILITY_SHIFT)
| (exec << EXECUTION_SHIFT);
if let Some((lo, hi)) = self.custom_range {
out |= HAS_CUSTOM_RANGE | ((lo as u128) << IN_LO_SHIFT) | ((hi as u128) << IN_HI_SHIFT);
}
out
}
const fn from_bits_raw(bits: u128) -> Option<Self> {
if (bits & 0xf) != MAGIC || (bits & RESERVED_LO_MASK) != 0 || (bits & RESERVED_HI_MASK) != 0
{
return None;
}
let domain = match (bits >> DOMAIN_SHIFT) & 0x1f {
0 => ExpfDomain::Finite,
1 => ExpfDomain::NormalOutput,
2 => ExpfDomain::NonPositive,
3 => ExpfDomain::SignedUnit,
4 => ExpfDomain::SingleOctave,
5 => ExpfDomain::PositiveOctave,
6 => ExpfDomain::AllFloats,
_ => return None,
};
let validation = match (bits >> VALIDATION_SHIFT) & 0x7 {
0 => Validation::Checked,
1 => Validation::ClampOutOfBounds,
2 => Validation::AssumeValid,
3 => Validation::ClampFinite,
_ => return None,
};
let precision = match (bits >> PREC_SHIFT) & 0x7 {
0 => ExpfPrecision::Midp,
1 => ExpfPrecision::Lowp,
_ => return None,
};
let portability = match (bits >> PORTABILITY_SHIFT) & 0x3 {
0 => Portability::BitExact,
1 => Portability::BackendDependent,
_ => return None,
};
let execution = match (bits >> EXECUTION_SHIFT) & 0x3 {
0 => Execution::Upgrade,
1 => Execution::Caller,
_ => return None,
};
let lo_u16 = ((bits >> IN_LO_SHIFT) & 0xffff) as u16;
let hi_u16 = ((bits >> IN_HI_SHIFT) & 0xffff) as u16;
let custom_range = if (bits & HAS_CUSTOM_RANGE) != 0 {
let Some(lo) = decode_bf16_finite(lo_u16) else {
return None;
};
let Some(hi) = decode_bf16_finite(hi_u16) else {
return None;
};
if !(lo <= hi && abs(lo) <= MAX_BF16 && abs(hi) <= MAX_BF16) {
return None;
}
Some((lo_u16, hi_u16))
} else {
if lo_u16 != 0 || hi_u16 != 0 {
return None;
}
None
};
Some(Self {
domain,
custom_range,
precision,
portability,
validation,
execution,
})
}
pub const fn from_bits(bits: u128) -> Option<Self> {
let Some(opts) = Self::from_bits_raw(bits) else {
return None;
};
if opts.is_valid() { Some(opts) } else { None }
}
pub(crate) const fn decode_const(bits: u128) -> Self {
let Some(opts) = Self::from_bits_raw(bits) else {
panic!("invalid expf const options bit pattern");
};
opts.validate();
opts
}
#[inline(always)]
pub const fn runtime(self) -> super::ExpfRuntime {
super::ExpfRuntime::new(self)
}
pub(crate) const fn is_full(&self) -> bool {
matches!(self.domain, ExpfDomain::AllFloats)
}
pub(crate) const fn is_lowp(&self) -> bool {
matches!(self.precision, ExpfPrecision::Lowp)
}
pub(crate) const fn is_backend(&self) -> bool {
matches!(self.portability, Portability::BackendDependent)
}
pub(crate) const fn clamp_oob(&self) -> bool {
matches!(
self.validation,
Validation::ClampOutOfBounds | Validation::ClampFinite
)
}
pub(crate) const fn preserve_clamped_nan(&self) -> bool {
matches!(self.validation, Validation::ClampOutOfBounds)
}
pub(crate) const fn assume_valid(&self) -> bool {
matches!(self.validation, Validation::AssumeValid)
}
pub(crate) const fn is_caller(&self) -> bool {
matches!(self.execution, Execution::Caller)
}
}