blob: e6322bf5df82d6d97309ceec72866c354308f592 [file] [log] [blame]
Edward O'Callaghan37a6a452009-08-07 20:30:09 +00001/* ===-- muldi3.c - Implement __muldi3 -------------------------------------===
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 __muldi3 for the compiler_rt library.
11 *
12 * ===----------------------------------------------------------------------===
Anton Korobeynikov1c5f89b2011-04-19 17:52:09 +000013 */
14#include "abi.h"
Daniel Dunbarb3a69012009-06-26 16:47:03 +000015
16#include "int_lib.h"
17
Edward O'Callaghan37a6a452009-08-07 20:30:09 +000018/* Returns: a * b */
Daniel Dunbarb3a69012009-06-26 16:47:03 +000019
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;
Edward O'Callaghan8bf1e092009-08-09 18:41:02 +000027 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;
Daniel Dunbarb3a69012009-06-26 16:47:03 +000030 t += (a >> bits_in_word_2) * (b & lower_mask);
Edward O'Callaghan8bf1e092009-08-09 18:41:02 +000031 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;
Daniel Dunbarb3a69012009-06-26 16:47:03 +000035 t += (b >> bits_in_word_2) * (a & lower_mask);
Edward O'Callaghan8bf1e092009-08-09 18:41:02 +000036 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);
Daniel Dunbarb3a69012009-06-26 16:47:03 +000039 return r.all;
40}
41
Edward O'Callaghan37a6a452009-08-07 20:30:09 +000042/* Returns: a * b */
Daniel Dunbarb3a69012009-06-26 16:47:03 +000043
Anton Korobeynikov37b97d12011-04-19 17:51:24 +000044ARM_EABI_FNALIAS(lmul, muldi3);
45
Anton Korobeynikov1c5f89b2011-04-19 17:52:09 +000046COMPILER_RT_ABI di_int
Daniel Dunbarb3a69012009-06-26 16:47:03 +000047__muldi3(di_int a, di_int b)
48{
49 dwords x;
50 x.all = a;
51 dwords y;
52 y.all = b;
53 dwords r;
Edward O'Callaghan8bf1e092009-08-09 18:41:02 +000054 r.all = __muldsi3(x.s.low, y.s.low);
55 r.s.high += x.s.high * y.s.low + x.s.low * y.s.high;
Daniel Dunbarb3a69012009-06-26 16:47:03 +000056 return r.all;
57}