NAME
ttypt/noise.h - Standalone generic N-dimensional wrapping procedural noise engine, dense matrix stepper, point sampler, and multi-octave cascade API.
SYNOPSIS
#include <stdint.h>
#include <stddef.h>
#include <stdlib.h>
#include <string.h>
Data Structures
Single octave descriptor for dense matrix stepping (edge shift and value shift).
Reusable scanline evaluation context holding per-octave corner caches.
Macros
Set to 1 when the inlined 4-byte XXH32 fast path is active.
Maximum unsigned 32-bit raw noise value (UINT32_MAX).
Maximum supported spatial dimensionality for noise queries (4).
Default value right-shift applied to raw 32-bit hash samples (8).
Bitmask aligning an int16 coordinate to a 2^x feature quad boundary.
Interpolation flag selecting C2 quintic smoothstep fade (6t^5 - 15t^4 + 10t^3).
Alias for NOISE_FADE (cubic smoothstep interpolation flag).
Layer combine mode: overwrite accumulator.
Layer combine mode: add to accumulator.
Seed mode: share one base seed across all layers.
Interpolation flag selecting C2 quintic fade.
Interpolation flag selecting pure linear interpolation.
Typedefs
Alias for octave_t.
Alias for noise_wcache_t.
Alias for noise_cascade_t.
Alias for noise_cache_t.
Alias for noise_scanline_t.
Functions
Portable 32-bit fallback hash mixer used when xxhash.h is unavailable.
Compute linear interpolation step increments along one dimension of a feature quad.
Advance hypercube interpolation values by mul steps.
Clip and rebase feature-quad corner values onto the target matrix origin.
Compute aligned feature-quad origin coordinates from input point p and mask.
Convenience 2D wrapper around noise_wpoint_n().
Convenience 3D wrapper around noise_wpoint_n().
Initialize a wrapping noise corner cache to the empty state.
O(1) zero-allocation 2D point noise query.
O(1) zero-allocation 3D point noise query.
O(1) zero-allocation 4D point noise query.
1D point noise query (chaff namespace alias).
2D point noise query (chaff namespace alias).
3D point noise query (chaff namespace alias).
4D point noise query (chaff namespace alias).
N-dimensional point noise query (chaff namespace alias).
Extended N-dimensional point noise query with explicit value shift.
Multi-octave dense matrix stepper (chaff namespace alias).
Wrapping N-dimensional point query (chaff namespace alias).
Cached wrapping N-dimensional point query (chaff namespace alias).
Wrapping scanline row evaluator (chaff namespace alias).
Wrapping 2D plane evaluator (chaff namespace alias).
Derive the effective 32-bit seed for layer index l.
Evaluate fractional smoothstep weight for sub-cell offset frac.
Return effective value shift for cascade cfg.
Return effective interpolation flags for cascade cfg.
Derive deterministic seed for octave index o in cascade cfg.
Initialize scanline evaluator scan for cascade cfg and dimension dim.
Evaluate multi-octave cascade cfg at 3D coordinates (x, y, z).
Evaluate multi-octave cascade cfg at 4D coordinates (x, y, z, w).
Fill a 2D dense matrix from cascade cfg (chaff namespace alias).
Initialize a scanline evaluator (chaff namespace alias).
Evaluate a scanline row (chaff namespace alias).
Sample 2D cascade cfg (chaff namespace alias).
Sample 3D cascade cfg (chaff namespace alias).
Sample 4D cascade cfg (chaff namespace alias).
Detailed Description
Standalone generic N-dimensional wrapping procedural noise engine, dense matrix stepper, point sampler, and multi-octave cascade API.
ttypt/noise.h โ Standalone Generic N-Dimensional Procedural Noise Library.
A noise algorithm that wraps well around data type limits.
LICENSE: BSD-2-Clause Copyright (c) 2026, tty-pt
DEFINITIONS:
A point has DIM coordinates (int16_t). A matrix has 2^y (cy) edge length and (2^cy)^dim total elements. A noise feature quad has 2^x edge length and 2^dim vertices with random values. Other values are the result of a fade (multilinear smoothstep) between those.
Provides:
- 1.
- Dense Matrix Stepper (noise_oct, noise_m, noise_quad): bulk hypercube generation.
- 2.
- Generic Point Sampler (noise_point_n, noise_point_2d, noise_point_3d): O(1) zero-allocation point queries.
- 3.
- Wrapping Original-Algorithm Sampler (noise_wpoint_n, noise_wpoint_c, noise_wrow): point / cached / row-stepped queries that reproduce the matrix stepper exactly, so coordinates (e.g. an ever-increasing time counter cast to int16_t) may keep incrementing across the data-type boundary without a seam.
Macro Definition Documentation
#define CHAFF_MAX NOISE_MAX
--- Canonical libchaff aliases (chaff_ prefix) ---------------------------
#define CHAFF_SEED_PRIME 0x85EBCA77u
Per-layer seed decorrelation multiplier (Murmur3 finalizer constant).
#define NOISE_CASCADE_LINEAR 0x80u
Flag for noise_cascade_t::flags to explicitly request raw linear interpolation (disabling NOISE_FADE).
#define NOISE_CONTRAST_IDENTITY 256u
Identity contrast multiplier in Q8.8 fixed-point (256 = 1.0x).
#define NOISE_FADE 1u
Point queries that follow the ORIGINAL lattice algorithm of noise_m:
- lattice keys are the masked int16 coordinates themselves (s = p & COORDMASK_LSHIFT(x)), hashed with noise_r;
- the far vertex of a quad is s + 2^x computed in int16_t, so it wraps exactly like the coordinate does (32767 + 1 == -32768): no seam at the data-type boundary on any axis;
- interpolation is the matrix stepper's: axis 0 first, step = (v1 - v0) >> x, value = v0 + f * step (see calc_steps / step / noise_quad). Results are therefore value-equal to noise_m over lattice-aligned matrices. Values are on noise_m's scale: (hash >> w), interpolated. With NOISE_FADE, f is remapped by a Hermite smoothstep onto [0, 2^x) (lattice values are unchanged, derivatives vanish at cell borders). Requires 1 <= dim <= 4 and 1 <= x <= 15.
#define NOISE_MAX_OCTAVES 16
Maximum number of octaves supported by noise_scanline_t and cascade evaluators.
Typedef Documentation
typedef noise_octave_t chaff_octave_t
Canonical declarative libchaff aliases
typedef noise_wcache_t noise_cache_t
Corner cache: aliased to noise_wcache_t to eliminate layout drift and type punning
Function Documentation
uint32_t chaff_fade_frac_ex (uint32_t fi, uint16_t frac, unsigned x, uint8_t flags) [inline], [static]
Fractional fade helper with selectable C1 cubic Hermite or C2 quintic Hermite in 16.16
void chaff_fill (const chaff_field_t * field, uint8_t layer_lo, uint8_t layer_hi, const chaff_coord_t origin[], const int32_t step_q16[], const uint16_t n[], int32_t * out, size_t stride, unsigned mode) [inline], [static]
Primary multi-layer region generator (point, row, plane, volume). Evaluates field layers [layer_lo, layer_hi) over a grid of size n[dim] starting at origin[dim] with step_q16[dim] (rational 16.16 fixed-point steps). Zero heap allocations; callers supply out.
int noise_fdiv (int a, int b) [inline], [static]
Euclidean integer floor division
void noise_get_v (uint32_t * v, int16_t * s, uint16_t x, unsigned w, unsigned seed, uint8_t dim) [inline], [static]
Generate values at N-dimensional quad vertices
void noise_get_v_periodic (uint32_t * v, const int16_t * s, uint16_t x, unsigned w, unsigned seed, uint8_t dim, uint8_t period_shift) [inline], [static]
Generate values at N-dimensional quad vertices with optional periodic domain wrapping
uint32_t noise_hash2_1 (int16_t p0, int16_t p1, uint32_t seed) [inline], [static]
Branchless 4-byte XXH32 matching XXH32(int16_t[2]{p0,p1}, 4, seed) bit-for-bit
void noise_hash2_4x (const uint32_t packed[4], uint32_t seed, unsigned w, uint32_t out[4]) [inline], [static]
4-wide 4-byte XXH32 hash + right-shift by w. Bit-exact with noise_r(p, seed, w, 2).
void noise_m (uint32_t * mat, int16_t * ms, unsigned x, unsigned w, unsigned seed, unsigned cy, uint8_t dim) [inline], [static]
Fills matrix "mat" with deterministic N-dimensional noise
void noise_matrix_fill_2d (const noise_cascade_t * cfg, int16_t origin_x, int16_t origin_y, uint8_t size_shift, int32_t * out_matrix) [inline], [static]
- 2.
- Universal Bulk Matrix Stepper Fills 2^size_shift x 2^size_shift chunk by scanline rows. Guarantees out_matrix[r * side + c] == noise_sample_2d(origin_x + c, origin_y + r). Zero heap allocations.
void noise_oct (uint32_t * m, int16_t * s, size_t oct_n, octave_t * oct, unsigned seed, unsigned cy, uint8_t dim) [inline], [static]
Multi-octave N-dimensional matrix noise evaluation
uint32_t noise_pack2 (int16_t p0, int16_t p1) [inline], [static]
Pack 2D lattice coordinate (p0, p1) into little-endian 4-byte key
int noise_point_1d (int16_t x, uint32_t seed, uint8_t shift) [inline], [static]
Point-evaluation ergonomic helpers
int noise_point_n (const int16_t * p, uint32_t seed, uint8_t shift, uint8_t dim) [inline], [static]
Evaluates continuous N-dimensional smoothstep value noise at point p[dim]. Returns fixed-point value in range 0..255. Zero heap allocations.
int noise_point_n_ex (const int16_t * p, uint32_t seed, uint8_t shift, uint8_t dim, uint8_t vshift) [inline], [static]
Evaluates continuous N-dimensional smoothstep value noise at point p[dim] with configurable bit-shift divider vshift (1..15; 0 defaults to NOISE_DEFAULT_VSHIFT = 8). Returns fixed-point value in range 0 .. (1 << (16 - vshift)) - 1. Zero heap allocations.
void noise_quad (uint32_t * c, uint32_t * vc, unsigned z, unsigned cy, uint8_t dim) [inline], [static]
Fade between vertices and step target values across matrix
uint32_t noise_r (int16_t * p, unsigned seed, unsigned w, uint8_t dim) [inline], [static]
Deterministic random value for an N-dimensional point p
int32_t noise_sample_2d (const noise_cascade_t * cfg, int16_t x, int16_t y) [inline], [static]
- 3.
- Sparse Point Sampler (Collision, Entrances, Caverns)
void noise_scanline_eval_row (noise_scanline_t * scan, int16_t start_x, int16_t y, int16_t z, size_t count, int32_t * out_row) [inline], [static]
- 1.
- Universal Scanline Stepper with Corner Cache Evaluates rows in-register, applying skew per sample so values match the point sampler. Zero heap allocations.
void noise_wcell (int16_t * s, uint32_t * f, const int16_t * p, unsigned x, unsigned flags, uint8_t dim) [inline], [static]
Lattice cell origin and in-quad offsets of p
const uint32_t * noise_wcorners (noise_wcache_t * c, int16_t * s, unsigned x, unsigned w, unsigned seed, uint8_t dim) [inline], [static]
Vertex values of the cell at s, from the cache when it matches
const uint32_t * noise_wcorners_periodic (noise_wcache_t * c, const int16_t * s, unsigned x, unsigned w, unsigned seed, uint8_t dim, uint8_t period_shift) [inline], [static]
Vertex values of the cell at s with optional periodic domain wrapping
uint32_t noise_wfade (uint32_t f, unsigned x) [inline], [static]
Hermite smoothstep of f in [0, 2^x), mapped onto [0, 2^x)
uint32_t noise_wfade5 (uint32_t f, unsigned x) [inline], [static]
Ken Perlin's quintic polynomial: 6t^5 - 15t^4 + 10t^3. First and second derivatives are zero at 0 and 1, ensuring C^2 continuity. Input f in [0, 2^x), returns value in [0, 2^x).
void noise_wfade5_4x (const uint32_t f[4], unsigned x, uint32_t out[4]) [inline], [static]
4-wide bit-exact Perlin quintic fade matching noise_wfade5(f[i], x)
void noise_wplane (uint32_t * out, unsigned n, unsigned rows, size_t stride, const int16_t * p0, unsigned x, unsigned w, unsigned seed, uint8_t dim, unsigned flags, noise_wcache_t * c) [inline], [static]
Plane stepper: out[r * stride + k] = noise_wpoint_n(p0 with the last two axes advanced by r (rows, axis dim-2) and k (columns, axis dim-1)), for r < rows, k < n; both coordinates wrap in int16_t. In noise_quad order it fetches the 2^dim corners ONCE per 2D lattice cell (rows x cols / 4^x fetches), collapses the fixed outer axes once per cell, the row axis once per row, and spends one multiply-add per sample. Requires dim >= 2. c may be NULL.
void noise_wpoint_2d_4x (noise_wcache_t * c, const int16_t py[4], const int16_t px[4], unsigned x, unsigned w, unsigned seed, unsigned flags, uint32_t out[4]) [inline], [static]
4-wide 2D wrapped point query: bit-exact with 4 calls to noise_wpoint_c(c, {py[i], px[i]}, x, w, seed, 2, flags).
uint32_t noise_wpoint_c (noise_wcache_t * c, const int16_t * p, unsigned x, unsigned w, unsigned seed, uint8_t dim, unsigned flags) [inline], [static]
Cached point query: value-equal to noise_wpoint_n
uint32_t noise_wpoint_c_periodic (noise_wcache_t * c, const int16_t * p, unsigned x, unsigned w, unsigned seed, uint8_t dim, unsigned flags, uint8_t period_shift) [inline], [static]
Cached point query with optional periodic domain wrapping
uint32_t noise_wpoint_n (const int16_t * p, unsigned x, unsigned w, unsigned seed, uint8_t dim, unsigned flags) [inline], [static]
Uncached point query (2^dim hashes)
uint32_t noise_wpoint_n_periodic (const int16_t * p, unsigned x, unsigned w, unsigned seed, uint8_t dim, unsigned flags, uint8_t period_shift) [inline], [static]
Uncached point query with optional periodic domain wrapping
void noise_wreduce (uint32_t * v, const uint32_t * f, unsigned x, unsigned levels, uint8_t dim, unsigned flags) [inline], [static]
Collapse the first levels axes of the 2^dim vertex values in v, axis 0 first, at in-quad offsets f[] (already faded if requested). With NOISE_FADE/NOISE_FADE5, multiplies before shifting to eliminate slope truncation across cells. Without NOISE_FADE, preserves exact bit-level noise_m calc_steps arithmetic.
void noise_wrow (uint32_t * out, unsigned n, const int16_t * p0, int16_t dstep, unsigned x, unsigned w, unsigned seed, uint8_t dim, unsigned flags, noise_wcache_t * c) [inline], [static]
Row stepper: out[k] = noise_wpoint_n(p0 + k * dstep along the LAST axis), k < n, the coordinate wrapping in int16_t. Like noise_get_v + noise_quad, it fetches the 2^dim corners once per lattice cell and collapses the other axes once per cell; each sample is then one multiply-add on the innermost axis. c may be NULL.
uint32_t noise_wstep (uint32_t v0, uint32_t v1, uint32_t f, unsigned x) [inline], [static]
Multiply-then-shift fixed-point linear interpolation step: Evaluates v0 + (((v1 - v0) * f) >> x) without premature division.
void noise_wstep_4x (const uint32_t v0[4], const uint32_t v1[4], const uint32_t f[4], unsigned x, uint32_t out[4]) [inline], [static]
4-wide bit-exact fixed-point linear interpolation matching noise_wstep(v0[i], v1[i], f[i], x)
int32_t noise_wstep_slope (uint32_t v0, uint32_t v1, unsigned x) [inline], [static]
Legacy slope helper for un-faded matrix steppers
Author
Generated automatically by Doxygen for chaff from the source code.