<html xmlns:v="urn:schemas-microsoft-com:vml" xmlns:o="urn:schemas-microsoft-com:office:office" xmlns:w="urn:schemas-microsoft-com:office:word" xmlns:m="http://schemas.microsoft.com/office/2004/12/omml" xmlns="http://www.w3.org/TR/REC-html40">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=us-ascii">
<meta name="Generator" content="Microsoft Word 15 (filtered medium)">
<style><!--
/* Font Definitions */
@font-face
        {font-family:"Cambria Math";
        panose-1:2 4 5 3 5 4 6 3 2 4;}
@font-face
        {font-family:Calibri;
        panose-1:2 15 5 2 2 2 4 3 2 4;}
@font-face
        {font-family:"Calisto MT";
        panose-1:2 4 6 3 5 5 5 3 3 4;}
/* Style Definitions */
p.MsoNormal, li.MsoNormal, div.MsoNormal
        {margin:0in;
        margin-bottom:.0001pt;
        font-size:12.0pt;
        font-family:"Times New Roman",serif;}
a:link, span.MsoHyperlink
        {mso-style-priority:99;
        color:blue;
        text-decoration:underline;}
a:visited, span.MsoHyperlinkFollowed
        {mso-style-priority:99;
        color:purple;
        text-decoration:underline;}
span.im
        {mso-style-name:im;}
span.EmailStyle18
        {mso-style-type:personal-reply;
        font-family:"Calisto MT",serif;
        color:#7030A0;}
.MsoChpDefault
        {mso-style-type:export-only;
        font-family:"Calibri",sans-serif;}
@page WordSection1
        {size:8.5in 11.0in;
        margin:1.0in 1.0in 1.0in 1.0in;}
div.WordSection1
        {page:WordSection1;}
--></style><!--[if gte mso 9]><xml>
<o:shapedefaults v:ext="edit" spidmax="1026" />
</xml><![endif]--><!--[if gte mso 9]><xml>
<o:shapelayout v:ext="edit">
<o:idmap v:ext="edit" data="1" />
</o:shapelayout></xml><![endif]-->
</head>
<body lang="EN-US" link="blue" vlink="purple">
<div class="WordSection1">
<p class="MsoNormal"><span style="font-family:&quot;Calisto MT&quot;,serif;color:#7030A0">Agreed, and that is the easier part :-)<o:p></o:p></span></p>
<p class="MsoNormal"><span style="font-family:&quot;Calisto MT&quot;,serif;color:#7030A0"><o:p>&nbsp;</o:p></span></p>
<p class="MsoNormal"><span style="font-family:&quot;Calisto MT&quot;,serif;color:#7030A0">I&#8217;m trying to find a middle-ground for people to get certainty (the implementation will tell you programmatically what the encoding is), while removing or reducing the surface area
 of undefined behavior.&nbsp; My suspicion is that we will end up with 2&#8217;s complement for most cases.&nbsp; But that is not based on a scientific study.<o:p></o:p></span></p>
<p class="MsoNormal"><span style="font-family:&quot;Calisto MT&quot;,serif;color:#7030A0"><o:p>&nbsp;</o:p></span></p>
<p class="MsoNormal"><span style="font-family:&quot;Calisto MT&quot;,serif;color:#7030A0">-- Gaby<o:p></o:p></span></p>
<p class="MsoNormal"><a name="_MailEndCompose"><span style="font-family:&quot;Calisto MT&quot;,serif;color:#7030A0"><o:p>&nbsp;</o:p></span></a></p>
<div style="border:none;border-left:solid blue 1.5pt;padding:0in 0in 0in 4.0pt">
<div>
<div style="border:none;border-top:solid #E1E1E1 1.0pt;padding:3.0pt 0in 0in 0in">
<p class="MsoNormal"><b><span style="font-size:11.0pt;font-family:&quot;Calibri&quot;,sans-serif">From:</span></b><span style="font-size:11.0pt;font-family:&quot;Calibri&quot;,sans-serif"> ub-bounces@open-std.org [mailto:ub-bounces@open-std.org]
<b>On Behalf Of </b>Jeffrey Yasskin<br>
<b>Sent:</b> Monday, November 17, 2014 12:32 PM<br>
<b>To:</b> WG21 UB study group<br>
<b>Subject:</b> Re: [ub] Signed shifting<o:p></o:p></span></p>
</div>
</div>
<p class="MsoNormal"><o:p>&nbsp;</o:p></p>
<div>
<p class="MsoNormal">We'd still need to define the behavior of signed shifting when encoding&lt;T&gt;==binary_system::twos_complement, for it to help this use case.<o:p></o:p></p>
<div>
<div>
<div>
<div>
<p class="MsoNormal"><o:p>&nbsp;</o:p></p>
<div>
<p class="MsoNormal">On Mon, Nov 17, 2014 at 11:59 AM, Gabriel Dos Reis &lt;<a href="mailto:gdr@microsoft.com" target="_blank">gdr@microsoft.com</a>&gt; wrote:<o:p></o:p></p>
<blockquote style="border:none;border-left:solid #CCCCCC 1.0pt;padding:0in 0in 0in 6.0pt;margin-left:4.8pt;margin-right:0in">
<p class="MsoNormal">Or require implementations to tell you programmatically what they are using as binary representation:<br>
<br>
&nbsp; &nbsp; enum class binary_system {<br>
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; other,<br>
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; sign_magnitude,<br>
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; ones_complement,<br>
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; twos_complement<br>
&nbsp; &nbsp; };<br>
<br>
&nbsp; &nbsp; template&lt;BuiltinIntegralType T&gt;<br>
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; constexpr binary_system encoding = /* implementation defined */<br>
<br>
-- Gaby<br>
<br>
<span class="im">| -----Original Message-----</span><br>
<span class="im">| From: <a href="mailto:ub-bounces@open-std.org">ub-bounces@open-std.org</a> [mailto:<a href="mailto:ub-bounces@open-std.org">ub-bounces@open-std.org</a>] On</span><br>
<span class="im">| Behalf Of Jeffrey Yasskin</span><br>
<span class="im">| Sent: Monday, November 17, 2014 11:11 AM</span><br>
<span class="im">| To: WG21 UB study group</span><br>
<span class="im">| Subject: Re: [ub] Signed shifting</span><br>
<span class="im">|</span><o:p></o:p></p>
<div>
<div>
<p class="MsoNormal">| * The last thread on two's-complement representation managed to find<br>
| one modern Unisys system that uses one's-complement, and the thread<br>
| reported that this one supports Java, so it has to have a way to<br>
| execute two's-complement shifts. We shouldn't be building a standard<br>
| for imaginary systems.<br>
|<br>
| * Even if 'int' needs to support non-two's-complement, the referenced<br>
| code<br>
| (<a href="https://github.com/teor2345/tor/blob/ced74e0144e967f838416e6af92cba6" target="_blank">https://github.com/teor2345/tor/blob/ced74e0144e967f838416e6af92cba6</a><br>
| 5c007d89b/src/ext/curve25519_donna/curve25519-donna.c#L56)<br>
| uses int64_t, which &lt;C99 7.18.1.1 Exact-width integer types&gt; says is<br>
| already two's complement. C&#43;&#43;14 defers to C99 for this. We could<br>
| define shifts on two's complement types that would stay<br>
| implementation-defined on anything else.<br>
|<br>
| * Programmers are used to writing &quot;X &lt;&lt; N&quot; to mean &quot;X * 2^N&quot;. Without<br>
| a good reason, we shouldn't make them use worse syntax for this, even<br>
| for signed numbers. &quot;Applying [bit-shifts] to signed numbers is a bug&quot;<br>
| is just incorrect given compilers' and hardware's actual behavior.<br>
| (Appealing to the fact that it's undefined behavior in the current<br>
| standard is just begging the question.)<br>
|<br>
| * If we wanted to provide a library function to represent shifting,<br>
| I'm having trouble coming up with a good name for it. A free library<br>
| function operating on integers is terrible: times_two_to_the(X, N)? A<br>
| member function for the constant-time class that competes with &quot;&lt;&lt; N&quot;<br>
| is also hard to produce: X.times_two_to_the(N)? X.times_pow2(N)? The<br>
| main objection to the constant-time library in LEWG was that we<br>
| weren't sure anyone would use it. Insisting that folks switch from<br>
| operators to functions would increase that risk.<br>
|<br>
| Jeffrey<br>
|<br>
| On Sat, Nov 15, 2014 at 9:15 AM, Jens Maurer &lt;<a href="mailto:Jens.Maurer@gmx.net">Jens.Maurer@gmx.net</a>&gt;<br>
| wrote:<br>
| &gt;<br>
| &gt; A preliminary observation: The C&#43;&#43; standard (in contrast to the C<br>
| &gt; standard) does not explicitly prescribe one of&nbsp; two's complement,<br>
| &gt; one's complement, sign-magnitude for signed integer representations,<br>
| &gt; but certainly those three choices are allowed by C&#43;&#43;.<br>
| &gt;<br>
| &gt; On 11/14/2014 11:45 PM, Jeffrey Yasskin wrote:<br>
| &gt;&gt; We could suggest that the programmers explicitly multiply by a power<br>
| &gt;&gt; of 2 instead, but they're using the &lt;&lt;s at least partly to get<br>
| &gt;&gt; constant-time operation, and relying on multiplication for this would<br>
| &gt;&gt; depend even more on implementation details than they already are.<br>
| &gt;<br>
| &gt; This seems to rely on two's complement representation for negative<br>
| &gt; numbers, which is (from the standard's perspective) a non-portable<br>
| &gt; assumption.<br>
| &gt;<br>
| &gt; In my opinion, users expecting constant-time operations would be<br>
| &gt; better served with a (small) library that allows them to express their<br>
| &gt; intent, instead of misusing shifts for multiplication.&nbsp; For example,<br>
| &gt; the following would help (after adding a &quot;multiplication-by-power-of-two&quot;<br>
| &gt; function):<br>
| &gt; <a href="http://www.open-std.org/jtc1/sc22/wg21/docs/papers/2014/n4145.html" target="_blank">
http://www.open-std.org/jtc1/sc22/wg21/docs/papers/2014/n4145.html</a><br>
| &gt;<br>
| &gt;&gt; Separately, a related piece of code uses a signed right-shift to<br>
| &gt;&gt; repeat the sign bit across all the bits of a number:<br>
| &gt;&gt;<br>
| <a href="https://boringssl.googlesource.com/boringssl/&#43;/e18d821dfcc32532caeeb1a2" target="_blank">
https://boringssl.googlesource.com/boringssl/&#43;/e18d821dfcc32532caeeb1a2</a><br>
| d15090672f592ce3/crypto/internal.h#157.<br>
| &gt;&gt; This is implementation-defined rather than undefined, so the<br>
| &gt;&gt; programmers can probably use #if to check for the behavior they<br>
| &gt;&gt; expect, as at<br>
| &gt;&gt;<br>
| <a href="https://github.com/teor2345/tor/blob/ced74e0144e967f838416e6af92cba65" target="_blank">
https://github.com/teor2345/tor/blob/ced74e0144e967f838416e6af92cba65</a><br>
| c007d89b/src/ext/curve25519_donna/curve25519-donna.c#L466<br>
| &gt;<br>
| &gt; Well, that's still a bit disappointing, since you get an #error if your<br>
| &gt; compiler doesn't provide the expected semantics.<br>
| &gt;<br>
| &gt; If we can identify a small set of basic operations that people want to<br>
| &gt; use, we should give them a (small) library instead of asking them to do<br>
| &gt; non-portable things.<br>
| &gt;<br>
| &gt;&gt; Is there space to improve the situation here?<br>
| &gt;<br>
| &gt; Yes, but (in my opinion), it's not in the direction of changing the<br>
| &gt; core language for these purposes.<br>
| &gt;<br>
| &gt; Bit-shifts are for unsigned numbers; applying them to signed numbers<br>
| &gt; is a bug, in my opinion.<br>
| &gt;<br>
| &gt; Jens<br>
| &gt; _______________________________________________<br>
| &gt; ub mailing list<br>
| &gt; <a href="mailto:ub@isocpp.open-std.org">ub@isocpp.open-std.org</a><br>
| &gt; <a href="http://www.open-std.org/mailman/listinfo/ub" target="_blank">http://www.open-std.org/mailman/listinfo/ub</a><br>
| _______________________________________________<br>
| ub mailing list<br>
| <a href="mailto:ub@isocpp.open-std.org">ub@isocpp.open-std.org</a><br>
| <a href="http://www.open-std.org/mailman/listinfo/ub" target="_blank">http://www.open-std.org/mailman/listinfo/ub</a><br>
_______________________________________________<br>
ub mailing list<br>
<a href="mailto:ub@isocpp.open-std.org">ub@isocpp.open-std.org</a><br>
<a href="http://www.open-std.org/mailman/listinfo/ub" target="_blank">http://www.open-std.org/mailman/listinfo/ub</a><o:p></o:p></p>
</div>
</div>
</blockquote>
</div>
<p class="MsoNormal"><o:p>&nbsp;</o:p></p>
</div>
</div>
</div>
</div>
</div>
</div>
</div>
</body>
</html>