C
C#6mo ago
thegu5

Float (signed) number comparisons compiled to unsigned version of instruction

Why does
public bool lteq(float a, float b) {
return a <= b;
}
public bool lteq(float a, float b) {
return a <= b;
}
compile to
IL_0000: ldarg.1
IL_0001: ldarg.2
IL_0002: cgt.un
IL_0004: ldc.i4.0
IL_0005: ceq
IL_0007: ret
IL_0000: ldarg.1
IL_0001: ldarg.2
IL_0002: cgt.un
IL_0004: ldc.i4.0
IL_0005: ceq
IL_0007: ret
whereas
public bool not(float a, float b) {
return !(a > b);
}
public bool not(float a, float b) {
return !(a > b);
}
compiles to the same thing but with cgt instead of cgt.un? Are these two expressions not equivalent? I'm also confused why it's using .un for two signed numbers that are ordered sharplab: https://sharplab.io/#v2:EYLgxg9gTgpgtADwGwBYA0AXEBLANgHwAEAmARgFgAoQgZgAIS6BhOgbyrs4fuAglzoA7CBgAUAM1wQAhhjrS0dSTLnAAlGw5dthAOx0AhKOl0AfHXUBuLZwC+N7hb4DcGGAEcJU2fMXKf6pqU2jr6JgA8ALwW1sFc9pS2QA
SharpLab
C#/VB/F# compiler playground.
3 Replies
thegu5
thegu5OP6mo ago
i'm new to messing with IL so I probably missed something
Petris
Petris6mo ago
iirc the un actually stands for unordered, not unsigned and signifies how NaN is handled for floats
thegu5
thegu5OP6mo ago
thank you!
Want results from more Discord servers?
Add your server