[vectorIntrinsics] RFR: Load store memory segment

Paul Sandoz psandoz at openjdk.java.net
Wed Mar 30 01:01:54 UTC 2022


On Tue, 29 Mar 2022 23:31:13 GMT, Quan Anh Mai <duke at openjdk.java.net> wrote:

> Hi, may I ask why do we reject loading a vector from a heap `MemorySegment` not backed by a `byte[]`?
> 

I wanted to first keep to the equivalent functionality that is currently supported, then think through the implications of supporting any primitive backing array and what that means regarding alignment and byte order.

MemorySegment now supports misaligned access on primitive arrays for values whose size is larger or smaller than the primitive array element size. However, by default the value layouts disallow that, for example (using the preview API for memory segments), for example:


MemorySegment ias = MemorySegment.ofArray(
        new int[] {0, 1, 2, 3, 4, 5, 6, 7, 8, 9});


// Access using element whose natural alignment is wider than the natural primitive array alignment

// java.lang.IllegalArgumentException: Misaligned access at address: 16
ias.set(JAVA_LONG, 0, 1L);

// No exception
ias.set(JAVA_LONG.withBitAlignment(8), 0, 1L);

// No exception
ias.set(JAVA_LONG.withBitAlignment(8), 1, 1L);


// Access using element whose natural alignment is smaller than the natural primitive array alignment

// java.lang.IllegalArgumentException: Misaligned access at address: 17
ias.set(JAVA_SHORT, 1, (short) 1);

// No exception
ias.set(JAVA_SHORT, 0, (short) 1);

// No exception
ias.set(JAVA_SHORT.withBitAlignment(8), 1, (short) 1);

-------------

PR: https://git.openjdk.java.net/panama-vector/pull/187


More information about the panama-dev mailing list