Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- unsigned add_floats(unsigned f1, unsigned f2) {
- unsigned f1_abs = f1 & 0x7fffffff;//Turn the numbers into absolute values for now
- unsigned f2_abs = f2 & 0x7fffffff;
- //infinity case and zero case taken care of, and when both numbers are equal
- if (((f1 & 0x80000000) >> 23 == 0) && ((f2 & 0x80000000) >> 23 == 0)) {//Both numbers are positive
- unsigned e1 = ((f1 & 0x7f800000) >> 23) - 127; //Extract the exponnent bits
- unsigned e2 = ((f2 & 0x7f800000) >> 23) - 127;
- unsigned e_diff;
- unsigned e_total;
- unsigned m1 = (f1 & 0x7fffff) | (1 << 23); //Extract the mantissa bits, add an implicit 1 so it becomes (1 + m1)
- unsigned m2 = (f2 & 0x7fffff) | (1 << 23);
- unsigned m_sum;
- //Exponent and Mantissa are reading just fine
- unsigned result;
- if (f1 == f2) {
- } else if (f1 < f2) {//i.e 5.1 + 16.6
- //METHOD 1 BELOW
- // e_total = e1;
- // e_diff = e2 - e1;
- // m2 <<= e_diff;
- // e2 -= e_diff;
- // m_sum = m1 + m2;
- // m_sum >>= e_diff;
- // e_total += e_diff;
- // if (m_sum & (1 << 24)) {
- // m_sum >>= 1;
- // e_total++;
- // }
- // e_total += 127;
- // result = (e_total << 23) | (m_sum);
- //METHOD 1 ABOVE
- //METHOD 2 BELOW, THIS FACTORS OUT THE HIGHEST POWER
- m1 >>= e2 - e1;
- e1 += e2 - e1; //2^2 + 2^4, a shift of 2
- m_sum = m1 + m2;
- if (m_sum & (1 << 24)) {//If there is a carry
- m_sum >>= 1;
- e1++;
- }
- e1 += 127;
- result = (e1 << 23) | (m_sum);
- //METHOD 2 ABOVE
- return result;
- } else if (f1 > f2) {
- }
- } else if ((f1_abs > f2_abs) && ((f1 & 0x80000000) >> 23 == 0) && ((f2 & 0x80000000) >> 23 == 1)) {//Number 1 is greater than 2, and number 2 is negative
- } else if ((f1_abs > f2_abs) && ((f1 & 0x80000000) >> 23 == 1) && ((f2 & 0x80000000) >> 23 == 0)) {//Number 1 is greater than 2, and number 1 is negative
- } else if ((f1_abs < f2_abs) && ((f1 & 0x80000000) >> 23 == 0) && ((f2 & 0x80000000) >> 23 == 1)) {//Number 2 is greater than 1, and number 2 is negative
- } else if ((f1_abs < f2_abs) && ((f1 & 0x80000000) >> 23 == 1) && ((f2 & 0x80000000) >> 23 == 0)) {//Number 2 is greater than 1, and number 1 is negative
- } else {//When both numbers are negative
- }
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement