Improve floating-point fraction and negative constant reconstruction - #4022
Improve floating-point fraction and negative constant reconstruction#4022SychicBoy wants to merge 2 commits into
Conversation
|
This PR looks like it changes a lot of stuff... can you split all the changes into multiple commits, so that there is one small commit per change? Thanks! |
|
https://github.com/icsharpcode/ILSpy/blob/master/CONTRIBUTING.md that awfully looks like a drive-by AI PR. Could you please elaborate why you picked that specific issue and given your Github timeline, how experienced you are with the internal of our decompiler engine? Letting your clanker implement an issue and post a PR is not how we work with contributors. |
Hi Christoph, No, this is not a “drive-by AI PR.” English is not my native language, so I sometimes use AI to help me phrase my messages more clearly and in a more formal tone. That may be why the wording came across that way. To clarify my approach to this PR: I picked #1459 because it was marked Help Wanted and looked like something I could solve. I am still new to ILSpy’s internals, but I have made an effort to understand the relevant code paths related to this change. I’m open to technical discussion and feedback on the implementation. I appreciate maintainers being careful about contributions, and I hope this PR can be evaluated based on the actual code and discussion around it. |
c3358db to
59361d9
Compare
Fixes #1459
Summary
Improve floating-point constant reconstruction by preferring exact, recognizable normalization fractions where appropriate, while preserving simpler existing representations.
This also extends well-known constant detection to recognize negated constants such as
-float.Epsilonand-double.Epsilon.Details
Some normalized floating-point values are currently reconstructed into less recognizable forms. For example:
may become:
Although equivalent, the
/255frepresentation better communicates common byte-normalization patterns.This change adds a restricted set of preferred denominators based on powers of two and
2^n - 1scales. Candidates are only selected when they reproduce the exact originalfloatordoublevalue.The existing fraction approximation logic remains unchanged, and simpler fractions are preserved rather than expanded into larger equivalent representations.
Negative well-known constants are also recognized, allowing values such as:
to be emitted symbolically instead of as numeric literals.
Examples
Before:
After:
Implementation notes
floatanddoublevalues and existing simpler fraction representations.Tests
Regression coverage has been added to the existing
WellKnownConstantsPretty test fixture for:/255fbyte-normalized valuesdoublefractions-float.Epsilon-double.EpsilonThe added cases verify both the new behavior and that less-readable equivalent representations are not introduced.