Skip to content

Commit c496345

Browse files
MaxGraeyWillem Wyndham
authored and
Willem Wyndham
committed
Add Math.signbit (AssemblyScript#333)
1 parent c0706e3 commit c496345

File tree

7 files changed

+4779
-4283
lines changed

7 files changed

+4779
-4283
lines changed

std/assembly/index.d.ts

+2
Original file line numberDiff line numberDiff line change
@@ -908,6 +908,8 @@ interface IMath<T> {
908908
round(x: T): T;
909909
/** Returns the sign of `x`, indicating whether the number is positive, negative or zero. */
910910
sign(x: T): T;
911+
/** Returns whether the sign bit of `x` is set */
912+
signbit(x: T): bool;
911913
/** Returns the sine of `x`. */
912914
sin(x: T): T;
913915
/** Returns the hyperbolic sine of `x`. */

std/assembly/math.ts

+12
Original file line numberDiff line numberDiff line change
@@ -992,6 +992,13 @@ export namespace NativeMath {
992992
}
993993
}
994994

995+
@inline
996+
export function signbit(x: f64): bool {
997+
// In ECMAScript all NaN values are indistinguishable from each other
998+
// so we need handle NaN and negative NaN in similar way
999+
return <bool>(<i32>(reinterpret<u64>(x) >>> 63) & (x == x));
1000+
}
1001+
9951002
export function sin(x: f64): f64 { // TODO
9961003
unreachable();
9971004
return 0;
@@ -2046,6 +2053,11 @@ export namespace NativeMathf {
20462053
}
20472054
}
20482055

2056+
@inline
2057+
export function signbit(x: f32): bool {
2058+
return <bool>((reinterpret<u32>(x) >>> 31) & (x == x));
2059+
}
2060+
20492061
export function sin(x: f32): f32 { // TODO
20502062
unreachable();
20512063
return 0;

std/portable/index.d.ts

+1
Original file line numberDiff line numberDiff line change
@@ -557,6 +557,7 @@ interface IMath {
557557
random(): f64;
558558
round(x: f64): f64;
559559
sign(x: f64): f64;
560+
signbit(x: f64): bool;
560561
sin(x: f64): f64;
561562
sinh(x: f64): f64;
562563
sqrt(x: f64): f64;

std/portable/index.js

+7-1
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,9 @@ var globalScope = typeof window !== "undefined" && window || typeof global !== "
44

55
globalScope.ASC_TARGET = 0;
66

7+
var F64 = new Float64Array(1);
8+
var U64 = new Uint32Array(F64.buffer);
9+
710
Object.defineProperties(
811
globalScope["i8"] = function i8(value) { return value << 24 >> 24; }
912
, {
@@ -210,7 +213,7 @@ globalScope["isString"] = function isString(arg) {
210213

211214
globalScope["isArray"] = Array.isArray;
212215

213-
globalScope["unchecked"] = function(expr) {
216+
globalScope["unchecked"] = function unchecked(expr) {
214217
return expr;
215218
};
216219

@@ -223,6 +226,9 @@ globalScope["fmodf"] = function fmodf(x, y) {
223226
};
224227

225228
globalScope["JSMath"] = Math;
229+
globalScope["JSMath"].signbit = function signbit(x) {
230+
F64[0] = x; return Boolean((U64[1] >>> 31) & (x == x));
231+
}
226232

227233
globalScope["memory"] = (() => {
228234
var HEAP = new Uint8Array(0);

0 commit comments

Comments
 (0)