Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 8 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,14 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0

## [Unreleased](https://github.com/LDeakin/microfloat/compare/v0.1.2...HEAD)

### Added

- Add const `next_up()` and `next_down()` methods to all float types

### Changed

- `classify_bits`, `is_infinity_bits` are now `const`

## [0.1.2](https://github.com/LDeakin/microfloat/releases/tag/v0.1.2) - 2026-04-29

### Added
Expand Down
102 changes: 93 additions & 9 deletions src/bits.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ pub struct Class {
pub is_nan: bool,
}

pub fn classify_bits<F: Format>(bits: u8) -> Class {
pub const fn classify_bits<F: Format>(bits: u8) -> Class {
if is_nan_bits::<F>(bits) {
return Class {
is_zero: false,
Expand All @@ -29,13 +29,16 @@ pub fn classify_bits<F: Format>(bits: u8) -> Class {
is_nan: false,
};
}
if F::ZERO == ZeroMode::None {
return Class {
is_zero: false,
is_subnormal: false,
is_infinite: false,
is_nan: false,
};
match F::ZERO {
ZeroMode::None => {
return Class {
is_zero: false,
is_subnormal: false,
is_infinite: false,
is_nan: false,
};
}
ZeroMode::Signed | ZeroMode::Unsigned => {}
}

let mag = magnitude_bits::<F>(bits);
Expand Down Expand Up @@ -174,7 +177,7 @@ pub const fn is_nan_bits<F: Format>(bits: u8) -> bool {
}
}

pub fn is_infinity_bits<F: Format>(bits: u8) -> bool {
pub const fn is_infinity_bits<F: Format>(bits: u8) -> bool {
F::HAS_INF
&& exponent_field::<F>(bits) == F::MAX_EXPONENT_FIELD
&& mantissa_field::<F>(bits) == 0
Expand Down Expand Up @@ -208,6 +211,87 @@ pub const fn abs_bits<F: Format>(bits: u8) -> u8 {
}
}

pub const fn next_up_bits<F: Format>(bits: u8) -> u8 {
let bits = bits & F::STORAGE_MASK;
let class = classify_bits::<F>(bits);
if class.is_nan {
return bits;
}
if class.is_infinite {
return if is_negative_bits::<F>(bits) {
max_finite_bits::<F>(true)
} else {
bits
};
}
if class.is_zero {
return 1;
}
match F::SIGN {
SignMode::Unsigned => {
return if bits == max_finite_bits::<F>(false) {
infinity_bits::<F>(false)
} else {
bits + 1
};
}
SignMode::Signed => {}
}
if is_negative_bits::<F>(bits) {
if magnitude_bits::<F>(bits) == 1 {
neg_zero_bits::<F>()
} else {
bits - 1
}
} else if bits == max_finite_bits::<F>(false) {
infinity_bits::<F>(false)
} else {
bits + 1
}
}

pub const fn next_down_bits<F: Format>(bits: u8) -> u8 {
let bits = bits & F::STORAGE_MASK;
let class = classify_bits::<F>(bits);
if class.is_nan {
return bits;
}
if class.is_infinite {
return if is_negative_bits::<F>(bits) {
bits
} else {
max_finite_bits::<F>(false)
};
}
if class.is_zero {
return match F::SIGN {
SignMode::Signed => negate_bits::<F>(1),
SignMode::Unsigned => infinity_bits::<F>(true),
};
}
match F::SIGN {
SignMode::Unsigned => {
return if bits == 0 {
infinity_bits::<F>(true)
} else {
bits - 1
};
}
SignMode::Signed => {}
}
if is_negative_bits::<F>(bits) {
if bits == max_finite_bits::<F>(true) {
infinity_bits::<F>(true)
} else {
bits + 1
}
} else if magnitude_bits::<F>(bits) == 1 {
F::ZERO_BITS
} else {
bits - 1
}
}

pub fn decode_f32<F: Format>(bits: u8) -> f32 {
if F::ZERO == ZeroMode::None {
return if bits == 0xff {
Expand Down
10 changes: 10 additions & 0 deletions src/formats.rs
Original file line number Diff line number Diff line change
Expand Up @@ -370,6 +370,16 @@ macro_rules! define_format {
Self(self.0.abs())
}

/// Returns the least representable value greater than `self`.
pub const fn next_up(self) -> Self {
Self(self.0.next_up())
}

/// Returns the greatest representable value less than `self`.
pub const fn next_down(self) -> Self {
Self(self.0.next_down())
}

/// Returns the greatest integer less than or equal to `self`, rounded to this format.
pub fn floor(self) -> Self {
Self(self.0.floor())
Expand Down
10 changes: 9 additions & 1 deletion src/micro.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ use core::num::FpCategory;

use crate::bits::{
abs_bits, classify_bits, decode_f32, encode_f32, infinity_bits, nan_bits, neg_zero_bits,
negate_bits, one_bits, total_key,
negate_bits, next_down_bits, next_up_bits, one_bits, total_key,
};
use crate::format::{Format, NanEncoding, SignMode};

Expand Down Expand Up @@ -156,6 +156,14 @@ impl<F: Format> MicroFloat<F> {
Self::from_bits(abs_bits::<F>(self.bits))
}

pub const fn next_up(self) -> Self {
Self::from_bits(next_up_bits::<F>(self.bits))
}

pub const fn next_down(self) -> Self {
Self::from_bits(next_down_bits::<F>(self.bits))
}

pub fn floor(self) -> Self {
unary_result(self, libm::floorf(self.to_f32()))
}
Expand Down
Loading