🚀 go-pugleaf

RetroBBS NetNews Server

Inspired by RockSolid Light RIP Retro Guy

12 total messages Started by fir Mon, 30 Nov 2026 13:54
optimising float3
#2019
Author: fir
Date: Mon, 30 Nov 2026 13:54
59 lines
2761 bytes
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); }
inline float3 add(float3 m, float3 s) {	return 
f3(m.x+s.x,m.y+s.y,m.z+s.z); }
inline float3 sub(float3 m, float3 s) {	return 
f3(m.x-s.x,m.y-s.y,m.z-s.z); }

inline float3 add_mul(float3 m, float3 s, float f) {	return f3(m.x + 
s.x*f, m.y + s.y*f, m.z + s.z*f); }


static inline void normalize(float3* vect)
{
    float len = length(*vect);
    if(len<=0)   { ERROR_("division by zero in normalize vector"); 
return;   }

    (*vect).x = (*vect).x/len;
    (*vect).y = (*vect).y/len;
    (*vect).z = (*vect).z/len;

}


and so on lot of it

i wonder if i should fefine

float3 as


typedef struct {float x,y,z, w;} float3 ;

or alike and tryin to kinda make it simd friendly like those functions 
above... do you know how?
Re: optimising float3
#2020
Author: fir
Date: Mon, 30 Nov 2026 14:18
169 lines
4643 bytes
fir pisze:
> 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); }
> inline float3 add(float3 m, float3 s) {    return 
> f3(m.x+s.x,m.y+s.y,m.z+s.z); }
> inline float3 sub(float3 m, float3 s) {    return 
> f3(m.x-s.x,m.y-s.y,m.z-s.z); }
> 
> inline float3 add_mul(float3 m, float3 s, float f) {    return f3(m.x + 
> s.x*f, m.y + s.y*f, m.z + s.z*f); }
> 
> 
> static inline void normalize(float3* vect)
> {
>     float len = length(*vect);
>     if(len<=0)   { ERROR_("division by zero in normalize vector"); 
> return;   }
> 
>     (*vect).x = (*vect).x/len;
>     (*vect).y = (*vect).y/len;
>     (*vect).z = (*vect).z/len;
> 
> }
> 
> 
> and so on lot of it
> 
> i wonder if i should fefine
> 
> float3 as
> 
> 
> typedef struct {float x,y,z, w;} float3 ;
> 
> or alike and tryin to kinda make it simd friendly like those functions 
> above... do you know how?

ai suggest

typedef float v4sf __attribute__((vector_size(16)));

typedef union __attribute__((aligned(16)))
{
     struct {float x,y,z,w;};
     v4sf v;
} 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)
{
     float3 r;
     r.v=(v4sf){x,y,z,0};
     return r;
}


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 f3(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(dot(v,v));
}



inline float distance(float3 p,float3 q)
{
     float3 a;
     a.v=q.v-p.v;
     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)
{
     float3 r;
     r.v=m.v*s;
     return r;
}


inline float3 add(float3 m,float3 s)
{
     float3 r;
     r.v=m.v+s.v;
     return r;
}


inline float3 sub(float3 m,float3 s)
{
     float3 r;
     r.v=m.v-s.v;
     return r;
}


inline float3 add_mul(float3 m,float3 s,float f)
{
     float3 r;
     r.v=m.v+s.v*f;
     return r;
}


static inline void normalize(float3 *vect)
{
     float len=length(*vect);

     if(len<=0)
     {
         ERROR_("division by zero in normalize vector");
         return;
     }

     (*vect).v=(*vect).v/len;
}



- and i just tested it and i get no speedup maybe a slowdown from 6-7 ms 
classical to 7-8 ms vector

so id doesnt seem to work
Re: optimising float3
#2021
Author: fir
Date: Mon, 30 Nov 2026 14:25
9 lines
1487 bytes
dissapointing, but i got no time to study it slow.. some suggest SOA
typedef struct
{
     float x[1000];
     float y[1000];
     float z[1000];
} Vertices;
but imo its rather stupid as those are those rigit optimisations i dont 
much like
Re: optimising float3
#2022
Author: fir
Date: Mon, 30 Nov 2026 14:41
15 lines
1649 bytes
fir pisze:
> 
> inline float3 add(float3 m,float3 s)
> {
>      float3 r;
>      r.v=m.v+s.v;
>      return r;
> }

thst would be obviously good (becouse at least i got almost normal 
vectro addition mul sub and div) but if its slower than it has no sense


hovever its possibly slower becouse tables occupy 25% more ram,
so maybe i should define float4 and use it at least? i dont know
Re: optimising float3
#2033
Author: bartc
Date: Mon, 30 Nov 2026 19:30
41 lines
2504 bytes
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.

Re: optimising float3
#2036
Author: Ross Finlayson
Date: Mon, 30 Nov 2026 20:22
435 lines
13083 bytes
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 */

/*  NB: "EVEX-encoded" */

.include "vwsw-common.S"

.set COUNT_BLOCKS, 4
.set COUNT_BANKS, 2
.set FW, W * COUNT_BLOCKS
.set FD, D * COUNT_BANKS


.section .rodata
.balign FW
.include "vwsw-constants.S"


.set vr_1_A, %zmm0
.set vr_2_A, %zmm1
.set vr_3_A, %zmm2
.set vr_4_A, %zmm3
.set vr_5_A, %zmm4
.set vr_6_A, %zmm5
.set vr_7_A, %zmm6
.set vr_8_A, %zmm7
.set vr_9_A, %zmm8
.set vr_10_A, %zmm9
.set vr_11_A, %zmm10

.set tmpA_A, %zmm11
.set tmpB_A, %zmm12
.set tmpC_A, %zmm13
.set tmpD_A, %zmm14
.set tmpE_A, %zmm15

.set vr_1_B, %zmm16
.set vr_2_B, %zmm17
.set vr_3_B, %zmm18
.set vr_4_B, %zmm19
.set vr_5_B, %zmm20
.set vr_6_B, %zmm21
.set vr_7_B, %zmm22
.set vr_8_B, %zmm23
.set vr_9_B, %zmm24
.set vr_10_B, %zmm25
.set vr_11_B, %zmm26

