Fast SQRT for cortex M4 with FPU unit

General discussion on mikroPascal PRO for ARM.
Post Reply
Author
Message
yo2lio
Posts: 1878
Joined: 19 Sep 2006 12:57
Location: Romania, Arad City
Contact:

Fast SQRT for cortex M4 with FPU unit

#1 Post by yo2lio » 07 Aug 2015 13:23

Hello,

STM32F4 have FPU unit.
So, almost floating point operations can be compute in one instruction cycle.

I made a fast SQRT function, that use VSQRT FPU instruction.

Enjoy.

Code: Select all

program Test_SQRT;

var res : real;

function SQRT_F4(f_in : real) : real;
begin
  R0 := @f_in;
  asm
    VLDR.32 S0, [R0, #0]
    VSQRT.F32 S0, S0
  end;
  R0 := @result;
  asm
    VSTR.32 S0, [R0, #0]
  end;
end;

begin
  res := SQRT_F4(1000000.0);  // 39 cycle, using FPU internal VSQRT instruction
  res := sqrt(1000000.0);     // 276 cycle, using internal trigon library
end.
Best regards, Florin Andrei Medrea.

http://www.microelemente.ro/
http://www.microelemente.ro/produse-si-servicii/
http://www.microelemente.ro/custom-software/

mail : florin@microelemente.ro

User avatar
filip
mikroElektronika team
Posts: 11874
Joined: 25 Jan 2008 09:56

Re: Fast SQRT for cortex M4 with FPU unit

#2 Post by filip » 14 Aug 2015 16:12

Hi,

Thanks for this, I will pass it to our developers.

Regards,
Filip.

SkvorZbynek
Posts: 2
Joined: 19 Nov 2016 15:18

Re: Fast SQRT for cortex M4 with FPU unit

#3 Post by SkvorZbynek » 19 Nov 2016 15:26

Thank you for nice post.

I miss the information whether the MicroPascal for STM 32 compiler can generate code that uses FPU where present (eg. for STM32F4.., STM32F7.. Cortex M4 and higher processors). May anybody help me to learn about it?

User avatar
Aleksandar.Mitrovic
mikroElektronika team
Posts: 1697
Joined: 11 Mar 2015 12:48

Re: Fast SQRT for cortex M4 with FPU unit

#4 Post by Aleksandar.Mitrovic » 21 Nov 2016 16:08

Hi,

Welcome to "mikroElektronika" forum.

Yes, you should be able to write code for FPU in our mikroPascal PRO for ARM compiler.

More information how to work with this one you should find in the datasheet for the specific microcontroller and application manual:
http://www.st.com/content/ccc/resource/ ... 047230.pdf

Kind regards,
Aleksandar

SkvorZbynek
Posts: 2
Joined: 19 Nov 2016 15:18

Re: Fast SQRT for cortex M4 with FPU unit

#5 Post by SkvorZbynek » 30 Nov 2016 11:02

Aleksandar.Mitrovic wrote:...

Yes, you should be able to write code for FPU in our mikroPascal PRO for ARM compiler.

...
Thank you. Let me rephrase the question in a different manner:

Say, I have Pascal code like

var res,a,b,c:real;
...
res:= sqr(a) + sqr(b) + a*b*sin(a*c);


Will the compiler automatically generate a code that uses FPU instructions in the case that Cortex 4 or Cortex 7 processor is used?
(Say like in the case a $N directive was used in Turbo or Borland Pascal).

Regards

User avatar
Aleksandar.Mitrovic
mikroElektronika team
Posts: 1697
Joined: 11 Mar 2015 12:48

Re: Fast SQRT for cortex M4 with FPU unit

#6 Post by Aleksandar.Mitrovic » 01 Dec 2016 14:22

Hi,
SkvorZbynek wrote:Will the compiler automatically generate a code that uses FPU instructions in the case that Cortex 4 or Cortex 7 processor is used?
If the selected MCU have hardware FPU, compiler will generate code for that.

In case it doesn't have that functionality like Cortex 3, it will use software FPU. Which is much slower.

You can write some example which use float variable and take a look at the project in Assembly or Lister view.

Kind regards,
Aleksandar

Post Reply

Return to “mikroPascal PRO for ARM General”