Jump to content
  • Advertisement
Sign in to follow this  
fir

small inline lib for math in mingw

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

If you intended to correct an error in the post then please contact us.

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 this post


Link to post
Share on other sites
Advertisement

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 this post


Link to post
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 this post


Link to post
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 this post


Link to post
Share on other sites

GLM

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 this post


Link to post
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 this post


Link to post
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)

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

GameDev.net is your game development community. Create an account for your GameDev Portfolio and participate in the largest developer community in the games industry.

Sign me up!