<rss version="2.0"><channel><title>S&#248;ren Sandmann Pedersen</title><link>https://ssp.impulsetrain.com/</link><description>S&#248;ren Sandmann Pedersen</description><item><title>First Class Goto</title><link>https://ssp.impulsetrain.com/goto.html</link><description>&lt;p&gt;&lt;a href="http://cgit.freedesktop.org/~sandmann/oort/"&gt;Oort&lt;/a&gt; is an experimental
programming language I have been working on, on and off (mostly off),
since 2007. It is a statically typed, object-oriented, imperative
language, where classes, functions and methods can be nested
arbitrarily, and where functions and methods are full closures, ie.,
they can be stored in variables and returned from functions. The
control structures are the usual ones: &lt;strong&gt;if&lt;/strong&gt;, &lt;strong&gt;for&lt;/strong&gt;, &lt;strong&gt;while&lt;/strong&gt;,
&lt;strong&gt;do&lt;/strong&gt;, &lt;strong&gt;goto&lt;/strong&gt;, etc.&lt;/p&gt;
&lt;p&gt;It also has an unusual feature: goto labels are &lt;em&gt;first class&lt;/em&gt;.&lt;/p&gt;
&lt;p&gt;What does it mean for labels to be first class? It means two things:
(1) they are lexically scoped so that they are visible from inside
nested functions. This makes it possible to jump from any point in the
program to any other location that is visible from that point, even if
that location is in another function. And (2) labels can be used as
values: They can be passed to and returned from functions and methods,
and they can be stored in data structures.&lt;/p&gt;
&lt;p&gt;As a simple example, consider a data structure with a &amp;ldquo;foreach&amp;rdquo; method
that takes a callback function and calls it for every item in the data
structure. In Oort this might look like this:&lt;/p&gt;
&lt;div class="codehilite"&gt;&lt;pre&gt;&lt;span class="err"&gt;table:&lt;/span&gt; &lt;span class="err"&gt;array[person_t];&lt;/span&gt;

&lt;span class="err"&gt;table.foreach&lt;/span&gt; &lt;span class="err"&gt;(fn&lt;/span&gt; &lt;span class="err"&gt;(p:&lt;/span&gt; &lt;span class="err"&gt;person_t)&lt;/span&gt; &lt;span class="err"&gt;-&amp;gt;&lt;/span&gt; &lt;span class="err"&gt;void&lt;/span&gt; &lt;span class="err"&gt;{&lt;/span&gt;
        &lt;span class="err"&gt;print&lt;/span&gt; &lt;span class="err"&gt;p.name;&lt;/span&gt;
        &lt;span class="err"&gt;print&lt;/span&gt; &lt;span class="err"&gt;p.age;&lt;/span&gt;
    &lt;span class="err"&gt;});&lt;/span&gt;
&lt;/pre&gt;&lt;/div&gt;


&lt;div class="para"&gt;&lt;/div&gt;

&lt;p&gt;A note about syntax. In Oort, anonymous functions are defined like this:&lt;/p&gt;
&lt;div class="codehilite"&gt;&lt;pre&gt;&lt;span class="n"&gt;fn&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="sr"&gt;&amp;lt;arguments&amp;gt;&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="o"&gt;-&amp;gt;&lt;/span&gt; &lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="n"&gt;type&lt;/span&gt;&lt;span class="o"&gt;&amp;gt;&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="o"&gt;...&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/pre&gt;&lt;/div&gt;


&lt;p&gt;and variables and arguments are declared like this:&lt;/p&gt;
&lt;div class="codehilite"&gt;&lt;pre&gt;&lt;span class="sr"&gt;&amp;lt;name&amp;gt;&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="sr"&gt;&amp;lt;type&amp;gt;&lt;/span&gt;
&lt;/pre&gt;&lt;/div&gt;


&lt;p&gt;so the code above defines an anonymous function that prints the name
and the age of person and passes that function to the foreach method
of the table.&lt;/p&gt;
&lt;p&gt;What if we want to stop the iteration? You could have the callback
return &lt;code&gt;true&lt;/code&gt; to stop, or you could have it throw an
exception. However, both methods are a little clumsy: The first
because the return value might be useful for other purposes, the
second because stopping the iteration isn&amp;rsquo;t really an exceptional
situation.&lt;/p&gt;
&lt;p&gt;With lexically scoped labels there is a direct solution &amp;ndash; just use
&lt;code&gt;goto&lt;/code&gt; to jump out of the callback:&lt;/p&gt;
&lt;div class="codehilite"&gt;&lt;pre&gt;  &lt;span class="n"&gt;table&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="k"&gt;foreach&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;fn&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;p:&lt;/span&gt; &lt;span class="n"&gt;person_t&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="o"&gt;-&amp;gt;&lt;/span&gt; &lt;span class="n"&gt;void&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
          &lt;span class="k"&gt;print&lt;/span&gt; &lt;span class="n"&gt;p&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;name&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
          &lt;span class="k"&gt;print&lt;/span&gt; &lt;span class="n"&gt;p&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;age&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;

          &lt;span class="k"&gt;if&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;p&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;age&lt;/span&gt; &lt;span class="o"&gt;&amp;gt;&lt;/span&gt; &lt;span class="mi"&gt;50&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
              &lt;span class="nb"&gt;goto&lt;/span&gt; &lt;span class="n"&gt;done&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
      &lt;span class="p"&gt;});&lt;/span&gt;

&lt;span class="nv"&gt;@done:&lt;/span&gt;
&lt;/pre&gt;&lt;/div&gt;


&lt;p&gt;Note what&amp;rsquo;s going on here: Once we find a person older than 50, we
jump out of the anonymous callback and back into the enclosing
function. The git tree has &lt;a href="http://cgit.freedesktop.org/~sandmann/oort/tree/examples/foreach.nl"&gt;a running
example&lt;/a&gt;.&lt;/p&gt;
&lt;h2&gt;Call/cc in terms of goto&lt;/h2&gt;
&lt;p&gt;In Scheme and some other languages there is a feature called call/cc,
which is famous for being both powerful and mindbending. What it does
is, it takes the concept of &amp;ldquo;where we are in the program&amp;rdquo; and packages
it up as a function. This function, called the &lt;em&gt;continuation&lt;/em&gt;, is then
passed to another, user-defined, function. If the user-defined
function calls the continuation, the program will resume from the
point where call/cc was invoked. The mindbending part is that a
continuation can be stored in data structures and called multiple
times, which means the call/cc invocation can in effect return more
than once.&lt;/p&gt;
&lt;p&gt;Lexically scoped labels are at least as expressive as call/cc, because
if you have them, you can write call/cc as a function:&lt;/p&gt;
&lt;div class="codehilite"&gt;&lt;pre&gt;&lt;span class="n"&gt;call_cc&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;callback:&lt;/span&gt; &lt;span class="n"&gt;fn&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;k:&lt;/span&gt; &lt;span class="n"&gt;fn&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt;&lt;span class="o"&gt;-&amp;gt;&lt;/span&gt;&lt;span class="n"&gt;void&lt;/span&gt;&lt;span class="p"&gt;))&lt;/span&gt; &lt;span class="o"&gt;-&amp;gt;&lt;/span&gt; &lt;span class="n"&gt;void&lt;/span&gt;
&lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="n"&gt;callback&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;fn&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt; &lt;span class="o"&gt;-&amp;gt;&lt;/span&gt; &lt;span class="n"&gt;void&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt; 
        &lt;span class="nb"&gt;goto&lt;/span&gt; &lt;span class="n"&gt;current_continuation&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
    &lt;span class="p"&gt;});&lt;/span&gt;

&lt;span class="nv"&gt;@current_continuation:&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/pre&gt;&lt;/div&gt;


&lt;p&gt;Let&amp;rsquo;s see what&amp;rsquo;s going on here. A function called call_cc() is defined:&lt;/p&gt;
&lt;div class="codehilite"&gt;&lt;pre&gt;&lt;span class="n"&gt;call_cc&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="o"&gt;...&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="o"&gt;-&amp;gt;&lt;/span&gt; &lt;span class="n"&gt;void&lt;/span&gt;
&lt;span class="p"&gt;{&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/pre&gt;&lt;/div&gt;


&lt;p&gt;This function takes another function as argument:&lt;/p&gt;
&lt;div class="codehilite"&gt;&lt;pre&gt;&lt;span class="err"&gt;callback:&lt;/span&gt; &lt;span class="err"&gt;fn&lt;/span&gt; &lt;span class="err"&gt;(...)&lt;/span&gt; &lt;span class="err"&gt;-&amp;gt;&lt;/span&gt; &lt;span class="err"&gt;void&lt;/span&gt;
&lt;/pre&gt;&lt;/div&gt;


&lt;p&gt;And that function takes the continuation as an argument:&lt;/p&gt;
&lt;div class="codehilite"&gt;&lt;pre&gt;&lt;span class="err"&gt;k:&lt;/span&gt; &lt;span class="err"&gt;fn()-&amp;gt;void&lt;/span&gt;
&lt;/pre&gt;&lt;/div&gt;


&lt;p&gt;The body of call/cc calls the callback:&lt;/p&gt;
&lt;div class="codehilite"&gt;&lt;pre&gt;&lt;span class="n"&gt;callback&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="o"&gt;...&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
&lt;/pre&gt;&lt;/div&gt;


&lt;p&gt;passing an anonymous function (the continuation):&lt;/p&gt;
&lt;div class="codehilite"&gt;&lt;pre&gt;    &lt;span class="n"&gt;fn&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt; &lt;span class="o"&gt;-&amp;gt;&lt;/span&gt; &lt;span class="n"&gt;void&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
        &lt;span class="nb"&gt;goto&lt;/span&gt; &lt;span class="n"&gt;current_continuation&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
    &lt;span class="p"&gt;}&lt;/span&gt;

&lt;span class="nv"&gt;@current_continuation:&lt;/span&gt;
&lt;/pre&gt;&lt;/div&gt;


&lt;p&gt;that just jumps to the point where &lt;code&gt;call_cc&lt;/code&gt; returns. So when &lt;code&gt;callback&lt;/code&gt;
decides to invoke the continuation, execution will resume at the point
where &lt;code&gt;call_cc&lt;/code&gt; was invoked. Since there is nothing stopping
&lt;code&gt;callback&lt;/code&gt; from storing the continuation in a data structure or from
invoking it multiple times, we have the full call/cc semantics.&lt;/p&gt;
&lt;h2&gt;Cooperative thread system&lt;/h2&gt;
&lt;p&gt;One of the examples on the &lt;a href="http://en.wikipedia.org/wiki/Call-with-current-continuation"&gt;Wikipedia page about
call/cc&lt;/a&gt;
is a cooperative thread system. With the &lt;code&gt;call_cc&lt;/code&gt; function above, we
could directly translate the Wikipedia code into Oort, but using the
second aspect of the first-class-ness of labels &amp;ndash; that they can be
stored directly in data structures &amp;ndash; makes it possible to write a
more straightforward version:&lt;/p&gt;
&lt;div class="codehilite"&gt;&lt;pre&gt;&lt;span class="err"&gt;run_list:&lt;/span&gt; &lt;span class="err"&gt;list[label]&lt;/span&gt; &lt;span class="err"&gt;=&lt;/span&gt; &lt;span class="err"&gt;new&lt;/span&gt; &lt;span class="err"&gt;list[label]();&lt;/span&gt;

