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

18
std/assembly.d.ts vendored
View File

@ -135,6 +135,10 @@ declare namespace f32 {
export const MAX_SAFE_INTEGER: f32;
/** Difference between 1 and the smallest representable value greater than 1. */
export const EPSILON: f32;
/** Returns the floating-point remainder of `x / y` (rounded towards zero). */
export function mod(x: f32, y: f32): f32;
/** Returns the floating-point remainder of `x / y` (rounded to nearest). */
export function rem(x: f32, y: f32): f32;
}
/** Converts any other numeric value to a 64-bit float. */
declare function f64(value: i8 | i16 | i32 | i64 | isize | u8 | u16 | u32 | u64 | usize | bool | f32 | f64): f64;
@ -391,7 +395,7 @@ interface IMath<T> {
/** Returns the smallest integer greater than or equal to `x`. */
ceil(x: T): T;
/** Returns the number of leading zero bits in the 32-bit binary representation of `x`. */
clz32(x: T): i32;
clz32(x: T): T;
/** Returns the cosine (in radians) of `x`. */
cos(x: T): T;
/** Returns the hyperbolic cosine of `x`. */
@ -407,7 +411,7 @@ interface IMath<T> {
/** Returns the square root of the sum of squares of its arguments. */
hypot(value1: T, value2: T): T; // TODO: rest
/** Returns the result of the C-like 32-bit multiplication of `a` and `b`. */
imul(a: T, b: T): i32;
imul(a: T, b: T): T;
/** Returns the natural logarithm (base e) of `x`. */
log(x: T): T;
/** Returns the base 10 logarithm of `x`. */
@ -442,17 +446,21 @@ interface IMath<T> {
trunc(x: T): T;
}
interface ISeedRandom {
interface INativeMath<T> extends IMath<T> {
/** Seeds the random number generator. */
seedRandom(value: i64): void;
/** Returns the floating-point remainder of `x / y` (rounded towards zero). */
mod(x: T, y: T): T;
/** Returns the floating-point remainder of `x / y` (rounded to nearest). */
rem(x: T, y: T): T;
}
/** Double precision math imported from JavaScript. */
declare const JSMath: IMath<f64>;
/** Double precision math implemented natively. */
declare const NativeMath: IMath<f64> & ISeedRandom;
declare const NativeMath: INativeMath<f64>;
/** Single precision math implemented natively. */
declare const NativeMathf: IMath<f32>;
declare const NativeMathf: INativeMath<f32>;
/** Alias of {@link NativeMath} or {@link JSMath} respectively. Defaults to `NativeMath`. */
declare const Math: IMath<f64>;

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);
}

File diff suppressed because it is too large Load Diff

View File

@ -444,6 +444,7 @@ function parse<T>(str: String, radix: i32 = 0): T {
return sign * num;
}
// FIXME: naive implementation
export function parseFloat(str: String): f64 {
var len: i32 = str.length;
if (!len) {