Skip to content

Commit fa14692

Browse files
committed
Merge pull request CesiumGS#1287 from AnalyticalGraphicsInc/nextPowerOfTwo
Added Math.nextPowerOfTwo
2 parents 1609bce + 9deb1a9 commit fa14692

File tree

3 files changed

+59
-3
lines changed

3 files changed

+59
-3
lines changed

CHANGES.md

+1
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,7 @@ Beta Releases
3434
* Added `Quaternion.log`, `Quaternion.exp`, `Quaternion.innerQuadrangle`, and `Quaternion.squad`.
3535
* Added `LinearSpline` and `QuaternionSpline`.
3636
* Added `perPositionHeight` option to `PolygonGeometry` and `PolygonOutlineGeometry`.
37+
* Added `Math.nextPowerOfTwo`.
3738

3839
### b22 - 2013-11-01
3940

Source/Core/Math.js

+34-3
Original file line numberDiff line numberDiff line change
@@ -540,19 +540,50 @@ define([
540540
*
541541
* @returns {Boolean} <code>true</code> if the number if a power of two; otherwise, <code>false</code>.
542542
*
543+
* @exception {DeveloperError} A number greater than or equal to 0 is required.
544+
*
543545
* @example
544546
* var t = CesiumMath.isPowerOfTwo(16); // true
545547
* var f = CesiumMath.isPowerOfTwo(20); // false
548+
*/
549+
CesiumMath.isPowerOfTwo = function(n) {
550+
if (typeof n !== 'number' || n < 0) {
551+
throw new DeveloperError('A number greater than or equal to 0 is required.');
552+
}
553+
554+
return (n !== 0) && ((n & (n - 1)) === 0);
555+
};
556+
557+
/**
558+
* Computes the next power-of-two integer greater than or equal to the provided positive integer.
559+
*
560+
* @memberof CesiumMath
561+
*
562+
* @param {Number} n The positive integer to test.
563+
*
564+
* @returns {Number} The next power-of-two integer.
546565
*
547566
* @exception {DeveloperError} A number greater than or equal to 0 is required.
567+
*
568+
* @example
569+
* var n = CesiumMath.nextPowerOfTwo(29); // 32
570+
* var m = CesiumMath.nextPowerOfTwo(32); // 32
548571
*/
549-
CesiumMath.isPowerOfTwo = function(n) {
572+
CesiumMath.nextPowerOfTwo = function(n) {
550573
if (typeof n !== 'number' || n < 0) {
551574
throw new DeveloperError('A number greater than or equal to 0 is required.');
552575
}
553576

554-
var m = defaultValue(n, 0);
555-
return (m !== 0) && ((m & (m - 1)) === 0);
577+
// From http://graphics.stanford.edu/~seander/bithacks.html#RoundUpPowerOf2
578+
--n;
579+
n |= n >> 1;
580+
n |= n >> 2;
581+
n |= n >> 4;
582+
n |= n >> 8;
583+
n |= n >> 16;
584+
++n;
585+
586+
return n;
556587
};
557588

558589
/**

Specs/Core/MathSpec.js

+24
Original file line numberDiff line numberDiff line change
@@ -140,6 +140,13 @@ defineSuite([
140140
expect(CesiumMath.isPowerOfTwo(12)).toEqual(false);
141141
});
142142

143+
it('nextPowerOfTwo finds next power of two', function() {
144+
expect(CesiumMath.nextPowerOfTwo(0)).toEqual(0);
145+
expect(CesiumMath.nextPowerOfTwo(257)).toEqual(512);
146+
expect(CesiumMath.nextPowerOfTwo(512)).toEqual(512);
147+
expect(CesiumMath.nextPowerOfTwo(1023)).toEqual(1024);
148+
});
149+
143150
it('factorial throws for non-numbers', function() {
144151
expect(function() {
145152
CesiumMath.factorial({});
@@ -185,4 +192,21 @@ defineSuite([
185192
}).toThrow();
186193
});
187194

195+
it('nextPowerOfTwo throws for non-numbers', function() {
196+
expect(function() {
197+
CesiumMath.nextPowerOfTwo({});
198+
}).toThrow();
199+
});
200+
201+
it('nextPowerOfTwo throws for negative numbers', function() {
202+
expect(function() {
203+
CesiumMath.nextPowerOfTwo(-1);
204+
}).toThrow();
205+
});
206+
207+
it('nextPowerOfTwo throws for undefined', function() {
208+
expect(function() {
209+
CesiumMath.nextPowerOfTwo();
210+
}).toThrow();
211+
});
188212
});

0 commit comments

Comments
 (0)