Archived

This topic is now archived and is closed to further replies.

ArnoAtWork

Maths lib

Recommended Posts

Hi, I developped a math library. In this I use PIII instructions to optimise the calculation. But if i want that my lib to be quick with all processors, I need to implement for PIII, AMD, .... The only problem is how to find the best implementation. Do you think that I have to implement an interface and code for each processor with the good instructions? using maths with polymorphism is not really optimised, what should I use? Thanks a lot.

Share this post


Link to post
Share on other sites
The best implementation would be:

Use the programming language of your choice. Get a compiler for that language that is able to produce processor-specific code.

Compile the library with said compiler for every processor it supports.

I''m not trying to be cynic, but really, do you want to rewrite your library everytime a new cpu comes out? Or do you simply want to recompile it with a cpu-supporting compiler?

This way your code could remain the same all the time, while it''s up to the compiler (as it should be) to optimze it.

All you have to do would be to name the libraries something like
math-486.lib
math-p3.lib
math-athlon.lib

Share this post


Link to post
Share on other sites
hum,

I just want to have a good and quick maths lib. Just for maths and maybe for different others topics.

Is it possible just to have a fonction at the beginning of the application to detect what processor is present and then use the corresponding code ?

Share this post


Link to post
Share on other sites
There are ways to determine what processor you're running on. Though I think they may not always work 100% correct.

(Assuming C/C++) You might use pointers to functions, instead of functions and make those point to the 'correct' function for your processor.
Something along the lines of:

if (cpuType==pentium)
   someMathFunctionPtr = somePentiumFunction
else if (cpuType==pentium3)
   someMathFunctionPtr = somePentium3Function

You would have to live with the added overhead of those pointers. (Dunno if that'd be all that relevant though).

But : I'm not quite familiar with MMX and all those other funky extensions, but do they offer a lot in regards of math speed-up? Because if you never use 'em I figure there's no speed difference except for the one you get by using a faster cpu anyways.

I've tried the '80386' and 'Pentium Pro' builds that come along with VC++ once to try what difference in speed I'd get. Guess what: None at all.
So, processor specific compiling will only result in a difference if you use the special opcodes that cpu adds (And if they're faster then a 'non-special' opcode workaround).

[edited by - Wildfire on July 2, 2003 12:18:13 PM]

Share this post


Link to post
Share on other sites
In fact coding 3D operations(matrix, vectors, ....) using SSE functions is really quicker.

Just try matrix multiplying with register 128 bits and intrinsic functions(which allows removing assemblor code for c-style functions), and you will see a real difference.

Share this post


Link to post
Share on other sites
quote:
Original post by ArnoAtWork
I just want to have a good and quick maths lib. Just for maths and maybe for different others topics.

Is it possible just to have a fonction at the beginning of the application to detect what processor is present and then use the corresponding code ?



Absolutely, but there is a good deal of overhead involved in making such function calls - via function pointers. This inhibits compile-time inlining of the code which can be a significant performance boost. So you have to write a set of optimized algorithms and detect at run-time which one(s) to use.

Share this post


Link to post
Share on other sites