OLD | NEW |
(Empty) | |
| 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 |
| 7 function 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 |
| 25 function 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 |
| 41 RunThreeWayTest(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 |
| 53 function 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 |
| 68 RunThreeWayTest(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 |
| 80 function 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 |
| 96 RunThreeWayTest(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 |
| 108 function LargeUnsignedLiterals() { |
| 109 "use asm"; |
| 110 function a() { |
| 111 var x = 2147483648; |
| 112 return +x; |
| 113 } |
| 114 function b() { |
| 115 var x = 2147483649; |
| 116 return +x; |
| 117 } |
| 118 function c() { |
| 119 var x = 0x80000000; |
| 120 return +x; |
| 121 } |
| 122 function d() { |
| 123 var x = 0x80000001; |
| 124 return +x; |
| 125 } |
| 126 function e() { |
| 127 var x = 0xffffffff; |
| 128 return +x; |
| 129 } |
| 130 return {a: a, b: b, c: c, d: d, e: e}; |
| 131 } |
| 132 |
| 133 RunThreeWayTest(LargeUnsignedLiterals, function(module) { |
| 134 return; // TODO(bradnelson): unsigned literals are broken! |
| 135 assertEquals(2147483648, module.a()); |
| 136 assertEquals(2147483649, module.b()); |
| 137 assertEquals(0x80000000, module.c()); |
| 138 assertEquals(0x80000001, module.d()); |
| 139 assertEquals(0xffffffff, module.e()); |
| 140 }); |
| 141 |
| 142 function ManyI32() { |
| 143 "use asm"; |
| 144 function main() { |
| 145 var a = 1 + -2 + 3 + -4 | 0; |
| 146 var b = 11 + -22 + 33 + -44 | 0; |
| 147 var c = 111 + -222 + 333 + -444 | 0; |
| 148 var d = 1111 + -2222 + 3333 + -4444 | 0; |
| 149 var e = 11111 + -22222 + 33333 + -44444 | 0; |
| 150 var f = 155555 + -266666 + 377777 + -488888 | 0; |
| 151 var g = 1155555 + -2266666 + 3377777 + -4488888 | 0; |
| 152 var h = 11155555 + -22266666 + 33377777 + -44488888 | 0; |
| 153 var i = 111155555 + -222266666 + 333377777 + -444488888 | 0; |
| 154 var j = ( |
| 155 0x1 + 0x2 + 0x4 + 0x8 + |
| 156 0x10 + 0x20 + 0x40 + 0x80 + |
| 157 0x10F + 0x200 + 0x400 + 0x800 + |
| 158 0x10E0 + 0x20F0 + 0x4000 + 0x8000 + |
| 159 0x10D00 + 0x20E00 + 0x400F0 + 0x80002 + |
| 160 0x10C000 + 0x20D000 + 0x400E00 + 0x800030 + |
| 161 0x10B0000 + 0x20C0000 + 0x400D000 + 0x8000400 + |
| 162 0x10A00000 + 0x20B00000 + 0x400C0000 + 0x80005000 |
| 163 ) | 0; |
| 164 return (a + b + c + d + e + f + g + h + i + j) | 0; |
| 165 } |
| 166 return {main: main}; |
| 167 } |
| 168 |
| 169 RunThreeWayTest(ManyI32, function(module) { |
| 170 assertEquals(-222411306, module.main()); |
| 171 }); |
| 172 |
| 173 |
| 174 function ManyF64a() { |
| 175 "use asm"; |
| 176 function main() { |
| 177 var a = +( 0.1 + -0.2 + 0.3 + -0.4); |
| 178 var b = +( 1.1 + -2.2 + 0.33 + -4.4); |
| 179 var c = +( 11.1 + -22.2 + 3.33 + -4.44); |
| 180 var d = +( 111.1 + -222.2 + 33.33 + -4.444); |
| 181 var e = +( 1111.1 + -2222.2 + 333.33 + -4.4444); |
| 182 var f = +( 15555.5 + -26666.6 + 3777.77 + -4.88888); |
| 183 var g = +( 115555.5 + -226666.6 + 33777.77 + -4.488888); |
| 184 var h = +( 1115555.5 + -2226666.6 + 333777.77 + -4.4488888); |
| 185 var i = +(11115555.5 + -22226666.6 + 3333777.77 + -4.44488888); |
| 186 return +(a + b + c + d + e + f + g + h + i); |
| 187 } |
| 188 return {main: main}; |
| 189 } |
| 190 |
| 191 RunThreeWayTest(ManyF64a, function(module) { |
| 192 assertEquals(-8640233.599945681, module.main()); |
| 193 }); |
| 194 |
| 195 function ManyF64b() { |
| 196 "use asm"; |
| 197 function k1() { return +(1.0e-25 + 3.0e-25 + 5.0e-25 + 6.0e-25 + 9.0e-25); } |
| 198 function k2() { return +(1.0e-20 + 3.0e-20 + 5.0e-20 + 6.0e-20 + 9.0e-20); } |
| 199 function k3() { return +(1.0e-15 + 3.0e-15 + 5.0e-15 + 6.0e-15 + 9.0e-15); } |
| 200 function k4() { return +(1.0e-10 + 3.0e-10 + 5.0e-10 + 6.0e-10 + 9.0e-10); } |
| 201 function k5() { return +(1.0e-5 + 3.0e-5 + 5.0e-5 + 6.0e-5 + 9.0e-5); } |
| 202 function k6() { return +(1.1e+0 + 3.1e+0 + 5.1e+0 + 6.1e+0 + 9.1e+0); } |
| 203 |
| 204 return {k1: k1, k2: k2, k3: k3, k4: k4, k5: k5, k6: k6}; |
| 205 } |
| 206 |
| 207 RunThreeWayTest(ManyF64b, function(module) { |
| 208 assertEquals(2.4e-24, module.k1()); |
| 209 assertEquals(2.4e-19, module.k2()); |
| 210 assertEquals(2.4e-14, module.k3()); |
| 211 assertEquals(2.4e-9, module.k4()); |
| 212 assertEquals(0.00024000000000000003, module.k5()); |
| 213 assertEquals(24.5, module.k6()); |
| 214 }); |
| 215 |
| 216 |
| 217 function ManyF64c() { |
| 218 "use asm"; |
| 219 function k1() { return +(1.0e+25 + 3.0e+25 + 5.0e+25 + 6.0e+25 + 9.0e+25); } |
| 220 function k2() { return +(1.0e+20 + 3.0e+20 + 5.0e+20 + 6.0e+20 + 9.0e+20); } |
| 221 function k3() { return +(1.0e+15 + 3.0e+15 + 5.0e+15 + 6.0e+15 + 9.0e+15); } |
| 222 function k4() { return +(1.0e+10 + 3.0e+10 + 5.0e+10 + 6.0e+10 + 9.0e+10); } |
| 223 function k5() { return +(1.0e+5 + 3.0e+5 + 5.0e+5 + 6.0e+5 + 9.0e+5); } |
| 224 function k6() { return +(1.4e+0 + 3.4e+0 + 5.4e+0 + 6.4e+0 + 9.4e+0); } |
| 225 |
| 226 return {k1: k1, k2: k2, k3: k3, k4: k4, k5: k5, k6: k6}; |
| 227 } |
| 228 |
| 229 RunThreeWayTest(ManyF64c, function(module) { |
| 230 assertEquals(2.4000000000000004e+26, module.k1()); |
| 231 assertEquals(2.4e+21, module.k2()); |
| 232 assertEquals(2.4e+16, module.k3()); |
| 233 assertEquals(2.4e+11, module.k4()); |
| 234 assertEquals(2.4e+6, module.k5()); |
| 235 assertEquals(26, module.k6()); |
| 236 }); |
| 237 |
| 238 function ManyF32a(stdlib) { |
| 239 "use asm"; |
| 240 var F = stdlib.Math.fround; |
| 241 |
| 242 function k1() { return F(F(1.0e-25) + F(5.0e-25) + F(6.0e-25) + F(9.0e-25)); } |
| 243 function k2() { return F(F(1.0e-20) + F(5.0e-20) + F(6.0e-20) + F(9.0e-20)); } |
| 244 function k3() { return F(F(1.0e-15) + F(5.0e-15) + F(6.0e-15) + F(9.0e-15)); } |
| 245 function k4() { return F(F(1.0e-10) + F(5.0e-10) + F(6.0e-10) + F(9.0e-10)); } |
| 246 function k5() { return F(F(1.0e-5) + F(5.0e-5) + F(6.0e-5) + F(9.0e-5)); } |
| 247 function k6() { return F(F(1.1e+0) + F(5.1e+0) + F(6.1e+0) + F(9.1e+0)); } |
| 248 |
| 249 return {k1: k1, k2: k2, k3: k3, k4: k4, k5: k5, k6: k6}; |
| 250 } |
| 251 |
| 252 if (false) { |
| 253 // TODO(bradnelson): fails validation of F32 literals somehow. |
| 254 RunThreeWayTest(ManyF32a, function(module) { |
| 255 assertEquals(2.0999999917333043e-24, module.k1()); |
| 256 assertEquals(2.099999868734112e-19, module.k2()); |
| 257 assertEquals(2.099999997029825e-14, module.k3()); |
| 258 assertEquals(2.099999951710174e-9, module.k4()); |
| 259 assertEquals(0.0002099999983329326, module.k5()); |
| 260 assertEquals(21.399999618530273, module.k6()); |
| 261 }); |
| 262 } |
OLD | NEW |