Skip to content

Fix numeric prims #2689

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 11 commits into from
Mar 7, 2024
Merged

Fix numeric prims #2689

merged 11 commits into from
Mar 7, 2024

Conversation

leonschoorl
Copy link
Member

@leonschoorl leonschoorl commented Mar 4, 2024

This fixes various issues with primitives on various number types.
Some of it caused by changes in GHC, like the change from integer-gmp to ghc-bignum and the addition of sized versions of unboxed Int and unboxed Word.
And some that were just never tested properly.

  • blackboxes for GHC.Prim.c[lt]z*
  • primitive evaluator for Int64 and Word64 primitives with GHC >= 9.4
  • big negative Integer literals in evaluator
  • VHDL rendering of Int32 literals with GHC >= 9.2
  • don't generate out of range number literals
  • VHDL blackbox for GHC.Num.Integer.integerTestBit#
  • VHDL blackboxes for all signed multiplication
  • (System)Verilog blackbox for Signed.fromInteger# now does the same as the VHDL one

For more details see the individual commits.

Still TODO:

  • Write a changelog entry (see changelog/README.md)
  • Check copyright notices are up to date in edited files

Copy link
Member

@martijnbastiaan martijnbastiaan 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 haven't thoroughly checked the changes, but the tests are pretty convincing I'd say. Minor changes requested.

@leonschoorl
Copy link
Member Author

I'll squash/fixup the new commits later.
But I've kept them separate for your re-reviewing pleasure.

They're used to implement countLeadingZeros and countTrailingZeros on Int* and Word*
With GHC 9.4 the unboxed version of Int64 changed
from Int# to Int64#. And similar for Word64.
This caused the evalutor fail to evalute many primitives on Word64 and Int64.
Now we don't truncate them to 16 bits any more.
Also deduplicate core Literal to netlist Literal conversion code.
In VHDL integers are only guarenteed to include [-2^31-1..2^31-1] inclusive.
For -2^31 we now fallback to path for wider numbers, that doesn't use VHDL integers.

In (System)Verilog we didn't do any limiting, which verilator didn't like.
Now we use the same algorithm as VHDL.
Which implements testBit @integer on ghc >= 9.0
This used to be implented as
  resize(~ARG[0] * ~ARG[1],~SIZE[~TYPO])

But resize on signed numbers in VHDL preserves the sign bit.
But in Haskell and the other HDLs we just truncate the result, and that 
has different behaviour on over/underflow.
Make it do the same thing as the VHDL version.
When expanding, it now duplicates the sign bit, preserving the value,
that's new in this commit.
When shrinking it does a bit slice, implementing the wrap-around
behavior that the Haskell simulation does.
@leonschoorl leonschoorl merged commit aff963f into master Mar 7, 2024
@leonschoorl leonschoorl deleted the fix-numeric-prims branch March 7, 2024 15:08
@mergify mergify bot mentioned this pull request Mar 7, 2024
martijnbastiaan added a commit that referenced this pull request Mar 8, 2024
@kleinreact kleinreact mentioned this pull request Sep 27, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants