`
androider
  • 浏览: 748435 次
  • 性别: Icon_minigender_1
  • 来自: 苏州
社区版块
存档分类
最新评论

fast integer math

UP 
阅读更多
Left bit shifting to multiply by any power of two

Approximately 300% faster.

x = x * 2;
x = x * 64;

//equals:
x = x << 1;
x = x << 6;

Right bit shifting to divide by any power of two

Approximately 350% faster.

x = x / 2;
x = x / 64;

//equals:
x = x >> 1;
x = x >> 6;

Number to integer conversion

Using int(x) is 10% faster in AS3. Still the bitwise version works better in AS2.

x = int(1.232)

//equals:
x = 1.232 >> 0;

Extracting color components

Not really a trick, but the regular way of extracting values using bit masking and shifting.

//24bit
var color:uint = 0x336699;
var r:uint = color >> 16;
var g:uint = color >> 8 & 0xFF;
var b:uint = color & 0xFF;

//32bit
var color:uint = 0xff336699;
var a:uint = color >>> 24;
var r:uint = color >>> 16 & 0xFF;
var g:uint = color >>>  8 & 0xFF;
var b:uint = color & 0xFF;

Combining color components

‘Shift up’ the values into the correct position and combine them.

//24bit
var r:uint = 0x33;
var g:uint = 0x66;
var b:uint = 0x99;
var color:uint = r << 16 | g << 8 | b;

//32bit
var a:uint = 0xff;
var r:uint = 0x33;
var g:uint = 0x66;
var b:uint = 0x99;
var color:uint = a << 24 | r << 16 | g << 8 | b;

Swap integers without a temporary variable using XOR

Pretty neat trick, it is explained in detail in the link at the top of the page. This is 20% faster.

var t:int = a;
a = b;
b = t;

//equals:
a ^= b;
b ^= a;
a ^= b;

Increment/decrement

This is much slower than the pre/post decrement operator, but a nice way to obfuscate your code ;-)

i = -~i; // i++
i = ~-i; // i--

Sign flipping using NOT or XOR

Strangely enough, this is 300%(!) faster.

i = -i;

//equals
i = ~i + 1;

//or
i = (i ^ -1) + 1;

Fast modulo operation using bitwise AND

If the divisor is a power of 2, the modulo (%) operation can be done with:
modulus = numerator & (divisor - 1);
This is about 600% faster.

x = 131 % 4;

//equals:
x = 131 & (4 - 1);

Check if an integer is even/uneven using bitwise AND

This is 600% faster.

isEven = (i % 2) == 0;

//equals:
isEven = (i & 1) == 0;

Absolute value

Forget Math.abs() for time critical code. Version 1 is 2500% faster than Math.abs(), and the funky bitwise version 2 is again 20% faster than version 1.

//version 1
i = x < 0 ? -x : x;

//version 2
i = (x ^ (x >> 31)) - (x >> 31);

Comparing two integers for equal sign

This is 35% faster.

eqSign = a * b > 0;

//equals:
eqSign = a ^ b >= 0;

Fast color conversion from R5G5B5 to R8G8B8 pixel format using shifts

R8 = (R5 << 3) | (R5 >> 2)
G8 = (R5 << 3) | (R5 >> 2)
B8 = (R5 << 3) | (R5 >> 2)

分享到:
评论

相关推荐

    BigNum Math Implementing Cryptographic Multiple Precision Arithmetic

    For instance, the Karatsuba algorithm is often used for fast multiplication of large numbers. 3. **Modular Arithmetic**: Cryptographic algorithms frequently use modular arithmetic, where operations ...

    LibFixMath

    libfixmath is a platform-independent fixed point maths library aimed at developers wanting to perform fast non-integer maths on platforms lacking a (or with a low performance) FPU. It offers ...

    面试宝典-Java面试题汇总

    - HashMap的迭代器是fail-fast的,而Hashtable的迭代器是legacy的。 6. **Collection 和 Collections的区别** - Collection是所有集合接口的根接口,代表一组元素。 - Collections是针对集合操作的工具类,提供...

    JS大数乘方计算.rar

    Number类型的最大安全数值是`Number.MAX_SAFE_INTEGER`,即9007199254740991,超过这个值的计算可能会出现不准确的结果。因此,当处理大数时,我们不能依赖于JavaScript的内置数据类型。 一种解决方法是使用字符串...

    libtomcrypt-1.17

    The math routines are pluggable which means you can use your own math provider if you want. Other standards PKCS #1 (v1.5 and v2.1 padding) PKCS #5 ASN.1 DER for SEQUENCE, INTEGER, BIT STRING, ...

    怎么引入(调用)一个JS文件

    document.getElementById("num").innerHTML = arr[parseInt(max * Math.random())]; } function start() { var timeId = setInterval("count();", 100); } function stop() { clearInterval(timeId); } ``` ...

    fish in flac3d

    - **预定义函数**:如`math.abs()`等,提供数学计算、字符串处理等功能。 ##### 2.2.3 Scope of Variables:变量的作用域 FISH支持局部变量和全局变量。局部变量仅在其定义的函数内可见,而全局变量在整个程序范围...

    算法-leetcode-剑指offer上的题很多

    - **整数转换(Convert Integer A to Integer B)**: 将一个整数转换为另一个整数。 - **阶乘末尾0的个数(Factorial Trailing Zeroes)**: 计算一个整数阶乘结果末尾有多少个零。 - **唯一二叉搜索树(Unique Binary ...

    python3.6.5参考手册 chm

    PEP 3127: Integer Literal Support and Syntax PEP 3129: Class Decorators PEP 3141: A Type Hierarchy for Numbers The fractions Module Other Language Changes Optimizations Interpreter Changes New ...

    数据结构与算法题解

    - **数学(Math)** - 解决数学问题的算法。 - 实例问题:实现求最大公约数的算法。 - **最大公约数(GreatestCommonDivisor)** - 求两个或多个整数共有约数中最大的一个。 - 实例问题:给出两个正整数,求它们的...

Global site tag (gtag.js) - Google Analytics