< prev index next >

src/jdk.incubator.vector/share/classes/jdk/incubator/vector/FloatVector.java

Print this page
rev 58165 : Fix the issue that unslice modifies the argument vector
Summary: Change the name of getElements() to vec() and clone argument value to
avoid it being modified


  72     // coded with portable definitions.
  73     // These are all @ForceInline in case
  74     // they need to be used performantly.
  75     // The various shape-specific subclasses
  76     // also specialize them by wrapping
  77     // them in a call like this:
  78     //    return (Byte128Vector)
  79     //       super.bOp((Byte128Vector) o);
  80     // The purpose of that is to forcibly inline
  81     // the generic definition from this file
  82     // into a sharply type- and size-specific
  83     // wrapper in the subclass file, so that
  84     // the JIT can specialize the code.
  85     // The code is only inlined and expanded
  86     // if it gets hot.  Think of it as a cheap
  87     // and lazy version of C++ templates.
  88 
  89     // Virtualized getter
  90 
  91     /*package-private*/
  92     abstract float[] getElements();
  93 
  94     // Virtualized constructors
  95 
  96     /**
  97      * Build a vector directly using my own constructor.
  98      * It is an error if the array is aliased elsewhere.
  99      */
 100     /*package-private*/
 101     abstract FloatVector vectorFactory(float[] vec);
 102 
 103     /**
 104      * Build a mask directly using my species.
 105      * It is an error if the array is aliased elsewhere.
 106      */
 107     /*package-private*/
 108     @ForceInline
 109     final
 110     AbstractMask<Float> maskFactory(boolean[] bits) {
 111         return vspecies().maskFactory(bits);
 112     }


 136             if (mbits[i]) {
 137                 res[i] = f.apply(i);
 138             }
 139         }
 140         return vectorFactory(res);
 141     }
 142 
 143     // Unary operator
 144 
 145     /*package-private*/
 146     interface FUnOp {
 147         float apply(int i, float a);
 148     }
 149 
 150     /*package-private*/
 151     abstract
 152     FloatVector uOp(FUnOp f);
 153     @ForceInline
 154     final
 155     FloatVector uOpTemplate(FUnOp f) {
 156         float[] vec = getElements();
 157         float[] res = new float[length()];
 158         for (int i = 0; i < res.length; i++) {
 159             res[i] = f.apply(i, vec[i]);
 160         }
 161         return vectorFactory(res);
 162     }
 163 
 164     /*package-private*/
 165     abstract
 166     FloatVector uOp(VectorMask<Float> m,
 167                              FUnOp f);
 168     @ForceInline
 169     final
 170     FloatVector uOpTemplate(VectorMask<Float> m,
 171                                      FUnOp f) {
 172         float[] vec = getElements();
 173         float[] res = new float[length()];
 174         boolean[] mbits = ((AbstractMask<Float>)m).getBits();
 175         for (int i = 0; i < res.length; i++) {
 176             res[i] = mbits[i] ? f.apply(i, vec[i]) : vec[i];
 177         }
 178         return vectorFactory(res);
 179     }
 180 
 181     // Binary operator
 182 
 183     /*package-private*/
 184     interface FBinOp {
 185         float apply(int i, float a, float b);
 186     }
 187 
 188     /*package-private*/
 189     abstract
 190     FloatVector bOp(Vector<Float> o,
 191                              FBinOp f);
 192     @ForceInline
 193     final
 194     FloatVector bOpTemplate(Vector<Float> o,
 195                                      FBinOp f) {
 196         float[] res = new float[length()];
 197         float[] vec1 = this.getElements();
 198         float[] vec2 = ((FloatVector)o).getElements();
 199         for (int i = 0; i < res.length; i++) {
 200             res[i] = f.apply(i, vec1[i], vec2[i]);
 201         }
 202         return vectorFactory(res);
 203     }
 204 
 205     /*package-private*/
 206     abstract
 207     FloatVector bOp(Vector<Float> o,
 208                              VectorMask<Float> m,
 209                              FBinOp f);
 210     @ForceInline
 211     final
 212     FloatVector bOpTemplate(Vector<Float> o,
 213                                      VectorMask<Float> m,
 214                                      FBinOp f) {
 215         float[] res = new float[length()];
 216         float[] vec1 = this.getElements();
 217         float[] vec2 = ((FloatVector)o).getElements();
 218         boolean[] mbits = ((AbstractMask<Float>)m).getBits();
 219         for (int i = 0; i < res.length; i++) {
 220             res[i] = mbits[i] ? f.apply(i, vec1[i], vec2[i]) : vec1[i];
 221         }
 222         return vectorFactory(res);
 223     }
 224 
 225     // Ternary operator
 226 
 227     /*package-private*/
 228     interface FTriOp {
 229         float apply(int i, float a, float b, float c);
 230     }
 231 
 232     /*package-private*/
 233     abstract
 234     FloatVector tOp(Vector<Float> o1,
 235                              Vector<Float> o2,
 236                              FTriOp f);
 237     @ForceInline
 238     final
 239     FloatVector tOpTemplate(Vector<Float> o1,
 240                                      Vector<Float> o2,
 241                                      FTriOp f) {
 242         float[] res = new float[length()];
 243         float[] vec1 = this.getElements();
 244         float[] vec2 = ((FloatVector)o1).getElements();
 245         float[] vec3 = ((FloatVector)o2).getElements();
 246         for (int i = 0; i < res.length; i++) {
 247             res[i] = f.apply(i, vec1[i], vec2[i], vec3[i]);
 248         }
 249         return vectorFactory(res);
 250     }
 251 
 252     /*package-private*/
 253     abstract
 254     FloatVector tOp(Vector<Float> o1,
 255                              Vector<Float> o2,
 256                              VectorMask<Float> m,
 257                              FTriOp f);
 258     @ForceInline
 259     final
 260     FloatVector tOpTemplate(Vector<Float> o1,
 261                                      Vector<Float> o2,
 262                                      VectorMask<Float> m,
 263                                      FTriOp f) {
 264         float[] res = new float[length()];
 265         float[] vec1 = this.getElements();
 266         float[] vec2 = ((FloatVector)o1).getElements();
 267         float[] vec3 = ((FloatVector)o2).getElements();
 268         boolean[] mbits = ((AbstractMask<Float>)m).getBits();
 269         for (int i = 0; i < res.length; i++) {
 270             res[i] = mbits[i] ? f.apply(i, vec1[i], vec2[i], vec3[i]) : vec1[i];
 271         }
 272         return vectorFactory(res);
 273     }
 274 
 275     // Reduction operator
 276 
 277     /*package-private*/
 278     abstract
 279     float rOp(float v, FBinOp f);
 280     @ForceInline
 281     final
 282     float rOpTemplate(float v, FBinOp f) {
 283         float[] vec = getElements();
 284         for (int i = 0; i < vec.length; i++) {
 285             v = f.apply(i, v, vec[i]);
 286         }
 287         return v;
 288     }
 289 
 290     // Memory reference
 291 
 292     /*package-private*/
 293     interface FLdOp<M> {
 294         float apply(M memory, int offset, int i);
 295     }
 296 
 297     /*package-private*/
 298     @ForceInline
 299     final
 300     <M> FloatVector ldOp(M memory, int offset,
 301                                   FLdOp<M> f) {
 302         //dummy; no vec = getElements();
 303         float[] res = new float[length()];
 304         for (int i = 0; i < res.length; i++) {
 305             res[i] = f.apply(memory, offset, i);
 306         }
 307         return vectorFactory(res);
 308     }
 309 
 310     /*package-private*/
 311     @ForceInline
 312     final
 313     <M> FloatVector ldOp(M memory, int offset,
 314                                   VectorMask<Float> m,
 315                                   FLdOp<M> f) {
 316         //float[] vec = getElements();
 317         float[] res = new float[length()];
 318         boolean[] mbits = ((AbstractMask<Float>)m).getBits();
 319         for (int i = 0; i < res.length; i++) {
 320             if (mbits[i]) {
 321                 res[i] = f.apply(memory, offset, i);
 322             }
 323         }
 324         return vectorFactory(res);
 325     }
 326 
 327     interface FStOp<M> {
 328         void apply(M memory, int offset, int i, float a);
 329     }
 330 
 331     /*package-private*/
 332     @ForceInline
 333     final
 334     <M> void stOp(M memory, int offset,
 335                   FStOp<M> f) {
 336         float[] vec = getElements();
 337         for (int i = 0; i < vec.length; i++) {
 338             f.apply(memory, offset, i, vec[i]);
 339         }
 340     }
 341 
 342     /*package-private*/
 343     @ForceInline
 344     final
 345     <M> void stOp(M memory, int offset,
 346                   VectorMask<Float> m,
 347                   FStOp<M> f) {
 348         float[] vec = getElements();
 349         boolean[] mbits = ((AbstractMask<Float>)m).getBits();
 350         for (int i = 0; i < vec.length; i++) {
 351             if (mbits[i]) {
 352                 f.apply(memory, offset, i, vec[i]);
 353             }
 354         }
 355     }
 356 
 357     // Binary test
 358 
 359     /*package-private*/
 360     interface FBinTest {
 361         boolean apply(int cond, int i, float a, float b);
 362     }
 363 
 364     /*package-private*/
 365     @ForceInline
 366     final
 367     AbstractMask<Float> bTest(int cond,
 368                                   Vector<Float> o,
 369                                   FBinTest f) {
 370         float[] vec1 = getElements();
 371         float[] vec2 = ((FloatVector)o).getElements();
 372         boolean[] bits = new boolean[length()];
 373         for (int i = 0; i < length(); i++){
 374             bits[i] = f.apply(cond, i, vec1[i], vec2[i]);
 375         }
 376         return maskFactory(bits);
 377     }
 378 
 379     /*package-private*/
 380     @ForceInline
 381     static boolean doBinTest(int cond, float a, float b) {
 382         switch (cond) {
 383         case BT_eq:  return a == b;
 384         case BT_ne:  return a != b;
 385         case BT_lt:  return a < b;
 386         case BT_le:  return a <= b;
 387         case BT_gt:  return a > b;
 388         case BT_ge:  return a >= b;
 389         }
 390         throw new AssertionError(Integer.toHexString(cond));
 391     }


1929      */
1930     @ForceInline
1931     public final FloatVector blend(long e,
1932                                             VectorMask<Float> m) {
1933         return blend(broadcast(e), m);
1934     }
1935 
1936     /**
1937      * {@inheritDoc} <!--workaround-->
1938      */
1939     @Override
1940     public abstract
1941     FloatVector slice(int origin, Vector<Float> v1);
1942 
1943     /*package-private*/
1944     final
1945     @ForceInline
1946     FloatVector sliceTemplate(int origin, Vector<Float> v1) {
1947         FloatVector that = (FloatVector) v1;
1948         that.check(this);
1949         float[] a0 = this.getElements();
1950         float[] a1 = that.getElements();
1951         float[] res = new float[a0.length];
1952         int vlen = res.length;
1953         int firstPart = vlen - origin;
1954         System.arraycopy(a0, origin, res, 0, firstPart);
1955         System.arraycopy(a1, 0, res, firstPart, origin);
1956         return vectorFactory(res);
1957     }
1958 
1959     /**
1960      * {@inheritDoc} <!--workaround-->
1961      */
1962     @Override
1963     @ForceInline
1964     public final
1965     FloatVector slice(int origin,
1966                                Vector<Float> w,
1967                                VectorMask<Float> m) {
1968         return broadcast(0).blend(slice(origin, w), m);
1969     }
1970 


1972      * {@inheritDoc} <!--workaround-->
1973      */
1974     @Override
1975     public abstract
1976     FloatVector slice(int origin);
1977 
1978     /**
1979      * {@inheritDoc} <!--workaround-->
1980      */
1981     @Override
1982     public abstract
1983     FloatVector unslice(int origin, Vector<Float> w, int part);
1984 
1985     /*package-private*/
1986     final
1987     @ForceInline
1988     FloatVector
1989     unsliceTemplate(int origin, Vector<Float> w, int part) {
1990         FloatVector that = (FloatVector) w;
1991         that.check(this);
1992         float[] slice = this.getElements();
1993         float[] res = that.getElements();
1994         int vlen = res.length;
1995         int firstPart = vlen - origin;
1996         switch (part) {
1997         case 0:
1998             System.arraycopy(slice, 0, res, origin, firstPart);
1999             break;
2000         case 1:
2001             System.arraycopy(slice, firstPart, res, 0, origin);
2002             break;
2003         default:
2004             throw wrongPartForSlice(part);
2005         }
2006         return vectorFactory(res);
2007     }
2008 
2009     /*package-private*/
2010     final
2011     @ForceInline
2012     <M extends VectorMask<Float>>
2013     FloatVector




  72     // coded with portable definitions.
  73     // These are all @ForceInline in case
  74     // they need to be used performantly.
  75     // The various shape-specific subclasses
  76     // also specialize them by wrapping
  77     // them in a call like this:
  78     //    return (Byte128Vector)
  79     //       super.bOp((Byte128Vector) o);
  80     // The purpose of that is to forcibly inline
  81     // the generic definition from this file
  82     // into a sharply type- and size-specific
  83     // wrapper in the subclass file, so that
  84     // the JIT can specialize the code.
  85     // The code is only inlined and expanded
  86     // if it gets hot.  Think of it as a cheap
  87     // and lazy version of C++ templates.
  88 
  89     // Virtualized getter
  90 
  91     /*package-private*/
  92     abstract float[] vec();
  93 
  94     // Virtualized constructors
  95 
  96     /**
  97      * Build a vector directly using my own constructor.
  98      * It is an error if the array is aliased elsewhere.
  99      */
 100     /*package-private*/
 101     abstract FloatVector vectorFactory(float[] vec);
 102 
 103     /**
 104      * Build a mask directly using my species.
 105      * It is an error if the array is aliased elsewhere.
 106      */
 107     /*package-private*/
 108     @ForceInline
 109     final
 110     AbstractMask<Float> maskFactory(boolean[] bits) {
 111         return vspecies().maskFactory(bits);
 112     }


 136             if (mbits[i]) {
 137                 res[i] = f.apply(i);
 138             }
 139         }
 140         return vectorFactory(res);
 141     }
 142 
 143     // Unary operator
 144 
 145     /*package-private*/
 146     interface FUnOp {
 147         float apply(int i, float a);
 148     }
 149 
 150     /*package-private*/
 151     abstract
 152     FloatVector uOp(FUnOp f);
 153     @ForceInline
 154     final
 155     FloatVector uOpTemplate(FUnOp f) {
 156         float[] vec = vec();
 157         float[] res = new float[length()];
 158         for (int i = 0; i < res.length; i++) {
 159             res[i] = f.apply(i, vec[i]);
 160         }
 161         return vectorFactory(res);
 162     }
 163 
 164     /*package-private*/
 165     abstract
 166     FloatVector uOp(VectorMask<Float> m,
 167                              FUnOp f);
 168     @ForceInline
 169     final
 170     FloatVector uOpTemplate(VectorMask<Float> m,
 171                                      FUnOp f) {
 172         float[] vec = vec();
 173         float[] res = new float[length()];
 174         boolean[] mbits = ((AbstractMask<Float>)m).getBits();
 175         for (int i = 0; i < res.length; i++) {
 176             res[i] = mbits[i] ? f.apply(i, vec[i]) : vec[i];
 177         }
 178         return vectorFactory(res);
 179     }
 180 
 181     // Binary operator
 182 
 183     /*package-private*/
 184     interface FBinOp {
 185         float apply(int i, float a, float b);
 186     }
 187 
 188     /*package-private*/
 189     abstract
 190     FloatVector bOp(Vector<Float> o,
 191                              FBinOp f);
 192     @ForceInline
 193     final
 194     FloatVector bOpTemplate(Vector<Float> o,
 195                                      FBinOp f) {
 196         float[] res = new float[length()];
 197         float[] vec1 = this.vec();
 198         float[] vec2 = ((FloatVector)o).vec();
 199         for (int i = 0; i < res.length; i++) {
 200             res[i] = f.apply(i, vec1[i], vec2[i]);
 201         }
 202         return vectorFactory(res);
 203     }
 204 
 205     /*package-private*/
 206     abstract
 207     FloatVector bOp(Vector<Float> o,
 208                              VectorMask<Float> m,
 209                              FBinOp f);
 210     @ForceInline
 211     final
 212     FloatVector bOpTemplate(Vector<Float> o,
 213                                      VectorMask<Float> m,
 214                                      FBinOp f) {
 215         float[] res = new float[length()];
 216         float[] vec1 = this.vec();
 217         float[] vec2 = ((FloatVector)o).vec();
 218         boolean[] mbits = ((AbstractMask<Float>)m).getBits();
 219         for (int i = 0; i < res.length; i++) {
 220             res[i] = mbits[i] ? f.apply(i, vec1[i], vec2[i]) : vec1[i];
 221         }
 222         return vectorFactory(res);
 223     }
 224 
 225     // Ternary operator
 226 
 227     /*package-private*/
 228     interface FTriOp {
 229         float apply(int i, float a, float b, float c);
 230     }
 231 
 232     /*package-private*/
 233     abstract
 234     FloatVector tOp(Vector<Float> o1,
 235                              Vector<Float> o2,
 236                              FTriOp f);
 237     @ForceInline
 238     final
 239     FloatVector tOpTemplate(Vector<Float> o1,
 240                                      Vector<Float> o2,
 241                                      FTriOp f) {
 242         float[] res = new float[length()];
 243         float[] vec1 = this.vec();
 244         float[] vec2 = ((FloatVector)o1).vec();
 245         float[] vec3 = ((FloatVector)o2).vec();
 246         for (int i = 0; i < res.length; i++) {
 247             res[i] = f.apply(i, vec1[i], vec2[i], vec3[i]);
 248         }
 249         return vectorFactory(res);
 250     }
 251 
 252     /*package-private*/
 253     abstract
 254     FloatVector tOp(Vector<Float> o1,
 255                              Vector<Float> o2,
 256                              VectorMask<Float> m,
 257                              FTriOp f);
 258     @ForceInline
 259     final
 260     FloatVector tOpTemplate(Vector<Float> o1,
 261                                      Vector<Float> o2,
 262                                      VectorMask<Float> m,
 263                                      FTriOp f) {
 264         float[] res = new float[length()];
 265         float[] vec1 = this.vec();
 266         float[] vec2 = ((FloatVector)o1).vec();
 267         float[] vec3 = ((FloatVector)o2).vec();
 268         boolean[] mbits = ((AbstractMask<Float>)m).getBits();
 269         for (int i = 0; i < res.length; i++) {
 270             res[i] = mbits[i] ? f.apply(i, vec1[i], vec2[i], vec3[i]) : vec1[i];
 271         }
 272         return vectorFactory(res);
 273     }
 274 
 275     // Reduction operator
 276 
 277     /*package-private*/
 278     abstract
 279     float rOp(float v, FBinOp f);
 280     @ForceInline
 281     final
 282     float rOpTemplate(float v, FBinOp f) {
 283         float[] vec = vec();
 284         for (int i = 0; i < vec.length; i++) {
 285             v = f.apply(i, v, vec[i]);
 286         }
 287         return v;
 288     }
 289 
 290     // Memory reference
 291 
 292     /*package-private*/
 293     interface FLdOp<M> {
 294         float apply(M memory, int offset, int i);
 295     }
 296 
 297     /*package-private*/
 298     @ForceInline
 299     final
 300     <M> FloatVector ldOp(M memory, int offset,
 301                                   FLdOp<M> f) {
 302         //dummy; no vec = vec();
 303         float[] res = new float[length()];
 304         for (int i = 0; i < res.length; i++) {
 305             res[i] = f.apply(memory, offset, i);
 306         }
 307         return vectorFactory(res);
 308     }
 309 
 310     /*package-private*/
 311     @ForceInline
 312     final
 313     <M> FloatVector ldOp(M memory, int offset,
 314                                   VectorMask<Float> m,
 315                                   FLdOp<M> f) {
 316         //float[] vec = vec();
 317         float[] res = new float[length()];
 318         boolean[] mbits = ((AbstractMask<Float>)m).getBits();
 319         for (int i = 0; i < res.length; i++) {
 320             if (mbits[i]) {
 321                 res[i] = f.apply(memory, offset, i);
 322             }
 323         }
 324         return vectorFactory(res);
 325     }
 326 
 327     interface FStOp<M> {
 328         void apply(M memory, int offset, int i, float a);
 329     }
 330 
 331     /*package-private*/
 332     @ForceInline
 333     final
 334     <M> void stOp(M memory, int offset,
 335                   FStOp<M> f) {
 336         float[] vec = vec();
 337         for (int i = 0; i < vec.length; i++) {
 338             f.apply(memory, offset, i, vec[i]);
 339         }
 340     }
 341 
 342     /*package-private*/
 343     @ForceInline
 344     final
 345     <M> void stOp(M memory, int offset,
 346                   VectorMask<Float> m,
 347                   FStOp<M> f) {
 348         float[] vec = vec();
 349         boolean[] mbits = ((AbstractMask<Float>)m).getBits();
 350         for (int i = 0; i < vec.length; i++) {
 351             if (mbits[i]) {
 352                 f.apply(memory, offset, i, vec[i]);
 353             }
 354         }
 355     }
 356 
 357     // Binary test
 358 
 359     /*package-private*/
 360     interface FBinTest {
 361         boolean apply(int cond, int i, float a, float b);
 362     }
 363 
 364     /*package-private*/
 365     @ForceInline
 366     final
 367     AbstractMask<Float> bTest(int cond,
 368                                   Vector<Float> o,
 369                                   FBinTest f) {
 370         float[] vec1 = vec();
 371         float[] vec2 = ((FloatVector)o).vec();
 372         boolean[] bits = new boolean[length()];
 373         for (int i = 0; i < length(); i++){
 374             bits[i] = f.apply(cond, i, vec1[i], vec2[i]);
 375         }
 376         return maskFactory(bits);
 377     }
 378 
 379     /*package-private*/
 380     @ForceInline
 381     static boolean doBinTest(int cond, float a, float b) {
 382         switch (cond) {
 383         case BT_eq:  return a == b;
 384         case BT_ne:  return a != b;
 385         case BT_lt:  return a < b;
 386         case BT_le:  return a <= b;
 387         case BT_gt:  return a > b;
 388         case BT_ge:  return a >= b;
 389         }
 390         throw new AssertionError(Integer.toHexString(cond));
 391     }


