<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
</head>
<body text="#000000" bgcolor="#FFFFFF">
<div class="moz-cite-prefix">Hi Matt,<br>
<br>
On 2019-02-27 4:31 PM, Matt Peddie wrote:<br>
</div>
<blockquote type="cite"
cite="mid:CAAwKdvhkGJ1hd7TXQZYBjg1VUQ-J0ch26MhWTUsy6p1TL1YSzw@mail.gmail.com">For
me [gcc] returns 32767 if I enable optimizations and 0 if I don't.</blockquote>
<br>
Yes, I get 0 without optimizations. Looking at the generated
assembly (with gcc -S) I can see that the non-optimized code is
converting the double to a 32-bit integer using a hardware
instruction (cvttsd2si) but in the optimized case it's using a
precomputed value. It's strange that the precomputed value doesn't
match, but as Ian said, all bets are off anyway.<br>
<br>
According to <a moz-do-not-send="true"
href="https://www.felixcloutier.com/x86/cvttsd2si">one authority</a>,
the conversion produces the 32-bit value 0x80000000. When truncated
to <tt>int16_t</tt> this is 0.<br>
<br>
<blockquote type="cite"
cite="mid:CAAwKdvhkGJ1hd7TXQZYBjg1VUQ-J0ch26MhWTUsy6p1TL1YSzw@mail.gmail.com">Clang
returns 0 independent of any flag changes I tried.</blockquote>
<br>
Clang's code uses a precomputed value even without optimization.<br>
<br>
Looking at GHC's generated core, it converts the Double to an Int
and then narrows down to Int16 after that.<br>
<br>
—Neil<br>
</body>
</html>