Which is faster: floating point division or casting to 64-bit int?

cancel
Showing results for 
Show  only  | Search instead for 
Did you mean: 

Which is faster: floating point division or casting to 64-bit int?

454 Views
aberger
Senior Contributor I

Consider the simple calculation:

result = (A/B)*scaleFactor

where all quantities are unsigned, 32-bit integers.

A/B is strictly less than 1, but 

A*scaleFactor could be larger than 2^32.

Therefore, it seems I have two options:

result = ((float)(A)/B)*scaleFactor

or

result = ((uint64_t)(A)*scaleFactor)/B

Which sequence of operations is faster on the K64?

Labels (1)
0 Kudos
Reply
8 Replies

50 Views
aberger
Senior Contributor I

I was able to find this partial response by digging into the web page source:

My guess was that your first version with the single precision float operation will be faster, as the ARM Cortex-M4F can use the FPU for this, while your 64bit version requires a library runtime routine which very likely is slower. All depends to some extend to compiler optimizations and value of da...

0 Kudos
Reply

54 Views
ErichStyger
Specialist I

Not sure what could have been the original solution, but a while back I wrote about this:

https://mcuoneclipse.com/2019/03/29/be-aware-floating-point-operations-on-arm-cortex-m4f/

 

0 Kudos
Reply

54 Views
ErichStyger
Specialist I

I have no idea. I even cannot display the content of this thread, all what I get is

"This widget could not be displayed."

0 Kudos
Reply

55 Views
myke_predko
Senior Contributor III

@aberger @BlackNight 

What happened to this thread?  I'd be interested in the comments.

0 Kudos
Reply

76 Views
aberger
Senior Contributor I

Why did the solution to this discussion vanish?

0 Kudos
Reply

81 Views
aberger
Senior Contributor I

Thanks Erich, this is great. At this time, I'm using the P&E debugger in MCUXpresso, so it unfortunately looks like I can't do the DWT cycle counting.

0 Kudos
Reply

81 Views
BlackNight
NXP Employee
NXP Employee

Yes, that's currently only supported with the LinkServer connection. For P&E you could directly read the DWT cycle counter in the memory view or store the values in your code, see Cycle Counting on ARM Cortex-M with DWT | MCU on Eclipse 

I hope this helps,

Erich

57 Views
aberger
Senior Contributor I

Hi @BlackNight - You previously provided the accepted solution to this post. For some reason NXP has removed that solution. Is there any chance you still have the your original post to this question? Would you mind re-sharing it? Thank you.

0 Kudos
Reply