Return to site

Java 26 — JEP 529: Vector API (Incubator)

· java
// "Species" describes the vector shape (lane count + preferred SIMD width on this CPU).
 static final VectorSpecies〈Float〉SPECIES = FloatVector.SPECIES_PREFERRED;

 /**
 * Computes, for each element i: c[i] = -(a[i]*a[i] + b[i]*b[i])
 * but does it in SIMD chunks using the Vector API.
 */
 void vectorComputation( float[] a, float[] b, float[] c ) {
 int i = 0;
 int upperBound = SPECIES.loopBound( a.length );

 for ( ; i 〈 upperBound; i += SPECIES.length() ) {
 // Load a vector from a[i .. i+lanes-1], same for b[]
 var va = FloatVector.fromArray( SPECIES, a, i );
 var vb = FloatVector.fromArray( SPECIES, b, i );

 // 1) va.mul(va) =〉 a^2
 // 2) vb.mul(vb) =〉 b^2
 // 3) .add(...) =〉 a^2 + b^2
 // 4) .neg() =〉-(a^2 + b^2)
 // 5) .intoArray(c, i) =〉store result into c[i .. i+lanes-1]
 va.mul( va )
 .add( vb.mul( vb ) )
 .neg()
 .intoArray( c, i );
 }
 }

• The Vector API is Java’s portable SIMD story for data-heavy workloads.

• You write vector operations in Java and let the JVM map them to the best hardware instructions available.

• It is still incubating, so it is best suited to performance-focused code paths and libraries.

#java #java26 #jep529 #vector #performance

Go further with Java certification:

Java👇

Spring👇

SpringBook👇

JavaBook👇