fix copysign
[vector-math.git] / src / prim.rs
1 #[cfg(not(feature = "std"))]
2 use crate::scalar::{Scalar, Value};
3 use crate::{
4 f16::F16,
5 traits::{ConvertFrom, ConvertTo},
6 };
7 use core::{fmt, hash, ops};
8
9 mod sealed {
10 use crate::f16::F16;
11
12 pub trait Sealed {}
13 impl Sealed for F16 {}
14 impl Sealed for f32 {}
15 impl Sealed for f64 {}
16 impl Sealed for u8 {}
17 impl Sealed for u16 {}
18 impl Sealed for u32 {}
19 impl Sealed for u64 {}
20 impl Sealed for i8 {}
21 impl Sealed for i16 {}
22 impl Sealed for i32 {}
23 impl Sealed for i64 {}
24 }
25
26 pub trait PrimBase:
27 sealed::Sealed
28 + Copy
29 + 'static
30 + Send
31 + Sync
32 + PartialOrd
33 + fmt::Debug
34 + fmt::Display
35 + ops::Add<Output = Self>
36 + ops::Sub<Output = Self>
37 + ops::Mul<Output = Self>
38 + ops::Div<Output = Self>
39 + ops::Rem<Output = Self>
40 + ops::AddAssign
41 + ops::SubAssign
42 + ops::MulAssign
43 + ops::DivAssign
44 + ops::RemAssign
45 + ConvertFrom<i8>
46 + ConvertFrom<u8>
47 + ConvertFrom<i16>
48 + ConvertFrom<u16>
49 + ConvertFrom<F16>
50 + ConvertFrom<i32>
51 + ConvertFrom<u32>
52 + ConvertFrom<f32>
53 + ConvertFrom<i64>
54 + ConvertFrom<u64>
55 + ConvertFrom<f64>
56 + ConvertTo<i8>
57 + ConvertTo<u8>
58 + ConvertTo<i16>
59 + ConvertTo<u16>
60 + ConvertTo<F16>
61 + ConvertTo<i32>
62 + ConvertTo<u32>
63 + ConvertTo<f32>
64 + ConvertTo<i64>
65 + ConvertTo<u64>
66 + ConvertTo<f64>
67 {
68 }
69
70 pub trait PrimInt:
71 PrimBase
72 + Ord
73 + hash::Hash
74 + fmt::Binary
75 + fmt::LowerHex
76 + fmt::Octal
77 + fmt::UpperHex
78 + ops::BitAnd<Output = Self>
79 + ops::BitOr<Output = Self>
80 + ops::BitXor<Output = Self>
81 + ops::Shl<Output = Self>
82 + ops::Shr<Output = Self>
83 + ops::Not<Output = Self>
84 + ops::BitAndAssign
85 + ops::BitOrAssign
86 + ops::BitXorAssign
87 + ops::ShlAssign
88 + ops::ShrAssign
89 {
90 const ZERO: Self;
91 const ONE: Self;
92 const MIN: Self;
93 const MAX: Self;
94 }
95
96 pub trait PrimUInt: PrimInt + ConvertFrom<Self::SignedType> {
97 type SignedType: PrimSInt<UnsignedType = Self> + ConvertFrom<Self>;
98 }
99
100 pub trait PrimSInt: PrimInt + ops::Neg<Output = Self> + ConvertFrom<Self::UnsignedType> {
101 type UnsignedType: PrimUInt<SignedType = Self> + ConvertFrom<Self>;
102 }
103
104 macro_rules! impl_int {
105 ($uint:ident, $sint:ident) => {
106 impl PrimBase for $uint {}
107 impl PrimBase for $sint {}
108 impl PrimInt for $uint {
109 const ZERO: Self = 0;
110 const ONE: Self = 1;
111 const MIN: Self = 0;
112 const MAX: Self = !0;
113 }
114 impl PrimInt for $sint {
115 const ZERO: Self = 0;
116 const ONE: Self = 1;
117 const MIN: Self = $sint::MIN;
118 const MAX: Self = $sint::MAX;
119 }
120 impl PrimUInt for $uint {
121 type SignedType = $sint;
122 }
123 impl PrimSInt for $sint {
124 type UnsignedType = $uint;
125 }
126 };
127 }
128
129 impl_int!(u8, i8);
130 impl_int!(u16, i16);
131 impl_int!(u32, i32);
132 impl_int!(u64, i64);
133
134 pub trait PrimFloat:
135 PrimBase + ops::Neg<Output = Self> + ConvertFrom<Self::BitsType> + ConvertFrom<Self::SignedBitsType>
136 {
137 type BitsType: PrimUInt<SignedType = Self::SignedBitsType> + ConvertFrom<Self>;
138 type SignedBitsType: PrimSInt<UnsignedType = Self::BitsType> + ConvertFrom<Self>;
139 const EXPONENT_BIAS_UNSIGNED: Self::BitsType;
140 const EXPONENT_BIAS_SIGNED: Self::SignedBitsType;
141 const SIGN_FIELD_WIDTH: Self::BitsType;
142 const EXPONENT_FIELD_WIDTH: Self::BitsType;
143 const MANTISSA_FIELD_WIDTH: Self::BitsType;
144 const SIGN_FIELD_SHIFT: Self::BitsType;
145 const EXPONENT_FIELD_SHIFT: Self::BitsType;
146 const MANTISSA_FIELD_SHIFT: Self::BitsType;
147 const SIGN_FIELD_MASK: Self::BitsType;
148 const EXPONENT_FIELD_MASK: Self::BitsType;
149 const MANTISSA_FIELD_MASK: Self::BitsType;
150 const IMPLICIT_MANTISSA_BIT: Self::BitsType;
151 const ZERO_SUBNORMAL_EXPONENT: Self::BitsType;
152 const NAN_INFINITY_EXPONENT: Self::BitsType;
153 const INFINITY_BITS: Self::BitsType;
154 const NAN_BITS: Self::BitsType;
155 fn is_nan(self) -> bool;
156 fn from_bits(bits: Self::BitsType) -> Self;
157 fn to_bits(self) -> Self::BitsType;
158 fn abs(self) -> Self;
159 fn max_contiguous_integer() -> Self {
160 (Self::BitsType::cvt_from(1) << (Self::MANTISSA_FIELD_WIDTH + 1.to())).to()
161 }
162 fn is_finite(self) -> bool;
163 fn trunc(self) -> Self;
164 /// round to nearest, ties to unspecified
165 fn round(self) -> Self;
166 fn copy_sign(self, sign: Self) -> Self;
167 }
168
169 macro_rules! impl_float {
170 (
171 impl PrimFloat for $float:ident {
172 type BitsType = $bits_type:ident;
173 type SignedBitsType = $signed_bits_type:ident;
174 const EXPONENT_FIELD_WIDTH: u32 = $exponent_field_width:literal;
175 const MANTISSA_FIELD_WIDTH: u32 = $mantissa_field_width:literal;
176 }
177 ) => {
178 impl PrimBase for $float {}
179
180 impl PrimFloat for $float {
181 type BitsType = $bits_type;
182 type SignedBitsType = $signed_bits_type;
183 const EXPONENT_BIAS_UNSIGNED: Self::BitsType =
184 (1 << (Self::EXPONENT_FIELD_WIDTH - 1)) - 1;
185 const EXPONENT_BIAS_SIGNED: Self::SignedBitsType = Self::EXPONENT_BIAS_UNSIGNED as _;
186 const SIGN_FIELD_WIDTH: Self::BitsType = 1;
187 const EXPONENT_FIELD_WIDTH: Self::BitsType = $exponent_field_width;
188 const MANTISSA_FIELD_WIDTH: Self::BitsType = $mantissa_field_width;
189 const SIGN_FIELD_SHIFT: Self::BitsType =
190 Self::EXPONENT_FIELD_SHIFT + Self::EXPONENT_FIELD_WIDTH;
191 const EXPONENT_FIELD_SHIFT: Self::BitsType = Self::MANTISSA_FIELD_WIDTH;
192 const MANTISSA_FIELD_SHIFT: Self::BitsType = 0;
193 const SIGN_FIELD_MASK: Self::BitsType = 1 << Self::SIGN_FIELD_SHIFT;
194 const EXPONENT_FIELD_MASK: Self::BitsType =
195 ((1 << Self::EXPONENT_FIELD_WIDTH) - 1) << Self::EXPONENT_FIELD_SHIFT;
196 const MANTISSA_FIELD_MASK: Self::BitsType = (1 << Self::MANTISSA_FIELD_WIDTH) - 1;
197 const IMPLICIT_MANTISSA_BIT: Self::BitsType = 1 << Self::MANTISSA_FIELD_WIDTH;
198 const ZERO_SUBNORMAL_EXPONENT: Self::BitsType = 0;
199 const NAN_INFINITY_EXPONENT: Self::BitsType = (1 << Self::EXPONENT_FIELD_WIDTH) - 1;
200 const INFINITY_BITS: Self::BitsType =
201 Self::NAN_INFINITY_EXPONENT << Self::EXPONENT_FIELD_SHIFT;
202 const NAN_BITS: Self::BitsType =
203 Self::INFINITY_BITS | (1 << (Self::MANTISSA_FIELD_WIDTH - 1));
204 fn is_nan(self) -> bool {
205 $float::is_nan(self)
206 }
207 fn from_bits(bits: Self::BitsType) -> Self {
208 $float::from_bits(bits)
209 }
210 fn to_bits(self) -> Self::BitsType {
211 self.to_bits()
212 }
213 fn abs(self) -> Self {
214 #[cfg(feature = "std")]
215 return $float::abs(self);
216 #[cfg(not(feature = "std"))]
217 return crate::algorithms::base::abs(Scalar, Value(self)).0;
218 }
219 fn is_finite(self) -> bool {
220 $float::is_finite(self)
221 }
222 fn trunc(self) -> Self {
223 #[cfg(feature = "std")]
224 return $float::trunc(self);
225 #[cfg(not(feature = "std"))]
226 return crate::algorithms::base::trunc(Scalar, Value(self)).0;
227 }
228 fn round(self) -> Self {
229 #[cfg(feature = "std")]
230 return $float::round(self);
231 #[cfg(not(feature = "std"))]
232 return crate::algorithms::base::round_to_nearest_ties_to_even(Scalar, Value(self))
233 .0;
234 }
235 fn copy_sign(self, sign: Self) -> Self {
236 #[cfg(feature = "std")]
237 return $float::copysign(self, sign);
238 #[cfg(not(feature = "std"))]
239 return crate::algorithms::base::copy_sign(Scalar, Value(self), Value(sign)).0;
240 }
241 }
242 };
243 }
244
245 impl_float! {
246 impl PrimFloat for F16 {
247 type BitsType = u16;
248 type SignedBitsType = i16;
249 const EXPONENT_FIELD_WIDTH: u32 = 5;
250 const MANTISSA_FIELD_WIDTH: u32 = 10;
251 }
252 }
253
254 impl_float! {
255 impl PrimFloat for f32 {
256 type BitsType = u32;
257 type SignedBitsType = i32;
258 const EXPONENT_FIELD_WIDTH: u32 = 8;
259 const MANTISSA_FIELD_WIDTH: u32 = 23;
260 }
261 }
262
263 impl_float! {
264 impl PrimFloat for f64 {
265 type BitsType = u64;
266 type SignedBitsType = i64;
267 const EXPONENT_FIELD_WIDTH: u32 = 11;
268 const MANTISSA_FIELD_WIDTH: u32 = 52;
269 }
270 }