more sign twiddling, today, How to know if 2 integers have the same sign, and how to get that info FAST ! var b:Boolean = (sign(i)==sign(j)) ? true : false to know if 2 integers have the same sign, we can do the obvious: var equalsignB:Boolean = (j=0); Can we do something faster? If we […]

~ read more ~## Posts in category Math

# sign(number) tricks !

I did a post before to fast extract the sign of an integer (http://guihaire.com/code/?p=544), what about Numbers ? The obvious way: sign = (j0)?1 : 0); And like for the integer version, we can remove the ternary operator: sign = int(j>0)-int(j0)-int(j

~ read more ~# integer Math.abs trick

lets try to replace the slow abs = Math.abs(integerVal); The obvious way, abs = j>31)^j)-(j>>31); here why it works: when j>0, j>>31 gives 0, so (0)^j gives j, and j-0 is j when j>31 gives -1, and (-1)^j invert all bits on j, so if the value was -5 , or 1111.1111.1111.1111.1111.1111.1111.1011 , ~5 gives […]

~ read more ~# integer sign trick

How to get the sign of an integer fast ? The obvious way: var sign:int = (intval0)?1 : 0); Can we do better? if we shift 31 bits to the right , we can extract the sign, (val>>31) : if the val=0: 0 (-val>>>31): if the val >0 :1 if the val >31)+(-intval>>>31); this version […]

~ read more ~# Math.sin tricks

Math.sin is slow, we have many options to optimize it, a good post from Jackson dunstan “Even Faster Trig Through Inlining” show lots of methods (http://jacksondunstan.com/articles/1213) But there is a major problem with all the proposed methods : it only works for a certain range of angle, and if you want to use those functions […]

~ read more ~
## Recent Comments