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