How to convert DoubleVector to IntVector in Java Vector API?

64 views Asked by At

The bellow Java Vector API code has a DoubleVector filled with doubles, named "A1". I am trying to convert this DoubleVector (A1) to IntVector (RESULT), so far unsuccessful.

    double[] v1 = {10237, 10709, 11340, 11468, 11771, 12003, 12196, 12456, 13904, 14557, 14636, 14811, 15337, 15468, 15719, 15990};
    double[] v2 = {150, 171, 180, 183, 184, 289, 301, 306, 358, 449, 486, 539, 567, 605, 643};
    int[] result = new int[v1.length];
    var species = DoubleVector.SPECIES_PREFERRED;
    for (int index = 0; index < v1.length; index += species.length()) {
        var V1 = DoubleVector.fromArray(species, v1, index);
        var V2 = DoubleVector.fromArray(species, v2, index);
        DoubleVector A1 = V1.div(V2).pow(2);
        //Create IntVector RESULT by the convertion of DoubleVector A1 to IntVector.
        RESULT.intoArray(result, index);
    }

EDIT 1: After more research on the subject, the updated code is:

    double[] v1 = {10237, 10709, 11340, 11468, 11771, 12003, 12196, 12456, 13904, 14557, 14636, 14811, 15337, 15468, 15719, 15990};
    double[] v2 = {150, 171, 180, 183, 184, 289, 301, 306, 358, 449, 486, 539, 567, 605, 643};
    int[] result = new int[v1.length];
    var species = DoubleVector.SPECIES_PREFERRED;
    for (int index = 0; index < v1.length; index += species.length()) {
        var V1 = DoubleVector.fromArray(species, v1, index);
        var V2 = DoubleVector.fromArray(species, v2, index);
        DoubleVector A1 = V1.div(V2).pow(2);
        //Create IntVector RESULT by the convertion of DoubleVector A1 to IntVector.
        IntVector RESULT = A1.convert(VectorOperators.D2I, index).reinterpretAsInts();    // <--- I need help to write this line correctly.
        RESULT.intoArray(result, index);
    }

There is an error message related to the size difference between double and int, that says: bad part number 4 converting Species[double, 4, S_256_BIT] -> Species[int, 8, S_256_BIT] (lanes are contracting by 2).

Edit 2: When I run the code with the this line:

    IntVector RESULT = A1.convert(VectorOperators.D2I, 0).reinterpretAsInts();    // "0" instead of "index"

Then the error message changes to: java.lang.IndexOutOfBoundsException: Index 12 out of bonds for length 9

0

There are 0 answers