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