< prev index next >

src/jdk.incubator.vector/share/classes/jdk/incubator/vector/LongVector.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 long[] 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 LongVector vectorFactory(long[] 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<Long> 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         long apply(int i, long a);
 148     }
 149 
 150     /*package-private*/
 151     abstract
 152     LongVector uOp(FUnOp f);
 153     @ForceInline
 154     final
 155     LongVector uOpTemplate(FUnOp f) {
 156         long[] vec = getElements();
 157         long[] res = new long[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     LongVector uOp(VectorMask<Long> m,
 167                              FUnOp f);
 168     @ForceInline
 169     final
 170     LongVector uOpTemplate(VectorMask<Long> m,
 171                                      FUnOp f) {
 172         long[] vec = getElements();
 173         long[] res = new long[length()];
 174         boolean[] mbits = ((AbstractMask<Long>)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         long apply(int i, long a, long b);
 186     }
 187 
 188     /*package-private*/
 189     abstract
 190     LongVector bOp(Vector<Long> o,
 191                              FBinOp f);
 192     @ForceInline
 193     final
 194     LongVector bOpTemplate(Vector<Long> o,
 195                                      FBinOp f) {
 196         long[] res = new long[length()];
 197         long[] vec1 = this.getElements();
 198         long[] vec2 = ((LongVector)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     LongVector bOp(Vector<Long> o,
 208                              VectorMask<Long> m,
 209                              FBinOp f);
 210     @ForceInline
 211     final
 212     LongVector bOpTemplate(Vector<Long> o,
 213                                      VectorMask<Long> m,
 214                                      FBinOp f) {
 215         long[] res = new long[length()];
 216         long[] vec1 = this.getElements();
 217         long[] vec2 = ((LongVector)o).getElements();
 218         boolean[] mbits = ((AbstractMask<Long>)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         long apply(int i, long a, long b, long c);
 230     }
 231 
 232     /*package-private*/
 233     abstract
 234     LongVector tOp(Vector<Long> o1,
 235                              Vector<Long> o2,
 236                              FTriOp f);
 237     @ForceInline
 238     final
 239     LongVector tOpTemplate(Vector<Long> o1,
 240                                      Vector<Long> o2,
 241                                      FTriOp f) {
 242         long[] res = new long[length()];
 243         long[] vec1 = this.getElements();
 244         long[] vec2 = ((LongVector)o1).getElements();
 245         long[] vec3 = ((LongVector)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     LongVector tOp(Vector<Long> o1,
 255                              Vector<Long> o2,
 256                              VectorMask<Long> m,
 257                              FTriOp f);
 258     @ForceInline
 259     final
 260     LongVector tOpTemplate(Vector<Long> o1,
 261                                      Vector<Long> o2,
 262                                      VectorMask<Long> m,
 263                                      FTriOp f) {
 264         long[] res = new long[length()];
 265         long[] vec1 = this.getElements();
 266         long[] vec2 = ((LongVector)o1).getElements();
 267         long[] vec3 = ((LongVector)o2).getElements();
 268         boolean[] mbits = ((AbstractMask<Long>)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     long rOp(long v, FBinOp f);
 280     @ForceInline
 281     final
 282     long rOpTemplate(long v, FBinOp f) {
 283         long[] 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         long apply(M memory, int offset, int i);
 295     }
 296 
 297     /*package-private*/
 298     @ForceInline
 299     final
 300     <M> LongVector ldOp(M memory, int offset,
 301                                   FLdOp<M> f) {
 302         //dummy; no vec = getElements();
 303         long[] res = new long[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> LongVector ldOp(M memory, int offset,
 314                                   VectorMask<Long> m,
 315                                   FLdOp<M> f) {
 316         //long[] vec = getElements();
 317         long[] res = new long[length()];
 318         boolean[] mbits = ((AbstractMask<Long>)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, long a);
 329     }
 330 
 331     /*package-private*/
 332     @ForceInline
 333     final
 334     <M> void stOp(M memory, int offset,
 335                   FStOp<M> f) {
 336         long[] 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<Long> m,
 347                   FStOp<M> f) {
 348         long[] vec = getElements();
 349         boolean[] mbits = ((AbstractMask<Long>)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, long a, long b);
 362     }
 363 
 364     /*package-private*/
 365     @ForceInline
 366     final
 367     AbstractMask<Long> bTest(int cond,
 368                                   Vector<Long> o,
 369                                   FBinTest f) {
 370         long[] vec1 = getElements();
 371         long[] vec2 = ((LongVector)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, long a, long 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     }


1901     @ForceInline
1902     public final LongVector blend(long e,
1903                                             VectorMask<Long> m) {
1904         return blend(broadcast(e), m);
1905     }
1906 
1907 
1908     /**
1909      * {@inheritDoc} <!--workaround-->
1910      */
1911     @Override
1912     public abstract
1913     LongVector slice(int origin, Vector<Long> v1);
1914 
1915     /*package-private*/
1916     final
1917     @ForceInline
1918     LongVector sliceTemplate(int origin, Vector<Long> v1) {
1919         LongVector that = (LongVector) v1;
1920         that.check(this);
1921         long[] a0 = this.getElements();
1922         long[] a1 = that.getElements();
1923         long[] res = new long[a0.length];
1924         int vlen = res.length;
1925         int firstPart = vlen - origin;
1926         System.arraycopy(a0, origin, res, 0, firstPart);
1927         System.arraycopy(a1, 0, res, firstPart, origin);
1928         return vectorFactory(res);
1929     }
1930 
1931     /**
1932      * {@inheritDoc} <!--workaround-->
1933      */
1934     @Override
1935     @ForceInline
1936     public final
1937     LongVector slice(int origin,
1938                                Vector<Long> w,
1939                                VectorMask<Long> m) {
1940         return broadcast(0).blend(slice(origin, w), m);
1941     }
1942 


1944      * {@inheritDoc} <!--workaround-->
1945      */
1946     @Override
1947     public abstract
1948     LongVector slice(int origin);
1949 
1950     /**
1951      * {@inheritDoc} <!--workaround-->
1952      */
1953     @Override
1954     public abstract
1955     LongVector unslice(int origin, Vector<Long> w, int part);
1956 
1957     /*package-private*/
1958     final
1959     @ForceInline
1960     LongVector
1961     unsliceTemplate(int origin, Vector<Long> w, int part) {
1962         LongVector that = (LongVector) w;
1963         that.check(this);
1964         long[] slice = this.getElements();
1965         long[] res = that.getElements();
1966         int vlen = res.length;
1967         int firstPart = vlen - origin;
1968         switch (part) {
1969         case 0:
1970             System.arraycopy(slice, 0, res, origin, firstPart);
1971             break;
1972         case 1:
1973             System.arraycopy(slice, firstPart, res, 0, origin);
1974             break;
1975         default:
1976             throw wrongPartForSlice(part);
1977         }
1978         return vectorFactory(res);
1979     }
1980 
1981     /*package-private*/
1982     final
1983     @ForceInline
1984     <M extends VectorMask<Long>>
1985     LongVector




  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 long[] 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 LongVector vectorFactory(long[] 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<Long> 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         long apply(int i, long a);
 148     }
 149 
 150     /*package-private*/
 151     abstract
 152     LongVector uOp(FUnOp f);
 153     @ForceInline
 154     final
 155     LongVector uOpTemplate(FUnOp f) {
 156         long[] vec = vec();
 157         long[] res = new long[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     LongVector uOp(VectorMask<Long> m,
 167                              FUnOp f);
 168     @ForceInline
 169     final
 170     LongVector uOpTemplate(VectorMask<Long> m,
 171                                      FUnOp f) {
 172         long[] vec = vec();
 173         long[] res = new long[length()];
 174         boolean[] mbits = ((AbstractMask<Long>)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         long apply(int i, long a, long b);
 186     }
 187 
 188     /*package-private*/
 189     abstract
 190     LongVector bOp(Vector<Long> o,
 191                              FBinOp f);
 192     @ForceInline
 193     final
 194     LongVector bOpTemplate(Vector<Long> o,
 195                                      FBinOp f) {
 196         long[] res = new long[length()];
 197         long[] vec1 = this.vec();
 198         long[] vec2 = ((LongVector)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     LongVector bOp(Vector<Long> o,
 208                              VectorMask<Long> m,
 209                              FBinOp f);
 210     @ForceInline
 211     final
 212     LongVector bOpTemplate(Vector<Long> o,
 213                                      VectorMask<Long> m,
 214                                      FBinOp f) {
 215         long[] res = new long[length()];
 216         long[] vec1 = this.vec();
 217         long[] vec2 = ((LongVector)o).vec();
 218         boolean[] mbits = ((AbstractMask<Long>)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         long apply(int i, long a, long b, long c);
 230     }
 231 
 232     /*package-private*/
 233     abstract
 234     LongVector tOp(Vector<Long> o1,
 235                              Vector<Long> o2,
 236                              FTriOp f);
 237     @ForceInline
 238     final
 239     LongVector tOpTemplate(Vector<Long> o1,
 240                                      Vector<Long> o2,
 241                                      FTriOp f) {
 242         long[] res = new long[length()];
 243         long[] vec1 = this.vec();
 244         long[] vec2 = ((LongVector)o1).vec();
 245         long[] vec3 = ((LongVector)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     LongVector tOp(Vector<Long> o1,
 255                              Vector<Long> o2,
 256                              VectorMask<Long> m,
 257                              FTriOp f);
 258     @ForceInline
 259     final
 260     LongVector tOpTemplate(Vector<Long> o1,
 261                                      Vector<Long> o2,
 262                                      VectorMask<Long> m,
 263                                      FTriOp f) {
 264         long[] res = new long[length()];
 265         long[] vec1 = this.vec();
 266         long[] vec2 = ((LongVector)o1).vec();
 267         long[] vec3 = ((LongVector)o2).vec();
 268         boolean[] mbits = ((AbstractMask<Long>)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     long rOp(long v, FBinOp f);
 280     @ForceInline
 281     final
 282     long rOpTemplate(long v, FBinOp f) {
 283         long[] 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         long apply(M memory, int offset, int i);
 295     }
 296 
 297     /*package-private*/
 298     @ForceInline
 299     final
 300     <M> LongVector ldOp(M memory, int offset,
 301                                   FLdOp<M> f) {
 302         //dummy; no vec = vec();
 303         long[] res = new long[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> LongVector ldOp(M memory, int offset,
 314                                   VectorMask<Long> m,
 315                                   FLdOp<M> f) {
 316         //long[] vec = vec();
 317         long[] res = new long[length()];
 318         boolean[] mbits = ((AbstractMask<Long>)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, long a);
 329     }
 330 
 331     /*package-private*/
 332     @ForceInline
 333     final
 334     <M> void stOp(M memory, int offset,
 335                   FStOp<M> f) {
 336         long[] 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<Long> m,
 347                   FStOp<M> f) {
 348         long[] vec = vec();
 349         boolean[] mbits = ((AbstractMask<Long>)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, long a, long b);
 362     }
 363 
 364     /*package-private*/
 365     @ForceInline
 366     final
 367     AbstractMask<Long> bTest(int cond,
 368                                   Vector<Long> o,
 369                                   FBinTest f) {
 370         long[] vec1 = vec();
 371         long[] vec2 = ((LongVector)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, long a, long 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     }


1901     @ForceInline
1902     public final LongVector blend(long e,
1903                                             VectorMask<Long> m) {
1904         return blend(broadcast(e), m);
1905     }
1906 
1907 
1908     /**
1909      * {@inheritDoc} <!--workaround-->
1910      */
1911     @Override
1912     public abstract
1913     LongVector slice(int origin, Vector<Long> v1);
1914 
1915     /*package-private*/
1916     final
1917     @ForceInline
1918     LongVector sliceTemplate(int origin, Vector<Long> v1) {
1919         LongVector that = (LongVector) v1;
1920         that.check(this);
1921         long[] a0 = this.vec();
1922         long[] a1 = that.vec();
1923         long[] res = new long[a0.length];
1924         int vlen = res.length;
1925         int firstPart = vlen - origin;
1926         System.arraycopy(a0, origin, res, 0, firstPart);
1927         System.arraycopy(a1, 0, res, firstPart, origin);
1928         return vectorFactory(res);
1929     }
1930 
1931     /**
1932      * {@inheritDoc} <!--workaround-->
1933      */
1934     @Override
1935     @ForceInline
1936     public final
1937     LongVector slice(int origin,
1938                                Vector<Long> w,
1939                                VectorMask<Long> m) {
1940         return broadcast(0).blend(slice(origin, w), m);
1941     }
1942 


1944      * {@inheritDoc} <!--workaround-->
1945      */
1946     @Override
1947     public abstract
1948     LongVector slice(int origin);
1949 
1950     /**
1951      * {@inheritDoc} <!--workaround-->
1952      */
1953     @Override
1954     public abstract
1955     LongVector unslice(int origin, Vector<Long> w, int part);
1956 
1957     /*package-private*/
1958     final
1959     @ForceInline
1960     LongVector
1961     unsliceTemplate(int origin, Vector<Long> w, int part) {
1962         LongVector that = (LongVector) w;
1963         that.check(this);
1964         long[] slice = this.vec();
1965         long[] res = that.vec().clone();
1966         int vlen = res.length;
1967         int firstPart = vlen - origin;
1968         switch (part) {
1969         case 0:
1970             System.arraycopy(slice, 0, res, origin, firstPart);
1971             break;
1972         case 1:
1973             System.arraycopy(slice, firstPart, res, 0, origin);
1974             break;
1975         default:
1976             throw wrongPartForSlice(part);
1977         }
1978         return vectorFactory(res);
1979     }
1980 
1981     /*package-private*/
1982     final
1983     @ForceInline
1984     <M extends VectorMask<Long>>
1985     LongVector


< prev index next >