You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
which is because the ISA lacks an instruction to compare bytes.
However, correct me if I'm wrong, but I believe boolean values in .NET are also represented as 0 or 1 internally. This means we can do the comparison in one instruction by testing only the bottom bit:
However, correct me if I'm wrong, but I believe boolean values in .NET are also represented as 0 or 1 internally
Not quite. They are specified to be 0/not-0 by the runtime.
It is the C# language that makes it practically hard to come by denormalized booleans - there they have the three states of 0/1/Undefined.
Ah that's interesting, that probably means we can't do this optimization. It looked to me like the JIT is always returning 0 or 1, but I had indeed only looked at C#.
There were a few interesting incompatibilities with "CLR bools" (true for any non-zero, false for zero) and "C# bools" (1 for true, 0 for false, anything else undefined) in the past, where older versions of the C# compiler generates IL that compares against actual values of 1 and 0 instead of 0 / non-0, resulting in neither-1-nor-0 values just passing though true-or-false conditions. dotnet/roslyn#24652
Here are examples of BCL code that normalises bools to be either 1 or 0:
@kunalspathak No, not directly, but the idea for the original ticket was to replace the tst as well. However the example I gave is now eagerly being handled by if-convert.
This ticket was that tb[n]z could be used for the boolean comparisons as well (same as the sign bit comparison for the second example which is already done), but that highly depends on the representation of boolean in .NET.
From the discussions before it looks like there's no defined boolean value in .NET? i.e. it's just 0 and !0? If that's the case than it can't be done, but would be good to know :)
Activity
SingleAccretion commentedon Sep 19, 2022
Not quite. They are specified to be
0/not-0by the runtime.It is the C# language that makes it practically hard to come by denormalized booleans - there they have the three states of
0/1/Undefined.TamarChristinaArm commentedon Sep 19, 2022
Ah that's interesting, that probably means we can't do this optimization. It looked to me like the JIT is always returning
0or1, but I had indeed only looked at C#.Gnbrkm41 commentedon Sep 26, 2022
There were a few interesting incompatibilities with "CLR bools" (true for any non-zero, false for zero) and "C# bools" (1 for true, 0 for false, anything else undefined) in the past, where older versions of the C# compiler generates IL that compares against actual values of 1 and 0 instead of 0 / non-0, resulting in neither-1-nor-0 values just passing though true-or-false conditions. dotnet/roslyn#24652
Here are examples of BCL code that normalises
bools to be either 1 or 0:runtime/src/libraries/System.Collections/src/System/Collections/BitArray.cs
Lines 139 to 141 in b092091
runtime/src/libraries/System.Collections/src/System/Collections/BitArray.cs
Lines 834 to 835 in b092091
kunalspathak commentedon Jun 5, 2023
@TamarChristinaArm - Is
cselexpensive thantbz? This is the codegen currently ondce5c4779a2of main.@a74nh
TamarChristinaArm commentedon Jun 6, 2023
@kunalspathak No, not directly, but the idea for the original ticket was to replace the
tstas well. However the example I gave is now eagerly being handled by if-convert.Instead consider:
something that can't be if-converted away (same as a non-trivial if block) still generates:
and
This ticket was that
tb[n]zcould be used for the boolean comparisons as well (same as the sign bit comparison for the second example which is already done), but that highly depends on the representation of boolean in .NET.From the discussions before it looks like there's no defined boolean value in .NET? i.e. it's just
0and!0? If that's the case than it can't be done, but would be good to know :)