Skip to content

Commit 4e89456

Browse files
MaxGraeydcodeIO
authored andcommitted
Add more numeric builtins (#330)
1 parent 3f9758f commit 4e89456

File tree

5 files changed

+1527
-24
lines changed

5 files changed

+1527
-24
lines changed

std/assembly/builtins.ts

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -46,12 +46,16 @@
4646
export namespace i8 {
4747
export const MIN_VALUE: i8 = -128;
4848
export const MAX_VALUE: i8 = 127;
49+
@inline export function parseInt(value: string, radix: i32 = 0): i8 { return <i8>parseI32(value, radix) }
50+
@inline export function parseFloat(value: string): i8 { return <i8>parseFloat(value) }
4951
}
5052

5153
@builtin export declare function i16(value: void): i16;
5254
export namespace i16 {
5355
export const MIN_VALUE: i16 = -32768;
5456
export const MAX_VALUE: i16 = 32767;
57+
@inline export function parseInt(value: string, radix: i32 = 0): i16 { return <i16>parseI32(value, radix) }
58+
@inline export function parseFloat(value: string): i16 { return <i16>parseFloat(value) }
5559
}
5660

5761
@builtin export declare function i32(value: void): i32;
@@ -72,6 +76,8 @@ export namespace i32 {
7276
@builtin export declare function store8(offset: usize, value: i32, constantOffset?: usize): void;
7377
@builtin export declare function store16(offset: usize, value: i32, constantOffset?: usize): void;
7478
@builtin export declare function store(offset: usize, value: i32, constantOffset?: usize): void;
79+
@inline export function parseInt(value: string, radix: i32 = 0): i32 { return <i32>parseI32(value, radix) }
80+
@inline export function parseFloat(value: string): i32 { return <i32>parseFloat(value) }
7581
}
7682

7783
@builtin export declare function i64(value: void): i64;
@@ -95,6 +101,8 @@ export namespace i64 {
95101
@builtin export declare function store16(offset: usize, value: i64, constantOffset?: usize): void;
96102
@builtin export declare function store32(offset: usize, value: i64, constantOffset?: usize): void;
97103
@builtin export declare function store(offset: usize, value: i64, constantOffset?: usize): void;
104+
@inline export function parseInt(value: string, radix: i32 = 0): i64 { return <i64>parseI64(value, radix) }
105+
@inline export function parseFloat(value: string): i64 { return <i64>parseFloat(value) }
98106
}
99107

100108
@builtin export declare function isize(value: void): isize;
@@ -105,30 +113,40 @@ export namespace isize {
105113
export const MAX_VALUE: isize = sizeof<i32>() == sizeof<isize>()
106114
? 2147483647
107115
: <isize>9223372036854775807;
116+
@inline export function parseInt(value: string, radix: i32 = 0): isize { return <isize>parseI64(value, radix) }
117+
@inline export function parseFloat(value: string): isize { return <isize>parseFloat(value) }
108118
}
109119

110120
@builtin export declare function u8(value: void): u8;
111121
export namespace u8 {
112122
export const MIN_VALUE: u8 = 0;
113123
export const MAX_VALUE: u8 = 255;
124+
@inline export function parseInt(value: string, radix: i32 = 0): u8 { return <u8>parseI32(value, radix) }
125+
@inline export function parseFloat(value: string): u8 { return <u8>parseFloat(value) }
114126
}
115127

116128
@builtin export declare function u16(value: void): u16;
117129
export namespace u16 {
118130
export const MIN_VALUE: u16 = 0;
119131
export const MAX_VALUE: u16 = 65535;
132+
@inline export function parseInt(value: string, radix: i32 = 0): u16 { return <u16>parseI32(value, radix) }
133+
@inline export function parseFloat(value: string): u16 { return <u16>parseFloat(value) }
120134
}
121135

122136
@builtin export declare function u32(value: void): u32;
123137
export namespace u32 {
124138
export const MIN_VALUE: u32 = 0;
125139
export const MAX_VALUE: u32 = 4294967295;
140+
@inline export function parseInt(value: string, radix: i32 = 0): u32 { return <u32>parseI32(value, radix) }
141+
@inline export function parseFloat(value: string): u32 { return <u32>parseFloat(value) }
126142
}
127143

128144
@builtin export declare function u64(value: void): u64;
129145
export namespace u64 {
130146
export const MIN_VALUE: u64 = 0;
131147
export const MAX_VALUE: u64 = 18446744073709551615;
148+
@inline export function parseInt(value: string, radix: i32 = 0): u64 { return <u64>parseI64(value, radix) }
149+
@inline export function parseFloat(value: string): u64 { return <u64>parseFloat(value) }
132150
}
133151

134152
@builtin export declare function usize(value: void): usize;
@@ -137,6 +155,8 @@ export namespace usize {
137155
export const MAX_VALUE: usize = sizeof<u32>() == sizeof<usize>()
138156
? 4294967295
139157
: <usize>18446744073709551615;
158+
@inline export function parseInt(value: string, radix: i32 = 0): usize { return <usize>parseI64(value, radix) }
159+
@inline export function parseFloat(value: string): usize { return <usize>parseFloat(value) }
140160
}
141161

142162
@builtin export declare function bool(value: void): bool;
@@ -168,6 +188,12 @@ export namespace f32 {
168188
@builtin export declare function sqrt(value: f32): f32;
169189
@builtin export declare function store(offset: usize, value: f32, constantOffset?: usize): void;
170190
@builtin export declare function trunc(value: f32): f32;
191+
@inline export function isNaN(value: f32): bool { return isNaN<f32>(value) }
192+
@inline export function isFinite(value: f32): bool { return isFinite<f32>(value) }
193+
@inline export function isSafeInteger(value: f32): bool { return abs<f32>(value) <= f32.MAX_SAFE_INTEGER && trunc<f32>(value) == value }
194+
@inline export function isInteger(value: f32): bool { return isFinite<f32>(value) && trunc<f32>(value) == value }
195+
@inline export function parseInt(value: string, radix: i32 = 0): f32 { return <f32>parseI64(value, radix) }
196+
@inline export function parseFloat(value: string): f32 { return <f32>parseFloat(value) }
171197
}
172198

173199
@builtin export declare function f64(value: void): f64;
@@ -193,6 +219,12 @@ export namespace f64 {
193219
@builtin export declare function sqrt(value: f64): f64;
194220
@builtin export declare function store(offset: usize, value: f64, constantOffset?: usize): void;
195221
@builtin export declare function trunc(value: f64): f64;
222+
@inline export function isNaN(value: f64): bool { return isNaN<f64>(value) }
223+
@inline export function isFinite(value: f64): bool { return isFinite<f64>(value) }
224+
@inline export function isSafeInteger(value: f64): bool { return abs<f64>(value) <= f64.MAX_SAFE_INTEGER && trunc<f64>(value) == value }
225+
@inline export function isInteger(value: f64): bool { return isFinite<f64>(value) && trunc<f64>(value) == value }
226+
@inline export function parseInt(value: string, radix: i32 = 0): f64 { return <f64>parseI64(value, radix) }
227+
@inline export function parseFloat(value: string): f64 { return parseFloat(value) }
196228
}
197229

198230
@builtin export declare function start(): void;

std/assembly/index.d.ts

Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -156,6 +156,10 @@ declare namespace i8 {
156156
export const MIN_VALUE: i8;
157157
/** Largest representable value. */
158158
export const MAX_VALUE: i8;
159+
/** Converts a string to a floating-point number and cast to target integer after. */
160+
export function parseFloat(string: string): i8;
161+
/** Converts A string to an integer. */
162+
export function parseInt(string: string, radix?: i32): i8;
159163
}
160164
/** Converts any other numeric value to a 16-bit signed integer. */
161165
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 {
164168
export const MIN_VALUE: i16;
165169
/** Largest representable value. */
166170
export const MAX_VALUE: i16;
171+
/** Converts a string to a floating-point number and cast to target integer after. */
172+
export function parseFloat(string: string): i16;
173+
/** Converts A string to an integer. */
174+
export function parseInt(string: string, radix?: i32): i16;
167175
}
168176
/** Converts any other numeric value to a 32-bit signed integer. */
169177
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 {
188196
export function store16(offset: usize, value: i32, constantOffset?: usize): void;
189197
/** Stores a 32-bit integer to memory. */
190198
export function store(offset: usize, value: i32, constantOffset?: usize): void;
199+
/** Converts a string to a floating-point number and cast to target integer after. */
200+
export function parseFloat(string: string): i32;
201+
/** Converts A string to an integer. */
202+
export function parseInt(string: string, radix?: i32): i32;
191203
}
192204
/** Converts any other numeric value to a 64-bit signed integer. */
193205
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 {
218230
export function store32(offset: usize, value: i64, constantOffset?: usize): void;
219231
/** Stores a 64-bit integer to memory. */
220232
export function store(offset: usize, value: i64, constantOffset?: usize): void;
233+
/** Converts a string to a floating-point number and cast to target integer after. */
234+
export function parseFloat(string: string): i64;
235+
/** Converts A string to an integer. */
236+
export function parseInt(string: string, radix?: i32): i64;
221237
}
222238
/** Converts any other numeric value to a 32-bit (in WASM32) respectivel 64-bit (in WASM64) signed integer. */
223239
declare var isize: i32 | i64;
@@ -228,6 +244,10 @@ declare namespace u8 {
228244
export const MIN_VALUE: u8;
229245
/** Largest representable value. */
230246
export const MAX_VALUE: u8;
247+
/** Converts a string to a floating-point number and cast to target integer after. */
248+
export function parseFloat(string: string): u8;
249+
/** Converts A string to an integer. */
250+
export function parseInt(string: string, radix?: i32): u8;
231251
}
232252
/** Converts any other numeric value to a 16-bit unsigned integer. */
233253
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 {
236256
export const MIN_VALUE: u16;
237257
/** Largest representable value. */
238258
export const MAX_VALUE: u16;
259+
/** Converts a string to a floating-point number and cast to target integer after. */
260+
export function parseFloat(string: string): u16;
261+
/** Converts A string to an integer. */
262+
export function parseInt(string: string, radix?: i32): u16;
239263
}
240264
/** Converts any other numeric value to a 32-bit unsigned integer. */
241265
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 {
244268
export const MIN_VALUE: u32;
245269
/** Largest representable value. */
246270
export const MAX_VALUE: u32;
271+
/** Converts a string to a floating-point number and cast to target integer after. */
272+
export function parseFloat(string: string): u64;
273+
/** Converts A string to an integer. */
274+
export function parseInt(string: string, radix?: i32): u64;
247275
}
248276
/** Converts any other numeric value to a 64-bit unsigned integer. */
249277
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 {
252280
export const MIN_VALUE: u64;
253281
/** Largest representable value. */
254282
export const MAX_VALUE: u64;
283+
/** Converts a string to a floating-point number. */
284+
export function parseFloat(string: string): u64;
285+
/** Converts A string to an integer. */
286+
export function parseInt(string: string, radix?: i32): u64;
255287
}
256288
/** Converts any other numeric value to a 32-bit (in WASM32) respectivel 64-bit (in WASM64) unsigned integer. */
257289
declare var usize: u32 | u64;
@@ -286,6 +318,18 @@ declare namespace f32 {
286318
export function load(offset: usize, constantOffset?: usize): f32;
287319
/** Stores a 32-bit float to memory. */
288320
export function store(offset: usize, value: f32, constantOffset?: usize): void;
321+
/** Returns a boolean value that indicates whether a value is the reserved value NaN (not a number). */
322+
export function isNaN(value: f32): bool;
323+
/** Returns true if passed value is finite. */
324+
export function isFinite(value: f32): bool;
325+
/** Returns true if the value passed is a safe integer. */
326+
export function isSafeInteger(value: f32): bool;
327+
/** Returns true if the value passed is an integer, false otherwise. */
328+
export function isInteger(value: f32): bool;
329+
/** Converts a string to a floating-point number. */
330+
export function parseFloat(string: string): f32;
331+
/** Converts A string to an integer. */
332+
export function parseInt(string: string, radix?: i32): f32;
289333
}
290334
/** Converts any other numeric value to a 64-bit float. */
291335
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 {
306350
export function load(offset: usize, constantOffset?: usize): f64;
307351
/** Stores a 64-bit float to memory. */
308352
export function store(offset: usize, value: f64, constantOffset?: usize): void;
353+
/** Returns a boolean value that indicates whether a value is the reserved value NaN (not a number). */
354+
export function isNaN(value: f32): bool;
355+
/** Returns true if passed value is finite. */
356+
export function isFinite(value: f32): bool;
357+
/** Returns true if the value passed is a safe integer. */
358+
export function isSafeInteger(value: f64): bool;
359+
/** Returns true if the value passed is an integer, false otherwise. */
360+
export function isInteger(value: f64): bool;
361+
/** Converts a string to a floating-point number. */
362+
export function parseFloat(string: string): f64;
363+
/** Converts A string to an integer. */
364+
export function parseInt(string: string, radix?: i32): f64;
309365
}
310366

311367
// User-defined diagnostic macros

std/portable/index.d.ts

Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -114,6 +114,10 @@ declare namespace i8 {
114114
export const MIN_VALUE: i8;
115115
/** Largest representable value. */
116116
export const MAX_VALUE: i8;
117+
/** Converts a string to a floating-point number and cast to target integer after. */
118+
export function parseFloat(string: string): i8;
119+
/** Converts A string to an integer. */
120+
export function parseInt(string: string, radix?: i32): i8;
117121
}
118122
/** Converts any other numeric value to a 16-bit signed integer. */
119123
declare function i16(value: i8 | i16 | i32 | isize | u8 | u16 | u32 | usize | bool | f32 | f64): i8;
@@ -122,6 +126,10 @@ declare namespace i16 {
122126
export const MIN_VALUE: i16;
123127
/** Largest representable value. */
124128
export const MAX_VALUE: i16;
129+
/** Converts a string to a floating-point number and cast to target integer after. */
130+
export function parseFloat(string: string): i16;
131+
/** Converts A string to an integer. */
132+
export function parseInt(string: string, radix?: i32): i16;
125133
}
126134
/** Converts any other numeric value to a 32-bit signed integer. */
127135
declare function i32(value: i8 | i16 | i32 | isize | u8 | u16 | u32 | usize | bool | f32 | f64): i32;
@@ -130,6 +138,10 @@ declare namespace i32 {
130138
export const MIN_VALUE: i32;
131139
/** Largest representable value. */
132140
export const MAX_VALUE: i32;
141+
/** Converts a string to a floating-point number and cast to target integer after. */
142+
export function parseFloat(string: string): i32;
143+
/** Converts A string to an integer. */
144+
export function parseInt(string: string, radix?: i32): i32;
133145
}
134146
/** Converts any other numeric value to a 32-bit (in WASM32) respectivel 64-bit (in WASM64) signed integer. */
135147
declare function isize(value: i8 | i16 | i32 | isize | u8 | u16 | u32 | usize | bool | f32 | f64): isize;
@@ -138,6 +150,10 @@ declare namespace isize {
138150
export const MIN_VALUE: isize;
139151
/** Largest representable value. */
140152
export const MAX_VALUE: isize;
153+
/** Converts a string to a floating-point number and cast to target integer after. */
154+
export function parseFloat(string: string): isize;
155+
/** Converts A string to an integer. */
156+
export function parseInt(string: string, radix?: i32): isize;
141157
}
142158
/** Converts any other numeric value to an 8-bit unsigned integer. */
143159
declare function u8(value: i8 | i16 | i32 | isize | u8 | u16 | u32 | usize | bool | f32 | f64): i8;
@@ -146,6 +162,10 @@ declare namespace u8 {
146162
export const MIN_VALUE: u8;
147163
/** Largest representable value. */
148164
export const MAX_VALUE: u8;
165+
/** Converts a string to a floating-point number and cast to target integer after. */
166+
export function parseFloat(string: string): u8;
167+
/** Converts A string to an integer. */
168+
export function parseInt(string: string, radix?: i32): u8;
149169
}
150170
/** Converts any other numeric value to a 16-bit unsigned integer. */
151171
declare function u16(value: i8 | i16 | i32 | isize | u8 | u16 | u32 | usize | bool | f32 | f64): i8;
@@ -154,6 +174,10 @@ declare namespace u16 {
154174
export const MIN_VALUE: u16;
155175
/** Largest representable value. */
156176
export const MAX_VALUE: u16;
177+
/** Converts a string to a floating-point number and cast to target integer after. */
178+
export function parseFloat(string: string): u16;
179+
/** Converts A string to an integer. */
180+
export function parseInt(string: string, radix?: i32): u16;
157181
}
158182
/** Converts any other numeric value to a 32-bit unsigned integer. */
159183
declare function u32(value: i8 | i16 | i32 | isize | u8 | u16 | u32 | usize | bool | f32 | f64): i32;
@@ -162,6 +186,10 @@ declare namespace u32 {
162186
export const MIN_VALUE: u32;
163187
/** Largest representable value. */
164188
export const MAX_VALUE: u32;
189+
/** Converts a string to a floating-point number and cast to target integer after. */
190+
export function parseFloat(string: string): u32;
191+
/** Converts A string to an integer. */
192+
export function parseInt(string: string, radix?: i32): u32;
165193
}
166194
/** Converts any other numeric value to a 32-bit (in WASM32) respectivel 64-bit (in WASM64) unsigned integer. */
167195
declare function usize(value: i8 | i16 | i32 | isize | u8 | u16 | u32 | usize | bool | f32 | f64): isize;
@@ -170,6 +198,10 @@ declare namespace usize {
170198
export const MIN_VALUE: usize;
171199
/** Largest representable value. */
172200
export const MAX_VALUE: usize;
201+
/** Converts a string to a floating-point number and cast to target integer after. */
202+
export function parseFloat(string: string): usize;
203+
/** Converts A string to an integer. */
204+
export function parseInt(string: string, radix?: i32): usize;
173205
}
174206
/** Converts any other numeric value to a 1-bit unsigned integer. */
175207
declare function bool(value: i8 | i16 | i32 | isize | u8 | u16 | u32 | usize | bool | f32 | f64): bool;
@@ -194,6 +226,18 @@ declare namespace f32 {
194226
export const MAX_SAFE_INTEGER: f32;
195227
/** Difference between 1 and the smallest representable value greater than 1. */
196228
export const EPSILON: f32;
229+
/** Returns a boolean value that indicates whether a value is the reserved value NaN (not a number). */
230+
export function isNaN(value: f32): bool;
231+
/** Returns true if passed value is finite. */
232+
export function isFinite(value: f32): bool;
233+
/** Returns true if the value passed is a safe integer. */
234+
export function isSafeInteger(value: f32): bool;
235+
/** Returns true if the value passed is an integer, false otherwise. */
236+
export function isInteger(value: f32): bool;
237+
/** Converts a string to a floating-point number. */
238+
export function parseFloat(string: string): f32;
239+
/** Converts A string to an integer. */
240+
export function parseInt(string: string, radix?: i32): f32;
197241
}
198242
/** Converts any other numeric value to a 64-bit float. */
199243
declare function f64(value: i8 | i16 | i32 | isize | u8 | u16 | u32 | usize | bool | f32 | f64): f64;
@@ -210,6 +254,18 @@ declare namespace f64 {
210254
export const MAX_SAFE_INTEGER: f64;
211255
/** Difference between 1 and the smallest representable value greater than 1. */
212256
export const EPSILON: f64;
257+
/** Returns a boolean value that indicates whether a value is the reserved value NaN (not a number). */
258+
export function isNaN(value: f32): bool;
259+
/** Returns true if passed value is finite. */
260+
export function isFinite(value: f32): bool;
261+
/** Returns true if the value passed is a safe integer. */
262+
export function isSafeInteger(value: f64): bool;
263+
/** Returns true if the value passed is an integer, false otherwise. */
264+
export function isInteger(value: f64): bool;
265+
/** Converts a string to a floating-point number. */
266+
export function parseFloat(string: string): f64;
267+
/** Converts A string to an integer. */
268+
export function parseInt(string: string, radix?: i32): f64;
213269
}
214270

215271
// Polyfills

0 commit comments

Comments
 (0)