104 */
105 public abstract class VectorMask<E> {
106 VectorMask() {}
107
108 /**
109 * Returns the species of this mask.
110 *
111 * @return the species of this mask
112 */
113 public abstract VectorSpecies<E> species();
114
115 /**
116 * Returns the number of mask lanes (the length).
117 *
118 * @return the number of mask lanes
119 */
120 public int length() { return species().length(); }
121
122 /**
123 * Returns a mask where each lane is set or unset according to given
124 * {@code boolean} values
125 * <p>
126 * For each mask lane, where {@code N} is the mask lane index,
127 * if the given {@code boolean} value at index {@code N} is {@code true}
128 * then the mask lane at index {@code N} is set, otherwise it is unset.
129 *
130 * @param species mask species
131 * @param bits the given {@code boolean} values
132 * @return a mask where each lane is set or unset according to the given {@code boolean} value
133 * @throws IndexOutOfBoundsException if {@code bits.length < species.length()}
134 */
135 @ForceInline
136 public static <E> VectorMask<E> fromValues(VectorSpecies<E> species, boolean... bits) {
137 return fromArray(species, bits, 0);
138 }
139
140 /**
141 * Loads a mask from a {@code boolean} array starting at an offset.
142 * <p>
143 * For each mask lane, where {@code N} is the mask lane index,
144 * if the array element at index {@code ix + N} is {@code true} then the
145 * mask lane at index {@code N} is set, otherwise it is unset.
146 *
147 * @param species mask species
148 * @param bits the {@code boolean} array
149 * @param offset the offset into the array
150 * @return the mask loaded from a {@code boolean} array
151 * @throws IndexOutOfBoundsException if {@code offset < 0}, or
152 * {@code offset > bits.length - species.length()}
153 */
154 @ForceInline
155 @SuppressWarnings("unchecked")
156 public static <E> VectorMask<E> fromArray(VectorSpecies<E> species, boolean[] bits, int offset) {
157 Objects.requireNonNull(bits);
158 offset = VectorIntrinsics.checkIndex(offset, bits.length, species.length());
159 return VectorIntrinsics.load((Class<VectorMask<E>>) species.maskType(), species.elementType(), species.length(),
160 bits, (long) offset + Unsafe.ARRAY_BOOLEAN_BASE_OFFSET,
161 bits, offset, species,
162 (boolean[] c, int idx, VectorSpecies<E> s) -> ((AbstractSpecies<E>)s).opm(n -> c[idx + n]));
163 }
164
165 /**
166 * Returns a mask where all lanes are set.
167 *
168 * @param species mask species
169 * @return a mask where all lanes are set
170 */
171 @ForceInline
172 @SuppressWarnings("unchecked")
173 public static <E> VectorMask<E> maskAllTrue(VectorSpecies<E> species) {
174 return VectorIntrinsics.broadcastCoerced((Class<VectorMask<E>>) species.maskType(), species.elementType(), species.length(),
175 -1, species,
176 ((z, s) -> AbstractMask.trueMask(s)));
177 }
178
179 /**
180 * Returns a mask where all lanes are unset.
181 *
182 * @param species mask species
183 * @return a mask where all lanes are unset
184 */
185 @ForceInline
186 @SuppressWarnings("unchecked")
187 public static <E> VectorMask<E> maskAllFalse(VectorSpecies<E> species) {
188 return VectorIntrinsics.broadcastCoerced((Class<VectorMask<E>>) species.maskType(), species.elementType(), species.length(),
189 0, species,
190 ((z, s) -> AbstractMask.falseMask(s)));
191 }
192
193 /**
194 * Converts this mask to a mask of the given species shape of element type {@code F}.
195 * <p>
196 * For each mask lane, where {@code N} is the lane index, if the
197 * mask lane at index {@code N} is set, then the mask lane at index
198 * {@code N} of the resulting mask is set, otherwise that mask lane is
199 * not set.
200 *
201 * @param s the species of the desired mask
202 * @param <F> the boxed element type of the species
203 * @return a mask converted by shape and element type
|
104 */
105 public abstract class VectorMask<E> {
106 VectorMask() {}
107
108 /**
109 * Returns the species of this mask.
110 *
111 * @return the species of this mask
112 */
113 public abstract VectorSpecies<E> species();
114
115 /**
116 * Returns the number of mask lanes (the length).
117 *
118 * @return the number of mask lanes
119 */
120 public int length() { return species().length(); }
121
122 /**
123 * Returns a mask where each lane is set or unset according to given
124 * {@code boolean} values.
125 * <p>
126 * For each mask lane, where {@code N} is the mask lane index,
127 * if the given {@code boolean} value at index {@code N} is {@code true}
128 * then the mask lane at index {@code N} is set, otherwise it is unset.
129 *
130 * @param species mask species
131 * @param bits the given {@code boolean} values
132 * @return a mask where each lane is set or unset according to the given {@code boolean} value
133 * @throws IndexOutOfBoundsException if {@code bits.length < species.length()}
134 * @see Vector#maskFromValues(boolean...)
135 */
136 @ForceInline
137 public static <E> VectorMask<E> fromValues(VectorSpecies<E> species, boolean... bits) {
138 return fromArray(species, bits, 0);
139 }
140
141 /**
142 * Loads a mask from a {@code boolean} array starting at an offset.
143 * <p>
144 * For each mask lane, where {@code N} is the mask lane index,
145 * if the array element at index {@code ix + N} is {@code true} then the
146 * mask lane at index {@code N} is set, otherwise it is unset.
147 *
148 * @param species mask species
149 * @param bits the {@code boolean} array
150 * @param offset the offset into the array
151 * @return the mask loaded from a {@code boolean} array
152 * @throws IndexOutOfBoundsException if {@code offset < 0}, or
153 * {@code offset > bits.length - species.length()}
154 * @see Vector#maskFromArray(boolean[], int)
155 */
156 @ForceInline
157 @SuppressWarnings("unchecked")
158 public static <E> VectorMask<E> fromArray(VectorSpecies<E> species, boolean[] bits, int offset) {
159 Objects.requireNonNull(bits);
160 offset = VectorIntrinsics.checkIndex(offset, bits.length, species.length());
161 return VectorIntrinsics.load((Class<VectorMask<E>>) species.maskType(), species.elementType(), species.length(),
162 bits, (long) offset + Unsafe.ARRAY_BOOLEAN_BASE_OFFSET,
163 bits, offset, species,
164 (boolean[] c, int idx, VectorSpecies<E> s) -> ((AbstractSpecies<E>)s).opm(n -> c[idx + n]));
165 }
166
167 /**
168 * Returns a mask where all lanes are set.
169 *
170 * @param species mask species
171 * @return a mask where all lanes are set
172 * @see Vector#maskAllTrue()
173 */
174 @ForceInline
175 @SuppressWarnings("unchecked")
176 public static <E> VectorMask<E> maskAllTrue(VectorSpecies<E> species) {
177 return VectorIntrinsics.broadcastCoerced((Class<VectorMask<E>>) species.maskType(), species.elementType(), species.length(),
178 -1, species,
179 ((z, s) -> AbstractMask.trueMask(s)));
180 }
181
182 /**
183 * Returns a mask where all lanes are unset.
184 *
185 * @param species mask species
186 * @return a mask where all lanes are unset
187 * @see Vector#maskAllFalse()
188 */
189 @ForceInline
190 @SuppressWarnings("unchecked")
191 public static <E> VectorMask<E> maskAllFalse(VectorSpecies<E> species) {
192 return VectorIntrinsics.broadcastCoerced((Class<VectorMask<E>>) species.maskType(), species.elementType(), species.length(),
193 0, species,
194 ((z, s) -> AbstractMask.falseMask(s)));
195 }
196
197 /**
198 * Converts this mask to a mask of the given species shape of element type {@code F}.
199 * <p>
200 * For each mask lane, where {@code N} is the lane index, if the
201 * mask lane at index {@code N} is set, then the mask lane at index
202 * {@code N} of the resulting mask is set, otherwise that mask lane is
203 * not set.
204 *
205 * @param s the species of the desired mask
206 * @param <F> the boxed element type of the species
207 * @return a mask converted by shape and element type
|