[工作积累] bitfield

ISO/IEC 14882:2003: 9.6 Bit-fields [class.bit]

  1. A member-declarator of the form identifieropt : constant-expression specifies a bit-field; its length is set off from the bit-field name by a colon. The bit-field attribute is not part of the type of the class member. The constant-expression shall be an integral constant-expression with a value greater than or equal to zero. The constant-expression may be larger than the number of bits in the object representation (3.9) of the bit-field’s type; in such cases the extra bits are used as padding bits and do not participate in the value representation (3.9) of the bit-field. Allocation of bit-fields within a class object is implementation-defined. Alignment of bit-fields is implementation-defined. Bit-fields are packed into some addressable allocation unit. [Note: bit-fields straddle allocation units on some machines and not on others. Bit-fields are assigned right-to-left on some machines, left-to-right on others. ]
  2. A declaration for a bit-field that omits the identifier declares an unnamed bit-field. Unnamed bit-fields are not members and cannot be initialized. [Note: an unnamed bit-field is useful for padding to conform to externally-imposed layouts. ] As a special case, an unnamed bit-field with a width of zero specifies alignment of the next bit-field at an allocation unit boundary. Only when declaring an unnamed bit-field may the constant-expression be a value equal to zero.
  3. A bit-field shall not be a static member. A bit-field shall have integral or enumeration type (3.9.1). It is implementation-defined whether a plain (neither explicitly signed nor unsigned) char, short, int or long bit-field is signed or unsigned. A bool value can successfully be stored in a bit-field of any nonzero size. The address-of operator & shall not be applied to a bit-field, so there are no pointers to bit-fields. A non-const reference shall not be bound to a bit-field (8.5.3). [Note: if the initializer for a reference of type const T& is an lvalue that refers to a bit-field, the reference is bound to a temporary initialized to hold the value of the bit-field; the reference is not bound to the bit-field directly. See 8.5.3. ]
  4. If the value true or false is stored into a bit-field of type bool of any size (including a one bit bitfield), the original bool value and the value of the bit-field shall compare equal. If the value of an enumerator is stored into a bit-field of the same enumeration type and the number of bits in the bit-field is large enough to hold all the values of that enumeration type, the original enumerator value and the value of the bit-field shall compare equal.

最近在64bit IOS上遇到一个问题, 发现bit field的数据不对. 这个bit field是一个data struct, 在host(win32)上生成. 运行时候发现IOS取的值不对.

比如

struct
{
    int a : 4;
    int b : 8;
    int c : 16;
    int d : 8;
    int e : 16;
};

在IOS的32位上, d会横跨两个allocation uint, 而在IOS的64位上, d从第二个allocation unit上从新开始. 根据C++标准, 这个是implementation defined, 可以横跨也可以不横跨. 所以严格来说也不算clang的bug. 只要将a,b,c对齐到32bit的整数, 这样强制d从新的allocation unit开始, 没有横跨的问题, 对于IOS32位和IOS64位都有效.

IOS 64bit的额外参考: 对齐等等其他因素

https://developer.apple.com/library/ios/documentation/General/Conceptual/CocoaTouch64BitGuide/ConvertingYourAppto64-Bit/ConvertingYourAppto64-Bit.html

https://developer.apple.com/library/ios/documentation/General/Conceptual/CocoaTouch64BitGuide/Major64-BitChanges/Major64-BitChanges.html

个人觉得bitfield用在game data里面会有平台的问题, 所以blade里面没有用过. 而公司的代码用的地方非常多, 这个是历史原因.

时间: 2024-10-16 14:24:49

[工作积累] bitfield的相关文章

工作积累之NDK编译STL (zhuan)

方法: 1.在jni目录下新建Application.mk; 加入 APP_STL :=  stlport_static  右边的值还可以换成下面几个: system - 使用默认最小的C++运行库,这样生成的应用体积小,内存占用小,但部分功能将无法支持 stlport_static - 使用STLport作为静态库,这项是Android开发网极力推荐的 stlport_shared - STLport 作为动态库,这个可能产生兼容性和部分低版本的Android固件,目前不推荐使用. gnust

工作积累(五)——使用[email protected]注解实现常量功能

之前的博客中提到过如何通过 java.util.ResourceBundle 和 java.util.Properties类通过读取 key-value 文件的形式实现常量功能.其实 spring 已经通过@Value 注解实现,下面看看如何使用. 1.创建.properties文件: 在如下目录创建 keyvalue.properties文件src/main/resources/META-INF/spring/keyvalue.properties ,写入如下内容: test.value=il

[工作积累] NDK通过Java获取package name 和version

////////////////////////////////////////////////////////////////////////// //Java code snippet //get APK's versionCode in AndroidManifest.xml public int getVersionCode() { int versionCode = 1; try{ PackageInfo packageInfo = this.getPackageManager().g

HAL层Camera模块Dump图片--工作积累

导出YUN数据进行调试,分析问题: 1 // dump图像数据 事列 2 void dump(const int width, const int height, void *yBuf, void *uvBuf) 3 { 4 char buf[256] = {'\0'}; 5 FILE* file_fd = fopen(buf, "wb"); 6 snprintf(buf, sizeof(buf), "/data/Effect/%dx%dvideodenoiser%d.yuv

[工作积累] Google/Amazon平台的各种坑

所谓坑, 就是文档中没有标明的特别需要处理的细节, 工作中会被无故的卡住各种令人恼火的问题. 包括系统级的bug和没有文档化的限制. 继Android的各种坑后, 现在做Amazon平台, 遇到的坑很多, 这里记录一下备忘: 先汇总下Android Native下的各种问题, 当然有些限制有明确文档说明,不算坑,但是限制太多还是很不爽: android平台下的某些限制: android下的各种坑 (我的C/C++/汇编/计算机原理博客) OBB的各种bug: OBB的解决方案 arm gcc t

[工作积累] UE4 并行渲染的同步 - Sync between FParallelCommandListSet & FRHICommandListImmediate calls

UE4 的渲染分为两个模式1.编辑器是同步绘制的 2.游戏里是FParallelCommandListSet并行派发的. mesh渲染也分两类,static mesh 使用TStaticMeshDrawList 来绘制, skinned mesh是用DrawingPolicyFactory::DrawDynamicMesh来画.这两类绘制不管是异步还是同步都会调用.具体可以参考DepthRendering.cpp 实际上,有在DX12/Vulkan/Metal 这些支持paralle commi

[工作积累] shadow map问题汇总

1.基本问题和相关 Common Techniques to Improve Shadow Depth Maps: https://msdn.microsoft.com/en-us/library/windows/desktop/ee416324(v=vs.85).aspx Cascaded Shadow Maps https://msdn.microsoft.com/en-us/library/windows/desktop/ee416307(v=vs.85).aspx Soft shadow

工作积累(九)——前后台传递类Map型参数

最近在工作中整合友盟消息推送服务时,遇到了用 Ajax 向 Java 后台传递自定义参数的需求,当时想要采取 java.util.Map ,但发现 Ajax 无法传递 java.util.Map 类型的参数,后来无奈采取的方式的是采用了这样的 Vo 对象: public ExtraVo {   private List<String> keys;   private List<String> values;   //省略 get | set 方法 } 取值时: if(extraVo

[工作积累] 32bit to 64bit: array index underflow

先贴一段C++标准(ISO/IEC 14882:2003): 5.2.1 Subscripting: 1 A postfix expression followed by an expression in square brackets is a postfix expression. One of theexpressions shall have the type “pointer to T” and the other shall have enumeration or integral