<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">I acknowledge that the type system is full of holes ("C++ is not safe") and the standard gives you leeway, but I've never before witnessed a cast to an integer type not yielding some value in range for that integer type.
 And even clang does provide such here, on x86 and amd64.</p>
<p style="margin-top:0;margin-bottom:0"><br>
</p>
<p style="margin-top:0;margin-bottom:0">I have to also read our spec to see what we are supposed to return here also.</p>
<p style="margin-top:0;margin-bottom:0">The "workaround" of casting to int first doesn't seem any better defined than casting directly to char, given a double that doesn't fit in int either.</p>
<p style="margin-top:0;margin-bottom:0"></p>
<p style="margin-top:0;margin-bottom:0"><br>
</p>
 - Jay
<div><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> James Y Knight <jyknight@google.com><br>
<b>Sent:</b> Friday, May 25, 2018 1:39 PM<br>
<b>To:</b> Hans Wennborg<br>
<b>Cc:</b> jayk123@hotmail.com; llvm-dev; lewurm@gmail.com<br>
<b>Subject:</b> Re: [llvm-dev] double to unsigned char cast</font>
<div> </div>
</div>
<meta content="text/html; charset=utf-8">
<div>
<div dir="ltr">I'd also note that -fsanitize=undefined will detect the error at runtime.</div>
<br>
<div class="x_gmail_quote">
<div dir="ltr">On Fri, May 25, 2018 at 4:38 AM Hans Wennborg via llvm-dev <<a href="mailto:llvm-dev@lists.llvm.org" id="LPlnk396035" class="OWAAutoLink" previewremoved="true">llvm-dev@lists.llvm.org</a>> wrote:<br>
</div>
<blockquote class="x_gmail_quote" style="margin:0 0 0 .8ex; border-left:1px #ccc solid; padding-left:1ex">
On Tue, May 22, 2018 at 6:17 PM, Jay K via llvm-dev<br>
<<a href="mailto:llvm-dev@lists.llvm.org" target="_blank" id="LPlnk611876" class="OWAAutoLink" previewremoved="true">llvm-dev@lists.llvm.org</a>> wrote:<br>
> This seems disappointing.<br>
><br>
> Casting any value to an integral type should yield a value within the range<br>
> of that integral type.<br>
<br>
As Tim pointed out, that's not what the standard says though.<br>
<br>
Trunk Clang (I'm not sure what version your Xcode Clang is based on)<br>
has a flag to disable this optimization:<br>
-fno-strict-float-cast-overflow<br>
<br>
 - Hans<br>
<br>
<br>
> The value can be anything, but it should be within range.<br>
><br>
> i.e. it should be as if I anded it with (type)~(type)0.<br>
><br>
><br>
> i.e. int i = (signed char)x;<br>
><br>
> assert(i >= -128 && i <= 127);<br>
><br>
><br>
> int i = (unsigned char)x;<br>
><br>
> assert(i >= 0 && i <= 255);<br>
><br>
><br>
> seem very reasonable, assuming there is no "trap"  before the assert.<br>
><br>
>  - Jay<br>
><br>
><br>
><br>
> ________________________________<br>
> From: Tim Northover <<a href="mailto:t.p.northover@gmail.com" target="_blank" id="LPlnk753615" class="OWAAutoLink" previewremoved="true">t.p.northover@gmail.com</a>><br>
> Sent: Thursday, April 5, 2018 4:41 PM<br>
> To: Jay K<br>
> Cc: <a href="mailto:llvm-dev@lists.llvm.org" target="_blank" id="LPlnk369615" class="OWAAutoLink" previewremoved="true">
llvm-dev@lists.llvm.org</a>; <a href="mailto:lewurm@gmail.com" target="_blank" id="LPlnk863075" class="OWAAutoLink" previewremoved="true">
lewurm@gmail.com</a><br>
> Subject: Re: [llvm-dev] double to unsigned char cast<br>
><br>
> Hi Jay,<br>
><br>
> On 5 April 2018 at 07:49, Jay K via llvm-dev <<a href="mailto:llvm-dev@lists.llvm.org" target="_blank" id="LPlnk613724" class="OWAAutoLink" previewremoved="true">llvm-dev@lists.llvm.org</a>><br>
> wrote:<br>
>> wrt<br>
>><br>
>> <a href="https://github.com/mono/mono/commit/fb91fce5d339bb9ffe507588f5bc1d8d6f244d9b" rel="noreferrer" target="_blank" id="LPlnk651054" 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 <a href="http://6.3.1.4" rel="noreferrer" target="_blank" id="LPlnk440245" class="OWAAutoLink" previewremoved="true">
6.3.1.4</a>: 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>
><br>
> _______________________________________________<br>
> LLVM Developers mailing list<br>
> <a href="mailto:llvm-dev@lists.llvm.org" target="_blank" id="LPlnk982971" class="OWAAutoLink" previewremoved="true">
llvm-dev@lists.llvm.org</a><br>
> <a href="http://lists.llvm.org/cgi-bin/mailman/listinfo/llvm-dev" rel="noreferrer" target="_blank" id="LPlnk92331" class="OWAAutoLink" previewremoved="true">
http://lists.llvm.org/cgi-bin/mailman/listinfo/llvm-dev</a><br>
><br>
_______________________________________________<br>
LLVM Developers mailing list<br>
<a href="mailto:llvm-dev@lists.llvm.org" target="_blank" id="LPlnk52192" class="OWAAutoLink" previewremoved="true">llvm-dev@lists.llvm.org</a><br>
<a href="http://lists.llvm.org/cgi-bin/mailman/listinfo/llvm-dev" rel="noreferrer" target="_blank" id="LPlnk807900" class="OWAAutoLink" previewremoved="true">http://lists.llvm.org/cgi-bin/mailman/listinfo/llvm-dev</a><br>
</blockquote>
</div>
</div>
</div>
</div>
</div>
</body>
</html>