1 //===-- mulvti3.c - Implement __mulvti3 -----------------------------------===//
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 __mulvti3 for the compiler_rt library.
11 //===----------------------------------------------------------------------===//
19 // Effects: aborts if a * b overflows
21 COMPILER_RT_ABI ti_int __mulvti3(ti_int a, ti_int b) {
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;
35 ti_int sa = a >> (N - 1);
36 ti_int abs_a = (a ^ sa) - sa;
37 ti_int sb = b >> (N - 1);
38 ti_int abs_b = (b ^ sb) - sb;
39 if (abs_a < 2 || abs_b < 2)
42 if (abs_a > MAX / abs_b)
45 if (abs_a > MIN / -abs_b)
51 #endif // CRT_HAS_128BIT