FastLED 3.9.15
Loading...
Searching...
No Matches
perlin_s16x16.h
Go to the documentation of this file.
1#pragma once
2
3// 2D Perlin noise implementation using s16x16 fixed-point arithmetic
4// Extracted from animartrix_detail.hpp for testing and reuse
5//
6// LUT-accelerated 2D Perlin noise using s16x16 fixed-point.
7// Internals use Q8.24 (24 fractional bits) for precision exceeding float.
8// The fade LUT replaces the 6t^5-15t^4+10t^3 polynomial with table lookup.
9// The z=0 specialization halves work vs full 3D noise.
10
12
13
14namespace fl {
15
17 static constexpr int HP_BITS = 24;
18 static constexpr fl::i32 HP_ONE = static_cast<fl::i32>(1) << HP_BITS; // 16777216 = 1.0
19
20 // Build 257-entry Perlin fade LUT in Q8.24 format.
21 static void init_fade_lut(fl::i32 *table);
22
23 // 2D Perlin noise. Input s16x16, output s16x16 approx [-1, 1].
24 // perm: 256-byte Perlin permutation table (indexed with & 255).
26 const fl::i32 *fade_lut,
27 const fl::u8 *perm);
28
29 // Raw i32 version: takes s16x16 raw values, returns s16x16 raw value.
30 // Avoids from_raw/raw() round-trips when caller already has raw values.
31 static fl::i32 pnoise2d_raw(fl::i32 fx_raw, fl::i32 fy_raw,
32 const fl::i32 *fade_lut,
33 const fl::u8 *perm);
34
35 static constexpr int FP_BITS = fl::s16x16::FRAC_BITS;
36 static constexpr fl::i32 FP_ONE = static_cast<fl::i32>(1) << FP_BITS;
37
38 // Decompose s16x16 raw value into integer floor and Q8.24 fractional part.
39 static void floor_frac(fl::i32 fp16, int &ifloor,
40 fl::i32 &frac24);
41
42 // LUT fade: 1 lookup + 1 lerp replaces 5 multiplies.
43 static fl::i32 fade(fl::i32 t, const fl::i32 *table);
44
45 static fl::i32 lerp(fl::i32 t, fl::i32 a, fl::i32 b);
46
47 // z=0 gradient via branchless coefficient LUT.
48 static fl::i32 grad(int hash, fl::i32 x, fl::i32 y);
49
50 // 3D Perlin noise. Input s16x16, output s16x16 approx [-1, 1].
52 const fl::i32 *fade_lut,
53 const fl::u8 *perm);
54
55 // Raw i32 version of 3D Perlin noise.
56 static fl::i32 pnoise3d_raw(fl::i32 fx_raw, fl::i32 fy_raw, fl::i32 fz_raw,
57 const fl::i32 *fade_lut,
58 const fl::u8 *perm);
59
60 // 3D gradient: 12-direction gradient matching float grad().
61 static fl::i32 grad3d(int hash, fl::i32 x, fl::i32 y, fl::i32 z);
62};
63
64} // namespace fl
uint32_t z[NUM_LAYERS]
Definition Fire2023.h:93
static constexpr int FRAC_BITS
Definition s16x16.h:22
unsigned char u8
Definition s16x16x4.h:132
Base definition for an LED controller.
Definition crgb.hpp:179
static fl::i32 fade(fl::i32 t, const fl::i32 *table)
static constexpr fl::i32 HP_ONE
static fl::s16x16 pnoise2d(fl::s16x16 fx, fl::s16x16 fy, const fl::i32 *fade_lut, const fl::u8 *perm)
static fl::i32 pnoise2d_raw(fl::i32 fx_raw, fl::i32 fy_raw, const fl::i32 *fade_lut, const fl::u8 *perm)
static fl::i32 pnoise3d_raw(fl::i32 fx_raw, fl::i32 fy_raw, fl::i32 fz_raw, const fl::i32 *fade_lut, const fl::u8 *perm)
static void floor_frac(fl::i32 fp16, int &ifloor, fl::i32 &frac24)
static fl::i32 lerp(fl::i32 t, fl::i32 a, fl::i32 b)
static constexpr int HP_BITS
static fl::s16x16 pnoise3d(fl::s16x16 fx, fl::s16x16 fy, fl::s16x16 fz, const fl::i32 *fade_lut, const fl::u8 *perm)
static void init_fade_lut(fl::i32 *table)
static constexpr int FP_BITS
static constexpr fl::i32 FP_ONE
static fl::i32 grad3d(int hash, fl::i32 x, fl::i32 y, fl::i32 z)
static fl::i32 grad(int hash, fl::i32 x, fl::i32 y)