muldi3.c 1.5 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556
  1. /* ===-- muldi3.c - Implement __muldi3 -------------------------------------===
  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. *
  10. * This file implements __muldi3 for the compiler_rt library.
  11. *
  12. * ===----------------------------------------------------------------------===
  13. */
  14. #include "int_lib.h"
  15. /* Returns: a * b */
  16. static
  17. di_int
  18. __muldsi3(su_int a, su_int b)
  19. {
  20. dwords r;
  21. const int bits_in_word_2 = (int)(sizeof(si_int) * CHAR_BIT) / 2;
  22. const su_int lower_mask = (su_int)~0 >> bits_in_word_2;
  23. r.s.low = (a & lower_mask) * (b & lower_mask);
  24. su_int t = r.s.low >> bits_in_word_2;
  25. r.s.low &= lower_mask;
  26. t += (a >> bits_in_word_2) * (b & lower_mask);
  27. r.s.low += (t & lower_mask) << bits_in_word_2;
  28. r.s.high = t >> bits_in_word_2;
  29. t = r.s.low >> bits_in_word_2;
  30. r.s.low &= lower_mask;
  31. t += (b >> bits_in_word_2) * (a & lower_mask);
  32. r.s.low += (t & lower_mask) << bits_in_word_2;
  33. r.s.high += t >> bits_in_word_2;
  34. r.s.high += (a >> bits_in_word_2) * (b >> bits_in_word_2);
  35. return r.all;
  36. }
  37. /* Returns: a * b */
  38. ARM_EABI_FNALIAS(lmul, muldi3)
  39. COMPILER_RT_ABI di_int
  40. __muldi3(di_int a, di_int b)
  41. {
  42. dwords x;
  43. x.all = a;
  44. dwords y;
  45. y.all = b;
  46. dwords r;
  47. r.all = __muldsi3(x.s.low, y.s.low);
  48. r.s.high += x.s.high * y.s.low + x.s.low * y.s.high;
  49. return r.all;
  50. }