blob: 0b8730f08cec0fcfe567ab22ff32dbe6f8216c8d [file] [log] [blame]
Edward O'Callaghan37a6a452009-08-07 20:30:09 +00001/* ===-- multi3.c - Implement __multi3 -------------------------------------===
2 *
3 * The LLVM Compiler Infrastructure
4 *
Howard Hinnant9ad441f2010-11-16 22:13:33 +00005 * This file is dual licensed under the MIT and the University of Illinois Open
6 * Source Licenses. See LICENSE.TXT for details.
Edward O'Callaghan37a6a452009-08-07 20:30:09 +00007 *
8 * ===----------------------------------------------------------------------===
9
10 * This file implements __multi3 for the compiler_rt library.
11 *
12 * ===----------------------------------------------------------------------===
13 */
Daniel Dunbarb3a69012009-06-26 16:47:03 +000014
Daniel Dunbarb3a69012009-06-26 16:47:03 +000015#include "int_lib.h"
16
Chandler Carruth7f2d7c72012-06-22 21:09:22 +000017#if __x86_64
18
Edward O'Callaghan37a6a452009-08-07 20:30:09 +000019/* Returns: a * b */
Daniel Dunbarb3a69012009-06-26 16:47:03 +000020
21static
22ti_int
23__mulddi3(du_int a, du_int b)
24{
25 twords r;
26 const int bits_in_dword_2 = (int)(sizeof(di_int) * CHAR_BIT) / 2;
27 const du_int lower_mask = (du_int)~0 >> bits_in_dword_2;
Edward O'Callaghan8bf1e092009-08-09 18:41:02 +000028 r.s.low = (a & lower_mask) * (b & lower_mask);
29 du_int t = r.s.low >> bits_in_dword_2;
30 r.s.low &= lower_mask;
Daniel Dunbarb3a69012009-06-26 16:47:03 +000031 t += (a >> bits_in_dword_2) * (b & lower_mask);
Edward O'Callaghan8bf1e092009-08-09 18:41:02 +000032 r.s.low += (t & lower_mask) << bits_in_dword_2;
33 r.s.high = t >> bits_in_dword_2;
34 t = r.s.low >> bits_in_dword_2;
35 r.s.low &= lower_mask;
Daniel Dunbarb3a69012009-06-26 16:47:03 +000036 t += (b >> bits_in_dword_2) * (a & lower_mask);
Edward O'Callaghan8bf1e092009-08-09 18:41:02 +000037 r.s.low += (t & lower_mask) << bits_in_dword_2;
38 r.s.high += t >> bits_in_dword_2;
39 r.s.high += (a >> bits_in_dword_2) * (b >> bits_in_dword_2);
Daniel Dunbarb3a69012009-06-26 16:47:03 +000040 return r.all;
41}
42
Edward O'Callaghan37a6a452009-08-07 20:30:09 +000043/* Returns: a * b */
Daniel Dunbarb3a69012009-06-26 16:47:03 +000044
45ti_int
46__multi3(ti_int a, ti_int b)
47{
48 twords x;
49 x.all = a;
50 twords y;
51 y.all = b;
52 twords r;
Edward O'Callaghanaabd9612009-09-03 09:12:20 +000053 r.all = __mulddi3(x.s.low, y.s.low);
54 r.s.high += x.s.high * y.s.low + x.s.low * y.s.high;
Daniel Dunbarb3a69012009-06-26 16:47:03 +000055 return r.all;
56}
57
Edward O'Callaghan8bf1e092009-08-09 18:41:02 +000058#endif /* __x86_64 */