FastLED 3.10.8
Loading...
Searching...
No Matches
AutoResearchAnimartrixBench.h
Go to the documentation of this file.
1// AutoResearchAnimartrixBench.h
2//
3// Animartrix-representative Perlin-noise benchmark: scalar float (`fl::pnoise`)
4// vs s16x16 fixed-point (`fl::perlin_i16_optimized::pnoise2d`).
5//
6// Why Perlin noise: it's the workhorse of every Animartrix effect — every
7// frame, every pixel goes through one or more `pnoise` calls to drive the
8// polar-coordinate field that shapes the pattern. If a fixed-point variant
9// wins here, it wins in Animartrix.
10//
11// Why s16x16 (and not s8x8): Animartrix's fade/lerp/grad inner loops need
12// the precision of Q16.16 to match the float reference. The s8x8 Q8.8
13// type loses too many bits in the multiplications inside the gradient
14// step. This benchmark mirrors what the FastLED tree already ships
15// (`fl/fx/2d/animartrix_detail/perlin_i16_optimized.cpp.hpp`).
16
17#pragma once
18
19#include <FastLED.h>
23#include "fl/stl/singleton.h"
24
25namespace autoresearch {
27
28// Volatile sink to defeat dead-code elimination on both the float and
29// the i16 outputs. Same trick the SIMD multiply benchmark uses.
38
42
44 int64_t iterations;
45 int64_t pnoise_float_us; // fl::pnoise(x, y, 0) total wall time
46 int64_t pnoise_i16_us; // fl::perlin_i16_optimized::pnoise2d total wall time
47 // The same coordinate grid is fed to both versions so the comparison
48 // measures purely the arithmetic cost of float vs i16 fixed-point.
49};
50
51// Sweep a 2D coordinate grid the same way an Animartrix render pass
52// would (one Perlin lookup per output pixel). 16*16 = 256 pixels per
53// outer iter, matching a 16x16 panel — small enough that the compiler
54// won't unroll the world, large enough that any per-iteration fixed
55// overhead is amortized.
58 r.iterations = iters;
59
60 // Init the i16 implementation's fade lookup table once, off the
61 // benchmark clock. Function-local static so the linter's
62 // static-in-header rule stays happy and C++11's thread-safe init
63 // guarantees a single initialization across calls. The float path
64 // has no equivalent setup.
66 const int32_t* fade_lut = state.fade_lut;
67
68 constexpr int GRID = 16; // 16x16 pixel pass per iteration
69 constexpr float STEP_F = 0.05f; // Animartrix-typical pixel step in noise space
70 // Mirror in fixed-point: 0.05 in Q16.16 = 0.05 * 65536 ≈ 3277
71 constexpr int32_t STEP_I = static_cast<int32_t>(0.05f * 65536.0f);
72
73 // ── Float pnoise ──────────────────────────────────────────────
74 {
75 float ax = 0.0f, ay = 0.0f;
76 int32_t sink = 0;
77 uint32_t t0 = micros();
78 for (int it = 0; it < iters; it++) {
79 // Slowly drift the origin across the iter so the compiler
80 // can't pre-compute. Same pattern Animartrix uses (the
81 // origin advances with `time_speed * dt` each frame).
82 ax += 0.011f;
83 ay += 0.013f;
84 for (int row = 0; row < GRID; row++) {
85 float y = ay + row * STEP_F;
86 for (int col = 0; col < GRID; col++) {
87 float x = ax + col * STEP_F;
88 float n = fl::pnoise(x, y, 0.0f);
89 // Map [-1, 1] → int8 the way Animartrix's output
90 // stage does. Sink to defeat DCE.
91 sink += static_cast<int32_t>(n * 127.0f);
92 }
93 }
94 }
95 uint32_t t1 = micros();
96 state.sink = sink;
97 r.pnoise_float_us = static_cast<int64_t>(t1 - t0);
98 }
99
100 // ── i16 fixed-point pnoise2d ──────────────────────────────────
101 {
102 int32_t ax_i = 0, ay_i = 0;
103 // Q16.16 increment for the slow drift (matches 0.011, 0.013 in float)
104 constexpr int32_t DRIFT_X = static_cast<int32_t>(0.011f * 65536.0f);
105 constexpr int32_t DRIFT_Y = static_cast<int32_t>(0.013f * 65536.0f);
106 int32_t sink = 0;
107 uint32_t t0 = micros();
108 for (int it = 0; it < iters; it++) {
109 ax_i += DRIFT_X;
110 ay_i += DRIFT_Y;
111 for (int row = 0; row < GRID; row++) {
112 int32_t y_i = ay_i + row * STEP_I;
113 for (int col = 0; col < GRID; col++) {
114 int32_t x_i = ax_i + col * STEP_I;
115 // pnoise2d_raw returns i32 Q16.16. Scale to int8
116 // the same way the float path does — divide by
117 // (HP_ONE / 127) so the magnitudes line up.
119 x_i, y_i, fade_lut, fl::PERLIN_NOISE);
120 sink += n >> 9; // crude scale; the actual op cost,
121 // not the value, is what we measure
122 }
123 }
124 }
125 uint32_t t1 = micros();
126 state.sink = sink;
127 r.pnoise_i16_us = static_cast<int64_t>(t1 - t0);
128 }
129
130 return r;
131}
132
133} // namespace animartrix_check
134} // namespace autoresearch
int y
Definition simple.h:93
int x
Definition simple.h:92
TestState state
static T & instance() FL_NO_EXCEPT
Definition singleton.h:81
PerlinBenchResult runPerlinBenchmark(int iters)
FASTLED_FORCE_INLINE float pnoise(float x, float y, float z) FL_NO_EXCEPT
fl::u32 uint32_t
Definition stdint.h:218
fl::i32 int32_t
Definition stdint.h:219
static void init_fade_lut(fl::i32 *table) 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