• What does this code

    From Bonita Montero@3:633/10 to All on Fri Nov 20 08:48:56 2026
    Cool code without any branches and conditional
    moves if the compiler does it as I intended.

    inline xsm_lock::operator int() const noexcept
    {
    return (int)m_shared | -(int)!m_pMtx;
    }

    m_pMtx is a pointer.
    Chris, I'm sure *you* would use relacy with that.


    --- PyGate Linux v1.5.19
    * Origin: Dragon's Lair, PyGate NNTP<>Fido Gate (3:633/10)
  • From Bonita Montero@3:633/10 to All on Fri Nov 20 08:49:34 2026
    Am 20.11.2026 um 08:48 schrieb Bonita Montero:
    Cool code without any branches and conditional
    moves if the compiler does it as I intended.

    inline xsm_lock::operator int() const noexcept
    {
    ˙˙˙˙return (int)m_shared | -(int)!m_pMtx;
    }

    m_pMtx is a pointer.

    m_shared is a bool.

    Chris, I'm sure *you* would use relacy with that.



    --- PyGate Linux v1.5.19
    * Origin: Dragon's Lair, PyGate NNTP<>Fido Gate (3:633/10)
  • From Michael S@3:633/10 to All on Fri Nov 20 11:44:27 2026
    On Fri, 20 Nov 2026 08:48:56 +0100
    Bonita Montero <Bonita.Montero@gmail.com> wrote:

    Cool code without any branches and conditional
    moves if the compiler does it as I intended.

    inline xsm_lock::operator int() const noexcept
    {
    return (int)m_shared | -(int)!m_pMtx;
    }

    m_pMtx is a pointer.
    Chris, I'm sure *you* would use relacy with that.


    The code that people could read and understand is compiled to exactly
    the same asm on RISC-V:
    https://godbolt.org/z/vbvxTGMv5
    And on ARM64:
    https://godbolt.org/z/rv9GT7fxM
    And on x86-64:
    https://godbolt.org/z/fsGb4Keq3

    That's clang.

    With gcc readable code is compiled to better x86-64 asm than the tricky
    one:
    https://godbolt.org/z/7zhMdrEzo

    The same number of instructions in bot cases, but in readable vesrsion dependency chain is shorter (2 vs 3).

    ARM64 is the same again:
    https://godbolt.org/z/641WfW9ds



    --- PyGate Linux v1.5.19
    * Origin: Dragon's Lair, PyGate NNTP<>Fido Gate (3:633/10)
  • From David Brown@3:633/10 to All on Fri Nov 20 11:33:24 2026
    On 20/11/2026 10:44, Michael S wrote:
    On Fri, 20 Nov 2026 08:48:56 +0100
    Bonita Montero <Bonita.Montero@gmail.com> wrote:

    Cool code without any branches and conditional
    moves if the compiler does it as I intended.

    inline xsm_lock::operator int() const noexcept
    {
    return (int)m_shared | -(int)!m_pMtx;
    }


    That's the kind of hack "manual optimisation" code that most C and C++ programmers were able to leave behind at the turn of the century as
    compilers got better at optimising well-written and clear source code.

    m_pMtx is a pointer.
    Chris, I'm sure *you* would use relacy with that.


    The code that people could read and understand is compiled to exactly
    the same asm on RISC-V:

    (I'm copying in the results so that others don't have to follow the
    godbolt links unless they want to play with things themselves.)

    int bar(bool shared, void* p)
    {
    return p ? shared : -1;
    }

    https://godbolt.org/z/vbvxTGMv5
    And on ARM64:
    https://godbolt.org/z/rv9GT7fxM
    And on x86-64:
    https://godbolt.org/z/fsGb4Keq3

    foo(bool, void*):
    xor eax, eax
    cmp rsi, 1
    sbb eax, eax
    or eax, edi
    ret


    That's clang.

    With gcc readable code is compiled to better x86-64 asm than the tricky
    one:
    https://godbolt.org/z/7zhMdrEzo

    The same number of instructions in bot cases, but in readable vesrsion dependency chain is shorter (2 vs 3).


    "foo(bool, void*)":
    cmp rsi, 1
    movzx edi, dil
    sbb eax, eax
    or eax, edi
    ret
    "bar(bool, void*)":
    movzx edi, dil
    test rsi, rsi
    mov eax, -1
    cmovne eax, edi
    ret


    MSVC, however, is not as smart - and that's the compiler Bonita uses AFAIK.

    shared$ = 8
    p$ = 16
    int foo(bool,void *) PROC ; foo, COMDAT
    movzx eax, cl
    test rdx, rdx
    mov ecx, -1
    cmove eax, ecx
    ret 0
    int foo(bool,void *) ENDP ; foo

    shared$ = 8
    p$ = 16
    int bar(bool,void *) PROC ; bar, COMDAT
    movzx eax, cl
    test rdx, rdx
    jne SHORT $LN4@bar
    mov eax, -1
    $LN4@bar:
    ret 0
    int bar(bool,void *) ENDP


    That is, MSVC needs that ugly source code to give as good results as gcc
    does with the much simpler code.


    I have no idea what the original code is intended to be, but using an
    "int" for a three-way classification like this sounds like a really
    obtuse way to write the code. For comparison, using an enum like this generates identical code to "bar" with gcc :

    enum class classifications {
    invalid_pointer = -1, unshared_null = 0, shared_null = 1
    };
    classifications foobar(bool shared, void* p) {
    if (!p) return classifications::invalid_pointer;
    if (shared) return classifications::shared_null;
    return classifications::unshared_null;
    }

    Without knowing the point of the code, or how it might be changed or
    expanded in the future, or what C++ code would clearly follow the specifications of the original function, it is impossible to say what
    style of C++ code would be clearest. But what we can see from this is
    that a good compiler can handle clear source code written in various
    ways, and generates the best results when given clear source code rather
    than obscure hacks.



    --- PyGate Linux v1.5.19
    * Origin: Dragon's Lair, PyGate NNTP<>Fido Gate (3:633/10)
  • From Bonita Montero@3:633/10 to All on Fri Nov 20 12:04:33 2026
    Am 20.11.2026 um 11:33 schrieb David Brown:

    That is, MSVC needs that ugly source code to give as good results as gcc does with the much simpler code.

    I checked the disassemly of clang 21.1.3, g++14 and MSVC. And they
    didn't generate that branch-free code. I like minimalsm code and
    for an expert it'easy to read, so I chose to do it that way.



    --- PyGate Linux v1.5.19
    * Origin: Dragon's Lair, PyGate NNTP<>Fido Gate (3:633/10)
  • From Michael S@3:633/10 to All on Fri Nov 20 13:22:29 2026
    On Fri, 20 Nov 2026 11:33:24 +0100
    David Brown <david.brown@hesbynett.no> wrote:

    On 20/11/2026 10:44, Michael S wrote:
    On Fri, 20 Nov 2026 08:48:56 +0100
    Bonita Montero <Bonita.Montero@gmail.com> wrote:

    Cool code without any branches and conditional
    moves if the compiler does it as I intended.

    inline xsm_lock::operator int() const noexcept
    {
    return (int)m_shared | -(int)!m_pMtx;
    }


    That's the kind of hack "manual optimisation" code that most C and
    C++ programmers were able to leave behind at the turn of the century
    as compilers got better at optimising well-written and clear source
    code.

    m_pMtx is a pointer.
    Chris, I'm sure *you* would use relacy with that.


    The code that people could read and understand is compiled to
    exactly the same asm on RISC-V:

    (I'm copying in the results so that others don't have to follow the
    godbolt links unless they want to play with things themselves.)

    int bar(bool shared, void* p)
    {
    return p ? shared : -1;
    }

    https://godbolt.org/z/vbvxTGMv5
    And on ARM64:
    https://godbolt.org/z/rv9GT7fxM
    And on x86-64:
    https://godbolt.org/z/fsGb4Keq3

    foo(bool, void*):
    xor eax, eax
    cmp rsi, 1
    sbb eax, eax
    or eax, edi
    ret


    That's clang.

    With gcc readable code is compiled to better x86-64 asm than the
    tricky one:
    https://godbolt.org/z/7zhMdrEzo

    The same number of instructions in bot cases, but in readable
    vesrsion dependency chain is shorter (2 vs 3).


    "foo(bool, void*)":
    cmp rsi, 1
    movzx edi, dil
    sbb eax, eax
    or eax, edi
    ret
    "bar(bool, void*)":
    movzx edi, dil
    test rsi, rsi
    mov eax, -1
    cmovne eax, edi
    ret


    MSVC, however, is not as smart - and that's the compiler Bonita uses
    AFAIK.

    shared$ = 8
    p$ = 16
    int foo(bool,void *) PROC ; foo, COMDAT
    movzx eax, cl
    test rdx, rdx
    mov ecx, -1
    cmove eax, ecx
    ret 0
    int foo(bool,void *) ENDP ; foo

    shared$ = 8
    p$ = 16
    int bar(bool,void *) PROC ; bar, COMDAT
    movzx eax, cl
    test rdx, rdx
    jne SHORT $LN4@bar
    mov eax, -1
    $LN4@bar:
    ret 0
    int bar(bool,void *) ENDP


    That is, MSVC needs that ugly source code to give as good results as
    gcc does with the much simpler code.


    Pay attention that -O in MSVC does not have the same meaning as -O in
    in gcc or clang. gcc -O is approximately the same as MSVC -O1 https://godbolt.org/z/sojq94P4n
    The same code for foo and bar.
    Bonita, being Bonita, probably uses -Ox, which is similar to gcc -O3.

    As to what is good result, it's not that simple. When condition is
    predictable, i.e. when p is ==0 >95% of time or <5% of time then code
    with branch will be faster than code without branch. Esp. so when p is originated outside of L1D cache.
    Of course, in original code of Bonita both 'p' and 'shared' are fields
    of the same structure, so situation of 'p' not in cache and 'shared' in
    cache does not appear likely. However even when they are both in cache, prdictable branch is at least as fast as branchless variant.


    I have no idea what the original code is intended to be, but using an
    "int" for a three-way classification like this sounds like a really
    obtuse way to write the code. For comparison, using an enum like
    this generates identical code to "bar" with gcc :

    enum class classifications {
    invalid_pointer = -1, unshared_null = 0, shared_null = 1
    };

    Your names do not sound right.
    I suppose that you had good intentions, but result (confusing names) is
    worse that original (no symbolic names).

    classifications foobar(bool shared, void* p) {
    if (!p) return classifications::invalid_pointer;
    if (shared) return classifications::shared_null;
    return classifications::unshared_null;
    }

    Without knowing the point of the code, or how it might be changed or expanded in the future, or what C++ code would clearly follow the specifications of the original function, it is impossible to say what
    style of C++ code would be clearest. But what we can see from this
    is that a good compiler can handle clear source code written in
    various ways, and generates the best results when given clear source
    code rather than obscure hacks.





    --- PyGate Linux v1.5.19
    * Origin: Dragon's Lair, PyGate NNTP<>Fido Gate (3:633/10)
  • From David Brown@3:633/10 to All on Fri Nov 20 12:39:07 2026
    On 20/11/2026 12:04, Bonita Montero wrote:
    Am 20.11.2026 um 11:33 schrieb David Brown:

    That is, MSVC needs that ugly source code to give as good results as
    gcc does with the much simpler code.

    I checked the disassemly of clang 21.1.3, g++14 and MSVC. And they
    didn't generate that branch-free code. I like minimalsm code and
    for an expert it'easy to read, so I chose to do it that way.

    Maybe you are doing something wrong. A quick check on godbolt shows gcc
    8 onwards generates branch-free code for each of these variants of
    source code - with better code for the clearer source.


    --- PyGate Linux v1.5.19
    * Origin: Dragon's Lair, PyGate NNTP<>Fido Gate (3:633/10)
  • From David Brown@3:633/10 to All on Fri Nov 20 12:56:25 2026
    On 20/11/2026 12:22, Michael S wrote:
    On Fri, 20 Nov 2026 11:33:24 +0100
    David Brown <david.brown@hesbynett.no> wrote:

    On 20/11/2026 10:44, Michael S wrote:
    On Fri, 20 Nov 2026 08:48:56 +0100
    Bonita Montero <Bonita.Montero@gmail.com> wrote:

    Cool code without any branches and conditional
    moves if the compiler does it as I intended.

    inline xsm_lock::operator int() const noexcept
    {
    return (int)m_shared | -(int)!m_pMtx;
    }


    That's the kind of hack "manual optimisation" code that most C and
    C++ programmers were able to leave behind at the turn of the century
    as compilers got better at optimising well-written and clear source
    code.

    m_pMtx is a pointer.
    Chris, I'm sure *you* would use relacy with that.


    The code that people could read and understand is compiled to
    exactly the same asm on RISC-V:

    (I'm copying in the results so that others don't have to follow the
    godbolt links unless they want to play with things themselves.)

    int bar(bool shared, void* p)
    {
    return p ? shared : -1;
    }

    https://godbolt.org/z/vbvxTGMv5
    And on ARM64:
    https://godbolt.org/z/rv9GT7fxM
    And on x86-64:
    https://godbolt.org/z/fsGb4Keq3

    foo(bool, void*):
    xor eax, eax
    cmp rsi, 1
    sbb eax, eax
    or eax, edi
    ret


    That's clang.

    With gcc readable code is compiled to better x86-64 asm than the
    tricky one:
    https://godbolt.org/z/7zhMdrEzo

    The same number of instructions in bot cases, but in readable
    vesrsion dependency chain is shorter (2 vs 3).


    "foo(bool, void*)":
    cmp rsi, 1
    movzx edi, dil
    sbb eax, eax
    or eax, edi
    ret
    "bar(bool, void*)":
    movzx edi, dil
    test rsi, rsi
    mov eax, -1
    cmovne eax, edi
    ret


    MSVC, however, is not as smart - and that's the compiler Bonita uses
    AFAIK.

    shared$ = 8
    p$ = 16
    int foo(bool,void *) PROC ; foo, COMDAT
    movzx eax, cl
    test rdx, rdx
    mov ecx, -1
    cmove eax, ecx
    ret 0
    int foo(bool,void *) ENDP ; foo

    shared$ = 8
    p$ = 16
    int bar(bool,void *) PROC ; bar, COMDAT
    movzx eax, cl
    test rdx, rdx
    jne SHORT $LN4@bar
    mov eax, -1
    $LN4@bar:
    ret 0
    int bar(bool,void *) ENDP


    That is, MSVC needs that ugly source code to give as good results as
    gcc does with the much simpler code.


    Pay attention that -O in MSVC does not have the same meaning as -O in
    in gcc or clang. gcc -O is approximately the same as MSVC -O1 https://godbolt.org/z/sojq94P4n
    The same code for foo and bar.
    Bonita, being Bonita, probably uses -Ox, which is similar to gcc -O3.

    I am not very familiar with the flags for MSVC - indeed, I have barely
    ever used the tool outside of brief checks on godbolt. So I don't know
    quite how they compare.

    My usual choice for gcc is "-O2", but for small functions like this you typically get the same results for -O1, -O2 or -O3. Interestingly, for
    MSVC the /O2 optimisation (which is what I used) and /Ox (which I did
    not know about until now) gives the results above. Using /O1, on the
    other hand, gives basically the same code for "bar" as for "foo" -
    branchless code object code from the clearer source code. (That's just
    with godbolt "x64 msvc v19.latest" - I don't haven't bothered checking
    other versions.)


    As to what is good result, it's not that simple. When condition is predictable, i.e. when p is ==0 >95% of time or <5% of time then code
    with branch will be faster than code without branch. Esp. so when p is originated outside of L1D cache.

    Indeed. The speed of code on "big" processors is always hard to
    predict, and can depend on many factors. (That's one reason I like microcontrollers - they are a lot more predictable.) Real-world speed
    depends on the values you give this kind of function, and all the
    surrounding code - a function like this is likely to be inlined.

    It's this kind of thing that makes the OP's code so pointless. Code
    written in a way that is clear to human readers is usually easier for compilers to optimise in different ways in different circumstances. And
    if you really need the absolute highest speeds, you have to measure it
    and profile it with real use-cases, targeting the specific processors
    you want to use (not just generic x86-64), and perhaps use
    profile-guided optimisation.

    Of course, in original code of Bonita both 'p' and 'shared' are fields
    of the same structure, so situation of 'p' not in cache and 'shared' in
    cache does not appear likely. However even when they are both in cache, prdictable branch is at least as fast as branchless variant.


    I have no idea what the original code is intended to be, but using an
    "int" for a three-way classification like this sounds like a really
    obtuse way to write the code. For comparison, using an enum like
    this generates identical code to "bar" with gcc :

    enum class classifications {
    invalid_pointer = -1, unshared_null = 0, shared_null = 1
    };

    Your names do not sound right.
    I suppose that you had good intentions, but result (confusing names) is
    worse that original (no symbolic names).

    I merely wanted to make the point that you could give names with an enumeration rather than magic values of an int, and that you could
    re-write the function in a completely different manner and still get the
    same object code. I was not suggesting these are sensible names or that
    this other way of writing the code would be good for the source - that's
    not something that could be determined from the original post.
    Obviously the person writing the real code should pick appropriate names.


    classifications foobar(bool shared, void* p) {
    if (!p) return classifications::invalid_pointer;
    if (shared) return classifications::shared_null;
    return classifications::unshared_null;
    }

    Without knowing the point of the code, or how it might be changed or
    expanded in the future, or what C++ code would clearly follow the
    specifications of the original function, it is impossible to say what
    style of C++ code would be clearest. But what we can see from this
    is that a good compiler can handle clear source code written in
    various ways, and generates the best results when given clear source
    code rather than obscure hacks.






    --- PyGate Linux v1.5.19
    * Origin: Dragon's Lair, PyGate NNTP<>Fido Gate (3:633/10)
  • From Michael S@3:633/10 to All on Fri Nov 20 14:43:37 2026
    On Fri, 20 Nov 2026 12:56:25 +0100
    David Brown <david.brown@hesbynett.no> wrote:

    On 20/11/2026 12:22, Michael S wrote:
    On Fri, 20 Nov 2026 11:33:24 +0100
    David Brown <david.brown@hesbynett.no> wrote:

    On 20/11/2026 10:44, Michael S wrote:
    On Fri, 20 Nov 2026 08:48:56 +0100
    Bonita Montero <Bonita.Montero@gmail.com> wrote:

    Cool code without any branches and conditional
    moves if the compiler does it as I intended.

    inline xsm_lock::operator int() const noexcept
    {
    return (int)m_shared | -(int)!m_pMtx;
    }


    That's the kind of hack "manual optimisation" code that most C and
    C++ programmers were able to leave behind at the turn of the
    century as compilers got better at optimising well-written and
    clear source code.

    m_pMtx is a pointer.
    Chris, I'm sure *you* would use relacy with that.


    The code that people could read and understand is compiled to
    exactly the same asm on RISC-V:

    (I'm copying in the results so that others don't have to follow the
    godbolt links unless they want to play with things themselves.)

    int bar(bool shared, void* p)
    {
    return p ? shared : -1;
    }

    https://godbolt.org/z/vbvxTGMv5
    And on ARM64:
    https://godbolt.org/z/rv9GT7fxM
    And on x86-64:
    https://godbolt.org/z/fsGb4Keq3

    foo(bool, void*):
    xor eax, eax
    cmp rsi, 1
    sbb eax, eax
    or eax, edi
    ret


    That's clang.

    With gcc readable code is compiled to better x86-64 asm than the
    tricky one:
    https://godbolt.org/z/7zhMdrEzo

    The same number of instructions in bot cases, but in readable
    vesrsion dependency chain is shorter (2 vs 3).


    "foo(bool, void*)":
    cmp rsi, 1
    movzx edi, dil
    sbb eax, eax
    or eax, edi
    ret
    "bar(bool, void*)":
    movzx edi, dil
    test rsi, rsi
    mov eax, -1
    cmovne eax, edi
    ret


    MSVC, however, is not as smart - and that's the compiler Bonita
    uses AFAIK.

    shared$ = 8
    p$ = 16
    int foo(bool,void *) PROC ; foo,
    COMDAT movzx eax, cl
    test rdx, rdx
    mov ecx, -1
    cmove eax, ecx
    ret 0
    int foo(bool,void *) ENDP ; foo

    shared$ = 8
    p$ = 16
    int bar(bool,void *) PROC ; bar,
    COMDAT movzx eax, cl
    test rdx, rdx
    jne SHORT $LN4@bar
    mov eax, -1
    $LN4@bar:
    ret 0
    int bar(bool,void *) ENDP


    That is, MSVC needs that ugly source code to give as good results
    as gcc does with the much simpler code.


    Pay attention that -O in MSVC does not have the same meaning as -O
    in in gcc or clang. gcc -O is approximately the same as MSVC -O1 https://godbolt.org/z/sojq94P4n
    The same code for foo and bar.
    Bonita, being Bonita, probably uses -Ox, which is similar to gcc
    -O3.

    I am not very familiar with the flags for MSVC - indeed, I have
    barely ever used the tool outside of brief checks on godbolt. So I
    don't know quite how they compare.

    My usual choice for gcc is "-O2", but for small functions like this
    you typically get the same results for -O1, -O2 or -O3.
    Interestingly, for MSVC the /O2 optimisation (which is what I used)
    and /Ox (which I did not know about until now) gives the results
    above. Using /O1, on the other hand, gives basically the same code
    for "bar" as for "foo" - branchless code object code from the clearer
    source code. (That's just with godbolt "x64 msvc v19.latest" - I
    don't haven't bothered checking other versions.)


    As to what is good result, it's not that simple. When condition is predictable, i.e. when p is ==0 >95% of time or <5% of time then
    code with branch will be faster than code without branch. Esp. so
    when p is originated outside of L1D cache.

    Indeed. The speed of code on "big" processors is always hard to
    predict, and can depend on many factors. (That's one reason I like microcontrollers - they are a lot more predictable.) Real-world
    speed depends on the values you give this kind of function, and all
    the surrounding code - a function like this is likely to be inlined.


    It is possible and relatively easy to convince clang to generate
    branchy code that is similar to MSVC /Ox and likely the fastest in
    practice:

    int baz(_Bool shared, void* p)
    {
    return __builtin_expect( (_Bool)p, 1) ? shared : -1;
    }
    https://godbolt.org/z/GdTxoqf4a

    It is much harder to convince gcc to do the same or similar.
    I see such stubbornness as a serious disadvantage of gcc optimizer.

    It's this kind of thing that makes the OP's code so pointless. Code
    written in a way that is clear to human readers is usually easier for compilers to optimise in different ways in different circumstances.
    And if you really need the absolute highest speeds, you have to
    measure it and profile it with real use-cases, targeting the specific processors you want to use (not just generic x86-64), and perhaps use profile-guided optimisation.



    --- PyGate Linux v1.5.19
    * Origin: Dragon's Lair, PyGate NNTP<>Fido Gate (3:633/10)
  • From David Brown@3:633/10 to All on Fri Nov 20 16:05:19 2026
    On 20/11/2026 13:43, Michael S wrote:
    On Fri, 20 Nov 2026 12:56:25 +0100
    David Brown <david.brown@hesbynett.no> wrote:

    On 20/11/2026 12:22, Michael S wrote:


    As to what is good result, it's not that simple. When condition is
    predictable, i.e. when p is ==0 >95% of time or <5% of time then
    code with branch will be faster than code without branch. Esp. so
    when p is originated outside of L1D cache.

    Indeed. The speed of code on "big" processors is always hard to
    predict, and can depend on many factors. (That's one reason I like
    microcontrollers - they are a lot more predictable.) Real-world
    speed depends on the values you give this kind of function, and all
    the surrounding code - a function like this is likely to be inlined.


    It is possible and relatively easy to convince clang to generate
    branchy code that is similar to MSVC /Ox and likely the fastest in
    practice:

    int baz(_Bool shared, void* p)
    {
    return __builtin_expect( (_Bool)p, 1) ? shared : -1;
    }
    https://godbolt.org/z/GdTxoqf4a

    It is much harder to convince gcc to do the same or similar.
    I see such stubbornness as a serious disadvantage of gcc optimizer.

    A technique for forcing particular code generation is to use volatile do-nothing inline assembly.

    int baz2(bool shared, void* p)
    {
    if (p) {
    asm volatile("");
    return shared;
    }
    return -1;
    }

    gcc:

    "baz2(bool, void*)":
    mov eax, -1
    test rsi, rsi
    je .L1
    movzx eax, dil
    .L1:
    ret


    clang:

    baz2(bool, void*):
    test rsi, rsi
    je .LBB0_1
    movzx eax, dil
    ret
    .LBB0_1:
    mov eax, -1
    ret


    The inline assembly is not standard C++, but neither is
    __builtin_expect. And it is highly portable assembly :-)


    If you prefer to have a single expression, you can have :

    inline int volatile_unspecified_int() {
    int x;
    asm volatile("" : "=r" (x));
    return x;
    }

    int baz3(bool shared, void* p)
    {
    return p ? (volatile_unspecified_int(), shared) : -1;
    }


    Of course it is difficult to give guarantees about exactly how code is generated, and things may change between versions, so this is all
    somewhat fragile and probably not a great idea in practice. But it is
    fun to play around with!


    --- PyGate Linux v1.5.19
    * Origin: Dragon's Lair, PyGate NNTP<>Fido Gate (3:633/10)
  • From Chris M. Thomasson@3:633/10 to All on Fri Nov 20 14:39:12 2026
    On 11/19/2026 11:48 PM, Bonita Montero wrote:
    Cool code without any branches and conditional
    moves if the compiler does it as I intended.

    inline xsm_lock::operator int() const noexcept
    {
    ˙˙˙˙return (int)m_shared | -(int)!m_pMtx;
    }

    m_pMtx is a pointer.
    Chris, I'm sure *you* would use relacy with that.


    ;^) What is m_shared? A raw pointer?

    Btw, there is no shame in using an interesting race detector.

    --- PyGate Linux v1.5.19
    * Origin: Dragon's Lair, PyGate NNTP<>Fido Gate (3:633/10)
  • From Chris M. Thomasson@3:633/10 to All on Fri Nov 20 14:39:53 2026
    On 11/20/2026 2:39 PM, Chris M. Thomasson wrote:
    On 11/19/2026 11:48 PM, Bonita Montero wrote:
    Cool code without any branches and conditional
    moves if the compiler does it as I intended.

    inline xsm_lock::operator int() const noexcept
    {
    ˙˙˙˙˙return (int)m_shared | -(int)!m_pMtx;
    }

    m_pMtx is a pointer.
    Chris, I'm sure *you* would use relacy with that.


    ;^) What is m_shared? A raw pointer?

    DOH!!!!! Sorry Bonita. You said it was a bool.


    Btw, there is no shame in using an interesting race detector.


    --- PyGate Linux v1.5.19
    * Origin: Dragon's Lair, PyGate NNTP<>Fido Gate (3:633/10)
  • From Chris M. Thomasson@3:633/10 to All on Fri Nov 20 14:45:03 2026
    On 11/20/2026 7:05 AM, David Brown wrote:
    On 20/11/2026 13:43, Michael S wrote:
    On Fri, 20 Nov 2026 12:56:25 +0100
    David Brown <david.brown@hesbynett.no> wrote:

    On 20/11/2026 12:22, Michael S wrote:


    As to what is good result, it's not that simple. When condition is
    predictable, i.e. when p is ==0 >95% of time or <5% of time then
    code with branch will be faster than code without branch. Esp. so
    when p is originated outside of L1D cache.

    Indeed.˙ The speed of code on "big" processors is always hard to
    predict, and can depend on many factors.˙ (That's one reason I like
    microcontrollers - they are a lot more predictable.)˙ Real-world
    speed depends on the values you give this kind of function, and all
    the surrounding code - a function like this is likely to be inlined.


    It is possible and relatively easy to convince clang to generate
    branchy code that is similar to MSVC /Ox and likely the fastest in
    practice:

    int baz(_Bool shared, void* p)
    {
    ˙˙ return __builtin_expect( (_Bool)p, 1) ? shared : -1;
    }
    https://godbolt.org/z/GdTxoqf4a

    It is much harder to convince gcc to do the same or similar.
    I see such stubbornness as a serious disadvantage of gcc optimizer.

    A technique for forcing particular code generation is to use volatile do-nothing inline assembly.

    int baz2(bool shared, void* p)
    {
    ˙˙˙˙if (p) {
    ˙˙˙˙˙˙˙ asm volatile("");
    ˙˙˙˙˙˙˙ return shared;
    ˙˙˙˙}
    ˙˙˙˙return -1;
    }

    gcc:

    "baz2(bool, void*)":
    ˙˙˙˙˙˙˙ mov˙˙˙˙ eax, -1
    ˙˙˙˙˙˙˙ test˙˙˙ rsi, rsi
    ˙˙˙˙˙˙˙ je˙˙˙˙˙ .L1
    ˙˙˙˙˙˙˙ movzx˙˙ eax, dil
    .L1:
    ˙˙˙˙˙˙˙ ret


    clang:

    baz2(bool, void*):
    ˙˙˙˙˙˙˙ test˙˙˙ rsi, rsi
    ˙˙˙˙˙˙˙ je˙˙˙˙˙ .LBB0_1
    ˙˙˙˙˙˙˙ movzx˙˙ eax, dil
    ˙˙˙˙˙˙˙ ret
    .LBB0_1:
    ˙˙˙˙˙˙˙ mov˙˙˙˙ eax, -1
    ˙˙˙˙˙˙˙ ret


    The inline assembly is not standard C++, but neither is
    __builtin_expect.˙ And it is highly portable assembly :-)


    If you prefer to have a single expression, you can have :

    inline int volatile_unspecified_int() {
    ˙˙˙˙int x;
    ˙˙˙˙asm volatile("" : "=r" (x));

    Can we use std::atomic_signal_fence for it? Iirc, its compiler only.


    ˙˙˙˙return x;
    }

    int baz3(bool shared, void* p)
    {
    ˙ return p ? (volatile_unspecified_int(), shared) : -1;
    }


    Of course it is difficult to give guarantees about exactly how code is generated, and things may change between versions, so this is all
    somewhat fragile and probably not a great idea in practice.˙ But it is
    fun to play around with!



    --- PyGate Linux v1.5.19
    * Origin: Dragon's Lair, PyGate NNTP<>Fido Gate (3:633/10)
  • From Bonita Montero@3:633/10 to All on Sat Nov 21 06:39:22 2026
    Am 20.11.2026 um 12:39 schrieb David Brown:

    Maybe you are doing something wrong.˙ A quick check on godbolt shows
    gcc 8 onwards generates branch-free code for each of these variants
    of source code - with better code for the clearer source.
    https://godbolt.org/z/TfEnTr3T7

    --- PyGate Linux v1.5.19
    * Origin: Dragon's Lair, PyGate NNTP<>Fido Gate (3:633/10)
  • From David Brown@3:633/10 to All on Sat Nov 21 12:38:13 2026
    On 21/11/2026 06:39, Bonita Montero wrote:
    Am 20.11.2026 um 12:39 schrieb David Brown:

    Maybe you are doing something wrong.˙ A quick check on godbolt shows
    gcc 8 onwards generates branch-free code for each of these variants
    of source code - with better code for the clearer source.
    https://godbolt.org/z/TfEnTr3T7

    It's helpful to copy the actual code, not just the godbolt link - then
    group readers can see it immediately:

    struct strct
    {
    explicit operator int() const noexcept;
    void *p;
    bool b;
    };

    strct::operator int() const noexcept
    {
    if( !p )
    return -1;
    return (int)b;
    }

    "strct::operator int() const":
    cmp QWORD PTR [rdi], 0
    je .L3
    movzx eax, BYTE PTR [rdi+8]
    ret
    .L3:
    mov eax, -1
    ret



    That is very different code, and the compiler has different heuristics
    for generating object code. Here the compiler is generating a
    conditional jump to avoid a load from memory. I would expect that to be
    a win, at least if the branch gets reasonably good prediction hit
    ratios. I am not an expert on the timing of modern x86 processors, but
    I believe that even if "b" is in the L1 cache, loading it (which would
    be necessary for branchless code) would take a lot more cycles than a predicted jump.


    --- PyGate Linux v1.5.19
    * Origin: Dragon's Lair, PyGate NNTP<>Fido Gate (3:633/10)
  • From David Brown@3:633/10 to All on Sat Nov 21 12:43:13 2026
    On 20/11/2026 23:45, Chris M. Thomasson wrote:
    On 11/20/2026 7:05 AM, David Brown wrote:
    On 20/11/2026 13:43, Michael S wrote:
    On Fri, 20 Nov 2026 12:56:25 +0100
    David Brown <david.brown@hesbynett.no> wrote:

    On 20/11/2026 12:22, Michael S wrote:


    As to what is good result, it's not that simple. When condition is
    predictable, i.e. when p is ==0 >95% of time or <5% of time then
    code with branch will be faster than code without branch. Esp. so
    when p is originated outside of L1D cache.

    Indeed.˙ The speed of code on "big" processors is always hard to
    predict, and can depend on many factors.˙ (That's one reason I like
    microcontrollers - they are a lot more predictable.)˙ Real-world
    speed depends on the values you give this kind of function, and all
    the surrounding code - a function like this is likely to be inlined.


    It is possible and relatively easy to convince clang to generate
    branchy code that is similar to MSVC /Ox and likely the fastest in
    practice:

    int baz(_Bool shared, void* p)
    {
    ˙˙ return __builtin_expect( (_Bool)p, 1) ? shared : -1;
    }
    https://godbolt.org/z/GdTxoqf4a

    It is much harder to convince gcc to do the same or similar.
    I see such stubbornness as a serious disadvantage of gcc optimizer.

    A technique for forcing particular code generation is to use volatile
    do-nothing inline assembly.

    int baz2(bool shared, void* p)
    {
    ˙˙˙˙˙if (p) {
    ˙˙˙˙˙˙˙˙ asm volatile("");
    ˙˙˙˙˙˙˙˙ return shared;
    ˙˙˙˙˙}
    ˙˙˙˙˙return -1;
    }

    gcc:

    "baz2(bool, void*)":
    ˙˙˙˙˙˙˙˙ mov˙˙˙˙ eax, -1
    ˙˙˙˙˙˙˙˙ test˙˙˙ rsi, rsi
    ˙˙˙˙˙˙˙˙ je˙˙˙˙˙ .L1
    ˙˙˙˙˙˙˙˙ movzx˙˙ eax, dil
    .L1:
    ˙˙˙˙˙˙˙˙ ret


    clang:

    baz2(bool, void*):
    ˙˙˙˙˙˙˙˙ test˙˙˙ rsi, rsi
    ˙˙˙˙˙˙˙˙ je˙˙˙˙˙ .LBB0_1
    ˙˙˙˙˙˙˙˙ movzx˙˙ eax, dil
    ˙˙˙˙˙˙˙˙ ret
    .LBB0_1:
    ˙˙˙˙˙˙˙˙ mov˙˙˙˙ eax, -1
    ˙˙˙˙˙˙˙˙ ret


    The inline assembly is not standard C++, but neither is
    __builtin_expect.˙ And it is highly portable assembly :-)


    If you prefer to have a single expression, you can have :

    inline int volatile_unspecified_int() {
    ˙˙˙˙˙int x;
    ˙˙˙˙˙asm volatile("" : "=r" (x));

    Can we use std::atomic_signal_fence for it? Iirc, its compiler only.


    I don't think so, but I can't say for sure. The point of the volatile assembly here is to introduce some "observable behaviour" to the code,
    thus constraining the possible code flow combining the compiler can make
    using "as if" logic - but without adding any actual extra work.

    But try some fences if you like, and see how it affects the code generation.


    --- PyGate Linux v1.5.19
    * Origin: Dragon's Lair, PyGate NNTP<>Fido Gate (3:633/10)
  • From Bonita Montero@3:633/10 to All on Sat Nov 21 13:10:45 2026
    Am 21.11.2026 um 12:38 schrieb David Brown:

    That is very different code, and the compiler has different heuristics
    for generating object code.˙...

    It doesn't fit with your statement.

    That's the kind of hack "manual optimisation" code that most C and
    C++ programmers were able to leave behind at the turn of the century
    as compilers got better at optimising well-written and clear source
    code.

    If I did it more simple and readable from the beginning the code
    wouldn't be as efficient - as you suggested.


    --- PyGate Linux v1.5.19
    * Origin: Dragon's Lair, PyGate NNTP<>Fido Gate (3:633/10)
  • From David Brown@3:633/10 to All on Sat Nov 21 14:55:12 2026
    On 21/11/2026 13:10, Bonita Montero wrote:
    Am 21.11.2026 um 12:38 schrieb David Brown:

    That is very different code, and the compiler has different heuristics
    for generating object code.˙...

    It doesn't fit with your statement.

    That's the kind of hack "manual optimisation" code that most C and
    C++ programmers were able to leave behind at the turn of the century
    as compilers got better at optimising well-written and clear source
    code.

    If I did it more simple and readable from the beginning the code
    wouldn't be as efficient - as you suggested.


    I think the branchless code will often be more efficient in practice
    when the data is already in registers (but see Michael's comments about
    the code - and he is quite experienced in that kind of thing). When the
    data is in memory, not a register, I expect the branched code is better.
    gcc generates the best object code - if my predictions are correct -
    from the clearest source code, both in your struct method and for the stand-alone function example.

    In real usage, methods like this would be inlined, and the code
    generated would depend on the surrounding source code. In general,
    clearer source code gives the compiler better opportunities to optimise
    the inlined code well.

    The code you wrote is not "cool" or "smart" - it is a smart-arse hack
    that results in significantly poorer source code quality, and worse
    object code efficiency (though it probably makes no measurable
    difference in real-world usage). Before writing such ugly code, you
    should be benchmarking the results of clear source code in real usage,
    and determining if there is actually a bottleneck in your code
    performance caused by the function. Then after writing your hack, you
    should test again and determine if there is a significant enough gain
    for it to be worth keeping. I do not believe you have made the
    slightest attempt to do that.


    --- PyGate Linux v1.5.19
    * Origin: Dragon's Lair, PyGate NNTP<>Fido Gate (3:633/10)
  • From Bonita Montero@3:633/10 to All on Sat Nov 21 16:55:24 2026
    Am 21.11.2026 um 14:55 schrieb David Brown:

    I think the branchless code will often be more efficient in practice
    when the data is already in registers (but see Michael's comments about
    the code - and he is quite experienced in that kind of thing).˙ When the data is in memory, not a register, I expect the branched code is better.

    The missprediction penalty is the same.

    gcc generates the best object code - if my predictions are correct -
    from the clearest source code, both in your struct method and for the stand-alone function example.

    I istructed the compiler to do the right thing by placing an
    [[unlikely]] at the if; it didn't help.


    In real usage, methods like this would be inlined, and the code
    generated would depend on the surrounding source code.

    Yes, the surrounding code would be discarded or not depending on if
    the branch is predicted correctly or not; but the penalty itself is
    the same.


    The code you wrote is not "cool" or "smart" - it is a smart-arse
    hack that results in significantly poorer source code quality, ...

    The one using this code doesn't need to know that. For developers that
    come from C and have this lowlevel optimizations in mind it's easy to
    read.
    I wouldn't program like this all this time; but as this is a one liner
    it's easy to understand for the mentioned people.

    and worse object code efficiency (though it probably makes no measurable difference in real-world usage).

    It depends on the usage.

    Before writing such ugly code, ...

    That's a matter of personal taste.

    --- PyGate Linux v1.5.19
    * Origin: Dragon's Lair, PyGate NNTP<>Fido Gate (3:633/10)
  • From Michael S@3:633/10 to All on Sat Nov 21 18:40:41 2026
    On Sat, 21 Nov 2026 14:55:12 +0100
    David Brown <david.brown@hesbynett.no> wrote:

    On 21/11/2026 13:10, Bonita Montero wrote:
    Am 21.11.2026 um 12:38 schrieb David Brown:

    That is very different code, and the compiler has different
    heuristics for generating object code.?...

    It doesn't fit with your statement.

    That's the kind of hack "manual optimisation" code that most C
    and C++ programmers were able to leave behind at the turn of the
    century as compilers got better at optimising well-written and
    clear source code.

    If I did it more simple and readable from the beginning the code
    wouldn't be as efficient - as you suggested.


    I think the branchless code will often be more efficient in practice
    when the data is already in registers (but see Michael's comments
    about the code - and he is quite experienced in that kind of thing).

    When prediction is correct close to 100% of time, branchy code will
    be more efficient (lower latency) evn with values in registers.
    Of course, it would be very hard to prove it by measurements, because
    the difference will be small.

    When the data is in memory, not a register, I expect the branched
    code is better. gcc generates the best object code - if my
    predictions are correct - from the clearest source code, both in your
    struct method and for the stand-alone function example.


    IMHO, it's not a matter of optimization.
    Branchless code in thhis case is not allowed by semantics of the
    language. In order to give compiler a chance, you'd have to modify a
    source. For example, to something like that:

    strct::operator int() const noexcept
    {
    bool bb = b;
    if (!p )
    return -1;
    return (int)bb;
    }

    In C it's much more obvious.


    I played a little with this variant using gcc and clang.
    Once again, like in previous example, I was able to influence code
    generation of clang by means of __builtin_expect() or of __builtin_expect_with_probability().
    Once again, like in previous example, gcc stubbornly generated the
    same (this time branchy) code regardless of what I was trying to tell
    it.

    In real usage, methods like this would be inlined, and the code
    generated would depend on the surrounding source code. In general,
    clearer source code gives the compiler better opportunities to
    optimise the inlined code well.

    The code you wrote is not "cool" or "smart" - it is a smart-arse hack
    that results in significantly poorer source code quality, and worse
    object code efficiency (though it probably makes no measurable
    difference in real-world usage). Before writing such ugly code, you
    should be benchmarking the results of clear source code in real
    usage, and determining if there is actually a bottleneck in your code performance caused by the function. Then after writing your hack,
    you should test again and determine if there is a significant enough
    gain for it to be worth keeping. I do not believe you have made the slightest attempt to do that.




    --- PyGate Linux v1.5.19
    * Origin: Dragon's Lair, PyGate NNTP<>Fido Gate (3:633/10)
  • From Chris M. Thomasson@3:633/10 to All on Sat Nov 21 14:26:47 2026
    On 11/21/2026 3:43 AM, David Brown wrote:
    On 20/11/2026 23:45, Chris M. Thomasson wrote:
    On 11/20/2026 7:05 AM, David Brown wrote:
    On 20/11/2026 13:43, Michael S wrote:
    On Fri, 20 Nov 2026 12:56:25 +0100
    David Brown <david.brown@hesbynett.no> wrote:

    On 20/11/2026 12:22, Michael S wrote:


    As to what is good result, it's not that simple. When condition is >>>>>> predictable, i.e. when p is ==0 >95% of time or <5% of time then
    code with branch will be faster than code without branch. Esp. so
    when p is originated outside of L1D cache.

    Indeed.˙ The speed of code on "big" processors is always hard to
    predict, and can depend on many factors.˙ (That's one reason I like
    microcontrollers - they are a lot more predictable.)˙ Real-world
    speed depends on the values you give this kind of function, and all
    the surrounding code - a function like this is likely to be inlined. >>>>>

    It is possible and relatively easy to convince clang to generate
    branchy code that is similar to MSVC /Ox and likely the fastest in
    practice:

    int baz(_Bool shared, void* p)
    {
    ˙˙ return __builtin_expect( (_Bool)p, 1) ? shared : -1;
    }
    https://godbolt.org/z/GdTxoqf4a

    It is much harder to convince gcc to do the same or similar.
    I see such stubbornness as a serious disadvantage of gcc optimizer.

    A technique for forcing particular code generation is to use volatile
    do-nothing inline assembly.

    int baz2(bool shared, void* p)
    {
    ˙˙˙˙˙if (p) {
    ˙˙˙˙˙˙˙˙ asm volatile("");
    ˙˙˙˙˙˙˙˙ return shared;
    ˙˙˙˙˙}
    ˙˙˙˙˙return -1;
    }

    gcc:

    "baz2(bool, void*)":
    ˙˙˙˙˙˙˙˙ mov˙˙˙˙ eax, -1
    ˙˙˙˙˙˙˙˙ test˙˙˙ rsi, rsi
    ˙˙˙˙˙˙˙˙ je˙˙˙˙˙ .L1
    ˙˙˙˙˙˙˙˙ movzx˙˙ eax, dil
    .L1:
    ˙˙˙˙˙˙˙˙ ret


    clang:

    baz2(bool, void*):
    ˙˙˙˙˙˙˙˙ test˙˙˙ rsi, rsi
    ˙˙˙˙˙˙˙˙ je˙˙˙˙˙ .LBB0_1
    ˙˙˙˙˙˙˙˙ movzx˙˙ eax, dil
    ˙˙˙˙˙˙˙˙ ret
    .LBB0_1:
    ˙˙˙˙˙˙˙˙ mov˙˙˙˙ eax, -1
    ˙˙˙˙˙˙˙˙ ret


    The inline assembly is not standard C++, but neither is
    __builtin_expect.˙ And it is highly portable assembly :-)


    If you prefer to have a single expression, you can have :

    inline int volatile_unspecified_int() {
    ˙˙˙˙˙int x;
    ˙˙˙˙˙asm volatile("" : "=r" (x));

    Can we use std::atomic_signal_fence for it? Iirc, its compiler only.


    I don't think so, but I can't say for sure.˙ The point of the volatile assembly here is to introduce some "observable behaviour" to the code,
    thus constraining the possible code flow combining the compiler can make using "as if" logic - but without adding any actual extra work.

    But try some fences if you like, and see how it affects the code
    generation.


    I would need to check it out on goldbolt. std::atomic_signal_fence is
    just for the compiler and not for the arch.

    --- PyGate Linux v1.5.19
    * Origin: Dragon's Lair, PyGate NNTP<>Fido Gate (3:633/10)