1*3cab2bb3Spatrick //===-- muldi3.c - Implement __muldi3 -------------------------------------===//
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 __muldi3 for the compiler_rt library.
10*3cab2bb3Spatrick //
11*3cab2bb3Spatrick //===----------------------------------------------------------------------===//
12*3cab2bb3Spatrick 
13*3cab2bb3Spatrick #include "int_lib.h"
14*3cab2bb3Spatrick 
15*3cab2bb3Spatrick // Returns: a * b
16*3cab2bb3Spatrick 
__muldsi3(su_int a,su_int b)17*3cab2bb3Spatrick static di_int __muldsi3(su_int a, su_int b) {
18*3cab2bb3Spatrick   dwords r;
19*3cab2bb3Spatrick   const int bits_in_word_2 = (int)(sizeof(si_int) * CHAR_BIT) / 2;
20*3cab2bb3Spatrick   const su_int lower_mask = (su_int)~0 >> bits_in_word_2;
21*3cab2bb3Spatrick   r.s.low = (a & lower_mask) * (b & lower_mask);
22*3cab2bb3Spatrick   su_int t = r.s.low >> bits_in_word_2;
23*3cab2bb3Spatrick   r.s.low &= lower_mask;
24*3cab2bb3Spatrick   t += (a >> bits_in_word_2) * (b & lower_mask);
25*3cab2bb3Spatrick   r.s.low += (t & lower_mask) << bits_in_word_2;
26*3cab2bb3Spatrick   r.s.high = t >> bits_in_word_2;
27*3cab2bb3Spatrick   t = r.s.low >> bits_in_word_2;
28*3cab2bb3Spatrick   r.s.low &= lower_mask;
29*3cab2bb3Spatrick   t += (b >> bits_in_word_2) * (a & lower_mask);
30*3cab2bb3Spatrick   r.s.low += (t & lower_mask) << bits_in_word_2;
31*3cab2bb3Spatrick   r.s.high += t >> bits_in_word_2;
32*3cab2bb3Spatrick   r.s.high += (a >> bits_in_word_2) * (b >> bits_in_word_2);
33*3cab2bb3Spatrick   return r.all;
34*3cab2bb3Spatrick }
35*3cab2bb3Spatrick 
36*3cab2bb3Spatrick // Returns: a * b
37*3cab2bb3Spatrick 
__muldi3(di_int a,di_int b)38*3cab2bb3Spatrick COMPILER_RT_ABI di_int __muldi3(di_int a, di_int b) {
39*3cab2bb3Spatrick   dwords x;
40*3cab2bb3Spatrick   x.all = a;
41*3cab2bb3Spatrick   dwords y;
42*3cab2bb3Spatrick   y.all = b;
43*3cab2bb3Spatrick   dwords r;
44*3cab2bb3Spatrick   r.all = __muldsi3(x.s.low, y.s.low);
45*3cab2bb3Spatrick   r.s.high += x.s.high * y.s.low + x.s.low * y.s.high;
46*3cab2bb3Spatrick   return r.all;
47*3cab2bb3Spatrick }
48*3cab2bb3Spatrick 
49*3cab2bb3Spatrick #if defined(__ARM_EABI__)
50*3cab2bb3Spatrick COMPILER_RT_ALIAS(__muldi3, __aeabi_lmul)
51*3cab2bb3Spatrick #endif
52