1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
#![no_std]
#![feature(core_intrinsics)]
#![warn(missing_docs)]
#![allow(unused_imports)]
#![allow(unused_macros)]

//! `hektor`: A math lib for hekkin vectors.
//!
//! This crate is core only, so it should work basically anywhere. Various types
//! are provided that all attempt to use explicit `sse` SIMD as much as possible
//! so that we don't have to just trust in the auto-vectorizer (because it's not
//! that smart). Fallbacks are provided when `sse` isn't available.
//!
//! ```toml
//! [dependencies]
//! nalgebra = "0.17"
//! ```
//!
//! ### SIMD Policy
//!
//! All CPU feature selection happens _at compile time_. Note that by default on
//! `i686` and `x86_64` you'll only have the `sse2` feature enabled. If you want
//! to have the additional features you'll need to enable them via the
//! [RUSTFLAGS](https://rust-lang-nursery.github.io/packed_simd/perf-guide/target-feature/rustflags.html)
//! setting. You can configure `RUSTFLAGS` via your [cargo config
//! file](https://doc.rust-lang.org/cargo/reference/config.html) so that you
//! don't forget to set it during a build.
//!
//! I might support `neon` some day, but I'm focusing on the `sse` stuff first.
//!
//! ### This Isn't A Generic Linear Algebra Crate
//!
//! It's just for computer graphics sorts of things. Dimensions cap out at 4 and
//! there's no generics to be found.

// Note(Lokathor): You'll only see this note if you've decided to read the
// source itself. If you're going to be source diving this library just be aware
// that I do all the explicit SIMD work with macros instead of functions, just
// so that the optimizer and inliner can't get in each other's way at all.

#[cfg(target_arch = "x86")]
pub(crate) use core::arch::x86::*;
#[cfg(target_arch = "x86_64")]
pub(crate) use core::arch::x86_64::*;
pub(crate) use core::{cmp::*, intrinsics::sqrtf32, ops::*};

#[cfg(feature = "serde1")]
extern crate serde;
#[cfg(feature = "serde1")]
#[macro_use]
extern crate serde_derive;

/// Use this to make it simpler to handle the branch between using `sse2` or
/// not.
///
/// The SIMD path is automatically wrapped in `unsafe`. Also, the cargo feature
/// `debug_force_no_sse2` will cause this macro to pick the non-sse2 path even
/// if the CPU supports it. Use this for easy testing of the fallback path.
#[macro_export]
macro_rules! if_sse2 {
  ($yes:block else $no:block) => {
    #[cfg(all(target_feature = "sse2", not(feature = "debug_force_no_sse2")))]
    #[allow(unused_unsafe)]
    unsafe {
      $yes
    }
    #[cfg(not(all(target_feature = "sse2", not(feature = "debug_force_no_sse2"))))]
    $no
  };
}

#[test]
fn test_if_sse2_macro() {
  if_sse2! {{
    assert!(cfg!(target_feature="sse2") && !cfg!(feature="debug_force_no_sse2"))
  } else {
    assert!(!(cfg!(target_feature="sse2") && !cfg!(feature="debug_force_no_sse2")))
  }}
}

/// Use this to make it simpler to handle the branch between using `sse3` or
/// not.
///
/// The SIMD path is automatically wrapped in `unsafe`. Also, the cargo feature
/// `debug_force_no_sse3` will cause this macro to pick the non-sse3 path even
/// if the CPU supports it. Use this for easy testing of the fallback path.
#[macro_export]
macro_rules! if_sse3 {
  ($yes:block else $no:block) => {
    #[cfg(all(target_feature = "sse3", not(feature = "debug_force_no_sse3")))]
    #[allow(unused_unsafe)]
    unsafe {
      $yes
    }
    #[cfg(not(all(target_feature = "sse3", not(feature = "debug_force_no_sse3"))))]
    $no
  };
}

#[test]
fn test_if_sse3_macro() {
  if_sse3! {{
    assert!(cfg!(target_feature="sse3") && !cfg!(feature="debug_force_no_sse3"))
  } else {
    assert!(!(cfg!(target_feature="sse3") && !cfg!(feature="debug_force_no_sse3")))
  }}
}

/// Use this to make it simpler to handle the branch between using `ssse3` or
/// not.
///
/// The SIMD path is automatically wrapped in `unsafe`. Also, the cargo feature
/// `debug_force_no_ssse3` will cause this macro to pick the non-ssse3 path even
/// if the CPU supports it. Use this for easy testing of the fallback path.
#[macro_export]
macro_rules! if_ssse3 {
  ($yes:block else $no:block) => {
    #[cfg(all(target_feature = "ssse3", not(feature = "debug_force_no_ssse3")))]
    #[allow(unused_unsafe)]
    unsafe {
      $yes
    }
    #[cfg(not(all(target_feature = "ssse3", not(feature = "debug_force_no_ssse3"))))]
    $no
  };
}

