D issues are now tracked on GitHub. This Bugzilla instance remains as a read-only archive.
Issue 3377 - [tdpl] static foreach should be implemented
Summary: [tdpl] static foreach should be implemented
Status: RESOLVED WONTFIX
Alias: None
Product: D
Classification: Unclassified
Component: dmd (show other issues)
Version: D2
Hardware: Other Linux
: P2 enhancement
Assignee: No Owner
URL:
Keywords:
Depends on:
Blocks:
 
Reported: 2009-10-08 12:12 UTC by Andrei Alexandrescu
Modified: 2015-06-09 05:14 UTC (History)
2 users (show)

See Also:


Attachments

Note You need to log in before you can comment on or make changes to this issue.
Description Andrei Alexandrescu 2009-10-08 12:12:29 UTC
This should compile:

import std.contracts;

double unrolledDotProduct(double[] a, double[] b) {
   enum branches = 4;
   enforce(a.length == b.length);
   double result = 0;
   auto n = (a.length / branches) * branches;
   double temp[branches];
   for (size_t i = 0; i != n; i += branches) {
      static foreach (j ; 0 .. branches) {
         temp[j] = a[i + j] * b[i + j];
      }
      result += inline_sum(temp);
   }
   foreach (j; n .. a.length) {
      result += a[j] * b[j];
   }
   return result;
}
Comment 1 FeepingCreature 2009-10-08 12:35:53 UTC
This does compile (on 1.0):

template Repeat(T, int I) {
  static if (!I) alias Tuple!() Repeat;
  else alias Tuple!(T, Repeat!(T, I - 1)) Repeat;
}

double unrolledDotProduct(double[] a, double[] b) {
   const branches = 4;
   assert(a.length == b.length);
   double result = 0;
   auto n = (a.length / branches) * branches;
   double temp[branches];
   for (size_t i = 0; i != n; i += branches) {
      foreach (j, BOGUS; Repeat!(void, branches)) {
         temp[j] = a[i + j] * b[i + j];
      }
      result += inline_sum(temp);
   }
   foreach (j; n .. a.length) {
      result += a[j] * b[j];
   }
   return result;
}
Comment 2 Walter Bright 2009-11-18 13:38:27 UTC
Many problems have come up with the design of this, so will mark as won't implement until a thorough design is developed.