Skip to content

Commit d9cba69

Browse files
committed
add Math.signbit
1 parent a79db87 commit d9cba69

File tree

7 files changed

+4635
-4283
lines changed

7 files changed

+4635
-4283
lines changed

std/assembly/index.d.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -736,6 +736,8 @@ interface IMath<T> {
736736
round(x: T): T;
737737
/** Returns the sign of `x`, indicating whether the number is positive, negative or zero. */
738738
sign(x: T): T;
739+
/** Returns whether the sign bit of `x` is set */
740+
signbit(x: T): bool;
739741
/** Returns the sine of `x`. */
740742
sin(x: T): T;
741743
/** Returns the hyperbolic sine of `x`. */

std/assembly/math.ts

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -992,6 +992,11 @@ export namespace NativeMath {
992992
}
993993
}
994994

995+
@inline
996+
export function signbit(x: f64): bool {
997+
return <bool>(reinterpret<u64>(x) >>> 63);
998+
}
999+
9951000
export function sin(x: f64): f64 { // TODO
9961001
unreachable();
9971002
return 0;
@@ -2046,6 +2051,11 @@ export namespace NativeMathf {
20462051
}
20472052
}
20482053

2054+
@inline
2055+
export function signbit(x: f32): bool {
2056+
return <bool>(reinterpret<u32>(x) >>> 31);
2057+
}
2058+
20492059
export function sin(x: f32): f32 { // TODO
20502060
unreachable();
20512061
return 0;

std/portable/index.d.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -517,6 +517,7 @@ interface IMath {
517517
random(): f64;
518518
round(x: f64): f64;
519519
sign(x: f64): f64;
520+
signbit(x: f64): bool;
520521
sin(x: f64): f64;
521522
sinh(x: f64): f64;
522523
sqrt(x: f64): f64;

std/portable/index.js

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -210,7 +210,7 @@ globalScope["isString"] = function isString(arg) {
210210

211211
globalScope["isArray"] = Array.isArray;
212212

213-
globalScope["unchecked"] = function(expr) {
213+
globalScope["unchecked"] = function unchecked(expr) {
214214
return expr;
215215
};
216216

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

225225
globalScope["JSMath"] = Math;
226+
globalScope["JSMath"].signbit = function signbit(x) {
227+
return (x = +x) != x ? x : x == 0 ? 1 / x == Infinity : x > 0;
228+
}
226229

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

0 commit comments

Comments
 (0)