int_mulv_impl.inc 1.3 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647
  1. //===-- int_mulv_impl.inc - Implement __mulv[sdt]i3 ---------------*- 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 __mulvsi3, __mulvdi3 and __mulvti3.
  10. //
  11. //===----------------------------------------------------------------------===//
  12. #include "int_lib.h"
  13. // Returns: a * b
  14. // Effects: aborts if a * b overflows
  15. static __inline fixint_t __mulvXi3(fixint_t a, fixint_t b) {
  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. if (a == MIN) {
  20. if (b == 0 || b == 1)
  21. return a * b;
  22. compilerrt_abort();
  23. }
  24. if (b == MIN) {
  25. if (a == 0 || a == 1)
  26. return a * b;
  27. compilerrt_abort();
  28. }
  29. fixint_t sa = a >> (N - 1);
  30. fixint_t abs_a = (a ^ sa) - sa;
  31. fixint_t sb = b >> (N - 1);
  32. fixint_t abs_b = (b ^ sb) - sb;
  33. if (abs_a < 2 || abs_b < 2)
  34. return a * b;
  35. if (sa == sb) {
  36. if (abs_a > MAX / abs_b)
  37. compilerrt_abort();
  38. } else {
  39. if (abs_a > MIN / -abs_b)
  40. compilerrt_abort();
  41. }
  42. return a * b;
  43. }