[compiler-rt] 182d14d - [NFC][compiler-rt] Factor out __mulv[sdt]i3 implementations to .inc file

Anatoly Trosinenko via llvm-commits llvm-commits at lists.llvm.org
Thu Aug 27 04:36:40 PDT 2020


Author: Anatoly Trosinenko
Date: 2020-08-27T14:33:48+03:00
New Revision: 182d14db07f19e7a9cafc2d7218315bf65a55c0b

URL: https://github.com/llvm/llvm-project/commit/182d14db07f19e7a9cafc2d7218315bf65a55c0b
DIFF: https://github.com/llvm/llvm-project/commit/182d14db07f19e7a9cafc2d7218315bf65a55c0b.diff

LOG: [NFC][compiler-rt] Factor out __mulv[sdt]i3 implementations to .inc file

The existing implementations are almost identical except for width of the
integer type.

Factor them out to int_mulv_impl.inc for better maintainability.

Reviewed By: MaskRay

Differential Revision: https://reviews.llvm.org/D86277

Added: 
    compiler-rt/lib/builtins/int_mulv_impl.inc

Modified: 
    compiler-rt/lib/builtins/mulvdi3.c
    compiler-rt/lib/builtins/mulvsi3.c
    compiler-rt/lib/builtins/mulvti3.c

Removed: 
    


################################################################################
diff  --git a/compiler-rt/lib/builtins/int_mulv_impl.inc b/compiler-rt/lib/builtins/int_mulv_impl.inc
new file mode 100644
index 000000000000..1e920716ec49
--- /dev/null
+++ b/compiler-rt/lib/builtins/int_mulv_impl.inc
@@ -0,0 +1,47 @@
+//===-- int_mulv_impl.inc - Implement __mulv[sdt]i3 ---------------*- C -*-===//
+//
+// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
+// See https://llvm.org/LICENSE.txt for license information.
+// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
+//
+//===----------------------------------------------------------------------===//
+//
+// Helper used by __mulvsi3, __mulvdi3 and __mulvti3.
+//
+//===----------------------------------------------------------------------===//
+
+#include "int_lib.h"
+
+// Returns: a * b
+
+// Effects: aborts if a * b overflows
+
+static __inline fixint_t __mulvXi3(fixint_t a, fixint_t b) {
+  const int N = (int)(sizeof(fixint_t) * CHAR_BIT);
+  const fixint_t MIN = (fixint_t)1 << (N - 1);
+  const fixint_t MAX = ~MIN;
+  if (a == MIN) {
+    if (b == 0 || b == 1)
+      return a * b;
+    compilerrt_abort();
+  }
+  if (b == MIN) {
+    if (a == 0 || a == 1)
+      return a * b;
+    compilerrt_abort();
+  }
+  fixint_t sa = a >> (N - 1);
+  fixint_t abs_a = (a ^ sa) - sa;
+  fixint_t sb = b >> (N - 1);
+  fixint_t abs_b = (b ^ sb) - sb;
+  if (abs_a < 2 || abs_b < 2)
+    return a * b;
+  if (sa == sb) {
+    if (abs_a > MAX / abs_b)
+      compilerrt_abort();
+  } else {
+    if (abs_a > MIN / -abs_b)
+      compilerrt_abort();
+  }
+  return a * b;
+}

diff  --git a/compiler-rt/lib/builtins/mulvdi3.c b/compiler-rt/lib/builtins/mulvdi3.c
index cecc97ccf22e..1d672c6dc155 100644
--- a/compiler-rt/lib/builtins/mulvdi3.c
+++ b/compiler-rt/lib/builtins/mulvdi3.c
@@ -10,38 +10,11 @@
 //
 //===----------------------------------------------------------------------===//
 
-#include "int_lib.h"
+#define fixint_t di_int
+#include "int_mulv_impl.inc"
 
 // Returns: a * b
 
 // Effects: aborts if a * b overflows
 
