Vectorised Logical Operations in Java 9

This is a short post for my own reference, since I feel I have already done the topic of does Java 9 use AVX for this? to death. Cutting to the chase, Java 9 autovectorises loops to compute logical ANDs, XORs, ORs and ANDNOTs between arrays, making use of the instructions VPXOR, VPOR and VPAND. You can replicate this by running the code at github.

XOR


    @Benchmark
    public long[] xor(LongData state) {
        long[] result = new long[state.data1.length];
        long[] data1 = state.data1;
        long[] data2 = state.data2;
        for (int i = 0; i < data1.length && i < data2.length; ++i) {
            result[i] = data1[i] ^ data2[i];
        }
        return result;
    }

  0x000002884a58a5a0: vmovdqu ymm0,ymmword ptr [r10+r13*8+10h]
                                                ;...c4
                                                ;...81
                                                ;...7e
                                                ;...6f
                                                ;...44
                                                ;...ea
                                                ;...10

  0x000002884a58a5a7: vpxor   ymm0,ymm0,ymmword ptr [rbx+r13*8+10h]
                                                ;...c4
                                                ;...a1
                                                ;...7d
                                                ;...ef
                                                ;...44
                                                ;...eb
                                                ;...10

  0x000002884a58a5ae: vmovdqu ymmword ptr [rax+r13*8+10h],ymm0
                                                ;...c4
                                                ;...a1
                                                ;...7e
                                                ;...7f
                                                ;...44
                                                ;...e8
                                                ;...10

OR


    @Benchmark
    public long[] or(LongData state) {
        long[] result = new long[state.data1.length];
        long[] data1 = state.data1;
        long[] data2 = state.data2;
        for (int i = 0; i < data1.length && i < data2.length; ++i) {
            result[i] = data1[i] | data2[i];
        }
        return result;
    }

  0x000001dbc4a6b738: vmovdqu ymm0,ymmword ptr [r10+rsi*8+30h]
                                                ;...c4
                                                ;...c1
                                                ;...7e
                                                ;...6f
                                                ;...44
                                                ;...f2
                                                ;...30

  0x000001dbc4a6b73f: vpor    ymm0,ymm0,ymmword ptr [rbx+rsi*8+30h]
                                                ;...c5
                                                ;...fd
                                                ;...eb
                                                ;...44
                                                ;...f3
                                                ;...30

  0x000001dbc4a6b745: vmovdqu ymmword ptr [rax+rsi*8+30h],ymm0
                                                ;...c5
                                                ;...fe
                                                ;...7f
                                                ;...44
                                                ;...f0
                                                ;...30

AND


    @Benchmark
    public long[] and(LongData state) {
        long[] result = new long[state.data1.length];
        long[] data1 = state.data1;
        long[] data2 = state.data2;
        for (int i = 0; i < data1.length && i < data2.length; ++i) {
            result[i] = data1[i] & data2[i];
        }
        return result;
    }

  0x000001d615c49da0: vmovdqu ymm0,ymmword ptr [r10+r13*8+10h]
                                                ;...c4
                                                ;...81
                                                ;...7e
                                                ;...6f
                                                ;...44
                                                ;...ea
                                                ;...10

  0x000001d615c49da7: vpand   ymm0,ymm0,ymmword ptr [rbx+r13*8+10h]
                                                ;...c4
                                                ;...a1
                                                ;...7d
                                                ;...db
                                                ;...44
                                                ;...eb
                                                ;...10

  0x000001d615c49dae: vmovdqu ymmword ptr [rax+r13*8+10h],ymm0
                                                ;...c4
                                                ;...a1
                                                ;...7e
                                                ;...7f
                                                ;...44
                                                ;...e8
                                                ;...10

ANDNOT


    @Benchmark
    public long[] andNot(LongData state) {
        long[] result = new long[state.data1.length];
        long[] data1 = state.data1;
        long[] data2 = state.data2;
        for (int i = 0; i < data1.length && i < data2.length; ++i) {
            result[i] = data1[i] & ~data2[i];
        }
        return result;
    }

  0x0000018187ae8a3f: vpunpcklqdq xmm0,xmm0,xmm0  ;...c5
                                                ;...f9
                                                ;...6c
                                                ;...c0

  0x0000018187ae8a43: vinserti128 ymm0,ymm0,xmm0,1h  ;...c4
                                                ;...e3
                                                ;...7d
                                                ;...38
                                                ;...c0
                                                ;...01

  0x000001dec4cbb14c: vmovdqu ymm1,ymmword ptr [rbx+r13*8+10h]
                                                ;...c4
                                                ;...a1
                                                ;...7e
                                                ;...6f
                                                ;...4c
                                                ;...eb
                                                ;...10

  0x000001dec4cbb153: vpxor   ymm1,ymm1,ymm0    ;...c5
                                                ;...f5
                                                ;...ef
                                                ;...c8

  0x000001dec4cbb157: vpand   ymm1,ymm1,ymmword ptr [r10+r13*8+10h]
                                                ;...c4
                                                ;...81
                                                ;...75
                                                ;...db
                                                ;...4c
                                                ;...ea
                                                ;...10

  0x000001dec4cbb15e: vmovdqu ymmword ptr [rax+r13*8+10h],ymm1
                                                ;...c4
                                                ;...a1
                                                ;...7e
                                                ;...7f
                                                ;...4c
                                                ;...e8
                                                ;...10
                                                ;*lastore {reexecute=0 rethrow=0 return_oop=0}
                                                ; - com.openkappa.simd.logical.Logicals::andNot@54 (line 51)

Leave a Reply

Your email address will not be published. Required fields are marked *

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <s> <strike> <strong>