-1

I have problem with linking libgcc.a in my project. I need to use sqrtf and log10f, so I have added libgcc.a in makefile by add linker flag "-lm", math.h as include in module and everything compile great but something went wrong in my opinion. When I looked more deeply into mapfile, simply use of sqrtf causes linking also function for double precision floating point handling, for example:

__aeabi_dadd, __aeabi_d2f, __aeabi_ddiv

and many more. I really don't know why single precision implementation of sqrtf or log10f, especially when FPU is on, links double precision functions. I looked into disassembly and I did not find uses of this function in my code. Only in library I found occurrences.

My toolchain is: arm-none-eabi version 6-2017-q2 Core: Cortex-M4F, FPU hardware enabled in linker

Does anyone have any idea why it is happening like this? Code size is really important for me in this case. Thanks for help in advance.

Esato
  • 127
  • 1
  • 9
  • 1
    The intermediate steps could be using higher precision than the input/output. If the library implementation is very heavy for you, you can compile from source any other open source implementation of `sqrt` or simply write your own. On a side note, the [`sqrt`](https://www.tutorialspoint.com/c_standard_library/c_function_sqrt.htm) function has double precision (unless you are using non standard headers). – Ajay Brahmakshatriya Sep 11 '17 at 16:58
  • `libm` is not the same as `libgcc`. The latter is normally automatically linked, even when not linking the standard library (IIRC, there is an option which prohibits linking both). They are not related to each other. Read [ask] and provide all relevant information with a [mcve]. Which standard library implementation do you use? Etc. – too honest for this site Sep 11 '17 at 17:19
  • @Ajay Brahmakshatriya you need to say the compiler how to generate the FP code. M4 FPU has single precision sqrt instruction – 0___________ Sep 11 '17 at 18:05

2 Answers2

0

Use the gcc multitarget - most known for me gcc toolchains do that. You need to specify the correct command line options both for the compiler & linker

example for stm32f303

arm-none-eabi-gcc  -fno-math-errno -mcpu=cortex-m4 -mthumb -mfloat-abi=hard  -fsingle-precision-constant -mfpu=fpv4-sp-d16  -specs=nosys.specs -specs=nano.specs 

-fno-math-errno - does not do the checks, errno is not set, sqrtf is compiled to the single fpu instruction -fsingle-precision-constant - when I mostly use floats I prefer this option to not write the f at the end of the float constants. -mfpu=fpv4-sp-d16 your FPU (actually my F303 one)

the rest is obvious I think. The toolchain should come with the libraries for the many fpus & cores, and linker will choose the correct one. It is lazy but efficient way.

float f;
...
printf("%f", sqrtf(f));

compiles to

 80028fa:   eddd 7a05   vldr    s15, [sp, #20]
 80028fe:   eeb1 7ae7   vsqrt.f32   s14, s15

if you want to benefit from the (almost sometimes) most recent gcc version visit my friends website and build/download his toolchain http://www.freddiechopin.info/en/articles/34-news/98-rewolucja-w-bleeding-edge-toolchain

0___________
  • 60,014
  • 4
  • 34
  • 74
  • I have tried compile your code but I still have software implementation of sqrtf, despite turning off errno by -fno-math-errno flag. I have the libgcc for hard floating point - the implementation of sqrtf uses hardware FP functions. Any idea why with using all flags which you suggest this problem is not solved? – Esato Sep 15 '17 at 13:44
  • @Esato what micro exactly? I suggest to install openSTM32 as it comes with the mulitiarch toolchain. If you install I can generate the project with the correct setting for you. – 0___________ Sep 15 '17 at 13:54
  • Thanks for your proposition but I cannot afford to setup new project and change toolchain in existing project. Rather I would know why proper flags in my toolchaing still generating redundant code for handling math errors. My microcontroler is nRF52832. – Esato Sep 19 '17 at 09:20
-1

You need a version of the standard library (libc+libm) built for an ARM hardfloat (normally "armhf"/"arm-*-eabihf" ABI, although you could use the plain eabi still but with use of VFP instructions, i.e. gcc's "softfp" mode, enabled) target. Otherwise the simple fact that you're configuring hard float support at compile time is not going to change the fact that your libc/libm contain code that was compiled to reference soft-float libgcc functions.

R.. GitHub STOP HELPING ICE
  • 208,859
  • 35
  • 376
  • 711
  • Cortex-M4F does not support VPF instructions. It only has a small, single precision FPU. OP want single precision floats with FPU support only. And libc is normally not a good idea to link on small bare-metal. Hard to tell with more information, though. – too honest for this site Sep 11 '17 at 17:22
  • @Olaf some more effort is requited to archive it. libc and libm should be compiled for the core/FPU combination. – 0___________ Sep 11 '17 at 17:43
  • VPF ? do you mean VFP? It does support v4 – 0___________ Sep 11 '17 at 18:03
  • @PeterJ_01: Hence one should build/install arm-none-eabi with multiarch-support. And yes, I mean VFP (hmm, maybe I was a bit wrong about that). https://www.youtube.com/watch?v=Xz7_3n7xyDg – too honest for this site Sep 11 '17 at 18:22
  • `Hence one should build/install arm-none-eabi with multiarch-support` Linaro (i think he uses it) is multiarch anyway. – 0___________ Sep 11 '17 at 18:51