Add Math.signbit (#333)

This commit is contained in:
Max Graey
2018-11-18 12:46:48 +02:00
committed by Daniel Wirtz
parent 1928404f3b
commit 4944280cdc
7 changed files with 14830 additions and 14334 deletions

View File

@ -762,6 +762,8 @@ interface IMath<T> {
round(x: T): T;
/** Returns the sign of `x`, indicating whether the number is positive, negative or zero. */
sign(x: T): T;
/** Returns whether the sign bit of `x` is set */
signbit(x: T): bool;
/** Returns the sine of `x`. */
sin(x: T): T;
/** Returns the hyperbolic sine of `x`. */

View File

@ -992,6 +992,13 @@ export namespace NativeMath {
}
}
@inline
export function signbit(x: f64): bool {
// In ECMAScript all NaN values are indistinguishable from each other
// so we need handle NaN and negative NaN in similar way
return <bool>(<i32>(reinterpret<u64>(x) >>> 63) & (x == x));
}
export function sin(x: f64): f64 { // TODO
unreachable();
return 0;
@ -2046,6 +2053,11 @@ export namespace NativeMathf {
}
}
@inline
export function signbit(x: f32): bool {
return <bool>((reinterpret<u32>(x) >>> 31) & (x == x));
}
export function sin(x: f32): f32 { // TODO
unreachable();
return 0;

View File

@ -556,6 +556,7 @@ interface IMath {
random(): f64;
round(x: f64): f64;
sign(x: f64): f64;
signbit(x: f64): bool;
sin(x: f64): f64;
sinh(x: f64): f64;
sqrt(x: f64): f64;

View File

@ -4,6 +4,9 @@ var globalScope = typeof window !== "undefined" && window || typeof global !== "
globalScope.ASC_TARGET = 0;
var F64 = new Float64Array(1);
var U64 = new Uint32Array(F64.buffer);
Object.defineProperties(
globalScope["i8"] = function i8(value) { return value << 24 >> 24; }
, {
@ -210,7 +213,7 @@ globalScope["isString"] = function isString(arg) {
globalScope["isArray"] = Array.isArray;
globalScope["unchecked"] = function(expr) {
globalScope["unchecked"] = function unchecked(expr) {
return expr;
};
@ -223,6 +226,9 @@ globalScope["fmodf"] = function fmodf(x, y) {
};
globalScope["JSMath"] = Math;
globalScope["JSMath"].signbit = function signbit(x) {
F64[0] = x; return Boolean((U64[1] >>> 31) & (x == x));
}
globalScope["memory"] = (() => {
var HEAP = new Uint8Array(0);