cranelift: Implement float rounding operations (#4397)

Implements the following operations on the interpreter:
* `ceil`
* `floor`
* `nearest`
* `trunc`
This commit is contained in:
Afonso Bordado
2022-07-07 00:43:54 +01:00
committed by GitHub
parent 9575ed4eb7
commit f98076ae88
7 changed files with 540 additions and 4 deletions

View File

@@ -800,6 +800,26 @@ impl Ieee32 {
pub fn is_zero(&self) -> bool {
self.as_f32() == 0.0
}
/// Returns the smallest integer greater than or equal to `self`.
pub fn ceil(self) -> Self {
Self::with_float(self.as_f32().ceil())
}
/// Returns the largest integer less than or equal to `self`.
pub fn floor(self) -> Self {
Self::with_float(self.as_f32().floor())
}
/// Returns the integer part of `self`. This means that non-integer numbers are always truncated towards zero.
pub fn trunc(self) -> Self {
Self::with_float(self.as_f32().trunc())
}
/// Returns the nearest integer to `self`. Round half-way cases away from `0.0`.
pub fn nearest(self) -> Self {
Self::with_float(self.as_f32().round())
}
}
impl PartialOrd for Ieee32 {
@@ -929,6 +949,26 @@ impl Ieee64 {
pub fn is_zero(&self) -> bool {
self.as_f64() == 0.0
}
/// Returns the smallest integer greater than or equal to `self`.
pub fn ceil(self) -> Self {
Self::with_float(self.as_f64().ceil())
}
/// Returns the largest integer less than or equal to `self`.
pub fn floor(self) -> Self {
Self::with_float(self.as_f64().floor())
}
/// Returns the integer part of `self`. This means that non-integer numbers are always truncated towards zero.
pub fn trunc(self) -> Self {
Self::with_float(self.as_f64().trunc())
}
/// Returns the nearest integer to `self`. Round half-way cases away from `0.0`.
pub fn nearest(self) -> Self {
Self::with_float(self.as_f64().round())
}
}
impl PartialOrd for Ieee64 {