タグ付けされた質問 「type-punning」


15
CおよびC ++での共用体の目的
私は以前にユニオンを快適に使用しました。今日、私はこの投稿を読んで、このコードが union ARGB { uint32_t colour; struct componentsTag { uint8_t b; uint8_t g; uint8_t r; uint8_t a; } components; } pixel; pixel.colour = 0xff040201; // ARGB::colour is the active member from now on // somewhere down the line, without any edit to pixel if(pixel.components.a) // accessing the non-active member ARGB::components …
254 c++  c  unions  type-punning 

3
RustでQuakeの高速InvSqrt()関数を書くことは可能ですか?
これは私自身の好奇心を満たすためです。 これの実装はありますか: float InvSqrt (float x) { float xhalf = 0.5f*x; int i = *(int*)&x; i = 0x5f3759df - (i>>1); x = *(float*)&i; x = x*(1.5f - xhalf*x*x); return x; } ルストで?存在する場合は、コードを投稿してください。 私はそれを試して失敗しました。整数形式を使用して浮動小数点数をエンコードする方法がわかりません。これが私の試みです: fn main() { println!("Hello, world!"); println!("sqrt1: {}, ",sqrt2(100f64)); } fn sqrt1(x: f64) -> f64 { x.sqrt() } …

3
std :: arrayを使用したstd :: bit_cast
彼の最近の講演で、「++現代Cでタイプpunning」ティムールDoumlerは言ったそのstd::bit_castビットを鋳造するために使用することはできませんfloatにunsigned char[4] Cスタイルの配列を関数から返すことができないため。のstd::memcpyようなものreinterpret_cast<unsigned char*>(&f)[i]が明確になるときは、C ++ 23(以降)を使用するか待つ必要があります。 C ++ 20では、std::arraywith std::bit_cast、 float f = /* some value */; auto bits = std::bit_cast<std::array<unsigned char, sizeof(float)>>(f); Cスタイルの配列の代わりにfloat?
弊社のサイトを使用することにより、あなたは弊社のクッキーポリシーおよびプライバシーポリシーを読み、理解したものとみなされます。
Licensed under cc by-sa 3.0 with attribution required.