Skip to content

Commit f7e3465

Browse files
authored
Merge pull request #128 from djsell/signed
add num_traits::Signed impl
2 parents cfe3cbc + 4d7c463 commit f7e3465

File tree

4 files changed

+40
-2
lines changed

4 files changed

+40
-2
lines changed

CHANGELOG.md

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,9 @@ and this project adheres to [Semantic Versioning](http://semver.org/spec/v2.0.0.
88
- `min` and `max` incorrectly propagate `NaN` values when `self` is `NaN`. Fixes [#126],
99
by [@mgottscho].
1010

11+
### Added
12+
- New `num-traits` implementations: `Signed` for `f16` and `bf16`.
13+
1114
## [2.6.0] - 2024-04-08 <a name="2.6.0"></a>
1215
### Changed
1316
- Fixed some incorrect minimum supported versions of dependencies that weren't caught due to

README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,7 @@ See the [crate documentation](https://docs.rs/half/) for more details.
3939
dependency on the [`serde`](https://crates.io/crates/serde) crate.
4040

4141
- **`num-traits`** — Enable `ToPrimitive`, `FromPrimitive`, `ToBytes`, `FromBytes`, `Num`, `Float`,
42-
`FloatCore` and `Bounded` trait implementations from the
42+
`FloatCore`, `Signed`, and `Bounded` trait implementations from the
4343
[`num-traits`](https://crates.io/crates/num-traits) crate.
4444

4545
- **`bytemuck`** — Enable `Zeroable` and `Pod` trait implementations from the

src/lib.rs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -89,7 +89,7 @@
8989
//!
9090
//! - **`num-traits`** — Adds support for the [`num-traits`] crate by implementing [`ToPrimitive`],
9191
//! [`FromPrimitive`], [`ToBytes`], `FromBytes`, [`AsPrimitive`], [`Num`], [`Float`],
92-
//! [`FloatCore`], and [`Bounded`] traits for both [`struct@f16`] and [`struct@bf16`].
92+
//! [`FloatCore`], [`Signed`], and [`Bounded`] traits for both [`struct@f16`] and [`struct@bf16`].
9393
//!
9494
//! - **`bytemuck`** — Adds support for the [`bytemuck`] crate by implementing [`Zeroable`] and
9595
//! [`Pod`] traits for both [`struct@f16`] and [`struct@bf16`].
@@ -149,6 +149,7 @@
149149
[`Num`]: ::num_traits::Num
150150
[`Float`]: ::num_traits::Float
151151
[`FloatCore`]: ::num_traits::float::FloatCore
152+
[`Signed`]: ::num_traits::Signed
152153
[`Bounded`]: ::num_traits::Bounded"
153154
)]
154155
#![cfg_attr(

src/num_traits.rs

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1548,3 +1548,37 @@ impl FromBytes for bf16 {
15481548
Self::from_ne_bytes(*bytes)
15491549
}
15501550
}
1551+
1552+
macro_rules! impl_signed {
1553+
($ty:ty) => {
1554+
impl ::num_traits::Signed for $ty {
1555+
#[inline]
1556+
fn abs(&self) -> Self {
1557+
::num_traits::float::Float::abs(*self)
1558+
}
1559+
1560+
#[inline]
1561+
fn abs_sub(&self, other: &Self) -> Self {
1562+
::num_traits::float::Float::abs_sub(*self, *other)
1563+
}
1564+
1565+
#[inline]
1566+
fn signum(&self) -> Self {
1567+
::num_traits::float::Float::signum(*self)
1568+
}
1569+
1570+
#[inline]
1571+
fn is_positive(&self) -> bool {
1572+
::num_traits::float::Float::is_sign_positive(*self)
1573+
}
1574+
1575+
#[inline]
1576+
fn is_negative(&self) -> bool {
1577+
::num_traits::float::Float::is_sign_negative(*self)
1578+
}
1579+
}
1580+
};
1581+
}
1582+
1583+
impl_signed!(f16);
1584+
impl_signed!(bf16);

0 commit comments

Comments
 (0)