#[test]
fn test_if_ssse3_macro() {
  if_ssse3! {{
    assert!(cfg!(target_feature="ssse3") && !cfg!(feature="debug_force_no_ssse3"))
  } else {
    assert!(!(cfg!(target_feature="ssse3") && !cfg!(feature="debug_force_no_ssse3")))
  }}
}

/// Use this to make it simpler to handle the branch between using `sse4.1` or
/// not.
///
/// The SIMD path is automatically wrapped in `unsafe`. Also, the cargo feature
/// `debug_force_no_sse41` will cause this macro to pick the non-sse4.1 path
/// even if the CPU supports it. Use this for easy testing of the fallback path.
#[macro_export]
macro_rules! if_sse41 {
  ($yes:block else $no:block) => {
    #[cfg(all(target_feature = "sse4.1", not(feature = "debug_force_no_sse41")))]
    #[allow(unused_unsafe)]
    unsafe {
      $yes
    }
    #[cfg(not(all(target_feature = "sse4.1", not(feature = "debug_force_no_sse41"))))]
    $no
  };
}

#[test]
fn test_if_sse41_macro() {
  if_sse41! {{
    assert!(cfg!(target_feature="sse4.1") && !cfg!(feature="debug_force_no_sse41"))
  } else {
    assert!(!(cfg!(target_feature="sse4.1") && !cfg!(feature="debug_force_no_sse41")))
  }}
}

/// Use this to make it simpler to handle the branch between using `sse4.2` or
/// not.
///
/// The SIMD path is automatically wrapped in `unsafe`. Also, the cargo feature
/// `debug_force_no_sse42` will cause this macro to pick the non-sse4.2 path
/// even if the CPU supports it. Use this for easy testing of the fallback path.
#[macro_export]
macro_rules! if_sse42 {
  ($yes:block else $no:block) => {
    #[cfg(all(target_feature = "sse4.2", not(feature = "debug_force_no_sse42")))]
    #[allow(unused_unsafe)]
    unsafe {
      $yes
    }
    #[cfg(not(all(target_feature = "sse4.2", not(feature = "debug_force_no_sse42"))))]
    $no
  };
}

#[test]
fn test_if_sse42_macro() {
  if_sse42! {{
    assert!(cfg!(target_feature="sse4.2") && !cfg!(feature="debug_force_no_sse42"))
  } else {
    assert!(!(cfg!(target_feature="sse4.2") && !cfg!(feature="debug_force_no_sse42")))
  }}
}

macro_rules! load_ps {
  ($x:expr) => {{
    _mm_load_ps($x.as_ptr())
  }};
}

macro_rules! store_ps {
  ($x:expr, $m:expr) => {{
    _mm_store_ps($x.as_mut_ptr(), $m)
  }};
}

macro_rules! new_store_ps {
  ($m:expr) => {{
    let mut out = Self::zero();
    _mm_store_ps(out.0.as_mut_ptr(), $m);
    out
  }};
}

macro_rules! horizontal_sum {
  ($m:expr) => {{
    let v = $m;
    let mut shuffled = _mm_shuffle_ps(v, v, 0b10_11_00_01);
    let mut sums = _mm_add_ps(v, shuffled);
    shuffled = _mm_movehl_ps(shuffled, sums);
    sums = _mm_add_ss(sums, shuffled);
    _mm_cvtss_f32(sums)
  }};
}

#[allow(missing_docs)]
pub mod ivec2;
#[allow(missing_docs)]
pub mod ivec3;
#[allow(missing_docs)]
pub mod ivec4;
#[allow(missing_docs)]
pub mod mat2;
#[allow(missing_docs)]
pub mod mat2x3;
#[allow(missing_docs)]
pub mod mat2x4;
#[allow(missing_docs)]
pub mod mat3;
#[allow(missing_docs)]
pub mod mat3x2;
#[allow(missing_docs)]
pub mod mat3x4;
pub mod mat4;
#[allow(missing_docs)]
pub mod mat4x2;
#[allow(missing_docs)]
pub mod mat4x3;
#[allow(missing_docs)]
pub mod quat;
#[allow(missing_docs)]
pub mod vec2;
#[allow(missing_docs)]
pub mod vec3;
pub mod vec4;

pub use ivec2::*;
pub use ivec3::*;
pub use ivec4::*;
pub use mat2::*;
pub use mat2x3::*;
pub use mat2x4::*;
pub use mat3::*;
pub use mat3x2::*;
pub use mat3x4::*;
pub use mat4::*;
pub use mat4x2::*;
pub use mat4x3::*;
pub use quat::*;
pub use vec2::*;
pub use vec3::*;
pub use vec4::*;