muldi3.c 1.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051
  1. //===-- muldi3.c - Implement __muldi3 -------------------------------------===//
  2. //
  3. // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
  4. // See https://llvm.org/LICENSE.txt for license information.
  5. // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
  6. //
  7. //===----------------------------------------------------------------------===//
  8. //
  9. // This file implements __muldi3 for the compiler_rt library.
  10. //
  11. //===----------------------------------------------------------------------===//
  12. #include "int_lib.h"
  13. // Returns: a * b
  14. static di_int __muldsi3(su_int a, su_int b) {
  15. dwords r;
  16. const int bits_in_word_2 = (int)(sizeof(si_int) * CHAR_BIT) / 2;
  17. const su_int lower_mask = (su_int)~0 >> bits_in_word_2;
  18. r.s.low = (a & lower_mask) * (b & lower_mask);
  19. su_int t = r.s.low >> bits_in_word_2;
  20. r.s.low &= lower_mask;
  21. t += (a >> bits_in_word_2) * (b & lower_mask);
  22. r.s.low += (t & lower_mask) << bits_in_word_2;
  23. r.s.high = t >> bits_in_word_2;
  24. t = r.s.low >> bits_in_word_2;
  25. r.s.low &= lower_mask;
  26. t += (b >> bits_in_word_2) * (a & lower_mask);
  27. r.s.low += (t & lower_mask) << bits_in_word_2;
  28. r.s.high += t >> bits_in_word_2;
  29. r.s.high += (a >> bits_in_word_2) * (b >> bits_in_word_2);
  30. return r.all;
  31. }
  32. // Returns: a * b
  33. COMPILER_RT_ABI di_int __muldi3(di_int a, di_int b) {
  34. dwords x;
  35. x.all = a;
  36. dwords y;
  37. y.all = b;
  38. dwords r;
  39. r.all = __muldsi3(x.s.low, y.s.low);
  40. r.s.high += x.s.high * y.s.low + x.s.low * y.s.high;
  41. return r.all;
  42. }
  43. #if defined(__ARM_EABI__)
  44. COMPILER_RT_ALIAS(__muldi3, __aeabi_lmul)
  45. #endif