C3 avoids a jump instruction by always performing an addition. For a super-tight loop like this, those jumps can make a big difference. I'm not sure that I would expect a C compiler to realize that C4 could be rewritten as C3.

I had a longer answer, but ran it past Claude.AI and it knew a lot more about the topic than I did and my answer would have been wrong. Of course this could be inaccurate/wrong too, but it looks more accurate than what I was going to say. At the risk of offending people, here's is a portion of its answer:

On x86-64, there isn't a direct conditional arithmetic instruction like you describe, but there are several approaches that can eliminate branches:
  1. CMOVcc (Conditional Move) - The most common approach
    ; Assuming condition result is in a flag mov eax, i lea edx, [rax + 1] ; edx = i + 1 cmovnz eax, edx ; if condition != 0, eax = edx mov i, eax
    or more directly:
    lea edx, [i + 1] test condition, condition cmovnz i, edx ; conditionally move the incremented value
  2. SETcc (Set Byte on Condition) - Convert condition to 0/1
    xor eax, eax test condition, condition setnz al ; al = (condition != 0) ? 1 : 0 add i, eax ; i += 0 or 1
    This is often the most efficient for your exact use case - it turns the condition into a 0 or 1, then unconditionally adds it.
  3. ADC/SBB (Add/Subtract with Carry) - If condition is in carry flag
    ; If your condition sets the carry flag: adc i, 0 ; i += carry flag

The SETcc + ADD pattern is typically what compilers generate for branchless if (cond) i++, and it's usually faster than a branch for unpredictable conditions.


In reply to Re: Weird performance issue with Strawberries and Inline::C by NERDVANA
in thread Weird performance issue with Strawberries and Inline::C by Anonymous Monk

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.