1// Copyright 2016 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: --expose-wasm 6 7function RunThreeWayTest(asmfunc, expect) { 8 var asm_source = asmfunc.toString(); 9 var nonasm_source = asm_source.replace(new RegExp("use asm"), ""); 10 var stdlib = {Math: Math}; 11 12 var js_module = eval("(" + nonasm_source + ")")(stdlib); 13 print("Testing " + asmfunc.name + " (js)..."); 14 expect(js_module); 15 16 print("Testing " + asmfunc.name + " (asm.js)..."); 17 var asm_module = asmfunc(stdlib); 18 expect(asm_module); 19 20 print("Testing " + asmfunc.name + " (wasm)..."); 21 var wasm_module = Wasm.instantiateModuleFromAsm(asm_source, stdlib); 22 expect(wasm_module); 23} 24 25function PositiveIntLiterals() { 26 "use asm"; 27 function f0() { return 0; } 28 function f1() { return 1; } 29 function f4() { return 4; } 30 function f64() { return 64; } 31 function f127() { return 127; } 32 function f128() { return 128; } 33 function f256() { return 256; } 34 function f1000() { return 1000; } 35 function f2000000() { return 2000000; } 36 function fmax() { return 2147483647; } 37 return {f0: f0, f1: f1, f4: f4, f64: f64, f127: f127, f128: f128, 38 f256: f256, f1000: f1000, f2000000, fmax: fmax}; 39} 40 41RunThreeWayTest(PositiveIntLiterals, function(module) { 42 assertEquals(0, module.f0()); 43 assertEquals(1, module.f1()); 44 assertEquals(4, module.f4()); 45 assertEquals(64, module.f64()); 46 assertEquals(128, module.f128()); 47 assertEquals(256, module.f256()); 48 assertEquals(1000, module.f1000()); 49 assertEquals(2000000, module.f2000000()); 50 assertEquals(2147483647, module.fmax()); 51}); 52 53function NegativeIntLiterals() { 54 "use asm"; 55 function f1() { return -1; } 56 function f4() { return -4; } 57 function f64() { return -64; } 58 function f127() { return -127; } 59 function f128() { return -128; } 60 function f256() { return -256; } 61 function f1000() { return -1000; } 62 function f2000000() { return -2000000; } 63 function fmin() { return -2147483648; } 64 return {f1: f1, f4: f4, f64: f64, f127: f127, f128: f128, 65 f256: f256, f1000: f1000, f2000000, fmin: fmin}; 66} 67 68RunThreeWayTest(NegativeIntLiterals, function (module) { 69 assertEquals(-1, module.f1()); 70 assertEquals(-4, module.f4()); 71 assertEquals(-64, module.f64()); 72 assertEquals(-127, module.f127()); 73 assertEquals(-128, module.f128()); 74 assertEquals(-256, module.f256()); 75 assertEquals(-1000, module.f1000()); 76 assertEquals(-2000000, module.f2000000()); 77 assertEquals(-2147483648, module.fmin()); 78}); 79 80function PositiveUnsignedLiterals() { 81 "use asm"; 82 function f0() { return 0 >>> 0; } 83 function f1() { return 1 >>> 0; } 84 function f4() { return 4 >>> 0; } 85 function f64() { return 64 >>> 0; } 86 function f127() { return 127 >>> 0; } 87 function f128() { return 128 >>> 0; } 88 function f256() { return 256 >>> 0; } 89 function f1000() { return 1000 >>> 0; } 90 function f2000000() { return 2000000 >>> 0; } 91 function fmax() { return 2147483647 >>> 0; } 92 return {f0: f0, f1: f1, f4: f4, f64: f64, f127: f127, f128: f128, 93 f256: f256, f1000: f1000, f2000000, fmax: fmax}; 94} 95 96RunThreeWayTest(PositiveUnsignedLiterals, function (module) { 97 assertEquals(0, module.f0()); 98 assertEquals(1, module.f1()); 99 assertEquals(4, module.f4()); 100 assertEquals(64, module.f64()); 101 assertEquals(128, module.f128()); 102 assertEquals(256, module.f256()); 103 assertEquals(1000, module.f1000()); 104 assertEquals(2000000, module.f2000000()); 105 assertEquals(2147483647, module.fmax()); 106}); 107 108function LargeUnsignedLiterals() { 109 "use asm"; 110 function a() { 111 var x = 2147483648; 112 return +(x >>> 0); 113 } 114 function b() { 115 var x = 2147483649; 116 return +(x >>> 0); 117 } 118 function c() { 119 var x = 0x80000000; 120 return +(x >>> 0); 121 } 122 function d() { 123 var x = 0x80000001; 124 return +(x >>> 0); 125 } 126 function e() { 127 var x = 0xffffffff; 128 return +(x >>> 0); 129 } 130 return {a: a, b: b, c: c, d: d, e: e}; 131} 132 133RunThreeWayTest(LargeUnsignedLiterals, function(module) { 134 assertEquals(2147483648, module.a()); 135 assertEquals(2147483649, module.b()); 136 assertEquals(0x80000000, module.c()); 137 assertEquals(0x80000001, module.d()); 138 assertEquals(0xffffffff, module.e()); 139}); 140 141function ManyI32() { 142 "use asm"; 143 function main() { 144 var a = 1 + -2 + 3 + -4 | 0; 145 var b = 11 + -22 + 33 + -44 | 0; 146 var c = 111 + -222 + 333 + -444 | 0; 147 var d = 1111 + -2222 + 3333 + -4444 | 0; 148 var e = 11111 + -22222 + 33333 + -44444 | 0; 149 var f = 155555 + -266666 + 377777 + -488888 | 0; 150 var g = 1155555 + -2266666 + 3377777 + -4488888 | 0; 151 var h = 11155555 + -22266666 + 33377777 + -44488888 | 0; 152 var i = 111155555 + -222266666 + 333377777 + -444488888 | 0; 153 var j = ( 154 0x1 + 0x2 + 0x4 + 0x8 + 155 0x10 + 0x20 + 0x40 + 0x80 + 156 0x10F + 0x200 + 0x400 + 0x800 + 157 0x10E0 + 0x20F0 + 0x4000 + 0x8000 + 158 0x10D00 + 0x20E00 + 0x400F0 + 0x80002 + 159 0x10C000 + 0x20D000 + 0x400E00 + 0x800030 + 160 0x10B0000 + 0x20C0000 + 0x400D000 + 0x8000400 + 161 0x10A00000 + 0x20B00000 + 0x400C0000 + 0x80005000 162 ) | 0; 163 return (a + b + c + d + e + f + g + h + i + j) | 0; 164 } 165 return {main: main}; 166} 167 168RunThreeWayTest(ManyI32, function(module) { 169 assertEquals(-222411306, module.main()); 170}); 171 172 173function ManyF64a() { 174 "use asm"; 175 function main() { 176 var a = +( 0.1 + -0.2 + 0.3 + -0.4); 177 var b = +( 1.1 + -2.2 + 0.33 + -4.4); 178 var c = +( 11.1 + -22.2 + 3.33 + -4.44); 179 var d = +( 111.1 + -222.2 + 33.33 + -4.444); 180 var e = +( 1111.1 + -2222.2 + 333.33 + -4.4444); 181 var f = +( 15555.5 + -26666.6 + 3777.77 + -4.88888); 182 var g = +( 115555.5 + -226666.6 + 33777.77 + -4.488888); 183 var h = +( 1115555.5 + -2226666.6 + 333777.77 + -4.4488888); 184 var i = +(11115555.5 + -22226666.6 + 3333777.77 + -4.44488888); 185 return +(a + b + c + d + e + f + g + h + i); 186 } 187 return {main: main}; 188} 189 190RunThreeWayTest(ManyF64a, function(module) { 191 assertEquals(-8640233.599945681, module.main()); 192}); 193 194function ManyF64b() { 195 "use asm"; 196 function k1() { return +(1.0e-25 + 3.0e-25 + 5.0e-25 + 6.0e-25 + 9.0e-25); } 197 function k2() { return +(1.0e-20 + 3.0e-20 + 5.0e-20 + 6.0e-20 + 9.0e-20); } 198 function k3() { return +(1.0e-15 + 3.0e-15 + 5.0e-15 + 6.0e-15 + 9.0e-15); } 199 function k4() { return +(1.0e-10 + 3.0e-10 + 5.0e-10 + 6.0e-10 + 9.0e-10); } 200 function k5() { return +(1.0e-5 + 3.0e-5 + 5.0e-5 + 6.0e-5 + 9.0e-5); } 201 function k6() { return +(1.1e+0 + 3.1e+0 + 5.1e+0 + 6.1e+0 + 9.1e+0); } 202 203 return {k1: k1, k2: k2, k3: k3, k4: k4, k5: k5, k6: k6}; 204} 205 206RunThreeWayTest(ManyF64b, function(module) { 207 assertEquals(2.4e-24, module.k1()); 208 assertEquals(2.4e-19, module.k2()); 209 assertEquals(2.4e-14, module.k3()); 210 assertEquals(2.4e-9, module.k4()); 211 assertEquals(0.00024000000000000003, module.k5()); 212 assertEquals(24.5, module.k6()); 213}); 214 215 216function ManyF64c() { 217 "use asm"; 218 function k1() { return +(1.0e+25 + 3.0e+25 + 5.0e+25 + 6.0e+25 + 9.0e+25); } 219 function k2() { return +(1.0e+20 + 3.0e+20 + 5.0e+20 + 6.0e+20 + 9.0e+20); } 220 function k3() { return +(1.0e+15 + 3.0e+15 + 5.0e+15 + 6.0e+15 + 9.0e+15); } 221 function k4() { return +(1.0e+10 + 3.0e+10 + 5.0e+10 + 6.0e+10 + 9.0e+10); } 222 function k5() { return +(1.0e+5 + 3.0e+5 + 5.0e+5 + 6.0e+5 + 9.0e+5); } 223 function k6() { return +(1.4e+0 + 3.4e+0 + 5.4e+0 + 6.4e+0 + 9.4e+0); } 224 225 return {k1: k1, k2: k2, k3: k3, k4: k4, k5: k5, k6: k6}; 226} 227 228RunThreeWayTest(ManyF64c, function(module) { 229 assertEquals(2.4000000000000004e+26, module.k1()); 230 assertEquals(2.4e+21, module.k2()); 231 assertEquals(2.4e+16, module.k3()); 232 assertEquals(2.4e+11, module.k4()); 233 assertEquals(2.4e+6, module.k5()); 234 assertEquals(26, module.k6()); 235}); 236 237function ManyF32a(stdlib) { 238 "use asm"; 239 var F = stdlib.Math.fround; 240 241 function k1() { return F(F(1.0e-25) + F(5.0e-25) + F(6.0e-25) + F(9.0e-25)); } 242 function k2() { return F(F(1.0e-20) + F(5.0e-20) + F(6.0e-20) + F(9.0e-20)); } 243 function k3() { return F(F(1.0e-15) + F(5.0e-15) + F(6.0e-15) + F(9.0e-15)); } 244 function k4() { return F(F(1.0e-10) + F(5.0e-10) + F(6.0e-10) + F(9.0e-10)); } 245 function k5() { return F(F(1.0e-5) + F(5.0e-5) + F(6.0e-5) + F(9.0e-5)); } 246 function k6() { return F(F(1.1e+0) + F(5.1e+0) + F(6.1e+0) + F(9.1e+0)); } 247 248 return {k1: k1, k2: k2, k3: k3, k4: k4, k5: k5, k6: k6}; 249} 250 251if (false) { 252 // TODO(bradnelson): fails validation of F32 literals somehow. 253RunThreeWayTest(ManyF32a, function(module) { 254 assertEquals(2.0999999917333043e-24, module.k1()); 255 assertEquals(2.099999868734112e-19, module.k2()); 256 assertEquals(2.099999997029825e-14, module.k3()); 257 assertEquals(2.099999951710174e-9, module.k4()); 258 assertEquals(0.0002099999983329326, module.k5()); 259 assertEquals(21.399999618530273, module.k6()); 260}); 261} 262