• Advertisement
Sign in to follow this  

Optimized math libraries out there

This topic is 3946 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

Hello, I have been looking for a fast math library, but my google and sourceforge searches have not produced anything good yet. What I am looking for: A minimal library that provides matrix and vector math, bonus would be quaternions, and other math helpers (random number generators and such) but matrix and vector math would be more than enough. There are plenty of libraries out there that have full source code, but what I need is a library that provides source code and that is written with optimizations (i.e. SSE2 or SSE3) I looked at AMD's Math library, and it seems overkill and cumbersome for what I need. I guess if I cannot find one I can always write it, but if somebody already has done it then I just want to use it and not have to write it. Thanks

Share this post


Link to post
Share on other sites
Advertisement
Thanks Dave,

Yes D3DX is an option, but I would prefer something with source code and that I could modify.

Share this post


Link to post
Share on other sites
What do you need to modify it for? Maybe there is an alternative solution as any extensive modification will be alot of work and could be best avoided.

What are you looking for that it doesn't provide?

Share this post


Link to post
Share on other sites
A want a minimalistic library with source code (a preference), I want the source code so I can modify it, update it, and extend it, and I want the source code to be part of my app, and not ship an extra dll if I can.

As I said D3DX fits the need, but there has to be something out there that is less heavy, provides source code, and is highly optimized.

Share this post


Link to post
Share on other sites
Thanks Zipster,

Unfortunately I do not see any optimizations made on that library (i.e. SSE, SSE2), I already have something similar and much simpler, but thanks for the pointer

Share this post


Link to post
Share on other sites
Quote:
Original post by Zipster
Not so long ago, one of our members (jyk) released a math library that should suit your needs. Of course he could provide further details as to what features it has :)
The CML uses expression templates for all the low-level math code, and so probably doesn't meet the OP's particular requirements (as the OP has noted).

Thanks for the mention though - it is much appreciated :)

Share this post


Link to post
Share on other sites
Quote:
Original post by ldeej
Hello,

I have been looking for a fast math library, but my google and sourceforge searches have not produced anything good yet.

What I am looking for:
A minimal library that provides matrix and vector math, bonus would be quaternions, and other math helpers (random number generators and such) but matrix and vector math would be more than enough.

There are plenty of libraries out there that have full source code, but what I need is a library that provides source code and that is written with optimizations (i.e. SSE2 or SSE3)


SSE2/SSE3 are not optimizations, these are assembly instructions. When people are starting to talk about math libraries with optimizations, they have often badly designed algorithm. And now a sweet secret, SSE2 are for double precision floating point numbers (and some whole numbers operations). If you will not use 64 bit floating point, these are for nothing. (On the other hand from my experience if you are not working with OGL, 64 bit floating point is worthy that potential slowdown. Whole numbers are even better.)

