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 /// reference used to build IR for Kazan; an empty type for `core::simd`
11 pub trait Context: Copy {
12 vector_math_proc_macro::make_context_types!();
13 fn make<T: Make<Context = Self>>(self, v: T::Prim) -> T {
18 pub trait Make: Copy {
20 type Context: Context;
21 fn ctx(self) -> Self::Context;
22 fn make(ctx: Self::Context, v: Self::Prim) -> Self;
25 pub trait ConvertFrom<T>: Sized {
26 fn cvt_from(v: T) -> Self;
29 impl<T> ConvertFrom<T> for T {
30 fn cvt_from(v: T) -> Self {
35 pub trait ConvertTo<T> {
39 impl<F, T: ConvertFrom<F>> ConvertTo<T> for F {
45 macro_rules! impl_convert_from_using_as {
46 ($first:ident $(, $ty:ident)*) => {
48 impl ConvertFrom<$first> for $ty {
49 fn cvt_from(v: $first) -> Self {
53 impl ConvertFrom<$ty> for $first {
54 fn cvt_from(v: $ty) -> Self {
59 impl_convert_from_using_as![$($ty),*];
65 impl_convert_from_using_as![u8, i8, u16, i16, u32, i32, u64, i64, f32, f64];
82 impl<T> Number for T where
99 + BitAnd<Output = Self>
100 + BitOr<Output = Self>
101 + BitXor<Output = Self>
109 impl<T> BitOps for T where
111 + BitAnd<Output = Self>
112 + BitOr<Output = Self>
113 + BitXor<Output = Self>
122 Number + BitOps + Shl<Output = Self> + Shr<Output = Self> + ShlAssign + ShrAssign
124 fn leading_zeros(self) -> Self;
125 fn leading_ones(self) -> Self {
126 self.not().leading_zeros()
128 fn trailing_zeros(self) -> Self;
129 fn trailing_ones(self) -> Self {
130 self.not().trailing_zeros()
132 fn count_zeros(self) -> Self {
133 self.not().count_ones()
135 fn count_ones(self) -> Self;
138 pub trait UInt: Int + Make<Prim = Self::PrimUInt> + ConvertFrom<Self::SignedType> {
139 type PrimUInt: PrimUInt<SignedType = <Self::SignedType as SInt>::PrimSInt>;
140 type SignedType: SInt
142 + Make<Context = Self::Context>
143 + Compare<Bool = Self::Bool>;
147 Int + Neg<Output = Self> + Make<Prim = Self::PrimSInt> + ConvertFrom<Self::UnsignedType>
149 type PrimSInt: PrimSInt<UnsignedType = <Self::UnsignedType as UInt>::PrimUInt>;
150 type UnsignedType: UInt
152 + Make<Context = Self::Context>
153 + Compare<Bool = Self::Bool>;
159 + Make<Prim = Self::PrimFloat>
160 + ConvertFrom<Self::SignedBitsType>
161 + ConvertFrom<Self::BitsType>
163 type PrimFloat: PrimFloat;
164 type BitsType: UInt<PrimUInt = <Self::PrimFloat as PrimFloat>::BitsType, SignedType = Self::SignedBitsType>
165 + Make<Context = Self::Context, Prim = <Self::PrimFloat as PrimFloat>::BitsType>
166 + Compare<Bool = Self::Bool>
168 type SignedBitsType: SInt<
169 PrimSInt = <Self::PrimFloat as PrimFloat>::SignedBitsType,
170 UnsignedType = Self::BitsType,
171 > + Make<Context = Self::Context, Prim = <Self::PrimFloat as PrimFloat>::SignedBitsType>
172 + Compare<Bool = Self::Bool>
174 fn abs(self) -> Self;
175 fn trunc(self) -> Self;
176 fn ceil(self) -> Self;
177 fn floor(self) -> Self;
178 /// round to nearest integer, unspecified which way half-way cases are rounded
179 fn round(self) -> Self;
180 /// returns `self * a + b` but only rounding once
181 #[cfg(feature = "fma")]
182 fn fma(self, a: Self, b: Self) -> Self;
183 /// returns `self * a + b` either using `fma` or `self * a + b`
184 fn mul_add_fast(self, a: Self, b: Self) -> Self {
185 #[cfg(feature = "fma")]
186 return self.fma(a, b);
187 #[cfg(not(feature = "fma"))]
190 fn is_nan(self) -> Self::Bool {
193 fn is_infinite(self) -> Self::Bool {
194 self.abs().eq(Self::infinity(self.ctx()))
196 fn infinity(ctx: Self::Context) -> Self {
197 Self::from_bits(ctx.make(Self::PrimFloat::INFINITY_BITS))
199 fn nan(ctx: Self::Context) -> Self {
200 Self::from_bits(ctx.make(Self::PrimFloat::NAN_BITS))
202 fn is_finite(self) -> Self::Bool;
203 fn is_zero_or_subnormal(self) -> Self::Bool {
204 self.extract_exponent_field()
205 .eq(self.ctx().make(Self::PrimFloat::ZERO_SUBNORMAL_EXPONENT))
207 fn from_bits(v: Self::BitsType) -> Self;
208 fn to_bits(self) -> Self::BitsType;
209 fn extract_exponent_field(self) -> Self::BitsType {
210 let mask = self.ctx().make(Self::PrimFloat::EXPONENT_FIELD_MASK);
211 let shift = self.ctx().make(Self::PrimFloat::EXPONENT_FIELD_SHIFT);
212 (self.to_bits() & mask) >> shift
214 fn extract_exponent_unbiased(self) -> Self::SignedBitsType {
215 Self::sub_exponent_bias(self.extract_exponent_field())
217 fn extract_mantissa_field(self) -> Self::BitsType {
218 let mask = self.ctx().make(Self::PrimFloat::MANTISSA_FIELD_MASK);
219 self.to_bits() & mask
221 fn sub_exponent_bias(exponent_field: Self::BitsType) -> Self::SignedBitsType {
222 Self::SignedBitsType::cvt_from(exponent_field)
225 .make(Self::PrimFloat::EXPONENT_BIAS_SIGNED)
227 fn add_exponent_bias(exponent: Self::SignedBitsType) -> Self::BitsType {
228 (exponent + exponent.ctx().make(Self::PrimFloat::EXPONENT_BIAS_SIGNED)).to()
232 pub trait Bool: Make + BitOps {}
234 pub trait Select<T>: Bool {
235 fn select(self, true_v: T, false_v: T) -> T;
238 pub trait Compare: Make {
239 type Bool: Bool + Select<Self>;
240 fn eq(self, rhs: Self) -> Self::Bool;
241 fn ne(self, rhs: Self) -> Self::Bool;
242 fn lt(self, rhs: Self) -> Self::Bool;
243 fn gt(self, rhs: Self) -> Self::Bool;
244 fn le(self, rhs: Self) -> Self::Bool;
245 fn ge(self, rhs: Self) -> Self::Bool;