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
saturating arithmetic builtins: add, sub, mul, shl #9619
Conversation
addresses #1284 |
Don't forget to update the langref |
Nice work! Did you want to implement the new syntax too? This syntax is planned:
If not, that's ok, since implementing them as builtins is an incremental improvement getting us closer to the final spec. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
If not, that's ok, since implementing them as builtins is an incremental improvement getting us closer to the final spec.
I am thinking the operator stuff should be a follow up pr to keep things a little more manageable.
I'm not sure what is causing the ci failure. I'm thinking its likely related to the bigint changes. Perhaps failing on a 32bit arch? Just a guess as i can't tell from looking at the logs. Anyway looks like i'll need to rework my bigint changes. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
please add some explanations on the logic.
Nits: a few interesting test cases and explaining the workaround with is_negative due to bigints_bits_needed implementation.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Nice work! I left you a few items to address but I think this is very close to being mergeable.
f0d4ba7
to
2475e9b
Compare
- adds 1 simple behavior tests for each which does integer and vector ops at runtime and comptime - adds bigint_*_sat() methods for each which simply set dest to the type min/max boundaries if they are exceeded. these should to be audited for correctness and perhaps there is a better existing way to accomplish this. - the mul intrinsic needs to somehow explicitly provide a scale parameter (which seems to default to 0?)
- rather than initializing new integers for min/max, compute the number of bits required using bigint_bits_needed(). NOTE: this doesn't allow for types larger than 64 bits. - rename bigint_saturate() -> bigint_clamp_by_bitcount() - adds a few more test cases
- use CreateIntrinsic() which accepts a variable number of arguments to pass the scale parameter
- rather than re-initializing, we only need to shift the least significant digit right by the number of 'extra' bits
- works for ints > 64 bits - add a few more test cases for ints > 64 bits
- move bound computations closer to usage within cases - rename op -> dest for consistency
- adds more test cases suggested by matu3ba - better explain clamping algorithm in comments
- rework test cases which were skipping runtime evaluation. this uncovered bugs related to *mul.fix.sat intrinsic. - removed extra type args in ZigLLVMBuild*MulFixSat so mul.fix.sat intrinsic is called correctly - replaced panics in ir.cpp with compile errors. - added case to test/compile_errors.zig given floats - updated langref: indcate future `a +| b` syntax
2475e9b
to
6b21164
Compare
- rework test cases which were skipping runtime evaluation. this uncovered bugs related to *mul.fix.sat intrinsic. - removed extra type args in ZigLLVMBuild*MulFixSat so mul.fix.sat intrinsic is called correctly - replaced panics in ir.cpp with compile errors. - added case to test/compile_errors.zig given floats - updated langref: indcate future `a +| b` syntax
- account for `maxInt(iN) *| -1 == minInt(iN) + 1` - explain upstream bug in llvm.smul.fix.sat and link to ziglang#9643 in langref and commented out test cases
- ci is erroring with 'LLVM ERROR: Unable to expand fixed point multiplication' when compiling for wasm32 - just trying to get ci to pass. not sure this is correct to skip
78058bf
to
4e11ae1
Compare
- prevent the llvm error by making test helper param `op` comptime. this way mul instructions are only generated in mul test
cf07a96
to
938b5bf
Compare
which does integer and vector ops at
runtime and comptime
which simply set dest to the type
min/max boundaries if they are
exceeded. these should to be
audited for correctness and perhaps
there is a better existing way to
accomplish this.
explicitly provide a scale parameter
(which seems to default to 0?)