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-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 sub = stdlib.Atomics.sub; 16 var fround = stdlib.Math.fround; 17 18 function subi8(i, x) { 19 i = i | 0; 20 x = x | 0; 21 return sub(MEM8, i, x)|0; 22 } 23 24 function subi16(i, x) { 25 i = i | 0; 26 x = x | 0; 27 return sub(MEM16, i, x)|0; 28 } 29 30 function subi32(i, x) { 31 i = i | 0; 32 x = x | 0; 33 return sub(MEM32, i, x)|0; 34 } 35 36 function subu8(i, x) { 37 i = i | 0; 38 x = x >>> 0; 39 return sub(MEMU8, i, x)>>>0; 40 } 41 42 function subu16(i, x) { 43 i = i | 0; 44 x = x >>> 0; 45 return sub(MEMU16, i, x)>>>0; 46 } 47 48 function subu32(i, x) { 49 i = i | 0; 50 x = x >>> 0; 51 return sub(MEMU32, i, x)>>>0; 52 } 53 54 return { 55 subi8: subi8, 56 subi16: subi16, 57 subi32: subi32, 58 subu8: subu8, 59 subu16: subu16, 60 subu32: subu32, 61 }; 62} 63 64function clearArray() { 65 var ui8 = new Uint8Array(sab); 66 for (var i = 0; i < sab.byteLength; ++i) { 67 ui8[i] = 0; 68 } 69} 70 71function testElementType(taConstr, f, offset) { 72 clearArray(); 73 74 var ta = new taConstr(sab, offset); 75 var name = Object.prototype.toString.call(ta); 76 ta[0] = 30; 77 assertEquals(30, f(0, 10), name); 78 assertEquals(20, ta[0]); 79 assertEquals(20, f(0, 10), name); 80 assertEquals(10, ta[0]); 81 // out of bounds 82 assertEquals(0, f(-1, 0), name); 83 assertEquals(0, f(ta.length, 0), name); 84} 85 86function testElement(m, offset) { 87 testElementType(Int8Array, m.subi8, offset); 88 testElementType(Int16Array, m.subi16, offset); 89 testElementType(Int32Array, m.subi32, offset); 90 testElementType(Uint8Array, m.subu8, offset); 91 testElementType(Uint16Array, m.subu16, offset); 92 testElementType(Uint32Array, m.subu32, offset); 93} 94 95var offset = 0; 96var sab = new SharedArrayBuffer(16); 97var m1 = Module(this, {}, sab, offset); 98testElement(m1, offset); 99 100offset = 32; 101sab = new SharedArrayBuffer(64); 102var m2 = Module(this, {}, sab, offset); 103testElement(m2, offset); 104