Skip to content

Emit cgt, likewise clt, for comparisons #61483

@dsyme

Description

@dsyme

I'm not sure of the right repo for this as it is a suggestion for C# optimization, not language.

I was talking with @tannergooding about the implementation of CompareTo in the BCL, which today is this: (likewise for Int64 etc.)

public int CompareTo(int value) {
            // Need to use compare because subtraction will wrap
            // to positive for very large neg numbers, etc.
            if m_value < value) return -1;
            if m_value > value) return 1;
            return 0;
        }

He suggested that the following would be faster as long as the (x > y) ? 1 : 0 and (x < y) ? 1 : 0 are emitted as the .NET instructions cgt and clt.

int tmp1 = (x > y) ? 1 : 0;
int tmp2 = (x < y) ? 1 : 0;
return tmp1 - tmp2;

We think it is reasonable for the C# compiler to emit these sequences like this when optimization is on.

Context: dotnet/fsharp#13187

Metadata

Metadata

Assignees

Labels

Area-CompilersCode Gen QualityRoom for improvement in the quality of the compiler's generated code

Type

No type

Projects

Status

Done

Milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions