FastLED 3.10.8
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#include "fl/stl/noexcept.h"
13
14
15namespace fl {
16
18 static constexpr int HP_BITS = 24;
19 static constexpr fl::i32 HP_ONE = static_cast<fl::i32>(1) << HP_BITS; // 16777216 = 1.0
20
21 // Build 257-entry Perlin fade LUT in Q8.24 format.
22 static void init_fade_lut(fl::i32 *table) FL_NO_EXCEPT;
23
24 // 2D Perlin noise. Input s16x16, output s16x16 approx [-1, 1].
25 // perm: 256-byte Perlin permutation table (indexed with & 255).
27 const fl::i32 *fade_lut,
28 const fl::u8 *perm) FL_NO_EXCEPT;
29
30 // Raw i32 version: takes s16x16 raw values, returns s16x16 raw value.
31 // Avoids from_raw/raw() round-trips when caller already has raw values.
32 static fl::i32 pnoise2d_raw(fl::i32 fx_raw, fl::i32 fy_raw,
33 const fl::i32 *fade_lut,
34 const fl::u8 *perm) FL_NO_EXCEPT;
35
36 static constexpr int FP_BITS = fl::s16x16::FRAC_BITS;
37 static constexpr fl::i32 FP_ONE = static_cast<fl::i32>(1) << FP_BITS;
38
39 // Decompose s16x16 raw value into integer floor and Q8.24 fractional part.
40 static void floor_frac(fl::i32 fp16, int &ifloor,
41 fl::i32 &frac24) FL_NO_EXCEPT;
42
43 // LUT fade: 1 lookup + 1 lerp replaces 5 multiplies.
44 static fl::i32 fade(fl::i32 t, const fl::i32 *table) FL_NO_EXCEPT;
45
46 static fl::i32 lerp(fl::i32 t, fl::i32 a, fl::i32 b) FL_NO_EXCEPT;
47
48 // z=0 gradient via branchless coefficient LUT.
49 static fl::i32 grad(int hash, fl::i32 x, fl::i32 y) FL_NO_EXCEPT;
50
51 // 3D Perlin noise. Input s16x16, output s16x16 approx [-1, 1].
53 const fl::i32 *fade_lut,
54 const fl::u8 *perm) FL_NO_EXCEPT;
55
56 // Raw i32 version of 3D Perlin noise.
57 static fl::i32 pnoise3d_raw(fl::i32 fx_raw, fl::i32 fy_raw, fl::i32 fz_raw,
58 const fl::i32 *fade_lut,
59 const fl::u8 *perm) FL_NO_EXCEPT;
60
61 // 3D gradient: 12-direction gradient matching float grad().
62 static fl::i32 grad3d(int hash, fl::i32 x, fl::i32 y, fl::i32 z) FL_NO_EXCEPT;
63};
64
65} // namespace fl
uint32_t z[NUM_LAYERS]
Definition Fire2023.h:93
static constexpr int FRAC_BITS
Definition s16x16.h:24
unsigned char u8
Definition stdint.h:131
u8 u8 fract8 blur_amount FL_NO_EXCEPT
Definition blur.h:186
Base definition for an LED controller.
Definition crgb.hpp:179
static fl::i32 grad3d(int hash, fl::i32 x, fl::i32 y, fl::i32 z) FL_NO_EXCEPT
static void floor_frac(fl::i32 fp16, int &ifloor, fl::i32 &frac24) FL_NO_EXCEPT
static constexpr fl::i32 HP_ONE
static fl::i32 fade(fl::i32 t, const fl::i32 *table) FL_NO_EXCEPT
static constexpr int HP_BITS
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) FL_NO_EXCEPT
static fl::i32 pnoise2d_raw(fl::i32 fx_raw, fl::i32 fy_raw, const fl::i32 *fade_lut, const fl::u8 *perm) FL_NO_EXCEPT
static fl::i32 grad(int hash, fl::i32 x, fl::i32 y) FL_NO_EXCEPT
static constexpr int FP_BITS
static fl::s16x16 pnoise3d(fl::s16x16 fx, fl::s16x16 fy, fl::s16x16 fz, const fl::i32 *fade_lut, const fl::u8 *perm) FL_NO_EXCEPT
static fl::i32 lerp(fl::i32 t, fl::i32 a, fl::i32 b) FL_NO_EXCEPT
static constexpr fl::i32 FP_ONE
static void init_fade_lut(fl::i32 *table) FL_NO_EXCEPT
static fl::s16x16 pnoise2d(fl::s16x16 fx, fl::s16x16 fy, const fl::i32 *fade_lut, const fl::u8 *perm) FL_NO_EXCEPT