Optimized math libraries out there

Started by
38 comments, last by kii 17 years ago
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.
Advertisement
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]
SlimDX | Ventspace Blog | Twitter | Diverse teams make better games. I am currently hiring capable C++ engine developers in Baltimore, MD.
@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.
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.
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
Quote:Original post by Raghar
BTW A logic (algorithmization) is often quite different from Math (computation) even if these could result into the same thing.


Can you clarify what you mean here? I am having trouble grasping your meaning.
Just how much faster can an "optimized" library get than a one just coded in straight c++? From what I've learned by talking to peeps on the devx forums, the compiler is way smart and optimizes the code quite well.

As far as the whole "logic is not math" debate, what about discrete mathmatics? I'll bet there are very few people who don't consider logic a math. And can't logic just be seen as a user defined math? I mean, in addition you put two numbers in and get a number out. The output depends on strict rules applied to the input. Same thing with logic, the output depends on the input. Consider the following:

If I want a cookie then I have to buy one for $0.50.

It doens't look like math, but it's the same as saying

Money = Money - (want_cookie+1) * .5

where false=-1 and true=0. And if I really wanted a cookie, really true could be 1...


Of course, someone could argue that the statements aren't equevelant cuz I may not want a cookie after realizing I have to pay .50 (expensive cookie...) for it. There's countless special cases affecting the first statement that may make it not match the second. But, as I was researching the definition of math (yes, I made sure I knew what I was talking about before I went and posted this) I came across a wikipedia article that mentioned Albert Einstien said something like math can't accurately represent reality. This makes sense, because there's always something that a mathematical model can't accurately represent. For instance: no mathematical model made by man could every simulate the exact flight of a simple paper airplane thrown by some arbitrary creature. There's just too much to take into account- the exact air currents, the effects of gravity, deformation of the plane, etc, etc. You would have to model the behavior of everything in the universe from the beginning of time until the plane was thrown. No computer could ever do that. The only solution would be to create another universe exactly the same as our own and wait however many billion of years until the plane was thrown to figure out exaclty what it's behavior would be. In short, the idea behind math isn't to model the world realistically, but to model it realistically enough given certain constraints and margins of error.

I realize I may sound like a jerk saying this stuff, but I come to forums like this one for accurate, usable information (and to show off when I make something cool). If I didn't know anything about elephants and decided to ask about them, you could say they fly. Of course I'd believe it. Same thing with SSE2/3. Please don't say they are not optimizations (leading me to think they have nothing to do with optimizations) if in fact they can be used to optimize code. And don't try to tell me elephants fly or that logic is not math.

Wow, I should really try to cut down on the ole' word count in my posts... I blame writing classes: demanding uber-high word counts and all... All this typing sure makes me want a cookie.
Quote:Original post by ldeejI have been looking for a fast math library, but my google and sourceforge searches have not produced anything good yet. I am looking for [SIMD/SSE2/SSE3/source-code/light-weight/modifiable/etc]

