🚀 go-pugleaf

RetroBBS NetNews Server

Inspired by RockSolid Light RIP Retro Guy

19 total messages Started by Andrey Tarasevich Wed, 07 Oct 2026 07:01
Re: will it be optimized?
#14
Author: Andrey Tarasevich
Date: Wed, 07 Oct 2026 07:01
83 lines
3374 bytes
On Mon 10/5/2026 5:24 PM, fir wrote:
> but the question is if i would use
> 
> f(O*a)
> {
>    O o = *a;
>    //... o.something here
> }
> 
> 
> will it be optimized or not?
It depends on the implementation. Optimizations are based on pattern 
matching. If someone in the compiler development team though of this 
specific pattern, they might've decided to implement it. If not, then it 
is not there. They could even resort to literal translation of your 
assignment into physical memory copying even if there's no real reason 
for doing so.

On a related node:

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

   struct S { char a[1024]; };

   struct S foo(void)
   {
     struct S s = { 0 };
     s.a[10] = 42;
     return s;
   }

into this (in -O3 mode!)

   .LC0:
         .zero   2
         .zero   1022
   "foo":
         push    rbx
         mov     edx, 1024
         mov     rbx, rdi
         mov     esi, OFFSET FLAT:.LC0
         sub     rsp, 1024
         mov     rdi, rsp
         call    "memcpy"
         mov     rsi, rsp
         mov     rdi, rbx
         mov     edx, 1024
         mov     BYTE PTR [rsp+10], 42
         call    "memcpy"
         add     rsp, 1024
         pop     rbx
         ret

https://godbolt.org/z/G89vfsvPa

Initialization is performed literally by copying from a block of zeros 
prepared elsewhere (!). Returning is performed by another copying from a 
local variable into a return area. Weird! Why?

Clang 21 does this

   foo:
         push    rbx
         mov     rbx, rdi
         mov     edx, 1024
         xor     esi, esi
         call    memset@PLT
         mov     byte ptr [rbx + 10], 42
         mov     rax, rbx
         pop     rbx
         ret

https://godbolt.org/z/bx5oM5s1M

So, it performs a typical C++-style RVO (no `return` copying) and is 
smart enough to initialize the struct without copying zeros from 
somewhere else.

-- 
Best regards,
Andrey
Re: will it be optimized?
#16
Author: fir
Date: Wed, 07 Oct 2026 08:09
194 lines
6245 bytes
Andrey Tarasevich pisze:
> On Mon 10/5/2026 5:24 PM, fir wrote:
>> but the question is if i would use
>>
>> f(O*a)
>> {
>>    O o = *a;
>>    //... o.something here
>> }
>>
>>
>> will it be optimized or not?
> It depends on the implementation. Optimizations are based on pattern 
> matching. If someone in the compiler development team though of this 
> specific pattern, they might've decided to implement it. If not, then it 
> is not there. They could even resort to literal translation of your 
> assignment into physical memory copying even if there's no real reason 
> for doing so.
> 
> On a related node:
> 
> 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
> 
>    struct S { char a[1024]; };
> 
>    struct S foo(void)
>    {
>      struct S s = { 0 };
>      s.a[10] = 42;
>      return s;
>    }
> 
> into this (in -O3 mode!)
> 
>    .LC0:
>          .zero   2
>          .zero   1022
>    "foo":
>          push    rbx
>          mov     edx, 1024
>          mov     rbx, rdi
>          mov     esi, OFFSET FLAT:.LC0
>          sub     rsp, 1024
>          mov     rdi, rsp
>          call    "memcpy"
>          mov     rsi, rsp
>          mov     rdi, rbx
>          mov     edx, 1024
>          mov     BYTE PTR [rsp+10], 42
>          call    "memcpy"
>          add     rsp, 1024
>          pop     rbx
>          ret
> 
> https://godbolt.org/z/G89vfsvPa
> 
> Initialization is performed literally by copying from a block of zeros 
> prepared elsewhere (!). Returning is performed by another copying from a 
> local variable into a return area. Weird! Why?
> 
> Clang 21 does this
> 
>    foo:
>          push    rbx
>          mov     rbx, rdi
>          mov     edx, 1024
>          xor     esi, esi
>          call    memset@PLT
>          mov     byte ptr [rbx + 10], 42
>          mov     rax, rbx
>          pop     rbx
>          ret
> 
> https://godbolt.org/z/bx5oM5s1M
> 
> So, it performs a typical C++-style RVO (no `return` copying) and is 
> smart enough to initialize the struct without copying zeros from 
> somewhere else.
> 


what if you would also add a piece of code which uses it ?

becuse imo it should be added becouse without the code of
calller its not full example imo


it should be like

main()
{
   Struct S  s = foo();
}

in my understanding gcc optimises quite deeply BUT i never know if some 
rigid official language rules or some specific cases hard to follow
without more thinking  do not stop some obvious optimisation..

what you say its kkinda sad but imo its needed to add this caller code


also you could test this my more specific example

