r/AskComputerScience • u/Negan6699 • Nov 21 '24
Want to know if my method for calculating floating point on 8/16 bit CPUs
So I was trying to find a way for a 8 or 16bit CPU to process 32/64bit floating point. Online I found a bunch of methods that were converting to fixed point and such and thought it to be unnecessary. Example of my method for 32bit floating point for 16bit CPU: 1) take the lower 16bits of the mantisa and treat it as normal 16bit integer, perform the math on it then store it 2) take the upper half and isolate the rest of the mantisa via an AND operation then perform math on it and store it 3) isolate the exponent with AND again and modify it if needed then store it 4) isolate and do the operations on the sign bit
What I want to ask is if this would give the same result or are the conversions to fixed point necessary ?
1
u/computerarchitect MSCS, CS Pro (10+) Nov 21 '24
No, that doesn't work. Consider base ten, without the implicit bit concept that IEEE 754 has.
1 + 10000.
The mantissa for both of these values is 1. Your code, as described, computes 2 as the mantissa, which is obviously wrong.
It's nowhere near as simple as you described, how do you handle denormals, NaNs, infinities, various rounding modes?
2
u/johndcochran Nov 21 '24
Basically, consider that there is only 2 major "operations" you need to implement.
Now, it's quite likely that you'll actually have separate multiply/divide routines simply because there isn't a lot of commonality between them. But for both of them, the order of operations is basically the same. They are:
And for both operations, the final justification (ignoring denormals and such) involves a single left or right shift combined with a simply increment/decrement of the exponent.
Add and subtract are extremely different. The order of operations is
As for step 1 above, the amount you shift may range from 0 (exponents already match) to the entire length of the mantissa. So, there can be quite a bit of shifting involved. Additionally, the justification for step 3 many also range from 0 to a rather significant number.
As for IEEE-754 format numbers, I'd recommend the following general process.
If you look at the old 8087 floating point coprocessor, you'll see that it effectively does the above by using the 80 bit extended precision format for internal calculations. That 80 bit format does not have any hidden implied bits, the absence of which makes things much simplier. It's rather unfortunate that Intel had to make that internal format programmer visible in order to allow an OS to save the state of the 8087 during task switches, but that's another issue.