<?xml version="1.0" encoding="utf-8"?><feed xmlns="http://www.w3.org/2005/Atom" ><generator uri="https://jekyllrb.com/" version="3.9.0">Jekyll</generator><link href="http://kqueue.org/feed.xml" rel="self" type="application/atom+xml" /><link href="http://kqueue.org/" rel="alternate" type="text/html" /><updated>2020-10-15T18:17:49+00:00</updated><id>http://kqueue.org/feed.xml</id><title type="html">kqueue.org</title><entry><title type="html">Correctness of a compiler for arithmetic expressions in Lean</title><link href="http://kqueue.org/blog/2020/10/15/arithcc/" rel="alternate" type="text/html" title="Correctness of a compiler for arithmetic expressions in Lean" /><published>2020-10-15T00:00:00+00:00</published><updated>2020-10-15T00:00:00+00:00</updated><id>http://kqueue.org/blog/2020/10/15/arithcc</id><content type="html" xml:base="http://kqueue.org/blog/2020/10/15/arithcc/">&lt;p&gt;“Correctness of a compiler for arithmetic expressions” (&lt;a href=&quot;http://jmc.stanford.edu/articles/mcpain/mcpain.pdf&quot;&gt;McCarthy and Painter 1967&lt;/a&gt;)
describes the first proof of compiler correctness.
To make it easier to play with the proof,
I coded it up using the &lt;a href=&quot;https://leanprover-community.github.io/&quot;&gt;Lean theorem prover&lt;/a&gt;.&lt;/p&gt;

&lt;p&gt;The Lean code is self-explanatory, so I won’t go into the details here.&lt;/p&gt;

&lt;ul&gt;
  &lt;li&gt;
    &lt;p&gt;&lt;a href=&quot;https://github.com/xiw/arithcc/blob/master/src/arithcc.lean&quot;&gt;Browse the Lean code&lt;/a&gt; on GitHub.&lt;/p&gt;
  &lt;/li&gt;
  &lt;li&gt;
    &lt;p&gt;&lt;a href=&quot;https://leanprover-community.github.io/lean-web-editor/#url=https%3A%2F%2Fraw.githubusercontent.com%2Fxiw%2Farithcc%2Fmaster%2Fsrc%2Farithcc.lean&quot;&gt;Run in the browser&lt;/a&gt; (laptop or desktop recommended;
tested with Lean/mathlib 3.21.0c).&lt;/p&gt;
  &lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Lean’s support for
&lt;a href=&quot;https://leanprover-community.github.io/extras/calc.html&quot;&gt;calculational proofs&lt;/a&gt;
is particularly nice for mirroring the proof in the paper.
For example, below is an excerpt of the proof in the paper:&lt;/p&gt;

\[\begin{aligned}
\zeta_1 &amp;amp; = \mathit{outcome}(\mathit{compile}(s1(e),t),\eta) \\
        &amp;amp; =_t a(\mathit{ac}, \upsilon_1, \eta) &amp;amp; \textrm{Induction Hypothesis}
\end{aligned}\]

&lt;p&gt;Here’s the corresponding part in Lean:&lt;/p&gt;

&lt;pre&gt;&lt;code class=&quot;language-lean&quot;&gt;calc ζ₁
    = outcome (compile map e_s₁ t) η : by cc
... ≃[t] {ac := ν₁, ..η}             : by apply e_ih_s₁; assumption
&lt;/code&gt;&lt;/pre&gt;

&lt;h2 id=&quot;typos&quot;&gt;Typos&lt;/h2&gt;

&lt;p&gt;The &lt;a href=&quot;http://jmc.stanford.edu/articles/mcpain/mcpain.pdf&quot;&gt;reprint version&lt;/a&gt; of the paper contains a number of typos
(not present in the original version):&lt;/p&gt;

&lt;ul&gt;
  &lt;li&gt;
    &lt;p&gt;Theorem 1: “\(c(\mathit{loc}(v, \eta) = c(v, \xi)\)” should be
“\(c(\mathit{loc}(v, \mathit{map}), \eta) = c(v, \xi)\).”&lt;/p&gt;
  &lt;/li&gt;
  &lt;li&gt;
    &lt;p&gt;Section 5.III, under “\(\zeta_2 = \mathit{outcome}(\mathit{mksto}(t), \zeta_1)\)”:
“\(a(t, \upsilon_1), \zeta_1)\)” should be “\(a(t, \upsilon_1, \zeta_1)\).”&lt;/p&gt;
  &lt;/li&gt;
  &lt;li&gt;
    &lt;p&gt;Section 5.III, next to “\(c(\mathit{loc}(\upsilon, \mathit{map}), \zeta_2)\)”:
“\(c(\mathit{loc}(\upsilon), \mathit{map})a(t, \upsilon_1, \eta))\)” should be
“\(c(\mathit{loc}(\upsilon, \mathit{map}), a(t, \upsilon_1, \eta))\).”&lt;/p&gt;
  &lt;/li&gt;
&lt;/ul&gt;

&lt;h2 id=&quot;links&quot;&gt;Links&lt;/h2&gt;

&lt;ul&gt;
  &lt;li&gt;
    &lt;p&gt;Formalization (&lt;a href=&quot;http://www.cs.umd.edu/~hjs/pubs/compilers/archive/mi72-mil-wey.pdf&quot;&gt;Milner and Weyhrauch 1972&lt;/a&gt;) using LCF.&lt;/p&gt;
  &lt;/li&gt;
  &lt;li&gt;
    &lt;p&gt;Xavier Leroy’s &lt;a href=&quot;https://xavierleroy.org/courses/EUTypes-2019/&quot;&gt;course&lt;/a&gt;
on proving the correctness of a compiler for IMP.&lt;/p&gt;
  &lt;/li&gt;
  &lt;li&gt;
    &lt;p&gt;The &lt;a href=&quot;http://compcert.inria.fr/&quot;&gt;CompCert&lt;/a&gt; C verified compiler.&lt;/p&gt;
  &lt;/li&gt;
  &lt;li&gt;
    &lt;p&gt;&lt;a href=&quot;https://github.com/AliveToolkit/alive2&quot;&gt;Alive&lt;/a&gt; and
&lt;a href=&quot;https://github.com/google/souper&quot;&gt;Souper&lt;/a&gt; for verified LLVM optimizations.&lt;/p&gt;
  &lt;/li&gt;
  &lt;li&gt;
    &lt;p&gt;&lt;a href=&quot;https://unsat.cs.washington.edu/projects/jitterbug/&quot;&gt;Jitterbug&lt;/a&gt;
for verifying BPF just-in-time compilers in the Linux kernel.&lt;/p&gt;
  &lt;/li&gt;
&lt;/ul&gt;</content><author><name>Xi Wang</name></author><category term="compiler" /><category term="verification" /><summary type="html">“Correctness of a compiler for arithmetic expressions” (McCarthy and Painter 1967) describes the first proof of compiler correctness. To make it easier to play with the proof, I coded it up using the Lean theorem prover.</summary></entry><entry><title type="html">Areas of Napoleon triangles</title><link href="http://kqueue.org/blog/2020/09/12/areas-of-napoleon-triangles/" rel="alternate" type="text/html" title="Areas of Napoleon triangles" /><published>2020-09-12T00:00:00+00:00</published><updated>2020-09-12T00:00:00+00:00</updated><id>http://kqueue.org/blog/2020/09/12/areas-of-napoleon-triangles</id><content type="html" xml:base="http://kqueue.org/blog/2020/09/12/areas-of-napoleon-triangles/">&lt;p&gt;NB: I came across a survey paper (&lt;a href=&quot;#zhao-2011&quot;&gt;Zhao, 2011&lt;/a&gt;) on
some plane geometry problems that a group of friends and I explored around 2000.
It’s a nice trip down the memory lane.
I played with
&lt;a href=&quot;/blog/2020/08/31/automated-geometry-bashing/&quot;&gt;computer-aided proofs using Maxima&lt;/a&gt;
recently and thought it might be fun to write down the results.&lt;/p&gt;

&lt;p&gt;Consider triangle \(ABC\).
The centers of equilateral triangles constructed outward on the sides
form the &lt;em&gt;outer&lt;/em&gt; Napoleon triangle, \(DEF\).
Similarly,
the centers of equilateral triangles constructed inward on the sides
form the &lt;em&gt;inner&lt;/em&gt; Napoleon triangle, \(D'E'F'\).
&lt;a href=&quot;https://en.wikipedia.org/wiki/Napoleon%27s_theorem&quot;&gt;Napoleon’s theorem&lt;/a&gt; says that
both \(DEF\) and \(D'E'F'\) are equilateral, one counterclockwise and the other clockwise.&lt;/p&gt;

&lt;p&gt;A particularly interesting property is that
the sum of the (signed) areas of \(DEF\) and \(D'E'F'\) equals the area of \(ABC\)
(&lt;a href=&quot;#coxeter-and-greitzer-1967&quot;&gt;Coxeter and Greitzer, 1967&lt;/a&gt;, §3.3).
A signed area is positive if the vertices are ordered counterclockwise,
and negative if clockwise.&lt;/p&gt;

&lt;p&gt;
&lt;div id=&quot;napoleon&quot; title=&quot;Napoleon's theorem&quot; class=&quot;cindy-canvas&quot;&gt;&lt;/div&gt;
&lt;/p&gt;

&lt;script id=&quot;napdraw&quot; type=&quot;text/x-cindyscript&quot;&gt;
xlegend = -9.5;
ylegend = -8;
fmt(x) := format(x, 4);
abc = area(A, B, C);
def = area(D, E, F);
def' = area(D', E', F');
drawtext((xlegend, ylegend),
  &quot;$\begin{array}{lllll}&quot; +
  &quot;\textrm{area}\ ABC \\&quot; +
  fmt(abc) + &quot; &amp; &amp; &amp; = &amp;&quot; + fmt(abc) + &quot;\\ \\&quot; +
  &quot;\textrm{area}\ DEF &amp; &amp; \textrm{area}\ D'E'F' \\&quot; +
  fmt(def) + &quot; &amp; + &amp; &quot; + fmt(def') + &quot; &amp; = &amp;&quot; + fmt(def + def') +
  &quot;\end{array}$&quot;, size-&gt;14);
&lt;/script&gt;

&lt;script type=&quot;text/javascript&quot;&gt;
CindyJS({
  ports: [{id: &quot;napoleon&quot;}],
  defaultAppearance: defaultAppearance,
  scripts: &quot;nap*&quot;,
  geometry: [
    {name: &quot;A&quot;, type: &quot;Free&quot;, pos: [-2,  3.5], color: [1, 1, 1], labeled: true, printname: &quot;$A$&quot;},
    {name: &quot;B&quot;, type: &quot;Free&quot;, pos: [-3, -4.5], color: [1, 1, 1], labeled: true, printname: &quot;$B$&quot;},
    {name: &quot;C&quot;, type: &quot;Free&quot;, pos: [10, -4.5], color: [1, 1, 1], labeled: true, printname: &quot;$C$&quot;},

    {name: &quot;l_BC&quot;, type: &quot;Join&quot;, args: [&quot;B&quot;, &quot;C&quot;], visible: false},
    {name: &quot;l_CA&quot;, type: &quot;Join&quot;, args: [&quot;C&quot;, &quot;A&quot;], visible: false},
    {name: &quot;l_AB&quot;, type: &quot;Join&quot;, args: [&quot;A&quot;, &quot;B&quot;], visible: false},

    {name: &quot;l_BD&quot;, type: &quot;LineByFixedAngle&quot;, angle: -30 * Math.PI / 180, args: [&quot;l_BC&quot;, &quot;B&quot;], visible: false},
    {name: &quot;l_CD&quot;, type: &quot;LineByFixedAngle&quot;, angle:  30 * Math.PI / 180, args: [&quot;l_BC&quot;, &quot;C&quot;], visible: false},
    {name: &quot;D&quot;, type: &quot;Meet&quot;, args: [&quot;l_BD&quot;, &quot;l_CD&quot;], labeled: true, printname: &quot;$D$&quot;},
    {name: &quot;tr_BC&quot;, type: &quot;TrReflection&quot;, args: [&quot;l_BC&quot;]},
    {name: &quot;D'&quot;, type: &quot;Transform&quot;, args: [&quot;tr_BC&quot;, &quot;D&quot;], labeled: true, printname: &quot;$D'$&quot;},

    {name: &quot;l_CE&quot;, type: &quot;LineByFixedAngle&quot;, angle: -30 * Math.PI / 180, args: [&quot;l_CA&quot;, &quot;C&quot;], visible: false},
    {name: &quot;l_AE&quot;, type: &quot;LineByFixedAngle&quot;, angle:  30 * Math.PI / 180, args: [&quot;l_CA&quot;, &quot;A&quot;], visible: false},
    {name: &quot;E&quot;, type: &quot;Meet&quot;, args: [&quot;l_CE&quot;, &quot;l_AE&quot;], labeled: true, printname: &quot;$E$&quot;},
    {name: &quot;tr_CA&quot;, type: &quot;TrReflection&quot;, args: [&quot;l_CA&quot;]},
    {name: &quot;E'&quot;, type: &quot;Transform&quot;, args: [&quot;tr_CA&quot;, &quot;E&quot;], labeled: true, printname: &quot;$E'$&quot;},

    {name: &quot;l_AF&quot;, type: &quot;LineByFixedAngle&quot;, angle: -30 * Math.PI / 180, args: [&quot;l_AB&quot;, &quot;A&quot;], visible: false},
    {name: &quot;l_BF&quot;, type: &quot;LineByFixedAngle&quot;, angle:  30 * Math.PI / 180, args: [&quot;l_AB&quot;, &quot;B&quot;], visible: false},
    {name: &quot;F&quot;, type: &quot;Meet&quot;, args: [&quot;l_AF&quot;, &quot;l_BF&quot;], labeled: true, printname: &quot;$F$&quot;},
    {name: &quot;tr_AB&quot;, type: &quot;TrReflection&quot;, args: [&quot;l_AB&quot;]},
    {name: &quot;F'&quot;, type: &quot;Transform&quot;, args: [&quot;tr_AB&quot;, &quot;F&quot;], labeled: true, printname: &quot;$F'$&quot;},

    {name: &quot;l_EF&quot;, type: &quot;Join&quot;, args: [&quot;E&quot;, &quot;F&quot;], visible: false},
    {name: &quot;l_FD&quot;, type: &quot;Join&quot;, args: [&quot;F&quot;, &quot;D&quot;], visible: false},
    {name: &quot;l_DE&quot;, type: &quot;Join&quot;, args: [&quot;D&quot;, &quot;E&quot;], visible: false},

    {name: &quot;DD'&quot;, type: &quot;Segment&quot;, args: [&quot;D&quot;, &quot;D'&quot;], alpha: .2},
    {name: &quot;EE'&quot;, type: &quot;Segment&quot;, args: [&quot;E&quot;, &quot;E'&quot;], alpha: .2},
    {name: &quot;FF'&quot;, type: &quot;Segment&quot;, args: [&quot;F&quot;, &quot;F'&quot;], alpha: .2},

    {name: &quot;ABC&quot;, type: &quot;Poly&quot;, args: [&quot;A&quot;, &quot;B&quot;, &quot;C&quot;], fillcolor: [1,0,0], fillalpha: .2},
    {name: &quot;DEF&quot;, type: &quot;Poly&quot;, args: [&quot;D&quot;, &quot;E&quot;, &quot;F&quot;], fillcolor: [0,0,1], fillalpha: .2},
    {name: &quot;D'E'F'&quot;, type: &quot;Poly&quot;, args: [&quot;D'&quot;, &quot;E'&quot;, &quot;F'&quot;], fillcolor: [0,0,1], fillalpha: .2},

    {name: &quot;l_A&quot;, type: &quot;Orthogonal&quot;, args: [&quot;l_EF&quot;, &quot;A&quot;], visible: false},
    {name: &quot;l_B&quot;, type: &quot;Orthogonal&quot;, args: [&quot;l_FD&quot;, &quot;B&quot;], visible: false},
    {name: &quot;X&quot;, type: &quot;Meet&quot;, args: [&quot;l_A&quot;, &quot;l_B&quot;]},
    {name: &quot;AX&quot;, type: &quot;Segment&quot;, args: [&quot;A&quot;, &quot;X&quot;], alpha: .2},
    {name: &quot;BX&quot;, type: &quot;Segment&quot;, args: [&quot;B&quot;, &quot;X&quot;], alpha: .2},
    {name: &quot;CX&quot;, type: &quot;Segment&quot;, args: [&quot;C&quot;, &quot;X&quot;], alpha: .2},
  ]
});
&lt;/script&gt;

&lt;p&gt;Note that in Napoleon triangle, \(A', B', C'\), the reflections
of \(A, B, C\) across \(EF, FD, DE\), respectively, coincide such that
\(\mathrm{area}\ A'B'C' = 0\).
A general and symmetric form of the areal property is the following:&lt;/p&gt;

\[\mathrm{area}\ ABC + \mathrm{area}\ A'B'C' = \mathrm{area}\ DEF + \mathrm{area}\ D'E'F'.\]

&lt;p&gt;This post focuses on generalizations of the areal property.&lt;/p&gt;

&lt;h2 id=&quot;antisimilitude-and-involution&quot;&gt;Antisimilitude and involution&lt;/h2&gt;

&lt;p&gt;First, we would like to generalize how \(D, E, F\) are chosen.
What’s a necessary and sufficient condition for their reflections
\(D', E', F'\) across \(BC, CA, AB\), respectively,
to form a triangle inversely similar to \(DEF\)
(i.e., with the same shape and opposite rotations)?&lt;/p&gt;

&lt;p&gt;&lt;a href=&quot;#ye-and-cao-1997&quot;&gt;Zhonghao Ye and Gang Cao (1997)&lt;/a&gt;
answered this question with the following theorem.&lt;/p&gt;

&lt;div class=&quot;theorem&quot;&gt;
  &lt;p&gt;&lt;strong&gt;Theorem 1&lt;/strong&gt;:
Consider non-degenerate triangles \(ABC\) and \(DEF\).
Let \(A', B', C', D', E', F'\) be the reflections of \(A, B, C, D, E, F\) across
\(EF, FD, DE, BC, CA, AB\), respectively.&lt;/p&gt;

  &lt;p&gt;Triangles \(DEF\) and \(D'E'F'\) ‘are inversely similar
if and only if the Möbius transformation that maps \(A, B, C\) to \(D, E, F\) is an involution
(or simply, \(A, B, C, D, E, F\) are involutoric).&lt;/p&gt;
&lt;/div&gt;

&lt;p&gt;
&lt;div id=&quot;inv&quot; title=&quot;The areal property (involution)&quot; class=&quot;cindy-canvas&quot;&gt;&lt;/div&gt;
&lt;/p&gt;

&lt;script id=&quot;drawArea&quot; type=&quot;text/x-cindyscript&quot;&gt;
xlegend = -9.5;
ylegend = -8;
fmt(x) := format(x, 4);
abc = area(A, B, C);
def = area(D, E, F);
abc' = area(A', B', C');
def' = area(D', E', F');
drawtext((xlegend, ylegend),
  &quot;$\begin{array}{lllll}&quot; +
  &quot;\textrm{area}\ ABC &amp; &amp; \textrm{area}\ A'B'C' \\&quot; +
  fmt(abc) + &quot; &amp; + &amp; &quot; + fmt(abc') + &quot; &amp; = &amp;&quot; + fmt(abc + abc') + &quot;\\ \\&quot; +
  &quot;\textrm{area}\ DEF &amp; &amp; \textrm{area}\ D'E'F' \\&quot; +
  fmt(def) + &quot; &amp; + &amp; &quot; + fmt(def') + &quot; &amp; = &amp;&quot; + fmt(def + def') +
  &quot;\end{array}$&quot;, size-&gt;14);
&lt;/script&gt;

&lt;script id=&quot;invinit&quot; type=&quot;text/x-cindyscript&quot;&gt;
moveto(F, realF.xy);
&lt;/script&gt;

&lt;script id=&quot;invmousedrag&quot; type=&quot;text/x-cindyscript&quot;&gt;
if(mover()==A, moveto(F, realF.xy));
if(mover()==B, moveto(F, realF.xy));
if(mover()==C, moveto(F, realF.xy));
if(mover()==D, moveto(F, realF.xy));
if(mover()==E, moveto(F, realF.xy));
if(mover()==F, moveto(C, realC.xy));
&lt;/script&gt;

&lt;script type=&quot;text/javascript&quot;&gt;
CindyJS({
  ports: [{id: &quot;inv&quot;}],
  scripts: &quot;inv*&quot;,
  drawscript: &quot;drawArea&quot;,
  defaultAppearance: defaultAppearance,
  geometry: [
    {name: &quot;A&quot;, type: &quot;Free&quot;, pos: [ 1,  3], color: [1, 1, 1], labeled: true, printname: &quot;$A$&quot;},
    {name: &quot;B&quot;, type: &quot;Free&quot;, pos: [-3, -4], color: [1, 1, 1], labeled: true, printname: &quot;$B$&quot;},
    {name: &quot;C&quot;, type: &quot;Free&quot;, pos: [10, -5], color: [1, 1, 1], labeled: true, printname: &quot;$C$&quot;},
    {name: &quot;D&quot;, type: &quot;Free&quot;, pos: [ 4, -8], color: [1, 1, 1], labeled: true, printname: &quot;$D$&quot;},
    {name: &quot;E&quot;, type: &quot;Free&quot;, pos: [ 7,  1], color: [1, 1, 1], labeled: true, printname: &quot;$E$&quot;},
    {name: &quot;F&quot;, type: &quot;Free&quot;, pos: [-2,  0], color: [1, 1, 1], labeled: true, printname: &quot;$F$&quot;},

    {name: &quot;trA&quot;, type: &quot;TrMoebius&quot;, args: [&quot;B&quot;, &quot;E&quot;, &quot;C&quot;, &quot;F&quot;, &quot;F&quot;, &quot;C&quot;]},
    {name: &quot;trB&quot;, type: &quot;TrMoebius&quot;, args: [&quot;C&quot;, &quot;F&quot;, &quot;A&quot;, &quot;D&quot;, &quot;D&quot;, &quot;A&quot;]},
    {name: &quot;trC&quot;, type: &quot;TrMoebius&quot;, args: [&quot;A&quot;, &quot;D&quot;, &quot;B&quot;, &quot;E&quot;, &quot;E&quot;, &quot;B&quot;]},
    {name: &quot;realA&quot;, type: &quot;Transform&quot;, args: [&quot;trA&quot;, &quot;D&quot;], visible: false},
    {name: &quot;realB&quot;, type: &quot;Transform&quot;, args: [&quot;trB&quot;, &quot;E&quot;], visible: false},
    {name: &quot;realC&quot;, type: &quot;Transform&quot;, args: [&quot;trC&quot;, &quot;F&quot;], visible: false},

    {name: &quot;trD&quot;, type: &quot;TrMoebius&quot;, args: [&quot;E&quot;, &quot;B&quot;, &quot;F&quot;, &quot;C&quot;, &quot;C&quot;, &quot;F&quot;]},
    {name: &quot;trE&quot;, type: &quot;TrMoebius&quot;, args: [&quot;F&quot;, &quot;C&quot;, &quot;D&quot;, &quot;A&quot;, &quot;A&quot;, &quot;D&quot;]},
    {name: &quot;trF&quot;, type: &quot;TrMoebius&quot;, args: [&quot;D&quot;, &quot;A&quot;, &quot;E&quot;, &quot;B&quot;, &quot;B&quot;, &quot;E&quot;]},
    {name: &quot;realD&quot;, type: &quot;Transform&quot;, args: [&quot;trD&quot;, &quot;A&quot;], visible: false},
    {name: &quot;realE&quot;, type: &quot;Transform&quot;, args: [&quot;trE&quot;, &quot;B&quot;], visible: false},
    {name: &quot;realF&quot;, type: &quot;Transform&quot;, args: [&quot;trF&quot;, &quot;C&quot;], visible: false},

    {name: &quot;l_BC&quot;, type: &quot;Join&quot;, args: [&quot;B&quot;, &quot;C&quot;], visible: false},
    {name: &quot;l_CA&quot;, type: &quot;Join&quot;, args: [&quot;C&quot;, &quot;A&quot;], visible: false},
    {name: &quot;l_AB&quot;, type: &quot;Join&quot;, args: [&quot;A&quot;, &quot;B&quot;], visible: false},
    {name: &quot;l_EF&quot;, type: &quot;Join&quot;, args: [&quot;E&quot;, &quot;F&quot;], visible: false},
    {name: &quot;l_FD&quot;, type: &quot;Join&quot;, args: [&quot;F&quot;, &quot;D&quot;], visible: false},
    {name: &quot;l_DE&quot;, type: &quot;Join&quot;, args: [&quot;D&quot;, &quot;E&quot;], visible: false},

    {name: &quot;tr_BC&quot;, type: &quot;TrReflection&quot;, args: [&quot;l_BC&quot;]},
    {name: &quot;tr_CA&quot;, type: &quot;TrReflection&quot;, args: [&quot;l_CA&quot;]},
    {name: &quot;tr_AB&quot;, type: &quot;TrReflection&quot;, args: [&quot;l_AB&quot;]},
    {name: &quot;tr_EF&quot;, type: &quot;TrReflection&quot;, args: [&quot;l_EF&quot;]},
    {name: &quot;tr_FD&quot;, type: &quot;TrReflection&quot;, args: [&quot;l_FD&quot;]},
    {name: &quot;tr_DE&quot;, type: &quot;TrReflection&quot;, args: [&quot;l_DE&quot;]},
    {name: &quot;A'&quot;, type: &quot;Transform&quot;, args: [&quot;tr_EF&quot;, &quot;A&quot;], labeled: true, printname: &quot;$A'$&quot;},
    {name: &quot;B'&quot;, type: &quot;Transform&quot;, args: [&quot;tr_FD&quot;, &quot;B&quot;], labeled: true, printname: &quot;$B'$&quot;},
    {name: &quot;C'&quot;, type: &quot;Transform&quot;, args: [&quot;tr_DE&quot;, &quot;C&quot;], labeled: true, printname: &quot;$C'$&quot;},
    {name: &quot;D'&quot;, type: &quot;Transform&quot;, args: [&quot;tr_BC&quot;, &quot;D&quot;], labeled: true, printname: &quot;$D'$&quot;},
    {name: &quot;E'&quot;, type: &quot;Transform&quot;, args: [&quot;tr_CA&quot;, &quot;E&quot;], labeled: true, printname: &quot;$E'$&quot;},
    {name: &quot;F'&quot;, type: &quot;Transform&quot;, args: [&quot;tr_AB&quot;, &quot;F&quot;], labeled: true, printname: &quot;$F'$&quot;},

    {name: &quot;ABC&quot;, type: &quot;Poly&quot;, args: [&quot;A&quot;, &quot;B&quot;, &quot;C&quot;], fillcolor: [1,0,0], fillalpha: .2},
    {name: &quot;DEF&quot;, type: &quot;Poly&quot;, args: [&quot;D&quot;, &quot;E&quot;, &quot;F&quot;], fillcolor: [0,0,1], fillalpha: .2},
    {name: &quot;A'B'C'&quot;, type: &quot;Poly&quot;, args: [&quot;A'&quot;, &quot;B'&quot;, &quot;C'&quot;], fillcolor: [1,0,0], fillalpha: .2},
    {name: &quot;D'E'F'&quot;, type: &quot;Poly&quot;, args: [&quot;D'&quot;, &quot;E'&quot;, &quot;F'&quot;], fillcolor: [0,0,1], fillalpha: .2},

    {name: &quot;AA'&quot;, type: &quot;Segment&quot;, args: [&quot;A&quot;, &quot;A'&quot;], alpha: .2},
    {name: &quot;BB'&quot;, type: &quot;Segment&quot;, args: [&quot;B&quot;, &quot;B'&quot;], alpha: .2},
    {name: &quot;CC'&quot;, type: &quot;Segment&quot;, args: [&quot;C&quot;, &quot;C'&quot;], alpha: .2},
    {name: &quot;DD'&quot;, type: &quot;Segment&quot;, args: [&quot;D&quot;, &quot;D'&quot;], alpha: .2},
    {name: &quot;EE'&quot;, type: &quot;Segment&quot;, args: [&quot;E&quot;, &quot;E'&quot;], alpha: .2},
    {name: &quot;FF'&quot;, type: &quot;Segment&quot;, args: [&quot;F&quot;, &quot;F'&quot;], alpha: .2},
  ]
});
&lt;/script&gt;