.set tmpA_B, %zmm27
.set tmpB_B, %zmm28
.set tmpC_B, %zmm29
.set tmpD_B, %zmm30
.set tmpE_B, %zmm31


.set reserved_maskregister_UNUSED, %k0
.set reserved_maskregister_A, %k1

/*  zero-out all registers (non-temporaries) */
.macro _zeroall
vzeroall
.endm

/*  load FWB aligned */
.macro _load dst, mem
vmovdqa \mem, \dst
.endm

/*  store FWB aligned */
.macro _stor mem, src
vmovdqa \src, \mem
.endm

/*  copy from src to dst */
.macro _copy dst, src
vmovdqa \src, \dst
.endm

/*  extract a WB word from src at block/bank to dst */
.macro _extract dst, src, imm_block=0, imm_bank=0
vextract \imm_block, \src, \dst
.endm

/*  insert a WB word from src to dst at block/bank */
.macro _insert dst, src, imm_block=0, imm_bank=0
vinsert \imm_block, \src, \src, \dst
.endm


/*  bit-wise-logical AND full-register */
.macro _and dst, src
/*  TODO: vpternlog for ternary-logical for AVX-512F profile */
pand \src, \dst
.endm

/*  bit-wise-logical OR full-register */
.macro _ior dst, src
por \src, \dst
.endm

/*  bit-wise-logical XOR full-register */
.macro _xor dst, src
pxor \src, \dst
.endm

/* byte-wise signed min, results min(dst, src) in dst */
/* preserves: rhs */
.macro _mins dst, rhs
vpminsb \dst, \rhs, \dst
.endm

/* byte-wise signed max, results max(dst, src) in dst */
/* preserves: rhs */
.macro _maxs dst, rhs
vpmaxsb \dst, \rhs, \dst
.endm

/* byte-wise un-signed min, results min(dst, src) in dst */
/* preserves: rhs */
.macro _minu dst, rhs
vpminub \dst, \rhs, \dst
.endm

/* byte-wise un-signed max, results max(dst, src) in dst */
/* preserves: rhs */
.macro _maxu dst, rhs
vpmaxub \dst, \rhs, \dst
.endm

/*  byte-wise compare dst to rhs result 0x00/0xFF in dst */
/*  preserves: rhs */
.macro _cmpeq dst, rhs
vpcmpeqb \dst, \rhs, reserved_maskregister_A
vpmovm2b reserved_maskregister_A, \dst
.endm

/*  byte-wise compare dst < rhs (unsigned) to result 0x00/0xFF in dst */
/*  preserves: rhs */
.macro _cmplt dst, rhs, tmp1, tmp2
_copy \tmp1, \rhs
_tuss \tmp1, \tmp2
_tuss \tmp2, \dst
vpcmpgtb \tmp2, \tmp1, reserved_maskregister_A
vpmovm2b reserved_maskregister_A, \dst
.endm

/*  byte-wise compare dst > rhs (unsigned) to result 0x00/0xFF in dst */
/*  preserves: rhs */
.macro _cmpgt dst, rhs, tmp1, tmp2
_copy \tmp1, \rhs
_tuss \tmp1, \tmp2
_tuss \tmp2, \dst
vpcmpgtb \tmp2, \tmp1, reserved_maskregister_A
vpmovm2b reserved_maskregister_A, \dst
.endm

/*  bit-wise-logical NOT */
.macro _not dst, tmp1
_ones \tmp1
_xor \dst, \tmp1
.endm

/*  load constant 0x00 byte-wise into dst */
.macro _zero dst
_xor \dst, \dst
.endm

/*  load constant 0xFF byte-wise into dst */
.macro _ones dst
_zero \dst
_cmpeq \dst, \dst
.endm

/*  load constant 0x80 byte-wise into dst */
.macro _bithi dst
_load \dst, constant_bithi(%rip)
.endm

/*  load constant 0x01 byte-wise into dst */
.macro _bitlo dst
_load \dst, constant_bitlo(%rip)
.endm

/*  load constant 0xF0 byte-wise into dst */
.macro _nybhi dst
_load \dst, constant_nybhi(%rip)
.endm

/*  load constant 0x0F byte-wise into dst */
.macro _nyblo dst
_load \dst, constant_nyblo(%rip)
.endm

/*  load constant 0x10 byte-wise into dst */
.macro _nybhibitlo dst
_load \dst, constant_nybhibitlo(%rip)
.endm

/*  load constant 0x08 byte-wise into dst */
.macro _nyblobithi dst
_load \dst, constant_nyblobithi(%rip)
.endm

/*  shift right byte-wise (effective division) */
.macro _shr dst, imm_byte_offset, tmp1
.set IMM, \imm_byte_offset
vpsrldq $IMM, \dst, \dst
.endm

/*  shift left byte-wise (effective multiplication) */
.macro _shl dst, imm_byte_offset, tmp1
.set IMM, \imm_byte_offset
vpslldq $IMM, \dst, \dst
.endm

/*  shift forward byte-wise */
.macro _shf dst, imm_byte_offset, tmp1
.set IMM, \imm_byte_offset
_shl \dst, $IMM
.endm

/*  shift backward byte-wise */
.macro _shb dst, imm_byte_offset, tmp1
.set IMM, \imm_byte_offset
_shr \dst, $IMM
.endm

/*  press each of FWB from src_g byte-wise saturated 0x00/0xFF into dst_v */
.macro _press dst_v, src_g
movq \src_g, \dst_v
vpshufb reserved_maskregister_UNUSED, constant_pressctrl(%rip), \dst_v, 
\dst_v
_and   \dst_v, constant_pressmask(%rip)
_cmpeq \dst_v, constant_pressmask(%rip)
.endm

/*  pick each of FWB from src_v high-bit as FWb into dst_g */
.macro _pick dst_g, src_v, tmp1, g_tmp
vpmovb2m \src_v, reserved_maskregister_A
kmovb reserved_maskregister_A, \dst_g
.endm

/* translate-signed-to-unsigned byte-wise in dst */
.macro _tsus dst, tmp1
_bithi \tmp1
_xor \dst, \tmp1
.endm

/* translate-unsigned-to-signed byte-wise in dst */
.macro _tuss dst, tmp1
_bithi \tmp1
paddb \tmp1, \dst
.endm

/*  true-if-false byte-wise translates 0x00 to 0xFF in dst */
.macro _tiff dst, tmp1, tmp2
_zero \tmp1
_copy \tmp2, \dst
_cmpeq \tmp2, \tmp1
_ior \dst, \tmp2
.endm

/* table/shuffle, as pshufb or tbl */
.macro _tshuf dst, constant_name
vpshufb reserved_maskregister_UNUSED, \constant_name\()(%rip), \dst, \dst
.endm

/* saturating un-signed byte-wise addition, overflow clamped 255 */
.macro _satuadd dst, src
paddusb \src, \dst
.endm

/* saturating signed byte-wise addition, overflow clamped 127, underflow 
clamped -128 */
.macro _satsadd dst, src
paddsb \src, \dst
.endm

/*  saturate the high-bit set/clear to 0xFF/0x00 in dst */
.macro _sathi dst, tmp1
_bithi \tmp1
_and \tmp1, \dst
_cmpeq \dst, \tmp1
.endm

/*  saturate the low-bit set/clear to 0xFF/0x00 in dst */
.macro _satlo dst, tmp1
_bitlo \tmp1
_and \tmp1, \dst
_cmpeq \dst, \tmp1
.endm

/*  saturate any set-bit to 0xFF else 0x00 in dst */
.macro _saton dst, tmp1
_zero \tmp1
_cmpeq \dst, \tmp1
_not \dst, \tmp1
.endm

.macro _tease1 dst, imm_bit_offset, tmp1
.set IMM, (8 - 1 - \imm_bit_offset)
_load \tmp1, constant_tease1(%rip)
vpsrlw reserved_maskregister_UNUSED, $IMM, \dst, \dst
_and \dst, tmp1
.endm

.macro _tease2 dst, imm_bit_offset, tmp1
.set IMM, (8 - 2 - \imm_bit_offset)
_load \tmp1, constant_tease2(%rip)
vpsrlw reserved_maskregister_UNUSED, $IMM, \dst, \dst
_and \dst, tmp1
.endm

.macro _tease3 dst, imm_bit_offset, tmp1
.set IMM, (8 - 3 - \imm_bit_offset)
_load \tmp1, constant_tease3(%rip)
vpsrlw reserved_maskregister_UNUSED, $IMM, \dst, \dst
_and \dst, tmp1
.endm

.macro _tease4 dst, imm_bit_offset, tmp1
.set IMM, (8 - 4 - \imm_bit_offset)
_load \tmp1, constant_tease4(%rip)
vpsrlw reserved_maskregister_UNUSED, $IMM, \dst, \dst
_and \dst, tmp1
.endm



Re: optimising float3
#2038
Author: fir
Date: Mon, 30 Nov 2026 20:59
47 lines
2948 bytes
bartc pisze:
> 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.
> 
> 
you see the words inline? if i write inline functions then i guess
passing by value and by pointer not makes difference...and i would 
assume compiler is not so stupid to not not inline it...
it generalli inlines quite heavier functions if asked, so he could
inline small
Re: optimising float3
#2039
Author: fir
Date: Mon, 30 Nov 2026 21:06
445 lines
14550 bytes
Ross Finlayson pisze:
> 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 */
> 
> /*  NB: "EVEX-encoded" */
> 
> .include "vwsw-common.S"
> 
> .set COUNT_BLOCKS, 4
> .set COUNT_BANKS, 2
> .set FW, W * COUNT_BLOCKS
> .set FD, D * COUNT_BANKS
> 
> 
> .section .rodata
> .balign FW
> .include "vwsw-constants.S"
> 
> 
> .set vr_1_A, %zmm0
> .set vr_2_A, %zmm1
> .set vr_3_A, %zmm2
> .set vr_4_A, %zmm3
> .set vr_5_A, %zmm4
> .set vr_6_A, %zmm5
> .set vr_7_A, %zmm6
> .set vr_8_A, %zmm7
> .set vr_9_A, %zmm8
> .set vr_10_A, %zmm9
> .set vr_11_A, %zmm10
> 
> .set tmpA_A, %zmm11
> .set tmpB_A, %zmm12
> .set tmpC_A, %zmm13
> .set tmpD_A, %zmm14
> .set tmpE_A, %zmm15
> 
> .set vr_1_B, %zmm16
> .set vr_2_B, %zmm17
> .set vr_3_B, %zmm18
> .set vr_4_B, %zmm19
> .set vr_5_B, %zmm20
> .set vr_6_B, %zmm21
> .set vr_7_B, %zmm22
> .set vr_8_B, %zmm23
> .set vr_9_B, %zmm24
> .set vr_10_B, %zmm25
> .set vr_11_B, %zmm26
> 
> .set tmpA_B, %zmm27
> .set tmpB_B, %zmm28
> .set tmpC_B, %zmm29
> .set tmpD_B, %zmm30
> .set tmpE_B, %zmm31
> 
> 
> .set reserved_maskregister_UNUSED, %k0
> .set reserved_maskregister_A, %k1
> 
> /*  zero-out all registers (non-temporaries) */
> .macro _zeroall
> vzeroall
> .endm
> 
> /*  load FWB aligned */
> .macro _load dst, mem
> vmovdqa \mem, \dst
> .endm
> 
> /*  store FWB aligned */
> .macro _stor mem, src
> vmovdqa \src, \mem
> .endm
> 
> /*  copy from src to dst */
> .macro _copy dst, src
> vmovdqa \src, \dst
> .endm
> 
> /*  extract a WB word from src at block/bank to dst */
> .macro _extract dst, src, imm_block=0, imm_bank=0
> vextract \imm_block, \src, \dst
> .endm
> 
> /*  insert a WB word from src to dst at block/bank */
> .macro _insert dst, src, imm_block=0, imm_bank=0
> vinsert \imm_block, \src, \src, \dst
> .endm
> 
> 
> /*  bit-wise-logical AND full-register */
> .macro _and dst, src
> /*  TODO: vpternlog for ternary-logical for AVX-512F profile */
> pand \src, \dst
> .endm
> 
> /*  bit-wise-logical OR full-register */
> .macro _ior dst, src
> por \src, \dst
> .endm
> 
> /*  bit-wise-logical XOR full-register */
> .macro _xor dst, src
> pxor \src, \dst
> .endm
> 
> /* byte-wise signed min, results min(dst, src) in dst */
> /* preserves: rhs */
> .macro _mins dst, rhs
> vpminsb \dst, \rhs, \dst
> .endm
> 
> /* byte-wise signed max, results max(dst, src) in dst */
> /* preserves: rhs */
> .macro _maxs dst, rhs
> vpmaxsb \dst, \rhs, \dst
> .endm
> 
> /* byte-wise un-signed min, results min(dst, src) in dst */
> /* preserves: rhs */
> .macro _minu dst, rhs
> vpminub \dst, \rhs, \dst
> .endm
> 
> /* byte-wise un-signed max, results max(dst, src) in dst */
> /* preserves: rhs */
> .macro _maxu dst, rhs
> vpmaxub \dst, \rhs, \dst
> .endm
> 
> /*  byte-wise compare dst to rhs result 0x00/0xFF in dst */
> /*  preserves: rhs */
> .macro _cmpeq dst, rhs
> vpcmpeqb \dst, \rhs, reserved_maskregister_A
> vpmovm2b reserved_maskregister_A, \dst
> .endm
> 
> /*  byte-wise compare dst < rhs (unsigned) to result 0x00/0xFF in dst */
> /*  preserves: rhs */
> .macro _cmplt dst, rhs, tmp1, tmp2
> _copy \tmp1, \rhs
> _tuss \tmp1, \tmp2
> _tuss \tmp2, \dst
> vpcmpgtb \tmp2, \tmp1, reserved_maskregister_A
> vpmovm2b reserved_maskregister_A, \dst
> .endm
> 
> /*  byte-wise compare dst > rhs (unsigned) to result 0x00/0xFF in dst */
> /*  preserves: rhs */
> .macro _cmpgt dst, rhs, tmp1, tmp2
> _copy \tmp1, \rhs
> _tuss \tmp1, \tmp2
> _tuss \tmp2, \dst
> vpcmpgtb \tmp2, \tmp1, reserved_maskregister_A
> vpmovm2b reserved_maskregister_A, \dst
> .endm
> 
> /*  bit-wise-logical NOT */
> .macro _not dst, tmp1
> _ones \tmp1
> _xor \dst, \tmp1
> .endm
> 
> /*  load constant 0x00 byte-wise into dst */
> .macro _zero dst
> _xor \dst, \dst
> .endm
> 
> /*  load constant 0xFF byte-wise into dst */
> .macro _ones dst
> _zero \dst
> _cmpeq \dst, \dst
> .endm
> 
> /*  load constant 0x80 byte-wise into dst */
> .macro _bithi dst
> _load \dst, constant_bithi(%rip)
> .endm
> 
> /*  load constant 0x01 byte-wise into dst */
> .macro _bitlo dst
> _load \dst, constant_bitlo(%rip)
> .endm
> 
> /*  load constant 0xF0 byte-wise into dst */
> .macro _nybhi dst
> _load \dst, constant_nybhi(%rip)
> .endm
> 
> /*  load constant 0x0F byte-wise into dst */
> .macro _nyblo dst
> _load \dst, constant_nyblo(%rip)
> .endm
> 
> /*  load constant 0x10 byte-wise into dst */
> .macro _nybhibitlo dst
> _load \dst, constant_nybhibitlo(%rip)
> .endm
> 
> /*  load constant 0x08 byte-wise into dst */
> .macro _nyblobithi dst
> _load \dst, constant_nyblobithi(%rip)
> .endm
> 
> /*  shift right byte-wise (effective division) */
> .macro _shr dst, imm_byte_offset, tmp1
> .set IMM, \imm_byte_offset
> vpsrldq $IMM, \dst, \dst
> .endm
> 
> /*  shift left byte-wise (effective multiplication) */
> .macro _shl dst, imm_byte_offset, tmp1
> .set IMM, \imm_byte_offset
> vpslldq $IMM, \dst, \dst
> .endm
> 
> /*  shift forward byte-wise */
> .macro _shf dst, imm_byte_offset, tmp1
> .set IMM, \imm_byte_offset
> _shl \dst, $IMM
> .endm
> 
> /*  shift backward byte-wise */
> .macro _shb dst, imm_byte_offset, tmp1
> .set IMM, \imm_byte_offset
> _shr \dst, $IMM
> .endm
> 
> /*  press each of FWB from src_g byte-wise saturated 0x00/0xFF into 
> dst_v */
> .macro _press dst_v, src_g
> movq \src_g, \dst_v
> vpshufb reserved_maskregister_UNUSED, constant_pressctrl(%rip), \dst_v, 
> \dst_v
> _and   \dst_v, constant_pressmask(%rip)
> _cmpeq \dst_v, constant_pressmask(%rip)
> .endm
> 
> /*  pick each of FWB from src_v high-bit as FWb into dst_g */
> .macro _pick dst_g, src_v, tmp1, g_tmp
> vpmovb2m \src_v, reserved_maskregister_A
> kmovb reserved_maskregister_A, \dst_g
> .endm
> 
> /* translate-signed-to-unsigned byte-wise in dst */
> .macro _tsus dst, tmp1
> _bithi \tmp1
> _xor \dst, \tmp1
> .endm
> 
> /* translate-unsigned-to-signed byte-wise in dst */
> .macro _tuss dst, tmp1
> _bithi \tmp1
> paddb \tmp1, \dst
> .endm
> 
> /*  true-if-false byte-wise translates 0x00 to 0xFF in dst */
> .macro _tiff dst, tmp1, tmp2
> _zero \tmp1
> _copy \tmp2, \dst
> _cmpeq \tmp2, \tmp1
> _ior \dst, \tmp2
> .endm
> 
> /* table/shuffle, as pshufb or tbl */
> .macro _tshuf dst, constant_name
> vpshufb reserved_maskregister_UNUSED, \constant_name\()(%rip), \dst, \dst
> .endm
> 
> /* saturating un-signed byte-wise addition, overflow clamped 255 */
> .macro _satuadd dst, src
> paddusb \src, \dst
> .endm
> 
> /* saturating signed byte-wise addition, overflow clamped 127, underflow 
> clamped -128 */
> .macro _satsadd dst, src
> paddsb \src, \dst
> .endm
> 
> /*  saturate the high-bit set/clear to 0xFF/0x00 in dst */
> .macro _sathi dst, tmp1
> _bithi \tmp1
> _and \tmp1, \dst
> _cmpeq \dst, \tmp1
> .endm
> 
> /*  saturate the low-bit set/clear to 0xFF/0x00 in dst */
> .macro _satlo dst, tmp1
> _bitlo \tmp1
> _and \tmp1, \dst
> _cmpeq \dst, \tmp1
> .endm
> 
> /*  saturate any set-bit to 0xFF else 0x00 in dst */
> .macro _saton dst, tmp1
> _zero \tmp1
> _cmpeq \dst, \tmp1
> _not \dst, \tmp1
> .endm
> 
> .macro _tease1 dst, imm_bit_offset, tmp1
> .set IMM, (8 - 1 - \imm_bit_offset)
> _load \tmp1, constant_tease1(%rip)
> vpsrlw reserved_maskregister_UNUSED, $IMM, \dst, \dst
> _and \dst, tmp1
> .endm
> 
> .macro _tease2 dst, imm_bit_offset, tmp1
> .set IMM, (8 - 2 - \imm_bit_offset)
> _load \tmp1, constant_tease2(%rip)
> vpsrlw reserved_maskregister_UNUSED, $IMM, \dst, \dst
> _and \dst, tmp1
> .endm
> 
> .macro _tease3 dst, imm_bit_offset, tmp1
> .set IMM, (8 - 3 - \imm_bit_offset)
> _load \tmp1, constant_tease3(%rip)
> vpsrlw reserved_maskregister_UNUSED, $IMM, \dst, \dst
> _and \dst, tmp1
> .endm
> 
> .macro _tease4 dst, imm_bit_offset, tmp1
> .set IMM, (8 - 4 - \imm_bit_offset)
> _load \tmp1, constant_tease4(%rip)
> vpsrlw reserved_maskregister_UNUSED, $IMM, \dst, \dst
> _and \dst, tmp1
> .endm
> 
> 
> as far as i remember those simds improve slightly aruthmetic but not imprve memory bandwidth and mem bandwidth s main factor...at east that was some years ago when i was more in mood of testing it (i eve might state the same question as here, i dont remember but there was something like that)

i dont know if to be happy or unhappy those optimisations give roughly 
noting compaed to normal old c

as for now the best optimisation seem to be taking older compiler 
version coz they really seem make slightly faster executables (but i may 
be wrong) and have somewhat better compilation times
Re: optimising float3
#2051
Author: Ross Finlayson
Date: Tue, 01 Dec 2026 11:09
803 lines
22437 bytes
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 */
>
> /*  NB: "EVEX-encoded" */
>
> .include "vwsw-common.S"
>
> .set COUNT_BLOCKS, 4
> .set COUNT_BANKS, 2
> .set FW, W * COUNT_BLOCKS
> .set FD, D * COUNT_BANKS
>
>
> .section .rodata
> .balign FW
> .include "vwsw-constants.S"
>
>
> .set vr_1_A, %zmm0
> .set vr_2_A, %zmm1
> .set vr_3_A, %zmm2
> .set vr_4_A, %zmm3
> .set vr_5_A, %zmm4
> .set vr_6_A, %zmm5
> .set vr_7_A, %zmm6
> .set vr_8_A, %zmm7
> .set vr_9_A, %zmm8
> .set vr_10_A, %zmm9
> .set vr_11_A, %zmm10
>
> .set tmpA_A, %zmm11
> .set tmpB_A, %zmm12
> .set tmpC_A, %zmm13
> .set tmpD_A, %zmm14
> .set tmpE_A, %zmm15
>
> .set vr_1_B, %zmm16
> .set vr_2_B, %zmm17
> .set vr_3_B, %zmm18
> .set vr_4_B, %zmm19
> .set vr_5_B, %zmm20
> .set vr_6_B, %zmm21
> .set vr_7_B, %zmm22
> .set vr_8_B, %zmm23
> .set vr_9_B, %zmm24
> .set vr_10_B, %zmm25
> .set vr_11_B, %zmm26
>
> .set tmpA_B, %zmm27
> .set tmpB_B, %zmm28
> .set tmpC_B, %zmm29
> .set tmpD_B, %zmm30
> .set tmpE_B, %zmm31
>
>
> .set reserved_maskregister_UNUSED, %k0
> .set reserved_maskregister_A, %k1
>
> /*  zero-out all registers (non-temporaries) */
> .macro _zeroall
> vzeroall
> .endm
>
> /*  load FWB aligned */
> .macro _load dst, mem
> vmovdqa \mem, \dst
> .endm
>
> /*  store FWB aligned */
> .macro _stor mem, src
> vmovdqa \src, \mem
> .endm
>
> /*  copy from src to dst */
> .macro _copy dst, src
> vmovdqa \src, \dst
> .endm
>
> /*  extract a WB word from src at block/bank to dst */
> .macro _extract dst, src, imm_block=0, imm_bank=0
> vextract \imm_block, \src, \dst
> .endm
>
> /*  insert a WB word from src to dst at block/bank */
> .macro _insert dst, src, imm_block=0, imm_bank=0
> vinsert \imm_block, \src, \src, \dst
> .endm
>
>
> /*  bit-wise-logical AND full-register */
> .macro _and dst, src
> /*  TODO: vpternlog for ternary-logical for AVX-512F profile */
> pand \src, \dst
> .endm
>
> /*  bit-wise-logical OR full-register */
> .macro _ior dst, src
> por \src, \dst
> .endm
>
> /*  bit-wise-logical XOR full-register */
> .macro _xor dst, src
> pxor \src, \dst
> .endm
>
> /* byte-wise signed min, results min(dst, src) in dst */
> /* preserves: rhs */
> .macro _mins dst, rhs
> vpminsb \dst, \rhs, \dst
> .endm
>
> /* byte-wise signed max, results max(dst, src) in dst */
> /* preserves: rhs */
> .macro _maxs dst, rhs
> vpmaxsb \dst, \rhs, \dst
> .endm
>
> /* byte-wise un-signed min, results min(dst, src) in dst */
> /* preserves: rhs */
> .macro _minu dst, rhs
> vpminub \dst, \rhs, \dst
> .endm
>
> /* byte-wise un-signed max, results max(dst, src) in dst */
> /* preserves: rhs */
> .macro _maxu dst, rhs
> vpmaxub \dst, \rhs, \dst
> .endm
>
> /*  byte-wise compare dst to rhs result 0x00/0xFF in dst */
> /*  preserves: rhs */
> .macro _cmpeq dst, rhs
> vpcmpeqb \dst, \rhs, reserved_maskregister_A
> vpmovm2b reserved_maskregister_A, \dst
> .endm
>
> /*  byte-wise compare dst < rhs (unsigned) to result 0x00/0xFF in dst */
> /*  preserves: rhs */
> .macro _cmplt dst, rhs, tmp1, tmp2
> _copy \tmp1, \rhs
> _tuss \tmp1, \tmp2
> _tuss \tmp2, \dst
> vpcmpgtb \tmp2, \tmp1, reserved_maskregister_A
> vpmovm2b reserved_maskregister_A, \dst
> .endm
>
> /*  byte-wise compare dst > rhs (unsigned) to result 0x00/0xFF in dst */
> /*  preserves: rhs */
> .macro _cmpgt dst, rhs, tmp1, tmp2
> _copy \tmp1, \rhs
> _tuss \tmp1, \tmp2
> _tuss \tmp2, \dst
> vpcmpgtb \tmp2, \tmp1, reserved_maskregister_A
> vpmovm2b reserved_maskregister_A, \dst
> .endm
>
> /*  bit-wise-logical NOT */
> .macro _not dst, tmp1
> _ones \tmp1
> _xor \dst, \tmp1
> .endm
>
> /*  load constant 0x00 byte-wise into dst */
> .macro _zero dst
> _xor \dst, \dst
> .endm
>
> /*  load constant 0xFF byte-wise into dst */
> .macro _ones dst
> _zero \dst
> _cmpeq \dst, \dst
> .endm
>
> /*  load constant 0x80 byte-wise into dst */
> .macro _bithi dst
> _load \dst, constant_bithi(%rip)
> .endm
>
> /*  load constant 0x01 byte-wise into dst */
> .macro _bitlo dst
> _load \dst, constant_bitlo(%rip)
> .endm
>
> /*  load constant 0xF0 byte-wise into dst */
> .macro _nybhi dst
> _load \dst, constant_nybhi(%rip)
> .endm
>
> /*  load constant 0x0F byte-wise into dst */
> .macro _nyblo dst
> _load \dst, constant_nyblo(%rip)
> .endm
>
> /*  load constant 0x10 byte-wise into dst */
> .macro _nybhibitlo dst
> _load \dst, constant_nybhibitlo(%rip)
> .endm
>
> /*  load constant 0x08 byte-wise into dst */
> .macro _nyblobithi dst
> _load \dst, constant_nyblobithi(%rip)
> .endm
>
> /*  shift right byte-wise (effective division) */
> .macro _shr dst, imm_byte_offset, tmp1
> .set IMM, \imm_byte_offset
> vpsrldq $IMM, \dst, \dst
> .endm
>
> /*  shift left byte-wise (effective multiplication) */
> .macro _shl dst, imm_byte_offset, tmp1
> .set IMM, \imm_byte_offset
> vpslldq $IMM, \dst, \dst
> .endm
>
> /*  shift forward byte-wise */
> .macro _shf dst, imm_byte_offset, tmp1
> .set IMM, \imm_byte_offset
> _shl \dst, $IMM
> .endm
>
> /*  shift backward byte-wise */
> .macro _shb dst, imm_byte_offset, tmp1
> .set IMM, \imm_byte_offset
> _shr \dst, $IMM
> .endm
>
> /*  press each of FWB from src_g byte-wise saturated 0x00/0xFF into
> dst_v */
> .macro _press dst_v, src_g
> movq \src_g, \dst_v
> vpshufb reserved_maskregister_UNUSED, constant_pressctrl(%rip), \dst_v,
> \dst_v
> _and   \dst_v, constant_pressmask(%rip)
> _cmpeq \dst_v, constant_pressmask(%rip)
> .endm
>
> /*  pick each of FWB from src_v high-bit as FWb into dst_g */
> .macro _pick dst_g, src_v, tmp1, g_tmp
> vpmovb2m \src_v, reserved_maskregister_A
> kmovb reserved_maskregister_A, \dst_g
> .endm
>
> /* translate-signed-to-unsigned byte-wise in dst */
> .macro _tsus dst, tmp1
> _bithi \tmp1
> _xor \dst, \tmp1
> .endm
>
> /* translate-unsigned-to-signed byte-wise in dst */
> .macro _tuss dst, tmp1
> _bithi \tmp1
> paddb \tmp1, \dst
> .endm
>
> /*  true-if-false byte-wise translates 0x00 to 0xFF in dst */
> .macro _tiff dst, tmp1, tmp2
> _zero \tmp1
> _copy \tmp2, \dst
> _cmpeq \tmp2, \tmp1
> _ior \dst, \tmp2
> .endm
>
> /* table/shuffle, as pshufb or tbl */
> .macro _tshuf dst, constant_name
> vpshufb reserved_maskregister_UNUSED, \constant_name\()(%rip), \dst, \dst
> .endm
>
> /* saturating un-signed byte-wise addition, overflow clamped 255 */
> .macro _satuadd dst, src
> paddusb \src, \dst
> .endm
>
> /* saturating signed byte-wise addition, overflow clamped 127, underflow
> clamped -128 */
> .macro _satsadd dst, src
> paddsb \src, \dst
> .endm
>
> /*  saturate the high-bit set/clear to 0xFF/0x00 in dst */
> .macro _sathi dst, tmp1
> _bithi \tmp1
> _and \tmp1, \dst
> _cmpeq \dst, \tmp1
> .endm
>
> /*  saturate the low-bit set/clear to 0xFF/0x00 in dst */
> .macro _satlo dst, tmp1
> _bitlo \tmp1
> _and \tmp1, \dst
> _cmpeq \dst, \tmp1
> .endm
>
> /*  saturate any set-bit to 0xFF else 0x00 in dst */
> .macro _saton dst, tmp1
> _zero \tmp1
> _cmpeq \dst, \tmp1
> _not \dst, \tmp1
> .endm
>
> .macro _tease1 dst, imm_bit_offset, tmp1
> .set IMM, (8 - 1 - \imm_bit_offset)
> _load \tmp1, constant_tease1(%rip)
> vpsrlw reserved_maskregister_UNUSED, $IMM, \dst, \dst
> _and \dst, tmp1
> .endm
>
> .macro _tease2 dst, imm_bit_offset, tmp1
> .set IMM, (8 - 2 - \imm_bit_offset)
> _load \tmp1, constant_tease2(%rip)
> vpsrlw reserved_maskregister_UNUSED, $IMM, \dst, \dst
> _and \dst, tmp1
> .endm
>
> .macro _tease3 dst, imm_bit_offset, tmp1
> .set IMM, (8 - 3 - \imm_bit_offset)
> _load \tmp1, constant_tease3(%rip)
> vpsrlw reserved_maskregister_UNUSED, $IMM, \dst, \dst
> _and \dst, tmp1
> .endm
>
> .macro _tease4 dst, imm_bit_offset, tmp1
> .set IMM, (8 - 4 - \imm_bit_offset)
> _load \tmp1, constant_tease4(%rip)
> vpsrlw reserved_maskregister_UNUSED, $IMM, \dst, \dst
> _and \dst, tmp1
> .endm
>
>
>
>

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 */

