c - matrix library: inline vs define -
i'm creating matrix library bunch of function 1 (it's long one):
void matx_multiply(int x, float mat1[], float mat2[], float result[]) { int row,col,k; (row=0; row<x; row++) { for(col=0; col<x; col++){ result[row + col*x]=0.0f; (k=0; k<x; k++) { result[row + col*x]+=mat1[row + k*x]*mat2[k + col*x]; } } } }
firstly, wonder if it's better change inline
function if use ~5 times in program (often in loop), x
being known @ compile time.
i think it's better inline
it, compiler can decide @ compile time whether want expanded in code or not (depending on optimization parameter). in addition, compiler might optimize loop if knows x
(for example, if x=2, may decide unroll loop)
more importantly, want add set of functions:
#define mat2_multiply(m1,m2,res) matx_multiply(2,m1,m2,res) #define mat3_multiply(m1,m2,res) matx_multiply(3,m1,m2,res) ...
or
static inline void mat2_multiply(float mat1[static 2], float mat2[static 2], float result[static 2]) { matx_multiply(2,mat1,mat2,result); } ...
or creating function (but create function call nothing)
- one way more concise , expanded (it verify if mat1 , mat2 array of float anyway)
- the second way more secure, verify length of array, it's less concies , may not expanded...
what do, , want matrix library ?
i want library fast (for opengl app), small , easy use.
use inline
keyword. there several disadvantages if use preprocessor develop function-like macros:
- no type safety checking
- no sanity checking
- bad debug
- bad readability
- expressions passed macros can evaluated more once
Comments
Post a Comment