xref: /netbsd-src/sys/external/bsd/compiler_rt/dist/lib/builtins/multi3.c (revision f7f78b3373aafafa211473d7733a8806c0e402c0)
1156cd587Sjoerg /* ===-- multi3.c - Implement __multi3 -------------------------------------===
2156cd587Sjoerg  *
3156cd587Sjoerg  *                     The LLVM Compiler Infrastructure
4156cd587Sjoerg  *
5156cd587Sjoerg  * This file is dual licensed under the MIT and the University of Illinois Open
6156cd587Sjoerg  * Source Licenses. See LICENSE.TXT for details.
7156cd587Sjoerg  *
8156cd587Sjoerg  * ===----------------------------------------------------------------------===
9156cd587Sjoerg 
10156cd587Sjoerg  * This file implements __multi3 for the compiler_rt library.
11156cd587Sjoerg  *
12156cd587Sjoerg  * ===----------------------------------------------------------------------===
13156cd587Sjoerg  */
14156cd587Sjoerg 
15156cd587Sjoerg #include "int_lib.h"
16156cd587Sjoerg 
17156cd587Sjoerg #ifdef CRT_HAS_128BIT
18156cd587Sjoerg 
19156cd587Sjoerg /* Returns: a * b */
20156cd587Sjoerg 
21156cd587Sjoerg static
22156cd587Sjoerg ti_int
__mulddi3(du_int a,du_int b)23156cd587Sjoerg __mulddi3(du_int a, du_int b)
24156cd587Sjoerg {
25156cd587Sjoerg     twords r;
26156cd587Sjoerg     const int bits_in_dword_2 = (int)(sizeof(di_int) * CHAR_BIT) / 2;
27156cd587Sjoerg     const du_int lower_mask = (du_int)~0 >> bits_in_dword_2;
28156cd587Sjoerg     r.s.low = (a & lower_mask) * (b & lower_mask);
29156cd587Sjoerg     du_int t = r.s.low >> bits_in_dword_2;
30156cd587Sjoerg     r.s.low &= lower_mask;
31156cd587Sjoerg     t += (a >> bits_in_dword_2) * (b & lower_mask);
32156cd587Sjoerg     r.s.low += (t & lower_mask) << bits_in_dword_2;
33156cd587Sjoerg     r.s.high = t >> bits_in_dword_2;
34156cd587Sjoerg     t = r.s.low >> bits_in_dword_2;
35156cd587Sjoerg     r.s.low &= lower_mask;
36156cd587Sjoerg     t += (b >> bits_in_dword_2) * (a & lower_mask);
37156cd587Sjoerg     r.s.low += (t & lower_mask) << bits_in_dword_2;
38156cd587Sjoerg     r.s.high += t >> bits_in_dword_2;
39156cd587Sjoerg     r.s.high += (a >> bits_in_dword_2) * (b >> bits_in_dword_2);
40156cd587Sjoerg     return r.all;
41156cd587Sjoerg }
42156cd587Sjoerg 
43156cd587Sjoerg /* Returns: a * b */
44156cd587Sjoerg 
45*f7f78b33Sjoerg COMPILER_RT_ABI ti_int
__multi3(ti_int a,ti_int b)46156cd587Sjoerg __multi3(ti_int a, ti_int b)
47156cd587Sjoerg {
48156cd587Sjoerg     twords x;
49156cd587Sjoerg     x.all = a;
50156cd587Sjoerg     twords y;
51156cd587Sjoerg     y.all = b;
52156cd587Sjoerg     twords r;
53156cd587Sjoerg     r.all = __mulddi3(x.s.low, y.s.low);
54156cd587Sjoerg     r.s.high += x.s.high * y.s.low + x.s.low * y.s.high;
55156cd587Sjoerg     return r.all;
56156cd587Sjoerg }
57156cd587Sjoerg 
58156cd587Sjoerg #endif /* CRT_HAS_128BIT */
59