Topic: [std-proposals] [[assert: disjoint(...)]]: Contract


Author: Phil Miller <phil.miller@intensecomputing.com>
Date: Sat, 6 Oct 2018 12:54:30 -0400
Raw View
--000000000000b5cb3d0577923fca
Content-Type: text/plain; charset="UTF-8"
Content-Transfer-Encoding: quoted-printable

The full text of the proposal I'm preparing to submit is on Google Docs
<https://docs.google.com/document/d/1ZSwJ5k-kvdHCtE6qVMMHbRvT0a32_pAe1ScT1K=
7gbB8/edit?usp=3Dsharing>.
The most critical excerpt is copied below. There's a bunch more data and
discussion there.

I *particularly* want to hear from compiler optimization implementers about
whether the form presented will provide suitable input to alias analysis.
Introduction

There have been many calls over the years for code to have a way to signal
compilers that certain objects do not alias, allowing the compiler to
optimize more aggressively. These include at least


   -

   C99's `restrict` keyword and associated `__restrict__` extensions in C++
   compilers
   -

   the alias sets proposed in N4150
   -

   `may_alias` attributes
   -

   IBM XL's `#pragma disjoint`
   -

   `restrict_ptr` proposals


However, specification of how C=E2=80=99s restrict extends to C++-specific
constructs has seemingly been comparatively fraught with difficulties, and
hence has not proceeded.

With the addition of standard contract syntax and semantics to the C++
language, I believe that similar effects can be achieved with no language
impact and greater clarity for programmers by adding a predicate to the
standard library, tentatively named `disjoint` here. This predicate could
be used in [[expects]] and [[assert]] contracts to convey the desired
information.
Present Proposal

Add a free function defined as follows to namespace std, in an appropriate
header TBD:

template <typename T, typename U>

bool disjoint(const T* pt, size_t nt, const U* pu, size_t nu)

{

 intptr_t bt =3D pt, et =3D pt+nt;

 intptr_t bu =3D pu, eu =3D pu+nu;

 return (et <=3D bu) || (eu <=3D bt);

}

Requirements:

   -

   The pointers pt and pu are valid.
   -

   The expressions pt+nt and pu+nu are valid. (i.e. they point to elements
   within the same array as pt and pu, respectively, or to one past the end=
,
   including the case where pt is a pointer is to a non-array object and nt=
 is
   1 (or pu and nu, respectively))

--=20
You received this message because you are subscribed to the Google Groups "=
ISO C++ Standard - Future Proposals" group.
To unsubscribe from this group and stop receiving emails from it, send an e=
mail to std-proposals+unsubscribe@isocpp.org.
To post to this group, send email to std-proposals@isocpp.org.
To view this discussion on the web visit https://groups.google.com/a/isocpp=
..org/d/msgid/std-proposals/CAMqG0kOM8BwrcNJxrBjAyZ73AMYtbCRrvGVuY2JOZGcW3w3=
7%3DA%40mail.gmail.com.

--000000000000b5cb3d0577923fca
Content-Type: text/html; charset="UTF-8"
Content-Transfer-Encoding: quoted-printable

<div dir=3D"ltr"><div>The full text of the proposal I&#39;m preparing to su=
bmit is <a href=3D"https://docs.google.com/document/d/1ZSwJ5k-kvdHCtE6qVMMH=
bRvT0a32_pAe1ScT1K7gbB8/edit?usp=3Dsharing" target=3D"_blank">on Google Doc=
s</a>. The most critical excerpt is copied below. There&#39;s a bunch more =
data and discussion there.</div><div><br></div><div>I <i>particularly</i>
 want to hear from compiler optimization implementers about whether the=20
