blob: b2dd80f7e3fd5193a416bde6cd1ca7e088d6066d [file] [log] [blame]
Shih-wei Liao77ed6142010-04-07 12:21:42 -07001/* ===-- 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
Bernhard Rosenkränzer3d4d14c2012-12-15 17:11:54 +010015#if !defined(__GNUC__) || __GNUC__ < 4 || (__GNUC__ == 4 && __GNUC_MINOR__ < 8) // gcc >= 4.8 implements this in libgcc
Shih-wei Liao77ed6142010-04-07 12:21:42 -070016#include "int_lib.h"
17
18/* Returns: a * b */
19
20static
21di_int
22__muldsi3(su_int a, su_int b)
23{
24 dwords r;
25 const int bits_in_word_2 = (int)(sizeof(si_int) * CHAR_BIT) / 2;
26 const su_int lower_mask = (su_int)~0 >> bits_in_word_2;
27 r.s.low = (a & lower_mask) * (b & lower_mask);
28 su_int t = r.s.low >> bits_in_word_2;
29 r.s.low &= lower_mask;
30 t += (a >> bits_in_word_2) * (b & lower_mask);
31 r.s.low += (t & lower_mask) << bits_in_word_2;
32 r.s.high = t >> bits_in_word_2;
33 t = r.s.low >> bits_in_word_2;
34 r.s.low &= lower_mask;
35 t += (b >> bits_in_word_2) * (a & lower_mask);
36 r.s.low += (t & lower_mask) << bits_in_word_2;
37 r.s.high += t >> bits_in_word_2;
38 r.s.high += (a >> bits_in_word_2) * (b >> bits_in_word_2);
39 return r.all;
40}
41
42/* Returns: a * b */
43
44di_int
45__muldi3(di_int a, di_int b)
46{
47 dwords x;
48 x.all = a;
49 dwords y;
50 y.all = b;
51 dwords r;
52 r.all = __muldsi3(x.s.low, y.s.low);
53 r.s.high += x.s.high * y.s.low + x.s.low * y.s.high;
54 return r.all;
55}
Bernhard Rosenkränzer3d4d14c2012-12-15 17:11:54 +010056#endif