<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=utf-8">
<meta name="Generator" content="Microsoft Word 15 (filtered medium)">
<style><!--
/* Font Definitions */
@font-face
        {font-family:"Cambria Math";
        panose-1:2 4 5 3 5 4 6 3 2 4;}
@font-face
        {font-family:Calibri;
        panose-1:2 15 5 2 2 2 4 3 2 4;}
@font-face
        {font-family:"Lucida Sans Unicode";
        panose-1:2 11 6 2 3 5 4 2 2 4;}
/* Style Definitions */
p.MsoNormal, li.MsoNormal, div.MsoNormal
        {margin:0in;
        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;}
code
        {mso-style-priority:99;
        font-family:"Courier New";}
span.EmailStyle21
        {mso-style-type:personal-reply;
        font-family:"Calibri",sans-serif;
        color:windowtext;}
.MsoChpDefault
        {mso-style-type:export-only;
        font-family:"Calibri",sans-serif;}
@page WordSection1
        {size:8.5in 11.0in;
        margin:1.0in 1.0in 1.0in 1.0in;}
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">The LangRef does specify that the rounding mode/exception behavior is advisory, not mandatory, although it could probably be spelled out better.<o:p></o:p></p>
<p class="MsoNormal"><o:p> </o:p></p>
<p class="MsoNormal">That said, there are some architectures where rounding mode can be configured on a per-instruction basis (e.g., AVX-512 has the SAE bits that serve this purpose, and I believe the various GPU architectures follow suit). It may be worth
 having some support for this in LLVM.<o:p></o:p></p>
<p class="MsoNormal"><o:p> </o:p></p>
<div style="border:none;border-left:solid blue 1.5pt;padding:0in 0in 0in 4.0pt">
<div>
<div style="border:none;border-top:solid #E1E1E1 1.0pt;padding:3.0pt 0in 0in 0in">
<p class="MsoNormal"><b>From:</b> llvm-dev <llvm-dev-bounces@lists.llvm.org> <b>On Behalf Of
</b>Craig Topper via llvm-dev<br>
<b>Sent:</b> Wednesday, September 9, 2020 1:28<br>
<b>To:</b> Xuan Tang <txstc55@gmail.com><br>
<b>Cc:</b> llvm-dev <llvm-dev@lists.llvm.org><br>
<b>Subject:</b> Re: [llvm-dev] constrained cosine rounding mode behavior<o:p></o:p></p>
</div>
</div>
<p class="MsoNormal"><o:p> </o:p></p>
<div>
<div>
<p class="MsoNormal">Hi Xuan Tang,<o:p></o:p></p>
</div>
<div>
<p class="MsoNormal"><o:p> </o:p></p>
</div>
<p class="MsoNormal">The rounding mode argument to the intrinsic is supposed to be a hint to the compiler to tell us what the rounding mode has been changed to. In theory we could use this to constant fold the intrinsic with correct rounding but I don't think
 much of that is implemented. The compiler does not add any code to change the rounding mode in the hardware floating point unit in response to the intrinsic. It's up to the user to call something like fesetround in libm to change the rounding mode in hardware.
 Changing the rounding mode in hardware could have an effect on the result returned by the cos library function that we will ultimately call, but I don't know for sure.<o:p></o:p></p>
