Neg in C
This commit is contained in:
28
src/lib.rs
28
src/lib.rs
@@ -5,7 +5,7 @@ use core::{
|
||||
cmp, fmt,
|
||||
iter::{Product, Sum},
|
||||
num::FpCategory,
|
||||
ops::{Add, AddAssign, Div, DivAssign, Mul, MulAssign, Neg, Rem, RemAssign, Sub, SubAssign},
|
||||
ops::{Add, AddAssign, Div, DivAssign, Mul, MulAssign, Rem, RemAssign, Sub, SubAssign},
|
||||
};
|
||||
|
||||
macro_rules! forward_freeze_self {
|
||||
@@ -377,30 +377,6 @@ macro_rules! impls {
|
||||
Product, product, mul, Self::ONE,
|
||||
}
|
||||
|
||||
impl Neg for $fast_ty {
|
||||
type Output = Self;
|
||||
|
||||
#[inline(always)]
|
||||
fn neg(self) -> Self::Output {
|
||||
// Safety:
|
||||
//
|
||||
// - encountering poison is safe because LLVM's negate instruction documents
|
||||
// not producing UB on any inputs. The value is also immediately wrapped, so
|
||||
// poison propagation is controlled
|
||||
let val = unsafe { self.0.maybe_poison() };
|
||||
$fast_ty::new(-val)
|
||||
}
|
||||
}
|
||||
|
||||
impl Neg for &$fast_ty {
|
||||
type Output = <$fast_ty as Neg>::Output;
|
||||
|
||||
#[inline]
|
||||
fn neg(self) -> Self::Output {
|
||||
-(*self)
|
||||
}
|
||||
}
|
||||
|
||||
// Branching on poison values is UB, so any operation that makes a bool is protected by
|
||||
// freezing the operands. This includes [Partial]Eq and [Partial]Ord. Unfortunately
|
||||
// freezing has a nontrivial impact on performance, so non-bool methods should be preferred
|
||||
@@ -526,5 +502,3 @@ macro_rules! impls {
|
||||
|
||||
impls! { FF32, f32 }
|
||||
impls! { FF64, f64 }
|
||||
|
||||
// TODO num_traits, libm?
|
||||
|
||||
@@ -23,6 +23,11 @@
|
||||
} \
|
||||
\
|
||||
__attribute__((always_inline)) \
|
||||
C_TYPE neg_ ## RUST_TYPE(C_TYPE a) { \
|
||||
return -a; \
|
||||
} \
|
||||
\
|
||||
__attribute__((always_inline)) \
|
||||
bool eq_ ## RUST_TYPE(C_TYPE a, C_TYPE b) { \
|
||||
return a == b; \
|
||||
} \
|
||||
@@ -62,9 +67,11 @@
|
||||
IMPL_OPERATIONS(float, f32)
|
||||
IMPL_OPERATIONS(double, f64)
|
||||
|
||||
// FIXME sqrt is not poison safe on some targets
|
||||
IMPL_UNARY_FUNCTION(float, f32, sqrt, sqrtf)
|
||||
IMPL_UNARY_FUNCTION(double, f64, sqrt, sqrt)
|
||||
|
||||
// FIXME mod is not poison safe, though LLVM frem is
|
||||
IMPL_BINARY_FUNCTION(float, f32, rem, fmodf)
|
||||
IMPL_BINARY_FUNCTION(double, f64, rem, fmod)
|
||||
|
||||
|
||||
@@ -1,5 +1,5 @@
|
||||
use crate::{poison::MaybePoison, FF32, FF64};
|
||||
use core::ops::{Add, Div, Mul, Rem, Sub};
|
||||
use core::ops::{Add, Div, Mul, Neg, Rem, Sub};
|
||||
use paste::paste;
|
||||
|
||||
impl FF32 {
|
||||
@@ -152,6 +152,8 @@ macro_rules! impl_extern_math {
|
||||
fn [<div_ $base_ty>](a: $fast_ty, b: $fast_ty) -> $fast_ty;
|
||||
fn [<rem_ $base_ty>](a: $fast_ty, b: $fast_ty) -> $fast_ty;
|
||||
|
||||
fn [<neg_ $base_ty>](a: $fast_ty) -> $fast_ty;
|
||||
|
||||
fn [<min_ $base_ty>](a: $fast_ty, b: $fast_ty) -> $fast_ty;
|
||||
fn [<max_ $base_ty>](a: $fast_ty, b: $fast_ty) -> $fast_ty;
|
||||
|
||||
@@ -167,6 +169,24 @@ macro_rules! impl_extern_math {
|
||||
Rem, rem, [<rem_ $base_ty>],
|
||||
}
|
||||
|
||||
impl Neg for $fast_ty {
|
||||
type Output = Self;
|
||||
|
||||
#[inline(always)]
|
||||
fn neg(self) -> Self::Output {
|
||||
unsafe { [<neg_ $base_ty>](self) }
|
||||
}
|
||||
}
|
||||
|
||||
impl Neg for &$fast_ty {
|
||||
type Output = <$fast_ty as Neg>::Output;
|
||||
|
||||
#[inline]
|
||||
fn neg(self) -> Self::Output {
|
||||
-(*self)
|
||||
}
|
||||
}
|
||||
|
||||
impl $fast_ty {
|
||||
#[inline]
|
||||
pub fn max(self, other: Self) -> Self {
|
||||
|
||||
Reference in New Issue
Block a user