1 use crate::{f16::F16, ieee754::FloatEncoding};
3 Add, AddAssign, BitAnd, BitAndAssign, BitOr, BitOrAssign, BitXor, BitXorAssign, Div, DivAssign,
4 Mul, MulAssign, Neg, Not, Rem, RemAssign, Shl, ShlAssign, Shr, ShrAssign, Sub, SubAssign,
7 /// reference used to build IR for Kazan; an empty type for `core::simd`
8 pub trait Context: Copy {
9 vector_math_proc_macro::make_context_types!();
10 fn make<T: Make<Context = Self>>(self, v: T::Prim) -> T {
15 pub trait Make: Copy {
17 type Context: Context;
18 fn ctx(self) -> Self::Context;
19 fn make(ctx: Self::Context, v: Self::Prim) -> Self;
22 pub trait ConvertFrom<T>: Sized {
23 fn cvt_from(v: T) -> Self;
26 impl<T> ConvertFrom<T> for T {
27 fn cvt_from(v: T) -> Self {
32 pub trait ConvertTo<T> {
36 impl<F, T: ConvertFrom<F>> ConvertTo<T> for F {
42 macro_rules! impl_convert_from_using_as {
43 ($first:ident $(, $ty:ident)*) => {
45 impl ConvertFrom<$first> for $ty {
46 fn cvt_from(v: $first) -> Self {
50 impl ConvertFrom<$ty> for $first {
51 fn cvt_from(v: $ty) -> Self {
56 impl_convert_from_using_as![$($ty),*];
62 impl_convert_from_using_as![u8, i8, u16, i16, u32, i32, u64, i64, f32, f64];
79 impl<T> Number for T where
96 + BitAnd<Output = Self>
97 + BitOr<Output = Self>
98 + BitXor<Output = Self>
106 impl<T> BitOps for T where
108 + BitAnd<Output = Self>
109 + BitOr<Output = Self>
110 + BitXor<Output = Self>
119 Number + BitOps + Shl<Output = Self> + Shr<Output = Self> + ShlAssign + ShrAssign
121 fn leading_zeros(self) -> Self;
122 fn leading_ones(self) -> Self {
123 self.not().leading_zeros()
125 fn trailing_zeros(self) -> Self;
126 fn trailing_ones(self) -> Self {
127 self.not().trailing_zeros()
129 fn count_zeros(self) -> Self {
130 self.not().count_ones()
132 fn count_ones(self) -> Self;
135 pub trait UInt: Int {}
137 pub trait SInt: Int + Neg<Output = Self> {}
139 pub trait Float: Number + Neg<Output = Self> {
140 type FloatEncoding: FloatEncoding + From<<Self as Make>::Prim> + Into<<Self as Make>::Prim>;
142 + Make<Context = Self::Context, Prim = <Self::FloatEncoding as FloatEncoding>::BitsType>
143 + ConvertTo<Self::SignedBitsType>
144 + Compare<Bool = Self::Bool>;
145 type SignedBitsType: SInt
146 + Make<Context = Self::Context, Prim = <Self::FloatEncoding as FloatEncoding>::SignedBitsType>
147 + ConvertTo<Self::BitsType>
148 + Compare<Bool = Self::Bool>;
149 fn abs(self) -> Self;
150 fn trunc(self) -> Self;
151 fn ceil(self) -> Self;
152 fn floor(self) -> Self;
153 /// round to nearest integer, unspecified which way half-way cases are rounded
154 fn round(self) -> Self;
155 /// returns `self * a + b` but only rounding once
156 #[cfg(feature = "fma")]
157 fn fma(self, a: Self, b: Self) -> Self;
158 /// returns `self * a + b` either using `fma` or `self * a + b`
159 fn mul_add_fast(self, a: Self, b: Self) -> Self {
160 #[cfg(feature = "fma")]
161 return self.fma(a, b);
162 #[cfg(not(feature = "fma"))]
165 fn is_nan(self) -> Self::Bool {
168 fn is_infinite(self) -> Self::Bool {
169 self.abs().eq(Self::infinity(self.ctx()))
171 fn infinity(ctx: Self::Context) -> Self {
172 Self::from_bits(ctx.make(Self::FloatEncoding::INFINITY_BITS))
174 fn nan(ctx: Self::Context) -> Self {
175 Self::from_bits(ctx.make(Self::FloatEncoding::NAN_BITS))
177 fn is_finite(self) -> Self::Bool;
178 fn is_zero_or_subnormal(self) -> Self::Bool {
179 self.extract_exponent_field().eq(self
181 .make(Self::FloatEncoding::ZERO_SUBNORMAL_EXPONENT))
183 fn from_bits(v: Self::BitsType) -> Self;
184 fn to_bits(self) -> Self::BitsType;
185 fn extract_exponent_field(self) -> Self::BitsType {
186 let mask = self.ctx().make(Self::FloatEncoding::EXPONENT_FIELD_MASK);
187 let shift = self.ctx().make(Self::FloatEncoding::EXPONENT_FIELD_SHIFT);
188 (self.to_bits() & mask) >> shift
190 fn extract_exponent_unbiased(self) -> Self::SignedBitsType {
191 Self::sub_exponent_bias(self.extract_exponent_field())
193 fn extract_mantissa_field(self) -> Self::BitsType {
194 let mask = self.ctx().make(Self::FloatEncoding::MANTISSA_FIELD_MASK);
195 self.to_bits() & mask
197 fn sub_exponent_bias(exponent_field: Self::BitsType) -> Self::SignedBitsType {
201 .make(Self::FloatEncoding::EXPONENT_BIAS_SIGNED)
203 fn add_exponent_bias(exponent: Self::SignedBitsType) -> Self::BitsType {
207 .make(Self::FloatEncoding::EXPONENT_BIAS_SIGNED))
212 pub trait Bool: Make + BitOps {}
214 pub trait Select<T>: Bool {
215 fn select(self, true_v: T, false_v: T) -> T;
218 pub trait Compare: Make {
219 type Bool: Bool + Select<Self>;
220 fn eq(self, rhs: Self) -> Self::Bool;
221 fn ne(self, rhs: Self) -> Self::Bool;
222 fn lt(self, rhs: Self) -> Self::Bool;
223 fn gt(self, rhs: Self) -> Self::Bool;
224 fn le(self, rhs: Self) -> Self::Bool;
225 fn ge(self, rhs: Self) -> Self::Bool;