1 use crate::{f16::F16, ieee754::FloatEncoding, scalar::Scalar};
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 ConvertTo<T> {
26 impl<T> ConvertTo<T> for T {
32 macro_rules! impl_convert_to_using_as {
33 ($first:ident $(, $ty:ident)*) => {
35 impl ConvertTo<$first> for $ty {
36 fn to(self) -> $first {
40 impl ConvertTo<$ty> for $first {
46 impl_convert_to_using_as![$($ty),*];
52 impl_convert_to_using_as![u8, i8, u16, i16, u32, i32, u64, i64, f32, f64];
69 impl<T> Number for T where
86 + BitAnd<Output = Self>
87 + BitOr<Output = Self>
88 + BitXor<Output = Self>
96 impl<T> BitOps for T where
98 + BitAnd<Output = Self>
99 + BitOr<Output = Self>
100 + BitXor<Output = Self>
109 Number + BitOps + Shl<Output = Self> + Shr<Output = Self> + ShlAssign + ShrAssign
111 fn leading_zeros(self) -> Self;
112 fn leading_ones(self) -> Self {
113 self.not().leading_zeros()
115 fn trailing_zeros(self) -> Self;
116 fn trailing_ones(self) -> Self {
117 self.not().trailing_zeros()
119 fn count_zeros(self) -> Self {
120 self.not().count_ones()
122 fn count_ones(self) -> Self;
125 pub trait UInt: Int {}
127 pub trait SInt: Int + Neg<Output = Self> {}
129 macro_rules! impl_int {
132 fn leading_zeros(self) -> Self {
133 self.leading_zeros() as Self
135 fn leading_ones(self) -> Self {
136 self.leading_ones() as Self
138 fn trailing_zeros(self) -> Self {
139 self.trailing_zeros() as Self
141 fn trailing_ones(self) -> Self {
142 self.trailing_ones() as Self
144 fn count_zeros(self) -> Self {
145 self.count_zeros() as Self
147 fn count_ones(self) -> Self {
148 self.count_ones() as Self
154 macro_rules! impl_uint {
155 ($($ty:ident),*) => {
163 impl_uint![u8, u16, u32, u64];
165 macro_rules! impl_sint {
166 ($($ty:ident),*) => {
174 impl_sint![i8, i16, i32, i64];
176 pub trait Float: Number + Neg<Output = Self> {
177 type FloatEncoding: FloatEncoding + Make<Context = Scalar, Prim = <Self as Make>::Prim>;
179 + Make<Context = Self::Context, Prim = <Self::FloatEncoding as Float>::BitsType>
180 + ConvertTo<Self::SignedBitsType>
181 + Compare<Bool = Self::Bool>;
182 type SignedBitsType: SInt
183 + Make<Context = Self::Context, Prim = <Self::FloatEncoding as Float>::SignedBitsType>
184 + ConvertTo<Self::BitsType>
185 + Compare<Bool = Self::Bool>;
186 fn abs(self) -> Self;
187 fn trunc(self) -> Self;
188 fn ceil(self) -> Self;
189 fn floor(self) -> Self;
190 /// round to nearest integer, unspecified which way half-way cases are rounded
191 fn round(self) -> Self;
192 /// returns `self * a + b` but only rounding once
193 #[cfg(feature = "fma")]
194 fn fma(self, a: Self, b: Self) -> Self;
195 /// returns `self * a + b` either using `fma` or `self * a + b`
196 fn mul_add_fast(self, a: Self, b: Self) -> Self {
197 #[cfg(feature = "fma")]
198 return self.fma(a, b);
199 #[cfg(not(feature = "fma"))]
202 fn is_nan(self) -> Self::Bool {
205 fn is_infinite(self) -> Self::Bool {
206 self.abs().eq(Self::infinity(self.ctx()))
208 fn infinity(ctx: Self::Context) -> Self {
209 Self::from_bits(ctx.make(Self::FloatEncoding::INFINITY_BITS))
211 fn nan(ctx: Self::Context) -> Self {
212 Self::from_bits(ctx.make(Self::FloatEncoding::NAN_BITS))
214 fn is_finite(self) -> Self::Bool;
215 fn is_zero_or_subnormal(self) -> Self::Bool {
216 self.extract_exponent_field().eq(self
218 .make(Self::FloatEncoding::ZERO_SUBNORMAL_EXPONENT))
220 fn from_bits(v: Self::BitsType) -> Self;
221 fn to_bits(self) -> Self::BitsType;
222 fn extract_exponent_field(self) -> Self::BitsType {
223 let mask = self.ctx().make(Self::FloatEncoding::EXPONENT_FIELD_MASK);
224 let shift = self.ctx().make(Self::FloatEncoding::EXPONENT_FIELD_SHIFT);
225 (self.to_bits() & mask) >> shift
227 fn extract_exponent_unbiased(self) -> Self::SignedBitsType {
228 Self::sub_exponent_bias(self.extract_exponent_field())
230 fn extract_mantissa_field(self) -> Self::BitsType {
231 let mask = self.ctx().make(Self::FloatEncoding::MANTISSA_FIELD_MASK);
232 self.to_bits() & mask
234 fn sub_exponent_bias(exponent_field: Self::BitsType) -> Self::SignedBitsType {
238 .make(Self::FloatEncoding::EXPONENT_BIAS_SIGNED)
240 fn add_exponent_bias(exponent: Self::SignedBitsType) -> Self::BitsType {
244 .make(Self::FloatEncoding::EXPONENT_BIAS_SIGNED))
249 macro_rules! impl_float {
250 ($ty:ty, $bits:ty, $signed_bits:ty) => {
252 type FloatEncoding = $ty;
253 type BitsType = $bits;
254 type SignedBitsType = $signed_bits;
255 fn abs(self) -> Self {
256 #[cfg(feature = "std")]
258 #[cfg(not(feature = "std"))]
261 fn trunc(self) -> Self {
262 #[cfg(feature = "std")]
264 #[cfg(not(feature = "std"))]
267 fn ceil(self) -> Self {
268 #[cfg(feature = "std")]
270 #[cfg(not(feature = "std"))]
273 fn floor(self) -> Self {
274 #[cfg(feature = "std")]
276 #[cfg(not(feature = "std"))]
279 fn round(self) -> Self {
280 #[cfg(feature = "std")]
282 #[cfg(not(feature = "std"))]
285 #[cfg(feature = "fma")]
286 fn fma(self, a: Self, b: Self) -> Self {
289 fn is_nan(self) -> Self::Bool {
292 fn is_infinite(self) -> Self::Bool {
295 fn is_finite(self) -> Self::Bool {
298 fn from_bits(v: Self::BitsType) -> Self {
301 fn to_bits(self) -> Self::BitsType {
308 impl_float!(f32, u32, i32);
309 impl_float!(f64, u64, i64);
311 pub trait Bool: Make + BitOps {}
313 impl Bool for bool {}
315 pub trait Select<T>: Bool {
316 fn select(self, true_v: T, false_v: T) -> T;
319 impl<T> Select<T> for bool {
320 fn select(self, true_v: T, false_v: T) -> T {
328 pub trait Compare: Make {
329 type Bool: Bool + Select<Self>;
330 fn eq(self, rhs: Self) -> Self::Bool;
331 fn ne(self, rhs: Self) -> Self::Bool;
332 fn lt(self, rhs: Self) -> Self::Bool;
333 fn gt(self, rhs: Self) -> Self::Bool;
334 fn le(self, rhs: Self) -> Self::Bool;
335 fn ge(self, rhs: Self) -> Self::Bool;
338 macro_rules! impl_compare_using_partial_cmp {
341 impl Compare for $ty {
343 fn eq(self, rhs: Self) -> Self::Bool {
346 fn ne(self, rhs: Self) -> Self::Bool {
349 fn lt(self, rhs: Self) -> Self::Bool {
352 fn gt(self, rhs: Self) -> Self::Bool {
355 fn le(self, rhs: Self) -> Self::Bool {
358 fn ge(self, rhs: Self) -> Self::Bool {
366 impl_compare_using_partial_cmp![bool, u8, i8, u16, i16, F16, u32, i32, f32, u64, i64, f64];