The Magic Kernel
Hmm, aside from my note in <a href="https:&#x2F;&#x2F;news.ycombinator.com&#x2F;item?id=26543937" rel="nofollow">https:&#x2F;&#x2F;news.ycombinator.com&#x2F;item?id=26543937</a> about how the family of kernels Costella discovered are precisely the uniform cardinal <i>B</i>-splines, I noticed this slight error:<p>&gt; Computationally, however, nothing short of Fast Fourier Transforms or hardware acceleration can make a large blurring job efficient: for large values of blur, the support of any blur kernel is large, and not practical for some at-scale applications.<p>The standard way to do Gaussian blur is to approximate it as a <i>B</i>-spline, specifically a cascade of simple moving average filters, also known as a Hogenauer filter or CIC filter in the time domain.  Typically a cascade of three filters is used, precisely as Costella describes.  This is both simpler and faster than doing the task with a Fourier transform, and moreover it is an online algorithm — it produces output samples at some processing lag behind the input samples.<p>As you can easily verify, the following program uses this approach to convolve its input buffer with a Gaussian kernel with support of 766 samples, constructed in just the way Costella describes (as a <i>B</i>-spline), in a single pass over the input buffer, with only six additions and subtractions per sample, not counting the additions and subtractions used for addressing.<p><pre><code>    #include &lt;stdio.h&gt;

    enum {lag = 256, image_size = 4096};

    unsigned long buf[image_size] = {0};

    int
    main(int argc, char **argv)
    {
      buf[image_size&#x2F;2] = 1;

      for (size_t i = 0; i &lt; image_size - 3 * lag - 3; i++) {
        buf[i + 3 * lag + 2] += buf[i + 3 * lag + 1];
        buf[i + 3 * lag + 1] += buf[i + 3 * lag];
        buf[i + 3 * lag] += buf[i + 3 * lag - 1];
        buf[i + 2 * lag] -= buf[i + 3 * lag];
        buf[i + lag] -= buf[i + 2 * lag];
        buf[i] = buf[i + lag] - buf[i];
      }

      for (size_t i = 0; i &lt; image_size - 3 * lag - 3; i++) {
        if (i) printf((i%8) ? &quot;, &quot; : &quot;,\n&quot;);
        printf(&quot;%6ld&quot;, buf[i]);
      }
      printf(&quot;\n&quot;);

      return 0;
    }
</code></pre>
Removing the printf calls, this takes about 162&#x27;677 instruction executions to run, according to Valgrind, which is 39 instructions per pixel.  But most of that is still startup and shutdown; increasing the image size by another 4096 increases the number to 232&#x27;309 instructions, only 69632 more instructions, or 17 instructions per sample.  (printf takes almost two orders of magnitude more time than the blur call.)<p>Adding an outer loop reveals that on my nine-year-old laptop it takes 6.5 μs to run over the whole buffer, or 1.6 nanoseconds per sample.  In theory this performance does not depend on the lag and thus the blur radius, but I&#x27;ve only tested a single lag.<p>The output samples are in some sense scaled by 2²⁴ = 16777216, or by 2¹⁶ = 65536 if you&#x27;re interpolating a signal that&#x27;s being upsampled by zero insertion by a factor of 256.  This scale factor does depend on the lag (it is the cube of the lag).<p>This version computes the results in place starting from output that is already in memory, but you can also use a ring buffer with a capacity of at least 3<i>lag</i> + 2 = 770 samples.  If you&#x27;re <i>downsampling</i>, an alternative is to decimate the samples as soon as they&#x27;ve passed through the cascade of three integrators at the top of the loop, thus reducing both the amount of memory needed and the number of operations to do.<p>Overflow of intermediate results can happen, but it is not a problem as long as the final output is within range, because exact integer addition mod <i>k</i> is a group.  If you were doing this in floating point, you might prefer to interleave the integrators with the comb filters to prevent intermediate overflow and consequent loss of precision leading to numerical instability.<p>Only the parts of the result buffer over which all six pipeline stages have passed contain valid results.  The simplest solution to this is padding, but you can also use special logic to run the integrators but not the combs over the beginning of the input.<p>I would argue that 17 instructions or 1.6 nanoseconds per sample is always &quot;efficient&quot; and &quot;practical&quot; for nearly all &quot;at-scale applications&quot;.  If you were blurring a 3840×2160 display at 120 fps, one gigapixel per second, it would take 13 milliseconds of my laptop CPU cores per second for each of the R, the G, and the B; you could do it on about 6 cores of a modern CPU, without a GPU.