form presented will provide suitable input to alias analysis.<br></div><h3 =
dir=3D"ltr" style=3D"line-height:1.38;margin-top:16pt;margin-bottom:4pt" id=
=3D"gmail-m_-3674295672825022268gmail-docs-internal-guid-4db4fd1e-7fff-3068=
-425c-65a65b647674"><span style=3D"font-size:14pt;font-family:Arial;color:r=
gb(67,67,67);background-color:transparent;font-weight:400;font-style:normal=
;font-variant:normal;text-decoration:none;vertical-align:baseline;white-spa=
ce:pre-wrap"></span></h3><h3 dir=3D"ltr" style=3D"line-height:1.38;margin-t=
op:16pt;margin-bottom:4pt" id=3D"gmail-m_-3674295672825022268gmail-docs-int=
ernal-guid-4db4fd1e-7fff-3068-425c-65a65b647674"><span style=3D"font-size:1=
4pt;font-family:Arial;color:rgb(67,67,67);background-color:transparent;font=
-weight:400;font-style:normal;font-variant:normal;text-decoration:none;vert=
ical-align:baseline;white-space:pre-wrap">Introduction</span></h3><p dir=3D=
"ltr" style=3D"line-height:1.38;margin-top:0pt;margin-bottom:0pt"><span sty=
le=3D"font-size:11pt;font-family:Arial;color:rgb(0,0,0);background-color:tr=
ansparent;font-weight:400;font-style:normal;font-variant:normal;text-decora=
tion:none;vertical-align:baseline;white-space:pre-wrap">There have been man=
y calls over the years for code to have a way to signal compilers that cert=
ain objects do not alias, allowing the compiler to optimize more aggressive=
ly. These include at least</span></p><br><ul style=3D"margin-top:0pt;margin=
-bottom:0pt"><li dir=3D"ltr" style=3D"list-style-type:disc;font-size:11pt;f=
ont-family:Arial;color:rgb(0,0,0);background-color:transparent;font-weight:=
400;font-style:normal;font-variant:normal;text-decoration:none;vertical-ali=
gn:baseline;white-space:pre-wrap"><p dir=3D"ltr" style=3D"line-height:1.38;=
margin-top:0pt;margin-bottom:0pt"><span style=3D"font-size:11pt;font-family=
:Arial;color:rgb(0,0,0);background-color:transparent;font-weight:400;font-s=
tyle:normal;font-variant:normal;text-decoration:none;vertical-align:baselin=
e;white-space:pre-wrap">C99&#39;s `</span><span style=3D"font-size:11pt;fon=
t-family:&quot;Courier New&quot;;color:rgb(0,0,0);background-color:transpar=
ent;font-weight:400;font-style:normal;font-variant:normal;text-decoration:n=
one;vertical-align:baseline;white-space:pre-wrap">restrict</span><span styl=
e=3D"font-size:11pt;font-family:Arial;color:rgb(0,0,0);background-color:tra=
nsparent;font-weight:400;font-style:normal;font-variant:normal;text-decorat=
ion:none;vertical-align:baseline;white-space:pre-wrap">` keyword and associ=
ated `</span><span style=3D"font-size:11pt;font-family:&quot;Courier New&qu=
ot;;color:rgb(0,0,0);background-color:transparent;font-weight:400;font-styl=
e:normal;font-variant:normal;text-decoration:none;vertical-align:baseline;w=
hite-space:pre-wrap">__restrict__</span><span style=3D"font-size:11pt;font-=
family:Arial;color:rgb(0,0,0);background-color:transparent;font-weight:400;=
font-style:normal;font-variant:normal;text-decoration:none;vertical-align:b=
aseline;white-space:pre-wrap">` extensions in C++ compilers</span></p></li>=
<li dir=3D"ltr" style=3D"list-style-type:disc;font-size:11pt;font-family:Ar=
ial;color:rgb(0,0,0);background-color:transparent;font-weight:400;font-styl=
e:normal;font-variant:normal;text-decoration:none;vertical-align:baseline;w=
hite-space:pre-wrap"><p dir=3D"ltr" style=3D"line-height:1.38;margin-top:0p=
t;margin-bottom:0pt"><span style=3D"font-size:11pt;font-family:Arial;color:=
rgb(0,0,0);background-color:transparent;font-weight:400;font-style:normal;f=
ont-variant:normal;text-decoration:none;vertical-align:baseline;white-space=
:pre-wrap">the alias sets proposed in N4150</span></p></li><li dir=3D"ltr" =
style=3D"list-style-type:disc;font-size:11pt;font-family:Arial;color:rgb(0,=
0,0);background-color:transparent;font-weight:400;font-style:normal;font-va=
riant:normal;text-decoration:none;vertical-align:baseline;white-space:pre-w=
rap"><p dir=3D"ltr" style=3D"line-height:1.38;margin-top:0pt;margin-bottom:=
0pt"><span style=3D"font-size:11pt;font-family:Arial;color:rgb(0,0,0);backg=
round-color:transparent;font-weight:400;font-style:normal;font-variant:norm=
al;text-decoration:none;vertical-align:baseline;white-space:pre-wrap">`</sp=
an><span style=3D"font-size:11pt;font-family:&quot;Courier New&quot;;color:=
rgb(0,0,0);background-color:transparent;font-weight:400;font-style:normal;f=
ont-variant:normal;text-decoration:none;vertical-align:baseline;white-space=
:pre-wrap">may_alias</span><span style=3D"font-size:11pt;font-family:Arial;=
color:rgb(0,0,0);background-color:transparent;font-weight:400;font-style:no=
rmal;font-variant:normal;text-decoration:none;vertical-align:baseline;white=
-space:pre-wrap">` attributes</span></p></li><li dir=3D"ltr" style=3D"list-=
style-type:disc;font-size:11pt;font-family:Arial;color:rgb(0,0,0);backgroun=
d-color:transparent;font-weight:400;font-style:normal;font-variant:normal;t=
ext-decoration:none;vertical-align:baseline;white-space:pre-wrap"><p dir=3D=
"ltr" style=3D"line-height:1.38;margin-top:0pt;margin-bottom:0pt"><span sty=
le=3D"font-size:11pt;font-family:Arial;color:rgb(0,0,0);background-color:tr=
ansparent;font-weight:400;font-style:normal;font-variant:normal;text-decora=
tion:none;vertical-align:baseline;white-space:pre-wrap">IBM XL&#39;s `</spa=
n><span style=3D"font-size:11pt;font-family:&quot;Courier New&quot;;color:r=
gb(0,0,0);background-color:transparent;font-weight:400;font-style:normal;fo=
nt-variant:normal;text-decoration:none;vertical-align:baseline;white-space:=
pre-wrap">#pragma disjoint</span><span style=3D"font-size:11pt;font-family:=
Arial;color:rgb(0,0,0);background-color:transparent;font-weight:400;font-st=
yle:normal;font-variant:normal;text-decoration:none;vertical-align:baseline=
;white-space:pre-wrap">`</span></p></li><li dir=3D"ltr" style=3D"list-style=
-type:disc;font-size:11pt;font-family:Arial;color:rgb(0,0,0);background-col=
or:transparent;font-weight:400;font-style:normal;font-variant:normal;text-d=
ecoration:none;vertical-align:baseline;white-space:pre-wrap"><p dir=3D"ltr"=
 style=3D"line-height:1.38;margin-top:0pt;margin-bottom:0pt"><span style=3D=
