Commits

Takafumi Arakaki  committed 83211ae

added const to `dot`; I should use "const double *", not "double * const"

  • Participants
  • Parent commits 18cd2a5

Comments (0)

Files changed (1)

File cexp/ext/src/constinloop.c

      x(s+1) = (1 - e) * x(s) + e * (a x(t) + b)
  */
 
-inline double dot(double * restrict a, double * restrict b, int num)
-{
-  int i;
-  double c = 0;
-  for (i = 0; i < num; ++i){
-    c += a[i] * b[i];
-  }
-  return c;
-}
-
 inline void copy(double * restrict dest, double * restrict trg, int num)
 {
   int i;
 import cog
 
 template = """
-inline void %(method)s(
+inline double dot_%(method)s(
+     %(const)sdouble * restrict a, %(const)sdouble * restrict b, int num)
+{
+  int i;
+  double c = 0;
+  for (i = 0; i < num; ++i){
+    c += a[i] * b[i];
+  }
+  return c;
+}
+
+inline void run_%(method)s(
     double ** restrict x, double ** restrict a,
-    double * restrict%(const)s b, double%(const)s e,
-    int%(const)s num_s, int%(const)s num_n)
+    %(const)sdouble * restrict b, %(const)sdouble e,
+    %(const)sint num_s, %(const)sint num_n)
 {
   int s, n;
   for (s = 1; s < num_s; ++s){
     for (n = 0; n < num_n; ++n){
-      x[s][n] += (1 - e) * x[s-1][n] + e * (dot(a[n], x[s-1], num_n) + b[n]);
+      x[s][n] += (1 - e) * x[s-1][n] +
+        e * (dot_%(method)s(a[n], x[s-1], num_n) + b[n]);
     }
   }
 }
 {
   int i;
   for(i = 0; i < repeat; ++i){
-    %(method)s(self->x, self->a, self->b, self->e, self->num_s, self->num_n);
+    run_%(method)s(
+      self->x, self->a, self->b, self->e, self->num_s, self->num_n);
   }
   copy(self->x[0], self->x[self->num_s - 1], self->num_n);
   return 0;
 }
 """
 
-for (method, const) in [('with_const', ' const'), ('without_const', '')]:
+for (method, const) in [('with_const', 'const '), ('without_const', '')]:
     cog.out(template % dict(method=method, const=const))
 ]]]*/
 
-inline void with_const(
+inline double dot_with_const(
+     const double * restrict a, const double * restrict b, int num)
+{
+  int i;
+  double c = 0;
+  for (i = 0; i < num; ++i){
+    c += a[i] * b[i];
+  }
+  return c;
+}
+
+inline void run_with_const(
     double ** restrict x, double ** restrict a,
-    double * restrict const b, double const e,
-    int const num_s, int const num_n)
+    const double * restrict b, const double e,
+    const int num_s, const int num_n)
 {
   int s, n;
   for (s = 1; s < num_s; ++s){
     for (n = 0; n < num_n; ++n){
-      x[s][n] += (1 - e) * x[s-1][n] + e * (dot(a[n], x[s-1], num_n) + b[n]);
+      x[s][n] += (1 - e) * x[s-1][n] +
+        e * (dot_with_const(a[n], x[s-1], num_n) + b[n]);
     }
   }
 }
 {
   int i;
   for(i = 0; i < repeat; ++i){
-    with_const(self->x, self->a, self->b, self->e, self->num_s, self->num_n);
+    run_with_const(
+      self->x, self->a, self->b, self->e, self->num_s, self->num_n);
   }
   copy(self->x[0], self->x[self->num_s - 1], self->num_n);
   return 0;
 }
 
-inline void without_const(
+inline double dot_without_const(
+     double * restrict a, double * restrict b, int num)
+{
+  int i;
+  double c = 0;
+  for (i = 0; i < num; ++i){
+    c += a[i] * b[i];
+  }
+  return c;
+}
+
+inline void run_without_const(
     double ** restrict x, double ** restrict a,
     double * restrict b, double e,
     int num_s, int num_n)
   int s, n;
   for (s = 1; s < num_s; ++s){
     for (n = 0; n < num_n; ++n){
-      x[s][n] += (1 - e) * x[s-1][n] + e * (dot(a[n], x[s-1], num_n) + b[n]);
+      x[s][n] += (1 - e) * x[s-1][n] +
+        e * (dot_without_const(a[n], x[s-1], num_n) + b[n]);
     }
   }
 }
 {
   int i;
   for(i = 0; i < repeat; ++i){
-    without_const(self->x, self->a, self->b, self->e, self->num_s, self->num_n);
+    run_without_const(
+      self->x, self->a, self->b, self->e, self->num_s, self->num_n);
   }
   copy(self->x[0], self->x[self->num_s - 1], self->num_n);
   return 0;