FastLED 3.9.15
Loading...
Searching...
No Matches
integer_math.h
Go to the documentation of this file.
1#pragma once
2
3#include "fl/stl/stdint.h" // IWYU pragma: keep
4
5namespace fl {
6namespace gfx {
7namespace detail {
8
12inline fl::i32 isqrt32_floor(fl::i32 n) {
13 if (n <= 0) return 0;
14 fl::u32 un = (fl::u32)n;
15 fl::u32 result = 0;
16 fl::u32 bit = 1u << 30; // highest even power of 2
17 while (bit > un) bit >>= 2; // find starting magnitude
18 while (bit != 0) {
19 fl::u32 t = result + bit;
20 result >>= 1;
21 if (un >= t) { un -= t; result += bit; }
22 bit >>= 2;
23 }
24 return (fl::i32)result;
25}
26
30inline fl::i32 isqrt32_ceil(fl::i32 n) {
31 if (n <= 0) return 0;
32 fl::u32 x = (fl::u32)n;
33 fl::u32 s = 1u << 16; // initial guess: sqrt(2^32) = 2^16
34 for (int i = 0; i < 8; ++i) { // 8 iterations converges for 32-bit input
35 s = (s + x / s) >> 1; // Newton iteration: s = (s + n/s) / 2
36 }
37 return (fl::i32)s;
38}
39
40} // namespace detail
41} // namespace gfx
42} // namespace fl
fl::i32 isqrt32_ceil(fl::i32 n)
Integer square root using Newton iteration Computes ceil(sqrt(n)) for positive integers More accurate...
fl::i32 isqrt32_floor(fl::i32 n)
Integer square root using digit-by-digit algorithm Computes floor(sqrt(n)) for positive integers Uses...
============================================================================
Definition primitives.h:132
expected< T, E > result
Alias for expected (Rust-style naming)
Definition result.h:31
Base definition for an LED controller.
Definition crgb.hpp:179