1 //===-- multi3.c - Implement __multi3 -------------------------------------===//
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
7 //===----------------------------------------------------------------------===//
9 // This file implements __multi3 for the compiler_rt library.
11 //===----------------------------------------------------------------------===//
19 static ti_int __mulddi3(du_int a, du_int b) {
21 const int bits_in_dword_2 = (int)(sizeof(di_int) * CHAR_BIT) / 2;
22 const du_int lower_mask = (du_int)~0 >> bits_in_dword_2;
23 r.s.low = (a & lower_mask) * (b & lower_mask);
24 du_int t = r.s.low >> bits_in_dword_2;
25 r.s.low &= lower_mask;
26 t += (a >> bits_in_dword_2) * (b & lower_mask);
27 r.s.low += (t & lower_mask) << bits_in_dword_2;
28 r.s.high = t >> bits_in_dword_2;
29 t = r.s.low >> bits_in_dword_2;
30 r.s.low &= lower_mask;
31 t += (b >> bits_in_dword_2) * (a & lower_mask);
32 r.s.low += (t & lower_mask) << bits_in_dword_2;
33 r.s.high += t >> bits_in_dword_2;
34 r.s.high += (a >> bits_in_dword_2) * (b >> bits_in_dword_2);
40 COMPILER_RT_ABI ti_int __multi3(ti_int a, ti_int b) {
46 r.all = __mulddi3(x.s.low, y.s.low);
47 r.s.high += x.s.high * y.s.low + x.s.low * y.s.high;
51 #endif // CRT_HAS_128BIT