Article View: comp.lang.c
Article #2053Re: optimising float3
From: Ross Finlayson
Date: Tue, 01 Dec 2026 11:43
Date: Tue, 01 Dec 2026 11:43
214 lines
9809 bytes
9809 bytes
On 12/01/2026 03:35 AM, Ross Finlayson wrote:
> On 12/01/2026 03:09 AM, Ross Finlayson wrote:
>> On 11/30/2026 12:22 PM, Ross Finlayson wrote:
>>> On 11/30/2026 11:30 AM, bartc wrote:
>>>> On 30/11/2026 13:54, fir wrote:
>>>>> im a heavy user of float3
>>>>>
>>>>> and functions liek
>>>>>
>>>>>
>>>>> typedef struct {float x,y,z;} float3 ;
>>>>> typedef struct { float3 pos, right, down, dir;} Position ;
>>>>> typedef struct {float3 a,b,c;} tri ;
>>>>>
>>>>>
>>>>>
>>>>> inline float3 f3(float x,float y,float z) { return
>>>>> (float3){x,y,z}; }
>>>>> inline float dot(float3 a, float3 b) { return a.x * b.x + a.y * b.y +
>>>>> a.z * b.z; }
>>>>> inline float3 cross(float3 a, float3 b) { return (float3) {a.y*b.z-
>>>>> a.z*b.y, a.z*b.x-a.x*b.z, a.x*b.y-a.y*b.x}; }
>>>>> inline float length(float3 v) { return sqrtf( v.x*v.x + v.y*v.y +
>>>>> v.z*v.z ); }
>>>>>
>>>>> inline float distance(float3 p, float3 q) { float3 a = {q.x-p.x, q.y-
>>>>> p.y, q.z-p.z}; return length(a); }
>>>>>
>>>>> inline float length3(float x, float y, float z) { return sqrtf( x*x +
>>>>> y*y + z*z ); }
>>>>>
>>>>> inline float3 mul(float3 m, float s) { return
>>>>> f3(m.x*s,m.y*s,m.z*s); }
>>>>
>>>> You're passing a struct by value. C will do lots of copying here, which
>>>> is likely to take longer than the savings of using SIMD.
>>>>
>>>> Use pointers instead.
>>>>
>>>> Even then, you are doing one operation at a time, each one having the
>>>> function call overhead, even using pointers.
>>>>
>>>> For best efficiency, do lots of them in a loop within the same
>>>> function.
>>>>
>>>> /Then/ look at possibly using SIMD. From C, lookup gcc SIMD intrinsics.
>>>>
>>>>
>>>
>>>
>>> One challenge of using SIMD, single-instruction-multiple-data, is that
>>> there's a proliferation of vector-units, that have grown over time,
>>> and various instruction-set-architectures, which of course are hidden
>>> by a language like C, with regards the compiler targeting them
>>> (targeting/targetting).
>>>
>>> So, the intrinsics introduce a bunch of types like what result the
>>> packed registers (vector-registers, v-registers) and about the lanes
>>> then of the tandem units as they are, yet that the various ISA's and
>>> various generations/editions of these, admit some commononality.
>>>
>>> So, the commonality of the vector-units is sort of like this: 16-Byte
>>> wide 16-many deep tandem v-register blocks, v-blocks, these as "virtual
>>> vector-blocks", vv-blocks, aliased onto the 128/256/512-bit wide
>>> registers, with insert and extract.
>>>
>>> What's meant by "tandem" is that these are essentially 16-Byte width
>>> vector units, teamed together, that accounts like "shift" and "rotate",
>>> besides the usual "vertical" account of the packed data-type registers,
>>> any "horizontal" account, and also about the predication of the lanes
>>> in later architectures, here about "AMD" and "ARM", or Intel/AMD with
>>> SSE(2, 4), AVX(2), AVX512, and ARM with NEON and SVE.
>>>
>>> So, besides after cpuid, or for example what "lscpu" on Linux gives,
>>> and later accounts like Intel's abandoning that, then about the
>>> Intel-issued intrinsics to gcc, can make for gas, the GNU assembler,
>>> accounts of macros, about various accounts of hiding the many & varied
>>> parallel/packed instructions, behind common macros that basically
>>> just consider these v-blocks as virtual v-blocks and blocks & banks,
>>> as data: loading onto the v-registers, then considering that a "type",
>>> for example to surface to C, and functions on that, as of as on those.
>>>
>>>
>>>
>>> Then, for example, these sorts macros are common to all
>>> instruction-set-architectures these commodity AMD & ARM vector-units,
>>> then for example there are only so many instructions that also act upon
>>> the quite wide registers just like regular registers, or
>>> "vector-wide-scalar-word".
>>>
>>> Each variation then just varies "COUNT_BLOCKS" and "COUNT_BANKS",
>>> and a common sort of treating a few of the 16-many v-registers
>>> in a v-block as "data", the rest "code", can have this sort of
>>> thing making then a model for "C", then though that this sort of
>>> account is mostly for using the vector-registers "byte-wise",
>>> which is an underserved sort of use-case when usually the
>>> vector-registers are doing embarrassingly parallel packed arithmetic.
>>>
>>> Figuring out how to surface that as "C" then is for making
>>> plain old functions then about the layouts, the layout of
>>> the data and layout of the code as what results data,
>>> then about to/from memory and to/from general-purpose
>>> or g-registers and these kinds of things.
>>>
>>> For most people it's "compile with -O3",
>>> and hope for the best.
>>>
>>>
>>>
>>>
>>>
>>>
>>> /* file:vwsw-avx512.S */
>>>
>> So, then when looking into tow_str and fro_str, "toward-string" and
>> "from-string", then it's not necessarily an unusual or unique idea to
>> write to what one of these "v-blocks" makes as a model of computation
>> itself, for example when there's for
>> SIMD/single-instruction-multiple-data, the complementary concept
>> SWAR/simd-within-a-register,
>> about emulating a v-block in g-registers, then figuring the actual
>> processor has 1-20+ v-blocks sort of like as for giving the thread for
>> some consecutive and repetitive functions, 1-20+ aligned, reasonably
>> sized memory-buffers.
>>
>> Then, this "Viswath: vector-wide-scalar-word" approach,
>> is basically about that this as a model of a self-contained machine:
>> is a bunch of un-rolled small loops, in multiples for example of
>> 16-bytes or 8-shorts or 4-ints/singles or 2-longs/doubles, usually
>> enough about the datatypes "byte, word (16b), double-word (32b),
>> quad-word (64b), double-quad-word (128b)", these being the
>> "data-types" in "Intel x86 assembler", or correspondingly ARM,
>> since ARM admits have this same sort of profile for these considered
>> instructions (which after looking around at all the specs of the ISAs,
>> these are pretty much all the operations available everywhere).
>>
>>
>>
>> For example, here's for ARM NEON, notice it's the same macros
>> with the same signatures the same sources & destinations and
>> the same given needed-temporaries as AMD AVX512, or AVX2
>> or SSE4, basically, also ARM SVE, more-than-less.
>>
>>
>>
>>
>> /* file:vwsw-neon.S */
>>
>
> Of course when I say "that's all there is to it",
> it's particularly about what's "byte-wise" on the SIMD vector-units,
> that the usual wider account was first single-floats and double-floats
> then int32's then uint32's then half-floats (16-bits) and the like,
> about word/dword/qword, then that the accounts of arithmetic which
> makes one of: arithmetic, bit-wise logical, or comparison, is
> granular in those sizes, for adding two vectors.
>
> (This is the "vertical" not getting yet into the "horizontal".)
>
> Then probably there's either "array-of-structs" or "struct-of-arrays"
> approach, that basically independent terms like the axes of 3-D then
> make for:
>
> struct {
> float x;
> float y;
> float z;
> float unused;
> }
>
> or for like
>
> struct {
> short r;
> short g;
> short b;
> short a;
> }
>
> so that then that equals 8-bytes and two of those load on a 16-byte
> alias ("virtual vector-register") via insert/extract on a 16, 32, 64,
> ... byte wide "full vector-register", or full-vector wide as long as
> it aligns and fits, then for
>
> v3d state[N];
> v3d change[N];
>
> then makes for
>
> void add_single_v3d(v3d* state_1, v3d* change_1) {
> state_1.x += change_1.x;
> state_1.y += change_1.y;
> state_1.z += change_1.z;
> state_1.unused += change_1.unused;
> }
>
> void add_vector_v3d(v3d* state_v, v3d* change_v, size_t count) {
> for (int i = 0; i < count; i++) {
> add_single_v3d( &(*state_v++), &(*change_v++) );
> }
> }
>
> and compile with "-O3" and hope for the best.
>
>
>
(Or rather "->" instead of "." there, since of course
some pointers are to thusly indexable arrays, and
other pointers are to thusly mutable objects. )
Technical Details
Message-ID:
<4SidnYwkMvFgL5P2nZ2dnZfqn_idnZ2d@giganews.com>Path:
mbox-mo1.pugleaf.net!srv.newsdeef.eu!weretis.net!feeder9.news.weretis.net!border-4.nntp.ord.giganews.com!nntp.giganews.com!Xl.tags.giganews.com!local-1.nntp.ord.giganews.com!news.giganews.com.POSTED!not-for-mailReferences:
<11ejv6f$fo9e$1@dont-email.me> <11ekiss$pbdj$1@dont-email.me> <CtKcnYQfdKaPRpD2nZ2dnZfqnPudnZ2d@giganews.com> <shucnbiQKpaGNpP2nZ2dnZfqn_adnZ2d@giganews.com> <CLecnd5b4KdnLZP2nZ2dnZfqn_GdnZ2d@giganews.com>Show Full Headers
Complete Article Headers
Path: mbox-mo1.pugleaf.net!srv.newsdeef.eu!weretis.net!feeder9.news.weretis.net!border-4.nntp.ord.giganews.com!nntp.giganews.com!Xl.tags.giganews.com!local-1.nntp.ord.giganews.com!news.giganews.com.POSTED!not-for-mail NNTP-Posting-Date: Tue, 01 Dec 2026 11:43:57 +0000 Subject: Re: optimising float3 Newsgroups: comp.lang.c References: <11ejv6f$fo9e$1@dont-email.me> <11ekiss$pbdj$1@dont-email.me> <CtKcnYQfdKaPRpD2nZ2dnZfqnPudnZ2d@giganews.com> <shucnbiQKpaGNpP2nZ2dnZfqn_adnZ2d@giganews.com> <CLecnd5b4KdnLZP2nZ2dnZfqn_GdnZ2d@giganews.com> From: Ross Finlayson <ross.a.finlayson@gmail.com> Date: Tue, 1 Dec 2026 03:43:43 -0800 User-Agent: Mozilla/5.0 (X11; Linux x86_64; rv:38.0) Gecko/20100101 Thunderbird/38.6.0 MIME-Version: 1.0 In-Reply-To: <CLecnd5b4KdnLZP2nZ2dnZfqn_GdnZ2d@giganews.com> Content-Type: text/plain; charset=utf-8; format=flowed Content-Transfer-Encoding: 7bit Message-ID: <4SidnYwkMvFgL5P2nZ2dnZfqn_idnZ2d@giganews.com> Lines: 214 X-Usenet-Provider: http://www.giganews.com X-Trace: sv3-c4XCJP9xe7iE3ldeHMHhhQl7WwLs0XoVxSfmTF1GfdZxSPrO3tOoCZxaee1KfZaKCPeJMpQQCcvPUNI!hkJGLkx3o4t8JtuvKUIFCniecVV3kVvRaaWK7iajUntWT5fCa1VV9OgF3vRxTmAnClEdDp6ouWhm X-Complaints-To: abuse@giganews.com X-DMCA-Notifications: http://www.giganews.com/info/dmca.html X-Abuse-and-DMCA-Info: Please be sure to forward a copy of ALL headers X-Abuse-and-DMCA-Info: Otherwise we will be unable to process your complaint properly X-Postfilter: 1.3.40