Skip to content

compiler: saturate float-to-int conversions #1574

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 1 commit into from
Jan 16, 2021

Conversation

niaow
Copy link
Member

@niaow niaow commented Jan 15, 2021

This works around some UB in LLVM, where an out-of-bounds conversion would produce a poison value.
The selected behavior is saturating, except that NaN is mapped to the minimum value.

Fixes #276

This works around some UB in LLVM, where an out-of-bounds conversion would produce a poison value.
The selected behavior is saturating, except that NaN is mapped to the minimum value.
@niaow niaow requested a review from aykevl January 15, 2021 15:57
@aykevl
Copy link
Member

aykevl commented Jan 15, 2021

The code itself looks reasonable, although I haven't studied it in depth.

Can you maybe add a few more test cases? I'm a little bit nervous around edge cases with floats, so for example I think it would be a good idea to test the values around the edges. You can look here for some examples: https://github.com/rust-lang/rust/blob/19e63bd50eb756e35416d7a8f01e2d63f419b373/src/test/run-pass/saturating-float-casts.rs

@aykevl
Copy link
Member

aykevl commented Jan 15, 2021

Also, I'm not sure whether this was intentional but the behavior in this PR differs slightly from the LLVM builtin, which we'll probably want to use eventually: https://reviews.llvm.org/D54749. The builtin converts NaN to 0 instead of the minimal integer value. Using the same behavior would help with testing when we can use that builtin instead.

@niaow
Copy link
Member Author

niaow commented Jan 16, 2021

The builtin converts NaN to 0 instead of the minimal integer value.

Yeah, this was something I noticed. I had omitted the extra step to do this for signed integers as it seemed to be a bit of extra complexity for now, but I can add it in.

@aykevl
Copy link
Member

aykevl commented Jan 16, 2021

I'm fine with it as it is. It just might need changing in the future.
The rationale for NaN to 0 conversion according to Rust is the following (source):

  • Matches the behaviour of ARM’s vcvt instruction, making this have no overhead there.
  • Should optimize reasonably well on other platforms.

I don't know whether softfloat implementations have the same behavior and whether LLVM optimizes this pattern, but at least in theory this pattern is cheap/free on ARM.

@deadprogram
Copy link
Member

Thank you @niaow for the contribution and @aykevl for the review. Now merging.

@deadprogram deadprogram merged commit a867b56 into tinygo-org:dev Jan 16, 2021
@niaow
Copy link
Member Author

niaow commented Jan 16, 2021

This was. . . not ready to be merged

I guess I will open a new PR to actually finish this.

@deadprogram
Copy link
Member

Oops, my bad. I misinterpreted the comments.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants