xref: /minix3/sys/external/bsd/compiler_rt/dist/lib/builtins/multi3.c (revision 0a6a1f1d05b60e214de2f05a7310ddd1f0e590e7)
1*0a6a1f1dSLionel Sambuc /* ===-- multi3.c - Implement __multi3 -------------------------------------===
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 __multi3 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 #ifdef CRT_HAS_128BIT
18*0a6a1f1dSLionel Sambuc 
19*0a6a1f1dSLionel Sambuc /* Returns: a * b */
20*0a6a1f1dSLionel Sambuc 
21*0a6a1f1dSLionel Sambuc static
22*0a6a1f1dSLionel Sambuc ti_int
__mulddi3(du_int a,du_int b)23*0a6a1f1dSLionel Sambuc __mulddi3(du_int a, du_int b)
24*0a6a1f1dSLionel Sambuc {
25*0a6a1f1dSLionel Sambuc     twords r;
26*0a6a1f1dSLionel Sambuc     const int bits_in_dword_2 = (int)(sizeof(di_int) * CHAR_BIT) / 2;
27*0a6a1f1dSLionel Sambuc     const du_int lower_mask = (du_int)~0 >> bits_in_dword_2;
28*0a6a1f1dSLionel Sambuc     r.s.low = (a & lower_mask) * (b & lower_mask);
29*0a6a1f1dSLionel Sambuc     du_int t = r.s.low >> bits_in_dword_2;
30*0a6a1f1dSLionel Sambuc     r.s.low &= lower_mask;
31*0a6a1f1dSLionel Sambuc     t += (a >> bits_in_dword_2) * (b & lower_mask);
32*0a6a1f1dSLionel Sambuc     r.s.low += (t & lower_mask) << bits_in_dword_2;
33*0a6a1f1dSLionel Sambuc     r.s.high = t >> bits_in_dword_2;
34*0a6a1f1dSLionel Sambuc     t = r.s.low >> bits_in_dword_2;
35*0a6a1f1dSLionel Sambuc     r.s.low &= lower_mask;
36*0a6a1f1dSLionel Sambuc     t += (b >> bits_in_dword_2) * (a & lower_mask);
37*0a6a1f1dSLionel Sambuc     r.s.low += (t & lower_mask) << bits_in_dword_2;
38*0a6a1f1dSLionel Sambuc     r.s.high += t >> bits_in_dword_2;
39*0a6a1f1dSLionel Sambuc     r.s.high += (a >> bits_in_dword_2) * (b >> bits_in_dword_2);
40*0a6a1f1dSLionel Sambuc     return r.all;
41*0a6a1f1dSLionel Sambuc }
42*0a6a1f1dSLionel Sambuc 
43*0a6a1f1dSLionel Sambuc /* Returns: a * b */
44*0a6a1f1dSLionel Sambuc 
45*0a6a1f1dSLionel Sambuc COMPILER_RT_ABI ti_int
__multi3(ti_int a,ti_int b)46*0a6a1f1dSLionel Sambuc __multi3(ti_int a, ti_int b)
47*0a6a1f1dSLionel Sambuc {
48*0a6a1f1dSLionel Sambuc     twords x;
49*0a6a1f1dSLionel Sambuc     x.all = a;
50*0a6a1f1dSLionel Sambuc     twords y;
51*0a6a1f1dSLionel Sambuc     y.all = b;
52*0a6a1f1dSLionel Sambuc     twords r;
53*0a6a1f1dSLionel Sambuc     r.all = __mulddi3(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*0a6a1f1dSLionel Sambuc 
58*0a6a1f1dSLionel Sambuc #endif /* CRT_HAS_128BIT */
59