www.digitalmars.com         C & C++   DMDScript  

digitalmars.D.learn - Alligned gc allocation of struct

reply Sjoerd Nijboer <dlang sjoerdnijboer.com> writes:
I've got a `struct Foo{ubyte16 field1, field2.... fieldn;}` for 
which I would like a heap allocation `Foo* foo = new Foo();` But 
the fields itsself must be 16 bytes aligned for SIMD instructions.
Is there a neat way to do this in D?
As far as I can tell the GC_allocator doesn't do aligned 
allocations by default.
Oct 05 2018
parent reply Kagamin <spam here.lot> writes:
GC allocations are 16 bytes aligned.
Oct 05 2018
next sibling parent Sjoerd Nijboer <dlang sjoerdnijboer.com> writes:
On Friday, 5 October 2018 at 10:03:35 UTC, Kagamin wrote:
 GC allocations are 16 bytes aligned.
That's perfect. Thank you!
Oct 05 2018
prev sibling parent reply Dennis <dkorpel gmail.com> writes:
On Friday, 5 October 2018 at 10:03:35 UTC, Kagamin wrote:
 GC allocations are 16 bytes aligned.
Is that an implementation detail or well-defined behavior?
Oct 05 2018
parent reply Sjoerd Nijboer <dlang sjoerdnijboer.com> writes:
On Friday, 5 October 2018 at 14:55:04 UTC, Dennis wrote:
 On Friday, 5 October 2018 at 10:03:35 UTC, Kagamin wrote:
 GC allocations are 16 bytes aligned.
Is that an implementation detail or well-defined behavior?
The GC_Allocator doesn't support alignedAllocate from the IAllocate interface, which is kinda unfortunate. So I'm a little worried that this might change in the future.
Oct 05 2018
parent Stanislav Blinov <stanislav.blinov gmail.com> writes:
On Friday, 5 October 2018 at 20:41:15 UTC, Sjoerd Nijboer wrote:
 On Friday, 5 October 2018 at 14:55:04 UTC, Dennis wrote:
 On Friday, 5 October 2018 at 10:03:35 UTC, Kagamin wrote:
 GC allocations are 16 bytes aligned.
Is that an implementation detail or well-defined behavior?
The GC_Allocator doesn't support alignedAllocate from the IAllocate interface, which is kinda unfortunate. So I'm a little worried that this might change in the future.
I don't think you should rely on them being specifically 16-bytes aligned, if portability is your goal. The `GC`'s methods such as `GC.malloc` do indeed return an "aligned" memory block, but that's about it as far as the spec goes. If, however, your target is specifically amd64 and SIMD, then you can safely assume the allocations are indeed always 16-bytes aligned. On a side note, I think the `alignedAllocate` from the allocator interface in std.experimental was a poor API decision. Better have a specific allocator instance that allocates in an appropriate alignment requirement. If I ever see an allocator that supports allocating with both an unspecified alignment and arbitrary alignment, I'd steer clear from it even without looking at the implementation.
Oct 07 2018