.include "vwsw-common.S"

.set COUNT_BLOCKS, 1
.set COUNT_BANKS, 2
.set FW, W * COUNT_BLOCKS
.set FD, D * COUNT_BANKS


.section .rodata
.balign FW
.include "vwsw-constants.S"


.set vr_1_B, v1
.set vr_2_B, v2
.set vr_3_B, v3
.set vr_4_B, v4
.set vr_5_B, v5
.set vr_6_B, v6
.set vr_7_B, v7
.set vr_8_B, v8
.set vr_9_B, v9
.set vr_10_B, v10
.set vr_11_B, v11

.set tmpA_B, v12
.set tmpB_B, v13
.set tmpC_B, v14
.set tmpD_B, v15
.set tmpE_B, v16

.set vr_1_B, v17
.set vr_2_B, v18
.set vr_3_B, v19
.set vr_4_B, v20
.set vr_5_B, v21
.set vr_6_B, v22
.set vr_7_B, v23
.set vr_8_B, v24
.set vr_9_B, v25
.set vr_10_B, v26
.set vr_11_B, v27

.set tmpA_B, v28
.set tmpB_B, v29
.set tmpC_B, v30
.set tmpD_B, v31
.set tmpE_B, v32

/*  zero-out all registers (non-temporaries) */
.macro _zeroall
_zero vr_1_B
_zero vr_2_B
_zero vr_3_B
_zero vr_4_B
_zero vr_5_B
_zero vr_6_B
_zero vr_7_B
_zero vr_8_B
_zero vr_9_B
_zero vr_10_B
_zero vr_11_B
_zero vr_1_B
_zero vr_2_B
_zero vr_3_B
_zero vr_4_B
_zero vr_5_B
_zero vr_6_B
_zero vr_7_B
_zero vr_8_B
_zero vr_9_B
_zero vr_10_B
_zero vr_11_B
.endm


