32 bit operations: long vs. float - diyAudio
 32 bit operations: long vs. float
 User Name Stay logged in? Password
 Home Forums Rules Articles diyAudio Store Gallery Wiki Blogs Register Donations FAQ Calendar Search Today's Posts Mark Forums Read Search

 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
 16th June 2010, 02:59 PM #1 diyAudio Member   Join Date: Sep 2005 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?
 16th June 2010, 04:18 PM #2 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.
diyAudio Member

Join Date: Apr 2010
Location: Evanston, IL
Quote:
 Originally Posted by rtarbell 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.

diyAudio Member

Join Date: Oct 2003
Quote:
 Originally Posted by rtarbell 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.

 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
 17th June 2010, 12:24 PM #6 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.
 23rd June 2010, 09:20 PM #7 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.
diyAudio Member

Join Date: Sep 2004
Location: North East
Blog Entries: 1
Quote:
 Originally Posted by rtarbell 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

 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 OffTrackbacks are Off Pingbacks are Off Refbacks are Off Forum Rules

 Similar Threads Thread Thread Starter Forum Replies Last Post ashok Digital Source 2 25th October 2009 06:00 AM scott wurcer Digital Line Level 6 18th July 2009 12:15 AM jan.didden Digital Source 54 14th March 2008 09:07 PM stelleg151 Digital Source 8 30th April 2006 08:23 PM Circlotron Digital Source 0 17th February 2003 12:39 AM

 New To Site? Need Help?

All times are GMT. The time now is 05:10 AM.