-
Notifications
You must be signed in to change notification settings - Fork 5.3k
Description
For a variety of test cases related to converting between a byte/int and a bool, the JIT (as of Core CLR v4.700.20.6603, which is what SharpLab is using), generates pretty poor code unless I perform a pointer-dereference cast between bool and byte.
What is more interesting and concerning is that I get better results by calling a utility method that does the same thing than doing it in that function. Observe:
public static unsafe int AsInteger(this bool foo) { return *(byte*)&foo; }
public static unsafe int Cast(bool foo) { return *(byte*)&foo; }
public static int CastUtility(bool foo) { return foo.AsInteger(); }
Cast's JIT output:
mov [rsp+0x8], ecx
movzx eax, byte [rsp+0x8]
ret
CastUtility's JIT output:
movzx eax, cl
ret
The codegen is actually substantially worse if I specify that I want to use Roslyn master, as well.
category:cq
theme:optimization
skill-level:intermediate
cost:medium