We have externed a few maths functions as C or CPP code using more raw math, but the difference is not as stark as you might think.
There is no discernable difference between the speed of imported CPP code and BlitzMax when performing raw maths.
Many operations cannot be written to one of the parameters. For example, you cannot multiply two matrices together and write the output to one of them because the function doesn't progress in order:
If mat2 and result are the same pointer, the results will be wrong. I tried doing a memcopy, but I actually found that the original BMX method of creating a new mat4 object and returning it was about 20% faster, I guess because I am allocating new memory anyways. You are right that it is better to write to an existing bit of memory, but that is not always possible, especially when it comes to matrices.
I will keep experimenting with it, but so far the only reason I have found to extern code to C is because it is a good learning experience, and it allows me to say the engine is written in C++ and BlitzMax.
There is no discernable difference between the speed of imported CPP code and BlitzMax when performing raw maths.
Many operations cannot be written to one of the parameters. For example, you cannot multiply two matrices together and write the output to one of them because the function doesn't progress in order:
Function MultMat4( mat1:Float Ptr, mat2:Float Ptr, result:Float Ptr ) result[0] = mat1[0]*mat2[0] + mat1[4]*mat2[1] + mat1[8]*mat2[2] + mat1[12]*mat2[3] result[1] = mat1[1]*mat2[0] + mat1[5]*mat2[1] + mat1[9]*mat2[2] + mat1[13]*mat2[3] result[2] = mat1[2]*mat2[0] + mat1[6]*mat2[1] + mat1[10]*mat2[2] + mat1[14]*mat2[3] result[3] = mat1[3]*mat2[0] + mat1[7]*mat2[1] + mat1[11]*mat2[2] + mat1[15]*mat2[3] result[4] = mat1[0]*mat2[4] + mat1[4]*mat2[5] + mat1[8]*mat2[6] + mat1[12]*mat2[7] result[5] = mat1[1]*mat2[4] + mat1[5]*mat2[5] + mat1[9]*mat2[6] + mat1[13]*mat2[7] result[6] = mat1[2]*mat2[4] + mat1[6]*mat2[5] + mat1[10]*mat2[6] + mat1[14]*mat2[7] result[7] = mat1[3]*mat2[4] + mat1[7]*mat2[5] + mat1[11]*mat2[6] + mat1[15]*mat2[7] result[8] = mat1[0]*mat2[8] + mat1[4]*mat2[9] + mat1[8]*mat2[10] + mat1[12]*mat2[11] result[9] = mat1[1]*mat2[8] + mat1[5]*mat2[9] + mat1[9]*mat2[10] + mat1[13]*mat2[11] result[10] = mat1[2]*mat2[8] + mat1[6]*mat2[9] + mat1[10]*mat2[10] + mat1[14]*mat2[11] result[11] = mat1[3]*mat2[8] + mat1[7]*mat2[9] + mat1[11]*mat2[10] + mat1[15]*mat2[11] result[12] = mat1[0]*mat2[12] + mat1[4]*mat2[13] + mat1[8]*mat2[14] + mat1[12]*mat2[15] result[13] = mat1[1]*mat2[12] + mat1[5]*mat2[13] + mat1[9]*mat2[14] + mat1[13]*mat2[15] result[14] = mat1[2]*mat2[12] + mat1[6]*mat2[13] + mat1[10]*mat2[14] + mat1[14]*mat2[15] result[15] = mat1[3]*mat2[12] + mat1[7]*mat2[13] + mat1[11]*mat2[14] + mat1[15]*mat2[15] EndFunction
If mat2 and result are the same pointer, the results will be wrong. I tried doing a memcopy, but I actually found that the original BMX method of creating a new mat4 object and returning it was about 20% faster, I guess because I am allocating new memory anyways. You are right that it is better to write to an existing bit of memory, but that is not always possible, especially when it comes to matrices.
I will keep experimenting with it, but so far the only reason I have found to extern code to C is because it is a good learning experience, and it allows me to say the engine is written in C++ and BlitzMax.