<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<title></title>
</head>
<body>
<div name="messageBodySection">
<div dir="auto">Hi Craig:<br />
<br />
Thanks for the reply. Now I put a set rounding mode before the corresponding code, but strange things happen.<br />
<br />
With the same input, the rounding down code returns larger value than the rounding up value with cos.<br />
<br />
Initially I thought it might because the input somehow got rounded down, and since the input is between 0 and pi, which is on a downward slope, rounding down the input will increase the value hence the error. Then I tested some value larger than pi but smaller than 2pi, which has positive derivative, but the error still happens. I think there might be something wrong with the cosine intrinsic?<br />
<br />
I’ve attached the code at the end, and before each call, the rounding mode is set back to original (round to nearest).<br />
<br />
<em style="color:#bbbbbb;font-size: 12px">; Function Attrs: ssp uwtable</em><span style="font-size: 12px"><br /></span><strong style="color:#b877db;font-size: 12px">define</strong><span style="color:#bbbbbb;font-size: 12px"> </span><strong style="color:#b877db;font-size: 12px">double</strong><span style="color:#bbbbbb;font-size: 12px"> </span><em style="color:#fac29a;font-size: 12px">@cosine_down</em><span style="color:#bbbbbb;font-size: 12px">(</span><strong style="color:#b877db;font-size: 12px">double</strong><span style="color:#bbbbbb;font-size: 12px"> returned) local_unnamed_addr #</span><span style="color:#f09483;font-size: 12px">0</span><span style="color:#bbbbbb;font-size: 12px"> {</span><span style="font-size: 12px"><br /></span><span style="color:#bbbbbb;font-size: 12px">  </span><em style="color:#fac29a;font-size: 12px">%2</em><span style="color:#bbbbbb;font-size: 12px"> = </span><strong style="color:#b877db;font-size: 12px">call</strong><span style="color:#bbbbbb;font-size: 12px"> </span><strong style="color:#b877db;font-size: 12px">i32</strong><span style="color:#bbbbbb;font-size: 12px"> </span><em style="color:#fac29a;font-size: 12px">@fesetround</em><span style="color:#bbbbbb;font-size: 12px">(</span><strong style="color:#b877db;font-size: 12px">i32</strong><span style="color:#bbbbbb;font-size: 12px"> </span><span style="color:#f09483;font-size: 12px">1024</span><span style="color:#bbbbbb;font-size: 12px">)</span><span style="font-size: 12px"><br /></span><span style="color:#bbbbbb;font-size: 12px">  </span><em style="color:#fac29a;font-size: 12px">%3</em><span style="color:#bbbbbb;font-size: 12px"> = </span><strong style="color:#b877db;font-size: 12px">call</strong><span style="color:#bbbbbb;font-size: 12px"> </span><strong style="color:#b877db;font-size: 12px">double</strong><span style="color:#bbbbbb;font-size: 12px"> </span><em style="color:#fac29a;font-size: 12px">@llvm.experimental.constrained.cos</em><span style="color:#bbbbbb;font-size: 12px">(</span><strong style="color:#b877db;font-size: 12px">double</strong><span style="color:#bbbbbb;font-size: 12px"> </span><em style="color:#fac29a;font-size: 12px">%0</em><span style="color:#bbbbbb;font-size: 12px">, </span><strong style="color:#b877db;font-size: 12px">metadata</strong><span style="color:#bbbbbb;font-size: 12px"> !</span><span style="color:#fab795;font-size: 12px">"round</span><span style="color:#25b0bc;font-size: 12px">.d</span><span style="color:#fab795;font-size: 12px">ownward"</span><span style="color:#bbbbbb;font-size: 12px">, </span><strong style="color:#b877db;font-size: 12px">metadata</strong><span style="color:#bbbbbb;font-size: 12px"> !</span><span style="color:#fab795;font-size: 12px">"fpexcept</span><span style="color:#25b0bc;font-size: 12px">.s</span><span style="color:#fab795;font-size: 12px">trict"</span><span style="color:#bbbbbb;font-size: 12px">)</span><span style="font-size: 12px"><br /></span><span style="color:#bbbbbb;font-size: 12px">  </span><strong style="color:#b877db;font-size: 12px">ret</strong><span style="color:#bbbbbb;font-size: 12px"> </span><strong style="color:#b877db;font-size: 12px">double</strong><span style="color:#bbbbbb;font-size: 12px"> </span><em style="color:#fac29a;font-size: 12px">%3</em><span style="font-size: 12px"><br /></span><span style="color:#bbbbbb;font-size: 12px">}</span><span style="font-size: 12px"><br /></span><span style="font-size: 12px"><br /></span><em style="color:#bbbbbb;font-size: 12px">; Function Attrs: ssp uwtable</em><span style="font-size: 12px"><br /></span><strong style="color:#b877db;font-size: 12px">define</strong><span style="color:#bbbbbb;font-size: 12px"> </span><strong style="color:#b877db;font-size: 12px">double</strong><span style="color:#bbbbbb;font-size: 12px"> </span><em style="color:#fac29a;font-size: 12px">@cosine_up</em><span style="color:#bbbbbb;font-size: 12px">(</span><strong style="color:#b877db;font-size: 12px">double</strong><span style="color:#bbbbbb;font-size: 12px"> returned) local_unnamed_addr #</span><span style="color:#f09483;font-size: 12px">0</span><span style="color:#bbbbbb;font-size: 12px"> {</span><span style="font-size: 12px"><br /></span><span style="color:#bbbbbb;font-size: 12px">  </span><em style="color:#fac29a;font-size: 12px">%2</em><span style="color:#bbbbbb;font-size: 12px"> = </span><strong style="color:#b877db;font-size: 12px">call</strong><span style="color:#bbbbbb;font-size: 12px"> </span><strong style="color:#b877db;font-size: 12px">i32</strong><span style="color:#bbbbbb;font-size: 12px"> </span><em style="color:#fac29a;font-size: 12px">@fesetround</em><span style="color:#bbbbbb;font-size: 12px">(</span><strong style="color:#b877db;font-size: 12px">i32</strong><span style="color:#bbbbbb;font-size: 12px"> </span><span style="color:#f09483;font-size: 12px">2048</span><span style="color:#bbbbbb;font-size: 12px">)</span><span style="font-size: 12px"><br /></span><span style="color:#bbbbbb;font-size: 12px">  </span><em style="color:#fac29a;font-size: 12px">%3</em><span style="color:#bbbbbb;font-size: 12px"> = </span><strong style="color:#b877db;font-size: 12px">call</strong><span style="color:#bbbbbb;font-size: 12px"> </span><strong style="color:#b877db;font-size: 12px">double</strong><span style="color:#bbbbbb;font-size: 12px"> </span><em style="color:#fac29a;font-size: 12px">@llvm.experimental.constrained.cos</em><span style="color:#bbbbbb;font-size: 12px">(</span><strong style="color:#b877db;font-size: 12px">double</strong><span style="color:#bbbbbb;font-size: 12px"> </span><em style="color:#fac29a;font-size: 12px">%0</em><span style="color:#bbbbbb;font-size: 12px">, </span><strong style="color:#b877db;font-size: 12px">metadata</strong><span style="color:#bbbbbb;font-size: 12px"> !</span><span style="color:#fab795;font-size: 12px">"round</span><span style="color:#25b0bc;font-size: 12px">.u</span><span style="color:#fab795;font-size: 12px">pward"</span><span style="color:#bbbbbb;font-size: 12px">, </span><strong style="color:#b877db;font-size: 12px">metadata</strong><span style="color:#bbbbbb;font-size: 12px"> !</span><span style="color:#fab795;font-size: 12px">"fpexcept</span><span style="color:#25b0bc;font-size: 12px">.s</span><span style="color:#fab795;font-size: 12px">trict"</span><span style="color:#bbbbbb;font-size: 12px">)</span><span style="font-size: 12px"><br /></span><span style="color:#bbbbbb;font-size: 12px">  </span><strong style="color:#b877db;font-size: 12px">ret</strong><span style="color:#bbbbbb;font-size: 12px"> </span><strong style="color:#b877db;font-size: 12px">double</strong><span style="color:#bbbbbb;font-size: 12px"> </span><em style="color:#fac29a;font-size: 12px">%3</em><span style="font-size: 12px"><br /></span><span style="color:#bbbbbb;font-size: 12px">}</span><span style="font-size: 12px"><br /></span><span style="font-size: 12px"><br /></span><span style="color:#d5dade;background-color:rgba(0, 0, 0, 0)">Test result: </span><br />
<br />
Input interval from 0.7935880586501369 to 0.7935880586501369, cosine result from 0.7012920012119436 to 0.7012920012119435<br />
is empty: true<br />
<br />
Input interval from 3.17588058650137 to 3.17588058650137, cosine result from -0.9994122264169831 to -0.9994122265888515<br />
is empty: true<br /></div>
</div>
<div name="messageSignatureSection"><br />
<div class="matchFont">Thank you
<div>Xuan Tang</div>
</div>
</div>
<div name="messageReplySection">On Sep 9, 2020, 00:28 -0500, Craig Topper <craig.topper@gmail.com>, wrote:<br />
<blockquote type="cite" style="border-left-color: grey; border-left-width: thin; border-left-style: solid; margin: 5px 5px;padding-left: 10px;">
<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>
</blockquote>
</div>
</body>
</html>