atrosinenko created this revision.
atrosinenko added reviewers: MaskRay, kamleshbhalui, luismarques, efriedma, 
aykevl.
Herald added subscribers: Sanitizers, dberris.
Herald added a project: Sanitizers.
atrosinenko requested review of this revision.

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

Factor them out to int_mulv_impl.inc for better maintainability.

The following command may help understanding the changes:

  diff int_mulv_impl.inc \
       <(git show master:./mulvsi3.c | sed 's/si_int/fixint_t/g')

The `si` and `di` variants has three lines different (with two of them being 
comments) and `ti` has a bit larger diff.


Repository:
  rG LLVM Github Monorepo

https://reviews.llvm.org/D86277

Files:
  compiler-rt/lib/builtins/int_mulv_impl.inc
  compiler-rt/lib/builtins/mulvdi3.c
  compiler-rt/lib/builtins/mulvsi3.c
  compiler-rt/lib/builtins/mulvti3.c

Index: compiler-rt/lib/builtins/mulvti3.c
===================================================================
--- compiler-rt/lib/builtins/mulvti3.c
+++ 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
Index: compiler-rt/lib/builtins/mulvsi3.c
===================================================================
--- compiler-rt/lib/builtins/mulvsi3.c
+++ 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); }
Index: compiler-rt/lib/builtins/mulvdi3.c
===================================================================
--- compiler-rt/lib/builtins/mulvdi3.c
+++ 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); }
Index: compiler-rt/lib/builtins/int_mulv_impl.inc
===================================================================
--- compiler-rt/lib/builtins/int_mulv_impl.inc
+++ compiler-rt/lib/builtins/int_mulv_impl.inc
@@ -1,4 +1,4 @@
-//===-- mulvdi3.c - Implement __mulvdi3 -----------------------------------===//
+//===-- int_mulv_impl.inc - Implement __mulv[sdt]i3 -----------------------===//
 //
 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
 // See https://llvm.org/LICENSE.txt for license information.
@@ -6,7 +6,7 @@
 //
 //===----------------------------------------------------------------------===//
 //
-// This file implements __mulvdi3 for the compiler_rt library.
+// Helper used by __mulvsi3, __mulvdi3 and __mulvti3.
 //
 //===----------------------------------------------------------------------===//
 
@@ -16,10 +16,10 @@
 
 // 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;
+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;
@@ -30,10 +30,10 @@
       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;
+  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) {
_______________________________________________
cfe-commits mailing list
cfe-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits

Reply via email to