&lt;span class="err"&gt;thread_fork&lt;/span&gt; &lt;span class="err"&gt;(child:&lt;/span&gt; &lt;span class="err"&gt;fn()&lt;/span&gt; &lt;span class="err"&gt;-&amp;gt;&lt;/span&gt; &lt;span class="err"&gt;void)&lt;/span&gt;
&lt;span class="err"&gt;{&lt;/span&gt;
    &lt;span class="err"&gt;run_list.append&lt;/span&gt; &lt;span class="err"&gt;(me);&lt;/span&gt;
    &lt;span class="err"&gt;child();&lt;/span&gt;
    &lt;span class="err"&gt;goto&lt;/span&gt; &lt;span class="err"&gt;run_list.pop_head();&lt;/span&gt;
&lt;span class="err"&gt;@me:&lt;/span&gt;
&lt;span class="err"&gt;}&lt;/span&gt;

&lt;span class="err"&gt;thread_yield()&lt;/span&gt;
&lt;span class="err"&gt;{&lt;/span&gt;
    &lt;span class="err"&gt;run_list.append&lt;/span&gt; &lt;span class="err"&gt;(me);&lt;/span&gt;
    &lt;span class="err"&gt;goto&lt;/span&gt; &lt;span class="err"&gt;run_list.pop_head&lt;/span&gt; &lt;span class="err"&gt;();&lt;/span&gt;
&lt;span class="err"&gt;@me:&lt;/span&gt;
&lt;span class="err"&gt;}&lt;/span&gt;

&lt;span class="err"&gt;thread_exit()&lt;/span&gt;
&lt;span class="err"&gt;{&lt;/span&gt;
    &lt;span class="err"&gt;if&lt;/span&gt; &lt;span class="err"&gt;(!run_list.is_empty())&lt;/span&gt;
        &lt;span class="err"&gt;goto&lt;/span&gt; &lt;span class="err"&gt;run_list.pop_head();&lt;/span&gt;
    &lt;span class="err"&gt;else&lt;/span&gt;
        &lt;span class="err"&gt;process_exit();&lt;/span&gt;
&lt;span class="err"&gt;}&lt;/span&gt;
&lt;/pre&gt;&lt;/div&gt;


&lt;p&gt;The &lt;code&gt;run_list&lt;/code&gt; variable is a list of labels containing the current
positions of all the active threads. The keyword &lt;code&gt;label&lt;/code&gt; in Oort is
simply a type specifier similar to &lt;code&gt;string&lt;/code&gt;.&lt;/p&gt;
&lt;p&gt;To create a new thread, &lt;code&gt;thread_fork&lt;/code&gt; first saves the position of the
current thread on the list, and then it calls the child
function. Similarly, &lt;code&gt;thread_yield&lt;/code&gt; yields to another thread by saving
the position of the current thread and jumping to the first label on
the list. Exiting a thread consists of jumping to the first thread if
there is one, and exiting the process if there isn&amp;rsquo;t.&lt;/p&gt;
&lt;p&gt;The code above doesn&amp;rsquo;t actually run because the current Oort
implementation doesn&amp;rsquo;t support genericity, but
&lt;a href="http://cgit.freedesktop.org/~sandmann/oort/tree/examples/pc.nl"&gt;here&lt;/a&gt;
is a somewhat uglier version that actually runs, while still
demonstrating the principle.&lt;/p&gt;</description><guid>https://ssp.impulsetrain.com/goto.html</guid><pubDate>Sun, 02 Mar 2014 00:00:00 </pubDate></item><item><title>Celebrities die 2.7183 at a time</title><link>https://ssp.impulsetrain.com/celebrities.html</link><description>&lt;p&gt;The claim that celebrities die in threes is usually dismissed as the
result of the human propensity to see patterns where there are
none. But celebrities don&amp;rsquo;t die at regularly spaced intervals
either. It would be very weird if a celebrity predictably died on the
14th of every month. And once you deviate from a regularly spaced
pattern, some amount of clustering is inevitable. Can we make this
more precise?&lt;/p&gt;
&lt;p&gt;Rather than trying to define exactly what constitutes a celebrity,
I&amp;rsquo;ll simply assume that they die at a fixed rate and that they do so
independently of each other (&lt;a href="http://www.geeksofdoom.com/2013/02/03/remembering-february-3-1959-the-day-the-music-died"&gt;The Day the Music
Died&lt;/a&gt;
notwithstanding). It follows that celebrity deaths is a &lt;a href="http://en.wikipedia.org/wiki/Poisson_process"&gt;Poisson
process&lt;/a&gt; with intensity
&lt;mathjax&gt;$\lambda$&lt;/mathjax&gt; where &lt;mathjax&gt;$\lambda$&lt;/mathjax&gt; is the number of deaths that occur in some
fixed time period.&lt;/p&gt;
&lt;p&gt;As an example, suppose we define celebrityhood in such a way that
twelve celebrities die each year on average. Then &lt;mathjax&gt;$\lambda =
12/\text{year}$&lt;/mathjax&gt;, and because the time between events in a Poisson
process is &lt;a href="http://en.wikipedia.org/wiki/Exponential_distribution"&gt;exponentially
distributed&lt;/a&gt;
with parameter &lt;mathjax&gt;$\lambda$&lt;/mathjax&gt;, the average time between two deaths is
&lt;mathjax&gt;$1/\lambda$&lt;/mathjax&gt; = 1/12th year, or one month.&lt;/p&gt;
&lt;p&gt;What does it mean for celebrities to die &lt;mathjax&gt;$n$&lt;/mathjax&gt; at a time? We will simply
say that two celebrities die together if the period between their
deaths is shorter than expected. If the celebrity death rate is
12/year, then two celebrities died together if their deaths were less
than one month apart. Similarly, three celebrities died together if
the period between death 1 and death 2 and the period between death 2
and death 3 were both shorter than a month. In general, &lt;mathjax&gt;$k$&lt;/mathjax&gt;
celebrities died together if the &lt;mathjax&gt;$k - 1$&lt;/mathjax&gt; periods between their deaths
were all shorter than expected.&lt;/p&gt;
&lt;p&gt;Here is a diagram of 10 years worth of randomly generated deaths with
12 deaths per year and clusters as defined above highlighted:&lt;/p&gt;
&lt;p&gt;&lt;img src="celebrities/diagram.png"&gt;&lt;/p&gt;
&lt;h2&gt;Average cluster size&lt;/h2&gt;
&lt;p&gt;Suppose a celebrity has just died after a longer than average
wait. This death will start a new cluster, and we want to figure out
what the size of it is.  &lt;/p&gt;
&lt;p&gt;In a Poisson process the waiting time between two events is
exponentially distributed with parameter &lt;mathjax&gt;$\lambda$&lt;/mathjax&gt;, so it can be
modelled with a stochastic variable &lt;mathjax&gt;$W \sim Exp(\lambda)$&lt;/mathjax&gt;. The cluster
size itself is modelled with another stochastic variable, &lt;mathjax&gt;$C$&lt;/mathjax&gt;, whose
distribution is derived as follows.&lt;/p&gt;
&lt;p&gt;The cluster size will be 1 when the waiting time for the next death is
larger than or equal to the average (which is &lt;mathjax&gt;$1/\lambda$&lt;/mathjax&gt; for the
exponential distribution):&lt;/p&gt;
&lt;blockquote&gt;
&lt;p&gt;&lt;mathjax&gt;$\text{P}(C = 1) = \text{P}(W &amp;gt; 1/\lambda)$&lt;/mathjax&gt;&lt;/p&gt;
&lt;/blockquote&gt;
&lt;p&gt;The probability that the cluster will have size 2 is the same as the
probability that the next waiting time is shorter than average and the
next one after that is longer:&lt;/p&gt;
&lt;blockquote&gt;
&lt;p&gt;&lt;mathjax&gt;$\text{P}(C = 2) = \text{P}(W \le 1/\lambda)\cdot \text{P}(W &amp;gt; 1/\lambda)$&lt;/mathjax&gt;&lt;/p&gt;
&lt;/blockquote&gt;
&lt;p&gt;For size three, it&amp;rsquo;s the probability that the next two waiting times
are shorter and the third one longer:&lt;/p&gt;
&lt;blockquote&gt;
&lt;p&gt;&lt;mathjax&gt;$\text{P}(C = 3) = \text{P}(W \le 1/\lambda)^2\cdot \text{P}(W &amp;gt; 1/\lambda)$&lt;/mathjax&gt;&lt;/p&gt;
&lt;/blockquote&gt;
&lt;p&gt;In general, the probability that the next cluster will be size &lt;mathjax&gt;$k$&lt;/mathjax&gt; is:&lt;/p&gt;
&lt;blockquote&gt;
&lt;p&gt;&lt;mathjax&gt;$\text{P}(C = k) = \text{P}(W \le 1/\lambda)^{k - 1}\cdot \text{P}(W &amp;gt; 1/\lambda)$&lt;/mathjax&gt;&lt;/p&gt;
&lt;/blockquote&gt;
&lt;p&gt;So what&amp;rsquo;s the average size of a Celebrity Death Cluster? The expected
value of &lt;mathjax&gt;$C$&lt;/mathjax&gt; is given by:&lt;/p&gt;
&lt;blockquote&gt;
&lt;p&gt;&lt;mathjax&gt;$\displaystyle \text{E}[C] = \sum_{k=1}^\infty k \cdot \text{P}(C = k) = \sum_{k=1}^\infty  k\cdot \text{P}(W \le 1/\lambda)^{k - 1}\cdot \text{P}(W &amp;gt; 1/\lambda)$&lt;/mathjax&gt;&lt;/p&gt;
&lt;/blockquote&gt;
&lt;p&gt;Plugging in the distribution function for the exponential
distribution, we get:&lt;/p&gt;
&lt;blockquote&gt;
&lt;p&gt;&lt;mathjax&gt;$
\begin{align*} \text{E}[C] &amp;amp;= \sum_{k=1}^\infty k \cdot (1 - e^{- \lambda \cdot (1/\lambda) })^{k - 1} \cdot  (1 - (1 - e^{- \lambda \cdot (1 / \lambda)}))\\
&amp;amp;= \sum_{k=1}^\infty k \cdot (1 - e^{- 1})^{k - 1} \cdot e^{-1}
\end{align*}
$&lt;/mathjax&gt;&lt;/p&gt;
&lt;/blockquote&gt;
&lt;p&gt;It&amp;rsquo;s not hard to show that this infinite series has sum &lt;mathjax&gt;$e$&lt;/mathjax&gt; (Hint: Use
the fact that &lt;mathjax&gt;$k x^{k - 1}$&lt;/mathjax&gt; is the derivative of &lt;mathjax&gt;$x^k$&lt;/mathjax&gt;), so on
average, celebrities die 2.7183 at a time.&lt;/p&gt;</description><guid>https://ssp.impulsetrain.com/celebrities.html</guid><pubDate>Wed, 26 Jun 2013 00:00:00 </pubDate></item><item><title>The Radix Heap</title><link>https://ssp.impulsetrain.com/radix-heap.html</link><description>&lt;p&gt;The &lt;em&gt;Radix Heap&lt;/em&gt; is a priority queue that has better caching behavior
than the well-known &lt;a href="http://en.wikipedia.org/wiki/Binary_Heap"&gt;binary heap&lt;/a&gt;, but also two restrictions: (a)
that all the keys in the heap are integers and (b) that you can never
insert a new item that is smaller than all the other items currently
in the heap.&lt;/p&gt;
&lt;p&gt;These restrictions are not that severe. The Radix Heap still works in
many algorithms that use heaps as a subroutine: Dijkstra&amp;rsquo;s
shortest-path algorithm, Prim&amp;rsquo;s minimum spanning tree algorithm,
various sweepline algorithms in computational geometry.&lt;/p&gt;
&lt;p&gt;Here is how it works. If we assume that the keys are 32 bit integers,
the radix heap will have 33 buckets, each one containing a list of
items. We also maintain one global value &lt;code&gt;last_deleted&lt;/code&gt;, which is
initially &lt;code&gt;MIN_INT&lt;/code&gt; and otherwise contains the last value extracted
from the queue.&lt;/p&gt;
&lt;p&gt;The invariant is this:&lt;/p&gt;
&lt;blockquote&gt;
&lt;p&gt;The items in bucket &lt;mathjax&gt;$k$&lt;/mathjax&gt; differ from &lt;code&gt;last_deleted&lt;/code&gt; in bit &lt;mathjax&gt;$k - 1$&lt;/mathjax&gt;,
  but not in bit &lt;mathjax&gt;$k$&lt;/mathjax&gt; or higher. The items in bucket 0 are equal to
  &lt;code&gt;last_deleted&lt;/code&gt;.&lt;/p&gt;
