memory-alignment

Detecting Aligned Memory requirement on target CPU

三世轮回 提交于 2019-12-04 12:39:29
I'm currently trying to build a code which is supposed to work on a wide range of machines, from handheld pockets and sensors to big servers in data centers. One of the (many) differences between these architectures is the requirement for aligned memory access. Aligned memory access is not required on "standard" x86 CPU, but many other CPU need it and produce an exception if the rule is not respected. Up to now, i've been dealing with it by forcing the compiler to be cautious on specific data accesses which are known to be risky, using the packed attribute (or pragma). And it works fine. The

Where can I find documentation on C++ memory alignment across different platforms/compilers?

纵然是瞬间 提交于 2019-12-04 11:52:16
问题 I'm looking for a good (comprehensive) doc about memory alignment in C++, typical approaches, differences between compilers, and common pitfalls. Just to check if my understanding of the topic is correct and to learn something new. This question is inspired by my answer to another question where I used following construct: char const buf[1000] = ...; unsigned int i = *reinterpret_cast<unsigned int*>(buf + shift); // shift can be anything It was criticized as not conforming to memory alignment

Does a phantom type have the same alignment as the original one?

自古美人都是妖i 提交于 2019-12-04 08:52:09
问题 Consider the following struct that contains some environment values: struct environment_values { uint16_t humidity; uint16_t temperature; uint16_t charging; }; I would like to add some additional information to those values with a phantom type* and make their types distinct at the same time: template <typename T, typename P> struct Tagged { T value; }; // Actual implementation will contain some more features struct Celsius{}; struct Power{}; struct Percent{}; struct Environment { Tagged

Does std::allocator handle over-aligned types in C++17?

北城以北 提交于 2019-12-04 08:11:38
C++17 introduces std::aligned_alloc and alignment-aware new that can do over-aligned allocations, but what about std::allocator ? Does it handle over-aligned types? In N4659(C++17 DIS), 23.10.9.1 [allocator.members], bullet 2 T* allocate(size_t n); Returns: A pointer to the initial element of an array of storage of size n * sizeof(T), aligned appropriately for objects of type T . Compared to C++14, the sentence It is implementation-defined whether over-aligned types are supported has been removed. So std::allocator should support over-aligned types in C++17. 来源: https://stackoverflow.com

Python ctypes align data structure

强颜欢笑 提交于 2019-12-04 07:42:20
I have a C library that is compiled to a shared object and want to build a ctypes interface around it to call the C functions from Python. In general it works fine but there is this definition of a double array in the C library: typedef double __attribute__ ((aligned (32))) double_array[512]; I found no way to access this type directly, so I defined in Python: DoubleArray = ctypes.c_double * 512 While this works out in most cases, sometimes the C library segfaults and I guess this happens because DoubleArray is not aligned to 32 bytes (maybe the library expects this because the data is passed

Memory alignment for fast FFT in Python using shared arrays

夙愿已清 提交于 2019-12-04 07:24:32
I write an image processing app that needs to do multiple things and it has to do them as much real-time as possible. Acquisition of the data and their processing runs in separate processes (mainly for performance reasons). The data itself is quite large (2MPix 16-bit grayscale images). I can share arrays between processes as it is described in this post: How do I pass large numpy arrays between python subprocesses without saving to disk? (I use the shmarray script from the numpy-shared package). I can perform the supplied Numpy FFT on those data without problem, but it is quite slow. Calling

How use alignof to force alignment for a heap allocation?

删除回忆录丶 提交于 2019-12-04 05:24:55
问题 I'd like to force a specific heap allocation to return an address that's 64-byte aligned, because that's a cache line boundary. I thought I could do it like this int *p = new alignas(64) int; but none of my compilers seem to give p an address that's a multiple of 64. Here's how I'm checking: #include <iostream> int main() { int *p = new alignas(64) int; std::cout << long(p) % 64 << '\n'; // should print 0 } I must be doing something wrong. But what? 回答1: The allocator called by the new

Reading from an unaligned uint8_t recast as a uint32_t array - not getting all values

不想你离开。 提交于 2019-12-04 05:06:59
问题 I am trying to cast a uint8_t array to uint32_t array. However, when i try to do this, I cant seem to be able to access every consecutive 4 bytes. Let us say I have a uint8_t array with 8 bytes. I would like to access byte 2 -> 6 as one uint32_t. These all get the same value *((uint32_t*)&uint8Array[0]) , *((uint32_t*)&uint8Array[1]) , *((uint32_t*)&uint8Array[2]) , *((uint32_t*)&uint8Array[3]) While *((uint32_t*)&uint8Array[4]) gets the bytes 4 -> 8 as expected. So it seem like I can not

Why alignment is power of 2?

試著忘記壹切 提交于 2019-12-04 03:02:22
There is a quote from cppreference : Every object type has the property called alignment requirement, which is an integer value (of type std::size_t, always a power of 2) representing the number of bytes between successive addresses at which objects of this type can be allocated. I understand, this reference is non-normative. But there is no something about value of alignof(T) in the standard, rather than it is no more than alignof(std::max_align_t) . It is not obviously, that alignment is power of 2. Why does alignment not be a 3? Deduplicator The standard has the final word for the language,

Is it guaranteed that Complex Float variables will be 8-byte aligned in memory?

末鹿安然 提交于 2019-12-04 02:04:13
问题 In C99 the new complex types were defined. I am trying to understand whether a compiler can take advantage of this knowledge in optimizing memory accesses. Are these objects ( A - F ) of type complex float guaranteed to be 8-byte aligned in memory? #include "complex.h" typedef complex float cfloat; cfloat A; cfloat B[10]; void func(cfloat C, cfloat *D) { cfloat E; cfloat F[10]; } Note that for D , the question relates to the object pointed to by D , not to the pointer storage itself. And, if