{"id":4187,"date":"2019-03-14T10:31:52","date_gmt":"2019-03-14T09:31:52","guid":{"rendered":"http:\/\/roboblog.fatal-fury.de\/?p=4187"},"modified":"2019-03-14T10:49:12","modified_gmt":"2019-03-14T09:49:12","slug":"c-guns-floating-point-bit-round-p0476r2-bit-casting-object-representations","status":"publish","type":"post","link":"http:\/\/roboblog.fatal-fury.de\/?p=4187","title":{"rendered":"C++ Guns: floating point bit round - p0476r2 Bit-casting object representations"},"content":{"rendered":"<p>C++20<\/p>\n<blockquote><p>Low-level code often seeks to interpret objects of one type as another: keep the same bits, but obtain an object of a different type. Doing so correctly is error-prone: using reinterpret_cast or union runs afoul of type-aliasing rules yet these are the intuitive solutions developers mistakenly turn to.<\/p>\n<p>Attuned developers use aligned_storage with memcpy, avoiding alignment pitfalls and allowing them to bit-cast non-default-constructible types.<\/p>\n<p>This proposal uses appropriate concepts to prevent misuse. As the sample implementation demonstrates we could as well use static_assert or template SFINAE, but the timing of this library feature will likely coincide with concept\u2019s standardization.<\/p>\n<p>Furthermore, it is currently impossible to implement a constexpr bit-cast function, as memcpy itself isn\u2019t constexpr. Marking the proposed function as constexpr doesn\u2019t require or prevent memcpy from becoming constexpr, but requires compiler support. This leaves implementations free to use their own internal solution (e.g. LLVM has a bitcast opcode).<\/p>\n<p>We should standardize this oft-used idiom, and avoid the pitfalls once and for all.<\/p><\/blockquote>\n<p><a href=\"http:\/\/www.open-std.org\/jtc1\/sc22\/wg21\/docs\/papers\/2018\/p0476r2.html\">http:\/\/www.open-std.org\/jtc1\/sc22\/wg21\/docs\/papers\/2018\/p0476r2.html<\/a><br \/>\n<a href=\"https:\/\/github.com\/jfbastien\/bit_cast\/\">https:\/\/github.com\/jfbastien\/bit_cast\/<\/a><\/p>\n<p>Solange wir noch kein C++20 haben um Bit Manipulationen an Gleitkommazahlen vorzunehmen, muss memcpy herhalten.<\/p>\n<pre class=\"brush: cpp; title: ; notranslate\" title=\"\">\r\n  double value;\r\n\r\n  \/\/ float -&gt; integer \r\n  std::uint64_t n;\r\n  std::memcpy(&amp;n, &amp;value, sizeof(value)); \/\/ no aliasing violation\r\n\r\n  \/\/ Bit Manipulation an Variable 'n'\r\n  \/\/ ...\r\n\r\n  \/\/ integer -&gt; float\r\n  std::memcpy(&amp;value, &amp;n, sizeof(n));\r\n<\/pre>\n","protected":false},"excerpt":{"rendered":"<p>C++20 Low-level code often seeks to interpret objects of one type as another: keep the same bits, but obtain an object of a different type. Doing so correctly is error-prone: using reinterpret_cast or union runs afoul of type-aliasing rules yet these are the intuitive solutions developers mistakenly turn to. Attuned developers use aligned_storage with memcpy, [&hellip;]<\/p>\n","protected":false},"author":2,"featured_media":0,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[4],"tags":[17],"class_list":["post-4187","post","type-post","status-publish","format-standard","hentry","category-allgemein","tag-cpp"],"_links":{"self":[{"href":"http:\/\/roboblog.fatal-fury.de\/index.php?rest_route=\/wp\/v2\/posts\/4187","targetHints":{"allow":["GET"]}}],"collection":[{"href":"http:\/\/roboblog.fatal-fury.de\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"http:\/\/roboblog.fatal-fury.de\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"http:\/\/roboblog.fatal-fury.de\/index.php?rest_route=\/wp\/v2\/users\/2"}],"replies":[{"embeddable":true,"href":"http:\/\/roboblog.fatal-fury.de\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=4187"}],"version-history":[{"count":3,"href":"http:\/\/roboblog.fatal-fury.de\/index.php?rest_route=\/wp\/v2\/posts\/4187\/revisions"}],"predecessor-version":[{"id":4190,"href":"http:\/\/roboblog.fatal-fury.de\/index.php?rest_route=\/wp\/v2\/posts\/4187\/revisions\/4190"}],"wp:attachment":[{"href":"http:\/\/roboblog.fatal-fury.de\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=4187"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"http:\/\/roboblog.fatal-fury.de\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=4187"},{"taxonomy":"post_tag","embeddable":true,"href":"http:\/\/roboblog.fatal-fury.de\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=4187"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}