&lt;/blockquote&gt;
&lt;p&gt;For example, if we compare an item from bucket 10 to &lt;code&gt;last_deleted&lt;/code&gt;,
we will find that bits 31&amp;ndash;10 are equal, bit 9 is different, and bits
8&amp;ndash;0 may or may not be different.&lt;/p&gt;
&lt;p&gt;Here is an example of a radix heap where the last extracted value was
7:&lt;/p&gt;
&lt;blockquote&gt;
&lt;p&gt;&lt;img alt="" src="radix-heap/radix1.png" /&gt;&lt;/p&gt;
&lt;/blockquote&gt;
&lt;p&gt;As an example, consider the item 13 in bucket 4. The bit pattern of 7
is 0111 and the bit pattern of 13 is 1101, so the highest bit that is
different is bit number 3. Therefore the item 13 belongs in bucket &lt;mathjax&gt;$3
+ 1 = 4$&lt;/mathjax&gt;. Buckets 1, 2, and 3 are empty, but that&amp;rsquo;s because a number
that differs from 7 in bits 0, 1, or 2 would be smaller than 7 and so
isn&amp;rsquo;t allowed in the heap according to restriction (b).&lt;/p&gt;
&lt;h2&gt;Operations&lt;/h2&gt;
&lt;p&gt;When a new item is inserted, it has to be added to the correct
bucket. How can we compute the bucket number? We have to find the
highest bit where the new item differs from &lt;code&gt;last_deleted&lt;/code&gt;. This is
easily done by &lt;code&gt;XOR&lt;/code&gt;ing them together and then finding the highest bit
in the result. Adding one then gives the bucket number:&lt;/p&gt;
&lt;div class="codehilite"&gt;&lt;pre&gt;&lt;span class="n"&gt;bucket_no&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;highest_bit&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;new_element&lt;/span&gt; &lt;span class="n"&gt;XOR&lt;/span&gt; &lt;span class="n"&gt;last_deleted&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="o"&gt;+&lt;/span&gt; &lt;span class="mi"&gt;1&lt;/span&gt;
&lt;/pre&gt;&lt;/div&gt;


