1276789Sdim/* ===-- multi3.c - Implement __multi3 -------------------------------------===
2276789Sdim *
3276789Sdim *                     The LLVM Compiler Infrastructure
4276789Sdim *
5276789Sdim * This file is dual licensed under the MIT and the University of Illinois Open
6276789Sdim * Source Licenses. See LICENSE.TXT for details.
7276789Sdim *
8276789Sdim * ===----------------------------------------------------------------------===
9276789Sdim
10276789Sdim * This file implements __multi3 for the compiler_rt library.
11276789Sdim *
12276789Sdim * ===----------------------------------------------------------------------===
13276789Sdim */
14276789Sdim
15276789Sdim#include "int_lib.h"
16276789Sdim
17276789Sdim#ifdef CRT_HAS_128BIT
18276789Sdim
19276789Sdim/* Returns: a * b */
20276789Sdim
21276789Sdimstatic
22276789Sdimti_int
23276789Sdim__mulddi3(du_int a, du_int b)
24276789Sdim{
25276789Sdim    twords r;
26276789Sdim    const int bits_in_dword_2 = (int)(sizeof(di_int) * CHAR_BIT) / 2;
27276789Sdim    const du_int lower_mask = (du_int)~0 >> bits_in_dword_2;
28276789Sdim    r.s.low = (a & lower_mask) * (b & lower_mask);
29276789Sdim    du_int t = r.s.low >> bits_in_dword_2;
30276789Sdim    r.s.low &= lower_mask;
31276789Sdim    t += (a >> bits_in_dword_2) * (b & lower_mask);
32276789Sdim    r.s.low += (t & lower_mask) << bits_in_dword_2;
33276789Sdim    r.s.high = t >> bits_in_dword_2;
34276789Sdim    t = r.s.low >> bits_in_dword_2;
35276789Sdim    r.s.low &= lower_mask;
36276789Sdim    t += (b >> bits_in_dword_2) * (a & lower_mask);
37276789Sdim    r.s.low += (t & lower_mask) << bits_in_dword_2;
38276789Sdim    r.s.high += t >> bits_in_dword_2;
39276789Sdim    r.s.high += (a >> bits_in_dword_2) * (b >> bits_in_dword_2);
40276789Sdim    return r.all;
41276789Sdim}
42276789Sdim
43276789Sdim/* Returns: a * b */
44276789Sdim
45276789SdimCOMPILER_RT_ABI ti_int
46276789Sdim__multi3(ti_int a, ti_int b)
47276789Sdim{
48276789Sdim    twords x;
49276789Sdim    x.all = a;
50276789Sdim    twords y;
51276789Sdim    y.all = b;
52276789Sdim    twords r;
53276789Sdim    r.all = __mulddi3(x.s.low, y.s.low);
54276789Sdim    r.s.high += x.s.high * y.s.low + x.s.low * y.s.high;
55276789Sdim    return r.all;
56276789Sdim}
57276789Sdim
58276789Sdim#endif /* CRT_HAS_128BIT */
59