muldi3.c revision 222656
1259701Sdim/* ===-- muldi3.c - Implement __muldi3 -------------------------------------===
2259701Sdim *
3259701Sdim *                     The LLVM Compiler Infrastructure
4259701Sdim *
5259701Sdim * This file is dual licensed under the MIT and the University of Illinois Open
6259701Sdim * Source Licenses. See LICENSE.TXT for details.
7259701Sdim *
8259701Sdim * ===----------------------------------------------------------------------===
9259701Sdim *
10259701Sdim * This file implements __muldi3 for the compiler_rt library.
11259701Sdim *
12259701Sdim * ===----------------------------------------------------------------------===
13259701Sdim */
14259701Sdim#include "abi.h"
15259701Sdim
16259701Sdim#include "int_lib.h"
17259701Sdim
18259701Sdim/* Returns: a * b */
19259701Sdim
20259701Sdimstatic
21259701Sdimdi_int
22259701Sdim__muldsi3(su_int a, su_int b)
23259701Sdim{
24259701Sdim    dwords r;
25259701Sdim    const int bits_in_word_2 = (int)(sizeof(si_int) * CHAR_BIT) / 2;
26259701Sdim    const su_int lower_mask = (su_int)~0 >> bits_in_word_2;
27259701Sdim    r.s.low = (a & lower_mask) * (b & lower_mask);
28259701Sdim    su_int t = r.s.low >> bits_in_word_2;
29259701Sdim    r.s.low &= lower_mask;
30259701Sdim    t += (a >> bits_in_word_2) * (b & lower_mask);
31259701Sdim    r.s.low += (t & lower_mask) << bits_in_word_2;
32259701Sdim    r.s.high = t >> bits_in_word_2;
33259701Sdim    t = r.s.low >> bits_in_word_2;
34259701Sdim    r.s.low &= lower_mask;
35259701Sdim    t += (b >> bits_in_word_2) * (a & lower_mask);
36259701Sdim    r.s.low += (t & lower_mask) << bits_in_word_2;
37259701Sdim    r.s.high += t >> bits_in_word_2;
38259701Sdim    r.s.high += (a >> bits_in_word_2) * (b >> bits_in_word_2);
39259701Sdim    return r.all;
40259701Sdim}
41259701Sdim
42259701Sdim/* Returns: a * b */
43259701Sdim
44259701SdimARM_EABI_FNALIAS(lmul, muldi3);
45259701Sdim
46259701SdimCOMPILER_RT_ABI di_int
47259701Sdim__muldi3(di_int a, di_int b)
48259701Sdim{
49259701Sdim    dwords x;
50259701Sdim    x.all = a;
51259701Sdim    dwords y;
52259701Sdim    y.all = b;
53259701Sdim    dwords r;
54259701Sdim    r.all = __muldsi3(x.s.low, y.s.low);
55259701Sdim    r.s.high += x.s.high * y.s.low + x.s.low * y.s.high;
56259701Sdim    return r.all;
57259701Sdim}
58259701Sdim