1214152Sed/* ===-- multi3.c - Implement __multi3 -------------------------------------===
2214152Sed *
3214152Sed *                     The LLVM Compiler Infrastructure
4214152Sed *
5222656Sed * This file is dual licensed under the MIT and the University of Illinois Open
6222656Sed * Source Licenses. See LICENSE.TXT for details.
7214152Sed *
8214152Sed * ===----------------------------------------------------------------------===
9214152Sed
10214152Sed * This file implements __multi3 for the compiler_rt library.
11214152Sed *
12214152Sed * ===----------------------------------------------------------------------===
13214152Sed */
14214152Sed
15263560Sdim#include "int_lib.h"
16263560Sdim
17263764Sdim#ifdef CRT_HAS_128BIT
18214152Sed
19214152Sed/* Returns: a * b */
20214152Sed
21214152Sedstatic
22214152Sedti_int
23214152Sed__mulddi3(du_int a, du_int b)
24214152Sed{
25214152Sed    twords r;
26214152Sed    const int bits_in_dword_2 = (int)(sizeof(di_int) * CHAR_BIT) / 2;
27214152Sed    const du_int lower_mask = (du_int)~0 >> bits_in_dword_2;
28214152Sed    r.s.low = (a & lower_mask) * (b & lower_mask);
29214152Sed    du_int t = r.s.low >> bits_in_dword_2;
30214152Sed    r.s.low &= lower_mask;
31214152Sed    t += (a >> bits_in_dword_2) * (b & lower_mask);
32214152Sed    r.s.low += (t & lower_mask) << bits_in_dword_2;
33214152Sed    r.s.high = t >> bits_in_dword_2;
34214152Sed    t = r.s.low >> bits_in_dword_2;
35214152Sed    r.s.low &= lower_mask;
36214152Sed    t += (b >> bits_in_dword_2) * (a & lower_mask);
37214152Sed    r.s.low += (t & lower_mask) << bits_in_dword_2;
38214152Sed    r.s.high += t >> bits_in_dword_2;
39214152Sed    r.s.high += (a >> bits_in_dword_2) * (b >> bits_in_dword_2);
40214152Sed    return r.all;
41214152Sed}
42214152Sed
43214152Sed/* Returns: a * b */
44214152Sed
45214152Sedti_int
46214152Sed__multi3(ti_int a, ti_int b)
47214152Sed{
48214152Sed    twords x;
49214152Sed    x.all = a;
50214152Sed    twords y;
51214152Sed    y.all = b;
52214152Sed    twords r;
53214152Sed    r.all = __mulddi3(x.s.low, y.s.low);
54214152Sed    r.s.high += x.s.high * y.s.low + x.s.low * y.s.high;
55214152Sed    return r.all;
56214152Sed}
57214152Sed
58263764Sdim#endif /* CRT_HAS_128BIT */
59