Page 1 of 1
ULP - Standard Math library functions not working in ULP
Posted: Thu Aug 07, 2025 1:26 pm
by geonimo22
Hello,
I try to implement some ULP functions with an ESP32S3 RISCV with IDF in PlatformIO, and I struggle to fix an issue with functions from standard math library (like sqrt(), pow(),...). Actually it works, if I do a sqrt(2) with a fix number, but does not if I do a sqrt(myvariable).
I get the following message when compiling : undefined reference to `sqrt'
I have tried to add the library manually, not working. I read that it could be solved with adding a '-lm' in the gcc command (linking math library), but I even don't know where to add this in IDF with PlatformIO.
Would you please help me to understand if this is the solution or not, and if yes, where to do this.
Thank you in advance
Re: ULP - Standard Math library functions not working in ULP
Posted: Fri Aug 08, 2025 1:46 pm
by MicroController
You likely don't want or need to do double-precision floating point math on the ULP.
Re: ULP - Standard Math library functions not working in ULP
Posted: Fri Aug 08, 2025 2:40 pm
by geonimo22
about the precision we will see later, but don't tell me that I have to wake up the main controller to do a basic sqrt(). Otherwise, what is the purpose of these ULP stuff, if you can't do basic operation. Better to go with an ATTiny then, right ?
Still, I would like still to fix the issue with math.h function not available.
Re: ULP - Standard Math library functions not working in ULP
Posted: Sat Aug 09, 2025 9:35 am
by MicroController
The ULP doesn't have an FPU, so all floating point operations have to be emulated in software. This is slow. Furthermore, the software emulation, especially of double precision floats, may introduce a lot of code into your ULP program - which all needs to fit into the RTC RAM.
Even on the main CPU double precision floats are slow, and the ULP is 100x slower. Closely review your ULP algorithm and you'll likely find that you can do it without sqrt(), or floating point, in the first place. If you can share your code we could help with that.
Re: ULP - Standard Math library functions not working in ULP
Posted: Tue Aug 12, 2025 9:14 am
by MicroController
For future reference:
Manually linking the math library's ("libm") floating point functions into the RISC-V ULP binary can be done:
https://github.com/espressif/esp-idf/is ... 3173468832
Including it would be a two step process -
- Switch to the newer ULP build system (if not already in use) which allows for adding/linking custom libraries to the ULP project. (See the build_system example for reference)
- In the CMakeLists.txt file for your ULP application, link the math library like - target_link_libraries(${ULP_APP_NAME} PRIVATE m)
Re: ULP - Standard Math library functions not working in ULP
Posted: Thu Aug 14, 2025 2:13 pm
by eriksl
And besides all that, I (really) don't think a fully-fledged sqrt is what is needed here, I can't imagine it's use on an ULP processor. I think some integer approximation would be probably be sufficient. It's amazing what you can do with a few bit shifts, additions and subtractions. I'd recommend searching for such an algorithm that is sufficiently accurate. Not only saves a lot of processor time, but also tons of math library code.
Re: ULP - Standard Math library functions not working in ULP
Posted: Thu Aug 14, 2025 5:19 pm
by MicroController
I'd even guess that no square root (or floating point) is needed here at all. You often find code like
where it should be
Re: ULP - Standard Math library functions not working in ULP
Posted: Thu Aug 14, 2025 5:28 pm
by eriksl
Ouch, that is horrible!
Looks like the code Bosch ships as example for it's BME sensor chips. Hurts my eyes...