1//===-- multi3.c - Implement __multi3 -------------------------------------===//
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 __multi3 for the compiler_rt library.
10//
11//===----------------------------------------------------------------------===//
12
13#include "int_lib.h"
14
15#ifdef CRT_HAS_128BIT
16
17// Returns: a * b
18
19static ti_int __mulddi3(du_int a, du_int b) {
20 twords r;
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);
35 return r.all;
36}
37
38// Returns: a * b
39
40COMPILER_RT_ABI ti_int __multi3(ti_int a, ti_int b) {
41 twords x;
42 x.all = a;
43 twords y;
44 y.all = b;
45 twords r;
46 r.all = __mulddi3(a: x.s.low, b: y.s.low);
47 r.s.high += x.s.high * y.s.low + x.s.low * y.s.high;
48 return r.all;
49}
50
51#endif // CRT_HAS_128BIT
52

source code of compiler-rt/lib/builtins/multi3.c