4

I want to convert a bool into an int. The "standard" option is:

static int F(bool b) 
{
    int i = Convert.ToInt32(b);

    return i;
}

//ILSpy told me about this
public static int ToInt32(bool value)
{
    if (!value)
    {
        return 0;
    }
    return 1;
}

This code generates following assembly:

<Program>$.<<Main>$>g__F|0_0(Boolean)
    L0000: test cl, cl
    L0002: jne short L0008
    L0004: xor eax, eax
    L0006: jmp short L000d
    L0008: mov eax, 1
    L000d: ret

As you might have noticed this is a questionable way (I think it is, I'm not an expert) of converting a bool into an int.

What I have tried

The hunt is for the following assembly which is generated by the GCC:

code:

__attribute__((ms_abi)) 
int
f(bool b) {
        int i;
        i = (int)b;

        return i;
}

asm:

f(bool):
        movzx   eax, cl
        ret
  • In the first step I combined the functions:
static int G(bool b) 
{
    int i = b == true ? 1 : 0;

    return i;
}

I think it helped a bit (see the comments in code).

<Program>$.<<Main>$>g__G|0_1(Boolean)
    L0000: test cl, cl
    L0002: jne short L0007
    L0004: xor eax, eax
    L0006: ret            ; This returns directly instead of jumping to RET instruction.
    L0007: mov eax, 1
    L000c: ret
  • In the next step I tried to use unsafe tricks:
static unsafe int H(bool b) 
{
    int i = *(int*)&b;         

    return i;
}

this generates:

<Program>$.<<Main>$>g__H|0_2(Boolean)
    L0000: mov [rsp+8], ecx           ; it looks better but I can't get rid of this line
    L0004: mov eax, [rsp+8]           ; it looks better but I can't get rid of this line
    L0008: movzx eax, al
    L000b: ret
  • In the next step I removed the temp variable (I thought it would help).
static unsafe int Y(bool b) 
{
    return *(int*)&b;
}

this generates the same ASM:

<Program>$.<<Main>$>g__Y|0_3(Boolean)
    L0000: mov [rsp+8], ecx
    L0004: mov eax, [rsp+8]
    L0008: movzx eax, al
    L000b: ret

Question

As you can see I'm stuck here (I don't know how to remove the first 2 instructions). Is there a way of converting a bool variable into an int one?

Note

  • In case you want to play with example: here is the SharpLab link.

  • Benchmark results:

on x64/Release for 5000000000 iterations:

  1. H() took ~1320ms
  2. F() took ~1610ms
  • Included the code for benchmarking:
var w = new Stopwatch();

long r = 0;
for (int i = 0; i < 10; ++i)
{
    w.Restart();
    for (long j = 0; j < 5000000000; ++j)
    {
        F(true);
        F(false);
    }
    w.Stop();
    r += w.ElapsedMilliseconds;
    Console.WriteLine(w.ElapsedMilliseconds);
}

Console.WriteLine("AV" + r / 10);
32
  • 1
    Are they actually, measurably slower?
    – canton7
    Apr 7, 2021 at 11:33
  • 1
    Why Convert.ToInt32(bool) uses a "questionable way"? Apr 7, 2021 at 11:34
  • 1
    If I'm not wrong. MOVZX has latency of 1 cycle, where the 1st version has latency of ~3-4 cycles? I'm not used to measure assembly instructions. Correct me if I'm wrong.
    – user12722843
    Apr 7, 2021 at 11:36
  • 1
    @PMF this asm was generated in Release mode.
    – user12722843
    Apr 7, 2021 at 11:40
  • 1
    @jdweng: no, the opcode for movzx eax, cl is 2 bytes (felixcloutier.com/x86/movzx), and the whole insn is 3 bytes long because it also includes a ModRM byte to specify the register (or memory) operands. Instruction-length isn't very significant in performance on modern x86 CPUs; hot code often runs from the uop cache which caches the result of decoding to uops. See realworldtech.com/sandy-bridge/4 and Agner Fog's microarch guide. Also, modern Intel pipelines are 4 or 5-wide, not 8, and fetch from I-cache in 16 byte chunks on uop-cache miss. Apr 7, 2021 at 19:43

2 Answers 2

3

It's not all that surprising that reading 4 bytes from a bool generates code that spills to memory first, then reloads, because that's a weird thing to do.

If you're going to mess around with unsafe pointer-casting for type-punning, surely you should be reading the bool into a same-sized integral type like unsigned char or uint8_t or whatever equivalent C# has, and then casting (or implicitly converting) that narrow type to int. Apparently that's Byte.

using System;
static unsafe int H(bool b) 
{
    return *(Byte*)&b;         
}

asm on Sharplab, and see below for this inlining into a caller for H(a == b).

<Program>$.<<Main>$>g__H|0_0(Boolean)
    L0000: mov eax, ecx
    L0002: ret

So apparently the ABI / calling convention passes narrow args like "bool" sign- or zero-extended to 32-bit already. Or else this is more unsafe than I realize and will actually result in int values that aren't 0 or 1!

