Rocksolid Light

Welcome to RetroBBS

mail  files  register  newsreader  groups  login

Message-ID:  

That's one small step for a man; one giant leap for mankind. -- Neil Armstrong


devel / comp.lang.c++ / Re: Strange optimization

SubjectAuthor
* Strange optimizationBonita Montero
`* Re: Strange optimizationBo Persson
 +* Re: Strange optimizationAlf P. Steinbach
 |`* Re: Strange optimizationBonita Montero
 | `* Re: Strange optimizationAlf P. Steinbach
 |  +- Re: Strange optimizationAlf P. Steinbach
 |  `* Re: Strange optimizationBonita Montero
 |   +- Re: Strange optimizationDavid Brown
 |   `* Re: Strange optimizationAlf P. Steinbach
 |    +- Re: Strange optimizationBonita Montero
 |    +* Re: Strange optimizationJames Kuyper
 |    |+* Re: Strange optimizationChris M. Thomasson
 |    ||`* Re: Strange optimizationChris M. Thomasson
 |    || `- Re: Strange optimizationChris M. Thomasson
 |    |+* Re: Strange optimizationDavid Brown
 |    ||`- Re: Strange optimizationV
 |    |`* Re: Strange optimizationAlf P. Steinbach
 |    | `* Re: Strange optimizationjames...@alumni.caltech.edu
 |    |  +* Re: Strange optimizationAlf P. Steinbach
 |    |  |+* Re: Strange optimizationDavid Brown
 |    |  ||`* Re: Strange optimizationAlf P. Steinbach
 |    |  || `* Re: Strange optimizationDavid Brown
 |    |  ||  `* Re: Strange optimizationAlf P. Steinbach
 |    |  ||   `- Re: Strange optimizationDavid Brown
 |    |  |`* Re: Strange optimizationjames...@alumni.caltech.edu
 |    |  | `* Re: Strange optimizationAlf P. Steinbach
 |    |  |  `* Re: Strange optimizationJames Kuyper
 |    |  |   +- Re: Strange optimizationBonita Montero
 |    |  |   `* Re: Strange optimizationAlf P. Steinbach
 |    |  |    +* Re: Strange optimizationKeith Thompson
 |    |  |    |`* Re: Strange optimizationAlf P. Steinbach
 |    |  |    | `- Re: Strange optimizationDavid Brown
 |    |  |    `- Re: Strange optimizationJames Kuyper
 |    |  `- Re: Strange optimizationBonita Montero
 |    `* Re: Strange optimizationDavid Brown
 |     `* Re: Strange optimizationAlf P. Steinbach
 |      +* Re: Strange optimizationDavid Brown
 |      |`* Re: Strange optimizationAlf P. Steinbach
 |      | +* Re: Strange optimizationDavid Brown
 |      | |+* Re: Strange optimizationBen Bacarisse
 |      | ||`- Re: Strange optimizationDavid Brown
 |      | |`* Re: Strange optimizationAlf P. Steinbach
 |      | | `* Re: Strange optimizationDavid Brown
 |      | |  `* Re: Strange optimizationAlf P. Steinbach
 |      | |   `- Re: Strange optimizationDavid Brown
 |      | `* Re: Strange optimizationKeith Thompson
 |      |  `- Re: Strange optimizationDavid Brown
 |      `- Re: Strange optimizationBonita Montero
 +* Re: Strange optimizationBonita Montero
 |`* Re: Strange optimizationDavid Brown
 | `* Re: Strange optimizationBonita Montero
 |  `* Re: Strange optimizationDavid Brown
 |   +* Re: Strange optimizationBonita Montero
 |   |`* Re: Strange optimizationDavid Brown
 |   | `* Re: Strange optimizationBonita Montero
 |   |  `* Re: Strange optimizationDavid Brown
 |   |   `* Re: Strange optimizationBonita Montero
 |   |    `* Re: Strange optimizationDavid Brown
 |   |     `- Re: Strange optimizationBonita Montero
 |   `* Re: Strange optimizationScott Lurndal
 |    +* Re: Strange optimizationBonita Montero
 |    |+- Re: Strange optimizationScott Lurndal
 |    |`* Re: Strange optimizationjames...@alumni.caltech.edu
 |    | `* Re: Strange optimizationBonita Montero
 |    |  `- Re: Strange optimizationjames...@alumni.caltech.edu
 |    `* Re: Strange optimizationDavid Brown
 |     `- Re: Strange optimizationScott Lurndal
 `* Re: Strange optimizationVir Campestris
  `- Re: Strange optimizationKeith Thompson

Pages:123
Re: Strange optimization

<u6ksqn$1aluq$1@dont-email.me>

  copy mid

https://www.rocksolidbbs.com/devel/article-flat.php?id=446&group=comp.lang.c%2B%2B#446

  copy link   Newsgroups: comp.lang.c++
Path: i2pn2.org!i2pn.org!eternal-september.org!news.eternal-september.org!.POSTED!not-for-mail
From: Bonita.Montero@gmail.com (Bonita Montero)
Newsgroups: comp.lang.c++
Subject: Re: Strange optimization
Date: Sat, 17 Jun 2023 20:08:25 +0200
Organization: A noiseless patient Spider
Lines: 9
Message-ID: <u6ksqn$1aluq$1@dont-email.me>
References: <u6acl4$3m5sb$1@dont-email.me> <kerr9cF31i7U1@mid.individual.net>
<u6bb5e$3tf93$1@dont-email.me> <u6be9i$3tpfh$1@dont-email.me>
<u6bkir$3ueat$1@dont-email.me> <u6bkqb$3ufs0$1@dont-email.me>
<u6d5dr$3rml$1@dont-email.me> <u6desf$4oa4$1@dont-email.me>
<u6h0j5$olvu$1@dont-email.me>
<a435a6d6-d86e-4a35-ac22-08f72c0fe369n@googlegroups.com>
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8; format=flowed
Content-Transfer-Encoding: 7bit
Injection-Date: Sat, 17 Jun 2023 18:08:23 -0000 (UTC)
Injection-Info: dont-email.me; posting-host="70ae5adec0be00e54b241ae7ebc9e0a2";
logging-data="1398746"; mail-complaints-to="abuse@eternal-september.org"; posting-account="U2FsdGVkX1+YZ6YRroWZbX8f5ut++3l1yJFcpYcQ5z8="
User-Agent: Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:102.0) Gecko/20100101
Thunderbird/102.12.0
Cancel-Lock: sha1:yzd6Aoy9YZX/G8cvaRYQt9b6hNw=
Content-Language: de-DE
In-Reply-To: <a435a6d6-d86e-4a35-ac22-08f72c0fe369n@googlegroups.com>
 by: Bonita Montero - Sat, 17 Jun 2023 18:08 UTC

Am 16.06.2023 um 23:16 schrieb james...@alumni.caltech.edu:

> ... The memcpy() would only be reasonable if it were possible
> for "data' to be a misaligned pointer; otherwise simple assignment
> would be simpler and equally safe.

I'm aliasing a char-array.

Re: Strange optimization

<u6kuh6$1ass5$1@dont-email.me>

  copy mid

https://www.rocksolidbbs.com/devel/article-flat.php?id=448&group=comp.lang.c%2B%2B#448

  copy link   Newsgroups: comp.lang.c++
Path: i2pn2.org!i2pn.org!eternal-september.org!news.eternal-september.org!.POSTED!not-for-mail
From: alf.p.steinbach@gmail.com (Alf P. Steinbach)
Newsgroups: comp.lang.c++
Subject: Re: Strange optimization
Date: Sat, 17 Jun 2023 20:37:25 +0200
Organization: A noiseless patient Spider
Lines: 144
Message-ID: <u6kuh6$1ass5$1@dont-email.me>
References: <u6acl4$3m5sb$1@dont-email.me> <kerr9cF31i7U1@mid.individual.net>
<u6bb5e$3tf93$1@dont-email.me> <u6be9i$3tpfh$1@dont-email.me>
<u6bkir$3ueat$1@dont-email.me> <u6bkqb$3ufs0$1@dont-email.me>
<u6d5dr$3rml$1@dont-email.me> <u6desf$4oa4$1@dont-email.me>
<u6h0j5$olvu$1@dont-email.me>
<a435a6d6-d86e-4a35-ac22-08f72c0fe369n@googlegroups.com>
<u6in1p$uvth$1@dont-email.me> <u6ks5p$1ajn0$1@dont-email.me>
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8; format=flowed
Content-Transfer-Encoding: 8bit
Injection-Date: Sat, 17 Jun 2023 18:37:26 -0000 (UTC)
Injection-Info: dont-email.me; posting-host="e7bb0977682eeae68857305e0469c284";
logging-data="1405829"; mail-complaints-to="abuse@eternal-september.org"; posting-account="U2FsdGVkX18gmyJM//NkK2QwC1FE2d5b"
User-Agent: Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:102.0) Gecko/20100101
Thunderbird/102.12.0
Cancel-Lock: sha1:7O6ytHMqeIgompty7OxX04laVx0=
Content-Language: en-US
In-Reply-To: <u6ks5p$1ajn0$1@dont-email.me>
 by: Alf P. Steinbach - Sat, 17 Jun 2023 18:37 UTC

On 2023-06-17 7:57 PM, David Brown wrote:
> On 17/06/2023 00:17, Alf P. Steinbach wrote:
>> On 2023-06-16 11:16 PM, james...@alumni.caltech.edu wrote:
>>> On Friday, June 16, 2023 at 2:48:24 AM UTC-4, Alf P. Steinbach wrote:
>>>> On 2023-06-15 12:27 AM, James Kuyper wrote:
>>>>> On 6/14/23 15:46, Alf P. Steinbach wrote:
>>> ...
>>>>> All you need is a platform where misaligned pointers do not merely
>>>>> cause
>>>>> the code to be inefficient, but to actually malfunction. On such a
>>>>> platform, if p_bytes is not correctly aligned to store a uint64_t,
>>>>> then
>>>>> the code will malfunction in the reinterpret_cast<>.
>>>> Yes, but irrelevant for the case discussed, because the values are
>>>> guaranteed correctly aligned.
>>>
>>> You said nothing about what p_bytes was when you asked
>>>
>>> "how is the compiler to know that it's generally called with a
>>> `*reinterpret_cast<uint64_t*>( p_bytes )` as argument?"
>>>
>>> In particular, you said nothing about p_bytes that would guarantee
>>> that it was
>>> correctly aligned.
>>
>> That was known from the optimization attempt in the original posting's
>> code (I'm not the OP):
>>
>>      #if defined(__GNUC__) || defined(__llvm__)
>>          if( (size_t)&data % 8 )
>>              __builtin_unreachable();
>>      #endif
>>
>>
>>> The memcpy() would only be reasonable if it were possible
>>> for "data' to be a misaligned pointer; otherwise simple assignment
>>> would be
>>> simpler and equally safe.
>>
>> On that we agree. :)
>
> If we knew that the original pointer pointed to a uint64_t, then we
> could /all/ agree that a plain assignment would be simpler, clearer, and
> as efficient as possible.
>
> But as far as I know, no such guarantee exists.  My guess, from how
> functions like this are sometimes used, is that the original data is in
> an array of unsigned char - perhaps a buffer for a received network
> packet or a file that has been read.
>
> And if the data does not start as a uint64_t (or compatible type), then
> reading it through a uint64_t glvalue is /not/ safe, even if alignment
> is guaranteed.
>
>>
>>
>>>> [snip]
>>>>> If p_bytes is correctly aligned, simple assignment will work just as
>>>>> well as memcpy().
>>>> Yes.
>>>>>> Even g++'s documentation of `-fstrict-aliasing` says "A character
>>>>>> type
>>>>>> may alias any other type.". ...
>>>>>
>>>>> True, but that's not what this reinterpret_cast does;
>>>> It is what this `reinterpret_cast` does.
>>>
>>> This reinterpret_cast converts p_bytes, which presumably points to the
>>> first element of an array of character type, into a pointer to
>>> uint64_t*. In
>>> other words, an array of char is being aliased as a uint64_t. Are you
>>> claiming that uint64_t is a character type?
>>
>> It may be that my English isn't good enough to understand a one-way
>> nature of the GCC docs' wording.
>>
>
> The "cppreference" site is often clearer than the standards language:
>
> <https://en.cppreference.com/w/cpp/language/reinterpret_cast#Type_aliasing>
>
> The key point is that a reinterpret_cast (or equivalent via a C-style
> cast) does not let you access incompatible types.  It does not, in gcc
> parlance, side-step the strict aliasing rules.
>
> Like a C cast, reinterpret_cast is a way to tell the compiler that you
> think it is safe to change the type of an object (usually a pointer).
> But it does not /make/ it safe.  And if the compiler can see that the
> actual object type is not compatible with the way you are accessing it,
> then you have a conflict - you are lying to the compiler, and no good
> will come of it.  A reinterpret_cast will not change that situation.
> (And separate compilation will only hide the problem.)
>
>
>> Perhaps.
>>
>> The way I think, aliasing is of interest to the compiler because where
>> it can assume that a T value is never changed via a U* pointer, and
>> that the value that the U* points to is never changed by a change to
>> the T value, it can optimize, e.g. not reload that value from memory
>> when it's already in a register. If T = float and U* = int*, then it
>> can make this assumption. Similarly if T = int and U* = float*, it can
>> assume this.
>>
>
> Yes.
>
>> But if T = char-type, such as the first char in an array, and U* is a
>> double*, then it can not reasonably make this assumption, and as I
>> read the docs quote g++ doesn't make this assumption for T = char-type.
>>
>
> No.  You can use a char-type pointer to access a non-char object, but
> you cannot use a non-char pointer to access a char (array) object.

Assuming no padding bits, which for clarity is not checked (AFAIK there
is no extant compiler that introduces padding bits):

#include <new>
#include <assert.h>
#include <stdio.h>

auto main() -> int
{
alignas( int ) char chars[sizeof( int )] = {};
int* const p = std::launder( new( (void*) chars ) int );
assert( *p == 0 );
chars[0] = 42;
printf( "This system is %s-endian.\n", (*p == 42)? "little" :
"big" );
}

One possible way to reconcile that working example with your assertion
"cannot" is that in your view the `*p` expressions do not access the
character array but the `double` object that resides there.

If that is the case then the assertion is meaningless nonsense,
consistent with your sequence of self-contradictions in this thread.

[snip]

- Alf

Re: Strange optimization

<u6lc5b$1co4a$1@dont-email.me>

  copy mid

https://www.rocksolidbbs.com/devel/article-flat.php?id=450&group=comp.lang.c%2B%2B#450

  copy link   Newsgroups: comp.lang.c++
Path: i2pn2.org!i2pn.org!eternal-september.org!news.eternal-september.org!.POSTED!not-for-mail
From: david.brown@hesbynett.no (David Brown)
Newsgroups: comp.lang.c++
Subject: Re: Strange optimization
Date: Sun, 18 Jun 2023 00:30:03 +0200
Organization: A noiseless patient Spider
Lines: 43
Message-ID: <u6lc5b$1co4a$1@dont-email.me>
References: <u6acl4$3m5sb$1@dont-email.me> <kerr9cF31i7U1@mid.individual.net>
<u6bb5e$3tf93$1@dont-email.me> <u6be9i$3tpfh$1@dont-email.me>
<u6bkir$3ueat$1@dont-email.me> <u6bkqb$3ufs0$1@dont-email.me>
<u6d5dr$3rml$1@dont-email.me> <u6desf$4oa4$1@dont-email.me>
<u6h0j5$olvu$1@dont-email.me>
<a435a6d6-d86e-4a35-ac22-08f72c0fe369n@googlegroups.com>
<u6in1p$uvth$1@dont-email.me> <u6ks5p$1ajn0$1@dont-email.me>
<u6kuh6$1ass5$1@dont-email.me>
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8; format=flowed
Content-Transfer-Encoding: 8bit
Injection-Date: Sat, 17 Jun 2023 22:30:03 -0000 (UTC)
Injection-Info: dont-email.me; posting-host="09ec3990a67b04adf909dac980fc873a";
logging-data="1466506"; mail-complaints-to="abuse@eternal-september.org"; posting-account="U2FsdGVkX1+gjc+fcsOTkAIEKlpItcUuRhaV4NtUICc="
User-Agent: Mozilla/5.0 (X11; Linux x86_64; rv:102.0) Gecko/20100101
Thunderbird/102.7.1
Cancel-Lock: sha1:w8hvJRwlUjIdkeOANdlEcYYR5Po=
In-Reply-To: <u6kuh6$1ass5$1@dont-email.me>
Content-Language: en-GB
 by: David Brown - Sat, 17 Jun 2023 22:30 UTC

On 17/06/2023 20:37, Alf P. Steinbach wrote:
> On 2023-06-17 7:57 PM, David Brown wrote:

>> No.  You can use a char-type pointer to access a non-char object, but
>> you cannot use a non-char pointer to access a char (array) object.
>
> Assuming no padding bits, which for clarity is not checked (AFAIK there
> is no extant compiler that introduces padding bits):
>
>     #include <new>
>     #include <assert.h>
>     #include <stdio.h>
>
>     auto main() -> int
>     {
>         alignas( int ) char chars[sizeof( int )] = {};
>         int* const p = std::launder( new( (void*) chars ) int );
>         assert( *p == 0 );
>         chars[0] = 42;
>         printf( "This system is %s-endian.\n", (*p == 42)? "little" :
> "big" );
>     }
>
> One possible way to reconcile that working example with your assertion
> "cannot" is that in your view the `*p` expressions do not access the
> character array but the `double` object that resides there.

It is an int, in your example, not a double, but otherwise that is
/exactly/ what happens. *p is accessing the int object, not a char array.

Then "chars[0] = 42;" is using a character pointer to access the memory
used for storing an int.

<https://en.cppreference.com/w/cpp/utility/launder>

>
> If that is the case then the assertion is meaningless nonsense,
> consistent with your sequence of self-contradictions in this thread.
>

I haven't contradicted myself - I have only contradicted you.

Re: Strange optimization

<u6lch4$1co4a$2@dont-email.me>

  copy mid

https://www.rocksolidbbs.com/devel/article-flat.php?id=451&group=comp.lang.c%2B%2B#451

  copy link   Newsgroups: comp.lang.c++
Path: i2pn2.org!i2pn.org!eternal-september.org!news.eternal-september.org!.POSTED!not-for-mail
From: david.brown@hesbynett.no (David Brown)
Newsgroups: comp.lang.c++
Subject: Re: Strange optimization
Date: Sun, 18 Jun 2023 00:36:20 +0200
Organization: A noiseless patient Spider
Lines: 54
Message-ID: <u6lch4$1co4a$2@dont-email.me>
References: <u6acl4$3m5sb$1@dont-email.me> <kerr9cF31i7U1@mid.individual.net>
<u6bb5e$3tf93$1@dont-email.me> <u6be9i$3tpfh$1@dont-email.me>
<u6bkir$3ueat$1@dont-email.me> <u6bkqb$3ufs0$1@dont-email.me>
<u6d5dr$3rml$1@dont-email.me> <u6egpk$bn1q$2@dont-email.me>
<u6h18t$op4b$1@dont-email.me> <u6h82j$phr2$1@dont-email.me>
<u6hdrh$q807$1@dont-email.me> <u6hm4v$r4fl$1@dont-email.me>
<u6hutm$rvk8$1@dont-email.me> <u6i2rp$sgu3$1@dont-email.me>
<u6i61g$srpv$1@dont-email.me>
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8; format=flowed
Content-Transfer-Encoding: 8bit
Injection-Date: Sat, 17 Jun 2023 22:36:21 -0000 (UTC)
Injection-Info: dont-email.me; posting-host="09ec3990a67b04adf909dac980fc873a";
logging-data="1466506"; mail-complaints-to="abuse@eternal-september.org"; posting-account="U2FsdGVkX1/f0qIOQCuZ8YDH/zhMzH7oURv27CrfTl4="
User-Agent: Mozilla/5.0 (X11; Linux x86_64; rv:102.0) Gecko/20100101
Thunderbird/102.7.1
Cancel-Lock: sha1:FpYtQrb802t7YrjokCM+masKPds=
In-Reply-To: <u6i61g$srpv$1@dont-email.me>
Content-Language: en-GB
 by: David Brown - Sat, 17 Jun 2023 22:36 UTC

On 16/06/2023 19:27, Alf P. Steinbach wrote:
> On 2023-06-16 6:32 PM, David Brown wrote:
> [snip]
>>>>
>>>> A "uint64_t" has a guaranteed fully-defined format and no padding bits.
> [snip]
>> The C++ standards change their numbering regularly.  These numbers are
>> from the C++20 draft N4860 - I don't believe the contents change much
>> between versions.
>>
>> 6.8.1p3 .. p5 describes the representation of unsigned types as
>> collections of bits representing powers of 2.  For unsigned integer
>> types in general, there may be padding bits, but there are no padding
>> bits in char, signed char, unsigned char and char8_t.
>
> [snip]
>
>> The ball is in your court.
>
> In the above you contradict yourself:

Your English is better than most native speakers - but you have to /try/
to read and understand posts. It is your comprehension that is at
fault, and I suspect also your assumptions about C and C++. (To be
fair, many of your assumptions are true in all realistic implementations
in the modern world.)

>
> * First you claim that there are no padding bits for `uint64_t`.

Correct. That's what the C standard says, and that's what the C++
standard says by delegation to the C standard.

> * Then you paraphrase the standard that "there may be padding bits".

You asked for references - I gave them. Look them up. You will see
that in general, unsigned integer types (other than unsigned char) may
have padding bits. Only specific types such as the size-specific types
in <stdint.h> are guaranteed not to have padding bits - /if/ they exist.
An implementation might not support these non-padded types at all. It
might support them as extended integer types, and have padding bits in
the standard integer types. It might have lots of unsigned types - some
with padding, some without.

But uint64_t and the other size-specific unsigned types are guaranteed
not to have padding.

>
> I elected now to not quote or respond to the rest which likewise
> involved some self-contradictions.
>

Re: Strange optimization

<u6lej6$1d1rr$1@dont-email.me>

  copy mid

https://www.rocksolidbbs.com/devel/article-flat.php?id=453&group=comp.lang.c%2B%2B#453

  copy link   Newsgroups: comp.lang.c++
Path: i2pn2.org!i2pn.org!eternal-september.org!news.eternal-september.org!.POSTED!not-for-mail
From: alf.p.steinbach@gmail.com (Alf P. Steinbach)
Newsgroups: comp.lang.c++
Subject: Re: Strange optimization
Date: Sun, 18 Jun 2023 01:11:33 +0200
Organization: A noiseless patient Spider
Lines: 49
Message-ID: <u6lej6$1d1rr$1@dont-email.me>
References: <u6acl4$3m5sb$1@dont-email.me> <kerr9cF31i7U1@mid.individual.net>
<u6bb5e$3tf93$1@dont-email.me> <u6be9i$3tpfh$1@dont-email.me>
<u6bkir$3ueat$1@dont-email.me> <u6bkqb$3ufs0$1@dont-email.me>
<u6d5dr$3rml$1@dont-email.me> <u6desf$4oa4$1@dont-email.me>
<u6h0j5$olvu$1@dont-email.me>
<a435a6d6-d86e-4a35-ac22-08f72c0fe369n@googlegroups.com>
<u6in1p$uvth$1@dont-email.me> <u6ks5p$1ajn0$1@dont-email.me>
<u6kuh6$1ass5$1@dont-email.me> <u6lc5b$1co4a$1@dont-email.me>
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8; format=flowed
Content-Transfer-Encoding: 8bit
Injection-Date: Sat, 17 Jun 2023 23:11:34 -0000 (UTC)
Injection-Info: dont-email.me; posting-host="354958b8aace14010e238235edd52678";
logging-data="1476475"; mail-complaints-to="abuse@eternal-september.org"; posting-account="U2FsdGVkX19V4TgAaKs5ePuVeXTnr2as"
User-Agent: Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:102.0) Gecko/20100101
Thunderbird/102.12.0
Cancel-Lock: sha1:UGfoUrHhH1vjStV8HnPN+uiRK50=
Content-Language: en-US
In-Reply-To: <u6lc5b$1co4a$1@dont-email.me>
 by: Alf P. Steinbach - Sat, 17 Jun 2023 23:11 UTC

On 2023-06-18 12:30 AM, David Brown wrote:
> On 17/06/2023 20:37, Alf P. Steinbach wrote:
>> On 2023-06-17 7:57 PM, David Brown wrote:
>
>>> No.  You can use a char-type pointer to access a non-char object, but
>>> you cannot use a non-char pointer to access a char (array) object.
>>
>> Assuming no padding bits, which for clarity is not checked (AFAIK
>> there is no extant compiler that introduces padding bits):
>>
>>      #include <new>
>>      #include <assert.h>
>>      #include <stdio.h>
>>
>>      auto main() -> int
>>      {
>>          alignas( int ) char chars[sizeof( int )] = {};
>>          int* const p = std::launder( new( (void*) chars ) int );
>>          assert( *p == 0 );
>>          chars[0] = 42;
>>          printf( "This system is %s-endian.\n", (*p == 42)? "little" :
>> "big" );
>>      }
>>
>> One possible way to reconcile that working example with your assertion
>> "cannot" is that in your view the `*p` expressions do not access the
>> character array but the `double` object that resides there.
>
> It is an int, in your example, not a double, but otherwise that is
> /exactly/ what happens.  *p is accessing the int object, not a char array.
>
> Then "chars[0] = 42;" is using a character pointer to access the memory
> used for storing an int.
>
> <https://en.cppreference.com/w/cpp/utility/launder>
>
>>
>> If that is the case then the assertion is meaningless nonsense,
>> consistent with your sequence of self-contradictions in this thread.
>>
>
> I haven't contradicted myself - I have only contradicted you.

Well I give up, as usual when I encounter strong religious beliefs with
argumentation consisting of self-contradictions, denials and advice.

- Alf

Re: Strange optimization

<ff2fe584-0af8-4bd8-997c-1359bac1c0d5n@googlegroups.com>

  copy mid

https://www.rocksolidbbs.com/devel/article-flat.php?id=456&group=comp.lang.c%2B%2B#456

  copy link   Newsgroups: comp.lang.c++
X-Received: by 2002:a05:622a:151:b0:3f6:aa29:b631 with SMTP id v17-20020a05622a015100b003f6aa29b631mr2550493qtw.2.1687065042592;
Sat, 17 Jun 2023 22:10:42 -0700 (PDT)
X-Received: by 2002:a05:6214:4c06:b0:62d:f6f6:4172 with SMTP id
qh6-20020a0562144c0600b0062df6f64172mr907547qvb.4.1687065042347; Sat, 17 Jun
2023 22:10:42 -0700 (PDT)
Path: i2pn2.org!i2pn.org!weretis.net!feeder6.news.weretis.net!news.misty.com!border-2.nntp.ord.giganews.com!nntp.giganews.com!news-out.google.com!nntp.google.com!postnews.google.com!google-groups.googlegroups.com!not-for-mail
Newsgroups: comp.lang.c++
Date: Sat, 17 Jun 2023 22:10:42 -0700 (PDT)
In-Reply-To: <u6in1p$uvth$1@dont-email.me>
Injection-Info: google-groups.googlegroups.com; posting-host=108.45.179.220; posting-account=Ix1u_AoAAAAILVQeRkP2ENwli-Uv6vO8
NNTP-Posting-Host: 108.45.179.220
References: <u6acl4$3m5sb$1@dont-email.me> <kerr9cF31i7U1@mid.individual.net>
<u6bb5e$3tf93$1@dont-email.me> <u6be9i$3tpfh$1@dont-email.me>
<u6bkir$3ueat$1@dont-email.me> <u6bkqb$3ufs0$1@dont-email.me>
<u6d5dr$3rml$1@dont-email.me> <u6desf$4oa4$1@dont-email.me>
<u6h0j5$olvu$1@dont-email.me> <a435a6d6-d86e-4a35-ac22-08f72c0fe369n@googlegroups.com>
<u6in1p$uvth$1@dont-email.me>
User-Agent: G2/1.0
MIME-Version: 1.0
Message-ID: <ff2fe584-0af8-4bd8-997c-1359bac1c0d5n@googlegroups.com>
Subject: Re: Strange optimization
From: jameskuyper@alumni.caltech.edu (james...@alumni.caltech.edu)
Injection-Date: Sun, 18 Jun 2023 05:10:42 +0000
Content-Type: text/plain; charset="UTF-8"
Content-Transfer-Encoding: quoted-printable
Lines: 95
 by: james...@alumni.calt - Sun, 18 Jun 2023 05:10 UTC

On 6/16/23 18:17, Alf P. Steinbach wrote:
> On 2023-06-16 11:16 PM, james...@alumni.caltech.edu wrote:
>> On Friday, June 16, 2023 at 2:48:24 AM UTC-4, Alf P. Steinbach wrote:
>>> On 2023-06-15 12:27 AM, James Kuyper wrote:
>>>> On 6/14/23 15:46, Alf P. Steinbach wrote:
>> ...
>>>> All you need is a platform where misaligned pointers do not merely
>>>> cause
>>>> the code to be inefficient, but to actually malfunction. On such a
>>>> platform, if p_bytes is not correctly aligned to store a uint64_t, then
>>>> the code will malfunction in the reinterpret_cast<>.
>>> Yes, but irrelevant for the case discussed, because the values are
>>> guaranteed correctly aligned.
>>
>> You said nothing about what p_bytes was when you asked
>>
>> "how is the compiler to know that it's generally called with a
>> `*reinterpret_cast<uint64_t*>( p_bytes )` as argument?"
>>
>> In particular, you said nothing about p_bytes that would guarantee
>> that it was
>> correctly aligned.
>
> That was known from the optimization attempt in the original posting's
> code (I'm not the OP):
>
> #if defined(__GNUC__) || defined(__llvm__)
> if( (size_t)&data % 8 )
> __builtin_unreachable();
> #endif

How is that supposed to help? On platforms where misaligned access
doesn't just cause inefficiency, but actually is a serious problem, the
unspecified result of the reinterpret_cast<> of a mis-aligned pointer
can be, and often is, a correctly aligned pointer which, therefore,
necessarily points at a different location in memory. It is quite
frequently one of the two nearest correctly aligned pointers. The result
of such a conversion would pass that test, and as a result it would
memcpy() from the wrong location.

>>> [snip]
>>>> If p_bytes is correctly aligned, simple assignment will work just as
>>>> well as memcpy().
>>> Yes.
>>>>> Even g++'s documentation of `-fstrict-aliasing` says "A character type
>>>>> may alias any other type.". ...
>>>>
>>>> True, but that's not what this reinterpret_cast does;
>>> It is what this `reinterpret_cast` does.
>>
>> This reinterpret_cast converts p_bytes, which presumably points to the
>> first element of an array of character type, into a pointer to
>> uint64_t*. In
>> other words, an array of char is being aliased as a uint64_t. Are you
>> claiming that uint64_t is a character type?
>
> It may be that my English isn't good enough to understand a one-way
> nature of the GCC docs' wording.

I was responding to a statement about both the C and the C++ standards.
The GCC docs merely reword the relevant clauses of the standards.
Insofar as gcc claims standard conformance, the wording of those
standards is more authoritative than GCC's docs.

The C standard's wording is quite explicitly asymmetrical. It
distinguishes between the effective type of an object, and of the lvalue
that is used to access it. The effective type of the object, in this
case, is an array of character type. The lvalue is uint64_t. There is a
special case that allows the lvalue to have a character type regardless
of the effective type; there is no special case that allows the
effective type to be an array of character when the lvalue's type isn't
also an array of character.

The relevant section of the C++ standard is 7.2.1p11 (in n4860.pdf). It
uses the term "dynamic type" instead of "effective type", and "glvalue"
rather than "lvalue", and is assymetrical in the same way: it has an
exception that allows the glvalue to be "char, unsigned char, or
std::byte" regardless of the dynamic type. It has no exception that
allows the dynamic type be an array of one of those types unless the
glvalue is also such an array.

....
> The way I think, aliasing is of interest to the compiler because where
> it can assume that a T value is never changed via a U* pointer, and
> that the value that the U* points to is never changed by a change to
> the T value, it can optimize, e.g. not reload that value from memory
> when it's already in a register. If T = float and U* = int*, then it
> can make this assumption. Similarly if T = int and U* = float*, it can
> assume this.

That is one relevant issue, but it ignores the alignment issue, which is
also a barrier to aliasing, and an inherently asymmetric one.

Re: Strange optimization

<u6mh3k$1k8ho$1@dont-email.me>

  copy mid

https://www.rocksolidbbs.com/devel/article-flat.php?id=457&group=comp.lang.c%2B%2B#457

  copy link   Newsgroups: comp.lang.c++
Path: i2pn2.org!i2pn.org!eternal-september.org!news.eternal-september.org!.POSTED!not-for-mail
From: david.brown@hesbynett.no (David Brown)
Newsgroups: comp.lang.c++
Subject: Re: Strange optimization
Date: Sun, 18 Jun 2023 11:00:35 +0200
Organization: A noiseless patient Spider
Lines: 47
Message-ID: <u6mh3k$1k8ho$1@dont-email.me>
References: <u6acl4$3m5sb$1@dont-email.me> <kerr9cF31i7U1@mid.individual.net>
<u6bb5e$3tf93$1@dont-email.me> <u6be9i$3tpfh$1@dont-email.me>
<u6bkir$3ueat$1@dont-email.me> <u6bkqb$3ufs0$1@dont-email.me>
<u6d5dr$3rml$1@dont-email.me> <u6egpk$bn1q$2@dont-email.me>
<u6h18t$op4b$1@dont-email.me> <u6h82j$phr2$1@dont-email.me>
<u6hdrh$q807$1@dont-email.me> <87o7lfgu2y.fsf@nosuchdomain.example.com>
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8; format=flowed
Content-Transfer-Encoding: 8bit
Injection-Date: Sun, 18 Jun 2023 09:00:36 -0000 (UTC)
Injection-Info: dont-email.me; posting-host="09ec3990a67b04adf909dac980fc873a";
logging-data="1712696"; mail-complaints-to="abuse@eternal-september.org"; posting-account="U2FsdGVkX1/Hv3pi1uX2C0NrX0je2fXu5MuOCQ5PKY0="
User-Agent: Mozilla/5.0 (X11; Linux x86_64; rv:102.0) Gecko/20100101
Thunderbird/102.7.1
Cancel-Lock: sha1:fz3lJcLLzmALS29KC4CYoOwPpNM=
Content-Language: en-GB
In-Reply-To: <87o7lfgu2y.fsf@nosuchdomain.example.com>
 by: David Brown - Sun, 18 Jun 2023 09:00 UTC

On 16/06/2023 20:06, Keith Thompson wrote:
> "Alf P. Steinbach" <alf.p.steinbach@gmail.com> writes:
>> On 2023-06-16 10:55 AM, David Brown wrote:
>>> On 16/06/2023 08:59, Alf P. Steinbach wrote:
>>>>
>>>> `memcpy` is not the safest tool around. It's rather the opposite,
>>>> something to avoid /if possible/. `memcpy` is the "weak tool" here.
>>>>
>>> I don't know how the function here is supposed to be used.  We know
>>> that the pointer (it is syntactically a reference, but effectively a
>>> pointer) is properly aligned for a uint64_t, but we don't know if it
>>> actually points to a uint64_t.  Perhaps it points to a double, or
>>> some other 64-bit type.
>>
>> In the case you sketch where the bits do not represent a valid
>> `uint64_t`, the `memcpy` does not make the behavior well-defined:
>> that's a (dangerous) misconception.
>
> I think David was suggesting, not the bits are not a valid
> representation for an object of type uint64_t, but that for example the
> pointer point to an object whose type is, say, double. (I haven't
> followed the discussion closely enough to have an opinion on whether
> that matters.)
>

All I am saying, is that if you have a double in memory somewhere, and
you have a pointer-to-uint64_t that happens to contain the address of
the double, you can't use that pointer to access the double without
invoking undefined behaviour. Alignment is not the main problem here
(it is not a problem at all if the double has a valid alignment for a
uint64_t). It is the effective type (C), dynamic type (C++) or strict
aliasing rules (gcc) that are the problem.

Moving parts of the code to separate compilation, as Alf suggested, does
not help. reinterpret_cast<> does not help. std::launder, another of
Alf's suggestions, does not help either if you want to read the double
as an uint64_t. (You can use std::launder to create a new uint64_t in
the space the double occupied, but not to let you read the existing
data. I believe that std::launder was so rarely used appropriately, and
so often used incorrectly, that there are plans to remove it from C++.)

memcpy(), on the other hand, /will/ let you access the memory safely and
with well-defined behaviour. This is why the OP's original code will
work correctly. It was merely the optimisation in that code that was
not as good as it could easily have been.

Re: Strange optimization

<u6mh45$1k8ho$2@dont-email.me>

  copy mid

https://www.rocksolidbbs.com/devel/article-flat.php?id=458&group=comp.lang.c%2B%2B#458

  copy link   Newsgroups: comp.lang.c++
Path: i2pn2.org!i2pn.org!eternal-september.org!news.eternal-september.org!.POSTED!not-for-mail
From: david.brown@hesbynett.no (David Brown)
Newsgroups: comp.lang.c++
Subject: Re: Strange optimization
Date: Sun, 18 Jun 2023 11:00:53 +0200
Organization: A noiseless patient Spider
Lines: 22
Message-ID: <u6mh45$1k8ho$2@dont-email.me>
References: <u6acl4$3m5sb$1@dont-email.me> <kerr9cF31i7U1@mid.individual.net>
<u6bb5e$3tf93$1@dont-email.me> <u6be9i$3tpfh$1@dont-email.me>
<u6bkir$3ueat$1@dont-email.me> <u6bkqb$3ufs0$1@dont-email.me>
<u6d5dr$3rml$1@dont-email.me> <u6desf$4oa4$1@dont-email.me>
<u6h0j5$olvu$1@dont-email.me>
<a435a6d6-d86e-4a35-ac22-08f72c0fe369n@googlegroups.com>
<u6in1p$uvth$1@dont-email.me> <u6ks5p$1ajn0$1@dont-email.me>
<u6kuh6$1ass5$1@dont-email.me> <u6lc5b$1co4a$1@dont-email.me>
<u6lej6$1d1rr$1@dont-email.me>
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8; format=flowed
Content-Transfer-Encoding: 7bit
Injection-Date: Sun, 18 Jun 2023 09:00:53 -0000 (UTC)
Injection-Info: dont-email.me; posting-host="09ec3990a67b04adf909dac980fc873a";
logging-data="1712696"; mail-complaints-to="abuse@eternal-september.org"; posting-account="U2FsdGVkX19FFnRMV+py8UUosE9j1uZVdtZRC9Tl2xk="
User-Agent: Mozilla/5.0 (X11; Linux x86_64; rv:102.0) Gecko/20100101
Thunderbird/102.7.1
Cancel-Lock: sha1:on9OJZlkJmXuRaadEcUyq8x6Gl0=
Content-Language: en-GB
In-Reply-To: <u6lej6$1d1rr$1@dont-email.me>
 by: David Brown - Sun, 18 Jun 2023 09:00 UTC

On 18/06/2023 01:11, Alf P. Steinbach wrote:
> On 2023-06-18 12:30 AM, David Brown wrote:

>> I haven't contradicted myself - I have only contradicted you.
>
> Well I give up, as usual when I encounter strong religious beliefs with
> argumentation consisting of self-contradictions, denials and advice.
>

I strongly resent the accusation of "religious" beliefs here. I put
considerable effort into trying to understand the details of the C and
C++ languages, both the theoretical aspects from the standards, and the
practical aspects of how the languages are implemented in practice. If
my understanding is incorrect, I like to be told, and learn from that.
I have done so many, many times over the years in this group (and c.l.c.).

I still have no idea what you are talking about when you say I am
contradicting myself. None. Not a clue. If you told me - I have asked
you to do so - then we might make progress. But no, you would rather
repeat your claims without proof or explanation - /that/ is
religious-style posting.

Re: Strange optimization

<u6mvuj$1ljtv$1@dont-email.me>

  copy mid

https://www.rocksolidbbs.com/devel/article-flat.php?id=459&group=comp.lang.c%2B%2B#459

  copy link   Newsgroups: comp.lang.c++
Path: i2pn2.org!i2pn.org!eternal-september.org!news.eternal-september.org!.POSTED!not-for-mail
From: vir.campestris@invalid.invalid (Vir Campestris)
Newsgroups: comp.lang.c++
Subject: Re: Strange optimization
Date: Sun, 18 Jun 2023 14:13:55 +0100
Organization: A noiseless patient Spider
Lines: 31
Message-ID: <u6mvuj$1ljtv$1@dont-email.me>
References: <u6acl4$3m5sb$1@dont-email.me> <kerr9cF31i7U1@mid.individual.net>
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8; format=flowed
Content-Transfer-Encoding: 8bit
Injection-Date: Sun, 18 Jun 2023 13:13:55 -0000 (UTC)
Injection-Info: dont-email.me; posting-host="b27fe0f7df6811d8d94ed28e186dc816";
logging-data="1757119"; mail-complaints-to="abuse@eternal-september.org"; posting-account="U2FsdGVkX19RJvIrDVz7wadaagPpY+U8Unz9w2I82jw="
User-Agent: Mozilla/5.0 (X11; Linux x86_64; rv:102.0) Gecko/20100101
Thunderbird/102.11.0
Cancel-Lock: sha1:+LsJTduexAImfbf5+xTC4S07AMM=
Content-Language: en-GB
In-Reply-To: <kerr9cF31i7U1@mid.individual.net>
 by: Vir Campestris - Sun, 18 Jun 2023 13:13 UTC

On 13/06/2023 19:36, Bo Persson wrote:
> On 2023-06-13 at 20:31, Bonita Montero wrote:
>> What is the if( ... ) below good for ?
>>
>> template<bit_cursor_iterator BitIt>
>> constexpr uint64_t bit_cursor<BitIt>::dRead( uint64_t const &data )
>> noexcept
>> {
>> #if defined(__GNUC__) || defined(__llvm__)
>>      if( (size_t)&data % 8 )
>>          __builtin_unreachable();
>> #endif
>>      uint64_t value;
>>      memcpy( &value, &data, sizeof(uint64_t) );
>>      return value;
>> }
>
> It tells the compiler that the data is properly aligned. Possibly saves
> a potential call to library memcpy to sort out unaligned bytes.
>
Coming in late to this, but having read the thread through -
It tells the compiler the data is aligned on an 8 word boundary.

I say word, because while almost all computers have byte addressing it
is not 100% (the first one I learned assembler on had 36-bit addressing
- and I didn't use C on it).

And even when it is 8-bit addressing this could be an excessive check,
for example on a machine with a 32 bit native word size.

Andy

Re: Strange optimization

<877cs0sfrp.fsf@nosuchdomain.example.com>

  copy mid

https://www.rocksolidbbs.com/devel/article-flat.php?id=462&group=comp.lang.c%2B%2B#462

  copy link   Newsgroups: comp.lang.c++
Path: i2pn2.org!i2pn.org!eternal-september.org!news.eternal-september.org!.POSTED!not-for-mail
From: Keith.S.Thompson+u@gmail.com (Keith Thompson)
Newsgroups: comp.lang.c++
Subject: Re: Strange optimization
Date: Sun, 18 Jun 2023 12:59:38 -0700
Organization: None to speak of
Lines: 44
Message-ID: <877cs0sfrp.fsf@nosuchdomain.example.com>
References: <u6acl4$3m5sb$1@dont-email.me> <kerr9cF31i7U1@mid.individual.net>
<u6mvuj$1ljtv$1@dont-email.me>
MIME-Version: 1.0
Content-Type: text/plain; charset=utf-8
Content-Transfer-Encoding: 8bit
Injection-Info: dont-email.me; posting-host="b94a4e20c35c4d2b2fdfcbfd4f24d564";
logging-data="1840006"; mail-complaints-to="abuse@eternal-september.org"; posting-account="U2FsdGVkX1/mw5BVSXBYWS0mU9VyUgR1"
User-Agent: Gnus/5.13 (Gnus v5.13) Emacs/27.2 (gnu/linux)
Cancel-Lock: sha1:GeCfuTWfkwrC4Qr9sXYpll2kxUI=
sha1:GQhCPmcIPFb0mDPlAnpffEHECPk=
 by: Keith Thompson - Sun, 18 Jun 2023 19:59 UTC

Vir Campestris <vir.campestris@invalid.invalid> writes:
> On 13/06/2023 19:36, Bo Persson wrote:
>> On 2023-06-13 at 20:31, Bonita Montero wrote:
>>> What is the if( ... ) below good for ?
>>>
>>> template<bit_cursor_iterator BitIt>
>>> constexpr uint64_t bit_cursor<BitIt>::dRead( uint64_t const &data )
>>> noexcept
>>> {
>>> #if defined(__GNUC__) || defined(__llvm__)
>>>      if( (size_t)&data % 8 )
>>>          __builtin_unreachable();
>>> #endif
>>>      uint64_t value;
>>>      memcpy( &value, &data, sizeof(uint64_t) );
>>>      return value;
>>> }
>> It tells the compiler that the data is properly aligned. Possibly
>> saves a potential call to library memcpy to sort out unaligned
>> bytes.
>>
> Coming in late to this, but having read the thread through -
> It tells the compiler the data is aligned on an 8 word boundary.
>
> I say word, because while almost all computers have byte addressing it
> is not 100% (the first one I learned assembler on had 36-bit
> addressing - and I didn't use C on it).

C++, as far as I know, doesn't define what a "word" is, and
doesn't use that term. It does define a "byte" to be CHAR_BIT
bits, where CHAR_BIT >= 8. An implementation for a system with
36-bit addressing might either define CHAR_BIT==36 or CHAR_BIT==9
with byte level accesses implemented in software. (And such an
implementation would not define uint64_t.)

sizeof (uint64_t) is most likely 8, but it could be 4, 2, or 1.

> And even when it is 8-bit addressing this could be an excessive check,
> for example on a machine with a 32 bit native word size.

--
Keith Thompson (The_Other_Keith) Keith.S.Thompson+u@gmail.com
Will write code for food.
void Void(void) { Void(); } /* The recursive call of the void */

Re: Strange optimization

<u6pphv$23eei$1@dont-email.me>

  copy mid

https://www.rocksolidbbs.com/devel/article-flat.php?id=464&group=comp.lang.c%2B%2B#464

  copy link   Newsgroups: comp.lang.c++
Path: i2pn2.org!i2pn.org!eternal-september.org!news.eternal-september.org!.POSTED!not-for-mail
From: alf.p.steinbach@gmail.com (Alf P. Steinbach)
Newsgroups: comp.lang.c++
Subject: Re: Strange optimization
Date: Mon, 19 Jun 2023 16:43:08 +0200
Organization: A noiseless patient Spider
Lines: 67
Message-ID: <u6pphv$23eei$1@dont-email.me>
References: <u6acl4$3m5sb$1@dont-email.me> <kerr9cF31i7U1@mid.individual.net>
<u6bb5e$3tf93$1@dont-email.me> <u6be9i$3tpfh$1@dont-email.me>
<u6bkir$3ueat$1@dont-email.me> <u6bkqb$3ufs0$1@dont-email.me>
<u6d5dr$3rml$1@dont-email.me> <u6desf$4oa4$1@dont-email.me>
<u6h0j5$olvu$1@dont-email.me>
<a435a6d6-d86e-4a35-ac22-08f72c0fe369n@googlegroups.com>
<u6in1p$uvth$1@dont-email.me>
<ff2fe584-0af8-4bd8-997c-1359bac1c0d5n@googlegroups.com>
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8; format=flowed
Content-Transfer-Encoding: 8bit
Injection-Date: Mon, 19 Jun 2023 14:43:11 -0000 (UTC)
Injection-Info: dont-email.me; posting-host="30c4578ad313cd301286151f975cc9a5";
logging-data="2210258"; mail-complaints-to="abuse@eternal-september.org"; posting-account="U2FsdGVkX188PSUFcF6gq14Jo4zgmfW1"
User-Agent: Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:102.0) Gecko/20100101
Thunderbird/102.12.0
Cancel-Lock: sha1:HpTG4ls4RZbmhgXJZkJjwdHTTo0=
Content-Language: en-US
In-Reply-To: <ff2fe584-0af8-4bd8-997c-1359bac1c0d5n@googlegroups.com>
 by: Alf P. Steinbach - Mon, 19 Jun 2023 14:43 UTC

On 2023-06-18 7:10 AM, james...@alumni.caltech.edu wrote:
> On 6/16/23 18:17, Alf P. Steinbach wrote:
>> On 2023-06-16 11:16 PM, james...@alumni.caltech.edu wrote:
>>> On Friday, June 16, 2023 at 2:48:24 AM UTC-4, Alf P. Steinbach wrote:
>>>> On 2023-06-15 12:27 AM, James Kuyper wrote:
>>>>> On 6/14/23 15:46, Alf P. Steinbach wrote:
>>> ...
>>>>> All you need is a platform where misaligned pointers do not merely
>>>>> cause
>>>>> the code to be inefficient, but to actually malfunction. On such a
>>>>> platform, if p_bytes is not correctly aligned to store a uint64_t, then
>>>>> the code will malfunction in the reinterpret_cast<>.
>>>> Yes, but irrelevant for the case discussed, because the values are
>>>> guaranteed correctly aligned.
>>>
>>> You said nothing about what p_bytes was when you asked
>>>
>>> "how is the compiler to know that it's generally called with a
>>> `*reinterpret_cast<uint64_t*>( p_bytes )` as argument?"
>>>
>>> In particular, you said nothing about p_bytes that would guarantee
>>> that it was
>>> correctly aligned.
>>
>> That was known from the optimization attempt in the original posting's
>> code (I'm not the OP):
>>
>> #if defined(__GNUC__) || defined(__llvm__)
>> if( (size_t)&data % 8 )
>> __builtin_unreachable();
>> #endif
>
> How is that supposed to help?

Regarding the cast to `size_t` you'd have to ask the original coder,
which is not necessarily even the original poster, which isn't me.

However, presumably you're asking how that quote is meant to help you
understand that there is an assumption of correct alignment.

That's because with incorrect alignment execution (with GCC and clang)
reaches `__builtin_unreachable`, which is UB by definition. This allows
the compiler to optimize /as if/ correct alignment is guaranteed. Thus
the quoted code explicitly expresses that assumption.

> On platforms where misaligned access
> doesn't just cause inefficiency, but actually is a serious problem, the
> unspecified result of the reinterpret_cast<> of a mis-aligned pointer
> can be, and often is, a correctly aligned pointer which, therefore,
> necessarily points at a different location in memory. It is quite
> frequently one of the two nearest correctly aligned pointers. The result
> of such a conversion would pass that test, and as a result it would
> memcpy() from the wrong location.

Yes that's trivially true: breaking an assumption can have dire
consequences.

However, /mentioning/ that indicates some relevance to something.

And AFAICS there is no relevance to anything.

[snip]

- Alf

Re: Strange optimization

<u6q6ic$24otl$1@dont-email.me>

  copy mid

https://www.rocksolidbbs.com/devel/article-flat.php?id=465&group=comp.lang.c%2B%2B#465

  copy link   Newsgroups: comp.lang.c++
Path: i2pn2.org!i2pn.org!eternal-september.org!news.eternal-september.org!.POSTED!not-for-mail
From: jameskuyper@alumni.caltech.edu (James Kuyper)
Newsgroups: comp.lang.c++
Subject: Re: Strange optimization
Date: Mon, 19 Jun 2023 14:25:16 -0400
Organization: A noiseless patient Spider
Lines: 86
Message-ID: <u6q6ic$24otl$1@dont-email.me>
References: <u6acl4$3m5sb$1@dont-email.me> <kerr9cF31i7U1@mid.individual.net>
<u6bb5e$3tf93$1@dont-email.me> <u6be9i$3tpfh$1@dont-email.me>
<u6bkir$3ueat$1@dont-email.me> <u6bkqb$3ufs0$1@dont-email.me>
<u6d5dr$3rml$1@dont-email.me> <u6desf$4oa4$1@dont-email.me>
<u6h0j5$olvu$1@dont-email.me>
<a435a6d6-d86e-4a35-ac22-08f72c0fe369n@googlegroups.com>
<u6in1p$uvth$1@dont-email.me>
<ff2fe584-0af8-4bd8-997c-1359bac1c0d5n@googlegroups.com>
<u6pphv$23eei$1@dont-email.me>
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8
Content-Transfer-Encoding: 8bit
Injection-Date: Mon, 19 Jun 2023 18:25:16 -0000 (UTC)
Injection-Info: dont-email.me; posting-host="983349fb0e674c7c68953a574c4290c0";
logging-data="2253749"; mail-complaints-to="abuse@eternal-september.org"; posting-account="U2FsdGVkX19/pJK71zwchxmMUPqnY8u5z7zS/eotEeE="
User-Agent: Mozilla/5.0 (X11; Linux x86_64; rv:102.0) Gecko/20100101
Thunderbird/102.11.0
Cancel-Lock: sha1:u+ziBRksC1N/U8QgAmJmTh38A2A=
In-Reply-To: <u6pphv$23eei$1@dont-email.me>
Content-Language: en-US
 by: James Kuyper - Mon, 19 Jun 2023 18:25 UTC

On 6/19/23 10:43, Alf P. Steinbach wrote:
> On 2023-06-18 7:10 AM, james...@alumni.caltech.edu wrote:
>> On 6/16/23 18:17, Alf P. Steinbach wrote:
>>> On 2023-06-16 11:16 PM, james...@alumni.caltech.edu wrote:
>>>> On Friday, June 16, 2023 at 2:48:24 AM UTC-4, Alf P. Steinbach wrote:
>>>>> On 2023-06-15 12:27 AM, James Kuyper wrote:
>>>>>> On 6/14/23 15:46, Alf P. Steinbach wrote:
>>>> ...
>>>>>> All you need is a platform where misaligned pointers do not merely
>>>>>> cause
>>>>>> the code to be inefficient, but to actually malfunction. On such a
>>>>>> platform, if p_bytes is not correctly aligned to store a uint64_t, then
>>>>>> the code will malfunction in the reinterpret_cast<>.
>>>>> Yes, but irrelevant for the case discussed, because the values are
>>>>> guaranteed correctly aligned.
>>>>
>>>> You said nothing about what p_bytes was when you asked
>>>>
>>>> "how is the compiler to know that it's generally called with a
>>>> `*reinterpret_cast<uint64_t*>( p_bytes )` as argument?"
>>>>
>>>> In particular, you said nothing about p_bytes that would guarantee
>>>> that it was
>>>> correctly aligned.
>>>
>>> That was known from the optimization attempt in the original posting's
>>> code (I'm not the OP):
>>>
>>> #if defined(__GNUC__) || defined(__llvm__)
>>> if( (size_t)&data % 8 )
>>> __builtin_unreachable();
>>> #endif
>>
>> How is that supposed to help?
>
> Regarding the cast to `size_t` you'd have to ask the original coder,
> which is not necessarily even the original poster, which isn't me.

That is not the point I was discussing, which you might reasonably have
concluded from the fact that I didn't even mention size_t or [u]intptr_t.

I agree that it is the wrong type, but other people have already
addressed that issue more than adequately. I don't normally respond to
the original poster, for reasons that should be abundantly clear by
looking at how she responded to discussions of that issue. Keep in mind
that she's apparently not the person who wrote the code, she just ran
into it and is trying to figure it out.

> However, presumably you're asking how that quote is meant to help you
> understand that there is an assumption of correct alignment.

Nope. I'm not how I'm supposed to figure out that that assumption was
made. That's trivial. I'm asking why you think the assumption is
guaranteed to be true. See above, where you say "the values are
guaranteed correctly aligned".

> That's because with incorrect alignment execution (with GCC and clang)
> reaches `__builtin_unreachable`, which is UB by definition. This allows
> the compiler to optimize /as if/ correct alignment is guaranteed. Thus
> the quoted code explicitly expresses that assumption.

So, this is designed to make the compiler generate code that is
optimized for correctly aligned pointers, which is valuable on a
platform where mis-aligned access is merely slower than correctly
aligned access. It doesn't do anything useful on a platform where
mis-aligned pointers simply cannot be safely dereferenced. Nor does it
do any good on platforms where uint64_t* is incapable of representing an
address that is mis-aligned, so the reinterpret_cast<> is guaranteed to
produce a pointer that points at a different location in memory.

The fact that you don't work on such platforms doesn't change the fact
that such platforms exist, and it doesn't negate the C and C++
committees' decisions to write the two standards in such a way as to
allow fully conforming implementations on such platforms.

....
> Yes that's trivially true: breaking an assumption can have dire
> consequences.

You said that this code has "UB that can't happen unless one informs a
really perverse compiler that it's there." You've just admitted that if
you compile for a platform where that assumption is not valid, the
consequences are dire. That's precisely what the UB specified by the
standard is intended to cover. That the code can also be used on
platforms where it isn't a problem doesn't make it any less appropriate
for the standard to say that has UB.

Re: Strange optimization

<u6q6pv$24r72$1@dont-email.me>

  copy mid

https://www.rocksolidbbs.com/devel/article-flat.php?id=466&group=comp.lang.c%2B%2B#466

  copy link   Newsgroups: comp.lang.c++
Path: i2pn2.org!i2pn.org!eternal-september.org!news.eternal-september.org!.POSTED!not-for-mail
From: Bonita.Montero@gmail.com (Bonita Montero)
Newsgroups: comp.lang.c++
Subject: Re: Strange optimization
Date: Mon, 19 Jun 2023 20:29:18 +0200
Organization: A noiseless patient Spider
Lines: 12
Message-ID: <u6q6pv$24r72$1@dont-email.me>
References: <u6acl4$3m5sb$1@dont-email.me> <kerr9cF31i7U1@mid.individual.net>
<u6bb5e$3tf93$1@dont-email.me> <u6be9i$3tpfh$1@dont-email.me>
<u6bkir$3ueat$1@dont-email.me> <u6bkqb$3ufs0$1@dont-email.me>
<u6d5dr$3rml$1@dont-email.me> <u6desf$4oa4$1@dont-email.me>
<u6h0j5$olvu$1@dont-email.me>
<a435a6d6-d86e-4a35-ac22-08f72c0fe369n@googlegroups.com>
<u6in1p$uvth$1@dont-email.me>
<ff2fe584-0af8-4bd8-997c-1359bac1c0d5n@googlegroups.com>
<u6pphv$23eei$1@dont-email.me> <u6q6ic$24otl$1@dont-email.me>
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8; format=flowed
Content-Transfer-Encoding: 7bit
Injection-Date: Mon, 19 Jun 2023 18:29:19 -0000 (UTC)
Injection-Info: dont-email.me; posting-host="3684d5eb37dfa19f4cda13045b7995ff";
logging-data="2256098"; mail-complaints-to="abuse@eternal-september.org"; posting-account="U2FsdGVkX19VZn0CAPDRE0n3Z+GkU3mR8Xv/CmYIZbQ="
User-Agent: Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:102.0) Gecko/20100101
Thunderbird/102.12.0
Cancel-Lock: sha1:z0QDLQK44w3EH0flGHqfNpKNcHQ=
In-Reply-To: <u6q6ic$24otl$1@dont-email.me>
Content-Language: de-DE
 by: Bonita Montero - Mon, 19 Jun 2023 18:29 UTC

Am 19.06.2023 um 20:25 schrieb James Kuyper:

> I agree that it is the wrong type, but other people have already
> addressed that issue more than adequately. I don't normally respond to
> the original poster, for reasons that should be abundantly clear by
> looking at how she responded to discussions of that issue. Keep in mind
> that she's apparently not the person who wrote the code, she just ran
> into it and is trying to figure it out.

I've written the code and I don't think that I'll ever run
into problems with that; I'm simply not that compulsive.

Re: Strange optimization

<u6qluc$26ech$1@dont-email.me>

  copy mid

https://www.rocksolidbbs.com/devel/article-flat.php?id=468&group=comp.lang.c%2B%2B#468

  copy link   Newsgroups: comp.lang.c++
Path: i2pn2.org!i2pn.org!eternal-september.org!news.eternal-september.org!.POSTED!not-for-mail
From: alf.p.steinbach@gmail.com (Alf P. Steinbach)
Newsgroups: comp.lang.c++
Subject: Re: Strange optimization
Date: Tue, 20 Jun 2023 00:47:39 +0200
Organization: A noiseless patient Spider
Lines: 110
Message-ID: <u6qluc$26ech$1@dont-email.me>
References: <u6acl4$3m5sb$1@dont-email.me> <kerr9cF31i7U1@mid.individual.net>
<u6bb5e$3tf93$1@dont-email.me> <u6be9i$3tpfh$1@dont-email.me>
<u6bkir$3ueat$1@dont-email.me> <u6bkqb$3ufs0$1@dont-email.me>
<u6d5dr$3rml$1@dont-email.me> <u6desf$4oa4$1@dont-email.me>
<u6h0j5$olvu$1@dont-email.me>
<a435a6d6-d86e-4a35-ac22-08f72c0fe369n@googlegroups.com>
<u6in1p$uvth$1@dont-email.me>
<ff2fe584-0af8-4bd8-997c-1359bac1c0d5n@googlegroups.com>
<u6pphv$23eei$1@dont-email.me> <u6q6ic$24otl$1@dont-email.me>
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8; format=flowed
Content-Transfer-Encoding: 8bit
Injection-Date: Mon, 19 Jun 2023 22:47:40 -0000 (UTC)
Injection-Info: dont-email.me; posting-host="537c938343d5be4041e8292a97cb5dd4";
logging-data="2308497"; mail-complaints-to="abuse@eternal-september.org"; posting-account="U2FsdGVkX19o0gG6VjvNu9yNX1YimniP"
User-Agent: Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:102.0) Gecko/20100101
Thunderbird/102.12.0
Cancel-Lock: sha1:4v9bp9gkquhnbKVBU+Z1dvnQw3g=
Content-Language: en-US
In-Reply-To: <u6q6ic$24otl$1@dont-email.me>
 by: Alf P. Steinbach - Mon, 19 Jun 2023 22:47 UTC

On 2023-06-19 8:25 PM, James Kuyper wrote:
> On 6/19/23 10:43, Alf P. Steinbach wrote:
>> On 2023-06-18 7:10 AM, james...@alumni.caltech.edu wrote:
>>> On 6/16/23 18:17, Alf P. Steinbach wrote:
>>>> On 2023-06-16 11:16 PM, james...@alumni.caltech.edu wrote:
>>>>> On Friday, June 16, 2023 at 2:48:24 AM UTC-4, Alf P. Steinbach wrote:
>>>>>> On 2023-06-15 12:27 AM, James Kuyper wrote:
>>>>>>> On 6/14/23 15:46, Alf P. Steinbach wrote:
>>>>> ...
>>>>>>> All you need is a platform where misaligned pointers do not merely
>>>>>>> cause
>>>>>>> the code to be inefficient, but to actually malfunction. On such a
>>>>>>> platform, if p_bytes is not correctly aligned to store a uint64_t, then
>>>>>>> the code will malfunction in the reinterpret_cast<>.
>>>>>> Yes, but irrelevant for the case discussed, because the values are
>>>>>> guaranteed correctly aligned.
>>>>>
>>>>> You said nothing about what p_bytes was when you asked
>>>>>
>>>>> "how is the compiler to know that it's generally called with a
>>>>> `*reinterpret_cast<uint64_t*>( p_bytes )` as argument?"
>>>>>
>>>>> In particular, you said nothing about p_bytes that would guarantee
>>>>> that it was
>>>>> correctly aligned.
>>>>
>>>> That was known from the optimization attempt in the original posting's
>>>> code (I'm not the OP):
>>>>
>>>> #if defined(__GNUC__) || defined(__llvm__)
>>>> if( (size_t)&data % 8 )
>>>> __builtin_unreachable();
>>>> #endif
>>>
>>> How is that supposed to help?
>>
>> Regarding the cast to `size_t` you'd have to ask the original coder,
>> which is not necessarily even the original poster, which isn't me.
>
> That is not the point I was discussing, which you might reasonably have
> concluded from the fact that I didn't even mention size_t or [u]intptr_t.
>
> I agree that it is the wrong type, but other people have already
> addressed that issue more than adequately. I don't normally respond to
> the original poster, for reasons that should be abundantly clear by
> looking at how she responded to discussions of that issue. Keep in mind
> that she's apparently not the person who wrote the code, she just ran
> into it and is trying to figure it out.
>
>> However, presumably you're asking how that quote is meant to help you
>> understand that there is an assumption of correct alignment.
>
> Nope. I'm not how I'm supposed to figure out that that assumption was
> made. That's trivial. I'm asking why you think the assumption is
> guaranteed to be true. See above, where you say "the values are
> guaranteed correctly aligned".
>
>> That's because with incorrect alignment execution (with GCC and clang)
>> reaches `__builtin_unreachable`, which is UB by definition. This allows
>> the compiler to optimize /as if/ correct alignment is guaranteed. Thus
>> the quoted code explicitly expresses that assumption.
>
> So, this is designed to make the compiler generate code that is
> optimized for correctly aligned pointers, which is valuable on a
> platform where mis-aligned access is merely slower than correctly
> aligned access. It doesn't do anything useful on a platform where
> mis-aligned pointers simply cannot be safely dereferenced.

Not sure I'm buying that evaluation; it assumes too much about
compiler's code generation and knowledge of calling context.

> Nor does it
> do any good on platforms where uint64_t* is incapable of representing an
> address that is mis-aligned, so the reinterpret_cast<> is guaranteed to
> produce a pointer that points at a different location in memory.
>
> The fact that you don't work on such platforms doesn't change the fact
> that such platforms exist, and it doesn't negate the C and C++
> committees' decisions to write the two standards in such a way as to
> allow fully conforming implementations on such platforms.
>
> ...
>> Yes that's trivially true: breaking an assumption can have dire
>> consequences.
>
> You said that this code has "UB that can't happen unless one informs a
> really perverse compiler that it's there." You've just admitted that if
> you compile for a platform where that assumption is not valid, the
> consequences are dire.

No, the assumption that was made in the code was not about the platform.

The assumption made was that it's guaranteed that the function is called
with a correctly aligned argument.

The platform has nothing to do with it.

> That's precisely what the UB specified by the
> standard is intended to cover. That the code can also be used on
> platforms where it isn't a problem doesn't make it any less appropriate
> for the standard to say that has UB.

These errors of reasoning follow from the one about platform-specificity.

- Alf

Re: Strange optimization

<87wmzzq88j.fsf@nosuchdomain.example.com>

  copy mid

https://www.rocksolidbbs.com/devel/article-flat.php?id=472&group=comp.lang.c%2B%2B#472

  copy link   Newsgroups: comp.lang.c++
Path: i2pn2.org!i2pn.org!eternal-september.org!news.eternal-september.org!.POSTED!not-for-mail
From: Keith.S.Thompson+u@gmail.com (Keith Thompson)
Newsgroups: comp.lang.c++
Subject: Re: Strange optimization
Date: Mon, 19 Jun 2023 17:37:32 -0700
Organization: None to speak of
Lines: 124
Message-ID: <87wmzzq88j.fsf@nosuchdomain.example.com>
References: <u6acl4$3m5sb$1@dont-email.me> <kerr9cF31i7U1@mid.individual.net>
<u6bb5e$3tf93$1@dont-email.me> <u6be9i$3tpfh$1@dont-email.me>
<u6bkir$3ueat$1@dont-email.me> <u6bkqb$3ufs0$1@dont-email.me>
<u6d5dr$3rml$1@dont-email.me> <u6desf$4oa4$1@dont-email.me>
<u6h0j5$olvu$1@dont-email.me>
<a435a6d6-d86e-4a35-ac22-08f72c0fe369n@googlegroups.com>
<u6in1p$uvth$1@dont-email.me>
<ff2fe584-0af8-4bd8-997c-1359bac1c0d5n@googlegroups.com>
<u6pphv$23eei$1@dont-email.me> <u6q6ic$24otl$1@dont-email.me>
<u6qluc$26ech$1@dont-email.me>
MIME-Version: 1.0
Content-Type: text/plain; charset=utf-8
Content-Transfer-Encoding: 8bit
Injection-Info: dont-email.me; posting-host="ec6f65492d80c8c8e08f1a6d42bbcd49";
logging-data="2330010"; mail-complaints-to="abuse@eternal-september.org"; posting-account="U2FsdGVkX1+Gm29dfnUIL6r1bgQQmOJ4"
User-Agent: Gnus/5.13 (Gnus v5.13) Emacs/27.2 (gnu/linux)
Cancel-Lock: sha1:60T6NYZENKfhZ9hSmoXBeTchmbg=
sha1:2Q3A/EMdHMKORMATxOMi5pmeAqM=
 by: Keith Thompson - Tue, 20 Jun 2023 00:37 UTC

"Alf P. Steinbach" <alf.p.steinbach@gmail.com> writes:
> On 2023-06-19 8:25 PM, James Kuyper wrote:
>> On 6/19/23 10:43, Alf P. Steinbach wrote:
>>> On 2023-06-18 7:10 AM, james...@alumni.caltech.edu wrote:
>>>> On 6/16/23 18:17, Alf P. Steinbach wrote:
>>>>> On 2023-06-16 11:16 PM, james...@alumni.caltech.edu wrote:
>>>>>> On Friday, June 16, 2023 at 2:48:24 AM UTC-4, Alf P. Steinbach wrote:
>>>>>>> On 2023-06-15 12:27 AM, James Kuyper wrote:
>>>>>>>> On 6/14/23 15:46, Alf P. Steinbach wrote:
>>>>>> ...
>>>>>>>> All you need is a platform where misaligned pointers do not merely
>>>>>>>> cause
>>>>>>>> the code to be inefficient, but to actually malfunction. On such a
>>>>>>>> platform, if p_bytes is not correctly aligned to store a uint64_t, then
>>>>>>>> the code will malfunction in the reinterpret_cast<>.
>>>>>>> Yes, but irrelevant for the case discussed, because the values are
>>>>>>> guaranteed correctly aligned.
>>>>>>
>>>>>> You said nothing about what p_bytes was when you asked
>>>>>>
>>>>>> "how is the compiler to know that it's generally called with a
>>>>>> `*reinterpret_cast<uint64_t*>( p_bytes )` as argument?"
>>>>>>
>>>>>> In particular, you said nothing about p_bytes that would guarantee
>>>>>> that it was
>>>>>> correctly aligned.
>>>>>
>>>>> That was known from the optimization attempt in the original posting's
>>>>> code (I'm not the OP):
>>>>>
>>>>> #if defined(__GNUC__) || defined(__llvm__)
>>>>> if( (size_t)&data % 8 )
>>>>> __builtin_unreachable();
>>>>> #endif
>>>>
>>>> How is that supposed to help?
>>>
>>> Regarding the cast to `size_t` you'd have to ask the original coder,
>>> which is not necessarily even the original poster, which isn't me.
>> That is not the point I was discussing, which you might reasonably
>> have
>> concluded from the fact that I didn't even mention size_t or [u]intptr_t.
>> I agree that it is the wrong type, but other people have already
>> addressed that issue more than adequately. I don't normally respond to
>> the original poster, for reasons that should be abundantly clear by
>> looking at how she responded to discussions of that issue. Keep in mind
>> that she's apparently not the person who wrote the code, she just ran
>> into it and is trying to figure it out.
>>
>>> However, presumably you're asking how that quote is meant to help you
>>> understand that there is an assumption of correct alignment.
>> Nope. I'm not how I'm supposed to figure out that that assumption
>> was
>> made. That's trivial. I'm asking why you think the assumption is
>> guaranteed to be true. See above, where you say "the values are
>> guaranteed correctly aligned".
>>
>>> That's because with incorrect alignment execution (with GCC and clang)
>>> reaches `__builtin_unreachable`, which is UB by definition. This allows
>>> the compiler to optimize /as if/ correct alignment is guaranteed. Thus
>>> the quoted code explicitly expresses that assumption.
>> So, this is designed to make the compiler generate code that is
>> optimized for correctly aligned pointers, which is valuable on a
>> platform where mis-aligned access is merely slower than correctly
>> aligned access. It doesn't do anything useful on a platform where
>> mis-aligned pointers simply cannot be safely dereferenced.
>
> Not sure I'm buying that evaluation; it assumes too much about
> compiler's code generation and knowledge of calling context.
>
>
>> Nor does it
>> do any good on platforms where uint64_t* is incapable of representing an
>> address that is mis-aligned, so the reinterpret_cast<> is guaranteed to
>> produce a pointer that points at a different location in memory.
>> The fact that you don't work on such platforms doesn't change the
>> fact
>> that such platforms exist, and it doesn't negate the C and C++
>> committees' decisions to write the two standards in such a way as to
>> allow fully conforming implementations on such platforms.
>> ...
>>> Yes that's trivially true: breaking an assumption can have dire
>>> consequences.
>> You said that this code has "UB that can't happen unless one informs
>> a
>> really perverse compiler that it's there." You've just admitted that if
>> you compile for a platform where that assumption is not valid, the
>> consequences are dire.
>
> No, the assumption that was made in the code was not about the platform.
>
> The assumption made was that it's guaranteed that the function is
> called with a correctly aligned argument.
>
> The platform has nothing to do with it.

I haven't followed this thread closely enough to be sure that nobody has
raised this point:

Nothing in the language standard guarantees that converting a pointer
value to an integer type and checking the low-order bits of the result
tells you anything about the alignment of the memory the pointer points
to. The assuption is likely to be valid on any platform supported by
gcc and/or llvm (clang), but I have no idea whether either of those
compilers would be clever enough to recognize what the code is trying to
assert and perform the desired optimization.

I've worked on systems where a pointer value with a representation
ending in binary 000 might be byte-aligned, or a pointer ending in
non-zero bits might be word-aligned. (Cray vector machines, where the
hardware can only address 64-bit words and a byte offset was stored in
the high-order 4 bits and managed in software.)

>> That's precisely what the UB specified by the
>> standard is intended to cover. That the code can also be used on
>> platforms where it isn't a problem doesn't make it any less appropriate
>> for the standard to say that has UB.
>
> These errors of reasoning follow from the one about platform-specificity.

--
Keith Thompson (The_Other_Keith) Keith.S.Thompson+u@gmail.com
Will write code for food.
void Void(void) { Void(); } /* The recursive call of the void */

Re: Strange optimization

<u6rh68$2cica$1@dont-email.me>

  copy mid

https://www.rocksolidbbs.com/devel/article-flat.php?id=473&group=comp.lang.c%2B%2B#473

  copy link   Newsgroups: comp.lang.c++
Path: i2pn2.org!i2pn.org!eternal-september.org!news.eternal-september.org!.POSTED!not-for-mail
From: alf.p.steinbach@gmail.com (Alf P. Steinbach)
Newsgroups: comp.lang.c++
Subject: Re: Strange optimization
Date: Tue, 20 Jun 2023 08:32:38 +0200
Organization: A noiseless patient Spider
Lines: 64
Message-ID: <u6rh68$2cica$1@dont-email.me>
References: <u6acl4$3m5sb$1@dont-email.me> <kerr9cF31i7U1@mid.individual.net>
<u6bb5e$3tf93$1@dont-email.me> <u6be9i$3tpfh$1@dont-email.me>
<u6bkir$3ueat$1@dont-email.me> <u6bkqb$3ufs0$1@dont-email.me>
<u6d5dr$3rml$1@dont-email.me> <u6desf$4oa4$1@dont-email.me>
<u6h0j5$olvu$1@dont-email.me>
<a435a6d6-d86e-4a35-ac22-08f72c0fe369n@googlegroups.com>
<u6in1p$uvth$1@dont-email.me>
<ff2fe584-0af8-4bd8-997c-1359bac1c0d5n@googlegroups.com>
<u6pphv$23eei$1@dont-email.me> <u6q6ic$24otl$1@dont-email.me>
<u6qluc$26ech$1@dont-email.me> <87wmzzq88j.fsf@nosuchdomain.example.com>
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8; format=flowed
Content-Transfer-Encoding: 7bit
Injection-Date: Tue, 20 Jun 2023 06:32:41 -0000 (UTC)
Injection-Info: dont-email.me; posting-host="537c938343d5be4041e8292a97cb5dd4";
logging-data="2509194"; mail-complaints-to="abuse@eternal-september.org"; posting-account="U2FsdGVkX18T6s4JGLaCzyHWA81HEXa6"
User-Agent: Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:102.0) Gecko/20100101
Thunderbird/102.12.0
Cancel-Lock: sha1:ILO9DjpBQ8e2bbV+Qn0kxK0kK+w=
Content-Language: en-US
In-Reply-To: <87wmzzq88j.fsf@nosuchdomain.example.com>
 by: Alf P. Steinbach - Tue, 20 Jun 2023 06:32 UTC

On 2023-06-20 2:37 AM, Keith Thompson wrote:
[snip]
> Nothing in the language standard guarantees that converting a pointer
> value to an integer type and checking the low-order bits of the result
> tells you anything about the alignment of the memory the pointer points
> to. The assuption is likely to be valid on any platform supported by
> gcc and/or llvm (clang), but I have no idea whether either of those
> compilers would be clever enough to recognize what the code is trying to
> assert and perform the desired optimization.

From a portability POV the example given in the original posting is
even worse:

* Your point that checking the low order bits is not guaranteed.
Apparently the standard library has no simple way to query the
alignment of an arbitrary address, only the alignment of a type. In
principle one could check the alignment by generating a corresponding
well aligned address via `std::align`, and comparing, but that may be
too convoluted to help the compiler understand an assertion about the
alignment. However, at the point where one is dealing with a byte array,
if one has ensured that the array itself is properly aligned it's
trivial to check/ensure that any part of it has some given alignment.

* The `% 8` in the code.
Should be `% alignof( Type )`. E.g. byte size /can/ be 8 octets.

* The cast to `size_t`.
Should be a cast to `uintptr_t`. `size_t` may be too small.

And I've remarked elsewhere that portable `for(;;){}` UB would probably
communicate the "can't get here" just as well as the non-portable GCC
intrinsic.

Plus, that

* The parameter type `uint64_t const &` /already/ expresses the
alignment assumption.

It's only with machine code inlining that the compiler might at all
consider that `void foo( int& )` might receive a non-aligned parameter.

Proper alignment is a tacit assumption of all C++ code.

> I've worked on systems where a pointer value with a representation
> ending in binary 000 might be byte-aligned, or a pointer ending in
> non-zero bits might be word-aligned. (Cray vector machines, where the
> hardware can only address 64-bit words and a byte offset was stored in
> the high-order 4 bits and managed in software.)

Gah. :-(

But that raises the question of which platforms modern C++ really cover
/in practice/. I.e. which platforms have C++20 compilers.

It's counter-productive with modern C++ rules that ensure portability of
some construct where that portability is only an issue for systems that
don't have modern C++ compilers, so maybe the standard should be fixed.

[snip]

- Alf

Re: Strange optimization

<u6rjlm$2cprq$1@dont-email.me>

  copy mid

https://www.rocksolidbbs.com/devel/article-flat.php?id=475&group=comp.lang.c%2B%2B#475

  copy link   Newsgroups: comp.lang.c++
Path: i2pn2.org!i2pn.org!eternal-september.org!news.eternal-september.org!.POSTED!not-for-mail
From: david.brown@hesbynett.no (David Brown)
Newsgroups: comp.lang.c++
Subject: Re: Strange optimization
Date: Tue, 20 Jun 2023 09:15:01 +0200
Organization: A noiseless patient Spider
Lines: 77
Message-ID: <u6rjlm$2cprq$1@dont-email.me>
References: <u6acl4$3m5sb$1@dont-email.me> <kerr9cF31i7U1@mid.individual.net>
<u6bb5e$3tf93$1@dont-email.me> <u6be9i$3tpfh$1@dont-email.me>
<u6bkir$3ueat$1@dont-email.me> <u6bkqb$3ufs0$1@dont-email.me>
<u6d5dr$3rml$1@dont-email.me> <u6desf$4oa4$1@dont-email.me>
<u6h0j5$olvu$1@dont-email.me>
<a435a6d6-d86e-4a35-ac22-08f72c0fe369n@googlegroups.com>
<u6in1p$uvth$1@dont-email.me>
<ff2fe584-0af8-4bd8-997c-1359bac1c0d5n@googlegroups.com>
<u6pphv$23eei$1@dont-email.me> <u6q6ic$24otl$1@dont-email.me>
<u6qluc$26ech$1@dont-email.me> <87wmzzq88j.fsf@nosuchdomain.example.com>
<u6rh68$2cica$1@dont-email.me>
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8; format=flowed
Content-Transfer-Encoding: 8bit
Injection-Date: Tue, 20 Jun 2023 07:15:02 -0000 (UTC)
Injection-Info: dont-email.me; posting-host="ffaba89ac21e46104e2b75102edce864";
logging-data="2516858"; mail-complaints-to="abuse@eternal-september.org"; posting-account="U2FsdGVkX1/cExMrzLBfVymc335mrDiAH4GCkFqmtZg="
User-Agent: Mozilla/5.0 (X11; Linux x86_64; rv:102.0) Gecko/20100101
Thunderbird/102.9.0
Cancel-Lock: sha1:ml66t8PE3gqXgMXrSvOCQ5gvqpw=
Content-Language: en-GB
In-Reply-To: <u6rh68$2cica$1@dont-email.me>
 by: David Brown - Tue, 20 Jun 2023 07:15 UTC

On 20/06/2023 08:32, Alf P. Steinbach wrote:
> On 2023-06-20 2:37 AM, Keith Thompson wrote:
> [snip]
>> Nothing in the language standard guarantees that converting a pointer
>> value to an integer type and checking the low-order bits of the result
>> tells you anything about the alignment of the memory the pointer points
>> to.  The assuption is likely to be valid on any platform supported by
>> gcc and/or llvm (clang), but I have no idea whether either of those
>> compilers would be clever enough to recognize what the code is trying to
>> assert and perform the desired optimization.

That assumption /is/ true for all gcc targets. (I am less familiar with
clang.) And gcc will /sometimes/ use the explicit undefined behaviour
to optimise the access as aligned. Sometimes, however, that information
appears to get lost before later optimisation passes. (On RISC-V, gcc
generates fast aligned code at -O1, but slow unaligned code at -O2.)
gcc has a better solution to this - __builtin_assume_aligned() - which
could also work if gcc ever targeted systems like the Cray you
mentioned. (The OP, being Bonita, decided that the original poorer code
was better than listening to anyone else.)

>
> From a portability POV the example given in the original posting is
> even worse:
>
> * Your point that checking the low order bits is not guaranteed.
>   Apparently the standard library has no simple way to query the
> alignment of an arbitrary address, only the alignment of a type.

C++20 has std::assume_aligned, which is basically a less flexible
standardisation of the gcc builtin. It does not let you query the
alignment, but it /does/ let you tell the compiler about it, which is
what you need for optimising code like the original post.

> In
> principle one could check the alignment by generating a corresponding
> well aligned address via `std::align`, and comparing, but that may be
> too convoluted to help the compiler understand an assertion about the
> alignment. However, at the point where one is dealing with a byte array,
> if one has ensured that the array itself is properly aligned it's
> trivial to check/ensure that any part of it has some given alignment.
>
> * The `% 8` in the code.
>   Should be `% alignof( Type )`. E.g. byte size /can/ be 8 octets.
>

Yes.

> * The cast to `size_t`.
>   Should be a cast to `uintptr_t`. `size_t` may be too small.
>

There's also the possibility that the target does not have an integer
type that is suitable for conversion of all data pointer types. If that
is the case, no "uintptr_t" type will exist for the platform - but
"size_t" will always exist. So if you have such a target, a cast to
"uintptr_t" will give a compile-time error, which is vastly preferable
to the silently dangerous compile you'd get with "size_t".

And of course, "uintptr_t" is better than "size_t" simply because you
would be using the correctly specified type for the purpose.

> And I've remarked elsewhere that portable `for(;;){}` UB would probably
> communicate the "can't get here" just as well as the non-portable GCC
> intrinsic.
>

In theory, perhaps. In practice, no. In some kinds of coding, infinite
loops like that can be exactly what you want, and compilers generally do
not assume that it is an error or undefined behaviour. (I am not at all
convinced that an infinite loop is undefined behaviour.)

If you are happy with C++23, then std::unreachable() would of course be
a better choice than a gcc extension.

Re: Strange optimization

<u6sjom$2fpsg$1@dont-email.me>

  copy mid

https://www.rocksolidbbs.com/devel/article-flat.php?id=478&group=comp.lang.c%2B%2B#478

  copy link   Newsgroups: comp.lang.c++
Path: i2pn2.org!i2pn.org!eternal-september.org!news.eternal-september.org!.POSTED!not-for-mail
From: jameskuyper@alumni.caltech.edu (James Kuyper)
Newsgroups: comp.lang.c++
Subject: Re: Strange optimization
Date: Tue, 20 Jun 2023 12:22:46 -0400
Organization: A noiseless patient Spider
Lines: 56
Message-ID: <u6sjom$2fpsg$1@dont-email.me>
References: <u6acl4$3m5sb$1@dont-email.me> <kerr9cF31i7U1@mid.individual.net>
<u6bb5e$3tf93$1@dont-email.me> <u6be9i$3tpfh$1@dont-email.me>
<u6bkir$3ueat$1@dont-email.me> <u6bkqb$3ufs0$1@dont-email.me>
<u6d5dr$3rml$1@dont-email.me> <u6desf$4oa4$1@dont-email.me>
<u6h0j5$olvu$1@dont-email.me>
<a435a6d6-d86e-4a35-ac22-08f72c0fe369n@googlegroups.com>
<u6in1p$uvth$1@dont-email.me>
<ff2fe584-0af8-4bd8-997c-1359bac1c0d5n@googlegroups.com>
<u6pphv$23eei$1@dont-email.me> <u6q6ic$24otl$1@dont-email.me>
<u6qluc$26ech$1@dont-email.me>
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8
Content-Transfer-Encoding: 7bit
Injection-Date: Tue, 20 Jun 2023 16:22:46 -0000 (UTC)
Injection-Info: dont-email.me; posting-host="2b24d9cec3f828e3ae8989f95d939832";
logging-data="2615184"; mail-complaints-to="abuse@eternal-september.org"; posting-account="U2FsdGVkX192/iYJOMOL7OA4Ji2UGrwn2foI6FaUc80="
User-Agent: Mozilla/5.0 (X11; Linux x86_64; rv:102.0) Gecko/20100101
Thunderbird/102.11.0
Cancel-Lock: sha1:Ey8tPk5SSgV+BYQT7DGps/o6of8=
In-Reply-To: <u6qluc$26ech$1@dont-email.me>
Content-Language: en-US
 by: James Kuyper - Tue, 20 Jun 2023 16:22 UTC

On 6/19/23 18:47, Alf P. Steinbach wrote:
> On 2023-06-19 8:25 PM, James Kuyper wrote:
>> On 6/19/23 10:43, Alf P. Steinbach wrote:
....
>>> That's because with incorrect alignment execution (with GCC and clang)
>>> reaches `__builtin_unreachable`, which is UB by definition. This allows
>>> the compiler to optimize /as if/ correct alignment is guaranteed. Thus
>>> the quoted code explicitly expresses that assumption.
>>
>> So, this is designed to make the compiler generate code that is
>> optimized for correctly aligned pointers, which is valuable on a
>> platform where mis-aligned access is merely slower than correctly
>> aligned access. It doesn't do anything useful on a platform where
>> mis-aligned pointers simply cannot be safely dereferenced.
>
> Not sure I'm buying that evaluation; it assumes too much about
> compiler's code generation and knowledge of calling context.

I'm merely trying to guess at plausible reasons why someone might
consider it desireable to instruct the compiler to generate code that
can't deal safely with misaligned pointers. Obviously, those reasons
will be platform-specific.

....
>>> Yes that's trivially true: breaking an assumption can have dire
>>> consequences.
>>
>> You said that this code has "UB that can't happen unless one informs a
>> really perverse compiler that it's there." You've just admitted that if
>> you compile for a platform where that assumption is not valid, the
>> consequences are dire.
>
>
> No, the assumption that was made in the code was not about the platform.
>
> The assumption made was that it's guaranteed that the function is
> called with a correctly aligned argument.

Nothing is more normal than writing code that has pre-conditions that
must be met for the code to work properly. However, I find it quite
bizarre that you describe such a practice by claiming that it is
guaranteed that the pre-conditions will be met, and that, as a result,
the corresponding UB can't happen. And you reiterate those claims after
admitting that "breaking an assumption can have dire consequences".
You're twisting the meaning of basic words in the English language such
as "guarantee", "assume", and "can't happen" in ways I hadn't considered
plausible. I'll keep that in mind for future conversations with you.

,,,
> The platform has nothing to do with it.

The platform's characteristics determine whether or not alignment even
matters, and in particular whether misaligned access is even an issue,
and if it is, whether it is merely inefficient, or positively dangerous.

Re: Strange optimization

<c834d51c-6afa-4119-9241-19623ae4d8ffn@googlegroups.com>

  copy mid

https://www.rocksolidbbs.com/devel/article-flat.php?id=745&group=comp.lang.c%2B%2B#745

  copy link   Newsgroups: comp.lang.c++
X-Received: by 2002:a05:6214:5609:b0:635:e3ae:e0a0 with SMTP id mg9-20020a056214560900b00635e3aee0a0mr21657qvb.9.1688841402430;
Sat, 08 Jul 2023 11:36:42 -0700 (PDT)
X-Received: by 2002:a17:902:ec82:b0:1b8:5c93:5736 with SMTP id
x2-20020a170902ec8200b001b85c935736mr9185687plg.13.1688841401974; Sat, 08 Jul
2023 11:36:41 -0700 (PDT)
Path: i2pn2.org!i2pn.org!usenet.blueworldhosting.com!diablo1.usenet.blueworldhosting.com!peer01.iad!feed-me.highwinds-media.com!news.highwinds-media.com!news-out.google.com!nntp.google.com!postnews.google.com!google-groups.googlegroups.com!not-for-mail
Newsgroups: comp.lang.c++
Date: Sat, 8 Jul 2023 11:36:41 -0700 (PDT)
In-Reply-To: <u6egi1$bn1q$1@dont-email.me>
Injection-Info: google-groups.googlegroups.com; posting-host=85.253.185.93; posting-account=ykVvQgoAAACp4VEbaWPL3R35slQMB5Au
NNTP-Posting-Host: 85.253.185.93
References: <u6acl4$3m5sb$1@dont-email.me> <kerr9cF31i7U1@mid.individual.net>
<u6bb5e$3tf93$1@dont-email.me> <u6be9i$3tpfh$1@dont-email.me>
<u6bkir$3ueat$1@dont-email.me> <u6bkqb$3ufs0$1@dont-email.me>
<u6d5dr$3rml$1@dont-email.me> <u6desf$4oa4$1@dont-email.me> <u6egi1$bn1q$1@dont-email.me>
User-Agent: G2/1.0
MIME-Version: 1.0
Message-ID: <c834d51c-6afa-4119-9241-19623ae4d8ffn@googlegroups.com>
Subject: Re: Strange optimization
From: vvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvva@hotmail.com (V)
Injection-Date: Sat, 08 Jul 2023 18:36:42 +0000
Content-Type: text/plain; charset="UTF-8"
Content-Transfer-Encoding: quoted-printable
X-Received-Bytes: 3742
 by: V - Sat, 8 Jul 2023 18:36 UTC

Hello, David.

Nice to see You around, man.

Seems line noone wants to post anymore to comp.programming.

Even Amine not.

You too moved out of there, like it is visible ?

On Thursday, June 15, 2023 at 11:02:30 AM UTC+3, David Brown wrote:
> On 15/06/2023 00:27, James Kuyper wrote:
> > On 6/14/23 15:46, Alf P. Steinbach wrote:
> >> On 2023-06-14 7:56 AM, Bonita Montero wrote:
> >>> Am 14.06.2023 um 07:52 schrieb Alf P. Steinbach:
> >>>
> >>>> It's copying an `uint64_t` that is known to be correctly aliased, to
> >>>> an `uint64_t`; that's nonsense.
> >>>
> >>> The reference intitially supplied by the caller is casted from a char
> >>> -array. memcpy() is the only legal way in C++ to alias that content.
> > ...
> >> So for the separately compiled function it does not matter technically,
> >> except possibly for performance, whether it uses clear, concise, safe
> >> and guaranteed max efficient `=`, or verbose and unsafe `memcpy`.
> >>
> >> That means that regarding this matter the common interpretation of the
> >> standard is not technical but instead specifies a formal UB that can't
> >> happen unless one informs a really perverse compiler that it's there.
> >
> > All you need is a platform where misaligned pointers do not merely cause
> > the code to be inefficient, but to actually malfunction. On such a
> > platform, if p_bytes is not correctly aligned to store a uint64_t, then
> > the code will malfunction in the reinterpret_cast<>.
> >
> And in case anyone has doubts, such platforms do exist. I have used
> embedded microcontrollers in which an unaligned access might mean access
> to the address rounded down (i.e., a 16-bit store to 0x2001 actually
> stores 16 bits at 0x2000). The stored data may or may not be
> byte-swapped - the behaviour is undefined, and I don't think it was
> consistent between different generations of the processor.
>
> There are also big processors which will fault on unaligned accesses.
> Even if there are OS services in place to simulate the access, the
> process is so massively slower than normal accesses that it could be
> considered a software malfunction for performance critical code.

Pages:123
server_pubkey.txt

rocksolid light 0.9.8
clearnet tor