Skip to content
This repository was archived by the owner on Feb 10, 2026. It is now read-only.

Commit 1c3fdb7

Browse files
committed
optimization: only use "float" math functions
- saves 5KB flash and some RAM -allow to build with -D WLED_USE_UNREAL_MATH, to restore old behaviour and save another 6KB flash
1 parent 166316e commit 1c3fdb7

2 files changed

Lines changed: 18 additions & 1 deletion

File tree

platformio.ini

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -352,6 +352,7 @@ platform_packages = ${common.platform_packages}
352352
board_build.ldscript = ${common.ldscript_1m128k}
353353
build_unflags = ${common.build_unflags}
354354
build_flags = ${common.build_flags_esp8266} -D WLED_RELEASE_NAME=ESP01 -D WLED_DISABLE_OTA
355+
; -D WLED_USE_UNREAL_MATH ;; may cause wrong sunset/sunrise times, but saves 7064 bytes FLASH and 975 bytes RAM
355356
lib_deps = ${esp8266.lib_deps}
356357

357358
[env:esp07]
@@ -605,6 +606,7 @@ build_flags = ${common.build_flags} ${esp32s2.build_flags} -D WLED_RELEASE_NAME=
605606
-DARDUINO_USB_DFU_ON_BOOT=0
606607
-DLOLIN_WIFI_FIX ; seems to work much better with this
607608
-D WLED_USE_PSRAM
609+
; -D WLED_USE_UNREAL_MATH ;; may cause wrong sunset/sunrise times, but saves 6792 bytes FLASH
608610
-D WLED_WATCHDOG_TIMEOUT=0
609611
-D CONFIG_ASYNC_TCP_USE_WDT=0
610612
-D LEDPIN=16

wled00/ntp.cpp

Lines changed: 16 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,21 @@
22
#include "wled.h"
33
#include "fcn_declare.h"
44

5+
// on esp8266, building with `-D WLED_USE_UNREAL_MATH` saves around 7Kb flash and 1KB RAM
6+
// warning: causes errors in sunset calculations, see #3400
7+
#if defined(WLED_USE_UNREAL_MATH)
8+
#define sinf sin_t
9+
#define asinf asin_t
10+
#define cosf cos_t
11+
#define acosf acos_t
12+
#define tanf tan_t
13+
#define atanf atan_t
14+
#define fmodf fmod_t
15+
#define floorf floor_t
16+
#else
17+
#include <math.h>
18+
#endif
19+
520
/*
621
* Acquires time from NTP server
722
*/
@@ -426,7 +441,7 @@ int getSunriseUTC(int year, int month, int day, float lat, float lon, bool sunse
426441
float L = fmodf(M + (1.916f * sinf(DEG_TO_RAD*M)) + (0.02f * sinf(2*DEG_TO_RAD*M)) + 282.634f, 360.0f);
427442

428443
//5a. calculate the Sun's right ascension
429-
float RA = fmodf(RAD_TO_DEG*atan(0.91764f * tan(DEG_TO_RAD*L)), 360.0f);
444+
float RA = fmodf(RAD_TO_DEG*atanf(0.91764f * tanf(DEG_TO_RAD*L)), 360.0f);
430445

431446
//5b. right ascension value needs to be in the same quadrant as L
432447
float Lquadrant = floorf( L/90) * 90;

0 commit comments

Comments
 (0)