<html xmlns:v="urn:schemas-microsoft-com:vml" xmlns:o="urn:schemas-microsoft-com:office:office" xmlns:w="urn:schemas-microsoft-com:office:word" xmlns:m="http://schemas.microsoft.com/office/2004/12/omml" xmlns="http://www.w3.org/TR/REC-html40"><head><meta http-equiv=Content-Type content="text/html; charset=us-ascii"><meta name=Generator content="Microsoft Word 14 (filtered medium)"><style><!--
/* Font Definitions */
@font-face
        {font-family:Calibri;
        panose-1:2 15 5 2 2 2 4 3 2 4;}
/* Style Definitions */
p.MsoNormal, li.MsoNormal, div.MsoNormal
        {margin:0cm;
        margin-bottom:.0001pt;
        font-size:11.0pt;
        font-family:"Calibri","sans-serif";}
a:link, span.MsoHyperlink
        {mso-style-priority:99;
        color:blue;
        text-decoration:underline;}
a:visited, span.MsoHyperlinkFollowed
        {mso-style-priority:99;
        color:purple;
        text-decoration:underline;}
span.EmailStyle17
        {mso-style-type:personal-compose;
        font-family:"Calibri","sans-serif";
        color:windowtext;}
.MsoChpDefault
        {mso-style-type:export-only;
        font-family:"Calibri","sans-serif";}
@page WordSection1
        {size:612.0pt 792.0pt;
        margin:72.0pt 72.0pt 72.0pt 72.0pt;}
div.WordSection1
        {page:WordSection1;}
--></style><!--[if gte mso 9]><xml>
<o:shapedefaults v:ext="edit" spidmax="1026" />
</xml><![endif]--><!--[if gte mso 9]><xml>
<o:shapelayout v:ext="edit">
<o:idmap v:ext="edit" data="1" />
</o:shapelayout></xml><![endif]--></head><body lang=EN-US link=blue vlink=purple><div class=WordSection1><p class=MsoNormal>Hi,<o:p></o:p></p><p class=MsoNormal><o:p> </o:p></p><p class=MsoNormal>I’m trying expand the Float2Int pass in order to make it able to optimize<o:p></o:p></p><p class=MsoNormal>expressions like f * x  > y, where x and y are integers (we’ll assume unsigned for<o:p></o:p></p><p class=MsoNormal>simplicity) and f is a floating point constant. The optimization would convert<o:p></o:p></p><p class=MsoNormal>the expression to something like:<o:p></o:p></p><p class=MsoNormal><o:p> </o:p></p><p class=MsoNormal>  (a * x)/b > y<o:p></o:p></p><p class=MsoNormal><o:p> </o:p></p><p class=MsoNormal>where a and b are integers guessed by the compiler (currently using continued<o:p></o:p></p><p class=MsoNormal>fractions, but some other method could also be used). The exact expression<o:p></o:p></p><p class=MsoNormal>would depend on the comparison type and on the type of input integers.<o:p></o:p></p><p class=MsoNormal><o:p> </o:p></p><p class=MsoNormal>Note that dividing by an integer constant should be a cheap operation<o:p></o:p></p><p class=MsoNormal>compared to FP multiplication and comparison as this would get lowered to a<o:p></o:p></p><p class=MsoNormal>multiply+subtract+shift sequence (and should certainly be true on cores with no<o:p></o:p></p><p class=MsoNormal>FP unit)<o:p></o:p></p><p class=MsoNormal><o:p> </o:p></p><p class=MsoNormal>The change will add some machinery to Float2Int in order to be able to<o:p></o:p></p><p class=MsoNormal>match certain known patterns. All patterns will have exactly representable<o:p></o:p></p><p class=MsoNormal>inputs and outputs. In this case, the FCMP will have two inputs, x and y and<o:p></o:p></p><p class=MsoNormal>one output (the result of the comparison). This allows us to use the existing<o:p></o:p></p><p class=MsoNormal>Float2Int infrastructure to track the integer length that would be required<o:p></o:p></p><p class=MsoNormal>for the conversion.<o:p></o:p></p><p class=MsoNormal><o:p> </o:p></p><p class=MsoNormal>The main difficulty I can see with this is validating the transformation. Ideally<o:p></o:p></p><p class=MsoNormal>the property that we want to prove for the guessed integers a and b would be<o:p></o:p></p><p class=MsoNormal>that (a*x)/b == round_to_zero(f * to_float(x)), for every integer x in a given<o:p></o:p></p><p class=MsoNormal>range. The problem with this is that we would need to come up with a reasonably<o:p></o:p></p><p class=MsoNormal>quick method of proving this.<o:p></o:p></p><p class=MsoNormal><o:p> </o:p></p><p class=MsoNormal>My best guess (still need to work out the exact details) would be that we only<o:p></o:p></p><p class=MsoNormal>need to check the first and last b numbers of the range. A hand-wavy argument<o:p></o:p></p><p class=MsoNormal>for this would be that since errors increase on the extremities of the range, so if<o:p></o:p></p><p class=MsoNormal>an error appears anywhere it must also appear  at the edges of the range.<o:p></o:p></p><p class=MsoNormal>I still need to prove this. If there are any floating-point experts that can help<o:p></o:p></p><p class=MsoNormal>with this, I would greatly appreciate it.<o:p></o:p></p><p class=MsoNormal><o:p> </o:p></p><p class=MsoNormal>Any other comments about the overall merits of the idea and the approach are<o:p></o:p></p><p class=MsoNormal>of course also welcome!<o:p></o:p></p><p class=MsoNormal><o:p> </o:p></p><p class=MsoNormal>Thanks,<o:p></o:p></p><p class=MsoNormal>Silviu<o:p></o:p></p></div></body></html>