mirror of
https://github.com/fluencelabs/assemblyscript
synced 2025-04-25 07:02:13 +00:00
Add Math.signbit (#333)
This commit is contained in:
parent
1928404f3b
commit
4944280cdc
2
std/assembly/index.d.ts
vendored
2
std/assembly/index.d.ts
vendored
@ -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`. */
|
||||
|
@ -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;
|
||||
|
1
std/portable/index.d.ts
vendored
1
std/portable/index.d.ts
vendored
@ -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;
|
||||
|
@ -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);
|
||||
|
File diff suppressed because it is too large
Load Diff
@ -33,15 +33,11 @@
|
||||
const js = true; // also test, and thus compare to, JS math?
|
||||
|
||||
// these flags are unused, but kept in case these might just so happen to become useful
|
||||
const INEXACT = 1 << 0;
|
||||
const INVALID = 1 << 1;
|
||||
const INEXACT = 1 << 0;
|
||||
const INVALID = 1 << 1;
|
||||
const DIVBYZERO = 1 << 2;
|
||||
const UNDERFLOW = 1 << 3;
|
||||
const OVERFLOW = 1 << 4;
|
||||
|
||||
function signbit(d: f64): i32 {
|
||||
return <i32>(reinterpret<u64>(d) >> 63);
|
||||
}
|
||||
const OVERFLOW = 1 << 4;
|
||||
|
||||
function eulp(x: f64): i32 {
|
||||
var u = reinterpret<u64>(x);
|
||||
@ -54,7 +50,7 @@ function ulperr(got: f64, want: f64, dwant: f64): f64 {
|
||||
const Ox1p1023 = reinterpret<f64>(0x7FE0000000000000);
|
||||
if (isNaN(got) && isNaN(want)) return 0;
|
||||
if (got == want) {
|
||||
if (signbit(got) == signbit(want)) return dwant;
|
||||
if (NativeMath.signbit(got) == NativeMath.signbit(want)) return dwant;
|
||||
return Infinity;
|
||||
}
|
||||
if (!isFinite(got)) {
|
||||
@ -64,10 +60,6 @@ function ulperr(got: f64, want: f64, dwant: f64): f64 {
|
||||
return NativeMath.scalbn(got - want, -eulp(want)) + dwant;
|
||||
}
|
||||
|
||||
function signbitf(f: f32): i32 {
|
||||
return <i32>(reinterpret<u32>(f) >> 31);
|
||||
}
|
||||
|
||||
function eulpf(x: f32): i32 {
|
||||
var u = reinterpret<u32>(x);
|
||||
var e = <i32>(u >> 23 & 0xff);
|
||||
@ -79,7 +71,7 @@ function ulperrf(got: f32, want: f32, dwant: f32): f32 {
|
||||
const Ox1p127f = reinterpret<f32>(0x7F000000);
|
||||
if (isNaN(got) && isNaN(want)) return 0;
|
||||
if (got == want) {
|
||||
if (signbitf(got) == signbitf(want)) return dwant;
|
||||
if (NativeMathf.signbit(got) == NativeMathf.signbit(want)) return dwant;
|
||||
return Infinity;
|
||||
}
|
||||
if (!isFinite(got)) {
|
||||
@ -2651,6 +2643,32 @@ assert(test_signf(Infinity, 1.0, 0.0, 0));
|
||||
assert(test_signf(-Infinity, -1.0, 0.0, 0));
|
||||
assert(test_signf(NaN, NaN, 0.0, 0));
|
||||
|
||||
////////////////////////////////////////////////////////////////////////////////////////////////////
|
||||
// Math.signbit
|
||||
////////////////////////////////////////////////////////////////////////////////////////////////////
|
||||
|
||||
assert(NativeMath.signbit(0.0) == false);
|
||||
assert(NativeMath.signbit(-0.0) == true);
|
||||
assert(NativeMath.signbit(1.0) == false);
|
||||
assert(NativeMath.signbit(-1.0) == true);
|
||||
assert(NativeMath.signbit(+NaN) == false);
|
||||
assert(NativeMath.signbit(-NaN) == false);
|
||||
assert(NativeMath.signbit(+Infinity) == false);
|
||||
assert(NativeMath.signbit(-Infinity) == true);
|
||||
|
||||
////////////////////////////////////////////////////////////////////////////////////////////////////
|
||||
// Mathf.signbit
|
||||
////////////////////////////////////////////////////////////////////////////////////////////////////
|
||||
|
||||
assert(NativeMathf.signbit(0.0) == false);
|
||||
assert(NativeMathf.signbit(-0.0) == true);
|
||||
assert(NativeMathf.signbit(1.0) == false);
|
||||
assert(NativeMathf.signbit(-1.0) == true);
|
||||
assert(NativeMathf.signbit(+NaN) == false);
|
||||
assert(NativeMathf.signbit(-NaN) == false);
|
||||
assert(NativeMathf.signbit(+Infinity) == false);
|
||||
assert(NativeMathf.signbit(-Infinity) == true);
|
||||
|
||||
////////////////////////////////////////////////////////////////////////////////////////////////////
|
||||
// Math.rem
|
||||
////////////////////////////////////////////////////////////////////////////////////////////////////
|
||||
|
File diff suppressed because it is too large
Load Diff
Loading…
x
Reference in New Issue
Block a user