繁体   English   中英

结构体在 ; 之前有联合是什么意思?

[英]What does it mean for a struct to have an union before the ;?

正如您在PointHVDIR结构中PointHVDIR ,它以EIGEN_ALIGN16

#define PCL_ADD_UNION_POINT4D_HVD       \
  union EIGEN_ALIGN16 {                 \
    float data[4];                      \
    struct {                            \
      float h;                          \
      float v;                          \
      float d;                          \
    };                                  \
  };

  struct PointHVDIR
  {
    PCL_ADD_POINT4D_HVD;                    // quad-word HVD
    float    intensity;                 ///< laser intensity reading
    uint16_t ring;                      ///< laser ring number
    EIGEN_MAKE_ALIGNED_OPERATOR_NEW     // ensure proper alignment
  } EIGEN_ALIGN16;

这是什么意思?

这似乎来自 Eigen 代码库? 一些快速搜索发现以下内容。

https://gitlab.com/libeigen/eigen/blob/master/Eigen/src/Core/util/ConfigureVectorization.h

/* EIGEN_ALIGN_TO_BOUNDARY(n) forces data to be n-byte aligned. This is used to satisfy SIMD requirements.
 * However, we do that EVEN if vectorization (EIGEN_VECTORIZE) is disabled,
 * so that vectorization doesn't affect binary compatibility.
 *
 * If we made alignment depend on whether or not EIGEN_VECTORIZE is defined, it would be impossible to link
 * vectorized and non-vectorized code.
 * 
 * FIXME: this code can be cleaned up once we switch to proper C++11 only.
 */

// (code defining EIGEN_ALIGN_TO_BOUNDARY and other things here...)

// Shortcuts to EIGEN_ALIGN_TO_BOUNDARY
#define EIGEN_ALIGN8  EIGEN_ALIGN_TO_BOUNDARY(8)
#define EIGEN_ALIGN16 EIGEN_ALIGN_TO_BOUNDARY(16)
#define EIGEN_ALIGN32 EIGEN_ALIGN_TO_BOUNDARY(32)
#define EIGEN_ALIGN64 EIGEN_ALIGN_TO_BOUNDARY(64)

本质上,它似乎是在指定 struct 在 16 字节边界上对齐。

union在你的榜样,那么是不是一个名为工会EIGEN_ALIGN16 相反,它是一个匿名的 16 字节对齐联合的#define ,然后作为结构的成员包含在内。

这将归结为数据结构对齐。 在某些平台上将结构数据对齐到 16、32 或 64 字节可能是有利的(因此您可以执行对齐的 SIMD 读/写)。 这就是宏的用武之地。从 C++11 开始,您可以使用 alignas() 运算符来执行此操作:

struct alignas(16) Vec4 
{
  float x,y,z,w;
};

但是,在 C++11 之前,您必须使用特定于编译器的扩展来对齐数据类型,例如使用 Visual C++:

struct __declspec(align(16)) Vec4 
{
  float x,y,z,w;
};

或使用 gcc/linux。

struct __attribute__((aligned(16))) Vec4 
{
  float x,y,z,w;
};

为了解决这个问题,大多数库最终都会将这些变体包装在一个宏中:

#if __cplusplus >= 201103
# define ALIGN(X) alignas(x)
#elif defined(_MSC_VER)
# define ALIGN(X) __declspec(align(16)) 
#else
# define ALIGN(X) __attribute__((aligned(16)))
#endif

这导致结构定义如下所示:

struct ALIGN(16) Vec4 
{
  float x,y,z,w;
};

在上述代码的情况下,由于外部 PointHVDIR 结构已被标记为 16 字节对齐,因此对齐匿名联合也是多余的。 可以通过以下方式使代码更具可读性:

#define PCL_ADD_UNION_POINT4D_HVD       \
  union {                               \
    float data[4];                      \
    struct {                            \
      float h;                          \
      float v;                          \
      float d;                          \
    };                                  \
  };

  struct PointHVDIR
  {
    PCL_ADD_POINT4D_HVD;           
    float    intensity;            
    uint16_t ring;                 
    EIGEN_MAKE_ALIGNED_OPERATOR_NEW //< ensure alignment when calling new
  } EIGEN_ALIGN16; //< ensure alignment within structs, and on the stack. 

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM