blob: 38963b72630d8ff78d4e8d467cfd04e10296d595 [file] [log] [blame]
Edward O'Callaghan37a6a452009-08-07 20:30:09 +00001/* ===-- muldi3.c - Implement __muldi3 -------------------------------------===
2 *
3 * The LLVM Compiler Infrastructure
4 *
5 * This file is distributed under the University of Illinois Open Source
6 * License. See LICENSE.TXT for details.
7 *
8 * ===----------------------------------------------------------------------===
9 *
10 * This file implements __muldi3 for the compiler_rt library.
11 *
12 * ===----------------------------------------------------------------------===
13 */
Daniel Dunbarb3a69012009-06-26 16:47:03 +000014
15#include "int_lib.h"
16
Edward O'Callaghan37a6a452009-08-07 20:30:09 +000017/* Returns: a * b */
Daniel Dunbarb3a69012009-06-26 16:47:03 +000018
19static
20di_int
21__muldsi3(su_int a, su_int b)
22{
23 dwords r;
24 const int bits_in_word_2 = (int)(sizeof(si_int) * CHAR_BIT) / 2;
25 const su_int lower_mask = (su_int)~0 >> bits_in_word_2;
Edward O'Callaghan8bf1e092009-08-09 18:41:02 +000026 r.s.low = (a & lower_mask) * (b & lower_mask);
27 su_int t = r.s.low >> bits_in_word_2;
28 r.s.low &= lower_mask;
Daniel Dunbarb3a69012009-06-26 16:47:03 +000029 t += (a >> bits_in_word_2) * (b & lower_mask);
Edward O'Callaghan8bf1e092009-08-09 18:41:02 +000030 r.s.low += (t & lower_mask) << bits_in_word_2;
31 r.s.high = t >> bits_in_word_2;
32 t = r.s.low >> bits_in_word_2;
33 r.s.low &= lower_mask;
Daniel Dunbarb3a69012009-06-26 16:47:03 +000034 t += (b >> bits_in_word_2) * (a & lower_mask);
Edward O'Callaghan8bf1e092009-08-09 18:41:02 +000035 r.s.low += (t & lower_mask) << bits_in_word_2;
36 r.s.high += t >> bits_in_word_2;
37 r.s.high += (a >> bits_in_word_2) * (b >> bits_in_word_2);
Daniel Dunbarb3a69012009-06-26 16:47:03 +000038 return r.all;
39}
40
Edward O'Callaghan37a6a452009-08-07 20:30:09 +000041/* Returns: a * b */
Daniel Dunbarb3a69012009-06-26 16:47:03 +000042
43di_int
44__muldi3(di_int a, di_int b)
45{
46 dwords x;
47 x.all = a;
48 dwords y;
49 y.all = b;
50 dwords r;
Edward O'Callaghan8bf1e092009-08-09 18:41:02 +000051 r.all = __muldsi3(x.s.low, y.s.low);
52 r.s.high += x.s.high * y.s.low + x.s.low * y.s.high;
Daniel Dunbarb3a69012009-06-26 16:47:03 +000053 return r.all;
54}