/*  load 16B aligned */
.macro _load dst, mem
ldr \dst\().16b, \mem
.endm

/*  store 16B aligned */
.macro _stor mem, src
str \mem, \src\().16b
.endm

/*  copy from src to dst */
.macro _copy dst, src
mov \dst, \src
.endm

/*  extract a 16B word from src at block/bank to dst */
.macro _extract dst, src, imm_block=0, imm_bank=0
/*  no-op */
.endm

/*  insert a 16B word from src to dst at block/bank */
.macro _insert dst, src, imm_block=0, imm_bank=0
/*  no-op */
.endm

/*  bit-wise-logical AND full-register */
.macro _and dst, src
and \dst\().16b, \src\().16b, \dst\().16b
.endm

/*  bit-wise-logical OR full-register */
.macro _ior dst, src
orr \dst\().16b, \src\().16b, \dst\().16b
.endm

/*  bit-wise-logical XOR full-register */
.macro _xor dst, src
eor \dst\().16b, \src\().16b, \dst\().16b
.endm

/* byte-wise signed min, results min(dst, src) in dst */
/* preserves: rhs */
.macro _mins dst, rhs
smin \rhs\().16b, \dst\().16b
.endm

/* byte-wise signed max, results max(dst, src) in dst */
.macro _maxs dst, rhs
smax \rhs\().16b, \dst\().16b
.endm

/* byte-wise un-signed min, results min(dst, src) in dst */
/* preserves: rhs */
.macro _minu dst, rhs
umin \rhs\().16b, \dst\().16b
.endm

/* byte-wise un-signed max, results max(dst, src) in dst */
.macro _maxu dst, rhs
umax \rhs\().16b, \dst\().16b
.endm

/*  byte-wise compare dst to rhs result 0x00/0xFF in dst */
.macro _cmpeq dst, rhs
cmeq \dst\().16b, \dst\().16b, \rhs\().16b
.endm

/*  byte-wise compare dst < rhs (unsigned) to result 0x00/0xFF in dst */
/*  preserves: rhs */
.macro _cmplt dst, rhs, tmp1, tmp2
_copy \tmp1, \rhs
_tuss \tmp2, \tmp1
_tuss \tmp1, \dst
cmhi \dst\().16b, \tmp1\().16b, \tmp2\().16b
.endm

