英特尔 Embree 中的这种联合有什么作用?
What does this union in Intel's Embree do?
这是来自英特尔的Embree代码中的vec3fa.h。
struct __aligned(16) Vec3fa
{
typedef float Scalar;
enum { N = 3 };
union {
__m128 m128;
struct { float x,y,z; union { int a; float w; }; };
};
// other stuff in struct
};
外部联合在做什么?内在的结合对我来说更神秘。a和w变量在代码中从未被引用。
看起来这提供了一种方便和干净的方式,可以使用适当的别名读写m128、x、y和z。它是如何工作的?
int型是怎么进来的??
这是匿名联合(和一个结构体)。它们所做的是就地定义结构体或联合的匿名实例,并用于在访问成员时避免混乱。以上代码的布局与下面的代码兼容:
struct __aligned(16) Vec3fa
{
typedef float Scalar;
enum { N = 3 };
union {
__m128 m128;
struct { float x,y,z; union { int a; float w; } u2; } s;
} u1;
// other stuff in struct
};
但是现在成员访问更复杂了:
Vec3fa v; // offset from struct start ((char*)&member - (char*)&v):
v.u1.m128; // 0
v.u1.s.x; // 0
v.u1.s.y; // 4
v.u1.s.z; // 8
v.u1.s.u2.w; // 12
v.u1.s.u2.a; // 12
代替库变量:
Vec3fa v; // offset from struct start ((char*)&member - (char*)&v):
v.m128; // 0
v.x; // 0
v.y; // 4
v.z; // 8
v.w; // 12
v.a; // 12
int型是怎么进来的??
英特尔的Embree是一个光线跟踪内核库。在计算机图形学中,您可以想象有时需要使用4元素向量来表示颜色和alpha,或者使用齐次坐标表示位置。
https://en.wikipedia.org/wiki/RGBA_color_spacehttps://en.wikipedia.org/wiki/Homogeneous_coordinates
相关文章:
- archive_read_support_filter_all() 有什么作用?
- 当我们为(;;) 写作时,它做了什么?for 循环中的双分号有什么作用?
- 这个递归函数有什么作用?运行时的复杂性是多少?
- DrawShadow 中的 Elevation 究竟有什么作用?
- 测试"stream >> char"有什么作用?
- "uFlags &= ~CHN_PANNING"命令有什么作用?
- GCC 的 -Wpsabi 选项究竟有什么作用?压制它意味着什么?
- int** a = new int*[n]();这个函数有什么作用?
- C++ 此函数调用之前的(void)有什么作用?
- i+=(i&-i) 有什么作用?它是便携式的吗?
- 这个size_t在 lambda 中有什么作用?C++代码
- 数组下标运算符 ([ ]) 对数组有什么作用?
- 结构之外的节点 * head 有什么作用?
- 重载括号运算符有什么作用?
- clang的"-Ofast"选项在实践中有什么作用,特别是对于与gcc的任何差异?
- std::make_unique_for_overwrite() 对 std::make_unique() 有什么作用?
- 运算符 delete[] 有什么作用?
- 下面的代码有什么作用?c++
- 这个陈述'p = I.ptr<uchar>(i);有什么作用?
- 这个嵌套的名称空间定义有什么作用