<div>
<p class="MsoNormal"><br clear="all">
<o:p></o:p></p>
<div>
<div>
<p class="MsoNormal">~Craig<o:p></o:p></p>
</div>
</div>
<p class="MsoNormal"><o:p> </o:p></p>
</div>
</div>
<p class="MsoNormal"><o:p> </o:p></p>
<div>
<div>
<p class="MsoNormal">On Tue, Sep 8, 2020 at 8:55 PM Xuan Tang via llvm-dev <<a href="mailto:llvm-dev@lists.llvm.org" target="_blank">llvm-dev@lists.llvm.org</a>> wrote:<o:p></o:p></p>
</div>
<blockquote style="border:none;border-left:solid #CCCCCC 1.0pt;padding:0in 0in 0in 6.0pt;margin-left:4.8pt;margin-right:0in">
<div>
<div name="messageBodySection">
<div>
<p class="MsoNormal">Hi:<br>
<br>
I am trying to implement interval arithmetic through llvm. I have a problem with the rounding mode with llvm.experimental.constrained.cos<br>
<br>
I have two pieces of codes:<br>
<br>
<em><span style="font-size:9.0pt;font-family:"Calibri",sans-serif;color:#BBBBBB">; Function Attrs: norecurse nounwind readnone ssp uwtable</span></em><span style="font-size:9.0pt"><br>
<strong><span style="font-family:"Calibri",sans-serif;color:#B877DB">define</span></strong><span style="color:#BBBBBB"> </span><strong><span style="font-family:"Calibri",sans-serif;color:#B877DB">double</span></strong><span style="color:#BBBBBB"> </span><em><span style="font-family:"Calibri",sans-serif;color:#FAC29A">@cosine_down</span></em><span style="color:#BBBBBB">(</span><strong><span style="font-family:"Calibri",sans-serif;color:#B877DB">double</span></strong><span style="color:#BBBBBB"> returned)
 local_unnamed_addr #</span><span style="color:#F09483">0</span><span style="color:#BBBBBB"> {</span><br>
<span style="color:#BBBBBB">  <em><span style="font-family:"Calibri",sans-serif">; call the llvm intrinsic to perform downward cosine</span></em></span><br>
<span style="color:#BBBBBB">  </span><em><span style="font-family:"Calibri",sans-serif;color:#FAC29A">%2</span></em><span style="color:#BBBBBB"> = </span><strong><span style="font-family:"Calibri",sans-serif;color:#B877DB">call</span></strong><span style="color:#BBBBBB"> </span><strong><span style="font-family:"Calibri",sans-serif;color:#B877DB">double</span></strong><span style="color:#BBBBBB"> </span><em><span style="font-family:"Calibri",sans-serif;color:#FAC29A">@llvm.experimental.constrained.cos</span></em><span style="color:#BBBBBB">(</span><strong><span style="font-family:"Calibri",sans-serif;color:#B877DB">double</span></strong><span style="color:#BBBBBB"> </span><em><span style="font-family:"Calibri",sans-serif;color:#FAC29A">%0</span></em><span style="color:#BBBBBB">, </span><strong><span style="font-family:"Calibri",sans-serif;color:#B877DB">metadata</span></strong><span style="color:#BBBBBB"> !</span><span style="color:#FAB795">"round</span><span style="color:#25B0BC">.d</span><span style="color:#FAB795">ownward"</span><span style="color:#BBBBBB">, </span><strong><span style="font-family:"Calibri",sans-serif;color:#B877DB">metadata</span></strong><span style="color:#BBBBBB"> !</span><span style="color:#FAB795">"fpexcept</span><span style="color:#25B0BC">.s</span><span style="color:#FAB795">trict"</span><span style="color:#BBBBBB">)</span><br>
<span style="color:#BBBBBB">  </span><strong><span style="font-family:"Calibri",sans-serif;color:#B877DB">ret</span></strong><span style="color:#BBBBBB"> </span><strong><span style="font-family:"Calibri",sans-serif;color:#B877DB">double</span></strong><span style="color:#BBBBBB"> </span><em><span style="font-family:"Calibri",sans-serif;color:#FAC29A">%2</span></em><br>
<span style="color:#BBBBBB">}</span><br>
<br>
<em><span style="font-family:"Calibri",sans-serif;color:#BBBBBB">; Function Attrs: norecurse nounwind readnone ssp uwtable</span></em><br>
<strong><span style="font-family:"Calibri",sans-serif;color:#B877DB">define</span></strong><span style="color:#BBBBBB"> </span><strong><span style="font-family:"Calibri",sans-serif;color:#B877DB">double</span></strong><span style="color:#BBBBBB"> </span><em><span style="font-family:"Calibri",sans-serif;color:#FAC29A">@cosine_up</span></em><span style="color:#BBBBBB">(</span><strong><span style="font-family:"Calibri",sans-serif;color:#B877DB">double</span></strong><span style="color:#BBBBBB"> returned)
 local_unnamed_addr #</span><span style="color:#F09483">0</span><span style="color:#BBBBBB"> {</span><br>
