Opened 5 months ago

Closed 10 days ago

#15196 closed task (fixed)

Invert floating point comparisons such that no extra parity check is required.

Reported by: AndreasK Owned by: AndreasK
Priority: normal Milestone: 8.8.1
Component: Compiler (NCG) Version: 8.4.3
Keywords: CodeGen Cc:
Operating System: Unknown/Multiple Architecture: Unknown/Multiple
Type of failure: None/Unknown Test Case:
Blocked By: Blocking:
Related Tickets: Differential Rev(s): Phab:D4990
Wiki Page:

Description (last modified by AndreasK)

This comment pretty much explains it already:

        -- We have to worry about unordered operands (eg. comparisons
        -- against NaN).  If the operands are unordered, the comparison
        -- sets the parity flag, carry flag and zero flag.
        -- All comparisons are supposed to return false for unordered
        -- operands except for !=, which returns true.
        --
        -- Optimisation: we don't have to test the parity flag if we
        -- know the test has already excluded the unordered case: eg >
        -- and >= test for a zero carry flag, which can only occur for
        -- ordered operands.
        --
        -- ToDo: by reversing comparisons we could avoid testing the
        -- parity flag in more cases.

This would turn a sequence of

jp foo
jb bar
jmp foo

into:

jge foo
jmp bar

Change History (9)

comment:1 Changed 5 months ago by AndreasK

Description: modified (diff)

comment:2 Changed 4 months ago by bgamari

Milestone: 8.6.18.8.1

These won't be addressed for GHC 8.6.

comment:3 Changed 3 months ago by AndreasK

Differential Rev(s): Phab:D4990

There is a patch but it's still buggy atm.

comment:4 Changed 2 months ago by AndreasK

Owner: set to AndreasK

comment:5 Changed 2 months ago by AndreasK

Status: newpatch

Ready for merge from my PoV

comment:6 Changed 5 weeks ago by Krzysztof Gogolewski <krz.gogolewski@…>

In 6bb9bc7/ghc:

Invert FP conditions to eliminate the explicit NaN check.

Summary:
Optimisation: we don't have to test the parity flag if we
know the test has already excluded the unordered case: eg >
and >= test for a zero carry flag, which can only occur for
ordered operands.

By reversing comparisons we can avoid testing the parity
for < and <= as well. This works since:
* If any of the arguments is an NaN CF gets set. Resulting in a false result.
* Since this allows us to rule out NaN we can exchange the arguments and invert the
  direction of the arrows.

Test Plan: ci/nofib

Reviewers: carter, bgamari, alpmestan

Reviewed By: alpmestan

Subscribers: alpmestan, simonpj, jmct, rwbarton, thomie

GHC Trac Issues: #15196

Differential Revision: https://phabricator.haskell.org/D4990

comment:7 Changed 5 weeks ago by monoidal

Status: patchmerge

comment:8 Changed 3 weeks ago by RyanGlScott

Milestone: 8.8.18.6.2

Moving to the 8.6.2 milestone, since these tickets were all recently marked as merge.

comment:9 Changed 10 days ago by bgamari

Milestone: 8.6.28.8.1
Resolution: fixed
Status: mergeclosed

I'm going to punt this to 8.8 to ensure it gets proper testing.

Note: See TracTickets for help on using tickets.