memory-alignment

How to align C for-loop body w/ GCC?

China☆狼群 提交于 2019-12-04 00:44:48
In our embedded architecture we have a 64-bit IAB (Instruction Alignment Buffer). In order to optimize the fetch sequence, it is required that the body of a loop will start aligned to an 8-byte boundary. It is easy to achieve this in assembly using the .balign directive, but I cannot find a syntax that will hint the C compiler to align the code. Trying to precede the for loop with inline assembly with the .balign directive doesn't work as it aligns the for loop prolog (setup) and not the loop body itself. Doing the same where the asm() line is inside the loop, adds nop -s to the loop body that

__memcpy_sse2_unaligned - what does this mean in detail?

风格不统一 提交于 2019-12-04 00:36:57
While working on my compiler I got this error: Program received signal SIGSEGV, Segmentation fault. __memcpy_sse2_unaligned () at ../sysdeps/x86_64/multiarch/memcpy-sse2-unaligned.S:33 How do I get details of what went wrong here? I know from the backtrace it's a memcpy line that causes it, but how do I see how the memory is aligned? And how do I know how it should be aligned? The project is a compiler with an LLVM back-end using the Zend/PHP runtime with the OCaml garbage collector, so there's is a lot of things that can go wrong. I suspect this line being part of the problem: zend_string

creating a C function with a given size in the text segment

爷,独闯天下 提交于 2019-12-04 00:10:37
I'm programming an embedded powerpc 32 system with a 32 kbyte 8-way set associative L2 instruction cache. To avoid cache thrashing we align functions in a way such that the text of a set of functions called at a high frequency (think interrupt code) ends up in separate cache sets. We do this by inserting dummy functions as needed, e.g. void high_freq1(void) { ... } void dummy(void) { __asm__(/* Silly opcodes to fill ~100 to ~1000 bytes of text segment */); } void high_freq2(void) { ... } This strikes me as ugly and suboptimal. What I'd like to do is avoid __asm__ entirely and use pure C89

Objective-C Runtime: What to put for size & alignment for class_addIvar?

余生长醉 提交于 2019-12-03 16:44:35
The Objective-C Runtime provides the class_addIvar C function : BOOL class_addIvar(Class cls, const char *name, size_t size, uint8_t alignment, const char *types) What do I put for size and alignment ? I'm adding an instance variable of type UITextPosition * , but no UITextPosition object is in scope. For size , can I just do sizeof(self) , where self is a subclass of UITextField ? I.e., can I assume that a UITextPosition object is the same size as a UITextField object? How do I get alignment ? The documentation on this stuff is not very informative, which does reflect that generally you

How to check if a pointer points to a properly aligned memory location?

喜欢而已 提交于 2019-12-03 15:44:05
问题 Given a void * to some storage, how to check whether it points to properly aligned storage without any implementation defined behavior? Of course we have std::align, but is there a more effective way to do this? template <std::size_t alignment> inline bool is_aligned(void * ptr) noexcept { std::size_t max = 1u; return std::align(alignment, 1u, ptr, max); } PS: I need to do this in a C++ standards-compatible fashion, without relying on any platform-specific (implementation defined) hacks. PPS:

combining packed data with aligned memory access

ぐ巨炮叔叔 提交于 2019-12-03 14:46:21
I'm trying to perform a memory optimization that should be theoretically possible but that I'm starting to doubt is within arm-elf-gcc's capability. Please show me that I'm wrong. I have an embedded system with a very small amount of main memory, and an even smaller amount of battery-backed nvram. I am storing checksummed configuration data in the nvram so that on boot I can validate the checksum and continue a previous run or start a new run if the checksum is invalid. During the run, I update various fields of various sizes in this configuration data (and it's okay that this invalidates the

SSE: unaligned load and store that crosses page boundary

回眸只為那壹抹淺笑 提交于 2019-12-03 12:49:28
问题 I read somewhere that before performing unaligned load or store next to page boundary (e.g. using _mm_loadu_si128 / _mm_storeu_si128 intrinsics), code should first check if whole vector (in this case 16 bytes) belongs to the same page, and switch to non-vector instructions if not. I understand that this is needed to prevent coredump if next page does not belong to process. But what if both pages belongs to process (e.g. they are part of one buffer, and I know size of that buffer)? I wrote

Take advantage of ARM unaligned memory access while writing clean C code

ぐ巨炮叔叔 提交于 2019-12-03 07:01:24
问题 It used to be that ARM processors were unable to properly handle unaligned memory access (ARMv5 and below). Something like u32 var32 = *(u32*)ptr; would just fail (raise exception) if ptr was not properly aligned on 4-bytes. Writing such a statement would work fine for x86/x64 though, since these CPU have always handled such situation very efficiently. But according to C standard, this is not a "proper" way to write it. u32 is apparently equivalent to a structure of 4 bytes which must be

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

*爱你&永不变心* 提交于 2019-12-03 06:43:13
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 rules. Can you please explain as a bonus why this approach is flawed from memory alignment point of

The future of C++ alignment: passing by value?

我是研究僧i 提交于 2019-12-03 04:56:45
Reading the Eigen library documentation, I noticed that some objects cannot be passed by value . Are there any developments in C++11 or planned developments that will make it safe to pass such objects by value? Also, why is there no problem with returning such objects by value? They could do this in C++11: class alignas(16) Matrix4f { // ... }; Now the class will always be aligned on a 16-byte boundary. Also, maybe I'm being silly but this shouldn't be an issue anyway. Given a class like this: class Matrix4f { public: // ... private: // their data type (aligned however they decided in that