1353358Sdim//===-- muldi3.c - Implement __muldi3 -------------------------------------===//
2353358Sdim//
3353358Sdim// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4353358Sdim// See https://llvm.org/LICENSE.txt for license information.
5353358Sdim// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6353358Sdim//
7353358Sdim//===----------------------------------------------------------------------===//
8353358Sdim//
9353358Sdim// This file implements __muldi3 for the compiler_rt library.
10353358Sdim//
11353358Sdim//===----------------------------------------------------------------------===//
12276789Sdim
13276789Sdim#include "int_lib.h"
14276789Sdim
15353358Sdim// Returns: a * b
16276789Sdim
17353358Sdimstatic di_int __muldsi3(su_int a, su_int b) {
18353358Sdim  dwords r;
19353358Sdim  const int bits_in_word_2 = (int)(sizeof(si_int) * CHAR_BIT) / 2;
20353358Sdim  const su_int lower_mask = (su_int)~0 >> bits_in_word_2;
21353358Sdim  r.s.low = (a & lower_mask) * (b & lower_mask);
22353358Sdim  su_int t = r.s.low >> bits_in_word_2;
23353358Sdim  r.s.low &= lower_mask;
24353358Sdim  t += (a >> bits_in_word_2) * (b & lower_mask);
25353358Sdim  r.s.low += (t & lower_mask) << bits_in_word_2;
26353358Sdim  r.s.high = t >> bits_in_word_2;
27353358Sdim  t = r.s.low >> bits_in_word_2;
28353358Sdim  r.s.low &= lower_mask;
29353358Sdim  t += (b >> bits_in_word_2) * (a & lower_mask);
30353358Sdim  r.s.low += (t & lower_mask) << bits_in_word_2;
31353358Sdim  r.s.high += t >> bits_in_word_2;
32353358Sdim  r.s.high += (a >> bits_in_word_2) * (b >> bits_in_word_2);
33353358Sdim  return r.all;
34276789Sdim}
35276789Sdim
36353358Sdim// Returns: a * b
37276789Sdim
38353358SdimCOMPILER_RT_ABI di_int __muldi3(di_int a, di_int b) {
39353358Sdim  dwords x;
40353358Sdim  x.all = a;
41353358Sdim  dwords y;
42353358Sdim  y.all = b;
43353358Sdim  dwords r;
44353358Sdim  r.all = __muldsi3(x.s.low, y.s.low);
45353358Sdim  r.s.high += x.s.high * y.s.low + x.s.low * y.s.high;
46353358Sdim  return r.all;
47276789Sdim}
48321369Sdim
49321369Sdim#if defined(__ARM_EABI__)
50353358SdimCOMPILER_RT_ALIAS(__muldi3, __aeabi_lmul)
51321369Sdim#endif
52