Article View: comp.lang.c
Article #50Re: will it be optimized?
From: Chris M. Thomasson
Date: Wed, 07 Oct 2026 21:12
Date: Wed, 07 Oct 2026 21:12
133 lines
6724 bytes
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
Technical Details
Message-ID:
<11a6cjn$38fv9$1@dont-email.me>Path:
mbox-mo1.pugleaf.net!archive.usenet.blueworldhosting.com!usenet.blueworldhosting.com!diablo1.usenet.blueworldhosting.com!eternal-september.org!feeder.eternal-september.org!nntp.eternal-september.org!.POSTED!not-for-mailReferences:
<11a1f4c$1g3sg$1@dont-email.me> <11a4qor$2kj95$1@dont-email.me> <11a50tk$2m3b3$2@dont-email.me> <11a5etj$2saav$1@dont-email.me> <11a5g8g$2ssm6$1@dont-email.me> <11a68rn$36tqd$2@dont-email.me> <11a6a2r$37a0r$2@dont-email.me>Show Full Headers
Complete Article Headers
Path: mbox-mo1.pugleaf.net!archive.usenet.blueworldhosting.com!usenet.blueworldhosting.com!diablo1.usenet.blueworldhosting.com!eternal-september.org!feeder.eternal-september.org!nntp.eternal-september.org!.POSTED!not-for-mail From: "Chris M. Thomasson" <chris.m.thomasson.1@gmail.com> Newsgroups: comp.lang.c Subject: Re: will it be optimized? Date: Wed, 7 Oct 2026 14:12:19 -0700 Organization: A noiseless patient Spider Lines: 133 Message-ID: <11a6cjn$38fv9$1@dont-email.me> References: <11a1f4c$1g3sg$1@dont-email.me> <11a4qor$2kj95$1@dont-email.me> <11a50tk$2m3b3$2@dont-email.me> <11a5etj$2saav$1@dont-email.me> <11a5g8g$2ssm6$1@dont-email.me> <11a68rn$36tqd$2@dont-email.me> <11a6a2r$37a0r$2@dont-email.me> MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8; format=flowed Content-Transfer-Encoding: 8bit Injection-Date: Wed, 07 Oct 2026 21:12:23 +0000 (UTC) Injection-Info: dont-email.me; logging-data="3424233"; mail-complaints-to="abuse@eternal-september.org"; posting-account="U2FsdGVkX18jQ++Agm30RPzP+uBy+Rh+EmF7Phr6f8o="; posting-host="9e5dad94c1931ab85b8ca42122ca3b99" User-Agent: Mozilla Thunderbird Cancel-Lock: sha1:J5MWhHuavcCedzmRVW5CqyRYQwg= sha256:XN8D3upgFCamRchJmZOnfA4/4u2CfAXI+m5z0AnMS9c= sha1:JCknDzISlm2Tqft8ysTREeULIgs= sha256:oapGAiuU+a7uncA52kvmFrNM8MgeWGY86qSOrzWaWWY= Content-Language: en-US In-Reply-To: <11a6a2r$37a0r$2@dont-email.me>