xref: /minix3/sys/external/bsd/compiler_rt/dist/lib/builtins/muldi3.c (revision 0a6a1f1d05b60e214de2f05a7310ddd1f0e590e7)
1*0a6a1f1dSLionel Sambuc /* ===-- muldi3.c - Implement __muldi3 -------------------------------------===
2*0a6a1f1dSLionel Sambuc  *
3*0a6a1f1dSLionel Sambuc  *                     The LLVM Compiler Infrastructure
4*0a6a1f1dSLionel Sambuc  *
5*0a6a1f1dSLionel Sambuc  * This file is dual licensed under the MIT and the University of Illinois Open
6*0a6a1f1dSLionel Sambuc  * Source Licenses. See LICENSE.TXT for details.
7*0a6a1f1dSLionel Sambuc  *
8*0a6a1f1dSLionel Sambuc  * ===----------------------------------------------------------------------===
9*0a6a1f1dSLionel Sambuc  *
10*0a6a1f1dSLionel Sambuc  * This file implements __muldi3 for the compiler_rt library.
11*0a6a1f1dSLionel Sambuc  *
12*0a6a1f1dSLionel Sambuc  * ===----------------------------------------------------------------------===
13*0a6a1f1dSLionel Sambuc  */
14*0a6a1f1dSLionel Sambuc 
15*0a6a1f1dSLionel Sambuc #include "int_lib.h"
16*0a6a1f1dSLionel Sambuc 
17*0a6a1f1dSLionel Sambuc /* Returns: a * b */
18*0a6a1f1dSLionel Sambuc 
19*0a6a1f1dSLionel Sambuc static
20*0a6a1f1dSLionel Sambuc di_int
__muldsi3(su_int a,su_int b)21*0a6a1f1dSLionel Sambuc __muldsi3(su_int a, su_int b)
22*0a6a1f1dSLionel Sambuc {
23*0a6a1f1dSLionel Sambuc     dwords r;
24*0a6a1f1dSLionel Sambuc     const int bits_in_word_2 = (int)(sizeof(si_int) * CHAR_BIT) / 2;
25*0a6a1f1dSLionel Sambuc     const su_int lower_mask = (su_int)~0 >> bits_in_word_2;
26*0a6a1f1dSLionel Sambuc     r.s.low = (a & lower_mask) * (b & lower_mask);
27*0a6a1f1dSLionel Sambuc     su_int t = r.s.low >> bits_in_word_2;
28*0a6a1f1dSLionel Sambuc     r.s.low &= lower_mask;
29*0a6a1f1dSLionel Sambuc     t += (a >> bits_in_word_2) * (b & lower_mask);
30*0a6a1f1dSLionel Sambuc     r.s.low += (t & lower_mask) << bits_in_word_2;
31*0a6a1f1dSLionel Sambuc     r.s.high = t >> bits_in_word_2;
32*0a6a1f1dSLionel Sambuc     t = r.s.low >> bits_in_word_2;
33*0a6a1f1dSLionel Sambuc     r.s.low &= lower_mask;
34*0a6a1f1dSLionel Sambuc     t += (b >> bits_in_word_2) * (a & lower_mask);
35*0a6a1f1dSLionel Sambuc     r.s.low += (t & lower_mask) << bits_in_word_2;
36*0a6a1f1dSLionel Sambuc     r.s.high += t >> bits_in_word_2;
37*0a6a1f1dSLionel Sambuc     r.s.high += (a >> bits_in_word_2) * (b >> bits_in_word_2);
38*0a6a1f1dSLionel Sambuc     return r.all;
39*0a6a1f1dSLionel Sambuc }
40*0a6a1f1dSLionel Sambuc 
41*0a6a1f1dSLionel Sambuc /* Returns: a * b */
42*0a6a1f1dSLionel Sambuc 
ARM_EABI_FNALIAS(lmul,muldi3)43*0a6a1f1dSLionel Sambuc ARM_EABI_FNALIAS(lmul, muldi3)
44*0a6a1f1dSLionel Sambuc 
45*0a6a1f1dSLionel Sambuc COMPILER_RT_ABI di_int
46*0a6a1f1dSLionel Sambuc __muldi3(di_int a, di_int b)
47*0a6a1f1dSLionel Sambuc {
48*0a6a1f1dSLionel Sambuc     dwords x;
49*0a6a1f1dSLionel Sambuc     x.all = a;
50*0a6a1f1dSLionel Sambuc     dwords y;
51*0a6a1f1dSLionel Sambuc     y.all = b;
52*0a6a1f1dSLionel Sambuc     dwords r;
53*0a6a1f1dSLionel Sambuc     r.all = __muldsi3(x.s.low, y.s.low);
54*0a6a1f1dSLionel Sambuc     r.s.high += x.s.high * y.s.low + x.s.low * y.s.high;
55*0a6a1f1dSLionel Sambuc     return r.all;
56*0a6a1f1dSLionel Sambuc }
57