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

Fix check for (neg) zero for fpclass emulation #2151

Closed
wants to merge 5 commits into from

Conversation

MrSidims
Copy link
Contributor

@MrSidims MrSidims commented Sep 8, 2023

We should compare not only to zero integer, but also 'negated' zero.

We should convert to integer, not bitcast to it.

Signed-off-by: Sidorov, Dmitry <dmitry.sidorov@intel.com>
@MrSidims
Copy link
Contributor Author

MrSidims commented Sep 8, 2023

@LU-JOHN please take a look

Signed-off-by: Sidorov, Dmitry <dmitry.sidorov@intel.com>
Signed-off-by: Sidorov, Dmitry <dmitry.sidorov@intel.com>
@MrSidims MrSidims requested a review from svenvh September 8, 2023 13:36
BM->addCmpInst(OpIEqual, ResTy, BitCastToInt, ZeroConst, BB);
if (FPClass & fcPosZero && FPClass & fcNegZero)
APInt ZeroInt = APInt::getZero(BitSize);
auto *ZeroConst =
Copy link
Contributor Author

@MrSidims MrSidims Sep 8, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

mmm, actually, should also move them under if/else

Signed-off-by: Sidorov, Dmitry <dmitry.sidorov@intel.com>
Signed-off-by: Sidorov, Dmitry <dmitry.sidorov@intel.com>
return BM->addCmpInst(OpIEqual, ResTy, BitCastToInt, ZeroConst, BB);
}
// Created 'negated' zero
ZeroInt.setSignBit();
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

As I understood APInt uses 2's complement, which does not have negative zero. Are you sure this is giving the correct result?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

this line in the test added after setting signed bit seems about right

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Right, it works because you're not doing any (2's complement) operations on the APInt.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hi @svenvh

Thanks for the review.
Is the use of APInt agreeable in this scenario, or do we have to change the logic here?

Thanks

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This becomes a moot point once the suggestion from @LU-JOHN below has been applied.

auto *TestIsZero =
BM->addCmpInst(OpIEqual, ResTy, BitCastToInt, ZeroConst, BB);
if (FPClass & fcPosZero && FPClass & fcNegZero)
if (FPClass & fcPosZero && FPClass & fcNegZero) {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Instead of generating:

if (val==+0 || val==-0)

we might get better performance with:

if ((val&0x7fff...)==0)

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We need both val == 0 and val == -0 results. So, it is worthwhile to generate both.

Thanks

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I agree with @LU-JOHN 's suggestion; that should cover both cases.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think I might be missing something here. How will val & 0x&7fffffff give result for TestIsPosZero(..) and TestIsNegZero(..)? Is the idea here to replace val == 0 with (val & 0xFFFFFFFFF == 0) and val == -0 with (val && 0xFFFFFFFF != 0) && (val && 0x7FFFFFFF == 0)?
Can @svenvh or @LU-JOHN, please clarify? Thanks

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

+0.0 is represented by 0x0000.0000 (for floats)
-0.0 is represented by 0x8000.0000 (for floats)
Instead of doing two tests we can check for both by doing:
(val & 0x7FFF.FFFF)==0x0000.0000

@svenvh
Copy link
Member

svenvh commented Sep 14, 2023

Closing as this was superseded by #2154

@svenvh svenvh closed this Sep 14, 2023
@asudarsa
Copy link
Contributor

Thanks @svenvh

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.

4 participants