phanrahan / magma

magma circuits
Other
253 stars 24 forks source link

Add bitlen utility #1325

Closed rkshthrmsh closed 1 year ago

rkshthrmsh commented 1 year ago

Although clog2 is provided as a utility function for approximating bit lengths from values, it fails in edge cases where the value itself is a power of 2. For example, clog2(16) = 4, but 5 bits are needed to store the value 16. The right formula here would be: floor(log_2(16)) + 1. Although most hardware designers might be aware of this, a helper function for bit length of any generic value could be helpful.

rsetaluri commented 1 year ago

Thanks. Can you add some tests to tests/test_bitutils.py? You can follow the pattern of the other tests in that file.

rkshthrmsh commented 1 year ago

Added tests.

codecov-commenter commented 1 year ago

Codecov Report

Patch coverage: 97.36% and project coverage change: -0.03% :warning:

Comparison is base (4475c36) 86.12% compared to head (1eeb035) 86.09%. Report is 1 commits behind head on master.

:exclamation: Current head 1eeb035 differs from pull request most recent head 4fb7cc2. Consider uploading reports for the commit 4fb7cc2 to get more accurate results

:exclamation: Your organization needs to install the Codecov GitHub app to enable full functionality.

Additional details and impacted files ```diff @@ Coverage Diff @@ ## master #1325 +/- ## ========================================== - Coverage 86.12% 86.09% -0.03% ========================================== Files 171 171 Lines 17869 17899 +30 ========================================== + Hits 15389 15411 +22 - Misses 2480 2488 +8 ``` | [Files Changed](https://app.codecov.io/gh/phanrahan/magma/pull/1325?src=pr&el=tree&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=None) | Coverage Δ | | |---|---|---| | [magma/backend/mlir/translation\_unit.py](https://app.codecov.io/gh/phanrahan/magma/pull/1325?src=pr&el=tree&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=None#diff-bWFnbWEvYmFja2VuZC9tbGlyL3RyYW5zbGF0aW9uX3VuaXQucHk=) | `92.70% <50.00%> (-0.64%)` | :arrow_down: | | [magma/backend/mlir/hw.py](https://app.codecov.io/gh/phanrahan/magma/pull/1325?src=pr&el=tree&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=None#diff-bWFnbWEvYmFja2VuZC9tbGlyL2h3LnB5) | `98.56% <83.33%> (-0.46%)` | :arrow_down: | | [magma/backend/mlir/compile\_to\_mlir\_opts.py](https://app.codecov.io/gh/phanrahan/magma/pull/1325?src=pr&el=tree&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=None#diff-bWFnbWEvYmFja2VuZC9tbGlyL2NvbXBpbGVfdG9fbWxpcl9vcHRzLnB5) | `100.00% <100.00%> (ø)` | | | [magma/backend/mlir/hardware\_module.py](https://app.codecov.io/gh/phanrahan/magma/pull/1325?src=pr&el=tree&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=None#diff-bWFnbWEvYmFja2VuZC9tbGlyL2hhcmR3YXJlX21vZHVsZS5weQ==) | `96.84% <100.00%> (+0.01%)` | :arrow_up: | | [magma/backend/mlir/magma\_ops.py](https://app.codecov.io/gh/phanrahan/magma/pull/1325?src=pr&el=tree&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=None#diff-bWFnbWEvYmFja2VuZC9tbGlyL21hZ21hX29wcy5weQ==) | `100.00% <100.00%> (ø)` | | | [magma/backend/mlir/mlir\_compiler.py](https://app.codecov.io/gh/phanrahan/magma/pull/1325?src=pr&el=tree&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=None#diff-bWFnbWEvYmFja2VuZC9tbGlyL21saXJfY29tcGlsZXIucHk=) | `95.45% <100.00%> (ø)` | | | [magma/backend/mlir/when\_utils.py](https://app.codecov.io/gh/phanrahan/magma/pull/1325?src=pr&el=tree&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=None#diff-bWFnbWEvYmFja2VuZC9tbGlyL3doZW5fdXRpbHMucHk=) | `99.41% <100.00%> (+0.05%)` | :arrow_up: | | [magma/backend/mlir/xmr\_utils.py](https://app.codecov.io/gh/phanrahan/magma/pull/1325?src=pr&el=tree&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=None#diff-bWFnbWEvYmFja2VuZC9tbGlyL3htcl91dGlscy5weQ==) | `91.93% <100.00%> (+0.13%)` | :arrow_up: | | [magma/bitutils.py](https://app.codecov.io/gh/phanrahan/magma/pull/1325?src=pr&el=tree&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=None#diff-bWFnbWEvYml0dXRpbHMucHk=) | `95.65% <100.00%> (+0.41%)` | :arrow_up: | | [magma/inline\_verilog.py](https://app.codecov.io/gh/phanrahan/magma/pull/1325?src=pr&el=tree&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=None#diff-bWFnbWEvaW5saW5lX3Zlcmlsb2cucHk=) | `98.11% <100.00%> (+0.89%)` | :arrow_up: | | ... and [3 more](https://app.codecov.io/gh/phanrahan/magma/pull/1325?src=pr&el=tree-more&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=None) | | ... and [1 file with indirect coverage changes](https://app.codecov.io/gh/phanrahan/magma/pull/1325/indirect-changes?src=pr&el=tree-more&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=None)

:umbrella: View full report in Codecov by Sentry.
:loudspeaker: Have feedback on the report? Share it here.

rsetaluri commented 1 year ago

@rkshthrmsh let's not include 2608825 in this PR, and do it separately

cdonovick commented 1 year ago

Worth noting that bit_length is already a method on int.

assert (16).bit_length() == 5

So probably no need to provide this as a utility.

rkshthrmsh commented 1 year ago

@cdonovick, thanks for pointing this out. @rsetaluri, I think this PR can be closed in favor of the bit_length method on int.