<span style="color:#BBBBBB">  <em><span style="font-family:"Calibri",sans-serif">; call the llvm intrinsic to perform upward cosine</span></em></span><br>
<span style="color:#BBBBBB">  </span><em><span style="font-family:"Calibri",sans-serif;color:#FAC29A">%2</span></em><span style="color:#BBBBBB"> = </span><strong><span style="font-family:"Calibri",sans-serif;color:#B877DB">call</span></strong><span style="color:#BBBBBB"> </span><strong><span style="font-family:"Calibri",sans-serif;color:#B877DB">double</span></strong><span style="color:#BBBBBB"> </span><em><span style="font-family:"Calibri",sans-serif;color:#FAC29A">@llvm.experimental.constrained.cos</span></em><span style="color:#BBBBBB">(</span><strong><span style="font-family:"Calibri",sans-serif;color:#B877DB">double</span></strong><span style="color:#BBBBBB"> </span><em><span style="font-family:"Calibri",sans-serif;color:#FAC29A">%0</span></em><span style="color:#BBBBBB">, </span><strong><span style="font-family:"Calibri",sans-serif;color:#B877DB">metadata</span></strong><span style="color:#BBBBBB"> !</span><span style="color:#FAB795">"round</span><span style="color:#25B0BC">.u</span><span style="color:#FAB795">pward"</span><span style="color:#BBBBBB">, </span><strong><span style="font-family:"Calibri",sans-serif;color:#B877DB">metadata</span></strong><span style="color:#BBBBBB"> !</span><span style="color:#FAB795">"fpexcept</span><span style="color:#25B0BC">.s</span><span style="color:#FAB795">trict"</span><span style="color:#BBBBBB">)</span><br>
<span style="color:#BBBBBB">  </span><strong><span style="font-family:"Calibri",sans-serif;color:#B877DB">ret</span></strong><span style="color:#BBBBBB"> </span><strong><span style="font-family:"Calibri",sans-serif;color:#B877DB">double</span></strong><span style="color:#BBBBBB"> </span><em><span style="font-family:"Calibri",sans-serif;color:#FAC29A">%2</span></em><br>
<span style="color:#BBBBBB">}</span><br>
<br>
</span><span style="color:#D5DADE">When calling the function on a test number</span><span style="font-size:9.0pt;color:#BBBBBB">: </span><span style="font-size:9.0pt;color:#F09483">0.79358805865013693</span><span style="font-size:9.0pt"><br>
<br>
</span><span style="color:#D5DADE">The two functions return the same value: </span>0.7012920012119437<span style="color:#D5DADE">. </span><br>
<br>
<span style="color:#D5DADE">Ideally, the two functions will give me the upper and lower bound of where the true value will lie within, so maybe </span>0.7012920012119436 to 0.7012920012119438, but it seems like even with the upward and downward rounding mode
 that’s not the case.<br>
<br>
I noted on the page <a href="https://llvm.org/docs/LangRef.html#llvm-experimental-constrained-cos-intrinsic" target="_blank">https://llvm.org/docs/LangRef.html#llvm-experimental-constrained-cos-intrinsic</a> it says  <span style="font-family:"Lucida Sans Unicode",sans-serif;color:black;background:white">This
 function returns the cosine of the specified operand, returning the same values as the libm </span><code><span style="font-size:10.0pt">cos</span></code><span style="font-family:"Lucida Sans Unicode",sans-serif;color:black;background:white"> functions would</span><br>
<br>
<span style="color:#D5DADE">So does this mean that constrained cos does not care about the rounding mode but instead will just return the same value?</span><o:p></o:p></p>
</div>
</div>
<div name="messageSignatureSection">
<p class="MsoNormal"><o:p> </o:p></p>
<div>
<p class="MsoNormal">Thank you <o:p></o:p></p>
<div>
<p class="MsoNormal">Xuan Tang<o:p></o:p></p>
</div>
</div>
</div>
</div>
<p class="MsoNormal">_______________________________________________<br>
LLVM Developers mailing list<br>
<a href="mailto:llvm-dev@lists.llvm.org" target="_blank">llvm-dev@lists.llvm.org</a><br>
<a href="https://lists.llvm.org/cgi-bin/mailman/listinfo/llvm-dev" target="_blank">https://lists.llvm.org/cgi-bin/mailman/listinfo/llvm-dev</a><o:p></o:p></p>
</blockquote>
</div>
</div>
</div>
</body>
</html>