Advertisement
Guest User

Untitled

a guest
Feb 20th, 2020
118
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 5.71 KB | None | 0 0
  1. unsigned add_floats(unsigned f1, unsigned f2) {
  2. unsigned long long m_res;
  3. unsigned e_res;
  4. unsigned sign_res;
  5.  
  6. unsigned f1_abs = f1 & 0x7fffffff;
  7. unsigned f2_abs = f2 & 0x7fffffff;
  8.  
  9. if (f1_abs < f2_abs) {//Force f1 to be the larger absolute value
  10. unsigned temp = f1;
  11. f1 = f2;
  12. f2 = temp;
  13.  
  14. temp = f1_abs;
  15. f1_abs = f2_abs;
  16. f2_abs = temp;
  17. }
  18.  
  19. unsigned e1 = (f1 & 0x7f800000) >> 23; //Extract the exponnent bits
  20. unsigned e2 = (f2 & 0x7f800000) >> 23;
  21.  
  22. unsigned long long m1 = (f1 & 0x7fffff) | (1 << 23); //Extract the mantissa bits, add an implicit 1 so it becomes (1 + m1)
  23. unsigned long long m2 = (f2 & 0x7fffff) | (1 << 23);
  24.  
  25. unsigned f1_sign = (f1 & 0x80000000) >> 31;//0 if positive, 1 if negative
  26. unsigned f2_sign = (f2 & 0x80000000) >> 31;
  27.  
  28. unsigned result;
  29. unsigned exp_diff;
  30.  
  31. // //This is to take care of the NaN case
  32. // if (!(exponent(f1) ^ 0xff) && mantissa(f1)) {
  33. // return f1;
  34. // }
  35. // if (!(exponent(f2) ^ 0xff) && mantissa(f2)) {
  36. // return f2;
  37. // }
  38.  
  39. // //This is to catch the infinity case
  40. // if (!(exponent(f1) ^ 0xff) && !(exponent(f2) ^ 0xff)) {
  41. // if (diff_sign) {
  42. // return 0x7f800000 + 1;//NaN, Infinity - Infinity
  43. // } else {
  44. // return f1; //Both are either Infinity, or -Infinity, return Infinity
  45. // }
  46. // }
  47.  
  48. // Need another one to catch the 0 case
  49.  
  50. m1 <<= 1;
  51. m2 <<= 1;
  52.  
  53. e_res = e1;
  54. exp_diff = e1 - e2;
  55. e2 += exp_diff;
  56. m2 >>= exp_diff;
  57.  
  58. if (exp_diff != 0) {
  59. if (((f1_sign == 0) && (f2_sign == 0)) || ((f1_sign == 1) && (f2_sign == 1))) {
  60. sign_res = f1_sign;
  61. m_res = m1 + m2;
  62.  
  63. if (m_res & 0x1 == 1) {
  64. m_res += 1;
  65. }
  66.  
  67. m_res >>= 1; //Rodned bit is shifted off, so now you only have 24 bits, check the 24th bit
  68.  
  69. if ((m_res & (1 << 23) == 0x1000000)) {
  70. m_res >>= 1;
  71. e_res++;
  72. }
  73.  
  74. m_res &= 0x7fffff;
  75. result = (sign_res << 31) | (e_res << 23) | (m_res);
  76.  
  77. } else if ((f1_sign == 0) && (f2_sign == 1)) {//f1 > f2, this should spit out a positive number
  78. sign_res = f1_sign;//This shit is broken by like 1 bit, that's about it
  79. m_res = m1 - m2;
  80.  
  81. // if (m_res & 0x1 == 1) {
  82. // m_res += 1;
  83. // }
  84.  
  85. m_res >>= 1; //Rodned bit is shifted off, so now you only have 24 bits, check the 24th bit
  86.  
  87. if ((m_res & (1 << 23) == 0x1000000)) {
  88. m_res >>= 1;
  89. e_res++;
  90. } else {
  91. m_res <<= 1;
  92. e_res--;
  93. }
  94.  
  95. m_res &= 0x7fffff;
  96. result = (sign_res << 31) | (e_res << 23) | (m_res);
  97.  
  98. } else if ((f1_sign == 1) && (f2_sign == 0)) {//f1 > f2, but it should spit out a negative number
  99. sign_res = f1_sign;//This shit is broken by like 1 bit, that's about it
  100. m_res = m1 - m2;
  101.  
  102. // if (m_res & 0x1 == 1) {
  103. // m_res += 1;
  104. // }
  105.  
  106. m_res >>= 1; //Rodned bit is shifted off, so now you only have 24 bits, check the 24th bit
  107.  
  108. if ((m_res & (1 << 23) == 0x1000000)) {
  109. m_res >>= 1;
  110. e_res++;
  111. } else {
  112. m_res <<= 1;
  113. e_res--;
  114. }
  115.  
  116. m_res &= 0x7fffff;
  117. result = (sign_res << 31) | (e_res << 23) | (m_res);
  118. }
  119.  
  120. } else {
  121. if (((f1_sign == 0) && (f2_sign == 0)) || ((f1_sign == 1) && (f2_sign == 1))) {// f1 > f2 always, exp_diff = 0
  122. sign_res = f1_sign;
  123. m_res = m1 + m2;
  124.  
  125. if (m_res & 0x1 == 1) {
  126. m_res += 1;
  127. }
  128.  
  129. m_res >>= 1; //Rodned bit is shifted off, so now you only have 24 bits, check the 24th bit
  130.  
  131. if ((m_res & (1 << 23) == 0x1000000)) {
  132. m_res >>= 1;
  133. e_res++;
  134. }
  135.  
  136. m_res &= 0x7fffff;
  137. result = (sign_res << 31) | (e_res << 23) | (m_res);
  138. } else if ((f1_sign == 0) && (f2_sign == 1)) {// going to be positive, just an incredibly small number, exp_diff = 0
  139. sign_res = f1_sign;
  140. m_res = m1 - m2;
  141.  
  142.  
  143. if (m_res & 0x1 == 1) {
  144. m_res += 1;
  145. }
  146.  
  147. m_res >>= 1;
  148.  
  149. for (unsigned i = 1; i < 25; i++) {//should only be left with 24 bits, keep shifting til the 24th bit is 0
  150. if ((m_res & (1 << 23) == 0x1000000)) {//I think I want to keep shifting until the 23rd bit, or MSB of Mantissa is a 1
  151. break;
  152. }
  153. m_res <<= 1;
  154. e_res--;
  155. }
  156.  
  157. m_res &- 0x7fffff;
  158. result = (sign_res << 31) | (e_res << 23) | (m_res);
  159.  
  160. } else if ((f1_sign == 1) && (f2_sign == 0)) {
  161. sign_res = f1_sign;
  162. m_res = m1 - m2;
  163.  
  164.  
  165. if (m_res & 0x1 == 1) {
  166. m_res += 1;
  167. }
  168.  
  169. m_res >>= 1;
  170.  
  171. for (unsigned i = 1; i < 25; i++) {//should only be left with 24 bits, keep shifting til the 24th bit is 0
  172. if ((m_res & (1 << 23) == 0x1000000)) {//I think I want to keep shifting until the 23rd bit, or MSB of Mantissa is a 1
  173. break;
  174. }
  175. m_res <<= 1;
  176. e_res--;
  177. }
  178.  
  179. m_res &- 0x7fffff;
  180. result = (sign_res << 31) | (e_res << 23) | (m_res);
  181. }
  182. }
  183. return result;
  184. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement