]> CyberLeo.Net >> Repos - FreeBSD/FreeBSD.git/blob - contrib/llvm-project/compiler-rt/lib/builtins/muloti4.c
Merge llvm, clang, compiler-rt, libc++, libunwind, lld, lldb and openmp
[FreeBSD/FreeBSD.git] / contrib / llvm-project / compiler-rt / lib / builtins / muloti4.c
1 //===-- muloti4.c - Implement __muloti4 -----------------------------------===//
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 __muloti4 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
19 // Effects: sets *overflow to 1  if a * b overflows
20
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;
25   *overflow = 0;
26   ti_int result = a * b;
27   if (a == MIN) {
28     if (b != 0 && b != 1)
29       *overflow = 1;
30     return result;
31   }
32   if (b == MIN) {
33     if (a != 0 && a != 1)
34       *overflow = 1;
35     return result;
36   }
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)
42     return result;
43   if (sa == sb) {
44     if (abs_a > MAX / abs_b)
45       *overflow = 1;
46   } else {
47     if (abs_a > MIN / -abs_b)
48       *overflow = 1;
49   }
50   return result;
51 }
52
53 #endif // CRT_HAS_128BIT