&lt;p&gt;where &lt;code&gt;highest_bit(x)&lt;/code&gt; is a function that returns the highest set bit
of &lt;code&gt;x&lt;/code&gt;, or &lt;mathjax&gt;$-1$&lt;/mathjax&gt; if &lt;code&gt;x&lt;/code&gt; is 0.&lt;/p&gt;
&lt;p&gt;Inserting the item clearly preserves the invariant because the new
item will be in the correct bucket, and &lt;code&gt;last_deleted&lt;/code&gt; didn&amp;rsquo;t change,
so all the existing items are still in the right place.&lt;/p&gt;
&lt;p&gt;Extracting the minimum involves first finding the minimal item by
walking the lowest-numbered non-empty bucket and finding the minimal
item in that bucket. Then that item is deleted and &lt;code&gt;last_deleted&lt;/code&gt; is
updated. Then the bucket is walked again and all the items are
redistributed into new buckets according to the new &lt;code&gt;last_deleted&lt;/code&gt;
item.&lt;/p&gt;
&lt;p&gt;The extracted item will be the minimal one in the data structure
because we picked the minimal item in the redistributed bucket, and
all the buckets with lower numbers are empty. And if there were a
smaller item in one of the buckets with higher numbers, it would be
differing from &lt;code&gt;last_deleted&lt;/code&gt; in one of the more significant bits, say
bit &lt;mathjax&gt;$k$&lt;/mathjax&gt;. But since the items in the redistributed bucket are equal to
&lt;code&gt;last_deleted&lt;/code&gt; in bit &lt;mathjax&gt;$k$&lt;/mathjax&gt;, the hypothetical smaller item would then
have to also be smaller than &lt;code&gt;last_deleted&lt;/code&gt;, which it can&amp;rsquo;t be because
of restriction (b) mentioned in the introduction. Note that this
argument also works for two-complement signed integers.&lt;/p&gt;
&lt;p&gt;We have to be sure this doesn&amp;rsquo;t violate the invariant. First note that
all the items that are being redistributed will satisfy the invariant
because they are simply being inserted. The items in a bucket with a
higher number &lt;mathjax&gt;$k$&lt;/mathjax&gt; were all different from the old &lt;code&gt;last_deleted&lt;/code&gt; in
the &lt;mathjax&gt;$(k-1)$&lt;/mathjax&gt;th bit. This bit must then necessarily also be different
from the &lt;mathjax&gt;$(k-1)$&lt;/mathjax&gt;th bit in the new &lt;code&gt;last_deleted&lt;/code&gt;, because if it
weren&amp;rsquo;t, the new &lt;code&gt;last_deleted&lt;/code&gt; would itself have belonged in bucket
&lt;mathjax&gt;$k$&lt;/mathjax&gt;. And finally, since the bucket being redistributed is the
lowest-numbered non-empty one, there can&amp;rsquo;t be any items in a bucket
with a lower number. So the invariant still holds.&lt;/p&gt;
&lt;p&gt;In the example above, if we extract the two &amp;lsquo;7&amp;rsquo;s from bucket 0 and the
&amp;lsquo;8&amp;rsquo; from bucket 4, the new heap will look like this:&lt;/p&gt;
&lt;blockquote&gt;
&lt;p&gt;&lt;img alt="" src="radix-heap/radix8.png" /&gt;&lt;/p&gt;
&lt;/blockquote&gt;
&lt;p&gt;Notice that bucket 4, where the &amp;lsquo;8&amp;rsquo; came from, is now empty.&lt;/p&gt;
&lt;h2&gt;Performance&lt;/h2&gt;
&lt;p&gt;Inserting into the radix heap takes constant time because all we have
to do is add the new item to a list. Determining the highest set bit
can be done in constant time with an instruction such as &lt;code&gt;bsr&lt;/code&gt;.&lt;/p&gt;
&lt;p&gt;The performance of extraction is dominated by the redistribution of
items. When a bucket is redistributed, it ends up being empty. To see
why, remember that all the items are different from &lt;code&gt;last_deleted&lt;/code&gt; in
the &lt;mathjax&gt;$(k - 1)$&lt;/mathjax&gt;th bit. Because the new &lt;code&gt;last_deleted&lt;/code&gt; comes from bucket
&lt;mathjax&gt;$k$&lt;/mathjax&gt;, the items are now all &lt;em&gt;equal&lt;/em&gt; to &lt;code&gt;last_deleted&lt;/code&gt; in the &lt;mathjax&gt;$(k -
1)th$&lt;/mathjax&gt; bit. Hence they will all be redistributed to a lower-numbered
bucket.&lt;/p&gt;
&lt;p&gt;Now consider the life-cycle of a single element. In the worst case it
starts out being added to bucket 31 and every time it is
redistributed, it moves to a lower-numbered bucket. When it reaches
bucket 0, it will be next in line for extraction. It follows that the
maximum number of redistributions that an element can experience is
31.&lt;/p&gt;
&lt;p&gt;Since a redistribution takes constant time per element distributed,
and since an element will only be redistributed &lt;mathjax&gt;$d$&lt;/mathjax&gt; times, where &lt;mathjax&gt;$d$&lt;/mathjax&gt;
is the number of bits in the element, it follows that the amortized
time complexity of extraction is &lt;mathjax&gt;$O(d)$&lt;/mathjax&gt;. In practice we will often do
better though, because most items will not move through all the
buckets.&lt;/p&gt;
&lt;h2&gt;Caching performance&lt;/h2&gt;
&lt;p&gt;Some descriptions of the radix heap recommend implementing the buckets
as doubly linked lists, but that would be a mistake because linked
lists have terrible cache locality. It is better to implement them as
dynamically growing arrays. If you do that, the top of the buckets
will tend to be hot which means the per-item number of cache misses
during redistribution of a bucket will tend to be &lt;mathjax&gt;$O(1/B)$&lt;/mathjax&gt;, where &lt;mathjax&gt;$B$&lt;/mathjax&gt;
is the number of integers in a cache line. This means the amortized
cache-miss complexity of extraction will be closer to &lt;mathjax&gt;$O(d/B)$&lt;/mathjax&gt; than to
&lt;mathjax&gt;$O(d)$&lt;/mathjax&gt;.&lt;/p&gt;
&lt;p&gt;In a regular binary heap, both insertion and extraction require
&lt;mathjax&gt;$\Theta(\log n)$&lt;/mathjax&gt; swaps in the worst case, and each swap (except for
those very close to the top of the heap) will cause a cache miss.&lt;/p&gt;
&lt;p&gt;In other words, if &lt;mathjax&gt;$d = \Theta(\log n)$&lt;/mathjax&gt;, extraction from a radix heap will
tend to generate &lt;mathjax&gt;$\Theta(\log n / B)$&lt;/mathjax&gt; cache misses, where a binary heap will
require &lt;mathjax&gt;$\Theta(\log n)$&lt;/mathjax&gt;.&lt;/p&gt;
&lt;!--
Surprisingly, the English-language Wikipedia doesn't have an article
on radix heaps. If someone wants to fix that, feel free to use any
material in this post under whatever license is useful to that end.
--&gt;</description><guid>https://ssp.impulsetrain.com/radix-heap.html</guid><pubDate>Sat, 25 May 2013 00:00:00 </pubDate></item><item><title>Porter/Duff Compositing and Blend Modes</title><link>https://ssp.impulsetrain.com/porterduff.html</link><description>&lt;p&gt;In the Porter/Duff compositing algebra, images are equipped with an
alpha channel that determines on a per-pixel basis whether the image
is there or not. When the alpha channel is 1, the image is fully
there, when it is 0, the image isn&amp;rsquo;t there at all, and when it is in
between, the image is partially there. In other words, the alpha
channel describes the &lt;em&gt;shape&lt;/em&gt; of the image, it does not describe
opacity. The way to think of images with an alpha channel is as
irregularly shaped pieces of cardboard, not as colored glass.&lt;/p&gt;
&lt;p&gt;Consider these two images:&lt;/p&gt;
&lt;blockquote&gt;
&lt;p&gt;&lt;img src="porterduff/source.png"/&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;
&lt;img src="porterduff/dest.png"/&gt;&lt;/p&gt;
&lt;/blockquote&gt;
&lt;p&gt;When we combine them, each pixel of the result can be divided into four regions:&lt;/p&gt;
&lt;blockquote&gt;
&lt;p&gt;&lt;img src="porterduff/diagram.png"&gt;&lt;/p&gt;
&lt;/blockquote&gt;
&lt;p&gt;One region where only the source is present, one where only the
destination is present, one where both are present, and one where
neither is present.&lt;/p&gt;
&lt;p&gt;By deciding on what happens in each of the four regions, various
effects can be generated. For example, if the destination-only region
is treated as blank, the source-only region is filled with the source
color, and the &amp;lsquo;both&amp;rsquo; region is filled with the destination color like
this:&lt;/p&gt;
&lt;blockquote&gt;
&lt;p&gt;&lt;img src="porterduff/destatop-diagram.png"/&gt;&lt;/p&gt;
&lt;/blockquote&gt;
&lt;p&gt;The effect is as if the destination image is trimmed to match the
source image, and then held up in front of it:&lt;/p&gt;
&lt;blockquote&gt;
&lt;p&gt;&lt;img src="porterduff/destatop.png"/&gt;&lt;/p&gt;
&lt;/blockquote&gt;
&lt;p&gt;The Porter/Duff operator that does this is called &amp;ldquo;Dest Atop&amp;rdquo;.&lt;/p&gt;
&lt;p&gt;There are twelve of these operators, each one characterized by its
behavior in the three regions: source, destination and both. The
&amp;lsquo;neither&amp;rsquo; region is always blank. The source and destination regions
can either be blank or filled with the source or destination colors
respectively.&lt;/p&gt;
&lt;p&gt;The formula for the operators is a linear combination of the contents
of the four regions, where the weights are the areas of each region:&lt;/p&gt;
&lt;blockquote&gt;
&lt;p&gt;&lt;mathjax&gt;$A_\text{src} \cdot [s] + A_\text{dest} \cdot [d] +  A_\text{both} \cdot [b]$&lt;/mathjax&gt;&lt;/p&gt;
&lt;/blockquote&gt;
&lt;p&gt;Where &lt;mathjax&gt;$[s]$&lt;/mathjax&gt; is either 0 or the color of the source pixel, &lt;mathjax&gt;$[d]$&lt;/mathjax&gt;
either 0 or the color of the destination pixel, and &lt;mathjax&gt;$[b]$&lt;/mathjax&gt; is either
0, the color of the source pixel, or the color of the destination
pixel. With the alpha channel being interpreted as coverage, the areas
are given by these formulas:&lt;/p&gt;
&lt;blockquote&gt;
&lt;p&gt;&lt;mathjax&gt;$A_\text{src} = \alpha_\text{s} \cdot (1 - \alpha_\text{d})\\
A_\text{dst} = \alpha_\text{d} \cdot (1 - \alpha_\text{s})\\
A_\text{both} = \alpha_\text{s} \cdot \alpha_\text{d}$&lt;/mathjax&gt;&lt;/p&gt;
&lt;/blockquote&gt;
&lt;p&gt;The alpha channel of the result is computed in a similar way:&lt;/p&gt;
&lt;blockquote&gt;
&lt;p&gt;&lt;mathjax&gt;$A_\text{src} \cdot [\text{as}] + A_\text{dest} \cdot [\text{ad}] + A_\text{both} \cdot [\text{ab}]$&lt;/mathjax&gt;&lt;/p&gt;
&lt;/blockquote&gt;
&lt;p&gt;where &lt;mathjax&gt;$[\text{as}]$&lt;/mathjax&gt; and &lt;mathjax&gt;$[\text{ad}]$&lt;/mathjax&gt; are either 0 or 1 depending
on whether the source and destination regions are present, and where
&lt;mathjax&gt;$[\text{ab}]$&lt;/mathjax&gt; is 0 when the &amp;lsquo;both&amp;rsquo; region is blank, and 1 otherwise.&lt;/p&gt;
&lt;p&gt;Here is a table of all the Porter/Duff operators:&lt;/p&gt;
&lt;table width="100%"&gt;
&lt;tr&gt;&lt;td/&gt;&lt;td&gt;$[\text{s}]$&lt;/td&gt;&lt;td&gt;$[\text{d}]$&lt;/td&gt;&lt;td&gt;$[\text{b}]$&lt;/td&gt;&lt;/tr&gt;
&lt;tr&gt;&lt;td&gt;Src&lt;/td&gt;&lt;td&gt;$s$&lt;/td&gt;&lt;td&gt;$0$&lt;/td&gt;&lt;td&gt;s&lt;/td&gt;&lt;/tr&gt;
&lt;tr&gt;&lt;td&gt;Atop&lt;/td&gt;&lt;td&gt;$0$&lt;/td&gt;&lt;td&gt;$d$&lt;/td&gt;&lt;td&gt;s&lt;/td&gt;&lt;/tr&gt;
&lt;tr&gt;&lt;td&gt;Over&lt;/td&gt;&lt;td&gt;$s$&lt;/td&gt;&lt;td&gt;$d$&lt;/td&gt;&lt;td&gt;s&lt;/td&gt;&lt;/tr&gt;
&lt;tr&gt;&lt;td&gt;In&lt;/td&gt;&lt;td&gt;$0$&lt;/td&gt;&lt;td&gt;$0$&lt;/td&gt;&lt;td&gt;s&lt;/td&gt;&lt;/tr&gt;
&lt;tr&gt;&lt;td&gt;Out&lt;/td&gt;&lt;td&gt;$s$&lt;/td&gt;&lt;td&gt;$0$&lt;/td&gt;&lt;td&gt;$0$&lt;/td&gt;&lt;/tr&gt;
&lt;tr&gt;&lt;td&gt;Dest&lt;/td&gt;&lt;td&gt;$0$&lt;/td&gt;&lt;td&gt;$d$&lt;/td&gt;&lt;td&gt;d&lt;/td&gt;&lt;/tr&gt;
&lt;tr&gt;&lt;td&gt;DestAtop&lt;/td&gt;&lt;td&gt;$s$&lt;/td&gt;&lt;td&gt;$0$&lt;/td&gt;&lt;td&gt;d&lt;/td&gt;&lt;/tr&gt;
&lt;tr&gt;&lt;td&gt;DestOver&lt;/td&gt;&lt;td&gt;$s$&lt;/td&gt;&lt;td&gt;$d$&lt;/td&gt;&lt;td&gt;d&lt;/td&gt;&lt;/tr&gt;
&lt;tr&gt;&lt;td&gt;DestIn&lt;/td&gt;&lt;td&gt;$0$&lt;/td&gt;&lt;td&gt;$0$&lt;/td&gt;&lt;td&gt;d&lt;/td&gt;&lt;/tr&gt;
&lt;tr&gt;&lt;td&gt;DestOut&lt;/td&gt;&lt;td&gt;$0$&lt;/td&gt;&lt;td&gt;$d$&lt;/td&gt;&lt;td&gt;$0$&lt;/td&gt;&lt;/tr&gt;
&lt;tr&gt;&lt;td&gt;Clear&lt;/td&gt;&lt;td&gt;$0$&lt;/td&gt;&lt;td&gt;$0$&lt;/td&gt;&lt;td&gt;$0$&lt;/td&gt;&lt;/tr&gt;
&lt;tr&gt;&lt;td&gt;Xor&lt;/td&gt;&lt;td&gt;$s$&lt;/td&gt;&lt;td&gt;$d$&lt;/td&gt;&lt;td&gt;$0$&lt;/td&gt;&lt;/tr&gt;
&lt;/table&gt;

&lt;p&gt;And here is how they look:&lt;/p&gt;
&lt;p&gt;&lt;img src="porterduff/table.png"/&gt;&lt;/p&gt;
&lt;p&gt;Despite being referred to as alpha blending and despite alpha often
being used to model opacity, in concept Porter/Duff is not a way to
blend the source and destination shapes. It is way to overlay, combine
and trim them as if they were pieces of cardboard. The only place
where source and destination pixels are actually &lt;em&gt;blended&lt;/em&gt; is along
the antialiased edges.&lt;/p&gt;
&lt;h2&gt;Blending&lt;/h2&gt;
&lt;p&gt;Photoshop and the Gimp have a concept of layers which are images
stacked on top of each other. In Porter/Duff, stacking images on top
of each other is done with the &amp;ldquo;Over&amp;rdquo; operator, which is also what
Photoshop/Gimp use by default to composite layers:&lt;/p&gt;
&lt;blockquote&gt;
&lt;p&gt;&lt;img src="porterduff/over-diagram.png"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;
&lt;img src="porterduff/over.png"&gt;&lt;/p&gt;
&lt;/blockquote&gt;
&lt;p&gt;Conceptually, two pieces of cardboard are held up with one in front of
the other. Neither shape is trimmed, and in places where both are
present, only the top layer is visible.&lt;/p&gt;
&lt;p&gt;A layer in these programs also has an associated &lt;em&gt;Blend Mode&lt;/em&gt; which
can be used to modify what happens in places where both are
visible. For example, the &amp;lsquo;Color Dodge&amp;rsquo; blend mode computes a mix of
source and destination according to this formula:&lt;/p&gt;
&lt;blockquote&gt;
&lt;p&gt;&lt;mathjax&gt;$
\begin{equation*}
B(s,d)=
\begin{cases} 0 &amp;amp; \text{if \(d=0\),}
\\
1 &amp;amp; \text{if \(d \ge (1 - s)\),}
\\
d / (1 - s) &amp;amp; \text{otherwise}
\end{cases}
\end{equation*}
$&lt;/mathjax&gt;&lt;/p&gt;
&lt;/blockquote&gt;
&lt;p&gt;The result is this:&lt;/p&gt;
&lt;blockquote&gt;
&lt;p&gt;&lt;img src="porterduff/colordodge-diagram.png"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;
&lt;img src="porterduff/colordodge-both.png"&gt;&lt;/p&gt;
&lt;/blockquote&gt;
&lt;p&gt;Unlike with the regular Over operator, in this case there is a
substantial chunk of the output where the result is actually a mix of
the source and destination.&lt;/p&gt;
&lt;p&gt;Layers in Photoshop and Gimp are not tailored to each other (except
for layer masks, which we will ignore here), so the compositing of the
layer stack is done with the source-only and destination-only region
set to source and destination respectively. However, there is nothing
in principle stopping us from setting the source-only and
destination-only regions to blank, but keeping the blend mode in the
&amp;lsquo;both&amp;rsquo; region, so that tailoring could be supported alongside
blending. For example, we could set the &amp;lsquo;source&amp;rsquo; region to blank, the
&amp;lsquo;destination&amp;rsquo; region to the destination color, and the &amp;lsquo;both&amp;rsquo; region
to ColorDodge:&lt;/p&gt;
&lt;blockquote&gt;
&lt;p&gt;&lt;img src="porterduff/colordodge-dest-diagram.png"/&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;
&lt;img src="porterduff/colordodge-dest.png"&gt;&lt;/p&gt;
&lt;/blockquote&gt;
&lt;p&gt;Here are the four combinations that involve a ColorDodge blend mode:&lt;/p&gt;
&lt;blockquote&gt;
&lt;p&gt;&lt;nobr&gt;&lt;img src="porterduff/colordodge-none.png"/&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&lt;/nobr&gt;
&lt;nobr&gt;&lt;img src="porterduff/colordodge-source.png"/&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&lt;/nobr&gt;
&lt;nobr&gt;&lt;img src="porterduff/colordodge-dest.png"/&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&lt;/nobr&gt;
&lt;nobr&gt;&lt;img src="porterduff/colordodge-both.png"/&gt;&lt;/nobr&gt;&lt;/p&gt;
&lt;/blockquote&gt;
&lt;p&gt;In this model the original twelve Porter/Duff operators can be viewed
as the results of three simple blend modes:&lt;/p&gt;
&lt;table&gt;
&lt;tr&gt;&lt;td&gt;Source:&lt;/td&gt;&lt;td&gt;$B(s, d) = s$&lt;/td&gt;&lt;/tr&gt;
&lt;tr&gt;&lt;td&gt;Dest:&lt;/td&gt;&lt;td&gt;$B(s, d) = d$&lt;/td&gt;&lt;/tr&gt;
&lt;tr&gt;&lt;td&gt;Zero:&lt;/td&gt;&lt;td&gt;$B(s, d) = 0$&lt;/td&gt;&lt;/tr&gt;
&lt;/table&gt;

