@@ -40,9 +40,49 @@ 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).
4242pub fn f64_to_wrapping_i32 ( n : f64 ) -> i32 {
43+ #[ cfg( all( target_arch = "aarch64" , target_os = "macos" ) ) ]
44+ {
45+ // macOS aarch64 always has jsconv feature
46+ // SAFETY: macOS aarch64 always supports jsconv
47+ unsafe { f64_to_wrapping_int32_arm64 ( n) }
48+ }
49+ #[ cfg( all( target_arch = "aarch64" , not( target_os = "macos" ) ) ) ]
50+ {
51+ if std:: arch:: is_aarch64_feature_detected!( "jsconv" ) {
52+ // SAFETY: Feature detection confirmed jsconv is available
53+ unsafe { f64_to_wrapping_int32_arm64 ( n) }
54+ } else {
55+ f64_to_wrapping_i32_generic ( n)
56+ }
57+ }
58+ #[ cfg( not( target_arch = "aarch64" ) ) ]
59+ {
60+ f64_to_wrapping_i32_generic ( n)
61+ }
62+ }
63+
64+ #[ allow( unused) ]
65+ fn f64_to_wrapping_i32_generic ( n : f64 ) -> i32 {
4366 f64_to_wrapping_u32 ( n) as i32
4467}
4568
69+ #[ allow( unused) ]
70+ #[ cfg( target_arch = "aarch64" ) ]
71+ #[ target_feature( enable = "jsconv" ) ]
72+ /// Converts an `f64` to an `i32` with ECMAScript `ToInt32` wrapping behavior.
73+ /// The value will be wrapped in the range [-2^31, 2^31).
74+ /// Optimized for macOS aarch64 with the fjcvtzs instruction.
75+ unsafe fn f64_to_wrapping_int32_arm64 ( number : f64 ) -> i32 {
76+ let ret: i32 ;
77+ // SAFETY: fjcvtzs instruction is available on macOS aarch64.
78+ std:: arch:: asm!(
79+ "fjcvtzs {dst:w}, {src:d}" ,
80+ src = in( vreg) number,
81+ dst = out( reg) ret,
82+ ) ;
83+ ret
84+ }
85+
4686/// Implements the IEEE-754 "Round to nearest, ties to even" rounding rule.
4787/// (e.g., both 1.5 and 2.5 will round to 2).
4888/// This also clamps out-of-range values and NaN to `i32::MIN`.
0 commit comments