-
-
Notifications
You must be signed in to change notification settings - Fork 8.2k
lib/libm/math: Add fabsf function. #3590
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Conversation
This looks fine, but it's strange that it's needed for Cortex M0... on the micro:bit port it seems that the compiler is generating the code for fabsf inline, so it must know what the definition is. |
This PR is triggered by the issue raised in the nrf51/52 port. After some investigation today, i see that we do not really need this patch as described in the issue tracker in the nrf port. However, i believe this might still be a valid patch. The reason for this function missing during linking was wrong flags to the compiler for nrf51 (cortex-m0), setting "-fno-builtins". Removing this flag solves the problem and fabsf is found. And now it starts getting interesting. Because, if -fno-builtins is removed, and this patch is not applied, it will increase the size of the .text by approximatly 1.2k .text. More investigation also shows that this can benefit cortex-m4 as well with approximately the same decrease of .text usage. If -fno-builtin is enabled on cortex-m4, it lacks two new float functions, one is already present in math.c and can be enabled (copysignf), the other is nanf. By implementing a dummy function of this i get the following results: NRF52/pca10040 (cortex-m4) -fno-builtin not set:
fabsf() patch + nanf() dummy + copysignf() enabled + -fno-builtin:
NRF51/microbit (cortex-m0) -fno-builtin not set:
fabsf() + -fno-builtin:
Summarized: |
cafbf84
to
e22ea4d
Compare
I've changed the PR a bit and added nRF52, before the change:
nRF52, with this patch,
So for me it saves 1248 bytes. |
According to the gcc docs, -fno-builtin will disable all builtins, like memcpy, alloca and others. So that could be what is leading to a significant change in code size with that option enabled. It looks like you can disable specific functions, eg But, regardless of the outcome of such an investigation, including these functions in the repo here is a good idea. They are obviously useful in cases that the compiler/toolchain doesn't provide them. The main question is: does including them in math.c mean they will be always used even if the compiler provides a built-in version? Eg does the output of the stm32 build change with this patch? |
In the atmel-samd port in CircuitPython, we only include
This guarantees inclusion or not. Elsewhere in the Makefile we have:
We found that using the MPy libm saved a lot of space over the toolchain libm. But we had to make sure all double-precision computations were removed so as not to drag in the software implementations of double-precision. See adafruit#325. |
I've discovered something interesting: this has only a major effect when LTO is enabled.
* I had to implement my own As you can see, without LTO this PR (and avoiding builtins) only brings a small improvement. But when LTO is enabled, it brings a much bigger improvement: ~1.2K on the nrf52 (M4) and ~1.3K on the nrf51 (M0). So somehow, link-time optimisation affects this, making it much harder to find the root cause. Additionally, it looks like LTO actually increases code size on both the nrf52 (M4) and nrf51. I'm sure I've seen a decrease before - not sure why it's increasing now. |
fix CPB SPI pin definitions
Closing due to inactivity (and the nrf boards build OK at the moment without this addition). If these functions are needed in the future then we can add them then. |
See: tralamazza#122
This translates to this very short sequence (ARM Cortex M0):
Some background: https://www.doc.ic.ac.uk/~eedwards/compsys/float/nan.html