1 //===-- muloti4.c - 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 for the compiler_rt library.
11 //===----------------------------------------------------------------------===//
19 // Effects: sets *overflow to 1 if a * b overflows
21 COMPILER_RT_ABI ti_int __muloti4(ti_int a, ti_int b, int *overflow) {
22 const int N = (int)(sizeof(ti_int) * CHAR_BIT);
23 const ti_int MIN = (ti_int)1 << (N - 1);
24 const ti_int MAX = ~MIN;
26 ti_int result = a * b;
37 ti_int sa = a >> (N - 1);
38 ti_int abs_a = (a ^ sa) - sa;
39 ti_int sb = b >> (N - 1);
40 ti_int abs_b = (b ^ sb) - sb;
41 if (abs_a < 2 || abs_b < 2)
44 if (abs_a > MAX / abs_b)
47 if (abs_a > MIN / -abs_b)
53 #endif // CRT_HAS_128BIT