&lt;p&gt;Let’s start with some definitions.
A &lt;a href=&quot;https://en.wikipedia.org/wiki/M%C3%B6bius_transformation&quot;&gt;Möbius transformation&lt;/a&gt;
is a linear fractional transformation in the following form:&lt;/p&gt;

\[\varphi(z) = \frac{\alpha z + \beta}{\gamma z + \delta}.\]

&lt;p&gt;Here \(\alpha, \beta, \gamma, \delta\) are complex numbers
such that \(\alpha\delta - \beta\gamma \neq 0\).
We use a lowercase letter \(z\) to represent the complex number of the corresponding
point \(Z\).&lt;/p&gt;

&lt;p&gt;A transformation \(\varphi\) is an involution if \(\varphi(\varphi(z)) = z\).
For example, if an involutoric transformation maps \(A\) to \(D\), then it also maps \(D\) to \(A\).&lt;/p&gt;

&lt;p&gt;A Möbius transformation is uniquely determined by three pairs of points
(&lt;a href=&quot;#deaux-1956&quot;&gt;Deaux 1956&lt;/a&gt;, article 74).
When the transformation determined by \((A, D), (B, E), (C, F)\)
is an involution,
these points satisfy the following (&lt;a href=&quot;#deaux-1956&quot;&gt;Deaux 1956&lt;/a&gt;, article 102):&lt;/p&gt;

\[\begin{vmatrix}
ad &amp;amp; a + d &amp;amp; 1 \\
be &amp;amp; b + e &amp;amp; 1 \\
cf &amp;amp; c + f &amp;amp; 1
\end{vmatrix} = 0.\]

&lt;p&gt;We use the above definition in the proofs.&lt;/p&gt;

&lt;p&gt;A more intuitive way is to rewrite the determinant as:&lt;/p&gt;

\[\frac{(a - f)(b - d)(c - e)}{(f - b)(d - c)(e - a)} = -1.\]

&lt;p&gt;Therefore, one may view an involution as
a hexagon that satisfy the following properties:&lt;/p&gt;

&lt;ul&gt;
  &lt;li&gt;
    &lt;p&gt;\(\dfrac{AF}{FB} \cdot \dfrac{BD}{DC} \cdot \dfrac{CE}{EA} = 1\);&lt;/p&gt;
  &lt;/li&gt;
  &lt;li&gt;
    &lt;p&gt;\(\angle AFB + \angle BDC + \angle CEA = 0\).&lt;/p&gt;
  &lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;We use \(\angle XYZ\) to denote the
&lt;a href=&quot;https://web.evanchen.cc/handouts/Directed-Angles/Directed-Angles.pdf&quot;&gt;directed angle&lt;/a&gt;
from line \(XY\) to line \(YZ\).&lt;/p&gt;

&lt;p&gt;With this view,
it’s easy to see that below are a few examples of involution:&lt;/p&gt;

&lt;ul&gt;
  &lt;li&gt;A triangle \(ABC\) and its outer (or inner) Napoleon triangle \(DEF\);&lt;/li&gt;
  &lt;li&gt;A triangle \(ABC\) and points \(D, E, F\) on \(BC, AC, AB\), respectively,
such that \(AD, BE, CF\) are concurrent
(&lt;a href=&quot;https://en.wikipedia.org/wiki/Ceva%27s_theorem&quot;&gt;Ceva’s theorem&lt;/a&gt;);&lt;/li&gt;
  &lt;li&gt;A triangle \(ABC\) and points \(D, E, F\) on \(BC, AC, AB\), respectively,
such that \(D, E, F\) are collinear
(&lt;a href=&quot;https://en.wikipedia.org/wiki/Menelaus%27s_theorem&quot;&gt;Menelaus’s theorem&lt;/a&gt;);&lt;/li&gt;
  &lt;li&gt;The three pairs of opposite vertices of a
&lt;a href=&quot;https://mathworld.wolfram.com/CompleteQuadrilateral.html&quot;&gt;complete quadrilateral&lt;/a&gt;
(&lt;a href=&quot;#deaux-1956&quot;&gt;Deaux 1956&lt;/a&gt;, article 103).&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;&lt;strong&gt;Proof.&lt;/strong&gt; \(DEF\) inversely similar to \(D'E'F'\) translates to:&lt;/p&gt;

\[\begin{vmatrix}
d &amp;amp; \overline{d'} &amp;amp; 1 \\
e &amp;amp; \overline{e'} &amp;amp; 1 \\
f &amp;amp; \overline{f'} &amp;amp; 1
\end{vmatrix} = 0.\]

&lt;p&gt;Since \(D'\) is the reflection of \(D\) across \(BC\), we have
\(d' = \dfrac{(b - c)\overline{d} + \overline{b}c - b\overline{c}}{\overline{b} - \overline{c}}\).
We obtain \(e'\) and \(f'\) similarly.
Plug \(d', e', f'\) into the above equation, which simplifies to:&lt;/p&gt;

\[\begin{vmatrix}
ad &amp;amp; a + d &amp;amp; 1 \\
be &amp;amp; b + e &amp;amp; 1 \\
cf &amp;amp; c + f &amp;amp; 1
\end{vmatrix}
\begin{vmatrix}
a &amp;amp; \overline{a} &amp;amp; 1 \\
b &amp;amp; \overline{b} &amp;amp; 1 \\
c &amp;amp; \overline{c} &amp;amp; 1
\end{vmatrix} = 0.\]

&lt;p&gt;Since \(ABC\) is a non-degenerate triangle, the above reduces to:&lt;/p&gt;

\[\begin{vmatrix}
ad &amp;amp; a + d &amp;amp; 1 \\
be &amp;amp; b + e &amp;amp; 1 \\
cf &amp;amp; c + f &amp;amp; 1
\end{vmatrix} = 0.\]

&lt;p&gt;Therefore, a necessary and sufficient condition
for \(DEF\) and \(D'E'F'\) to be inversely similar
is that \(A, B, C, D, E, F\) are involutoric.
&lt;strong&gt;Q.E.D.&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;Note that by symmetry, this is also the necessary and sufficient condition for
triangles \(ABC\) and \(A'B'C'\) to be inversely similar.&lt;/p&gt;

&lt;p&gt;How are antisimilitude and involution related to the areal property?
Below is my answer to this question in a letter to Zhonghao Ye
(personal communication, 1999; &lt;a href=&quot;#zhao-2011&quot;&gt;Zhao, 2011&lt;/a&gt;).&lt;/p&gt;

&lt;div class=&quot;theorem&quot;&gt;
  &lt;p&gt;&lt;strong&gt;Theorem 2&lt;/strong&gt;:
Consider points \(A, B, C, D, E, F\) in the plane.
Let \(A', B', C', D', E', F'\) be the reflections of \(A, B, C, D, E, F\) across
\(EF, FD, DE, BC, CA, AB\), respectively.&lt;/p&gt;

  &lt;p&gt;If \(A, B, C, D, E, F\) are involutoric,
the sum of the areas of \(ABC\) and \(A'B'C'\) equals
the sum of the areas of \(DEF\) and \(D'E'F'\):&lt;/p&gt;

\[\mathrm{area}\ ABC + \mathrm{area}\ A'B'C' = \mathrm{area}\ DEF + \mathrm{area}\ D'E'F'.\]

&lt;/div&gt;

&lt;p&gt;&lt;strong&gt;Proof.&lt;/strong&gt;
Suppose the involution determined by \(A, B, C, D, E, F\) is:&lt;/p&gt;

\[\varphi(z) = \frac{\alpha z + \beta}{\gamma z + \delta}.\]

&lt;p&gt;This boils down to three cases.&lt;/p&gt;

&lt;p&gt;If \(\gamma = 0\), given \(\varphi(\varphi(z)) = z\), this reduces
to one of the following:&lt;/p&gt;

&lt;ul&gt;
  &lt;li&gt;
    &lt;p&gt;\(\alpha = \delta, \beta = 0\).  In this case,
\(\varphi\) reduces to the identity transformation \(\varphi(z) = z\),
where \(DEF\) coincides with \(ABC\).
We have \(d = a, e = b, f = c\).&lt;/p&gt;
  &lt;/li&gt;
  &lt;li&gt;
    &lt;p&gt;\(\alpha = -\delta\).  In this case,
\(\varphi\) reduces to \(\varphi(z) = -z + {\beta}/{\delta}\),
where \(DEF\) is the symmetry of \(ABC\) with respect to point \(\dfrac{\beta/\delta}{2}\).
Choose that point as the origin.
We have \(d = -a, e = -b, f = -c\).&lt;/p&gt;
  &lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;If \(\gamma \neq 0\), \(\varphi\) has two fixed points \(p_i\) such that
\(\varphi(p_i) = p_i (i = 1, 2)\):&lt;/p&gt;

&lt;ul&gt;
  &lt;li&gt;WLOG, choose the midpoint of \(p_1, p_2\) as the origin and the unit such that
\(p_1 = 1, p_2 = -1\).  In this case,
\(\varphi\) reduces to \(\varphi(z) = 1/z\).
We have \(d = 1/a, e = 1/b, f = 1/c\).&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;The rest is calculation for each of the three cases.
See &lt;a href=&quot;https://github.com/xiw/geometry/tree/master/napoleon/area.mac&quot;&gt;area.mac&lt;/a&gt; in Maxima.
&lt;strong&gt;Q.E.D.&lt;/strong&gt;&lt;/p&gt;

&lt;h2 id=&quot;concyclicity&quot;&gt;Concyclicity&lt;/h2&gt;

&lt;p&gt;Involution is necessary and sufficient for antisimilitude
(e.g., \(DEF\) inversely similar to \(D'E'F'\)).
It is also sufficient for the areal property,
but not necessary,
as evidenced by the following theorem.&lt;/p&gt;

&lt;div class=&quot;theorem&quot;&gt;
  &lt;p&gt;&lt;strong&gt;Theorem 3&lt;/strong&gt;:
Consider points \(A, B, C, D, E, F\) in the plane.
Let \(A', B', C', D', E', F'\) be the reflections of \(A, B, C, D, E, F\) across
\(EF, FD, DE, BC, CA, AB\), respectively.&lt;/p&gt;

  &lt;p&gt;If \(A, B, C, D, E, F\) are concyclic,
the sum of the areas of \(ABC\) and \(A'B'C'\) equals
the sum of the areas of \(DEF\) and \(D'E'F'\):&lt;/p&gt;

\[\mathrm{area}\ ABC + \mathrm{area}\ A'B'C' = \mathrm{area}\ DEF + \mathrm{area}\ D'E'F'.\]

&lt;/div&gt;

&lt;p&gt;
&lt;div id=&quot;cyclic&quot; title=&quot;The areal property (concyclicity)&quot; class=&quot;cindy-canvas&quot;&gt;&lt;/div&gt;
&lt;/p&gt;

&lt;script type=&quot;text/javascript&quot;&gt;
CindyJS({
  ports: [{id: &quot;cyclic&quot;}],
  drawscript: &quot;drawArea&quot;,
  defaultAppearance: defaultAppearance,
  geometry: [
    {name: &quot;O&quot;, type: &quot;Free&quot;, pos: [4, -1], color: [1, 1, 1], visible: false},
    {name: &quot;R&quot;, type: &quot;Free&quot;, pos: [10, -1], color: [1, 1, 1], visible: false},
    {name: &quot;c0&quot;, type: &quot;CircleMP&quot;, args: [&quot;O&quot;, &quot;R&quot;], alpha: .2},

    {name: &quot;A&quot;, type: &quot;PointOnCircle&quot;, args: [&quot;c0&quot;], pos: [ 2,  3], color: [1, 1, 1], labeled: true, printname: &quot;$A$&quot;},
    {name: &quot;B&quot;, type: &quot;PointOnCircle&quot;, args: [&quot;c0&quot;], pos: [-1, -4], color: [1, 1, 1], labeled: true, printname: &quot;$B$&quot;},
    {name: &quot;C&quot;, type: &quot;PointOnCircle&quot;, args: [&quot;c0&quot;], pos: [ 6, -3], color: [1, 1, 1], labeled: true, printname: &quot;$C$&quot;},
    {name: &quot;D&quot;, type: &quot;PointOnCircle&quot;, args: [&quot;c0&quot;], pos: [ 4, -6], color: [1, 1, 1], labeled: true, printname: &quot;$D$&quot;},
    {name: &quot;E&quot;, type: &quot;PointOnCircle&quot;, args: [&quot;c0&quot;], pos: [ 7,  1], color: [1, 1, 1], labeled: true, printname: &quot;$E$&quot;},
    {name: &quot;F&quot;, type: &quot;PointOnCircle&quot;, args: [&quot;c0&quot;], pos: [-1,  1], color: [1, 1, 1], labeled: true, printname: &quot;$F$&quot;},

    {name: &quot;l_BC&quot;, type: &quot;Join&quot;, args: [&quot;B&quot;, &quot;C&quot;], visible: false},
    {name: &quot;l_CA&quot;, type: &quot;Join&quot;, args: [&quot;C&quot;, &quot;A&quot;], visible: false},
    {name: &quot;l_AB&quot;, type: &quot;Join&quot;, args: [&quot;A&quot;, &quot;B&quot;], visible: false},
    {name: &quot;l_EF&quot;, type: &quot;Join&quot;, args: [&quot;E&quot;, &quot;F&quot;], visible: false},
    {name: &quot;l_FD&quot;, type: &quot;Join&quot;, args: [&quot;F&quot;, &quot;D&quot;], visible: false},
    {name: &quot;l_DE&quot;, type: &quot;Join&quot;, args: [&quot;D&quot;, &quot;E&quot;], visible: false},

    {name: &quot;tr_BC&quot;, type: &quot;TrReflection&quot;, args: [&quot;l_BC&quot;]},
    {name: &quot;tr_CA&quot;, type: &quot;TrReflection&quot;, args: [&quot;l_CA&quot;]},
    {name: &quot;tr_AB&quot;, type: &quot;TrReflection&quot;, args: [&quot;l_AB&quot;]},
    {name: &quot;tr_EF&quot;, type: &quot;TrReflection&quot;, args: [&quot;l_EF&quot;]},
    {name: &quot;tr_FD&quot;, type: &quot;TrReflection&quot;, args: [&quot;l_FD&quot;]},
    {name: &quot;tr_DE&quot;, type: &quot;TrReflection&quot;, args: [&quot;l_DE&quot;]},
    {name: &quot;A'&quot;, type: &quot;Transform&quot;, args: [&quot;tr_EF&quot;, &quot;A&quot;], labeled: true, printname: &quot;$A'$&quot;},
    {name: &quot;B'&quot;, type: &quot;Transform&quot;, args: [&quot;tr_FD&quot;, &quot;B&quot;], labeled: true, printname: &quot;$B'$&quot;},
    {name: &quot;C'&quot;, type: &quot;Transform&quot;, args: [&quot;tr_DE&quot;, &quot;C&quot;], labeled: true, printname: &quot;$C'$&quot;},
    {name: &quot;D'&quot;, type: &quot;Transform&quot;, args: [&quot;tr_BC&quot;, &quot;D&quot;], labeled: true, printname: &quot;$D'$&quot;},
    {name: &quot;E'&quot;, type: &quot;Transform&quot;, args: [&quot;tr_CA&quot;, &quot;E&quot;], labeled: true, printname: &quot;$E'$&quot;},
    {name: &quot;F'&quot;, type: &quot;Transform&quot;, args: [&quot;tr_AB&quot;, &quot;F&quot;], labeled: true, printname: &quot;$F'$&quot;},

    {name: &quot;ABC&quot;, type: &quot;Poly&quot;, args: [&quot;A&quot;, &quot;B&quot;, &quot;C&quot;], fillcolor: [1,0,0], fillalpha: .2},
    {name: &quot;DEF&quot;, type: &quot;Poly&quot;, args: [&quot;D&quot;, &quot;E&quot;, &quot;F&quot;], fillcolor: [0,0,1], fillalpha: .2},
    {name: &quot;A'B'C'&quot;, type: &quot;Poly&quot;, args: [&quot;A'&quot;, &quot;B'&quot;, &quot;C'&quot;], fillcolor: [1,0,0], fillalpha: .2},
    {name: &quot;D'E'F'&quot;, type: &quot;Poly&quot;, args: [&quot;D'&quot;, &quot;E'&quot;, &quot;F'&quot;], fillcolor: [0,0,1], fillalpha: .2},

    {name: &quot;AA'&quot;, type: &quot;Segment&quot;, args: [&quot;A&quot;, &quot;A'&quot;], alpha: .2},
    {name: &quot;BB'&quot;, type: &quot;Segment&quot;, args: [&quot;B&quot;, &quot;B'&quot;], alpha: .2},
    {name: &quot;CC'&quot;, type: &quot;Segment&quot;, args: [&quot;C&quot;, &quot;C'&quot;], alpha: .2},
    {name: &quot;DD'&quot;, type: &quot;Segment&quot;, args: [&quot;D&quot;, &quot;D'&quot;], alpha: .2},
    {name: &quot;EE'&quot;, type: &quot;Segment&quot;, args: [&quot;E&quot;, &quot;E'&quot;], alpha: .2},
    {name: &quot;FF'&quot;, type: &quot;Segment&quot;, args: [&quot;F&quot;, &quot;F'&quot;], alpha: .2},
  ]
});
&lt;/script&gt;

&lt;p&gt;&lt;strong&gt;Proof.&lt;/strong&gt;
Choose the circle on which \(A, B, C, D, E, F\) all lie as the unit circle.&lt;/p&gt;

&lt;p&gt;We have \(\overline{a} = 1/a, \overline{b} = 1/b, \overline{c} = 1/c,
\overline{d} = 1/d, \overline{e} = 1/e, \overline{f} = 1/f\).&lt;/p&gt;

&lt;p&gt;The rest of the proof is similar to that of Theorem 2. See &lt;a href=&quot;https://github.com/xiw/geometry/tree/master/napoleon/area.mac&quot;&gt;area.mac&lt;/a&gt;.
&lt;strong&gt;Q.E.D.&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;Theorems 2 and 3 indicate that to unify the areal property
for both involutoric and cyclic cases,
a property “weaker” than antisimilitude is needed,
as detailed next.&lt;/p&gt;

&lt;h2 id=&quot;hexagons-with-opposite-sides-parallel&quot;&gt;Hexagons with opposite sides parallel&lt;/h2&gt;

&lt;p&gt;&lt;a href=&quot;#zhao-2011&quot;&gt;Yong Zhao (2011)&lt;/a&gt; described several new results
regarding the areal property.&lt;/p&gt;

&lt;p&gt;First, Yong Zhao considered the hexagon formed by \(A_1, B_1, C_1, D_1, E_1, F_1\),
the feet of the altitudes from \(A, B, C, D, E, F\) to \(EF, FD, DE, BC, CA, AB\), respectively:
if \(A, B, C, D, E, F\) are involutoric,
the opposite sides of hexagon \(A_1F_1B_1D_1C_1E_1\) are parallel (not necessarily equal in length):
\(A_1F_1 \parallel D_1C_1, F_1B_1 \parallel C_1E_1, B_1D_1 \parallel E_1A_1\).&lt;/p&gt;

&lt;p&gt;
&lt;div id=&quot;parinv&quot; title=&quot;Hexagons with opposite sides parallel (involution)&quot; class=&quot;cindy-canvas&quot;&gt;&lt;/div&gt;
&lt;/p&gt;

&lt;script type=&quot;text/javascript&quot;&gt;
CindyJS({
  ports: [{id: &quot;parinv&quot;}],
  scripts: &quot;inv*&quot;,
  defaultAppearance: defaultAppearance,
  geometry: [
    {name: &quot;A&quot;, type: &quot;Free&quot;, pos: [ 1,  3], color: [1, 1, 1], labeled: true, printname: &quot;$A$&quot;},
    {name: &quot;B&quot;, type: &quot;Free&quot;, pos: [-3, -4], color: [1, 1, 1], labeled: true, printname: &quot;$B$&quot;},
    {name: &quot;C&quot;, type: &quot;Free&quot;, pos: [10, -5], color: [1, 1, 1], labeled: true, printname: &quot;$C$&quot;},
    {name: &quot;D&quot;, type: &quot;Free&quot;, pos: [ 3, -8], color: [1, 1, 1], labeled: true, printname: &quot;$D$&quot;},
    {name: &quot;E&quot;, type: &quot;Free&quot;, pos: [ 7,  3], color: [1, 1, 1], labeled: true, printname: &quot;$E$&quot;},
    {name: &quot;F&quot;, type: &quot;Free&quot;, pos: [-2,  0], color: [1, 1, 1], labeled: true, printname: &quot;$F$&quot;},

    {name: &quot;trA&quot;, type: &quot;TrMoebius&quot;, args: [&quot;B&quot;, &quot;E&quot;, &quot;C&quot;, &quot;F&quot;, &quot;F&quot;, &quot;C&quot;]},
    {name: &quot;trB&quot;, type: &quot;TrMoebius&quot;, args: [&quot;C&quot;, &quot;F&quot;, &quot;A&quot;, &quot;D&quot;, &quot;D&quot;, &quot;A&quot;]},
    {name: &quot;trC&quot;, type: &quot;TrMoebius&quot;, args: [&quot;A&quot;, &quot;D&quot;, &quot;B&quot;, &quot;E&quot;, &quot;E&quot;, &quot;B&quot;]},
    {name: &quot;realA&quot;, type: &quot;Transform&quot;, args: [&quot;trA&quot;, &quot;D&quot;], visible: false},
    {name: &quot;realB&quot;, type: &quot;Transform&quot;, args: [&quot;trB&quot;, &quot;E&quot;], visible: false},
    {name: &quot;realC&quot;, type: &quot;Transform&quot;, args: [&quot;trC&quot;, &quot;F&quot;], visible: false},

    {name: &quot;trD&quot;, type: &quot;TrMoebius&quot;, args: [&quot;E&quot;, &quot;B&quot;, &quot;F&quot;, &quot;C&quot;, &quot;C&quot;, &quot;F&quot;]},
    {name: &quot;trE&quot;, type: &quot;TrMoebius&quot;, args: [&quot;F&quot;, &quot;C&quot;, &quot;D&quot;, &quot;A&quot;, &quot;A&quot;, &quot;D&quot;]},
    {name: &quot;trF&quot;, type: &quot;TrMoebius&quot;, args: [&quot;D&quot;, &quot;A&quot;, &quot;E&quot;, &quot;B&quot;, &quot;B&quot;, &quot;E&quot;]},
    {name: &quot;realD&quot;, type: &quot;Transform&quot;, args: [&quot;trD&quot;, &quot;A&quot;], visible: false},
    {name: &quot;realE&quot;, type: &quot;Transform&quot;, args: [&quot;trE&quot;, &quot;B&quot;], visible: false},
    {name: &quot;realF&quot;, type: &quot;Transform&quot;, args: [&quot;trF&quot;, &quot;C&quot;], visible: false},

    {name: &quot;l_BC&quot;, type: &quot;Join&quot;, args: [&quot;B&quot;, &quot;C&quot;], visible: false},
    {name: &quot;l_CA&quot;, type: &quot;Join&quot;, args: [&quot;C&quot;, &quot;A&quot;], visible: false},
    {name: &quot;l_AB&quot;, type: &quot;Join&quot;, args: [&quot;A&quot;, &quot;B&quot;], visible: false},
    {name: &quot;l_EF&quot;, type: &quot;Join&quot;, args: [&quot;E&quot;, &quot;F&quot;], visible: false},
    {name: &quot;l_FD&quot;, type: &quot;Join&quot;, args: [&quot;F&quot;, &quot;D&quot;], visible: false},
    {name: &quot;l_DE&quot;, type: &quot;Join&quot;, args: [&quot;D&quot;, &quot;E&quot;], visible: false},

    {name: &quot;l_A&quot;, type: &quot;Orthogonal&quot;, args: [&quot;l_EF&quot;, &quot;A&quot;], visible: false},
    {name: &quot;A1&quot;, type: &quot;Meet&quot;, args: [&quot;l_A&quot;, &quot;l_EF&quot;], labeled: true, printname: &quot;$A_1$&quot;},
    {name: &quot;l_B&quot;, type: &quot;Orthogonal&quot;, args: [&quot;l_FD&quot;, &quot;B&quot;], visible: false},
    {name: &quot;B1&quot;, type: &quot;Meet&quot;, args: [&quot;l_B&quot;, &quot;l_FD&quot;], labeled: true, printname: &quot;$B_1$&quot;},
    {name: &quot;l_C&quot;, type: &quot;Orthogonal&quot;, args: [&quot;l_DE&quot;, &quot;C&quot;], visible: false},
    {name: &quot;C1&quot;, type: &quot;Meet&quot;, args: [&quot;l_C&quot;, &quot;l_DE&quot;], labeled: true, printname: &quot;$C_1$&quot;},
    {name: &quot;l_D&quot;, type: &quot;Orthogonal&quot;, args: [&quot;l_BC&quot;, &quot;D&quot;], visible: false},
    {name: &quot;D1&quot;, type: &quot;Meet&quot;, args: [&quot;l_D&quot;, &quot;l_BC&quot;], labeled: true, printname: &quot;$D_1$&quot;},
    {name: &quot;l_E&quot;, type: &quot;Orthogonal&quot;, args: [&quot;l_CA&quot;, &quot;E&quot;], visible: false},
    {name: &quot;E1&quot;, type: &quot;Meet&quot;, args: [&quot;l_E&quot;, &quot;l_CA&quot;], labeled: true, printname: &quot;$E_1$&quot;},
    {name: &quot;l_F&quot;, type: &quot;Orthogonal&quot;, args: [&quot;l_AB&quot;, &quot;F&quot;], visible: false},
    {name: &quot;F1&quot;, type: &quot;Meet&quot;, args: [&quot;l_F&quot;, &quot;l_AB&quot;], labeled: true, printname: &quot;$F_1$&quot;},

    {name: &quot;ABC&quot;, type: &quot;Poly&quot;, args: [&quot;A&quot;, &quot;B&quot;, &quot;C&quot;]},
    {name: &quot;DEF&quot;, type: &quot;Poly&quot;, args: [&quot;D&quot;, &quot;E&quot;, &quot;F&quot;]},
    {name: &quot;Poly1&quot;, type: &quot;Poly&quot;, args: [&quot;A1&quot;, &quot;F1&quot;, &quot;B1&quot;, &quot;D1&quot;, &quot;C1&quot;, &quot;E1&quot;]},

    {name: &quot;AA1&quot;, type: &quot;Segment&quot;, args: [&quot;A&quot;, &quot;A1&quot;], alpha: .2},
    {name: &quot;BB1&quot;, type: &quot;Segment&quot;, args: [&quot;B&quot;, &quot;B1&quot;], alpha: .2},
    {name: &quot;CC1&quot;, type: &quot;Segment&quot;, args: [&quot;C&quot;, &quot;C1&quot;], alpha: .2},
    {name: &quot;DD1&quot;, type: &quot;Segment&quot;, args: [&quot;D&quot;, &quot;D1&quot;], alpha: .2},
    {name: &quot;EE1&quot;, type: &quot;Segment&quot;, args: [&quot;E&quot;, &quot;E1&quot;], alpha: .2},
    {name: &quot;FF1&quot;, type: &quot;Segment&quot;, args: [&quot;F&quot;, &quot;F1&quot;], alpha: .2},
  ]
});
&lt;/script&gt;

&lt;p&gt;Second, it’s straightforward to show that
if \(A, B, C, D, E, F\) are concyclic,
the opposite sides of hexagon \(A_1F_1B_1D_1C_1E_1\) are also parallel.&lt;/p&gt;

&lt;p&gt;
&lt;div id=&quot;parcyclic&quot; title=&quot;Hexagons with opposite sides parallel (concyclicity)&quot; class=&quot;cindy-canvas&quot;&gt;&lt;/div&gt;
&lt;/p&gt;

&lt;script type=&quot;text/javascript&quot;&gt;
CindyJS({
  ports: [{id: &quot;parcyclic&quot;}],
  defaultAppearance: defaultAppearance,
  geometry: [
    {name: &quot;O&quot;, type: &quot;Free&quot;, pos: [3, -2], color: [1, 1, 1], visible: false},
    {name: &quot;R&quot;, type: &quot;Free&quot;, pos: [9, -1], color: [1, 1, 1], visible: false},
    {name: &quot;c0&quot;, type: &quot;CircleMP&quot;, args: [&quot;O&quot;, &quot;R&quot;], alpha: .2},

    {name: &quot;A&quot;, type: &quot;PointOnCircle&quot;, args: [&quot;c0&quot;], pos: [ 2,  3], color: [1, 1, 1], labeled: true, printname: &quot;$A$&quot;},
    {name: &quot;B&quot;, type: &quot;PointOnCircle&quot;, args: [&quot;c0&quot;], pos: [-1, -4], color: [1, 1, 1], labeled: true, printname: &quot;$B$&quot;},
    {name: &quot;C&quot;, type: &quot;PointOnCircle&quot;, args: [&quot;c0&quot;], pos: [ 6, -3], color: [1, 1, 1], labeled: true, printname: &quot;$C$&quot;},
    {name: &quot;D&quot;, type: &quot;PointOnCircle&quot;, args: [&quot;c0&quot;], pos: [ 3, -6], color: [1, 1, 1], labeled: true, printname: &quot;$D$&quot;},
    {name: &quot;E&quot;, type: &quot;PointOnCircle&quot;, args: [&quot;c0&quot;], pos: [ 7,  1], color: [1, 1, 1], labeled: true, printname: &quot;$E$&quot;},
    {name: &quot;F&quot;, type: &quot;PointOnCircle&quot;, args: [&quot;c0&quot;], pos: [-1,  1], color: [1, 1, 1], labeled: true, printname: &quot;$F$&quot;},

    {name: &quot;l_BC&quot;, type: &quot;Join&quot;, args: [&quot;B&quot;, &quot;C&quot;], visible: false},
    {name: &quot;l_CA&quot;, type: &quot;Join&quot;, args: [&quot;C&quot;, &quot;A&quot;], visible: false},
    {name: &quot;l_AB&quot;, type: &quot;Join&quot;, args: [&quot;A&quot;, &quot;B&quot;], visible: false},
    {name: &quot;l_EF&quot;, type: &quot;Join&quot;, args: [&quot;E&quot;, &quot;F&quot;], visible: false},
    {name: &quot;l_FD&quot;, type: &quot;Join&quot;, args: [&quot;F&quot;, &quot;D&quot;], visible: false},
    {name: &quot;l_DE&quot;, type: &quot;Join&quot;, args: [&quot;D&quot;, &quot;E&quot;], visible: false},

    {name: &quot;l_A&quot;, type: &quot;Orthogonal&quot;, args: [&quot;l_EF&quot;, &quot;A&quot;], visible: false},
    {name: &quot;A1&quot;, type: &quot;Meet&quot;, args: [&quot;l_A&quot;, &quot;l_EF&quot;], labeled: true, printname: &quot;$A_1$&quot;},
    {name: &quot;l_B&quot;, type: &quot;Orthogonal&quot;, args: [&quot;l_FD&quot;, &quot;B&quot;], visible: false},
    {name: &quot;B1&quot;, type: &quot;Meet&quot;, args: [&quot;l_B&quot;, &quot;l_FD&quot;], labeled: true, printname: &quot;$B_1$&quot;},
    {name: &quot;l_C&quot;, type: &quot;Orthogonal&quot;, args: [&quot;l_DE&quot;, &quot;C&quot;], visible: false},
    {name: &quot;C1&quot;, type: &quot;Meet&quot;, args: [&quot;l_C&quot;, &quot;l_DE&quot;], labeled: true, printname: &quot;$C_1$&quot;},
    {name: &quot;l_D&quot;, type: &quot;Orthogonal&quot;, args: [&quot;l_BC&quot;, &quot;D&quot;], visible: false},
    {name: &quot;D1&quot;, type: &quot;Meet&quot;, args: [&quot;l_D&quot;, &quot;l_BC&quot;], labeled: true, printname: &quot;$D_1$&quot;},
    {name: &quot;l_E&quot;, type: &quot;Orthogonal&quot;, args: [&quot;l_CA&quot;, &quot;E&quot;], visible: false},
    {name: &quot;E1&quot;, type: &quot;Meet&quot;, args: [&quot;l_E&quot;, &quot;l_CA&quot;], labeled: true, printname: &quot;$E_1$&quot;},
    {name: &quot;l_F&quot;, type: &quot;Orthogonal&quot;, args: [&quot;l_AB&quot;, &quot;F&quot;], visible: false},
    {name: &quot;F1&quot;, type: &quot;Meet&quot;, args: [&quot;l_F&quot;, &quot;l_AB&quot;], labeled: true, printname: &quot;$F_1$&quot;},

    {name: &quot;ABC&quot;, type: &quot;Poly&quot;, args: [&quot;A&quot;, &quot;B&quot;, &quot;C&quot;]},
    {name: &quot;DEF&quot;, type: &quot;Poly&quot;, args: [&quot;D&quot;, &quot;E&quot;, &quot;F&quot;]},
    {name: &quot;Poly1&quot;, type: &quot;Poly&quot;, args: [&quot;A1&quot;, &quot;F1&quot;, &quot;B1&quot;, &quot;D1&quot;, &quot;C1&quot;, &quot;E1&quot;]},

    {name: &quot;AA1&quot;, type: &quot;Segment&quot;, args: [&quot;A&quot;, &quot;A1&quot;], alpha: .2},
    {name: &quot;BB1&quot;, type: &quot;Segment&quot;, args: [&quot;B&quot;, &quot;B1&quot;], alpha: .2},
    {name: &quot;CC1&quot;, type: &quot;Segment&quot;, args: [&quot;C&quot;, &quot;C1&quot;], alpha: .2},
    {name: &quot;DD1&quot;, type: &quot;Segment&quot;, args: [&quot;D&quot;, &quot;D1&quot;], alpha: .2},
    {name: &quot;EE1&quot;, type: &quot;Segment&quot;, args: [&quot;E&quot;, &quot;E1&quot;], alpha: .2},
    {name: &quot;FF1&quot;, type: &quot;Segment&quot;, args: [&quot;F&quot;, &quot;F1&quot;], alpha: .2},
  ]
});
&lt;/script&gt;

&lt;p&gt;Third, the hexagon with opposite sides parallel has the following property:&lt;/p&gt;

\[\mathrm{area}\ A_1B_1C_1 = \mathrm{area}\ D_1E_1F_1.\]

&lt;p&gt;Zhouxing Mao further derived an alternative proof of Theorem 2
using the above property, demonstrating the connection between the areal property
and the hexagon with opposite sides parallel.&lt;/p&gt;

&lt;p&gt;Therefore, the key is to find a necessary and sufficient condition
for the opposite sides of hexagon \(A_1F_1B_1D_1C_1E_1\) to be parallel.&lt;/p&gt;

&lt;p&gt;Libing Huang conjectured the following:&lt;/p&gt;

&lt;div class=&quot;theorem&quot;&gt;
  &lt;p&gt;&lt;strong&gt;Theorem 4.&lt;/strong&gt;
Consider distinct points \(A, B, C, D, E, F\) in the plane.
Let \(A_1, B_1, C_1, D_1, E_1, F_1\) be
the feet of the altitudes from \(A, B, C, D, E, F\) to \(EF, FD, DE, BC, CA, AB\), respectively.&lt;/p&gt;

  &lt;p&gt;The opposite sides of hexagon \(A_1F_1B_1D_1C_1E_1\) are parallel
if and only if \(A, B, C, D, E, F\) are either involutoric or concyclic.&lt;/p&gt;
&lt;/div&gt;

&lt;p&gt;Libing Huang mentioned that he had a computer-aided proof but didn’t provide details.
Below I’ll describe a proof in Maxima.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Proof.&lt;/strong&gt;
The proof of the sufficient condition is similar to those of theorems 2 and 3.
Let’s focus on the necessary condition.&lt;/p&gt;

&lt;p&gt;Since \(A_1\) is the midpoint of \(A\) and \(A'\) (the reflection of \(A\) across \(EF\)), we have:&lt;/p&gt;

\[a_1 = \frac{a + a'}{2} =
\frac{a(\overline{e} - \overline{f}) + \overline{a}(e - f) + \overline{e}f - e\overline{f}}
     {2(\overline{e} - \overline{f})}.\]

&lt;p&gt;We obtain \(b_1, c_1, d_1, e_1, f_1\) similarly.&lt;/p&gt;

&lt;p&gt;\(A_1F_1 \parallel D_1C_1\) translates to:&lt;/p&gt;

\[(a_1 - f_1)(\overline{d_1} - \overline{c_1}) - (\overline{a_1} - \overline{f_1})(d_1 - c_1) = 0.\]

&lt;p&gt;Plug \(a_1, c_1, d_1, f_1\) into the above equation, we have:&lt;/p&gt;

\[\left[(a - b)(\overline{e} - \overline{f}) + (\overline{a} - \overline{b})(e - f)\right]
\left[(b - c)(\overline{d} - \overline{e}) + (\overline{b} - \overline{c})(d - e)\right]
p_1 = 0.\]

&lt;p&gt;The detail of \(p_1\) is omitted due to the complexity (120 subexpressions!).
The other two expressions correspond to the degenerate cases,
\(AB \bot EF\) (\(A_1\) coincides with \(F_1\)) and
\(BC \bot DE\) (\(D_1\) coincides with \(C_1\)),
which we don’t consider.
Therefore, the above equation simplifies to \(p_1 = 0\).&lt;/p&gt;

&lt;p&gt;Similarly, from \(F_1B_1 \parallel C_1E_1\) we obtain \(p_2 = 0\)
(details omitted).&lt;/p&gt;

&lt;p&gt;Eliminating \(\overline{d}\) from \(p_1 = 0, p_2 = 0\)
gives the following two cases:&lt;/p&gt;

\[\begin{vmatrix}
ad &amp;amp; a + d &amp;amp; 1 \\
be &amp;amp; b + e &amp;amp; 1 \\
cf &amp;amp; c + f &amp;amp; 1
\end{vmatrix} = 0\]

\[\left(c - a\right)\left(f - b\right)\left(\overline{c} - \overline{b}\right)\left(\overline{f} - \overline{a}\right) -
      \left(\overline{c} - \overline{a}\right)\left(\overline{f} - \overline{b}\right)\left(c - b\right)\left(f - a\right) = 0.\]

&lt;p&gt;The first corresponds to the case where \(A, B, C, D, E, F\) are involutoric.
The second corresponds to the case where \(A, B, C, F\) are concyclic; similarly,
we obtain that \(A, B, C, D\) and \(A, B, C, E\) are concyclic,
together implying that \(A, B, C, D, E, F\) are concyclic.
See &lt;a href=&quot;https://github.com/xiw/geometry/tree/master/napoleon/parallel.mac&quot;&gt;parallel.mac&lt;/a&gt; in Maxima for the complete proof.
&lt;strong&gt;Q.E.D.&lt;/strong&gt;&lt;/p&gt;

&lt;h2 id=&quot;summary&quot;&gt;Summary&lt;/h2&gt;

&lt;p&gt;Below is a list of the Maxima files used in this post.&lt;/p&gt;
&lt;ul&gt;
  &lt;li&gt;Common library: &lt;a href=&quot;https://github.com/xiw/geometry/blob/master/lib/geometry.mac&quot;&gt;geometry.mac&lt;/a&gt;
(see &lt;a href=&quot;/blog/2020/08/31/automated-geometry-bashing/&quot;&gt;&lt;em&gt;Automated geometry bashing&lt;/em&gt;&lt;/a&gt;)&lt;/li&gt;
  &lt;li&gt;Proof of Theorems 2 and 3: &lt;a href=&quot;https://github.com/xiw/geometry/tree/master/napoleon/area.mac&quot;&gt;area.mac&lt;/a&gt;&lt;/li&gt;
  &lt;li&gt;Proof of Theorem 4: &lt;a href=&quot;https://github.com/xiw/geometry/tree/master/napoleon/parallel.mac&quot;&gt;parallel.mac&lt;/a&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;&lt;strong&gt;Acknowledgments&lt;/strong&gt;: Zhonghao Ye provided a copy of my original proof of Theorems 2 and 3.
Zhilei Xu provided feedback on a draft of this post.&lt;/p&gt;

&lt;h2 id=&quot;references&quot;&gt;References&lt;/h2&gt;

&lt;ul&gt;
  &lt;li&gt;
    &lt;p&gt;&lt;a id=&quot;coxeter-and-greitzer-1967&quot;&gt;&lt;/a&gt;H. S. M. Coxeter and S. L. Greitzer. (1967).
Geometry Revisited.
The Mathematical Association of America.&lt;/p&gt;
  &lt;/li&gt;
  &lt;li&gt;
    &lt;p&gt;&lt;a id=&quot;deaux-1956&quot;&gt;&lt;/a&gt;Roland Deaux. 1956.
Introduction to the Geometry of Complex Numbers.
Translated by Howard Eves.&lt;/p&gt;
  &lt;/li&gt;
  &lt;li&gt;
    &lt;p&gt;&lt;a id=&quot;ye-and-cao-1997&quot;&gt;&lt;/a&gt;Zhonghao Ye and Gang Cao. (1997, September). Solutions to Problem 165.
Bulletin of Mathematics, no. 9. 问题征解栏: 165题的解答. 数学通讯.&lt;/p&gt;
  &lt;/li&gt;
  &lt;li&gt;
    &lt;p&gt;&lt;a id=&quot;zhao-2011&quot;&gt;&lt;/a&gt;Yong Zhao. (2011).
A Survey of Involutoric Hexagons.
完美六边形研究综述 (&lt;a href=&quot;https://bbs.cnool.net/5443327.html&quot;&gt;draft&lt;/a&gt;).&lt;/p&gt;
  &lt;/li&gt;
&lt;/ul&gt;

&lt;style&gt;
.theorem {
  padding-left: .5em;
  border-left: 1px solid;
}
&lt;/style&gt;</content><author><name>Xi Wang</name></author><category term="geometry" /><summary type="html">NB: I came across a survey paper (Zhao, 2011) on some plane geometry problems that a group of friends and I explored around 2000. It’s a nice trip down the memory lane. I played with computer-aided proofs using Maxima recently and thought it might be fun to write down the results.</summary></entry><entry><title type="html">Automated geometry bashing</title><link href="http://kqueue.org/blog/2020/08/31/automated-geometry-bashing/" rel="alternate" type="text/html" title="Automated geometry bashing" /><published>2020-08-31T00:00:00+00:00</published><updated>2020-08-31T00:00:00+00:00</updated><id>http://kqueue.org/blog/2020/08/31/automated-geometry-bashing</id><content type="html" xml:base="http://kqueue.org/blog/2020/08/31/automated-geometry-bashing/">&lt;p&gt;The &lt;a href=&quot;https://imo-grand-challenge.github.io/&quot;&gt;IMO Grand Challenge&lt;/a&gt; aims to “build an AI that can win a gold
medal in the (International Mathematical Olympiad) competition.”
Geometry problems are an attractive target
(e.g., see &lt;a href=&quot;https://mathoverflow.net/questions/337558/automatically-solving-olympiad-geometry-problems&quot;&gt;this MathOverflow discussion&lt;/a&gt; or
&lt;a href=&quot;https://leanprover-community.github.io/archive/stream/208328-IMO-grand-challenge/&quot;&gt;this Lean’s Zulip stream&lt;/a&gt;),
given the rich literature on
&lt;a href=&quot;http://www.mat.uc.pt/~pedro/cientificos/presentationUrbino2019.pdf&quot;&gt;automated theorem proving in geometry&lt;/a&gt;.&lt;/p&gt;

&lt;p&gt;Olympiad participants have long been using well-known &lt;em&gt;computational&lt;/em&gt; techniques
(i.e., “bashing”) to solve geometry problems, such as complex numbers or barycentric coordinates.
How much can we automate these techniques?
As a case study,
I write programs to solve the two geometry problems in &lt;a href=&quot;https://www.imo2019.uk/&quot;&gt;IMO 2019&lt;/a&gt;.
For this post, I use &lt;a href=&quot;http://maxima.sourceforge.net/&quot;&gt;Maxima&lt;/a&gt;, a computer algebra system
(easy to install via apt-get on Debian/Ubuntu or Homebrew on macOS).
The focus is to explore how to encode these geometry problems such that
they can be solved by Maxima within a reasonable amount of time.&lt;/p&gt;

&lt;p&gt;NB: I also tried several SMT solvers that support
&lt;a href=&quot;https://smt-comp.github.io/2020/results/qf-nra-single-query&quot;&gt;nonlinear real arithmetic&lt;/a&gt;.
I was able to prove &lt;a href=&quot;https://en.wikipedia.org/wiki/Napoleon%27s_theorem&quot;&gt;Napoleon’s theorem&lt;/a&gt; (the first example below),
but not the other two IMO 2019 problems (with a 30-minute timeout).
If you have success in finding the right SMT encodings, please let me know!&lt;/p&gt;

&lt;h2 id=&quot;complex-numbers&quot;&gt;Complex numbers&lt;/h2&gt;

&lt;p&gt;There are many good notes on encoding geometry problems using complex numbers.
Search for “complex bashing” or refer to Evan Chen’s
“&lt;a href=&quot;https://web.evanchen.cc/handouts/cmplx/en-cmplx.pdf&quot;&gt;Bashing Geometry with Complex Numbers&lt;/a&gt;”.
The high-level idea is to represent each point with Cartesian coordinate \((x, y)\)
as complex number \(z = x + yi\). This often leads to simpler calculation.&lt;/p&gt;

&lt;p&gt;The rest of the post uses \(a\) (in lower case) to denote
the complex number of the corresponding point \(A\) (in upper case).
Below are a few examples.&lt;/p&gt;
&lt;ul&gt;
  &lt;li&gt;The reflection of point \(A\) over the real axis is:
\(\overline{a}\) (i.e., the conjugate of \(a\)).&lt;/li&gt;
  &lt;li&gt;The rotation of point \(A\) about point \(B\) by counterclockwise angle \(\theta\) is:
\((a - b) \exp(i\theta) + b\).&lt;/li&gt;
  &lt;li&gt;The centroid of triangle \(ABC\) is:
\({(a + b + c)}/{3}\).&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;See the file &lt;a href=&quot;https://github.com/xiw/geometry/blob/master/lib/geometry.mac&quot;&gt;geometry.mac&lt;/a&gt; for more results (e.g., collinearity, concyclicity, and intersection)
coded up in Maxima, which will be used for later proofs.&lt;/p&gt;

&lt;p&gt;As a simple example, let’s prove &lt;a href=&quot;https://en.wikipedia.org/wiki/Napoleon%27s_theorem&quot;&gt;Napoleon’s theorem&lt;/a&gt;:
the centers of equilateral triangles constructed outward on the sides of a triangle
form an equilateral triangle (known as the &lt;em&gt;outer Napoleon triangle&lt;/em&gt;).&lt;/p&gt;

&lt;p&gt;
&lt;div id=&quot;napoleon&quot; title=&quot;Napoleon's theorem&quot; class=&quot;cindy-canvas&quot;&gt;&lt;/div&gt;
&lt;/p&gt;

&lt;script type=&quot;text/javascript&quot;&gt;
CindyJS({
  ports: [{id: &quot;napoleon&quot;}],
  defaultAppearance: defaultAppearance,
  geometry: [
    {name: &quot;A&quot;, type: &quot;Free&quot;, pos: [ 1,  3], color: [1, 1, 1], labeled: true, printname: &quot;$A$&quot;},
    {name: &quot;B&quot;, type: &quot;Free&quot;, pos: [-1, -3], color: [1, 1, 1], labeled: true, printname: &quot;$B$&quot;},
    {name: &quot;C&quot;, type: &quot;Free&quot;, pos: [ 5, -3], color: [1, 1, 1], labeled: true, printname: &quot;$C$&quot;},
    {name: &quot;BC&quot;, type: &quot;Segment&quot;, args: [&quot;B&quot;, &quot;C&quot;]},
    {name: &quot;CA&quot;, type: &quot;Segment&quot;, args: [&quot;C&quot;, &quot;A&quot;]},
    {name: &quot;AB&quot;, type: &quot;Segment&quot;, args: [&quot;A&quot;, &quot;B&quot;]},
    {name: &quot;l_BL&quot;, type: &quot;LineByFixedAngle&quot;, angle: -60 * Math.PI / 180, args: [&quot;BC&quot;, &quot;B&quot;], visible: false},
    {name: &quot;l_CL&quot;, type: &quot;LineByFixedAngle&quot;, angle:  60 * Math.PI / 180, args: [&quot;BC&quot;, &quot;C&quot;], visible: false},
    {name: &quot;L&quot;, type: &quot;Meet&quot;, args: [&quot;l_BL&quot;, &quot;l_CL&quot;], labeled: true, printname: &quot;$M_1$&quot;},
    {name: &quot;BL&quot;, type: &quot;Segment&quot;, args: [&quot;B&quot;, &quot;L&quot;]},
    {name: &quot;CL&quot;, type: &quot;Segment&quot;, args: [&quot;C&quot;, &quot;L&quot;]},
    {name: &quot;c_BCL&quot;, type: &quot;CircleBy3&quot;, args: [&quot;B&quot;, &quot;C&quot;, &quot;L&quot;], visible: false},
    {name: &quot;D&quot;, type: &quot;CenterOfConic&quot;, args: [&quot;c_BCL&quot;], labeled: true, printname: &quot;$N_1$&quot;},
    {name: &quot;l_CM&quot;, type: &quot;LineByFixedAngle&quot;, angle: -60 * Math.PI / 180, args: [&quot;CA&quot;, &quot;C&quot;], visible: false},
    {name: &quot;l_AM&quot;, type: &quot;LineByFixedAngle&quot;, angle:  60 * Math.PI / 180, args: [&quot;CA&quot;, &quot;A&quot;], visible: false},
    {name: &quot;M&quot;, type: &quot;Meet&quot;, args: [&quot;l_CM&quot;, &quot;l_AM&quot;], labeled: true, printname: &quot;$M_2$&quot;},
    {name: &quot;CM&quot;, type: &quot;Segment&quot;, args: [&quot;C&quot;, &quot;M&quot;]},
    {name: &quot;AM&quot;, type: &quot;Segment&quot;, args: [&quot;A&quot;, &quot;M&quot;]},
    {name: &quot;c_CAM&quot;, type: &quot;CircleBy3&quot;, args: [&quot;C&quot;, &quot;A&quot;, &quot;M&quot;], visible: false},
    {name: &quot;E&quot;, type: &quot;CenterOfConic&quot;, args: [&quot;c_CAM&quot;], labeled: true, printname: &quot;$N_2$&quot;},
    {name: &quot;l_AN&quot;, type: &quot;LineByFixedAngle&quot;, angle: -60 * Math.PI / 180, args: [&quot;AB&quot;, &quot;A&quot;], visible: false},
    {name: &quot;l_BN&quot;, type: &quot;LineByFixedAngle&quot;, angle:  60 * Math.PI / 180, args: [&quot;AB&quot;, &quot;B&quot;], visible: false},
    {name: &quot;N&quot;, type: &quot;Meet&quot;, args: [&quot;l_AN&quot;, &quot;l_BN&quot;], labeled: true, printname: &quot;$M_3$&quot;},
    {name: &quot;AN&quot;, type: &quot;Segment&quot;, args: [&quot;A&quot;, &quot;N&quot;]},
    {name: &quot;BN&quot;, type: &quot;Segment&quot;, args: [&quot;B&quot;, &quot;N&quot;]},
    {name: &quot;c_ABN&quot;, type: &quot;CircleBy3&quot;, args: [&quot;A&quot;, &quot;B&quot;, &quot;N&quot;], visible: false},
    {name: &quot;F&quot;, type: &quot;CenterOfConic&quot;, args: [&quot;c_ABN&quot;], labeled: true, printname: &quot;$N_3$&quot;},
    {name: &quot;DEF&quot;, type: &quot;Poly&quot;, fillcolor: [0, 1, 0], fillalpha: 0.5, args: [&quot;D&quot;, &quot;E&quot;, &quot;F&quot;], alpha: 0.2}
  ]
});
&lt;/script&gt;

&lt;p&gt;Let’s do the calculation manually first. \(A, B, C\) are free points.&lt;/p&gt;

&lt;p&gt;\(M_1\) is the rotation of \(B\) about \(C\) by \(60\degree\) (i.e., \(\pi/3\)):&lt;/p&gt;

\[m_1 = (b - c)\exp(i\pi/3) + c = \dfrac{(\sqrt{3}i + 1)b - (\sqrt{3}i - 1)c}{2}.\]

&lt;p&gt;\(N_1\) is the center (centroid) of equilateral triangle \(M_1BC\), \((m_1 + b + c) / 3\).
Therefore, we have:&lt;/p&gt;

\[n_1 = \dfrac{(\sqrt{3}i + 3)b - (\sqrt{3}i - 3)c}{6}.\]

&lt;p&gt;Similarly,&lt;/p&gt;

\[\begin{aligned}
n_2 &amp;amp; = \dfrac{(\sqrt{3}i + 3)c - (\sqrt{3}i - 3)a}{6} \\
n_3 &amp;amp; = \dfrac{(\sqrt{3}i + 3)a - (\sqrt{3}i - 3)b}{6}.
\end{aligned}\]

&lt;p&gt;With all the points calculated, it’s straightforward to show that triangle \(N_1N_2N_3\) is
equilateral using any of the following encodings for equilaterality:&lt;/p&gt;
&lt;ul&gt;
  &lt;li&gt;\((n_3 - n_2)\exp(i\pi/3) + n_2 = n_1\),&lt;/li&gt;
  &lt;li&gt;\(\lvert n_1 - n_2\rvert = \lvert n_2 - n_3\rvert = \lvert n_3 - n_1\rvert\),&lt;/li&gt;
  &lt;li&gt;\(n_1^2 + n_2^2 + n_3^2 = n_1n_2 + n_2n_3 + n_3n_1\) (see &lt;a href=&quot;https://proofwiki.org/wiki/Vertices_of_Equilateral_Triangle_in_Complex_Plane&quot;&gt;proof&lt;/a&gt;).&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Now let’s automate the calculation using the following Maxima program:&lt;/p&gt;

&lt;pre&gt;&lt;code class=&quot;language-maxima&quot;&gt;load(&quot;geometry.mac&quot;);
  
/* Declare triangle ABC. */
declare([a, b, c], complex);

/* Compute the outer Napoleon triangle. */
p(x, y) := centroid(x, y, rotate(x, y, %pi/3));
[n1, n2, n3] : [p(b, c), p(c, a), p(a, b)];

/* Prove that the outer Napoleon triangle is equilateral. */
prove(equilateral(n1, n2, n3));

quit();
&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;The &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;prove&lt;/code&gt; function (defined in &lt;a href=&quot;https://github.com/xiw/geometry/blob/master/lib/geometry.mac&quot;&gt;geometry.mac&lt;/a&gt;) calls
Maxima’s built-in &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;is&lt;/code&gt; function to determine whether a predicate is &lt;em&gt;provably&lt;/em&gt; true.
Below is the execution trace of this program:&lt;/p&gt;
&lt;pre&gt;&lt;code class=&quot;language-maxima-console&quot;&gt;% maxima -b napoleon.mac
Maxima 5.44.0 http://maxima.sourceforge.net
using Lisp SBCL 2.0.5
Distributed under the GNU Public License. See the file COPYING.
Dedicated to the memory of William Schelter.
The function bug_report() provides bug reporting information.
(%i1) batch(&quot;napoleon.mac&quot;)

read and interpret napoleon.mac
(%i2) load(&quot;geometry.mac&quot;)
(%o2)                             geometry.mac
(%i3) declare([a,b,c],complex)
(%o3)                                done
(%i4) p(x,y):=centroid(x,y,rotate(x,y,%pi/3))
                                                        %pi
(%o4)            p(x, y) := centroid(x, y, rotate(x, y, ---))
                                                         3
(%i5) [n1,n2,n3]:[p(b,c),p(c,a),p(a,b)]
              sqrt(3) %i   1                sqrt(3) %i   1
       2 c + (---------- + -) (b - c) + b  (---------- + -) (c - a) + c + 2 a
                  2        2                    2        2
(%o5) [----------------------------------, ----------------------------------, 
                       3                                   3
                                                   sqrt(3) %i   1
                                            2 b + (---------- + -) (a - b) + a
                                                       2        2
                                            ----------------------------------]
                                                            3
(%i6) prove(equilateral(n1,n2,n3))
(%o6)                                true
(%i7) quit()
&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;If we attempt to prove something false, such as \(N_1N_2C\) equilateral,
the execution fails:&lt;/p&gt;

&lt;pre&gt;&lt;code class=&quot;language-maxima-console&quot;&gt;(%i6) prove(equilateral(n1,n2,c))
prove(equilateral(n1,n2,c))
Unable to evaluate predicate errexp1
...
 -- an error. To debug this try: debugmode(true);
&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;&lt;strong&gt;Exercise 1&lt;/strong&gt;: Extend the above program to prove that the center of
the outer Napoleon triangle \(N_1N_2N_3\) corresponds to the centroid
of triangle \(ABC\).&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Exercise 2&lt;/strong&gt;: Prove Napoleon’s theorem using an SMT solver or Gröbner bases.&lt;/p&gt;

&lt;ul&gt;
  &lt;li&gt;
    &lt;p&gt;For SMT, you may find Leonardo de Moura’s
“&lt;a href=&quot;https://leodemoura.github.io/blog/2013/01/26/complex.html&quot;&gt;Complex Numbers in Z3&lt;/a&gt;” useful.
To make the code work in Python 3,
change &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;__div__&lt;/code&gt; to &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;__truediv__&lt;/code&gt; and &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;__rdiv__&lt;/code&gt; to &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;__rtruediv__&lt;/code&gt;.&lt;/p&gt;
  &lt;/li&gt;
  &lt;li&gt;
    &lt;p&gt;For Gröbner bases, you may find Dan Roozemond’s
“&lt;a href=&quot;http://magma.maths.usyd.edu.au/~danr/site/pubs/0310ProvingCinderella.pdf&quot;&gt;Gröbner Bases in Practice&lt;/a&gt;”
chapter useful.  The paper uses the &lt;span style=&quot;font-variant:small-caps;&quot;&gt;&lt;a href=&quot;https://www.singular.uni-kl.de/&quot;&gt;Singular&lt;/a&gt;&lt;/span&gt;
computer algebra system.  &lt;a href=&quot;http://maxima.sourceforge.net/&quot;&gt;Maxima&lt;/a&gt; and &lt;a href=&quot;https://www.sympy.org/&quot;&gt;SymPy&lt;/a&gt; should also work.&lt;/p&gt;
  &lt;/li&gt;
&lt;/ul&gt;

&lt;h2 id=&quot;imo-2019-problem-2&quot;&gt;IMO 2019, problem 2&lt;/h2&gt;

&lt;p&gt;In triangle \(ABC\), point \(A_1\) lies on side \(BC\) and
point \(B_1\) lies on side \(AC\).
Let \(P\) and \(Q\) be points on segments \(AA_1\) and \(BB_1\), respectively,
such that \(PQ\) is parallel to \(AB\).
Let \(P_1\) be a point on line \(PB_1\), such that \(B_1\) lies strictly between
\(P\) and \(P_1\), and \(\angle PP_1C = \angle BAC\).
Similarly, let \(Q_1\) be a point on line \(QA_1\), such that \(A_1\) lies strictly between
\(Q\) and \(Q_1\), and \(\angle CQ_1Q = \angle CBA\).&lt;/p&gt;

&lt;p&gt;Prove that points \(P\), \(Q\), \(P_1\), and \(Q_1\) are concyclic.&lt;/p&gt;

&lt;p&gt;
&lt;div id=&quot;imo2019p2&quot; title=&quot;IMO 2019, problem 2&quot; class=&quot;cindy-canvas&quot;&gt;&lt;/div&gt;
&lt;/p&gt;

&lt;script type=&quot;text/javascript&quot;&gt;
CindyJS({
  ports: [{id: &quot;imo2019p2&quot;}],
  defaultAppearance: defaultAppearance,
  geometry: [
    // ABC
    {name: &quot;A&quot;, type: &quot;Free&quot;, pos: [-4, -8], color: [1, 1, 1], labeled: true, printname: &quot;$A$&quot;},
    {name: &quot;B&quot;, type: &quot;Free&quot;, pos: [ 8, -8], color: [1, 1, 1], labeled: true, printname: &quot;$B$&quot;},
    {name: &quot;C&quot;, type: &quot;Free&quot;, pos: [ 1,  0], color: [1, 1, 1], labeled: true, printname: &quot;$C$&quot;},
    {name: &quot;c0&quot;, type: &quot;ArcBy3&quot;, args: [&quot;B&quot;, &quot;C&quot;, &quot;A&quot;], visible: false},
    {name: &quot;BC&quot;, type: &quot;Segment&quot;, args: [&quot;B&quot;, &quot;C&quot;]},
    {name: &quot;CA&quot;, type: &quot;Segment&quot;, args: [&quot;C&quot;, &quot;A&quot;]},
    {name: &quot;AB&quot;, type: &quot;Segment&quot;, args: [&quot;A&quot;, &quot;B&quot;]},
    // A1
    {name: &quot;A1&quot;, type: &quot;PointOnLine&quot;, args: [&quot;BC&quot;], pos: [6, -2.5], labeled: true, printname: &quot;$A_1$&quot;},
    {name: &quot;AA1&quot;, type: &quot;Segment&quot;, args: [&quot;A&quot;, &quot;A1&quot;]},
    {name: &quot;A2&quot;, type: &quot;OtherIntersectionCL&quot;, args: [&quot;c0&quot;, &quot;AA1&quot;, &quot;A&quot;], visible: false},
    // B1
    {name: &quot;B1&quot;, type: &quot;PointOnLine&quot;, args: [&quot;CA&quot;], pos: [-2.5, -3.5], labeled: true, printname: &quot;$B_1$&quot;},
    {name: &quot;BB1&quot;, type: &quot;Segment&quot;, args: [&quot;B&quot;, &quot;B1&quot;]},
    {name: &quot;B2&quot;, type: &quot;OtherIntersectionCL&quot;, args: [&quot;c0&quot;, &quot;BB1&quot;, &quot;B&quot;], visible: false},
    // PQ
    {name: &quot;K&quot;, type: &quot;Free&quot;, pos: [ 1.6, -6.5], color: [1, 1, 1], labeled: true, printname: &quot;$K$&quot;},
    {name: &quot;l_PQ&quot;, type: &quot;Parallel&quot;, args: [&quot;AB&quot;, &quot;K&quot;], alpha: .2},
    {name: &quot;l_AA2&quot;, type: &quot;Join&quot;, args: [&quot;A&quot;, &quot;A2&quot;], visible: false},
    {name: &quot;l_BB2&quot;, type: &quot;Join&quot;, args: [&quot;B&quot;, &quot;B2&quot;], visible: false},
    {name: &quot;P&quot;, type: &quot;Meet&quot;, args: [&quot;l_AA2&quot;, &quot;l_PQ&quot;], labeled: true, printname: &quot;$P$&quot;},
    {name: &quot;Q&quot;, type: &quot;Meet&quot;, args: [&quot;l_BB2&quot;, &quot;l_PQ&quot;], labeled: true, printname: &quot;$Q$&quot;},
    {name: &quot;PQ&quot;, type: &quot;Segment&quot;, args: [&quot;P&quot;, &quot;Q&quot;]},
    // P1
    {name: &quot;c_P&quot;, type: &quot;CircleBy3&quot;, args: [&quot;C&quot;, &quot;B1&quot;, &quot;B2&quot;], visible: false},
    {name: &quot;l_PB1&quot;, type: &quot;Join&quot;, args: [&quot;P&quot;, &quot;B1&quot;], visible: false},
    {name: &quot;P1&quot;, type: &quot;OtherIntersectionCL&quot;, args: [&quot;c_P&quot;, &quot;l_PB1&quot;, &quot;B1&quot;], labeled: true, printname: &quot;$P_1$&quot;},
    {name: &quot;PP1&quot;, type: &quot;Segment&quot;, args: [&quot;P&quot;, &quot;P1&quot;]},
    {name: &quot;CP1&quot;, type: &quot;Segment&quot;, args: [&quot;C&quot;, &quot;P1&quot;]},
    // Q1
    {name: &quot;c_Q&quot;, type: &quot;CircleBy3&quot;, args: [&quot;C&quot;, &quot;A1&quot;, &quot;A2&quot;], visible: false},
    {name: &quot;l_QA1&quot;, type: &quot;Join&quot;, args: [&quot;Q&quot;, &quot;A1&quot;], visible: false},
    {name: &quot;Q1&quot;, type: &quot;OtherIntersectionCL&quot;, args: [&quot;c_Q&quot;, &quot;l_QA1&quot;, &quot;A1&quot;], labeled: true, printname: &quot;$Q_1$&quot;},
    {name: &quot;QP1&quot;, type: &quot;Segment&quot;, args: [&quot;Q&quot;, &quot;Q1&quot;]},
    {name: &quot;CQ1&quot;, type: &quot;Segment&quot;, args: [&quot;C&quot;, &quot;Q1&quot;]},
    // P1PQQ1
    {name: &quot;c_PQ&quot;, type: &quot;CircleBy3&quot;, args: [&quot;P1&quot;, &quot;P&quot;, &quot;Q1&quot;], alpha: .2},
  ]
});
&lt;/script&gt;

&lt;p&gt;See the solution in Maxima &lt;a href=&quot;https://github.com/xiw/geometry/blob/master/imo2019/imo2019p2.mac&quot;&gt;imo2019p2.mac&lt;/a&gt;.&lt;/p&gt;

&lt;p&gt;Here’s the high-level flow.
\(A, B, C\) are free points,
from which we derive \(A_1\) and \(B_1\).
We derive \(P\) and \(Q\) by introducing another free point \(K\),
the line through which parallel to \(AB\)
intersects \(AA_1\) and \(BB_1\) at \(P\) and \(Q\), respectively.
The remaining question is how to encode \(P_1\) and \(Q_1\).&lt;/p&gt;

&lt;p&gt;First,
\(P_1\) on \(PB_1\) may translate to:&lt;/p&gt;

\[\frac{p_1 - b_1}{b_1 - p} = \overline{\left(\frac{p_1 - b_1}{b_1 - p}\right)}.\]

&lt;p&gt;\(\angle PP_1C = \angle BAC\) may translate to:&lt;/p&gt;

\[\frac{p_1 - c}{p_1 - p} / \frac{a - c}{a - b} =
\overline{\left(\frac{p_1 - c}{p_1 - p} / \frac{a - c}{a - b}\right)}.\]

&lt;p&gt;Since \(a, b, c, p\) are known at this point,
one may solve \(p_1\) by eliminating \(\overline{p_1}\) from the above two equations
(by hand or using Maxima’s &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;solve&lt;/code&gt; function).
One may solve \(q_1\) similarly.&lt;/p&gt;

&lt;p&gt;Next, it’s straightforward to prove that \(P, Q, P_1, Q_1\) are concyclic by testing the following:&lt;/p&gt;

\[\frac{p_1 - p}{p_1 - q} / \frac{q_1 - p}{q_1 - q} =
\overline{\left(\frac{p_1 - p}{p_1 - q} / \frac{q_1 - p}{q_1 - q}\right)}.\]

&lt;p&gt;Maxima reported the following error on my first attempt:&lt;/p&gt;
&lt;div class=&quot;language-plaintext highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;PQUOTIENT: Quotient by a polynomial of higher degree (case 2b)
 -- an error. To debug this try: debugmode(true);
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;I worked around this error by choosing \(C\) as the origin (i.e., \(c = 0\)),
with which Maxima is able to finish the proof.&lt;/p&gt;

&lt;p&gt;Notice that the problem description is not &lt;em&gt;constructive&lt;/em&gt;:
it asserts the existence of points \(P_1\) and \(Q_1\) but doesn’t say
how to construct them—imagine how to draw the figure without solving polynomials.
Below is one way to rephrase the problem (in fact, that’s how I drew the above figure),
inspired by “solution 1” of this problem’s &lt;a href=&quot;https://www.imo2019.uk/&quot;&gt;reference solutions&lt;/a&gt;.&lt;/p&gt;

&lt;p&gt;Problem 2*:
Points \(A_2\) and \(B_2\) are on the circumcircle of triangle \(ABC\).
\(AA_2\) intersects \(BC\) at \(A_1\).
\(BB_2\) intersects \(CA\) at \(B_1\).
Let \(P\) and \(Q\) be points on segments \(AA_1\) and \(BB_1\), respectively,
such that \(PQ\) is parallel to \(AB\).
\(PB_1\) intersects the circumcircle of triangle \(CB_1B_2\) again at \(P_1\).
\(QA_1\) intersects the circumcircle of triangle \(CA_1A_2\) again at \(Q_1\).&lt;/p&gt;

&lt;p&gt;Prove that points \(A_2, B_2, P, Q, P_1, Q_1\) are concyclic.&lt;/p&gt;

&lt;p&gt;
&lt;div id=&quot;imo2019p2e3&quot; title=&quot;IMO 2019, problem 2&quot; class=&quot;cindy-canvas&quot;&gt;&lt;/div&gt;
&lt;/p&gt;

&lt;script type=&quot;text/javascript&quot;&gt;
CindyJS({
  ports: [{id: &quot;imo2019p2e3&quot;}],
  defaultAppearance: defaultAppearance,
  geometry: [
    // ABC
    {name: &quot;A&quot;, type: &quot;Free&quot;, pos: [-4, -8], color: [1, 1, 1], labeled: true, printname: &quot;$A$&quot;},
    {name: &quot;B&quot;, type: &quot;Free&quot;, pos: [ 8, -8], color: [1, 1, 1], labeled: true, printname: &quot;$B$&quot;},
    {name: &quot;C&quot;, type: &quot;Free&quot;, pos: [ 1,  0], color: [1, 1, 1], labeled: true, printname: &quot;$C$&quot;},
    {name: &quot;c0&quot;, type: &quot;ArcBy3&quot;, args: [&quot;B&quot;, &quot;C&quot;, &quot;A&quot;], alpha: .2},
    {name: &quot;BC&quot;, type: &quot;Segment&quot;, args: [&quot;B&quot;, &quot;C&quot;]},
    {name: &quot;CA&quot;, type: &quot;Segment&quot;, args: [&quot;C&quot;, &quot;A&quot;]},
    {name: &quot;AB&quot;, type: &quot;Segment&quot;, args: [&quot;A&quot;, &quot;B&quot;]},
    // A1
    {name: &quot;A2&quot;, type: &quot;PointOnCircle&quot;, args: [&quot;c0&quot;], pos: [7, -2], color: [1, 1, 1], labeled: true, printname: &quot;$A_2$&quot;},
    {name: &quot;AA2&quot;, type: &quot;Segment&quot;, args: [&quot;A&quot;, &quot;A2&quot;]},
    {name: &quot;A1&quot;, type: &quot;Meet&quot;, args: [&quot;BC&quot;, &quot;AA2&quot;], labeled: true, printname: &quot;$A_1$&quot;},
    // B1
    {name: &quot;B2&quot;, type: &quot;PointOnCircle&quot;, args: [&quot;c0&quot;], pos: [-3, -3], color: [1, 1, 1], labeled: true, printname: &quot;$B_2$&quot;},
    {name: &quot;BB2&quot;, type: &quot;Segment&quot;, args: [&quot;B&quot;, &quot;B2&quot;]},
    {name: &quot;B1&quot;, type: &quot;Meet&quot;, args: [&quot;CA&quot;, &quot;BB2&quot;], labeled: true, printname: &quot;$B_1$&quot;},
    // PQ
    {name: &quot;K&quot;, type: &quot;Free&quot;, pos: [ 1.6, -6.5], color: [1, 1, 1]},
    {name: &quot;l_PQ&quot;, type: &quot;Parallel&quot;, args: [&quot;AB&quot;, &quot;K&quot;], alpha: .2},
    {name: &quot;l_AA2&quot;, type: &quot;Join&quot;, args: [&quot;A&quot;, &quot;A2&quot;], visible: false},
    {name: &quot;l_BB2&quot;, type: &quot;Join&quot;, args: [&quot;B&quot;, &quot;B2&quot;], visible: false},
    {name: &quot;P&quot;, type: &quot;Meet&quot;, args: [&quot;l_AA2&quot;, &quot;l_PQ&quot;], labeled: true, printname: &quot;$P$&quot;},
    {name: &quot;Q&quot;, type: &quot;Meet&quot;, args: [&quot;l_BB2&quot;, &quot;l_PQ&quot;], labeled: true, printname: &quot;$Q$&quot;},
    {name: &quot;PQ&quot;, type: &quot;Segment&quot;, args: [&quot;P&quot;, &quot;Q&quot;]},
    // P1
    {name: &quot;c_P&quot;, type: &quot;CircleBy3&quot;, args: [&quot;C&quot;, &quot;B1&quot;, &quot;B2&quot;], alpha: .2},
    {name: &quot;l_PB1&quot;, type: &quot;Join&quot;, args: [&quot;P&quot;, &quot;B1&quot;], visible: false},
    {name: &quot;P1&quot;, type: &quot;OtherIntersectionCL&quot;, args: [&quot;c_P&quot;, &quot;l_PB1&quot;, &quot;B1&quot;], labeled: true, printname: &quot;$P_1$&quot;},
    {name: &quot;PP1&quot;, type: &quot;Segment&quot;, args: [&quot;P&quot;, &quot;P1&quot;]},
    {name: &quot;CP1&quot;, type: &quot;Segment&quot;, args: [&quot;C&quot;, &quot;P1&quot;]},
    // Q1
    {name: &quot;c_Q&quot;, type: &quot;CircleBy3&quot;, args: [&quot;C&quot;, &quot;A1&quot;, &quot;A2&quot;], alpha: .2},
    {name: &quot;l_QA1&quot;, type: &quot;Join&quot;, args: [&quot;Q&quot;, &quot;A1&quot;], visible: false},
    {name: &quot;Q1&quot;, type: &quot;OtherIntersectionCL&quot;, args: [&quot;c_Q&quot;, &quot;l_QA1&quot;, &quot;A1&quot;], labeled: true, printname: &quot;$Q_1$&quot;},
    {name: &quot;QP1&quot;, type: &quot;Segment&quot;, args: [&quot;Q&quot;, &quot;Q1&quot;]},
    {name: &quot;CQ1&quot;, type: &quot;Segment&quot;, args: [&quot;C&quot;, &quot;Q1&quot;]},
    // P1PQQ1
    {name: &quot;c_PQ&quot;, type: &quot;CircleBy3&quot;, args: [&quot;P1&quot;, &quot;P&quot;, &quot;Q1&quot;], alpha: .2},
    {name: &quot;CA2&quot;, type: &quot;Segment&quot;, args: [&quot;C&quot;, &quot;A2&quot;], alpha: .2},
    {name: &quot;CB2&quot;, type: &quot;Segment&quot;, args: [&quot;C&quot;, &quot;B2&quot;], alpha: .2},
  ]
});
&lt;/script&gt;

&lt;p&gt;It’s easy to see that this constructive version is equivalent to the original problem,
given \(\angle PP_1C = \angle B_1B_2C = \angle CAB\)
and \(\angle QQ_1C = \angle A_1A_2C = \angle CBA\).&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Exercise 3&lt;/strong&gt;: Write a Maxima program to prove problem 2*.&lt;/p&gt;

&lt;h2 id=&quot;imo-2019-problem-6&quot;&gt;IMO 2019, problem 6&lt;/h2&gt;

&lt;p&gt;Let \(I\) be the incenter of acute triangle \(ABC\) with \(AB \neq AC\).
The incircle \(\omega\) of \(ABC\) is tangent to sides \(BC\), \(CA\), and \(AB\)
at \(D\), \(E\), and \(F\), respectively.
The line through \(D\) perpendicular to \(EF\) meets \(\omega\) again at \(R\).
Line \(AR\) meets \(\omega\) again at \(P\).
The circumcircles of triangles \(PCE\) and \(PBF\) meet again at \(Q\).&lt;/p&gt;

&lt;p&gt;Prove that lines \(DI\) and \(PQ\) meet on the line through \(A\)
perpendicular to \(AI\).&lt;/p&gt;

&lt;p&gt;
&lt;div id=&quot;imo2019p6&quot; title=&quot;IMO 2019, problem 6&quot; class=&quot;cindy-canvas&quot;&gt;&lt;/div&gt;
&lt;/p&gt;

&lt;script type=&quot;text/javascript&quot;&gt;
CindyJS({
  ports: [{id: &quot;imo2019p6&quot;}],
  defaultAppearance: defaultAppearance,
  geometry: [
    // ABC
    {name: &quot;A&quot;, type: &quot;Free&quot;, pos: [5.2, 3], color: [1, 1, 1], labeled: true, printname: &quot;$A$&quot;},
    {name: &quot;B&quot;, type: &quot;Free&quot;, pos: [-4, -6], color: [1, 1, 1], labeled: true, printname: &quot;$B$&quot;},
    {name: &quot;C&quot;, type: &quot;Free&quot;, pos: [ 8, -6], color: [1, 1, 1], labeled: true, printname: &quot;$C$&quot;},
    {name: &quot;BC&quot;, type: &quot;Segment&quot;, args: [&quot;B&quot;, &quot;C&quot;]},
    {name: &quot;CA&quot;, type: &quot;Segment&quot;, args: [&quot;C&quot;, &quot;A&quot;]},
    {name: &quot;AB&quot;, type: &quot;Segment&quot;, args: [&quot;A&quot;, &quot;B&quot;]},
    // I
    {name: &quot;ls_B&quot;, type: &quot;AngularBisector&quot;, args: [&quot;AB&quot;, &quot;BC&quot;, &quot;B&quot;]},
    {name: &quot;l_B&quot;, type: &quot;SelectL&quot;, args: [&quot;ls_B&quot;], index: 2, visible: false},
    {name: &quot;ls_C&quot;, type: &quot;AngularBisector&quot;, args: [&quot;BC&quot;, &quot;CA&quot;, &quot;C&quot;]},
    {name: &quot;l_C&quot;, type: &quot;SelectL&quot;, args: [&quot;ls_C&quot;], visible: false},
    {name: &quot;I&quot;, type: &quot;Meet&quot;, args: [&quot;l_B&quot;, &quot;l_C&quot;], labeled: true, printname: &quot;$I$&quot;},
    // DEF
    {name: &quot;l_ID&quot;, type: &quot;Orthogonal&quot;, args: [&quot;BC&quot;, &quot;I&quot;], visible: false},
    {name: &quot;D&quot;, type: &quot;Meet&quot;, args: [&quot;l_ID&quot;, &quot;BC&quot;], labeled: true, printname: &quot;$D$&quot;},
    {name: &quot;l_IE&quot;, type: &quot;Orthogonal&quot;, args: [&quot;CA&quot;, &quot;I&quot;], visible: false},
    {name: &quot;E&quot;, type: &quot;Meet&quot;, args: [&quot;l_IE&quot;, &quot;CA&quot;], labeled: true, printname: &quot;$E$&quot;},
    {name: &quot;l_IF&quot;, type: &quot;Orthogonal&quot;, args: [&quot;AB&quot;, &quot;I&quot;], visible: false},
    {name: &quot;F&quot;, type: &quot;Meet&quot;, args: [&quot;l_IF&quot;, &quot;AB&quot;], labeled: true, printname: &quot;$F$&quot;},
    {name: &quot;c0&quot;, type: &quot;CircleBy3&quot;, args: [&quot;D&quot;, &quot;E&quot;, &quot;F&quot;]},
    // R
    {name: &quot;EF&quot;, type: &quot;Segment&quot;, args: [&quot;E&quot;, &quot;F&quot;]},
    {name: &quot;l_DR&quot;, type: &quot;Orthogonal&quot;, args: [&quot;EF&quot;, &quot;D&quot;], visible: false},
    {name: &quot;R&quot;, type: &quot;OtherIntersectionCL&quot;, args: [&quot;c0&quot;, &quot;l_DR&quot;, &quot;D&quot;], labeled: true, printname: &quot;$R$&quot;},
    {name: &quot;DR&quot;, type: &quot;Segment&quot;, args: [&quot;D&quot;, &quot;R&quot;]},
    // P
    {name: &quot;l_AR&quot;, type: &quot;Join&quot;, args: [&quot;A&quot;, &quot;R&quot;], visible: false},
    {name: &quot;P&quot;, type: &quot;OtherIntersectionCL&quot;, args: [&quot;c0&quot;, &quot;l_AR&quot;, &quot;R&quot;], labeled: true, printname: &quot;$P$&quot;},
    {name: &quot;AP&quot;, type: &quot;Segment&quot;, args: [&quot;A&quot;, &quot;P&quot;]},
    // Q
    {name: &quot;c_B&quot;, type: &quot;CircleBy3&quot;, args: [&quot;P&quot;, &quot;B&quot;, &quot;F&quot;], alpha: .2},
    {name: &quot;c_C&quot;, type: &quot;CircleBy3&quot;, args: [&quot;P&quot;, &quot;C&quot;, &quot;E&quot;], alpha: .2},
    {name: &quot;Q&quot;, type: &quot;OtherIntersectionCC&quot;, args: [&quot;c_B&quot;, &quot;c_C&quot;, &quot;P&quot;], labeled: true, printname: &quot;$Q$&quot;},
    // DI, PQ, AL
    {name: &quot;l_DI&quot;, type: &quot;Join&quot;, args: [&quot;D&quot;, &quot;I&quot;], visible: false},
    {name: &quot;l_PQ&quot;, type: &quot;Join&quot;, args: [&quot;P&quot;, &quot;Q&quot;], visible: false},
    {name: &quot;L&quot;, type: &quot;Meet&quot;, args: [&quot;l_DI&quot;, &quot;l_PQ&quot;], labeled: true, printname: &quot;$L$&quot;},
    {name: &quot;IA&quot;, type: &quot;Segment&quot;, args: [&quot;A&quot;, &quot;I&quot;]},
    {name: &quot;LD&quot;, type: &quot;Segment&quot;, args: [&quot;L&quot;, &quot;D&quot;]},
    {name: &quot;LP&quot;, type: &quot;Segment&quot;, args: [&quot;L&quot;, &quot;P&quot;]},
    {name: &quot;l_AL&quot;, type: &quot;Orthogonal&quot;, args: [&quot;IA&quot;, &quot;A&quot;], alpha: .2},
  ]
});
&lt;/script&gt;

&lt;p&gt;See the solution in Maxima &lt;a href=&quot;https://github.com/xiw/geometry/blob/master/imo2019/imo2019p6.mac&quot;&gt;imo2019p6.mac&lt;/a&gt;.&lt;/p&gt;

&lt;p&gt;The problem description is constructive, and it’s fairly straightforward to calculate each point.
Two optimizations are worth mentioning.&lt;/p&gt;

&lt;p&gt;First,
to avoid calculating the incenter, a standard approach is to choose \(I\) as the origin
and \(D, E, F\) as points on a unit circle.
Let’s introduce angle \(\theta_d\) such that \(D\) is \(\exp(i\theta_d)\);
\(E\) and \(F\) are calculated similarly.
The rest of the points can be derived from \(D, E, F\).&lt;/p&gt;

&lt;p&gt;Second, choose \(IA\) as the real axis (i.e., \(\theta_f = -\theta_e\)),
which exploits the symmetry of \(E\) and \(F\).
Without this optimization, the program timed out (in 30 minutes).&lt;/p&gt;

&lt;p&gt;One may notice the complexity in calculating \(L\),
the intersection of \(DI\) and \(PQ\),
which is due to the complexity in \(P\) and \(Q\).
One possible optimization to calculate \(L\)
from points with simpler coordinates,
\(DI\) and the line through \(A\) perpendicular to \(AI\),
and instead prove that \(P, Q, L\) are collinear.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Exercise 4&lt;/strong&gt;: Optimize the Maxima code using the above approach.&lt;/p&gt;

&lt;h2 id=&quot;summary&quot;&gt;Summary&lt;/h2&gt;

&lt;p&gt;Below is a list of the Maxima files used in this post.&lt;/p&gt;
&lt;ul&gt;
  &lt;li&gt;Common library: &lt;a href=&quot;https://github.com/xiw/geometry/blob/master/lib/geometry.mac&quot;&gt;geometry.mac&lt;/a&gt;&lt;/li&gt;
  &lt;li&gt;IMO 2019, problem 2: &lt;a href=&quot;https://github.com/xiw/geometry/blob/master/imo2019/imo2019p2.mac&quot;&gt;imo2019p2.mac&lt;/a&gt;&lt;/li&gt;
  &lt;li&gt;IMO 2019, problem 6: &lt;a href=&quot;https://github.com/xiw/geometry/blob/master/imo2019/imo2019p6.mac&quot;&gt;imo2019p6.mac&lt;/a&gt;&lt;/li&gt;
  &lt;li&gt;Solution to exercise 3: &lt;a href=&quot;https://github.com/xiw/geometry/blob/master/imo2019/imo2019p2e3.mac&quot;&gt;imo2019p2e3.mac&lt;/a&gt;&lt;/li&gt;
  &lt;li&gt;Solution to exercise 4: &lt;a href=&quot;https://github.com/xiw/geometry/blob/master/imo2019p6e4.mac&quot;&gt;imo2019p6e4.mac&lt;/a&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;The proofs described in this post are close to those written by hand using complex numbers.
The optimizations are fairly standard, such as choosing a point as the origin or
choosing the real axis to exploit symmetry, which might be possible to automate
in the future.&lt;/p&gt;

&lt;p&gt;The &lt;a href=&quot;https://imo-grand-challenge.github.io/&quot;&gt;IMO Grand Challenge&lt;/a&gt; expects an AI to produce mechanical proofs that
can be checked by the &lt;a href=&quot;https://leanprover.github.io/&quot;&gt;Lean&lt;/a&gt; theorem prover.
Some of the other IMO 2019 problems are already formalized in Lean
(e.g., &lt;a href=&quot;https://xenaproject.wordpress.com/2019/08/01/imo-2019-q1/&quot;&gt;problem 1&lt;/a&gt; by Kevin Buzzard
and &lt;a href=&quot;https://gist.github.com/fpvandoorn/e0bd9d116a59a5f01d1d661f3677b72f&quot;&gt;problem 4&lt;/a&gt; by Floris van Doorn).
Maxima doesn’t produce Lean-checkable proofs, so it requires further work
(e.g., building on efforts such as &lt;a href=&quot;https://geocoq.github.io/GeoCoq/&quot;&gt;GeoCoq&lt;/a&gt;).
Another important factor is &lt;a href=&quot;https://github.com/IMO-grand-challenge/formal-encoding&quot;&gt;how these problems will be encoded formally&lt;/a&gt;.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Acknowledgments&lt;/strong&gt;: Luke Nelson, Emina Torlak, and Zhilei Xu
provided feedback on a draft of this post.&lt;/p&gt;</content><author><name>Xi Wang</name></author><category term="geometry" /><summary type="html">The IMO Grand Challenge aims to “build an AI that can win a gold medal in the (International Mathematical Olympiad) competition.” Geometry problems are an attractive target (e.g., see this MathOverflow discussion or this Lean’s Zulip stream), given the rich literature on automated theorem proving in geometry.</summary></entry><entry><title type="html">The shifting range in RISC-V</title><link href="http://kqueue.org/blog/2020/08/22/shifting-range/" rel="alternate" type="text/html" title="The shifting range in RISC-V" /><published>2020-08-22T00:00:00+00:00</published><updated>2020-08-22T00:00:00+00:00</updated><id>http://kqueue.org/blog/2020/08/22/shifting-range</id><content type="html" xml:base="http://kqueue.org/blog/2020/08/22/shifting-range/">&lt;p&gt;There are several commonly used RISC-V
&lt;a href=&quot;https://arxiv.org/pdf/1607.02318.pdf&quot;&gt;instruction pairs&lt;/a&gt;
with 32-bit immediates.
Below is an example of loading a 32-bit immediate
into a register using &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;lui&lt;/code&gt;/&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;addi&lt;/code&gt;:&lt;/p&gt;
&lt;pre&gt;&lt;code class=&quot;language-riscvasm&quot;&gt;lui	rd,imm[31:12]
addi	rd,rd,imm[11:0]
&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;Here &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;lui&lt;/code&gt; places
a (&lt;em&gt;sign&lt;/em&gt;-extended) 20-bit immediate into register &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;rd&lt;/code&gt;
and fills the lowest 12 bits with zeros,
and &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;addi&lt;/code&gt; adds a &lt;em&gt;sign&lt;/em&gt;-extended 12-bit immediate
to register &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;rd&lt;/code&gt;.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Question&lt;/strong&gt;: does this work for any 32-bit immediate?
It may be trickier than you think.&lt;/p&gt;

&lt;p&gt;Clearly, on &lt;em&gt;32-bit&lt;/em&gt; systems,
this instruction pair can be used to load any 32-bit immediate
in the range [-2&lt;sup&gt;31&lt;/sup&gt;, 2&lt;sup&gt;31&lt;/sup&gt;-1].
For example,
to load &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;0x7fffff00&lt;/code&gt;,
one may use &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;lui&lt;/code&gt; with a 20-bit &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;0x80000&lt;/code&gt; and
&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;addi&lt;/code&gt; with a 12-bit &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;0xf00&lt;/code&gt;,
as adding &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;0x8000000&lt;/code&gt; and &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;0xffffff00&lt;/code&gt; (sign-extended from &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;0xf00&lt;/code&gt;) produces &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;0x7fffff00&lt;/code&gt;.&lt;/p&gt;

&lt;p&gt;How about &lt;em&gt;64-bit&lt;/em&gt; systems?
Specifically,
let’s look at loading the 32-bit immediate &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;0x7fffff00&lt;/code&gt; again.
Note that the same 20-bit and 12-bit values used on 32-bit systems won’t work,
as adding &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;0xffffffff_8000000&lt;/code&gt; (sign-extended from &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;0x80000000&lt;/code&gt;) and
&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;0xffffffff_ffffff00&lt;/code&gt; (sign-extended from &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;0xf00&lt;/code&gt;) produces &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;0x0fffffff_f7ffff00&lt;/code&gt;.
Does there exist any 20-bit and 12-bit values
that make &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;lui&lt;/code&gt;/&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;addi&lt;/code&gt; work on 64-bit systems?&lt;/p&gt;

&lt;p&gt;Similar questions can be asked about other instruction pairs,
such as &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;lui&lt;/code&gt;/&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;ld&lt;/code&gt; for loading a value at a 32-bit address,
or &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;auipc&lt;/code&gt;/&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;jalr&lt;/code&gt; for jumping to a 32-bit pc-relative offset.&lt;/p&gt;

&lt;p&gt;The short answer is &lt;em&gt;no&lt;/em&gt;.
You may be interested in
the &lt;a href=&quot;https://groups.google.com/a/groups.riscv.org/forum/#!topic/isa-dev/bwWFhBnnZFQ&quot;&gt;discussion in the RISC-V ISA Dev group&lt;/a&gt; started by Luke Nelson,
which prompted the RISC-V ISA specification to clarify the range
in the “RV64I Base Integer Instruction Set” chapter:&lt;/p&gt;

&lt;blockquote&gt;
  &lt;p&gt;Note that the set of address offsets that can be formed by pairing
LUI with LD, AUIPC with JALR, etc. in RV64I is
[−2&lt;sup&gt;31&lt;/sup&gt;−2&lt;sup&gt;11&lt;/sup&gt;, 2&lt;sup&gt;31&lt;/sup&gt;−2&lt;sup&gt;11&lt;/sup&gt;−1].&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;In other words,
the 32-bit range reachable by such instruction pairs
in 64-bit RISC-V is shifted by -2&lt;sup&gt;11&lt;/sup&gt;
from [-2&lt;sup&gt;31&lt;/sup&gt;, 2&lt;sup&gt;31&lt;/sup&gt;-1].
Therefore, &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;0x7fffff00&lt;/code&gt; doesn’t fall in the range.&lt;/p&gt;

&lt;p&gt;Intuitively, the shifting is caused by the choice of the
sign extension of immediates in the RISC-V ISA.
See examples of issues
reported in &lt;a href=&quot;https://blogs.coreboot.org/blog/2016/06/13/gsoc-better-risc-v-support-week-3/&quot;&gt;coreboot&lt;/a&gt;
and &lt;a href=&quot;https://git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git/commit/?id=489553dd13a88d8a882db10622ba8b9b58582ce4&quot;&gt;the BPF JIT for RV64 in the Linux kernel&lt;/a&gt;,
as well as our upcoming &lt;a href=&quot;https://unsat.cs.washington.edu/papers/#nelson:jitterbug&quot;&gt;Jitterbug paper&lt;/a&gt;.&lt;/p&gt;

&lt;p&gt;To check the correctness of the range,
I wrote a simple &lt;a href=&quot;https://emina.github.io/rosette/&quot;&gt;Rosette&lt;/a&gt; program,
as follows:&lt;/p&gt;

&lt;div class=&quot;language-racket highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;&lt;span class=&quot;o&quot;&gt;#&lt;/span&gt;&lt;span class=&quot;nv&quot;&gt;lang&lt;/span&gt; &lt;span class=&quot;nv&quot;&gt;rosette&lt;/span&gt;

&lt;span class=&quot;c1&quot;&gt;; integer register width in bits&lt;/span&gt;
&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;k&quot;&gt;define&lt;/span&gt; &lt;span class=&quot;nv&quot;&gt;XLEN&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;64&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;

&lt;span class=&quot;c1&quot;&gt;; symbolic 20-bit and 12-bit values&lt;/span&gt;
&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;nf&quot;&gt;define-symbolic&lt;/span&gt; &lt;span class=&quot;nv&quot;&gt;imm20&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;nf&quot;&gt;bitvector&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;20&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;))&lt;/span&gt;
&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;nf&quot;&gt;define-symbolic&lt;/span&gt; &lt;span class=&quot;nv&quot;&gt;imm12&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;nf&quot;&gt;bitvector&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;12&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;))&lt;/span&gt;

&lt;span class=&quot;c1&quot;&gt;; mimic the result of an instruction pair&lt;/span&gt;
&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;k&quot;&gt;define&lt;/span&gt; &lt;span class=&quot;nv&quot;&gt;v&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;nf&quot;&gt;bvadd&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;nf&quot;&gt;sign-extend&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;nf&quot;&gt;concat&lt;/span&gt; &lt;span class=&quot;nv&quot;&gt;imm20&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;nf&quot;&gt;bv&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;0&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;12&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;))&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;nf&quot;&gt;bitvector&lt;/span&gt; &lt;span class=&quot;nv&quot;&gt;XLEN&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;))&lt;/span&gt;
                 &lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;nf&quot;&gt;sign-extend&lt;/span&gt; &lt;span class=&quot;nv&quot;&gt;imm12&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;nf&quot;&gt;bitvector&lt;/span&gt; &lt;span class=&quot;nv&quot;&gt;XLEN&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;))))&lt;/span&gt;

&lt;span class=&quot;c1&quot;&gt;; lower and upper bounds&lt;/span&gt;
&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;nf&quot;&gt;define-symbolic&lt;/span&gt; &lt;span class=&quot;nv&quot;&gt;lower&lt;/span&gt; &lt;span class=&quot;nv&quot;&gt;upper&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;nf&quot;&gt;bitvector&lt;/span&gt; &lt;span class=&quot;nv&quot;&gt;XLEN&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;))&lt;/span&gt;

&lt;span class=&quot;c1&quot;&gt;; find the lower and upper bounds via optimization&lt;/span&gt;
&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;nf&quot;&gt;optimize&lt;/span&gt; &lt;span class=&quot;nt&quot;&gt;#:maximize&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;nb&quot;&gt;list&lt;/span&gt; &lt;span class=&quot;nv&quot;&gt;lower&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
          &lt;span class=&quot;nt&quot;&gt;#:minimize&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;nb&quot;&gt;list&lt;/span&gt; &lt;span class=&quot;nv&quot;&gt;upper&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
          &lt;span class=&quot;nt&quot;&gt;#:guarantee&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;nf&quot;&gt;assert&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;nf&quot;&gt;forall&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;nb&quot;&gt;list&lt;/span&gt; &lt;span class=&quot;nv&quot;&gt;imm12&lt;/span&gt; &lt;span class=&quot;nv&quot;&gt;imm20&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
                                      &lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;nf&quot;&gt;&amp;amp;&amp;amp;&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;nf&quot;&gt;bvsge&lt;/span&gt; &lt;span class=&quot;nv&quot;&gt;v&lt;/span&gt; &lt;span class=&quot;nv&quot;&gt;lower&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
                                          &lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;nf&quot;&gt;bvsle&lt;/span&gt; &lt;span class=&quot;nv&quot;&gt;v&lt;/span&gt; &lt;span class=&quot;nv&quot;&gt;upper&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)))))&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;Rosette invokes the &lt;a href=&quot;https://rise4fun.com/Z3/tutorial/optimization&quot;&gt;Z3 SMT solver&lt;/a&gt;
to find the lower and upper bounds of the reachable range
(you may also use SMT or the Z3 API directly).
The output of the above program is:&lt;/p&gt;

&lt;div class=&quot;language-racket highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;nf&quot;&gt;model&lt;/span&gt;
 &lt;span class=&quot;p&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;nf&quot;&gt;lower&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;nf&quot;&gt;bv&lt;/span&gt; &lt;span class=&quot;mh&quot;&gt;#xffffffff7ffff800&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;64&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)]&lt;/span&gt;
 &lt;span class=&quot;p&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;nf&quot;&gt;upper&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;nf&quot;&gt;bv&lt;/span&gt; &lt;span class=&quot;mh&quot;&gt;#x000000007ffff7ff&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;64&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)])&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;This is consistent with the clarification in the RISC-V ISA specification.&lt;/p&gt;

&lt;p&gt;Exercise:
if you were asked to make one change in the 64-bit RISC-V ISA
(e.g., the semantics of an instruction)
to make the range remain
[-2&lt;sup&gt;31&lt;/sup&gt;, 2&lt;sup&gt;31&lt;/sup&gt;-1],
what would you do?
The above Rosette program may be helpful.
You may also want to check the &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;addiw&lt;/code&gt; instruction.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Acknowledgments&lt;/strong&gt;: James Bornholt, Luke Nelson, and Emina Torlak
provided feedback on a draft of this post.&lt;/p&gt;</content><author><name>Xi Wang</name></author><category term="RISC-V" /><summary type="html">There are several commonly used RISC-V instruction pairs with 32-bit immediates. Below is an example of loading a 32-bit immediate into a register using lui/addi: lui rd,imm[31:12] addi rd,rd,imm[11:0] Here lui places a (sign-extended) 20-bit immediate into register rd and fills the lowest 12 bits with zeros, and addi adds a sign-extended 12-bit immediate to register rd.</summary></entry><entry><title type="html">Mars retrograde in 2020</title><link href="http://kqueue.org/blog/2020/08/19/mars-retrograde/" rel="alternate" type="text/html" title="Mars retrograde in 2020" /><published>2020-08-19T00:00:00+00:00</published><updated>2020-08-19T00:00:00+00:00</updated><id>http://kqueue.org/blog/2020/08/19/mars-retrograde</id><content type="html" xml:base="http://kqueue.org/blog/2020/08/19/mars-retrograde/">&lt;p&gt;There is a &lt;a href=&quot;https://mars.nasa.gov/all-about-mars/night-sky/retrograde/&quot;&gt;Mars retrograde&lt;/a&gt;
this year, from September 9 to November 13.
Mars appears to move backwards as Earth overtakes Mars.
Below is a plot of its positions in the sky using &lt;a href=&quot;https://d3js.org/&quot;&gt;d3.js&lt;/a&gt;.
It also includes Jupiter and Saturn retrogrades,
both from mid May to mid September.&lt;/p&gt;

&lt;p&gt;
&lt;center&gt;
&lt;div id=&quot;mars2020&quot; title=&quot;Mars (2020)&quot;&gt;&lt;/div&gt;
&lt;/center&gt;
&lt;/p&gt;

&lt;p&gt;As described in the earlier posts
&lt;a href=&quot;/blog/2016/05/15/mars-close-approach-in-2016/&quot;&gt;&lt;em&gt;Mars close approach in 2016&lt;/em&gt;&lt;/a&gt;
and
&lt;a href=&quot;/blog/2013/08/21/mars-or-ufo/&quot;&gt;&lt;em&gt;Mars or UFO&lt;/em&gt;&lt;/a&gt;,
the coordinates of the planets
(&lt;a href=&quot;/downloads/code/ti/mars2020.txt&quot;&gt;Mars&lt;/a&gt;,
 &lt;a href=&quot;/downloads/code/ti/jupiter2020.txt&quot;&gt;Jupiter&lt;/a&gt;, and
 &lt;a href=&quot;/downloads/code/ti/saturn2020.txt&quot;&gt;Saturn&lt;/a&gt;)
are computed via a &lt;a href=&quot;/downloads/code/ti/coordgen.c&quot;&gt;small program&lt;/a&gt;
using NASA’s &lt;a href=&quot;http://naif.jpl.nasa.gov/&quot;&gt;SPICE toolkit&lt;/a&gt;,
and the coordinates of the &lt;a href=&quot;/downloads/code/ti/stars.txt&quot;&gt;stars&lt;/a&gt; are retrieved from Wikipedia
(e.g., &lt;a href=&quot;https://en.wikipedia.org/wiki/List_of_stars_in_Pisces&quot;&gt;Pisces&lt;/a&gt;).&lt;/p&gt;

&lt;script src=&quot;/downloads/code/ti/ti.js&quot;&gt;&lt;/script&gt;

&lt;script&gt;
ti.load(&quot;#mars2020&quot;, {
  width: 800,
  height: 450,
  margin: {top: 10, bottom: 10},
  offset: {ra: 300},
  stars: {
    src: &quot;/downloads/code/ti/stars.txt&quot;,
    map: [&quot;Sagittarius&quot;, &quot;Capricornus&quot;, &quot;Aquarius&quot;, &quot;Pisces&quot;, &quot;Aries&quot;,
          &quot;Sculptor&quot;, &quot;Pegasus&quot;, &quot;Aquila&quot;, &quot;Cetus&quot;],
  },
  planets: [{
    src: &quot;/downloads/code/ti/mars2020.txt&quot;,
    attr: ti.marsAttr,
  }, {
    src: &quot;/downloads/code/ti/jupiter2020.txt&quot;,
    attr: ti.jupiterAttr,
  }, {
    src: &quot;/downloads/code/ti/saturn2020.txt&quot;,
    attr: ti.saturnAttr,
  }],
  duration: 15,
});
&lt;/script&gt;</content><author><name>Xi Wang</name></author><category term="astronomy" /><summary type="html">There is a Mars retrograde this year, from September 9 to November 13. Mars appears to move backwards as Earth overtakes Mars. Below is a plot of its positions in the sky using d3.js. It also includes Jupiter and Saturn retrogrades, both from mid May to mid September.</summary></entry><entry><title type="html">Mars close approach in 2016</title><link href="http://kqueue.org/blog/2016/05/15/mars-close-approach-in-2016/" rel="alternate" type="text/html" title="Mars close approach in 2016" /><published>2016-05-15T00:00:00+00:00</published><updated>2016-05-15T00:00:00+00:00</updated><id>http://kqueue.org/blog/2016/05/15/mars-close-approach-in-2016</id><content type="html" xml:base="http://kqueue.org/blog/2016/05/15/mars-close-approach-in-2016/">&lt;p&gt;According to NASA,
&lt;a href=&quot;http://mars.nasa.gov/allaboutmars/nightsky/mars-close-approach/&quot;&gt;Mars Close Approach&lt;/a&gt;
this year is May 30 (or wait until 2018).
So I re-ran my program from the earlier post
&lt;a href=&quot;/blog/2013/08/21/mars-or-ufo/&quot;&gt;&lt;em&gt;Mars or UFO&lt;/em&gt;&lt;/a&gt;
to compute the locations of Mars in the night sky in 2016,
including retrograde from April to June, as shown below:&lt;/p&gt;

&lt;center&gt;
&lt;div id=&quot;mars2016&quot; title=&quot;Mars (2016)&quot;&gt;&lt;/div&gt;
&lt;/center&gt;

&lt;p&gt;One thing still missing is updating the size of Mars.
Feel free to send me a patch.&lt;/p&gt;

&lt;script src=&quot;/downloads/code/ti/ti.js&quot;&gt;&lt;/script&gt;

&lt;script&gt;
ti.load(&quot;#mars2016&quot;, {
  width: 800,
  height: 450,
  margin: {top: 10, left: 10},
  stars: {
    src: &quot;/downloads/code/ti/stars.txt&quot;,
    map: [&quot;Libra&quot;, &quot;Scorpius&quot;, &quot;Sagittarius&quot;, &quot;Capricornus&quot;],
  },
  planets: [{
    src: &quot;/downloads/code/ti/mars2016.txt&quot;,
    attr: ti.marsAttr,
  }],
  duration: 20,
});
&lt;/script&gt;</content><author><name>Xi Wang</name></author><category term="astronomy" /><summary type="html">According to NASA, Mars Close Approach this year is May 30 (or wait until 2018). So I re-ran my program from the earlier post Mars or UFO to compute the locations of Mars in the night sky in 2016, including retrograde from April to June, as shown below:</summary></entry><entry><title type="html">A mini symbolic execution engine</title><link href="http://kqueue.org/blog/2015/05/26/mini-mc/" rel="alternate" type="text/html" title="A mini symbolic execution engine" /><published>2015-05-26T00:00:00+00:00</published><updated>2015-05-26T00:00:00+00:00</updated><id>http://kqueue.org/blog/2015/05/26/mini-mc</id><content type="html" xml:base="http://kqueue.org/blog/2015/05/26/mini-mc/">&lt;p&gt;It has been a while since I blogged last time.  One change is that
I started to have nightmares about me forgetting to prepare for
lecture, rather than forgetting to turn in homework as in the past
20(?) years—life does get better.&lt;/p&gt;

&lt;p&gt;This post is about teaching.
Last week in grad OS class (&lt;a href=&quot;http://courses.cs.washington.edu/courses/cse551/15sp/&quot;&gt;CSE 551&lt;/a&gt;) we talked about
symbolic execution, using the papers of &lt;a href=&quot;http://klee.github.io/&quot;&gt;KLEE&lt;/a&gt; and &lt;a href=&quot;http://research.microsoft.com/en-us/um/people/pg/public_psfiles/ndss2008.pdf&quot;&gt;SAGE&lt;/a&gt;.
To illustrate the basic idea, I wrote a mini symbolic execution
implementation (~20 lines of Python code), &lt;a href=&quot;https://github.com/xiw/mini-mc&quot;&gt;mini-mc&lt;/a&gt;, using Z3.&lt;/p&gt;

&lt;p&gt;Consider a simple example, &lt;a href=&quot;https://github.com/xiw/mini-mc/blob/master/test_me.py&quot;&gt;test_me.py&lt;/a&gt;:&lt;/p&gt;

&lt;div class=&quot;language-python highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;&lt;span class=&quot;kn&quot;&gt;from&lt;/span&gt; &lt;span class=&quot;nn&quot;&gt;mc&lt;/span&gt; &lt;span class=&quot;kn&quot;&gt;import&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;*&lt;/span&gt;

&lt;span class=&quot;k&quot;&gt;def&lt;/span&gt; &lt;span class=&quot;nf&quot;&gt;test_me&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;x&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;y&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;):&lt;/span&gt;
  &lt;span class=&quot;n&quot;&gt;z&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;2&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;*&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;x&lt;/span&gt;
  &lt;span class=&quot;k&quot;&gt;if&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;z&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;==&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;y&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt;
    &lt;span class=&quot;k&quot;&gt;if&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;y&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;==&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;x&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;+&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;10&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt;
      &lt;span class=&quot;k&quot;&gt;assert&lt;/span&gt; &lt;span class=&quot;bp&quot;&gt;False&lt;/span&gt;

