1 /*
2 * Copyright (c) 2015, 2025, 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.
8 *
9 * This code is distributed in the hope that it will be useful, but WITHOUT
10 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
11 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
12 * version 2 for more details (a copy is included in the LICENSE file that
13 * accompanied this code).
14 *
15 * You should have received a copy of the GNU General Public License version
16 * 2 along with this work; if not, write to the Free Software Foundation,
17 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
18 *
19 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
20 * or visit www.oracle.com if you need additional information or have any
21 * questions.
22 */
23
24 /*
25 * @test
26 * @comment Set CompileThresholdScaling to 0.1 so that the warmup loop sets to 2000 iterations
27 * to hit compilation thresholds
28 * @run junit/othervm -Diters=2000 -XX:CompileThresholdScaling=0.1 VarHandleTestMethodHandleAccessFloat
29 */
30
31 import java.lang.invoke.MethodHandle;
32 import java.lang.invoke.MethodHandles;
33 import java.lang.invoke.VarHandle;
34 import java.util.ArrayList;
35 import java.util.List;
36
37 import static org.junit.jupiter.api.Assertions.*;
38 import org.junit.jupiter.api.BeforeAll;
39 import org.junit.jupiter.api.TestInstance;
40 import org.junit.jupiter.params.ParameterizedTest;
41 import org.junit.jupiter.params.provider.MethodSource;
42
43 @TestInstance(TestInstance.Lifecycle.PER_CLASS)
44 public class VarHandleTestMethodHandleAccessFloat extends VarHandleBaseTest {
45 static final float static_final_v = 1.0f;
46
47 static float static_v;
48
49 final float final_v = 1.0f;
50
51 float v;
52
53 VarHandle vhFinalField;
54
55 VarHandle vhField;
56
57 VarHandle vhStaticField;
58
59 VarHandle vhStaticFinalField;
60
61 VarHandle vhArray;
62
63 @BeforeAll
64 public void setup() throws Exception {
65 vhFinalField = MethodHandles.lookup().findVarHandle(
66 VarHandleTestMethodHandleAccessFloat.class, "final_v", float.class);
67
68 vhField = MethodHandles.lookup().findVarHandle(
69 VarHandleTestMethodHandleAccessFloat.class, "v", float.class);
70
71 vhStaticFinalField = MethodHandles.lookup().findStaticVarHandle(
72 VarHandleTestMethodHandleAccessFloat.class, "static_final_v", float.class);
73
74 vhStaticField = MethodHandles.lookup().findStaticVarHandle(
75 VarHandleTestMethodHandleAccessFloat.class, "static_v", float.class);
76
77 vhArray = MethodHandles.arrayElementVarHandle(float[].class);
78 }
79
80 public Object[][] accessTestCaseProvider() throws Exception {
81 List<AccessTestCase<?>> cases = new ArrayList<>();
82
83 for (VarHandleToMethodHandle f : VarHandleToMethodHandle.values()) {
84 cases.add(new MethodHandleAccessTestCase("Instance field",
85 vhField, f, hs -> testInstanceField(this, hs)));
86 cases.add(new MethodHandleAccessTestCase("Instance field unsupported",
87 vhField, f, hs -> testInstanceFieldUnsupported(this, hs),
88 false));
89
90 cases.add(new MethodHandleAccessTestCase("Static field",
91 vhStaticField, f, VarHandleTestMethodHandleAccessFloat::testStaticField));
92 cases.add(new MethodHandleAccessTestCase("Static field unsupported",
93 vhStaticField, f, VarHandleTestMethodHandleAccessFloat::testStaticFieldUnsupported,
94 false));
95
96 cases.add(new MethodHandleAccessTestCase("Array",
97 vhArray, f, VarHandleTestMethodHandleAccessFloat::testArray));
98 cases.add(new MethodHandleAccessTestCase("Array unsupported",
99 vhArray, f, VarHandleTestMethodHandleAccessFloat::testArrayUnsupported,
100 false));
101 cases.add(new MethodHandleAccessTestCase("Array index out of bounds",
102 vhArray, f, VarHandleTestMethodHandleAccessFloat::testArrayIndexOutOfBounds,
103 false));
104 }
105
106 // Work around issue with jtreg summary reporting which truncates
107 // the String result of Object.toString to 30 characters, hence
108 // the first dummy argument
109 return cases.stream().map(tc -> new Object[]{tc.toString(), tc}).toArray(Object[][]::new);
110 }
111
112 @ParameterizedTest
113 @MethodSource("accessTestCaseProvider")
114 public <T> void testAccess(String desc, AccessTestCase<T> atc) throws Throwable {
115 T t = atc.get();
116 int iters = atc.requiresLoop() ? ITERS : 1;
117 for (int c = 0; c < iters; c++) {
118 atc.testAccess(t);
119 }
120 }
121
122 static void testInstanceField(VarHandleTestMethodHandleAccessFloat recv, Handles hs) throws Throwable {
123 // Plain
124 {
125 hs.get(TestAccessMode.SET).invokeExact(recv, 1.0f);
126 float x = (float) hs.get(TestAccessMode.GET).invokeExact(recv);
127 assertEquals(1.0f, x, "set float value");
128 }
129
130
131 // Volatile
132 {
133 hs.get(TestAccessMode.SET_VOLATILE).invokeExact(recv, 2.0f);
134 float x = (float) hs.get(TestAccessMode.GET_VOLATILE).invokeExact(recv);
135 assertEquals(2.0f, x, "setVolatile float value");
136 }
137
138 // Lazy
139 {
140 hs.get(TestAccessMode.SET_RELEASE).invokeExact(recv, 1.0f);
141 float x = (float) hs.get(TestAccessMode.GET_ACQUIRE).invokeExact(recv);
142 assertEquals(1.0f, x, "setRelease float value");
143 }
144
145 // Opaque
146 {
147 hs.get(TestAccessMode.SET_OPAQUE).invokeExact(recv, 2.0f);
148 float x = (float) hs.get(TestAccessMode.GET_OPAQUE).invokeExact(recv);
149 assertEquals(2.0f, x, "setOpaque float value");
150 }
151
152 hs.get(TestAccessMode.SET).invokeExact(recv, 1.0f);
153
154 // Compare
155 {
156 boolean r = (boolean) hs.get(TestAccessMode.COMPARE_AND_SET).invokeExact(recv, 1.0f, 2.0f);
157 assertEquals(r, true, "success compareAndSet float");
158 float x = (float) hs.get(TestAccessMode.GET).invokeExact(recv);
159 assertEquals(2.0f, x, "success compareAndSet float value");
160 }
161
162 {
163 boolean r = (boolean) hs.get(TestAccessMode.COMPARE_AND_SET).invokeExact(recv, 1.0f, 3.0f);
164 assertEquals(r, false, "failing compareAndSet float");
165 float x = (float) hs.get(TestAccessMode.GET).invokeExact(recv);
166 assertEquals(2.0f, x, "failing compareAndSet float value");
167 }
168
169 {
170 float r = (float) hs.get(TestAccessMode.COMPARE_AND_EXCHANGE).invokeExact(recv, 2.0f, 1.0f);
171 assertEquals(r, 2.0f, "success compareAndExchange float");
172 float x = (float) hs.get(TestAccessMode.GET).invokeExact(recv);
173 assertEquals(1.0f, x, "success compareAndExchange float value");
174 }
175
176 {
177 float r = (float) hs.get(TestAccessMode.COMPARE_AND_EXCHANGE).invokeExact(recv, 2.0f, 3.0f);
178 assertEquals(r, 1.0f, "failing compareAndExchange float");
179 float x = (float) hs.get(TestAccessMode.GET).invokeExact(recv);
180 assertEquals(1.0f, x, "failing compareAndExchange float value");
181 }
182
183 {
184 float r = (float) hs.get(TestAccessMode.COMPARE_AND_EXCHANGE_ACQUIRE).invokeExact(recv, 1.0f, 2.0f);
185 assertEquals(r, 1.0f, "success compareAndExchangeAcquire float");
186 float x = (float) hs.get(TestAccessMode.GET).invokeExact(recv);
187 assertEquals(2.0f, x, "success compareAndExchangeAcquire float value");
188 }
189
190 {
191 float r = (float) hs.get(TestAccessMode.COMPARE_AND_EXCHANGE_ACQUIRE).invokeExact(recv, 1.0f, 3.0f);
192 assertEquals(r, 2.0f, "failing compareAndExchangeAcquire float");
193 float x = (float) hs.get(TestAccessMode.GET).invokeExact(recv);
194 assertEquals(2.0f, x, "failing compareAndExchangeAcquire float value");
195 }
196
197 {
198 float r = (float) hs.get(TestAccessMode.COMPARE_AND_EXCHANGE_RELEASE).invokeExact(recv, 2.0f, 1.0f);
199 assertEquals(r, 2.0f, "success compareAndExchangeRelease float");
200 float x = (float) hs.get(TestAccessMode.GET).invokeExact(recv);
201 assertEquals(1.0f, x, "success compareAndExchangeRelease float value");
202 }
203
204 {
205 float r = (float) hs.get(TestAccessMode.COMPARE_AND_EXCHANGE_RELEASE).invokeExact(recv, 2.0f, 3.0f);
206 assertEquals(r, 1.0f, "failing compareAndExchangeRelease float");
207 float x = (float) hs.get(TestAccessMode.GET).invokeExact(recv);
208 assertEquals(1.0f, x, "failing compareAndExchangeRelease float value");
209 }
210
211 {
212 MethodHandle mh = hs.get(TestAccessMode.WEAK_COMPARE_AND_SET_PLAIN);
213 boolean success = false;
214 for (int c = 0; c < WEAK_ATTEMPTS && !success; c++) {
215 success = (boolean) mh.invokeExact(recv, 1.0f, 2.0f);
216 if (!success) weakDelay();
217 }
218 assertEquals(success, true, "success weakCompareAndSetPlain float");
219 float x = (float) hs.get(TestAccessMode.GET).invokeExact(recv);
220 assertEquals(2.0f, x, "success weakCompareAndSetPlain float value");
221 }
222
223 {
224 boolean success = (boolean) hs.get(TestAccessMode.WEAK_COMPARE_AND_SET_PLAIN).invokeExact(recv, 1.0f, 3.0f);
225 assertEquals(success, false, "failing weakCompareAndSetPlain float");
226 float x = (float) hs.get(TestAccessMode.GET).invokeExact(recv);
227 assertEquals(2.0f, x, "failing weakCompareAndSetPlain float value");
228 }
229
230 {
231 MethodHandle mh = hs.get(TestAccessMode.WEAK_COMPARE_AND_SET_ACQUIRE);
232 boolean success = false;
233 for (int c = 0; c < WEAK_ATTEMPTS && !success; c++) {
234 success = (boolean) mh.invokeExact(recv, 2.0f, 1.0f);
235 if (!success) weakDelay();
236 }
237 assertEquals(success, true, "success weakCompareAndSetAcquire float");
238 float x = (float) hs.get(TestAccessMode.GET).invokeExact(recv);
239 assertEquals(1.0f, x, "success weakCompareAndSetAcquire float");
240 }
241
242 {
243 boolean success = (boolean) hs.get(TestAccessMode.WEAK_COMPARE_AND_SET_ACQUIRE).invokeExact(recv, 2.0f, 3.0f);
244 assertEquals(success, false, "failing weakCompareAndSetAcquire float");
245 float x = (float) hs.get(TestAccessMode.GET).invokeExact(recv);
246 assertEquals(1.0f, x, "failing weakCompareAndSetAcquire float value");
247 }
248
249 {
250 MethodHandle mh = hs.get(TestAccessMode.WEAK_COMPARE_AND_SET_RELEASE);
251 boolean success = false;
252 for (int c = 0; c < WEAK_ATTEMPTS && !success; c++) {
253 success = (boolean) mh.invokeExact(recv, 1.0f, 2.0f);
254 if (!success) weakDelay();
255 }
256 assertEquals(success, true, "success weakCompareAndSetRelease float");
257 float x = (float) hs.get(TestAccessMode.GET).invokeExact(recv);
258 assertEquals(2.0f, x, "success weakCompareAndSetRelease float");
259 }
260
261 {
262 boolean success = (boolean) hs.get(TestAccessMode.WEAK_COMPARE_AND_SET_RELEASE).invokeExact(recv, 1.0f, 3.0f);
263 assertEquals(success, false, "failing weakCompareAndSetRelease float");
264 float x = (float) hs.get(TestAccessMode.GET).invokeExact(recv);
265 assertEquals(2.0f, x, "failing weakCompareAndSetRelease float value");
266 }
267
268 {
269 boolean success = false;
270 MethodHandle mh = hs.get(TestAccessMode.WEAK_COMPARE_AND_SET);
271 for (int c = 0; c < WEAK_ATTEMPTS && !success; c++) {
272 success = (boolean) mh.invokeExact(recv, 2.0f, 1.0f);
273 if (!success) weakDelay();
274 }
275 assertEquals(success, true, "success weakCompareAndSet float");
276 float x = (float) hs.get(TestAccessMode.GET).invokeExact(recv);
277 assertEquals(1.0f, x, "success weakCompareAndSet float");
278 }
279
280 {
281 boolean success = (boolean) hs.get(TestAccessMode.WEAK_COMPARE_AND_SET).invokeExact(recv, 2.0f, 3.0f);
282 assertEquals(success, false, "failing weakCompareAndSet float");
283 float x = (float) hs.get(TestAccessMode.GET).invokeExact(recv);
284 assertEquals(1.0f, x, "failing weakCompareAndSet float value");
285 }
286
287 // Compare set and get
288 {
289 float o = (float) hs.get(TestAccessMode.GET_AND_SET).invokeExact(recv, 2.0f);
290 assertEquals(1.0f, o, "getAndSet float");
291 float x = (float) hs.get(TestAccessMode.GET).invokeExact(recv);
292 assertEquals(2.0f, x, "getAndSet float value");
293 }
294
295 // get and add, add and get
296 {
297 hs.get(TestAccessMode.SET).invokeExact(recv, 1.0f);
298
299 float o = (float) hs.get(TestAccessMode.GET_AND_ADD).invokeExact(recv, 2.0f);
300 assertEquals(1.0f, o, "getAndAdd float");
301 float x = (float) hs.get(TestAccessMode.GET).invokeExact(recv);
302 assertEquals((float)(1.0f + 2.0f), x, "getAndAdd float value");
303 }
304
305 {
306 hs.get(TestAccessMode.SET).invokeExact(recv, 1.0f);
307
308 float o = (float) hs.get(TestAccessMode.GET_AND_ADD_ACQUIRE).invokeExact(recv, 2.0f);
309 assertEquals(1.0f, o, "getAndAddAcquire float");
310 float x = (float) hs.get(TestAccessMode.GET).invokeExact(recv);
311 assertEquals((float)(1.0f + 2.0f), x, "getAndAddAcquire float value");
312 }
313
314 {
315 hs.get(TestAccessMode.SET).invokeExact(recv, 1.0f);
316
317 float o = (float) hs.get(TestAccessMode.GET_AND_ADD_RELEASE).invokeExact(recv, 2.0f);
318 assertEquals(1.0f, o, "getAndAddRelease float");
319 float x = (float) hs.get(TestAccessMode.GET).invokeExact(recv);
320 assertEquals((float)(1.0f + 2.0f), x, "getAndAddRelease float value");
321 }
322
323 }
324
325 static void testInstanceFieldUnsupported(VarHandleTestMethodHandleAccessFloat recv, Handles hs) throws Throwable {
326
327
328 for (TestAccessMode am : testAccessModesOfType(TestAccessType.GET_AND_BITWISE)) {
329 checkUOE(am, () -> {
330 float r = (float) hs.get(am).invokeExact(recv, 1.0f);
331 });
332 }
333 }
334
335
336 static void testStaticField(Handles hs) throws Throwable {
337 // Plain
338 {
339 hs.get(TestAccessMode.SET).invokeExact(1.0f);
340 float x = (float) hs.get(TestAccessMode.GET).invokeExact();
341 assertEquals(1.0f, x, "set float value");
342 }
343
344
345 // Volatile
346 {
347 hs.get(TestAccessMode.SET_VOLATILE).invokeExact(2.0f);
348 float x = (float) hs.get(TestAccessMode.GET_VOLATILE).invokeExact();
349 assertEquals(2.0f, x, "setVolatile float value");
350 }
351
352 // Lazy
353 {
354 hs.get(TestAccessMode.SET_RELEASE).invokeExact(1.0f);
355 float x = (float) hs.get(TestAccessMode.GET_ACQUIRE).invokeExact();
356 assertEquals(1.0f, x, "setRelease float value");
357 }
358
359 // Opaque
360 {
361 hs.get(TestAccessMode.SET_OPAQUE).invokeExact(2.0f);
362 float x = (float) hs.get(TestAccessMode.GET_OPAQUE).invokeExact();
363 assertEquals(2.0f, x, "setOpaque float value");
364 }
365
366 hs.get(TestAccessMode.SET).invokeExact(1.0f);
367
368 // Compare
369 {
370 boolean r = (boolean) hs.get(TestAccessMode.COMPARE_AND_SET).invokeExact(1.0f, 2.0f);
371 assertEquals(r, true, "success compareAndSet float");
372 float x = (float) hs.get(TestAccessMode.GET).invokeExact();
373 assertEquals(2.0f, x, "success compareAndSet float value");
374 }
375
376 {
377 boolean r = (boolean) hs.get(TestAccessMode.COMPARE_AND_SET).invokeExact(1.0f, 3.0f);
378 assertEquals(r, false, "failing compareAndSet float");
379 float x = (float) hs.get(TestAccessMode.GET).invokeExact();
380 assertEquals(2.0f, x, "failing compareAndSet float value");
381 }
382
383 {
384 float r = (float) hs.get(TestAccessMode.COMPARE_AND_EXCHANGE).invokeExact(2.0f, 1.0f);
385 assertEquals(r, 2.0f, "success compareAndExchange float");
386 float x = (float) hs.get(TestAccessMode.GET).invokeExact();
387 assertEquals(1.0f, x, "success compareAndExchange float value");
388 }
389
390 {
391 float r = (float) hs.get(TestAccessMode.COMPARE_AND_EXCHANGE).invokeExact(2.0f, 3.0f);
392 assertEquals(r, 1.0f, "failing compareAndExchange float");
393 float x = (float) hs.get(TestAccessMode.GET).invokeExact();
394 assertEquals(1.0f, x, "failing compareAndExchange float value");
395 }
396
397 {
398 float r = (float) hs.get(TestAccessMode.COMPARE_AND_EXCHANGE_ACQUIRE).invokeExact(1.0f, 2.0f);
399 assertEquals(r, 1.0f, "success compareAndExchangeAcquire float");
400 float x = (float) hs.get(TestAccessMode.GET).invokeExact();
401 assertEquals(2.0f, x, "success compareAndExchangeAcquire float value");
402 }
403
404 {
405 float r = (float) hs.get(TestAccessMode.COMPARE_AND_EXCHANGE_ACQUIRE).invokeExact(1.0f, 3.0f);
406 assertEquals(r, 2.0f, "failing compareAndExchangeAcquire float");
407 float x = (float) hs.get(TestAccessMode.GET).invokeExact();
408 assertEquals(2.0f, x, "failing compareAndExchangeAcquire float value");
409 }
410
411 {
412 float r = (float) hs.get(TestAccessMode.COMPARE_AND_EXCHANGE_RELEASE).invokeExact(2.0f, 1.0f);
413 assertEquals(r, 2.0f, "success compareAndExchangeRelease float");
414 float x = (float) hs.get(TestAccessMode.GET).invokeExact();
415 assertEquals(1.0f, x, "success compareAndExchangeRelease float value");
416 }
417
418 {
419 float r = (float) hs.get(TestAccessMode.COMPARE_AND_EXCHANGE_RELEASE).invokeExact(2.0f, 3.0f);
420 assertEquals(r, 1.0f, "failing compareAndExchangeRelease float");
421 float x = (float) hs.get(TestAccessMode.GET).invokeExact();
422 assertEquals(1.0f, x, "failing compareAndExchangeRelease float value");
423 }
424
425 {
426 MethodHandle mh = hs.get(TestAccessMode.WEAK_COMPARE_AND_SET_PLAIN);
427 boolean success = false;
428 for (int c = 0; c < WEAK_ATTEMPTS && !success; c++) {
429 success = (boolean) mh.invokeExact(1.0f, 2.0f);
430 if (!success) weakDelay();
431 }
432 assertEquals(success, true, "success weakCompareAndSetPlain float");
433 float x = (float) hs.get(TestAccessMode.GET).invokeExact();
434 assertEquals(2.0f, x, "success weakCompareAndSetPlain float value");
435 }
436
437 {
438 boolean success = (boolean) hs.get(TestAccessMode.WEAK_COMPARE_AND_SET_PLAIN).invokeExact(1.0f, 3.0f);
439 assertEquals(success, false, "failing weakCompareAndSetPlain float");
440 float x = (float) hs.get(TestAccessMode.GET).invokeExact();
441 assertEquals(2.0f, x, "failing weakCompareAndSetPlain float value");
442 }
443
444 {
445 MethodHandle mh = hs.get(TestAccessMode.WEAK_COMPARE_AND_SET_ACQUIRE);
446 boolean success = false;
447 for (int c = 0; c < WEAK_ATTEMPTS && !success; c++) {
448 success = (boolean) mh.invokeExact(2.0f, 1.0f);
449 if (!success) weakDelay();
450 }
451 assertEquals(success, true, "success weakCompareAndSetAcquire float");
452 float x = (float) hs.get(TestAccessMode.GET).invokeExact();
453 assertEquals(1.0f, x, "success weakCompareAndSetAcquire float");
454 }
455
456 {
457 MethodHandle mh = hs.get(TestAccessMode.WEAK_COMPARE_AND_SET_ACQUIRE);
458 boolean success = (boolean) mh.invokeExact(2.0f, 3.0f);
459 assertEquals(success, false, "failing weakCompareAndSetAcquire float");
460 float x = (float) hs.get(TestAccessMode.GET).invokeExact();
461 assertEquals(1.0f, x, "failing weakCompareAndSetAcquire float value");
462 }
463
464 {
465 MethodHandle mh = hs.get(TestAccessMode.WEAK_COMPARE_AND_SET_RELEASE);
466 boolean success = false;
467 for (int c = 0; c < WEAK_ATTEMPTS && !success; c++) {
468 success = (boolean) mh.invokeExact(1.0f, 2.0f);
469 if (!success) weakDelay();
470 }
471 assertEquals(success, true, "success weakCompareAndSetRelease float");
472 float x = (float) hs.get(TestAccessMode.GET).invokeExact();
473 assertEquals(2.0f, x, "success weakCompareAndSetRelease float");
474 }
475
476 {
477 boolean success = (boolean) hs.get(TestAccessMode.WEAK_COMPARE_AND_SET_RELEASE).invokeExact(1.0f, 3.0f);
478 assertEquals(success, false, "failing weakCompareAndSetRelease float");
479 float x = (float) hs.get(TestAccessMode.GET).invokeExact();
480 assertEquals(2.0f, x, "failing weakCompareAndSetRelease float value");
481 }
482
483 {
484 MethodHandle mh = hs.get(TestAccessMode.WEAK_COMPARE_AND_SET);
485 boolean success = false;
486 for (int c = 0; c < WEAK_ATTEMPTS && !success; c++) {
487 success = (boolean) mh.invokeExact(2.0f, 1.0f);
488 if (!success) weakDelay();
489 }
490 assertEquals(success, true, "success weakCompareAndSet float");
491 float x = (float) hs.get(TestAccessMode.GET).invokeExact();
492 assertEquals(1.0f, x, "success weakCompareAndSet float");
493 }
494
495 {
496 boolean success = (boolean) hs.get(TestAccessMode.WEAK_COMPARE_AND_SET).invokeExact(2.0f, 3.0f);
497 assertEquals(success, false, "failing weakCompareAndSet float");
498 float x = (float) hs.get(TestAccessMode.GET).invokeExact();
499 assertEquals(1.0f, x, "failing weakCompareAndSetRe float value");
500 }
501
502 // Compare set and get
503 {
504 hs.get(TestAccessMode.SET).invokeExact(1.0f);
505
506 float o = (float) hs.get(TestAccessMode.GET_AND_SET).invokeExact(2.0f);
507 assertEquals(1.0f, o, "getAndSet float");
508 float x = (float) hs.get(TestAccessMode.GET).invokeExact();
509 assertEquals(2.0f, x, "getAndSet float value");
510 }
511
512 // Compare set and get
513 {
514 hs.get(TestAccessMode.SET).invokeExact(1.0f);
515
516 float o = (float) hs.get(TestAccessMode.GET_AND_SET_ACQUIRE).invokeExact(2.0f);
517 assertEquals(1.0f, o, "getAndSetAcquire float");
518 float x = (float) hs.get(TestAccessMode.GET).invokeExact();
519 assertEquals(2.0f, x, "getAndSetAcquire float value");
520 }
521
522 // Compare set and get
523 {
524 hs.get(TestAccessMode.SET).invokeExact(1.0f);
525
526 float o = (float) hs.get(TestAccessMode.GET_AND_SET_RELEASE).invokeExact(2.0f);
527 assertEquals(1.0f, o, "getAndSetRelease float");
528 float x = (float) hs.get(TestAccessMode.GET).invokeExact();
529 assertEquals(2.0f, x, "getAndSetRelease float value");
530 }
531
532 // get and add, add and get
533 {
534 hs.get(TestAccessMode.SET).invokeExact(1.0f);
535
536 float o = (float) hs.get(TestAccessMode.GET_AND_ADD).invokeExact(2.0f);
537 assertEquals(1.0f, o, "getAndAdd float");
538 float x = (float) hs.get(TestAccessMode.GET).invokeExact();
539 assertEquals((float)(1.0f + 2.0f), x, "getAndAdd float value");
540 }
541
542 {
543 hs.get(TestAccessMode.SET).invokeExact(1.0f);
544
545 float o = (float) hs.get(TestAccessMode.GET_AND_ADD_ACQUIRE).invokeExact(2.0f);
546 assertEquals(1.0f, o, "getAndAddAcquire float");
547 float x = (float) hs.get(TestAccessMode.GET).invokeExact();
548 assertEquals((float)(1.0f + 2.0f), x, "getAndAddAcquire float value");
549 }
550
551 {
552 hs.get(TestAccessMode.SET).invokeExact(1.0f);
553
554 float o = (float) hs.get(TestAccessMode.GET_AND_ADD_RELEASE).invokeExact(2.0f);
555 assertEquals(1.0f, o, "getAndAddRelease float");
556 float x = (float) hs.get(TestAccessMode.GET).invokeExact();
557 assertEquals((float)(1.0f + 2.0f), x, "getAndAddRelease float value");
558 }
559
560 }
561
562 static void testStaticFieldUnsupported(Handles hs) throws Throwable {
563
564
565 for (TestAccessMode am : testAccessModesOfType(TestAccessType.GET_AND_BITWISE)) {
566 checkUOE(am, () -> {
567 float r = (float) hs.get(am).invokeExact(1.0f);
568 });
569 }
570 }
571
572
573 static void testArray(Handles hs) throws Throwable {
574 float[] array = new float[10];
575
576 for (int i = 0; i < array.length; i++) {
577 // Plain
578 {
579 hs.get(TestAccessMode.SET).invokeExact(array, i, 1.0f);
580 float x = (float) hs.get(TestAccessMode.GET).invokeExact(array, i);
581 assertEquals(1.0f, x, "get float value");
582 }
583
584
585 // Volatile
586 {
587 hs.get(TestAccessMode.SET_VOLATILE).invokeExact(array, i, 2.0f);
588 float x = (float) hs.get(TestAccessMode.GET_VOLATILE).invokeExact(array, i);
589 assertEquals(2.0f, x, "setVolatile float value");
590 }
591
592 // Lazy
593 {
594 hs.get(TestAccessMode.SET_RELEASE).invokeExact(array, i, 1.0f);
595 float x = (float) hs.get(TestAccessMode.GET_ACQUIRE).invokeExact(array, i);
596 assertEquals(1.0f, x, "setRelease float value");
597 }
598
599 // Opaque
600 {
601 hs.get(TestAccessMode.SET_OPAQUE).invokeExact(array, i, 2.0f);
602 float x = (float) hs.get(TestAccessMode.GET_OPAQUE).invokeExact(array, i);
603 assertEquals(2.0f, x, "setOpaque float value");
604 }
605
606 hs.get(TestAccessMode.SET).invokeExact(array, i, 1.0f);
607
608 // Compare
609 {
610 boolean r = (boolean) hs.get(TestAccessMode.COMPARE_AND_SET).invokeExact(array, i, 1.0f, 2.0f);
611 assertEquals(r, true, "success compareAndSet float");
612 float x = (float) hs.get(TestAccessMode.GET).invokeExact(array, i);
613 assertEquals(2.0f, x, "success compareAndSet float value");
614 }
615
616 {
617 boolean r = (boolean) hs.get(TestAccessMode.COMPARE_AND_SET).invokeExact(array, i, 1.0f, 3.0f);
618 assertEquals(r, false, "failing compareAndSet float");
619 float x = (float) hs.get(TestAccessMode.GET).invokeExact(array, i);
620 assertEquals(2.0f, x, "failing compareAndSet float value");
621 }
622
623 {
624 float r = (float) hs.get(TestAccessMode.COMPARE_AND_EXCHANGE).invokeExact(array, i, 2.0f, 1.0f);
625 assertEquals(r, 2.0f, "success compareAndExchange float");
626 float x = (float) hs.get(TestAccessMode.GET).invokeExact(array, i);
627 assertEquals(1.0f, x, "success compareAndExchange float value");
628 }
629
630 {
631 float r = (float) hs.get(TestAccessMode.COMPARE_AND_EXCHANGE).invokeExact(array, i, 2.0f, 3.0f);
632 assertEquals(r, 1.0f, "failing compareAndExchange float");
633 float x = (float) hs.get(TestAccessMode.GET).invokeExact(array, i);
634 assertEquals(1.0f, x, "failing compareAndExchange float value");
635 }
636
637 {
638 float r = (float) hs.get(TestAccessMode.COMPARE_AND_EXCHANGE_ACQUIRE).invokeExact(array, i, 1.0f, 2.0f);
639 assertEquals(r, 1.0f, "success compareAndExchangeAcquire float");
640 float x = (float) hs.get(TestAccessMode.GET).invokeExact(array, i);
641 assertEquals(2.0f, x, "success compareAndExchangeAcquire float value");
642 }
643
644 {
645 float r = (float) hs.get(TestAccessMode.COMPARE_AND_EXCHANGE_ACQUIRE).invokeExact(array, i, 1.0f, 3.0f);
646 assertEquals(r, 2.0f, "failing compareAndExchangeAcquire float");
647 float x = (float) hs.get(TestAccessMode.GET).invokeExact(array, i);
648 assertEquals(2.0f, x, "failing compareAndExchangeAcquire float value");
649 }
650
651 {
652 float r = (float) hs.get(TestAccessMode.COMPARE_AND_EXCHANGE_RELEASE).invokeExact(array, i, 2.0f, 1.0f);
653 assertEquals(r, 2.0f, "success compareAndExchangeRelease float");
654 float x = (float) hs.get(TestAccessMode.GET).invokeExact(array, i);
655 assertEquals(1.0f, x, "success compareAndExchangeRelease float value");
656 }
657
658 {
659 float r = (float) hs.get(TestAccessMode.COMPARE_AND_EXCHANGE_RELEASE).invokeExact(array, i, 2.0f, 3.0f);
660 assertEquals(r, 1.0f, "failing compareAndExchangeRelease float");
661 float x = (float) hs.get(TestAccessMode.GET).invokeExact(array, i);
662 assertEquals(1.0f, x, "failing compareAndExchangeRelease float value");
663 }
664
665 {
666 MethodHandle mh = hs.get(TestAccessMode.WEAK_COMPARE_AND_SET_PLAIN);
667 boolean success = false;
668 for (int c = 0; c < WEAK_ATTEMPTS && !success; c++) {
669 success = (boolean) mh.invokeExact(array, i, 1.0f, 2.0f);
670 if (!success) weakDelay();
671 }
672 assertEquals(success, true, "success weakCompareAndSetPlain float");
673 float x = (float) hs.get(TestAccessMode.GET).invokeExact(array, i);
674 assertEquals(2.0f, x, "success weakCompareAndSetPlain float value");
675 }
676
677 {
678 boolean success = (boolean) hs.get(TestAccessMode.WEAK_COMPARE_AND_SET_PLAIN).invokeExact(array, i, 1.0f, 3.0f);
679 assertEquals(success, false, "failing weakCompareAndSetPlain float");
680 float x = (float) hs.get(TestAccessMode.GET).invokeExact(array, i);
681 assertEquals(2.0f, x, "failing weakCompareAndSetPlain float value");
682 }
683
684 {
685 MethodHandle mh = hs.get(TestAccessMode.WEAK_COMPARE_AND_SET_ACQUIRE);
686 boolean success = false;
687 for (int c = 0; c < WEAK_ATTEMPTS && !success; c++) {
688 success = (boolean) mh.invokeExact(array, i, 2.0f, 1.0f);
689 if (!success) weakDelay();
690 }
691 assertEquals(success, true, "success weakCompareAndSetAcquire float");
692 float x = (float) hs.get(TestAccessMode.GET).invokeExact(array, i);
693 assertEquals(1.0f, x, "success weakCompareAndSetAcquire float");
694 }
695
696 {
697 boolean success = (boolean) hs.get(TestAccessMode.WEAK_COMPARE_AND_SET_ACQUIRE).invokeExact(array, i, 2.0f, 3.0f);
698 assertEquals(success, false, "failing weakCompareAndSetAcquire float");
699 float x = (float) hs.get(TestAccessMode.GET).invokeExact(array, i);
700 assertEquals(1.0f, x, "failing weakCompareAndSetAcquire float value");
701 }
702
703 {
704 MethodHandle mh = hs.get(TestAccessMode.WEAK_COMPARE_AND_SET_RELEASE);
705 boolean success = false;
706 for (int c = 0; c < WEAK_ATTEMPTS && !success; c++) {
707 success = (boolean) mh.invokeExact(array, i, 1.0f, 2.0f);
708 if (!success) weakDelay();
709 }
710 assertEquals(success, true, "success weakCompareAndSetRelease float");
711 float x = (float) hs.get(TestAccessMode.GET).invokeExact(array, i);
712 assertEquals(2.0f, x, "success weakCompareAndSetRelease float");
713 }
714
715 {
716 boolean success = (boolean) hs.get(TestAccessMode.WEAK_COMPARE_AND_SET_ACQUIRE).invokeExact(array, i, 1.0f, 3.0f);
717 assertEquals(success, false, "failing weakCompareAndSetAcquire float");
718 float x = (float) hs.get(TestAccessMode.GET).invokeExact(array, i);
719 assertEquals(2.0f, x, "failing weakCompareAndSetAcquire float value");
720 }
721
722 {
723 MethodHandle mh = hs.get(TestAccessMode.WEAK_COMPARE_AND_SET);
724 boolean success = false;
725 for (int c = 0; c < WEAK_ATTEMPTS && !success; c++) {
726 success = (boolean) mh.invokeExact(array, i, 2.0f, 1.0f);
727 if (!success) weakDelay();
728 }
729 assertEquals(success, true, "success weakCompareAndSet float");
730 float x = (float) hs.get(TestAccessMode.GET).invokeExact(array, i);
731 assertEquals(1.0f, x, "success weakCompareAndSet float");
732 }
733
734 {
735 boolean success = (boolean) hs.get(TestAccessMode.WEAK_COMPARE_AND_SET).invokeExact(array, i, 2.0f, 3.0f);
736 assertEquals(success, false, "failing weakCompareAndSet float");
737 float x = (float) hs.get(TestAccessMode.GET).invokeExact(array, i);
738 assertEquals(1.0f, x, "failing weakCompareAndSet float value");
739 }
740
741 // Compare set and get
742 {
743 hs.get(TestAccessMode.SET).invokeExact(array, i, 1.0f);
744
745 float o = (float) hs.get(TestAccessMode.GET_AND_SET).invokeExact(array, i, 2.0f);
746 assertEquals(1.0f, o, "getAndSet float");
747 float x = (float) hs.get(TestAccessMode.GET).invokeExact(array, i);
748 assertEquals(2.0f, x, "getAndSet float value");
749 }
750
751 {
752 hs.get(TestAccessMode.SET).invokeExact(array, i, 1.0f);
753
754 float o = (float) hs.get(TestAccessMode.GET_AND_SET_ACQUIRE).invokeExact(array, i, 2.0f);
755 assertEquals(1.0f, o, "getAndSetAcquire float");
756 float x = (float) hs.get(TestAccessMode.GET).invokeExact(array, i);
757 assertEquals(2.0f, x, "getAndSetAcquire float value");
758 }
759
760 {
761 hs.get(TestAccessMode.SET).invokeExact(array, i, 1.0f);
762
763 float o = (float) hs.get(TestAccessMode.GET_AND_SET_RELEASE).invokeExact(array, i, 2.0f);
764 assertEquals(1.0f, o, "getAndSetRelease float");
765 float x = (float) hs.get(TestAccessMode.GET).invokeExact(array, i);
766 assertEquals(2.0f, x, "getAndSetRelease float value");
767 }
768
769 // get and add, add and get
770 {
771 hs.get(TestAccessMode.SET).invokeExact(array, i, 1.0f);
772
773 float o = (float) hs.get(TestAccessMode.GET_AND_ADD).invokeExact(array, i, 2.0f);
774 assertEquals(1.0f, o, "getAndAdd float");
775 float x = (float) hs.get(TestAccessMode.GET).invokeExact(array, i);
776 assertEquals((float)(1.0f + 2.0f), x, "getAndAdd float value");
777 }
778
779 {
780 hs.get(TestAccessMode.SET).invokeExact(array, i, 1.0f);
781
782 float o = (float) hs.get(TestAccessMode.GET_AND_ADD_ACQUIRE).invokeExact(array, i, 2.0f);
783 assertEquals(1.0f, o, "getAndAddAcquire float");
784 float x = (float) hs.get(TestAccessMode.GET).invokeExact(array, i);
785 assertEquals((float)(1.0f + 2.0f), x, "getAndAddAcquire float value");
786 }
787
788 {
789 hs.get(TestAccessMode.SET).invokeExact(array, i, 1.0f);
790
791 float o = (float) hs.get(TestAccessMode.GET_AND_ADD_RELEASE).invokeExact(array, i, 2.0f);
792 assertEquals(1.0f, o, "getAndAddRelease float");
793 float x = (float) hs.get(TestAccessMode.GET).invokeExact(array, i);
794 assertEquals((float)(1.0f + 2.0f), x, "getAndAddRelease float value");
795 }
796
797 }
798 }
799
800 static void testArrayUnsupported(Handles hs) throws Throwable {
801 float[] array = new float[10];
802
803 final int i = 0;
804
805
806 for (TestAccessMode am : testAccessModesOfType(TestAccessType.GET_AND_BITWISE)) {
807 checkUOE(am, () -> {
808 float o = (float) hs.get(am).invokeExact(array, i, 1.0f);
809 });
810 }
811 }
812
813 static void testArrayIndexOutOfBounds(Handles hs) throws Throwable {
814 float[] array = new float[10];
815
816 for (int i : new int[]{-1, Integer.MIN_VALUE, 10, 11, Integer.MAX_VALUE}) {
817 final int ci = i;
818
819 for (TestAccessMode am : testAccessModesOfType(TestAccessType.GET)) {
820 checkAIOOBE(am, () -> {
821 float x = (float) hs.get(am).invokeExact(array, ci);
822 });
823 }
824
825 for (TestAccessMode am : testAccessModesOfType(TestAccessType.SET)) {
826 checkAIOOBE(am, () -> {
827 hs.get(am).invokeExact(array, ci, 1.0f);
828 });
829 }
830
831 for (TestAccessMode am : testAccessModesOfType(TestAccessType.COMPARE_AND_SET)) {
832 checkAIOOBE(am, () -> {
833 boolean r = (boolean) hs.get(am).invokeExact(array, ci, 1.0f, 2.0f);
834 });
835 }
836
837 for (TestAccessMode am : testAccessModesOfType(TestAccessType.COMPARE_AND_EXCHANGE)) {
838 checkAIOOBE(am, () -> {
839 float r = (float) hs.get(am).invokeExact(array, ci, 2.0f, 1.0f);
840 });
841 }
842
843 for (TestAccessMode am : testAccessModesOfType(TestAccessType.GET_AND_SET)) {
844 checkAIOOBE(am, () -> {
845 float o = (float) hs.get(am).invokeExact(array, ci, 1.0f);
846 });
847 }
848
849 for (TestAccessMode am : testAccessModesOfType(TestAccessType.GET_AND_ADD)) {
850 checkAIOOBE(am, () -> {
851 float o = (float) hs.get(am).invokeExact(array, ci, 3.0f);
852 });
853 }
854
855 }
856 }
857 }
858