&lt;p&gt;In this generalization of Porter/Duff the blend mode is chosen from a
large set of formulas, and each formula gives rise to four new
compositing operators characterized by whether the source and
destination are blank or contain the corresponding pixel color.&lt;/p&gt;
&lt;p&gt;Here is a table of the operators that are generated by various blend
modes:&lt;/p&gt;
&lt;p&gt;&lt;img src="porterduff/colordodge-table.png"/&gt;&lt;/p&gt;
&lt;p&gt;The general formula is still an area weighted average:&lt;/p&gt;
&lt;blockquote&gt;
&lt;p&gt;&lt;mathjax&gt;$A_\text{src} \cdot [s] + A_\text{dest} \cdot [d] + A_\text{both}\cdot B(s, d)$&lt;/mathjax&gt;&lt;/p&gt;
&lt;/blockquote&gt;
&lt;p&gt;where [s] and [d] are the source and destination colors respectively
or 0, but where &lt;mathjax&gt;$B(s, d)$&lt;/mathjax&gt; is no longer restricted to one of &lt;mathjax&gt;$0$&lt;/mathjax&gt;, &lt;mathjax&gt;$s$&lt;/mathjax&gt;,
and &lt;mathjax&gt;$d$&lt;/mathjax&gt;, but can instead be chosen from a large set of formulas.&lt;/p&gt;
&lt;p&gt;The output of the alpha channel is the same as before:&lt;/p&gt;
&lt;blockquote&gt;
&lt;p&gt;&lt;mathjax&gt;$A_\text{src} \cdot [\text{as}] + A_\text{dest} \cdot [\text{ad}] +
A_\text{both} \cdot [\text{ab}]$&lt;/mathjax&gt;&lt;/p&gt;
&lt;/blockquote&gt;
&lt;p&gt;except that [ab] is now determined by the blend mode. For the Zero
blend mode there is no coverage in the both region, so [ab] is 0; for
most others, there is full coverage, so [ab] is 1.&lt;/p&gt;</description><guid>https://ssp.impulsetrain.com/porterduff.html</guid><pubDate>Sun, 17 Mar 2013 00:00:00 </pubDate></item><item><title>Big-O Misconceptions</title><link>https://ssp.impulsetrain.com/big-o.html</link><description>&lt;p&gt;In computer science and sometimes mathematics, big-O notation is used
to talk about how quickly a function grows while disregarding
multiplicative and additive constants. When classifying algorithms,
big-O notation is useful because it lets us abstract away the
differences between real computers as just multiplicative and additive
constants.&lt;/p&gt;
&lt;p&gt;Big-O is not a difficult concept at all, but it seems to be common
even for people who should know better to misunderstand some aspects
of it. The following is a list of misconceptions that I have seen in
the wild.&lt;/p&gt;
&lt;p&gt;But first a definition: We write&lt;/p&gt;
&lt;blockquote&gt;
&lt;p&gt;&lt;mathjax&gt;$f(n) = O(g(n))$&lt;/mathjax&gt;&lt;/p&gt;
&lt;/blockquote&gt;
&lt;p&gt;when &lt;mathjax&gt;$f(n) \le M g(n)$&lt;/mathjax&gt; for sufficiently large &lt;mathjax&gt;$n$&lt;/mathjax&gt;, for some positive constant &lt;mathjax&gt;$M$&lt;/mathjax&gt;.&lt;/p&gt;
&lt;h2&gt;Misconception 1: &amp;ldquo;The Equals Sign Means Equality&amp;rdquo;&lt;/h2&gt;
&lt;p&gt;The equals sign in&lt;/p&gt;
&lt;blockquote&gt;
&lt;p&gt;&lt;mathjax&gt;$f(n) = O(g(n))$&lt;/mathjax&gt;&lt;/p&gt;
&lt;/blockquote&gt;
&lt;p&gt;is a widespread travestry. If you take it at face value, you can
deduce that since &lt;mathjax&gt;$5 n$&lt;/mathjax&gt; and &lt;mathjax&gt;$3 n$&lt;/mathjax&gt; are both equal to &lt;mathjax&gt;$O(n)$&lt;/mathjax&gt;, then &lt;mathjax&gt;$3 n$&lt;/mathjax&gt;
must be equal to &lt;mathjax&gt;$5 n$&lt;/mathjax&gt; and so &lt;mathjax&gt;$3 = 5$&lt;/mathjax&gt;.&lt;/p&gt;
&lt;p&gt;The expression &lt;mathjax&gt;$f(n) = O(g(n))$&lt;/mathjax&gt; doesn&amp;rsquo;t type check. The left-hand-side
is a function, the right-hand-side is a &amp;#8230; what, exactly? There is no
help to be found in the definition. It just says &amp;ldquo;we write&amp;rdquo; without
concerning itself with the fact that what &amp;ldquo;we write&amp;rdquo; is total
nonsense.&lt;/p&gt;
&lt;p&gt;The way to interpret the right-hand side is as a &lt;em&gt;set&lt;/em&gt; of functions:&lt;/p&gt;
&lt;blockquote&gt;
&lt;p&gt;&lt;mathjax&gt;$ O(f) = \{ g \mid g(n) \le M f(n) \text{ for some \(M &amp;gt; 0\) for large \(n\)}\}. $&lt;/mathjax&gt;&lt;/p&gt;
&lt;/blockquote&gt;
&lt;p&gt;With this definition, the world makes sense again: If &lt;mathjax&gt;$f(n) = 3 n$&lt;/mathjax&gt;
and &lt;mathjax&gt;$g(n) = 5 n$&lt;/mathjax&gt;, then &lt;mathjax&gt;$f \in O(n)$&lt;/mathjax&gt; and &lt;mathjax&gt;$g \in O(n)$&lt;/mathjax&gt;, but there
is no equality involved so we can&amp;rsquo;t make bogus deductions like
&lt;mathjax&gt;$3=5$&lt;/mathjax&gt;. We can however make the correct observation that &lt;mathjax&gt;$O(n)
\subseteq O(n \log n)\subseteq O(n^2) \subseteq O(n^3)$&lt;/mathjax&gt;, something
that would be difficult to express with the equals sign.&lt;/p&gt;
&lt;h2&gt;Misconception 2: &amp;ldquo;Informally, Big-O Means &amp;lsquo;Approximately Equal&amp;rsquo;"&lt;/h2&gt;
&lt;p&gt;If an algorithm takes &lt;mathjax&gt;$5 n^2$&lt;/mathjax&gt; seconds to complete, that algorithm is
&lt;mathjax&gt;$O(n^2)$&lt;/mathjax&gt; because for the constant &lt;mathjax&gt;$M=7$&lt;/mathjax&gt; and sufficiently large &lt;mathjax&gt;$n$&lt;/mathjax&gt;, &lt;mathjax&gt;$5
n^2 \le 7 n^2$&lt;/mathjax&gt;. But an algorithm that runs in constant time, say 3
seconds, is also &lt;mathjax&gt;$O(n^2)$&lt;/mathjax&gt; because for sufficiently large &lt;mathjax&gt;$n$&lt;/mathjax&gt;, &lt;mathjax&gt;$3 \le
n^2$&lt;/mathjax&gt;.&lt;/p&gt;
&lt;p&gt;So informally, big-O means &lt;em&gt;approximately less than or equal&lt;/em&gt;,
not &lt;em&gt;approximately equal&lt;/em&gt;.&lt;/p&gt;
&lt;p&gt;If someone says &amp;ldquo;Topological Sort, like other sorting algorithms, is
&lt;mathjax&gt;$O(n \log n)$&lt;/mathjax&gt;", then that is &lt;em&gt;technically&lt;/em&gt; correct, but severely
misleading, because Toplogical Sort is also &lt;mathjax&gt;$O(n)$&lt;/mathjax&gt; which is a subset
of &lt;mathjax&gt;$O(n \log n)$&lt;/mathjax&gt;. Chances are whoever said it meant something false.&lt;/p&gt;
&lt;p&gt;If someone says &amp;ldquo;In the worst case, any comparison based sorting
algorithm must make &lt;mathjax&gt;$O(n \log n)$&lt;/mathjax&gt; comparisons&amp;rdquo; that is &lt;em&gt;not&lt;/em&gt; a
correct statement. Translated into English it becomes:&lt;/p&gt;
&lt;blockquote&gt;
&lt;p&gt;&amp;ldquo;In the worst case, any comparison based sorting algorithm must make
fewer than or equal to &lt;mathjax&gt;$M n \log (n)$&lt;/mathjax&gt; comparisons&amp;rdquo;&lt;/p&gt;
&lt;/blockquote&gt;
&lt;p&gt;which is not true: You can easily come up with a comparison based
sorting algorithm that makes more comparisons in the worst case.&lt;/p&gt;
&lt;p&gt;To be precise about these things we have other types of notation at
our disposal. Informally:&lt;/p&gt;
&lt;blockquote&gt;
&lt;p&gt;&lt;table&gt;
&lt;tr&gt;&lt;td&gt;&lt;mathjax&gt;$O()$&lt;/mathjax&gt;:&lt;/td&gt;&lt;td&gt;Less than or equal, disregarding constants&lt;/td&gt;&lt;/tr&gt;
&lt;tr&gt;&lt;td&gt;&lt;mathjax&gt;$\Omega()$&lt;/mathjax&gt;:&lt;/td&gt;&lt;td&gt;Greater than or equal, disregarding constants&lt;/td&gt;&lt;/tr&gt;
&lt;tr&gt;&lt;td&gt;&lt;mathjax&gt;$o()$&lt;/mathjax&gt;:&lt;/td&gt;&lt;td&gt;Stricly less than, disregarding constants&lt;/td&gt;&lt;/tr&gt;
&lt;tr&gt;&lt;td&gt;&lt;mathjax&gt;$\Theta()$&lt;/mathjax&gt;:&lt;/td&gt;&lt;td&gt;Equal to, disregarding constants&lt;/td&gt;&lt;/tr&gt;
&lt;/table&gt;&lt;/p&gt;
&lt;/blockquote&gt;
&lt;p&gt;and &lt;a href="http://en.wikipedia.org/wiki/Big_O_notation#Family_of_Bachmann.E2.80.93Landau_notations"&gt;some more&lt;/a&gt;.
The correct statement about lower bounds is this: &amp;ldquo;In the worst case,
any comparison based sorting algorithm must make &lt;mathjax&gt;$\Omega(n \log n)$&lt;/mathjax&gt;
comparisons&amp;rdquo;. In English that becomes:&lt;/p&gt;
&lt;blockquote&gt;
&lt;p&gt;&amp;ldquo;In the worst case, any comparison based sorting algorithm must make
at least &lt;mathjax&gt;$M n \log (n)$&lt;/mathjax&gt; comparisons&amp;rdquo;&lt;/p&gt;
&lt;/blockquote&gt;
&lt;p&gt;which is true. And a correct, non-misleading statement about
Topological Sort is that it is &lt;mathjax&gt;$\Theta(n)$&lt;/mathjax&gt;, because it has a lower
bound of &lt;mathjax&gt;$\Omega(n)$&lt;/mathjax&gt; and an upper bound of &lt;mathjax&gt;$O(n)$&lt;/mathjax&gt;.&lt;/p&gt;
&lt;h2&gt;Misconception 3: &amp;ldquo;Big-O is a Statement About Time&amp;rdquo;&lt;/h2&gt;
&lt;p&gt;Big-O is used for making statements about functions. The functions can
measure time or space or cache misses or rabbits on an island or
anything or nothing. Big-O notation doesn&amp;rsquo;t care.&lt;/p&gt;
&lt;p&gt;In fact, when used for algorithms, big-O is almost never about
time. It is about primitive operations.&lt;/p&gt;
&lt;p&gt;When someone says that the time complexity of MergeSort is &lt;mathjax&gt;$O(n \log
n)$&lt;/mathjax&gt;, they usually mean that the number of comparisons that MergeSort
makes is &lt;mathjax&gt;$O(n \log n)$&lt;/mathjax&gt;. That in itself doesn&amp;rsquo;t tell us what the &lt;em&gt;time&lt;/em&gt;
complexity of any particular MergeSort might be because that would
depend how much time it takes to make a comparison. In other words,
the &lt;mathjax&gt;$O(n \log n)$&lt;/mathjax&gt; refers to &lt;em&gt;comparisons&lt;/em&gt; as the primitive operation.&lt;/p&gt;
&lt;p&gt;The important point here is that when big-O is applied to algorithms,
there is always an underlying model of computation. The claim that the
&lt;em&gt;time&lt;/em&gt; complexity of MergeSort is &lt;mathjax&gt;$O(n \log n)$&lt;/mathjax&gt;, is implicitly
referencing a model of computation where a comparison takes constant
time and everything else is free.&lt;/p&gt;
&lt;p&gt;Which is fine as far as it goes. It lets us compare MergeSort to other
comparison based sorts, such as QuickSort or ShellSort or BubbleSort,
and in many real situations, comparing two sort keys really does take
constant time.&lt;/p&gt;
&lt;p&gt;However, it doesn&amp;rsquo;t allow us to compare MergeSort to RadixSort because
RadixSort is not comparison based. It simply doesn&amp;rsquo;t ever make a
comparison between two keys, so its time complexity in the comparison
model is 0. The statement that RadixSort is &lt;mathjax&gt;$O(n)$&lt;/mathjax&gt; implicitly
references a model in which the keys can be lexicographically picked
apart in constant time. Which is also fine, because in many real
situations, you actually can do that.&lt;/p&gt;
&lt;p&gt;To compare RadixSort to MergeSort, we must first define a shared model
of computation. If we are sorting strings that are &lt;mathjax&gt;$k$&lt;/mathjax&gt; bytes long, we
might take &amp;ldquo;read a byte&amp;rdquo; as a primitive operation that takes constant
time with everything else being free.&lt;/p&gt;
&lt;p&gt;In this model, MergeSort makes &lt;mathjax&gt;$O(n \log n)$&lt;/mathjax&gt; string comparisons each
of which makes &lt;mathjax&gt;$O(k)$&lt;/mathjax&gt; byte comparisons, so the time complexity is
&lt;mathjax&gt;$O(k\cdot n \log n)$&lt;/mathjax&gt;. One common implementation of RadixSort will make
&lt;mathjax&gt;$k$&lt;/mathjax&gt; passes over the &lt;mathjax&gt;$n$&lt;/mathjax&gt; strings with each pass reading one byte, and
so has time complexity &lt;mathjax&gt;$O(n k)$&lt;/mathjax&gt;.&lt;/p&gt;
&lt;h2&gt;Misconception 4: &amp;ldquo;Big-O Is About Worst Case&amp;rdquo;&lt;/h2&gt;
&lt;p&gt;Big-O is often used to make statements about functions that measure
the worst case behavior of an algorithm, but big-O notation doesn&amp;rsquo;t
imply anything of the sort.&lt;/p&gt;
&lt;p&gt;If someone is talking about the randomized QuickSort and says that it
is &lt;mathjax&gt;$O(n \log n)$&lt;/mathjax&gt;, they presumably mean that its &lt;em&gt;expected running
time&lt;/em&gt; is &lt;mathjax&gt;$O(n \log n)$&lt;/mathjax&gt;. If they say that QuickSort is &lt;mathjax&gt;$O(n^2)$&lt;/mathjax&gt; they
are probably talking about its worst case complexity. Both statements
can be considered true depending on what type of running time the
functions involved are measuring.&lt;/p&gt;</description><guid>https://ssp.impulsetrain.com/big-o.html</guid><pubDate>Mon, 15 Oct 2012 00:00:00 </pubDate></item><item><title>Sysprof 1.2.0</title><link>https://ssp.impulsetrain.com/sysprof-1.2.0.html</link><description>&lt;p&gt;A &lt;a href="https://lkml.org/lkml/2012/9/8/143"&gt;new stable release&lt;/a&gt; of &lt;a href="http://sysprof.com/"&gt;Sysprof&lt;/a&gt; is now available. Download
&lt;a href="http://sysprof.com/sysprof-1.2.0.tar.gz"&gt;version 1.2.0&lt;/a&gt;.&lt;/p&gt;</description><guid>https://ssp.impulsetrain.com/sysprof-1.2.0.html</guid><pubDate>Sat, 08 Sep 2012 00:00:00 </pubDate></item><item><title>Over is not Translucency</title><link>https://ssp.impulsetrain.com/translucency.html</link><description>&lt;p&gt;The &lt;a href="https://ssp.impulsetrain.com/porterduff.html"&gt;Porter/Duff&lt;/a&gt; Over
operator, also known as the &amp;ldquo;Normal&amp;rdquo; blend mode in Photoshop, computes
the amount of light that is reflected when a pixel partially covers
another:&lt;/p&gt;
&lt;blockquote&gt;
&lt;p&gt;&lt;img alt="The Porter/Duff OVER operator" src="translucency/bg-fg.png" /&gt;&lt;/p&gt;
&lt;/blockquote&gt;
&lt;p&gt;The fraction of bg that is covered is denoted alpha. This operator is
the correct one to use when the foreground image is an opaque mask
that partially covers the background:&lt;/p&gt;
&lt;p&gt;&lt;img alt="Red mask on blue background" src="translucency/big-over1.png" /&gt;&lt;/p&gt;
&lt;p&gt;A photon that hits this image will be reflected back to your eyes by
either the foreground or the background, but not both. For each
foreground pixel, the alpha value tells us the probability of each:&lt;/p&gt;
&lt;blockquote&gt;
&lt;p&gt;&lt;mathjax&gt;$a \cdot \text{fg} + (1 - a) \cdot \text{bg}$&lt;/mathjax&gt;&lt;/p&gt;
&lt;/blockquote&gt;
&lt;p&gt;This is the definition of the Porter/Duff Over operator for
non-premultiplied pixels.&lt;/p&gt;
&lt;p&gt;But if alpha is interpreted as &lt;em&gt;translucency,&lt;/em&gt; then the Over operator
is not the correct one to use. The Over operator will act as if each
pixel is partially covering the background:&lt;/p&gt;
&lt;p&gt;&lt;img alt="" src="translucency/shaped-over.png" /&gt;&lt;/p&gt;
&lt;p&gt;Which is not how translucency works. A translucent material reflects
some light and lets other light through. The light that is let through
is reflected by the background and &lt;em&gt;interacts with the foreground
again&lt;/em&gt;.&lt;/p&gt;
&lt;p&gt;&lt;img style="margin-left: 28px; margin-bottom: 28px" align="right" src="translucency/Translucency.png" alt="" width="256"
height="329"/&gt;Let&amp;rsquo;s look at this in more detail. Please follow along
in the diagram to the right. First with probability &lt;mathjax&gt;$a$&lt;/mathjax&gt;, the
photon is reflected back towards the viewer:&lt;/p&gt;
&lt;blockquote&gt;
&lt;p&gt;&lt;mathjax&gt;$\displaystyle
\begin{align*}
&amp;amp;a \cdot \text{fg}
\end{align*}
$&lt;/mathjax&gt;&lt;/p&gt;
&lt;/blockquote&gt;
&lt;p&gt;With probability &lt;mathjax&gt;$(1 - a)$&lt;/mathjax&gt;, it passes through the foreground, hits the
background, and is reflected back out. The photon now hits the
&lt;em&gt;backside&lt;/em&gt; of the foreground pixel. With probability &lt;mathjax&gt;$(1 - a)$&lt;/mathjax&gt;, the
foreground pixel lets the photon back out to the viewer. The result so
far:&lt;/p&gt;
&lt;blockquote&gt;
&lt;p&gt;&lt;mathjax&gt;$\displaystyle
\begin{align*}
a\cdot \text{fg} &amp;amp;+(1 - a) \cdot \text{bg} \cdot (1 - a)
\end{align*}
$&lt;/mathjax&gt;&lt;/p&gt;
&lt;/blockquote&gt;
&lt;p&gt;But we are not done yet, because with probability &lt;mathjax&gt;$a$&lt;/mathjax&gt; the foreground pixel reflects the photon once again back towards the background pixel. There it will be reflected, hit the backside of the foreground pixel again, which lets it through to our eyes with probability &lt;mathjax&gt;$(1 - a)$&lt;/mathjax&gt;. We get another term where the final &lt;mathjax&gt;$(1 - a)$&lt;/mathjax&gt; is replaced with &lt;mathjax&gt;$a \cdot \text{fg} \cdot \text {bg} \cdot (1 - a)$&lt;/mathjax&gt;:&lt;/p&gt;
&lt;blockquote&gt;
&lt;p&gt;&lt;mathjax&gt;$\displaystyle
\begin{align*}
a\cdot \text{fg} &amp;amp;+(1 - a) \cdot \text{bg} \cdot (1 - a)\\
&amp;amp;+(1 - a) \cdot \text{bg} \cdot a \cdot \text{fg} \cdot \text{bg} \cdot (1 - a)
\end{align*}
$&lt;/mathjax&gt;&lt;/p&gt;
&lt;/blockquote&gt;
&lt;p&gt;And so on. In each round, we gain another term which is identical to
the previous one, except that it has an additional &lt;mathjax&gt;$a \cdot \text{fg}
\cdot \text{bg}$&lt;/mathjax&gt; factor:&lt;/p&gt;
&lt;blockquote&gt;
&lt;p&gt;&lt;mathjax&gt;$\displaystyle
\begin{align*}
a\cdot \text{fg} &amp;amp;+(1 - a) \cdot \text{bg} \cdot (1 - a)\\
&amp;amp;+(1 - a) \cdot \text{bg} \cdot a \cdot \text{fg} \cdot \text{bg} \cdot (1 - a)\\
&amp;amp;+(1 - a) \cdot \text{bg} \cdot a \cdot \text{fg} \cdot \text{bg} \cdot a \cdot \text{fg} \cdot \text{bg} \cdot (1 - a) \\
&amp;amp;+\cdots
\end{align*}
$&lt;/mathjax&gt;&lt;/p&gt;
&lt;/blockquote&gt;
&lt;p&gt;or more compactly:&lt;/p&gt;
&lt;blockquote&gt;
&lt;p&gt;&lt;mathjax&gt;$\displaystyle
\begin{align*}
&amp;amp;a \cdot \text{fg} + (1 - a)^2 \cdot \text{bg} \cdot
\sum_{i=0}^\infty (a \cdot \text{fg} \cdot \text{bg})^i
\end{align*}
$&lt;/mathjax&gt;&lt;/p&gt;
&lt;/blockquote&gt;
&lt;p&gt;Because we are dealing with pixels, both &lt;mathjax&gt;$a$&lt;/mathjax&gt;, &lt;mathjax&gt;$\text{fg}$&lt;/mathjax&gt;, and
&lt;mathjax&gt;$\text{bg}$&lt;/mathjax&gt; are less than 1, so the sum is a &lt;a href="https://en.wikipedia.org/wiki/Geometric_series"&gt;geometric
series&lt;/a&gt;:&lt;/p&gt;
&lt;blockquote&gt;
&lt;p&gt;&lt;mathjax&gt;$\displaystyle
\begin{align*}
&amp;amp;\sum_{i=0}^\infty x^i = \frac{1}{1 - x}
\end{align*}
$&lt;/mathjax&gt;&lt;/p&gt;
&lt;/blockquote&gt;
&lt;p&gt;Putting them together, we get:&lt;/p&gt;
&lt;blockquote&gt;
&lt;p&gt;&lt;mathjax&gt;$\displaystyle
\begin{align*}
&amp;amp;a \cdot \text{fg} + \frac{(1 - a)^2 \cdot bg}{1 - a \cdot \text{fg} \cdot \text{bg}}
\end{align*}
$&lt;/mathjax&gt;&lt;/p&gt;
&lt;/blockquote&gt;
&lt;p&gt;I have sidestepped the issue of premultiplication by assuming that
background alpha is 1. The calculations with premultipled colors are
similar, and for the color components, the result is simply:&lt;/p&gt;
&lt;blockquote&gt;
&lt;p&gt;&lt;mathjax&gt;$\displaystyle
\begin{align*}
&amp;amp;r = \text{fg} + \frac{(1 - a_\text{fg})^2 \cdot \text{bg}}{1 - \text{fg}\cdot\text{bg}}
\end{align*}
$&lt;/mathjax&gt;&lt;/p&gt;
&lt;/blockquote&gt;
&lt;p&gt;The issue of destination alpha is more complicated. With the Over
operator, both foreground and background are opaque masks, so the
light that survives both has the same color as the input light. With
translucency, the transmitted light has a different color, which means
the resulting alpha value must in principle be different for each
color component. But that&amp;rsquo;s not possible for ARGB pixels. A similar
argument to the above shows that the resulting alpha value would be:&lt;/p&gt;
&lt;blockquote&gt;
&lt;p&gt;&lt;mathjax&gt;$\displaystyle
\begin{align*}
&amp;amp;r = 1 - \frac{(1 - a)\cdot (1 - b)}{1 - \text{fg} \cdot \text{bg}}
\end{align*}
$&lt;/mathjax&gt;&lt;/p&gt;
&lt;/blockquote&gt;
&lt;p&gt;where &lt;mathjax&gt;$b$&lt;/mathjax&gt; is the background alpha. The problem is the dependency on
&lt;mathjax&gt;$\text{fg}$&lt;/mathjax&gt; and &lt;mathjax&gt;$\text{bg}$&lt;/mathjax&gt;. If we simply assume for the purposes of
the alpha computation that &lt;mathjax&gt;$\text{fg}$&lt;/mathjax&gt; and &lt;mathjax&gt;$\text{bg}$&lt;/mathjax&gt; are equal to
&lt;mathjax&gt;$a$&lt;/mathjax&gt; and &lt;mathjax&gt;$b$&lt;/mathjax&gt;, we get this:&lt;/p&gt;
&lt;blockquote&gt;
&lt;p&gt;&lt;mathjax&gt;$\displaystyle
\begin{align*}
&amp;amp;r = 1 - \frac{(1 - a)\cdot (1 - b)}{1 - a \cdot b}
\end{align*}
$&lt;/mathjax&gt;&lt;/p&gt;
&lt;/blockquote&gt;
&lt;p&gt;which is equal to&lt;/p&gt;
&lt;blockquote&gt;
&lt;p&gt;&lt;mathjax&gt;$\displaystyle
\begin{align*}
&amp;amp;a + \frac{(1 - a)^2 \cdot b}{1 - a \cdot b}
\end{align*}
$&lt;/mathjax&gt;&lt;/p&gt;
&lt;/blockquote&gt;
&lt;p&gt;Ie., exactly the same computation as the one for the color
channels. So we can define the &lt;em&gt;Translucency Operator&lt;/em&gt; as this:&lt;/p&gt;
&lt;blockquote&gt;
&lt;p&gt;&lt;mathjax&gt;$\displaystyle
\begin{align*}
r = \text{fg} + \frac{(1 - a)^2 \cdot \text{bg}}{1 - \text{fg} \cdot \text{bg}}
\end{align*}
$&lt;/mathjax&gt;&lt;/p&gt;
&lt;/blockquote&gt;
&lt;p&gt;for all four channels.&lt;/p&gt;
&lt;p&gt;Here is an example of what the operator looks like. The image below is
what you will get if you use the Over operator to implement a
selection rectangle. Mouse over to see what it would look like if you
used the Translucency operator.&lt;/p&gt;
&lt;p&gt;&lt;a onmouseover="document.selectimg.src='translucency/select-trans1.png'" onmouseout="document.selectimg.src='translucency/select-over1.png'"&gt;
&lt;img name=selectimg src="translucency/select-over1.png" alt=""/&gt;
&lt;/a&gt;&lt;/p&gt;
&lt;p&gt;Both were computed in linear RGB. Typical implementations will often
compute &lt;a href="translucency/select-over-srgb.png"&gt;the Over operator in sRGB&lt;/a&gt;, so that&amp;rsquo;s
what see if you actually select some icons in Nautilus. If you want to
compare all three, open these in tabs:&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;&lt;a href="translucency/select-over-srgb.png"&gt;Over, in sRGB&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href="translucency/select-trans.png"&gt;Translucency, in linear RGB&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href="translucency/select-over.png"&gt;Over, in linear RGB&lt;/a&gt;&lt;/li&gt;
&lt;/ul&gt;
&lt;p&gt;And for good measure, even though it makes zero sense to do this,&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;&lt;a href="translucency/select-trans-srgb.png"&gt;Translucency, in sRGB&lt;/a&gt;&lt;/li&gt;
&lt;/ul&gt;</description><guid>https://ssp.impulsetrain.com/translucency.html</guid><pubDate>Mon, 26 Sep 2011 00:00:00 </pubDate></item><item><title>Gamma Correction vs. Premultiplied Pixels</title><link>https://ssp.impulsetrain.com/gamma-premult.html</link><description>&lt;p&gt;Pixels with 8 bits per channel are normally sRGB encoded because that
allocates more bits to darker colors where human vision is the most
sensitive. (Actually, it&amp;rsquo;s really more of a historical accident, but
sRGB nevertheless remains useful for this reason). The relationship
between sRGB and linear RGB is that you get an sRGB pixel by raising
each component of a linear pixel to the power of &lt;mathjax&gt;$1/2.2$&lt;/mathjax&gt;.&lt;/p&gt;
&lt;p&gt;It is common for graphics software to perform alpha blending directly
on these sRGB pixels using alpha values that are linearly coded (ie.,
an alpha value of 0 means no coverage, 0.5 means half coverage, and 1
means full coverage). Because alpha blending is best done with
premultiplied pixels, such systems store pixels in this format:&lt;/p&gt;
&lt;blockquote&gt;
&lt;p&gt;&lt;mathjax&gt;$\left[\,\alpha,\enspace\alpha \cdot \text{R}^{1/2.2},\enspace\alpha \cdot \text{G}^{1/2.2},\enspace\alpha \cdot \text{B}^{1/2.2}\,\right]$&lt;/mathjax&gt;,&lt;/p&gt;
&lt;/blockquote&gt;
&lt;p&gt;that is, the alpha channel is linearly coded, while the R, G, and B
channels are first sRGB coded, then premultiplied with the linear
alpha.  This works well as long as you are happy with blending in
sRGB. And if you discard the alpha channel of such pixels and display
them directly on a monitor, it will look as if the pixels were alpha
blended (in sRGB space) on top of a black background, which is the
desired result.&lt;/p&gt;
&lt;p&gt;But what if you want to blend in linear RGB? If you use the format
above, some expensive conversions will be required. To convert to
premultiplied linear, you have to first divide by alpha, then raise
each color to 2.2, then multiply by alpha. To convert back, you must
divide by alpha, raise to &lt;mathjax&gt;$1/2.2$&lt;/mathjax&gt;, then multiply with alpha.&lt;/p&gt;
&lt;p&gt;Those conversions can be avoided if you store the pixels linearly,
ie., keeping the premultiplication, but coding red, green, and blue
linearly instead of as sRGB:&lt;/p&gt;
&lt;blockquote&gt;
&lt;p&gt;&lt;mathjax&gt;$\left[\,\alpha,\enspace\alpha \cdot \text{R},\enspace\alpha \cdot \text{G},\enspace\alpha \cdot \text{B}\,\right]$&lt;/mathjax&gt;.&lt;/p&gt;
&lt;/blockquote&gt;
&lt;p&gt;This makes blending fast, but 8 bits per channel is no longer good
enough. Without the sRBG encoding, too much resolution will be lost in
darker tones. And to display these pixels on a monitor, they have to
first be converted to sRGB, either manually, or, if the video card can
scan them out directly, by setting the gamma ramp appropriately.&lt;/p&gt;
&lt;p&gt;Can we get the best of both worlds? Yes. The format to use is this:&lt;/p&gt;
&lt;blockquote&gt;
&lt;p&gt;&lt;mathjax&gt;$\left[\,\alpha,\enspace (\alpha \cdot \text{R})^{1/2.2},\enspace (\alpha \cdot \text{G})^{1/2.2},\enspace \left(\alpha \cdot \text{B}\right)^{1/2.2}\,\right]$&lt;/mathjax&gt;,&lt;/p&gt;
&lt;/blockquote&gt;
&lt;p&gt;The alpha channel is stored linearly, and the color channels are first
premultiplied with the linear alpha, then raised to &lt;mathjax&gt;$1/2.2$&lt;/mathjax&gt;.&lt;/p&gt;
&lt;p&gt;With this format, 8 bits per channel is sufficient. Discarding the
alpha channel and displaying the pixels directly on a monitor will
look as if the pixels were alpha blended (in linear space) against
black, as desired.&lt;/p&gt;
&lt;p&gt;You can convert to linear RGB simply by raising the R, G, and B
components to 2.2, and back by raising to &lt;mathjax&gt;$1/2.2$&lt;/mathjax&gt;. Or, if you feel
like cheating, use an exponent of 2 so that the conversions become a
multiplication and a square root respectively.&lt;/p&gt;
&lt;p&gt;This is also the pixel format to use with texture samplers that
implement the sRGB OpenGL extensions
(&lt;a href="http://www.opengl.org/registry/specs/EXT/texture_sRGB.txt"&gt;textures&lt;/a&gt;
and
&lt;a href="http://www.opengl.org/registry/specs/ARB/framebuffer_sRGB.txt"&gt;framebuffers&lt;/a&gt;). These
extensions say precisely that the R, G, and B components are raised to
2.2 before texture filtering, and raised to 1/2.2 after the final
raster operation.&lt;/p&gt;</description><guid>https://ssp.impulsetrain.com/gamma-premult.html</guid><pubDate>Wed, 10 Aug 2011 00:00:00 </pubDate></item><item><title>Sysprof 1.1.8</title><link>https://ssp.impulsetrain.com/sysprof-1.1.8.html</link><description>&lt;p&gt;A new version &lt;a href="http://sysprof.com/sysprof-1.1.8.tar.gz"&gt;1.1.8&lt;/a&gt; of
&lt;a href="http://sysprof.com"&gt;Sysprof&lt;/a&gt; is out.&lt;/p&gt;
&lt;p&gt;This is a release candidate for 1.2.0 and contains mainly bug fixes.&lt;/p&gt;</description><guid>https://ssp.impulsetrain.com/sysprof-1.1.8.html</guid><pubDate>Fri, 15 Jul 2011 00:00:00 </pubDate></item><item><title>Fast Multiplication of Normalized 16 bit Numbers with SSE2</title><link>https://ssp.impulsetrain.com/16bit.html</link><description>&lt;p&gt;If you are compositing pixels with 16 bits per component, you often
need this computation:&lt;/p&gt;
&lt;div class="codehilite"&gt;&lt;pre&gt;&lt;span class="n"&gt;uint16_t&lt;/span&gt; &lt;span class="n"&gt;a&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;b&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;r&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;

