Article View: comp.lang.c
Article #2039Re: optimising float3
From: fir
Date: Mon, 30 Nov 2026 21:06
Date: Mon, 30 Nov 2026 21:06
445 lines
14550 bytes
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
Technical Details
Message-ID:
<11ekog6$s30g$1@dont-email.me>Path:
mbox-mo1.pugleaf.net!news-can.pugleaf.net!srl.newsdeef.eu!news.hispagatos.org!eternal-september.org!feeder.eternal-september.org!nntp.eternal-september.org!.POSTED!not-for-mailReferences:
<11ejv6f$fo9e$1@dont-email.me> <11ekiss$pbdj$1@dont-email.me> <CtKcnYQfdKaPRpD2nZ2dnZfqnPudnZ2d@giganews.com>Show Full Headers
Complete Article Headers
Path: mbox-mo1.pugleaf.net!news-can.pugleaf.net!srl.newsdeef.eu!news.hispagatos.org!eternal-september.org!feeder.eternal-september.org!nntp.eternal-september.org!.POSTED!not-for-mail From: fir <profesor.fir@gmail.com> Newsgroups: comp.lang.c Subject: Re: optimising float3 Date: Mon, 30 Nov 2026 22:06:14 +0100 Organization: A noiseless patient Spider Lines: 445 Message-ID: <11ekog6$s30g$1@dont-email.me> References: <11ejv6f$fo9e$1@dont-email.me> <11ekiss$pbdj$1@dont-email.me> <CtKcnYQfdKaPRpD2nZ2dnZfqnPudnZ2d@giganews.com> MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8; format=flowed Content-Transfer-Encoding: 8bit Injection-Date: Mon, 30 Nov 2026 21:06:14 +0000 (UTC) Injection-Info: dont-email.me; logging-data="920592"; mail-complaints-to="abuse@eternal-september.org"; posting-account="U2FsdGVkX1+bPbTQNCjlAnDn156G/abKw1izfJPc/sg="; posting-host="2edf17490e4dad8cc45cff77aa4a7fc7" User-Agent: Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:140.0) Gecko/20100101 Firefox/140.0 SeaMonkey/2.53.24 Cancel-Lock: sha1:13WG12/tc/bApbTXdODe3TPOd+I= sha256:Yp2wiL/aaz3H8XMXbg/nF0/W4naJ6K6v3rTI8XcItCw= sha1:ZcivWTgwAbFDYlqzjTYSLaghsgM= sha256:AhKWHuHXSLpU8RloS8sOVfTtpw4ayu8YiudCtr4TX20= In-Reply-To: <CtKcnYQfdKaPRpD2nZ2dnZfqnPudnZ2d@giganews.com>