/* * Copyright (c) 2017, Oracle and/or its affiliates. All rights reserved. * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. * * This code is free software; you can redistribute it and/or modify it * under the terms of the GNU General Public License version 2 only, as * published by the Free Software Foundation. Oracle designates this * particular file as subject to the "Classpath" exception as provided * by Oracle in the LICENSE file that accompanied this code. * * This code is distributed in the hope that it will be useful, but WITHOUT * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License * version 2 for more details (a copy is included in the LICENSE file that * accompanied this code). * * You should have received a copy of the GNU General Public License version * 2 along with this work; if not, write to the Free Software Foundation, * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. * * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA * or visit www.oracle.com if you need additional information or have * questions. */ package jdk.incubator.vector; import jdk.internal.vm.annotation.ForceInline; import java.nio.ByteBuffer; import java.nio.ByteOrder; import java.nio.DoubleBuffer; import java.util.Objects; import java.util.concurrent.ThreadLocalRandom; /** * A specialized {@link Vector} representing an ordered immutable sequence of * {@code double} values. * * @param the type of shape of this vector */ @SuppressWarnings("cast") public abstract class DoubleVector extends Vector { DoubleVector() {} // Unary operator interface FUnOp { double apply(int i, double a); } abstract DoubleVector uOp(FUnOp f); abstract DoubleVector uOp(Mask m, FUnOp f); // Binary operator interface FBinOp { double apply(int i, double a, double b); } abstract DoubleVector bOp(Vector v, FBinOp f); abstract DoubleVector bOp(Vector v, Mask m, FBinOp f); // Trinary operator interface FTriOp { double apply(int i, double a, double b, double c); } abstract DoubleVector tOp(Vector v1, Vector v2, FTriOp f); abstract DoubleVector tOp(Vector v1, Vector v2, Mask m, FTriOp f); // Reduction operator abstract double rOp(double v, FBinOp f); // Binary test interface FBinTest { boolean apply(int i, double a, double b); } abstract Mask bTest(Vector v, FBinTest f); // Foreach interface FUnCon { void apply(int i, double a); } abstract void forEach(FUnCon f); abstract void forEach(Mask m, FUnCon f); // @Override public abstract DoubleVector add(Vector v); /** * Adds this vector to the broadcast of an input scalar. *

* This is a vector binary operation where the primitive addition operation * ({@code +}) is applied to lane elements. * * @param s the input scalar * @return the result of adding this vector to the broadcast of an input * scalar */ public abstract DoubleVector add(double s); @Override public abstract DoubleVector add(Vector v, Mask m); /** * Adds this vector to broadcast of an input scalar, * selecting lane elements controlled by a mask. *

* This is a vector binary operation where the primitive addition operation * ({@code +}) is applied to lane elements. * * @param s the input scalar * @param m the mask controlling lane selection * @return the result of adding this vector to the broadcast of an input * scalar */ public abstract DoubleVector add(double s, Mask m); @Override public abstract DoubleVector sub(Vector v); /** * Subtracts the broadcast of an input scalar from this vector. *

* This is a vector binary operation where the primitive subtraction * operation ({@code -}) is applied to lane elements. * * @param s the input scalar * @return the result of subtracting the broadcast of an input * scalar from this vector */ public abstract DoubleVector sub(double s); @Override public abstract DoubleVector sub(Vector v, Mask m); /** * Subtracts the broadcast of an input scalar from this vector, selecting * lane elements controlled by a mask. *

* This is a vector binary operation where the primitive subtraction * operation ({@code -}) is applied to lane elements. * * @param s the input scalar * @param m the mask controlling lane selection * @return the result of subtracting the broadcast of an input * scalar from this vector */ public abstract DoubleVector sub(double s, Mask m); @Override public abstract DoubleVector mul(Vector v); /** * Multiplies this vector with the broadcast of an input scalar. *

* This is a vector binary operation where the primitive multiplication * operation ({@code *}) is applied to lane elements. * * @param s the input scalar * @return the result of multiplying this vector with the broadcast of an * input scalar */ public abstract DoubleVector mul(double s); @Override public abstract DoubleVector mul(Vector v, Mask m); /** * Multiplies this vector with the broadcast of an input scalar, selecting * lane elements controlled by a mask. *

* This is a vector binary operation where the primitive multiplication * operation ({@code *}) is applied to lane elements. * * @param s the input scalar * @param m the mask controlling lane selection * @return the result of multiplying this vector with the broadcast of an * input scalar */ public abstract DoubleVector mul(double s, Mask m); @Override public abstract DoubleVector neg(); @Override public abstract DoubleVector neg(Mask m); @Override public abstract DoubleVector abs(); @Override public abstract DoubleVector abs(Mask m); @Override public abstract DoubleVector min(Vector v); /** * Returns the minimum of this vector and the broadcast of an input scalar. *

* This is a vector binary operation where the operation * {@code (a, b) -> a < b ? a : b} is applied to lane elements. * * @param s the input scalar * @return the minimum of this vector and the broadcast of an input scalar */ public abstract DoubleVector min(double s); @Override public abstract DoubleVector max(Vector v); /** * Returns the maximum of this vector and the broadcast of an input scalar. *

* This is a vector binary operation where the operation * {@code (a, b) -> a > b ? a : b} is applied to lane elements. * * @param s the input scalar * @return the maximum of this vector and the broadcast of an input scalar */ public abstract DoubleVector max(double s); @Override public abstract Mask equal(Vector v); /** * Tests if this vector is equal to the broadcast of an input scalar. *

* This is a vector binary test operation where the primitive equals * operation ({@code ==}) is applied to lane elements. * * @param s the input scalar * @return the result mask of testing if this vector is equal to the * broadcast of an input scalar */ public abstract Mask equal(double s); @Override public abstract Mask notEqual(Vector v); /** * Tests if this vector is not equal to the broadcast of an input scalar. *

* This is a vector binary test operation where the primitive not equals * operation ({@code !=}) is applied to lane elements. * * @param s the input scalar * @return the result mask of testing if this vector is not equal to the * broadcast of an input scalar */ public abstract Mask notEqual(double s); @Override public abstract Mask lessThan(Vector v); /** * Tests if this vector is less than the broadcast of an input scalar. *

* This is a vector binary test operation where the primitive less than * operation ({@code <}) is applied to lane elements. * * @param s the input scalar * @return the mask result of testing if this vector is less than the * broadcast of an input scalar */ public abstract Mask lessThan(double s); @Override public abstract Mask lessThanEq(Vector v); /** * Tests if this vector is less or equal to the broadcast of an input scalar. *

* This is a vector binary test operation where the primitive less than * or equal to operation ({@code <=}) is applied to lane elements. * * @param s the input scalar * @return the mask result of testing if this vector is less than or equal * to the broadcast of an input scalar */ public abstract Mask lessThanEq(double s); @Override public abstract Mask greaterThan(Vector v); /** * Tests if this vector is greater than the broadcast of an input scalar. *

* This is a vector binary test operation where the primitive greater than * operation ({@code >}) is applied to lane elements. * * @param s the input scalar * @return the mask result of testing if this vector is greater than the * broadcast of an input scalar */ public abstract Mask greaterThan(double s); @Override public abstract Mask greaterThanEq(Vector v); /** * Tests if this vector is greater than or equal to the broadcast of an * input scalar. *

* This is a vector binary test operation where the primitive greater than * or equal to operation ({@code >=}) is applied to lane elements. * * @param s the input scalar * @return the mask result of testing if this vector is greater than or * equal to the broadcast of an input scalar */ public abstract Mask greaterThanEq(double s); @Override public abstract DoubleVector blend(Vector v, Mask m); /** * Blends the lane elements of this vector with those of the broadcast of an * input scalar, selecting lanes controlled by a mask. *

* For each lane of the mask, at lane index {@code N}, if the mask lane * is set then the lane element at {@code N} from the input vector is * selected and placed into the resulting vector at {@code N}, * otherwise the the lane element at {@code N} from this input vector is * selected and placed into the resulting vector at {@code N}. * * @param s the input scalar * @param m the mask controlling lane selection * @return the result of blending the lane elements of this vector with * those of the broadcast of an input scalar */ public abstract DoubleVector blend(double s, Mask m); @Override public abstract DoubleVector rearrange(Vector v, Shuffle s, Mask m); @Override public abstract DoubleVector rearrange(Shuffle m); @Override @ForceInline public DoubleVector resize(Species species) { return (DoubleVector) species.resize(this); } @Override public abstract DoubleVector rotateEL(int i); @Override public abstract DoubleVector rotateER(int i); @Override public abstract DoubleVector shiftEL(int i); @Override public abstract DoubleVector shiftER(int i); /** * Divides this vector by an input vector. *

* This is a vector binary operation where the primitive division * operation ({@code /}) is applied to lane elements. * * @param v the input vector * @return the result of dividing this vector by the input vector */ public abstract DoubleVector div(Vector v); /** * Divides this vector by the broadcast of an input scalar. *

* This is a vector binary operation where the primitive division * operation ({@code /}) is applied to lane elements. * * @param v the input scalar * @return the result of dividing this vector by the broadcast of an input * scalar */ public abstract DoubleVector div(double s); /** * Divides this vector by an input vector, selecting lane elements * controlled by a mask. *

* This is a vector binary operation where the primitive division * operation ({@code /}) is applied to lane elements. * * @param v the input vector * @param m the mask controlling lane selection * @return the result of dividing this vector by the input vector */ public abstract DoubleVector div(Vector v, Mask m); /** * Divides this vector by the broadcast of an input scalar, selecting lane * elements controlled by a mask. *

* This is a vector binary operation where the primitive division * operation ({@code /}) is applied to lane elements. * * @param v the input scalar * @param m the mask controlling lane selection * @return the result of dividing this vector by the broadcast of an input * scalar */ public abstract DoubleVector div(double s, Mask m); // @@@ Many methods are refer to Math or StrictMath functions that only accept // double values, what should be the behaviour for lane elements of float // vectors? down and then upcast? Or will some numeric algorithms differ? // The answers might also depend if strict definitions are required // to ensure portability. // Leveraging the existing defintions in Math/StrictMath is very convenient // but its unclear if it is t /** * Calculates the square root of this vector. *

* This is a vector unary operation where the {@link Math#sqrt} operation * is applied to lane elements. * * @return the square root of this vector */ public abstract DoubleVector sqrt(); /** * Calculates the square root of this vector, selecting lane elements * controlled by a mask. *

* This is a vector unary operation where the {@link Math#sqrt} operation * ({@code -}) is applied to lane elements. * * @param m the mask controlling lane selection * @return the square root of this vector */ public DoubleVector sqrt(Mask m) { return uOp(m, (i, a) -> (double) Math.sqrt((double) a)); } /** * Calculates the trigonometric tangent of this vector. *

* This is a vector unary operation where the {@link Math#tan} operation * is applied to lane elements. * * @return the tangent of this vector */ public DoubleVector tan() { return uOp((i, a) -> (double) Math.tan((double) a)); } /** * Calculates the trigonometric tangent of this vector, selecting lane * elements controlled by a mask. *

* This is a vector unary operation where the {@link Math#tan} operation * is applied to lane elements. * * @param m the mask controlling lane selection * @return the tangent of this vector */ public DoubleVector tan(Mask m) { return uOp(m, (i, a) -> (double) Math.tan((double) a)); } /** * Calculates the hyperbolic tangent of this vector. *

* This is a vector unary operation where the {@link Math#tanh} operation * is applied to lane elements. * * @return the hyperbolic tangent of this vector */ public DoubleVector tanh() { return uOp((i, a) -> (double) Math.tanh((double) a)); } /** * Calculates the hyperbolic tangent of this vector, selecting lane elements * controlled by a mask. *

* This is a vector unary operation where the {@link Math#tanh} operation * is applied to lane elements. * * @param m the mask controlling lane selection * @return the hyperbolic tangent of this vector */ public DoubleVector tanh(Mask m) { return uOp(m, (i, a) -> (double) Math.tanh((double) a)); } /** * Calculates the trigonometric sine of this vector. *

* This is a vector unary operation where the {@link Math#sin} operation * is applied to lane elements. * * @return the sine of this vector */ public DoubleVector sin() { return uOp((i, a) -> (double) Math.sin((double) a)); } /** * Calculates the trigonometric sine of this vector, selecting lane elements * controlled by a mask. *

* This is a vector unary operation where the {@link Math#sin} operation * is applied to lane elements. * * @param m the mask controlling lane selection * @return the sine of this vector */ public DoubleVector sin(Mask m) { return uOp(m, (i, a) -> (double) Math.sin((double) a)); } /** * Calculates the hyperbolic sine of this vector. *

* This is a vector unary operation where the {@link Math#sinh} operation * is applied to lane elements. * * @return the hyperbolic sine of this vector */ public DoubleVector sinh() { return uOp((i, a) -> (double) Math.sinh((double) a)); } /** * Calculates the hyperbolic sine of this vector, selecting lane elements * controlled by a mask. *

* This is a vector unary operation where the {@link Math#sinh} operation * is applied to lane elements. * * @param m the mask controlling lane selection * @return the hyperbolic sine of this vector */ public DoubleVector sinh(Mask m) { return uOp(m, (i, a) -> (double) Math.sinh((double) a)); } /** * Calculates the trigonometric cosine of this vector. *

* This is a vector unary operation where the {@link Math#cos} operation * is applied to lane elements. * * @return the cosine of this vector */ public DoubleVector cos() { return uOp((i, a) -> (double) Math.cos((double) a)); } /** * Calculates the trigonometric cosine of this vector, selecting lane * elements controlled by a mask. *

* This is a vector unary operation where the {@link Math#cos} operation * is applied to lane elements. * * @param m the mask controlling lane selection * @return the cosine of this vector */ public DoubleVector cos(Mask m) { return uOp(m, (i, a) -> (double) Math.cos((double) a)); } /** * Calculates the hyperbolic cosine of this vector. *

* This is a vector unary operation where the {@link Math#cosh} operation * is applied to lane elements. * * @return the hyperbolic cosine of this vector */ public DoubleVector cosh() { return uOp((i, a) -> (double) Math.cosh((double) a)); } /** * Calculates the hyperbolic cosine of this vector, selecting lane elements * controlled by a mask. *

* This is a vector unary operation where the {@link Math#cosh} operation * is applied to lane elements. * * @param m the mask controlling lane selection * @return the hyperbolic cosine of this vector */ public DoubleVector cosh(Mask m) { return uOp(m, (i, a) -> (double) Math.cosh((double) a)); } /** * Calculates the arc sine of this vector. *

* This is a vector unary operation where the {@link Math#asin} operation * is applied to lane elements. * * @return the arc sine of this vector */ public DoubleVector asin() { return uOp((i, a) -> (double) Math.asin((double) a)); } /** * Calculates the arc sine of this vector, selecting lane elements * controlled by a mask. *

* This is a vector unary operation where the {@link Math#asin} operation * is applied to lane elements. * * @param m the mask controlling lane selection * @return the arc sine of this vector */ public DoubleVector asin(Mask m) { return uOp(m, (i, a) -> (double) Math.asin((double) a)); } /** * Calculates the arc cosine of this vector. *

* This is a vector unary operation where the {@link Math#acos} operation * is applied to lane elements. * * @return the arc cosine of this vector */ public DoubleVector acos() { return uOp((i, a) -> (double) Math.acos((double) a)); } /** * Calculates the arc cosine of this vector, selecting lane elements * controlled by a mask. *

* This is a vector unary operation where the {@link Math#acos} operation * is applied to lane elements. * * @param m the mask controlling lane selection * @return the arc cosine of this vector */ public DoubleVector acos(Mask m) { return uOp(m, (i, a) -> (double) Math.acos((double) a)); } /** * Calculates the arc tangent of this vector. *

* This is a vector unary operation where the {@link Math#atan} operation * is applied to lane elements. * * @return the arc tangent of this vector */ public DoubleVector atan() { return uOp((i, a) -> (double) Math.atan((double) a)); } /** * Calculates the arc tangent of this vector, selecting lane elements * controlled by a mask. *

* This is a vector unary operation where the {@link Math#atan} operation * is applied to lane elements. * * @param m the mask controlling lane selection * @return the arc tangent of this vector */ public DoubleVector atan(Mask m) { return uOp(m, (i, a) -> (double) Math.atan((double) a)); } /** * Calculates the arc tangent of this vector divided by an input vector. *

* This is a vector binary operation where the {@link Math#atan2} operation * is applied to lane elements. * * @param v the input vector * @return the arc tangent of this vector divided by the input vector */ public DoubleVector atan2(Vector v) { return bOp(v, (i, a, b) -> (double) Math.atan2((double) a, (double) b)); } /** * Calculates the arc tangent of this vector divided by the broadcast of an * an input scalar. *

* This is a vector binary operation where the {@link Math#atan2} operation * is applied to lane elements. * * @param s the input scalar * @return the arc tangent of this vector over the input vector */ public abstract DoubleVector atan2(double s); /** * Calculates the arc tangent of this vector divided by an input vector, * selecting lane elements controlled by a mask. *

* This is a vector binary operation where the {@link Math#atan2} operation * is applied to lane elements. * * @param v the input vector * @param m the mask controlling lane selection * @return the arc tangent of this vector divided by the input vector */ public DoubleVector atan2(Vector v, Mask m) { return bOp(v, m, (i, a, b) -> (double) Math.atan2((double) a, (double) b)); } /** * Calculates the arc tangent of this vector divided by the broadcast of an * an input scalar, selecting lane elements controlled by a mask. *

* This is a vector binary operation where the {@link Math#atan2} operation * is applied to lane elements. * * @param s the input scalar * @param m the mask controlling lane selection * @return the arc tangent of this vector over the input vector */ public abstract DoubleVector atan2(double s, Mask m); /** * Calculates the cube root of this vector. *

* This is a vector unary operation where the {@link Math#cbrt} operation * is applied to lane elements. * * @return the cube root of this vector */ public DoubleVector cbrt() { return uOp((i, a) -> (double) Math.cbrt((double) a)); } /** * Calculates the cube root of this vector, selecting lane elements * controlled by a mask. *

* This is a vector unary operation where the {@link Math#cbrt} operation * is applied to lane elements. * * @param m the mask controlling lane selection * @return the cube root of this vector */ public DoubleVector cbrt(Mask m) { return uOp(m, (i, a) -> (double) Math.cbrt((double) a)); } /** * Calculates the natural logarithm of this vector. *

* This is a vector unary operation where the {@link Math#log} operation * is applied to lane elements. * * @return the natural logarithm of this vector */ public DoubleVector log() { return uOp((i, a) -> (double) Math.log((double) a)); } /** * Calculates the natural logarithm of this vector, selecting lane elements * controlled by a mask. *

* This is a vector unary operation where the {@link Math#log} operation * is applied to lane elements. * * @param m the mask controlling lane selection * @return the natural logarithm of this vector */ public DoubleVector log(Mask m) { return uOp(m, (i, a) -> (double) Math.log((double) a)); } /** * Calculates the base 10 logarithm of this vector. *

* This is a vector unary operation where the {@link Math#log10} operation * is applied to lane elements. * * @return the base 10 logarithm of this vector */ public DoubleVector log10() { return uOp((i, a) -> (double) Math.log10((double) a)); } /** * Calculates the base 10 logarithm of this vector, selecting lane elements * controlled by a mask. *

* This is a vector unary operation where the {@link Math#log10} operation * is applied to lane elements. * * @param m the mask controlling lane selection * @return the base 10 logarithm of this vector */ public DoubleVector log10(Mask m) { return uOp(m, (i, a) -> (double) Math.log10((double) a)); } /** * Calculates the natural logarithm of the sum of this vector and the * broadcast of {@code 1}. *

* This is a vector unary operation where the {@link Math#log1p} operation * is applied to lane elements. * * @return the natural logarithm of the sum of this vector and the broadcast * of {@code 1} */ public DoubleVector log1p() { return uOp((i, a) -> (double) Math.log1p((double) a)); } /** * Calculates the natural logarithm of the sum of this vector and the * broadcast of {@code 1}, selecting lane elements controlled by a mask. *

* This is a vector unary operation where the {@link Math#log1p} operation * is applied to lane elements. * * @param m the mask controlling lane selection * @return the natural logarithm of the sum of this vector and the broadcast * of {@code 1} */ public DoubleVector log1p(Mask m) { return uOp(m, (i, a) -> (double) Math.log1p((double) a)); } /** * Calculates this vector raised to the power of an input vector. *

* This is a vector binary operation where the {@link Math#pow} operation * is applied to lane elements. * * @param v the input vector * @return this vector raised to the power of an input vector */ public DoubleVector pow(Vector v) { return bOp(v, (i, a, b) -> (double) Math.pow((double) a, (double) b)); } /** * Calculates this vector raised to the power of the broadcast of an input * scalar. *

* This is a vector binary operation where the {@link Math#pow} operation * is applied to lane elements. * * @param s the input scalar * @return this vector raised to the power of the broadcast of an input * scalar. */ public abstract DoubleVector pow(double s); /** * Calculates this vector raised to the power of an input vector, selecting * lane elements controlled by a mask. *

* This is a vector binary operation where the {@link Math#pow} operation * is applied to lane elements. * * @param v the input vector * @param m the mask controlling lane selection * @return this vector raised to the power of an input vector */ public DoubleVector pow(Vector v, Mask m) { return bOp(v, m, (i, a, b) -> (double) Math.pow((double) a, (double) b)); } /** * Calculates this vector raised to the power of the broadcast of an input * scalar, selecting lane elements controlled by a mask. *

* This is a vector binary operation where the {@link Math#pow} operation * is applied to lane elements. * * @param s the input scalar * @param m the mask controlling lane selection * @return this vector raised to the power of the broadcast of an input * scalar. */ public abstract DoubleVector pow(double s, Mask m); /** * Calculates the broadcast of Euler's number {@code e} raised to the power * of this vector. *

* This is a vector unary operation where the {@link Math#exp} operation * is applied to lane elements. * * @return the broadcast of Euler's number {@code e} raised to the power of * this vector */ public DoubleVector exp() { return uOp((i, a) -> (double) Math.exp((double) a)); } /** * Calculates the broadcast of Euler's number {@code e} raised to the power * of this vector, selecting lane elements controlled by a mask. *

* This is a vector unary operation where the {@link Math#exp} operation * is applied to lane elements. * * @param m the mask controlling lane selection * @return the broadcast of Euler's number {@code e} raised to the power of * this vector */ public DoubleVector exp(Mask m) { return uOp(m, (i, a) -> (double) Math.exp((double) a)); } /** * Calculates the broadcast of Euler's number {@code e} raised to the power * of this vector minus the broadcast of {@code -1}. * More specifically as if the following (ignoring any differences in * numerical accuracy): *

{@code
     *   this.exp().sub(this.species().broadcast(1))
     * }
*

* This is a vector unary operation where the {@link Math#expm1} operation * is applied to lane elements. * * @return the broadcast of Euler's number {@code e} raised to the power of * this vector minus the broadcast of {@code -1} */ public DoubleVector expm1() { return uOp((i, a) -> (double) Math.expm1((double) a)); } /** * Calculates the broadcast of Euler's number {@code e} raised to the power * of this vector minus the broadcast of {@code -1}, selecting lane elements * controlled by a mask * More specifically as if the following (ignoring any differences in * numerical accuracy): *

{@code
     *   this.exp(m).sub(this.species().broadcast(1), m)
     * }
*

* This is a vector unary operation where the {@link Math#expm1} operation * is applied to lane elements. * * @param m the mask controlling lane selection * @return the broadcast of Euler's number {@code e} raised to the power of * this vector minus the broadcast of {@code -1} */ public DoubleVector expm1(Mask m) { return uOp(m, (i, a) -> (double) Math.expm1((double) a)); } /** * Calculates the product of this vector and a first input vector summed * with a second input vector. * More specifically as if the following (ignoring any differences in * numerical accuracy): *

{@code
     *   this.mul(v1).add(v2)
     * }
*

* This is a vector ternary operation where the {@link Math#fma} operation * is applied to lane elements. * * @param v1 the first input vector * @param v2 the second input vector * @return the product of this vector and the first input vector summed with * the second input vector */ public abstract DoubleVector fma(Vector v1, Vector v2); /** * Calculates the product of this vector and the broadcast of a first input * scalar summed with the broadcast of a second input scalar. * More specifically as if the following: *

{@code
     *   this.fma(this.species().broadcast(s1), this.species().broadcast(s2))
     * }
*

* This is a vector ternary operation where the {@link Math#fma} operation * is applied to lane elements. * * @param s1 the first input scalar * @param s2 the second input scalar * @return the product of this vector and the broadcast of a first input * scalar summed with the broadcast of a second input scalar */ public abstract DoubleVector fma(double s1, double s2); /** * Calculates the product of this vector and a first input vector summed * with a second input vector, selecting lane elements controlled by a mask. * More specifically as if the following (ignoring any differences in * numerical accuracy): *

{@code
     *   this.mul(v1, m).add(v2, m)
     * }
*

* This is a vector ternary operation where the {@link Math#fma} operation * is applied to lane elements. * * @param v1 the first input vector * @param v2 the second input vector * @param m the mask controlling lane selection * @return the product of this vector and the first input vector summed with * the second input vector */ public DoubleVector fma(Vector v1, Vector v2, Mask m) { return tOp(v1, v2, m, (i, a, b, c) -> Math.fma(a, b, c)); } /** * Calculates the product of this vector and the broadcast of a first input * scalar summed with the broadcast of a second input scalar, selecting lane * elements controlled by a mask * More specifically as if the following: *

{@code
     *   this.fma(this.species().broadcast(s1), this.species().broadcast(s2), m)
     * }
*

* This is a vector ternary operation where the {@link Math#fma} operation * is applied to lane elements. * * @param s1 the first input scalar * @param s2 the second input scalar * @param m the mask controlling lane selection * @return the product of this vector and the broadcast of a first input * scalar summed with the broadcast of a second input scalar */ public abstract DoubleVector fma(double s1, double s2, Mask m); // Computes the square root of the sum of the squares of x and y /** * Calculates square root of the sum of the squares of this vector and an * input vector. * More specifically as if the following (ignoring any differences in * numerical accuracy): *

{@code
     *   this.mul(this).add(v.mul(v)).sqrt()
     * }
*

* This is a vector binary operation where the {@link Math#hypot} operation * is applied to lane elements. * * @param v the input vector * @return square root of the sum of the squares of this vector and an input * vector */ public DoubleVector hypot(Vector v) { return bOp(v, (i, a, b) -> (double) Math.hypot((double) a, (double) b)); } /** * Calculates square root of the sum of the squares of this vector and the * broadcast of an input scalar. * More specifically as if the following (ignoring any differences in * numerical accuracy): *

{@code
     *   this.mul(this).add(this.species().broadcast(v * v)).sqrt()
     * }
*

* This is a vector binary operation where the {@link Math#hypot} operation * is applied to lane elements. * * @param s the input scalar * @return square root of the sum of the squares of this vector and the * broadcast of an input scalar */ public abstract DoubleVector hypot(double s); /** * Calculates square root of the sum of the squares of this vector and an * input vector, selecting lane elements controlled by a mask. * More specifically as if the following (ignoring any differences in * numerical accuracy): *

{@code
     *   this.mul(this, m).add(v.mul(v), m).sqrt(m)
     * }
*

* This is a vector binary operation where the {@link Math#hypot} operation * is applied to lane elements. * * @param v the input vector * @param m the mask controlling lane selection * @return square root of the sum of the squares of this vector and an input * vector */ public DoubleVector hypot(Vector v, Mask m) { return bOp(v, m, (i, a, b) -> (double) Math.hypot((double) a, (double) b)); } /** * Calculates square root of the sum of the squares of this vector and the * broadcast of an input scalar, selecting lane elements controlled by a * mask. * More specifically as if the following (ignoring any differences in * numerical accuracy): *

{@code
     *   this.mul(this, m).add(this.species().broadcast(v * v), m).sqrt(m)
     * }
*

* This is a vector binary operation where the {@link Math#hypot} operation * is applied to lane elements. * * @param s the input scalar * @param m the mask controlling lane selection * @return square root of the sum of the squares of this vector and the * broadcast of an input scalar */ public abstract DoubleVector hypot(double s, Mask m); @Override public abstract void intoByteArray(byte[] a, int ix); @Override public abstract void intoByteArray(byte[] a, int ix, Mask m); @Override public abstract void intoByteBuffer(ByteBuffer bb, int ix); @Override public abstract void intoByteBuffer(ByteBuffer bb, int ix, Mask m); // Type specific horizontal reductions // @@@ For floating point vectors order matters for reproducibility // with equivalent sequential reduction. Some order needs to be specified // by default. If that default is sequential encounter order then there // could be a "go faster" option that is unspecified, essentially giving // implementation flexibility at the expense of reproducibility and/or // accuracy. // @@@ Mask versions? /** * Adds all lane elements of this vector. *

* This is an associative vector reduction operation where the addition * operation ({@code +}) is applied to lane elements, * and the identity value is {@code 0}. * * @return the addition of all the lane elements of this vector */ public abstract double addAll(); /** * Adds all lane elements of this vector, selecting lane elements * controlled by a mask. *

* This is an associative vector reduction operation where the addition * operation ({@code +}) is applied to lane elements, * and the identity value is {@code 0}. * * @param m the mask controlling lane selection * @return the addition of all the lane elements of this vector */ public abstract double addAll(Mask m); /** * Subtracts all lane elements of this vector. *

* This is an associative vector reduction operation where the subtraction * operation ({@code -}) is applied to lane elements, * and the identity value is {@code 0}. * * @return the subtraction of all the lane elements of this vector */ public abstract double subAll(); /** * Subtracts all lane elements of this vector, selecting lane elements * controlled by a mask. *

* This is an associative vector reduction operation where the subtraction * operation ({@code -}) is applied to lane elements, * and the identity value is {@code 0}. * * @param m the mask controlling lane selection * @return the subtraction of all the lane elements of this vector */ public abstract double subAll(Mask m); /** * Multiplies all lane elements of this vector. *

* This is an associative vector reduction operation where the * multiplication operation ({@code *}) is applied to lane elements, * and the identity value is {@code 1}. * * @return the multiplication of all the lane elements of this vector */ public abstract double mulAll(); /** * Multiplies all lane elements of this vector, selecting lane elements * controlled by a mask. *

* This is an associative vector reduction operation where the * multiplication operation ({@code *}) is applied to lane elements, * and the identity value is {@code 1}. * * @param m the mask controlling lane selection * @return the multiplication of all the lane elements of this vector */ public abstract double mulAll(Mask m); /** * Returns the minimum lane element of this vector. *

* This is an associative vector reduction operation where the operation * {@code (a, b) -> a > b ? b : a} is applied to lane elements, * and the identity value is {@link Double.MAX_VALUE}. * * @return the minimum lane element of this vector */ public abstract double minAll(); /** * Returns the minimum lane element of this vector, selecting lane elements * controlled by a mask. *

* This is an associative vector reduction operation where the operation * {@code (a, b) -> a > b ? b : a} is applied to lane elements, * and the identity value is {@link Double.MAX_VALUE}. * * @param m the mask controlling lane selection * @return the minimum lane element of this vector */ public abstract double minAll(Mask m); /** * Returns the maximum lane element of this vector. *

* This is an associative vector reduction operation where the operation * {@code (a, b) -> a < b ? b : a} is applied to lane elements, * and the identity value is {@link Double.MIN_VALUE}. * * @return the maximum lane element of this vector */ public abstract double maxAll(); /** * Returns the maximum lane element of this vector, selecting lane elements * controlled by a mask. *

* This is an associative vector reduction operation where the operation * {@code (a, b) -> a < b ? b : a} is applied to lane elements, * and the identity value is {@link Double.MIN_VALUE}. * * @param m the mask controlling lane selection * @return the maximum lane element of this vector */ public abstract double maxAll(Mask m); // Type specific accessors /** * Gets the lane element at lane index {@code i} * * @param i the lane index * @return the lane element at lane index {@code i} * @throws IllegalArgumentException if the index is is out of range * ({@code < 0 || >= length()}) */ public abstract double get(int i); /** * Replaces the lane element of this vector at lane index {@code i} with * value {@code e}. *

* This is a cross-lane operation and behaves as if it returns the result * of blending this vector with an input vector that is the result of * broadcasting {@code e} and a mask that has only one lane set at lane * index {@code i}. * * @param i the lane index of the lane element to be replaced * @param e the value to be placed * @return the result of replacing the lane element of this vector at lane * index {@code i} with value {@code e}. * @throws IllegalArgumentException if the index is is out of range * ({@code < 0 || >= length()}) */ public abstract DoubleVector with(int i, double e); // Type specific extractors /** * Returns an array containing the lane elements of this vector. *

* This method behaves as if it {@link #intoArray(double[], int)} stores} * this vector into an allocated array and returns the array as follows: *

{@code
     *   double[] a = new double[this.length()];
     *   this.intoArray(a, 0);
     *   return a;
     * }
* * @return an array containing the the lane elements of this vector */ @ForceInline public final double[] toArray() { // @@@ could allocate without zeroing, see Unsafe.allocateUninitializedArray double[] a = new double[species().length()]; intoArray(a, 0); return a; } /** * Stores this vector into an array starting at offset. *

* For each vector lane, where {@code N} is the vector lane index, * the lane element at index {@code N} is stored into the array at index * {@code i + N}. * * @param a the array * @param i the offset into the array * @throws IndexOutOfBoundsException if {@code i < 0}, or * {@code i > a.length - this.length()} */ public abstract void intoArray(double[] a, int i); /** * Stores this vector into an array starting at offset and using a mask. *

* For each vector lane, where {@code N} is the vector lane index, * if the mask lane at index {@code N} is set then the lane element at * index {@code N} is stored into the array index {@code i + N}. * * @param a the array * @param i the offset into the array * @param m the mask * @throws IndexOutOfBoundsException if {@code i < 0}, or * for any vector lane index {@code N} where the mask at lane {@code N} * is set {@code i >= a.length - N} */ public abstract void intoArray(double[] a, int i, Mask m); /** * Stores this vector into an array using indexes obtained from an index * map. *

* For each vector lane, where {@code N} is the vector lane index, the * lane element at index {@code N} is stored into the array at index * {@code i + indexMap[j + N]}. * * @param a the array * @param i the offset into the array, may be negative if relative * indexes in the index map compensate to produce a value within the * array bounds * @param indexMap the index map * @param j the offset into the index map * @throws IndexOutOfBoundsException if {@code j < 0}, or * {@code j > indexMap.length - this.length()}, * or for any vector lane index {@code N} the result of * {@code i + indexMap[j + N]} is {@code < 0} or {@code >= a.length} */ public void intoArray(double[] a, int i, int[] indexMap, int j) { forEach((n, e) -> a[i + indexMap[j + n]] = e); } /** * Stores this vector into an array using indexes obtained from an index * map and using a mask. *

* For each vector lane, where {@code N} is the vector lane index, * if the mask lane at index {@code N} is set then the lane element at * index {@code N} is stored into the array at index * {@code i + indexMap[j + N]}. * * @param a the array * @param i the offset into the array, may be negative if relative * indexes in the index map compensate to produce a value within the * array bounds * @param m the mask * @param indexMap the index map * @param j the offset into the index map * @throws IndexOutOfBoundsException if {@code j < 0}, or * {@code j > indexMap.length - this.length()}, * or for any vector lane index {@code N} where the mask at lane * {@code N} is set the result of {@code i + indexMap[j + N]} is * {@code < 0} or {@code >= a.length} */ public void intoArray(double[] a, int i, Mask m, int[] indexMap, int j) { forEach(m, (n, e) -> a[i + indexMap[j + n]] = e); } // Species @Override public abstract DoubleSpecies species(); /** * A specialized factory for creating {@link DoubleVector} value of the same * shape, and a {@link Mask} and {@link Shuffle} values of the same shape * and {@code int} element type. * * @param the type of shape of this species */ public static abstract class DoubleSpecies extends Vector.Species { interface FOp { double apply(int i); } abstract DoubleVector op(FOp f); abstract DoubleVector op(Mask m, FOp f); // Factories @Override public abstract DoubleVector zero(); /** * Returns a vector where all lane elements are set to the primitive * value {@code e}. * * @param e the value * @return a vector of vector where all lane elements are set to * the primitive value {@code e} */ public abstract DoubleVector broadcast(double e); /** * Returns a vector where the first lane element is set to the primtive * value {@code e}, all other lane elements are set to the default * value. * * @param e the value * @return a vector where the first lane element is set to the primitive * value {@code e} */ @ForceInline public final DoubleVector single(double e) { return zero().with(0, e); } /** * Returns a vector where each lane element is set to a randomly * generated primitive value. * @@@ what are the properties of the random number generator? * * @return a vector where each lane elements is set to a randomly * generated primitive value */ public DoubleVector random() { ThreadLocalRandom r = ThreadLocalRandom.current(); return op(i -> r.nextDouble()); } /** * Returns a vector where each lane element is set to a given * primitive value. *

* For each vector lane, where {@code N} is the vector lane index, the * the primitive value at index {@code N} is placed into the resulting * vector at lane index {@code N}. * * @@@ What should happen if es.length < this.length() ? use the default * value or throw IndexOutOfBoundsException * * @param es the given primitive values * @return a vector where each lane element is set to a given primitive * value */ public abstract DoubleVector scalars(double... es); /** * Loads a vector from an array starting at offset. *

* For each vector lane, where {@code N} is the vector lane index, the * array element at index {@code i + N} is placed into the * resulting vector at lane index {@code N}. * * @param a the array * @param i the offset into the array * @return the vector loaded from an array * @throws IndexOutOfBoundsException if {@code i < 0}, or * {@code i > a.length - this.length()} */ public abstract DoubleVector fromArray(double[] a, int i); /** * Loads a vector from an array starting at offset and using a mask. *

* For each vector lane, where {@code N} is the vector lane index, * if the mask lane at index {@code N} is set then the array element at * index {@code i + N} is placed into the resulting vector at lane index * {@code N}, otherwise the default element value is placed into the * resulting vector at lane index {@code N}. * * @param a the array * @param i the offset into the array * @param m the mask * @return the vector loaded from an array * @throws IndexOutOfBoundsException if {@code i < 0}, or * for any vector lane index {@code N} where the mask at lane {@code N} * is set {@code i > a.length - N} */ public abstract DoubleVector fromArray(double[] a, int i, Mask m); /** * Loads a vector from an array using indexes obtained from an index * map. *

* For each vector lane, where {@code N} is the vector lane index, the * array element at index {@code i + indexMap[j + N]} is placed into the * resulting vector at lane index {@code N}. * * @param a the array * @param i the offset into the array, may be negative if relative * indexes in the index map compensate to produce a value within the * array bounds * @param indexMap the index map * @param j the offset into the index map * @return the vector loaded from an array * @throws IndexOutOfBoundsException if {@code j < 0}, or * {@code j > indexMap.length - this.length()}, * or for any vector lane index {@code N} the result of * {@code i + indexMap[j + N]} is {@code < 0} or {@code >= a.length} */ public DoubleVector fromArray(double[] a, int i, int[] indexMap, int j) { return op(n -> a[i + indexMap[j + n]]); } /** * Loads a vector from an array using indexes obtained from an index * map and using a mask. *

* For each vector lane, where {@code N} is the vector lane index, * if the mask lane at index {@code N} is set then the array element at * index {@code i + indexMap[j + N]} is placed into the resulting vector * at lane index {@code N}. * * @param a the array * @param i the offset into the array, may be negative if relative * indexes in the index map compensate to produce a value within the * array bounds * @param indexMap the index map * @param j the offset into the index map * @return the vector loaded from an array * @throws IndexOutOfBoundsException if {@code j < 0}, or * {@code j > indexMap.length - this.length()}, * or for any vector lane index {@code N} where the mask at lane * {@code N} is set the result of {@code i + indexMap[j + N]} is * {@code < 0} or {@code >= a.length} */ public DoubleVector fromArray(double[] a, int i, Mask m, int[] indexMap, int j) { return op(m, n -> a[i + indexMap[j + n]]); } @Override public abstract DoubleVector fromByteArray(byte[] a, int ix); @Override public abstract DoubleVector fromByteArray(byte[] a, int ix, Mask m); @Override public abstract DoubleVector fromByteBuffer(ByteBuffer bb, int ix); @Override public abstract DoubleVector fromByteBuffer(ByteBuffer bb, int ix, Mask m); @Override public DoubleVector reshape(Vector o) { int blen = Math.max(o.species().bitSize(), bitSize()) / Byte.SIZE; ByteBuffer bb = ByteBuffer.allocate(blen).order(ByteOrder.nativeOrder()); o.intoByteBuffer(bb, 0); return fromByteBuffer(bb, 0); } @Override public abstract DoubleVector rebracket(Vector o); @Override public abstract DoubleVector resize(Vector o); @Override public abstract DoubleVector cast(Vector v); } /** * Finds the preferred species for an element type of {@code double}. *

* A preferred species is a species chosen by the platform that has a * shape of maximal bit size. A preferred species for different element * types will have the same shape, and therefore vectors, masks, and * shuffles created from such species will be shape compatible. * * @return the preferred species for an element type of {@code double} */ @SuppressWarnings("unchecked") public static DoubleSpecies preferredSpecies() { return (DoubleSpecies) Vector.preferredSpecies(double.class); } /** * Finds a species for an element type of {@code double} and shape. * * @param s the shape * @param the type of shape * @return a species for an element type of {@code double} and shape * @throws IllegalArgumentException if no such species exists for the shape */ @SuppressWarnings("unchecked") public static DoubleSpecies species(S s) { Objects.requireNonNull(s); if (s == Shapes.S_64_BIT) { return (DoubleSpecies) Double64Vector.SPECIES; } else if (s == Shapes.S_128_BIT) { return (DoubleSpecies) Double128Vector.SPECIES; } else if (s == Shapes.S_256_BIT) { return (DoubleSpecies) Double256Vector.SPECIES; } else if (s == Shapes.S_512_BIT) { return (DoubleSpecies) Double512Vector.SPECIES; } else if (s == Shapes.S_Scalable_BIT) { return (DoubleSpecies) DoubleScalableVector.SPECIES; } else { throw new IllegalArgumentException("Bad shape: " + s); } } }