| 1 |
use core::{ |
| 2 |
arch::{ |
| 3 |
aarch64::{float32x4_t, float64x2_t, uint16x4_t}, |
| 4 |
asm, |
| 5 |
}, |
| 6 |
mem::MaybeUninit, |
| 7 |
ptr, |
| 8 |
}; |
| 9 |
use zerocopy::transmute; |
| 10 |
|
| 11 |
#[target_feature(enable = "fp16")] |
| 12 |
#[inline] |
| 13 |
pub(super) unsafe fn f16_to_f32_fp16(i: u16) -> f32 { |
| 14 |
let result: f32; |
| 15 |
asm!( |
| 16 |
"fcvt {0:s}, {1:h}", |
| 17 |
out(vreg) result, |
| 18 |
in(vreg) i, |
| 19 |
options(pure, nomem, nostack, preserves_flags)); |
| 20 |
result |
| 21 |
} |
| 22 |
|
| 23 |
#[target_feature(enable = "fp16")] |
| 24 |
#[inline] |
| 25 |
pub(super) unsafe fn f16_to_f64_fp16(i: u16) -> f64 { |
| 26 |
let result: f64; |
| 27 |
asm!( |
| 28 |
"fcvt {0:d}, {1:h}", |
| 29 |
out(vreg) result, |
| 30 |
in(vreg) i, |
| 31 |
options(pure, nomem, nostack, preserves_flags)); |
| 32 |
result |
| 33 |
} |
| 34 |
|
| 35 |
#[target_feature(enable = "fp16")] |
| 36 |
#[inline] |
| 37 |
pub(super) unsafe fn f32_to_f16_fp16(f: f32) -> u16 { |
| 38 |
let result: u16; |
| 39 |
asm!( |
| 40 |
"fcvt {0:h}, {1:s}", |
| 41 |
out(vreg) result, |
| 42 |
in(vreg) f, |
| 43 |
options(pure, nomem, nostack, preserves_flags)); |
| 44 |
result |
| 45 |
} |
| 46 |
|
| 47 |
#[target_feature(enable = "fp16")] |
| 48 |
#[inline] |
| 49 |
pub(super) unsafe fn f64_to_f16_fp16(f: f64) -> u16 { |
| 50 |
let result: u16; |
| 51 |
asm!( |
| 52 |
"fcvt {0:h}, {1:d}", |
| 53 |
out(vreg) result, |
| 54 |
in(vreg) f, |
| 55 |
options(pure, nomem, nostack, preserves_flags)); |
| 56 |
result |
| 57 |
} |
| 58 |
|
| 59 |
#[target_feature(enable = "fp16")] |
| 60 |
#[inline] |
| 61 |
pub(super) unsafe fn f16x4_to_f32x4_fp16(v: &[u16; 4]) -> [f32; 4] { |
| 62 |
let vec: uint16x4_t = transmute!(*v); |
| 63 |
let result: float32x4_t; |
| 64 |
asm!( |
| 65 |
"fcvtl {0:v}.4s, {1:v}.4h", |
| 66 |
out(vreg) result, |
| 67 |
in(vreg) vec, |
| 68 |
options(pure, nomem, nostack)); |
| 69 |
transmute!(result) |
| 70 |
} |
| 71 |
|
| 72 |
#[target_feature(enable = "fp16")] |
| 73 |
#[inline] |
| 74 |
pub(super) unsafe fn f32x4_to_f16x4_fp16(v: &[f32; 4]) -> [u16; 4] { |
| 75 |
let vec: float32x4_t = transmute!(*v); |
| 76 |
let result: uint16x4_t; |
| 77 |
asm!( |
| 78 |
"fcvtn {0:v}.4h, {1:v}.4s", |
| 79 |
out(vreg) result, |
| 80 |
in(vreg) vec, |
| 81 |
options(pure, nomem, nostack)); |
| 82 |
transmute!(result) |
| 83 |
} |
| 84 |
|
| 85 |
#[target_feature(enable = "fp16")] |
| 86 |
#[inline] |
| 87 |
pub(super) unsafe fn f16x4_to_f64x4_fp16(v: &[u16; 4]) -> [f64; 4] { |
| 88 |
let vec: uint16x4_t = transmute!(*v); |
| 89 |
let low: float64x2_t; |
| 90 |
let high: float64x2_t; |
| 91 |
asm!( |
| 92 |
"fcvtl {2:v}.4s, {3:v}.4h", |
| 93 |
"fcvtl {0:v}.2d, {2:v}.2s", |
| 94 |
"fcvtl2 {1:v}.2d, {2:v}.4s", |
| 95 |
lateout(vreg) low, |
| 96 |
lateout(vreg) high, |
| 97 |
out(vreg) _, |
| 98 |
in(vreg) vec, |
| 99 |
options(pure, nomem, nostack)); |
| 100 |
transmute!([low, high]) |
| 101 |
} |
| 102 |
|
| 103 |
#[target_feature(enable = "fp16")] |
| 104 |
#[inline] |
| 105 |
pub(super) unsafe fn f64x4_to_f16x4_fp16(v: &[f64; 4]) -> [u16; 4] { |
| 106 |
let mut low = MaybeUninit::<float64x2_t>::uninit(); |
| 107 |
let mut high = MaybeUninit::<float64x2_t>::uninit(); |
| 108 |
ptr::copy_nonoverlapping(v.as_ptr(), low.as_mut_ptr().cast(), 2); |
| 109 |
ptr::copy_nonoverlapping(v[2..].as_ptr(), high.as_mut_ptr().cast(), 2); |
| 110 |
let result: uint16x4_t; |
| 111 |
asm!( |
| 112 |
"fcvtn {1:v}.2s, {2:v}.2d", |
| 113 |
"fcvtn2 {1:v}.4s, {3:v}.2d", |
| 114 |
"fcvtn {0:v}.4h, {1:v}.4s", |
| 115 |
lateout(vreg) result, |
| 116 |
out(vreg) _, |
| 117 |
in(vreg) low.assume_init(), |
| 118 |
in(vreg) high.assume_init(), |
| 119 |
options(pure, nomem, nostack)); |
| 120 |
transmute!(result) |
| 121 |
} |
| 122 |
|
| 123 |
#[target_feature(enable = "fp16")] |
| 124 |
#[inline] |
| 125 |
pub(super) unsafe fn add_f16_fp16(a: u16, b: u16) -> u16 { |
| 126 |
let result: u16; |
| 127 |
asm!( |
| 128 |
"fadd {0:h}, {1:h}, {2:h}", |
| 129 |
out(vreg) result, |
| 130 |
in(vreg) a, |
| 131 |
in(vreg) b, |
| 132 |
options(pure, nomem, nostack)); |
| 133 |
result |
| 134 |
} |
| 135 |
|
| 136 |
#[target_feature(enable = "fp16")] |
| 137 |
#[inline] |
| 138 |
pub(super) unsafe fn subtract_f16_fp16(a: u16, b: u16) -> u16 { |
| 139 |
let result: u16; |
| 140 |
asm!( |
| 141 |
"fsub {0:h}, {1:h}, {2:h}", |
| 142 |
out(vreg) result, |
| 143 |
in(vreg) a, |
| 144 |
in(vreg) b, |
| 145 |
options(pure, nomem, nostack)); |
| 146 |
result |
| 147 |
} |
| 148 |
|
| 149 |
#[target_feature(enable = "fp16")] |
| 150 |
#[inline] |
| 151 |
pub(super) unsafe fn multiply_f16_fp16(a: u16, b: u16) -> u16 { |
| 152 |
let result: u16; |
| 153 |
asm!( |
| 154 |
"fmul {0:h}, {1:h}, {2:h}", |
| 155 |
out(vreg) result, |
| 156 |
in(vreg) a, |
| 157 |
in(vreg) b, |
| 158 |
options(pure, nomem, nostack)); |
| 159 |
result |
| 160 |
} |
| 161 |
|
| 162 |
#[target_feature(enable = "fp16")] |
| 163 |
#[inline] |
| 164 |
pub(super) unsafe fn divide_f16_fp16(a: u16, b: u16) -> u16 { |
| 165 |
let result: u16; |
| 166 |
asm!( |
| 167 |
"fdiv {0:h}, {1:h}, {2:h}", |
| 168 |
out(vreg) result, |
| 169 |
in(vreg) a, |
| 170 |
in(vreg) b, |
| 171 |
options(pure, nomem, nostack)); |
| 172 |
result |
| 173 |
} |