&lt;span class=&quot;n&quot;&gt;x&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;BitVec&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;&quot;x&quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;32&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
&lt;span class=&quot;n&quot;&gt;y&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;BitVec&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;&quot;y&quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;32&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
&lt;span class=&quot;n&quot;&gt;test_me&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;x&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;y&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;Here &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;test_me()&lt;/code&gt; looks like a normal Python function:
the program would crash if the execution hits &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;assert False&lt;/code&gt;.
The two input variables &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;x&lt;/code&gt; and &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;y&lt;/code&gt; are initialized to symbolic 32-bit integers,
through
&lt;a href=&quot;http://research.microsoft.com/en-us/um/redmond/projects/z3/z3.html&quot;&gt;Z3’s python interface&lt;/a&gt;.&lt;/p&gt;

&lt;p&gt;To find input that would trigger a crash with symbolic execution,
one can fork at every if-branch, and explore each side of the branch.
With &lt;a href=&quot;https://github.com/xiw/mini-mc&quot;&gt;mini-mc&lt;/a&gt;, simply running the Python program will do so,
producing the following output:&lt;/p&gt;

&lt;div class=&quot;language-plaintext highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;[3216] assume (2*x == y)
[3217] assume ¬(2*x == y)
[3217] exit
[3216] assume (y == x + 10)
[3218] assume ¬(y == x + 10)
[3218] exit
[3216] Traceback (most recent call last):
  File &quot;./test_me.py&quot;, line 11, in &amp;lt;module&amp;gt;
    test_me(x, y)
  File &quot;./test_me.py&quot;, line 7, in test_me
    assert False
AssertionError: x = 10, y = 20
[3216] exit
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;The output says that the execution starts with PID 3216,
and forks a child process 3217 to explore the false branch of &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;if z == y&lt;/code&gt;,
while itself continues to explore the true branch.
It does so similarly for a second branch &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;if y == x + 10&lt;/code&gt;,
and then hits &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;assert False&lt;/code&gt; with input &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;x = 10, y = 20&lt;/code&gt;.&lt;/p&gt;

&lt;p&gt;Here’s an almost complete implementation of the symbolic execution
engine in &lt;a href=&quot;https://github.com/xiw/mini-mc/blob/master/mc.py&quot;&gt;mc.py&lt;/a&gt;:&lt;/p&gt;

&lt;div class=&quot;language-python highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;&lt;span class=&quot;k&quot;&gt;def&lt;/span&gt; &lt;span class=&quot;nf&quot;&gt;sched_fork&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;bp&quot;&gt;self&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;):&lt;/span&gt;
  &lt;span class=&quot;n&quot;&gt;pid&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;os&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;fork&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;()&lt;/span&gt;
  &lt;span class=&quot;k&quot;&gt;if&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;pid&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt;
    &lt;span class=&quot;n&quot;&gt;solver&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;add&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;bp&quot;&gt;self&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
    &lt;span class=&quot;n&quot;&gt;r&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;bp&quot;&gt;True&lt;/span&gt;
    &lt;span class=&quot;n&quot;&gt;mc_log&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;&quot;assume (%s)&quot;&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;%&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;nb&quot;&gt;str&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;bp&quot;&gt;self&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;),))&lt;/span&gt;
  &lt;span class=&quot;k&quot;&gt;else&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt;
    &lt;span class=&quot;n&quot;&gt;solver&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;add&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;Not&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;bp&quot;&gt;self&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;))&lt;/span&gt;
    &lt;span class=&quot;n&quot;&gt;r&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;bp&quot;&gt;False&lt;/span&gt;
    &lt;span class=&quot;n&quot;&gt;mc_log&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;&quot;assume ¬(%s)&quot;&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;%&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;nb&quot;&gt;str&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;bp&quot;&gt;self&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;),))&lt;/span&gt;
  &lt;span class=&quot;k&quot;&gt;if&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;solver&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;check&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;()&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;!=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;sat&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt;
    &lt;span class=&quot;n&quot;&gt;mc_log&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;&quot;unreachable&quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
    &lt;span class=&quot;n&quot;&gt;sys&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;nb&quot;&gt;exit&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;0&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
  &lt;span class=&quot;k&quot;&gt;return&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;r&lt;/span&gt;
