|
1 |
| -use core::arch::aarch64::{ |
2 |
| - float32x2_t, float64x1_t, vdup_n_f32, vdup_n_f64, vget_lane_f32, vget_lane_f64, vrndn_f32, |
3 |
| - vrndn_f64, |
4 |
| -}; |
| 1 | +//! Architecture-specific support for aarch64 with neon. |
5 | 2 |
|
6 |
| -pub fn rint(x: f64) -> f64 { |
7 |
| - // SAFETY: only requires target_feature=neon, ensured by `cfg_if` in parent module. |
8 |
| - let x_vec: float64x1_t = unsafe { vdup_n_f64(x) }; |
| 3 | +use core::arch::asm; |
9 | 4 |
|
10 |
| - // SAFETY: only requires target_feature=neon, ensured by `cfg_if` in parent module. |
11 |
| - let result_vec: float64x1_t = unsafe { vrndn_f64(x_vec) }; |
| 5 | +pub fn fma(mut x: f64, y: f64, z: f64) -> f64 { |
| 6 | + // SAFETY: `fmadd` is available with neon and has no side effects. |
| 7 | + unsafe { |
| 8 | + asm!( |
| 9 | + "fmadd {x:d}, {x:d}, {y:d}, {z:d}", |
| 10 | + x = inout(vreg) x, |
| 11 | + y = in(vreg) y, |
| 12 | + z = in(vreg) z, |
| 13 | + options(nomem, nostack, pure) |
| 14 | + ); |
| 15 | + } |
| 16 | + x |
| 17 | +} |
12 | 18 |
|
13 |
| - // SAFETY: only requires target_feature=neon, ensured by `cfg_if` in parent module. |
14 |
| - let result: f64 = unsafe { vget_lane_f64::<0>(result_vec) }; |
| 19 | +pub fn fmaf(mut x: f32, y: f32, z: f32) -> f32 { |
| 20 | + // SAFETY: `fmadd` is available with neon and has no side effects. |
| 21 | + unsafe { |
| 22 | + asm!( |
| 23 | + "fmadd {x:s}, {x:s}, {y:s}, {z:s}", |
| 24 | + x = inout(vreg) x, |
| 25 | + y = in(vreg) y, |
| 26 | + z = in(vreg) z, |
| 27 | + options(nomem, nostack, pure) |
| 28 | + ); |
| 29 | + } |
| 30 | + x |
| 31 | +} |
15 | 32 |
|
16 |
| - result |
| 33 | +pub fn rint(mut x: f64) -> f64 { |
| 34 | + // SAFETY: `frintn` is available with neon and has no side effects. |
| 35 | + // |
| 36 | + // `frintn` is always round-to-nearest which does not match the C specification, but Rust does |
| 37 | + // not support rounding modes. |
| 38 | + unsafe { |
| 39 | + asm!( |
| 40 | + "frintn {x:d}, {x:d}", |
| 41 | + x = inout(vreg) x, |
| 42 | + options(nomem, nostack, pure) |
| 43 | + ); |
| 44 | + } |
| 45 | + x |
17 | 46 | }
|
18 | 47 |
|
19 |
| -pub fn rintf(x: f32) -> f32 { |
20 |
| - // There's a scalar form of this instruction (FRINTN) but core::arch doesn't expose it, so we |
21 |
| - // have to use the vector form and drop the other lanes afterwards. |
| 48 | +pub fn rintf(mut x: f32) -> f32 { |
| 49 | + // SAFETY: `frintn` is available with neon and has no side effects. |
| 50 | + // |
| 51 | + // `frintn` is always round-to-nearest which does not match the C specification, but Rust does |
| 52 | + // not support rounding modes. |
| 53 | + unsafe { |
| 54 | + asm!( |
| 55 | + "frintn {x:s}, {x:s}", |
| 56 | + x = inout(vreg) x, |
| 57 | + options(nomem, nostack, pure) |
| 58 | + ); |
| 59 | + } |
| 60 | + x |
| 61 | +} |
22 | 62 |
|
23 |
| - // SAFETY: only requires target_feature=neon, ensured by `cfg_if` in parent module. |
24 |
| - let x_vec: float32x2_t = unsafe { vdup_n_f32(x) }; |
| 63 | +#[cfg(all(f16_enabled, target_feature = "fp16"))] |
| 64 | +pub fn rintf16(mut x: f16) -> f16 { |
| 65 | + // SAFETY: `frintn` is available for `f16` with `fp16` (implies `neon`) and has no side effects. |
| 66 | + // |
| 67 | + // `frintn` is always round-to-nearest which does not match the C specification, but Rust does |
| 68 | + // not support rounding modes. |
| 69 | + unsafe { |
| 70 | + asm!( |
| 71 | + "frintn {x:h}, {x:h}", |
| 72 | + x = inout(vreg) x, |
| 73 | + options(nomem, nostack, pure) |
| 74 | + ); |
| 75 | + } |
| 76 | + x |
| 77 | +} |
25 | 78 |
|
26 |
| - // SAFETY: only requires target_feature=neon, ensured by `cfg_if` in parent module. |
27 |
| - let result_vec: float32x2_t = unsafe { vrndn_f32(x_vec) }; |
| 79 | +pub fn sqrt(mut x: f64) -> f64 { |
| 80 | + // SAFETY: `fsqrt` is available with neon and has no side effects. |
| 81 | + unsafe { |
| 82 | + asm!( |
| 83 | + "fsqrt {x:d}, {x:d}", |
| 84 | + x = inout(vreg) x, |
| 85 | + options(nomem, nostack, pure) |
| 86 | + ); |
| 87 | + } |
| 88 | + x |
| 89 | +} |
28 | 90 |
|
29 |
| - // SAFETY: only requires target_feature=neon, ensured by `cfg_if` in parent module. |
30 |
| - let result: f32 = unsafe { vget_lane_f32::<0>(result_vec) }; |
| 91 | +pub fn sqrtf(mut x: f32) -> f32 { |
| 92 | + // SAFETY: `fsqrt` is available with neon and has no side effects. |
| 93 | + unsafe { |
| 94 | + asm!( |
| 95 | + "fsqrt {x:s}, {x:s}", |
| 96 | + x = inout(vreg) x, |
| 97 | + options(nomem, nostack, pure) |
| 98 | + ); |
| 99 | + } |
| 100 | + x |
| 101 | +} |
31 | 102 |
|
32 |
| - result |
| 103 | +#[cfg(all(f16_enabled, target_feature = "fp16"))] |
| 104 | +pub fn sqrtf16(mut x: f16) -> f16 { |
| 105 | + // SAFETY: `fsqrt` is available for `f16` with `fp16` (implies `neon`) and has no |
| 106 | + // side effects. |
| 107 | + unsafe { |
| 108 | + asm!( |
| 109 | + "fsqrt {x:h}, {x:h}", |
| 110 | + x = inout(vreg) x, |
| 111 | + options(nomem, nostack, pure) |
| 112 | + ); |
| 113 | + } |
| 114 | + x |
33 | 115 | }
|
0 commit comments