OLD | NEW |
(Empty) | |
| 1 // 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-atomics --harmony-sharedarraybuffer |
| 6 |
| 7 function Module(stdlib, foreign, heap) { |
| 8 "use asm"; |
| 9 var MEM8 = new stdlib.Int8Array(heap); |
| 10 var MEM16 = new stdlib.Int16Array(heap); |
| 11 var MEM32 = new stdlib.Int32Array(heap); |
| 12 var MEMU8 = new stdlib.Uint8Array(heap); |
| 13 var MEMU16 = new stdlib.Uint16Array(heap); |
| 14 var MEMU32 = new stdlib.Uint32Array(heap); |
| 15 var MEMF32 = new stdlib.Float32Array(heap); |
| 16 var MEMF64 = new stdlib.Float64Array(heap); |
| 17 var compareExchange = stdlib.Atomics.compareExchange; |
| 18 var fround = stdlib.Math.fround; |
| 19 |
| 20 function compareExchangei8(i, o, n) { |
| 21 i = i | 0; |
| 22 o = o | 0; |
| 23 n = n | 0; |
| 24 return compareExchange(MEM8, i, o, n)|0; |
| 25 } |
| 26 |
| 27 function compareExchangei16(i, o, n) { |
| 28 i = i | 0; |
| 29 o = o | 0; |
| 30 n = n | 0; |
| 31 return compareExchange(MEM16, i, o, n)|0; |
| 32 } |
| 33 |
| 34 function compareExchangei32(i, o, n) { |
| 35 i = i | 0; |
| 36 o = o | 0; |
| 37 n = n | 0; |
| 38 return compareExchange(MEM32, i, o, n)|0; |
| 39 } |
| 40 |
| 41 function compareExchangeu8(i, o, n) { |
| 42 i = i | 0; |
| 43 o = o >>> 0; |
| 44 n = n >>> 0; |
| 45 return compareExchange(MEMU8, i, o, n)>>>0; |
| 46 } |
| 47 |
| 48 function compareExchangeu16(i, o, n) { |
| 49 i = i | 0; |
| 50 o = o >>> 0; |
| 51 n = n >>> 0; |
| 52 return compareExchange(MEMU16, i, o, n)>>>0; |
| 53 } |
| 54 |
| 55 function compareExchangeu32(i, o, n) { |
| 56 i = i | 0; |
| 57 o = o >>> 0; |
| 58 n = n >>> 0; |
| 59 return compareExchange(MEMU32, i, o, n)>>>0; |
| 60 } |
| 61 |
| 62 function compareExchangef32(i, o, n) { |
| 63 i = i | 0; |
| 64 o = fround(o); |
| 65 n = fround(n); |
| 66 return fround(compareExchange(MEMF32, i, o, n)); |
| 67 } |
| 68 |
| 69 function compareExchangef64(i, o, n) { |
| 70 i = i | 0; |
| 71 o = +o; |
| 72 n = +n; |
| 73 return +compareExchange(MEMF64, i, o, n); |
| 74 } |
| 75 |
| 76 return { |
| 77 compareExchangei8: compareExchangei8, |
| 78 compareExchangei16: compareExchangei16, |
| 79 compareExchangei32: compareExchangei32, |
| 80 compareExchangeu8: compareExchangeu8, |
| 81 compareExchangeu16: compareExchangeu16, |
| 82 compareExchangeu32: compareExchangeu32, |
| 83 compareExchangef32: compareExchangef32, |
| 84 compareExchangef64: compareExchangef64 |
| 85 }; |
| 86 } |
| 87 |
| 88 var sab = new SharedArrayBuffer(16); |
| 89 var m = Module(this, {}, sab); |
| 90 |
| 91 function clearArray() { |
| 92 var ui8 = new Uint8Array(sab); |
| 93 for (var i = 0; i < sab.byteLength; ++i) { |
| 94 ui8[i] = 0; |
| 95 } |
| 96 } |
| 97 |
| 98 function testElementType(taConstr, f, oobValue) { |
| 99 clearArray(); |
| 100 |
| 101 var ta = new taConstr(sab); |
| 102 var name = Object.prototype.toString.call(ta); |
| 103 assertEquals(0, ta[0]); |
| 104 assertEquals(0, f(0, 0, 50), name); |
| 105 assertEquals(50, ta[0]); |
| 106 // Value is not equal to 0, so compareExchange won't store 100 |
| 107 assertEquals(50, f(0, 0, 100), name); |
| 108 assertEquals(50, ta[0]); |
| 109 // out of bounds |
| 110 assertEquals(oobValue, f(-1, 0, 0), name); |
| 111 assertEquals(oobValue, f(ta.length, 0, 0), name); |
| 112 } |
| 113 |
| 114 testElementType(Int8Array, m.compareExchangei8, 0); |
| 115 testElementType(Int16Array, m.compareExchangei16, 0); |
| 116 testElementType(Int32Array, m.compareExchangei32, 0); |
| 117 testElementType(Uint8Array, m.compareExchangeu8, 0); |
| 118 testElementType(Uint16Array, m.compareExchangeu16, 0); |
| 119 testElementType(Uint32Array, m.compareExchangeu32, 0); |
| 120 testElementType(Float32Array, m.compareExchangef32, NaN); |
| 121 testElementType(Float64Array, m.compareExchangef64, NaN); |
OLD | NEW |