cranelift: Implement float rounding operations (#4397)
Implements the following operations on the interpreter: * `ceil` * `floor` * `nearest` * `trunc`
This commit is contained in:
@@ -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 {
|
||||
|
||||
Reference in New Issue
Block a user