🚀 go-pugleaf

RetroBBS NetNews Server

Inspired by RockSolid Light RIP Retro Guy

Article View: comp.lang.c
Article #2033

Re: optimising float3

#2033
From: 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.

Technical Details
Message-ID: <11ekiss$pbdj$1@dont-email.me>
Path: mbox-mo1.pugleaf.net!srv.newsdeef.eu!news.hispagatos.org!eternal-september.org!feeder.eternal-september.org!nntp.eternal-september.org!.POSTED!not-for-mail
References: <11ejv6f$fo9e$1@dont-email.me>

Show Full Headers
Complete Article Headers
Path: mbox-mo1.pugleaf.net!srv.newsdeef.eu!news.hispagatos.org!eternal-september.org!feeder.eternal-september.org!nntp.eternal-september.org!.POSTED!not-for-mail
From: bartc <bc@freeuk.com>
Newsgroups: comp.lang.c
Subject: Re: optimising float3
Date: Mon, 30 Nov 2026 19:30:36 +0000
Organization: A noiseless patient Spider
Lines: 41
Message-ID: <11ekiss$pbdj$1@dont-email.me>
References: <11ejv6f$fo9e$1@dont-email.me>
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8; format=flowed
Content-Transfer-Encoding: 8bit
Injection-Date: Mon, 30 Nov 2026 19:30:37 +0000 (UTC)
Injection-Info: dont-email.me;
	logging-data="830899"; mail-complaints-to="abuse@eternal-september.org";	posting-account="U2FsdGVkX1/luBksJWkjxXwdT3Xo627o"; posting-host="5ed4506f965496c75385fe9fa398e096"
User-Agent: Mozilla Thunderbird
Cancel-Lock: sha1:JE77ANyyxvkQ1u3vRyX8c3YartY= sha256:k5DdZEJbGQ9Mt3lEQ3hiF/Mdtq0TNI5fVnF550Yv2eU=
	sha1:8n2iIG8voz4tLlCY4hiD41JdaPM= sha256:pKJUM+16Tfq0Fu/DISuDnuKIWpluTI19iGWmyRru7Fw=
In-Reply-To: <11ejv6f$fo9e$1@dont-email.me>
Content-Language: en-GB