This thread is very funny! Not long ago I wrote (elsewhere) about how utterly wimpy most so-called programmers have become - almost terrified to write any "significant" code themselves, except [tangled webs of] invocations of tools and libraries provided by others! When I accidentally ran across this thread, I realized most of the replies squirm every which way EXCEPT provide what the original poster most-reasonably asked for ("here is some SIMD assembly-language code).

Aha! Absolute proof (okay, how about "good evidence") that my [very unpopular] opinion is correct. So I applaud the original poster for wanting what he wants - and for thinking what he wants is reasonable! I mean *good grief*, virtually EVERYONE who ever wrote 3D code should have written this code!!!!!

I am not a knight on a white horse, but I will at least provide some hope. What I probably *should* do is write a book that is nothing more than simple SIMD/SSE2/SSE3/SSE4+ code that 1000 programmers are too chicken to write. But hey, that's not my gig. Hmmmm, but maybe it should be. $$$$$

So I provide an example of what the poor original author is talking about. This way all those wimps (you know who you are) who *know* the author has no reasonable need/purpose/justification for what he asks - at least know exactly what you are so terrified of! Surprise: it's easy! And if people put 1/10 as much effort into thinking and writing code as they do to telling others why code is pointless and unnecessary, 100 versions of the code would already be posted on various web-sites. Maybe it is "out there", but it seems like not. Hence, the following is supplied to prove how little work is involved.

I provide versions of probably the two most important SIMD routines for 3D. Note: f32 and f64 are my datatype names for "double-precision floating point" and "single-precision floating point".

The first is a conventional function that multiplies two f64 4x4 matrices to produce one result f64 4x4 matrix (as in: multiply 2 transformation matrices).

I extraced the second chunk of inline assembly from the middle of a function. It transforms two 4-element f64 vectors (x,y,z,w) by the f64 4x4 (16-element)transformation matrix --- to transform a vertex-position and its normal-vector from one coordinate-system to another.

These are probably the two most-executed and fundamental routines in 3D math, hence maybe the most important to optimize (with SIMD/SSE assembly-language). I removed a few lines of totally special-purpose code that tests a bit to see whether the color or texture-coordinates changed, and save them to the result matrix if they do (they rarely if ever do). And of course the second routine presumes a specific vertex structure (position, normal, color, tcoord, etc).

Also note that the vertex position-and-normal transformation routine converts both 4-element f64 vectors to 4-element f64 (and f32) vectors before it saves them into the result structures (and transfers the f32 structure into a VBO in the video-card). Many applications do not need to save the f64 vector.

The code is slightly unconventional in that it computes in f64 instead of f32 like most software does. I have f32 equivalent routines (somewhere around here), but the speed difference is not as great as you might imagine, plus I hate to throw away precision I worked hard to keep. Also, I write optics, orbit computations and other scientific/engineering applications with my matrix routines, and they often *absolutely require* f64 precision. Also note that this code does not contain the code that pre-fetches the position and normal of the next vertex during the current computation to maximize speed. But this prefetch can be very important in some systems, depending on how the default cache look-ahead is configured. Finally, these are not my fastest versions of these routines - but they ARE the easiest to read. To squeeze every last cycle of speed out of code like this you need to carefully reorder instructions to prevent pipeline stalls. But this is increasingly pointless because different versions of even the same CPU do different instruction re-ordering tricks (and thereby cancel all your hard work). These are the most readable versions. This routine take ~40ns per vertex and normal transformation on my computer (in one core of my dual-core Athlon64 CPU). Not too shabby - and DEFINITELY worth the effort if you're serious about the speed/quality of your programs.

I WILL give permission for anyone to put unmodified or modified versions of this code into their open-source code, freeware code, or freeware book as long as they credit me in the appropriate place and way, and explicitly pass-on the same conditions to others that I grant to them. You must send me a personal message to get specific details (my username at gamedev.net is "bootstrap"). Otherwise you must tell me what you want this code for and get my permission for your specific applications (which is easy and free-to-cheap, but you must do so nonetheless). You do NOT have permission to make minor/moderate/drastic changes to this code and then call the result your own. You DO have the right to learn from this code, put it away for 30-days+, then write your own from scratch (not from your "[semi]-photographic memory" (don't be cute), but from all that you understand (of the problems/techniques/routines/methods/etc)).


--- snip super-fast and efficient SIMD/SSE2+ assembly-language source code ---
--- remove matrix multiply ---
--- snip super-fast and efficient SIMD/SSE2+ assembly-language source code ---

--- snip super-fast and efficient SIMD/SSE2+ assembly-language source code ---
--- remove transform vertex-position and normal-vector ---
--- snip super-fast and efficient SIMD/SSE2+ assembly-language source code ---


To the good humanoids frequenting this forum, I am sorry to have removed two excellent hand-coded, super-optimized SIMD/SSE2+ assembly-language routines to accomplish the most crucial inner-loop processes in 3D game/graphics engines.

Why? I do not care that nobody added to my points in appreciation for the difficult-to-find excellent work I provided (as one poster called it). I do care that some lame-brains formed a gang to trash my points score, down 53 points in 2 days. Perhaps the code competes with a product sold by somebody who reads these forums? Or perhaps they want to knock my score down low enough to have my posts excluded from many readers cutoff viewing limit --- to censor the code, or my opinions, perhaps? Frankly, I am not sure, and I do not much care either. Whatever problem this gang has, they are costing you good info. All I do is offer my honest, well considered observations and opinions and ideas and code and techniques for anyone who may benefit from them. If the policy of this forum is to protect and advance the reputations of rotten malicious people, but destroy decent folks who dare point out the problems in the products pushed by the malicious ones, then you have a very interesting system here. It will effectively force everyone to keep their mouths shut, never point out weaknesses and faults of programs and products by any person or organization - because hordes of their followers may decend in hordes to smack you and your messages down by endless pounding on your "low point button" - until your score drops below the threshhold of invisibility (not displayed).

If any honest, decent, straightforward 3D graphics programmer wants the source-code that I just removed (and other routines as well), you can send me a personal message and we can arrange to get you the code you want. The rest of you childish monkeys bouncing around in your cage, laughing insanely, and endlessly pounding on down-points-buttons --- you can write your own routines. I need nothing from you chimps, or this website and forum, if this continues.


[Edited by - bootstrap on April 2, 2007 11:25:31 AM]
Quote:Original post by xenovacivus
And don't try to tell me elephants fly or that logic is not math.



Logic is not math. No more than physics is. You use math to do physics. For physics math is mainly a tool.

Similarly, you use logic to do math. In math logic is a means to an end and not the ends itself.
Ok, ok. Everybody has thier own oppinion about it, and it's really trivial. My programs don't suddenly explode when all of a sudden different terms (math/logic/physics/purple) are applied to them. Maybe I believe logic is math because in 7th grade math class I was introduced to if/then principles. Or maybe I believe so just because it makes so much sense. But I've never been one to push my beliefs on others. So sure, logic is not math if you want to believe so.

Also, I'd like to correct myself about flying elephants. They, in fact, can fly. the movie "Operation Dumbo Drop" confirms it.

And nice stuff there bootstrap! 40ns for a vertex operation using your code? What would this compare to in c++? If it's a significant decrease in time I'll definetly be checking out assembly.

This topic is closed to new replies.

Advertisement