Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Floating-point addition assembly algorithm

I'm trying to write a binary 8 bit floating point addition algorithm for a picoblaze microcontroller (1 sign bit, 4 exponent bits, and 3 mantissa bits)

I got it to work with positive numbers but I can't figure out how to do it when there are negative numbers too.

My main problem is setting the sign bit of the result, can someone explain how to set it correctly?

My idea was to check the sign of both numbers; then if they're both positive set the sign to 0, if they're both negative set the sign to 1 and use the same methods as before for the addition, and if one is negative and one is positive compare the numbers and use the sign bit of the larger one, but I'm not sure how to compare the two numbers and the code is getting a little cluttered, is there a better way to do it?

like image 979
alpacaboi Avatar asked Jun 22 '26 07:06

alpacaboi


1 Answers

In general (ignoring things like NaN), for A = B + C:

  • if C has larger magnitude than B, swap B and C so that you know that B must have "larger or equal" magnitude. Note: Magnitude ignores the sign bits (e.g. -6 has larger magnitude than +4 because 6 > 4).

  • if B and C have different signs, negate C and do subtract_internal; else do add_internal.

  • for subtract_internal, ignore the sign bits, subtract the magnitudes (not forgetting that B must have "larger or equal" magnitude), then set the sign of A equal to the sign of either B or C (they will have the same sign anyway).

  • for add_internal, ignore the sign bits, add the magnitudes, then set the sign of A equal to the sign of either B or C (they will have the same sign anyway).

Also, in general (ignoring things like NaN), for A = B - C:

  • if C has larger magnitude than B, swap B and C and negate both of them (e.g. A - C == (-C) - (-A)) so that you know that B must have "larger or equal" magnitude.

  • if B and C have different signs, negate C and do add_internal; else do subtract_internal.

like image 186
Brendan Avatar answered Jun 25 '26 18:06

Brendan