0

What is the situation with alignment of arrays in Mono implementation of CLR? Are they guaranteed to be naturally aligned on all platforms? If no, on which platforms it is safe to assume natural alignment of CLR-managed arrays?

There are two related questions on SO:

I am looking for similar information on Mono framework.

Community
  • 1
  • 1
Marat Dukhan
  • 11,993
  • 4
  • 27
  • 41
  • It's vague. Do you mean the array reference, the array object itself or the array elements? Guessing at the latter, what type of element? The rule that governs first is the CLI guarantee that updates of object references and values of type int are atomic. The only practical way to provide that guarantee that is to get them aligned to at least a multiple of 4 for a 32-bit process, of 8 for a 64-bit process. This isn't unique to managed code. And if you need to know the answer to the question then you are probably doing it wrong. – Hans Passant Oct 06 '13 at 21:52
  • I mean array elements. Since arrays of 1-, 2-, and 4- elements are naturally aligned by ECMA-335, my concern is about 8-byte elements (long, ulong, and double). – Marat Dukhan Oct 06 '13 at 22:05
  • Don't count on it. Why does this matter? And if it really matters, what are you going to do about it? You can change the code, it is Mono. – Hans Passant Oct 06 '13 at 22:13
  • 1
    I develop a math library (www.yeppp.info) with C# bindings. It allows C# code to call SIMD-optimized native functions to get better performance. SIMD load/store instructions are more efficient when they work with pointers aligned on 16/32/64 bytes (depending on SIMD ISA). If the array pointer is initially naturally aligned, I can make it aligned by 16/32/64 bytes by processing first few elements one-by-one. If I can not rely on array being naturally aligned, a lot of realignment code is needed, which also affects performance. – Marat Dukhan Oct 06 '13 at 22:25
  • SSE2 requires 16-byte alignment, you cannot get that in any CLI implementation. This is a horrible XY question. – Hans Passant Oct 06 '13 at 22:28
  • If the input array of doubles is aligned on 8 bytes, I need to process at most 1 element in scalar mode to get it aligned by 16 bytes. – Marat Dukhan Oct 06 '13 at 22:29

1 Answers1

2

For long and double arrays, we guarantee the array elements are naturally aligned on 64 bit platforms.

On 32 bit platforms they happen to be naturally aligned, too and this is unlikely to change since also some 32 bit architectures require doubles to be aligned as well and we'll keep the code consistent on them.

Note that you want your library to support processing starting at any index in the array and since you're using SIMD instructions which require 16 byte alignment, you will always need to check and do scalar processing at the start/end of the data anyway.

As for long and double fields inside structs/classes, the alignment is not guaranteed on all the 32 bit systems, it depends on the ABI, but it is at least 32 bits.

lupus
  • 3,963
  • 1
  • 18
  • 13