1929      */
1930     @ForceInline
1931     public final FloatVector blend(long e,
1932                                             VectorMask<Float> m) {
1933         return blend(broadcast(e), m);
1934     }
1935 
1936     /**
1937      * {@inheritDoc} <!--workaround-->
1938      */
1939     @Override
1940     public abstract
1941     FloatVector slice(int origin, Vector<Float> v1);
1942 
1943     /*package-private*/
1944     final
1945     @ForceInline
1946     FloatVector sliceTemplate(int origin, Vector<Float> v1) {
1947         FloatVector that = (FloatVector) v1;
1948         that.check(this);
1949         float[] a0 = this.vec();
1950         float[] a1 = that.vec();
1951         float[] res = new float[a0.length];
1952         int vlen = res.length;
1953         int firstPart = vlen - origin;
1954         System.arraycopy(a0, origin, res, 0, firstPart);
1955         System.arraycopy(a1, 0, res, firstPart, origin);
1956         return vectorFactory(res);
1957     }
1958 
1959     /**
1960      * {@inheritDoc} <!--workaround-->
1961      */
1962     @Override
1963     @ForceInline
1964     public final
1965     FloatVector slice(int origin,
1966                                Vector<Float> w,
1967                                VectorMask<Float> m) {
1968         return broadcast(0).blend(slice(origin, w), m);
1969     }
1970 


1972      * {@inheritDoc} <!--workaround-->
1973      */
1974     @Override
1975     public abstract
1976     FloatVector slice(int origin);
1977 
1978     /**
1979      * {@inheritDoc} <!--workaround-->
1980      */
1981     @Override
1982     public abstract
1983     FloatVector unslice(int origin, Vector<Float> w, int part);
1984 
1985     /*package-private*/
1986     final
1987     @ForceInline
1988     FloatVector
1989     unsliceTemplate(int origin, Vector<Float> w, int part) {
1990         FloatVector that = (FloatVector) w;
1991         that.check(this);
1992         float[] slice = this.vec();
1993         float[] res = that.vec().clone();
1994         int vlen = res.length;
1995         int firstPart = vlen - origin;
1996         switch (part) {
1997         case 0:
1998             System.arraycopy(slice, 0, res, origin, firstPart);
1999             break;
2000         case 1:
2001             System.arraycopy(slice, firstPart, res, 0, origin);
2002             break;
2003         default:
2004             throw wrongPartForSlice(part);
2005         }
2006         return vectorFactory(res);
2007     }
2008 
2009     /*package-private*/
2010     final
2011     @ForceInline
2012     <M extends VectorMask<Float>>
2013     FloatVector


< prev index next >