@jonobr1/force-directed-graph
Version:
GPU supercharged attraction-graph visualizations for the web built on top of Three.js
1 lines • 294 kB
Source Map (JSON)
{"version":3,"sources":["~lib/rt/common.ts","src/wasm/texture-processor.ts","~lib/util/math.ts","~lib/util/number.ts","~lib/util/string.ts","~lib/math.ts","~lib/memory.ts","~lib/rt/stub.ts","~lib/util/error.ts"],"names":[],"mappings":"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","sourceRoot":"./texture-processor","sourcesContent":["// Alignment guarantees\n\n// @ts-ignore: decorator\n@inline export const AL_BITS: u32 = 4; // 16 bytes to fit up to v128\n// @ts-ignore: decorator\n@inline export const AL_SIZE: usize = 1 << <usize>AL_BITS;\n// @ts-ignore: decorator\n@inline export const AL_MASK: usize = AL_SIZE - 1;\n\n// Extra debugging\n\n// @ts-ignore: decorator\n@inline export const DEBUG = true;\n// @ts-ignore: decorator\n@inline export const TRACE = false;\n// @ts-ignore: decorator\n@inline export const RTRACE = isDefined(ASC_RTRACE);\n// @ts-ignore: decorator\n@inline export const PROFILE = isDefined(ASC_PROFILE);\n\n// Memory manager\n\n// ╒════════════ Memory manager block layout (32-bit) ═════════════╕\n// 3 2 1\n// 1 0 9 8 7 6 5 4 3 2 1 0 9 8 7 6 5 4 3 2 1 0 9 8 7 6 5 4 3 2 1 0 bits\n// ├─┴─┴─┴─┴─┴─┴─┴─┴─┴─┴─┴─┴─┴─┴─┴─┴─┴─┴─┴─┴─┴─┴─┴─┴─┴─┴─┴─┴─┴─┴─┴─┤\n// │ MM info │ -4\n// ╞>ptr═══════════════════════════════════════════════════════════╡\n// │ ... │\n@unmanaged export class BLOCK {\n /** Memory manager info. */\n mmInfo: usize;\n}\n\n/** Overhead of a memory manager block. */\n// @ts-ignore: decorator\n@inline export const BLOCK_OVERHEAD: usize = offsetof<BLOCK>();\n\n/** Maximum size of a memory manager block's payload. */\n// @ts-ignore: decorator\n@inline export const BLOCK_MAXSIZE: usize = (1 << 30) - BLOCK_OVERHEAD;\n\n// Garbage collector\n\n// ╒══════════ Garbage collector object layout (32-bit) ═══════════╕\n// 3 2 1\n// 1 0 9 8 7 6 5 4 3 2 1 0 9 8 7 6 5 4 3 2 1 0 9 8 7 6 5 4 3 2 1 0 bits\n// ├─┴─┴─┴─┴─┴─┴─┴─┴─┴─┴─┴─┴─┴─┴─┴─┴─┴─┴─┴─┴─┴─┴─┴─┴─┴─┴─┴─┴─┴─┴─┴─┤\n// │ Memory manager block │ -20\n// ╞═══════════════════════════════════════════════════════════════╡\n// │ GC info │ -16\n// ├───────────────────────────────────────────────────────────────┤\n// │ GC info │ -12\n// ├───────────────────────────────────────────────────────────────┤\n// │ RT id │ -8\n// ├───────────────────────────────────────────────────────────────┤\n// │ RT size │ -4\n// ╞>ptr═══════════════════════════════════════════════════════════╡\n// │ ... │\n@unmanaged export class OBJECT extends BLOCK {\n /** Garbage collector info. */\n gcInfo: u32;\n /** Garbage collector info. */\n gcInfo2: u32;\n /** Runtime class id. */\n rtId: u32;\n /** Runtime object size. */\n rtSize: u32;\n}\n\n/** Overhead of a garbage collector object. Excludes memory manager block overhead. */\n// @ts-ignore: decorator\n@inline export const OBJECT_OVERHEAD: usize = (offsetof<OBJECT>() - BLOCK_OVERHEAD + AL_MASK) & ~AL_MASK;\n\n/** Maximum size of a garbage collector object's payload. */\n// @ts-ignore: decorator\n@inline export const OBJECT_MAXSIZE: usize = BLOCK_MAXSIZE - OBJECT_OVERHEAD;\n\n/** Total of memory manager and garbage collector overhead. */\n// @ts-ignore: decorator\n@inline export const TOTAL_OVERHEAD: usize = BLOCK_OVERHEAD + OBJECT_OVERHEAD;\n","/**\n * AssemblyScript texture processor for Force Directed Graph\n * Handles high-performance texture data processing for GPU compute shaders\n */\n\n// Memory layout constants\nconst FLOAT32_BYTES = 4;\nconst RGBA_COMPONENTS = 4;\n\n// Export memory to be accessible from JavaScript\nexport declare const __heap_base: usize;\n\n/**\n * Process node positions into texture data\n * @param nodesDataPtr Pointer to serialized node data\n * @param nodesCount Number of nodes\n * @param textureSize Power-of-2 texture size\n * @param positionsPtr Pointer to output positions texture data\n * @param frustumSize Frustum size for out-of-bounds nodes\n */\nexport function processNodePositions(\n nodesDataPtr: usize,\n nodesCount: i32,\n textureSize: i32,\n positionsPtr: usize,\n frustumSize: f32\n): void {\n const totalElements = textureSize * textureSize;\n \n for (let i = 0; i < totalElements; i++) {\n const positionOffset = positionsPtr + i * RGBA_COMPONENTS * FLOAT32_BYTES;\n \n if (i < nodesCount) {\n // Read node data (x, y, z, isStatic) using direct memory access\n const nodeOffset = nodesDataPtr + i * 4 * FLOAT32_BYTES;\n const x = load<f32>(nodeOffset + 0 * FLOAT32_BYTES);\n const y = load<f32>(nodeOffset + 1 * FLOAT32_BYTES);\n const z = load<f32>(nodeOffset + 2 * FLOAT32_BYTES);\n const isStatic = load<f32>(nodeOffset + 3 * FLOAT32_BYTES);\n \n // Use provided position or random fallback\n store<f32>(positionOffset + 0 * FLOAT32_BYTES, !isFinite(x) ? f32(Math.random() * 2.0 - 1.0) : x);\n store<f32>(positionOffset + 1 * FLOAT32_BYTES, !isFinite(y) ? f32(Math.random() * 2.0 - 1.0) : y);\n store<f32>(positionOffset + 2 * FLOAT32_BYTES, !isFinite(z) ? f32(Math.random() * 2.0 - 1.0) : z);\n store<f32>(positionOffset + 3 * FLOAT32_BYTES, isStatic);\n } else {\n // Place extraneous nodes far away\n const farAway = frustumSize * 10.0;\n store<f32>(positionOffset + 0 * FLOAT32_BYTES, farAway);\n store<f32>(positionOffset + 1 * FLOAT32_BYTES, farAway);\n store<f32>(positionOffset + 2 * FLOAT32_BYTES, farAway);\n store<f32>(positionOffset + 3 * FLOAT32_BYTES, 0.0);\n }\n }\n}\n\n/**\n * Process links into texture data with UV coordinates\n * @param linksDataPtr Pointer to serialized link data (source, target indices)\n * @param linksCount Number of links\n * @param textureSize Power-of-2 texture size\n * @param linksTexturePtr Pointer to output links texture data\n */\nexport function processLinks(\n linksDataPtr: usize,\n linksCount: i32,\n nodesCount: i32,\n textureSize: i32,\n linksTexturePtr: usize,\n linkRangesTexturePtr: usize\n): i32 {\n const totalElements = textureSize * textureSize;\n const textureSizeF = f32(textureSize);\n const texelStride = RGBA_COMPONENTS * FLOAT32_BYTES;\n\n for (let i = 0; i < totalElements; i++) {\n const linkOffset = linksTexturePtr + i * texelStride;\n const rangeOffset = linkRangesTexturePtr + i * texelStride;\n\n // Clear output textures before writing packed link data.\n store<f32>(linkOffset + 0 * FLOAT32_BYTES, 0.0);\n store<f32>(linkOffset + 1 * FLOAT32_BYTES, 0.0);\n store<f32>(linkOffset + 2 * FLOAT32_BYTES, 0.0);\n store<f32>(linkOffset + 3 * FLOAT32_BYTES, 0.0);\n store<f32>(rangeOffset + 0 * FLOAT32_BYTES, 0.0);\n store<f32>(rangeOffset + 1 * FLOAT32_BYTES, 0.0);\n store<f32>(rangeOffset + 2 * FLOAT32_BYTES, 0.0);\n store<f32>(rangeOffset + 3 * FLOAT32_BYTES, 0.0);\n }\n\n if (nodesCount <= 0) {\n return 0;\n }\n\n const intsSize = nodesCount * FLOAT32_BYTES;\n const degreeCountsPtr = heap.alloc(intsSize);\n const startOffsetsPtr = heap.alloc(intsSize);\n const cursorsPtr = heap.alloc(intsSize);\n\n for (let i = 0; i < nodesCount; i++) {\n const offset = i * FLOAT32_BYTES;\n store<i32>(degreeCountsPtr + offset, 0);\n store<i32>(startOffsetsPtr + offset, 0);\n store<i32>(cursorsPtr + offset, 0);\n }\n\n for (let i = 0; i < linksCount; i++) {\n const linkDataOffset = linksDataPtr + i * 2 * FLOAT32_BYTES;\n const sourceIndex = load<i32>(linkDataOffset + 0 * FLOAT32_BYTES);\n const targetIndex = load<i32>(linkDataOffset + 1 * FLOAT32_BYTES);\n\n const isValid =\n sourceIndex >= 0 &&\n sourceIndex < nodesCount &&\n targetIndex >= 0 &&\n targetIndex < nodesCount;\n\n if (!isValid) {\n continue;\n }\n\n const sourceOffset = sourceIndex * FLOAT32_BYTES;\n store<i32>(degreeCountsPtr + sourceOffset, load<i32>(degreeCountsPtr + sourceOffset) + 1);\n\n if (sourceIndex != targetIndex) {\n const targetOffset = targetIndex * FLOAT32_BYTES;\n store<i32>(degreeCountsPtr + targetOffset, load<i32>(degreeCountsPtr + targetOffset) + 1);\n }\n }\n\n let packedLinkAmount = 0;\n for (let i = 0; i < nodesCount; i++) {\n const nodeOffset = i * FLOAT32_BYTES;\n const count = load<i32>(degreeCountsPtr + nodeOffset);\n const start = packedLinkAmount;\n\n store<i32>(startOffsetsPtr + nodeOffset, start);\n store<i32>(cursorsPtr + nodeOffset, start);\n packedLinkAmount += count;\n\n const rangeOffset = linkRangesTexturePtr + i * texelStride;\n store<f32>(rangeOffset + 0 * FLOAT32_BYTES, f32(start));\n store<f32>(rangeOffset + 1 * FLOAT32_BYTES, f32(count));\n }\n\n if (packedLinkAmount > totalElements) {\n heap.free(cursorsPtr);\n heap.free(startOffsetsPtr);\n heap.free(degreeCountsPtr);\n return -1;\n }\n\n for (let i = 0; i < linksCount; i++) {\n const linkDataOffset = linksDataPtr + i * 2 * FLOAT32_BYTES;\n const sourceIndex = load<i32>(linkDataOffset + 0 * FLOAT32_BYTES);\n const targetIndex = load<i32>(linkDataOffset + 1 * FLOAT32_BYTES);\n\n const isValid =\n sourceIndex >= 0 &&\n sourceIndex < nodesCount &&\n targetIndex >= 0 &&\n targetIndex < nodesCount;\n\n if (!isValid) {\n continue;\n }\n\n const sourceOffset = sourceIndex * FLOAT32_BYTES;\n let sourceCursor = load<i32>(cursorsPtr + sourceOffset);\n store<i32>(cursorsPtr + sourceOffset, sourceCursor + 1);\n\n let linkOffset = linksTexturePtr + sourceCursor * texelStride;\n store<f32>(linkOffset + 0 * FLOAT32_BYTES, f32(sourceIndex % textureSize) / textureSizeF);\n store<f32>(linkOffset + 1 * FLOAT32_BYTES, f32(sourceIndex / textureSize) / textureSizeF);\n store<f32>(linkOffset + 2 * FLOAT32_BYTES, f32(targetIndex % textureSize) / textureSizeF);\n store<f32>(linkOffset + 3 * FLOAT32_BYTES, f32(targetIndex / textureSize) / textureSizeF);\n\n if (sourceIndex != targetIndex) {\n const targetOffset = targetIndex * FLOAT32_BYTES;\n let targetCursor = load<i32>(cursorsPtr + targetOffset);\n store<i32>(cursorsPtr + targetOffset, targetCursor + 1);\n\n linkOffset = linksTexturePtr + targetCursor * texelStride;\n store<f32>(linkOffset + 0 * FLOAT32_BYTES, f32(sourceIndex % textureSize) / textureSizeF);\n store<f32>(linkOffset + 1 * FLOAT32_BYTES, f32(sourceIndex / textureSize) / textureSizeF);\n store<f32>(linkOffset + 2 * FLOAT32_BYTES, f32(targetIndex % textureSize) / textureSizeF);\n store<f32>(linkOffset + 3 * FLOAT32_BYTES, f32(targetIndex / textureSize) / textureSizeF);\n }\n }\n\n heap.free(cursorsPtr);\n heap.free(startOffsetsPtr);\n heap.free(degreeCountsPtr);\n\n return packedLinkAmount;\n}\n\n/**\n * Combined processing function for both nodes and links\n * @param nodesDataPtr Pointer to serialized node data\n * @param nodesCount Number of nodes\n * @param linksDataPtr Pointer to serialized link data\n * @param linksCount Number of links\n * @param textureSize Power-of-2 texture size\n * @param positionsPtr Pointer to output positions texture data\n * @param linksTexturePtr Pointer to output links texture data\n * @param frustumSize Frustum size for out-of-bounds nodes\n */\nexport function processTextures(\n nodesDataPtr: usize,\n nodesCount: i32,\n linksDataPtr: usize,\n linksCount: i32,\n textureSize: i32,\n positionsPtr: usize,\n linksTexturePtr: usize,\n linkRangesTexturePtr: usize,\n frustumSize: f32\n): i32 {\n processNodePositions(nodesDataPtr, nodesCount, textureSize, positionsPtr, frustumSize);\n return processLinks(\n linksDataPtr,\n linksCount,\n nodesCount,\n textureSize,\n linksTexturePtr,\n linkRangesTexturePtr\n );\n}\n\n/**\n * Allocate memory for texture data\n * @param size Size in bytes\n * @returns Pointer to allocated memory\n */\nexport function allocateMemory(size: i32): usize {\n return heap.alloc(size);\n}\n\n/**\n * Free allocated memory\n * @param ptr Pointer to memory to free\n */\nexport function freeMemory(ptr: usize): void {\n heap.free(ptr);\n}\n\n/**\n * Get memory usage statistics\n * @returns Memory usage in bytes\n */\nexport function getMemoryUsage(): i32 {\n return i32(memory.size() * 65536); // Pages to bytes\n}\n","//\n// Lookup data for exp2f\n//\n\n// @ts-ignore: decorator\n@inline const EXP2F_TABLE_BITS = 5;\n\n// @ts-ignore: decorator\n@lazy @inline const EXP2F_DATA_TAB = memory.data<u64>([\n // exp2f_data_tab[i] = uint(2^(i/N)) - (i << 52-BITS)\n // used for computing 2^(k/N) for an int |k| < 150 N as\n // double(tab[k%N] + (k << 52-BITS))\n 0x3FF0000000000000, 0x3FEFD9B0D3158574, 0x3FEFB5586CF9890F, 0x3FEF9301D0125B51,\n 0x3FEF72B83C7D517B, 0x3FEF54873168B9AA, 0x3FEF387A6E756238, 0x3FEF1E9DF51FDEE1,\n 0x3FEF06FE0A31B715, 0x3FEEF1A7373AA9CB, 0x3FEEDEA64C123422, 0x3FEECE086061892D,\n 0x3FEEBFDAD5362A27, 0x3FEEB42B569D4F82, 0x3FEEAB07DD485429, 0x3FEEA47EB03A5585,\n 0x3FEEA09E667F3BCD, 0x3FEE9F75E8EC5F74, 0x3FEEA11473EB0187, 0x3FEEA589994CCE13,\n 0x3FEEACE5422AA0DB, 0x3FEEB737B0CDC5E5, 0x3FEEC49182A3F090, 0x3FEED503B23E255D,\n 0x3FEEE89F995AD3AD, 0x3FEEFF76F2FB5E47, 0x3FEF199BDD85529C, 0x3FEF3720DCEF9069,\n 0x3FEF5818DCFBA487, 0x3FEF7C97337B9B5F, 0x3FEFA4AFA2A490DA, 0x3FEFD0765B6E4540\n]);\n\n// ULP error: 0.502 (nearest rounding.)\n// Relative error: 1.69 * 2^-34 in [-1/64, 1/64] (before rounding.)\n// Wrong count: 168353 (all nearest rounding wrong results with fma.)\n// @ts-ignore: decorator\n@inline\nexport function exp2f_lut(x: f32): f32 {\n const\n N = 1 << EXP2F_TABLE_BITS,\n N_MASK = N - 1,\n shift = reinterpret<f64>(0x4338000000000000) / N, // 0x1.8p+52\n Ox127f = reinterpret<f32>(0x7F000000);\n\n const\n C0 = reinterpret<f64>(0x3FAC6AF84B912394), // 0x1.c6af84b912394p-5\n C1 = reinterpret<f64>(0x3FCEBFCE50FAC4F3), // 0x1.ebfce50fac4f3p-3\n C2 = reinterpret<f64>(0x3FE62E42FF0C52D6); // 0x1.62e42ff0c52d6p-1\n\n let xd = <f64>x;\n let ix = reinterpret<u32>(x);\n let ux = ix >> 20 & 0x7FF;\n if (ux >= 0x430) {\n // |x| >= 128 or x is nan.\n if (ix == 0xFF800000) return 0; // x == -Inf -> 0\n if (ux >= 0x7F8) return x + x; // x == Inf/NaN -> Inf/NaN\n if (x > 0) return x * Ox127f; // x > 0 -> HugeVal (Owerflow)\n if (x <= -150) return 0; // x <= -150 -> 0 (Underflow)\n }\n\n // x = k/N + r with r in [-1/(2N), 1/(2N)] and int k.\n let kd = xd + shift;\n let ki = reinterpret<u64>(kd);\n let r = xd - (kd - shift);\n let t: u64, y: f64, s: f64;\n\n // exp2(x) = 2^(k/N) * 2^r ~= s * (C0*r^3 + C1*r^2 + C2*r + 1)\n t = load<u64>(EXP2F_DATA_TAB + ((<usize>ki & N_MASK) << alignof<u64>()));\n t += ki << (52 - EXP2F_TABLE_BITS);\n s = reinterpret<f64>(t);\n y = C2 * r + 1;\n y += (C0 * r + C1) * (r * r);\n y *= s;\n\n return <f32>y;\n}\n\n// ULP error: 0.502 (nearest rounding.)\n// Relative error: 1.69 * 2^-34 in [-ln2/64, ln2/64] (before rounding.)\n// Wrong count: 170635 (all nearest rounding wrong results with fma.)\n// @ts-ignore: decorator\n@inline\nexport function expf_lut(x: f32): f32 {\n const\n N = 1 << EXP2F_TABLE_BITS,\n N_MASK = N - 1,\n shift = reinterpret<f64>(0x4338000000000000), // 0x1.8p+52\n InvLn2N = reinterpret<f64>(0x3FF71547652B82FE) * N, // 0x1.71547652b82fep+0\n Ox1p127f = reinterpret<f32>(0x7F000000);\n\n const\n C0 = reinterpret<f64>(0x3FAC6AF84B912394) / N / N / N, // 0x1.c6af84b912394p-5\n C1 = reinterpret<f64>(0x3FCEBFCE50FAC4F3) / N / N, // 0x1.ebfce50fac4f3p-3\n C2 = reinterpret<f64>(0x3FE62E42FF0C52D6) / N; // 0x1.62e42ff0c52d6p-1\n\n let xd = <f64>x;\n let ix = reinterpret<u32>(x);\n let ux = ix >> 20 & 0x7FF;\n if (ux >= 0x42B) {\n // |x| >= 88 or x is nan.\n if (ix == 0xFF800000) return 0; // x == -Inf -> 0\n if (ux >= 0x7F8) return x + x; // x == Inf/NaN -> Inf/NaN\n if (x > reinterpret<f32>(0x42B17217)) return x * Ox1p127f; // x > log(0x1p128) ~= 88.72 -> HugeVal (Owerflow)\n if (x < reinterpret<f32>(0xC2CFF1B4)) return 0; // x < log(0x1p-150) ~= -103.97 -> 0 (Underflow)\n }\n\n // x*N/Ln2 = k + r with r in [-1/2, 1/2] and int k.\n let z = InvLn2N * xd;\n\n // Round and convert z to int, the result is in [-150*N, 128*N] and\n // ideally ties-to-even rule is used, otherwise the magnitude of r\n // can be bigger which gives larger approximation error.\n let kd = <f64>(z + shift);\n let ki = reinterpret<u64>(kd);\n let r = z - (kd - shift);\n let s: f64, y: f64, t: u64;\n\n // exp(x) = 2^(k/N) * 2^(r/N) ~= s * (C0*r^3 + C1*r^2 + C2*r + 1)\n t = load<u64>(EXP2F_DATA_TAB + ((<usize>ki & N_MASK) << alignof<u64>()));\n t += ki << (52 - EXP2F_TABLE_BITS);\n s = reinterpret<f64>(t);\n z = C0 * r + C1;\n y = C2 * r + 1;\n y += z * (r * r);\n y *= s;\n\n return <f32>y;\n}\n\n//\n// Lookup data for log2f\n//\n\n// @ts-ignore: decorator\n@inline const LOG2F_TABLE_BITS = 4;\n\n// @ts-ignore: decorator\n@lazy @inline const LOG2F_DATA_TAB = memory.data<u64>([\n 0x3FF661EC79F8F3BE, 0xBFDEFEC65B963019, // 0x1.661ec79f8f3bep+0, -0x1.efec65b963019p-2,\n 0x3FF571ED4AAF883D, 0xBFDB0B6832D4FCA4, // 0x1.571ed4aaf883dp+0, -0x1.b0b6832d4fca4p-2,\n 0x3FF49539F0F010B0, 0xBFD7418B0A1FB77B, // 0x1.49539f0f010bp+0 , -0x1.7418b0a1fb77bp-2,\n 0x3FF3C995B0B80385, 0xBFD39DE91A6DCF7B, // 0x1.3c995b0b80385p+0, -0x1.39de91a6dcf7bp-2,\n 0x3FF30D190C8864A5, 0xBFD01D9BF3F2B631, // 0x1.30d190c8864a5p+0, -0x1.01d9bf3f2b631p-2,\n 0x3FF25E227B0B8EA0, 0xBFC97C1D1B3B7AF0, // 0x1.25e227b0b8eap+0 , -0x1.97c1d1b3b7afp-3 ,\n 0x3FF1BB4A4A1A343F, 0xBFC2F9E393AF3C9F, // 0x1.1bb4a4a1a343fp+0, -0x1.2f9e393af3c9fp-3,\n 0x3FF12358F08AE5BA, 0xBFB960CBBF788D5C, // 0x1.12358f08ae5bap+0, -0x1.960cbbf788d5cp-4,\n 0x3FF0953F419900A7, 0xBFAA6F9DB6475FCE, // 0x1.0953f419900a7p+0, -0x1.a6f9db6475fcep-5,\n 0x3FF0000000000000, 0, // 0x1p+0, 0x0,\n 0x3FEE608CFD9A47AC, 0x3FB338CA9F24F53D, // 0x1.e608cfd9a47acp-1, 0x1.338ca9f24f53dp-4,\n 0x3FECA4B31F026AA0, 0x3FC476A9543891BA, // 0x1.ca4b31f026aap-1 , 0x1.476a9543891bap-3,\n 0x3FEB2036576AFCE6, 0x3FCE840B4AC4E4D2, // 0x1.b2036576afce6p-1, 0x1.e840b4ac4e4d2p-3,\n 0x3FE9C2D163A1AA2D, 0x3FD40645F0C6651C, // 0x1.9c2d163a1aa2dp-1, 0x1.40645f0c6651cp-2,\n 0x3FE886E6037841ED, 0x3FD88E9C2C1B9FF8, // 0x1.886e6037841edp-1, 0x1.88e9c2c1b9ff8p-2,\n 0x3FE767DCF5534862, 0x3FDCE0A44EB17BCC // 0x1.767dcf5534862p-1, 0x1.ce0a44eb17bccp-2\n]);\n\n// ULP error: 0.752 (nearest rounding.)\n// Relative error: 1.9 * 2^-26 (before rounding.)\n// @ts-ignore: decorator\n@inline\nexport function log2f_lut(x: f32): f32 {\n const\n N_MASK = (1 << LOG2F_TABLE_BITS) - 1,\n Ox1p23f = reinterpret<f32>(0x4B000000); // 0x1p23f\n\n const\n A0 = reinterpret<f64>(0xBFD712B6F70A7E4D), // -0x1.712b6f70a7e4dp-2\n A1 = reinterpret<f64>(0x3FDECABF496832E0), // 0x1.ecabf496832ep-2\n A2 = reinterpret<f64>(0xBFE715479FFAE3DE), // -0x1.715479ffae3dep-1\n A3 = reinterpret<f64>(0x3FF715475F35C8B8); // 0x1.715475f35c8b8p0\n\n let ux = reinterpret<u32>(x);\n // Fix sign of zero with downward rounding when x==1.\n // if (WANT_ROUNDING && predict_false(ix == 0x3f800000)) return 0;\n if (ux - 0x00800000 >= 0x7F800000 - 0x00800000) {\n // x < 0x1p-126 or inf or nan.\n if (ux * 2 == 0) return -Infinity;\n if (ux == 0x7F800000) return x; // log2(inf) == inf.\n if ((ux >> 31) || ux * 2 >= 0xFF000000) return (x - x) / (x - x);\n // x is subnormal, normalize it.\n ux = reinterpret<u32>(x * Ox1p23f);\n ux -= 23 << 23;\n }\n // x = 2^k z; where z is in range [OFF,2*OFF] and exact.\n // The range is split into N subintervals.\n // The ith subinterval contains z and c is near its center.\n let tmp = ux - 0x3F330000;\n let i = (tmp >> (23 - LOG2F_TABLE_BITS)) & N_MASK;\n let top = tmp & 0xFF800000;\n let iz = ux - top;\n let k = <i32>tmp >> 23;\n\n let invc = load<f64>(LOG2F_DATA_TAB + (i << (1 + alignof<f64>())), 0 << alignof<f64>());\n let logc = load<f64>(LOG2F_DATA_TAB + (i << (1 + alignof<f64>())), 1 << alignof<f64>());\n let z = <f64>reinterpret<f32>(iz);\n\n // log2(x) = log1p(z/c-1)/ln2 + log2(c) + k\n let r = z * invc - 1;\n let y0 = logc + <f64>k;\n\n // Pipelined polynomial evaluation to approximate log1p(r)/ln2.\n let y = A1 * r + A2;\n let p = A3 * r + y0;\n let r2 = r * r;\n y += A0 * r2;\n y = y * r2 + p;\n\n return <f32>y;\n}\n\n//\n// Lookup data for logf. See: https://git.musl-libc.org/cgit/musl/tree/src/math/logf.c\n//\n\n// @ts-ignore: decorator\n@inline const LOGF_TABLE_BITS = 4;\n\n// @ts-ignore: decorator\n@lazy @inline const LOGF_DATA_TAB = memory.data<u64>([\n 0x3FF661EC79F8F3BE, 0xBFD57BF7808CAADE, // 0x1.661ec79f8f3bep+0, -0x1.57bf7808caadep-2,\n 0x3FF571ED4AAF883D, 0xBFD2BEF0A7C06DDB, // 0x1.571ed4aaf883dp+0, -0x1.2bef0a7c06ddbp-2,\n 0x3FF49539F0F010B0, 0xBFD01EAE7F513A67, // 0x1.49539f0f010bp+0 , -0x1.01eae7f513a67p-2,\n 0x3FF3C995B0B80385, 0xBFCB31D8A68224E9, // 0x1.3c995b0b80385p+0, -0x1.b31d8a68224e9p-3,\n 0x3FF30D190C8864A5, 0xBFC6574F0AC07758, // 0x1.30d190c8864a5p+0, -0x1.6574f0ac07758p-3,\n 0x3FF25E227B0B8EA0, 0xBFC1AA2BC79C8100, // 0x1.25e227b0b8eap+0 , -0x1.1aa2bc79c81p-3 ,\n 0x3FF1BB4A4A1A343F, 0xBFBA4E76CE8C0E5E, // 0x1.1bb4a4a1a343fp+0, -0x1.a4e76ce8c0e5ep-4,\n 0x3FF12358F08AE5BA, 0xBFB1973C5A611CCC, // 0x1.12358f08ae5bap+0, -0x1.1973c5a611cccp-4,\n 0x3FF0953F419900A7, 0xBFA252F438E10C1E, // 0x1.0953f419900a7p+0, -0x1.252f438e10c1ep-5,\n 0x3FF0000000000000, 0, // 0x1p+0, 0,\n 0x3FEE608CFD9A47AC, 0x3FAAA5AA5DF25984, // 0x1.e608cfd9a47acp-1, 0x1.aa5aa5df25984p-5,\n 0x3FECA4B31F026AA0, 0x3FBC5E53AA362EB4, // 0x1.ca4b31f026aap-1 , 0x1.c5e53aa362eb4p-4,\n 0x3FEB2036576AFCE6, 0x3FC526E57720DB08, // 0x1.b2036576afce6p-1, 0x1.526e57720db08p-3,\n 0x3FE9C2D163A1AA2D, 0x3FCBC2860D224770, // 0x1.9c2d163a1aa2dp-1, 0x1.bc2860d22477p-3 ,\n 0x3FE886E6037841ED, 0x3FD1058BC8A07EE1, // 0x1.886e6037841edp-1, 0x1.1058bc8a07ee1p-2,\n 0x3FE767DCF5534862, 0x3FD4043057B6EE09 // 0x1.767dcf5534862p-1, 0x1.4043057b6ee09p-2\n]);\n\n// ULP error: 0.818 (nearest rounding.)\n// Relative error: 1.957 * 2^-26 (before rounding.)\n// @ts-ignore: decorator\n@inline\nexport function logf_lut(x: f32): f32 {\n const\n N_MASK = (1 << LOGF_TABLE_BITS) - 1,\n Ox1p23f = reinterpret<f32>(0x4B000000); // 0x1p23f\n\n const\n Ln2 = reinterpret<f64>(0x3FE62E42FEFA39EF), // 0x1.62e42fefa39efp-1;\n A0 = reinterpret<f64>(0xBFD00EA348B88334), // -0x1.00ea348b88334p-2\n A1 = reinterpret<f64>(0x3FD5575B0BE00B6A), // 0x1.5575b0be00b6ap-2\n A2 = reinterpret<f64>(0xBFDFFFFEF20A4123); // -0x1.ffffef20a4123p-2\n\n let ux = reinterpret<u32>(x);\n // Fix sign of zero with downward rounding when x==1.\n // if (WANT_ROUNDING && ux == 0x3f800000) return 0;\n if (ux - 0x00800000 >= 0x7F800000 - 0x00800000) {\n // x < 0x1p-126 or inf or nan.\n if ((ux << 1) == 0) return -Infinity;\n if (ux == 0x7F800000) return x; // log(inf) == inf.\n if ((ux >> 31) || (ux << 1) >= 0xFF000000) return (x - x) / (x - x);\n // x is subnormal, normalize it.\n ux = reinterpret<u32>(x * Ox1p23f);\n ux -= 23 << 23;\n }\n // x = 2^k z; where z is in range [OFF,2*OFF] and exact.\n // The range is split into N subintervals.\n // The ith subinterval contains z and c is near its center.\n let tmp = ux - 0x3F330000;\n let i = (tmp >> (23 - LOGF_TABLE_BITS)) & N_MASK;\n let k = <i32>tmp >> 23;\n let iz = ux - (tmp & 0x1FF << 23);\n\n let invc = load<f64>(LOGF_DATA_TAB + (i << (1 + alignof<f64>())), 0 << alignof<f64>());\n let logc = load<f64>(LOGF_DATA_TAB + (i << (1 + alignof<f64>())), 1 << alignof<f64>());\n\n let z = <f64>reinterpret<f32>(iz);\n\n // log(x) = log1p(z/c-1) + log(c) + k*Ln2\n let r = z * invc - 1;\n let y0 = logc + <f64>k * Ln2;\n\n // Pipelined polynomial evaluation to approximate log1p(r).\n let r2 = r * r;\n let y = A1 * r + A2;\n y += A0 * r2;\n y = y * r2 + (y0 + r);\n\n return <f32>y;\n}\n\n//\n// Lookup data for powf. See: https://git.musl-libc.org/cgit/musl/tree/src/math/powf.c\n//\n\n// @ts-ignore: decorator\n@inline\nfunction zeroinfnanf(ux: u32): bool {\n return (ux << 1) - 1 >= (<u32>0x7f800000 << 1) - 1;\n}\n\n// Returns 0 if not int, 1 if odd int, 2 if even int. The argument is\n// the bit representation of a non-zero finite floating-point value.\n// @ts-ignore: decorator\n@inline\nfunction checkintf(iy: u32): i32 {\n let e = iy >> 23 & 0xFF;\n if (e < 0x7F ) return 0;\n if (e > 0x7F + 23) return 2;\n e = 1 << (0x7F + 23 - e);\n if (iy & (e - 1)) return 0;\n if (iy & e ) return 1;\n return 2;\n}\n\n// Subnormal input is normalized so ix has negative biased exponent.\n// Output is multiplied by N (POWF_SCALE) if TOINT_INTRINICS is set.\n// @ts-ignore: decorator\n@inline\nfunction log2f_inline(ux: u32): f64 {\n const N_MASK = (1 << LOG2F_TABLE_BITS) - 1;\n\n const\n A0 = reinterpret<f64>(0x3FD27616C9496E0B), // 0x1.27616c9496e0bp-2\n A1 = reinterpret<f64>(0xBFD71969A075C67A), // -0x1.71969a075c67ap-2\n A2 = reinterpret<f64>(0x3FDEC70A6CA7BADD), // 0x1.ec70a6ca7baddp-2\n A3 = reinterpret<f64>(0xBFE7154748BEF6C8), // -0x1.7154748bef6c8p-1\n A4 = reinterpret<f64>(0x3FF71547652AB82B); // 0x1.71547652ab82bp+0\n\n // x = 2^k z; where z is in range [OFF,2*OFF] and exact.\n // The range is split into N subintervals.\n // The ith subinterval contains z and c is near its center.\n let tmp = ux - 0x3F330000;\n let i = usize((tmp >> (23 - LOG2F_TABLE_BITS)) & N_MASK);\n let top = tmp & 0xFF800000;\n let uz = ux - top;\n let k = <i32>top >> 23;\n\n let invc = load<f64>(LOG2F_DATA_TAB + (i << (1 + alignof<f64>())), 0 << alignof<f64>());\n let logc = load<f64>(LOG2F_DATA_TAB + (i << (1 + alignof<f64>())), 1 << alignof<f64>());\n let z = <f64>reinterpret<f32>(uz);\n\n // log2(x) = log1p(z/c-1)/ln2 + log2(c) + k\n let r = z * invc - 1;\n let y0 = logc + <f64>k;\n\n // Pipelined polynomial evaluation to approximate log1p(r)/ln2.\n let y = A0 * r + A1;\n let p = A2 * r + A3;\n let q = A4 * r + y0;\n\n r *= r;\n q += p * r;\n y = y * (r * r) + q;\n\n return y;\n}\n\n// The output of log2 and thus the input of exp2 is either scaled by N\n// (in case of fast toint intrinsics) or not. The unscaled xd must be\n// in [-1021,1023], sign_bias sets the sign of the result.\n// @ts-ignore: decorator\n@inline\nfunction exp2f_inline(xd: f64, signBias: u32): f32 {\n const\n N = 1 << EXP2F_TABLE_BITS,\n N_MASK = N - 1,\n shift = reinterpret<f64>(0x4338000000000000) / N; // 0x1.8p+52\n\n const\n C0 = reinterpret<f64>(0x3FAC6AF84B912394), // 0x1.c6af84b912394p-5\n C1 = reinterpret<f64>(0x3FCEBFCE50FAC4F3), // 0x1.ebfce50fac4f3p-3\n C2 = reinterpret<f64>(0x3FE62E42FF0C52D6); // 0x1.62e42ff0c52d6p-1\n\n // x = k/N + r with r in [-1/(2N), 1/(2N)]\n let kd = <f64>(xd + shift);\n let ki = reinterpret<u64>(kd);\n let r = xd - (kd - shift);\n let t: u64, z: f64, y: f64, s: f64;\n\n // exp2(x) = 2^(k/N) * 2^r ~= s * (C0*r^3 + C1*r^2 + C2*r + 1)\n t = load<u64>(EXP2F_DATA_TAB + ((<usize>ki & N_MASK) << alignof<u64>()));\n t += (ki + signBias) << (52 - EXP2F_TABLE_BITS);\n s = reinterpret<f64>(t);\n z = C0 * r + C1;\n y = C2 * r + 1;\n y += z * (r * r);\n y *= s;\n return <f32>y;\n}\n\n// @ts-ignore: decorator\n@inline\nfunction xflowf(sign: u32, y: f32): f32 {\n return select<f32>(-y, y, sign) * y;\n}\n\n// @ts-ignore: decorator\n@inline\nfunction oflowf(sign: u32): f32 {\n return xflowf(sign, reinterpret<f32>(0x70000000)); // 0x1p97f\n}\n\n// @ts-ignore: decorator\n@inline\nfunction uflowf(sign: u32): f32 {\n return xflowf(sign, reinterpret<f32>(0x10000000)); // 0x1p-95f\n}\n\n// @ts-ignore: decorator\n@inline\nexport function powf_lut(x: f32, y: f32): f32 {\n const\n Ox1p23f = reinterpret<f32>(0x4B000000), // 0x1p23f\n UPPER_LIMIT = reinterpret<f64>(0x405FFFFFFFD1D571), // 0x1.fffffffd1d571p+6\n LOWER_LIMIT = -150.0,\n SIGN_BIAS = 1 << (EXP2F_TABLE_BITS + 11);\n\n let signBias: u32 = 0;\n let ix = reinterpret<u32>(x);\n let iy = reinterpret<u32>(y);\n let ny = 0;\n\n if (i32(ix - 0x00800000 >= 0x7f800000 - 0x00800000) | (ny = i32(zeroinfnanf(iy)))) {\n // Either (x < 0x1p-126 or inf or nan) or (y is 0 or inf or nan).\n if (ny) {\n if ((iy << 1) == 0) return 1.0;\n if (ix == 0x3F800000) return NaN; // original: 1.0\n if ((ix << 1) > (<u32>0x7F800000 << 1) || (iy << 1) > (<u32>0x7F800000 << 1)) return x + y;\n if ((ix << 1) == (0x3F800000 << 1)) return NaN; // original: 1.0\n if (((ix << 1) < (0x3F800000 << 1)) == !(iy >> 31)) return 0; // |x| < 1 && y==inf or |x| > 1 && y==-inf.\n return y * y;\n }\n if (zeroinfnanf(ix)) {\n let x2 = x * x;\n if ((ix >> 31) && checkintf(iy) == 1) x2 = -x2;\n return <i32>iy < 0 ? 1 / x2 : x2;\n }\n // x and y are non-zero finite.\n if (<i32>ix < 0) {\n // Finite x < 0.\n let yint = checkintf(iy);\n if (yint == 0) return (x - x) / (x - x);\n if (yint == 1) signBias = SIGN_BIAS;\n ix &= 0x7FFFFFFF;\n }\n if (ix < 0x00800000) {\n // Normalize subnormal x so exponent becomes negative.\n ix = reinterpret<u32>(x * Ox1p23f);\n ix &= 0x7FFFFFFF;\n ix -= 23 << 23;\n }\n }\n let logx = log2f_inline(ix);\n let ylogx = y * logx; // cannot overflow, y is single prec.\n if ((reinterpret<u64>(ylogx) >> 47 & 0xFFFF) >= 0x80BF) { // reinterpret<u64>(126.0) >> 47\n // |y * log(x)| >= 126\n if (ylogx > UPPER_LIMIT) return oflowf(signBias); // overflow\n if (ylogx <= LOWER_LIMIT) return uflowf(signBias); // underflow\n }\n return exp2f_inline(ylogx, signBias);\n}\n\n//\n// Lookup data for exp. See: https://git.musl-libc.org/cgit/musl/tree/src/math/exp.c\n//\n\n// @ts-ignore: decorator\n@inline const EXP_TABLE_BITS = 7;\n\n// @ts-ignore: decorator\n@lazy @inline const EXP_DATA_TAB = memory.data<u64>([\n 0x0000000000000000, 0x3FF0000000000000,\n 0x3C9B3B4F1A88BF6E, 0x3FEFF63DA9FB3335,\n 0xBC7160139CD8DC5D, 0x3FEFEC9A3E778061,\n 0xBC905E7A108766D1, 0x3FEFE315E86E7F85,\n 0x3C8CD2523567F613, 0x3FEFD9B0D3158574,\n 0xBC8BCE8023F98EFA, 0x3FEFD06B29DDF6DE,\n 0x3C60F74E61E6C861, 0x3FEFC74518759BC8,\n 0x3C90A3E45B33D399, 0x3FEFBE3ECAC6F383,\n 0x3C979AA65D837B6D, 0x3FEFB5586CF9890F,\n 0x3C8EB51A92FDEFFC, 0x3FEFAC922B7247F7,\n 0x3C3EBE3D702F9CD1, 0x3FEFA3EC32D3D1A2,\n 0xBC6A033489906E0B, 0x3FEF9B66AFFED31B,\n 0xBC9556522A2FBD0E, 0x3FEF9301D0125B51,\n 0xBC5080EF8C4EEA55, 0x3FEF8ABDC06C31CC,\n 0xBC91C923B9D5F416, 0x3FEF829AAEA92DE0,\n 0x3C80D3E3E95C55AF, 0x3FEF7A98C8A58E51,\n 0xBC801B15EAA59348, 0x3FEF72B83C7D517B,\n 0xBC8F1FF055DE323D, 0x3FEF6AF9388C8DEA,\n 0x3C8B898C3F1353BF, 0x3FEF635BEB6FCB75,\n 0xBC96D99C7611EB26, 0x3FEF5BE084045CD4,\n 0x3C9AECF73E3A2F60, 0x3FEF54873168B9AA,\n 0xBC8FE782CB86389D, 0x3FEF4D5022FCD91D,\n 0x3C8A6F4144A6C38D, 0x3FEF463B88628CD6,\n 0x3C807A05B0E4047D, 0x3FEF3F49917DDC96,\n 0x3C968EFDE3A8A894, 0x3FEF387A6E756238,\n 0x3C875E18F274487D, 0x3FEF31CE4FB2A63F,\n 0x3C80472B981FE7F2, 0x3FEF2B4565E27CDD,\n 0xBC96B87B3F71085E, 0x3FEF24DFE1F56381,\n 0x3C82F7E16D09AB31, 0x3FEF1E9DF51FDEE1,\n 0xBC3D219B1A6FBFFA, 0x3FEF187FD0DAD990,\n 0x3C8B3782720C0AB4, 0x3FEF1285A6E4030B,\n 0x3C6E149289CECB8F, 0x3FEF0CAFA93E2F56,\n 0x3C834D754DB0ABB6, 0x3FEF06FE0A31B715,\n 0x3C864201E2AC744C, 0x3FEF0170FC4CD831,\n 0x3C8FDD395DD3F84A, 0x3FEEFC08B26416FF,\n 0xBC86A3803B8E5B04, 0x3FEEF6C55F929FF1,\n 0xBC924AEDCC4B5068, 0x3FEEF1A7373AA9CB,\n 0xBC9907F81B512D8E, 0x3FEEECAE6D05D866,\n 0xBC71D1E83E9436D2, 0x3FEEE7DB34E59FF7,\n 0xBC991919B3CE1B15, 0x3FEEE32DC313A8E5,\n 0x3C859F48A72A4C6D, 0x3FEEDEA64C123422,\n 0xBC9312607A28698A, 0x3FEEDA4504AC801C,\n 0xBC58A78F4817895B, 0x3FEED60A21F72E2A,\n 0xBC7C2C9B67499A1B, 0x3FEED1F5D950A897,\n 0x3C4363ED60C2AC11, 0x3FEECE086061892D,\n 0x3C9666093B0664EF, 0x3FEECA41ED1D0057,\n 0x3C6ECCE1DAA10379, 0x3FEEC6A2B5C13CD0,\n 0x3C93FF8E3F0F1230, 0x3FEEC32AF0D7D3DE,\n 0x3C7690CEBB7AAFB0, 0x3FEEBFDAD5362A27,\n 0x3C931DBDEB54E077, 0x3FEEBCB299FDDD0D,\n 0xBC8F94340071A38E, 0x3FEEB9B2769D2CA7,\n 0xBC87DECCDC93A349, 0x3FEEB6DAA2CF6642,\n 0xBC78DEC6BD0F385F, 0x3FEEB42B569D4F82,\n 0xBC861246EC7B5CF6, 0x3FEEB1A4CA5D920F,\n 0x3C93350518FDD78E, 0x3FEEAF4736B527DA,\n 0x3C7B98B72F8A9B05, 0x3FEEAD12D497C7FD,\n 0x3C9063E1E21C5409, 0x3FEEAB07DD485429,\n 0x3C34C7855019C6EA, 0x3FEEA9268A5946B7,\n 0x3C9432E62B64C035, 0x3FEEA76F15AD2148,\n 0xBC8CE44A6199769F, 0x3FEEA5E1B976DC09,\n 0xBC8C33C53BEF4DA8, 0x3FEEA47EB03A5585,\n 0xBC845378892BE9AE, 0x3FEEA34634CCC320,\n 0xBC93CEDD78565858, 0x3FEEA23882552225,\n 0x3C5710AA807E1964, 0x3FEEA155D44CA973,\n 0xBC93B3EFBF5E2228, 0x3FEEA09E667F3BCD,\n 0xBC6A12AD8734B982, 0x3FEEA012750BDABF,\n 0xBC6367EFB86DA9EE, 0x3FEE9FB23C651A2F,\n 0xBC80DC3D54E08851, 0x3FEE9F7DF9519484,\n 0xBC781F647E5A3ECF, 0x3FEE9F75E8EC5F74,\n 0xBC86EE4AC08B7DB0, 0x3FEE9F9A48A58174,\n 0xBC8619321E55E68A, 0x3FEE9FEB564267C9,\n 0x3C909CCB5E09D4D3, 0x3FEEA0694FDE5D3F,\n 0xBC7B32DCB94DA51D, 0x3FEEA11473EB0187,\n 0x3C94ECFD5467C06B, 0x3FEEA1ED0130C132,\n 0x3C65EBE1ABD66C55, 0x3FEEA2F336CF4E62,\n 0xBC88A1C52FB3CF42, 0x3FEEA427543E1A12,\n 0xBC9369B6F13B3734, 0x3FEEA589994CCE13,\n 0xBC805E843A19FF1E, 0x3FEEA71A4623C7AD,\n 0xBC94D450D872576E, 0x3FEEA8D99B4492ED,\n 0x3C90AD675B0E8A00, 0x3FEEAAC7D98A6699,\n 0x3C8DB72FC1F0EAB4, 0x3FEEACE5422AA0DB,\n 0xBC65B6609CC5E7FF, 0x3FEEAF3216B5448C,\n 0x3C7BF68359F35F44, 0x3FEEB1AE99157736,\n 0xBC93091FA71E3D83, 0x3FEEB45B0B91FFC6,\n 0xBC5DA9B88B6C1E29, 0x3FEEB737B0CDC5E5,\n 0xBC6C23F97C90B959, 0x3FEEBA44CBC8520F,\n 0xBC92434322F4F9AA, 0x3FEEBD829FDE4E50,\n 0xBC85CA6CD7668E4B, 0x3FEEC0F170CA07BA,\n 0x3C71AFFC2B91CE27, 0x3FEEC49182A3F090,\n 0x3C6DD235E10A73BB, 0x3FEEC86319E32323,\n 0xBC87C50422622263, 0x3FEECC667B5DE565,\n 0x3C8B1C86E3E231D5, 0x3FEED09BEC4A2D33,\n 0xBC91BBD1D3BCBB15, 0x3FEED503B23E255D,\n 0x3C90CC319CEE31D2, 0x3FEED99E1330B358,\n 0x3C8469846E735AB3, 0x3FEEDE6B5579FDBF,\n 0xBC82DFCD978E9DB4, 0x3FEEE36BBFD3F37A,\n 0x3C8C1A7792CB3387, 0x3FEEE89F995AD3AD,\n 0xBC907B8F4AD1D9FA, 0x3FEEEE07298DB666,\n 0xBC55C3D956DCAEBA, 0x3FEEF3A2B84F15FB,\n 0xBC90A40E3DA6F640, 0x3FEEF9728DE5593A,\n 0xBC68D6F438AD9334, 0x3FEEFF76F2FB5E47,\n 0xBC91EEE26B588A35, 0x3FEF05B030A1064A,\n 0x3C74FFD70A5FDDCD, 0x3FEF0C1E904BC1D2,\n 0xBC91BDFBFA9298AC, 0x3FEF12C25BD71E09,\n 0x3C736EAE30AF0CB3, 0x3FEF199BDD85529C,\n 0x3C8EE3325C9FFD94, 0x3FEF20AB5FFFD07A,\n 0x3C84E08FD10959AC, 0x3FEF27F12E57D14B,\n 0x3C63CDAF384E1A67, 0x3FEF2F6D9406E7B5,\n 0x3C676B2C6C921968, 0x3FEF3720DCEF9069,\n 0xBC808A1883CCB5D2, 0x3FEF3F0B555DC3FA,\n 0xBC8FAD5D3FFFFA6F, 0x3FEF472D4A07897C,\n 0xBC900DAE3875A949, 0x3FEF4F87080D89F2,\n 0x3C74A385A63D07A7, 0x3FEF5818DCFBA487,\n 0xBC82919E2040220F, 0x3FEF60E316C98398,\n 0x3C8E5A50D5C192AC, 0x3FEF69E603DB3285,\n 0x3C843A59AC016B4B, 0x3FEF7321F301B460,\n 0xBC82D52107B43E1F, 0x3FEF7C97337B9B5F,\n 0xBC892AB93B470DC9, 0x3FEF864614F5A129,\n 0x3C74B604603A88D3, 0x3FEF902EE78B3FF6,\n 0x3C83C5EC519D7271, 0x3FEF9A51FBC74C83,\n 0xBC8FF7128FD391F0, 0x3FEFA4AFA2A490DA,\n 0xBC8DAE98E223747D, 0x3FEFAF482D8E67F1,\n 0x3C8EC3BC41AA2008, 0x3FEFBA1BEE615A27,\n 0x3C842B94C3A9EB32, 0x3FEFC52B376BBA97,\n 0x3C8A64A931D185EE, 0x3FEFD0765B6E4540,\n 0xBC8E37BAE43BE3ED, 0x3FEFDBFDAD9CBE14,\n 0x3C77893B4D91CD9D, 0x3FEFE7C1819E90D8,\n 0x3C5305C14160CC89, 0x3FEFF3C22B8F71F1\n]);\n\n// Handle cases that may overflow or underflow when computing the result that\n// is scale*(1+TMP) without intermediate rounding. The bit representation of\n// scale is in SBITS, however it has a computed exponent that may have\n// overflown into the sign bit so that needs to be adjusted before using it as\n// a double. (int32_t)KI is the k used in the argument reduction and exponent\n// adjustment of scale, positive k here means the result may overflow and\n// negative k means the result may underflow.\n// @ts-ignore: decorator\n@inline\nfunction specialcase(tmp: f64, sbits: u64, ki: u64): f64 {\n const\n Ox1p_1022 = reinterpret<f64>(0x0010000000000000), // 0x1p-1022\n Ox1p1009 = reinterpret<f64>(0x7F00000000000000); // 0x1p1009\n\n let scale: f64;\n if (!(ki & 0x80000000)) {\n // k > 0, the exponent of scale might have overflowed by <= 460.\n sbits -= u64(1009) << 52;\n scale = reinterpret<f64>(sbits);\n return Ox1p1009 * (scale + scale * tmp); // 0x1p1009\n }\n // k < 0, need special care in the subnormal range.\n sbits += u64(1022) << 52;\n // Note: sbits is signed scale.\n scale = reinterpret<f64>(sbits);\n let y = scale + scale * tmp;\n if (abs(y) < 1.0) {\n // Round y to the right precision before scaling it into the subnormal\n // range to avoid double rounding that can cause 0.5+E/2 ulp error where\n // E is the worst-case ulp error outside the subnormal range. So this\n // is only useful if the goal is better than 1 ulp worst-case error.\n let one = copysign(1.0, y);\n let lo = scale - y + scale * tmp;\n let hi = one + y;\n lo = one - hi + y + lo;\n y = (hi + lo) - one;\n // Fix the sign of 0.\n if (y == 0.0) y = reinterpret<f64>(sbits & 0x8000000000000000);\n }\n return y * Ox1p_1022;\n}\n\n// @ts-ignore: decorator\n@inline\nexport function exp_lut(x: f64): f64 {\n const\n N = 1 << EXP_TABLE_BITS,\n N_MASK = N - 1;\n\n const\n InvLn2N = reinterpret<f64>(0x3FF71547652B82FE) * N, // 0x1.71547652b82fep0\n NegLn2hiN = reinterpret<f64>(0xBF762E42FEFA0000), // -0x1.62e42fefa0000p-8\n NegLn2loN = reinterpret<f64>(0xBD0CF79ABC9E3B3A), // -0x1.cf79abc9e3b3ap-47\n shift = reinterpret<f64>(0x4338000000000000); // 0x1.8p52;\n\n const\n C2 = reinterpret<f64>(0x3FDFFFFFFFFFFDBD), // __exp_data.poly[0] (0x1.ffffffffffdbdp-2)\n C3 = reinterpret<f64>(0x3FC555555555543C), // __exp_data.poly[1] (0x1.555555555543cp-3)\n C4 = reinterpret<f64>(0x3FA55555CF172B91), // __exp_data.poly[2] (0x1.55555cf172b91p-5)\n C5 = reinterpret<f64>(0x3F81111167A4D017); // __exp_data.poly[3] (0x1.1111167a4d017p-7)\n\n let ux = reinterpret<u64>(x);\n let abstop = u32(ux >> 52) & 0x7FF;\n if (abstop - 0x3C9 >= 0x03F) {\n if (abstop - 0x3C9 >= 0x80000000) return 1;\n if (abstop >= 0x409) {\n if (ux == 0xFFF0000000000000) return 0;\n if (abstop >= 0x7FF) {\n return 1.0 + x;\n } else {\n return select<f64>(0, Infinity, <i64>ux < 0);\n }\n }\n // Large x is special cased below.\n abstop = 0;\n }\n\n // exp(x) = 2^(k/N) * exp(r), with exp(r) in [2^(-1/2N),2^(1/2N)]\n // x = ln2/N*k + r, with int k and r in [-ln2/2N, ln2/2N]\n let z = InvLn2N * x;\n // #if TOINT_INTRINSICS\n // \tkd = roundtoint(z);\n // \tki = converttoint(z);\n // #elif EXP_USE_TOINT_NARROW\n // \t// z - kd is in [-0.5-2^-16, 0.5] in all rounding modes.\n // let kd = z + shift;\n // let ki = reinterpret<u64>(kd) >> 16;\n // let kd = <f64><i32>ki;\n // #else\n // z - kd is in [-1, 1] in non-nearest rounding modes.\n let kd = z + shift;\n let ki = reinterpret<u64>(kd);\n kd -= shift;\n // #endif\n let r = x + kd * NegLn2hiN + kd * NegLn2loN;\n // 2^(k/N) ~= scale * (1 + tail).\n let idx = usize((ki & N_MASK) << 1);\n let top = ki << (52 - EXP_TABLE_BITS);\n\n let tail = reinterpret<f64>(load<u64>(EXP_DATA_TAB + (idx << alignof<u64>()))); // T[idx]\n // This is only a valid scale when -1023*N < k < 1024*N\n let sbits = load<u64>(EXP_DATA_TAB + (idx << alignof<u64>()), 1 << alignof<u64>()) + top; // T[idx + 1]\n // exp(x) = 2^(k/N) * exp(r) ~= scale + scale * (tail + exp(r) - 1).\n // Evaluation is optimized assuming superscalar pipelined execution.\n let r2 = r * r;\n // Without fma the worst case error is 0.25/N ulp larger.\n // Worst case error is less than 0.5+1.11/N+(abs poly error * 2^53) ulp.\n let tmp = tail + r + r2 * (C2 + r * C3) + r2 * r2 * (C4 + r * C5);\n if (abstop == 0) return specialcase(tmp, sbits, ki);\n let scale = reinterpret<f64>(sbits);\n // Note: tmp == 0 or |tmp| > 2^-200 and scale > 2^-739, so there\n // is no spurious underflow here even without fma.\n return scale + scale * tmp;\n}\n\n//\n// Lookup data for exp2. See: https://git.musl-libc.org/cgit/musl/tree/src/math/exp2.c\n//\n\n// Handle cases that may overflow or underflow when computing the result that\n// is scale*(1+TMP) without intermediate rounding. The bit representation of\n// scale is in SBITS, however it has a computed exponent that may have\n// overflown into the sign bit so that needs to be adjusted before using it as\n// a double. (int32_t)KI is the k used in the argument reduction and exponent\n// adjustment of scale, positive k here means the result may overflow and\n// negative k means the result may underflow.\n// @ts-ignore: decorator\n@inline\nfunction specialcase2(tmp: f64, sbits: u64, ki: u64): f64 {\n const Ox1p_1022 = reinterpret<f64>(0x10000000000000); // 0x1p-1022\n let scale: f64;\n if ((ki & 0x80000000) == 0) {\n // k > 0, the exponent of scale might have overflowed by 1\n sbits -= u64(1) << 52;\n scale = reinterpret<f64>(sbits);\n return 2 * (scale * tmp + scale);\n }\n // k < 0, need special care in the subnormal range\n sbits += u64(1022) << 52;\n scale = reinterpret<f64>(sbits);\n let y = scale * tmp + scale;\n if (y < 1.0) {\n // Round y to the right precision before scaling it into the subnormal\n // range to avoid double rounding that can cause 0.5+E/2 ulp error where\n // E is the worst-case ulp error outside the subnormal range. So this\n // is only useful if the goal is better than 1 ulp worst-case error.\n let hi: f64, lo: f64;\n lo = scale - y + scale * tmp;\n hi = 1.0 + y;\n lo = 1.0 - hi + y + lo;\n y = (hi + lo) - 1.0;\n }\n return y * Ox1p_1022;\n}\n\n// @ts-ignore: decorator\n@inline\nexport function exp2_lut(x: f64): f64 {\n const\n N = 1 << EXP_TABLE_BITS,\n N_MASK = N - 1,\n shift = reinterpret<f64>(0x4338000000000000) / N; // 0x1.8p52\n\n const\n C1 = reinterpret<f64>(0x3FE62E42FEFA39EF), // 0x1.62e42fefa39efp-1\n C2 = reinterpret<f64>(0x3FCEBFBDFF82C424), // 0x1.ebfbdff82c424p-3\n C3 = reinterpret<f64>(0x3FAC6B08D70CF4B5), // 0x1.c6b08d70cf4b5p-5\n C4 = reinterpret<f64>(0x3F83B2ABD24650CC), // 0x1.3b2abd24650ccp-7\n C5 = reinterpret<f64>(0x3F55D7E09B4E3A84); // 0x1.5d7e09b4e3a84p-10\n\n let ux = reinterpret<u64>(x);\n let abstop = u32(ux >> 52) & 0x7ff;\n if (abstop - 0x3C9 >= 0x03F) {\n if (abstop - 0x3C9 >= 0x80000000) return 1.0;\n if (abstop >= 0x409) {\n if (ux == 0xFFF0000000000000) return 0;\n if (abstop >= 0x7FF) return 1.0 + x;\n if (<i64>ux >= 0) return Infinity;\n else if (ux >= 0xC090CC0000000000) return 0;\n }\n if ((ux << 1) > 0x811A000000000000) abstop = 0; // Large x is special cased below.\n }\n\n // exp2(x) = 2^(k/N) * 2^r, with 2^r in [2^(-1/2N),2^(1/2N)].\n // x = k/N + r, with int k and r in [-1/2N, 1/2N]\n let kd = x + shift;\n let ki = reinterpret<u64>(kd);\n kd -= shift; // k/N for int k\n let r = x - kd;\n // 2^(k/N) ~= scale * (1 + tail)\n let idx = usize((ki & N_MASK) << 1);\n let top = ki << (52 - EXP_TABLE_BITS);\n\n let tail = reinterpret<f64>(load<u64>(EXP_DATA_TAB + (idx << alignof<u64>()), 0 << alignof<u64>())); // T[idx])\n // This is only a valid scale when -1023*N < k < 1024*N\n let sbits = load<u64>(EXP_DATA_TAB + (idx << alignof<u64>()), 1 << alignof<u64>()) + top; // T[idx + 1]\n // exp2(x) = 2^(k/N) * 2^r ~= scale + scale * (tail + 2^r - 1).\n // Evaluation is optimized assuming superscalar pipelined execution\n let r2 = r * r;\n // Without fma the worst case error is 0.5/N ulp larger.\n // Worst case error is less than 0.5+0.86/N+(abs poly error * 2^53) ulp.\n let tmp = tail + r * C1 + r2 * (C2 + r * C3) + r2 * r2 * (C4 + r * C5);\n if (abstop == 0) return specialcase2(tmp, sbits, ki);\n let scale = reinterpret<f64>(sbits);\n // Note: tmp == 0 or |tmp| > 2^-65 and scale > 2^-928, so there\n // is no spurious underflow here even without fma.\n return scale * tmp + scale;\n}\n\n//\n// Lookup data for log2. See: https://git.musl-libc.org/cgit/musl/tree/src/math/log2.c\n//\n\n// @ts-ignore: decorator\n@inline const LOG2_TABLE_BITS = 6;\n\n/* Algorithm:\n\n x = 2^k z\n log2(x) = k + log2(c) + log2(z/c)\n log2(z/c) = poly(z/c - 1)\n\nwhere z is in [1.6p-1; 1.6p0] which is split into N subintervals and z falls\ninto the ith one, then table entries are computed as\n\n tab[i].invc = 1/c\n tab[i].logc = (double)log2(c)\n tab2[i].chi = (double)c\n tab2[i].clo = (double)(c - (double)c)\n\nwhere c is near the center of the subinterval and is chosen by trying +-2^29\nfloating point invc candidates around 1/center and selecting one for which\n\n 1) the rounding error in 0x1.8p10 + logc is 0,\n 2) the rounding error in z - chi - clo is < 0x1p-64 and\n 3) the rounding error in (double)log2(c) is minimized (< 0x1p-68).\n\nNote: 1) ensures that k + logc can be computed without rounding error, 2)\nensures that z/c - 1 can be computed as (z - chi - clo)*invc with close to a\nsingle rounding error when there is no fast fma for z*invc - 1, 3) ensures\nthat logc + poly(z/c - 1) has small error, however near x == 1 when\n|log2(x)| < 0x1p-4, this is not enough so that is special cased. */\n\n// @ts-ignore: decorator\n@lazy @inline const LOG2_DATA_TAB1 = memory.data<u64>([\n // invc , logc\n 0x3FF724286BB1ACF8, 0xBFE1095FEECDB000,\n 0x3FF6E1F766D2CCA1, 0xBFE08494BD76D000,\n 0x3FF6A13D0E30D48A, 0xBFE00143AEE8F800,\n 0x3FF661EC32D06C85, 0xBFDEFEC5360B4000,\n 0x3FF623FA951198F8, 0xBFDDFDD91AB7E000,\n 0x3FF5E75BA4CF026C, 0xBFDCFFAE0CC79000,\n 0x3FF5AC055A214FB8, 0xBFDC043811FDA000,\n 0x3FF571ED0F166E1E, 0xBFDB0B67323AE000,\n 0x3FF53909590BF835, 0xBFDA152F5A2DB000,\n 0x3FF5014FED61ADDD, 0xBFD9217F5AF86000,\n 0x3FF4CAB88E487BD0, 0xBFD8304DB0719000,\n 0x3FF49539B4334FEE, 0xBFD74189F9A9E000,\n 0x3FF460CBDFAFD569, 0xBFD6552BB5199000,\n 0x3FF42D664EE4B953, 0xBFD56B23A29B1000,\n 0x3FF3FB01111DD8A6, 0xBFD483650F5FA000,\n 0x3FF3C995B70C5836, 0xBFD39DE937F6A000,\n 0x3FF3991C4AB6FD4A, 0xBFD2BAA1538D6000,\n 0x3FF3698E0CE099B5, 0xBFD1D98340CA4000,\n 0x3FF33AE48213E7B2, 0xBFD0FA853A40E000,\n 0x3FF30D191985BDB1, 0xBFD01D9C32E73000,\n 0x3FF2E025CAB271D7, 0xBFCE857DA2FA6000,\n 0x3FF2B404CF13CD82, 0xBFCCD3C8633D8000,\n 0x3FF288B02C7CCB50, 0xBFCB26034C14A000,\n 0x