<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
<style type="text/css" style="display:none;"><!-- P {margin-top:0;margin-bottom:0;} --></style>
</head>
<body dir="ltr">
<div id="divtagdefaultwrapper" style="font-size:12pt;color:#000000;font-family:Calibri,Helvetica,sans-serif;" dir="ltr">
<p style="margin-top:0;margin-bottom:0">This seems disappointing.</p>
<p style="margin-top:0;margin-bottom:0"><span style="font-size: 12pt;">Casting any value to an integral type should yield a value within the range of that integral type.</span><br>
</p>
<p style="margin-top:0;margin-bottom:0">The value can be anything, but it should be within range.</p>
<p style="margin-top:0;margin-bottom:0">i.e. it should be as if I anded it with (type)~(type)0.</p>
<p style="margin-top:0;margin-bottom:0"><br>
</p>
<p style="margin-top:0;margin-bottom:0">i.e. int i = (signed char)x;</p>
<p style="margin-top:0;margin-bottom:0">assert(i >= -128 && i <= 127);</p>
<p style="margin-top:0;margin-bottom:0"><br>
</p>
<p style="margin-top:0;margin-bottom:0"></p>
<p style="font-family: Calibri, Helvetica, sans-serif, Helvetica, EmojiFont, "Apple Color Emoji", "Segoe UI Emoji", NotoColorEmoji, "Segoe UI Symbol", "Android Emoji", EmojiSymbols; font-size: 16px;">
int i = (unsigned char)x;</p>
<p style="font-family: Calibri, Helvetica, sans-serif, Helvetica, EmojiFont, "Apple Color Emoji", "Segoe UI Emoji", NotoColorEmoji, "Segoe UI Symbol", "Android Emoji", EmojiSymbols; font-size: 16px;">
assert(i >= 0 && i <= 255);</p>
<div><br>
</div>
<div>seem very reasonable, assuming there is no "trap"  before the assert.</div>
<br>
<p></p>
<p style="margin-top:0;margin-bottom:0"> - Jay</p>
<p style="margin-top:0;margin-bottom:0"><br>
</p>
<br>
<div style="color: rgb(0, 0, 0);">
<hr style="display:inline-block;width:98%" tabindex="-1">
<div id="divRplyFwdMsg" dir="ltr"><font face="Calibri, sans-serif" style="font-size:11pt" color="#000000"><b>From:</b> Tim Northover <t.p.northover@gmail.com><br>
<b>Sent:</b> Thursday, April 5, 2018 4:41 PM<br>
<b>To:</b> Jay K<br>
<b>Cc:</b> llvm-dev@lists.llvm.org; lewurm@gmail.com<br>
<b>Subject:</b> Re: [llvm-dev] double to unsigned char cast</font>
<div> </div>
</div>
<div class="BodyFragment"><font size="2"><span style="font-size:11pt;">
<div class="PlainText">Hi Jay,<br>
<br>
On 5 April 2018 at 07:49, Jay K via llvm-dev <llvm-dev@lists.llvm.org> wrote:<br>
> wrt<br>
> <a href="https://github.com/mono/mono/commit/fb91fce5d339bb9ffe507588f5bc1d8d6f244d9b" id="LPlnk216227" class="OWAAutoLink" previewremoved="true">
https://github.com/mono/mono/commit/fb91fce5d339bb9ffe507588f5bc1d8d6f244d9b</a><br>
<br>
><br>
><br>
> This doesn't seem right to me.<br>
<br>
Nevertheless, it's what the standard says. C99 6.3.1.4: When a finite<br>
value of real floating type is converted to an integer type other than<br>
_Bool, the fractional part is discarded (i.e. the value is truncated<br>
toward zero). If the value of the integral part cannot be represented<br>
by the integer type, the behavior is undefined.<br>
<br>
By definition any double you're allowed to convert to an unsigned char<br>
doesn't need the "and".<br>
<br>
Cheers.<br>
<br>
Tim.<br>
</div>
</span></font></div>
</div>
</div>
</body>
</html>