Quaternions are bit unnecessary, and RNG are not related to matrix and vector operations, as RNGs are NOT math (in a same way as "IF" isn't a math).

BTW I seen a butt ugly .h file from Nvidia, simple and probably working. Try to look into theirs SDK/developer section. (I don't recall precisely its name because of HD crash.)


SSE2 instructions are not worthy for math library if that math library isn't handwritten in a 64 bit ASM, as these additional registers (in 64 bit mode) are important for cache.

Share this post


Link to post
Share on other sites
Thanks Raghar,

We can play the language game as much as you want, sure SSE are assembly instructions, but are assembly instructions that can apply the same instruction to multiple data (SIMD) making operations faster (i.e. running a dot product as a single assembly instruction rather than multiple ones)

Note also that you can safely use SSE with 32-bit data, not only 64-bit, by packing 32-bit data into memory aligned for SSE consumption.

If you think it is not worth it you should try to compare any unoptimzed library to the D3DX math library, and you will see the difference, D3DX is optimized for both Intel and AMD, and it uses SSE (It might use other data)

To use SSE you do not need to hand code assembly, you can use compiler intrinsics/extensions, and compiler settings. The VC compiler has settings to let the optimizer use SSE when it is cheaper and it makes sense, the Intel compiler has similar settings.

Thanks for the reply though

Share this post


Link to post
Share on other sites
This is probably going to sound rather obtuse, but doesn't the compiler automatically attempt to build SSE and SIMD friendly binaries? Are you specifically looking for a math library with in line assembler?

Share this post


Link to post
Share on other sites
Thanks Numsgil,

Interestingly enough I turned on the SSE2 instruction set on my VS 2005 SP1 project settings, and the code generated is using SSE and fairly fast, so maybe that is what I want.

It seems like the solution is to find a good library (there are several out there), with minimal dependencies, and compile it the SSE settings.

Thanks

Share this post


Link to post
Share on other sites
Quote:
Original post by Numsgil
This is probably going to sound rather obtuse, but doesn't the compiler automatically attempt to build SSE and SIMD friendly binaries?


Not always. Often only the programmer could decide if an optimization is valid, or not. Because compilers must behave in safe way, they can't throw numerical accuracy out of window just because they might believe a programmer might like it faster. The fastest function is

xor rax, rax
ret

Quite a lot of programmers might be VERY unhappy if the compiler would optimize theirs program this way.

So a compiler would generate SSE/SSE2 friendly code, if it feels like so. Considering the Charles Bloom was screaming murder about lack of peephole optimization in a leading C++ compiler, you shouldn't hold any overexpectations.

Share this post


Link to post
Share on other sites
Quote:
Original post by Raghar
RNG are not related to matrix and vector operations, as RNGs are NOT math (in a same way as "IF" isn't a math).
Bullshit. Of course RNGs are math. Just look at your typical LCG -- it's just a small bit of arithmetic. And even their relation or not to matrix and vector operations is debatable. Consider the Mersenne Twister, that keeps a large vector of values that is used to generate future values.

Share this post


Link to post
Share on other sites
I just wanted to report back with some information about my findings in case other people would be interested:

suitable math libraries out there:


gmtl, nice template library for math functions only http://ggt.sourceforge.net/html/index.html

vmmlib, nice template library from http://sourceforge.net/projects/vmmlib/

tinymath, http://sourceforge.net/projects/tinymath/

glmath, http://sourceforge.net/projects/glmath/

gamemath library proposals: http://game-math3d.svn.sourceforge.net/viewvc/game-math3d/proposals/

mathlib3d for C++ and java: http://sourceforge.net/projects/mathlib3d/

mtl3d optimized SIMD: http://fvmtl3d.svn.sourceforge.net/viewvc/fvmtl3d/trunk/

geometric tools: http://www.geometrictools.com/LibFoundation/Interpolation/Interpolation.html

cmldev, from gamedev member: http://cmldev.net


I would probably try gmtl enabling optimizations on my compiler, to see what I get.

Thanks

Share this post


Link to post
Share on other sites
Probably the most highly optimized math library out there is Intel's Math Kernel Library. It isn't free, but before making it a commercial product, it was free for use. Many routines are hand-optimized to Intel processors, taking advantage of the latest instructions (and, for older computers, even automatically switching to hand-optimized versions for older instruction sets!). If you can find their old free version, that probably best meets your specs.

EDIT: If you're a student, it seems this may be available for only $30!

Share this post


Link to post
Share on other sites
Thanks AIDev,

I briefly looked at it, and looked at AMDs library as well (which is free).

The problem with those libraries is that they are not well suited for game math, and are fairly cumbersome to use.

For what I was able to grasp by looking at the docs, it seems like doing simple operations like dot products ends up being fairly complex, and you end up going through a lot of hoops.

Thanks for the reply

I looked at gmtl a bit more, it fails its own unit tests, and I am testing to see the code generated with SSE enabled...

Share this post


Link to post
Share on other sites
Quote:
Original post by Promit
Quote:
Original post by Raghar
RNG are not related to matrix and vector operations, as RNGs are NOT math (in a same way as "IF" isn't a math).
Bullshit. Of course RNGs are math. Just look at your typical LCG -- it's just a small bit of arithmetic. And even their relation or not to matrix and vector operations is debatable. Consider the Mersenne Twister, that keeps a large vector of values that is used to generate future values.


So you are considering an "If" a math? It looks more like a programming construct. RNGs are just an abstraction of a thermal diode, or an abstraction of other simple ways how to get a random number into the computer.
LCG is just one multiplication and an addition followed by modulo operation on a finite size binary register. Other algorithms were based on a cryptography algorithms, for example ISAAC, or DES. MT is just a big array followed by XOR operations.

Considering I'm author of the fastest 64 bit Java implementation, I should know something about MT. MT is an algorithms based on a feedback register. So that large array is in fact a one big virtual register similar of those already present on the CPU, thus calling it a vector would be VERY misleading. I don't like to drift this thread into gritty details about irregularities in MT, or if CFB mode would be of any use, suffice to say a skilled programmer (NOT code monkey) could modify it without knowledge about underlying math. Thus it's not a math algorithm. (And it's unrelated to an non bloated vector math library.)

I call "math algorithm" an algorithm that requires a high knowledge about math, and requires a lot of numerical computations to be safely modified. A "non math algorithm" is an algorithm that is easy to modify by looking into a source code just by intuition and experience.


Re ldeej
Quote:
I just wanted to report back with some information about my findings in case other people would be interested

Thanks. Thought mathlib3d is discontinued.

Re AIDev

I'd say freaks like Intel should live from HW sales. It looks like when CPU were somehow more standardized they wanted a little insurance against AMD. Bad Intel. On the other hand these are not that feature rich, so you can easily live without Intel libraries for small projects.

Share this post


Link to post
Share on other sites
Quote:
Original post by Raghar
[snip]

RNGs are just a bad example, so don't get too caught up on them. Indeed, I could just read values directly from a sensor monitoring some random thermal process, and the only math involved would be in converting the analog signal into a digital representation. That math wouldn't be responsible for actually "generating" the value.

But logic, discrete math, number theory, etc., are all fields of mathematics. Even programming is very mathematical (think of functional languages). It has to be, otherwise how would you prove anything about its correctness?

As for "if" statements in particular, think piecewise functions.

[Edited by - Zipster on March 23, 2007 10:25:34 AM]

Share this post


Link to post
Share on other sites
Logic is not a subfield of mathematics. If anything it should be ordered the other way round.

Share this post


Link to post
Share on other sites
You're right, I was thinking one thing but wrote something else :) Unfortunately I can't remember what it was I was originally thinking, so such scratch that. But even as a super-field the point still holds.

Share this post


Link to post
Share on other sites
Quote:
Original post by Raghar
So you are considering an "If" a math?
An if is a compare followed by a branch. The compare is usually done as a subtraction. As you may know, having authored the world's fastest 64 bit java MT library, a subtraction is "arithmetic", commonly considered to be a branch of mathematics. The branch is a distinctly non-mathematical construct, though.
Quote:
RNGs are just an abstraction of a thermal diode, or an abstraction of other simple ways how to get a random number into the computer.
I don't agree, but since the point has nothing to do with anything, I won't bother to elaborate. Just keep in mind that it's PRNG. It's math that is intended to pretend to be random. It isn't actually random the way true random sources are.
Quote:
LCG is just one multiplication and an addition followed by modulo operation on a finite size binary register.
Yes. Those are also "arithmetic" operations, and generally considered mathematical in nature. Perhaps you believe they are programming constructs instead, but I'm fairly sure I've added numbers even when not programming.
Quote:
That's some Other algorithms were based on a cryptography algorithms, for example ISAAC, or DES. MT is just a big array followed by XOR operations.
All crypto algorithms are math. Some even use complex mathematical operations such as "exponentiation", but again, with your extensive math library experience, I'm sure you know that.
Quote:
MT is an algorithms based on a feedback register. So that large array is in fact a one big virtual register similar of those already present on the CPU, thus calling it a vector would be VERY misleading.
Strikes me as a purely semantic difference.
Quote:
I call "math algorithm" an algorithm that requires a high knowledge about math, and requires a lot of numerical computations to be safely modified. A "non math algorithm" is an algorithm that is easy to modify by looking into a source code just by intuition and experience.
And I call a "zero rated poster" one who has no credibility. You can define arbitrary terms all you want, but your definition of what constitutes a "math algorithm" has no bearing on what is or is not mathematical -- and almost everything a computer does that isn't control flow or IO is math. A lot of it is extremely simple arithmetic, but it's still math. The most absurd part of your definition is that it uses the nebulous concept of "high knowledge about math". What does that even mean? I don't consider basic differential calculus to be "high math" -- others may disagree.

[Edited by - Promit on March 23, 2007 8:55:09 PM]

Share this post


Link to post
Share on other sites
@Zipster

Logic and math are indeed quite related. In fact its hard to say where one ends and the other begins. Boolean algebra vs boolean logic for example. But in scope I believe it is safe to state math as a subset from logic. Consider the First Order logic for example.

Share this post


Link to post
Share on other sites
Quote:
Original post by Promit
Quote:
Original post by Raghar
So you are considering an "If" a math?
An if is a compare followed by a branch. The compare is usually done as a subtraction.


An "if" is a programming construct that could be whatever a compiler want it to be, it says to a CPU there might be an unexpected value in registry RIP. The actual required information is a value of a flag with respect to actual instruction. Of course majority of CPUs would try to predict result, and just backward verify it. (and flush pipeline if they would estimate wrong branch)

Quote:
Quote:
MT is an algorithms based on a feedback register. So that large array is in fact a one big virtual register similar of those already present on the CPU, thus calling it a vector would be VERY misleading.
Strikes me as a purely semantic difference.

You should be very careful with this sentence.
Quote:
Quote:
I call "math algorithm" an algorithm that requires a high knowledge about math, and requires a lot of numerical computations to be safely modified. A "non math algorithm" is an algorithm that is easy to modify by looking into a source code just by intuition and experience.
And I call a "zero rated poster" one who has no credibility. You can define arbitrary terms all you want, but your definition of what constitutes a "math algorithm" has no bearing on what is or is not mathematical

Your behavior is a direct representation of this site, and considering this site lost few articles already, you might notice I didn't talked about amount of your citations, or amount of publicly accessible libraries.

Quote:
nebulous concept of "high knowledge about math". What does that even mean? I don't consider basic differential calculus to be "high math" -- others may disagree.

Master degree might be sufficient.

In case you like a real world example. Diana Gruber looks like she knows what she is doing, and she is able use her knowledge in the field.
On the other hand there are PhD that are able to blow up in tasks a high school dropout might solve easily.

BTW A logic (algorithmization) is often quite different from Math (computation) even if these could result into the same thing.

Share this post


Link to post
Share on other sites
Pseudorandom Number Generators *are* mathematical algorithms just like cryptographic algorithms, graph algorithms and many others you would apparently "degrade" to "non-math" algorithms, that's what discrete Math is all about.

And I have to agree with Promit:
Quote:
I call "math algorithm" an algorithm that requires a high knowledge about math, and requires a lot of numerical computations to be safely modified. A "non math algorithm" is an algorithm that is easy to modify by looking into a source code just by intuition and experience.


Is probably the *worst* definitions of a "math algorithm" I have ever heard.

Just for an example, do you know this?
link

It's (very) simple and it's a "math algorithm".

@OP: for a nice SIMD PRNG, check out the new SIMD MT:

http://www.math.sci.hiroshima-u.ac.jp/~m-mat/MT/SFMT/index.html

Share this post


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

  • Advertisement