Skip to content

Commit f841f0f

Browse files
MaxGraeydcodeIO
authored andcommitted
Add portable sincos polyfill (AssemblyScript#529)
1 parent 783dd32 commit f841f0f

File tree

3 files changed

+48
-15
lines changed

3 files changed

+48
-15
lines changed

std/assembly/math.ts

Lines changed: 14 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -2329,18 +2329,21 @@ export namespace NativeMathf {
23292329
}
23302330
return tan_kernf(x, 0);
23312331
}
2332-
if (ix <= 0x407b53d1) { /* |x| ~<= 5π/4 */
2333-
if (ix <= 0x4016cbe3) { /* |x| ~<= 3π/4 */
2334-
return tan_kernf((sign ? x + t1pio2 : x - t1pio2), 1);
2335-
} else {
2336-
return tan_kernf((sign ? x + t2pio2 : x - t2pio2), 0);
2332+
2333+
if (ASC_SHRINK_LEVEL < 1) {
2334+
if (ix <= 0x407b53d1) { /* |x| ~<= 5π/4 */
2335+
if (ix <= 0x4016cbe3) { /* |x| ~<= 3π/4 */
2336+
return tan_kernf((sign ? x + t1pio2 : x - t1pio2), 1);
2337+
} else {
2338+
return tan_kernf((sign ? x + t2pio2 : x - t2pio2), 0);
2339+
}
23372340
}
2338-
}
2339-
if (ix <= 0x40e231d5) { /* |x| ~<= /4 */
2340-
if (ix <= 0x40afeddf) { /* |x| ~<= 7π/4 */
2341-
return tan_kernf((sign ? x + t3pio2 : x - t3pio2), 1);
2342-
} else {
2343-
return tan_kernf((sign ? x + t4pio2 : x - t4pio2), 0);
2341+
if (ix <= 0x40e231d5) { /* |x| ~<= 9π/4 */
2342+
if (ix <= 0x40afeddf) { /* |x| ~<= /4 */
2343+
return tan_kernf((sign ? x + t3pio2 : x - t3pio2), 1);
2344+
} else {
2345+
return tan_kernf((sign ? x + t4pio2 : x - t4pio2), 0);
2346+
}
23442347
}
23452348
}
23462349

std/portable/index.d.ts

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -100,13 +100,17 @@ declare function isFinite<T = f32 | f64>(value: T): bool;
100100
declare function isInteger(value: any): value is number;
101101
/** Tests if the specified value is a valid float. Can't distinguish a float from an integer. */
102102
declare function isFloat(value: any): value is number;
103+
/** Tests if the specified value is of a nullable reference type. */
104+
declare function isNullable(value: any): bool;
103105
/** Tests if the specified value is of a reference type. */
104106
declare function isReference(value: any): value is object | string;
107+
/** Tests if the specified value is of a function type */
108+
declare function isFunction(value: any): value is Function;
105109
/** Tests if the specified value can be used as a string. */
106110
declare function isString(value: any): value is string | String;
107111
/** Tests if the specified value can be used as an array. */
108112
declare function isArray(value: any): value is Array<any>;
109-
/** Tests if the specified type *or* expression can be used as an array like object. Compiles to a constant. */
113+
/** Tests if the specified type *or* expression can be used as an array like object. */
110114
declare function isArrayLike(value: any): value is ArrayLike<any>;
111115
/** Tests if the specified expression resolves to a defined element. */
112116
declare function isDefined(expression: any): bool;
@@ -567,6 +571,10 @@ interface IMath {
567571
readonly PI: f64;
568572
readonly SQRT1_2: f64;
569573
readonly SQRT2: f64;
574+
575+
sincos_sin: f64;
576+
sincos_cos: f64;
577+
570578
abs(x: f64): f64;
571579
acos(x: f64): f64;
572580
acosh(x: f64): f64;
@@ -598,6 +606,7 @@ interface IMath {
598606
sign(x: f64): f64;
599607
signbit(x: f64): bool;
600608
sin(x: f64): f64;
609+
sincos(x: f64): f64;
601610
sinh(x: f64): f64;
602611
sqrt(x: f64): f64;
603612
tan(x: f64): f64;

std/portable/index.js

Lines changed: 24 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -206,10 +206,18 @@ globalScope["isFloat"] = function isFloat(arg) {
206206
return typeof arg === "number";
207207
};
208208

209+
globalScope["isNullable"] = function isNullable(arg) {
210+
return true;
211+
}
212+
209213
globalScope["isReference"] = function isReference(arg) {
210214
return typeof arg === "object" || typeof arg === "string";
211215
};
212216

217+
globalScope["isFunction"] = function isFunction(arg) {
218+
return typeof arg === "function";
219+
}
220+
213221
globalScope["isString"] = function isString(arg) {
214222
return typeof arg === "string" || arg instanceof String;
215223
};
@@ -244,9 +252,22 @@ globalScope["fmodf"] = function fmodf(x, y) {
244252
};
245253

246254
globalScope["JSMath"] = Math;
247-
globalScope["JSMath"].signbit = function signbit(x) {
248-
F64[0] = x; return Boolean((U64[1] >>> 31) & (x == x));
249-
}
255+
256+
Object.defineProperties(globalScope["JSMath"], {
257+
sincos_sin: { value: 0.0, writable: true },
258+
sincos_cos: { value: 0.0, writable: true },
259+
signbit: {
260+
value: function signbit(x) {
261+
F64[0] = x; return Boolean((U64[1] >>> 31) & (x == x));
262+
}
263+
},
264+
sincos: {
265+
value: function sincos(x) {
266+
this.sincos_sin = Math.sin(x);
267+
this.sincos_cos = Math.cos(x);
268+
}
269+
}
270+
});
250271

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

0 commit comments

Comments
 (0)