int_mulo_impl.inc 1.4 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849
  1. //===-- int_mulo_impl.inc - Implement __mulo[sdt]i4 ---------------*- C -*-===//
  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. // Helper used by __mulosi4, __mulodi4 and __muloti4.
  10. //
  11. //===----------------------------------------------------------------------===//
  12. #include "int_lib.h"
  13. // Returns: a * b
  14. // Effects: sets *overflow to 1 if a * b overflows
  15. static __inline fixint_t __muloXi4(fixint_t a, fixint_t b, int *overflow) {
  16. const int N = (int)(sizeof(fixint_t) * CHAR_BIT);
  17. const fixint_t MIN = (fixint_t)((fixuint_t)1 << (N - 1));
  18. const fixint_t MAX = ~MIN;
  19. *overflow = 0;
  20. fixint_t result = (fixuint_t)a * b;
  21. if (a == MIN) {
  22. if (b != 0 && b != 1)
  23. *overflow = 1;
  24. return result;
  25. }
  26. if (b == MIN) {
  27. if (a != 0 && a != 1)
  28. *overflow = 1;
  29. return result;
  30. }
  31. fixint_t sa = a >> (N - 1);
  32. fixint_t abs_a = (a ^ sa) - sa;
  33. fixint_t sb = b >> (N - 1);
  34. fixint_t abs_b = (b ^ sb) - sb;
  35. if (abs_a < 2 || abs_b < 2)
  36. return result;
  37. if (sa == sb) {
  38. if (abs_a > MAX / abs_b)
  39. *overflow = 1;
  40. } else {
  41. if (abs_a > MIN / -abs_b)
  42. *overflow = 1;
  43. }
  44. return result;
  45. }