|
| 1 | +//! Vertical floating-point `tanh` |
| 2 | +#![allow(unused)] |
| 3 | + |
| 4 | +// FIXME 64-bit 1 elem vectors tanh |
| 5 | + |
| 6 | +use crate::*; |
| 7 | + |
| 8 | +crate trait Tanh { |
| 9 | + fn tanh(self) -> Self; |
| 10 | +} |
| 11 | + |
| 12 | +macro_rules! define_tanh { |
| 13 | + |
| 14 | + ($name:ident, $basetype:ty, $simdtype:ty, $lanes:expr, $trait:path) => { |
| 15 | + fn $name(x: $simdtype) -> $simdtype { |
| 16 | + use core::intrinsics::transmute; |
| 17 | + let mut buf: [$basetype; $lanes] = unsafe { transmute(x) }; |
| 18 | + for elem in &mut buf { |
| 19 | + *elem = <$basetype as $trait>::tanh(*elem); |
| 20 | + } |
| 21 | + unsafe { transmute(buf) } |
| 22 | + } |
| 23 | + }; |
| 24 | + |
| 25 | + (f32 => $name:ident, $type:ty, $lanes:expr) => { |
| 26 | + define_tanh!($name, f32, $type, $lanes, libm::F32Ext); |
| 27 | + }; |
| 28 | + |
| 29 | + (f64 => $name:ident, $type:ty, $lanes:expr) => { |
| 30 | + define_tanh!($name, f64, $type, $lanes, libm::F64Ext); |
| 31 | + }; |
| 32 | +} |
| 33 | + |
| 34 | +// llvm does not seem to expose the hyperbolic versions of trigonometric functions; |
| 35 | +// we thus call the classical rust versions on all of them (which stem from cmath). |
| 36 | +define_tanh!(f32 => tanh_v2f32, f32x2, 2); |
| 37 | +define_tanh!(f32 => tanh_v4f32, f32x4, 4); |
| 38 | +define_tanh!(f32 => tanh_v8f32, f32x8, 8); |
| 39 | +define_tanh!(f32 => tanh_v16f32, f32x16, 16); |
| 40 | + |
| 41 | +define_tanh!(f64 => tanh_v2f64, f64x2, 2); |
| 42 | +define_tanh!(f64 => tanh_v4f64, f64x4, 4); |
| 43 | +define_tanh!(f64 => tanh_v8f64, f64x8, 8); |
| 44 | + |
| 45 | +fn tanh_f32(x: f32) -> f32 { |
| 46 | + libm::F32Ext::tanh(x) |
| 47 | +} |
| 48 | + |
| 49 | +fn tanh_f64(x: f64) -> f64 { |
| 50 | + libm::F64Ext::tanh(x) |
| 51 | +} |
| 52 | + |
| 53 | +gen_unary_impl_table!(Tanh, tanh); |
| 54 | + |
| 55 | +cfg_if! { |
| 56 | + if #[cfg(target_arch = "s390x")] { |
| 57 | + // FIXME: https://github.com/rust-lang-nursery/packed_simd/issues/14 |
| 58 | + impl_unary!(f32x2[f32; 2]: tanh_f32); |
| 59 | + impl_unary!(f32x4[f32; 4]: tanh_f32); |
| 60 | + impl_unary!(f32x8[f32; 8]: tanh_f32); |
| 61 | + impl_unary!(f32x16[f32; 16]: tanh_f32); |
| 62 | + |
| 63 | + impl_unary!(f64x2[f64; 2]: tanh_f64); |
| 64 | + impl_unary!(f64x4[f64; 4]: tanh_f64); |
| 65 | + impl_unary!(f64x8[f64; 8]: tanh_f64); |
| 66 | + } else if #[cfg(all(target_arch = "x86_64", feature = "sleef-sys"))] { |
| 67 | + use sleef_sys::*; |
| 68 | + cfg_if! { |
| 69 | + if #[cfg(target_feature = "avx2")] { |
| 70 | + impl_unary!(f32x2[t => f32x4]: Sleef_tanhf4_u10avx2128); |
| 71 | + impl_unary!(f32x16[h => f32x8]: Sleef_tanhf8_u10avx2); |
| 72 | + impl_unary!(f64x8[h => f64x4]: Sleef_tanhd4_u10avx2); |
| 73 | + |
| 74 | + impl_unary!(f32x4: Sleef_tanhf4_u10avx2128); |
| 75 | + impl_unary!(f32x8: Sleef_tanhf8_u10avx2); |
| 76 | + impl_unary!(f64x2: Sleef_tanhd2_u10avx2128); |
| 77 | + impl_unary!(f64x4: Sleef_tanhd4_u10avx2); |
| 78 | + } else if #[cfg(target_feature = "avx")] { |
| 79 | + impl_unary!(f32x2[t => f32x4]: Sleef_tanhf4_u10sse4); |
| 80 | + impl_unary!(f32x16[h => f32x8]: Sleef_tanhf8_u10avx); |
| 81 | + impl_unary!(f64x8[h => f64x4]: Sleef_tanhd4_u10avx); |
| 82 | + |
| 83 | + impl_unary!(f32x4: Sleef_tanhf4_u10sse4); |
| 84 | + impl_unary!(f32x8: Sleef_tanhf8_u10avx); |
| 85 | + impl_unary!(f64x2: Sleef_tanhd2_u10sse4); |
| 86 | + impl_unary!(f64x4: Sleef_tanhd4_u10avx); |
| 87 | + } else if #[cfg(target_feature = "sse4.2")] { |
| 88 | + impl_unary!(f32x2[t => f32x4]: Sleef_tanhf4_u10sse4); |
| 89 | + impl_unary!(f32x16[q => f32x4]: Sleef_tanhf4_u10sse4); |
| 90 | + impl_unary!(f64x8[q => f64x2]: Sleef_tanhd2_u10sse4); |
| 91 | + |
| 92 | + impl_unary!(f32x4: Sleef_tanhf4_u10sse4); |
| 93 | + impl_unary!(f32x8[h => f32x4]: Sleef_tanhf4_u10sse4); |
| 94 | + impl_unary!(f64x2: Sleef_tanhd2_u10sse4); |
| 95 | + impl_unary!(f64x4[h => f64x2]: Sleef_tanhd2_u10sse4); |
| 96 | + } else { |
| 97 | + impl_unary!(f32x2[f32; 2]: tanh_f32); |
| 98 | + impl_unary!(f32x16: tanh_v16f32); |
| 99 | + impl_unary!(f64x8: tanh_v8f64); |
| 100 | + |
| 101 | + impl_unary!(f32x4: tanh_v4f32); |
| 102 | + impl_unary!(f32x8: tanh_v8f32); |
| 103 | + impl_unary!(f64x2: tanh_v2f64); |
| 104 | + impl_unary!(f64x4: tanh_v4f64); |
| 105 | + } |
| 106 | + } |
| 107 | + } else { |
| 108 | + impl_unary!(f32x2[f32; 2]: tanh_f32); |
| 109 | + impl_unary!(f32x4: tanh_v4f32); |
| 110 | + impl_unary!(f32x8: tanh_v8f32); |
| 111 | + impl_unary!(f32x16: tanh_v16f32); |
| 112 | + |
| 113 | + impl_unary!(f64x2: tanh_v2f64); |
| 114 | + impl_unary!(f64x4: tanh_v4f64); |
| 115 | + impl_unary!(f64x8: tanh_v8f64); |
| 116 | + } |
| 117 | +} |
0 commit comments