lkang2 / glmatrix

Automatically exported from code.google.com/p/glmatrix
0 stars 0 forks source link

Bitshifting instead of multiplying in frustum #7

Closed GoogleCodeExporter closed 8 years ago

GoogleCodeExporter commented 8 years ago
mat4.frustum = function(left, right, bottom, top, near, far, dest) {
    dest[0] = near << 1 / (right - left);
    dest[1] = 0;
    dest[2] = 0;
    dest[3] = 0;
    dest[4] = 0;
    dest[5] = near << 1 / (top - bottom);
    dest[6] = 0;
    dest[7] = 0;
    dest[8] = (right + left) / (right - left);
    dest[9] = (top + bottom) / (top - bottom);
    dest[10] = -(far + near) / (far - near);
    dest[11] = -1;
    dest[12] = 0;
    dest[13] = 0;
    dest[14] = -(far * near << 1) / (far - near);
    dest[15] = 0;
    return dest;
};

Shaves about 6% from benchmark here.

Proof with microbenchmark:

<html>
<script type="text/javascript">

var d = new Date();
var i = 100;
for (var a = 0; a < 10000000; a++) {
    var i = i << 1;
}
var e = new Date() - d;

var f = new Date();
var i = 100;
for (var a = 0; a < 10000000; a++) {
    var i = i * 2;
}
var g = new Date() - f;

alert(e);
alert(g);
</script>
</html>

Original issue reported on code.google.com by danielhe...@gmail.com on 5 Jun 2010 at 7:51

GoogleCodeExporter commented 8 years ago
Hmm... very interesting! I'm curious, though: Is bitshifting widely supported 
by the 
browsers? (Or, at the very least, WebGL enabled browsers?)

Original comment by Tojiro@gmail.com on 5 Jun 2010 at 8:32

GoogleCodeExporter commented 8 years ago
Yes for sure. It is at least in the ECMAscript 262 first edition from 1997
http://www.mozilla.org/js/language/E262.pdf ) so I think we can assume it works!

Original comment by danielhe...@gmail.com on 5 Jun 2010 at 9:02

GoogleCodeExporter commented 8 years ago
Excellent! I'll see if I can work it in, then!

Original comment by Tojiro@gmail.com on 7 Jun 2010 at 3:14

GoogleCodeExporter commented 8 years ago

Original comment by Tojiro@gmail.com on 12 Jun 2010 at 5:19

GoogleCodeExporter commented 8 years ago

Original comment by Tojiro@gmail.com on 12 Jun 2010 at 5:28

GoogleCodeExporter commented 8 years ago

Original comment by Tojiro@gmail.com on 13 Jun 2010 at 4:13