add.rs 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326
  1. use core::num::Wrapping;
  2. use float::Float;
  3. macro_rules! add {
  4. ($intrinsic:ident: $ty:ty) => {
  5. /// Returns `a + b`
  6. #[allow(unused_parens)]
  7. #[cfg_attr(not(test), no_mangle)]
  8. pub extern fn $intrinsic(a: $ty, b: $ty) -> $ty {
  9. let one = Wrapping(1 as <$ty as Float>::Int);
  10. let zero = Wrapping(0 as <$ty as Float>::Int);
  11. let bits = Wrapping(<$ty>::bits() as <$ty as Float>::Int);
  12. let significand_bits = Wrapping(<$ty>::significand_bits() as <$ty as Float>::Int);
  13. let exponent_bits = bits - significand_bits - one;
  14. let max_exponent = (one << exponent_bits.0 as usize) - one;
  15. let implicit_bit = one << significand_bits.0 as usize;
  16. let significand_mask = implicit_bit - one;
  17. let sign_bit = one << (significand_bits + exponent_bits).0 as usize;
  18. let abs_mask = sign_bit - one;
  19. let exponent_mask = abs_mask ^ significand_mask;
  20. let inf_rep = exponent_mask;
  21. let quiet_bit = implicit_bit >> 1;
  22. let qnan_rep = exponent_mask | quiet_bit;
  23. let mut a_rep = Wrapping(a.repr());
  24. let mut b_rep = Wrapping(b.repr());
  25. let a_abs = a_rep & abs_mask;
  26. let b_abs = b_rep & abs_mask;
  27. // Detect if a or b is zero, infinity, or NaN.
  28. if a_abs - one >= inf_rep - one ||
  29. b_abs - one >= inf_rep - one {
  30. // NaN + anything = qNaN
  31. if a_abs > inf_rep {
  32. return (<$ty as Float>::from_repr((a_abs | quiet_bit).0));
  33. }
  34. // anything + NaN = qNaN
  35. if b_abs > inf_rep {
  36. return (<$ty as Float>::from_repr((b_abs | quiet_bit).0));
  37. }
  38. if a_abs == inf_rep {
  39. // +/-infinity + -/+infinity = qNaN
  40. if (a.repr() ^ b.repr()) == sign_bit.0 {
  41. return (<$ty as Float>::from_repr(qnan_rep.0));
  42. } else {
  43. // +/-infinity + anything remaining = +/- infinity
  44. return a;
  45. }
  46. }
  47. // anything remaining + +/-infinity = +/-infinity
  48. if b_abs == inf_rep {
  49. return b;
  50. }
  51. // zero + anything = anything
  52. if a_abs.0 == 0 {
  53. // but we need to get the sign right for zero + zero
  54. if b_abs.0 == 0 {
  55. return (<$ty as Float>::from_repr(a.repr() & b.repr()));
  56. } else {
  57. return b;
  58. }
  59. }
  60. // anything + zero = anything
  61. if b_abs.0 == 0 {
  62. return a;
  63. }
  64. }
  65. // Swap a and b if necessary so that a has the larger absolute value.
  66. if b_abs > a_abs {
  67. let temp = a_rep;
  68. a_rep = b_rep;
  69. b_rep = temp;
  70. }
  71. // Extract the exponent and significand from the (possibly swapped) a and b.
  72. let mut a_exponent = Wrapping((a_rep >> significand_bits.0 as usize & max_exponent).0 as i32);
  73. let mut b_exponent = Wrapping((b_rep >> significand_bits.0 as usize & max_exponent).0 as i32);
  74. let mut a_significand = a_rep & significand_mask;
  75. let mut b_significand = b_rep & significand_mask;
  76. // normalize any denormals, and adjust the exponent accordingly.
  77. if a_exponent.0 == 0 {
  78. let (exponent, significand) = <$ty>::normalize(a_significand.0);
  79. a_exponent = Wrapping(exponent);
  80. a_significand = Wrapping(significand);
  81. }
  82. if b_exponent.0 == 0 {
  83. let (exponent, significand) = <$ty>::normalize(b_significand.0);
  84. b_exponent = Wrapping(exponent);
  85. b_significand = Wrapping(significand);
  86. }
  87. // The sign of the result is the sign of the larger operand, a. If they
  88. // have opposite signs, we are performing a subtraction; otherwise addition.
  89. let result_sign = a_rep & sign_bit;
  90. let subtraction = ((a_rep ^ b_rep) & sign_bit) != zero;
  91. // Shift the significands to give us round, guard and sticky, and or in the
  92. // implicit significand bit. (If we fell through from the denormal path it
  93. // was already set by normalize(), but setting it twice won't hurt
  94. // anything.)
  95. a_significand = (a_significand | implicit_bit) << 3;
  96. b_significand = (b_significand | implicit_bit) << 3;
  97. // Shift the significand of b by the difference in exponents, with a sticky
  98. // bottom bit to get rounding correct.
  99. let align = Wrapping((a_exponent - b_exponent).0 as <$ty as Float>::Int);
  100. if align.0 != 0 {
  101. if align < bits {
  102. let sticky = ((b_significand << (bits - align).0 as usize).0 != 0) as <$ty as Float>::Int;
  103. b_significand = (b_significand >> align.0 as usize) | Wrapping(sticky);
  104. } else {
  105. b_significand = one; // sticky; b is known to be non-zero.
  106. }
  107. }
  108. if subtraction {
  109. a_significand -= b_significand;
  110. // If a == -b, return +zero.
  111. if a_significand.0 == 0 {
  112. return (<$ty as Float>::from_repr(0));
  113. }
  114. // If partial cancellation occured, we need to left-shift the result
  115. // and adjust the exponent:
  116. if a_significand < implicit_bit << 3 {
  117. let shift = a_significand.0.leading_zeros() as i32
  118. - (implicit_bit << 3).0.leading_zeros() as i32;
  119. a_significand <<= shift as usize;
  120. a_exponent -= Wrapping(shift);
  121. }
  122. } else /* addition */ {
  123. a_significand += b_significand;
  124. // If the addition carried up, we need to right-shift the result and
  125. // adjust the exponent:
  126. if (a_significand & implicit_bit << 4).0 != 0 {
  127. let sticky = ((a_significand & one).0 != 0) as <$ty as Float>::Int;
  128. a_significand = a_significand >> 1 | Wrapping(sticky);
  129. a_exponent += Wrapping(1);
  130. }
  131. }
  132. // If we have overflowed the type, return +/- infinity:
  133. if a_exponent >= Wrapping(max_exponent.0 as i32) {
  134. return (<$ty>::from_repr((inf_rep | result_sign).0));
  135. }
  136. if a_exponent.0 <= 0 {
  137. // Result is denormal before rounding; the exponent is zero and we
  138. // need to shift the significand.
  139. let shift = Wrapping((Wrapping(1) - a_exponent).0 as <$ty as Float>::Int);
  140. let sticky = ((a_significand << (bits - shift).0 as usize).0 != 0) as <$ty as Float>::Int;
  141. a_significand = a_significand >> shift.0 as usize | Wrapping(sticky);
  142. a_exponent = Wrapping(0);
  143. }
  144. // Low three bits are round, guard, and sticky.
  145. let round_guard_sticky: i32 = (a_significand.0 & 0x7) as i32;
  146. // Shift the significand into place, and mask off the implicit bit.
  147. let mut result = a_significand >> 3 & significand_mask;
  148. // Insert the exponent and sign.
  149. result |= Wrapping(a_exponent.0 as <$ty as Float>::Int) << significand_bits.0 as usize;
  150. result |= result_sign;
  151. // Final rounding. The result may overflow to infinity, but that is the
  152. // correct result in that case.
  153. if round_guard_sticky > 0x4 { result += one; }
  154. if round_guard_sticky == 0x4 { result += result & one; }
  155. return (<$ty>::from_repr(result.0));
  156. }
  157. }
  158. }
  159. add!(__addsf3: f32);
  160. add!(__adddf3: f64);
  161. // FIXME: Implement these using aliases
  162. #[cfg(target_arch = "arm")]
  163. #[cfg_attr(not(test), no_mangle)]
  164. pub extern fn __aeabi_dadd(a: f64, b: f64) -> f64 {
  165. __adddf3(a, b)
  166. }
  167. #[cfg(target_arch = "arm")]
  168. #[cfg_attr(not(test), no_mangle)]
  169. pub extern fn __aeabi_fadd(a: f32, b: f32) -> f32 {
  170. __addsf3(a, b)
  171. }
  172. #[cfg(test)]
  173. mod tests {
  174. use core::{f32, f64};
  175. use qc::{U32, U64};
  176. use float::Float;
  177. // NOTE The tests below have special handing for NaN values.
  178. // Because NaN != NaN, the floating-point representations must be used
  179. // Because there are many diffferent values of NaN, and the implementation
  180. // doesn't care about calculating the 'correct' one, if both values are NaN
  181. // the values are considered equivalent.
  182. // TODO: Add F32/F64 to qc so that they print the right values (at the very least)
  183. quickcheck! {
  184. fn addsf3(a: U32, b: U32) -> bool {
  185. let (a, b) = (f32::from_repr(a.0), f32::from_repr(b.0));
  186. let x = super::__addsf3(a, b);
  187. let y = a + b;
  188. if !(x.is_nan() && y.is_nan()) {
  189. x.repr() == y.repr()
  190. } else {
  191. true
  192. }
  193. }
  194. fn adddf3(a: U64, b: U64) -> bool {
  195. let (a, b) = (f64::from_repr(a.0), f64::from_repr(b.0));
  196. let x = super::__adddf3(a, b);
  197. let y = a + b;
  198. if !(x.is_nan() && y.is_nan()) {
  199. x.repr() == y.repr()
  200. } else {
  201. true
  202. }
  203. }
  204. }
  205. // More tests for special float values
  206. #[test]
  207. fn test_float_tiny_plus_tiny() {
  208. let tiny = f32::from_repr(1);
  209. let r = super::__addsf3(tiny, tiny);
  210. assert_eq!(r, tiny + tiny);
  211. }
  212. #[test]
  213. fn test_double_tiny_plus_tiny() {
  214. let tiny = f64::from_repr(1);
  215. let r = super::__adddf3(tiny, tiny);
  216. assert_eq!(r, tiny + tiny);
  217. }
  218. #[test]
  219. fn test_float_small_plus_small() {
  220. let a = f32::from_repr(327);
  221. let b = f32::from_repr(256);
  222. let r = super::__addsf3(a, b);
  223. assert_eq!(r, a + b);
  224. }
  225. #[test]
  226. fn test_double_small_plus_small() {
  227. let a = f64::from_repr(327);
  228. let b = f64::from_repr(256);
  229. let r = super::__adddf3(a, b);
  230. assert_eq!(r, a + b);
  231. }
  232. #[test]
  233. fn test_float_one_plus_one() {
  234. let r = super::__addsf3(1f32, 1f32);
  235. assert_eq!(r, 1f32 + 1f32);
  236. }
  237. #[test]
  238. fn test_double_one_plus_one() {
  239. let r = super::__adddf3(1f64, 1f64);
  240. assert_eq!(r, 1f64 + 1f64);
  241. }
  242. #[test]
  243. fn test_float_different_nan() {
  244. let a = f32::from_repr(1);
  245. let b = f32::from_repr(0b11111111100100010001001010101010);
  246. let x = super::__addsf3(a, b);
  247. let y = a + b;
  248. if !(x.is_nan() && y.is_nan()) {
  249. assert_eq!(x.repr(), y.repr());
  250. }
  251. }
  252. #[test]
  253. fn test_double_different_nan() {
  254. let a = f64::from_repr(1);
  255. let b = f64::from_repr(
  256. 0b1111111111110010001000100101010101001000101010000110100011101011);
  257. let x = super::__adddf3(a, b);
  258. let y = a + b;
  259. if !(x.is_nan() && y.is_nan()) {
  260. assert_eq!(x.repr(), y.repr());
  261. }
  262. }
  263. #[test]
  264. fn test_float_nan() {
  265. let r = super::__addsf3(f32::NAN, 1.23);
  266. assert_eq!(r.repr(), f32::NAN.repr());
  267. }
  268. #[test]
  269. fn test_double_nan() {
  270. let r = super::__adddf3(f64::NAN, 1.23);
  271. assert_eq!(r.repr(), f64::NAN.repr());
  272. }
  273. #[test]
  274. fn test_float_inf() {
  275. let r = super::__addsf3(f32::INFINITY, -123.4);
  276. assert_eq!(r, f32::INFINITY);
  277. }
  278. #[test]
  279. fn test_double_inf() {
  280. let r = super::__adddf3(f64::INFINITY, -123.4);
  281. assert_eq!(r, f64::INFINITY);
  282. }
  283. }