Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

add Math.signbit #333

Merged
merged 5 commits into from
Nov 18, 2018
Merged
Show file tree
Hide file tree
Changes from 4 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions std/assembly/index.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -736,6 +736,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`. */
Expand Down
12 changes: 12 additions & 0 deletions std/assembly/math.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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 != 0) & <i32>(x == x));
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Similar to the f32 version, the != 0 instruction can be saved here as well.

Copy link
Member Author

@MaxGraey MaxGraey Nov 17, 2018

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm not sure. Because without != 0 u64 will be cast (extend op code) to i32 anyway

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Oh, actually != 0 doesn't matter because it has to convert from 64 to 32 bits anyway.

}

export function sin(x: f64): f64 { // TODO
unreachable();
return 0;
Expand Down Expand Up @@ -2046,6 +2053,11 @@ export namespace NativeMathf {
}
}

@inline
export function signbit(x: f32): bool {
return <bool>((reinterpret<u32>(x) >>> 31) & <i32>(x == x));
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This looks like it might result in an unnecessary & 1 to convert from i32 to bool. A better way to write it is: <bool>(reinterpret<u32>(x) >>> 31) & (x == x), as it keeps type conversions to a minimum.

}

export function sin(x: f32): f32 { // TODO
unreachable();
return 0;
Expand Down
1 change: 1 addition & 0 deletions std/portable/index.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -517,6 +517,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;
Expand Down
8 changes: 7 additions & 1 deletion std/portable/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -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; }
, {
Expand Down Expand Up @@ -210,7 +213,7 @@ globalScope["isString"] = function isString(arg) {

globalScope["isArray"] = Array.isArray;

globalScope["unchecked"] = function(expr) {
globalScope["unchecked"] = function unchecked(expr) {
return expr;
};

Expand All @@ -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);
Expand Down
Loading