🚀 go-pugleaf

RetroBBS NetNews Server

Inspired by RockSolid Light RIP Retro Guy

Article View: comp.lang.c
Article #2056

Re: optimising float3

#2056
From: Ross Finlayson
Date: Tue, 01 Dec 2026 13:47
382 lines
16591 bytes
On 12/01/2026 03:43 AM, Ross Finlayson wrote:
> 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. )
>
>
>
>

Of course, any sort of function with more than one pointer parameter,
should have whether "restrict" applies or whether it doesn't,
that "C" originally didn't say yet since C99 says, that whether "restrict"
or "not-restrict" should be written into the signature of any function
with more than one pointer in its parameters.


#define _pr restrict /* pointer "restrict" */
#define _pm /* pointer "mixed", interleaved (no-op) */

v3d_single_add(v3d* _pr state, v3d* _pr change);
v3d_vector_add(v3d* _pr state, v3d* _pr change, size_t count);

Then, this can also work up some assertions, about the expectations,
in the various cases, of whether a pointer is indexing, or a pointer is
mutating.

#define _pr_check1(lhs, rhs) assert(lhs != rhs)
#define _pr_checkN(lhs, rhs, N) assert(lhs > rhs + N || rhs > lhs + N)

(These assume lhs and rhs are the same type, or for a sort
of idiom with sizeof.)

#define _pr_checkN(lhs, rhs, N) assert(lhs > (char*)(void*)rhs + N * 
sizeof(*rhs)  || rhs > (char*)(void*)lhs + N + sizeof(*lhs))

I've only compiled this with my mind so results may vary. I.e.,
if void* doesn't admit pointer arithmetic like it was with sizeof 1,
then there's that "char*" or "byte_t*" again.

Anyways, wherever possible a function on two or more pointers
should indicate "restrict" or not, then since it would be on everything
should be brief and unobtrusive. Then, also having a convention in
assertions, has it so that in runtime (or debug-runtime, or where
assertions are enabled), will also check that "restrict" is satisfied in
effect.

I'd rather write it on the function or even for the entire header than
on the parameters, "restrict", since it's usually always so, then that
"mixed" is instead the special-case, and don't even know whether
it's a compiler option, yet though of course that it goes in the header
since it declares an expectation in the signature, then for assertions
to verify that.


Then, it also makes sense to indicate for pointers, whether they're
the start/end offsets ("beyond" the end, about afore/after and
before/behind) or first/last, or, just like mutable references.

#define _par restrict /* pointer "restrict", array */
#define _pam /* pointer "mixed", interleaved (no-op), array */
#define _pmr restrict /* pointer "restrict", mutable */
#define _pmm /* pointer "mixed", interleaved (no-op), mutable */

Then is for assertions that basically indexing never calls ->
(nor ., except after *) and mutation never calls ++, ..., about
building in the language the purpose of the pointer, no way
in the language to do that, though.


Then, since I figure to leave the code that way forever,
looking into the briefest sort of account to indicate "restrict",
basically "_r" or "__r.

v3d_single_add(v3d* _r state, v3d* _r change);
v3d_vector_add(v3d* _r state, v3d* _r change, size_t count);


I'd rather put it after both the type and the identifier, "far East", say.


There's a not un-usual idea that reads should always be "."
and writes should always be "->", since it can be overloaded
both ways.

state->x += *change.x

or perhaps vice- versa

*state.x += change->x

though usually it's the "mutable" that the "writes" would be
as of a pointer then for "->" instead of "*". Then also is the
idea to not confuse C++ programmers familiar usually with
reference-semantics and "." everywhere when using pointers
and "->" generally, that the "*change." sticks out, vis-a-vis, "state->".

https://en.wikipedia.org/wiki/Pointee

So, "const" should also be sorted out along with "restrict".

"A const pointer cannot be reassigned
to point to a different object from the one it is initially assigned,
but it can be used to modify the value that it points to (called the
pointee)."

