multi3.c 1.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051
  1. //===-- multi3.c - Implement __multi3 -------------------------------------===//
  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 __multi3 for the compiler_rt library.
  10. //
  11. //===----------------------------------------------------------------------===//
  12. #include "int_lib.h"
  13. #ifdef CRT_HAS_128BIT
  14. // Returns: a * b
  15. static ti_int __mulddi3(du_int a, du_int b) {
  16. twords r;
  17. const int bits_in_dword_2 = (int)(sizeof(di_int) * CHAR_BIT) / 2;
  18. const du_int lower_mask = (du_int)~0 >> bits_in_dword_2;
  19. r.s.low = (a & lower_mask) * (b & lower_mask);
  20. du_int t = r.s.low >> bits_in_dword_2;
  21. r.s.low &= lower_mask;
  22. t += (a >> bits_in_dword_2) * (b & lower_mask);
  23. r.s.low += (t & lower_mask) << bits_in_dword_2;
  24. r.s.high = t >> bits_in_dword_2;
  25. t = r.s.low >> bits_in_dword_2;
  26. r.s.low &= lower_mask;
  27. t += (b >> bits_in_dword_2) * (a & lower_mask);
  28. r.s.low += (t & lower_mask) << bits_in_dword_2;
  29. r.s.high += t >> bits_in_dword_2;
  30. r.s.high += (a >> bits_in_dword_2) * (b >> bits_in_dword_2);
  31. return r.all;
  32. }
  33. // Returns: a * b
  34. COMPILER_RT_ABI ti_int __multi3(ti_int a, ti_int b) {
  35. twords x;
  36. x.all = a;
  37. twords y;
  38. y.all = b;
  39. twords r;
  40. r.all = __mulddi3(x.s.low, y.s.low);
  41. r.s.high += x.s.high * y.s.low + x.s.low * y.s.high;
  42. return r.all;
  43. }
  44. #endif // CRT_HAS_128BIT