&lt;span class=&quot;c1&quot;&gt;# intercept BoolRef -&amp;gt; bool conversions
&lt;/span&gt;&lt;span class=&quot;nb&quot;&gt;setattr&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;BoolRef&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;s&quot;&gt;&quot;__bool__&quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;sched_fork&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
&lt;span class=&quot;nb&quot;&gt;setattr&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;BoolRef&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;s&quot;&gt;&quot;__nonzero__&quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;nb&quot;&gt;getattr&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;BoolRef&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;s&quot;&gt;&quot;__bool__&quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;))&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;That’s it - just one function, to show the basic fork-explore-check idea.&lt;/p&gt;

&lt;p&gt;The trick is that, as values are symbolic (e.g., via Z3’s &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;BitVec&lt;/code&gt;),
the Python VM will try to convert them into bool at &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;if&lt;/code&gt; statements;
let’s intercept the conversion and replace it with &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;sched_fork()&lt;/code&gt;,
by rewriting &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;__bool__&lt;/code&gt; (Python 3.x) or &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;__nonzero__&lt;/code&gt; (Python 2.x).&lt;/p&gt;

&lt;p&gt;There are two more fun examples on equivalence checking:&lt;/p&gt;

&lt;ul&gt;
  &lt;li&gt;&lt;a href=&quot;https://github.com/xiw/mini-mc/blob/master/ffs_eqv.py&quot;&gt;ffs_eqv.py&lt;/a&gt;:
check the equivalence of two find-first-set implementations,
from the UC-KLEE paper (CAV 2011); and&lt;/li&gt;
  &lt;li&gt;&lt;a href=&quot;https://github.com/xiw/mini-mc/blob/master/mod_eqv.py&quot;&gt;mod_eqv.py&lt;/a&gt;:
check the equivalence of two modulo implementations,
from the KLEE paper (OSDI 2008).&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Hope this is useful for classroom demonstration.&lt;/p&gt;

&lt;p&gt;BTW, &lt;a href=&quot;https://github.com/xiw/mini-mc/blob/master/mc.py&quot;&gt;mc.py&lt;/a&gt; also contains a mini implementation of concolic execution.
Run &lt;a href=&quot;https://github.com/xiw/mini-mc/blob/master/bad.py&quot;&gt;bad.py&lt;/a&gt;, which resembles &lt;a href=&quot;http://research.microsoft.com/en-us/um/people/pg/public_psfiles/ndss2008.pdf&quot;&gt;Figure 1 of the SAGE paper&lt;/a&gt;,
and you will see 5 (out of 16) inputs that trigger the exception:&lt;/p&gt;

&lt;div class=&quot;language-plaintext highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;[4324] ============================================================
[4324] #0: s[0] = 0, s[1] = 0, s[2] = 0, s[3] = 0
[4324] s[0] == 98: False
[4324] s[1] == 97: False
[4324] s[2] == 100: False
[4324] s[3] == 33: False
[4324] ============================================================
[4324] #1: s[0] = 0, s[1] = 0, s[2] = 0, s[3] = 33
[4324] s[0] == 98: False
[4324] s[1] == 97: False
[4324] s[2] == 100: False
[4324] s[3] == 33: True
       ...
[4324] ============================================================
[4324] #15: s[0] = 98, s[1] = 97, s[2] = 100, s[3] = 33
[4324] s[0] == 98: True
[4324] s[1] == 97: True
[4324] s[2] == 100: True
[4324] s[3] == 33: True
[4324] Traceback (most recent call last):
  File &quot;./bad.py&quot;, line 32, in &amp;lt;lambda&amp;gt;
    mc_fuzz(lambda: top(s), s, [0] * n)
  File &quot;./bad.py&quot;, line 26, in top
    assert False
AssertionError: s[0] = 98, s[1] = 97, s[2] = 100, s[3] = 33
[4324] exit
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;You can modify &lt;a href=&quot;https://github.com/xiw/mini-mc/blob/master/test_me.py&quot;&gt;test_me.py&lt;/a&gt; to do the same.  See the &lt;a href=&quot;https://github.com/xiw/mini-mc&quot;&gt;mini-mc&lt;/a&gt; repo
on github for details.&lt;/p&gt;</content><author><name>Xi Wang</name></author><category term="teaching" /><summary type="html">It has been a while since I blogged last time. One change is that I started to have nightmares about me forgetting to prepare for lecture, rather than forgetting to turn in homework as in the past 20(?) years—life does get better.</summary></entry><entry><title type="html">The cltq story</title><link href="http://kqueue.org/blog/2013/09/17/cltq/" rel="alternate" type="text/html" title="The cltq story" /><published>2013-09-17T00:00:00+00:00</published><updated>2013-09-17T00:00:00+00:00</updated><id>http://kqueue.org/blog/2013/09/17/cltq</id><content type="html" xml:base="http://kqueue.org/blog/2013/09/17/cltq/">&lt;p&gt;Stare at the C code below and guess what could go wrong.
It’s originally from a CPU simulator,
implementing a 16×16⇒32 unsigned multiplication.
Thanks to Mattias Engdegård at Intel for sharing the story.&lt;/p&gt;

