Skip to content

Commit

Permalink
Simplify f16 and f32 trait impls.
Browse files Browse the repository at this point in the history
  • Loading branch information
Alexhuszagh committed Dec 8, 2024
1 parent 9b26c69 commit 779d63d
Show file tree
Hide file tree
Showing 5 changed files with 81 additions and 84 deletions.
4 changes: 4 additions & 0 deletions CHANGELOG
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,10 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0

## [Unreleased]

### Added

- Additional trait impls for `f16` and `f32` to better match Rust's interface.

## [1.0.4] 2024-12-07

### Changed
Expand Down
62 changes: 20 additions & 42 deletions lexical-util/src/bf16.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,10 @@
#![doc(hidden)]

use core::cmp::Ordering;
use core::{fmt, ops};
use core::fmt;
use core::ops::*;

use crate::numtypes::op_impl;

/// Brain floating point type.
#[allow(non_camel_case_types)]
Expand Down Expand Up @@ -91,7 +94,7 @@ impl fmt::Display for bf16 {
}
}

impl ops::Add for bf16 {
impl Add for bf16 {
type Output = Self;

#[inline(always)]
Expand All @@ -100,7 +103,9 @@ impl ops::Add for bf16 {
}
}

impl ops::Div for bf16 {
op_impl!(bf16, Add, AddAssign, add, add_assign);

impl Div for bf16 {
type Output = Self;

#[inline(always)]
Expand All @@ -109,7 +114,9 @@ impl ops::Div for bf16 {
}
}

impl ops::Mul for bf16 {
op_impl!(bf16, Div, DivAssign, div, div_assign);

impl Mul for bf16 {
type Output = Self;

#[inline(always)]
Expand All @@ -118,7 +125,9 @@ impl ops::Mul for bf16 {
}
}

impl ops::Sub for bf16 {
op_impl!(bf16, Mul, MulAssign, mul, mul_assign);

impl Sub for bf16 {
type Output = Self;

#[inline(always)]
Expand All @@ -127,7 +136,9 @@ impl ops::Sub for bf16 {
}
}

impl ops::Rem for bf16 {
op_impl!(bf16, Sub, SubAssign, sub, sub_assign);

impl Rem for bf16 {
type Output = Self;

#[inline(always)]
Expand All @@ -136,46 +147,13 @@ impl ops::Rem for bf16 {
}
}

impl ops::Neg for bf16 {
op_impl!(bf16, Rem, RemAssign, rem, rem_assign);

impl Neg for bf16 {
type Output = Self;

#[inline(always)]
fn neg(self) -> Self::Output {
Self::from_bits(self.0 ^ (1 << 15))
}
}

impl ops::AddAssign for bf16 {
#[inline(always)]
fn add_assign(&mut self, rhs: Self) {
*self = *self + rhs;
}
}

impl ops::DivAssign for bf16 {
#[inline(always)]
fn div_assign(&mut self, rhs: Self) {
*self = *self / rhs;
}
}

impl ops::MulAssign for bf16 {
#[inline(always)]
fn mul_assign(&mut self, rhs: Self) {
*self = *self * rhs;
}
}

impl ops::SubAssign for bf16 {
#[inline(always)]
fn sub_assign(&mut self, rhs: Self) {
*self = *self - rhs;
}
}

impl ops::RemAssign for bf16 {
#[inline(always)]
fn rem_assign(&mut self, rhs: Self) {
*self = *self % rhs;
}
}
61 changes: 19 additions & 42 deletions lexical-util/src/f16.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,9 +15,11 @@
#![doc(hidden)]

use core::cmp::Ordering;
use core::{fmt, ops};
use core::fmt;
use core::ops::*;

use crate::num::Float;
use crate::numtypes::op_impl;

/// Half-precision IEEE-754 floating point type.
#[allow(non_camel_case_types)]
Expand Down Expand Up @@ -81,7 +83,7 @@ impl fmt::Display for f16 {
}
}

impl ops::Add for f16 {
impl Add for f16 {
type Output = Self;

#[inline(always)]
Expand All @@ -90,7 +92,9 @@ impl ops::Add for f16 {
}
}

impl ops::Div for f16 {
op_impl!(f16, Add, AddAssign, add, add_assign);

impl Div for f16 {
type Output = Self;

#[inline(always)]
Expand All @@ -99,7 +103,9 @@ impl ops::Div for f16 {
}
}

impl ops::Mul for f16 {
op_impl!(f16, Div, DivAssign, div, div_assign);

impl Mul for f16 {
type Output = Self;

#[inline(always)]
Expand All @@ -108,7 +114,9 @@ impl ops::Mul for f16 {
}
}

impl ops::Sub for f16 {
op_impl!(f16, Mul, MulAssign, mul, mul_assign);

impl Sub for f16 {
type Output = Self;

#[inline(always)]
Expand All @@ -117,7 +125,9 @@ impl ops::Sub for f16 {
}
}

impl ops::Rem for f16 {
op_impl!(f16, Sub, SubAssign, sub, sub_assign);

impl Rem for f16 {
type Output = Self;

#[inline(always)]
Expand All @@ -126,7 +136,9 @@ impl ops::Rem for f16 {
}
}

impl ops::Neg for f16 {
op_impl!(f16, Rem, RemAssign, rem, rem_assign);

impl Neg for f16 {
type Output = Self;

#[inline(always)]
Expand All @@ -135,41 +147,6 @@ impl ops::Neg for f16 {
}
}

impl ops::AddAssign for f16 {
#[inline(always)]
fn add_assign(&mut self, rhs: Self) {
*self = *self + rhs;
}
}

impl ops::DivAssign for f16 {
#[inline(always)]
fn div_assign(&mut self, rhs: Self) {
*self = *self / rhs;
}
}

impl ops::MulAssign for f16 {
#[inline(always)]
fn mul_assign(&mut self, rhs: Self) {
*self = *self * rhs;
}
}

impl ops::SubAssign for f16 {
#[inline(always)]
fn sub_assign(&mut self, rhs: Self) {
*self = *self - rhs;
}
}

impl ops::RemAssign for f16 {
#[inline(always)]
fn rem_assign(&mut self, rhs: Self) {
*self = *self % rhs;
}
}

// In the below functions, round to nearest, with ties to even.
// Let us call the most significant bit that will be shifted out the round_bit.
//
Expand Down
1 change: 1 addition & 0 deletions lexical-util/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -176,4 +176,5 @@ mod format_builder;
mod format_flags;
mod noskip;
mod not_feature_format;
mod numtypes;
mod skip;
37 changes: 37 additions & 0 deletions lexical-util/src/numtypes.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
//! Utilties for defining custom numeric types.
//!
//! This defines primarily macros intended to be used when
//! defining your own types.
#![cfg(feature = "f16")]
#![doc(hidden)]

/// Implement the reference and op assign variants of a trait.
macro_rules! op_impl {
($t:ty, $trait:ident, $assign:ident, $op:ident, $op_assign:ident) => {
impl $trait<&$t> for $t {
type Output = <Self as $trait>::Output;

#[inline(always)]
fn $op(self, rhs: &Self) -> Self::Output {
self.$op(*rhs)
}
}

impl $assign for $t {
#[inline(always)]
fn $op_assign(&mut self, other: Self) {
*self = self.$op(other);
}
}

impl $assign<&$t> for $t {
#[inline(always)]
fn $op_assign(&mut self, other: &Self) {
*self = self.$op(other);
}
}
};
}

pub(crate) use op_impl;

0 comments on commit 779d63d

Please sign in to comment.