• API
• FAQ
• Tools
• Archive
SHARE
TWEET

# Untitled

a guest Feb 16th, 2020 103 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
1. unsigned add_floats(unsigned f1, unsigned f2) {
2.     unsigned f1_abs = f1 & 0x7fffffff;//Turn the numbers into absolute values for now
3.     unsigned f2_abs = f2 & 0x7fffffff;
4.
5.     //infinity case and zero case taken care of, and when both numbers are equal
6.
7.     if (((f1 & 0x80000000) >> 23 == 0) && ((f2 & 0x80000000) >> 23 == 0)) {//Both numbers are positive
8.         unsigned e1 = ((f1 & 0x7f800000) >> 23) - 127; //Extract the exponnent bits
9.         unsigned e2 = ((f2 & 0x7f800000) >> 23) - 127;
10.         unsigned e_diff;
11.         unsigned e_total;
12.
13.         unsigned m1 = (f1 & 0x7fffff) | (1 << 23); //Extract the mantissa bits, add an implicit 1 so it becomes (1 + m1)
14.         unsigned m2 = (f2 & 0x7fffff) | (1 << 23);
15.         unsigned m_sum;
16.         //Exponent and Mantissa are reading just fine
17.
18.         unsigned result;
19.
20.         if (f1 == f2) {
21.
22.         } else if (f1 < f2) {//i.e 5.1 + 16.6
23.             //METHOD 1 BELOW
24.             // e_total = e1;
25.             // e_diff = e2 - e1;
26.
27.             // m2 <<= e_diff;
28.             // e2 -= e_diff;
29.
30.             // m_sum = m1 + m2;
31.
32.             // m_sum >>= e_diff;
33.             // e_total += e_diff;
34.
35.             // if (m_sum & (1 << 24)) {
36.             //     m_sum >>= 1;
37.             //     e_total++;
38.             // }
39.
40.             // e_total += 127;
41.             // result = (e_total << 23) | (m_sum);
42.             //METHOD 1 ABOVE
43.
44.
45.             //METHOD 2 BELOW, THIS FACTORS OUT THE HIGHEST POWER
46.             m1 >>= e2 - e1;
47.             e1 += e2 - e1; //2^2 + 2^4, a shift of 2
48.
49.             m_sum = m1 + m2;
50.
51.             if (m_sum & (1 << 24)) {//If there is a carry
52.                 m_sum >>= 1;
53.                 e1++;
54.             }
55.
56.             e1 += 127;
57.
58.             result = (e1 << 23) | (m_sum);
59.             //METHOD 2 ABOVE
60.
61.             return result;
62.
63.         } else if (f1 > f2) {
64.
65.         }
66.
67.
68.     } 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
69.
70.     } 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
71.
72.     } 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
73.
74.     } 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
75.
76.     } else {//When both numbers are negative
77.
78.     }
79.
80.     return 0;
81. }
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy.
Top