multi3.c 1.6 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758
  1. /* ===-- multi3.c - Implement __multi3 -------------------------------------===
  2. *
  3. * The LLVM Compiler Infrastructure
  4. *
  5. * This file is dual licensed under the MIT and the University of Illinois Open
  6. * Source Licenses. See LICENSE.TXT for details.
  7. *
  8. * ===----------------------------------------------------------------------===
  9. * This file implements __multi3 for the compiler_rt library.
  10. *
  11. * ===----------------------------------------------------------------------===
  12. */
  13. #include "int_lib.h"
  14. #ifdef CRT_HAS_128BIT
  15. /* Returns: a * b */
  16. static
  17. ti_int
  18. __mulddi3(du_int a, du_int b)
  19. {
  20. twords r;
  21. const int bits_in_dword_2 = (int)(sizeof(di_int) * CHAR_BIT) / 2;
  22. const du_int lower_mask = (du_int)~0 >> bits_in_dword_2;
  23. r.s.low = (a & lower_mask) * (b & lower_mask);
  24. du_int t = r.s.low >> bits_in_dword_2;
  25. r.s.low &= lower_mask;
  26. t += (a >> bits_in_dword_2) * (b & lower_mask);
  27. r.s.low += (t & lower_mask) << bits_in_dword_2;
  28. r.s.high = t >> bits_in_dword_2;
  29. t = r.s.low >> bits_in_dword_2;
  30. r.s.low &= lower_mask;
  31. t += (b >> bits_in_dword_2) * (a & lower_mask);
  32. r.s.low += (t & lower_mask) << bits_in_dword_2;
  33. r.s.high += t >> bits_in_dword_2;
  34. r.s.high += (a >> bits_in_dword_2) * (b >> bits_in_dword_2);
  35. return r.all;
  36. }
  37. /* Returns: a * b */
  38. COMPILER_RT_ABI ti_int
  39. __multi3(ti_int a, ti_int b)
  40. {
  41. twords x;
  42. x.all = a;
  43. twords y;
  44. y.all = b;
  45. twords r;
  46. r.all = __mulddi3(x.s.low, y.s.low);
  47. r.s.high += x.s.high * y.s.low + x.s.low * y.s.high;
  48. return r.all;
  49. }
  50. #endif /* CRT_HAS_128BIT */