blob: 84d38504b32933c51ee8c5049d3e49975c9fd6f2 [file] [log] [blame]
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00001// Copyright 2015 the V8 project authors. All rights reserved.
2// Use of this source code is governed by a BSD-style license that can be
3// found in the LICENSE file.
4
5// Flags: --harmony-sharedarraybuffer
6
7function Module(stdlib, foreign, heap, offset) {
8 "use asm";
9 var MEM8 = new stdlib.Int8Array(heap, offset);
10 var MEM16 = new stdlib.Int16Array(heap, offset);
11 var MEM32 = new stdlib.Int32Array(heap, offset);
12 var MEMU8 = new stdlib.Uint8Array(heap, offset);
13 var MEMU16 = new stdlib.Uint16Array(heap, offset);
14 var MEMU32 = new stdlib.Uint32Array(heap, offset);
15 var compareExchange = stdlib.Atomics.compareExchange;
16 var fround = stdlib.Math.fround;
17
18 function compareExchangei8(i, o, n) {
19 i = i | 0;
20 o = o | 0;
21 n = n | 0;
22 return compareExchange(MEM8, i, o, n)|0;
23 }
24
25 function compareExchangei16(i, o, n) {
26 i = i | 0;
27 o = o | 0;
28 n = n | 0;
29 return compareExchange(MEM16, i, o, n)|0;
30 }
31
32 function compareExchangei32(i, o, n) {
33 i = i | 0;
34 o = o | 0;
35 n = n | 0;
36 return compareExchange(MEM32, i, o, n)|0;
37 }
38
39 function compareExchangeu8(i, o, n) {
40 i = i | 0;
41 o = o >>> 0;
42 n = n >>> 0;
43 return compareExchange(MEMU8, i, o, n)>>>0;
44 }
45
46 function compareExchangeu16(i, o, n) {
47 i = i | 0;
48 o = o >>> 0;
49 n = n >>> 0;
50 return compareExchange(MEMU16, i, o, n)>>>0;
51 }
52
53 function compareExchangeu32(i, o, n) {
54 i = i | 0;
55 o = o >>> 0;
56 n = n >>> 0;
57 return compareExchange(MEMU32, i, o, n)>>>0;
58 }
59
60 return {
61 compareExchangei8: compareExchangei8,
62 compareExchangei16: compareExchangei16,
63 compareExchangei32: compareExchangei32,
64 compareExchangeu8: compareExchangeu8,
65 compareExchangeu16: compareExchangeu16,
66 compareExchangeu32: compareExchangeu32,
67 };
68}
69
70function clearArray() {
71 var ui8 = new Uint8Array(sab);
72 for (var i = 0; i < sab.byteLength; ++i) {
73 ui8[i] = 0;
74 }
75}
76
77function testElementType(taConstr, f, oobValue, offset) {
78 clearArray();
79
80 var ta = new taConstr(sab, offset);
81 var name = Object.prototype.toString.call(ta);
82 assertEquals(0, ta[0]);
83 assertEquals(0, f(0, 0, 50), name);
84 assertEquals(50, ta[0]);
85 // Value is not equal to 0, so compareExchange won't store 100
86 assertEquals(50, f(0, 0, 100), name);
87 assertEquals(50, ta[0]);
88 // out of bounds
89 assertEquals(oobValue, f(-1, 0, 0), name);
90 assertEquals(oobValue, f(ta.length, 0, 0), name);
91}
92
93function testElement(m, offset) {
94 testElementType(Int8Array, m.compareExchangei8, 0, offset);
95 testElementType(Int16Array, m.compareExchangei16, 0, offset);
96 testElementType(Int32Array, m.compareExchangei32, 0, offset);
97 testElementType(Uint8Array, m.compareExchangeu8, 0, offset);
98 testElementType(Uint16Array, m.compareExchangeu16, 0, offset);
99 testElementType(Uint32Array, m.compareExchangeu32, 0, offset);
100}
101
102var offset = 0;
103var sab = new SharedArrayBuffer(16);
104var m1 = Module(this, {}, sab, offset);
105testElement(m1, offset);
106
107offset = 32;
108sab = new SharedArrayBuffer(64);
109var m2 = Module(this, {}, sab, offset);
110testElement(m2, offset);