[gtk/update-ci-to-f34: 63/64] Help the compiler out




commit 014fb414482f586715d6f3c8ea79a64063e9ea26
Author: Matthias Clasen <mclasen redhat com>
Date:   Sat May 1 20:21:54 2021 -0400

    Help the compiler out
    
    gcc says: error: iteration 2147483649 invokes undefined behavior
                                [-Werror=aggressive-loop-optimizations]
    which of course, never happens because ncols is always >= 2.
    Add some assertions to tell the compiler.

 demos/gtk-demo/singular_value_decomposition.c | 15 ++++++++++++++-
 1 file changed, 14 insertions(+), 1 deletion(-)
---
diff --git a/demos/gtk-demo/singular_value_decomposition.c b/demos/gtk-demo/singular_value_decomposition.c
index e6d063ab4b..3dafb36242 100644
--- a/demos/gtk-demo/singular_value_decomposition.c
+++ b/demos/gtk-demo/singular_value_decomposition.c
@@ -2,6 +2,7 @@
 #include <float.h>
 #include <math.h>
 #include <glib.h>
+#include <assert.h>
 
 /* See Golub and Reinsch,
  * "Handbook for Automatic Computation vol II - Linear Algebra",
@@ -39,6 +40,9 @@ householder_reduction (double *A,
   double *pu, *pui, *pv, *pvi;
   double half_norm_squared;
 
+  assert (nrows >= 2);
+  assert (ncols >= 2);
+
   memcpy (U, A, sizeof (double) * nrows * ncols);
 
   diagonal[0] = 0.0;
@@ -205,6 +209,9 @@ givens_reduction (int nrows,
   int rotation_test;
   int iteration_count;
 
+  assert (nrows >= 2);
+  assert (ncols >= 2);
+
   for (i = 0, x = 0.0; i < ncols; i++)
     {
       y = fabs (diagonal[i]) + fabs (superdiagonal[i]);
@@ -342,6 +349,9 @@ sort_singular_values (int     nrows,
   double temp;
   double *p1, *p2;
 
+  assert (nrows >= 2);
+  assert (ncols >= 2);
+
   for (i = 0; i < ncols - 1; i++)
     {
       max_index = i;
@@ -433,9 +443,12 @@ singular_value_decomposition_solve (double *U,
   double d;
   double tolerance;
 
+  assert (nrows >= 2);
+  assert (ncols >= 2);
+
   tolerance = DBL_EPSILON * S[0] * (double) ncols;
 
-  for ( i = 0, pv = V; i < ncols; i++, pv += ncols)
+  for (i = 0, pv = V; i < ncols; i++, pv += ncols)
     {
       x[i] = 0.0;
       for (j = 0; j < ncols; j++)


[Date Prev][Date Next]   [Thread Prev][Thread Next]   [Thread Index] [Date Index] [Author Index]