Behavior on overflow (Index
, Signed
, ....)
#1903
Replies: 6 comments 32 replies
-
My personal opinion on this matter can be captured in a few points:
These points lead me to believe the orange crab language got it right once more, and we should go with Option 3: make saturation mode non-wrapping (error) by default for Unsigned, Signed, ... Couple of points:
|
Beta Was this translation helpful? Give feedback.
-
Clash wraps around on overflow (for |
Beta Was this translation helpful? Give feedback.
-
For reference: my proposal also comes with a mostly finished implementation at |
Beta Was this translation helpful? Give feedback.
-
So |
Beta Was this translation helpful? Give feedback.
-
Completely anecdotal data point: I have hacked Clash 1.4 locally so that Even though it's quite a small code base, it was written through and through with the assumption that Now, I realize existing code shouldn't be an argument against progress especially if the current behaviour is "wrong" or at least not intended to be exploited by user code, but the wraparound behaviour of (Option #2 sounds great by the way, I was never quite sure why BTW, I was so convinced that the wraparound behaviour of
|
Beta Was this translation helpful? Give feedback.
-
To everyone who replied on this thread:
Until we have "sane" stack trace support in GHC (https://gitlab.haskell.org/ghc/ghc/-/merge_requests/5456), I'd like to close this discussion. Once that has landed we can revisit to see if option 3 would be reasonable. I'd like to close #688 as this implements wrapping by default (option 2), which to reiterate has been rejected on (HDL) performance concerns. |
Beta Was this translation helpful? Give feedback.
-
#688 has been open for almost two years now, and I feel bad that we haven't responded to it properly. This mostly stems from the fact that its a "controversial" idea, with lots of different opinions. I'm opening a discussion for two reasons:
The gist of this issue is captured in this trace:
There are a couple of hypothetical reasons this decrepancy is currently there:
Unsigned
andSigned
do not raise an error on overflow. Either because their implementation is bigint (Python, Ruby) or because of -presumably- performance considerations (Haskell, C++, Java). A notable exception to this rule is 🦀 Rust 🦀, it throws an exception in "debug" mode and reverts to silently overflowing on "release" mode.Int
, it makes sense to mirror them forUnsigned
/Signed
/..Index
needs bound checking anyway to be useful, so raising an error is not a performance consideration.Options
I believe we have a couple of options. If you'd like one added to the list, please edit this post or leave a comment.
Option 1: keep as is (reject PR)
Self-explanatory.
Option 2: make saturation mode wrapping by default for
Index
(~accept PR)Do not error on overflow for operations on
Index
. (I'd like to save discussions on an exact implemenation for later.)Option 3: make saturation mode non-wrapping (error) by default for
Unsigned
,Signed
, ..Similar to Rust, we could make overflowing an error for all Clash data types.
Type-level saturation mode
PR #688 also implements a way of specifying saturation mode at the type-level. However, I think we pretty much all agree that @alex-mckenna's proposal makes most sense here. If this does happen to be contentious, we should discuss it elsewhere.
Beta Was this translation helpful? Give feedback.
All reactions