Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]
Groups > comp.lang.c++ > #84373
| From | David Brown <david.brown@hesbynett.no> |
|---|---|
| Newsgroups | comp.lang.c++, comp.lang.c, de.comp.lang.c |
| Subject | Re: Fast trunc() algorithm |
| Date | 2022-05-30 08:59 +0200 |
| Organization | A noiseless patient Spider |
| Message-ID | <t71pvp$d5o$1@dont-email.me> (permalink) |
| References | <t6va0v$eno$1@dont-email.me> <t70p0f$a8$1@dont-email.me> <t71nqf$6p4$1@gioia.aioe.org> |
Cross-posted to 3 groups.
On 30/05/2022 08:22, Juha Nieminen wrote: > In comp.lang.c++ Christian Gollwitzer <auriocus@gmx.de> wrote: >> Am 29.05.22 um 10:14 schrieb Bonita Montero: >>> >>> memcpy() is the only valid way to have writeable aliasing in C++. >>> memcpy() is normally an intrinsic function and in the above code >>> the memcpy()s between integral and floating point types are just >>> moves between general purpose and FPU-registers. >> >> How about reinterpret_cast<> ? > > Doing type punning, ie. interpreting the bit representation of some type > as a value of an incompatible type, by reinterpret-casting a pointer to > that value, or by using a union, is technically speaking UB in C++ > (but allowed in C, I think since C11 or the like), even though in > practice it does work (for the same reason it does in C). Using unions for type-punning has, AFAIUI, always been defined behaviour in C. The wording was changed in C99 to make it clear that it is allowed, but the way the change was done (as a footnote) indicates that this was meant as a clarification, not a change to the language. In C++, however, using unions for type-punning has always been UB. (This can be viewed as an indication that C++ is more strongly typed than C.) (You are of course correct that reinterpret_cast can't be used for accessing one type as an incompatible type.) > The only "official" way of doing it (prior to C++20) was to use > std::memcpy() (and hope that the compiler will optimize it into > equivalent code as using a union). I believe you could also write your own memcpy version - using char or unsigned char to access the data, or std::byte in C++17. (You might want that if you have a poorly optimising compiler that uses a library call for memcpy even in simple cases.) > > C++20 added an official way of doing type punning: std::bit_cast. Yes.
Back to comp.lang.c++ | Previous | Next — Previous in thread | Next in thread | Find similar | Unroll thread
Fast trunc() algorithm Bonita Montero <Bonita.Montero@gmail.com> - 2022-05-29 10:14 +0200
Re: Fast trunc() algorithm Muttley@dastardlyhq.com - 2022-05-29 08:21 +0000
Re: Fast trunc() algorithm Bonita Montero <Bonita.Montero@gmail.com> - 2022-05-29 10:29 +0200
Re: Fast trunc() algorithm Freethinker <freethinker@mymail.com> - 2022-05-29 15:20 +0200
Re: Fast trunc() algorithm Bonita Montero <Bonita.Montero@gmail.com> - 2022-05-29 15:27 +0200
Re: Fast trunc() algorithm Freethinker <freethinker@mymail.com> - 2022-05-29 15:37 +0200
Re: Fast trunc() algorithm Bonita Montero <Bonita.Montero@gmail.com> - 2022-05-29 15:46 +0200
Re: Fast trunc() algorithm John McCue <jmccue@magnetar.hsd1.ma.comcast.net> - 2022-05-29 21:32 +0000
Re: Fast trunc() algorithm Christian Gollwitzer <auriocus@gmx.de> - 2022-05-29 23:36 +0200
Re: Fast trunc() algorithm Juha Nieminen <nospam@thanks.invalid> - 2022-05-30 06:22 +0000
Re: Fast trunc() algorithm David Brown <david.brown@hesbynett.no> - 2022-05-30 08:59 +0200
Re: Fast trunc() algorithm muttley@dastardlyhq.com - 2022-05-30 07:52 +0000
Re: Fast trunc() algorithm Paavo Helde <eesnimi@osa.pri.ee> - 2022-05-30 11:58 +0300
Re: Fast trunc() algorithm Bonita Montero <Bonita.Montero@gmail.com> - 2022-05-30 11:32 +0200
Re: Fast trunc() algorithm Bonita Montero <Bonita.Montero@gmail.com> - 2022-05-30 11:34 +0200
Re: Fast trunc() algorithm Bonita Montero <Bonita.Montero@gmail.com> - 2022-05-31 05:41 +0200
Re: Fast trunc() algorithm Bonita Montero <Bonita.Montero@gmail.com> - 2022-06-01 18:07 +0200
Re: Fast trunc() algorithm Bonita Montero <Bonita.Montero@gmail.com> - 2022-06-01 18:09 +0200
Re: Fast trunc() algorithm Bonita Montero <Bonita.Montero@gmail.com> - 2022-06-02 04:43 +0200
Re: Fast trunc() algorithm jak <nospam@please.ty> - 2022-06-02 07:44 +0200
Re: Fast trunc() algorithm Bonita Montero <Bonita.Montero@gmail.com> - 2022-06-02 08:14 +0200
Re: Fast trunc() algorithm jak <nospam@please.ty> - 2022-06-02 11:14 +0200
Re: Fast trunc() algorithm Bonita Montero <Bonita.Montero@gmail.com> - 2022-06-02 12:48 +0200
csiph-web