We get a movzx-load if we take a pointer-to-bool that's not already in a register:

static unsafe int from_mem(bool *b) 
{
    return *(Byte*)b;
}
<Program>$.<<Main>$>g__from_mem|0_1(Boolean*)
    L0000: movzx eax, byte ptr [rcx]
    L0003: ret

Re: performance benefit

There were some questions raised in comments about which is actually better. (And some nonsensical performance claims about code-size and front-end fetch which I replied to in comments.)

If branching was better in general, C and C++ compilers would do that, but they don't. This is very much a missed optimization in current C# implementations; that branching asm is insane, IMO. Possibly / hopefully that goes away with 2nd-stage JITing for hot code-paths, in which case messing around with unsafe could make things worse. So there is some merit to testing real use-cases.

movzx eax, cl has zero latency on current Intel CPUs (Can x86's MOV really be "free"? Why can't I reproduce this at all?), or 1 cycle latency on AMD. (https://uops.info/ and https://agner.org/optimize/). So the only cost is 1 uop for the front-end, and a data dependency on the input. (i.e. the int value isn't ready for use by later instructions until the bool value is ready, like with normal operations such as +)

Branching has the possible upside of using the result now and verifying it was correct when the bool is actually available (branch prediction + speculative exec break the data dependency), but has the huge downside that branch mispredicts stall the pipeline for ~15 cycles, and wastes any work done since the branch. Unless it's very predictable, movzx is much better.

The most likely case for "very predictable" would be a value that never changes, in which case reading it should be cheap (unless it misses in cache) and out-of-order exec can do that nice and early, which would make movzx good and avoid using up space in the CPU's branch-predictor unnecessarily.

Branching on a bool to create a 0 / 1 is basically using branch prediction to do value-prediction. It's certainly possible that it's a good idea in a few rare cases, but it's not what you want by default.


C and C++ compilers can use movzx when widening a bool to an int because the object-representation of a bool is guaranteed / required by the ABI to be 0 or 1. I assume that's the case in most C# implementations as well, not just a byte with 0 / some-non-zero value that might not be 1.

(But even if you did have an arbitrary non-zero value, the normal way to booleanize it to 0 / 1 is xor eax, eax / test cl,cl / setnz al. i.e. to implement int retval = !!x for an integer byte x.)


Real use-case when inlining:

static int countmatch(int total, int a, int b) {
    //return total + (a==b);   // C
    return total + H(a == b);
}

Sharplab

<Program>$.<<Main>$>g__countmatch|0_2(Int32, Int32, Int32)
    L0000: cmp edx, r8d
    L0003: sete al
    L0006: movzx eax, al
    L0009: add eax, ecx
    L000b: ret

Pretty normal code-gen; what you'd expect from a C compiler, just one missed optimization: should use xor eax,eax / cmp / sete al to take the movzx zero-extension off the critical path for latency. (AL and EAX being part of the same register mean that even on Intel CPUs, mov-elimination doesn't apply). Clang, gcc, and MSVC do this (https://godbolt.org/z/E9fKhh5K8), although older GCC sometimes has trouble avoiding the movzx in other more complicated cases, perhaps minimizing register pressure.

Sharplab doesn't seem to have AArch64 output to let you see if it can compile to cmp w1, w2 / cinc w0, w0, eq like C compilers do. (As well as conditional-select, ARM64 provides a csinc conditional select-increment, which it uses with the zero-register to build cset (x86 setcc) and cinc (add a FLAG condition).) I wouldn't be too optimistic; I'd guess probably still materializing a bool into a register and adding it.

static int countmatch_safe(int total, int a, int b) {
    return total + Convert.ToInt32(a == b);
}

Without unsafe in C#, the silly code-gen inlines and still materialized a boolean for add, instead of branching around an inc. This is even worse than if(a==b) total++; which does compile the way you'd expect.

<Program>$.<<Main>$>g__countmatch_safe|0_3(Int32, Int32, Int32)
    L0000: cmp edx, r8d
    L0003: je short L0009
    L0005: xor eax, eax
    L0007: jmp short L000e
    L0009: mov eax, 1
    L000e: add eax, ecx
    L0010: ret
0

In a system where the conversion could have an impact the most efficient way is to not convert and to keep trues and falses as target type (int or byte, etc.).

4
  • You're absolutely right, the problem is: I can NOT pass an expression like: a == 1 in my parameter when I have byte there.
    – user12722843
    Apr 7, 2021 at 12:09
  • You can pass 1-a which gives you not(a == 1)
    – tymtam
    Apr 7, 2021 at 12:13
  • That was an example. I want to say that it'll be hard to pass a bool there (not hard but you have to go into conversion). Think about str == "ABCD". This becomes already hard to do with bytes. Am I wrong here?
    – user12722843
    Apr 7, 2021 at 12:15
  • 2
    If you work with strings then optimising cycles spent on converting int to bool just doesn't have a measurable impact.
    – tymtam
    Apr 7, 2021 at 12:21

Your Answer

Reminder: Answers generated by Artificial Intelligence tools are not allowed on Stack Overflow. Learn more

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.