32 bit operations: long vs. float

Status
This old topic is closed. If you want to reopen this topic, contact a moderator using the "Report Post" button.
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?
 
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:
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.
 
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.
 
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.
 
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/ieee754status/754story.html

What Every Computer Scientist Should Know About Floating Point - Computational Science and Engineering
 
Status
This old topic is closed. If you want to reopen this topic, contact a moderator using the "Report Post" button.