Skip to content

Commit 8f75b24

Browse files
committed
core: Add ARM64 FJCVTZS instruction optimization for f64 to i32
1 parent 7ab3548 commit 8f75b24

File tree

1 file changed

+39
-0
lines changed

1 file changed

+39
-0
lines changed

core/src/ecma_conversions.rs

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -40,9 +40,48 @@ pub fn f64_to_wrapping_u32(n: f64) -> u32 {
4040
/// Converts an `f64` to an `i32` with ECMAScript `ToInt32` wrapping behavior.
4141
/// The value will be wrapped in the range [-2^31, 2^31).
4242
pub fn f64_to_wrapping_i32(n: f64) -> i32 {
43+
#[cfg(target_arch = "aarch64")]
44+
{
45+
#[cfg(target_feature = "jsconv")]
46+
unsafe {
47+
f64_to_wrapping_int32_aarch64(n)
48+
}
49+
#[cfg(not(target_feature = "jsconv"))]
50+
if std::arch::is_aarch64_feature_detected!("jsconv") {
51+
unsafe { f64_to_wrapping_int32_aarch64(n) }
52+
} else {
53+
f64_to_wrapping_i32_generic(n)
54+
}
55+
}
56+
#[cfg(not(target_arch = "aarch64"))]
57+
f64_to_wrapping_i32_generic(n)
58+
}
59+
60+
#[allow(unused)]
61+
fn f64_to_wrapping_i32_generic(n: f64) -> i32 {
4362
f64_to_wrapping_u32(n) as i32
4463
}
4564

65+
#[allow(unused)]
66+
#[cfg(target_arch = "aarch64")]
67+
#[target_feature(enable = "jsconv")]
68+
/// Converts an `f64` to an `i32` with ECMAScript `ToInt32` wrapping behavior.
69+
/// The value will be wrapped in the range [-2^31, 2^31).
70+
/// Optimized for aarch64 cpu with the fjcvtzs instruction.
71+
unsafe fn f64_to_wrapping_int32_aarch64(number: f64) -> i32 {
72+
let ret: i32;
73+
// SAFETY: fjcvtzs instruction is available under jsconv feature.
74+
unsafe {
75+
std::arch::asm!(
76+
"fjcvtzs {dst:w}, {src:d}",
77+
src = in(vreg) number,
78+
dst = out(reg) ret,
79+
options(nostack, nomem, pure)
80+
);
81+
}
82+
ret
83+
}
84+
4685
/// Implements the IEEE-754 "Round to nearest, ties to even" rounding rule.
4786
/// (e.g., both 1.5 and 2.5 will round to 2).
4887
/// This also clamps out-of-range values and NaN to `i32::MIN`.

0 commit comments

Comments
 (0)