32 bit operations: long vs. float - diyAudio
Go Back   Home > Forums > General Interest > Everything Else

Everything Else Anything related to audio / video / electronics etc) BUT remember- we have many new forums where your thread may now fit! .... Parts, Equipment & Tools, Construction Tips, Software Tools......

Please consider donating to help us continue to serve you.

Ads on/off / Custom Title / More PMs / More album space / Advanced printing & mass image saving
Reply
 
Thread Tools Search this Thread
Old 16th June 2010, 02:59 PM   #1
diyAudio Member
 
Join Date: Sep 2005
Default 32 bit operations: long vs. float

Quick question for the digital people:

If I have a 32 bit microcontroller (say, a PIC32), and I add two "long" variables together, will that take the same amount of time (processor cycles) as adding two "float" variables together (since they are both 32 bit numbers)?

What about the same question above, except with multiplication/division?
  Reply With Quote
Old 16th June 2010, 04:18 PM   #2
jcx is offline jcx  United States
diyAudio Member
 
Join Date: Feb 2003
Location: ..
assuming at best integer multiplier accumulator

adding floats takes longer since the mantissas have to be aligned to the same exponent by shifting

multiplication only takes a few more ops since you multiply mantissas and add exponents

both have overhead in "unpacking" exponent from mantissa in floating point

do everything in your power to rearrange the math so you avoid division



there are chips with floating point "engines" - usually the manufacturer is very vocal about offering floating point hardware: if you don't see it in the features/datasheet bullet items then you likely have integer ALU/MAC

Last edited by jcx; 16th June 2010 at 04:35 PM.
  Reply With Quote
Old 16th June 2010, 04:41 PM   #3
diyAudio Member
 
Join Date: Apr 2010
Location: Evanston, IL
Quote:
Originally Posted by rtarbell View Post
Quick question for the digital people:

If I have a 32 bit microcontroller (say, a PIC32), and I add two "long" variables together, will that take the same amount of time (processor cycles) as adding two "float" variables together (since they are both 32 bit numbers)?

What about the same question above, except with multiplication/division?
Based on my limited knowledge, integer operations (such as int, long) will generally be faster than floating point operations (such as float, double). Thumb rule: If you are using whole numbers, stick to integers and longs, and use floats only if you are using fractional numbers. Plus, you also need to be careful with floating point as they can cause rounding errors, so use it only if you must.

In terms of performance, one exception to this rule is if you are using a microprocessor that is specifically built to process floating point data, and even in this case, floating point operations will generally only equal the performance of integer operations, and will not exceed.

Float versus Integer arithmetic performance on modern chips - Stack Overflow

Please note that what I say is only a general principle that I know - I have not worked with microcontrollers enough to give you a definitive answer.
  Reply With Quote
Old 16th June 2010, 05:02 PM   #4
macboy is offline macboy  Canada
diyAudio Member
 
Join Date: Oct 2003
Location: Ottawa, Canada
Quote:
Originally Posted by rtarbell View Post
Quick question for the digital people:

If I have a 32 bit microcontroller (say, a PIC32), and I add two "long" variables together, will that take the same amount of time (processor cycles) as adding two "float" variables together (since they are both 32 bit numbers)?

What about the same question above, except with multiplication/division?
On a PIC32, no, the float definitely takes a lot longer. The PIC32 does not have a FPU, so it does not handle float internally at all. All floating point operations are emulated in software (via your compiler). You can expect a FP add/sub to take between a few and a few dozen cycles, a multiply somewhere in the order of 100 to 1000 and a divide will take even longer. Start doing things like log and antilog and it gets hairy, taking thousands of cycles. The fact that it is a 32-bit MCU doesn't help that much when you start doing FP. For 32-bit integer though, the PIC32 has add/sub/multiply, and MAC (multiply and add or subtract) instructions. IIRC it has division assistance, which helps, but divide is still slow, and takes a lot of software to do (there are no divide instructions).

Why not put both Int and FP operations into a very simple program, compile it, and run it in the MPSIM simulator? You can get cycle-accurate timings that way and then you'll see your answer right there in front of you. This is also a great way to see the impact of different optimization switches.
  Reply With Quote
Old 16th June 2010, 09:56 PM   #5
diyAudio Member
 
Join Date: Aug 2008
You can always use integers in place of floating point by changing the units.

Also, multiplication and division by powers of 2 (2, 4, 8, 16...) is very fast, because it's just a bit shift.
__________________
Tyler
  Reply With Quote
Old 17th June 2010, 12:24 PM   #6
macboy is offline macboy  Canada
diyAudio Member
 
Join Date: Oct 2003
Location: Ottawa, Canada
I need to correct myself, the PIC32 does have int divide instructions, both signed and unsigned. I was thinking of the PIC24 that has divide assistance.
  Reply With Quote
Old 23rd June 2010, 09:20 PM   #7
fzaad is offline fzaad  Europe
diyAudio Member
 
Join Date: Nov 2006
so what programming language and compiler you are using. can't think straight right now but generally if you have 32-bit processor it takes twice as long to calculate two 32-bit vectors than two 16-bit long vectors. Bit wide of float, douple... and others differ between programming language.
  Reply With Quote
Old 27th June 2010, 02:04 PM   #8
PB2 is offline PB2  United States
diyAudio Member
 
PB2's Avatar
 
Join Date: Sep 2004
Location: North East
Blog Entries: 1
Quote:
Originally Posted by rtarbell View Post
Quick question for the digital people:

If I have a 32 bit microcontroller (say, a PIC32), and I add two "long" variables together, will that take the same amount of time (processor cycles) as adding two "float" variables together (since they are both 32 bit numbers)?

What about the same question above, except with multiplication/division?
http://www.cs.berkeley.edu/~wkahan/i.../754story.html

What Every Computer Scientist Should Know About Floating Point - Computational Science and Engineering
__________________
Pete Basel
http://www.linkedin.com/in/petebasel
  Reply With Quote

Reply


Hide this!Advertise here!
Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are Off
Pingbacks are Off
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Anyone hear this 32 bit/192Khz source ? ashok Digital Source 2 25th October 2009 06:00 AM
Cooledit/Audition 32 bit not IEEE-754? scott wurcer Digital Line Level 6 18th July 2009 12:15 AM
32-bit DAC jan.didden Digital Source 54 14th March 2008 09:07 PM
32 bit DAC? stelleg151 Digital Source 8 30th April 2006 08:23 PM
32-bit mono on CD-A Circlotron Digital Source 0 17th February 2003 12:39 AM


New To Site? Need Help?

All times are GMT. The time now is 06:51 PM.


vBulletin Optimisation provided by vB Optimise (Pro) - vBulletin Mods & Addons Copyright © 2014 DragonByte Technologies Ltd.
Copyright 1999-2014 diyAudio

Content Relevant URLs by vBSEO 3.3.2