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