&lt;span class="n"&gt;r&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;a&lt;/span&gt; &lt;span class="o"&gt;*&lt;/span&gt; &lt;span class="n"&gt;b&lt;/span&gt; &lt;span class="o"&gt;+&lt;/span&gt; &lt;span class="mh"&gt;0x7fff&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="o"&gt;/&lt;/span&gt; &lt;span class="mi"&gt;65535&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;/pre&gt;&lt;/div&gt;


&lt;p&gt;There is a well-known way to do this quickly without a division:&lt;/p&gt;
&lt;div class="codehilite"&gt;&lt;pre&gt;&lt;span class="n"&gt;uint32_t&lt;/span&gt; &lt;span class="n"&gt;t&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;

&lt;span class="n"&gt;t&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;a&lt;/span&gt; &lt;span class="o"&gt;*&lt;/span&gt; &lt;span class="n"&gt;b&lt;/span&gt; &lt;span class="o"&gt;+&lt;/span&gt; &lt;span class="mh"&gt;0x8000&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;

&lt;span class="n"&gt;r&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;t&lt;/span&gt; &lt;span class="o"&gt;+&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;t&lt;/span&gt; &lt;span class="o"&gt;&amp;gt;&amp;gt;&lt;/span&gt; &lt;span class="mi"&gt;16&lt;/span&gt;&lt;span class="p"&gt;))&lt;/span&gt; &lt;span class="o"&gt;&amp;gt;&amp;gt;&lt;/span&gt; &lt;span class="mi"&gt;16&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;/pre&gt;&lt;/div&gt;


