3 prim::{PrimFloat, PrimSInt, PrimUInt},
6 Add, AddAssign, BitAnd, BitAndAssign, BitOr, BitOrAssign, BitXor, BitXorAssign, Div, DivAssign,
7 Mul, MulAssign, Neg, Not, Rem, RemAssign, Shl, ShlAssign, Shr, ShrAssign, Sub, SubAssign,
10 include!(concat!(env!("OUT_DIR"), "/context_trait.rs"));
12 pub trait Make: Copy {
14 type Context: Context;
15 fn ctx(self) -> Self::Context;
16 fn make(ctx: Self::Context, v: Self::Prim) -> Self;
19 pub trait ConvertFrom<T>: Sized {
20 fn cvt_from(v: T) -> Self;
23 impl<T> ConvertFrom<T> for T {
24 fn cvt_from(v: T) -> Self {
29 pub trait ConvertTo<T> {
33 impl<F, T: ConvertFrom<F>> ConvertTo<T> for F {
39 macro_rules! impl_convert_from_using_as {
40 ($first:ident $(, $ty:ident)*) => {
42 impl ConvertFrom<$first> for $ty {
43 fn cvt_from(v: $first) -> Self {
47 impl ConvertFrom<$ty> for $first {
48 fn cvt_from(v: $ty) -> Self {
53 impl_convert_from_using_as![$($ty),*];
59 impl_convert_from_using_as![u8, i8, u16, i16, u32, i32, u64, i64, f32, f64];
76 impl<T> Number for T where
93 + BitAnd<Output = Self>
94 + BitOr<Output = Self>
95 + BitXor<Output = Self>
103 impl<T> BitOps for T where
105 + BitAnd<Output = Self>
106 + BitOr<Output = Self>
107 + BitXor<Output = Self>
116 Number + BitOps + Shl<Output = Self> + Shr<Output = Self> + ShlAssign + ShrAssign
118 fn leading_zeros(self) -> Self;
119 fn leading_ones(self) -> Self {
120 self.not().leading_zeros()
122 fn trailing_zeros(self) -> Self;
123 fn trailing_ones(self) -> Self {
124 self.not().trailing_zeros()
126 fn count_zeros(self) -> Self {
127 self.not().count_ones()
129 fn count_ones(self) -> Self;
132 pub trait UInt: Int + Make<Prim = Self::PrimUInt> + ConvertFrom<Self::SignedType> {
133 type PrimUInt: PrimUInt<SignedType = <Self::SignedType as SInt>::PrimSInt>;
134 type SignedType: SInt
136 + Make<Context = Self::Context>
137 + Compare<Bool = Self::Bool>;
141 Int + Neg<Output = Self> + Make<Prim = Self::PrimSInt> + ConvertFrom<Self::UnsignedType>
143 type PrimSInt: PrimSInt<UnsignedType = <Self::UnsignedType as UInt>::PrimUInt>;
144 type UnsignedType: UInt
146 + Make<Context = Self::Context>
147 + Compare<Bool = Self::Bool>;
153 + Make<Prim = Self::PrimFloat>
154 + ConvertFrom<Self::SignedBitsType>
155 + ConvertFrom<Self::BitsType>
157 type PrimFloat: PrimFloat;
158 type BitsType: UInt<PrimUInt = <Self::PrimFloat as PrimFloat>::BitsType, SignedType = Self::SignedBitsType>
159 + Make<Context = Self::Context, Prim = <Self::PrimFloat as PrimFloat>::BitsType>
160 + Compare<Bool = Self::Bool>
162 type SignedBitsType: SInt<
163 PrimSInt = <Self::PrimFloat as PrimFloat>::SignedBitsType,
164 UnsignedType = Self::BitsType,
165 > + Make<Context = Self::Context, Prim = <Self::PrimFloat as PrimFloat>::SignedBitsType>
166 + Compare<Bool = Self::Bool>
168 fn abs(self) -> Self;
169 fn copy_sign(self, sign: Self) -> Self {
170 crate::algorithms::base::copy_sign(self.ctx(), self, sign)
172 fn trunc(self) -> Self;
173 fn ceil(self) -> Self;
174 fn floor(self) -> Self;
175 /// round to nearest integer, unspecified which way half-way cases are rounded
176 fn round(self) -> Self;
177 /// returns `self * a + b` but only rounding once
178 #[cfg(feature = "fma")]
179 fn fma(self, a: Self, b: Self) -> Self;
180 /// returns `self * a + b` either using `fma` or `self * a + b`
181 fn mul_add_fast(self, a: Self, b: Self) -> Self {
182 #[cfg(feature = "fma")]
183 return self.fma(a, b);
184 #[cfg(not(feature = "fma"))]
187 fn is_nan(self) -> Self::Bool {
190 fn is_infinite(self) -> Self::Bool {
191 self.abs().eq(Self::infinity(self.ctx()))
193 fn infinity(ctx: Self::Context) -> Self {
194 Self::from_bits(ctx.make(Self::PrimFloat::INFINITY_BITS))
196 fn nan(ctx: Self::Context) -> Self {
197 Self::from_bits(ctx.make(Self::PrimFloat::NAN_BITS))
199 fn is_finite(self) -> Self::Bool;
200 fn is_zero_or_subnormal(self) -> Self::Bool {
201 self.extract_exponent_field()
202 .eq(self.ctx().make(Self::PrimFloat::ZERO_SUBNORMAL_EXPONENT))
204 fn from_bits(v: Self::BitsType) -> Self;
205 fn to_bits(self) -> Self::BitsType;
206 fn extract_exponent_field(self) -> Self::BitsType {
207 let mask = self.ctx().make(Self::PrimFloat::EXPONENT_FIELD_MASK);
208 let shift = self.ctx().make(Self::PrimFloat::EXPONENT_FIELD_SHIFT);
209 (self.to_bits() & mask) >> shift
211 fn extract_exponent_unbiased(self) -> Self::SignedBitsType {
212 Self::sub_exponent_bias(self.extract_exponent_field())
214 fn extract_mantissa_field(self) -> Self::BitsType {
215 let mask = self.ctx().make(Self::PrimFloat::MANTISSA_FIELD_MASK);
216 self.to_bits() & mask
218 fn is_sign_negative(self) -> Self::Bool {
219 let mask = self.ctx().make(Self::PrimFloat::SIGN_FIELD_MASK);
221 .make::<Self::BitsType>(0.to())
222 .ne(self.to_bits() & mask)
224 fn is_sign_positive(self) -> Self::Bool {
225 let mask = self.ctx().make(Self::PrimFloat::SIGN_FIELD_MASK);
227 .make::<Self::BitsType>(0.to())
228 .eq(self.to_bits() & mask)
230 fn extract_sign_field(self) -> Self::BitsType {
231 let shift = self.ctx().make(Self::PrimFloat::SIGN_FIELD_SHIFT);
232 self.to_bits() >> shift
235 sign_field: Self::BitsType,
236 exponent_field: Self::BitsType,
237 mantissa_field: Self::BitsType,
239 let sign_shift = sign_field.ctx().make(Self::PrimFloat::SIGN_FIELD_SHIFT);
240 let exponent_shift = sign_field.ctx().make(Self::PrimFloat::EXPONENT_FIELD_SHIFT);
242 (sign_field << sign_shift) | (exponent_field << exponent_shift) | mantissa_field,
245 fn with_exponent_field(self, exponent_field: Self::BitsType) -> Self {
246 let exponent_shift = self.ctx().make(Self::PrimFloat::EXPONENT_FIELD_SHIFT);
247 let not_exponent_mask = self.ctx().make(!Self::PrimFloat::EXPONENT_FIELD_MASK);
248 Self::from_bits((self.to_bits() & not_exponent_mask) | (exponent_field << exponent_shift))
250 fn with_exponent_unbiased(self, exponent: Self::SignedBitsType) -> Self {
251 self.with_exponent_field(Self::add_exponent_bias(exponent))
253 fn sub_exponent_bias(exponent_field: Self::BitsType) -> Self::SignedBitsType {
254 Self::SignedBitsType::cvt_from(exponent_field)
257 .make(Self::PrimFloat::EXPONENT_BIAS_SIGNED)
259 fn add_exponent_bias(exponent: Self::SignedBitsType) -> Self::BitsType {
260 (exponent + exponent.ctx().make(Self::PrimFloat::EXPONENT_BIAS_SIGNED)).to()
264 pub trait Bool: Make<Prim = bool> + BitOps + Select<Self> {}
266 pub trait Select<T> {
267 fn select(self, true_v: T, false_v: T) -> T;
270 pub trait Compare: Make {
271 type Bool: Bool + Select<Self> + Make<Context = Self::Context>;
272 fn eq(self, rhs: Self) -> Self::Bool;
273 fn ne(self, rhs: Self) -> Self::Bool;
274 fn lt(self, rhs: Self) -> Self::Bool;
275 fn gt(self, rhs: Self) -> Self::Bool;
276 fn le(self, rhs: Self) -> Self::Bool;
277 fn ge(self, rhs: Self) -> Self::Bool;