-
Notifications
You must be signed in to change notification settings - Fork 88
Feature/round #34
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Feature/round #34
Changes from all commits
Commits
Show all changes
9 commits
Select commit
Hold shift + click to select a range
ffedbe5
Add rounding functions
calebzulawski 75fdacd
Add rounding to integers
calebzulawski 3d8721b
Fix casts, add tests
calebzulawski 5805c7a
Fix comment
calebzulawski 6e07982
Fix UB in test
calebzulawski c27c761
Fix UB in test (really this time)
calebzulawski dc85c13
Account for sign bit
calebzulawski 3ad356d
Disable riscv64gc
calebzulawski 3870633
Add rounding mode test
calebzulawski File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,153 @@ | ||
macro_rules! implement { | ||
{ | ||
impl $type:ident { | ||
int_type = $int_type:ident, | ||
floor = $floor_intrinsic:literal, | ||
ceil = $ceil_intrinsic:literal, | ||
round = $round_intrinsic:literal, | ||
trunc = $trunc_intrinsic:literal, | ||
} | ||
} => { | ||
mod $type { | ||
#[allow(improper_ctypes)] | ||
extern "C" { | ||
#[link_name = $floor_intrinsic] | ||
fn floor_intrinsic(x: crate::$type) -> crate::$type; | ||
#[link_name = $ceil_intrinsic] | ||
fn ceil_intrinsic(x: crate::$type) -> crate::$type; | ||
#[link_name = $round_intrinsic] | ||
fn round_intrinsic(x: crate::$type) -> crate::$type; | ||
#[link_name = $trunc_intrinsic] | ||
fn trunc_intrinsic(x: crate::$type) -> crate::$type; | ||
} | ||
|
||
impl crate::$type { | ||
/// Returns the largest integer less than or equal to each lane. | ||
#[must_use = "method returns a new vector and does not mutate the original value"] | ||
#[inline] | ||
pub fn floor(self) -> Self { | ||
unsafe { floor_intrinsic(self) } | ||
} | ||
|
||
/// Returns the smallest integer greater than or equal to each lane. | ||
#[must_use = "method returns a new vector and does not mutate the original value"] | ||
#[inline] | ||
pub fn ceil(self) -> Self { | ||
unsafe { ceil_intrinsic(self) } | ||
} | ||
|
||
/// Returns the nearest integer to each lane. Round half-way cases away from 0.0. | ||
Lokathor marked this conversation as resolved.
Show resolved
Hide resolved
|
||
#[must_use = "method returns a new vector and does not mutate the original value"] | ||
#[inline] | ||
pub fn round(self) -> Self { | ||
unsafe { round_intrinsic(self) } | ||
} | ||
|
||
/// Returns the integer part of each lane. | ||
#[must_use = "method returns a new vector and does not mutate the original value"] | ||
#[inline] | ||
pub fn trunc(self) -> Self { | ||
unsafe { trunc_intrinsic(self) } | ||
} | ||
|
||
/// Returns the fractional part of each lane. | ||
#[must_use = "method returns a new vector and does not mutate the original value"] | ||
#[inline] | ||
pub fn fract(self) -> Self { | ||
self - self.trunc() | ||
} | ||
|
||
/// Rounds toward zero and converts to the same-width integer type, assuming that | ||
/// the value is finite and fits in that type. | ||
/// | ||
/// # Safety | ||
/// The value must: | ||
/// | ||
/// * Not be NaN | ||
/// * Not be infinite | ||
/// * Be representable in the return type, after truncating off its fractional part | ||
#[inline] | ||
pub unsafe fn to_int_unchecked(self) -> crate::$int_type { | ||
crate::intrinsics::simd_cast(self) | ||
} | ||
|
||
/// Creates a floating-point vector from an integer vector. Rounds values that are | ||
/// not exactly representable. | ||
#[inline] | ||
pub fn round_from_int(value: crate::$int_type) -> Self { | ||
unsafe { crate::intrinsics::simd_cast(value) } | ||
} | ||
} | ||
} | ||
} | ||
} | ||
|
||
implement! { | ||
impl f32x2 { | ||
int_type = i32x2, | ||
floor = "llvm.floor.v2f32", | ||
ceil = "llvm.ceil.v2f32", | ||
round = "llvm.round.v2f32", | ||
trunc = "llvm.trunc.v2f32", | ||
} | ||
} | ||
workingjubilee marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
||
implement! { | ||
impl f32x4 { | ||
int_type = i32x4, | ||
floor = "llvm.floor.v4f32", | ||
ceil = "llvm.ceil.v4f32", | ||
round = "llvm.round.v4f32", | ||
trunc = "llvm.trunc.v4f32", | ||
} | ||
} | ||
|
||
implement! { | ||
impl f32x8 { | ||
int_type = i32x8, | ||
floor = "llvm.floor.v8f32", | ||
ceil = "llvm.ceil.v8f32", | ||
round = "llvm.round.v8f32", | ||
trunc = "llvm.trunc.v8f32", | ||
} | ||
} | ||
|
||
implement! { | ||
impl f32x16 { | ||
int_type = i32x16, | ||
floor = "llvm.floor.v16f32", | ||
ceil = "llvm.ceil.v16f32", | ||
round = "llvm.round.v16f32", | ||
trunc = "llvm.trunc.v16f32", | ||
} | ||
} | ||
|
||
implement! { | ||
impl f64x2 { | ||
int_type = i64x2, | ||
floor = "llvm.floor.v2f64", | ||
ceil = "llvm.ceil.v2f64", | ||
round = "llvm.round.v2f64", | ||
trunc = "llvm.trunc.v2f64", | ||
} | ||
} | ||
|
||
implement! { | ||
impl f64x4 { | ||
int_type = i64x4, | ||
floor = "llvm.floor.v4f64", | ||
ceil = "llvm.ceil.v4f64", | ||
round = "llvm.round.v4f64", | ||
trunc = "llvm.trunc.v4f64", | ||
} | ||
} | ||
|
||
implement! { | ||
impl f64x8 { | ||
int_type = i64x8, | ||
floor = "llvm.floor.v8f64", | ||
ceil = "llvm.ceil.v8f64", | ||
round = "llvm.round.v8f64", | ||
trunc = "llvm.trunc.v8f64", | ||
} | ||
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,6 +1,6 @@ | ||
use super::helpers; | ||
|
||
float_tests! { f32x2, f32 } | ||
float_tests! { f32x4, f32 } | ||
float_tests! { f32x8, f32 } | ||
float_tests! { f32x16, f32 } | ||
float_tests! { f32x2, f32, i32x2, i32 } | ||
float_tests! { f32x4, f32, i32x4, i32 } | ||
float_tests! { f32x8, f32, i32x8, i32 } | ||
float_tests! { f32x16, f32, i32x16, i32 } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,5 +1,5 @@ | ||
use super::helpers; | ||
|
||
float_tests! { f64x2, f64 } | ||
float_tests! { f64x4, f64 } | ||
float_tests! { f64x8, f64 } | ||
float_tests! { f64x2, f64, i64x2, i64 } | ||
float_tests! { f64x4, f64, i64x4, i64 } | ||
float_tests! { f64x8, f64, i64x8, i64 } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.