&lt;p&gt;Since we are compositing pixels we want to do this with SSE2
instructions, but because the code above uses 32 bit arithmetic, we
can only do four operations at a time, even though SSE registers have
room for eight 16 bit values. Here is a direct translation into SSE2:&lt;/p&gt;
&lt;div class="codehilite"&gt;&lt;pre&gt;&lt;span class="n"&gt;a&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;punpcklwd&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;a&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="mi"&gt;0&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
&lt;span class="n"&gt;b&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;punpcklwd&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;b&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="mi"&gt;0&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
&lt;span class="n"&gt;a&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;pmulld&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;a&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;b&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
&lt;span class="n"&gt;a&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;paddd&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;a&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="mh"&gt;0x8000&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
&lt;span class="n"&gt;b&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;psrld&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;a&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="mi"&gt;16&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
&lt;span class="n"&gt;a&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;paddd&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;a&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;b&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
&lt;span class="n"&gt;a&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;psrld&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;a&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="mi"&gt;16&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
&lt;span class="n"&gt;a&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;packusdw&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;a&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="mi"&gt;0&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
&lt;/pre&gt;&lt;/div&gt;


&lt;p&gt;But there is another way that better matches SSE2:&lt;/p&gt;
&lt;div class="codehilite"&gt;&lt;pre&gt;&lt;span class="n"&gt;uint16_t&lt;/span&gt; &lt;span class="n"&gt;lo&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;hi&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;t&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;r&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;

&lt;span class="n"&gt;hi&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;a&lt;/span&gt; &lt;span class="o"&gt;*&lt;/span&gt; &lt;span class="n"&gt;b&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="o"&gt;&amp;gt;&amp;gt;&lt;/span&gt; &lt;span class="mi"&gt;16&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="n"&gt;lo&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;a&lt;/span&gt; &lt;span class="o"&gt;*&lt;/span&gt; &lt;span class="n"&gt;b&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="o"&gt;&amp;amp;&lt;/span&gt; &lt;span class="mh"&gt;0xffff&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;

&lt;span class="n"&gt;t&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;lo&lt;/span&gt; &lt;span class="o"&gt;&amp;gt;&amp;gt;&lt;/span&gt; &lt;span class="mi"&gt;15&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="n"&gt;hi&lt;/span&gt; &lt;span class="o"&gt;+=&lt;/span&gt; &lt;span class="n"&gt;t&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="n"&gt;t&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;hi&lt;/span&gt; &lt;span class="o"&gt;^&lt;/span&gt; &lt;span class="mh"&gt;0x7fff&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;

&lt;span class="k"&gt;if&lt;/span&gt; &lt;span class="p"&gt;((&lt;/span&gt;&lt;span class="n"&gt;int16_t&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="n"&gt;lo&lt;/span&gt; &lt;span class="o"&gt;&amp;gt;&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;int16_t&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="n"&gt;t&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
    &lt;span class="n"&gt;lo&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="mh"&gt;0xffff&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="k"&gt;else&lt;/span&gt;
    &lt;span class="n"&gt;lo&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="mh"&gt;0x0000&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;

&lt;span class="n"&gt;r&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;hi&lt;/span&gt; &lt;span class="o"&gt;-&lt;/span&gt; &lt;span class="n"&gt;lo&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;/pre&gt;&lt;/div&gt;


&lt;p&gt;This version is better because it avoids the unpacking to 32
bits. Here is the translation into SSE2:&lt;/p&gt;
&lt;div class="codehilite"&gt;&lt;pre&gt;&lt;span class="n"&gt;t&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;pmulhuw&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;a&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;b&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
&lt;span class="n"&gt;a&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;pmullw&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;a&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;b&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
&lt;span class="n"&gt;b&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;psrlw&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;a&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="mi"&gt;15&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
&lt;span class="n"&gt;t&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;paddw&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;t&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;b&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
&lt;span class="n"&gt;b&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;pxor&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;t&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="mh"&gt;0x7fff&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
&lt;span class="n"&gt;a&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;pcmpgtw&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;a&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;b&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
&lt;span class="n"&gt;a&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;psubw&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;t&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;a&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
&lt;/pre&gt;&lt;/div&gt;


&lt;p&gt;This is not only shorter, it also makes use of the full width of the
SSE registers, computing eight results at a time.&lt;/p&gt;
&lt;p&gt;Unfortunately SSE2 doesn&amp;rsquo;t have 8-bit variants of &lt;code&gt;pmulhuw&lt;/code&gt;, &lt;code&gt;pmullw&lt;/code&gt;, and
&lt;code&gt;psrlw&lt;/code&gt;, so we can&amp;rsquo;t use this trick for the more common case where
pixels have 8 bits per component.&lt;/p&gt;
&lt;p&gt;Exercise: Why does the second version work?&lt;/p&gt;</description><guid>https://ssp.impulsetrain.com/16bit.html</guid><pubDate>Sun, 03 Jul 2011 00:00:00 </pubDate></item></channel></rss>