<div><span style="color:rgb(34,34,34);font-family:arial,sans-serif;font-size:13px;background-color:rgb(255,255,255)">This should work if your float is between INT_MIN and INT_MAX:</span></div><span style="color:rgb(34,34,34);font-family:arial,sans-serif;font-size:13px;background-color:rgb(255,255,255)"><div>
<span style="color:rgb(34,34,34);font-family:arial,sans-serif;font-size:13px;background-color:rgb(255,255,255)"><br></span></div>unsigned char trunc(float f) {</span><br style="color:rgb(34,34,34);font-family:arial,sans-serif;font-size:13px;background-color:rgb(255,255,255)">
<span style="color:rgb(34,34,34);font-family:arial,sans-serif;font-size:13px;background-color:rgb(255,255,255)">  return (unsigned char)(int)f;</span><br style="color:rgb(34,34,34);font-family:arial,sans-serif;font-size:13px;background-color:rgb(255,255,255)">
<span style="color:rgb(34,34,34);font-family:arial,sans-serif;font-size:13px;background-color:rgb(255,255,255)">}</span><div><br></div><div><div>int main() {</div><div>  printf("%d\n", trunc(-1.0));</div><div>}</div>
<div><br></div><div>For this, Clang produces:</div><div><br></div><div><div>trunc:</div></div><div><span class="Apple-tab-span" style="white-space:pre">        </span>trunc.w.s<span class="Apple-tab-span" style="white-space:pre">   </span>$f0, $f12</div>
<div><div><span class="Apple-tab-span" style="white-space:pre">   </span>mfc1<span class="Apple-tab-span" style="white-space:pre">        </span>$2, $f0</div><div><span class="Apple-tab-span" style="white-space:pre">      </span>andi<span class="Apple-tab-span" style="white-space:pre">        </span>$2, $2, 255</div>
<div><span class="Apple-tab-span" style="white-space:pre">      </span>jr<span class="Apple-tab-span" style="white-space:pre">  </span>$ra</div><div><span class="Apple-tab-span" style="white-space:pre">  </span>nop</div></div><div>
<br></div><div class="gmail_quote">On Mon, Aug 6, 2012 at 1:28 PM, ryan baird <span dir="ltr"><<a href="mailto:ryanrbaird@gmail.com" target="_blank">ryanrbaird@gmail.com</a>></span> wrote:<br><blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex">
<div class="gmail_quote"><blockquote class="gmail_quote" style="margin:0pt 0pt 0pt 0.8ex;border-left:1px solid rgb(204,204,204);padding-left:1ex">I didn't realize the code was undefined, I'll let my professor know; but for code comparison purposes we're still seeking advice on producing the output that matches the other compilers (even if it involves doing a translation on the .ll file or additional translation to the produced assembly).  We can't fairly compare the code if it doesn't do the same thing.  This bug came up in susan from the mibench test suite at the end of setup_brightness_lut where it casts float temp to an unsigned char.  I don't think we can include the results in our report if we change the tests.<br>


<br>Even though the behavior is undefined, it doesn't make sense to me that a function that returns an unsigned char would ever return a 32 bit -1, because that value does not fit the functions return type.<div class="im">
<div>
<div><br><br><div class="gmail_quote">
On Mon, Aug 6, 2012 at 2:03 PM, Eli Friedman <span dir="ltr"><<a href="mailto:eli.friedman@gmail.com" target="_blank">eli.friedman@gmail.com</a>></span> wrote:<br><blockquote class="gmail_quote" style="margin:0pt 0pt 0pt 0.8ex;border-left:1px solid rgb(204,204,204);padding-left:1ex">


<div>On Mon, Aug 6, 2012 at 12:43 PM, ryan baird <<a href="mailto:ryanrbaird@gmail.com" target="_blank">ryanrbaird@gmail.com</a>> wrote:<br>
> I am compiling the following code for the MIPS architecture:<br>
><br>
> unsigned char trunc(float f) {<br>
>         return (unsigned char) f;<br>
> }<br>
><br>
> and it produces the following assembly (directives removed for convenience:<br>
> trunc:<br>
>     trunc.w.s    $f0, $f12<br>
>     mfc1    $2, $f0<br>
>     jr    $ra<br>
>     nop<br>
><br>
> However, this does not seem to produce the correct output for negative<br>
> numbers. When I run the following code, I get -1 instead of 255 (which is<br>
> produced by compiling natively with gcc).<br>
> int trunc(float c);<br>
> int main() {<br>
>   printf("%d\n", trunc(-1.0));<br>
> }<br>
<br>
</div>That code has undefined behavior; see 6.3.1.4p1 in C99<br>
<span><font color="#888888"><br>
-Eli<br>
</font></span></blockquote></div><br>
</div></div></div></blockquote></div><br>
<br>_______________________________________________<br>
LLVM Developers mailing list<br>
<a href="mailto:LLVMdev@cs.uiuc.edu">LLVMdev@cs.uiuc.edu</a>         <a href="http://llvm.cs.uiuc.edu" target="_blank">http://llvm.cs.uiuc.edu</a><br>
<a href="http://lists.cs.uiuc.edu/mailman/listinfo/llvmdev" target="_blank">http://lists.cs.uiuc.edu/mailman/listinfo/llvmdev</a><br>
<br></blockquote></div><br></div>