/*  byte-wise compare dst > rhs (unsigned) to result 0x00/0xFF in dst */
/*  preserves: rhs */
.macro _cmpgt dst, rhs, tmp1, tmp2
_copy \tmp1, \rhs
_tuss \tmp2, \tmp1
_tuss \tmp1, \dst
cmhi \dst\().16b, \tmp1\().16b, \tmp2\().16b
.endm

/*  bit-wise-logical NOT */
.macro _not dst, tmp1
not \dst\().16b, \dst\().16b
.endm

/*  load constant 0x00 byte-wise into dst */
.macro _zero dst
movi \dst\().16b, #0x00
.endm

/*  load constant 0xFF byte-wise into dst */
.macro _ones dst
movi \dst\().16b, #0xFF
.endm

/*  load constant 0x80 byte-wise into dst */
.macro _bithi dst
movi \dst\().16b, #0x80
.endm

/*  load constant 0x01 byte-wise into dst */
.macro _bitlo dst
movi \dst\().16b, #0x01
.endm

/*  load constant 0xF0 byte-wise into dst */
.macro _nybhi dst
movi \dst\().16b, #0xF0
.endm

/*  load constant 0x0F byte-wise into dst */
.macro _nyblo dst
movi \dst\().16b, #0x0F
.endm

/*  load constant 0x10 byte-wise into dst */
.macro _nybhibitlo dst
movi \dst\().16b, #0x10
.endm

/*  load constant 0x08 byte-wise into dst */
.macro _nyblobithi dst
movi \dst\().16b, #0x08
.endm

/*  shift right byte-wise (effective division) */
.macro _shr dst, imm_byte_offset, tmp1
_zero \tmp1
ext \dst\().16b, \tmp1\().16b, \dst\().16b, #(\imm_byte_offset)
.endm

/*  shift left byte-wise (effective multiplication) */
.macro _shl dst, imm_byte_offset, tmp1
_zero \tmp1
ext \dst\().16b, \dst\().16b, \tmp1\().16b, #(16 - \imm_byte_offset)
.endm

/*  shift forward byte-wise */
.macro _shf dst, imm_byte_offset, tmp1
_shr \imm_byte_offset, \dst, \tmp1
.endm

/*  shift backward byte-wise */
.macro _shb dst, imm_byte_offset, tmp1
_shl \imm_byte_offset, \dst, \tmp1
.endm

/*  press each of 16b from src_g byte-wise saturated 0x00/0xFF into dst_v */
.macro _press dst_v, src_g
mov \dst_v, \src_g
tbl \dst_v, constant_pressctrl(%rip), \dst_v
_and   constant_pressmask(%rip), \dst_v
_cmpeq constant_pressmask(%rip), \dst_v
.endm

/*  pick each of 16B from src_v high-bit as 16b into dst_g */
.macro _pick dst_g, src_v, tmp1, g_tmp
_load constant_pressmask(%rip), \tmp1
_and \tmp1, \src_v, \src_v
_load constant_octaquadhi, \tmp1
_and \tmp1, \src_v, \src_v
addv \src_v, \src_v
mov \g_tmp, \src_v
_load constant_pressmask(%rip), \tmp1
_and \tmp1, \src_v, \src_v
_load constant_octaquadlo, \tmp1
_and \tmp1, \src_v, \src_v
addv \src_v, \src_v
mov \dst_g, \src_v
add \dst_g, \dst_g, \g_tmp, LSL #8
.endm

/* translate-signed-to-unsigned byte-wise in dst */
.macro _tsus dst, tmp1
_bithi \tmp1
_xor \dst, \tmp1
.endm

/* translate-unsigned-to-signed byte-wise in dst */
.macro _tuss dst, tmp1
_bithi \tmp1
add \dst\().16b, \dst\().16b, \tmp1\().16b
.endm

/*  true-if-false byte-wise translates 0x00 to 0xFF in dst */
.macro _tiff dst, tmp1, tmp2
_zero \tmp1
_copy \tmp2, \dst
_cmpeq \tmp2, \tmp1
_ior \dst, \tmp2
.endm


/* saturating signed byte-wise addition, overflow clamped 127, underflow 
clamped -128 */
.macro _satsadd dst, src
sqadd \dst\().16b, \src\().16b
.endm

/* saturating un-signed byte-wise addition, overflow clamped 255, 
underflow clamped 0 */
.macro _satuadd dst, src
uqadd \dst\().16b, \src\().16b
.endm

/*  saturate the high-bit set/clear to 0xFF/0x00 in dst */
.macro _sathi dst, tmp1
_bithi \tmp1
_and \tmp1, \dst
_cmpeq \dst, \tmp1
.endm

/*  saturate the low-bit set/clear to 0xFF/0x00 in dst */
.macro _satlo dst, tmp1
_bitlo \tmp1
_and \tmp1, \dst
_cmpeq \dst, \tmp1
.endm

/*  saturate any set-bit to 0xFF else 0x00 in dst */
.macro _saton dst, tmp1
_zero \tmp1
_cmpeq \tmp1, \dst
_not \dst, \tmp1
.endm



.macro _tease1 dst imm_bit_offset, tmp1
ushr \dst\().16b, #(8 - 1 - imm_bit_offset)
.endm

.macro _tease2 dst imm_bit_offset, tmp1
ushr \dst\().16b, #(8 - 2 - imm_bit_offset)
.endm

.macro _tease3 dst imm_bit_offset, tmp1
ushr \dst\().16b, #(8 - 3 - imm_bit_offset)
.endm

.macro _tease4 dst imm_bit_offset, tmp1
ushr \dst\().16b, #(8 - 4 - imm_bit_offset)
.endm





Re: optimising float3
#2052
Author: Ross Finlayson
Date: Tue, 01 Dec 2026 11:35
204 lines
9293 bytes
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.


Re: optimising float3
#2053
Author: Ross Finlayson
Date: Tue, 01 Dec 2026 11:43
214 lines
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. )



Re: optimising float3
#2056
Author: 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);

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