Skip to content

JIT: Wrapper around a float generates additional asm compared to float #115193

Closed as not planned
@MechWarrior99

Description

@MechWarrior99

Description

A wrapper around a float generates additional asm when used compared to using a float. While a wrapper around a int will generate the same asm to a int.

static FloatWrapper WrapperAdd(FloatWrapper fw1, FloatWrapper fw2)
{
    return fw1 + fw2;
}

static float FloatAdd(float f1, float f2)
{
     return f1 + f2;   
}

readonly struct FloatWrapper(float value)
{
    private readonly float _value = value;

    public static implicit operator float (FloatWrapperx) => x._value;
    public static implicit operator FloatWrapper(float x) => new(x);

    public static FloatWrapper operator+(FloatWrapperx, FloatWrappery) => x._value + y._value;
}

Generated asm:

Program.<<Main>$>g__WrapperAdd|0_0(FloatWrapper, FloatWrapper)
    L0000: vmovd xmm0, ecx
    L0004: vmovd xmm1, edx
    L0008: vaddss xmm0, xmm0, xmm1
    L000c: vmovd eax, xmm0
    L0010: ret

Program.<<Main>$>g__FloatAdd|0_1(Single, Single)
    L0000: vaddss xmm0, xmm0, xmm1
    L0004: ret

Generated asm when using the same code but change all floats to ints.

Program.<<Main>$>g__WrapperAdd|0_0(IntWrapper, IntWrapper)
    L0000: lea eax, [rcx+rdx]
    L0003: ret

Program.<<Main>$>g__IntAdd|0_1(Int32, Int32)
    L0000: lea eax, [rcx+rdx]
    L0003: ret

Configuration

net 9
Windows
x64

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions