1 /*===-- int128_builtins.cpp - Implement __muloti4 --------------------------===
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 __muloti4, and is stolen from the compiler_rt library.
11 * FIXME: we steal and re-compile it into filesystem, which uses __int128_t,
12 * and requires this builtin when sanitized. See llvm.org/PR30643
14 * ===----------------------------------------------------------------------===
19 #if _LIBCPP_HAS_INT128
21 extern "C" __attribute__((no_sanitize("undefined"))) _LIBCPP_EXPORTED_FROM_ABI __int128_t
22 __muloti4(__int128_t a
, __int128_t b
, int* overflow
) {
23 const int N
= (int)(sizeof(__int128_t
) * CHAR_BIT
);
24 const __int128_t MIN
= (__int128_t
)1 << (N
- 1);
25 const __int128_t MAX
= ~MIN
;
27 __int128_t result
= a
* b
;
38 __int128_t sa
= a
>> (N
- 1);
39 __int128_t abs_a
= (a
^ sa
) - sa
;
40 __int128_t sb
= b
>> (N
- 1);
41 __int128_t abs_b
= (b
^ sb
) - sb
;
42 if (abs_a
< 2 || abs_b
< 2)
45 if (abs_a
> MAX
/ abs_b
)
48 if (abs_a
> MIN
/ -abs_b
)