&lt;div class=&quot;language-c highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;&lt;span class=&quot;kt&quot;&gt;uint64_t&lt;/span&gt; &lt;span class=&quot;nf&quot;&gt;mul&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;kt&quot;&gt;uint16_t&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;a&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;kt&quot;&gt;uint16_t&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;b&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
&lt;span class=&quot;p&quot;&gt;{&lt;/span&gt;
	&lt;span class=&quot;kt&quot;&gt;uint32_t&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;c&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;a&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;*&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;b&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;;&lt;/span&gt;
	&lt;span class=&quot;k&quot;&gt;return&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;c&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;;&lt;/span&gt;
&lt;span class=&quot;p&quot;&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;To be more specific, let’s assume x86-64.
Is it possible that for some &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;a&lt;/code&gt; and &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;b&lt;/code&gt;,
the result of &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;mul(a, b)&lt;/code&gt; changes when compiling the code with
different optimization levels using gcc?&lt;/p&gt;

&lt;p&gt;Spoiler alert: here’s what will happen.
When compiling the code with &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;gcc -O0&lt;/code&gt;, you’ll get:&lt;/p&gt;

&lt;div class=&quot;language-plaintext highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;$ ./mul 65535 65535
65535 * 65535 = 4294836225 (0xfffe0001)
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;Hope you’re not expecting the multiplication to overflow and output
&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;1&lt;/code&gt; here, which would be the case in Go but not in C/C++.
We’ll come back to that later.&lt;/p&gt;

&lt;p&gt;With &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;gcc -O2&lt;/code&gt;, you’ll get this:&lt;/p&gt;

&lt;div class=&quot;language-plaintext highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;$ ./mul 65535 65535
65535 * 65535 = 18446744073709420545 (0xfffffffffffe0001)
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;This doesn’t look pretty, does it?
And here’s what gcc 4.7.3/4.8.1 actually emits with &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;-O2&lt;/code&gt;:&lt;/p&gt;

&lt;div class=&quot;language-nasm highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;&lt;span class=&quot;nf&quot;&gt;movzwl&lt;/span&gt;  &lt;span class=&quot;o&quot;&gt;%&lt;/span&gt;&lt;span class=&quot;nb&quot;&gt;di&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;%&lt;/span&gt;&lt;span class=&quot;nb&quot;&gt;eax&lt;/span&gt;       &lt;span class=&quot;c1&quot;&gt;; zero-extend %di (a) to %eax&lt;/span&gt;
&lt;span class=&quot;nf&quot;&gt;movzwl&lt;/span&gt;  &lt;span class=&quot;o&quot;&gt;%&lt;/span&gt;&lt;span class=&quot;nb&quot;&gt;si&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;%&lt;/span&gt;&lt;span class=&quot;nb&quot;&gt;esi&lt;/span&gt;       &lt;span class=&quot;c1&quot;&gt;; zero-extend %si (b) to %esi&lt;/span&gt;
&lt;span class=&quot;nf&quot;&gt;imull&lt;/span&gt;   &lt;span class=&quot;o&quot;&gt;%&lt;/span&gt;&lt;span class=&quot;nb&quot;&gt;esi&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;%&lt;/span&gt;&lt;span class=&quot;nb&quot;&gt;eax&lt;/span&gt;      &lt;span class=&quot;c1&quot;&gt;; store their product in %eax&lt;/span&gt;
&lt;span class=&quot;nf&quot;&gt;cltq&lt;/span&gt;                    &lt;span class=&quot;c1&quot;&gt;; sign-extend %eax to %rax&lt;/span&gt;
&lt;span class=&quot;nf&quot;&gt;ret&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;Wait, the C code uses &lt;em&gt;unsigned&lt;/em&gt; integers only.
What’s this sign-extension instruction &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;cltq&lt;/code&gt;
(&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;cdqe&lt;/code&gt; in Intel/AMD manuals) doing here?
Is this a gcc bug?&lt;/p&gt;

&lt;p&gt;Actually,
even though emitting &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;cltq&lt;/code&gt; is odd,
it doesn’t violate the C standard.
There’s a very subtle bug in the &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;mul&lt;/code&gt; function.
I’ll show how gcc emits &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;cltq&lt;/code&gt; by exploiting two odd C
rules, &lt;em&gt;integer promotions&lt;/em&gt; and &lt;em&gt;undefined behavior&lt;/em&gt;, and discuss
possible ways to avoid/detect such problems.&lt;/p&gt;

&lt;h2 id=&quot;how-gcc-emits-cltq&quot;&gt;How gcc emits cltq&lt;/h2&gt;

&lt;p&gt;To see what gcc does, invoke it with &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;-fdump-tree-all&lt;/code&gt; to dump the
IR after each pass.
Here’s the (simplified) output after &lt;em&gt;vrp&lt;/em&gt; (value range propagation).&lt;/p&gt;

&lt;div class=&quot;language-c highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;&lt;span class=&quot;n&quot;&gt;mul&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;kt&quot;&gt;uint16_t&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;a&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;kt&quot;&gt;uint16_t&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;b&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;c1&quot;&gt;// a, b: VARYING&lt;/span&gt;
&lt;span class=&quot;p&quot;&gt;{&lt;/span&gt;
  &lt;span class=&quot;kt&quot;&gt;uint32_t&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;c&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;;&lt;/span&gt;
  &lt;span class=&quot;kt&quot;&gt;int&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;_2&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;;&lt;/span&gt;
  &lt;span class=&quot;kt&quot;&gt;int&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;_4&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;;&lt;/span&gt;
  &lt;span class=&quot;kt&quot;&gt;int&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;_5&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;;&lt;/span&gt;
  &lt;span class=&quot;kt&quot;&gt;uint64_t&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;_7&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;;&lt;/span&gt;

  &lt;span class=&quot;o&quot;&gt;&amp;lt;&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;bb2&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;&amp;gt;:&lt;/span&gt;
  &lt;span class=&quot;n&quot;&gt;_2&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;kt&quot;&gt;int&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;a&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;;&lt;/span&gt;              &lt;span class=&quot;c1&quot;&gt;// _2  : [0, 65535]&lt;/span&gt;
  &lt;span class=&quot;n&quot;&gt;_4&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;kt&quot;&gt;int&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;b&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;;&lt;/span&gt;              &lt;span class=&quot;c1&quot;&gt;// _4  : [0, 65535]&lt;/span&gt;
  &lt;span class=&quot;n&quot;&gt;_5&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;_2&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;*&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;_4&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;;&lt;/span&gt;              &lt;span class=&quot;c1&quot;&gt;// _5  : [0, +INF(OVF)]&lt;/span&gt;
  &lt;span class=&quot;n&quot;&gt;c&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;kt&quot;&gt;uint32_t&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;_5&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;;&lt;/span&gt;         &lt;span class=&quot;c1&quot;&gt;// c   : [0, +INF]&lt;/span&gt;
  &lt;span class=&quot;n&quot;&gt;_7&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;kt&quot;&gt;uint64_t&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;_5&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;;&lt;/span&gt;        &lt;span class=&quot;c1&quot;&gt;// _7  : [0, 4294967295]&lt;/span&gt;
  &lt;span class=&quot;k&quot;&gt;return&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;_7&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;;&lt;/span&gt;
&lt;span class=&quot;p&quot;&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;First of all, &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;a&lt;/code&gt; and &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;b&lt;/code&gt; are of type &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;uint16_t&lt;/code&gt;,
which is smaller than &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;int&lt;/code&gt; (assuming 32-bit &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;int&lt;/code&gt;).
According to the rule of integer promotions (C11, 6.3.1.1/2),
they are converted to &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;int&lt;/code&gt; for multiplication.
In other words, &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;a * b&lt;/code&gt; is actually &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;(int)a * (int)b&lt;/code&gt;,
a &lt;em&gt;signed&lt;/em&gt; multiplication.&lt;/p&gt;

&lt;p&gt;Second, since signed integer overflow is undefined behavior in C,
gcc assumes the signed multiplication doesn’t overflow.  This is shown
in the range information: the product &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;_5&lt;/code&gt; is considered non-negative.
Therefore, &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;c&lt;/code&gt; and &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;_5&lt;/code&gt; are indistinguishable.&lt;/p&gt;

&lt;p&gt;Now the sign conversion &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;c = (uint32_t) _5&lt;/code&gt; becomes dead code, and gcc’s next
pass &lt;em&gt;dce&lt;/em&gt; (dead code elimination) removes it.
The return value &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;_7&lt;/code&gt; is basically &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;(uint64_t)((int)a * (int)b)&lt;/code&gt;.
Note that this is a &lt;em&gt;sign&lt;/em&gt; extension from &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;int&lt;/code&gt; to &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;uint64_t&lt;/code&gt;,
which will be lowered to &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;cltq&lt;/code&gt;.
This is how &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;cltq&lt;/code&gt; pops out.&lt;/p&gt;

&lt;h2 id=&quot;fixes-and-workarounds&quot;&gt;Fixes and workarounds&lt;/h2&gt;

&lt;p&gt;One way to fix the C code is to convert &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;a&lt;/code&gt; and &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;b&lt;/code&gt; to &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;uint32_t&lt;/code&gt; before
multiplication (assuming &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;int&lt;/code&gt; is 32-bits).&lt;/p&gt;

&lt;div class=&quot;language-c highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;&lt;span class=&quot;kt&quot;&gt;uint64_t&lt;/span&gt; &lt;span class=&quot;nf&quot;&gt;mul_fixed&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;kt&quot;&gt;uint16_t&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;a&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;kt&quot;&gt;uint16_t&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;b&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
&lt;span class=&quot;p&quot;&gt;{&lt;/span&gt;
	&lt;span class=&quot;kt&quot;&gt;uint32_t&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;c&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;kt&quot;&gt;uint32_t&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;a&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;*&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;kt&quot;&gt;uint32_t&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;b&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;;&lt;/span&gt;
	&lt;span class=&quot;k&quot;&gt;return&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;c&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;;&lt;/span&gt;
&lt;span class=&quot;p&quot;&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;Instead of fixing the code, another approach is to add a workaround
compiler option.  Here’s what gcc 4.7.3/4.8.1 emits with &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;-O2 -fwrapv&lt;/code&gt;:&lt;/p&gt;

&lt;div class=&quot;language-nasm highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;&lt;span class=&quot;nf&quot;&gt;movzwl&lt;/span&gt;  &lt;span class=&quot;o&quot;&gt;%&lt;/span&gt;&lt;span class=&quot;nb&quot;&gt;si&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;%&lt;/span&gt;&lt;span class=&quot;nb&quot;&gt;eax&lt;/span&gt;
&lt;span class=&quot;nf&quot;&gt;movzwl&lt;/span&gt;  &lt;span class=&quot;o&quot;&gt;%&lt;/span&gt;&lt;span class=&quot;nb&quot;&gt;di&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;%&lt;/span&gt;&lt;span class=&quot;nb&quot;&gt;edi&lt;/span&gt;
&lt;span class=&quot;nf&quot;&gt;imull&lt;/span&gt;   &lt;span class=&quot;o&quot;&gt;%&lt;/span&gt;&lt;span class=&quot;nb&quot;&gt;edi&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;%&lt;/span&gt;&lt;span class=&quot;nb&quot;&gt;eax&lt;/span&gt;
&lt;span class=&quot;nf&quot;&gt;ret&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;Everything looks good now.  However, if you use &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;-fno-strict-overflow&lt;/code&gt;
instead of &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;-fwrapv&lt;/code&gt;, congratulations, you’ll get bitten by &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;cltq&lt;/code&gt; again:&lt;/p&gt;

&lt;div class=&quot;language-nasm highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;&lt;span class=&quot;nf&quot;&gt;movzwl&lt;/span&gt;  &lt;span class=&quot;o&quot;&gt;%&lt;/span&gt;&lt;span class=&quot;nb&quot;&gt;di&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;%&lt;/span&gt;&lt;span class=&quot;nb&quot;&gt;eax&lt;/span&gt;
&lt;span class=&quot;nf&quot;&gt;movzwl&lt;/span&gt;  &lt;span class=&quot;o&quot;&gt;%&lt;/span&gt;&lt;span class=&quot;nb&quot;&gt;si&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;%&lt;/span&gt;&lt;span class=&quot;nb&quot;&gt;esi&lt;/span&gt;
&lt;span class=&quot;nf&quot;&gt;imull&lt;/span&gt;   &lt;span class=&quot;o&quot;&gt;%&lt;/span&gt;&lt;span class=&quot;nb&quot;&gt;esi&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;%&lt;/span&gt;&lt;span class=&quot;nb&quot;&gt;eax&lt;/span&gt;
&lt;span class=&quot;nf&quot;&gt;cltq&lt;/span&gt;
&lt;span class=&quot;nf&quot;&gt;ret&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;I never really understand the
&lt;a href=&quot;http://www.airs.com/blog/archives/120&quot;&gt;difference&lt;/a&gt;
between &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;-fwrapv&lt;/code&gt; and &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;-fno-strict-overflow&lt;/code&gt;,
and &lt;a href=&quot;https://lkml.org/lkml/2009/7/12/90&quot;&gt;which option to choose&lt;/a&gt;.
Looks like it’s safer to just fix the C code.&lt;/p&gt;

&lt;p&gt;As a comparison, here’s what clang 3.3 emits with &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;-O2&lt;/code&gt;:&lt;/p&gt;

&lt;div class=&quot;language-nasm highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;&lt;span class=&quot;nf&quot;&gt;imulq&lt;/span&gt;   &lt;span class=&quot;o&quot;&gt;%&lt;/span&gt;&lt;span class=&quot;nb&quot;&gt;rdi&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;%&lt;/span&gt;&lt;span class=&quot;nb&quot;&gt;rsi&lt;/span&gt;
&lt;span class=&quot;nf&quot;&gt;movq&lt;/span&gt;    &lt;span class=&quot;o&quot;&gt;%&lt;/span&gt;&lt;span class=&quot;nb&quot;&gt;rsi&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;%&lt;/span&gt;&lt;span class=&quot;nb&quot;&gt;rax&lt;/span&gt;
&lt;span class=&quot;nf&quot;&gt;ret&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;Don’t worry, no &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;cltq&lt;/code&gt; here.&lt;/p&gt;

&lt;h2 id=&quot;possible-detection-methods&quot;&gt;Possible detection methods&lt;/h2&gt;

&lt;p&gt;In &lt;a href=&quot;http://css.csail.mit.edu/stack/&quot;&gt;our STACK paper&lt;/a&gt;,
we use the term &lt;em&gt;unstable code&lt;/em&gt; to refer to
program fragments being optimized away by the compiler due to undefined behavior.
&lt;a href=&quot;http://css.csail.mit.edu/stack/&quot;&gt;STACK&lt;/a&gt; is a static checker
for detecting unstable code.
While it works well for other cases,
unfortunately, STACK doesn’t work here.
The main reason is that STACK accepts &lt;a href=&quot;http://llvm.org/docs/LangRef.html&quot;&gt;LLVM IR&lt;/a&gt;,
which has no sign conversion instruction;
the code being optimized away,
the sign conversion &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;c = (uint32_t) _5&lt;/code&gt; as in gcc,
doesn’t exist in LLVM IR:&lt;/p&gt;

&lt;div class=&quot;language-llvm highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;&lt;span class=&quot;k&quot;&gt;define&lt;/span&gt; &lt;span class=&quot;kt&quot;&gt;i64&lt;/span&gt; &lt;span class=&quot;vg&quot;&gt;@mul&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;kt&quot;&gt;i16&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;zeroext&lt;/span&gt; &lt;span class=&quot;nv&quot;&gt;%a&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;kt&quot;&gt;i16&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;zeroext&lt;/span&gt; &lt;span class=&quot;nv&quot;&gt;%b&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;vg&quot;&gt;#0&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;{&lt;/span&gt;
&lt;span class=&quot;nl&quot;&gt;entry:&lt;/span&gt;
  &lt;span class=&quot;nv&quot;&gt;%conv&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;zext&lt;/span&gt; &lt;span class=&quot;kt&quot;&gt;i16&lt;/span&gt; &lt;span class=&quot;nv&quot;&gt;%a&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;to&lt;/span&gt; &lt;span class=&quot;kt&quot;&gt;i32&lt;/span&gt;
  &lt;span class=&quot;nv&quot;&gt;%conv1&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;zext&lt;/span&gt; &lt;span class=&quot;kt&quot;&gt;i16&lt;/span&gt; &lt;span class=&quot;nv&quot;&gt;%b&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;to&lt;/span&gt; &lt;span class=&quot;kt&quot;&gt;i32&lt;/span&gt;
  &lt;span class=&quot;nv&quot;&gt;%mul&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;mul&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;nsw&lt;/span&gt; &lt;span class=&quot;kt&quot;&gt;i32&lt;/span&gt; &lt;span class=&quot;nv&quot;&gt;%conv&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;nv&quot;&gt;%conv1&lt;/span&gt;
  &lt;span class=&quot;nv&quot;&gt;%conv2&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;zext&lt;/span&gt; &lt;span class=&quot;kt&quot;&gt;i32&lt;/span&gt; &lt;span class=&quot;nv&quot;&gt;%mul&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;to&lt;/span&gt; &lt;span class=&quot;kt&quot;&gt;i64&lt;/span&gt;
  &lt;span class=&quot;k&quot;&gt;ret&lt;/span&gt; &lt;span class=&quot;kt&quot;&gt;i64&lt;/span&gt; &lt;span class=&quot;nv&quot;&gt;%conv2&lt;/span&gt;
&lt;span class=&quot;p&quot;&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;Since in LLVM IR there’s no instruction to be optimized away,
STACK doesn’t report any warning.
One could port STACK to gcc to catch this case though.&lt;/p&gt;

&lt;p&gt;Another possible way is to extend STACK with an oracle that rewrites
&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;zext&lt;/code&gt; (zero extension) to &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;sext&lt;/code&gt; (sign extension).
The idea is that
if the code after rewriting is equivalent to the original &lt;em&gt;only&lt;/em&gt;
under the assumption of undefined behavior,
then this rewriting exploits undefined behavior,
and the code may be unstable.&lt;/p&gt;

&lt;p&gt;I coded up a prototype and it did work for this &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;mul&lt;/code&gt; case
(see the &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;sign&lt;/code&gt; branch in STACK’s git repository if interested).
But it reported too many false positives.
After all, the compiler being able to remove code often indicates a bug,
while being able to flip between &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;zext&lt;/code&gt; and &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;sext&lt;/code&gt; doesn’t.
Maybe we need better heuristics to reduce false positives.&lt;/p&gt;

&lt;p&gt;There are a few handy tools for finding undefined behavior.
For example,
run the C code with Frama-C’s &lt;a href=&quot;http://frama-c.com/value.html&quot;&gt;value analysis&lt;/a&gt;
and you’ll see the following warning:&lt;/p&gt;

&lt;div class=&quot;language-plaintext highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;$ frama-c -val mul.c
[value] Analyzing a complete application starting at main
...
mul.c:3:[kernel] warning: signed overflow. assert (int)a*(int)b ≤ 2147483647;
mul.c:3:[value] assigning non deterministic value for the first time
...
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;You can also compile it with clang’s &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;-fsanitize=undefined&lt;/code&gt; (based
on &lt;a href=&quot;http://embed.cs.utah.edu/ioc/&quot;&gt;IOC&lt;/a&gt;).
You need an input to trigger the undefined behavior, such as:&lt;/p&gt;

&lt;div class=&quot;language-plaintext highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;$ ./mul 65535 65535
mul.c:3:17: runtime error: signed integer overflow
          : 65535 * 65535 cannot be represented in type 'int'
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;Though these tools won’t tell you what code may be “miscompiled” as STACK does,
fixing the warnings will reduce the chances of being bitten by the compiler.&lt;/p&gt;

&lt;h2 id=&quot;summary&quot;&gt;Summary&lt;/h2&gt;

&lt;p&gt;This is one of my favorite unstable code examples.
The code is very short, uses unsigned integers only,
and yet confusing.
Fixing the problem, especially by using gcc’s workaround options,
is tricky.&lt;/p&gt;

&lt;p&gt;Update from Sep 18th:
Add results from Frama-C and clang’s &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;-fsanitize=undefined&lt;/code&gt;.
Thanks to David Mentré and John Regehr.&lt;/p&gt;</content><author><name>Xi Wang</name></author><category term="optimization" /><summary type="html">Stare at the C code below and guess what could go wrong. It’s originally from a CPU simulator, implementing a 16×16⇒32 unsigned multiplication. Thanks to Mattias Engdegård at Intel for sharing the story.</summary></entry><entry><title type="html">Mars or UFO: The DIY night sky</title><link href="http://kqueue.org/blog/2013/08/21/mars-or-ufo/" rel="alternate" type="text/html" title="Mars or UFO: The DIY night sky" /><published>2013-08-21T00:00:00+00:00</published><updated>2013-08-21T00:00:00+00:00</updated><id>http://kqueue.org/blog/2013/08/21/mars-or-ufo</id><content type="html" xml:base="http://kqueue.org/blog/2013/08/21/mars-or-ufo/">&lt;p&gt;Came across a funny book titled &lt;em&gt;5000 years of UFOs&lt;/em&gt; (1997) last weekend.
The author claims that a number of ancient astronomical records from Chinese
history books are actually about UFOs.
Here’s one example, where the author cites the chronicle
&lt;a href=&quot;http://en.wikipedia.org/wiki/Zizhi_Tongjian&quot;&gt;&lt;em&gt;Zizhi Tongjian&lt;/em&gt;&lt;/a&gt; (1084):&lt;/p&gt;

&lt;blockquote&gt;
  &lt;p&gt;In the 9th lunar month of the year 415, Mars looped the loop in the constellation Gemini, sometimes forward, sometime backward, sometimes left, sometimes right.&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;The author questions “how can Mars move like this”&lt;sup id=&quot;fnref:1&quot; role=&quot;doc-noteref&quot;&gt;&lt;a href=&quot;#fn:1&quot; class=&quot;footnote&quot;&gt;1&lt;/a&gt;&lt;/sup&gt; and
suspects a UFO.&lt;/p&gt;

&lt;p&gt;Well, Mars can move like this.
That’s why Mars is called a &lt;a href=&quot;http://en.wikipedia.org/wiki/Planet&quot;&gt;&lt;em&gt;planet&lt;/em&gt;&lt;/a&gt;
(“wandering stars” in Greek).
Here’s the last retrograde motion of Mars in 2011–2012,
plotted using &lt;a href=&quot;http://d3js.org/&quot;&gt;D3.js&lt;/a&gt; (JavaScript+SVG required) and data from NASA.&lt;/p&gt;

&lt;center&gt;
&lt;div id=&quot;mars2011&quot; title=&quot;Mars (2011-2012)&quot;&gt;&lt;/div&gt;
&lt;/center&gt;

&lt;p&gt;Compare it with the
&lt;a href=&quot;http://apod.nasa.gov/apod/ap120809.html&quot;&gt;actual photos from NASA&lt;/a&gt;,
and you can see how Mars brakes, changes its direction, and moves forward again.
Mars appears to move backward in the sky
when Earth overtakes Mars, since Earth moves faster around the sun
(see more explanations from
&lt;a href=&quot;http://mars.nasa.gov/allaboutmars/nightsky/nightsky04/&quot;&gt;NASA&lt;/a&gt;
and
&lt;a href=&quot;http://www.youtube.com/watch?v=kbynKfNfHk4&quot;&gt;BBC&lt;/a&gt;).&lt;/p&gt;

&lt;p&gt;However,
according to either my plots or other historical records,
Mars did &lt;em&gt;not&lt;/em&gt; loop the loop in Gemini in late 415 at all.
In fact, it is nowhere near Gemini at that time.
I suspect that the actual date of this record is late 414 or early 415;
the chronicle &lt;a href=&quot;http://en.wikipedia.org/wiki/Zizhi_Tongjian&quot;&gt;&lt;em&gt;Zizhi Tongjian&lt;/em&gt;&lt;/a&gt; may make a mistake here.
I’ll explain this later.&lt;/p&gt;

&lt;h2 id=&quot;plotting-the-night-sky&quot;&gt;Plotting the night sky&lt;/h2&gt;

&lt;p&gt;Here are some technical details.
The x-y coordinates of my plots are
&lt;a href=&quot;http://en.wikipedia.org/wiki/Right_ascension&quot;&gt;right ascension (RA)&lt;/a&gt; and
&lt;a href=&quot;http://en.wikipedia.org/wiki/Declination&quot;&gt;declination (dec)&lt;/a&gt;,
“longitude” and “latitude” of the celestial sphere, respectively.
For stars, I got their RA/dec data
(see &lt;a href=&quot;/downloads/code/ti/stars.txt&quot;&gt;stars.txt&lt;/a&gt;)
from Wikipedia (e.g.,
&lt;a href=&quot;http://en.wikipedia.org/wiki/List_of_stars_in_Gemini&quot;&gt;Gemini&lt;/a&gt;),
which are assumed not to change.&lt;/p&gt;

&lt;p&gt;The key part is to get the RA/dec of Mars over time.
There are a few options:&lt;/p&gt;

&lt;ul&gt;
  &lt;li&gt;Use &lt;a href=&quot;http://www.stellarium.org/&quot;&gt;Stellarium&lt;/a&gt;, but it’s hard to export the coordinates from there;&lt;/li&gt;
  &lt;li&gt;Calculate the coordinates using
&lt;a href=&quot;http://ssd.jpl.nasa.gov/?planet_pos&quot;&gt;lower accuracy formulae&lt;/a&gt;;&lt;/li&gt;
  &lt;li&gt;NASA’s &lt;a href=&quot;http://ssd.jpl.nasa.gov/?horizons&quot;&gt;HORIZONS&lt;/a&gt; provides a nice web service
for generating coordinates, but it doesn’t work for dates earlier than 1900.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;I ended up with writing a small program (see
&lt;a href=&quot;/downloads/code/ti/coordgen.c&quot;&gt;coordgen.c&lt;/a&gt;)
using NASA’s &lt;a href=&quot;http://naif.jpl.nasa.gov/&quot;&gt;SPICE toolkit&lt;/a&gt;,
along with the ephemeris data
&lt;a href=&quot;http://naif.jpl.nasa.gov/pub/naif/generic_kernels/spk/planets/&quot;&gt;DE431&lt;/a&gt;
that covers dates back to 13210BC.&lt;/p&gt;

&lt;div class=&quot;language-plaintext highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;$ ./coordgen &quot;MARS BARYCENTER&quot; 2011-SEP-01 2012-AUG-31
2011-SEP-01 00:00 110.18800  22.84568
2011-SEP-02 00:00 110.87321  22.77037
...
2012-AUG-31 00:00 212.12717 -13.52374
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;Now we have the RA/dec of Mars for that time span
(see &lt;a href=&quot;/downloads/code/ti/mars2011.txt&quot;&gt;mars2011.txt&lt;/a&gt;).
Similarly, we can get those of Mars in the year 415
(and even those of other planets such as Saturn),
as I’ll show later.&lt;/p&gt;

&lt;p&gt;Note that these coordinates may not be exactly the same as seen from Earth.
They are of the Mars barycenter, not the mass center.
Also, they don’t take into account atmospheric refraction nor the
light time from Mars to an observer on Earth.
They are accurate enough for our purpose though.&lt;/p&gt;

&lt;p&gt;I wrapped up my plotting code in &lt;a href=&quot;/downloads/code/ti/ti.js&quot;&gt;ti.js&lt;/a&gt;.
To generate a plot, simply call &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;ti.load()&lt;/code&gt; with your data and configuration.
See the source code of this webpage for details.&lt;/p&gt;

&lt;h2 id=&quot;the-fall-of-a-kingdom&quot;&gt;The fall of a kingdom&lt;/h2&gt;

&lt;p&gt;Mars in the year 415 played an interesting role in history.
According to &lt;a href=&quot;http://en.wikipedia.org/wiki/Zizhi_Tongjian&quot;&gt;&lt;em&gt;Zizhi Tongjian&lt;/em&gt;&lt;/a&gt;,
in the 9th lunar month of 415,
an astronomical officer of the &lt;a href=&quot;http://en.wikipedia.org/wiki/Northern_Wei&quot;&gt;Northern Wei&lt;/a&gt;
dynasty reported that Mars was “gone,” which was a serious issue at that time.
The emperor then called for a meeting.
A high-level official &lt;a href=&quot;http://en.wikipedia.org/wiki/Cui_Hao&quot;&gt;Cui Hao&lt;/a&gt;
predicted that another kingdom, &lt;a href=&quot;http://en.wikipedia.org/wiki/Later_Qin&quot;&gt;Later Qin&lt;/a&gt;, would fall soon,
as indicated by the motion of Mars,
though others didn’t believe him.
About 80 days later, Mars appeared looping the loop in Gemini.
The emperor of Later Qin, &lt;a href=&quot;http://en.wikipedia.org/wiki/Yao_Xing&quot;&gt;Yao Xing&lt;/a&gt;,
died the next year (416), and Later Qin did collapse the year after (417).&lt;sup id=&quot;fnref:2&quot; role=&quot;doc-noteref&quot;&gt;&lt;a href=&quot;#fn:2&quot; class=&quot;footnote&quot;&gt;2&lt;/a&gt;&lt;/sup&gt;&lt;/p&gt;

&lt;p&gt;Let’s plot the motion of Mars from late 415 to early 416.&lt;/p&gt;

&lt;center&gt;
&lt;div id=&quot;mars415&quot; title=&quot;Mars (415-416)&quot;&gt;&lt;/div&gt;
&lt;/center&gt;

&lt;p&gt;Clearly, Mars was traveling between Virgo and Aquarius, way far away from Gemini.&lt;/p&gt;

&lt;p&gt;I also looked up the astronomical records from
&lt;a href=&quot;http://en.wikipedia.org/wiki/Book_of_Jin&quot;&gt;&lt;em&gt;Book of Jin&lt;/em&gt;&lt;/a&gt; (648), which mentioned the following:&lt;/p&gt;

&lt;ul&gt;
  &lt;li&gt;Jul 13th, 415, Mars moved to Virgo.&lt;sup id=&quot;fnref:3&quot; role=&quot;doc-noteref&quot;&gt;&lt;a href=&quot;#fn:3&quot; class=&quot;footnote&quot;&gt;3&lt;/a&gt;&lt;/sup&gt;
(Note that all the dates in this article are converted from the original
&lt;a href=&quot;http://en.wikipedia.org/wiki/Chinese_calendar&quot;&gt;lunar dates&lt;/a&gt;
and should be considered approximate.)&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;We can confirm the accuracy of this record using the plot above.
Based on this record, since Mars was in Virgo around July,
we can also infer that it wouldn’t reach Gemini soon.&lt;/p&gt;

&lt;h2 id=&quot;415-or-414&quot;&gt;415 or 414?&lt;/h2&gt;

&lt;p&gt;So, what went wrong?&lt;/p&gt;

&lt;p&gt;Later, I found one record for the previous year 414,
also from &lt;a href=&quot;http://en.wikipedia.org/wiki/Book_of_Jin&quot;&gt;&lt;em&gt;Book of Jin&lt;/em&gt;&lt;/a&gt; (648):&lt;/p&gt;

&lt;ul&gt;
  &lt;li&gt;Aug 19th, 414, Mars approached Gemini and Saturn approached Cancer, 
looping the loop.&lt;sup id=&quot;fnref:4&quot; role=&quot;doc-noteref&quot;&gt;&lt;a href=&quot;#fn:4&quot; class=&quot;footnote&quot;&gt;4&lt;/a&gt;&lt;/sup&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Hmm, let’s plug in the data.&lt;/p&gt;

&lt;center&gt;
&lt;div id=&quot;mars414&quot; title=&quot;Mars and Saturn (414-415)&quot;&gt;&lt;/div&gt;
&lt;/center&gt;

&lt;p&gt;This plot shows Mars (in Gemini) and Saturn (in Cancer) in late 414 and early 415,
both in retrograde motion.
This is consistent with the ancient records.&lt;/p&gt;

&lt;p&gt;So, is it possible that &lt;a href=&quot;http://en.wikipedia.org/wiki/Zizhi_Tongjian&quot;&gt;&lt;em&gt;Zizhi Tongjian&lt;/em&gt;&lt;/a&gt; (1084) made a mistake on the actual date?&lt;/p&gt;

&lt;p&gt;From &lt;a href=&quot;http://en.wikipedia.org/wiki/Book_of_Wei&quot;&gt;Book of Wei&lt;/a&gt; (554),
I found an earlier version of the story
that &lt;a href=&quot;http://en.wikipedia.org/wiki/Cui_Hao&quot;&gt;Cui Hao&lt;/a&gt; predicted the fall of &lt;a href=&quot;http://en.wikipedia.org/wiki/Later_Qin&quot;&gt;Later Qin&lt;/a&gt;
based on the motion of Mars.
It is basically the same story, but the date recorded in that book is
“qian sui” before &lt;a href=&quot;http://en.wikipedia.org/wiki/Yao_Xing&quot;&gt;Yao Xing&lt;/a&gt;, the emperor of Later Qin, died in 416.&lt;sup id=&quot;fnref:5&quot; role=&quot;doc-noteref&quot;&gt;&lt;a href=&quot;#fn:5&quot; class=&quot;footnote&quot;&gt;5&lt;/a&gt;&lt;/sup&gt;&lt;/p&gt;

&lt;p&gt;Well, “qian sui” is a confusing term in Chinese.  It could mean
one year before (415), two years before (414), or even
several years before.
Based on the plot above,
I suspect the actual date of the story is 414, while the author
of &lt;a href=&quot;http://en.wikipedia.org/wiki/Zizhi_Tongjian&quot;&gt;&lt;em&gt;Zizhi Tongjian&lt;/em&gt;&lt;/a&gt; misread the date,
possibly due to the ambiguity of the term “qian sui.”&lt;/p&gt;

&lt;p&gt;To sum up, this is what I believe happened:&lt;/p&gt;

&lt;ul&gt;
  &lt;li&gt;Aug 414, Mars started retrograde motion in Gemini.&lt;/li&gt;
  &lt;li&gt;Late 414, &lt;a href=&quot;http://en.wikipedia.org/wiki/Cui_Hao&quot;&gt;Cui Hao&lt;/a&gt; predicted the fall of &lt;a href=&quot;http://en.wikipedia.org/wiki/Later_Qin&quot;&gt;Later Qin&lt;/a&gt;.&lt;/li&gt;
  &lt;li&gt;Early 415, Mars ended retrograde motion and moved out of Gemini.&lt;/li&gt;
  &lt;li&gt;416, &lt;a href=&quot;http://en.wikipedia.org/wiki/Yao_Xing&quot;&gt;Yao Xing&lt;/a&gt;, the emperor of Later Qin, died.&lt;/li&gt;
  &lt;li&gt;417, Later Qin collapsed.&lt;/li&gt;
  &lt;li&gt;554, &lt;a href=&quot;http://en.wikipedia.org/wiki/Book_of_Wei&quot;&gt;&lt;em&gt;Book of Wei&lt;/em&gt;&lt;/a&gt; was published;
it says the prediction was made “qian sui” before Yao Xing died.&lt;/li&gt;
  &lt;li&gt;1084, &lt;a href=&quot;http://en.wikipedia.org/wiki/Zizhi_Tongjian&quot;&gt;&lt;em&gt;Zizhi Tongjian&lt;/em&gt;&lt;/a&gt; was published;
it says the prediction was made in late 415,
which should have been late 414.&lt;/li&gt;
&lt;/ul&gt;

&lt;h2 id=&quot;footnotes&quot;&gt;Footnotes&lt;/h2&gt;

&lt;script src=&quot;/downloads/code/ti/ti.js&quot;&gt;&lt;/script&gt;

&lt;script&gt;
ti.load(&quot;#mars2011&quot;, {
  width: 800,
  height: 450,
  margin: {top: 10, left: 10},
  stars: {
    src: &quot;/downloads/code/ti/stars.txt&quot;,
    map: [&quot;Cancer&quot;, &quot;Leo&quot;, &quot;Virgo&quot;, &quot;Sextans&quot;],
  },
  planets: [{
    src: &quot;/downloads/code/ti/mars2011.txt&quot;,
    attr: ti.marsAttr,
  }],
  duration: 20,
});

ti.load(&quot;#mars415&quot;, {
  width: 800,
  height: 350,
  margin: {top: 10, bottom: 10},
  offset: {ra: 200},
  stars: {
    src: &quot;/downloads/code/ti/stars.txt&quot;,
    map: [&quot;Virgo&quot;, &quot;Libra&quot;, &quot;Scorpius&quot;, &quot;Sagittarius&quot;, &quot;Capricornus&quot;, &quot;Aquarius&quot;],
  },
  planets: [{
    src: &quot;/downloads/code/ti/mars415.txt&quot;,
    attr: ti.marsAttr,
  }],
  duration: 10,
  font: {
    constellation: &quot;font-size: 80%; font-family: sans-serif;&quot;,
    star: &quot;font-size: 70%; font-family: sans-serif;&quot;,
  },
});

ti.load(&quot;#mars414&quot;, {
  width: 800,
  height: 450,
  margin: {top: 10, bottom: 10},
  stars: {
    src: &quot;/downloads/code/ti/stars.txt&quot;,
    map: [&quot;Gemini&quot;, &quot;Cancer&quot;],
  },
  planets: [{
    src: &quot;/downloads/code/ti/mars414.txt&quot;,
    attr: ti.marsAttr,
  }, {
    src: &quot;/downloads/code/ti/saturn414.txt&quot;,
    attr: ti.saturnAttr,
  }],
  duration: 18,
});
&lt;/script&gt;

&lt;div class=&quot;footnotes&quot; role=&quot;doc-endnotes&quot;&gt;
  &lt;ol&gt;
    &lt;li id=&quot;fn:1&quot; role=&quot;doc-endnote&quot;&gt;
      &lt;p&gt;Excerpts from &lt;em&gt;5000 years of UFOs&lt;/em&gt;: “415年，《資治通鑑》「東晉安帝十一年九月，熒惑出東井（雙子座），留守句己，久之乃去。去而復來，乍前乍後，乍左乍右」，熒惑火星會如此飛來飛去嗎？” While the first half of the cited piece does appear in &lt;a href=&quot;http://en.wikipedia.org/wiki/Zizhi_Tongjian&quot;&gt;&lt;em&gt;Zizhi Tongjian&lt;/em&gt;&lt;/a&gt;, the second half doesn’t.  It is actually from &lt;a href=&quot;http://en.wikipedia.org/wiki/Book_of_Jin&quot;&gt;&lt;em&gt;Book of Jin&lt;/em&gt;&lt;/a&gt; (&lt;a href=&quot;http://zh.wikisource.org/wiki/%E6%99%89%E6%9B%B8/%E5%8D%B7012&quot;&gt;晉書·志第二·天文中)&lt;/a&gt;, talking about observing Mars in general.  The author mixed them up, probably due to using a &lt;a href=&quot;http://zh.wikisource.org/wiki/%E8%B3%87%E6%B2%BB%E9%80%9A%E9%91%92_(%E8%83%A1%E4%B8%89%E7%9C%81%E9%9F%B3%E6%B3%A8)/%E5%8D%B7117&quot;&gt;commented version&lt;/a&gt; of &lt;em&gt;Zizhi Tongjian&lt;/em&gt;. &lt;a href=&quot;#fnref:1&quot; class=&quot;reversefootnote&quot; role=&quot;doc-backlink&quot;&gt;&amp;#8617;&lt;/a&gt;&lt;/p&gt;
    &lt;/li&gt;
    &lt;li id=&quot;fn:2&quot; role=&quot;doc-endnote&quot;&gt;
      &lt;p&gt;&lt;a href=&quot;http://zh.wikisource.org/wiki/%E8%B3%87%E6%B2%BB%E9%80%9A%E9%91%92_(%E8%83%A1%E4%B8%89%E7%9C%81%E9%9F%B3%E6%B3%A8)/%E5%8D%B7117&quot;&gt;資治通鑑·卷第一百一十七·晉紀三十九&lt;/a&gt;: “義熙十一年…九月…魏太史奏…” &lt;a href=&quot;#fnref:2&quot; class=&quot;reversefootnote&quot; role=&quot;doc-backlink&quot;&gt;&amp;#8617;&lt;/a&gt;&lt;/p&gt;
    &lt;/li&gt;
    &lt;li id=&quot;fn:3&quot; role=&quot;doc-endnote&quot;&gt;
      &lt;p&gt;&lt;a href=&quot;http://zh.wikisource.org/wiki/%E6%99%89%E6%9B%B8/%E5%8D%B7013&quot;&gt;晉書·志第三·天文下&lt;/a&gt;: “義熙…十一年三月…己卯 熒惑入輿鬼… 五月癸卯 熒惑入太微…” &lt;a href=&quot;#fnref:3&quot; class=&quot;reversefootnote&quot; role=&quot;doc-backlink&quot;&gt;&amp;#8617;&lt;/a&gt;&lt;/p&gt;
    &lt;/li&gt;
    &lt;li id=&quot;fn:4&quot; role=&quot;doc-endnote&quot;&gt;
      &lt;p&gt;&lt;a href=&quot;http://zh.wikisource.org/wiki/%E6%99%89%E6%9B%B8/%E5%8D%B7013&quot;&gt;晉書·志第三·天文下&lt;/a&gt;: “義熙…十年…七月庚辰…熒惑犯井鉞 填星犯輿鬼 遂守之…” &lt;a href=&quot;#fnref:4&quot; class=&quot;reversefootnote&quot; role=&quot;doc-backlink&quot;&gt;&amp;#8617;&lt;/a&gt;&lt;/p&gt;
    &lt;/li&gt;
    &lt;li id=&quot;fn:5&quot; role=&quot;doc-endnote&quot;&gt;
      &lt;p&gt;&lt;a href=&quot;http://zh.wikisource.org/wiki/%E9%AD%8F%E6%9B%B8/%E5%8D%B735&quot;&gt;魏書·列傳第二十三·崔浩&lt;/a&gt;: “初 姚興死之前歲也…” &lt;a href=&quot;#fnref:5&quot; class=&quot;reversefootnote&quot; role=&quot;doc-backlink&quot;&gt;&amp;#8617;&lt;/a&gt;&lt;/p&gt;
    &lt;/li&gt;
  &lt;/ol&gt;
&lt;/div&gt;</content><author><name>Xi Wang</name></author><category term="astronomy" /><category term="history" /><summary type="html">Came across a funny book titled 5000 years of UFOs (1997) last weekend. The author claims that a number of ancient astronomical records from Chinese history books are actually about UFOs. Here’s one example, where the author cites the chronicle Zizhi Tongjian (1084):</summary></entry><entry><title type="html">IDIV DoS</title><link href="http://kqueue.org/blog/2012/12/31/idiv-dos/" rel="alternate" type="text/html" title="IDIV DoS" /><published>2012-12-31T00:00:00+00:00</published><updated>2012-12-31T00:00:00+00:00</updated><id>http://kqueue.org/blog/2012/12/31/idiv-dos</id><content type="html" xml:base="http://kqueue.org/blog/2012/12/31/idiv-dos/">&lt;p&gt;A little fun for the last day of 2012:
how to crash a program via division?
x86’s IDIV instruction traps not only on division by zero,
but also on &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;INT_MIN / -1&lt;/code&gt; (signed integer overflow).&lt;/p&gt;

&lt;h2 id=&quot;cc&quot;&gt;C/C++&lt;/h2&gt;

&lt;p&gt;Try to compile this little function.&lt;/p&gt;

&lt;div class=&quot;language-c highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;&lt;span class=&quot;kt&quot;&gt;long&lt;/span&gt; &lt;span class=&quot;kt&quot;&gt;long&lt;/span&gt; &lt;span class=&quot;nf&quot;&gt;crash&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;()&lt;/span&gt;
&lt;span class=&quot;p&quot;&gt;{&lt;/span&gt;
	&lt;span class=&quot;k&quot;&gt;return&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;1LL&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;&amp;lt;&amp;lt;&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;63&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;%&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;-&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;1&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;;&lt;/span&gt;
&lt;span class=&quot;p&quot;&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;It will crash the
&lt;a href=&quot;http://bellard.org/tcc/&quot;&gt;tcc&lt;/a&gt; compiler and
the &lt;a href=&quot;https://sparse.wiki.kernel.org/&quot;&gt;sparse&lt;/a&gt; checker.&lt;/p&gt;

&lt;h2 id=&quot;sql&quot;&gt;SQL&lt;/h2&gt;

&lt;p&gt;You need a 64-bit PostgreSQL (older than 9.2.2/9.1.7/9.0.11)
installed on Windows.&lt;/p&gt;

&lt;p&gt;Try the following SQL statement.&lt;/p&gt;

&lt;div class=&quot;language-sql highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;&lt;span class=&quot;k&quot;&gt;SELECT&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;((&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;-&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;9223372036854775808&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)::&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;int8&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;%&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;-&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;1&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;);&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;Instead of producing 0 from modulo -1,
it will crash your PostgreSQL server.
The modulo trick also worked in &lt;a href=&quot;http://www.firebirdsql.org/&quot;&gt;Firebird&lt;/a&gt;. 
This is &lt;a href=&quot;http://git.postgresql.org/gitweb/?p=postgresql.git;a=commitdiff;h=a235b85a0ba06666dbbfdb9249a65dbfa9b42ebd&quot;&gt;fixed&lt;/a&gt; by adding a check on the divisor.&lt;/p&gt;

&lt;p&gt;Here goes more evil SQL.&lt;/p&gt;

&lt;div class=&quot;language-sql highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;&lt;span class=&quot;k&quot;&gt;SELECT&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;((&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;-&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;2147483648&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)::&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;int4&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;/&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;((&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;-&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;1&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)::&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;int2&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;);&lt;/span&gt;
&lt;span class=&quot;k&quot;&gt;SELECT&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;((&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;-&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;9223372036854775808&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)::&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;int8&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;/&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;-&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;1&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;);&lt;/span&gt;
&lt;span class=&quot;k&quot;&gt;SELECT&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;((&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;-&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;9223372036854775808&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)::&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;int8&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;*&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;((&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;-&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;1&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)::&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;int8&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;);&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;The first two are straightforward.
The third one (multiplication) crashes PostgreSQL
because the overflow check is done via division.&lt;/p&gt;

&lt;p&gt;The
&lt;a href=&quot;http://git.postgresql.org/gitweb/?p=postgresql.git;a=commitdiff;h=1f7cb5c30983752ff8de833de30afcaee63536d0&quot;&gt;fix&lt;/a&gt; 
is simple: do the overflow check before the division, not after.&lt;/p&gt;

&lt;p&gt;It’s interesting that the developers
&lt;a href=&quot;http://archives.postgresql.org/pgsql-patches/2006-06/msg00104.php&quot;&gt;fixed 32-bit division crashes&lt;/a&gt;,
but missed 64-bit cases.
My guess is that the developers did the tests on a 32-bit
Windows.  In that case, since there’s no 64-bit IDIV instruction,
the compiler instead generates a call to a runtime function &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;lldiv&lt;/code&gt;,
which doesn’t trap on &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;INT_MIN / -1&lt;/code&gt;.
This would lead to the incorrect conclusion that 64-bit division
wouldn’t trap.&lt;/p&gt;

&lt;h2 id=&quot;clamav-bytecode&quot;&gt;ClamAV bytecode&lt;/h2&gt;

&lt;p&gt;The ClamAV engine accepts bytecode signatures as extensions to
detect new viruses.  You can write one to crash ClamAV’s interpreter,
as follows.&lt;/p&gt;

&lt;div class=&quot;language-c highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;&lt;span class=&quot;kt&quot;&gt;int&lt;/span&gt; &lt;span class=&quot;nf&quot;&gt;entrypoint&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;kt&quot;&gt;void&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
&lt;span class=&quot;p&quot;&gt;{&lt;/span&gt;
	&lt;span class=&quot;kt&quot;&gt;long&lt;/span&gt; &lt;span class=&quot;kt&quot;&gt;long&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;x&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;1LL&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;&amp;lt;&amp;lt;&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;63&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;;&lt;/span&gt;
	&lt;span class=&quot;kt&quot;&gt;int&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;y&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;__is_bigendian&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;()&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;-&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;1&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;;&lt;/span&gt;
	&lt;span class=&quot;k&quot;&gt;if&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;x&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;/&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;y&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
		&lt;span class=&quot;n&quot;&gt;foundVirus&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;&quot;idiv&quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;);&lt;/span&gt;
	&lt;span class=&quot;k&quot;&gt;return&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;0&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;;&lt;/span&gt;
&lt;span class=&quot;p&quot;&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;It basically does &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;INT_MIN / -1&lt;/code&gt;, only to prevent ClamAV’s bytecode
compiler from optimizing away the division.
Then compile the function into bytecode, load it into &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;clamscan&lt;/code&gt;,
and it will crash the interpreter.&lt;/p&gt;

&lt;p&gt;Actually the interpreter does have a sanity check for signed division,
which was
&lt;a href=&quot;https://github.com/vrtadmin/clamav-devel/commit/82ca2ab4209a46a9e7322777452000465070ffb6&quot;&gt;introduced&lt;/a&gt;
in 2009.&lt;/p&gt;

&lt;div class=&quot;language-c highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;&lt;span class=&quot;k&quot;&gt;static&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;always_inline&lt;/span&gt; &lt;span class=&quot;kt&quot;&gt;int&lt;/span&gt; &lt;span class=&quot;nf&quot;&gt;check_sdivops&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;kt&quot;&gt;int64_t&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;op0&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;kt&quot;&gt;int64_t&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;op1&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
&lt;span class=&quot;p&quot;&gt;{&lt;/span&gt;
	&lt;span class=&quot;k&quot;&gt;return&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;op1&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;==&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;0&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;||&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;op0&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;==&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;-&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;1&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;&amp;amp;&amp;amp;&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;op1&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;==&lt;/span&gt;  &lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;-&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;9223372036854775807LL&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;-&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;1LL&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;));&lt;/span&gt;
&lt;span class=&quot;p&quot;&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;The sanity check doesn’t work because, well, you really should
swap &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;op0&lt;/code&gt; and &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;op1&lt;/code&gt; in the second half of the check.&lt;/p&gt;

&lt;p&gt;Let me know if you have more stories.
Happy new year!&lt;/p&gt;</content><author><name>Xi Wang</name></author><category term="security" /><summary type="html">A little fun for the last day of 2012: how to crash a program via division? x86’s IDIV instruction traps not only on division by zero, but also on INT_MIN / -1 (signed integer overflow).</summary></entry></feed>