Skip to content

Commit 9f1540f

Browse files
committed
Add numeric instructions for Wasm not available in core
1 parent f3f4e1e commit 9f1540f

File tree

1 file changed

+112
-0
lines changed
  • crates/core_arch/src/wasm32

1 file changed

+112
-0
lines changed

crates/core_arch/src/wasm32/mod.rs

+112
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,118 @@ pub fn unreachable() -> ! {
3232
crate::intrinsics::abort()
3333
}
3434

35+
/// Generates the [`f32.ceil`] instruction, returning the smallest integer greater than or equal to `a`.
36+
///
37+
/// [`f32.ceil`]: https://webassembly.github.io/spec/core/syntax/instructions.html#syntax-instr-numeric
38+
#[cfg_attr(test, assert_instr(f32.ceil))]
39+
#[inline]
40+
#[must_use = "method returns a new number and does not mutate the original value"]
41+
#[stable(feature = "wasm_numeric_instr", since = "1.84.0")]
42+
pub fn f32_ceil(a: f32) -> f32 {
43+
unsafe { crate::intrinsics::ceilf32(a) }
44+
}
45+
46+
/// Generates the [`f32.floor`] instruction, returning the largest integer less than or equal to `a`.
47+
///
48+
/// [`f32.ceil`]: https://webassembly.github.io/spec/core/syntax/instructions.html#syntax-instr-numeric
49+
#[cfg_attr(test, assert_instr(f32.floor))]
50+
#[inline]
51+
#[must_use = "method returns a new number and does not mutate the original value"]
52+
#[stable(feature = "wasm_numeric_instr", since = "1.84.0")]
53+
pub fn f32_floor(a: f32) -> f32 {
54+
unsafe { crate::intrinsics::floorf32(a) }
55+
}
56+
57+
/// Generates the [`f32.trunc`] instruction, roundinging to the nearest integer towards zero.
58+
///
59+
/// [`f32.trunc`]: https://webassembly.github.io/spec/core/syntax/instructions.html#syntax-instr-numeric
60+
#[cfg_attr(test, assert_instr(f32.trunc))]
61+
#[inline]
62+
#[must_use = "method returns a new number and does not mutate the original value"]
63+
#[stable(feature = "wasm_numeric_instr", since = "1.84.0")]
64+
pub fn f32_trunc(a: f32) -> f32 {
65+
unsafe { crate::intrinsics::truncf32(a) }
66+
}
67+
68+
/// Generates the [`f32.nearest`] instruction, roundinging to the nearest integer. Rounds half-way
69+
/// cases to the number with an even least significant digit.
70+
///
71+
/// [`f32.nearest`]: https://webassembly.github.io/spec/core/syntax/instructions.html#syntax-instr-numeric
72+
#[cfg_attr(test, assert_instr(f32.nearest))]
73+
#[inline]
74+
#[must_use = "method returns a new number and does not mutate the original value"]
75+
#[stable(feature = "wasm_numeric_instr", since = "1.84.0")]
76+
pub fn f32_nearest(a: f32) -> f32 {
77+
unsafe { crate::intrinsics::rintf32(a) }
78+
}
79+
80+
/// Generates the [`f32.sqrt`] instruction, returning the square root of the number `a`.
81+
///
82+
/// [`f32.nearest`]: https://webassembly.github.io/spec/core/syntax/instructions.html#syntax-instr-numeric
83+
#[cfg_attr(test, assert_instr(f32.sqrt))]
84+
#[inline]
85+
#[must_use = "method returns a new number and does not mutate the original value"]
86+
#[stable(feature = "wasm_numeric_instr", since = "1.84.0")]
87+
pub fn f32_sqrt(a: f32) -> f32 {
88+
unsafe { crate::intrinsics::sqrtf32(a) }
89+
}
90+
91+
/// Generates the [`f64.ceil`] instruction, returning the smallest integer greater than or equal to `a`.
92+
///
93+
/// [`f64.ceil`]: https://webassembly.github.io/spec/core/syntax/instructions.html#syntax-instr-numeric
94+
#[cfg_attr(test, assert_instr(f64.ceil))]
95+
#[inline]
96+
#[must_use = "method returns a new number and does not mutate the original value"]
97+
#[stable(feature = "wasm_numeric_instr", since = "1.84.0")]
98+
pub fn f64_ceil(a: f64) -> f64 {
99+
unsafe { crate::intrinsics::ceilf64(a) }
100+
}
101+
102+
/// Generates the [`f64.floor`] instruction, returning the largest integer less than or equal to `a`.
103+
///
104+
/// [`f64.ceil`]: https://webassembly.github.io/spec/core/syntax/instructions.html#syntax-instr-numeric
105+
#[cfg_attr(test, assert_instr(f64.floor))]
106+
#[inline]
107+
#[must_use = "method returns a new number and does not mutate the original value"]
108+
#[stable(feature = "wasm_numeric_instr", since = "1.84.0")]
109+
pub fn f64_floor(a: f64) -> f64 {
110+
unsafe { crate::intrinsics::floorf64(a) }
111+
}
112+
113+
/// Generates the [`f64.trunc`] instruction, roundinging to the nearest integer towards zero.
114+
///
115+
/// [`f64.trunc`]: https://webassembly.github.io/spec/core/syntax/instructions.html#syntax-instr-numeric
116+
#[cfg_attr(test, assert_instr(f64.trunc))]
117+
#[inline]
118+
#[must_use = "method returns a new number and does not mutate the original value"]
119+
#[stable(feature = "wasm_numeric_instr", since = "1.84.0")]
120+
pub fn f64_trunc(a: f64) -> f64 {
121+
unsafe { crate::intrinsics::truncf64(a) }
122+
}
123+
124+
/// Generates the [`f64.nearest`] instruction, roundinging to the nearest integer. Rounds half-way
125+
/// cases to the number with an even least significant digit.
126+
///
127+
/// [`f64.nearest`]: https://webassembly.github.io/spec/core/syntax/instructions.html#syntax-instr-numeric
128+
#[cfg_attr(test, assert_instr(f64.nearest))]
129+
#[inline]
130+
#[must_use = "method returns a new number and does not mutate the original value"]
131+
#[stable(feature = "wasm_numeric_instr", since = "1.84.0")]
132+
pub fn f64_nearest(a: f64) -> f64 {
133+
unsafe { crate::intrinsics::rintf64(a) }
134+
}
135+
136+
/// Generates the [`f64.sqrt`] instruction, returning the square root of the number `a`.
137+
///
138+
/// [`f64.nearest`]: https://webassembly.github.io/spec/core/syntax/instructions.html#syntax-instr-numeric
139+
#[cfg_attr(test, assert_instr(f64.sqrt))]
140+
#[inline]
141+
#[must_use = "method returns a new number and does not mutate the original value"]
142+
#[stable(feature = "wasm_numeric_instr", since = "1.84.0")]
143+
pub fn f64_sqrt(a: f64) -> f64 {
144+
unsafe { crate::intrinsics::sqrtf64(a) }
145+
}
146+
35147
extern "C-unwind" {
36148
#[link_name = "llvm.wasm.throw"]
37149
fn wasm_throw(tag: i32, ptr: *mut u8) -> !;

0 commit comments

Comments
 (0)