• Re: will it be optimized?

    From Andrey Tarasevich@3:633/10 to All on Saturday, November 21, 2026 07:54:04
    On Wed 10/7/2026 12:01 AM, Andrey Tarasevich wrote:
    I was quite surprised to discover that GCC treats returning struct types
    by value overly literally, i.e. fails to optimize it at all. For reasons that defy any rationalization, GCC 16 translates this
    Another example I recently discovered, which also looks quite perplexing
    is this

    typedef struct P
    {
    unsigned long long r;
    unsigned char e;
    } P;

    P foo(void)
    {
    unsigned long long r = rand();
    return (P) { r, r % 2 };
    }

    which in GCC 16 (-O3) generates the following

    "foo":
    sub rsp, 24
    call "rand"
    mov DWORD PTR [rsp], 0
    movsxd rsi, eax
    mov DWORD PTR [rsp+3], 0
    movabs rax, 72057594037927935
    and rax, QWORD PTR [rsp]
    sal rax, 8
    add rsp, 24
    mov rdx, rax
    mov rax, rsi
    mov dl, sil
    and dl, 1
    ret

    https://godbolt.org/z/s6r5n6bj9

    The struct type is small, so the result is returned in RAX:RDX register
    pair, which is great. However, as you can see, the code still fiddles
    with local memory for some reason... All these weird manipulations (two
    movs, movabs, and, sal) are there simply to lay out a sequence of 7 (!?)
    zero bytes in memory (!?), then load them into the register as a way to zero-initialize the padding bytes (!) in that register. They spend all
    this circuitous effort just to zero-initialize padding, Karl!

    Why?!

    It appears that the use of a compound literal is what triggers the
    strange code generation, since in the following versions of the function

    P bar(void)
    {
    P p = { 0 };
    p.r = rand();
    p.e = p.r % 2;
    return p;
    }

    P baz(void)
    {
    unsigned long long r = rand();
    P p = { r, r % 2 };
    return p;
    }

    the resultant x86 code looks completely unremarkable

    "bar":
    sub rsp, 8
    call "rand"
    add rsp, 8
    mov ecx, eax
    cdqe
    movzx edx, cl
    and dl, 1
    ret

    The C++ version of the original function (i.e. simply using {}-list
    under `return` instead of a compound literal) suffers from the same
    problem in C++ code

    P foo(void)
    {
    unsigned long long r = rand();
    return { r, r % 2 };
    }

    https://godbolt.org/z/xfzKbqnc4

    --
    Best regards,
    Andrey

    --- 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 Saturday, November 21, 2026 17:23:14
    Am 07.10.2026 um 08:42 schrieb fir:

    youre like 2 peasants watching airplane saying its devil.. you should develop a bit more brain imo...i dont˙ want to comment on someone mental lacks˙ though coz this is group about programing not personal mantal
    lacks ..we already got rick hodgin here back then

    The question you?re trying to answer could be resolved in 2 minutes
    using godbolt.org ? or 10 minutes, if `O` happens to be a truly
    complex type. Instead, you?re just being annoying here with such
    trivial questions.

    --- PyGate Linux v1.5.19
    * Origin: Dragon's Lair, PyGate NNTP<>Fido Gate (3:633/10)
  • From fir@3:633/10 to All on Sunday, November 22, 2026 13:14:37
    Andrey Tarasevich pisze:
    On Wed 10/7/2026 12:01 AM, Andrey Tarasevich wrote:
    I was quite surprised to discover that GCC treats returning struct
    types by value overly literally, i.e. fails to optimize it at all. For
    reasons that defy any rationalization, GCC 16 translates this
    Another example I recently discovered, which also looks quite perplexing
    is this

    ˙ typedef struct P
    ˙ {
    ˙˙˙ unsigned long long r;
    ˙˙˙ unsigned char e;
    ˙ } P;

    ˙ P foo(void)
    ˙ {
    ˙˙˙ unsigned long long r = rand();
    ˙˙˙ return (P) { r, r % 2 };
    ˙ }

    which in GCC 16 (-O3) generates the following

    ˙ "foo":
    ˙˙˙˙˙˙˙ sub˙˙˙˙ rsp, 24
    ˙˙˙˙˙˙˙ call˙˙˙ "rand"
    ˙˙˙˙˙˙˙ mov˙˙˙˙ DWORD PTR [rsp], 0
    ˙˙˙˙˙˙˙ movsxd˙ rsi, eax
    ˙˙˙˙˙˙˙ mov˙˙˙˙ DWORD PTR [rsp+3], 0
    ˙˙˙˙˙˙˙ movabs˙ rax, 72057594037927935
    ˙˙˙˙˙˙˙ and˙˙˙˙ rax, QWORD PTR [rsp]
    ˙˙˙˙˙˙˙ sal˙˙˙˙ rax, 8
    ˙˙˙˙˙˙˙ add˙˙˙˙ rsp, 24
    ˙˙˙˙˙˙˙ mov˙˙˙˙ rdx, rax
    ˙˙˙˙˙˙˙ mov˙˙˙˙ rax, rsi
    ˙˙˙˙˙˙˙ mov˙˙˙˙ dl, sil
    ˙˙˙˙˙˙˙ and˙˙˙˙ dl, 1
    ˙˙˙˙˙˙˙ ret

    https://godbolt.org/z/s6r5n6bj9

    The struct type is small, so the result is returned in RAX:RDX register pair, which is great. However, as you can see, the code still fiddles
    with local memory for some reason... All these weird manipulations (two movs, movabs, and, sal) are there simply to lay out a sequence of 7 (!?) zero bytes in memory (!?), then load them into the register as a way to zero-initialize the padding bytes (!) in that register. They spend all
    this circuitous effort just to zero-initialize padding, Karl!

    Why?!

    It appears that the use of a compound literal is what triggers the
    strange code generation, since in the following versions of the function

    ˙ P bar(void)
    ˙ {
    ˙˙˙ P p = { 0 };
    ˙˙˙ p.r = rand();
    ˙˙˙ p.e = p.r % 2;
    ˙˙˙ return p;
    ˙ }

    ˙ P baz(void)
    ˙ {
    ˙˙˙ unsigned long long r = rand();
    ˙˙˙ P p = { r, r % 2 };
    ˙˙˙ return p;
    ˙ }

    the resultant x86 code looks completely unremarkable

    ˙ "bar":
    ˙˙˙˙˙˙˙ sub˙˙˙˙ rsp, 8
    ˙˙˙˙˙˙˙ call˙˙˙ "rand"
    ˙˙˙˙˙˙˙ add˙˙˙˙ rsp, 8
    ˙˙˙˙˙˙˙ mov˙˙˙˙ ecx, eax
    ˙˙˙˙˙˙˙ cdqe
    ˙˙˙˙˙˙˙ movzx˙˙ edx, cl
    ˙˙˙˙˙˙˙ and˙˙˙˙ dl, 1
    ˙˙˙˙˙˙˙ ret

    The C++ version of the original function (i.e. simply using {}-list
    under `return` instead of a compound literal) suffers from the same
    problem in C++ code

    ˙ P foo(void)
    ˙ {
    ˙˙˙ unsigned long long r = rand();
    ˙˙˙ return { r, r % 2 };
    ˙ }

    https://godbolt.org/z/xfzKbqnc4

    im to weary to do such checks..more over i compile sutill in 32 bit mode
    (no need to more than 2GB of ram, test show i use more like 25 MB)

    over ally i relayed on using a lot of inlines to defer worries but it
    showed the exe size may grow quite a lot (removing one inline of main rasterizig routine decrased exe size from 900kB to 400kB..and if that
    exe was 900 kB windows defender was finding false malvare alerts whats
    worse)



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