Add more numeric builtins (#330)

This commit is contained in:
Max Graey 2018-11-12 08:42:28 +02:00 committed by Daniel Wirtz
parent 3f9758f35a
commit 4e89456dcb
5 changed files with 1510 additions and 7 deletions

View File

@ -46,12 +46,16 @@
export namespace i8 {
export const MIN_VALUE: i8 = -128;
export const MAX_VALUE: i8 = 127;
@inline export function parseInt(value: string, radix: i32 = 0): i8 { return <i8>parseI32(value, radix) }
@inline export function parseFloat(value: string): i8 { return <i8>parseFloat(value) }
}
@builtin export declare function i16(value: void): i16;
export namespace i16 {
export const MIN_VALUE: i16 = -32768;
export const MAX_VALUE: i16 = 32767;
@inline export function parseInt(value: string, radix: i32 = 0): i16 { return <i16>parseI32(value, radix) }
@inline export function parseFloat(value: string): i16 { return <i16>parseFloat(value) }
}
@builtin export declare function i32(value: void): i32;
@ -72,6 +76,8 @@ export namespace i32 {
@builtin export declare function store8(offset: usize, value: i32, constantOffset?: usize): void;
@builtin export declare function store16(offset: usize, value: i32, constantOffset?: usize): void;
@builtin export declare function store(offset: usize, value: i32, constantOffset?: usize): void;
@inline export function parseInt(value: string, radix: i32 = 0): i32 { return <i32>parseI32(value, radix) }
@inline export function parseFloat(value: string): i32 { return <i32>parseFloat(value) }
}
@builtin export declare function i64(value: void): i64;
@ -95,6 +101,8 @@ export namespace i64 {
@builtin export declare function store16(offset: usize, value: i64, constantOffset?: usize): void;
@builtin export declare function store32(offset: usize, value: i64, constantOffset?: usize): void;
@builtin export declare function store(offset: usize, value: i64, constantOffset?: usize): void;
@inline export function parseInt(value: string, radix: i32 = 0): i64 { return <i64>parseI64(value, radix) }
@inline export function parseFloat(value: string): i64 { return <i64>parseFloat(value) }
}
@builtin export declare function isize(value: void): isize;
@ -105,30 +113,40 @@ export namespace isize {
export const MAX_VALUE: isize = sizeof<i32>() == sizeof<isize>()
? 2147483647
: <isize>9223372036854775807;
@inline export function parseInt(value: string, radix: i32 = 0): isize { return <isize>parseI64(value, radix) }
@inline export function parseFloat(value: string): isize { return <isize>parseFloat(value) }
}
@builtin export declare function u8(value: void): u8;
export namespace u8 {
export const MIN_VALUE: u8 = 0;
export const MAX_VALUE: u8 = 255;
@inline export function parseInt(value: string, radix: i32 = 0): u8 { return <u8>parseI32(value, radix) }
@inline export function parseFloat(value: string): u8 { return <u8>parseFloat(value) }
}
@builtin export declare function u16(value: void): u16;
export namespace u16 {
export const MIN_VALUE: u16 = 0;
export const MAX_VALUE: u16 = 65535;
@inline export function parseInt(value: string, radix: i32 = 0): u16 { return <u16>parseI32(value, radix) }
@inline export function parseFloat(value: string): u16 { return <u16>parseFloat(value) }
}
@builtin export declare function u32(value: void): u32;
export namespace u32 {
export const MIN_VALUE: u32 = 0;
export const MAX_VALUE: u32 = 4294967295;
@inline export function parseInt(value: string, radix: i32 = 0): u32 { return <u32>parseI32(value, radix) }
@inline export function parseFloat(value: string): u32 { return <u32>parseFloat(value) }
}
@builtin export declare function u64(value: void): u64;
export namespace u64 {
export const MIN_VALUE: u64 = 0;
export const MAX_VALUE: u64 = 18446744073709551615;
@inline export function parseInt(value: string, radix: i32 = 0): u64 { return <u64>parseI64(value, radix) }
@inline export function parseFloat(value: string): u64 { return <u64>parseFloat(value) }
}
@builtin export declare function usize(value: void): usize;
@ -137,6 +155,8 @@ export namespace usize {
export const MAX_VALUE: usize = sizeof<u32>() == sizeof<usize>()
? 4294967295
: <usize>18446744073709551615;
@inline export function parseInt(value: string, radix: i32 = 0): usize { return <usize>parseI64(value, radix) }
@inline export function parseFloat(value: string): usize { return <usize>parseFloat(value) }
}
@builtin export declare function bool(value: void): bool;
@ -168,6 +188,12 @@ export namespace f32 {
@builtin export declare function sqrt(value: f32): f32;
@builtin export declare function store(offset: usize, value: f32, constantOffset?: usize): void;
@builtin export declare function trunc(value: f32): f32;
@inline export function isNaN(value: f32): bool { return isNaN<f32>(value) }
@inline export function isFinite(value: f32): bool { return isFinite<f32>(value) }
@inline export function isSafeInteger(value: f32): bool { return abs<f32>(value) <= f32.MAX_SAFE_INTEGER && trunc<f32>(value) == value }
@inline export function isInteger(value: f32): bool { return isFinite<f32>(value) && trunc<f32>(value) == value }
@inline export function parseInt(value: string, radix: i32 = 0): f32 { return <f32>parseI64(value, radix) }
@inline export function parseFloat(value: string): f32 { return <f32>parseFloat(value) }
}
@builtin export declare function f64(value: void): f64;
@ -193,6 +219,12 @@ export namespace f64 {
@builtin export declare function sqrt(value: f64): f64;
@builtin export declare function store(offset: usize, value: f64, constantOffset?: usize): void;
@builtin export declare function trunc(value: f64): f64;
@inline export function isNaN(value: f64): bool { return isNaN<f64>(value) }
@inline export function isFinite(value: f64): bool { return isFinite<f64>(value) }
@inline export function isSafeInteger(value: f64): bool { return abs<f64>(value) <= f64.MAX_SAFE_INTEGER && trunc<f64>(value) == value }
@inline export function isInteger(value: f64): bool { return isFinite<f64>(value) && trunc<f64>(value) == value }
@inline export function parseInt(value: string, radix: i32 = 0): f64 { return <f64>parseI64(value, radix) }
@inline export function parseFloat(value: string): f64 { return parseFloat(value) }
}
@builtin export declare function start(): void;

View File

@ -156,6 +156,10 @@ declare namespace i8 {
export const MIN_VALUE: i8;
/** Largest representable value. */
export const MAX_VALUE: i8;
/** Converts a string to a floating-point number and cast to target integer after. */
export function parseFloat(string: string): i8;
/** Converts A string to an integer. */
export function parseInt(string: string, radix?: i32): i8;
}
/** Converts any other numeric value to a 16-bit signed integer. */
declare function i16(value: i8 | i16 | i32 | i64 | isize | u8 | u16 | u32 | u64 | usize | bool | f32 | f64): i8;
@ -164,6 +168,10 @@ declare namespace i16 {
export const MIN_VALUE: i16;
/** Largest representable value. */
export const MAX_VALUE: i16;
/** Converts a string to a floating-point number and cast to target integer after. */
export function parseFloat(string: string): i16;
/** Converts A string to an integer. */
export function parseInt(string: string, radix?: i32): i16;
}
/** Converts any other numeric value to a 32-bit signed integer. */
declare function i32(value: i8 | i16 | i32 | i64 | isize | u8 | u16 | u32 | u64 | usize | bool | f32 | f64): i32;
@ -188,6 +196,10 @@ declare namespace i32 {
export function store16(offset: usize, value: i32, constantOffset?: usize): void;
/** Stores a 32-bit integer to memory. */
export function store(offset: usize, value: i32, constantOffset?: usize): void;
/** Converts a string to a floating-point number and cast to target integer after. */
export function parseFloat(string: string): i32;
/** Converts A string to an integer. */
export function parseInt(string: string, radix?: i32): i32;
}
/** Converts any other numeric value to a 64-bit signed integer. */
declare function i64(value: i8 | i16 | i32 | i64 | isize | u8 | u16 | u32 | u64 | usize | bool | f32 | f64): i64;
@ -218,6 +230,10 @@ declare namespace i64 {
export function store32(offset: usize, value: i64, constantOffset?: usize): void;
/** Stores a 64-bit integer to memory. */
export function store(offset: usize, value: i64, constantOffset?: usize): void;
/** Converts a string to a floating-point number and cast to target integer after. */
export function parseFloat(string: string): i64;
/** Converts A string to an integer. */
export function parseInt(string: string, radix?: i32): i64;
}
/** Converts any other numeric value to a 32-bit (in WASM32) respectivel 64-bit (in WASM64) signed integer. */
declare var isize: i32 | i64;
@ -228,6 +244,10 @@ declare namespace u8 {
export const MIN_VALUE: u8;
/** Largest representable value. */
export const MAX_VALUE: u8;
/** Converts a string to a floating-point number and cast to target integer after. */
export function parseFloat(string: string): u8;
/** Converts A string to an integer. */
export function parseInt(string: string, radix?: i32): u8;
}
/** Converts any other numeric value to a 16-bit unsigned integer. */
declare function u16(value: i8 | i16 | i32 | i64 | isize | u8 | u16 | u32 | u64 | usize | bool | f32 | f64): i8;
@ -236,6 +256,10 @@ declare namespace u16 {
export const MIN_VALUE: u16;
/** Largest representable value. */
export const MAX_VALUE: u16;
/** Converts a string to a floating-point number and cast to target integer after. */
export function parseFloat(string: string): u16;
/** Converts A string to an integer. */
export function parseInt(string: string, radix?: i32): u16;
}
/** Converts any other numeric value to a 32-bit unsigned integer. */
declare function u32(value: i8 | i16 | i32 | i64 | isize | u8 | u16 | u32 | u64 | usize | bool | f32 | f64): i32;
@ -244,6 +268,10 @@ declare namespace u32 {
export const MIN_VALUE: u32;
/** Largest representable value. */
export const MAX_VALUE: u32;
/** Converts a string to a floating-point number and cast to target integer after. */
export function parseFloat(string: string): u64;
/** Converts A string to an integer. */
export function parseInt(string: string, radix?: i32): u64;
}
/** Converts any other numeric value to a 64-bit unsigned integer. */
declare function u64(value: i8 | i16 | i32 | i64 | isize | u8 | u16 | u32 | u64 | usize | bool | f32 | f64): i64;
@ -252,6 +280,10 @@ declare namespace u64 {
export const MIN_VALUE: u64;
/** Largest representable value. */
export const MAX_VALUE: u64;
/** Converts a string to a floating-point number. */
export function parseFloat(string: string): u64;
/** Converts A string to an integer. */
export function parseInt(string: string, radix?: i32): u64;
}
/** Converts any other numeric value to a 32-bit (in WASM32) respectivel 64-bit (in WASM64) unsigned integer. */
declare var usize: u32 | u64;
@ -286,6 +318,18 @@ declare namespace f32 {
export function load(offset: usize, constantOffset?: usize): f32;
/** Stores a 32-bit float to memory. */
export function store(offset: usize, value: f32, constantOffset?: usize): void;
/** Returns a boolean value that indicates whether a value is the reserved value NaN (not a number). */
export function isNaN(value: f32): bool;
/** Returns true if passed value is finite. */
export function isFinite(value: f32): bool;
/** Returns true if the value passed is a safe integer. */
export function isSafeInteger(value: f32): bool;
/** Returns true if the value passed is an integer, false otherwise. */
export function isInteger(value: f32): bool;
/** Converts a string to a floating-point number. */
export function parseFloat(string: string): f32;
/** Converts A string to an integer. */
export function parseInt(string: string, radix?: i32): 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;
@ -306,6 +350,18 @@ declare namespace f64 {
export function load(offset: usize, constantOffset?: usize): f64;
/** Stores a 64-bit float to memory. */
export function store(offset: usize, value: f64, constantOffset?: usize): void;
/** Returns a boolean value that indicates whether a value is the reserved value NaN (not a number). */
export function isNaN(value: f32): bool;
/** Returns true if passed value is finite. */
export function isFinite(value: f32): bool;
/** Returns true if the value passed is a safe integer. */
export function isSafeInteger(value: f64): bool;
/** Returns true if the value passed is an integer, false otherwise. */
export function isInteger(value: f64): bool;
/** Converts a string to a floating-point number. */
export function parseFloat(string: string): f64;
/** Converts A string to an integer. */
export function parseInt(string: string, radix?: i32): f64;
}
// User-defined diagnostic macros

View File

@ -114,6 +114,10 @@ declare namespace i8 {
export const MIN_VALUE: i8;
/** Largest representable value. */
export const MAX_VALUE: i8;
/** Converts a string to a floating-point number and cast to target integer after. */
export function parseFloat(string: string): i8;
/** Converts A string to an integer. */
export function parseInt(string: string, radix?: i32): i8;
}
/** Converts any other numeric value to a 16-bit signed integer. */
declare function i16(value: i8 | i16 | i32 | isize | u8 | u16 | u32 | usize | bool | f32 | f64): i8;
@ -122,6 +126,10 @@ declare namespace i16 {
export const MIN_VALUE: i16;
/** Largest representable value. */
export const MAX_VALUE: i16;
/** Converts a string to a floating-point number and cast to target integer after. */
export function parseFloat(string: string): i16;
/** Converts A string to an integer. */
export function parseInt(string: string, radix?: i32): i16;
}
/** Converts any other numeric value to a 32-bit signed integer. */
declare function i32(value: i8 | i16 | i32 | isize | u8 | u16 | u32 | usize | bool | f32 | f64): i32;
@ -130,6 +138,10 @@ declare namespace i32 {
export const MIN_VALUE: i32;
/** Largest representable value. */
export const MAX_VALUE: i32;
/** Converts a string to a floating-point number and cast to target integer after. */
export function parseFloat(string: string): i32;
/** Converts A string to an integer. */
export function parseInt(string: string, radix?: i32): i32;
}
/** Converts any other numeric value to a 32-bit (in WASM32) respectivel 64-bit (in WASM64) signed integer. */
declare function isize(value: i8 | i16 | i32 | isize | u8 | u16 | u32 | usize | bool | f32 | f64): isize;
@ -138,6 +150,10 @@ declare namespace isize {
export const MIN_VALUE: isize;
/** Largest representable value. */
export const MAX_VALUE: isize;
/** Converts a string to a floating-point number and cast to target integer after. */
export function parseFloat(string: string): isize;
/** Converts A string to an integer. */
export function parseInt(string: string, radix?: i32): isize;
}
/** Converts any other numeric value to an 8-bit unsigned integer. */
declare function u8(value: i8 | i16 | i32 | isize | u8 | u16 | u32 | usize | bool | f32 | f64): i8;
@ -146,6 +162,10 @@ declare namespace u8 {
export const MIN_VALUE: u8;
/** Largest representable value. */
export const MAX_VALUE: u8;
/** Converts a string to a floating-point number and cast to target integer after. */
export function parseFloat(string: string): u8;
/** Converts A string to an integer. */
export function parseInt(string: string, radix?: i32): u8;
}
/** Converts any other numeric value to a 16-bit unsigned integer. */
declare function u16(value: i8 | i16 | i32 | isize | u8 | u16 | u32 | usize | bool | f32 | f64): i8;
@ -154,6 +174,10 @@ declare namespace u16 {
export const MIN_VALUE: u16;
/** Largest representable value. */
export const MAX_VALUE: u16;
/** Converts a string to a floating-point number and cast to target integer after. */
export function parseFloat(string: string): u16;
/** Converts A string to an integer. */
export function parseInt(string: string, radix?: i32): u16;
}
/** Converts any other numeric value to a 32-bit unsigned integer. */
declare function u32(value: i8 | i16 | i32 | isize | u8 | u16 | u32 | usize | bool | f32 | f64): i32;
@ -162,6 +186,10 @@ declare namespace u32 {
export const MIN_VALUE: u32;
/** Largest representable value. */
export const MAX_VALUE: u32;
/** Converts a string to a floating-point number and cast to target integer after. */
export function parseFloat(string: string): u32;
/** Converts A string to an integer. */
export function parseInt(string: string, radix?: i32): u32;
}
/** Converts any other numeric value to a 32-bit (in WASM32) respectivel 64-bit (in WASM64) unsigned integer. */
declare function usize(value: i8 | i16 | i32 | isize | u8 | u16 | u32 | usize | bool | f32 | f64): isize;
@ -170,6 +198,10 @@ declare namespace usize {
export const MIN_VALUE: usize;
/** Largest representable value. */
export const MAX_VALUE: usize;
/** Converts a string to a floating-point number and cast to target integer after. */
export function parseFloat(string: string): usize;
/** Converts A string to an integer. */
export function parseInt(string: string, radix?: i32): usize;
}
/** Converts any other numeric value to a 1-bit unsigned integer. */
declare function bool(value: i8 | i16 | i32 | isize | u8 | u16 | u32 | usize | bool | f32 | f64): bool;
@ -194,6 +226,18 @@ 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 a boolean value that indicates whether a value is the reserved value NaN (not a number). */
export function isNaN(value: f32): bool;
/** Returns true if passed value is finite. */
export function isFinite(value: f32): bool;
/** Returns true if the value passed is a safe integer. */
export function isSafeInteger(value: f32): bool;
/** Returns true if the value passed is an integer, false otherwise. */
export function isInteger(value: f32): bool;
/** Converts a string to a floating-point number. */
export function parseFloat(string: string): f32;
/** Converts A string to an integer. */
export function parseInt(string: string, radix?: i32): f32;
}
/** Converts any other numeric value to a 64-bit float. */
declare function f64(value: i8 | i16 | i32 | isize | u8 | u16 | u32 | usize | bool | f32 | f64): f64;
@ -210,6 +254,18 @@ declare namespace f64 {
export const MAX_SAFE_INTEGER: f64;
/** Difference between 1 and the smallest representable value greater than 1. */
export const EPSILON: f64;
/** Returns a boolean value that indicates whether a value is the reserved value NaN (not a number). */
export function isNaN(value: f32): bool;
/** Returns true if passed value is finite. */
export function isFinite(value: f32): bool;
/** Returns true if the value passed is a safe integer. */
export function isSafeInteger(value: f64): bool;
/** Returns true if the value passed is an integer, false otherwise. */
export function isInteger(value: f64): bool;
/** Converts a string to a floating-point number. */
export function parseFloat(string: string): f64;
/** Converts A string to an integer. */
export function parseInt(string: string, radix?: i32): f64;
}
// Polyfills

View File

@ -315,6 +315,27 @@ assert(f32.MIN_SAFE_INTEGER == -16777215);
assert(f32.MAX_SAFE_INTEGER == 16777215);
assert(f32.EPSILON == 1.19209290e-07);
assert(isNaN<f32>(f32.NaN));
assert(f32.isSafeInteger(f32.MIN_SAFE_INTEGER - 1) == false);
assert(f32.isSafeInteger(f32.MIN_SAFE_INTEGER) == true);
assert(f32.isSafeInteger(+0.0) == true);
assert(f32.isSafeInteger(-0.0) == true);
assert(f32.isSafeInteger(NaN) == false);
assert(f32.isSafeInteger(Infinity) == false);
assert(f32.isSafeInteger(f32.MAX_SAFE_INTEGER) == true);
assert(f32.isSafeInteger(f32.MAX_SAFE_INTEGER + 1) == false);
assert(f32.isSafeInteger(0.5) == false);
assert(f32.isInteger(+0.0) == true);
assert(f32.isInteger(-0.0) == true);
assert(f32.isInteger(NaN) == false);
assert(f32.isInteger(Infinity) == false);
assert(f32.isInteger(f32.EPSILON) == false);
assert(f32.isInteger(+1.0) == true);
assert(f32.isInteger(-1.0) == true);
assert(f32.isInteger(f32.MIN_SAFE_INTEGER) == true);
assert(f32.isInteger(f32.MAX_SAFE_INTEGER) == true);
assert(f32.isInteger(+0.5) == false);
assert(f32.isInteger(-1.5) == false);
assert(f64.MIN_NORMAL_VALUE == 2.2250738585072014e-308);
assert(f64.MIN_VALUE == 5e-324);
assert(f64.MAX_VALUE == 1.7976931348623157e+308);
@ -322,6 +343,26 @@ assert(f64.MIN_SAFE_INTEGER == -9007199254740991);
assert(f64.MAX_SAFE_INTEGER == 9007199254740991);
assert(f64.EPSILON == 2.2204460492503131e-16);
assert(isNaN<f64>(f64.NaN));
assert(f64.isSafeInteger(f64.MIN_SAFE_INTEGER - 1) == false);
assert(f64.isSafeInteger(f64.MIN_SAFE_INTEGER) == true);
assert(f64.isSafeInteger(+0.0) == true);
assert(f64.isSafeInteger(-0.0) == true);
assert(f64.isSafeInteger(NaN) == false);
assert(f64.isSafeInteger(Infinity) == false);
assert(f64.isSafeInteger(f64.MAX_SAFE_INTEGER) == true);
assert(f64.isSafeInteger(f64.MAX_SAFE_INTEGER + 1) == false);
assert(f64.isSafeInteger(0.5) == false);
assert(f64.isInteger(+0.0) == true);
assert(f64.isInteger(-0.0) == true);
assert(f64.isInteger(NaN) == false);
assert(f64.isInteger(Infinity) == false);
assert(f64.isInteger(f64.EPSILON) == false);
assert(f64.isInteger(+1.0) == true);
assert(f64.isInteger(-1.0) == true);
assert(f64.isInteger(f64.MIN_SAFE_INTEGER) == true);
assert(f64.isInteger(f64.MAX_SAFE_INTEGER) == true);
assert(f64.isInteger(+0.5) == false);
assert(f64.isInteger(-1.5) == false);
// inline-assembler

File diff suppressed because it is too large Load Diff