FastLED 3.10.8
Loading...
Searching...
No Matches
perlin_q16.h
Go to the documentation of this file.
1#pragma once
2
3// 2D Perlin noise Q16 implementation
4// Extracted from animartrix_detail.hpp
5//
6// Q16 variant: Uses 16 fractional bits instead of 24 for faster arithmetic.
7// Trades internal precision for speed: i32 ops instead of i64, smaller LUT.
8
11#include "fl/stl/noexcept.h"
12
13namespace fl {
14
15struct perlin_q16 {
16 static constexpr int HP_BITS = 16;
17 static constexpr fl::i32 HP_ONE = static_cast<fl::i32>(1) << HP_BITS; // 65536 = 1.0
18
19 // Build 257-entry Perlin fade LUT in Q16 format (16 fractional bits).
20 static void init_fade_lut(fl::i32 *table) FL_NO_EXCEPT;
21
22 // 2D Perlin noise. Input s16x16, output s16x16 approx [-1, 1].
24 const fl::i32 *fade_lut,
25 const fl::u8 *perm) FL_NO_EXCEPT;
26
27 // Raw i32 version using Q16 internal precision.
28 static fl::i32 pnoise2d_raw(fl::i32 fx_raw, fl::i32 fy_raw,
29 const fl::i32 *fade_lut,
30 const fl::u8 *perm) FL_NO_EXCEPT;
31
32 private:
33 static constexpr int FP_BITS = fl::s16x16::FRAC_BITS;
34 static constexpr fl::i32 FP_ONE = static_cast<fl::i32>(1) << FP_BITS;
35
36 // Decompose s16x16 raw value into integer floor and Q16 fractional part.
37 static FASTLED_FORCE_INLINE void floor_frac(fl::i32 fp16, int &ifloor,
38 fl::i32 &frac16) FL_NO_EXCEPT;
39
40 // LUT fade: 1 lookup + 1 lerp replaces 5 multiplies.
41 static FASTLED_FORCE_INLINE fl::i32 fade(fl::i32 t, const fl::i32 *table) FL_NO_EXCEPT;
42
43 static FASTLED_FORCE_INLINE fl::i32 lerp(fl::i32 t, fl::i32 a, fl::i32 b) FL_NO_EXCEPT;
44
45 // z=0 gradient via branchless coefficient LUT (Q16 format).
46 static FASTLED_FORCE_INLINE fl::i32 grad(int hash, fl::i32 x, fl::i32 y) FL_NO_EXCEPT;
47};
48
49} // namespace fl
static constexpr int FRAC_BITS
Definition s16x16.h:24
#define FASTLED_FORCE_INLINE
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 constexpr int HP_BITS
Definition perlin_q16.h:16
static FASTLED_FORCE_INLINE void floor_frac(fl::i32 fp16, int &ifloor, fl::i32 &frac16) FL_NO_EXCEPT
static constexpr fl::i32 FP_ONE
Definition perlin_q16.h:34
static FASTLED_FORCE_INLINE fl::i32 grad(int hash, fl::i32 x, fl::i32 y) FL_NO_EXCEPT
static constexpr int FP_BITS
Definition perlin_q16.h:33
static FASTLED_FORCE_INLINE fl::i32 fade(fl::i32 t, const fl::i32 *table) FL_NO_EXCEPT
static FASTLED_FORCE_INLINE fl::i32 lerp(fl::i32 t, fl::i32 a, fl::i32 b) 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::s16x16 pnoise2d(fl::s16x16 fx, fl::s16x16 fy, const fl::i32 *fade_lut, const fl::u8 *perm) FL_NO_EXCEPT
static constexpr fl::i32 HP_ONE
Definition perlin_q16.h:17
static void init_fade_lut(fl::i32 *table) FL_NO_EXCEPT