Even more math (#56)

Remaining implementations of JavaScript's Math functions (except sin/cos/tan), both double (Math) and single (Mathf) precision, ported from musl incl. tests from libc-test, plus some changes to match JS semantics. Also binds fmod to `%` and pow to `**`.
This commit is contained in:
Daniel Wirtz
2018-03-30 17:25:54 +02:00
committed by GitHub
parent e47a130771
commit 164f134053
29 changed files with 143290 additions and 15143 deletions

View File

@ -80,25 +80,25 @@ export declare function abort(
export declare function i8(value: void): i8;
export namespace i8 {
export const MIN_VALUE: i8 = -128;
export const MAX_VALUE: i8 = 127;
export const MAX_VALUE: i8 = 127;
}
export declare function i16(value: void): i16;
export namespace i16 {
export const MIN_VALUE: i16 = -32768;
export const MAX_VALUE: i16 = 32767;
export const MAX_VALUE: i16 = 32767;
}
export declare function i32(value: void): i32;
export namespace i32 {
export const MIN_VALUE: i32 = -2147483648;
export const MAX_VALUE: i32 = 2147483647;
export const MAX_VALUE: i32 = 2147483647;
}
export declare function i64(value: void): i64;
export namespace i64 {
export const MIN_VALUE: i64 = -9223372036854775808;
export const MAX_VALUE: i64 = 9223372036854775807;
export const MAX_VALUE: i64 = 9223372036854775807;
}
export declare function isize(value: void): isize;
@ -151,144 +151,24 @@ export namespace bool {
export declare function f32(value: void): f32;
export namespace f32 {
export const MIN_VALUE: f32 = -3.40282347e+38;
export const MAX_VALUE: f32 = 3.40282347e+38;
export const MIN_POSITIVE_VALUE: f32 = 1.175494351e-38;
export const MIN_VALUE = reinterpret<f32>(0xFF7FFFFF); // -0x1.fffffep+127f
export const MAX_VALUE = reinterpret<f32>(0x7F7FFFFF); // 0x1.fffffep+127f
export const MIN_POSITIVE_VALUE = reinterpret<f32>(0x00800000); // 0x1p-126f
export const MIN_SAFE_INTEGER: f32 = -16777215;
export const MAX_SAFE_INTEGER: f32 = 16777215;
export const EPSILON: f32 = 1.19209290e-07;
export const MAX_SAFE_INTEGER: f32 = 16777215;
export const EPSILON = reinterpret<f32>(0x34000000); // 0x1p-23f
}
export declare function f64(value: void): f64;
export namespace f64 {
export const MIN_VALUE: f64 = -1.7976931348623157e+308;
export const MAX_VALUE: f64 = 1.7976931348623157e+308;
export const MIN_POSITIVE_VALUE: f64 = 2.2250738585072014e-308;
export const MIN_VALUE = reinterpret<f64>(0xFFEFFFFFFFFFFFFF); // -0x1.fffffffffffffp+1023
export const MAX_VALUE = reinterpret<f64>(0x7FEFFFFFFFFFFFFF); // 0x1.fffffffffffffp+1023
export const MIN_POSITIVE_VALUE = reinterpret<f64>(0x0010000000000000); // 0x1p-1022
export const MIN_SAFE_INTEGER: f64 = -9007199254740991;
export const MAX_SAFE_INTEGER: f64 = 9007199254740991;
export const EPSILON: f64 = 2.2204460492503131e-16;
export const MAX_SAFE_INTEGER: f64 = 9007199254740991;
export const EPSILON = reinterpret<f64>(0x3CB0000000000000); // 0x1p-52
}
export declare const HEAP_BASE: usize;
export declare function start(): void;
export function fmod(x: f64, y: f64): f64 {
// based on musl's implementation of fmod
var ux = reinterpret<u64>(x);
var uy = reinterpret<u64>(y);
var ex = <i32>(ux >> 52 & 0x7ff);
var ey = <i32>(uy >> 52 & 0x7ff);
var sx = <i32>(ux >> 63);
if (uy << 1 == 0 || isNaN<f64>(y) || ex == 0x7ff) {
return (x * y) / (x * y);
}
if (ux << 1 <= uy << 1) {
if (ux << 1 == uy << 1) return 0 * x;
return x;
}
// normalize x and y
var i: u64;
if (!ex) {
for (i = ux << 12; !(i >> 63); i <<= 1) --ex;
ux <<= -ex + 1;
} else {
ux &= <u64>-1 >> 12;
ux |= 1 << 52;
}
if (!ey) {
for (i = uy << 12; !(i >> 63); i <<= 1) --ey;
uy <<= -ey + 1;
} else {
uy &= <u64>-1 >> 12;
uy |= 1 << 52;
}
// x mod y
for (; ex > ey; ex--) {
i = ux - uy;
if (!(i >> 63)) {
if (!i) return 0 * x;
ux = i;
}
ux <<= 1;
}
i = ux - uy;
if (!(i >> 63)) {
if (!i) return 0 * x;
ux = i;
}
for (; !(ux >> 52); ux <<= 1) --ex;
// scale result
if (ex > 0) {
ux -= 1 << 52;
ux |= <u64>ex << 52;
} else {
ux >>= -ex + 1;
}
ux |= <u64>sx << 63;
return reinterpret<f64>(ux);
}
export function fmodf(x: f32, y: f32): f32 {
// based on musl's implementation of fmodf
var ux = reinterpret<u32>(x);
var uy = reinterpret<u32>(y);
var ex = <i32>(ux >> 23 & 0xff);
var ey = <i32>(uy >> 23 & 0xff);
var sx = ux & 0x80000000;
if (uy << 1 == 0 || isNaN<f32>(y) || ex == 0xff) {
return (x * y) / (x * y);
}
if (ux << 1 <= uy << 1) {
if (ux << 1 == uy << 1) return 0 * x;
return x;
}
// normalize x and y
var i: u32;
if (!ex) {
for (i = ux << 9; !(i >> 31); i <<= 1) --ex;
ux <<= -ex + 1;
} else {
ux &= <u32>-1 >> 9;
ux |= 1 << 23;
}
if (!ey) {
for (i = uy << 9; !(i >> 31); i <<= 1) --ey;
uy <<= -ey + 1;
} else {
uy &= <u32>-1 >> 9;
uy |= 1 << 23;
}
// x mod y
for (; ex > ey; --ex) {
i = ux - uy;
if (!(i >> 31)) {
if (!i) return 0 * x;
ux = i;
}
ux <<= 1;
}
i = ux - uy;
if (!(i >> 31)) {
if (!i) return 0 * x;
ux = i;
}
for (; !(ux >> 23); ux <<= 1) --ex;
// scale result
if (ex > 0) {
ux -= 1 << 23;
ux |= <u32>ex << 23;
} else {
ux >>= -ex + 1;
}
ux |= sx;
return reinterpret<f32>(ux);
}