- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- 17
- 18
- 19
- 20
- 21
- 22
- 23
- 24
- 25
- 26
- 27
- 28
- 29
- 30
- 31
- 32
- 33
- 34
- 35
- 36
- 37
- 38
- 39
- 40
- 41
- 42
- 43
- 44
- 45
- 46
- 47
- 48
- 49
- 50
- 51
- 52
- 53
- 54
- 55
- 56
- 57
- 58
// Taken from the bit-twiddle package
/**
* Rounds to next power of two.
* @function nextPow2
* @memberof PIXI.utils
* @param {number} v - input value
* @returns {number} - next rounded power of two
*/
export function nextPow2(v: number): number
{
v += v === 0 ? 1 : 0;
--v;
v |= v >>> 1;
v |= v >>> 2;
v |= v >>> 4;
v |= v >>> 8;
v |= v >>> 16;
return v + 1;
}
/**
* Checks if a number is a power of two.
* @function isPow2
* @memberof PIXI.utils
* @param {number} v - input value
* @returns {boolean} `true` if value is power of two
*/
export function isPow2(v: number): boolean
{
return !(v & (v - 1)) && (!!v);
}
/**
* Computes ceil of log base 2
* @function log2
* @memberof PIXI.utils
* @param {number} v - input value
* @returns {number} logarithm base 2
*/
export function log2(v: number): number
{
let r = (v > 0xFFFF ? 1 : 0) << 4;
v >>>= r;
let shift = (v > 0xFF ? 1 : 0) << 3;
v >>>= shift; r |= shift;
shift = (v > 0xF ? 1 : 0) << 2;
v >>>= shift; r |= shift;
shift = (v > 0x3 ? 1 : 0) << 1;
v >>>= shift; r |= shift;
return r | (v >> 1);
}