testResizeLoadStore.js 2.3 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859
  1. //-------------------------------------------------------------------------------------------------------
  2. // Copyright (C) Microsoft. All rights reserved.
  3. // Licensed under the MIT license. See LICENSE.txt file in the project root for full license information.
  4. //-------------------------------------------------------------------------------------------------------
  5. this.WScript.LoadScriptFile("..\\UnitTestFramework\\SimdJsHelpers.js");
  6. var m = function(stdlib,imports,buffer){
  7. "use asm";
  8. //var F32=stdlib.Int32Array;
  9. var F32=stdlib.Float32Array;
  10. var I32=stdlib.Int32Array;
  11. var f32=new F32(buffer);
  12. var i32=new I32(buffer);
  13. var len=stdlib.byteLength;
  14. var i4 = stdlib.SIMD.Int32x4;
  15. var i4load = i4.load;
  16. var i4store = i4.store;
  17. var i4check = i4.check;
  18. function ch(newBuffer)
  19. {
  20. if(len(newBuffer) & 0xffffff || len(newBuffer) <= 0xffffff || len(newBuffer) > 0x80000000)
  21. return false;
  22. f32=new F32(newBuffer);
  23. i32=new I32(newBuffer);
  24. buffer=newBuffer;
  25. return true
  26. }
  27. function store(value, loc) { value=i4check(value); loc = loc|0; loc = loc<<2; i4store(i32, loc>>2, value); }
  28. function load(loc) {loc = loc|0; loc = loc<<2; return i4load(i32, loc>>2); }
  29. return { load:load
  30. ,store:store
  31. ,changeHeap:ch}
  32. };
  33. var buf1 = new ArrayBuffer(0x1000000);
  34. var f32 = new Float32Array(buf1);
  35. var i32 = new Int32Array(buf1);
  36. this['byteLength'] =
  37. Function.prototype.call.bind(Object.getOwnPropertyDescriptor(ArrayBuffer.prototype, 'byteLength').get);
  38. var o = m(this,{},buf1);
  39. o.store(SIMD.Int32x4(-5,6,17,8000),4);
  40. var ret = o.load(4);
  41. equalSimd([-5, 6, 17, 8000],ret, SIMD.Int32x4, "");
  42. o.store(SIMD.Int32x4(-5,6,17,8000), i32.length-4);
  43. var ret = o.load(i32.length-4);
  44. equalSimd([-5, 6, 17, 8000],ret, SIMD.Int32x4, "");
  45. try {o.store(SIMD.Int32x4(11, 304, -22239, 34010), f32.length); print("Wrong")} catch(err) { print("Correct");}
  46. var buf2 = new ArrayBuffer(0x2000000);
  47. print(o.changeHeap(buf2));
  48. // heap doubled, no OOB
  49. o.store(SIMD.Int32x4(511, 304, -22239, 34010), i32.length);
  50. var ret = o.load(i32.length);
  51. equalSimd([511, 304, -22239, 34010],ret, SIMD.Int32x4, "");
  52. o.store(SIMD.Int32x4(511, 304, -22239, 34010), i32.length * 2 - 4);
  53. print("PASS");