ULP - Standard Math library functions not working in ULP

geonimo22
Posts: 4
Joined: Thu May 22, 2025 12:24 pm

ULP - Standard Math library functions not working in ULP

Postby geonimo22 » Thu Aug 07, 2025 1:26 pm

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

MicroController
Posts: 2663
Joined: Mon Oct 17, 2022 7:38 pm
Location: Europe, Germany

Re: ULP - Standard Math library functions not working in ULP

Postby MicroController » Fri Aug 08, 2025 1:46 pm

You likely don't want or need to do double-precision floating point math on the ULP.

geonimo22
Posts: 4
Joined: Thu May 22, 2025 12:24 pm

Re: ULP - Standard Math library functions not working in ULP

Postby geonimo22 » Fri Aug 08, 2025 2:40 pm

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.

MicroController
Posts: 2663
Joined: Mon Oct 17, 2022 7:38 pm
Location: Europe, Germany

Re: ULP - Standard Math library functions not working in ULP

Postby MicroController » Sat Aug 09, 2025 9:35 am

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.

MicroController
Posts: 2663
Joined: Mon Oct 17, 2022 7:38 pm
Location: Europe, Germany

Re: ULP - Standard Math library functions not working in ULP

Postby MicroController » Tue Aug 12, 2025 9:14 am

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 -
  1. 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)
  2. In the CMakeLists.txt file for your ULP application, link the math library like - target_link_libraries(${ULP_APP_NAME} PRIVATE m)

eriksl
Posts: 199
Joined: Thu Dec 14, 2023 3:23 pm
Location: Netherlands

Re: ULP - Standard Math library functions not working in ULP

Postby eriksl » Thu Aug 14, 2025 2:13 pm

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.

MicroController
Posts: 2663
Joined: Mon Oct 17, 2022 7:38 pm
Location: Europe, Germany

Re: ULP - Standard Math library functions not working in ULP

Postby MicroController » Thu Aug 14, 2025 5:19 pm

I'd even guess that no square root (or floating point) is needed here at all. You often find code like

Code: Select all

if( sqrt(a*a+b*b) > 10.0f ) {...}
where it should be

Code: Select all

if( (a*a+b*b) > (10*10) ) {...}
Last edited by MicroController on Thu Aug 14, 2025 5:29 pm, edited 1 time in total.

eriksl
Posts: 199
Joined: Thu Dec 14, 2023 3:23 pm
Location: Netherlands

Re: ULP - Standard Math library functions not working in ULP

Postby eriksl » Thu Aug 14, 2025 5:28 pm

Ouch, that is horrible!

Looks like the code Bosch ships as example for it's BME sensor chips. Hurts my eyes...

Who is online

Users browsing this forum: Bytespider, Qwantbot and 3 guests