mirror of
https://github.com/fluencelabs/assemblyscript
synced 2025-06-19 09:51:33 +00:00
srsly
This commit is contained in:
@ -1,8 +1,15 @@
|
||||
/** Number of alignment bits. */
|
||||
// @ts-ignore: decorator
|
||||
@inline export const AL_BITS: u32 = 3;
|
||||
|
||||
/** Number of possible alignment values. */
|
||||
// @ts-ignore: decorator
|
||||
@inline export const AL_SIZE: usize = 1 << <usize>AL_BITS;
|
||||
|
||||
/** Mask to obtain just the alignment bits. */
|
||||
// @ts-ignore: decorator
|
||||
@inline export const AL_MASK: usize = AL_SIZE - 1;
|
||||
|
||||
/** Maximum 32-bit allocation size. */
|
||||
// @ts-ignore: decorator
|
||||
@inline export const MAX_SIZE_32: usize = 1 << 30; // 1GB
|
||||
|
@ -1,16 +1,24 @@
|
||||
// @ts-ignore: decorator
|
||||
@inline export function HASH<T>(key: T): u32 {
|
||||
if (isString(key)) {
|
||||
// @ts-ignore: type
|
||||
return hashStr(key);
|
||||
} else if (isReference<T>()) {
|
||||
if (sizeof<T>() == 4) return hash32(changetype<u32>(key));
|
||||
if (sizeof<T>() == 8) return hash64(changetype<u64>(key));
|
||||
} else if (isFloat<T>()) {
|
||||
// @ts-ignore: type
|
||||
if (sizeof<T>() == 4) return hash32(reinterpret<u32>(key));
|
||||
// @ts-ignore: type
|
||||
if (sizeof<T>() == 8) return hash64(reinterpret<u64>(key));
|
||||
} else {
|
||||
// @ts-ignore: type
|
||||
if (sizeof<T>() == 1) return hash8 (<u32>key);
|
||||
// @ts-ignore: type
|
||||
if (sizeof<T>() == 2) return hash16(<u32>key);
|
||||
// @ts-ignore: type
|
||||
if (sizeof<T>() == 4) return hash32(<u32>key);
|
||||
// @ts-ignore: type
|
||||
if (sizeof<T>() == 8) return hash64(<u64>key);
|
||||
}
|
||||
unreachable();
|
||||
@ -18,7 +26,9 @@
|
||||
|
||||
// FNV-1a 32-bit as a starting point, see: http://isthe.com/chongo/tech/comp/fnv/
|
||||
|
||||
// @ts-ignore: decorator
|
||||
@inline const FNV_OFFSET: u32 = 2166136261;
|
||||
// @ts-ignore: decorator
|
||||
@inline const FNV_PRIME: u32 = 16777619;
|
||||
|
||||
function hash8(key: u32): u32 {
|
||||
|
@ -1,8 +1,10 @@
|
||||
import { runtime } from "../runtime";
|
||||
import { CharCode } from "./string";
|
||||
|
||||
// @ts-ignore: decorator
|
||||
@inline export const MAX_DOUBLE_LENGTH = 28;
|
||||
|
||||
// @ts-ignore: decorator
|
||||
@lazy @inline const POWERS10: u32[] = [
|
||||
1,
|
||||
10,
|
||||
@ -30,6 +32,7 @@ import { CharCode } from "./string";
|
||||
"80", "81", "82", "83", "84", "85", "86", "87", "88", "89",
|
||||
"90", "91", "92", "93", "94", "95", "96", "97", "98", "99"
|
||||
*/
|
||||
// @ts-ignore: decorator
|
||||
@lazy @inline const DIGITS: u32[] = [
|
||||
0x00300030, 0x00310030, 0x00320030, 0x00330030, 0x00340030,
|
||||
0x00350030, 0x00360030, 0x00370030, 0x00380030, 0x00390030,
|
||||
@ -53,6 +56,7 @@ import { CharCode } from "./string";
|
||||
0x00350039, 0x00360039, 0x00370039, 0x00380039, 0x00390039
|
||||
];
|
||||
|
||||
// @ts-ignore: decorator
|
||||
@lazy @inline const EXP_POWERS: i16[] = [
|
||||
-1220, -1193, -1166, -1140, -1113, -1087, -1060, -1034, -1007, -980,
|
||||
-954, -927, -901, -874, -847, -821, -794, -768, -741, -715,
|
||||
@ -66,6 +70,7 @@ import { CharCode } from "./string";
|
||||
];
|
||||
|
||||
// 1e-348, 1e-340, ..., 1e340
|
||||
// @ts-ignore: decorator
|
||||
@lazy @inline const FRC_POWERS: u64[] = [
|
||||
0xFA8FD5A0081C0288, 0xBAAEE17FA23EBF76, 0x8B16FB203055AC76, 0xCF42894A5DCE35EA,
|
||||
0x9A6BB0AA55653B2D, 0xE61ACF033D1A45DF, 0xAB70FE17C79AC6CA, 0xFF77B1FCBEBCDC4F,
|
||||
|
@ -1,45 +1,59 @@
|
||||
import { compareImpl } from "./string";
|
||||
|
||||
// @ts-ignore: decorator
|
||||
@inline export function COMPARATOR<T>(): (a: T, b: T) => i32 {
|
||||
if (isInteger<T>()) {
|
||||
if (isSigned<T>() && sizeof<T>() <= 4) {
|
||||
// @ts-ignore: type
|
||||
return (a: T, b: T): i32 => (<i32>(a - b));
|
||||
} else {
|
||||
// @ts-ignore: type
|
||||
return (a: T, b: T): i32 => (<i32>(a > b) - <i32>(a < b));
|
||||
}
|
||||
} else if (isFloat<T>()) {
|
||||
if (sizeof<T>() == 4) {
|
||||
return (a: T, b: T): i32 => {
|
||||
// @ts-ignore: type
|
||||
var ia = reinterpret<i32>(a);
|
||||
// @ts-ignore: type
|
||||
var ib = reinterpret<i32>(b);
|
||||
ia ^= (ia >> 31) >>> 1;
|
||||
ib ^= (ib >> 31) >>> 1;
|
||||
// @ts-ignore: type
|
||||
return <i32>(ia > ib) - <i32>(ia < ib);
|
||||
};
|
||||
} else {
|
||||
return (a: T, b: T): i32 => {
|
||||
// @ts-ignore: type
|
||||
var ia = reinterpret<i64>(a);
|
||||
// @ts-ignore: type
|
||||
var ib = reinterpret<i64>(b);
|
||||
ia ^= (ia >> 63) >>> 1;
|
||||
ib ^= (ib >> 63) >>> 1;
|
||||
// @ts-ignore: type
|
||||
return <i32>(ia > ib) - <i32>(ia < ib);
|
||||
};
|
||||
}
|
||||
} else if (isString<T>()) {
|
||||
return (a: T, b: T): i32 => {
|
||||
if (a === b || a === null || b === null) return 0;
|
||||
// @ts-ignore: type
|
||||
var alen = (<string>a).length;
|
||||
// @ts-ignore: type
|
||||
var blen = (<string>b).length;
|
||||
if (!alen && !blen) return 0;
|
||||
if (!alen) return -1;
|
||||
if (!blen) return 1;
|
||||
// @ts-ignore: type
|
||||
return compareImpl(<string>a, 0, <string>b, 0, <usize>min(alen, blen));
|
||||
};
|
||||
} else {
|
||||
// @ts-ignore: type
|
||||
return (a: T, b: T): i32 => (<i32>(a > b) - <i32>(a < b));
|
||||
}
|
||||
}
|
||||
|
||||
// @ts-ignore: decorator
|
||||
@inline export function SORT<T>(
|
||||
dataStart: usize,
|
||||
length: i32,
|
||||
|
@ -8,6 +8,7 @@ export function compareImpl(str1: string, index1: usize, str2: string, index2: u
|
||||
return result;
|
||||
}
|
||||
|
||||
// @ts-ignore: decorator
|
||||
@inline export const enum CharCode {
|
||||
PLUS = 0x2B,
|
||||
MINUS = 0x2D,
|
||||
@ -57,29 +58,35 @@ export function isWhiteSpaceOrLineTerminator(c: u16): bool {
|
||||
/** Parses a string to an integer (usually), using the specified radix. */
|
||||
export function parse<T>(str: string, radix: i32 = 0): T {
|
||||
var len: i32 = str.length;
|
||||
// @ts-ignore: type
|
||||
if (!len) return <T>NaN;
|
||||
|
||||
var ptr = changetype<usize>(str) /* + HEAD -> offset */;
|
||||
var code = <i32>load<u16>(ptr, HEADER_SIZE);
|
||||
var code = <i32>load<u16>(ptr);
|
||||
|
||||
// determine sign
|
||||
var sign: T;
|
||||
if (code == CharCode.MINUS) {
|
||||
// @ts-ignore: type
|
||||
if (!--len) return <T>NaN;
|
||||
code = <i32>load<u16>(ptr += 2, HEADER_SIZE);
|
||||
code = <i32>load<u16>(ptr += 2);
|
||||
// @ts-ignore: type
|
||||
sign = -1;
|
||||
} else if (code == CharCode.PLUS) {
|
||||
// @ts-ignore: type
|
||||
if (!--len) return <T>NaN;
|
||||
code = <i32>load<u16>(ptr += 2, HEADER_SIZE);
|
||||
code = <i32>load<u16>(ptr += 2);
|
||||
// @ts-ignore: type
|
||||
sign = 1;
|
||||
} else {
|
||||
// @ts-ignore: type
|
||||
sign = 1;
|
||||
}
|
||||
|
||||
// determine radix
|
||||
if (!radix) {
|
||||
if (code == CharCode._0 && len > 2) {
|
||||
switch (<i32>load<u16>(ptr + 2, HEADER_SIZE)) {
|
||||
switch (<i32>load<u16>(ptr + 2)) {
|
||||
case CharCode.B:
|
||||
case CharCode.b: {
|
||||
ptr += 4; len -= 2;
|
||||
@ -102,13 +109,15 @@ export function parse<T>(str: string, radix: i32 = 0): T {
|
||||
}
|
||||
} else radix = 10;
|
||||
} else if (radix < 2 || radix > 36) {
|
||||
// @ts-ignore: cast
|
||||
return <T>NaN;
|
||||
}
|
||||
|
||||
// calculate value
|
||||
// @ts-ignore: type
|
||||
var num: T = 0;
|
||||
while (len--) {
|
||||
code = <i32>load<u16>(ptr, HEADER_SIZE);
|
||||
code = <i32>load<u16>(ptr);
|
||||
if (code >= CharCode._0 && code <= CharCode._9) {
|
||||
code -= CharCode._0;
|
||||
} else if (code >= CharCode.A && code <= CharCode.Z) {
|
||||
@ -117,8 +126,10 @@ export function parse<T>(str: string, radix: i32 = 0): T {
|
||||
code -= CharCode.a - 10;
|
||||
} else break;
|
||||
if (code >= radix) break;
|
||||
// @ts-ignore: type
|
||||
num = (num * radix) + code;
|
||||
ptr += 2;
|
||||
}
|
||||
// @ts-ignore: type
|
||||
return sign * num;
|
||||
}
|
||||
|
Reference in New Issue
Block a user