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.util.Arrays; 29 import java.util.Objects; 30 import jdk.internal.vm.annotation.ForceInline; 31 import static jdk.incubator.vector.VectorIntrinsics.*; 32 33 @SuppressWarnings("cast") 34 final class Short512Vector extends ShortVector<Shapes.S512Bit> { 35 static final Short512Species SPECIES = new Short512Species(); 36 37 static final Short512Vector ZERO = new Short512Vector(); 38 39 static final int LENGTH = SPECIES.length(); 40 41 private final short[] vec; // Don't access directly, use getElements() instead. 42 43 private short[] getElements() { 44 return VectorIntrinsics.maybeRebox(this).vec; 45 } 46 47 Short512Vector() { 48 vec = new short[SPECIES.length()]; 49 } 50 51 Short512Vector(short[] v) { 52 vec = v; 53 } 54 55 @Override 56 public int length() { return LENGTH; } 57 58 // Unary operator 59 60 @Override 61 Short512Vector uOp(FUnOp f) { 62 short[] vec = getElements(); 63 short[] res = new short[length()]; 64 for (int i = 0; i < length(); i++) { 65 res[i] = f.apply(i, vec[i]); 66 } 67 return new Short512Vector(res); 68 } 69 70 @Override 71 Short512Vector uOp(Mask<Short, Shapes.S512Bit> o, FUnOp f) { 72 short[] vec = getElements(); 73 short[] res = new short[length()]; 74 boolean[] mbits = ((Short512Mask)o).getBits(); 75 for (int i = 0; i < length(); i++) { 76 res[i] = mbits[i] ? f.apply(i, vec[i]) : vec[i]; 77 } 78 return new Short512Vector(res); 79 } 80 81 // Binary operator 82 83 @Override 84 Short512Vector bOp(Vector<Short, Shapes.S512Bit> o, FBinOp f) { 85 short[] res = new short[length()]; 86 short[] vec1 = this.getElements(); 87 short[] vec2 = ((Short512Vector)o).getElements(); 88 for (int i = 0; i < length(); i++) { 89 res[i] = f.apply(i, vec1[i], vec2[i]); 90 } 91 return new Short512Vector(res); 92 } 93 94 @Override 95 Short512Vector bOp(Vector<Short, Shapes.S512Bit> o1, Mask<Short, Shapes.S512Bit> o2, FBinOp f) { 96 short[] res = new short[length()]; 97 short[] vec1 = this.getElements(); 98 short[] vec2 = ((Short512Vector)o1).getElements(); 99 boolean[] mbits = ((Short512Mask)o2).getBits(); 100 for (int i = 0; i < length(); i++) { 101 res[i] = mbits[i] ? f.apply(i, vec1[i], vec2[i]) : vec1[i]; 102 } 103 return new Short512Vector(res); 104 } 105 106 // Trinary operator 107 108 @Override 109 Short512Vector tOp(Vector<Short, Shapes.S512Bit> o1, Vector<Short, Shapes.S512Bit> o2, FTriOp f) { 110 short[] res = new short[length()]; 111 short[] vec1 = this.getElements(); 112 short[] vec2 = ((Short512Vector)o1).getElements(); 113 short[] vec3 = ((Short512Vector)o2).getElements(); 114 for (int i = 0; i < length(); i++) { 115 res[i] = f.apply(i, vec1[i], vec2[i], vec3[i]); 116 } 117 return new Short512Vector(res); 118 } 119 120 @Override 121 Short512Vector tOp(Vector<Short, Shapes.S512Bit> o1, Vector<Short, Shapes.S512Bit> o2, Mask<Short, Shapes.S512Bit> o3, FTriOp f) { 122 short[] res = new short[length()]; 123 short[] vec1 = getElements(); 124 short[] vec2 = ((Short512Vector)o1).getElements(); 125 short[] vec3 = ((Short512Vector)o2).getElements(); 126 boolean[] mbits = ((Short512Mask)o3).getBits(); 127 for (int i = 0; i < length(); i++) { 128 res[i] = mbits[i] ? f.apply(i, vec1[i], vec2[i], vec3[i]) : vec1[i]; 129 } 130 return new Short512Vector(res); 131 } 132 133 @Override 134 short rOp(short v, FBinOp f) { 135 short[] vec = getElements(); 136 for (int i = 0; i < length(); i++) { 137 v = f.apply(i, v, vec[i]); 138 } 139 return v; 140 } 141 142 // Binary operations with scalars 143 144 @Override 145 @ForceInline 146 public ShortVector<Shapes.S512Bit> add(short o) { 147 return add(SPECIES.broadcast(o)); 148 } 149 150 @Override 151 @ForceInline 152 public ShortVector<Shapes.S512Bit> add(short o, Mask<Short,Shapes.S512Bit> m) { 153 return add(SPECIES.broadcast(o), m); 154 } 155 156 @Override 157 @ForceInline 158 public ShortVector<Shapes.S512Bit> addSaturate(short o) { 159 return addSaturate(SPECIES.broadcast(o)); 160 } 161 162 @Override 163 @ForceInline 164 public ShortVector<Shapes.S512Bit> addSaturate(short o, Mask<Short,Shapes.S512Bit> m) { 165 return addSaturate(SPECIES.broadcast(o), m); 166 } 167 168 @Override 169 @ForceInline 170 public ShortVector<Shapes.S512Bit> sub(short o) { 171 return sub(SPECIES.broadcast(o)); 172 } 173 174 @Override 175 @ForceInline 176 public ShortVector<Shapes.S512Bit> sub(short o, Mask<Short,Shapes.S512Bit> m) { 177 return sub(SPECIES.broadcast(o), m); 178 } 179 180 @Override 181 @ForceInline 182 public ShortVector<Shapes.S512Bit> subSaturate(short o) { 183 return subSaturate(SPECIES.broadcast(o)); 184 } 185 186 @Override 187 @ForceInline 188 public ShortVector<Shapes.S512Bit> subSaturate(short o, Mask<Short,Shapes.S512Bit> m) { 189 return subSaturate(SPECIES.broadcast(o), m); 190 } 191 192 @Override 193 @ForceInline 194 public ShortVector<Shapes.S512Bit> mul(short o) { 195 return mul(SPECIES.broadcast(o)); 196 } 197 198 @Override 199 @ForceInline 200 public ShortVector<Shapes.S512Bit> mul(short o, Mask<Short,Shapes.S512Bit> m) { 201 return mul(SPECIES.broadcast(o), m); 202 } 203 204 @Override 205 @ForceInline 206 public ShortVector<Shapes.S512Bit> min(short o) { 207 return min(SPECIES.broadcast(o)); 208 } 209 210 @Override 211 @ForceInline 212 public ShortVector<Shapes.S512Bit> max(short o) { 213 return max(SPECIES.broadcast(o)); 214 } 215 216 @Override 217 @ForceInline 218 public Mask<Short, Shapes.S512Bit> equal(short o) { 219 return equal(SPECIES.broadcast(o)); 220 } 221 222 @Override 223 @ForceInline 224 public Mask<Short, Shapes.S512Bit> notEqual(short o) { 225 return notEqual(SPECIES.broadcast(o)); 226 } 227 228 @Override 229 @ForceInline 230 public Mask<Short, Shapes.S512Bit> lessThan(short o) { 231 return lessThan(SPECIES.broadcast(o)); 232 } 233 234 @Override 235 @ForceInline 236 public Mask<Short, Shapes.S512Bit> lessThanEq(short o) { 237 return lessThanEq(SPECIES.broadcast(o)); 238 } 239 240 @Override 241 @ForceInline 242 public Mask<Short, Shapes.S512Bit> greaterThan(short o) { 243 return greaterThan(SPECIES.broadcast(o)); 244 } 245 246 @Override 247 @ForceInline 248 public Mask<Short, Shapes.S512Bit> greaterThanEq(short o) { 249 return greaterThanEq(SPECIES.broadcast(o)); 250 } 251 252 @Override 253 @ForceInline 254 public ShortVector<Shapes.S512Bit> blend(short o, Mask<Short,Shapes.S512Bit> m) { 255 return blend(SPECIES.broadcast(o), m); 256 } 257 258 259 @Override 260 @ForceInline 261 public ShortVector<Shapes.S512Bit> and(short o) { 262 return and(SPECIES.broadcast(o)); 263 } 264 265 @Override 266 @ForceInline 267 public ShortVector<Shapes.S512Bit> and(short o, Mask<Short,Shapes.S512Bit> m) { 268 return and(SPECIES.broadcast(o), m); 269 } 270 271 @Override 272 @ForceInline 273 public ShortVector<Shapes.S512Bit> or(short o) { 274 return or(SPECIES.broadcast(o)); 275 } 276 277 @Override 278 @ForceInline 279 public ShortVector<Shapes.S512Bit> or(short o, Mask<Short,Shapes.S512Bit> m) { 280 return or(SPECIES.broadcast(o), m); 281 } 282 283 @Override 284 @ForceInline 285 public ShortVector<Shapes.S512Bit> xor(short o) { 286 return xor(SPECIES.broadcast(o)); 287 } 288 289 @Override 290 @ForceInline 291 public ShortVector<Shapes.S512Bit> xor(short o, Mask<Short,Shapes.S512Bit> m) { 292 return xor(SPECIES.broadcast(o), m); 293 } 294 295 296 // Unary operations 297 298 299 300 @Override 301 @ForceInline 302 public Short512Vector not() { 303 return (Short512Vector) VectorIntrinsics.unaryOp( 304 VECTOR_OP_NOT, Short512Vector.class, short.class, LENGTH, 305 this, 306 v1 -> ((Short512Vector)v1).uOp((i, a) -> (short) ~a)); 307 } 308 // Binary operations 309 310 @Override 311 @ForceInline 312 public Short512Vector add(Vector<Short,Shapes.S512Bit> o) { 313 Objects.requireNonNull(o); 314 Short512Vector v = (Short512Vector)o; 315 return (Short512Vector) VectorIntrinsics.binaryOp( 316 VECTOR_OP_ADD, Short512Vector.class, short.class, LENGTH, 317 this, v, 318 (v1, v2) -> ((Short512Vector)v1).bOp(v2, (i, a, b) -> (short)(a + b))); 319 } 320 321 @Override 322 @ForceInline 323 public Short512Vector sub(Vector<Short,Shapes.S512Bit> o) { 324 Objects.requireNonNull(o); 325 Short512Vector v = (Short512Vector)o; 326 return (Short512Vector) VectorIntrinsics.binaryOp( 327 VECTOR_OP_SUB, Short512Vector.class, short.class, LENGTH, 328 this, v, 329 (v1, v2) -> ((Short512Vector)v1).bOp(v2, (i, a, b) -> (short)(a - b))); 330 } 331 332 @Override 333 @ForceInline 334 public Short512Vector mul(Vector<Short,Shapes.S512Bit> o) { 335 Objects.requireNonNull(o); 336 Short512Vector v = (Short512Vector)o; 337 return (Short512Vector) VectorIntrinsics.binaryOp( 338 VECTOR_OP_MUL, Short512Vector.class, short.class, LENGTH, 339 this, v, 340 (v1, v2) -> ((Short512Vector)v1).bOp(v2, (i, a, b) -> (short)(a * b))); 341 } 342 343 344 345 @Override 346 @ForceInline 347 public Short512Vector and(Vector<Short,Shapes.S512Bit> o) { 348 Objects.requireNonNull(o); 349 Short512Vector v = (Short512Vector)o; 350 return (Short512Vector) VectorIntrinsics.binaryOp( 351 VECTOR_OP_AND, Short512Vector.class, short.class, LENGTH, 352 this, v, 353 (v1, v2) -> ((Short512Vector)v1).bOp(v2, (i, a, b) -> (short)(a & b))); 354 } 355 356 @Override 357 @ForceInline 358 public Short512Vector or(Vector<Short,Shapes.S512Bit> o) { 359 Objects.requireNonNull(o); 360 Short512Vector v = (Short512Vector)o; 361 return (Short512Vector) VectorIntrinsics.binaryOp( 362 VECTOR_OP_OR, Short512Vector.class, short.class, LENGTH, 363 this, v, 364 (v1, v2) -> ((Short512Vector)v1).bOp(v2, (i, a, b) -> (short)(a | b))); 365 } 366 367 @Override 368 @ForceInline 369 public Short512Vector xor(Vector<Short,Shapes.S512Bit> o) { 370 Objects.requireNonNull(o); 371 Short512Vector v = (Short512Vector)o; 372 return (Short512Vector) VectorIntrinsics.binaryOp( 373 VECTOR_OP_XOR, Short512Vector.class, short.class, LENGTH, 374 this, v, 375 (v1, v2) -> ((Short512Vector)v1).bOp(v2, (i, a, b) -> (short)(a ^ b))); 376 } 377 378 @Override 379 @ForceInline 380 public Short512Vector and(Vector<Short,Shapes.S512Bit> v, Mask<Short, Shapes.S512Bit> m) { 381 return blend(and(v), m); 382 } 383 384 @Override 385 @ForceInline 386 public Short512Vector or(Vector<Short,Shapes.S512Bit> v, Mask<Short, Shapes.S512Bit> m) { 387 return blend(or(v), m); 388 } 389 390 @Override 391 @ForceInline 392 public Short512Vector xor(Vector<Short,Shapes.S512Bit> v, Mask<Short, Shapes.S512Bit> m) { 393 return blend(xor(v), m); 394 } 395 396 @Override 397 @ForceInline 398 public Short512Vector shiftL(int s) { 399 return (Short512Vector) VectorIntrinsics.broadcastInt( 400 VECTOR_OP_LSHIFT, Short512Vector.class, short.class, LENGTH, 401 this, s, 402 (v, i) -> v.uOp((__, a) -> (short) (a << i))); 403 } 404 405 @Override 406 @ForceInline 407 public Short512Vector shiftR(int s) { 408 return (Short512Vector) VectorIntrinsics.broadcastInt( 409 VECTOR_OP_URSHIFT, Short512Vector.class, short.class, LENGTH, 410 this, s, 411 (v, i) -> v.uOp((__, a) -> (short) (a >>> i))); 412 } 413 414 @Override 415 @ForceInline 416 public Short512Vector aShiftR(int s) { 417 return (Short512Vector) VectorIntrinsics.broadcastInt( 418 VECTOR_OP_RSHIFT, Short512Vector.class, short.class, LENGTH, 419 this, s, 420 (v, i) -> v.uOp((__, a) -> (short) (a >> i))); 421 } 422 423 // Ternary operations 424 425 426 // Type specific horizontal reductions 427 428 @Override 429 @ForceInline 430 public short addAll() { 431 return (short) VectorIntrinsics.reductionCoerced( 432 VECTOR_OP_ADD, Short512Vector.class, short.class, LENGTH, 433 this, 434 v -> (long) v.rOp((short) 0, (i, a, b) -> (short) (a + b))); 435 } 436 437 @Override 438 @ForceInline 439 public short andAll() { 440 return (short) VectorIntrinsics.reductionCoerced( 441 VECTOR_OP_AND, Short512Vector.class, short.class, LENGTH, 442 this, 443 v -> (long) v.rOp((short) -1, (i, a, b) -> (short) (a & b))); 444 } 445 446 // Memory operations 447 448 @Override 449 @ForceInline 450 public void intoArray(short[] a, int ix) { 451 Objects.requireNonNull(a); 452 ix = VectorIntrinsics.checkIndex(ix, a.length, LENGTH); 453 VectorIntrinsics.store(Short512Vector.class, short.class, LENGTH, 454 a, ix, this, 455 (arr, idx, v) -> v.forEach((i, a_) -> ((short[])arr)[idx + i] = a_)); 456 } 457 458 @Override 459 @ForceInline 460 public void intoArray(short[] a, int ax, Mask<Short, Shapes.S512Bit> m) { 461 // TODO: use better default impl: forEach(m, (i, a_) -> a[ax + i] = a_); 462 Short512Vector oldVal = SPECIES.fromArray(a, ax); 463 Short512Vector newVal = oldVal.blend(this, m); 464 newVal.intoArray(a, ax); 465 } 466 467 // 468 469 @Override 470 public String toString() { 471 return Arrays.toString(getElements()); 472 } 473 474 @Override 475 public boolean equals(Object o) { 476 if (this == o) return true; 477 if (o == null || this.getClass() != o.getClass()) return false; 478 479 Short512Vector that = (Short512Vector) o; 480 return Arrays.equals(this.getElements(), that.getElements()); 481 } 482 483 @Override 484 public int hashCode() { 485 return Arrays.hashCode(vec); 486 } 487 488 // Binary test 489 490 @Override 491 Short512Mask bTest(Vector<Short, Shapes.S512Bit> o, FBinTest f) { 492 short[] vec1 = getElements(); 493 short[] vec2 = ((Short512Vector)o).getElements(); 494 boolean[] bits = new boolean[length()]; 495 for (int i = 0; i < length(); i++){ 496 bits[i] = f.apply(i, vec1[i], vec2[i]); 497 } 498 return new Short512Mask(bits); 499 } 500 501 // Comparisons 502 503 @Override 504 @ForceInline 505 public Short512Mask equal(Vector<Short, Shapes.S512Bit> o) { 506 Objects.requireNonNull(o); 507 Short512Vector v = (Short512Vector)o; 508 509 return (Short512Mask) VectorIntrinsics.compare( 510 BT_eq, Short512Vector.class, Short512Mask.class, short.class, LENGTH, 511 this, v, 512 (v1, v2) -> v1.bTest(v2, (i, a, b) -> a == b)); 513 } 514 515 @Override 516 @ForceInline 517 public Short512Mask notEqual(Vector<Short, Shapes.S512Bit> o) { 518 Objects.requireNonNull(o); 519 Short512Vector v = (Short512Vector)o; 520 521 return (Short512Mask) VectorIntrinsics.compare( 522 BT_ne, Short512Vector.class, Short512Mask.class, short.class, LENGTH, 523 this, v, 524 (v1, v2) -> v1.bTest(v2, (i, a, b) -> a != b)); 525 } 526 527 @Override 528 @ForceInline 529 public Short512Mask lessThan(Vector<Short, Shapes.S512Bit> o) { 530 Objects.requireNonNull(o); 531 Short512Vector v = (Short512Vector)o; 532 533 return (Short512Mask) VectorIntrinsics.compare( 534 BT_lt, Short512Vector.class, Short512Mask.class, short.class, LENGTH, 535 this, v, 536 (v1, v2) -> v1.bTest(v2, (i, a, b) -> a < b)); 537 } 538 539 @Override 540 @ForceInline 541 public Short512Mask lessThanEq(Vector<Short, Shapes.S512Bit> o) { 542 Objects.requireNonNull(o); 543 Short512Vector v = (Short512Vector)o; 544 545 return (Short512Mask) VectorIntrinsics.compare( 546 BT_le, Short512Vector.class, Short512Mask.class, short.class, LENGTH, 547 this, v, 548 (v1, v2) -> v1.bTest(v2, (i, a, b) -> a <= b)); 549 } 550 551 @Override 552 @ForceInline 553 public Short512Mask greaterThan(Vector<Short, Shapes.S512Bit> o) { 554 Objects.requireNonNull(o); 555 Short512Vector v = (Short512Vector)o; 556 557 return (Short512Mask) VectorIntrinsics.compare( 558 BT_gt, Short512Vector.class, Short512Mask.class, short.class, LENGTH, 559 this, v, 560 (v1, v2) -> v1.bTest(v2, (i, a, b) -> a > b)); 561 } 562 563 @Override 564 @ForceInline 565 public Short512Mask greaterThanEq(Vector<Short, Shapes.S512Bit> o) { 566 Objects.requireNonNull(o); 567 Short512Vector v = (Short512Vector)o; 568 569 return (Short512Mask) VectorIntrinsics.compare( 570 BT_ge, Short512Vector.class, Short512Mask.class, short.class, LENGTH, 571 this, v, 572 (v1, v2) -> v1.bTest(v2, (i, a, b) -> a >= b)); 573 } 574 575 // Foreach 576 577 @Override 578 void forEach(FUnCon f) { 579 short[] vec = getElements(); 580 for (int i = 0; i < length(); i++) { 581 f.apply(i, vec[i]); 582 } 583 } 584 585 @Override 586 void forEach(Mask<Short, Shapes.S512Bit> o, FUnCon f) { 587 boolean[] mbits = ((Short512Mask)o).getBits(); 588 forEach((i, a) -> { 589 if (mbits[i]) { f.apply(i, a); } 590 }); 591 } 592 593 594 595 @Override 596 public Short512Vector rotateEL(int j) { 597 short[] vec = getElements(); 598 short[] res = new short[length()]; 599 for (int i = 0; i < length(); i++){ 600 res[(j + i) % length()] = vec[i]; 601 } 602 return new Short512Vector(res); 603 } 604 605 @Override 606 public Short512Vector rotateER(int j) { 607 short[] vec = getElements(); 608 short[] res = new short[length()]; 609 for (int i = 0; i < length(); i++){ 610 int z = i - j; 611 if(j < 0) { 612 res[length() + z] = vec[i]; 613 } else { 614 res[z] = vec[i]; 615 } 616 } 617 return new Short512Vector(res); 618 } 619 620 @Override 621 public Short512Vector shiftEL(int j) { 622 short[] vec = getElements(); 623 short[] res = new short[length()]; 624 for (int i = 0; i < length() - j; i++) { 625 res[i] = vec[i + j]; 626 } 627 return new Short512Vector(res); 628 } 629 630 @Override 631 public Short512Vector shiftER(int j) { 632 short[] vec = getElements(); 633 short[] res = new short[length()]; 634 for (int i = 0; i < length() - j; i++){ 635 res[i + j] = vec[i]; 636 } 637 return new Short512Vector(res); 638 } 639 640 @Override 641 public Short512Vector shuffle(Vector<Short, Shapes.S512Bit> o, Shuffle<Short, Shapes.S512Bit> s) { 642 Short512Vector v = (Short512Vector) o; 643 return uOp((i, a) -> { 644 short[] vec = this.getElements(); 645 int e = s.getElement(i); 646 if(e >= 0 && e < length()) { 647 //from this 648 return vec[e]; 649 } else if(e < length() * 2) { 650 //from o 651 return v.getElements()[e - length()]; 652 } else { 653 throw new ArrayIndexOutOfBoundsException("Bad reordering for shuffle"); 654 } 655 }); 656 } 657 658 @Override 659 public Short512Vector swizzle(Shuffle<Short, Shapes.S512Bit> s) { 660 return uOp((i, a) -> { 661 short[] vec = this.getElements(); 662 int e = s.getElement(i); 663 if(e >= 0 && e < length()) { 664 return vec[e]; 665 } else { 666 throw new ArrayIndexOutOfBoundsException("Bad reordering for shuffle"); 667 } 668 }); 669 } 670 671 @Override 672 @ForceInline 673 public Short512Vector blend(Vector<Short, Shapes.S512Bit> o1, Mask<Short, Shapes.S512Bit> o2) { 674 Objects.requireNonNull(o1); 675 Objects.requireNonNull(o2); 676 Short512Vector v = (Short512Vector)o1; 677 Short512Mask m = (Short512Mask)o2; 678 679 return (Short512Vector) VectorIntrinsics.blend( 680 Short512Vector.class, Short512Mask.class, short.class, LENGTH, 681 this, v, m, 682 (v1, v2, m_) -> v1.bOp(v2, (i, a, b) -> m_.getElement(i) ? b : a)); 683 } 684 685 // Accessors 686 687 @Override 688 public short get(int i) { 689 short[] vec = getElements(); 690 return vec[i]; 691 } 692 693 @Override 694 public Short512Vector with(int i, short e) { 695 short[] res = vec.clone(); 696 res[i] = e; 697 return new Short512Vector(res); 698 } 699 700 // Mask 701 702 static final class Short512Mask extends AbstractMask<Short, Shapes.S512Bit> { 703 static final Short512Mask TRUE_MASK = new Short512Mask(true); 704 static final Short512Mask FALSE_MASK = new Short512Mask(false); 705 706 // FIXME: was temporarily put here to simplify rematerialization support in the JVM 707 private final boolean[] bits; // Don't access directly, use getBits() instead. 708 709 public Short512Mask(boolean[] bits) { 710 this(bits, 0); 711 } 712 713 public Short512Mask(boolean[] bits, int i) { 714 this.bits = Arrays.copyOfRange(bits, i, i + species().length()); 715 } 716 717 public Short512Mask(boolean val) { 718 boolean[] bits = new boolean[species().length()]; 719 Arrays.fill(bits, val); 720 this.bits = bits; 721 } 722 723 boolean[] getBits() { 724 return VectorIntrinsics.maybeRebox(this).bits; 725 } 726 727 @Override 728 Short512Mask uOp(MUnOp f) { 729 boolean[] res = new boolean[species().length()]; 730 boolean[] bits = getBits(); 731 for (int i = 0; i < species().length(); i++) { 732 res[i] = f.apply(i, bits[i]); 733 } 734 return new Short512Mask(res); 735 } 736 737 @Override 738 Short512Mask bOp(Mask<Short, Shapes.S512Bit> o, MBinOp f) { 739 boolean[] res = new boolean[species().length()]; 740 boolean[] bits = getBits(); 741 boolean[] mbits = ((Short512Mask)o).getBits(); 742 for (int i = 0; i < species().length(); i++) { 743 res[i] = f.apply(i, bits[i], mbits[i]); 744 } 745 return new Short512Mask(res); 746 } 747 748 @Override 749 public Short512Species species() { 750 return SPECIES; 751 } 752 753 @Override 754 public Short512Vector toVector() { 755 short[] res = new short[species().length()]; 756 boolean[] bits = getBits(); 757 for (int i = 0; i < species().length(); i++) { 758 res[i] = (short) (bits[i] ? -1 : 0); 759 } 760 return new Short512Vector(res); 761 } 762 763 @Override 764 @ForceInline 765 @SuppressWarnings("unchecked") 766 public <Z> Mask<Z, Shapes.S512Bit> rebracket(Species<Z, Shapes.S512Bit> species) { 767 Objects.requireNonNull(species); 768 // TODO: check proper element type 769 return VectorIntrinsics.reinterpret( 770 Short512Mask.class, short.class, LENGTH, 771 species.elementType(), species.length(), this, 772 (m, t) -> m.reshape(species) 773 ); 774 } 775 776 // Unary operations 777 778 //Mask<E, S> not(); 779 780 // Binary operations 781 782 @Override 783 @ForceInline 784 public Short512Mask and(Mask<Short,Shapes.S512Bit> o) { 785 Objects.requireNonNull(o); 786 Short512Mask m = (Short512Mask)o; 787 return VectorIntrinsics.binaryOp(VECTOR_OP_AND, Short512Mask.class, short.class, LENGTH, 788 this, m, 789 (m1, m2) -> m1.bOp(m2, (i, a, b) -> a & b)); 790 } 791 792 @Override 793 @ForceInline 794 public Short512Mask or(Mask<Short,Shapes.S512Bit> o) { 795 Objects.requireNonNull(o); 796 Short512Mask m = (Short512Mask)o; 797 return VectorIntrinsics.binaryOp(VECTOR_OP_OR, Short512Mask.class, short.class, LENGTH, 798 this, m, 799 (m1, m2) -> m1.bOp(m2, (i, a, b) -> a | b)); 800 } 801 802 // Reductions 803 804 @Override 805 @ForceInline 806 public boolean anyTrue() { 807 return VectorIntrinsics.test(COND_notZero, Short512Mask.class, short.class, LENGTH, 808 this, this, 809 (m1, m2) -> super.anyTrue()); 810 } 811 812 @Override 813 @ForceInline 814 public boolean allTrue() { 815 return VectorIntrinsics.test(COND_carrySet, Short512Mask.class, short.class, LENGTH, 816 this, species().maskAllTrue(), 817 (m1, m2) -> super.allTrue()); 818 } 819 } 820 821 // Shuffle 822 823 static final class Short512Shuffle extends AbstractShuffle<Short, Shapes.S512Bit> { 824 static final IntVector.IntSpecies<Shapes.S512Bit> INT_SPECIES = IntVector.speciesInstance(Shapes.S_512_BIT); 825 826 public Short512Shuffle(int[] reorder) { 827 super(reorder); 828 } 829 830 public Short512Shuffle(int[] reorder, int i) { 831 super(reorder, i); 832 } 833 834 @Override 835 public Short512Species species() { 836 return SPECIES; 837 } 838 839 @Override 840 public IntVector.IntSpecies<Shapes.S512Bit> intSpecies() { 841 return INT_SPECIES; 842 } 843 } 844 845 // Species 846 847 @Override 848 public Short512Species species() { 849 return SPECIES; 850 } 851 852 static final class Short512Species extends ShortSpecies<Shapes.S512Bit> { 853 static final int BIT_SIZE = Shapes.S_512_BIT.bitSize(); 854 855 static final int LENGTH = BIT_SIZE / Short.SIZE; 856 857 @Override 858 public String toString() { 859 StringBuilder sb = new StringBuilder("Shape["); 860 sb.append(bitSize()).append(" bits, "); 861 sb.append(length()).append(" ").append(short.class.getSimpleName()).append("s x "); 862 sb.append(elementSize()).append(" bits"); 863 sb.append("]"); 864 return sb.toString(); 865 } 866 867 @Override 868 @ForceInline 869 public int bitSize() { 870 return BIT_SIZE; 871 } 872 873 @Override 874 @ForceInline 875 public int length() { 876 return LENGTH; 877 } 878 879 @Override 880 @ForceInline 881 public Class<Short> elementType() { 882 return short.class; 883 } 884 885 @Override 886 @ForceInline 887 public int elementSize() { 888 return Short.SIZE; 889 } 890 891 @Override 892 @ForceInline 893 public Shapes.S512Bit shape() { 894 return Shapes.S_512_BIT; 895 } 896 897 @Override 898 Short512Vector op(FOp f) { 899 short[] res = new short[length()]; 900 for (int i = 0; i < length(); i++) { 901 res[i] = f.apply(i); 902 } 903 return new Short512Vector(res); 904 } 905 906 @Override 907 Short512Vector op(Mask<Short, Shapes.S512Bit> o, FOp f) { 908 short[] res = new short[length()]; 909 boolean[] mbits = ((Short512Mask)o).getBits(); 910 for (int i = 0; i < length(); i++) { 911 if (mbits[i]) { 912 res[i] = f.apply(i); 913 } 914 } 915 return new Short512Vector(res); 916 } 917 918 // Factories 919 920 @Override 921 public Short512Mask maskFromValues(boolean... bits) { 922 return new Short512Mask(bits); 923 } 924 925 @Override 926 public Short512Mask maskFromArray(boolean[] bits, int i) { 927 return new Short512Mask(bits, i); 928 } 929 930 @Override 931 public Short512Shuffle shuffleFromValues(int... ixs) { 932 return new Short512Shuffle(ixs); 933 } 934 935 @Override 936 public Short512Shuffle shuffleFromArray(int[] ixs, int i) { 937 return new Short512Shuffle(ixs, i); 938 } 939 940 @Override 941 public Short512Shuffle shuffleFromVector(Vector<Integer, Shapes.S512Bit> v) { 942 int[] a = ((IntVector<Shapes.S512Bit>) v).toArray(); 943 return new Short512Shuffle(a, 0); 944 } 945 946 @Override 947 @ForceInline 948 public Short512Vector zero() { 949 return VectorIntrinsics.broadcastCoerced(Short512Vector.class, short.class, LENGTH, 950 0, 951 (z -> ZERO)); 952 } 953 954 @Override 955 @ForceInline 956 public Short512Vector broadcast(short e) { 957 return VectorIntrinsics.broadcastCoerced( 958 Short512Vector.class, short.class, LENGTH, 959 e, 960 ((long bits) -> SPECIES.op(i -> (short)bits))); 961 } 962 963 @Override 964 @ForceInline 965 public Short512Mask maskAllTrue() { 966 return VectorIntrinsics.broadcastCoerced(Short512Mask.class, short.class, LENGTH, 967 (short)-1, 968 (z -> Short512Mask.TRUE_MASK)); 969 } 970 971 @Override 972 @ForceInline 973 public Short512Mask maskAllFalse() { 974 return VectorIntrinsics.broadcastCoerced(Short512Mask.class, short.class, LENGTH, 975 0, 976 (z -> Short512Mask.FALSE_MASK)); 977 } 978 979 @Override 980 @ForceInline 981 public Short512Vector fromArray(short[] a, int ix) { 982 Objects.requireNonNull(a); 983 ix = VectorIntrinsics.checkIndex(ix, a.length, LENGTH); 984 return (Short512Vector) VectorIntrinsics.load(Short512Vector.class, short.class, LENGTH, 985 a, ix, 986 (arr, idx) -> super.fromArray((short[]) arr, idx)); 987 } 988 989 @Override 990 @ForceInline 991 public Short512Vector fromArray(short[] a, int ax, Mask<Short, Shapes.S512Bit> m) { 992 return zero().blend(fromArray(a, ax), m); // TODO: use better default impl: op(m, i -> a[ax + i]); 993 } 994 995 @Override 996 @ForceInline 997 @SuppressWarnings("unchecked") 998 public <F> Short512Vector rebracket(Vector<F, Shapes.S512Bit> o) { 999 Objects.requireNonNull(o); 1000 if (o.elementType() == byte.class) { 1001 Byte512Vector so = (Byte512Vector)o; 1002 return VectorIntrinsics.reinterpret( 1003 Byte512Vector.class, byte.class, so.length(), 1004 short.class, LENGTH, so, 1005 (v, t) -> (Short512Vector)reshape(v) 1006 ); 1007 } else if (o.elementType() == short.class) { 1008 Short512Vector so = (Short512Vector)o; 1009 return VectorIntrinsics.reinterpret( 1010 Short512Vector.class, short.class, so.length(), 1011 short.class, LENGTH, so, 1012 (v, t) -> (Short512Vector)reshape(v) 1013 ); 1014 } else if (o.elementType() == int.class) { 1015 Int512Vector so = (Int512Vector)o; 1016 return VectorIntrinsics.reinterpret( 1017 Int512Vector.class, int.class, so.length(), 1018 short.class, LENGTH, so, 1019 (v, t) -> (Short512Vector)reshape(v) 1020 ); 1021 } else if (o.elementType() == long.class) { 1022 Long512Vector so = (Long512Vector)o; 1023 return VectorIntrinsics.reinterpret( 1024 Long512Vector.class, long.class, so.length(), 1025 short.class, LENGTH, so, 1026 (v, t) -> (Short512Vector)reshape(v) 1027 ); 1028 } else if (o.elementType() == float.class) { 1029 Float512Vector so = (Float512Vector)o; 1030 return VectorIntrinsics.reinterpret( 1031 Float512Vector.class, float.class, so.length(), 1032 short.class, LENGTH, so, 1033 (v, t) -> (Short512Vector)reshape(v) 1034 ); 1035 } else if (o.elementType() == double.class) { 1036 Double512Vector so = (Double512Vector)o; 1037 return VectorIntrinsics.reinterpret( 1038 Double512Vector.class, double.class, so.length(), 1039 short.class, LENGTH, so, 1040 (v, t) -> (Short512Vector)reshape(v) 1041 ); 1042 } else { 1043 throw new InternalError("Unimplemented size"); 1044 } 1045 } 1046 1047 @Override 1048 @ForceInline 1049 @SuppressWarnings("unchecked") 1050 public <T extends Shape> Short512Vector resize(Vector<Short, T> o) { 1051 Objects.requireNonNull(o); 1052 if (o.bitSize() == 64) { 1053 Short64Vector so = (Short64Vector)o; 1054 return VectorIntrinsics.reinterpret( 1055 Short64Vector.class, short.class, so.length(), 1056 short.class, LENGTH, so, 1057 (v, t) -> (Short512Vector)reshape(v) 1058 ); 1059 } else if (o.bitSize() == 128) { 1060 Short128Vector so = (Short128Vector)o; 1061 return VectorIntrinsics.reinterpret( 1062 Short128Vector.class, short.class, so.length(), 1063 short.class, LENGTH, so, 1064 (v, t) -> (Short512Vector)reshape(v) 1065 ); 1066 } else if (o.bitSize() == 256) { 1067 Short256Vector so = (Short256Vector)o; 1068 return VectorIntrinsics.reinterpret( 1069 Short256Vector.class, short.class, so.length(), 1070 short.class, LENGTH, so, 1071 (v, t) -> (Short512Vector)reshape(v) 1072 ); 1073 } else if (o.bitSize() == 512) { 1074 Short512Vector so = (Short512Vector)o; 1075 return VectorIntrinsics.reinterpret( 1076 Short512Vector.class, short.class, so.length(), 1077 short.class, LENGTH, so, 1078 (v, t) -> (Short512Vector)reshape(v) 1079 ); 1080 } else { 1081 throw new InternalError("Unimplemented size"); 1082 } 1083 } 1084 } 1085 }