🚀 go-pugleaf

RetroBBS NetNews Server

Inspired by RockSolid Light RIP Retro Guy

Article View: comp.lang.c
Article #2020

Re: optimising float3

#2020
From: 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
Technical Details
Message-ID: <11ek0k8$gbp4$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: fir <profesor.fir@gmail.com>
Newsgroups: comp.lang.c
Subject: Re: optimising float3
Date: Mon, 30 Nov 2026 15:18:47 +0100
Organization: A noiseless patient Spider
Lines: 169
Message-ID: <11ek0k8$gbp4$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 14:18:48 +0000 (UTC)
Injection-Info: dont-email.me;
	logging-data="536356"; mail-complaints-to="abuse@eternal-september.org";	posting-account="U2FsdGVkX1/oBQWQDVrdGlZHUPzImZkCDlp5lV2mLfA="; 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:VEsV/2cNBIftr/RYTBIbWIfvSRk= sha256:3awqmHxi6rNcgEvs/kpi1uLlZOyOZuXqsnbt9LyKytI=
	sha1:mx5Zflref9760ScSqS6BADMGxOY= sha256:IG5zx9Ckc6Jkf5I4Iq9Uc+kvy8dfPmoi0Zw9Aayo2tk=
In-Reply-To: <11ejv6f$fo9e$1@dont-email.me>