xref: /openbsd-src/gnu/llvm/compiler-rt/lib/builtins/multi3.c (revision 3cab2bb3f667058bece8e38b12449a63a9d73c4b)
1*3cab2bb3Spatrick //===-- multi3.c - Implement __multi3 -------------------------------------===//
2*3cab2bb3Spatrick //
3*3cab2bb3Spatrick // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4*3cab2bb3Spatrick // See https://llvm.org/LICENSE.txt for license information.
5*3cab2bb3Spatrick // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6*3cab2bb3Spatrick //
7*3cab2bb3Spatrick //===----------------------------------------------------------------------===//
8*3cab2bb3Spatrick //
9*3cab2bb3Spatrick // This file implements __multi3 for the compiler_rt library.
10*3cab2bb3Spatrick //
11*3cab2bb3Spatrick //===----------------------------------------------------------------------===//
12*3cab2bb3Spatrick 
13*3cab2bb3Spatrick #include "int_lib.h"
14*3cab2bb3Spatrick 
15*3cab2bb3Spatrick #ifdef CRT_HAS_128BIT
16*3cab2bb3Spatrick 
17*3cab2bb3Spatrick // Returns: a * b
18*3cab2bb3Spatrick 
__mulddi3(du_int a,du_int b)19*3cab2bb3Spatrick static ti_int __mulddi3(du_int a, du_int b) {
20*3cab2bb3Spatrick   twords r;
21*3cab2bb3Spatrick   const int bits_in_dword_2 = (int)(sizeof(di_int) * CHAR_BIT) / 2;
22*3cab2bb3Spatrick   const du_int lower_mask = (du_int)~0 >> bits_in_dword_2;
23*3cab2bb3Spatrick   r.s.low = (a & lower_mask) * (b & lower_mask);
24*3cab2bb3Spatrick   du_int t = r.s.low >> bits_in_dword_2;
25*3cab2bb3Spatrick   r.s.low &= lower_mask;
26*3cab2bb3Spatrick   t += (a >> bits_in_dword_2) * (b & lower_mask);
27*3cab2bb3Spatrick   r.s.low += (t & lower_mask) << bits_in_dword_2;
28*3cab2bb3Spatrick   r.s.high = t >> bits_in_dword_2;
29*3cab2bb3Spatrick   t = r.s.low >> bits_in_dword_2;
30*3cab2bb3Spatrick   r.s.low &= lower_mask;
31*3cab2bb3Spatrick   t += (b >> bits_in_dword_2) * (a & lower_mask);
32*3cab2bb3Spatrick   r.s.low += (t & lower_mask) << bits_in_dword_2;
33*3cab2bb3Spatrick   r.s.high += t >> bits_in_dword_2;
34*3cab2bb3Spatrick   r.s.high += (a >> bits_in_dword_2) * (b >> bits_in_dword_2);
35*3cab2bb3Spatrick   return r.all;
36*3cab2bb3Spatrick }
37*3cab2bb3Spatrick 
38*3cab2bb3Spatrick // Returns: a * b
39*3cab2bb3Spatrick 
__multi3(ti_int a,ti_int b)40*3cab2bb3Spatrick COMPILER_RT_ABI ti_int __multi3(ti_int a, ti_int b) {
41*3cab2bb3Spatrick   twords x;
42*3cab2bb3Spatrick   x.all = a;
43*3cab2bb3Spatrick   twords y;
44*3cab2bb3Spatrick   y.all = b;
45*3cab2bb3Spatrick   twords r;
46*3cab2bb3Spatrick   r.all = __mulddi3(x.s.low, y.s.low);
47*3cab2bb3Spatrick   r.s.high += x.s.high * y.s.low + x.s.low * y.s.high;
48*3cab2bb3Spatrick   return r.all;
49*3cab2bb3Spatrick }
50*3cab2bb3Spatrick 
51*3cab2bb3Spatrick #endif // CRT_HAS_128BIT
52