Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- unsigned add_floats(unsigned f1, unsigned f2) {
- unsigned long long m_res;
- unsigned e_res;
- unsigned sign_res;
- unsigned f1_abs = f1 & 0x7fffffff;
- unsigned f2_abs = f2 & 0x7fffffff;
- if (f1_abs < f2_abs) {//Force f1 to be the larger absolute value
- unsigned temp = f1;
- f1 = f2;
- f2 = temp;
- temp = f1_abs;
- f1_abs = f2_abs;
- f2_abs = temp;
- }
- unsigned e1 = (f1 & 0x7f800000) >> 23; //Extract the exponnent bits
- unsigned e2 = (f2 & 0x7f800000) >> 23;
- unsigned long long m1 = (f1 & 0x7fffff) | (1 << 23); //Extract the mantissa bits, add an implicit 1 so it becomes (1 + m1)
- unsigned long long m2 = (f2 & 0x7fffff) | (1 << 23);
- unsigned f1_sign = (f1 & 0x80000000) >> 31;//0 if positive, 1 if negative
- unsigned f2_sign = (f2 & 0x80000000) >> 31;
- unsigned result;
- unsigned exp_diff;
- // //This is to take care of the NaN case
- // if (!(exponent(f1) ^ 0xff) && mantissa(f1)) {
- // return f1;
- // }
- // if (!(exponent(f2) ^ 0xff) && mantissa(f2)) {
- // return f2;
- // }
- // //This is to catch the infinity case
- // if (!(exponent(f1) ^ 0xff) && !(exponent(f2) ^ 0xff)) {
- // if (diff_sign) {
- // return 0x7f800000 + 1;//NaN, Infinity - Infinity
- // } else {
- // return f1; //Both are either Infinity, or -Infinity, return Infinity
- // }
- // }
- // Need another one to catch the 0 case
- m1 <<= 1;
- m2 <<= 1;
- e_res = e1;
- exp_diff = e1 - e2;
- e2 += exp_diff;
- m2 >>= exp_diff;
- if (exp_diff != 0) {
- if (((f1_sign == 0) && (f2_sign == 0)) || ((f1_sign == 1) && (f2_sign == 1))) {
- sign_res = f1_sign;
- m_res = m1 + m2;
- if (m_res & 0x1 == 1) {
- m_res += 1;
- }
- m_res >>= 1; //Rodned bit is shifted off, so now you only have 24 bits, check the 24th bit
- if ((m_res & (1 << 23) == 0x1000000)) {
- m_res >>= 1;
- e_res++;
- }
- m_res &= 0x7fffff;
- result = (sign_res << 31) | (e_res << 23) | (m_res);
- } else if ((f1_sign == 0) && (f2_sign == 1)) {//f1 > f2, this should spit out a positive number
- sign_res = f1_sign;//This shit is broken by like 1 bit, that's about it
- m_res = m1 - m2;
- // if (m_res & 0x1 == 1) {
- // m_res += 1;
- // }
- m_res >>= 1; //Rodned bit is shifted off, so now you only have 24 bits, check the 24th bit
- if ((m_res & (1 << 23) == 0x1000000)) {
- m_res >>= 1;
- e_res++;
- } else {
- m_res <<= 1;
- e_res--;
- }
- m_res &= 0x7fffff;
- result = (sign_res << 31) | (e_res << 23) | (m_res);
- } else if ((f1_sign == 1) && (f2_sign == 0)) {//f1 > f2, but it should spit out a negative number
- sign_res = f1_sign;//This shit is broken by like 1 bit, that's about it
- m_res = m1 - m2;
- // if (m_res & 0x1 == 1) {
- // m_res += 1;
- // }
- m_res >>= 1; //Rodned bit is shifted off, so now you only have 24 bits, check the 24th bit
- if ((m_res & (1 << 23) == 0x1000000)) {
- m_res >>= 1;
- e_res++;
- } else {
- m_res <<= 1;
- e_res--;
- }
- m_res &= 0x7fffff;
- result = (sign_res << 31) | (e_res << 23) | (m_res);
- }
- } else {
- if (((f1_sign == 0) && (f2_sign == 0)) || ((f1_sign == 1) && (f2_sign == 1))) {// f1 > f2 always, exp_diff = 0
- sign_res = f1_sign;
- m_res = m1 + m2;
- if (m_res & 0x1 == 1) {
- m_res += 1;
- }
- m_res >>= 1; //Rodned bit is shifted off, so now you only have 24 bits, check the 24th bit
- if ((m_res & (1 << 23) == 0x1000000)) {
- m_res >>= 1;
- e_res++;
- }
- m_res &= 0x7fffff;
- result = (sign_res << 31) | (e_res << 23) | (m_res);
- } else if ((f1_sign == 0) && (f2_sign == 1)) {// going to be positive, just an incredibly small number, exp_diff = 0
- sign_res = f1_sign;
- m_res = m1 - m2;
- if (m_res & 0x1 == 1) {
- m_res += 1;
- }
- m_res >>= 1;
- for (unsigned i = 1; i < 25; i++) {//should only be left with 24 bits, keep shifting til the 24th bit is 0
- if ((m_res & (1 << 23) == 0x1000000)) {//I think I want to keep shifting until the 23rd bit, or MSB of Mantissa is a 1
- break;
- }
- m_res <<= 1;
- e_res--;
- }
- m_res &- 0x7fffff;
- result = (sign_res << 31) | (e_res << 23) | (m_res);
- } else if ((f1_sign == 1) && (f2_sign == 0)) {
- sign_res = f1_sign;
- m_res = m1 - m2;
- if (m_res & 0x1 == 1) {
- m_res += 1;
- }
- m_res >>= 1;
- for (unsigned i = 1; i < 25; i++) {//should only be left with 24 bits, keep shifting til the 24th bit is 0
- if ((m_res & (1 << 23) == 0x1000000)) {//I think I want to keep shifting until the 23rd bit, or MSB of Mantissa is a 1
- break;
- }
- m_res <<= 1;
- e_res--;
- }
- m_res &- 0x7fffff;
- result = (sign_res << 31) | (e_res << 23) | (m_res);
- }
- }
- return result;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement