-
Notifications
You must be signed in to change notification settings - Fork 1
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
Add saturating integer math #7
Conversation
@kimikage I know you were interested in this kind of extension, so I tried it out. Feel free to take a look of you have a minute. |
You really get your work done fast! I have not been able to track down the details yet, but let me discuss the module configuration. I envision the following configuration:
The submodules may be a premature optimization. |
Thanks! Want to make sure I understand your suggestions. Wrapping is just what Julia does now, not something else right? I'm curious if you're aware of a reason the checked methods are in a Base submodule? It always seemed odd to me. But might make sense to echo that. Why the Arithmetic intermediate? |
That is partially true. |
I don't know if this answers your question, but I think it means that |
There are three major reasons.
If your question is why WRT |
Thanks! That's helpful and suggests a roadmap. I see your point about Hmm. Apparently |
So I think the underlying issue is that in LLVM, division overflows are deemed undefined behavior, unlike addition, subtraction, and multiplication. https://llvm.org/docs/LangRef.html#add-instruction |
In the spirit of it being undefined behavior... wrapping |
For integers with small bitwidth, using floating-point division might not be such a bad choice, given the handling of |
After some thought, I think that the native default operators, other than for division, should always be considered unchecked. It's the most performant and it matches the defaults of other languages, and LLVM itself. When the default varies from this, it's because there's a very good reason - undefined behavior would occur (integer division), or the type's existence is intended to prevent it (e.g. SaferIntegers). So that gives us:
I'm wondering if we might even not need the submodules, might be a way to do all this without exporting anything... But if not, having the modules does make sense to echo Base. So what to do about division operations? E.g. C# doesn't turn off throwing exceptions in their I still see some utility in making unchecked integer division methods available and switchable, but perhaps it just needs to be separated in some way. Maybe we can have an option passed to the macro to opt-in to unchecked division methods? I'll open an issue to explore that. |
I think I'm coming around to some of your initial proposal of having wrapping, checked, and saturating modes, and a default/unchecked mode. And I'm also thinking we probably shouldn't have explicit wrapping/saturating/checked methods falling back to the default methods for If e.g. SaferIntegers or some other type ends up in an explicit For Julia's built-in types, |
No description provided.