sangeeta chowdhary via llvm-dev
2018-Aug-21 15:17 UTC
[llvm-dev] different output with fast-math flag
Why the output is different for this below program when compiled using clang with fast-math optimization #include<stdio.h> int main() { double d = 1.0; double max = 1.79769e+308; d /= max; printf("d:%e:\n", d); d *= max; printf("d:%e:\n", d); return 0; } prints 0 with fast math but 1 without fast math. -------------- next part -------------- An HTML attachment was scrubbed... URL: <http://lists.llvm.org/pipermail/llvm-dev/attachments/20180821/4f9a7ee4/attachment.html>
Stephen Canon via llvm-dev
2018-Aug-21 16:45 UTC
[llvm-dev] different output with fast-math flag
<html><head></head><body dir="auto" style="word-wrap: break-word; -webkit-nbsp-mode: space; line-break: after-white-space;"><meta http-equiv="Content-Type" content="text/html; charset=utf-8"><div style="word-wrap: break-word; -webkit-nbsp-mode: space; line-break: after-white-space;" class=""><blockquote type="cite" class="">On Aug 21, 2018, at 11:17 AM, sangeeta chowdhary via llvm-dev <<a href="mailto:llvm-dev@lists.llvm.org" class="">llvm-dev@lists.llvm.org</a>> wrote:<br class=""></blockquote><div><blockquote type="cite" class=""><br class="Apple-interchange-newline"><div class=""><div dir="ltr" class=""><font face="arial, helvetica, sans-serif" class="">Why the output is different for this below program when compiled using clang with fast-math optimization</font><div class=""><font face="arial, helvetica, sans-serif" class=""><br class=""></font></div><div class=""><div style="margin: 0px; font-variant-numeric: normal; font-variant-east-asian: normal; font-stretch: normal; line-height: normal; color: rgb(195, 55, 32);" class=""><font face="arial, helvetica, sans-serif" class=""><span class="gmail-s1" style="font-variant-ligatures:no-common-ligatures;color:rgb(213,59,211)">#include</span><span class="gmail-s2" style="font-variant-ligatures:no-common-ligatures"><stdio.h></span></font></div><div style="margin: 0px; font-variant-numeric: normal; font-variant-east-asian: normal; font-stretch: normal; line-height: normal; min-height: 21px;" class=""><font face="arial, helvetica, sans-serif" class=""><span class="gmail-s2" style="font-variant-ligatures:no-common-ligatures"></span><br class=""></font></div><div style="margin: 0px; font-variant-numeric: normal; font-variant-east-asian: normal; font-stretch: normal; line-height: normal;" class=""><font face="arial, helvetica, sans-serif" class=""><span class="gmail-s3" style="font-variant-ligatures:no-common-ligatures;color:rgb(52,188,38)">int</span><span class="gmail-s2" style="font-variant-ligatures:no-common-ligatures"> main() {</span></font></div><div style="margin: 0px; font-variant-numeric: normal; font-variant-east-asian: normal; font-stretch: normal; line-height: normal;" class=""><font face="arial, helvetica, sans-serif" class=""><span class="gmail-s2" style="font-variant-ligatures:no-common-ligatures"><span class="gmail-Apple-converted-space"> </span></span><span class="gmail-s3" style="font-variant-ligatures:no-common-ligatures;color:rgb(52,188,38)">double</span><span class="gmail-s2" style="font-variant-ligatures:no-common-ligatures"> d = </span><span class="gmail-s4" style="font-variant-ligatures:no-common-ligatures;color:rgb(195,55,32)">1.0</span><span class="gmail-s2" style="font-variant-ligatures:no-common-ligatures">;</span></font></div><div style="margin: 0px; font-variant-numeric: normal; font-variant-east-asian: normal; font-stretch: normal; line-height: normal;" class=""><font face="arial, helvetica, sans-serif" class=""><span class="gmail-s5" style="text-decoration-line:underline;font-variant-ligatures:no-common-ligatures"><span class="gmail-Apple-converted-space"> </span></span><span class="gmail-s6" style="text-decoration-line:underline;font-variant-ligatures:no-common-ligatures;color:rgb(52,188,38)">double</span><span class="gmail-s5" style="text-decoration-line:underline;font-variant-ligatures:no-common-ligatures"> max = </span><span class="gmail-s7" style="text-decoration-line:underline;font-variant-ligatures:no-common-ligatures;color:rgb(195,55,32)">1.79769e+308</span><span class="gmail-s5" style="text-decoration-line:underline;font-variant-ligatures:no-common-ligatures">; <span class="gmail-Apple-converted-space"> </span></span></font></div><div style="margin: 0px; font-variant-numeric: normal; font-variant-east-asian: normal; font-stretch: normal; line-height: normal;" class=""><span class="gmail-s2" style="font-variant-ligatures:no-common-ligatures"><font face="arial, helvetica, sans-serif" class=""><span class="gmail-Apple-converted-space"> </span>d /= max;</font></span></div><div style="margin: 0px; font-variant-numeric: normal; font-variant-east-asian: normal; font-stretch: normal; line-height: normal;" class=""><font face="arial, helvetica, sans-serif" class=""><span class="gmail-s2" style="font-variant-ligatures:no-common-ligatures"><span class="gmail-Apple-converted-space"> </span>printf(</span><span class="gmail-s4" style="font-variant-ligatures:no-common-ligatures;color:rgb(195,55,32)">"d:</span><span class="gmail-s1" style="font-variant-ligatures:no-common-ligatures;color:rgb(213,59,211)">%e</span><span class="gmail-s4" style="font-variant-ligatures:no-common-ligatures;color:rgb(195,55,32)">:</span><span class="gmail-s1" style="font-variant-ligatures:no-common-ligatures;color:rgb(213,59,211)">\n</span><span class="gmail-s4" style="font-variant-ligatures:no-common-ligatures;color:rgb(195,55,32)">"</span><span class="gmail-s2" style="font-variant-ligatures:no-common-ligatures">, d);</span></font></div><div style="margin: 0px; font-variant-numeric: normal; font-variant-east-asian: normal; font-stretch: normal; line-height: normal;" class=""><span class="gmail-s2" style="font-variant-ligatures:no-common-ligatures"><font face="arial, helvetica, sans-serif" class=""><span class="gmail-Apple-converted-space"> </span>d *= max;</font></span></div><div style="margin: 0px; font-variant-numeric: normal; font-variant-east-asian: normal; font-stretch: normal; line-height: normal;" class=""><font face="arial, helvetica, sans-serif" class=""><span class="gmail-s2" style="font-variant-ligatures:no-common-ligatures"><span class="gmail-Apple-converted-space"> </span>printf(</span><span class="gmail-s4" style="font-variant-ligatures:no-common-ligatures;color:rgb(195,55,32)">"d:</span><span class="gmail-s1" style="font-variant-ligatures:no-common-ligatures;color:rgb(213,59,211)">%e</span><span class="gmail-s4" style="font-variant-ligatures:no-common-ligatures;color:rgb(195,55,32)">:</span><span class="gmail-s1" style="font-variant-ligatures:no-common-ligatures;color:rgb(213,59,211)">\n</span><span class="gmail-s4" style="font-variant-ligatures:no-common-ligatures;color:rgb(195,55,32)">"</span><span class="gmail-s2" style="font-variant-ligatures:no-common-ligatures">, d);</span></font></div><div style="margin: 0px; font-variant-numeric: normal; font-variant-east-asian: normal; font-stretch: normal; line-height: normal; color: rgb(205, 121, 35);" class=""><font face="arial, helvetica, sans-serif" class=""><span class="gmail-s8" style="font-variant-ligatures: no-common-ligatures;"><span class="gmail-Apple-converted-space"> </span></span><span class="gmail-s2" style="font-variant-ligatures:no-common-ligatures">return</span><span class="gmail-s8" style="font-variant-ligatures: no-common-ligatures;"> </span><span class="gmail-s4" style="font-variant-ligatures:no-common-ligatures;color:rgb(195,55,32)">0</span><span class="gmail-s8" style="font-variant-ligatures: no-common-ligatures;">;</span></font></div><div style="margin: 0px; font-variant-numeric: normal; font-variant-east-asian: normal; font-stretch: normal; line-height: normal;" class=""><span class="gmail-s2" style="font-variant-ligatures:no-common-ligatures"><font face="arial, helvetica, sans-serif" class="">}</font></span></div><div style="margin: 0px; font-variant-numeric: normal; font-variant-east-asian: normal; font-stretch: normal; line-height: normal;" class=""><span class="gmail-s2" style="font-variant-ligatures:no-common-ligatures"><font face="arial, helvetica, sans-serif" class=""><br class=""></font></span></div><div style="margin: 0px; font-variant-numeric: normal; font-variant-east-asian: normal; font-stretch: normal; line-height: normal;" class=""><span style="font-variant-ligatures:no-common-ligatures" class=""><font face="arial, helvetica, sans-serif" style="" class="">prints 0 with fast math but 1 without fast math.</font></span></div></div></div></div></blockquote><br class=""></div><div>Please do not ask llvm-dev to do your homework. If this is genuinely not a school assignment, reply to me off-list and I’ll help you understand what’s happening here.</div><div><br class=""></div><div>– Steve</div></div></body></html>
sangeeta chowdhary via llvm-dev
2018-Aug-21 16:50 UTC
[llvm-dev] different output with fast-math flag
This is of course not homework. I am trying to understand how fast math optimizations work in llvm. When I compared IR for both the programs, the only thing I have noticed is that fdiv and fmul are replaced with fdiv fast and fmul fast. Not sure what happens in fdiv fast and fmul fast. I feel that its because d/max is really small number and fast-math does not care about small numbers and consider them to zero but this is so incorrect. On Tue, Aug 21, 2018 at 12:45 PM Stephen Canon <scanon at apple.com> wrote:> On Aug 21, 2018, at 11:17 AM, sangeeta chowdhary via llvm-dev < > llvm-dev at lists.llvm.org> wrote: > > > Why the output is different for this below program when compiled using > clang with fast-math optimization > > #include<stdio.h> > > int main() { > double d = 1.0; > double max = 1.79769e+308; > > d /= max; > printf("d:%e:\n", d); > d *= max; > printf("d:%e:\n", d); > return 0; > } > > prints 0 with fast math but 1 without fast math. > > > Please do not ask llvm-dev to do your homework. If this is genuinely not a > school assignment, reply to me off-list and I’ll help you understand what’s > happening here. > > – Steve >-------------- next part -------------- An HTML attachment was scrubbed... URL: <http://lists.llvm.org/pipermail/llvm-dev/attachments/20180821/ec04d063/attachment.html>