<html><head><meta http-equiv="Content-Type" content="text/html charset=windows-1252"><base href="x-msg://1942/"></head><body style="word-wrap: break-word; -webkit-nbsp-mode: space; -webkit-line-break: after-white-space; "><br><div><div>On Nov 17, 2014, at 3:57 PM, Gabriel Dos Reis &lt;<a href="mailto:gdr@microsoft.com">gdr@microsoft.com</a>&gt; wrote:</div><br class="Apple-interchange-newline"><blockquote type="cite"><div lang="EN-US" link="blue" vlink="purple" style="font-family: Helvetica; font-size: medium; font-style: normal; font-variant: normal; font-weight: normal; letter-spacing: normal; line-height: normal; orphans: 2; text-align: -webkit-auto; text-indent: 0px; text-transform: none; white-space: normal; widows: 2; word-spacing: 0px; -webkit-text-size-adjust: auto; -webkit-text-stroke-width: 0px; "><div class="WordSection1" style="page: WordSection1; "><div style="margin: 0in 0in 0.0001pt; font-size: 12pt; font-family: 'Times New Roman', serif; "><span style="font-family: 'Calisto MT', serif; color: rgb(112, 48, 160); ">Agreed, and that is the easier part :-)<o:p></o:p></span></div><div style="margin: 0in 0in 0.0001pt; font-size: 12pt; font-family: 'Times New Roman', serif; "><span style="font-family: 'Calisto MT', serif; color: rgb(112, 48, 160); ">&nbsp;</span></div><div style="margin: 0in 0in 0.0001pt; font-size: 12pt; font-family: 'Times New Roman', serif; "><span style="font-family: 'Calisto MT', serif; color: rgb(112, 48, 160); ">I’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’s complement for most cases.&nbsp; But that is not based on a scientific study.</span></div></div></div></blockquote><div><br></div><div>We could define shifting of signed values to work as-if on a twos-complement system.</div><div><br></div><div>I could be wrong, but that might even be the preference of people with non-twos-complement hardware. &nbsp; Given a choice of having code ported to their platform failing in subtle ways vs. running slightly slower, they might prefer the latter.</div><div><br></div><div>John.</div><br><blockquote type="cite"><div lang="EN-US" link="blue" vlink="purple" style="font-family: Helvetica; font-size: medium; font-style: normal; font-variant: normal; font-weight: normal; letter-spacing: normal; line-height: normal; orphans: 2; text-align: -webkit-auto; text-indent: 0px; text-transform: none; white-space: normal; widows: 2; word-spacing: 0px; -webkit-text-size-adjust: auto; -webkit-text-stroke-width: 0px; "><div class="WordSection1" style="page: WordSection1; "><div style="margin: 0in 0in 0.0001pt; font-size: 12pt; font-family: 'Times New Roman', serif; "><span style="font-family: 'Calisto MT', serif; color: rgb(112, 48, 160); "><o:p></o:p></span></div><div style="margin: 0in 0in 0.0001pt; font-size: 12pt; font-family: 'Times New Roman', serif; "><span style="font-family: 'Calisto MT', serif; color: rgb(112, 48, 160); ">&nbsp;</span></div><div style="margin: 0in 0in 0.0001pt; font-size: 12pt; font-family: 'Times New Roman', serif; "><span style="font-family: 'Calisto MT', serif; color: rgb(112, 48, 160); ">-- Gaby<o:p></o:p></span></div><div style="margin: 0in 0in 0.0001pt; font-size: 12pt; font-family: 'Times New Roman', serif; "><a name="_MailEndCompose"><span style="font-family: 'Calisto MT', serif; color: rgb(112, 48, 160); ">&nbsp;</span></a></div><div style="border-style: none none none solid; border-left-width: 1.5pt; border-left-color: blue; padding: 0in 0in 0in 4pt; "><div><div style="border-style: solid none none; border-top-width: 1pt; border-top-color: rgb(225, 225, 225); padding: 3pt 0in 0in; "><div style="margin: 0in 0in 0.0001pt; font-size: 12pt; font-family: 'Times New Roman', serif; "><b><span style="font-size: 11pt; font-family: Calibri, sans-serif; ">From:</span></b><span style="font-size: 11pt; font-family: Calibri, sans-serif; "><span class="Apple-converted-space">&nbsp;</span><a href="mailto:ub-bounces@open-std.org">ub-bounces@open-std.org</a> [mailto:ub-<a href="mailto:bounces@open-std.org">bounces@open-std.org</a>]<span class="Apple-converted-space">&nbsp;</span><b>On Behalf Of<span class="Apple-converted-space">&nbsp;</span></b>Jeffrey Yasskin<br><b>Sent:</b><span class="Apple-converted-space">&nbsp;</span>Monday, November 17, 2014 12:32 PM<br><b>To:</b><span class="Apple-converted-space">&nbsp;</span>WG21 UB study group<br><b>Subject:</b><span class="Apple-converted-space">&nbsp;</span>Re: [ub] Signed shifting<o:p></o:p></span></div></div></div><div style="margin: 0in 0in 0.0001pt; font-size: 12pt; font-family: 'Times New Roman', serif; "><o:p>&nbsp;</o:p></div><div><div style="margin: 0in 0in 0.0001pt; font-size: 12pt; font-family: 'Times New Roman', serif; ">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></div><div><div style="margin: 0in 0in 0.0001pt; font-size: 12pt; font-family: 'Times New Roman', serif; "><o:p>&nbsp;</o:p></div><div><div style="margin: 0in 0in 0.0001pt; font-size: 12pt; font-family: 'Times New Roman', serif; ">On Mon, Nov 17, 2014 at 11:59 AM, Gabriel Dos Reis &lt;<a href="mailto:gdr@microsoft.com" target="_blank" style="color: purple; text-decoration: underline; ">gdr@microsoft.com</a>&gt; wrote:<o:p></o:p></div><blockquote style="border-style: none none none solid; border-left-width: 1pt; border-left-color: rgb(204, 204, 204); padding: 0in 0in 0in 6pt; margin-left: 4.8pt; margin-right: 0in; "><div style="margin: 0in 0in 0.0001pt; font-size: 12pt; font-family: 'Times New Roman', serif; ">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:<span class="Apple-converted-space">&nbsp;</span><a href="mailto:ub-bounces@open-std.org" style="color: purple; text-decoration: underline; ">ub-bounces@open-std.org</a><span class="Apple-converted-space">&nbsp;</span>[mailto:<a href="mailto:ub-bounces@open-std.org" style="color: purple; text-decoration: underline; ">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></div><div><div style="margin: 0in 0in 0.0001pt; font-size: 12pt; font-family: 'Times New Roman', serif; ">| * 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" style="color: purple; text-decoration: underline; ">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++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 "X &lt;&lt; N" to mean "X * 2^N". Without<br>| a good reason, we shouldn't make them use worse syntax for this, even<br>| for signed numbers. "Applying [bit-shifts] to signed numbers is a bug"<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 "&lt;&lt; N"<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" style="color: purple; text-decoration: underline; ">Jens.Maurer@gmx.net</a>&gt;<br>| wrote:<br>| &gt;<br>| &gt; A preliminary observation: The C++ 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++.<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 "multiplication-by-power-of-two"<br>| &gt; function):<br>| &gt;<span class="Apple-converted-space">&nbsp;</span><a href="http://www.open-std.org/jtc1/sc22/wg21/docs/papers/2014/n4145.html" target="_blank" style="color: purple; text-decoration: underline; ">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>|<span class="Apple-converted-space">&nbsp;</span><a href="https://boringssl.googlesource.com/boringssl/+/e18d821dfcc32532caeeb1a2" target="_blank" style="color: purple; text-decoration: underline; ">https://boringssl.googlesource.com/boringssl/+/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>|<span class="Apple-converted-space">&nbsp;</span><a href="https://github.com/teor2345/tor/blob/ced74e0144e967f838416e6af92cba65" target="_blank" style="color: purple; text-decoration: underline; ">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;<span class="Apple-converted-space">&nbsp;</span><a href="mailto:ub@isocpp.open-std.org" style="color: purple; text-decoration: underline; ">ub@isocpp.open-std.org</a><br>| &gt;<span class="Apple-converted-space">&nbsp;</span><a href="http://www.open-std.org/mailman/listinfo/ub" target="_blank" style="color: purple; text-decoration: underline; ">http://www.open-std.org/mailman/listinfo/ub</a><br>| _______________________________________________<br>| ub mailing list<br>|<span class="Apple-converted-space">&nbsp;</span><a href="mailto:ub@isocpp.open-std.org" style="color: purple; text-decoration: underline; ">ub@isocpp.open-std.org</a><br>|<span class="Apple-converted-space">&nbsp;</span><a href="http://www.open-std.org/mailman/listinfo/ub" target="_blank" style="color: purple; text-decoration: underline; ">http://www.open-std.org/mailman/listinfo/ub</a><br>_______________________________________________<br>ub mailing list<br><a href="mailto:ub@isocpp.open-std.org" style="color: purple; text-decoration: underline; ">ub@isocpp.open-std.org</a><br><a href="http://www.open-std.org/mailman/listinfo/ub" target="_blank" style="color: purple; text-decoration: underline; ">http://www.open-std.org/mailman/listinfo/ub</a><o:p></o:p></div></div></blockquote></div><div style="margin: 0in 0in 0.0001pt; font-size: 12pt; font-family: 'Times New Roman', serif; "><o:p>&nbsp;</o:p></div></div></div></div></div>_______________________________________________<br>ub mailing list<br><a href="mailto:ub@isocpp.open-std.org">ub@isocpp.open-std.org</a><br>http://www.open-std.org/mailman/listinfo/ub</div></blockquote></div><br></body></html>