3 traits::{Bool, Compare, Context, ConvertFrom, Float, Int, Make, SInt, Select, UInt},
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 #[derive(Clone, Copy, PartialEq, Eq, Hash, Debug, Default)]
13 #[derive(Clone, Copy, PartialEq, Eq, PartialOrd, Ord, Hash, Debug, Default)]
15 pub struct Value<T>(pub T);
17 macro_rules! impl_convert_from {
18 ($first:ident $(, $ty:ident)*) => {
20 impl ConvertFrom<Value<$first>> for Value<$ty> {
21 fn cvt_from(v: Value<$first>) -> Self {
22 Value(ConvertFrom::cvt_from(v.0))
25 impl ConvertFrom<Value<$ty>> for Value<$first> {
26 fn cvt_from(v: Value<$ty>) -> Self {
27 Value(ConvertFrom::cvt_from(v.0))
31 impl_convert_from![$($ty),*];
37 impl_convert_from![u8, i8, u16, i16, F16, u32, i32, u64, i64, f32, f64];
39 macro_rules! impl_bit_ops {
41 impl BitAnd for Value<$ty> {
44 fn bitand(self, rhs: Self) -> Self {
49 impl BitOr for Value<$ty> {
52 fn bitor(self, rhs: Self) -> Self {
57 impl BitXor for Value<$ty> {
60 fn bitxor(self, rhs: Self) -> Self {
65 impl Not for Value<$ty> {
68 fn not(self) -> Self {
73 impl BitAndAssign for Value<$ty> {
74 fn bitand_assign(&mut self, rhs: Self) {
79 impl BitOrAssign for Value<$ty> {
80 fn bitor_assign(&mut self, rhs: Self) {
85 impl BitXorAssign for Value<$ty> {
86 fn bitxor_assign(&mut self, rhs: Self) {
93 macro_rules! impl_wrapping_int_ops {
95 impl Add for Value<$ty> {
98 fn add(self, rhs: Self) -> Self {
99 Value(self.0.wrapping_add(rhs.0))
103 impl Sub for Value<$ty> {
106 fn sub(self, rhs: Self) -> Self {
107 Value(self.0.wrapping_sub(rhs.0))
111 impl Mul for Value<$ty> {
114 fn mul(self, rhs: Self) -> Self {
115 Value(self.0.wrapping_mul(rhs.0))
119 impl Div for Value<$ty> {
122 fn div(self, rhs: Self) -> Self {
123 Value(self.0.wrapping_div(rhs.0))
127 impl Rem for Value<$ty> {
130 fn rem(self, rhs: Self) -> Self {
131 Value(self.0.wrapping_rem(rhs.0))
135 impl Shl for Value<$ty> {
138 fn shl(self, rhs: Self) -> Self {
139 Value(self.0.wrapping_shl(rhs.0 as u32))
143 impl Shr for Value<$ty> {
146 fn shr(self, rhs: Self) -> Self {
147 Value(self.0.wrapping_shr(rhs.0 as u32))
151 impl Neg for Value<$ty> {
154 fn neg(self) -> Self {
155 Value(self.0.wrapping_neg())
159 impl AddAssign for Value<$ty> {
160 fn add_assign(&mut self, rhs: Self) {
161 *self = self.add(rhs);
165 impl SubAssign for Value<$ty> {
166 fn sub_assign(&mut self, rhs: Self) {
167 *self = self.sub(rhs);
171 impl MulAssign for Value<$ty> {
172 fn mul_assign(&mut self, rhs: Self) {
173 *self = self.mul(rhs);
177 impl DivAssign for Value<$ty> {
178 fn div_assign(&mut self, rhs: Self) {
179 *self = self.div(rhs);
183 impl RemAssign for Value<$ty> {
184 fn rem_assign(&mut self, rhs: Self) {
185 *self = self.rem(rhs);
189 impl ShlAssign for Value<$ty> {
190 fn shl_assign(&mut self, rhs: Self) {
191 *self = self.shl(rhs);
195 impl ShrAssign for Value<$ty> {
196 fn shr_assign(&mut self, rhs: Self) {
197 *self = self.shr(rhs);
202 macro_rules! impl_int {
205 impl_wrapping_int_ops!($ty);
206 impl Int for Value<$ty> {
207 fn leading_zeros(self) -> Self {
208 Value(self.0.leading_zeros() as $ty)
210 fn leading_ones(self) -> Self {
211 Value(self.0.leading_ones() as $ty)
213 fn trailing_zeros(self) -> Self {
214 Value(self.0.trailing_zeros() as $ty)
216 fn trailing_ones(self) -> Self {
217 Value(self.0.trailing_ones() as $ty)
219 fn count_zeros(self) -> Self {
220 Value(self.0.count_zeros() as $ty)
222 fn count_ones(self) -> Self {
223 Value(self.0.count_ones() as $ty)
229 macro_rules! impl_uint {
230 ($($ty:ident),*) => {
233 impl UInt for Value<$ty> {}
238 impl_uint![u8, u16, u32, u64];
240 macro_rules! impl_sint {
241 ($($ty:ident),*) => {
244 impl SInt for Value<$ty> {}
249 impl_sint![i8, i16, i32, i64];
251 macro_rules! impl_float_ops {
253 impl Add for Value<$ty> {
256 fn add(self, rhs: Self) -> Self {
257 Value(self.0.add(rhs.0))
261 impl Sub for Value<$ty> {
264 fn sub(self, rhs: Self) -> Self {
265 Value(self.0.sub(rhs.0))
269 impl Mul for Value<$ty> {
272 fn mul(self, rhs: Self) -> Self {
273 Value(self.0.mul(rhs.0))
277 impl Div for Value<$ty> {
280 fn div(self, rhs: Self) -> Self {
281 Value(self.0.div(rhs.0))
285 impl Rem for Value<$ty> {
288 fn rem(self, rhs: Self) -> Self {
289 Value(self.0.rem(rhs.0))
293 impl Neg for Value<$ty> {
296 fn neg(self) -> Self {
301 impl AddAssign for Value<$ty> {
302 fn add_assign(&mut self, rhs: Self) {
303 *self = self.add(rhs);
307 impl SubAssign for Value<$ty> {
308 fn sub_assign(&mut self, rhs: Self) {
309 *self = self.sub(rhs);
313 impl MulAssign for Value<$ty> {
314 fn mul_assign(&mut self, rhs: Self) {
315 *self = self.mul(rhs);
319 impl DivAssign for Value<$ty> {
320 fn div_assign(&mut self, rhs: Self) {
321 *self = self.div(rhs);
325 impl RemAssign for Value<$ty> {
326 fn rem_assign(&mut self, rhs: Self) {
327 *self = self.rem(rhs);
333 impl_float_ops!(F16);
335 macro_rules! impl_float {
336 ($ty:ident, $bits:ty, $signed_bits:ty) => {
337 impl_float_ops!($ty);
338 impl Float for Value<$ty> {
339 type FloatEncoding = $ty;
340 type BitsType = Value<$bits>;
341 type SignedBitsType = Value<$signed_bits>;
342 fn abs(self) -> Self {
343 #[cfg(feature = "std")]
344 return Value(self.0.abs());
345 #[cfg(not(feature = "std"))]
348 fn trunc(self) -> Self {
349 #[cfg(feature = "std")]
350 return Value(self.0.trunc());
351 #[cfg(not(feature = "std"))]
354 fn ceil(self) -> Self {
355 #[cfg(feature = "std")]
356 return Value(self.0.ceil());
357 #[cfg(not(feature = "std"))]
360 fn floor(self) -> Self {
361 #[cfg(feature = "std")]
362 return Value(self.0.floor());
363 #[cfg(not(feature = "std"))]
366 fn round(self) -> Self {
367 #[cfg(feature = "std")]
368 return Value(self.0.round());
369 #[cfg(not(feature = "std"))]
372 #[cfg(feature = "fma")]
373 fn fma(self, a: Self, b: Self) -> Self {
374 Value(self.0.mul_add(a.0, b.0))
376 fn is_nan(self) -> Self::Bool {
377 Value(self.0.is_nan())
379 fn is_infinite(self) -> Self::Bool {
380 Value(self.0.is_infinite())
382 fn is_finite(self) -> Self::Bool {
383 Value(self.0.is_finite())
385 fn from_bits(v: Self::BitsType) -> Self {
386 Value(<$ty>::from_bits(v.0))
388 fn to_bits(self) -> Self::BitsType {
389 Value(self.0.to_bits())
395 impl_float!(f32, u32, i32);
396 impl_float!(f64, u64, i64);
398 macro_rules! impl_compare_using_partial_cmp {
401 impl Compare for Value<$ty> {
402 type Bool = Value<bool>;
403 fn eq(self, rhs: Self) -> Self::Bool {
406 fn ne(self, rhs: Self) -> Self::Bool {
409 fn lt(self, rhs: Self) -> Self::Bool {
412 fn gt(self, rhs: Self) -> Self::Bool {
415 fn le(self, rhs: Self) -> Self::Bool {
418 fn ge(self, rhs: Self) -> Self::Bool {
426 impl_compare_using_partial_cmp![bool, u8, i8, u16, i16, F16, u32, i32, f32, u64, i64, f64];
428 impl Bool for Value<bool> {}
432 impl<T> Select<Value<T>> for Value<bool> {
433 fn select(self, true_v: Value<T>, false_v: Value<T>) -> Value<T> {
442 macro_rules! impl_from {
443 ($src:ident => [$($dest:ident),*]) => {
445 impl From<Value<$src>> for Value<$dest> {
446 fn from(v: Value<$src>) -> Self {
454 impl_from!(u8 => [u16, i16, F16, u32, i32, f32, u64, i64, f64]);
455 impl_from!(u16 => [u32, i32, f32, u64, i64, f64]);
456 impl_from!(u32 => [u64, i64, f64]);
457 impl_from!(i8 => [i16, F16, i32, f32, i64, f64]);
458 impl_from!(i16 => [i32, f32, i64, f64]);
459 impl_from!(i32 => [i64, f64]);
460 impl_from!(F16 => [f32, f64]);
461 impl_from!(f32 => [f64]);
463 macro_rules! impl_context {
465 impl Context for Scalar {
466 $(type $name:ident = Value<$ty:ident>;)*
468 $(type $vec_name:ident = Value<$vec_ty:ident>;)*
471 impl Context for Scalar {
472 $(type $name = Value<$ty>;)*
473 $(type $vec_name = Value<$vec_ty>;)*
477 impl Make for Value<$ty> {
479 type Context = Scalar;
480 fn ctx(self) -> Self::Context {
483 fn make(_ctx: Self::Context, v: Self::Prim) -> Self {
492 impl Context for Scalar {
493 type Bool = Value<bool>;
496 type U16 = Value<u16>;
497 type I16 = Value<i16>;
498 type F16 = Value<F16>;
499 type U32 = Value<u32>;
500 type I32 = Value<i32>;
501 type F32 = Value<f32>;
502 type U64 = Value<u64>;
503 type I64 = Value<i64>;
504 type F64 = Value<f64>;
506 type VecBool8 = Value<bool>;
507 type VecU8 = Value<u8>;
508 type VecI8 = Value<i8>;
509 type VecBool16 = Value<bool>;
510 type VecU16 = Value<u16>;
511 type VecI16 = Value<i16>;
512 type VecF16 = Value<F16>;
513 type VecBool32 = Value<bool>;
514 type VecU32 = Value<u32>;
515 type VecI32 = Value<i32>;
516 type VecF32 = Value<f32>;
517 type VecBool64 = Value<bool>;
518 type VecU64 = Value<u64>;
519 type VecI64 = Value<i64>;
520 type VecF64 = Value<f64>;