]> git.proxmox.com Git - rustc.git/blob - vendor/packed_simd_2/src/api/cast.rs
New upstream version 1.74.1+dfsg1
[rustc.git] / vendor / packed_simd_2 / src / api / cast.rs
1 //! Implementation of `FromCast` and `IntoCast`.
2 #![allow(clippy::module_name_repetitions)]
3
4 /// Numeric cast from `T` to `Self`.
5 ///
6 /// > Note: This is a temporary workaround until the conversion traits
7 /// specified > in [RFC2484] are implemented.
8 ///
9 /// Numeric cast between vectors with the same number of lanes, such that:
10 ///
11 /// * casting integer vectors whose lane types have the same size (e.g. `i32xN`
12 /// -> `u32xN`) is a **no-op**,
13 ///
14 /// * casting from a larger integer to a smaller integer (e.g. `u32xN` ->
15 /// `u8xN`) will **truncate**,
16 ///
17 /// * casting from a smaller integer to a larger integer (e.g. `u8xN` ->
18 /// `u32xN`) will:
19 /// * **zero-extend** if the source is unsigned, or
20 /// * **sign-extend** if the source is signed,
21 ///
22 /// * casting from a float to an integer will **round the float towards zero**,
23 ///
24 /// * casting from an integer to float will produce the floating point
25 /// representation of the integer, **rounding to nearest, ties to even**,
26 ///
27 /// * casting from an `f32` to an `f64` is perfect and lossless,
28 ///
29 /// * casting from an `f64` to an `f32` **rounds to nearest, ties to even**.
30 ///
31 /// [RFC2484]: https://github.com/rust-lang/rfcs/pull/2484
32 pub trait FromCast<T>: crate::marker::Sized {
33 /// Numeric cast from `T` to `Self`.
34 fn from_cast(_: T) -> Self;
35 }
36
37 /// Numeric cast from `Self` to `T`.
38 ///
39 /// > Note: This is a temporary workaround until the conversion traits
40 /// specified > in [RFC2484] are implemented.
41 ///
42 /// Numeric cast between vectors with the same number of lanes, such that:
43 ///
44 /// * casting integer vectors whose lane types have the same size (e.g. `i32xN`
45 /// -> `u32xN`) is a **no-op**,
46 ///
47 /// * casting from a larger integer to a smaller integer (e.g. `u32xN` ->
48 /// `u8xN`) will **truncate**,
49 ///
50 /// * casting from a smaller integer to a larger integer (e.g. `u8xN` ->
51 /// `u32xN`) will:
52 /// * **zero-extend** if the source is unsigned, or
53 /// * **sign-extend** if the source is signed,
54 ///
55 /// * casting from a float to an integer will **round the float towards zero**,
56 ///
57 /// * casting from an integer to float will produce the floating point
58 /// representation of the integer, **rounding to nearest, ties to even**,
59 ///
60 /// * casting from an `f32` to an `f64` is perfect and lossless,
61 ///
62 /// * casting from an `f64` to an `f32` **rounds to nearest, ties to even**.
63 ///
64 /// [RFC2484]: https://github.com/rust-lang/rfcs/pull/2484
65 pub trait Cast<T>: crate::marker::Sized {
66 /// Numeric cast from `self` to `T`.
67 fn cast(self) -> T;
68 }
69
70 /// `FromCast` implies `Cast`.
71 impl<T, U> Cast<U> for T
72 where
73 U: FromCast<T>,
74 {
75 #[inline]
76 fn cast(self) -> U {
77 U::from_cast(self)
78 }
79 }
80
81 /// `FromCast` and `Cast` are reflexive
82 impl<T> FromCast<T> for T {
83 #[inline]
84 fn from_cast(t: Self) -> Self {
85 t
86 }
87 }
88
89 #[macro_use]
90 mod macros;
91
92 mod v16;
93 pub use self::v16::*;
94
95 mod v32;
96 pub use self::v32::*;
97
98 mod v64;
99 pub use self::v64::*;
100
101 mod v128;
102 pub use self::v128::*;
103
104 mod v256;
105 pub use self::v256::*;
106
107 mod v512;
108 pub use self::v512::*;