Advertisement
Guest User

Untitled

a guest
Feb 18th, 2020
78
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.00 KB | None | 0 0
  1. unsigned add_floats(unsigned f1, unsigned f2) {
  2. unsigned 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.  
  15. unsigned e1 = (f1 & 0x7f800000) >> 23; //Extract the exponnent bits
  16. unsigned e2 = (f2 & 0x7f800000) >> 23;
  17.  
  18. unsigned long long m1 = (f1 & 0x7fffff) | (1 << 23); //Extract the mantissa bits, add an implicit 1 so it becomes (1 + m1)
  19. unsigned long long m2 = (f2 & 0x7fffff) | (1 << 23);
  20.  
  21. unsigned f1_sign = (f1 & 0x80000000) >> 31;//0 if positive, 1 if negative
  22. unsigned f2_sign = (f2 & 0x80000000) >> 31;
  23.  
  24. unsigned result = 0x0;
  25.  
  26. // //This is to take care of the NaN case
  27. // if (!(exponent(f1) ^ 0xff) && mantissa(f1)) {
  28. // return f1;
  29. // }
  30. // if (!(exponent(f2) ^ 0xff) && mantissa(f2)) {
  31. // return f2;
  32. // }
  33.  
  34. // //This is to catch the infinity case
  35. // if (!(exponent(f1) ^ 0xff) && !(exponent(f2) ^ 0xff)) {
  36. // if (diff_sign) {
  37. // return 0x7f800000 + 1;//NaN, Infinity - Infinity
  38. // } else {
  39. // return f1; //Both are either Infinity, or -Infinity, return Infinity
  40. // }
  41. // }
  42.  
  43.  
  44. //Exponent could be negative my guy
  45. unsigned exp_diff; //Sign difference is also correct
  46. //unsigned f1_bigger_abs;
  47.  
  48. // if (f1_abs > f2_abs) {
  49. // f1_bigger_abs = 1;
  50. // } else {
  51. // f1_bigger_abs = 0;
  52. // }
  53.  
  54. //if (e1 > e2) {//Exponent portion is correct, the exponent is NOT dependent on the sign difference
  55. e_res = e1;
  56. exp_diff = e1 - e2;
  57. e1 -= exp_diff;
  58. m1 <<= exp_diff;
  59. // } else {//Exponent smaller
  60. // e_res = e2;
  61. // exp_diff = e2 - e1;
  62. // e2 -= exp_diff;
  63. // m2 <<= exp_diff;
  64. // }
  65.  
  66. // if (f1_bigger_abs) { //if input 1 is bigger than input 2 disregarding sign for now
  67.  
  68.  
  69. if (((f1_sign == 0) && (f2_sign == 0)) || ((f1_sign == 1) && (f2_sign == 1))) { //both negative or positive
  70. //The case that f1 is bigger than f2, and that either both are positive or both are negative
  71. sign_res = f1_sign;
  72.  
  73. m_res = m1 + m2;
  74.  
  75. if (e2 != (e1 + exp_diff)) {
  76. m_res >>= exp_diff - 1;
  77. }
  78.  
  79. if (m_res & 0x1 == 1) {//For rounding purposes
  80. m_res++;
  81. }
  82.  
  83. if (e2 != (e1 + exp_diff)) {
  84. m_res >>= 1;
  85. }
  86.  
  87. if ((m_res & (1 << 24)) == 0x1000000) {//If there is a carry
  88. m_res >>= 1;
  89. e_res++;
  90. }
  91.  
  92. m_res &= 0x7fffff;//Get rid of the implied bit
  93.  
  94. result = (sign_res << 31) | (e_res << 23) | (m_res);
  95. }
  96.  
  97. // } else {//if input 1 is smaller than input 2 disregarding sign
  98.  
  99. // }
  100.  
  101. return result;
  102. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement