Rocksolid Light

Welcome to novaBBS (click a section below)

mail  files  register  newsreader  groups  login

Message-ID:  

19 May, 2024: Line wrapping has been changed to be more consistent with Usenet standards.
 If you find that it is broken please let me know here rocksolid.nodes.help


devel / comp.lang.c++ / 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
Strange optimization

<u6acl4$3m5sb$1@dont-email.me>

  copy mid

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

  copy link   Newsgroups: comp.lang.c++
Path: i2pn2.org!i2pn.org!eternal-september.org!news.eternal-september.org!.POSTED!not-for-mail
From: Bonita.M...@gmail.com (Bonita Montero)
Newsgroups: comp.lang.c++
Subject: Strange optimization
Date: Tue, 13 Jun 2023 20:31:00 +0200
Organization: A noiseless patient Spider
Lines: 13
Message-ID: <u6acl4$3m5sb$1@dont-email.me>
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8; format=flowed
Content-Transfer-Encoding: 7bit
Injection-Date: Tue, 13 Jun 2023 18:31:00 -0000 (UTC)
Injection-Info: dont-email.me; posting-host="c2e878786c6e585a7e31e84fb42c1c56";
logging-data="3872651"; mail-complaints-to="abuse@eternal-september.org"; posting-account="U2FsdGVkX1/lXcZic0MCcIUsZ72Qgde1b8CRfppy3L0="
User-Agent: Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:102.0) Gecko/20100101
Thunderbird/102.12.0
Cancel-Lock: sha1:hICP3xyJbAlhxJg/AODEgPSlVz4=
Content-Language: de-DE
 by: Bonita Montero - Tue, 13 Jun 2023 18:31 UTC

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;
}

Re: Strange optimization

<kerr9cF31i7U1@mid.individual.net>

  copy mid

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

  copy link   Newsgroups: comp.lang.c++
Path: i2pn2.org!i2pn.org!news.swapon.de!fu-berlin.de!uni-berlin.de!individual.net!not-for-mail
From: bo...@bo-persson.se (Bo Persson)
Newsgroups: comp.lang.c++
Subject: Re: Strange optimization
Date: Tue, 13 Jun 2023 20:36:27 +0200
Lines: 19
Message-ID: <kerr9cF31i7U1@mid.individual.net>
References: <u6acl4$3m5sb$1@dont-email.me>
Mime-Version: 1.0
Content-Type: text/plain; charset=UTF-8; format=flowed
Content-Transfer-Encoding: 8bit
X-Trace: individual.net goM2njTlmr2BGJAwNPN+3w6XycesIMofJ/scCvKl/R5+uygBpN
Cancel-Lock: sha1:dtdhAcbNEkitE+P78TtbSEZKPrQ=
User-Agent: Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:102.0) Gecko/20100101
Thunderbird/102.12.0
Content-Language: sv
In-Reply-To: <u6acl4$3m5sb$1@dont-email.me>
 by: Bo Persson - Tue, 13 Jun 2023 18:36 UTC

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.

Re: Strange optimization

<u6bb5e$3tf93$1@dont-email.me>

  copy mid

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

  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.st...@gmail.com (Alf P. Steinbach)
Newsgroups: comp.lang.c++
Subject: Re: Strange optimization
Date: Wed, 14 Jun 2023 05:11:38 +0200
Organization: A noiseless patient Spider
Lines: 47
Message-ID: <u6bb5e$3tf93$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: Wed, 14 Jun 2023 03:11:42 -0000 (UTC)
Injection-Info: dont-email.me; posting-host="5b4d584567085f50e3eea1a6bb67d902";
logging-data="4111651"; mail-complaints-to="abuse@eternal-september.org"; posting-account="U2FsdGVkX1+BvDFmZNJJik4SzPRKrtQc"
User-Agent: Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:102.0) Gecko/20100101
Thunderbird/102.12.0
Cancel-Lock: sha1:yX4kD6FMEBm3/d+COgZnxYk0DNw=
In-Reply-To: <kerr9cF31i7U1@mid.individual.net>
Content-Language: en-US
 by: Alf P. Steinbach - Wed, 14 Jun 2023 03:11 UTC

On 2023-06-13 8:36 PM, 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.

But the function is nonsense, copying an `uint64_t` to `uint64_t`.

And with that in mind it doesn't matter that proper alignment is already
guaranteed via the types, i.e. that the alignment hint is nonsense
embedded in nonsense.

At a guess, the whole thing is probably wrapped in some nonsense at the
level where it's called, to make it Churchill-esque: nonsense containing
some nonsense wrapped in nonsense.

---

More constructively, the compiler-specific `__builtin_unreachable();`
can possibly be replaced with portable `for(;;){}`, which is formally UB
and thus tells the compiler to assume it will never be reached.

And the nonsensical `memcpy` can be replaced with safer assignment, `=`.

And the assignment can be replaced with initialization.

---

But then, the whole nonsensical shebang can be replaced with nothing,
and I guess that's worth keeping in mind in general.

- Alf

Re: Strange optimization

<u6be9i$3tpfh$1@dont-email.me>

  copy mid

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

  copy link   Newsgroups: comp.lang.c++
Path: i2pn2.org!i2pn.org!eternal-september.org!news.eternal-september.org!.POSTED!not-for-mail
From: Bonita.M...@gmail.com (Bonita Montero)
Newsgroups: comp.lang.c++
Subject: Re: Strange optimization
Date: Wed, 14 Jun 2023 06:05:06 +0200
Organization: A noiseless patient Spider
Lines: 7
Message-ID: <u6be9i$3tpfh$1@dont-email.me>
References: <u6acl4$3m5sb$1@dont-email.me> <kerr9cF31i7U1@mid.individual.net>
<u6bb5e$3tf93$1@dont-email.me>
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8; format=flowed
Content-Transfer-Encoding: 7bit
Injection-Date: Wed, 14 Jun 2023 04:05:06 -0000 (UTC)
Injection-Info: dont-email.me; posting-host="21067f4b658527782aa5be2eecdd29ac";
logging-data="4122097"; mail-complaints-to="abuse@eternal-september.org"; posting-account="U2FsdGVkX1/MizNgLUR6ET93iNaSvnWcx2BdPzOFrUE="
User-Agent: Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:102.0) Gecko/20100101
Thunderbird/102.12.0
Cancel-Lock: sha1:SiI4tcojgDMTISd80nmG7XnlmE0=
Content-Language: de-DE
In-Reply-To: <u6bb5e$3tf93$1@dont-email.me>
 by: Bonita Montero - Wed, 14 Jun 2023 04:05 UTC

Am 14.06.2023 um 05:11 schrieb Alf P. Steinbach:

> But the function is nonsense, copying an `uint64_t` to `uint64_t`.

No, it isn't because the uint64_t is an aliased content.

Re: Strange optimization

<u6bkir$3ueat$1@dont-email.me>

  copy mid

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

  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.st...@gmail.com (Alf P. Steinbach)
Newsgroups: comp.lang.c++
Subject: Re: Strange optimization
Date: Wed, 14 Jun 2023 07:52:24 +0200
Organization: A noiseless patient Spider
Lines: 18
Message-ID: <u6bkir$3ueat$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>
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8; format=flowed
Content-Transfer-Encoding: 7bit
Injection-Date: Wed, 14 Jun 2023 05:52:27 -0000 (UTC)
Injection-Info: dont-email.me; posting-host="5b4d584567085f50e3eea1a6bb67d902";
logging-data="4143453"; mail-complaints-to="abuse@eternal-september.org"; posting-account="U2FsdGVkX1/4hCPkHHKzn1RGjy4wy4Se"
User-Agent: Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:102.0) Gecko/20100101
Thunderbird/102.12.0
Cancel-Lock: sha1:bkXzaMMZY9kbfxr53qGW9ItxkUw=
In-Reply-To: <u6be9i$3tpfh$1@dont-email.me>
Content-Language: en-US
 by: Alf P. Steinbach - Wed, 14 Jun 2023 05:52 UTC

On 2023-06-14 6:05 AM, Bonita Montero wrote:
> Am 14.06.2023 um 05:11 schrieb Alf P. Steinbach:
>
>> But the function is nonsense, copying an `uint64_t` to `uint64_t`.
>
> No, it isn't because the uint64_t is an aliased content.

It's copying an `uint64_t` that is known to be correctly aliased, to an
`uint64_t`; that's nonsense.

It's using `memcpy` instead of `=` for the copying; that's some nested
nonsense.

Maybe I should state this more clearly: it's retarded code.

- Alf

Re: Strange optimization

<u6bkmb$3ueat$2@dont-email.me>

  copy mid

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

  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.st...@gmail.com (Alf P. Steinbach)
Newsgroups: comp.lang.c++
Subject: Re: Strange optimization
Date: Wed, 14 Jun 2023 07:54:17 +0200
Organization: A noiseless patient Spider
Lines: 23
Message-ID: <u6bkmb$3ueat$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>
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8; format=flowed
Content-Transfer-Encoding: 7bit
Injection-Date: Wed, 14 Jun 2023 05:54:19 -0000 (UTC)
Injection-Info: dont-email.me; posting-host="5b4d584567085f50e3eea1a6bb67d902";
logging-data="4143453"; mail-complaints-to="abuse@eternal-september.org"; posting-account="U2FsdGVkX1+DRsuw++6Wme2eJrKlQWsO"
User-Agent: Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:102.0) Gecko/20100101
Thunderbird/102.12.0
Cancel-Lock: sha1:pVYf3SwqX1w10z1rFZ8UzTuUciA=
Content-Language: en-US
In-Reply-To: <u6bkir$3ueat$1@dont-email.me>
 by: Alf P. Steinbach - Wed, 14 Jun 2023 05:54 UTC

On 2023-06-14 7:52 AM, Alf P. Steinbach wrote:
> On 2023-06-14 6:05 AM, Bonita Montero wrote:
>> Am 14.06.2023 um 05:11 schrieb Alf P. Steinbach:
>>
>>> But the function is nonsense, copying an `uint64_t` to `uint64_t`.
>>
>> No, it isn't because the uint64_t is an aliased content.
>
> It's copying an `uint64_t` that is known to be correctly aliased, to an
> `uint64_t`; that's nonsense.

aligned, aligned. sry.

>
> It's using `memcpy` instead of `=` for the copying; that's some nested
> nonsense.
>
> Maybe I should state this more clearly: it's retarded code.
>
>
> - Alf
>

Re: Strange optimization

<u6bkqb$3ufs0$1@dont-email.me>

  copy mid

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

  copy link   Newsgroups: comp.lang.c++
Path: i2pn2.org!i2pn.org!eternal-september.org!news.eternal-september.org!.POSTED!not-for-mail
From: Bonita.M...@gmail.com (Bonita Montero)
Newsgroups: comp.lang.c++
Subject: Re: Strange optimization
Date: Wed, 14 Jun 2023 07:56:28 +0200
Organization: A noiseless patient Spider
Lines: 8
Message-ID: <u6bkqb$3ufs0$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>
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8; format=flowed
Content-Transfer-Encoding: 7bit
Injection-Date: Wed, 14 Jun 2023 05:56:27 -0000 (UTC)
Injection-Info: dont-email.me; posting-host="21067f4b658527782aa5be2eecdd29ac";
logging-data="4145024"; mail-complaints-to="abuse@eternal-september.org"; posting-account="U2FsdGVkX19Mn/ZpCzXmJmCngCqDFa3xH5QHNhKvoQA="
User-Agent: Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:102.0) Gecko/20100101
Thunderbird/102.12.0
Cancel-Lock: sha1:u1rtkButwpVtjpwevuZg8Kwkyq4=
In-Reply-To: <u6bkir$3ueat$1@dont-email.me>
Content-Language: de-DE
 by: Bonita Montero - Wed, 14 Jun 2023 05:56 UTC

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.

Re: Strange optimization

<u6bpr3$3v0ra$1@dont-email.me>

  copy mid

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

  copy link   Newsgroups: comp.lang.c++
Path: i2pn2.org!i2pn.org!news.niel.me!news.gegeweb.eu!gegeweb.org!eternal-september.org!news.eternal-september.org!.POSTED!not-for-mail
From: Bonita.M...@gmail.com (Bonita Montero)
Newsgroups: comp.lang.c++
Subject: Re: Strange optimization
Date: Wed, 14 Jun 2023 09:22:11 +0200
Organization: A noiseless patient Spider
Lines: 31
Message-ID: <u6bpr3$3v0ra$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: Wed, 14 Jun 2023 07:22:11 -0000 (UTC)
Injection-Info: dont-email.me; posting-host="21067f4b658527782aa5be2eecdd29ac";
logging-data="4162410"; mail-complaints-to="abuse@eternal-september.org"; posting-account="U2FsdGVkX1+c75M8mwfZRxIt1JEvPKUvDjY+JQt1ADs="
User-Agent: Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:102.0) Gecko/20100101
Thunderbird/102.12.0
Cancel-Lock: sha1:A8P+c9aCstEUqRLuUDb2+k0Elok=
Content-Language: de-DE
In-Reply-To: <kerr9cF31i7U1@mid.individual.net>
 by: Bonita Montero - Wed, 14 Jun 2023 07:22 UTC

Am 13.06.2023 um 20:36 schrieb Bo Persson:

> 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.

memcpy() is a partitially intrsinic function. I.e. if you want to copy
a static small amount of memory the compiler uses some moves; or even
copies the content directly to a register.
My bit_cursor is for having bit_field acess in a char-sized array. The
compiler can't guess that the address supplied to memcpy() is aligned,
so I give it a hint with the above code. Accoding to godbolt this works
with current clang++, but unfortunately not with g++ when you compile
code for RISC-V (unaligned acesses RISC-V are optional with RISC-V).

Re: Strange optimization

<u6btus$3vgqg$1@dont-email.me>

  copy mid

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

  copy link   Newsgroups: comp.lang.c++
Path: i2pn2.org!i2pn.org!eternal-september.org!news.eternal-september.org!.POSTED!not-for-mail
From: david.br...@hesbynett.no (David Brown)
Newsgroups: comp.lang.c++
Subject: Re: Strange optimization
Date: Wed, 14 Jun 2023 10:32:28 +0200
Organization: A noiseless patient Spider
Lines: 14
Message-ID: <u6btus$3vgqg$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>
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8; format=flowed
Content-Transfer-Encoding: 7bit
Injection-Date: Wed, 14 Jun 2023 08:32:28 -0000 (UTC)
Injection-Info: dont-email.me; posting-host="bad7058f6ec3ea79d200aabcbfe89d38";
logging-data="4178768"; mail-complaints-to="abuse@eternal-september.org"; posting-account="U2FsdGVkX1/HD8ltMNU0CXs3+0tGSV4G1FBHqgyR5q8="
User-Agent: Mozilla/5.0 (X11; Linux x86_64; rv:102.0) Gecko/20100101
Thunderbird/102.9.0
Cancel-Lock: sha1:6s2Yhe4BJx+Gsd42PELQGLoJeJw=
Content-Language: en-GB
In-Reply-To: <u6bkqb$3ufs0$1@dont-email.me>
 by: David Brown - Wed, 14 Jun 2023 08:32 UTC

On 14/06/2023 07:56, 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.
>

Well, you can also use std::byte as an alternative to memcpy. But
memcpy works for all standards.

Re: Strange optimization

<u6c6vf$hh4$1@dont-email.me>

  copy mid

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

  copy link   Newsgroups: comp.lang.c++
Path: i2pn2.org!i2pn.org!eternal-september.org!news.eternal-september.org!.POSTED!not-for-mail
From: david.br...@hesbynett.no (David Brown)
Newsgroups: comp.lang.c++
Subject: Re: Strange optimization
Date: Wed, 14 Jun 2023 13:06:22 +0200
Organization: A noiseless patient Spider
Lines: 99
Message-ID: <u6c6vf$hh4$1@dont-email.me>
References: <u6acl4$3m5sb$1@dont-email.me> <kerr9cF31i7U1@mid.individual.net>
<u6bpr3$3v0ra$1@dont-email.me>
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8; format=flowed
Content-Transfer-Encoding: 8bit
Injection-Date: Wed, 14 Jun 2023 11:06:23 -0000 (UTC)
Injection-Info: dont-email.me; posting-host="bad7058f6ec3ea79d200aabcbfe89d38";
logging-data="17956"; mail-complaints-to="abuse@eternal-september.org"; posting-account="U2FsdGVkX18SrMTjwQ8ykBumHMpkK2j5V+BRls4po48="
User-Agent: Mozilla/5.0 (X11; Linux x86_64; rv:102.0) Gecko/20100101
Thunderbird/102.9.0
Cancel-Lock: sha1:YxkoPGEIBRBzUbInPRs1PNxqCk4=
In-Reply-To: <u6bpr3$3v0ra$1@dont-email.me>
Content-Language: en-GB
 by: David Brown - Wed, 14 Jun 2023 11:06 UTC

On 14/06/2023 09:22, Bonita Montero wrote:
> Am 13.06.2023 um 20:36 schrieb Bo Persson:
>
>> 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.
>
> memcpy() is a partitially intrsinic function. I.e. if you want to copy
> a static small amount of memory the compiler uses some moves;  or even
> copies the content directly to a register.

Note that this applies only to specific implementations of memcpy().
The function itself is a standard library function, and can be
implemented with whatever magic the compiler provides, but there is
absolutely no requirement for a given implementation to handle it as
inline or with any special optimisations.

Most decent compilers will, however, optimise memcpy() as inline when
given a small constant size for the number of bytes, and even omit it
entirely if it doesn't need to do anything in the generated code. (It
can still be useful for accessing data in ways that might be contrary to
the "strict aliasing" rules or alignment requirements, even when no code
is generated - as in this case.)

> My bit_cursor is for having bit_field acess in a char-sized array. The
> compiler can't guess that the address supplied to memcpy() is aligned,
> so I give it a hint with the above code. Accoding to godbolt this works
> with current clang++, but unfortunately not with g++ when you compile
> code for RISC-V (unaligned acesses RISC-V are optional with RISC-V).
>

I can understand the attempt, but it doesn't actually work. Look at the
generated code with godbolt.org.

First, it's worth noting that the use of "constexpr" here is wrong. Use
of uninitialised variables, memcpy and pointer casts are all naughty in
constexpr functions (later C++ standards are a little laxer than earlier
ones).

There is no need to test for "__llvm__" here when you test for
"__GNUC__", as clang/llvm also defines "__GNUC__". Your cast should be
to "uintptr_t", not "size_t". And if you want to be /really/ portable,
or at least consistent, you should use sizeof(uint64_t) instead of 8.
(All of gcc's targets have 8-bit char, so it is also not unreasonable to
use 8 everywhere instead of sizeof(uint64_t).)

Regarding the optimisation hint, you have a problem.
"__builtin_unreachable()" can /sometimes/ be used as an optimisation
hint, but it is not really the purpose of the builtin, and sometimes it
does not work as you expect. In particular, different passes of the
optimiser can make things work in different ways. So if you look at
32-bit ARM or 64-bit RISC-V, your code gives optimal results for -O1.
However, at -O2 the compiler is eliminating the unreachable part earlier
and losing that information, leading to slow unaligned load code.

The correct way to handle this kind of thing is __builtin_assume_unaligned :

uint64_t dRead2(const uint64_t &data) {
#if defined(__GNUC__) || defined(__llvm__)
uint64_t value;
const uint64_t * p = (const uint64_t * )
__builtin_assume_aligned (&data, sizeof(uint64_t);
memcpy( &value, p, sizeof(uint64_t) );
return value;
#else
uint64_t value;
memcpy( &value, &data, sizeof(uint64_t) );
return value;
#endif
}

<https://godbolt.org/z/P3oj1fzs9>

Still, it would be nice if your code was also optimised the way you
want, so I filed a missed-optimisation issue for gcc.

<https://gcc.gnu.org/bugzilla/show_bug.cgi?id=110249>

Re: Strange optimization

<u6cffm$1efd$1@dont-email.me>

  copy mid

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

  copy link   Newsgroups: comp.lang.c++
Path: i2pn2.org!i2pn.org!eternal-september.org!news.eternal-september.org!.POSTED!not-for-mail
From: Bonita.M...@gmail.com (Bonita Montero)
Newsgroups: comp.lang.c++
Subject: Re: Strange optimization
Date: Wed, 14 Jun 2023 15:31:35 +0200
Organization: A noiseless patient Spider
Lines: 28
Message-ID: <u6cffm$1efd$1@dont-email.me>
References: <u6acl4$3m5sb$1@dont-email.me> <kerr9cF31i7U1@mid.individual.net>
<u6bpr3$3v0ra$1@dont-email.me> <u6c6vf$hh4$1@dont-email.me>
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8; format=flowed
Content-Transfer-Encoding: 8bit
Injection-Date: Wed, 14 Jun 2023 13:31:34 -0000 (UTC)
Injection-Info: dont-email.me; posting-host="21067f4b658527782aa5be2eecdd29ac";
logging-data="47597"; mail-complaints-to="abuse@eternal-september.org"; posting-account="U2FsdGVkX19k7Glo3iShnuapPYaxRIhBOwIC6G4PIjc="
User-Agent: Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:102.0) Gecko/20100101
Thunderbird/102.12.0
Cancel-Lock: sha1:aZvDuDiuupvujobqwfPG3Uvk8tM=
Content-Language: de-DE
In-Reply-To: <u6c6vf$hh4$1@dont-email.me>
 by: Bonita Montero - Wed, 14 Jun 2023 13:31 UTC

Am 14.06.2023 um 13:06 schrieb David Brown:

> Note that this applies only to specific implementations of memcpy(). ...

memcpy() is the only way to reliably alias something in C++ and
therefore it's safe to assume that it is partitially intrinsic.

> I can understand the attempt, but it doesn't actually work.
> Look at the generated code with godbolt.org.

I checked it for clang++ and RISC_V and the trick worked; without
it does eight loads to eight registers followes by eight stores
to an aligned entitity on the stack and a single load from the
stack. But this currently doesn't work with g++ and RISC-V.

> There is no need to test for "__llvm__" here when you test for
> "__GNUC__", as clang/llvm also defines "__GNUC__".  Your cast
> should be to "uintptr_t", not "size_t".

I use size_t because that's a safe assumption with flat memory.

> Regarding the optimisation hint, you have a problem.
> "__builtin_unreachable()" can /sometimes/ be used as
> an optimisation hint, ...

Check godbolt and RISC-V clang++.

Re: Strange optimization

<u6cj8u$1sek$1@dont-email.me>

  copy mid

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

  copy link   Newsgroups: comp.lang.c++
Path: i2pn2.org!i2pn.org!eternal-september.org!news.eternal-september.org!.POSTED!not-for-mail
From: david.br...@hesbynett.no (David Brown)
Newsgroups: comp.lang.c++
Subject: Re: Strange optimization
Date: Wed, 14 Jun 2023 16:36:14 +0200
Organization: A noiseless patient Spider
Lines: 74
Message-ID: <u6cj8u$1sek$1@dont-email.me>
References: <u6acl4$3m5sb$1@dont-email.me> <kerr9cF31i7U1@mid.individual.net>
<u6bpr3$3v0ra$1@dont-email.me> <u6c6vf$hh4$1@dont-email.me>
<u6cffm$1efd$1@dont-email.me>
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8; format=flowed
Content-Transfer-Encoding: 8bit
Injection-Date: Wed, 14 Jun 2023 14:36:14 -0000 (UTC)
Injection-Info: dont-email.me; posting-host="bad7058f6ec3ea79d200aabcbfe89d38";
logging-data="61908"; mail-complaints-to="abuse@eternal-september.org"; posting-account="U2FsdGVkX1/3T5aN41AqMbFmKKnXwA/tFSFfmzooCJg="
User-Agent: Mozilla/5.0 (X11; Linux x86_64; rv:102.0) Gecko/20100101
Thunderbird/102.9.0
Cancel-Lock: sha1:DURzmSLOkJ2rgYGXY68QE56wp/8=
In-Reply-To: <u6cffm$1efd$1@dont-email.me>
Content-Language: en-GB
 by: David Brown - Wed, 14 Jun 2023 14:36 UTC

On 14/06/2023 15:31, Bonita Montero wrote:
> Am 14.06.2023 um 13:06 schrieb David Brown:
>
>> Note that this applies only to specific implementations of memcpy(). ...
>
> memcpy() is the only way to reliably alias something in C++ and
> therefore it's safe to assume that it is partitially intrinsic.

That's not what "intrinsic" means. An "intrinsic" is basically a
wrapper function around a cpu-specific feature or instruction. So if
you want to use advanced SIMD instructions, you are likely to include a
header with macros (they are often macros, for use in C as much as C++)
defining these for a particular CPU family.

It is not long since memcpy() on MSVC was always implemented as a call
to an external library function in a DLL, which was a huge overhead for
such a simple feature.

And no, "memcpy" is /not/ the only way to access aliased data or access
data through pointers of non-compatible types. You can use any
character pointer, with pointers to unsigned char being particularly
useful. (This is precisely how "memcpy" is able to work.) std::byte is
a more modern C++ replacement for using unsigned char in such contexts.

I agree that memcpy is often the right choice here, and I agree that
most decent compilers will optimise it very well. But /some/ compilers
do a bad job of handling it.

>
>> I can understand the attempt, but it doesn't actually work.
>> Look at the generated code with godbolt.org.
>
> I checked it for clang++ and RISC_V and the trick worked; without
> it does eight loads to eight registers followes by eight stores
> to an aligned entitity on the stack and a single load from the
>  stack. But this currently doesn't work with g++ and RISC-V.
>

You did not check gcc.

You might have looked at the results for "-O1" optimisation, but you did
not look at the results for "-O2" optimisation.

I spend quite a bit of effort on the post I made to you, because I
thought it was an interesting case. I would appreciate it if you
actually read the post.

>> There is no need to test for "__llvm__" here when you test for
>> "__GNUC__", as clang/llvm also defines "__GNUC__".  Your cast
>> should be  to "uintptr_t", not "size_t".
>
> I use size_t because that's a safe assumption with flat memory.
>

It is still not the appropriate type. "uintptr_t" is the type for such
use-cases (even though it will almost certainly be implemented with the
same underlying integer type as size_t). The flatness of the memory is
irrelevant.

>> Regarding the optimisation hint, you have a problem.
>> "__builtin_unreachable()" can /sometimes/ be used as
>> an optimisation hint, ...
>
> Check godbolt and RISC-V clang++.
>

You did not look at gcc.

Writing code with a specific conditional compilation that explicitly
targets gcc, and then not testing properly for gcc, is just silly.

Did you try the godbolt link I sent?

Re: Strange optimization

<u6cjrm$1ts6$1@dont-email.me>

  copy mid

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

  copy link   Newsgroups: comp.lang.c++
Path: i2pn2.org!i2pn.org!eternal-september.org!news.eternal-september.org!.POSTED!not-for-mail
From: Bonita.M...@gmail.com (Bonita Montero)
Newsgroups: comp.lang.c++
Subject: Re: Strange optimization
Date: Wed, 14 Jun 2023 16:46:16 +0200
Organization: A noiseless patient Spider
Lines: 37
Message-ID: <u6cjrm$1ts6$1@dont-email.me>
References: <u6acl4$3m5sb$1@dont-email.me> <kerr9cF31i7U1@mid.individual.net>
<u6bpr3$3v0ra$1@dont-email.me> <u6c6vf$hh4$1@dont-email.me>
<u6cffm$1efd$1@dont-email.me> <u6cj8u$1sek$1@dont-email.me>
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8; format=flowed
Content-Transfer-Encoding: 8bit
Injection-Date: Wed, 14 Jun 2023 14:46:14 -0000 (UTC)
Injection-Info: dont-email.me; posting-host="21067f4b658527782aa5be2eecdd29ac";
logging-data="63366"; mail-complaints-to="abuse@eternal-september.org"; posting-account="U2FsdGVkX19m1VAgEj4BlgFQQOxq6n1AqNcUUC/k3y4="
User-Agent: Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:102.0) Gecko/20100101
Thunderbird/102.12.0
Cancel-Lock: sha1:MBkyw0/BdFExPqbpHlT+x1lh3y0=
Content-Language: de-DE
In-Reply-To: <u6cj8u$1sek$1@dont-email.me>
 by: Bonita Montero - Wed, 14 Jun 2023 14:46 UTC

Am 14.06.2023 um 16:36 schrieb David Brown:

> On 14/06/2023 15:31, Bonita Montero wrote:

>> memcpy() is the only way to reliably alias something in C++ and
>> therefore it's safe to assume that it is partitially intrinsic.

> That's not what "intrinsic" means. <rest unread>

For me it's partitially intrinsic.

> It is not long since memcpy() on MSVC was always implemented as a call
> to an external library function in a DLL, which was a huge overhead for
> such a simple feature.

Since it's the only way to have safe aliasing in C++ you can silently
rely on that.

> And no, "memcpy" is /not/ the only way to access aliased data or access
> data through pointers of non-compatible types.  You can use any
> character pointer, ...

In C you can alias anything as a char-array and vise versa,
but not in C++.

> You did not check gcc.

I checked it and this trick doesn't work with g++ / RISC-V
_with_ -O2 but I hope this will change, so I check for __GNUC__.

> It is still not the appropriate type. ...

For me it is because I use systems with flat memory. And in this case
I could even use a char since I only check for the three lower bits to
be zero.

Re: Strange optimization

<3PkiM.61009$hl93.4930@fx18.iad>

  copy mid

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

  copy link   Newsgroups: comp.lang.c++
Path: i2pn2.org!i2pn.org!weretis.net!feeder8.news.weretis.net!newsreader4.netcologne.de!news.netcologne.de!peer01.ams1!peer.ams1.xlned.com!news.xlned.com!peer03.iad!feed-me.highwinds-media.com!news.highwinds-media.com!fx18.iad.POSTED!not-for-mail
X-newsreader: xrn 9.03-beta-14-64bit
Sender: scott@dragon.sl.home (Scott Lurndal)
From: sco...@slp53.sl.home (Scott Lurndal)
Reply-To: slp53@pacbell.net
Subject: Re: Strange optimization
Newsgroups: comp.lang.c++
References: <u6acl4$3m5sb$1@dont-email.me> <kerr9cF31i7U1@mid.individual.net> <u6bpr3$3v0ra$1@dont-email.me> <u6c6vf$hh4$1@dont-email.me> <u6cffm$1efd$1@dont-email.me> <u6cj8u$1sek$1@dont-email.me>
Lines: 14
Message-ID: <3PkiM.61009$hl93.4930@fx18.iad>
X-Complaints-To: abuse@usenetserver.com
NNTP-Posting-Date: Wed, 14 Jun 2023 15:14:07 UTC
Organization: UsenetServer - www.usenetserver.com
Date: Wed, 14 Jun 2023 15:14:07 GMT
X-Received-Bytes: 1393
 by: Scott Lurndal - Wed, 14 Jun 2023 15:14 UTC

David Brown <david.brown@hesbynett.no> writes:
>On 14/06/2023 15:31, Bonita Montero wrote:
>> Am 14.06.2023 um 13:06 schrieb David Brown:
>>
>>> Note that this applies only to specific implementations of memcpy(). ...
>>
>> memcpy() is the only way to reliably alias something in C++ and
>> therefore it's safe to assume that it is partitially intrinsic.
>
>That's not what "intrinsic" means. An "intrinsic" is basically a
>wrapper function around a cpu-specific feature or instruction.

FWIW, the HP-3000 MPE operating system interfaces (system calls) were called
"intrinsics".

Re: Strange optimization

<u6cm5p$26u1$1@dont-email.me>

  copy mid

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

  copy link   Newsgroups: comp.lang.c++
Path: i2pn2.org!i2pn.org!eternal-september.org!news.eternal-september.org!.POSTED!not-for-mail
From: Bonita.M...@gmail.com (Bonita Montero)
Newsgroups: comp.lang.c++
Subject: Re: Strange optimization
Date: Wed, 14 Jun 2023 17:25:46 +0200
Organization: A noiseless patient Spider
Lines: 14
Message-ID: <u6cm5p$26u1$1@dont-email.me>
References: <u6acl4$3m5sb$1@dont-email.me> <kerr9cF31i7U1@mid.individual.net>
<u6bpr3$3v0ra$1@dont-email.me> <u6c6vf$hh4$1@dont-email.me>
<u6cffm$1efd$1@dont-email.me> <u6cj8u$1sek$1@dont-email.me>
<3PkiM.61009$hl93.4930@fx18.iad>
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8; format=flowed
Content-Transfer-Encoding: 7bit
Injection-Date: Wed, 14 Jun 2023 15:25:45 -0000 (UTC)
Injection-Info: dont-email.me; posting-host="21067f4b658527782aa5be2eecdd29ac";
logging-data="72641"; mail-complaints-to="abuse@eternal-september.org"; posting-account="U2FsdGVkX19gtAelP+FWKfGAuGGUJXciCnBh1hrenHY="
User-Agent: Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:102.0) Gecko/20100101
Thunderbird/102.12.0
Cancel-Lock: sha1:CIOmCTQXAluRUoGdNk2lOqE+mYs=
Content-Language: de-DE
In-Reply-To: <3PkiM.61009$hl93.4930@fx18.iad>
 by: Bonita Montero - Wed, 14 Jun 2023 15:25 UTC

Am 14.06.2023 um 17:14 schrieb Scott Lurndal:

> FWIW, the HP-3000 MPE operating system interfaces (system calls) were called
> "intrinsics".

An intrinsic is a function that directly maps to a processor instruc-
tion. That's holds true for memcpy() with small static sizes because
memcpy() is too important to be mapped to a library call for that.
Because if you're pedantic memcpy() is the only safe way to have
aliasing in C++.
In C you can alias anything as a char array and a char array as
anything so it would be safe to alias anything as anything with
double-casting (I guess that's correctly supported by the compilers).

Re: Strange optimization

<keliM.13354$NuA9.6762@fx03.iad>

  copy mid

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

  copy link   Newsgroups: comp.lang.c++
Path: i2pn2.org!i2pn.org!usenet.blueworldhosting.com!diablo1.usenet.blueworldhosting.com!peer03.iad!feed-me.highwinds-media.com!news.highwinds-media.com!fx03.iad.POSTED!not-for-mail
X-newsreader: xrn 9.03-beta-14-64bit
Sender: scott@dragon.sl.home (Scott Lurndal)
From: sco...@slp53.sl.home (Scott Lurndal)
Reply-To: slp53@pacbell.net
Subject: Re: Strange optimization
Newsgroups: comp.lang.c++
References: <u6acl4$3m5sb$1@dont-email.me> <kerr9cF31i7U1@mid.individual.net> <u6bpr3$3v0ra$1@dont-email.me> <u6c6vf$hh4$1@dont-email.me> <u6cffm$1efd$1@dont-email.me> <u6cj8u$1sek$1@dont-email.me> <3PkiM.61009$hl93.4930@fx18.iad> <u6cm5p$26u1$1@dont-email.me>
Lines: 11
Message-ID: <keliM.13354$NuA9.6762@fx03.iad>
X-Complaints-To: abuse@usenetserver.com
NNTP-Posting-Date: Wed, 14 Jun 2023 15:43:12 UTC
Organization: UsenetServer - www.usenetserver.com
Date: Wed, 14 Jun 2023 15:43:12 GMT
X-Received-Bytes: 1141
 by: Scott Lurndal - Wed, 14 Jun 2023 15:43 UTC

Bonita Montero <Bonita.Montero@gmail.com> writes:
>Am 14.06.2023 um 17:14 schrieb Scott Lurndal:
>
>> FWIW, the HP-3000 MPE operating system interfaces (system calls) were called
>> "intrinsics".
>
>An intrinsic is a function that directly maps to a processor instruc-
>tion.

You don't get to unilateraly determine anything, much less what an
"intrinsic" is.

Re: Strange optimization

<u6d5dr$3rml$1@dont-email.me>

  copy mid

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

  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.st...@gmail.com (Alf P. Steinbach)
Newsgroups: comp.lang.c++
Subject: Re: Strange optimization
Date: Wed, 14 Jun 2023 21:46:02 +0200
Organization: A noiseless patient Spider
Lines: 42
Message-ID: <u6d5dr$3rml$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>
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8; format=flowed
Content-Transfer-Encoding: 7bit
Injection-Date: Wed, 14 Jun 2023 19:46:03 -0000 (UTC)
Injection-Info: dont-email.me; posting-host="5b4d584567085f50e3eea1a6bb67d902";
logging-data="126677"; mail-complaints-to="abuse@eternal-september.org"; posting-account="U2FsdGVkX1/R0KHjTyZnDMZVguXB/eDC"
User-Agent: Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:102.0) Gecko/20100101
Thunderbird/102.12.0
Cancel-Lock: sha1:AVrJJfDw3ZxD0QOQxSFk+ySngpY=
In-Reply-To: <u6bkqb$3ufs0$1@dont-email.me>
Content-Language: en-US
 by: Alf P. Steinbach - Wed, 14 Jun 2023 19:46 UTC

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.

When that function is separately compiled in a different translation
unit, how is the compiler to know when compiling calling code that the
function uses `memcpy` internally,

and when compiling the function, how is the compiler to know that it's
generally called with a `*reinterpret_cast<uint64_t*>( p_bytes )` as
argument?

Answer: it doesn't know, in either case.

So generally (let's disregard global optimization with link time
compilation) `memcpy` versus `=` can't affect the outcome.

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.
Even g++'s documentation of `-fstrict-aliasing` says "A character type
may alias any other type.". So g++ is /not/ that perverse a compiler,
and I believe you won't find any sufficiently perverse compiler.

So in practice one can and should not accommodate the common standard
interpretation; it's nonsense, built on the assumption of magic effects.

Let the committee come up with their COBOL-inspired C++26 formal
solution. Let others waste their time trying to write then formally
correct COBOL++. I.e. just ignore all that; say no to nonsense.

- Alf

Re: Strange optimization

<u6d5r1$3sea$1@dont-email.me>

  copy mid

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

  copy link   Newsgroups: comp.lang.c++
Path: i2pn2.org!i2pn.org!eternal-september.org!news.eternal-september.org!.POSTED!not-for-mail
From: Bonita.M...@gmail.com (Bonita Montero)
Newsgroups: comp.lang.c++
Subject: Re: Strange optimization
Date: Wed, 14 Jun 2023 21:53:07 +0200
Organization: A noiseless patient Spider
Lines: 15
Message-ID: <u6d5r1$3sea$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>
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8; format=flowed
Content-Transfer-Encoding: 7bit
Injection-Date: Wed, 14 Jun 2023 19:53:05 -0000 (UTC)
Injection-Info: dont-email.me; posting-host="21067f4b658527782aa5be2eecdd29ac";
logging-data="127434"; mail-complaints-to="abuse@eternal-september.org"; posting-account="U2FsdGVkX19ukgbm0xWZXvteT2f2J+uIfrhabPgf2SQ="
User-Agent: Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:102.0) Gecko/20100101
Thunderbird/102.12.0
Cancel-Lock: sha1:Lp4fiWI9y0FHOtszdWR+1egGccE=
Content-Language: de-DE
In-Reply-To: <u6d5dr$3rml$1@dont-email.me>
 by: Bonita Montero - Wed, 14 Jun 2023 19:53 UTC

Am 14.06.2023 um 21:46 schrieb Alf P. Steinbach:

> When that function is separately compiled in a different translation
> unit, how is the compiler to know when compiling calling code that the
> function uses `memcpy` internally,

It doesn't matter if my function is inlined or not.

> So generally (let's disregard global optimization with link
> time compilation) `memcpy` versus `=` can't affect the outcome.

Thats not safe if you do aliasing.

Rest unread.

Re: Strange optimization

<u6dbee$4cof$1@dont-email.me>

  copy mid

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

  copy link   Newsgroups: comp.lang.c++
Path: i2pn2.org!i2pn.org!eternal-september.org!news.eternal-september.org!.POSTED!not-for-mail
From: david.br...@hesbynett.no (David Brown)
Newsgroups: comp.lang.c++
Subject: Re: Strange optimization
Date: Wed, 14 Jun 2023 23:28:46 +0200
Organization: A noiseless patient Spider
Lines: 70
Message-ID: <u6dbee$4cof$1@dont-email.me>
References: <u6acl4$3m5sb$1@dont-email.me> <kerr9cF31i7U1@mid.individual.net>
<u6bpr3$3v0ra$1@dont-email.me> <u6c6vf$hh4$1@dont-email.me>
<u6cffm$1efd$1@dont-email.me> <u6cj8u$1sek$1@dont-email.me>
<u6cjrm$1ts6$1@dont-email.me>
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8; format=flowed
Content-Transfer-Encoding: 8bit
Injection-Date: Wed, 14 Jun 2023 21:28:46 -0000 (UTC)
Injection-Info: dont-email.me; posting-host="5366c05854848f828fcc598a2d029cef";
logging-data="144143"; mail-complaints-to="abuse@eternal-september.org"; posting-account="U2FsdGVkX1/2sLOEY9EL64A5V1/Jvfe0ugCVQtwMsKU="
User-Agent: Mozilla/5.0 (X11; Linux x86_64; rv:102.0) Gecko/20100101
Thunderbird/102.7.1
Cancel-Lock: sha1:s9YiJZ6wZ8nYFq3u7UMig+73BXE=
In-Reply-To: <u6cjrm$1ts6$1@dont-email.me>
Content-Language: en-GB
 by: David Brown - Wed, 14 Jun 2023 21:28 UTC

On 14/06/2023 16:46, Bonita Montero wrote:
> Am 14.06.2023 um 16:36 schrieb David Brown:
>
>> On 14/06/2023 15:31, Bonita Montero wrote:
>
>>> memcpy() is the only way to reliably alias something in C++ and
>>> therefore it's safe to assume that it is partitially intrinsic.
>
>> That's not what "intrinsic" means. <rest unread>
>
> For me it's partitially intrinsic.

Please don't use words like that in totally different ways from everyone
else.

>
>> It is not long since memcpy() on MSVC was always implemented as a call
>> to an external library function in a DLL, which was a huge overhead
>> for such a simple feature.
>
> Since it's the only way to have safe aliasing in C++ you can silently
> rely on that.
>

You said that before - you are still wrong.

>> And no, "memcpy" is /not/ the only way to access aliased data or
>> access data through pointers of non-compatible types.  You can use any
>> character pointer, ...
>
> In C you can alias anything as a char-array and vise versa,
> but not in C++.

<https://en.cppreference.com/w/cpp/language/reinterpret_cast#Type_aliasing>

<https://en.cppreference.com/w/cpp/types/byte>

Look it up in the standards if you prefer.

>
>> You did not check gcc.
>
> I checked it and this trick doesn't work with g++ / RISC-V
> _with_ -O2 but I hope this will change, so I check for __GNUC__.
>

Finally you have managed to check it, and found - as I told you at the
start - that it does not work for -O2. (Not for RISC-V, and not for any
other target that makes a distinction between aligned and unaligned
accesses.)

While I too hope this will change (that's why I registered it as a bug),
the sane thing to do is write the code properly using the correct gcc
extension for the purpose - __builtin_assume_aligned - as I suggested.

>> It is still not the appropriate type. ...
>
> For me it is because I use systems with flat memory. And in this case
> I could even use a char since I only check for the three lower bits to
> be zero.
>

No, it is still the wrong type regardless of the memory flatness.

It is really very simple. "size_t" is a type used for the size of
objects. "uintptr_t" is a type used for converting pointers to unsigned
integers. Use the correct type. You have /nothing/ to gain by using
the wrong type here other than a reputation for being stubborn in your
insistence on writing pointlessly odd code.

Re: Strange optimization

<u6dbmi$4cof$2@dont-email.me>

  copy mid

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

  copy link   Newsgroups: comp.lang.c++
Path: i2pn2.org!i2pn.org!eternal-september.org!news.eternal-september.org!.POSTED!not-for-mail
From: david.br...@hesbynett.no (David Brown)
Newsgroups: comp.lang.c++
Subject: Re: Strange optimization
Date: Wed, 14 Jun 2023 23:33:06 +0200
Organization: A noiseless patient Spider
Lines: 25
Message-ID: <u6dbmi$4cof$2@dont-email.me>
References: <u6acl4$3m5sb$1@dont-email.me> <kerr9cF31i7U1@mid.individual.net>
<u6bpr3$3v0ra$1@dont-email.me> <u6c6vf$hh4$1@dont-email.me>
<u6cffm$1efd$1@dont-email.me> <u6cj8u$1sek$1@dont-email.me>
<3PkiM.61009$hl93.4930@fx18.iad>
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8; format=flowed
Content-Transfer-Encoding: 7bit
Injection-Date: Wed, 14 Jun 2023 21:33:07 -0000 (UTC)
Injection-Info: dont-email.me; posting-host="5366c05854848f828fcc598a2d029cef";
logging-data="144143"; mail-complaints-to="abuse@eternal-september.org"; posting-account="U2FsdGVkX1/s6RCfdVcGUDM8nRu/7kD/w5ChIkMITWM="
User-Agent: Mozilla/5.0 (X11; Linux x86_64; rv:102.0) Gecko/20100101
Thunderbird/102.7.1
Cancel-Lock: sha1:oHQIOxKvlDyaKhxEnruoaWQXQxM=
In-Reply-To: <3PkiM.61009$hl93.4930@fx18.iad>
Content-Language: en-GB
 by: David Brown - Wed, 14 Jun 2023 21:33 UTC

On 14/06/2023 17:14, Scott Lurndal wrote:
> David Brown <david.brown@hesbynett.no> writes:
>> On 14/06/2023 15:31, Bonita Montero wrote:
>>> Am 14.06.2023 um 13:06 schrieb David Brown:
>>>
>>>> Note that this applies only to specific implementations of memcpy(). ...
>>>
>>> memcpy() is the only way to reliably alias something in C++ and
>>> therefore it's safe to assume that it is partitially intrinsic.
>>
>> That's not what "intrinsic" means. An "intrinsic" is basically a
>> wrapper function around a cpu-specific feature or instruction.
>
> FWIW, the HP-3000 MPE operating system interfaces (system calls) were called
> "intrinsics".

It is not uncommon for system calls to be handled by embedded assembly
instructions for software interrupts. You could also argue that the
system calls are native to the target but not to the C (or C++)
language, just like assembly instructions, and can justifiably be called
"intrinsics".

"memcpy", on the other hand, is no more "intrinsic" than any other
standard library function.

Re: Strange optimization

<u6desf$4oa4$1@dont-email.me>

  copy mid

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

  copy link   Newsgroups: comp.lang.c++
Path: i2pn2.org!i2pn.org!eternal-september.org!news.eternal-september.org!.POSTED!not-for-mail
From: jameskuy...@alumni.caltech.edu (James Kuyper)
Newsgroups: comp.lang.c++
Subject: Re: Strange optimization
Date: Wed, 14 Jun 2023 18:27:26 -0400
Organization: A noiseless patient Spider
Lines: 49
Message-ID: <u6desf$4oa4$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>
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8
Content-Transfer-Encoding: 8bit
Injection-Date: Wed, 14 Jun 2023 22:27:27 -0000 (UTC)
Injection-Info: dont-email.me; posting-host="dad1b356a67cf6eba3a4c0e5b993bee6";
logging-data="155972"; mail-complaints-to="abuse@eternal-september.org"; posting-account="U2FsdGVkX19HdocL8ANXeEj0z2J9zE4sHZ2zBVOklCk="
User-Agent: Mozilla/5.0 (X11; Linux x86_64; rv:102.0) Gecko/20100101
Thunderbird/102.11.0
Cancel-Lock: sha1:wXUX19C1DMMKtdvQucSz0rmnKqw=
In-Reply-To: <u6d5dr$3rml$1@dont-email.me>
Content-Language: en-US
 by: James Kuyper - Wed, 14 Jun 2023 22:27 UTC

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<>.

"When a prvalue v of object pointer type is converted to the object
pointer type “pointer to cv T”, the result is
static_cast<cv T*>(static_cast<cv void*>(v))." (7.6.1.9p7)

"A prvalue of type “pointer to cv1 void” can be converted to a prvalue
of type “pointer to cv2 T”, where T is an object type and cv2 is the
same cv-qualification as, or greater cv-qualification than, cv1. If the
original pointer value represents the address A of a byte in memory and
A does not satisfy the alignment requirement of T, then the resulting
pointer value is unspecified." (7.6.1.8p13).

In particular, because the pointer value is unspecified, it's not
guaranteed to be dereferenceable. A common possibility is that the
result of the conversion will be a pointer to the nearest preceding or
following correctly aligned location.

If p_bytes is correctly aligned, simple assignment will work just as
well as memcpy().

> 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 aliases a
character type with uint64_t, and that a problem if pbytes is not
correctly aligned to hold a uint64_t. The anti-aliasing rules are not
symmetric.

Re: Strange optimization

<gtriM.1556$zcM5.1256@fx11.iad>

  copy mid

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

  copy link   Newsgroups: comp.lang.c++
Path: i2pn2.org!i2pn.org!usenet.blueworldhosting.com!diablo1.usenet.blueworldhosting.com!peer03.iad!feed-me.highwinds-media.com!news.highwinds-media.com!fx11.iad.POSTED!not-for-mail
X-newsreader: xrn 9.03-beta-14-64bit
Sender: scott@dragon.sl.home (Scott Lurndal)
From: sco...@slp53.sl.home (Scott Lurndal)
Reply-To: slp53@pacbell.net
Subject: Re: Strange optimization
Newsgroups: comp.lang.c++
References: <u6acl4$3m5sb$1@dont-email.me> <kerr9cF31i7U1@mid.individual.net> <u6bpr3$3v0ra$1@dont-email.me> <u6c6vf$hh4$1@dont-email.me> <u6cffm$1efd$1@dont-email.me> <u6cj8u$1sek$1@dont-email.me> <3PkiM.61009$hl93.4930@fx18.iad> <u6dbmi$4cof$2@dont-email.me>
Lines: 26
Message-ID: <gtriM.1556$zcM5.1256@fx11.iad>
X-Complaints-To: abuse@usenetserver.com
NNTP-Posting-Date: Wed, 14 Jun 2023 22:48:44 UTC
Organization: UsenetServer - www.usenetserver.com
Date: Wed, 14 Jun 2023 22:48:44 GMT
X-Received-Bytes: 1844
 by: Scott Lurndal - Wed, 14 Jun 2023 22:48 UTC

David Brown <david.brown@hesbynett.no> writes:
>On 14/06/2023 17:14, Scott Lurndal wrote:
>> David Brown <david.brown@hesbynett.no> writes:
>>> On 14/06/2023 15:31, Bonita Montero wrote:
>>>> Am 14.06.2023 um 13:06 schrieb David Brown:
>>>>
>>>>> Note that this applies only to specific implementations of memcpy(). ...
>>>>
>>>> memcpy() is the only way to reliably alias something in C++ and
>>>> therefore it's safe to assume that it is partitially intrinsic.
>>>
>>> That's not what "intrinsic" means. An "intrinsic" is basically a
>>> wrapper function around a cpu-specific feature or instruction.
>>
>> FWIW, the HP-3000 MPE operating system interfaces (system calls) were called
>> "intrinsics".
>
>It is not uncommon for system calls to be handled by embedded assembly
>instructions for software interrupts.

In this case, it was more generic than that, since:

1) The HP-3000 didn't have an assembler
2) Intrinsic calls were just function calls into a different segment
(a stack architecture similar to the Burroughs systems).

Re: Strange optimization

<u6dgsr$4ums$2@dont-email.me>

  copy mid

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

  copy link   Newsgroups: comp.lang.c++
Path: i2pn2.org!i2pn.org!eternal-september.org!news.eternal-september.org!.POSTED!not-for-mail
From: chris.m....@gmail.com (Chris M. Thomasson)
Newsgroups: comp.lang.c++
Subject: Re: Strange optimization
Date: Wed, 14 Jun 2023 16:01:46 -0700
Organization: A noiseless patient Spider
Lines: 28
Message-ID: <u6dgsr$4ums$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>
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8; format=flowed
Content-Transfer-Encoding: 7bit
Injection-Date: Wed, 14 Jun 2023 23:01:47 -0000 (UTC)
Injection-Info: dont-email.me; posting-host="bc222fdc488b0299ae962f359e97db3d";
logging-data="162524"; mail-complaints-to="abuse@eternal-september.org"; posting-account="U2FsdGVkX18jAkD127UVdLBFWJeaGfqRYIUxRvIXotE="
User-Agent: Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:102.0) Gecko/20100101
Thunderbird/102.12.0
Cancel-Lock: sha1:VyoozU7yrXEzRTaS/SIQItsJoo8=
Content-Language: en-US
In-Reply-To: <u6desf$4oa4$1@dont-email.me>
 by: Chris M. Thomasson - Wed, 14 Jun 2023 23:01 UTC

On 6/14/2023 3:27 PM, 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<>.
[...]

What about some code that crosses a L2 cache line boundary and causes
the damn processor to assert a bus lock... Argh!

Re: Strange optimization

<u6egi1$bn1q$1@dont-email.me>

  copy mid

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

  copy link   Newsgroups: comp.lang.c++
Path: i2pn2.org!i2pn.org!eternal-september.org!news.eternal-september.org!.POSTED!not-for-mail
From: david.br...@hesbynett.no (David Brown)
Newsgroups: comp.lang.c++
Subject: Re: Strange optimization
Date: Thu, 15 Jun 2023 10:02:09 +0200
Organization: A noiseless patient Spider
Lines: 36
Message-ID: <u6egi1$bn1q$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>
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8; format=flowed
Content-Transfer-Encoding: 7bit
Injection-Date: Thu, 15 Jun 2023 08:02:09 -0000 (UTC)
Injection-Info: dont-email.me; posting-host="fff2e5cb247c5ed47788381bd530146c";
logging-data="384058"; mail-complaints-to="abuse@eternal-september.org"; posting-account="U2FsdGVkX1+1zxsxDZ/cjCh8jqGaBKTRMLL9EGi2JrI="
User-Agent: Mozilla/5.0 (X11; Linux x86_64; rv:102.0) Gecko/20100101
Thunderbird/102.9.0
Cancel-Lock: sha1:Umy+bp4bckGjHjkfAZA3rbZJC3I=
In-Reply-To: <u6desf$4oa4$1@dont-email.me>
Content-Language: en-GB
 by: David Brown - Thu, 15 Jun 2023 08:02 UTC

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.

Re: Strange optimization

<u6egpk$bn1q$2@dont-email.me>

  copy mid

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

  copy link   Newsgroups: comp.lang.c++
Path: i2pn2.org!i2pn.org!eternal-september.org!news.eternal-september.org!.POSTED!not-for-mail
From: david.br...@hesbynett.no (David Brown)
Newsgroups: comp.lang.c++
Subject: Re: Strange optimization
Date: Thu, 15 Jun 2023 10:06:12 +0200
Organization: A noiseless patient Spider
Lines: 34
Message-ID: <u6egpk$bn1q$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>
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8; format=flowed
Content-Transfer-Encoding: 7bit
Injection-Date: Thu, 15 Jun 2023 08:06:12 -0000 (UTC)
Injection-Info: dont-email.me; posting-host="fff2e5cb247c5ed47788381bd530146c";
logging-data="384058"; mail-complaints-to="abuse@eternal-september.org"; posting-account="U2FsdGVkX19AVNLwWIYbieNEfWzZmvBb7IvI05Iw510="
User-Agent: Mozilla/5.0 (X11; Linux x86_64; rv:102.0) Gecko/20100101
Thunderbird/102.9.0
Cancel-Lock: sha1:ScQsLhqgzhwVk6ZtoTbAF6Y06dM=
In-Reply-To: <u6d5dr$3rml$1@dont-email.me>
Content-Language: en-GB
 by: David Brown - Thu, 15 Jun 2023 08:06 UTC

On 14/06/2023 21: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.
>
> When that function is separately compiled in a different translation
> unit, how is the compiler to know when compiling calling code that the
> function uses `memcpy` internally,
>
> and when compiling the function, how is the compiler to know that it's
> generally called with a `*reinterpret_cast<uint64_t*>( p_bytes )` as
> argument?
>
> Answer: it doesn't know, in either case.
>
> So generally (let's disregard global optimization with link time
> compilation) `memcpy` versus `=` can't affect the outcome.
>
> 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`.
>

Code that relies on limited optimisation or separate compilation for
correct behaviour, is an extremely bad idea - it is fragile and a hidden
bug waiting to explode in the future. Shortcuts now will cost dearly
later on. Take pride in your work, and code responsibly - do what you
can to make your code /correct/, rather than relying on weak tools!


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

Pages:123
server_pubkey.txt

rocksolid light 0.9.81
clearnet tor