"font-size:11pt;font-family:Arial;color:rgb(0,0,0);background-color:transpa=
rent;font-weight:400;font-style:normal;font-variant:normal;text-decoration:=
none;vertical-align:baseline;white-space:pre-wrap">`</span><span style=3D"f=
ont-size:11pt;font-family:&quot;Courier New&quot;;color:rgb(0,0,0);backgrou=
nd-color:transparent;font-weight:400;font-style:normal;font-variant:normal;=
text-decoration:none;vertical-align:baseline;white-space:pre-wrap">restrict=
_ptr</span><span style=3D"font-size:11pt;font-family:Arial;color:rgb(0,0,0)=
;background-color:transparent;font-weight:400;font-style:normal;font-varian=
t:normal;text-decoration:none;vertical-align:baseline;white-space:pre-wrap"=
>` proposals</span></p></li></ul><br><p dir=3D"ltr" style=3D"line-height:1.=
38;margin-top:0pt;margin-bottom:0pt"><span style=3D"font-size:11pt;font-fam=
ily:Arial;color:rgb(0,0,0);background-color:transparent;font-weight:400;fon=
t-style:normal;font-variant:normal;text-decoration:none;vertical-align:base=
line;white-space:pre-wrap">However, specification of how C=E2=80=99s </span=
><span style=3D"font-size:11pt;font-family:&quot;Courier New&quot;;color:rg=
b(0,0,0);background-color:transparent;font-weight:400;font-style:normal;fon=
t-variant:normal;text-decoration:none;vertical-align:baseline;white-space:p=
re-wrap">restrict</span><span style=3D"font-size:11pt;font-family:Arial;col=
or:rgb(0,0,0);background-color:transparent;font-weight:400;font-style:norma=
l;font-variant:normal;text-decoration:none;vertical-align:baseline;white-sp=
ace:pre-wrap"> extends to C++-specific constructs has seemingly been compar=
atively fraught with difficulties, and hence has not proceeded. </span></p>=
<br><p dir=3D"ltr" style=3D"line-height:1.38;margin-top:0pt;margin-bottom:0=
pt"><span style=3D"font-size:11pt;font-family:Arial;color:rgb(0,0,0);backgr=
ound-color:transparent;font-weight:400;font-style:normal;font-variant:norma=
l;text-decoration:none;vertical-align:baseline;white-space:pre-wrap">With t=
he addition of standard contract syntax and semantics to the C++ language, =
I believe that similar effects can be achieved with no language impact and =
greater clarity for programmers by adding a predicate to the standard libra=
ry, tentatively named `</span><span style=3D"font-size:11pt;font-family:&qu=
ot;Courier New&quot;;color:rgb(0,0,0);background-color:transparent;font-wei=
ght:400;font-style:normal;font-variant:normal;text-decoration:none;vertical=
-align:baseline;white-space:pre-wrap">disjoint</span><span style=3D"font-si=
ze:11pt;font-family:Arial;color:rgb(0,0,0);background-color:transparent;fon=
t-weight:400;font-style:normal;font-variant:normal;text-decoration:none;ver=
tical-align:baseline;white-space:pre-wrap">` here. This predicate could be =
used in </span><span style=3D"font-size:11pt;font-family:&quot;Courier New&=
quot;;color:rgb(0,0,0);background-color:transparent;font-weight:400;font-st=
yle:normal;font-variant:normal;text-decoration:none;vertical-align:baseline=
;white-space:pre-wrap">[[expects]]</span><span style=3D"font-size:11pt;font=
-family:Arial;color:rgb(0,0,0);background-color:transparent;font-weight:400=
;font-style:normal;font-variant:normal;text-decoration:none;vertical-align:=
baseline;white-space:pre-wrap"> and </span><span style=3D"font-size:11pt;fo=
nt-family:&quot;Courier New&quot;;color:rgb(0,0,0);background-color:transpa=
rent;font-weight:400;font-style:normal;font-variant:normal;text-decoration:=
none;vertical-align:baseline;white-space:pre-wrap">[[assert]] </span><span =
style=3D"font-size:11pt;font-family:Arial;color:rgb(0,0,0);background-color=
:transparent;font-weight:400;font-style:normal;font-variant:normal;text-dec=
oration:none;vertical-align:baseline;white-space:pre-wrap">contracts to con=
vey the desired information.</span></p><h3 dir=3D"ltr" style=3D"line-height=
:1.38;margin-top:16pt;margin-bottom:4pt"><span style=3D"font-size:14pt;font=
-family:Arial;color:rgb(67,67,67);background-color:transparent;font-weight:=
400;font-style:normal;font-variant:normal;text-decoration:none;vertical-ali=
gn:baseline;white-space:pre-wrap">Present Proposal</span></h3><p dir=3D"ltr=
" style=3D"line-height:1.38;margin-top:0pt;margin-bottom:0pt"><span style=
=3D"font-size:11pt;font-family:Arial;color:rgb(0,0,0);background-color:tran=
sparent;font-weight:400;font-style:normal;font-variant:normal;text-decorati=
on:none;vertical-align:baseline;white-space:pre-wrap">Add a free function d=
efined as follows to namespace std, in an appropriate header TBD:</span></p=
><br><p dir=3D"ltr" style=3D"line-height:1.38;margin-top:0pt;margin-bottom:=
0pt"><span style=3D"font-size:11pt;font-family:&quot;Courier New&quot;;colo=
r:rgb(0,0,0);background-color:transparent;font-weight:400;font-style:normal=
;font-variant:normal;text-decoration:none;vertical-align:baseline;white-spa=
ce:pre-wrap">template &lt;typename T, typename U&gt;</span></p><p dir=3D"lt=
r" style=3D"line-height:1.38;margin-top:0pt;margin-bottom:0pt"><span style=
=3D"font-size:11pt;font-family:&quot;Courier New&quot;;color:rgb(0,0,0);bac=
kground-color:transparent;font-weight:400;font-style:normal;font-variant:no=
rmal;text-decoration:none;vertical-align:baseline;white-space:pre-wrap">boo=
l disjoint(const T* pt, size_t nt, const U* pu, size_t nu)</span></p><p dir=
=3D"ltr" style=3D"line-height:1.38;margin-top:0pt;margin-bottom:0pt"><span =
style=3D"font-size:11pt;font-family:&quot;Courier New&quot;;color:rgb(0,0,0=
);background-color:transparent;font-weight:400;font-style:normal;font-varia=
nt:normal;text-decoration:none;vertical-align:baseline;white-space:pre-wrap=
">{</span></p><p dir=3D"ltr" style=3D"line-height:1.38;margin-top:0pt;margi=
n-bottom:0pt"><span style=3D"font-size:11pt;font-family:&quot;Courier New&q=
uot;;color:rgb(0,0,0);background-color:transparent;font-weight:400;font-sty=
le:normal;font-variant:normal;text-decoration:none;vertical-align:baseline;=
white-space:pre-wrap"> =C2=A0intptr_t bt =3D pt, et =3D pt+nt;</span></p><p=
 dir=3D"ltr" style=3D"line-height:1.38;margin-top:0pt;margin-bottom:0pt"><s=
pan style=3D"font-size:11pt;font-family:&quot;Courier New&quot;;color:rgb(0=
,0,0);background-color:transparent;font-weight:400;font-style:normal;font-v=
ariant:normal;text-decoration:none;vertical-align:baseline;white-space:pre-=
wrap"> =C2=A0intptr_t bu =3D pu, eu =3D pu+nu;</span></p><br><p dir=3D"ltr"=
 style=3D"line-height:1.38;margin-top:0pt;margin-bottom:0pt"><span style=3D=
"font-size:11pt;font-family:&quot;Courier New&quot;;color:rgb(0,0,0);backgr=
ound-color:transparent;font-weight:400;font-style:normal;font-variant:norma=
l;text-decoration:none;vertical-align:baseline;white-space:pre-wrap"> =C2=
=A0return (et &lt;=3D bu) || (eu &lt;=3D bt);</span></p><p dir=3D"ltr" styl=
e=3D"line-height:1.38;margin-top:0pt;margin-bottom:0pt"><span style=3D"font=
-size:11pt;font-family:&quot;Courier New&quot;;color:rgb(0,0,0);background-=
color:transparent;font-weight:400;font-style:normal;font-variant:normal;tex=
t-decoration:none;vertical-align:baseline;white-space:pre-wrap">}</span></p=
><br><p dir=3D"ltr" style=3D"line-height:1.38;margin-top:0pt;margin-bottom:=
0pt"><span style=3D"font-size:11pt;font-family:Arial;color:rgb(0,0,0);backg=
round-color:transparent;font-weight:400;font-style:normal;font-variant:norm=
al;text-decoration:none;vertical-align:baseline;white-space:pre-wrap">Requi=
rements:</span></p><ul style=3D"margin-top:0pt;margin-bottom:0pt"><li dir=
=3D"ltr" style=3D"list-style-type:disc;font-size:11pt;font-family:Arial;col=
or:rgb(0,0,0);background-color:transparent;font-weight:400;font-style:norma=
l;font-variant:normal;text-decoration:none;vertical-align:baseline;white-sp=
ace:pre-wrap"><p dir=3D"ltr" style=3D"line-height:1.38;margin-top:0pt;margi=
n-bottom:0pt"><span style=3D"font-size:11pt;font-family:Arial;color:rgb(0,0=
,0);background-color:transparent;font-weight:400;font-style:normal;font-var=
iant:normal;text-decoration:none;vertical-align:baseline;white-space:pre-wr=
ap">The pointers pt and pu are valid.</span></p></li><li dir=3D"ltr" style=
=3D"list-style-type:disc;font-size:11pt;font-family:Arial;color:rgb(0,0,0);=
background-color:transparent;font-weight:400;font-style:normal;font-variant=
:normal;text-decoration:none;vertical-align:baseline;white-space:pre-wrap">=
<p dir=3D"ltr" style=3D"line-height:1.38;margin-top:0pt;margin-bottom:0pt">=
<span style=3D"font-size:11pt;font-family:Arial;color:rgb(0,0,0);background=
-color:transparent;font-weight:400;font-style:normal;font-variant:normal;te=
xt-decoration:none;vertical-align:baseline;white-space:pre-wrap">The expres=
sions pt+nt and pu+nu are valid. (i.e. they point to elements within the sa=
me array as pt and pu, respectively, or to one past the end, including the =
case where pt is a pointer is to a non-array object and nt is 1 (or pu and =
nu, respectively))</span></p></li></ul><br></div>

<p></p>

-- <br />
You received this message because you are subscribed to the Google Groups &=
quot;ISO C++ Standard - Future Proposals&quot; group.<br />
To unsubscribe from this group and stop receiving emails from it, send an e=
mail to <a href=3D"mailto:std-proposals+unsubscribe@isocpp.org">std-proposa=
ls+unsubscribe@isocpp.org</a>.<br />
To post to this group, send email to <a href=3D"mailto:std-proposals@isocpp=
..org">std-proposals@isocpp.org</a>.<br />
To view this discussion on the web visit <a href=3D"https://groups.google.c=
om/a/isocpp.org/d/msgid/std-proposals/CAMqG0kOM8BwrcNJxrBjAyZ73AMYtbCRrvGVu=
Y2JOZGcW3w37%3DA%40mail.gmail.com?utm_medium=3Demail&utm_source=3Dfooter">h=
ttps://groups.google.com/a/isocpp.org/d/msgid/std-proposals/CAMqG0kOM8BwrcN=
JxrBjAyZ73AMYtbCRrvGVuY2JOZGcW3w37%3DA%40mail.gmail.com</a>.<br />

--000000000000b5cb3d0577923fca--

.