<div dir="ltr"><div>Hi Xuan Tang,</div><div><br></div>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.<div><br clear="all"><div><div dir="ltr" data-smartmail="gmail_signature">~Craig</div></div><br></div></div><br><div class="gmail_quote"><div dir="ltr" class="gmail_attr">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:<br></div><blockquote class="gmail_quote" style="margin:0px 0px 0px 0.8ex;border-left:1px solid rgb(204,204,204);padding-left:1ex">



<div>
<div name="messageBodySection">
<div dir="auto">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 style="color:rgb(187,187,187);font-size:12px">; Function Attrs: norecurse nounwind readnone ssp uwtable</em><span style="font-size:12px"><br></span><strong style="color:rgb(184,119,219);font-size:12px">define</strong><span style="color:rgb(187,187,187);font-size:12px"> </span><strong style="color:rgb(184,119,219);font-size:12px">double</strong><span style="color:rgb(187,187,187);font-size:12px"> </span><em style="color:rgb(250,194,154);font-size:12px">@cosine_down</em><span style="color:rgb(187,187,187);font-size:12px">(</span><strong style="color:rgb(184,119,219);font-size:12px">double</strong><span style="color:rgb(187,187,187);font-size:12px"> returned) local_unnamed_addr #</span><span style="color:rgb(240,148,131);font-size:12px">0</span><span style="color:rgb(187,187,187);font-size:12px"> {</span><span style="font-size:12px"><br></span><span style="color:rgb(187,187,187);font-size:12px">  </span><em style="color:rgb(187,187,187);font-size:12px">; call the llvm intrinsic to perform downward cosine</em><span style="font-size:12px"><br></span><span style="color:rgb(187,187,187);font-size:12px">  </span><em style="color:rgb(250,194,154);font-size:12px">%2</em><span style="color:rgb(187,187,187);font-size:12px"> = </span><strong style="color:rgb(184,119,219);font-size:12px">call</strong><span style="color:rgb(187,187,187);font-size:12px"> </span><strong style="color:rgb(184,119,219);font-size:12px">double</strong><span style="color:rgb(187,187,187);font-size:12px"> </span><em style="color:rgb(250,194,154);font-size:12px">@llvm.experimental.constrained.cos</em><span style="color:rgb(187,187,187);font-size:12px">(</span><strong style="color:rgb(184,119,219);font-size:12px">double</strong><span style="color:rgb(187,187,187);font-size:12px"> </span><em style="color:rgb(250,194,154);font-size:12px">%0</em><span style="color:rgb(187,187,187);font-size:12px">, </span><strong style="color:rgb(184,119,219);font-size:12px">metadata</strong><span style="color:rgb(187,187,187);font-size:12px"> !</span><span style="color:rgb(250,183,149);font-size:12px">"round</span><span style="color:rgb(37,176,188);font-size:12px">.d</span><span style="color:rgb(250,183,149);font-size:12px">ownward"</span><span style="color:rgb(187,187,187);font-size:12px">, </span><strong style="color:rgb(184,119,219);font-size:12px">metadata</strong><span style="color:rgb(187,187,187);font-size:12px"> !</span><span style="color:rgb(250,183,149);font-size:12px">"fpexcept</span><span style="color:rgb(37,176,188);font-size:12px">.s</span><span style="color:rgb(250,183,149);font-size:12px">trict"</span><span style="color:rgb(187,187,187);font-size:12px">)</span><span style="font-size:12px"><br></span><span style="color:rgb(187,187,187);font-size:12px">  </span><strong style="color:rgb(184,119,219);font-size:12px">ret</strong><span style="color:rgb(187,187,187);font-size:12px"> </span><strong style="color:rgb(184,119,219);font-size:12px">double</strong><span style="color:rgb(187,187,187);font-size:12px"> </span><em style="color:rgb(250,194,154);font-size:12px">%2</em><span style="font-size:12px"><br></span><span style="color:rgb(187,187,187);font-size:12px">}</span><span style="font-size:12px"><br></span><span style="font-size:12px"><br></span><em style="color:rgb(187,187,187);font-size:12px">; Function Attrs: norecurse nounwind readnone ssp uwtable</em><span style="font-size:12px"><br></span><strong style="color:rgb(184,119,219);font-size:12px">define</strong><span style="color:rgb(187,187,187);font-size:12px"> </span><strong style="color:rgb(184,119,219);font-size:12px">double</strong><span style="color:rgb(187,187,187);font-size:12px"> </span><em style="color:rgb(250,194,154);font-size:12px">@cosine_up</em><span style="color:rgb(187,187,187);font-size:12px">(</span><strong style="color:rgb(184,119,219);font-size:12px">double</strong><span style="color:rgb(187,187,187);font-size:12px"> returned) local_unnamed_addr #</span><span style="color:rgb(240,148,131);font-size:12px">0</span><span style="color:rgb(187,187,187);font-size:12px"> {</span><span style="font-size:12px"><br></span><span style="color:rgb(187,187,187);font-size:12px">  </span><em style="color:rgb(187,187,187);font-size:12px">; call the llvm intrinsic to perform upward cosine</em><span style="font-size:12px"><br></span><span style="color:rgb(187,187,187);font-size:12px">  </span><em style="color:rgb(250,194,154);font-size:12px">%2</em><span style="color:rgb(187,187,187);font-size:12px"> = </span><strong style="color:rgb(184,119,219);font-size:12px">call</strong><span style="color:rgb(187,187,187);font-size:12px"> </span><strong style="color:rgb(184,119,219);font-size:12px">double</strong><span style="color:rgb(187,187,187);font-size:12px"> </span><em style="color:rgb(250,194,154);font-size:12px">@llvm.experimental.constrained.cos</em><span style="color:rgb(187,187,187);font-size:12px">(</span><strong style="color:rgb(184,119,219);font-size:12px">double</strong><span style="color:rgb(187,187,187);font-size:12px"> </span><em style="color:rgb(250,194,154);font-size:12px">%0</em><span style="color:rgb(187,187,187);font-size:12px">, </span><strong style="color:rgb(184,119,219);font-size:12px">metadata</strong><span style="color:rgb(187,187,187);font-size:12px"> !</span><span style="color:rgb(250,183,149);font-size:12px">"round</span><span style="color:rgb(37,176,188);font-size:12px">.u</span><span style="color:rgb(250,183,149);font-size:12px">pward"</span><span style="color:rgb(187,187,187);font-size:12px">, </span><strong style="color:rgb(184,119,219);font-size:12px">metadata</strong><span style="color:rgb(187,187,187);font-size:12px"> !</span><span style="color:rgb(250,183,149);font-size:12px">"fpexcept</span><span style="color:rgb(37,176,188);font-size:12px">.s</span><span style="color:rgb(250,183,149);font-size:12px">trict"</span><span style="color:rgb(187,187,187);font-size:12px">)</span><span style="font-size:12px"><br></span><span style="color:rgb(187,187,187);font-size:12px">  </span><strong style="color:rgb(184,119,219);font-size:12px">ret</strong><span style="color:rgb(187,187,187);font-size:12px"> </span><strong style="color:rgb(184,119,219);font-size:12px">double</strong><span style="color:rgb(187,187,187);font-size:12px"> </span><em style="color:rgb(250,194,154);font-size:12px">%2</em><span style="font-size:12px"><br></span><span style="color:rgb(187,187,187);font-size:12px">}</span><span style="font-size:12px"><br></span><span style="font-size:12px"><br></span><span style="color:rgb(213,218,222)">When calling the function on a test number</span><span style="color:rgb(187,187,187);font-size:12px">: </span><span style="color:rgb(240,148,131);font-size:12px">0.79358805865013693</span><span style="font-size:12px"><br></span><span style="font-size:12px"><br></span><span style="color:rgb(213,218,222)">The two functions return the same value: </span>0.7012920012119437<span style="color:rgb(213,218,222)">. </span><br>
<br>
<span style="color:rgb(213,218,222)">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="color:rgb(0,0,0);background-color:rgb(255,255,255);font-family:"Lucida Grande","Lucida Sans Unicode",Geneva,Verdana,sans-serif">This function returns the cosine of the specified operand, returning the same values as the libm </span><code>cos</code><span style="color:rgb(0,0,0);background-color:rgb(255,255,255);font-family:"Lucida Grande","Lucida Sans Unicode",Geneva,Verdana,sans-serif"> functions would</span><br>
<br>
<span style="color:rgb(213,218,222);background-color:rgba(0,0,0,0)">So does this mean that constrained cos does not care about the rounding mode but instead will just return the same value?</span></div>
</div>
<div name="messageSignatureSection"><br>
<div>Thank you
<div>Xuan Tang</div>
</div>
</div>
</div>

_______________________________________________<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" rel="noreferrer" target="_blank">https://lists.llvm.org/cgi-bin/mailman/listinfo/llvm-dev</a><br>
</blockquote></div>