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