# small inline lib for math in mingw

This topic is 1801 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

## Recommended Posts

I would need a small inline library for doing basic math

operation (and maybe others usable small inline routines)

i mean like dot cross normalize vector and stuff like that,

(specyficaly i am doing basic raytracing and my naive functions

i think are slower than the things i could find somewhere and use)

smething that is usable efficient and i could just include it

in my code

is there something like that?

##### Share on other sites

You cant be sure that GCC inlines certain functions. (thats a good thing because it also optimizes for instruction cache, etc) and there is not much to speed up on a 2 line function I think. Maybe change divisions to multiplications, eliminate every branch?

Here is my dot, normalize, cross:

static float    scalarNumber(const CVector3f *const v1, const CVector3f *const v2)              {return (v1->x * v2->x + v1->y * v2->y + v1->z * v2->z);}

//********************************************************************
float CVector3f::normalize(){

float length = sqrt( x*x + y*y + z*z);

if( unlikely( CMathUtilEA::isNearZero( length)))
return 0.0f;
else{

x /= length;
y /= length;
z /= length;

return length;
}

}

//********************************************************************
void CVector3f::crossP(const CVector3f *const A, const CVector3f *const B, const CVector3f *const C){

CVector3f v1( A, B);
CVector3f v2( A, C);

x = v1.y * v2.z - v1.z * v2.y;
y = v1.z * v2.x - v1.x * v2.z;
z = v1.x * v2.y - v1.y * v2.x;

}


##### Share on other sites

You cant be sure that GCC inlines certain functions. (thats a good thing because it also optimizes for instruction cache, etc) and there is not much to speed up on a 2 line function I think. Maybe change divisions to multiplications, eliminate every branch?

Here is my dot, normalize, cross:

static float    scalarNumber(const CVector3f *const v1, const CVector3f *const v2)              {return (v1->x * v2->x + v1->y * v2->y + v1->z * v2->z);}

//********************************************************************
float CVector3f::normalize(){

float length = sqrt( x*x + y*y + z*z);

if( unlikely( CMathUtilEA::isNearZero( length)))
return 0.0f;
else{

x /= length;
y /= length;
z /= length;

return length;
}

}

//********************************************************************
void CVector3f::crossP(const CVector3f *const A, const CVector3f *const B, const CVector3f *const C){

CVector3f v1( A, B);
CVector3f v2( A, C);

x = v1.y * v2.z - v1.z * v2.y;
y = v1.z * v2.x - v1.x * v2.z;
z = v1.x * v2.y - v1.y * v2.x;

}


this is bad for sure you should count the inversion and does 3 multiplies in normalize - and better is to get some better code

at all (using sse intrinsics of something)

##### Share on other sites

and better is to get some better code at all

Like unreadable, non-portable, bugprone hand-optimized code? inline assembly?

If you code in a way that helps the compiler it will usually output pretty neat assembly.

(But anyway you asked for a proper library and not for coding advice so sorry, disregard me.)

your code will make three divisions and i think it will be about 2x slower than necessary -i am searching for faster routines not slower

##### Share on other sites

+1 for GLM. Write your own library only if you are completely confident of what you're doing.

##### Share on other sites

this is heavy c++ i need something thus kind but in c and i would preffer  smaller thing - may be just one .h file

##### Share on other sites

http://www.cs.kent.edu/~farrell/cg02/reference/natetut/glm.c

http://www.cs.kent.edu/~farrell/cg02/reference/natetut/glm.h

It is definitely not as full-featured as glm proper, and definitely is not heavily optimized.

Other choices (not aimed directly at graphics programming):

http://www.gnu.org/software/gsl/

http://www.netlib.org/clapack/ (LAPACK, Linear Algebra PACKage)

http://math-atlas.sourceforge.net/ (BLAS, Basic Linear Algebra Subprograms)

I honestly haven't liked any of the choices I could find in C as much as I like GLM for C++, and the math library issue is probably the only reason I'm even using C++ for my current project instead of C.

##### Share on other sites

I need much simpler thing preferably just one header file with some optymized basic math inline routines (could be inline assembly)

• ### What is your GameDev Story?

In 2019 we are celebrating 20 years of GameDev.net! Share your GameDev Story with us.

• 15
• 9
• 11
• 9
• 9
• ### Forum Statistics

• Total Topics
634135
• Total Posts
3015754
×