FastMath & Matrix Acceleration
Hardware-accelerated trigonometry lookup tables, TRNG random generators, 3D vector maths, and 4x4 transformation matrix batch processing in KryonOS.
Hardware Math Acceleration: FastMath
The FastMath (or System.math) object provides native C++ numerical acceleration for game physics, audio DSP, and 3D vertex transformations. It bypasses Duktape stack overhead to achieve 300% to 800% higher throughput than standard JavaScript math functions.
Implementation details are hosted on the KryonOS GitHub Repository.
1. Constants
| Constant | Value | Description |
|---|---|---|
FastMath.PI | 3.14159265 | Ratio of circumference to diameter |
FastMath.HALF_PI | 1.57079632 | Half Pi (90 degrees) |
FastMath.TWO_PI | 6.28318530 | Two Pi (360 degrees) |
FastMath.DEG_TO_RAD | 0.01745329 | Degrees to radians multiplier |
FastMath.RAD_TO_DEG | 57.2957795 | Radians to degrees multiplier |
2. Hardware Trigonometry & LUT Functions
KryonOS precomputes high-density Sine/Cosine lookup tables in firmware ROM, providing sub-microsecond calculation.
FastMath.sin(rad) / FastMath.cos(rad)
Fast lookup trigonometry computed from radians.
- Parameters:
rad(Float) - Returns:
Float(-1.0to1.0).
FastMath.sinDeg(deg) / FastMath.cosDeg(deg)
Fast lookup trigonometry computed from degree angles (0 to 360).
- Parameters:
deg(Float) - Returns:
Float
FastMath.invSqrt(val)
Fast inverse square root ($1 / \sqrt$) based on the Quake algorithm for lighting normalizations.
- Parameters:
val(Float) - Returns:
Float
3. Hardware TRNG & Range Utilities
FastMath.random(min, max)
Generates true random floating numbers sourced from the ESP32 hardware True Random Number Generator (TRNG) entropy ring.
- Parameters:
min(Float),max(Float) - Returns:
Float
FastMath.randomInt(min, max)
Generates an integer between min and max inclusive via hardware TRNG.
- Parameters:
min(Integer),max(Integer) - Returns:
Integer
FastMath.clamp(val, min, max)
Restricts a value between minimum and maximum bounds.
- Parameters:
val,min,max(Float) - Returns:
Float
FastMath.lerp(start, end, t)
Performs linear interpolation between start and end with factor t (0.0 to 1.0).
- Parameters:
start,end,t(Float) - Returns:
Float
FastMath.map(val, inMin, inMax, outMin, outMax)
Remaps a number from one range to another.
- Parameters:
val,inMin,inMax,outMin,outMax(Float) - Returns:
Float
4. 3D Vector Operations
FastMath.vec3Dot(x1, y1, z1, x2, y2, z2)
Computes the vector scalar dot product.
- Returns:
Float
FastMath.vec3Cross(x1, y1, z1, x2, y2, z2)
Computes the perpendicular cross product vector.
- Returns:
Array[x, y, z]
FastMath.vec3Normalize(x, y, z)
Computes the unit vector (length 1.0).
- Returns:
Array[nx, ny, nz]
5. 4x4 Transformation Matrices & Batch Processing
When rendering 3D scenes or particle simulations, transforming thousands of coordinate points one-by-one in JavaScript is slow. FastMath transforms entire arrays in a single native C++ call.
FastMath.mat4Identity()
Returns a flat 16-element identity matrix array.
- Returns:
Array(16 floats)
FastMath.mat4Rotate(matrix, angleRad, axisX, axisY, axisZ)
Applies an Euler axis rotation to an existing 4x4 matrix.
- Returns:
Array(16 floats)
FastMath.batchTransform(verticesArray, matrix)
Multiplies hundreds of 3D vertices [x0, y0, z0, x1, y1, z1, ...] by a 4x4 matrix simultaneously in C++.
- Parameters:
verticesArray(Arrayof floats)matrix(Arrayof 16 floats)
- Returns:
Arrayof transformed vertices.
// Transform 100 3D vertices in one call
var vertices = [
-1, -1, 1,
1, -1, 1,
1, 1, 1,
-1, 1, 1
];
var mat = FastMath.mat4Identity();
mat = FastMath.mat4Rotate(mat, 0.785, 0, 1, 0); // Rotate 45 deg on Y
// 500% faster than looping in JavaScript
var transformed = FastMath.batchTransform(vertices, mat);