xref: /openbsd-src/sys/lib/libkern/multi3.c (revision 0a23da6d899cac04b5edee3582d508bfdcaddebd)
1 /* ===-- multi3.c - Implement __multi3 -------------------------------------===
2  *
3  *                     The LLVM Compiler Infrastructure
4  *
5  * This file is dual licensed under the MIT and the University of Illinois Open
6  * Source Licenses. See LICENSE.TXT for details.
7  *
8  * ===----------------------------------------------------------------------===
9 
10  * This file implements __multi3 for the compiler_rt library.
11  *
12  * ===----------------------------------------------------------------------===
13  */
14 
15 #include "crt_glue.h"
16 
17 /* Returns: a * b */
18 
19 static
20 ti_int
__mulddi3(du_int a,du_int b)21 __mulddi3(du_int a, du_int b)
22 {
23     twords r;
24     const int bits_in_dword_2 = (int)(sizeof(di_int) * CHAR_BIT) / 2;
25     const du_int lower_mask = (du_int)~0 >> bits_in_dword_2;
26     r.s.low = (a & lower_mask) * (b & lower_mask);
27     du_int t = r.s.low >> bits_in_dword_2;
28     r.s.low &= lower_mask;
29     t += (a >> bits_in_dword_2) * (b & lower_mask);
30     r.s.low += (t & lower_mask) << bits_in_dword_2;
31     r.s.high = t >> bits_in_dword_2;
32     t = r.s.low >> bits_in_dword_2;
33     r.s.low &= lower_mask;
34     t += (b >> bits_in_dword_2) * (a & lower_mask);
35     r.s.low += (t & lower_mask) << bits_in_dword_2;
36     r.s.high += t >> bits_in_dword_2;
37     r.s.high += (a >> bits_in_dword_2) * (b >> bits_in_dword_2);
38     return r.all;
39 }
40 
41 /* Returns: a * b */
42 
43 ti_int
__multi3(ti_int a,ti_int b)44 __multi3(ti_int a, ti_int b)
45 {
46     twords x;
47     x.all = a;
48     twords y;
49     y.all = b;
50     twords r;
51     r.all = __mulddi3(x.s.low, y.s.low);
52     r.s.high += x.s.high * y.s.low + x.s.low * y.s.high;
53     return r.all;
54 }
55