"Although C/C++ allows such definitions (which closely match
the English language when reading the definitions from left to right),
the compiler still reads the definitions according to the abovementioned
procedure: from right to left. But putting const before what must be
constant quickly introduces mismatches between what is intended
to be written and what the compiler decides was written."

So, "const-ness" and "restrict-ness" should be in usual locations
for "indexing" and "mutation" cases and combined in one macro,
for "_prr" and "_prw". So, it should usually go in front or "far-West".

https://en.wikipedia.org/wiki/Restrict_keyword

Then it looks like any use of pointers should have both before & after
the type (where the "pointer-ized" type is considered its own type),
the const-ness and restrict-ness, or not-const-ness and not-restrict-ness.


Anyways this is just a note on style except that
functions with two (or more) pointer parameters
should always indicate where "restrict" is applicable,
or rather, both where it is and where it isn't.

Then, since "restrict" is associative across all the pointers
marked "restrict", i.e., there isn't a pair-wise sort of "these
are mutually restrict, those aren't", ..., is for not having
that kind of code.

https://en.wikipedia.org/wiki/Pointer_(computer_programming)


"In many languages, pointers have the additional restriction
that the object they point to has a specific type."


"In addition, further conversion to bytes and arithmetic
will throw off optimizers trying to keep track the [sic] use of
pointers, a problem still being elucidated in academic research."

"Many languages, [...], replace pointers with a more opaque
type of reference, typically referred to as simply a reference,
[....]. Array indexing is handled as a special case."

"Before ANSI C, K&R used char * for the "type-agnostic pointer" purpose."

The article there says "pointers are references" yet the opposite
view is "references are pointers".

ID/IDREF -> ID's a pointer


Anyways it should always be indicated "restrict" or "non-restrict"
on each pointer parameter in any function signature with more
than two pointer parameters.

Then it may also make sense to indicate whether the purpose
of the pointers is mutation or indexing.

Then it makes sense to always include "const-ness" and "restrict-ness",
or the lack thereof, even when it's only one pointer, for-any.


v3d_single_add(_m v3d* _r state, _c v3d* _r change);
v3d_vector_add(_am v3d* _r state, _ac v3d* _r change, size_t count);

Technical Details
Message-ID: <a8idnfYrbvtzUpP2nZ2dnZfqn_Vj4p2d@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-3.nntp.ord.giganews.com!news.giganews.com.POSTED!not-for-mail
References: <11ejv6f$fo9e$1@dont-email.me> <11ekiss$pbdj$1@dont-email.me> <CtKcnYQfdKaPRpD2nZ2dnZfqnPudnZ2d@giganews.com> <shucnbiQKpaGNpP2nZ2dnZfqn_adnZ2d@giganews.com> <CLecnd5b4KdnLZP2nZ2dnZfqn_GdnZ2d@giganews.com> <4SidnYwkMvFgL5P2nZ2dnZfqn_idnZ2d@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-3.nntp.ord.giganews.com!news.giganews.com.POSTED!not-for-mail
NNTP-Posting-Date: Tue, 01 Dec 2026 13:47:26 +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>
 <4SidnYwkMvFgL5P2nZ2dnZfqn_idnZ2d@giganews.com>
From: Ross Finlayson <ross.a.finlayson@gmail.com>
Date: Tue, 1 Dec 2026 05:47: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: <4SidnYwkMvFgL5P2nZ2dnZfqn_idnZ2d@giganews.com>
Content-Type: text/plain; charset=utf-8; format=flowed
Content-Transfer-Encoding: 7bit
Message-ID: <a8idnfYrbvtzUpP2nZ2dnZfqn_Vj4p2d@giganews.com>
Lines: 382
X-Usenet-Provider: http://www.giganews.com
X-Trace: sv3-wbqWymEZ5brORcSeRrOgErA4kLqgyBFh/xZEZ8fKXs79bOfbjmYJCT3gHZ03ay10HP7fJ5ZvH7FX2Lq!hSKsjhw7yGkJc9IcL4OZJtT0Mi8ffkohx+N8vqwjIEOYkwnrFZAWRJbDyDqbrl56uCzpmsmdRGyJ
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