blob: 98299e6f027af992b499de917d869d880f6a9846 [file] [log] [blame]
Daniel Dunbarb3a69012009-06-26 16:47:03 +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
14#include "int_lib.h"
15
16// Returns: a * b
17
18static
19di_int
20__muldsi3(su_int a, su_int b)
21{
22 dwords r;
23 const int bits_in_word_2 = (int)(sizeof(si_int) * CHAR_BIT) / 2;
24 const su_int lower_mask = (su_int)~0 >> bits_in_word_2;
25 r.low = (a & lower_mask) * (b & lower_mask);
26 su_int t = r.low >> bits_in_word_2;
27 r.low &= lower_mask;
28 t += (a >> bits_in_word_2) * (b & lower_mask);
29 r.low += (t & lower_mask) << bits_in_word_2;
30 r.high = t >> bits_in_word_2;
31 t = r.low >> bits_in_word_2;
32 r.low &= lower_mask;
33 t += (b >> bits_in_word_2) * (a & lower_mask);
34 r.low += (t & lower_mask) << bits_in_word_2;
35 r.high += t >> bits_in_word_2;
36 r.high += (a >> bits_in_word_2) * (b >> bits_in_word_2);
37 return r.all;
38}
39
40// Returns: a * b
41
42di_int
43__muldi3(di_int a, di_int b)
44{
45 dwords x;
46 x.all = a;
47 dwords y;
48 y.all = b;
49 dwords r;
50 r.all = __muldsi3(x.low, y.low);
51 r.high += x.high * y.low + x.low * y.high;
52 return r.all;
53}