1 /* 2 * Copyright (c) 2017, Oracle and/or its affiliates. All rights reserved. 3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. 4 * 5 * This code is free software; you can redistribute it and/or modify it 6 * under the terms of the GNU General Public License version 2 only, as 7 * published by the Free Software Foundation. Oracle designates this 8 * particular file as subject to the "Classpath" exception as provided 9 * by Oracle in the LICENSE file that accompanied this code. 10 * 11 * This code is distributed in the hope that it will be useful, but WITHOUT 12 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or 13 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License 14 * version 2 for more details (a copy is included in the LICENSE file that 15 * accompanied this code). 16 * 17 * You should have received a copy of the GNU General Public License version 18 * 2 along with this work; if not, write to the Free Software Foundation, 19 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. 20 * 21 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA 22 * or visit www.oracle.com if you need additional information or have 23 * questions. 24 */ 25 package jdk.incubator.vector; 26 27 import java.nio.ByteBuffer; 28 import java.nio.ByteOrder; 29 import java.nio.ShortBuffer; 30 import java.nio.ReadOnlyBufferException; 31 import java.util.Arrays; 32 import java.util.Objects; 33 import java.util.function.IntUnaryOperator; 34 35 import jdk.internal.misc.Unsafe; 36 import jdk.internal.vm.annotation.ForceInline; 37 import static jdk.incubator.vector.VectorIntrinsics.*; 38 39 @SuppressWarnings("cast") 40 final class Short256Vector extends ShortVector { 41 private static final VectorSpecies<Short> SPECIES = ShortVector.SPECIES_256; 42 43 static final Short256Vector ZERO = new Short256Vector(); 44 45 static final int LENGTH = SPECIES.length(); 46 47 private final short[] vec; // Don't access directly, use getElements() instead. 48 49 private short[] getElements() { 50 return VectorIntrinsics.maybeRebox(this).vec; 51 } 52 53 Short256Vector() { 54 vec = new short[SPECIES.length()]; 55 } 56 57 Short256Vector(short[] v) { 58 vec = v; 59 } 60 61 @Override 62 public int length() { return LENGTH; } 63 64 // Unary operator 65 66 @Override 67 Short256Vector uOp(FUnOp f) { 68 short[] vec = getElements(); 69 short[] res = new short[length()]; 70 for (int i = 0; i < length(); i++) { 71 res[i] = f.apply(i, vec[i]); 72 } 73 return new Short256Vector(res); 74 } 75 76 @Override 77 Short256Vector uOp(VectorMask<Short> o, FUnOp f) { 78 short[] vec = getElements(); 79 short[] res = new short[length()]; 80 boolean[] mbits = ((Short256Mask)o).getBits(); 81 for (int i = 0; i < length(); i++) { 82 res[i] = mbits[i] ? f.apply(i, vec[i]) : vec[i]; 83 } 84 return new Short256Vector(res); 85 } 86 87 // Binary operator 88 89 @Override 90 Short256Vector bOp(Vector<Short> o, FBinOp f) { 91 short[] res = new short[length()]; 92 short[] vec1 = this.getElements(); 93 short[] vec2 = ((Short256Vector)o).getElements(); 94 for (int i = 0; i < length(); i++) { 95 res[i] = f.apply(i, vec1[i], vec2[i]); 96 } 97 return new Short256Vector(res); 98 } 99 100 @Override 101 Short256Vector bOp(Vector<Short> o1, VectorMask<Short> o2, FBinOp f) { 102 short[] res = new short[length()]; 103 short[] vec1 = this.getElements(); 104 short[] vec2 = ((Short256Vector)o1).getElements(); 105 boolean[] mbits = ((Short256Mask)o2).getBits(); 106 for (int i = 0; i < length(); i++) { 107 res[i] = mbits[i] ? f.apply(i, vec1[i], vec2[i]) : vec1[i]; 108 } 109 return new Short256Vector(res); 110 } 111 112 // Trinary operator 113 114 @Override 115 Short256Vector tOp(Vector<Short> o1, Vector<Short> o2, FTriOp f) { 116 short[] res = new short[length()]; 117 short[] vec1 = this.getElements(); 118 short[] vec2 = ((Short256Vector)o1).getElements(); 119 short[] vec3 = ((Short256Vector)o2).getElements(); 120 for (int i = 0; i < length(); i++) { 121 res[i] = f.apply(i, vec1[i], vec2[i], vec3[i]); 122 } 123 return new Short256Vector(res); 124 } 125 126 @Override 127 Short256Vector tOp(Vector<Short> o1, Vector<Short> o2, VectorMask<Short> o3, FTriOp f) { 128 short[] res = new short[length()]; 129 short[] vec1 = getElements(); 130 short[] vec2 = ((Short256Vector)o1).getElements(); 131 short[] vec3 = ((Short256Vector)o2).getElements(); 132 boolean[] mbits = ((Short256Mask)o3).getBits(); 133 for (int i = 0; i < length(); i++) { 134 res[i] = mbits[i] ? f.apply(i, vec1[i], vec2[i], vec3[i]) : vec1[i]; 135 } 136 return new Short256Vector(res); 137 } 138 139 @Override 140 short rOp(short v, FBinOp f) { 141 short[] vec = getElements(); 142 for (int i = 0; i < length(); i++) { 143 v = f.apply(i, v, vec[i]); 144 } 145 return v; 146 } 147 148 @Override 149 @ForceInline 150 public <F> Vector<F> cast(VectorSpecies<F> s) { 151 Objects.requireNonNull(s); 152 if (s.length() != LENGTH) 153 throw new IllegalArgumentException("Vector length this species length differ"); 154 155 return VectorIntrinsics.cast( 156 Short256Vector.class, 157 short.class, LENGTH, 158 s.boxType(), 159 s.elementType(), LENGTH, 160 this, s, 161 (species, vector) -> vector.castDefault(species) 162 ); 163 } 164 165 @SuppressWarnings("unchecked") 166 @ForceInline 167 private <F> Vector<F> castDefault(VectorSpecies<F> s) { 168 int limit = s.length(); 169 170 Class<?> stype = s.elementType(); 171 if (stype == byte.class) { 172 byte[] a = new byte[limit]; 173 for (int i = 0; i < limit; i++) { 174 a[i] = (byte) this.lane(i); 175 } 176 return (Vector) ByteVector.fromArray((VectorSpecies<Byte>) s, a, 0); 177 } else if (stype == short.class) { 178 short[] a = new short[limit]; 179 for (int i = 0; i < limit; i++) { 180 a[i] = (short) this.lane(i); 181 } 182 return (Vector) ShortVector.fromArray((VectorSpecies<Short>) s, a, 0); 183 } else if (stype == int.class) { 184 int[] a = new int[limit]; 185 for (int i = 0; i < limit; i++) { 186 a[i] = (int) this.lane(i); 187 } 188 return (Vector) IntVector.fromArray((VectorSpecies<Integer>) s, a, 0); 189 } else if (stype == long.class) { 190 long[] a = new long[limit]; 191 for (int i = 0; i < limit; i++) { 192 a[i] = (long) this.lane(i); 193 } 194 return (Vector) LongVector.fromArray((VectorSpecies<Long>) s, a, 0); 195 } else if (stype == float.class) { 196 float[] a = new float[limit]; 197 for (int i = 0; i < limit; i++) { 198 a[i] = (float) this.lane(i); 199 } 200 return (Vector) FloatVector.fromArray((VectorSpecies<Float>) s, a, 0); 201 } else if (stype == double.class) { 202 double[] a = new double[limit]; 203 for (int i = 0; i < limit; i++) { 204 a[i] = (double) this.lane(i); 205 } 206 return (Vector) DoubleVector.fromArray((VectorSpecies<Double>) s, a, 0); 207 } else { 208 throw new UnsupportedOperationException("Bad lane type for casting."); 209 } 210 } 211 212 @Override 213 @ForceInline 214 @SuppressWarnings("unchecked") 215 public <F> Vector<F> reinterpret(VectorSpecies<F> s) { 216 Objects.requireNonNull(s); 217 218 if(s.elementType().equals(short.class)) { 219 return (Vector<F>) reshape((VectorSpecies<Short>)s); 220 } 221 if(s.bitSize() == bitSize()) { 222 return reinterpretType(s); 223 } 224 225 return defaultReinterpret(s); 226 } 227 228 @ForceInline 229 private <F> Vector<F> reinterpretType(VectorSpecies<F> s) { 230 Objects.requireNonNull(s); 231 232 Class<?> stype = s.elementType(); 233 if (stype == byte.class) { 234 return VectorIntrinsics.reinterpret( 235 Short256Vector.class, 236 short.class, LENGTH, 237 Byte256Vector.class, 238 byte.class, Byte256Vector.LENGTH, 239 this, s, 240 (species, vector) -> vector.defaultReinterpret(species) 241 ); 242 } else if (stype == short.class) { 243 return VectorIntrinsics.reinterpret( 244 Short256Vector.class, 245 short.class, LENGTH, 246 Short256Vector.class, 247 short.class, Short256Vector.LENGTH, 248 this, s, 249 (species, vector) -> vector.defaultReinterpret(species) 250 ); 251 } else if (stype == int.class) { 252 return VectorIntrinsics.reinterpret( 253 Short256Vector.class, 254 short.class, LENGTH, 255 Int256Vector.class, 256 int.class, Int256Vector.LENGTH, 257 this, s, 258 (species, vector) -> vector.defaultReinterpret(species) 259 ); 260 } else if (stype == long.class) { 261 return VectorIntrinsics.reinterpret( 262 Short256Vector.class, 263 short.class, LENGTH, 264 Long256Vector.class, 265 long.class, Long256Vector.LENGTH, 266 this, s, 267 (species, vector) -> vector.defaultReinterpret(species) 268 ); 269 } else if (stype == float.class) { 270 return VectorIntrinsics.reinterpret( 271 Short256Vector.class, 272 short.class, LENGTH, 273 Float256Vector.class, 274 float.class, Float256Vector.LENGTH, 275 this, s, 276 (species, vector) -> vector.defaultReinterpret(species) 277 ); 278 } else if (stype == double.class) { 279 return VectorIntrinsics.reinterpret( 280 Short256Vector.class, 281 short.class, LENGTH, 282 Double256Vector.class, 283 double.class, Double256Vector.LENGTH, 284 this, s, 285 (species, vector) -> vector.defaultReinterpret(species) 286 ); 287 } else { 288 throw new UnsupportedOperationException("Bad lane type for casting."); 289 } 290 } 291 292 @Override 293 @ForceInline 294 public ShortVector reshape(VectorSpecies<Short> s) { 295 Objects.requireNonNull(s); 296 if (s.bitSize() == 64 && (s.boxType() == Short64Vector.class)) { 297 return VectorIntrinsics.reinterpret( 298 Short256Vector.class, 299 short.class, LENGTH, 300 Short64Vector.class, 301 short.class, Short64Vector.LENGTH, 302 this, s, 303 (species, vector) -> (ShortVector) vector.defaultReinterpret(species) 304 ); 305 } else if (s.bitSize() == 128 && (s.boxType() == Short128Vector.class)) { 306 return VectorIntrinsics.reinterpret( 307 Short256Vector.class, 308 short.class, LENGTH, 309 Short128Vector.class, 310 short.class, Short128Vector.LENGTH, 311 this, s, 312 (species, vector) -> (ShortVector) vector.defaultReinterpret(species) 313 ); 314 } else if (s.bitSize() == 256 && (s.boxType() == Short256Vector.class)) { 315 return VectorIntrinsics.reinterpret( 316 Short256Vector.class, 317 short.class, LENGTH, 318 Short256Vector.class, 319 short.class, Short256Vector.LENGTH, 320 this, s, 321 (species, vector) -> (ShortVector) vector.defaultReinterpret(species) 322 ); 323 } else if (s.bitSize() == 512 && (s.boxType() == Short512Vector.class)) { 324 return VectorIntrinsics.reinterpret( 325 Short256Vector.class, 326 short.class, LENGTH, 327 Short512Vector.class, 328 short.class, Short512Vector.LENGTH, 329 this, s, 330 (species, vector) -> (ShortVector) vector.defaultReinterpret(species) 331 ); 332 } else if ((s.bitSize() > 0) && (s.bitSize() <= 2048) 333 && (s.bitSize() % 128 == 0) && (s.boxType() == ShortMaxVector.class)) { 334 return VectorIntrinsics.reinterpret( 335 Short256Vector.class, 336 short.class, LENGTH, 337 ShortMaxVector.class, 338 short.class, ShortMaxVector.LENGTH, 339 this, s, 340 (species, vector) -> (ShortVector) vector.defaultReinterpret(species) 341 ); 342 } else { 343 throw new InternalError("Unimplemented size"); 344 } 345 } 346 347 // Binary operations with scalars 348 349 @Override 350 @ForceInline 351 public ShortVector add(short o) { 352 return add((Short256Vector)ShortVector.broadcast(SPECIES, o)); 353 } 354 355 @Override 356 @ForceInline 357 public ShortVector add(short o, VectorMask<Short> m) { 358 return add((Short256Vector)ShortVector.broadcast(SPECIES, o), m); 359 } 360 361 @Override 362 @ForceInline 363 public ShortVector sub(short o) { 364 return sub((Short256Vector)ShortVector.broadcast(SPECIES, o)); 365 } 366 367 @Override 368 @ForceInline 369 public ShortVector sub(short o, VectorMask<Short> m) { 370 return sub((Short256Vector)ShortVector.broadcast(SPECIES, o), m); 371 } 372 373 @Override 374 @ForceInline 375 public ShortVector mul(short o) { 376 return mul((Short256Vector)ShortVector.broadcast(SPECIES, o)); 377 } 378 379 @Override 380 @ForceInline 381 public ShortVector mul(short o, VectorMask<Short> m) { 382 return mul((Short256Vector)ShortVector.broadcast(SPECIES, o), m); 383 } 384 385 @Override 386 @ForceInline 387 public ShortVector min(short o) { 388 return min((Short256Vector)ShortVector.broadcast(SPECIES, o)); 389 } 390 391 @Override 392 @ForceInline 393 public ShortVector max(short o) { 394 return max((Short256Vector)ShortVector.broadcast(SPECIES, o)); 395 } 396 397 @Override 398 @ForceInline 399 public VectorMask<Short> equal(short o) { 400 return equal((Short256Vector)ShortVector.broadcast(SPECIES, o)); 401 } 402 403 @Override 404 @ForceInline 405 public VectorMask<Short> notEqual(short o) { 406 return notEqual((Short256Vector)ShortVector.broadcast(SPECIES, o)); 407 } 408 409 @Override 410 @ForceInline 411 public VectorMask<Short> lessThan(short o) { 412 return lessThan((Short256Vector)ShortVector.broadcast(SPECIES, o)); 413 } 414 415 @Override 416 @ForceInline 417 public VectorMask<Short> lessThanEq(short o) { 418 return lessThanEq((Short256Vector)ShortVector.broadcast(SPECIES, o)); 419 } 420 421 @Override 422 @ForceInline 423 public VectorMask<Short> greaterThan(short o) { 424 return greaterThan((Short256Vector)ShortVector.broadcast(SPECIES, o)); 425 } 426 427 @Override 428 @ForceInline 429 public VectorMask<Short> greaterThanEq(short o) { 430 return greaterThanEq((Short256Vector)ShortVector.broadcast(SPECIES, o)); 431 } 432 433 @Override 434 @ForceInline 435 public ShortVector blend(short o, VectorMask<Short> m) { 436 return blend((Short256Vector)ShortVector.broadcast(SPECIES, o), m); 437 } 438 439 440 @Override 441 @ForceInline 442 public ShortVector and(short o) { 443 return and((Short256Vector)ShortVector.broadcast(SPECIES, o)); 444 } 445 446 @Override 447 @ForceInline 448 public ShortVector and(short o, VectorMask<Short> m) { 449 return and((Short256Vector)ShortVector.broadcast(SPECIES, o), m); 450 } 451 452 @Override 453 @ForceInline 454 public ShortVector or(short o) { 455 return or((Short256Vector)ShortVector.broadcast(SPECIES, o)); 456 } 457 458 @Override 459 @ForceInline 460 public ShortVector or(short o, VectorMask<Short> m) { 461 return or((Short256Vector)ShortVector.broadcast(SPECIES, o), m); 462 } 463 464 @Override 465 @ForceInline 466 public ShortVector xor(short o) { 467 return xor((Short256Vector)ShortVector.broadcast(SPECIES, o)); 468 } 469 470 @Override 471 @ForceInline 472 public ShortVector xor(short o, VectorMask<Short> m) { 473 return xor((Short256Vector)ShortVector.broadcast(SPECIES, o), m); 474 } 475 476 @Override 477 @ForceInline 478 public Short256Vector neg() { 479 return (Short256Vector)zero(SPECIES).sub(this); 480 } 481 482 // Unary operations 483 484 @ForceInline 485 @Override 486 public Short256Vector neg(VectorMask<Short> m) { 487 return blend(neg(), m); 488 } 489 490 @Override 491 @ForceInline 492 public Short256Vector abs() { 493 return VectorIntrinsics.unaryOp( 494 VECTOR_OP_ABS, Short256Vector.class, short.class, LENGTH, 495 this, 496 v1 -> v1.uOp((i, a) -> (short) Math.abs(a))); 497 } 498 499 @ForceInline 500 @Override 501 public Short256Vector abs(VectorMask<Short> m) { 502 return blend(abs(), m); 503 } 504 505 506 @Override 507 @ForceInline 508 public Short256Vector not() { 509 return VectorIntrinsics.unaryOp( 510 VECTOR_OP_NOT, Short256Vector.class, short.class, LENGTH, 511 this, 512 v1 -> v1.uOp((i, a) -> (short) ~a)); 513 } 514 515 @ForceInline 516 @Override 517 public Short256Vector not(VectorMask<Short> m) { 518 return blend(not(), m); 519 } 520 // Binary operations 521 522 @Override 523 @ForceInline 524 public Short256Vector add(Vector<Short> o) { 525 Objects.requireNonNull(o); 526 Short256Vector v = (Short256Vector)o; 527 return VectorIntrinsics.binaryOp( 528 VECTOR_OP_ADD, Short256Vector.class, short.class, LENGTH, 529 this, v, 530 (v1, v2) -> v1.bOp(v2, (i, a, b) -> (short)(a + b))); 531 } 532 533 @Override 534 @ForceInline 535 public Short256Vector add(Vector<Short> v, VectorMask<Short> m) { 536 return blend(add(v), m); 537 } 538 539 @Override 540 @ForceInline 541 public Short256Vector sub(Vector<Short> o) { 542 Objects.requireNonNull(o); 543 Short256Vector v = (Short256Vector)o; 544 return VectorIntrinsics.binaryOp( 545 VECTOR_OP_SUB, Short256Vector.class, short.class, LENGTH, 546 this, v, 547 (v1, v2) -> v1.bOp(v2, (i, a, b) -> (short)(a - b))); 548 } 549 550 @Override 551 @ForceInline 552 public Short256Vector sub(Vector<Short> v, VectorMask<Short> m) { 553 return blend(sub(v), m); 554 } 555 556 @Override 557 @ForceInline 558 public Short256Vector mul(Vector<Short> o) { 559 Objects.requireNonNull(o); 560 Short256Vector v = (Short256Vector)o; 561 return VectorIntrinsics.binaryOp( 562 VECTOR_OP_MUL, Short256Vector.class, short.class, LENGTH, 563 this, v, 564 (v1, v2) -> v1.bOp(v2, (i, a, b) -> (short)(a * b))); 565 } 566 567 @Override 568 @ForceInline 569 public Short256Vector mul(Vector<Short> v, VectorMask<Short> m) { 570 return blend(mul(v), m); 571 } 572 573 @Override 574 @ForceInline 575 public Short256Vector min(Vector<Short> o) { 576 Objects.requireNonNull(o); 577 Short256Vector v = (Short256Vector)o; 578 return (Short256Vector) VectorIntrinsics.binaryOp( 579 VECTOR_OP_MIN, Short256Vector.class, short.class, LENGTH, 580 this, v, 581 (v1, v2) -> v1.bOp(v2, (i, a, b) -> (short) Math.min(a, b))); 582 } 583 584 @Override 585 @ForceInline 586 public Short256Vector min(Vector<Short> v, VectorMask<Short> m) { 587 return blend(min(v), m); 588 } 589 590 @Override 591 @ForceInline 592 public Short256Vector max(Vector<Short> o) { 593 Objects.requireNonNull(o); 594 Short256Vector v = (Short256Vector)o; 595 return VectorIntrinsics.binaryOp( 596 VECTOR_OP_MAX, Short256Vector.class, short.class, LENGTH, 597 this, v, 598 (v1, v2) -> v1.bOp(v2, (i, a, b) -> (short) Math.max(a, b))); 599 } 600 601 @Override 602 @ForceInline 603 public Short256Vector max(Vector<Short> v, VectorMask<Short> m) { 604 return blend(max(v), m); 605 } 606 607 @Override 608 @ForceInline 609 public Short256Vector and(Vector<Short> o) { 610 Objects.requireNonNull(o); 611 Short256Vector v = (Short256Vector)o; 612 return VectorIntrinsics.binaryOp( 613 VECTOR_OP_AND, Short256Vector.class, short.class, LENGTH, 614 this, v, 615 (v1, v2) -> v1.bOp(v2, (i, a, b) -> (short)(a & b))); 616 } 617 618 @Override 619 @ForceInline 620 public Short256Vector or(Vector<Short> o) { 621 Objects.requireNonNull(o); 622 Short256Vector v = (Short256Vector)o; 623 return VectorIntrinsics.binaryOp( 624 VECTOR_OP_OR, Short256Vector.class, short.class, LENGTH, 625 this, v, 626 (v1, v2) -> v1.bOp(v2, (i, a, b) -> (short)(a | b))); 627 } 628 629 @Override 630 @ForceInline 631 public Short256Vector xor(Vector<Short> o) { 632 Objects.requireNonNull(o); 633 Short256Vector v = (Short256Vector)o; 634 return VectorIntrinsics.binaryOp( 635 VECTOR_OP_XOR, Short256Vector.class, short.class, LENGTH, 636 this, v, 637 (v1, v2) -> v1.bOp(v2, (i, a, b) -> (short)(a ^ b))); 638 } 639 640 @Override 641 @ForceInline 642 public Short256Vector and(Vector<Short> v, VectorMask<Short> m) { 643 return blend(and(v), m); 644 } 645 646 @Override 647 @ForceInline 648 public Short256Vector or(Vector<Short> v, VectorMask<Short> m) { 649 return blend(or(v), m); 650 } 651 652 @Override 653 @ForceInline 654 public Short256Vector xor(Vector<Short> v, VectorMask<Short> m) { 655 return blend(xor(v), m); 656 } 657 658 @Override 659 @ForceInline 660 public Short256Vector shiftL(int s) { 661 return VectorIntrinsics.broadcastInt( 662 VECTOR_OP_LSHIFT, Short256Vector.class, short.class, LENGTH, 663 this, s, 664 (v, i) -> v.uOp((__, a) -> (short) (a << (i & 15)))); 665 } 666 667 @Override 668 @ForceInline 669 public Short256Vector shiftL(int s, VectorMask<Short> m) { 670 return blend(shiftL(s), m); 671 } 672 673 @Override 674 @ForceInline 675 public Short256Vector shiftR(int s) { 676 return VectorIntrinsics.broadcastInt( 677 VECTOR_OP_URSHIFT, Short256Vector.class, short.class, LENGTH, 678 this, s, 679 (v, i) -> v.uOp((__, a) -> (short) ((a & 0xFFFF) >>> (i & 15)))); 680 } 681 682 @Override 683 @ForceInline 684 public Short256Vector shiftR(int s, VectorMask<Short> m) { 685 return blend(shiftR(s), m); 686 } 687 688 @Override 689 @ForceInline 690 public Short256Vector aShiftR(int s) { 691 return VectorIntrinsics.broadcastInt( 692 VECTOR_OP_RSHIFT, Short256Vector.class, short.class, LENGTH, 693 this, s, 694 (v, i) -> v.uOp((__, a) -> (short) (a >> (i & 15)))); 695 } 696 697 @Override 698 @ForceInline 699 public Short256Vector aShiftR(int s, VectorMask<Short> m) { 700 return blend(aShiftR(s), m); 701 } 702 // Ternary operations 703 704 705 // Type specific horizontal reductions 706 707 @Override 708 @ForceInline 709 public short addAll() { 710 return (short) VectorIntrinsics.reductionCoerced( 711 VECTOR_OP_ADD, Short256Vector.class, short.class, LENGTH, 712 this, 713 v -> (long) v.rOp((short) 0, (i, a, b) -> (short) (a + b))); 714 } 715 716 @Override 717 @ForceInline 718 public short andAll() { 719 return (short) VectorIntrinsics.reductionCoerced( 720 VECTOR_OP_AND, Short256Vector.class, short.class, LENGTH, 721 this, 722 v -> (long) v.rOp((short) -1, (i, a, b) -> (short) (a & b))); 723 } 724 725 @Override 726 @ForceInline 727 public short andAll(VectorMask<Short> m) { 728 return ShortVector.broadcast(SPECIES, (short) -1).blend(this, m).andAll(); 729 } 730 731 @Override 732 @ForceInline 733 public short minAll() { 734 return (short) VectorIntrinsics.reductionCoerced( 735 VECTOR_OP_MIN, Short256Vector.class, short.class, LENGTH, 736 this, 737 v -> (long) v.rOp(Short.MAX_VALUE , (i, a, b) -> (short) Math.min(a, b))); 738 } 739 740 @Override 741 @ForceInline 742 public short maxAll() { 743 return (short) VectorIntrinsics.reductionCoerced( 744 VECTOR_OP_MAX, Short256Vector.class, short.class, LENGTH, 745 this, 746 v -> (long) v.rOp(Short.MIN_VALUE , (i, a, b) -> (short) Math.max(a, b))); 747 } 748 749 @Override 750 @ForceInline 751 public short mulAll() { 752 return (short) VectorIntrinsics.reductionCoerced( 753 VECTOR_OP_MUL, Short256Vector.class, short.class, LENGTH, 754 this, 755 v -> (long) v.rOp((short) 1, (i, a, b) -> (short) (a * b))); 756 } 757 758 @Override 759 @ForceInline 760 public short orAll() { 761 return (short) VectorIntrinsics.reductionCoerced( 762 VECTOR_OP_OR, Short256Vector.class, short.class, LENGTH, 763 this, 764 v -> (long) v.rOp((short) 0, (i, a, b) -> (short) (a | b))); 765 } 766 767 @Override 768 @ForceInline 769 public short orAll(VectorMask<Short> m) { 770 return ShortVector.broadcast(SPECIES, (short) 0).blend(this, m).orAll(); 771 } 772 773 @Override 774 @ForceInline 775 public short xorAll() { 776 return (short) VectorIntrinsics.reductionCoerced( 777 VECTOR_OP_XOR, Short256Vector.class, short.class, LENGTH, 778 this, 779 v -> (long) v.rOp((short) 0, (i, a, b) -> (short) (a ^ b))); 780 } 781 782 @Override 783 @ForceInline 784 public short xorAll(VectorMask<Short> m) { 785 return ShortVector.broadcast(SPECIES, (short) 0).blend(this, m).xorAll(); 786 } 787 788 789 @Override 790 @ForceInline 791 public short addAll(VectorMask<Short> m) { 792 return ShortVector.broadcast(SPECIES, (short) 0).blend(this, m).addAll(); 793 } 794 795 796 @Override 797 @ForceInline 798 public short mulAll(VectorMask<Short> m) { 799 return ShortVector.broadcast(SPECIES, (short) 1).blend(this, m).mulAll(); 800 } 801 802 @Override 803 @ForceInline 804 public short minAll(VectorMask<Short> m) { 805 return ShortVector.broadcast(SPECIES, Short.MAX_VALUE).blend(this, m).minAll(); 806 } 807 808 @Override 809 @ForceInline 810 public short maxAll(VectorMask<Short> m) { 811 return ShortVector.broadcast(SPECIES, Short.MIN_VALUE).blend(this, m).maxAll(); 812 } 813 814 @Override 815 @ForceInline 816 public VectorShuffle<Short> toShuffle() { 817 short[] a = toArray(); 818 int[] sa = new int[a.length]; 819 for (int i = 0; i < a.length; i++) { 820 sa[i] = (int) a[i]; 821 } 822 return VectorShuffle.fromArray(SPECIES, sa, 0); 823 } 824 825 // Memory operations 826 827 private static final int ARRAY_SHIFT = 31 - Integer.numberOfLeadingZeros(Unsafe.ARRAY_SHORT_INDEX_SCALE); 828 private static final int BOOLEAN_ARRAY_SHIFT = 31 - Integer.numberOfLeadingZeros(Unsafe.ARRAY_BOOLEAN_INDEX_SCALE); 829 830 @Override 831 @ForceInline 832 public void intoArray(short[] a, int ix) { 833 Objects.requireNonNull(a); 834 ix = VectorIntrinsics.checkIndex(ix, a.length, LENGTH); 835 VectorIntrinsics.store(Short256Vector.class, short.class, LENGTH, 836 a, (((long) ix) << ARRAY_SHIFT) + Unsafe.ARRAY_SHORT_BASE_OFFSET, 837 this, 838 a, ix, 839 (arr, idx, v) -> v.forEach((i, e) -> arr[idx + i] = e)); 840 } 841 842 @Override 843 @ForceInline 844 public final void intoArray(short[] a, int ax, VectorMask<Short> m) { 845 ShortVector oldVal = ShortVector.fromArray(SPECIES, a, ax); 846 ShortVector newVal = oldVal.blend(this, m); 847 newVal.intoArray(a, ax); 848 } 849 850 @Override 851 @ForceInline 852 public void intoByteArray(byte[] a, int ix) { 853 Objects.requireNonNull(a); 854 ix = VectorIntrinsics.checkIndex(ix, a.length, bitSize() / Byte.SIZE); 855 VectorIntrinsics.store(Short256Vector.class, short.class, LENGTH, 856 a, ((long) ix) + Unsafe.ARRAY_BYTE_BASE_OFFSET, 857 this, 858 a, ix, 859 (c, idx, v) -> { 860 ByteBuffer bbc = ByteBuffer.wrap(c, idx, c.length - idx).order(ByteOrder.nativeOrder()); 861 ShortBuffer tb = bbc.asShortBuffer(); 862 v.forEach((i, e) -> tb.put(e)); 863 }); 864 } 865 866 @Override 867 @ForceInline 868 public final void intoByteArray(byte[] a, int ix, VectorMask<Short> m) { 869 Short256Vector oldVal = (Short256Vector) ShortVector.fromByteArray(SPECIES, a, ix); 870 Short256Vector newVal = oldVal.blend(this, m); 871 newVal.intoByteArray(a, ix); 872 } 873 874 @Override 875 @ForceInline 876 public void intoByteBuffer(ByteBuffer bb, int ix) { 877 if (bb.order() != ByteOrder.nativeOrder()) { 878 throw new IllegalArgumentException(); 879 } 880 if (bb.isReadOnly()) { 881 throw new ReadOnlyBufferException(); 882 } 883 ix = VectorIntrinsics.checkIndex(ix, bb.limit(), bitSize() / Byte.SIZE); 884 VectorIntrinsics.store(Short256Vector.class, short.class, LENGTH, 885 U.getReference(bb, BYTE_BUFFER_HB), ix + U.getLong(bb, BUFFER_ADDRESS), 886 this, 887 bb, ix, 888 (c, idx, v) -> { 889 ByteBuffer bbc = c.duplicate().position(idx).order(ByteOrder.nativeOrder()); 890 ShortBuffer tb = bbc.asShortBuffer(); 891 v.forEach((i, e) -> tb.put(e)); 892 }); 893 } 894 895 @Override 896 @ForceInline 897 public void intoByteBuffer(ByteBuffer bb, int ix, VectorMask<Short> m) { 898 Short256Vector oldVal = (Short256Vector) ShortVector.fromByteBuffer(SPECIES, bb, ix); 899 Short256Vector newVal = oldVal.blend(this, m); 900 newVal.intoByteBuffer(bb, ix); 901 } 902 903 // 904 905 @Override 906 public String toString() { 907 return Arrays.toString(getElements()); 908 } 909 910 @Override 911 public boolean equals(Object o) { 912 if (this == o) return true; 913 if (o == null || this.getClass() != o.getClass()) return false; 914 915 Short256Vector that = (Short256Vector) o; 916 return this.equal(that).allTrue(); 917 } 918 919 @Override 920 public int hashCode() { 921 return Arrays.hashCode(vec); 922 } 923 924 // Binary test 925 926 @Override 927 Short256Mask bTest(Vector<Short> o, FBinTest f) { 928 short[] vec1 = getElements(); 929 short[] vec2 = ((Short256Vector)o).getElements(); 930 boolean[] bits = new boolean[length()]; 931 for (int i = 0; i < length(); i++){ 932 bits[i] = f.apply(i, vec1[i], vec2[i]); 933 } 934 return new Short256Mask(bits); 935 } 936 937 // Comparisons 938 939 @Override 940 @ForceInline 941 public Short256Mask equal(Vector<Short> o) { 942 Objects.requireNonNull(o); 943 Short256Vector v = (Short256Vector)o; 944 945 return VectorIntrinsics.compare( 946 BT_eq, Short256Vector.class, Short256Mask.class, short.class, LENGTH, 947 this, v, 948 (v1, v2) -> v1.bTest(v2, (i, a, b) -> a == b)); 949 } 950 951 @Override 952 @ForceInline 953 public Short256Mask notEqual(Vector<Short> o) { 954 Objects.requireNonNull(o); 955 Short256Vector v = (Short256Vector)o; 956 957 return VectorIntrinsics.compare( 958 BT_ne, Short256Vector.class, Short256Mask.class, short.class, LENGTH, 959 this, v, 960 (v1, v2) -> v1.bTest(v2, (i, a, b) -> a != b)); 961 } 962 963 @Override 964 @ForceInline 965 public Short256Mask lessThan(Vector<Short> o) { 966 Objects.requireNonNull(o); 967 Short256Vector v = (Short256Vector)o; 968 969 return VectorIntrinsics.compare( 970 BT_lt, Short256Vector.class, Short256Mask.class, short.class, LENGTH, 971 this, v, 972 (v1, v2) -> v1.bTest(v2, (i, a, b) -> a < b)); 973 } 974 975 @Override 976 @ForceInline 977 public Short256Mask lessThanEq(Vector<Short> o) { 978 Objects.requireNonNull(o); 979 Short256Vector v = (Short256Vector)o; 980 981 return VectorIntrinsics.compare( 982 BT_le, Short256Vector.class, Short256Mask.class, short.class, LENGTH, 983 this, v, 984 (v1, v2) -> v1.bTest(v2, (i, a, b) -> a <= b)); 985 } 986 987 @Override 988 @ForceInline 989 public Short256Mask greaterThan(Vector<Short> o) { 990 Objects.requireNonNull(o); 991 Short256Vector v = (Short256Vector)o; 992 993 return (Short256Mask) VectorIntrinsics.compare( 994 BT_gt, Short256Vector.class, Short256Mask.class, short.class, LENGTH, 995 this, v, 996 (v1, v2) -> v1.bTest(v2, (i, a, b) -> a > b)); 997 } 998 999 @Override 1000 @ForceInline 1001 public Short256Mask greaterThanEq(Vector<Short> o) { 1002 Objects.requireNonNull(o); 1003 Short256Vector v = (Short256Vector)o; 1004 1005 return VectorIntrinsics.compare( 1006 BT_ge, Short256Vector.class, Short256Mask.class, short.class, LENGTH, 1007 this, v, 1008 (v1, v2) -> v1.bTest(v2, (i, a, b) -> a >= b)); 1009 } 1010 1011 // Foreach 1012 1013 @Override 1014 void forEach(FUnCon f) { 1015 short[] vec = getElements(); 1016 for (int i = 0; i < length(); i++) { 1017 f.apply(i, vec[i]); 1018 } 1019 } 1020 1021 @Override 1022 void forEach(VectorMask<Short> o, FUnCon f) { 1023 boolean[] mbits = ((Short256Mask)o).getBits(); 1024 forEach((i, a) -> { 1025 if (mbits[i]) { f.apply(i, a); } 1026 }); 1027 } 1028 1029 1030 1031 @Override 1032 public Short256Vector rotateEL(int j) { 1033 short[] vec = getElements(); 1034 short[] res = new short[length()]; 1035 for (int i = 0; i < length(); i++){ 1036 res[(j + i) % length()] = vec[i]; 1037 } 1038 return new Short256Vector(res); 1039 } 1040 1041 @Override 1042 public Short256Vector rotateER(int j) { 1043 short[] vec = getElements(); 1044 short[] res = new short[length()]; 1045 for (int i = 0; i < length(); i++){ 1046 int z = i - j; 1047 if(j < 0) { 1048 res[length() + z] = vec[i]; 1049 } else { 1050 res[z] = vec[i]; 1051 } 1052 } 1053 return new Short256Vector(res); 1054 } 1055 1056 @Override 1057 public Short256Vector shiftEL(int j) { 1058 short[] vec = getElements(); 1059 short[] res = new short[length()]; 1060 for (int i = 0; i < length() - j; i++) { 1061 res[i] = vec[i + j]; 1062 } 1063 return new Short256Vector(res); 1064 } 1065 1066 @Override 1067 public Short256Vector shiftER(int j) { 1068 short[] vec = getElements(); 1069 short[] res = new short[length()]; 1070 for (int i = 0; i < length() - j; i++){ 1071 res[i + j] = vec[i]; 1072 } 1073 return new Short256Vector(res); 1074 } 1075 1076 @Override 1077 @ForceInline 1078 public Short256Vector rearrange(Vector<Short> v, 1079 VectorShuffle<Short> s, VectorMask<Short> m) { 1080 return this.rearrange(s).blend(v.rearrange(s), m); 1081 } 1082 1083 @Override 1084 @ForceInline 1085 public Short256Vector rearrange(VectorShuffle<Short> o1) { 1086 Objects.requireNonNull(o1); 1087 Short256Shuffle s = (Short256Shuffle)o1; 1088 1089 return VectorIntrinsics.rearrangeOp( 1090 Short256Vector.class, Short256Shuffle.class, short.class, LENGTH, 1091 this, s, 1092 (v1, s_) -> v1.uOp((i, a) -> { 1093 int ei = s_.lane(i); 1094 return v1.lane(ei); 1095 })); 1096 } 1097 1098 @Override 1099 @ForceInline 1100 public Short256Vector blend(Vector<Short> o1, VectorMask<Short> o2) { 1101 Objects.requireNonNull(o1); 1102 Objects.requireNonNull(o2); 1103 Short256Vector v = (Short256Vector)o1; 1104 Short256Mask m = (Short256Mask)o2; 1105 1106 return VectorIntrinsics.blend( 1107 Short256Vector.class, Short256Mask.class, short.class, LENGTH, 1108 this, v, m, 1109 (v1, v2, m_) -> v1.bOp(v2, (i, a, b) -> m_.lane(i) ? b : a)); 1110 } 1111 1112 // Accessors 1113 1114 @Override 1115 public short lane(int i) { 1116 if (i < 0 || i >= LENGTH) { 1117 throw new IllegalArgumentException("Index " + i + " must be zero or positive, and less than " + LENGTH); 1118 } 1119 return (short) VectorIntrinsics.extract( 1120 Short256Vector.class, short.class, LENGTH, 1121 this, i, 1122 (vec, ix) -> { 1123 short[] vecarr = vec.getElements(); 1124 return (long)vecarr[ix]; 1125 }); 1126 } 1127 1128 @Override 1129 public Short256Vector with(int i, short e) { 1130 if (i < 0 || i >= LENGTH) { 1131 throw new IllegalArgumentException("Index " + i + " must be zero or positive, and less than " + LENGTH); 1132 } 1133 return VectorIntrinsics.insert( 1134 Short256Vector.class, short.class, LENGTH, 1135 this, i, (long)e, 1136 (v, ix, bits) -> { 1137 short[] res = v.getElements().clone(); 1138 res[ix] = (short)bits; 1139 return new Short256Vector(res); 1140 }); 1141 } 1142 1143 // Mask 1144 1145 static final class Short256Mask extends AbstractMask<Short> { 1146 static final Short256Mask TRUE_MASK = new Short256Mask(true); 1147 static final Short256Mask FALSE_MASK = new Short256Mask(false); 1148 1149 private final boolean[] bits; // Don't access directly, use getBits() instead. 1150 1151 public Short256Mask(boolean[] bits) { 1152 this(bits, 0); 1153 } 1154 1155 public Short256Mask(boolean[] bits, int offset) { 1156 boolean[] a = new boolean[species().length()]; 1157 for (int i = 0; i < a.length; i++) { 1158 a[i] = bits[offset + i]; 1159 } 1160 this.bits = a; 1161 } 1162 1163 public Short256Mask(boolean val) { 1164 boolean[] bits = new boolean[species().length()]; 1165 Arrays.fill(bits, val); 1166 this.bits = bits; 1167 } 1168 1169 boolean[] getBits() { 1170 return VectorIntrinsics.maybeRebox(this).bits; 1171 } 1172 1173 @Override 1174 Short256Mask uOp(MUnOp f) { 1175 boolean[] res = new boolean[species().length()]; 1176 boolean[] bits = getBits(); 1177 for (int i = 0; i < species().length(); i++) { 1178 res[i] = f.apply(i, bits[i]); 1179 } 1180 return new Short256Mask(res); 1181 } 1182 1183 @Override 1184 Short256Mask bOp(VectorMask<Short> o, MBinOp f) { 1185 boolean[] res = new boolean[species().length()]; 1186 boolean[] bits = getBits(); 1187 boolean[] mbits = ((Short256Mask)o).getBits(); 1188 for (int i = 0; i < species().length(); i++) { 1189 res[i] = f.apply(i, bits[i], mbits[i]); 1190 } 1191 return new Short256Mask(res); 1192 } 1193 1194 @Override 1195 public VectorSpecies<Short> species() { 1196 return SPECIES; 1197 } 1198 1199 @Override 1200 public Short256Vector toVector() { 1201 short[] res = new short[species().length()]; 1202 boolean[] bits = getBits(); 1203 for (int i = 0; i < species().length(); i++) { 1204 // -1 will result in the most significant bit being set in 1205 // addition to some or all other bits 1206 res[i] = (short) (bits[i] ? -1 : 0); 1207 } 1208 return new Short256Vector(res); 1209 } 1210 1211 @Override 1212 @ForceInline 1213 @SuppressWarnings("unchecked") 1214 public <E> VectorMask<E> cast(VectorSpecies<E> species) { 1215 if (length() != species.length()) 1216 throw new IllegalArgumentException("VectorMask length and species length differ"); 1217 Class<?> stype = species.elementType(); 1218 boolean [] maskArray = toArray(); 1219 if (stype == byte.class) { 1220 return (VectorMask <E>) new Byte256Vector.Byte256Mask(maskArray); 1221 } else if (stype == short.class) { 1222 return (VectorMask <E>) new Short256Vector.Short256Mask(maskArray); 1223 } else if (stype == int.class) { 1224 return (VectorMask <E>) new Int256Vector.Int256Mask(maskArray); 1225 } else if (stype == long.class) { 1226 return (VectorMask <E>) new Long256Vector.Long256Mask(maskArray); 1227 } else if (stype == float.class) { 1228 return (VectorMask <E>) new Float256Vector.Float256Mask(maskArray); 1229 } else if (stype == double.class) { 1230 return (VectorMask <E>) new Double256Vector.Double256Mask(maskArray); 1231 } else { 1232 throw new UnsupportedOperationException("Bad lane type for casting."); 1233 } 1234 } 1235 1236 // Unary operations 1237 1238 @Override 1239 @ForceInline 1240 public Short256Mask not() { 1241 return (Short256Mask) VectorIntrinsics.unaryOp( 1242 VECTOR_OP_NOT, Short256Mask.class, short.class, LENGTH, 1243 this, 1244 (m1) -> m1.uOp((i, a) -> !a)); 1245 } 1246 1247 // Binary operations 1248 1249 @Override 1250 @ForceInline 1251 public Short256Mask and(VectorMask<Short> o) { 1252 Objects.requireNonNull(o); 1253 Short256Mask m = (Short256Mask)o; 1254 return VectorIntrinsics.binaryOp(VECTOR_OP_AND, Short256Mask.class, short.class, LENGTH, 1255 this, m, 1256 (m1, m2) -> m1.bOp(m2, (i, a, b) -> a & b)); 1257 } 1258 1259 @Override 1260 @ForceInline 1261 public Short256Mask or(VectorMask<Short> o) { 1262 Objects.requireNonNull(o); 1263 Short256Mask m = (Short256Mask)o; 1264 return VectorIntrinsics.binaryOp(VECTOR_OP_OR, Short256Mask.class, short.class, LENGTH, 1265 this, m, 1266 (m1, m2) -> m1.bOp(m2, (i, a, b) -> a | b)); 1267 } 1268 1269 // Reductions 1270 1271 @Override 1272 @ForceInline 1273 public boolean anyTrue() { 1274 return VectorIntrinsics.test(BT_ne, Short256Mask.class, short.class, LENGTH, 1275 this, this, 1276 (m, __) -> anyTrueHelper(((Short256Mask)m).getBits())); 1277 } 1278 1279 @Override 1280 @ForceInline 1281 public boolean allTrue() { 1282 return VectorIntrinsics.test(BT_overflow, Short256Mask.class, short.class, LENGTH, 1283 this, VectorMask.maskAllTrue(species()), 1284 (m, __) -> allTrueHelper(((Short256Mask)m).getBits())); 1285 } 1286 } 1287 1288 // Shuffle 1289 1290 static final class Short256Shuffle extends AbstractShuffle<Short> { 1291 Short256Shuffle(byte[] reorder) { 1292 super(reorder); 1293 } 1294 1295 public Short256Shuffle(int[] reorder) { 1296 super(reorder); 1297 } 1298 1299 public Short256Shuffle(int[] reorder, int i) { 1300 super(reorder, i); 1301 } 1302 1303 public Short256Shuffle(IntUnaryOperator f) { 1304 super(f); 1305 } 1306 1307 @Override 1308 public VectorSpecies<Short> species() { 1309 return SPECIES; 1310 } 1311 1312 @Override 1313 public ShortVector toVector() { 1314 short[] va = new short[SPECIES.length()]; 1315 for (int i = 0; i < va.length; i++) { 1316 va[i] = (short) lane(i); 1317 } 1318 return ShortVector.fromArray(SPECIES, va, 0); 1319 } 1320 1321 @Override 1322 @ForceInline 1323 @SuppressWarnings("unchecked") 1324 public <F> VectorShuffle<F> cast(VectorSpecies<F> species) { 1325 if (length() != species.length()) 1326 throw new IllegalArgumentException("Shuffle length and species length differ"); 1327 Class<?> stype = species.elementType(); 1328 int [] shuffleArray = toArray(); 1329 if (stype == byte.class) { 1330 return (VectorShuffle<F>) new Byte256Vector.Byte256Shuffle(shuffleArray); 1331 } else if (stype == short.class) { 1332 return (VectorShuffle<F>) new Short256Vector.Short256Shuffle(shuffleArray); 1333 } else if (stype == int.class) { 1334 return (VectorShuffle<F>) new Int256Vector.Int256Shuffle(shuffleArray); 1335 } else if (stype == long.class) { 1336 return (VectorShuffle<F>) new Long256Vector.Long256Shuffle(shuffleArray); 1337 } else if (stype == float.class) { 1338 return (VectorShuffle<F>) new Float256Vector.Float256Shuffle(shuffleArray); 1339 } else if (stype == double.class) { 1340 return (VectorShuffle<F>) new Double256Vector.Double256Shuffle(shuffleArray); 1341 } else { 1342 throw new UnsupportedOperationException("Bad lane type for casting."); 1343 } 1344 } 1345 1346 @Override 1347 public Short256Shuffle rearrange(VectorShuffle<Short> o) { 1348 Short256Shuffle s = (Short256Shuffle) o; 1349 byte[] r = new byte[reorder.length]; 1350 for (int i = 0; i < reorder.length; i++) { 1351 r[i] = reorder[s.reorder[i]]; 1352 } 1353 return new Short256Shuffle(r); 1354 } 1355 } 1356 1357 // VectorSpecies 1358 1359 @Override 1360 public VectorSpecies<Short> species() { 1361 return SPECIES; 1362 } 1363 }