Skip to main content

safe_arch/x86_x64/
m128d_.rs

1//! This module is for the `m128d` wrapper type, its bonus methods, and all
2//! necessary trait impls.
3//!
4//! Intrinsics should _not_ be in this module! They should all be free-functions
5//! in the other modules, sorted by CPU target feature.
6
7use super::*;
8
9/// The data for a 128-bit SSE register of two `f64` values.
10///
11/// * This is _very similar to_ having `[f64; 2]`. The main difference is that
12///   it's aligned to 16 instead of just 4, and of course you can perform
13///   various intrinsic operations on it.
14#[repr(transparent)]
15#[allow(non_camel_case_types)]
16pub struct m128d(pub __m128d);
17
18#[cfg(feature = "bytemuck")]
19unsafe impl bytemuck::Zeroable for m128d {}
20#[cfg(feature = "bytemuck")]
21unsafe impl bytemuck::Pod for m128d {}
22#[cfg(feature = "bytemuck")]
23unsafe impl bytemuck::TransparentWrapper<__m128d> for m128d {}
24
25impl m128d {
26  /// Transmutes the `m128d` to an array.
27  ///
28  /// Same as `m.into()`, just lets you be more explicit about what's happening.
29  #[must_use]
30  #[inline(always)]
31  pub fn to_array(self) -> [f64; 2] {
32    self.into()
33  }
34
35  /// Transmutes an array into `m128d`.
36  ///
37  /// Same as `m128d::from(arr)`, it just lets you be more explicit about what's
38  /// happening.
39  #[must_use]
40  #[inline(always)]
41  pub fn from_array(f: [f64; 2]) -> Self {
42    f.into()
43  }
44
45  //
46
47  /// Converts into the bit patterns of these doubles (`[u64;2]`).
48  ///
49  /// Like [`f64::to_bits`](f64::to_bits), but both lanes at once.
50  #[must_use]
51  #[inline(always)]
52  pub fn to_bits(self) -> [u64; 2] {
53    unsafe { core::mem::transmute(self) }
54  }
55
56  /// Converts from the bit patterns of these doubles (`[u64;2]`).
57  ///
58  /// Like [`f64::from_bits`](f64::from_bits), but both lanes at once.
59  #[must_use]
60  #[inline(always)]
61  pub fn from_bits(bits: [u64; 2]) -> Self {
62    unsafe { core::mem::transmute(bits) }
63  }
64}
65
66impl Clone for m128d {
67  #[inline(always)]
68  fn clone(&self) -> Self {
69    *self
70  }
71}
72impl Copy for m128d {}
73
74impl Default for m128d {
75  #[inline(always)]
76  fn default() -> Self {
77    unsafe { core::mem::zeroed() }
78  }
79}
80
81impl From<[f64; 2]> for m128d {
82  #[inline(always)]
83  fn from(arr: [f64; 2]) -> Self {
84    // Safety: because this semantically moves the value from the input position
85    // (align8) to the output position (align16) it is fine to increase our
86    // required alignment without worry.
87    unsafe { core::mem::transmute(arr) }
88  }
89}
90
91impl From<m128d> for [f64; 2] {
92  #[inline(always)]
93  fn from(m: m128d) -> Self {
94    // We can of course transmute to a lower alignment
95    unsafe { core::mem::transmute(m) }
96  }
97}
98
99//
100// PLEASE KEEP ALL THE FORMAT IMPL JUNK AT THE END OF THE FILE
101//
102
103impl Debug for m128d {
104  /// Debug formats each double.
105  /// ```
106  /// # use safe_arch::*;
107  /// let f = format!("{:?}", m128d::default());
108  /// assert_eq!(&f, "m128d(0.0, 0.0)");
109  /// ```
110  fn fmt(&self, f: &mut core::fmt::Formatter) -> core::fmt::Result {
111    let a = self.to_array();
112    write!(f, "m128d(")?;
113    Debug::fmt(&a[0], f)?;
114    write!(f, ", ")?;
115    Debug::fmt(&a[1], f)?;
116    write!(f, ")")
117  }
118}
119
120impl Display for m128d {
121  /// Display formats each double, and leaves the type name off of the font.
122  /// ```
123  /// # use safe_arch::*;
124  /// let f = format!("{}", m128d::default());
125  /// assert_eq!(&f, "(0, 0)");
126  /// ```
127  fn fmt(&self, f: &mut core::fmt::Formatter) -> core::fmt::Result {
128    let a = self.to_array();
129    write!(f, "(")?;
130    Display::fmt(&a[0], f)?;
131    write!(f, ", ")?;
132    Display::fmt(&a[1], f)?;
133    write!(f, ")")
134  }
135}
136
137impl Binary for m128d {
138  /// Binary formats each double's bit pattern (via [`f64::to_bits`]).
139  /// ```
140  /// # use safe_arch::*;
141  /// let f = format!("{:b}", m128d::default());
142  /// assert_eq!(&f, "(0, 0)");
143  /// ```
144  fn fmt(&self, f: &mut core::fmt::Formatter) -> core::fmt::Result {
145    let a = self.to_array();
146    write!(f, "(")?;
147    Binary::fmt(&a[0].to_bits(), f)?;
148    write!(f, ", ")?;
149    Binary::fmt(&a[1].to_bits(), f)?;
150    write!(f, ")")
151  }
152}
153
154impl LowerExp for m128d {
155  /// LowerExp formats each double.
156  /// ```
157  /// # use safe_arch::*;
158  /// let f = format!("{:e}", m128d::default());
159  /// assert_eq!(&f, "(0e0, 0e0)");
160  /// ```
161  fn fmt(&self, f: &mut core::fmt::Formatter) -> core::fmt::Result {
162    let a = self.to_array();
163    write!(f, "(")?;
164    LowerExp::fmt(&a[0], f)?;
165    write!(f, ", ")?;
166    LowerExp::fmt(&a[1], f)?;
167    write!(f, ")")
168  }
169}
170
171impl UpperExp for m128d {
172  /// UpperExp formats each double.
173  /// ```
174  /// # use safe_arch::*;
175  /// let f = format!("{:E}", m128d::default());
176  /// assert_eq!(&f, "(0E0, 0E0)");
177  /// ```
178  fn fmt(&self, f: &mut core::fmt::Formatter) -> core::fmt::Result {
179    let a = self.to_array();
180    write!(f, "(")?;
181    UpperExp::fmt(&a[0], f)?;
182    write!(f, ", ")?;
183    UpperExp::fmt(&a[1], f)?;
184    write!(f, ")")
185  }
186}
187
188impl LowerHex for m128d {
189  /// LowerHex formats each double's bit pattern (via [`f64::to_bits`]).
190  /// ```
191  /// # use safe_arch::*;
192  /// let f = format!("{:x}", m128d::default());
193  /// assert_eq!(&f, "(0, 0)");
194  /// ```
195  fn fmt(&self, f: &mut core::fmt::Formatter) -> core::fmt::Result {
196    let a = self.to_array();
197    write!(f, "(")?;
198    LowerHex::fmt(&a[0].to_bits(), f)?;
199    write!(f, ", ")?;
200    LowerHex::fmt(&a[1].to_bits(), f)?;
201    write!(f, ")")
202  }
203}
204
205impl UpperHex for m128d {
206  /// UpperHex formats each double's bit pattern (via [`f64::to_bits`]).
207  /// ```
208  /// # use safe_arch::*;
209  /// let f = format!("{:X}", m128d::default());
210  /// assert_eq!(&f, "(0, 0)");
211  /// ```
212  fn fmt(&self, f: &mut core::fmt::Formatter) -> core::fmt::Result {
213    let a = self.to_array();
214    write!(f, "(")?;
215    UpperHex::fmt(&a[0].to_bits(), f)?;
216    write!(f, ", ")?;
217    UpperHex::fmt(&a[1].to_bits(), f)?;
218    write!(f, ")")
219  }
220}
221
222impl Octal for m128d {
223  /// Octal formats each double's bit pattern (via [`f64::to_bits`]).
224  /// ```
225  /// # use safe_arch::*;
226  /// let f = format!("{:o}", m128d::default());
227  /// assert_eq!(&f, "(0, 0)");
228  /// ```
229  fn fmt(&self, f: &mut core::fmt::Formatter) -> core::fmt::Result {
230    let a = self.to_array();
231    write!(f, "(")?;
232    Debug::fmt(&a[0].to_bits(), f)?;
233    write!(f, ", ")?;
234    Debug::fmt(&a[1].to_bits(), f)?;
235    write!(f, ")")
236  }
237}