Add portable ctz, popcnt, rotl, rotr, nearest & copysign (#75)

Thank you!
This commit is contained in:
Max Graey 2018-04-15 01:35:43 +03:00 committed by Daniel Wirtz
parent 990fa850ff
commit d66f9d205c
2 changed files with 45 additions and 0 deletions

12
std/portable.d.ts vendored
View File

@ -135,16 +135,28 @@ declare namespace f64 {
/** Performs the sign-agnostic count leading zero bits operation on a 32-bit integer. All zero bits are considered leading if the value is zero. */
declare function clz<T = i32>(value: T): T;
/** Performs the sign-agnostic count tailing zero bits operation on a 32-bit integer. All zero bits are considered trailing if the value is zero. */
declare function ctz<T = i32>(value: T): T;
/** Performs the sign-agnostic count number of one bits operation on a 32-bit integer. */
declare function popcnt<T = i32>(value: T): T;
/** Performs the sign-agnostic rotate left operation on a 32-bit integer. */
declare function rotl<T = i32>(value: T, shift: T): T;
/** Performs the sign-agnostic rotate right operation on a 32-bit integer. */
declare function rotr<T = i32>(value: T, shift: T): T;
/** Computes the absolute value of an integer or float. */
declare function abs<T = i32 | f32 | f64>(value: T): T;
/** Determines the maximum of two integers or floats. If either operand is `NaN`, returns `NaN`. */
declare function max<T = i32 | f32 | f64>(left: T, right: T): T;
/** Determines the minimum of two integers or floats. If either operand is `NaN`, returns `NaN`. */
declare function min<T = i32 | f32 | f64>(left: T, right: T): T;
/** Composes a 32-bit or 64-bit float from the magnitude of `x` and the sign of `y`. */
declare function copysign<T = f32 | f64>(x: T, y: T): T;
/** Performs the ceiling operation on a 32-bit or 64-bit float. */
declare function ceil<T = f32 | f64>(value: T): T;
/** Performs the floor operation on a 32-bit or 64-bit float. */
declare function floor<T = f32 | f64>(value: T): T;
/** Rounds to the nearest integer tied to even of a 32-bit or 64-bit float. */
declare function nearest<T = f32 | f64>(value: T): T;
/** Selects one of two pre-evaluated values depending on the condition. */
declare function select<T>(ifTrue: T, ifFalse: T, condition: bool): T;
/** Calculates the square root of a 32-bit or 64-bit float. */

View File

@ -75,6 +75,27 @@ Object.defineProperties(
globalScope["clz"] = Math.clz32;
globalScope["ctz"] = function ctz(value) {
var c = Math.clz32(value & -value);
return value ? 31 - c : c;
};
globalScope["popcnt"] = function popcnt(value) {
value -= value >>> 1 & 0x55555555;
value = (value & 0x33333333) + (value >>> 2 & 0x33333333);
return (((value + (value >>> 4)) & 0x0F0F0F0F) * 0x01010101) >>> 24;
};
globalScope["rotl"] = function rotl(value, shift) {
shift &= 31;
return (value << shift) | (value >>> (32 - shift));
};
globalScope["rotr"] = function rotr(value, shift) {
shift &= 31;
return (value >>> shift) | (value << (32 - shift));
};
globalScope["abs"] = Math.abs;
globalScope["max"] = Math.max;
@ -85,6 +106,14 @@ globalScope["ceil"] = Math.ceil;
globalScope["floor"] = Math.floor;
// Adopt code from https://github.com/rfk/wasm-polyfill
globalScope["nearest"] = function nearest(value) {
if (Math.abs(value - Math.trunc(value)) === 0.5) {
return 2.0 * Math.round(value * 0.5);
}
return Math.round(value);
};
globalScope["select"] = function select(ifTrue, ifFalse, condition) {
return condition ? ifTrue : ifFalse;
};
@ -93,6 +122,10 @@ globalScope["sqrt"] = Math.sqrt;
globalScope["trunc"] = Math.trunc;
globalScope["copysign"] = function copysign(x, y) {
return Math.abs(x) * Math.sign(y);
};
globalScope["bswap"] = function bswap(value) {
var a = value >> 8 & 0x00FF00FF;
var b = (value & 0x00FF00FF) << 8;