typedef struct {

   float field1;
   float field2;
   float field3;
   float field4;
   float field5;`
   float field6;
   float field7;
   float field8;
   float field9;
   float field10;
   float field11;
   float field12;

} BigStructure;



    boo(BigStructure* o)
    {
	(*o).field2+=9.0;
    }

     foo(BigStructure* o)
     {
       BigSTructure a = *o;

       a.field7+=4;
	  boo(&a);
    	
	*o=a;
     }

   main()
{
    BigStructure o;
     foo(&o);

}

relying on ocmpiler its overally sad situation (becouse it can fail)

overally it seems just to be prbably c fault here, though i would need 
to focus a bit to say why


BS foo(BS o)
{
   return BS;
}

it just seems

C is particularly bad suited to working with large structures passed as 
function arguments.

(note if my sentence has dot and stars with big letter its made by ai 
(like by translation form polish later edited)

some could say foo(BS*o) is okay but the syntax inside if o is big and
you acces many fields is horrible

HOW TO MEND IT?


c++ reference is sorta way or yet better something that iw as saying
already few years ago

there should be no passing by value for big structures

BS foo(BS o)
{
   return BS;
}

just kinda hiden(/not explicit like in c++) reference
probably only for read when pasing and only for  write when
returning

it could eventually by rethinked if there are no some complications with 
it as today im not quite able to deeper focus as it seems

(for example in syntax above as one reference is for read and one for 
write syntax suggest it may point to two objects and there is question 
if it has some sense and shoud be allowed or if not and if so it 
shouldnt be this way)

Re: will it be optimized?
#19
Author: David Brown
Date: Wed, 07 Oct 2026 08:46
76 lines
3859 bytes
On 07/10/2026 09:01, Andrey Tarasevich wrote:
> 
> On a related node:
> 
> 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
> 
>    struct S { char a[1024]; };
> 
>    struct S foo(void)
>    {
>      struct S s = { 0 };
>      s.a[10] = 42;
>      return s;
>    }
> 
> into this (in -O3 mode!)
> 
>    .LC0:
>          .zero   2
>          .zero   1022
>    "foo":
>          push    rbx
>          mov     edx, 1024
>          mov     rbx, rdi
>          mov     esi, OFFSET FLAT:.LC0
>          sub     rsp, 1024
>          mov     rdi, rsp
>          call    "memcpy"
>          mov     rsi, rsp
>          mov     rdi, rbx
>          mov     edx, 1024
>          mov     BYTE PTR [rsp+10], 42
>          call    "memcpy"
>          add     rsp, 1024
>          pop     rbx
>          ret
> 
> https://godbolt.org/z/G89vfsvPa
> 
> Initialization is performed literally by copying from a block of zeros 
> prepared elsewhere (!). Returning is performed by another copying from a 
> local variable into a return area. Weird! Why?
> 
> Clang 21 does this
> 
>    foo:
>          push    rbx
>          mov     rbx, rdi
>          mov     edx, 1024
>          xor     esi, esi
>          call    memset@PLT
>          mov     byte ptr [rbx + 10], 42
>          mov     rax, rbx
>          pop     rbx
>          ret
> 
> https://godbolt.org/z/bx5oM5s1M
> 
> So, it performs a typical C++-style RVO (no `return` copying) and is 
> smart enough to initialize the struct without copying zeros from 
> somewhere else.
> 

That is interesting.  I can think of no reason to suggest that clang's 
code might be incorrect (I've seen a number of occasions where clang has 
generated more efficient code than gcc, but has been lax in adherence to 
C standards or platform ABIs to do so).

When compiling as C++, gcc uses RVO - there is only a single memcpy() 
from a zero block.  When the initialiser for s is changed to the more 
modern "S s = {};" it uses memset rather than copying from a zero block, 
in both C and C++.  But the C version still has the additional memcpy.

Re: will it be optimized?
#26
Author: fir
Date: Wed, 07 Oct 2026 12:45
92 lines
4747 bytes
David Brown pisze:
> On 07/10/2026 09:01, Andrey Tarasevich wrote:
>>
>> On a related node:
>>
>> 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
>>
>>    struct S { char a[1024]; };
>>
>>    struct S foo(void)
>>    {
>>      struct S s = { 0 };
>>      s.a[10] = 42;
>>      return s;
>>    }
>>
>> into this (in -O3 mode!)
>>
>>    .LC0:
>>          .zero   2
>>          .zero   1022
>>    "foo":
>>          push    rbx
>>          mov     edx, 1024
>>          mov     rbx, rdi
>>          mov     esi, OFFSET FLAT:.LC0
>>          sub     rsp, 1024
>>          mov     rdi, rsp
>>          call    "memcpy"
>>          mov     rsi, rsp
>>          mov     rdi, rbx
>>          mov     edx, 1024
>>          mov     BYTE PTR [rsp+10], 42
>>          call    "memcpy"
>>          add     rsp, 1024
>>          pop     rbx
>>          ret
>>
>> https://godbolt.org/z/G89vfsvPa
>>
>> Initialization is performed literally by copying from a block of zeros 
>> prepared elsewhere (!). Returning is performed by another copying from 
>> a local variable into a return area. Weird! Why?
>>
>> Clang 21 does this
>>
>>    foo:
>>          push    rbx
>>          mov     rbx, rdi
>>          mov     edx, 1024
>>          xor     esi, esi
>>          call    memset@PLT
>>          mov     byte ptr [rbx + 10], 42
>>          mov     rax, rbx
>>          pop     rbx
>>          ret
>>
>> https://godbolt.org/z/bx5oM5s1M
>>
>> So, it performs a typical C++-style RVO (no `return` copying) and is 
>> smart enough to initialize the struct without copying zeros from 
>> somewhere else.
>>
> 
> That is interesting.  I can think of no reason to suggest that clang's 
> code might be incorrect (I've seen a number of occasions where clang has 
> generated more efficient code than gcc, but has been lax in adherence to 
> C standards or platform ABIs to do so).
> 
> When compiling as C++, gcc uses RVO - there is only a single memcpy() 
> from a zero block.  When the initialiser for s is changed to the more 
> modern "S s = {};" it uses memset rather than copying from a zero block, 
> in both C and C++.  But the C version still has the additional memcpy.
> 
> 
now all this my rasterizer (ehich i port from older code and make 
cleaning) its not big 12k lines (oryginal is bigger but i dont bother to 
check now becouse of being tired and sleepy (after being in forest for 
the mushroms)

all that i compile in pure c now..just to remember how its different 
from c in c++ mode..with this enum for tab_max in fact i can live
with this typedef struct also..co c is still practically ok
for me..though this references are tempting..also operator overloading 
for vectors are indeed godo for such 3D code i write

really c should have that
1) references like in c++
2) adhoc damn enums /tags
3) builtin operators for float3 type
Re: will it be optimized?
#28
Author: fir
Date: Wed, 07 Oct 2026 13:08
106 lines
5452 bytes
fir pisze:
> David Brown pisze:
>> On 07/10/2026 09:01, Andrey Tarasevich wrote:
>>>
>>> On a related node:
>>>
>>> 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
>>>
>>>    struct S { char a[1024]; };
>>>
>>>    struct S foo(void)
>>>    {
>>>      struct S s = { 0 };
>>>      s.a[10] = 42;
>>>      return s;
>>>    }
>>>
>>> into this (in -O3 mode!)
>>>
>>>    .LC0:
>>>          .zero   2
>>>          .zero   1022
>>>    "foo":
>>>          push    rbx
>>>          mov     edx, 1024
>>>          mov     rbx, rdi
>>>          mov     esi, OFFSET FLAT:.LC0
>>>          sub     rsp, 1024
>>>          mov     rdi, rsp
>>>          call    "memcpy"
>>>          mov     rsi, rsp
>>>          mov     rdi, rbx
>>>          mov     edx, 1024
>>>          mov     BYTE PTR [rsp+10], 42
>>>          call    "memcpy"
>>>          add     rsp, 1024
>>>          pop     rbx
>>>          ret
>>>
>>> https://godbolt.org/z/G89vfsvPa
>>>
>>> Initialization is performed literally by copying from a block of 
>>> zeros prepared elsewhere (!). Returning is performed by another 
>>> copying from a local variable into a return area. Weird! Why?
>>>
>>> Clang 21 does this
>>>
>>>    foo:
>>>          push    rbx
>>>          mov     rbx, rdi
>>>          mov     edx, 1024
>>>          xor     esi, esi
>>>          call    memset@PLT
>>>          mov     byte ptr [rbx + 10], 42
>>>          mov     rax, rbx
>>>          pop     rbx
>>>          ret
>>>
>>> https://godbolt.org/z/bx5oM5s1M
>>>
>>> So, it performs a typical C++-style RVO (no `return` copying) and is 
>>> smart enough to initialize the struct without copying zeros from 
>>> somewhere else.
>>>
>>
>> That is interesting.  I can think of no reason to suggest that clang's 
>> code might be incorrect (I've seen a number of occasions where clang 
>> has generated more efficient code than gcc, but has been lax in 
>> adherence to C standards or platform ABIs to do so).
>>
>> When compiling as C++, gcc uses RVO - there is only a single memcpy() 
>> from a zero block.  When the initialiser for s is changed to the more 
>> modern "S s = {};" it uses memset rather than copying from a zero 
>> block, in both C and C++.  But the C version still has the additional 
>> memcpy.
>>
>>
> now all this my rasterizer (ehich i port from older code and make 
> cleaning) its not big 12k lines (oryginal is bigger but i dont bother to 
> check now becouse of being tired and sleepy (after being in forest for 
> the mushroms)
> 
> all that i compile in pure c now..just to remember how its different 
> from c in c++ mode..with this enum for tab_max in fact i can live
> with this typedef struct also..co c is still practically ok
> for me..though this references are tempting..also operator overloading 
> for vectors are indeed godo for such 3D code i write
> 
> really c should have that
> 1) references like in c++
> 2) adhoc damn enums /tags
> 3) builtin operators for float3 type


btw its quite notable how hard is to shoot 7-8 m long ship from 500 
meters in open space..they are movin aroung 180 km/h so not much fast
but even one which is not moving hard to hit..sor rasterizers as it show 
may make physical discovery..my bullets though fly slow (150 m/s which 
is3 times slower than real ones)


as t coordinates there was also a question how 1 float unit should map
on mters and as some models i get showed 140 units could be like 7-8 meters
i can take 1 unit of float is 5 cm, so 20 is 1 meter 200 is 10 meters
Re: will it be optimized?
#31
Author: bart
Date: Wed, 07 Oct 2026 14:31
16 lines
1734 bytes
On 07/10/2026 13:45, fir wrote:

> really c should have that
> 1) references like in c++
> 2) adhoc damn enums /tags
> 3) builtin operators for float3 type

That last is a very specific type for C.

Why not go instead for a more general ability to be able to overload 
operators for user-defined types?

You know, like C++.

Or maybe you can just use C++ because it's right there, and you use a 
C++ compiler anyway.
Re: will it be optimized?
#35
Author: fir
Date: Wed, 07 Oct 2026 18:41
26 lines
2169 bytes
bart pisze:
> On 07/10/2026 13:45, fir wrote:
> 
>> really c should have that
>> 1) references like in c++
>> 2) adhoc damn enums /tags
>> 3) builtin operators for float3 type
> 
> That last is a very specific type for C.
> 
> Why not go instead for a more general ability to be able to overload 
> operators for user-defined types?
> 
> You know, like C++.
> 
> Or maybe you can just use C++ because it's right there, and you use a 
> C++ compiler anyway.


ye operator 'overloading' and references are good concepts.. here above 
i  jut mentioned more modest changes that are needed (more specifically 
i mentioned those who anoy me practically when i code this rasterizer, 
and the lack of operators for float3 is heavy pain)


Re: will it be optimized?
#43
Author: Chris M. Thomasson
Date: Wed, 07 Oct 2026 20:08
116 lines
5800 bytes
On 10/7/2026 6:08 AM, fir wrote:
> fir pisze:
>> David Brown pisze:
>>> On 07/10/2026 09:01, Andrey Tarasevich wrote:
>>>>
>>>> On a related node:
>>>>
>>>> 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
>>>>
>>>>    struct S { char a[1024]; };
>>>>
>>>>    struct S foo(void)
>>>>    {
>>>>      struct S s = { 0 };
>>>>      s.a[10] = 42;
>>>>      return s;
>>>>    }
>>>>
>>>> into this (in -O3 mode!)
>>>>
>>>>    .LC0:
>>>>          .zero   2
>>>>          .zero   1022
>>>>    "foo":
>>>>          push    rbx
>>>>          mov     edx, 1024
>>>>          mov     rbx, rdi
>>>>          mov     esi, OFFSET FLAT:.LC0
>>>>          sub     rsp, 1024
>>>>          mov     rdi, rsp
>>>>          call    "memcpy"
>>>>          mov     rsi, rsp
>>>>          mov     rdi, rbx
>>>>          mov     edx, 1024
>>>>          mov     BYTE PTR [rsp+10], 42
>>>>          call    "memcpy"
>>>>          add     rsp, 1024
>>>>          pop     rbx
>>>>          ret
>>>>
>>>> https://godbolt.org/z/G89vfsvPa
>>>>
>>>> Initialization is performed literally by copying from a block of 
>>>> zeros prepared elsewhere (!). Returning is performed by another 
>>>> copying from a local variable into a return area. Weird! Why?
>>>>
>>>> Clang 21 does this
>>>>
>>>>    foo:
>>>>          push    rbx
>>>>          mov     rbx, rdi
>>>>          mov     edx, 1024
>>>>          xor     esi, esi
>>>>          call    memset@PLT
>>>>          mov     byte ptr [rbx + 10], 42
>>>>          mov     rax, rbx
>>>>          pop     rbx
>>>>          ret
>>>>
>>>> https://godbolt.org/z/bx5oM5s1M
>>>>
>>>> So, it performs a typical C++-style RVO (no `return` copying) and is 
>>>> smart enough to initialize the struct without copying zeros from 
>>>> somewhere else.
>>>>
>>>
>>> That is interesting.  I can think of no reason to suggest that 
>>> clang's code might be incorrect (I've seen a number of occasions 
>>> where clang has generated more efficient code than gcc, but has been 
>>> lax in adherence to C standards or platform ABIs to do so).
>>>
>>> When compiling as C++, gcc uses RVO - there is only a single memcpy() 
>>> from a zero block.  When the initialiser for s is changed to the more 
>>> modern "S s = {};" it uses memset rather than copying from a zero 
>>> block, in both C and C++.  But the C version still has the additional 
>>> memcpy.
>>>
>>>
>> now all this my rasterizer (ehich i port from older code and make 
>> cleaning) its not big 12k lines (oryginal is bigger but i dont bother 
>> to check now becouse of being tired and sleepy (after being in forest 
>> for the mushroms)
>>
>> all that i compile in pure c now..just to remember how its different 
>> from c in c++ mode..with this enum for tab_max in fact i can live
>> with this typedef struct also..co c is still practically ok
>> for me..though this references are tempting..also operator overloading 
>> for vectors are indeed godo for such 3D code i write
>>
>> really c should have that
>> 1) references like in c++
>> 2) adhoc damn enums /tags
>> 3) builtin operators for float3 type
> 
> 
> btw its quite notable how hard is to shoot 7-8 m long ship from 500 
> meters in open space..they are movin aroung 180 km/h so not much fast
> but even one which is not moving hard to hit..sor rasterizers as it show 
> may make physical discovery..my bullets though fly slow (150 m/s which 
> is3 times slower than real ones)
> 
> 
> as t coordinates there was also a question how 1 float unit should map
> on mters and as some models i get showed 140 units could be like 7-8 meters
> i can take 1 unit of float is 5 cm, so 20 is 1 meter 200 is 10 meters

Fwiw, check this crap out. The yellow lines are the path of the 
"interceptors":

https://youtu.be/_8IN9eZv7yg

This is another rather crude test of mine:

https://youtu.be/W7FnwnyP0wQ
Re: will it be optimized?
#46
Author: fir
Date: Wed, 07 Oct 2026 20:29
125 lines
6288 bytes
Chris M. Thomasson pisze:
> On 10/7/2026 6:08 AM, fir wrote:
>> fir pisze:
>>> David Brown pisze:
>>>> On 07/10/2026 09:01, Andrey Tarasevich wrote:
>>>>>
>>>>> On a related node:
>>>>>
>>>>> 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
>>>>>
>>>>>    struct S { char a[1024]; };
>>>>>
>>>>>    struct S foo(void)
>>>>>    {
>>>>>      struct S s = { 0 };
>>>>>      s.a[10] = 42;
>>>>>      return s;
>>>>>    }
>>>>>
>>>>> into this (in -O3 mode!)
>>>>>
>>>>>    .LC0:
>>>>>          .zero   2
>>>>>          .zero   1022
>>>>>    "foo":
>>>>>          push    rbx
>>>>>          mov     edx, 1024
>>>>>          mov     rbx, rdi
>>>>>          mov     esi, OFFSET FLAT:.LC0
>>>>>          sub     rsp, 1024
>>>>>          mov     rdi, rsp
>>>>>          call    "memcpy"
>>>>>          mov     rsi, rsp
>>>>>          mov     rdi, rbx
>>>>>          mov     edx, 1024
>>>>>          mov     BYTE PTR [rsp+10], 42
>>>>>          call    "memcpy"
>>>>>          add     rsp, 1024
>>>>>          pop     rbx
>>>>>          ret
>>>>>
>>>>> https://godbolt.org/z/G89vfsvPa
>>>>>
>>>>> Initialization is performed literally by copying from a block of 
>>>>> zeros prepared elsewhere (!). Returning is performed by another 
>>>>> copying from a local variable into a return area. Weird! Why?
>>>>>
>>>>> Clang 21 does this
>>>>>
>>>>>    foo:
>>>>>          push    rbx
>>>>>          mov     rbx, rdi
>>>>>          mov     edx, 1024
>>>>>          xor     esi, esi
>>>>>          call    memset@PLT
>>>>>          mov     byte ptr [rbx + 10], 42
>>>>>          mov     rax, rbx
>>>>>          pop     rbx
>>>>>          ret
>>>>>
>>>>> https://godbolt.org/z/bx5oM5s1M
>>>>>
>>>>> So, it performs a typical C++-style RVO (no `return` copying) and 
>>>>> is smart enough to initialize the struct without copying zeros from 
>>>>> somewhere else.
>>>>>
>>>>
>>>> That is interesting.  I can think of no reason to suggest that 
>>>> clang's code might be incorrect (I've seen a number of occasions 
>>>> where clang has generated more efficient code than gcc, but has been 
>>>> lax in adherence to C standards or platform ABIs to do so).
>>>>
>>>> When compiling as C++, gcc uses RVO - there is only a single 
>>>> memcpy() from a zero block.  When the initialiser for s is changed 
>>>> to the more modern "S s = {};" it uses memset rather than copying 
>>>> from a zero block, in both C and C++.  But the C version still has 
>>>> the additional memcpy.
>>>>
>>>>
>>> now all this my rasterizer (ehich i port from older code and make 
>>> cleaning) its not big 12k lines (oryginal is bigger but i dont bother 
>>> to check now becouse of being tired and sleepy (after being in forest 
>>> for the mushroms)
>>>
>>> all that i compile in pure c now..just to remember how its different 
>>> from c in c++ mode..with this enum for tab_max in fact i can live
>>> with this typedef struct also..co c is still practically ok
>>> for me..though this references are tempting..also operator 
>>> overloading for vectors are indeed godo for such 3D code i write
>>>
>>> really c should have that
>>> 1) references like in c++
>>> 2) adhoc damn enums /tags
>>> 3) builtin operators for float3 type
>>
>>
>> btw its quite notable how hard is to shoot 7-8 m long ship from 500 
>> meters in open space..they are movin aroung 180 km/h so not much fast
>> but even one which is not moving hard to hit..sor rasterizers as it 
>> show may make physical discovery..my bullets though fly slow (150 m/s 
>> which is3 times slower than real ones)
>>
>>
>> as t coordinates there was also a question how 1 float unit should map
>> on mters and as some models i get showed 140 units could be like 7-8 
>> meters
>> i can take 1 unit of float is 5 cm, so 20 is 1 meter 200 is 10 meters
> 
> Fwiw, check this crap out. The yellow lines are the path of the 
> "interceptors":
> 
> https://youtu.be/_8IN9eZv7yg
> 
> This is another rather crude test of mine:
> 
> https://youtu.be/W7FnwnyP0wQ


its fine.. but i cant comment on that i was not doing such things..

now im more interested like in general setup fr some arcade space 
shooter in 3d or something (i dont want to  finish it just to code few 
things and gather some observations and conclusions)
Re: will it be optimized?
#48
Author: Chris M. Thomasson
Date: Wed, 07 Oct 2026 20:56
32 lines
2386 bytes
On 10/7/2026 11:41 AM, fir wrote:
> bart pisze:
>> On 07/10/2026 13:45, fir wrote:
>>
>>> really c should have that
>>> 1) references like in c++
>>> 2) adhoc damn enums /tags
>>> 3) builtin operators for float3 type
>>
>> That last is a very specific type for C.
>>
>> Why not go instead for a more general ability to be able to overload 
>> operators for user-defined types?
>>
>> You know, like C++.
>>
>> Or maybe you can just use C++ because it's right there, and you use a 
>> C++ compiler anyway.
> 
> 
> ye operator 'overloading' and references are good concepts.. here above 
> i  jut mentioned more modest changes that are needed (more specifically 
> i mentioned those who anoy me practically when i code this rasterizer, 
> and the lack of operators for float3 is heavy pain)
> 
> 
> 

Well, when you are really bored.... Bust out some C++ and give GLM a go. 
Or, go into its code and port it to C?

https://github.com/g-truc/glm
Re: will it be optimized?
#50
Author: Chris M. Thomasson
Date: Wed, 07 Oct 2026 21:12
133 lines
6724 bytes
On 10/7/2026 1:29 PM, fir wrote:
> Chris M. Thomasson pisze:
>> On 10/7/2026 6:08 AM, fir wrote:
>>> fir pisze:
>>>> David Brown pisze:
>>>>> On 07/10/2026 09:01, Andrey Tarasevich wrote:
>>>>>>
>>>>>> On a related node:
>>>>>>
>>>>>> 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
>>>>>>
>>>>>>    struct S { char a[1024]; };
>>>>>>
>>>>>>    struct S foo(void)
>>>>>>    {
>>>>>>      struct S s = { 0 };
>>>>>>      s.a[10] = 42;
>>>>>>      return s;
>>>>>>    }
>>>>>>
>>>>>> into this (in -O3 mode!)
>>>>>>
>>>>>>    .LC0:
>>>>>>          .zero   2
>>>>>>          .zero   1022
>>>>>>    "foo":
>>>>>>          push    rbx
>>>>>>          mov     edx, 1024
>>>>>>          mov     rbx, rdi
>>>>>>          mov     esi, OFFSET FLAT:.LC0
>>>>>>          sub     rsp, 1024
>>>>>>          mov     rdi, rsp
>>>>>>          call    "memcpy"
>>>>>>          mov     rsi, rsp
>>>>>>          mov     rdi, rbx
>>>>>>          mov     edx, 1024
>>>>>>          mov     BYTE PTR [rsp+10], 42
>>>>>>          call    "memcpy"
>>>>>>          add     rsp, 1024
>>>>>>          pop     rbx
>>>>>>          ret
>>>>>>
>>>>>> https://godbolt.org/z/G89vfsvPa
>>>>>>
>>>>>> Initialization is performed literally by copying from a block of 
>>>>>> zeros prepared elsewhere (!). Returning is performed by another 
>>>>>> copying from a local variable into a return area. Weird! Why?
>>>>>>
>>>>>> Clang 21 does this
>>>>>>
>>>>>>    foo:
>>>>>>          push    rbx
>>>>>>          mov     rbx, rdi
>>>>>>          mov     edx, 1024
>>>>>>          xor     esi, esi
>>>>>>          call    memset@PLT
>>>>>>          mov     byte ptr [rbx + 10], 42
>>>>>>          mov     rax, rbx
>>>>>>          pop     rbx
>>>>>>          ret
>>>>>>
>>>>>> https://godbolt.org/z/bx5oM5s1M
>>>>>>
>>>>>> So, it performs a typical C++-style RVO (no `return` copying) and 
>>>>>> is smart enough to initialize the struct without copying zeros 
>>>>>> from somewhere else.
>>>>>>
>>>>>
>>>>> That is interesting.  I can think of no reason to suggest that 
>>>>> clang's code might be incorrect (I've seen a number of occasions 
>>>>> where clang has generated more efficient code than gcc, but has 
>>>>> been lax in adherence to C standards or platform ABIs to do so).
>>>>>
>>>>> When compiling as C++, gcc uses RVO - there is only a single 
>>>>> memcpy() from a zero block.  When the initialiser for s is changed 
>>>>> to the more modern "S s = {};" it uses memset rather than copying 
>>>>> from a zero block, in both C and C++.  But the C version still has 
>>>>> the additional memcpy.
>>>>>
>>>>>
>>>> now all this my rasterizer (ehich i port from older code and make 
>>>> cleaning) its not big 12k lines (oryginal is bigger but i dont 
>>>> bother to check now becouse of being tired and sleepy (after being 
>>>> in forest for the mushroms)
>>>>
>>>> all that i compile in pure c now..just to remember how its different 
>>>> from c in c++ mode..with this enum for tab_max in fact i can live
>>>> with this typedef struct also..co c is still practically ok
>>>> for me..though this references are tempting..also operator 
>>>> overloading for vectors are indeed godo for such 3D code i write
>>>>
>>>> really c should have that
>>>> 1) references like in c++
>>>> 2) adhoc damn enums /tags
>>>> 3) builtin operators for float3 type
>>>
>>>
>>> btw its quite notable how hard is to shoot 7-8 m long ship from 500 
>>> meters in open space..they are movin aroung 180 km/h so not much fast
>>> but even one which is not moving hard to hit..sor rasterizers as it 
>>> show may make physical discovery..my bullets though fly slow (150 m/s 
>>> which is3 times slower than real ones)
>>>
>>>
>>> as t coordinates there was also a question how 1 float unit should map
>>> on mters and as some models i get showed 140 units could be like 7-8 
>>> meters
>>> i can take 1 unit of float is 5 cm, so 20 is 1 meter 200 is 10 meters
>>
>> Fwiw, check this crap out. The yellow lines are the path of the 
>> "interceptors":
>>
>> https://youtu.be/_8IN9eZv7yg
>>
>> This is another rather crude test of mine:
>>
>> https://youtu.be/W7FnwnyP0wQ
> 
> 
> its fine.. but i cant comment on that i was not doing such things..
> 
> now im more interested like in general setup fr some arcade space 
> shooter in 3d or something (i dont want to  finish it just to code few 
> things and gather some observations and conclusions)

If you are going to make a game. Well, its HARD work. You need to have 
decent lighting and music. Fwiw, I made this all from my own code. No 
AI! The MIDI music and everything. It took a while, but once it was 
done! BAM! Have it.

https://youtu.be/HwIkk9zENcg
Re: will it be optimized?
#56
Author: fir
Date: Thu, 08 Oct 2026 06:34
72 lines
3307 bytes
fir pisze:
> bart pisze:
>> On 07/10/2026 13:45, fir wrote:
>>
>>> really c should have that
>>> 1) references like in c++
>>> 2) adhoc damn enums /tags
>>> 3) builtin operators for float3 type
>>
>> That last is a very specific type for C.
>>
>> Why not go instead for a more general ability to be able to overload 
>> operators for user-defined types?
>>
>> You know, like C++.
>>
>> Or maybe you can just use C++ because it's right there, and you use a 
>> C++ compiler anyway.
> 
> 
> ye operator 'overloading' and references are good concepts.. here above 
> i  jut mentioned more modest changes that are needed (more specifically 
> i mentioned those who anoy me practically when i code this rasterizer, 
> and the lack of operators for float3 is heavy pain)
> 
> 
> 

aside the fact that syntax for this operators in c++ are not quite good 
imo (where builtin operators for float3 for sure would have proper syntax )


in fact i recently think that function definition syntax in c also may 
not be much good..if you got this what i call hipermodularity or fractal 
modularity i language then the function not "returns" value but "has a 
value"

like

foo()
{
   static float x;
   static float y;
   static float z;
}

which you eventually oculd "cast" (or something other which waork close 
way) on different subsets (or maybe even supersets) of its value


printf ("%.1f ", (x) foo()); // call and take x

(x,z) foo(); //call and take x y


(x,x,y) foo(); //call and take x x y

(z,y,x) foo  //dont call just take z y x


clasic return hass to narrow usability probably and with this cast you 
could just fit result of one function to another

like also

boo ( (2,3 x,y, "sasaS") foo() )

im not sure as to syntax though, as this should be done carefully and 
its not clear  now


Re: will it be optimized?
#63
Author: fir
Date: Thu, 08 Oct 2026 19:47
115 lines
4793 bytes
fir pisze:
> fir pisze:
>> bart pisze:
>>> On 07/10/2026 13:45, fir wrote:
>>>
>>>> really c should have that
>>>> 1) references like in c++
>>>> 2) adhoc damn enums /tags
>>>> 3) builtin operators for float3 type
>>>
>>> That last is a very specific type for C.
>>>
>>> Why not go instead for a more general ability to be able to overload 
>>> operators for user-defined types?
>>>
>>> You know, like C++.
>>>
>>> Or maybe you can just use C++ because it's right there, and you use a 
>>> C++ compiler anyway.
>>
>>
>> ye operator 'overloading' and references are good concepts.. here 
>> above i  jut mentioned more modest changes that are needed (more 
>> specifically i mentioned those who anoy me practically when i code 
>> this rasterizer, and the lack of operators for float3 is heavy pain)
>>
>>
>>
> 
> aside the fact that syntax for this operators in c++ are not quite good 
> imo (where builtin operators for float3 for sure would have proper syntax )
> 
> 
> in fact i recently think that function definition syntax in c also may 
> not be much good..if you got this what i call hipermodularity or fractal 
> modularity i language then the function not "returns" value but "has a 
> value"
> 
> like
> 
> foo()
> {
>    static float x;
>    static float y;
>    static float z;
> }
> 
> which you eventually oculd "cast" (or something other which waork close 
> way) on different subsets (or maybe even supersets) of its value
> 
> 
> printf ("%.1f ", (x) foo()); // call and take x
> 
> (x,z) foo(); //call and take x y
> 
> 
> (x,x,y) foo(); //call and take x x y
> 
> (z,y,x) foo  //dont call just take z y x
> 
> 
> clasic return hass to narrow usability probably and with this cast you 
> could just fit result of one function to another
> 
> like also
> 
> boo ( (2,3 x,y, "sasaS") foo() )
> 
> im not sure as to syntax though, as this should be done carefully and 
> its not clear  now
> 
> 
if returning is showed to be a bad concept (and it seems so)

and returning is replaced by function having a value just as structure 
has a value then the syntax problem in simplest version is like that

if a is a structure (like float x,y,z) c got such things like a.x a.y 
a.z but it has lack of something like a.xy a.yxz and so on and it is 
needed and a question is how syntax to have to express it

casting is somewhat suitable for that

(x y)a

but im not sure as to this its kinda simple but not so easy to read 
sometimes maybe and not sure if enough general

it is important becouse its quite fundamental operation (takin a field(s))
around as same fundamental as assigning


it also seems quite opposite as calling becouse
if you call f(a b)  you liek "pass" a b into f and
when you take things out (a b) f its like you pull things out of f

wonder if some take this syntax ('pushing' and 'pulling') and it
it would work or just make syntax errors

(c d)f(a b)

is the above absolute clear you push a b into f and pull c d form it?
now what if you use composing of such syntaxes


(c d)f(( f k)g((v)a(3) b))


im not sure

for sure classic a.b.c.d cant quite stay as its too poor it only like 
has serial factor of pulling but not parralel factor and not denotes pushing


Re: will it be optimized?
#74
Author: fir
Date: Fri, 09 Oct 2026 08:56
133 lines
5307 bytes
fir pisze:
> fir pisze:
>> fir pisze:
>>> bart pisze:
>>>> On 07/10/2026 13:45, fir wrote:
>>>>
>>>>> really c should have that
>>>>> 1) references like in c++
>>>>> 2) adhoc damn enums /tags
>>>>> 3) builtin operators for float3 type
>>>>
>>>> That last is a very specific type for C.
>>>>
>>>> Why not go instead for a more general ability to be able to overload 
>>>> operators for user-defined types?
>>>>
>>>> You know, like C++.
>>>>
>>>> Or maybe you can just use C++ because it's right there, and you use 
>>>> a C++ compiler anyway.
>>>
>>>
>>> ye operator 'overloading' and references are good concepts.. here 
>>> above i  jut mentioned more modest changes that are needed (more 
>>> specifically i mentioned those who anoy me practically when i code 
>>> this rasterizer, and the lack of operators for float3 is heavy pain)
>>>
>>>
>>>
>>
>> aside the fact that syntax for this operators in c++ are not quite 
>> good imo (where builtin operators for float3 for sure would have 
>> proper syntax )
>>
>>
>> in fact i recently think that function definition syntax in c also may 
>> not be much good..if you got this what i call hipermodularity or 
>> fractal modularity i language then the function not "returns" value 
>> but "has a value"
>>
>> like
>>
>> foo()
>> {
>>    static float x;
>>    static float y;
>>    static float z;
>> }
>>
>> which you eventually oculd "cast" (or something other which waork 
>> close way) on different subsets (or maybe even supersets) of its value
>>
>>
>> printf ("%.1f ", (x) foo()); // call and take x
>>
>> (x,z) foo(); //call and take x y
>>
>>
>> (x,x,y) foo(); //call and take x x y
>>
>> (z,y,x) foo  //dont call just take z y x
>>
>>
>> clasic return hass to narrow usability probably and with this cast you 
>> could just fit result of one function to another
>>
>> like also
>>
>> boo ( (2,3 x,y, "sasaS") foo() )
>>
>> im not sure as to syntax though, as this should be done carefully and 
>> its not clear  now
>>
>>
> if returning is showed to be a bad concept (and it seems so)
> 
> and returning is replaced by function having a value just as structure 
> has a value then the syntax problem in simplest version is like that
> 
> if a is a structure (like float x,y,z) c got such things like a.x a.y 
> a.z but it has lack of something like a.xy a.yxz and so on and it is 
> needed and a question is how syntax to have to express it
> 

a.yxz

  cant stand but maybe there should be operator that groups things like
in structure

then a.x+y+z or a.x&z&y would mean return such fields

probably should coz to much of this all () could have problems

> casting is somewhat suitable for that
> 
> (x y)a
> 
> but im not sure as to this its kinda simple but not so easy to read 
> sometimes maybe and not sure if enough general
> 
> it is important becouse its quite fundamental operation (takin a field(s))
> around as same fundamental as assigning
> 
> 
> it also seems quite opposite as calling becouse
> if you call f(a b)  you liek "pass" a b into f and
> when you take things out (a b) f its like you pull things out of f
> 
> wonder if some take this syntax ('pushing' and 'pulling') and it
> it would work or just make syntax errors
> 
> (c d)f(a b)
> 
> is the above absolute clear you push a b into f and pull c d form it?
> now what if you use composing of such syntaxes
> 
> 
> (c d)f(( f k)g((v)a(3) b))
> 

this above is sorta unabiguos and not so hard to read but
seems just to much of this ()

> 
> im not sure
> 
> for sure classic a.b.c.d cant quite stay as its too poor it only like 
> has serial factor of pulling but not parralel factor and not denotes 
> pushing
> 
> 
> 
Re: will it be optimized?
#82
Author: fir
Date: Fri, 09 Oct 2026 13:01
179 lines
6359 bytes
fir pisze:
> fir pisze:
>> fir pisze:
>>> fir pisze:
>>>> bart pisze:
>>>>> On 07/10/2026 13:45, fir wrote:
>>>>>
>>>>>> really c should have that
>>>>>> 1) references like in c++
>>>>>> 2) adhoc damn enums /tags
>>>>>> 3) builtin operators for float3 type
>>>>>
>>>>> That last is a very specific type for C.
>>>>>
>>>>> Why not go instead for a more general ability to be able to 
>>>>> overload operators for user-defined types?
>>>>>
>>>>> You know, like C++.
>>>>>
>>>>> Or maybe you can just use C++ because it's right there, and you use 
>>>>> a C++ compiler anyway.
>>>>
>>>>
>>>> ye operator 'overloading' and references are good concepts.. here 
>>>> above i  jut mentioned more modest changes that are needed (more 
>>>> specifically i mentioned those who anoy me practically when i code 
>>>> this rasterizer, and the lack of operators for float3 is heavy pain)
>>>>
>>>>
>>>>
>>>
>>> aside the fact that syntax for this operators in c++ are not quite 
>>> good imo (where builtin operators for float3 for sure would have 
>>> proper syntax )
>>>
>>>
>>> in fact i recently think that function definition syntax in c also 
>>> may not be much good..if you got this what i call hipermodularity or 
>>> fractal modularity i language then the function not "returns" value 
>>> but "has a value"
>>>
>>> like
>>>
>>> foo()
>>> {
>>>    static float x;
>>>    static float y;
>>>    static float z;
>>> }
>>>
>>> which you eventually oculd "cast" (or something other which waork 
>>> close way) on different subsets (or maybe even supersets) of its value
>>>
>>>
>>> printf ("%.1f ", (x) foo()); // call and take x
>>>
>>> (x,z) foo(); //call and take x y
>>>
>>>
>>> (x,x,y) foo(); //call and take x x y
>>>
>>> (z,y,x) foo  //dont call just take z y x
>>>
>>>
>>> clasic return hass to narrow usability probably and with this cast 
>>> you could just fit result of one function to another
>>>
>>> like also
>>>
>>> boo ( (2,3 x,y, "sasaS") foo() )
>>>
>>> im not sure as to syntax though, as this should be done carefully and 
>>> its not clear  now
>>>
>>>
>> if returning is showed to be a bad concept (and it seems so)
>>
>> and returning is replaced by function having a value just as structure 
>> has a value then the syntax problem in simplest version is like that
>>
>> if a is a structure (like float x,y,z) c got such things like a.x a.y 
>> a.z but it has lack of something like a.xy a.yxz and so on and it is 
>> needed and a question is how syntax to have to express it
>>
> 
> a.yxz
> 
>   cant stand but maybe there should be operator that groups things like
> in structure
> 
> then a.x+y+z or a.x&z&y would mean return such fields
> 
> probably should coz to much of this all () could have problems
> 
>> casting is somewhat suitable for that
>>
>> (x y)a
>>
>> but im not sure as to this its kinda simple but not so easy to read 
>> sometimes maybe and not sure if enough general
>>
>> it is important becouse its quite fundamental operation (takin a 
>> field(s))
>> around as same fundamental as assigning
>>
>>
>> it also seems quite opposite as calling becouse
>> if you call f(a b)  you liek "pass" a b into f and
>> when you take things out (a b) f its like you pull things out of f
>>
>> wonder if some take this syntax ('pushing' and 'pulling') and it
>> it would work or just make syntax errors
>>
>> (c d)f(a b)
>>
>> is the above absolute clear you push a b into f and pull c d form it?
>> now what if you use composing of such syntaxes
>>
>>
>> (c d)f(( f k)g((v)a(3) b))
>>
> 

PROBABLY

a.x|y culd be used

x|y|z means here adhoc structure


so

x|y|z = foo(a|b|c)  //pass the struct and ret the struct (if returning 
would be good idea)


returning seem to be bad idea iit also assumes like copying
if returning byval or writing thru pointer..when traating function as a 
value its just acces


foo(a|b|c).x|y


could be considered

hovever this

x|y|z = foo(a|b|c)

also can be used but if someone uses additional copying

overally with this  hipermodularity if foo is just a kind of
namespace opening than

foo(a|b|c) just opens its inner fields to stright use

OVERRALY im happy with thsis noticing that

i alrady considered this syntax lik foo a|b|c|d as
an optional separator for arguments but now i see it can be used
for adhoc structure and subset/superset of fields (and thats nwe thought)



> this above is sorta unabiguos and not so hard to read but
> seems just to much of this ()
> 
>>
>> im not sure
>>
>> for sure classic a.b.c.d cant quite stay as its too poor it only like 
>> has serial factor of pulling but not parralel factor and not denotes 
>> pushing
>>
>>
>>
> 
Re: will it be optimized?
#86
Author: fir
Date: Fri, 09 Oct 2026 16:03
34 lines
2419 bytes
fir pisze:
> 
> returning seem to be bad idea iit also assumes like copying
> if returning byval or writing thru pointer..when traating function as a 
> value its just acces
> 
> 
> foo(a|b|c).x|y
> 
> 
> could be considered
> 
> hovever this
> 
> x|y|z = foo(a|b|c)
> 
> also can be used but if someone uses additional copying
> 
> overally with this  hipermodularity if foo is just a kind of
> namespace opening than
> 
> foo(a|b|c) just opens its inner fields to stright use
> 
> OVERRALY im happy with thsis noticing that
> 
> i alrady considered this syntax lik foo a|b|c|d as
> an optional separator for arguments but now i see it can be used
> for adhoc structure and subset/superset of fields (and thats nwe thought)
> 

well not to say it is finally good (coz it dont seems so) but its good 
as a stage

Re: will it be optimized?
#91
Author: Chris M. Thomasson
Date: Fri, 09 Oct 2026 20:30
5 lines
1609 bytes
On 10/9/2026 1:56 AM, fir wrote:
[...]

if you like a.yxz, then you should take a look at GLSL.
Re: will it be optimized?
#1753
Author: Andrey Tarasevich
Date: Sat, 21 Nov 2026 15:54
94 lines
3699 bytes
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
Re: will it be optimized?
#1770
Author: fir
Date: Sun, 22 Nov 2026 12:14
102 lines
4691 bytes
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)

Thread Navigation

This is a paginated view of messages in the thread with full content displayed inline.

Messages are displayed in chronological order, with the original post highlighted in green.

Use pagination controls to navigate through all messages in large threads.

Back to All Threads