-COMPILER_RT_ABI di_int __mulvdi3(di_int a, di_int b) {
-  const int N = (int)(sizeof(di_int) * CHAR_BIT);
-  const di_int MIN = (di_int)1 << (N - 1);
-  const di_int MAX = ~MIN;
-  if (a == MIN) {
-    if (b == 0 || b == 1)
-      return a * b;
-    compilerrt_abort();
-  }
-  if (b == MIN) {
-    if (a == 0 || a == 1)
-      return a * b;
-    compilerrt_abort();
-  }
-  di_int sa = a >> (N - 1);
-  di_int abs_a = (a ^ sa) - sa;
-  di_int sb = b >> (N - 1);
-  di_int abs_b = (b ^ sb) - sb;
-  if (abs_a < 2 || abs_b < 2)
-    return a * b;
-  if (sa == sb) {
-    if (abs_a > MAX / abs_b)
-      compilerrt_abort();
-  } else {
-    if (abs_a > MIN / -abs_b)
-      compilerrt_abort();
-  }
-  return a * b;
-}
+COMPILER_RT_ABI di_int __mulvdi3(di_int a, di_int b) { return __mulvXi3(a, b); }

diff  --git a/compiler-rt/lib/builtins/mulvsi3.c b/compiler-rt/lib/builtins/mulvsi3.c
index 0d6b18ad01a4..00b2e50eeca9 100644
--- a/compiler-rt/lib/builtins/mulvsi3.c
+++ b/compiler-rt/lib/builtins/mulvsi3.c
@@ -10,38 +10,11 @@
 //
 //===----------------------------------------------------------------------===//
 
-#include "int_lib.h"
+#define fixint_t si_int
+#include "int_mulv_impl.inc"
 
 // Returns: a * b
 
 // Effects: aborts if a * b overflows
 
-COMPILER_RT_ABI si_int __mulvsi3(si_int a, si_int b) {
-  const int N = (int)(sizeof(si_int) * CHAR_BIT);
-  const si_int MIN = (si_int)1 << (N - 1);
-  const si_int MAX = ~MIN;
-  if (a == MIN) {
-    if (b == 0 || b == 1)
-      return a * b;
-    compilerrt_abort();
-  }
-  if (b == MIN) {
-    if (a == 0 || a == 1)
-      return a * b;
-    compilerrt_abort();
-  }
-  si_int sa = a >> (N - 1);
-  si_int abs_a = (a ^ sa) - sa;
-  si_int sb = b >> (N - 1);
-  si_int abs_b = (b ^ sb) - sb;
-  if (abs_a < 2 || abs_b < 2)
-    return a * b;
-  if (sa == sb) {
-    if (abs_a > MAX / abs_b)
-      compilerrt_abort();
-  } else {
-    if (abs_a > MIN / -abs_b)
-      compilerrt_abort();
-  }
-  return a * b;
-}
+COMPILER_RT_ABI si_int __mulvsi3(si_int a, si_int b) { return __mulvXi3(a, b); }

diff  --git a/compiler-rt/lib/builtins/mulvti3.c b/compiler-rt/lib/builtins/mulvti3.c
index 03963a0ca694..ba355149f9a7 100644
--- a/compiler-rt/lib/builtins/mulvti3.c
+++ b/compiler-rt/lib/builtins/mulvti3.c
@@ -18,34 +18,9 @@
 
 // Effects: aborts if a * b overflows
 
-COMPILER_RT_ABI ti_int __mulvti3(ti_int a, ti_int b) {
-  const int N = (int)(sizeof(ti_int) * CHAR_BIT);
-  const ti_int MIN = (ti_int)1 << (N - 1);
-  const ti_int MAX = ~MIN;
-  if (a == MIN) {
-    if (b == 0 || b == 1)
-      return a * b;
-    compilerrt_abort();
-  }
-  if (b == MIN) {
-    if (a == 0 || a == 1)
-      return a * b;
-    compilerrt_abort();
-  }
-  ti_int sa = a >> (N - 1);
-  ti_int abs_a = (a ^ sa) - sa;
-  ti_int sb = b >> (N - 1);
-  ti_int abs_b = (b ^ sb) - sb;
-  if (abs_a < 2 || abs_b < 2)
-    return a * b;
-  if (sa == sb) {
-    if (abs_a > MAX / abs_b)
-      compilerrt_abort();
-  } else {
-    if (abs_a > MIN / -abs_b)
-      compilerrt_abort();
-  }
-  return a * b;
-}
+#define fixint_t ti_int
+#include "int_mulv_impl.inc"
+
+COMPILER_RT_ABI ti_int __mulvti3(ti_int a, ti_int b) { return __mulvXi3(a, b); }
 
 #endif // CRT_HAS_128BIT


        


More information about the llvm-commits mailing list