Skip to content
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

Merged
merged 16 commits into from Sep 1, 2021

Conversation

travisstaloch
Copy link
Sponsor Contributor

  • 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?)

@travisstaloch
Copy link
Sponsor Contributor Author

addresses #1284

@Snektron
Copy link
Collaborator

Don't forget to update the langref

src/stage1/bigint.cpp Outdated Show resolved Hide resolved
src/zig_llvm.cpp Outdated Show resolved Hide resolved
@andrewrk
Copy link
Member

Nice work! Did you want to implement the new syntax too? This syntax is planned:

  • a +| b - saturating addition

  • a -| b - saturating subtraction

  • a *| b - saturating multiplication

  • a <<| b - saturating shift left

  • a +|= b - saturating assignment addition

  • a -|= b - saturating assignment subtraction

  • a *|= b - saturating assignment multiplication

  • a <<|= b - saturating assignment shift left

If not, that's ok, since implementing them as builtins is an incremental improvement getting us closer to the final spec.

Copy link
Sponsor Contributor Author

@travisstaloch travisstaloch left a 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.

src/Zir.zig Outdated Show resolved Hide resolved
src/stage1/bigint.cpp Outdated Show resolved Hide resolved
src/zig_llvm.cpp Outdated Show resolved Hide resolved
src/stage1/bigint.cpp Outdated Show resolved Hide resolved
src/stage1/bigint.cpp Outdated Show resolved Hide resolved
@travisstaloch
Copy link
Sponsor Contributor Author

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.

Copy link
Contributor

@matu3ba matu3ba left a 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.

test/behavior/saturating_arithmetic.zig Outdated Show resolved Hide resolved
test/behavior/saturating_arithmetic.zig Outdated Show resolved Hide resolved
test/behavior/saturating_arithmetic.zig Outdated Show resolved Hide resolved
src/stage1/bigint.cpp Outdated Show resolved Hide resolved
src/stage1/bigint.cpp Outdated Show resolved Hide resolved
Copy link
Member

@andrewrk andrewrk left a 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.

src/stage1/ir.cpp Outdated Show resolved Hide resolved
test/behavior/saturating_arithmetic.zig Outdated Show resolved Hide resolved
src/Zir.zig Outdated Show resolved Hide resolved
doc/langref.html.in Show resolved Hide resolved
- 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
- 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
- prevent the llvm error by making test helper param `op` comptime.  this way mul instructions are only generated in mul test
@andrewrk andrewrk merged commit 21a5769 into ziglang:master Sep 1, 2021
@travisstaloch travisstaloch deleted the sat-arithmetic branch September 1, 2021 22:10
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.

None yet

4 participants