Math lib
Math lib
Postby blubb » Thu Aug 13, 2015 7:40 am
Will we eventually get an official math lib from Espressif? Compiling with -lm currently does not work. The compiler tries to put everything into RAM...
ESP8266 is a powerful chip and I would expect to be able to do anything I did with the (rather slow and very limited) ATmega.
Re: Math lib
Postby datltq » Thu Aug 13, 2015 3:08 pm
I did not use the non-RTOS SDK but recently updated of RTOS one include an irom version of libm. I think the non-RTOS SDK should be the same.
https://github.com/espressif/esp_iot_rtos_sdk/
NEW VERSION: 1.0.4
1. memory optimize;
2. add irom version libm.a: libmirom.a;
Dat.
Re: Math lib
Postby blubb » Thu Aug 13, 2015 8:25 pm
There are two problems. The first is, M_PI is not defined. It's not a real issue, of course. Some C compilers don't do this.
The second problem concerns fmod.
Please try to compile
Code: Select all
double ICACHE_FLASH_ATTR test(void) {
return fmod(-10, 3);
}
Error message:
Code: Select all
/opt/Espressif/esp_iot_sdk_v1.3.0/lib/libmirom.a(lib_a-e_fmod.o):(.irom0.literal+0x0): undefined reference to `__ieee754_remainder'
/opt/Espressif/esp_iot_sdk_v1.3.0/lib/libmirom.a(lib_a-e_fmod.o): In function `__ieee754_fmod':
/home/wjg/Workspace/esp-open-sdk/crosstool-NG/.build/src/newlib-2.0.0/newlib/libm/math/e_fmod.c:38: undefined reference to `__ieee754_remainder'
I don't know if this is the result of mixing the libs or if the problem exists in the RTOS-SDK as well. Maybe someone can try that?
Until that I have to implement fmod myself, which luckily is not too hard.
So my request to Espressif would be: Please fix fmod and include libmirom.a in the standard SDK.

Re: Math lib
Postby eriksl » Thu Aug 13, 2015 10:46 pm
The math library IS available and it works, the problem is that everything ends up in iram which fills up before you know it.
So what needs to be done is a math library that has all/most symbols in irom. Do we have volunteers?
I ended up collecting source code replacements for the functions I needed and compiled them myself, including the irom region directive, problem solved...
Re: Math lib
Postby blubb » Fri Aug 14, 2015 2:04 am
https://github.com/espressif/esp_iot_rt ... libmirom.a
Re: Math lib
Postby datltq » Fri Aug 14, 2015 11:25 am
https://github.com/anakod/esp_microc
https://github.com/SuperHouse/esp-open-rtos (newlib with patch for threadsafe)
In the latest RTOS SDK, text are placed in IROM section by default. It's a nice thing

Re: Math lib
Postby eriksl » Fri Aug 14, 2015 5:23 pm
The moving of read only constants to irom sounds interesting. I believe nodemcu already does something like that. What I understand from it, is that you need to install an exception handler (in a very dirty way), because the esp8266 doesn't allow the flash to be used for read only data, and throws an execption. I can find the exception handler in the nodemcu code, but it doesn't seem to do anything but ignoring the exception. I'd expect a piece of emulation code, so I don't really understand, maybe somebody can explain this to me...?
By default all data (read write, read only, static or auto) ends up in dram, just like on Atmel.
Re: Math lib
Postby dkinzer » Sat Aug 15, 2015 7:32 am
I believe that this assertion is incorrect. I have several const data tables in Flash in my application. The only caveat is that you can't access such data directly unless every element happens to be 32-bit aligned and is a multiple of 32-bits in size. I handle this in my app by using some helper routines (similar to the __LPM_xxx() macros for the AVR) to access the elements of the data. Inside the routines, all reads are done on 32-bit boundaries and in multiples of 32-bits irrespective of the alignment and size of the data being requested.eriksl wrote:[...] the esp8266 doesn't allow the flash to be used for read only data, and throws an execption.
Beaverton, OR, USA
Re: Math lib
Postby eriksl » Sat Aug 15, 2015 3:04 pm

So if I would define a struct that is a multiple of 32 bits in size and that elements that are also multiples of 32 bits and I'd make an array of it, it would work? Maybe that's something we can leave to the compiler by specifying alignment and padding requirements.
It does seem a bit cumbersome for arbitrary character strings, though, which take up the largest part of my dram.
Re: Math lib
Postby dkinzer » Sat Aug 15, 2015 10:53 pm
As I mentioned, I use helper routines to retrieve read-only data from Flash. I have specialized routines for byte, word, and dword values as well as a routine to copy an arbitrary byte stream from an arbitrary address. An adaptation of the latter routine appears below and the others can be derived from it. The code below was not compiled so there may be some errors in it but the concept should be clear.eriksl wrote:It does seem a bit cumbersome for arbitrary character strings, though, which take up the largest part of my dram.
Code: Select all
void ICACHE_FLASH_ATTR
flashReadData(const void *addr, void *bufp, uint16_t cnt)
{
if ((bufp == NULL) || (cnt == 0))
return;
// reads from Flash must be done on 32-bit word boundaries
uint16_t part;
uint32_t ofst = (uint32_t)addr & 0x03;
uint8_t *datap = bufp;
union { uint32_t d; uint8_t b[4]; } data;
const char *p = (const char *)addr;
if (ofst)
{
// copy the bytes from the first partial word
if ((part = 4 - ofst) > cnt)
part = cnt;
cnt -= part;
data.d = *(uint32_t *)(p - ofst);
p += part;
while (part--)
*datap++ = data.b[ofst++];
}
// copy the remainder of the data
while (cnt)
{
if ((part = cnt) > 4)
part = 4;
data.d = *(uint32_t *)p;
p += part;
cnt -= part;
ofst = 0;
while (part--)
*datap++ = data.b[ofst++];
}
}
Beaverton, OR, USA
Who is online
Users browsing this forum: No registered users and 28 guests
Login
Newbies Start Here
Are you new to ESP8266?
Unsure what to do?
Dunno where to start?
Start right here!
Latest SDK
Documentation
Complete listing of the official ESP8266 related documentation release by ESPRESSIF!
Must read here!
- All times are UTC+08:00
- Top
- Delete all board cookies
About Us
Espressif Systems is a fabless semiconductor company providing cutting-edge low power WiFi SoCs and wireless solutions for wireless communications and Internet of Things applications. We are the manufacturer of ESP8266EX.