Chromium Code Reviews| 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 TestStdlibConstants() { | |
| 8 function Module(stdlib) { | |
| 9 "use asm"; | |
| 10 | |
| 11 var StdlibInfinity = stdlib.Infinity; | |
| 12 var StdlibNaN = stdlib.NaN; | |
| 13 var StdlibMathE = stdlib.Math.E; | |
| 14 var StdlibMathLN10 = stdlib.Math.LN10; | |
| 15 var StdlibMathLN2 = stdlib.Math.LN2; | |
| 16 var StdlibMathLOG2E = stdlib.Math.LOG2E; | |
| 17 var StdlibMathLOG10E = stdlib.Math.LOG10E; | |
| 18 var StdlibMathPI = stdlib.Math.PI; | |
| 19 var StdlibMathSQRT1_2 = stdlib.Math.SQRT1_2; | |
| 20 var StdlibMathSQRT2 = stdlib.Math.SQRT2; | |
| 21 | |
| 22 function caller() { | |
| 23 if (StdlibInfinity != 1.0 / 0.0) return 0; | |
| 24 if (StdlibMathE != 2.718281828459045) return 0; | |
| 25 if (StdlibMathLN10 != 2.302585092994046) return 0; | |
| 26 if (StdlibMathLN2 != 0.6931471805599453) return 0; | |
| 27 if (StdlibMathLOG2E != 1.4426950408889634) return 0; | |
| 28 if (StdlibMathLOG10E != 0.4342944819032518) return 0; | |
| 29 if (StdlibMathPI != 3.141592653589793) return 0; | |
| 30 if (StdlibMathSQRT1_2 != 0.7071067811865476) return 0; | |
| 31 if (StdlibMathSQRT2 != 1.4142135623730951) return 0; | |
| 32 return 1; | |
| 33 } | |
| 34 | |
| 35 function nanCheck() { | |
| 36 return +StdlibNaN; | |
| 37 } | |
| 38 | |
| 39 return {caller:caller, nanCheck:nanCheck}; | |
| 40 } | |
| 41 | |
| 42 var m =Wasm.instantiateModuleFromAsm(Module.toString()); | |
| 43 assertEquals(1, m.caller()); | |
| 44 assertTrue(isNaN(m.nanCheck())); | |
| 45 })(); | |
| 46 | |
| 47 | |
| 48 (function TestStdlibFunctionsInside() { | |
| 49 function Module(stdlib) { | |
| 50 "use asm"; | |
| 51 | |
| 52 var StdlibMathCeil = stdlib.Math.ceil; | |
| 53 var StdlibMathFloor = stdlib.Math.floor; | |
| 54 var StdlibMathSqrt = stdlib.Math.sqrt; | |
| 55 var StdlibMathAbs = stdlib.Math.abs; | |
| 56 var StdlibMathMin = stdlib.Math.min; | |
| 57 var StdlibMathMax = stdlib.Math.max; | |
| 58 | |
| 59 var StdlibMathAcos = stdlib.Math.acos; | |
| 60 var StdlibMathAsin = stdlib.Math.asin; | |
| 61 var StdlibMathAtan = stdlib.Math.atan; | |
| 62 var StdlibMathCos = stdlib.Math.cos; | |
| 63 var StdlibMathSin = stdlib.Math.sin; | |
| 64 var StdlibMathTan = stdlib.Math.tan; | |
| 65 var StdlibMathExp = stdlib.Math.exp; | |
| 66 var StdlibMathLog = stdlib.Math.log; | |
| 67 | |
| 68 var StdlibMathAtan2 = stdlib.Math.atan2; | |
| 69 var StdlibMathPow = stdlib.Math.pow; | |
| 70 var StdlibMathImul = stdlib.Math.imul; | |
| 71 | |
| 72 var fround = stdlib.Math.fround; | |
| 73 | |
| 74 function deltaEqual(x, y) { | |
| 75 x = +x; | |
| 76 y = +y; | |
| 77 var t = 0.0; | |
| 78 t = x - y; | |
| 79 if (t < 0.0) { | |
| 80 t = t * -1.0; | |
| 81 } | |
| 82 return (t < 1.0e-10) | 0; | |
| 83 } | |
| 84 | |
| 85 function caller() { | |
| 86 if (!deltaEqual(StdlibMathSqrt(123.0), 11.090536506409418)) return 0; | |
| 87 if (StdlibMathSqrt(fround(256.0)) != fround(16.0)) return 0; | |
| 88 if (StdlibMathCeil(123.7) != 124.0) return 0; | |
| 89 if (StdlibMathCeil(fround(123.7)) != fround(124.0)) return 0; | |
| 90 if (StdlibMathFloor(123.7) != 123.0) return 0; | |
| 91 if (StdlibMathFloor(fround(123.7)) != fround(123.0)) return 0; | |
| 92 if (StdlibMathAbs(-123.0) != 123.0) return 0; | |
| 93 if (StdlibMathAbs(fround(-123.0)) != fround(123.0)) return 0; | |
| 94 if (StdlibMathMin(123.4, 1236.4) != 123.4) return 0; | |
| 95 if (StdlibMathMin(fround(123.4), | |
| 96 fround(1236.4)) != fround(123.4)) return 0; | |
| 97 if (StdlibMathMax(123.4, 1236.4) != 1236.4) return 0; | |
| 98 if (StdlibMathMax(fround(123.4), fround(1236.4)) | |
| 99 != fround(1236.4)) return 0; | |
| 100 | |
| 101 if (!deltaEqual(StdlibMathAcos(0.1), 1.4706289056333368)) return 0; | |
| 102 if (!deltaEqual(StdlibMathAsin(0.2), 0.2013579207903308)) return 0; | |
| 103 if (!deltaEqual(StdlibMathAtan(0.2), 0.19739555984988078)) return 0; | |
| 104 if (!deltaEqual(StdlibMathCos(0.2), 0.9800665778412416)) return 0; | |
| 105 if (!deltaEqual(StdlibMathSin(0.2), 0.19866933079506122)) return 0; | |
| 106 if (!deltaEqual(StdlibMathTan(0.2), 0.20271003550867250)) return 0; | |
| 107 if (!deltaEqual(StdlibMathExp(0.2), 1.2214027581601699)) return 0; | |
| 108 if (!deltaEqual(StdlibMathLog(0.2), -1.6094379124341003)) return 0; | |
| 109 | |
| 110 if (StdlibMathImul(6, 7) != 42) return 0; | |
| 111 if (!deltaEqual(StdlibMathAtan2(6.0, 7.0), 0.7086262721276703)) return 0; | |
| 112 if (StdlibMathPow(6.0, 7.0) != 279936.0) return 0; | |
| 113 | |
| 114 return 1; | |
| 115 } | |
| 116 | |
| 117 return {caller:caller}; | |
| 118 } | |
| 119 | |
| 120 var m = Wasm.instantiateModuleFromAsm(Module.toString()); | |
| 121 assertEquals(1, m.caller()); | |
| 122 })(); | |
| 123 | |
| 124 | |
| 125 (function TestStdlibFunctionOutside() { | |
| 126 function looseEqual(x, y, delta) { | |
| 127 if (delta === undefined) { | |
| 128 delta = 1.0e-13; | |
| 129 } | |
| 130 if (isNaN(x) && isNaN(y)) { | |
| 131 return true; | |
| 132 } | |
| 133 if (!isFinite(x) && !isFinite(y)) { | |
| 134 return true; | |
| 135 } | |
| 136 x = +x; | |
| 137 y = +y; | |
| 138 var t = 0.0; | |
| 139 t = x - y; | |
| 140 if (t < 0.0) { | |
| 141 t = t * -1.0; | |
| 142 } | |
| 143 return (t < delta) | 0; | |
| 144 } | |
| 145 | |
| 146 function plainEqual(x, y) { | |
| 147 if (isNaN(x) && isNaN(y)) { | |
| 148 return true; | |
| 149 } | |
| 150 return x === y; | |
| 151 } | |
| 152 | |
| 153 function Module(stdlib) { | |
| 154 "use asm"; | |
| 155 var ceil = stdlib.Math.ceil; | |
| 156 var floor = stdlib.Math.floor; | |
| 157 var sqrt = stdlib.Math.sqrt; | |
| 158 var abs = stdlib.Math.abs; | |
| 159 var fround = stdlib.Math.fround; | |
| 160 | |
| 161 var acos = stdlib.Math.acos; | |
| 162 var asin = stdlib.Math.asin; | |
| 163 var atan = stdlib.Math.atan; | |
| 164 var cos = stdlib.Math.cos; | |
| 165 var sin = stdlib.Math.sin; | |
| 166 var tan = stdlib.Math.tan; | |
| 167 var exp = stdlib.Math.exp; | |
| 168 var log = stdlib.Math.log; | |
| 169 | |
| 170 var atan2 = stdlib.Math.atan2; | |
| 171 var pow = stdlib.Math.pow; | |
| 172 var imul = stdlib.Math.imul; | |
| 173 var min = stdlib.Math.min; | |
| 174 var max = stdlib.Math.max; | |
| 175 | |
| 176 function ceil_f64(x) { x = +x; return +ceil(x); } | |
| 177 function ceil_f32(x) { x = fround(x); return fround(ceil(x)); } | |
| 178 | |
| 179 function floor_f64(x) { x = +x; return +floor(x); } | |
| 180 function floor_f32(x) { x = fround(x); return fround(floor(x)); } | |
| 181 | |
| 182 function sqrt_f64(x) { x = +x; return +sqrt(x); } | |
| 183 function sqrt_f32(x) { x = fround(x); return fround(sqrt(x)); } | |
| 184 | |
| 185 function abs_f64(x) { x = +x; return +abs(x); } | |
| 186 function abs_f32(x) { x = fround(x); return fround(abs(x)); } | |
| 187 function abs_i32(x) { x = x | 0; return abs(x|0) | 0; } | |
| 188 | |
| 189 function acos_f64(x) { x = +x; return +acos(x); } | |
| 190 function asin_f64(x) { x = +x; return +asin(x); } | |
| 191 function atan_f64(x) { x = +x; return +atan(x); } | |
| 192 function cos_f64(x) { x = +x; return +cos(x); } | |
| 193 function sin_f64(x) { x = +x; return +sin(x); } | |
| 194 function tan_f64(x) { x = +x; return +tan(x); } | |
| 195 function exp_f64(x) { x = +x; return +exp(x); } | |
| 196 function log_f64(x) { x = +x; return +log(x); } | |
| 197 | |
| 198 function atan2_f64(x, y) { x = +x; y = +y; return +atan2(x, y); } | |
| 199 function pow_f64(x, y) { x = +x; y = +y; return +atan2(x, y); } | |
| 200 | |
| 201 function imul_i32(x, y) { x = x | 0; y = y | 0; return imul(x, y) | 0; } | |
| 202 function imul_u32(x, y) { | |
| 203 x = x | 0; y = y | 0; return imul(x>>>0, y>>>0) | 0; } | |
| 204 | |
| 205 function fround_i32(x) { x = x | 0; return fround(x|0) | 0; } | |
| 206 function fround_u32(x) { x = x | 0; return fround(x>>>0) | 0; } | |
| 207 function fround_f32(x) { x = fround(x); return fround(x); } | |
| 208 // TODO(bradnelson): Fix and enable. | |
| 209 //function fround_f64(x) { x = +x; return fround(x); } | |
| 210 | |
| 211 function min_i32(x, y) { x = x | 0; y = y | 0; return min(x|0, y|0) | 0; } | |
| 212 // TODO(bradnelson): Fix and enable. | |
| 213 //function min_u32(x, y) { | |
| 214 // x = x | 0; y = y | 0; return min(x>>>0, y>>>0) | 0; } | |
| 215 function min_f32(x, y) { | |
| 216 x = fround(x); y = fround(y); return fround(min(x, y)); } | |
| 217 function min_f64(x, y) { x = +x; y = +y; return +min(x, y); } | |
| 218 | |
| 219 function max_i32(x, y) { x = x | 0; y = y | 0; return max(x|0, y|0) | 0; } | |
| 220 // TODO(bradnelson): Fix and enable. | |
| 221 //function max_u32(x, y) { | |
| 222 // x = x | 0; y = y | 0; return max(x>>>0, y>>>0) | 0; } | |
| 223 function max_f32(x, y) { | |
| 224 x = fround(x); y = fround(y); return fround(max(x, y)); } | |
| 225 function max_f64(x, y) { x = +x; y = +y; return +max(x, y); } | |
| 226 | |
| 227 return { | |
| 228 ceil_f64: ceil_f64, | |
| 229 ceil_f32: ceil_f32, | |
| 230 floor_f64: floor_f64, | |
| 231 floor_f32: floor_f32, | |
| 232 sqrt_f64: sqrt_f64, | |
| 233 sqrt_f32: sqrt_f32, | |
| 234 abs_f64: abs_f64, | |
| 235 abs_f32: abs_f32, | |
| 236 abs_i32: abs_i32, | |
| 237 acos_f64: acos_f64, | |
| 238 asin_f64: asin_f64, | |
| 239 atan_f64: atan_f64, | |
| 240 cos_f64: cos_f64, | |
| 241 sin_f64: sin_f64, | |
| 242 tan_f64: tan_f64, | |
| 243 exp_f64: exp_f64, | |
| 244 log_f64: log_f64, | |
| 245 imul_i32: imul_i32, | |
| 246 imul_u32: imul_u32, | |
| 247 fround_i32: fround_i32, | |
| 248 fround_u32: fround_u32, | |
| 249 fround_f32: fround_f32, | |
| 250 // TODO(bradnelson): Fix and enable. | |
| 251 // fround_f64: fround_f64, | |
| 252 min_i32: min_i32, | |
| 253 // TODO(bradnelson): Fix and enable. | |
| 254 // min_u32: min_u32, | |
| 255 min_f32: min_f32, | |
| 256 min_f64: min_f64, | |
| 257 max_i32: max_i32, | |
| 258 // TODO(bradnelson): Fix and enable. | |
| 259 // max_u32: max_u32, | |
| 260 max_f32: max_f32, | |
| 261 max_f64: max_f64, | |
| 262 }; | |
| 263 } | |
| 264 var m = Wasm.instantiateModuleFromAsm(Module.toString()); | |
| 265 var values = { | |
| 266 'i32': [ | |
| 267 0, 1, -1, 123, 456, -123, -456, | |
| 268 0x40000000, 0x7FFFFFFF, -0x80000000, | |
| 269 ], | |
| 270 'u32': [ | |
| 271 0, 1, 123, 456, | |
| 272 0x40000000, 0x7FFFFFFF, 0xFFFFFFFF, 0x80000000, | |
| 273 ], | |
| 274 'f32': [ | |
| 275 0, -0, 1, -1, 0.9, -0.9, 1.414, 0x7F, -0x80, -0x8000, -0x80000000, | |
|
titzer
2016/03/16 09:44:05
Not sure if it matters here but 0.9 is not exactly
bradnelson
2016/03/19 03:48:48
Got these "edge case" values from the simd tests.
| |
| 276 0x7FFF, 0x7FFFFFFF, Infinity, -Infinity, NaN, | |
| 277 ], | |
| 278 'f64': [ | |
| 279 0, -0, 1, -1, 0.9, -0.9, 1.414, 0x7F, -0x80, -0x8000, -0x80000000, | |
| 280 0x7FFF, 0x7FFFFFFF, Infinity, -Infinity, NaN, | |
| 281 ], | |
| 282 }; | |
| 283 var converts = { | |
| 284 'i32': function(x) { return x | 0; }, | |
|
titzer
2016/03/16 09:44:05
You can drop the single quotes from these property
bradnelson
2016/03/19 03:48:48
Done.
| |
| 285 'u32': function(x) { return x >>> 0; }, | |
| 286 'f32': function(x) { return Math.fround(x); }, | |
| 287 'f64': function(x) { return x; }, | |
| 288 }; | |
| 289 var two_args = {'atan2': true, 'pow': true, 'imul': true, | |
| 290 'min': true, 'max': true}; | |
| 291 var funcs = { | |
| 292 'ceil': ['f32', 'f64'], | |
| 293 'floor': ['f32', 'f64'], | |
| 294 'sqrt': ['f32', 'f64'], | |
| 295 'abs': ['i32', 'f32', 'f64'], | |
| 296 'acos': ['f64'], | |
| 297 'asin': ['f64'], | |
| 298 'atan': ['f64'], | |
| 299 'cos': ['f64'], | |
| 300 'sin': ['f64'], | |
| 301 'tan': ['f64'], | |
| 302 'exp': ['f64'], | |
| 303 'log': ['f64'], | |
| 304 'imul': ['i32', 'u32'], | |
| 305 // TODO(bradnelson): Fix and enable. | |
| 306 // 'fround': ['i32', 'u32', 'f32', 'f64'], | |
| 307 'fround': ['i32', 'u32', 'f32'], | |
| 308 // TODO(bradnelson): Fix and enable. | |
| 309 // 'min': ['i32', 'u32', 'f32', 'f64'], | |
| 310 'min': ['i32', 'f32', 'f64'], | |
| 311 // TODO(bradnelson): Fix and enable. | |
| 312 // 'max': ['i32', 'u32', 'f32', 'f64'], | |
| 313 'max': ['i32', 'f32', 'f64'], | |
| 314 }; | |
| 315 var equals = { | |
| 316 'f64': looseEqual, | |
| 317 'f32': plainEqual, | |
| 318 'i32': plainEqual, | |
| 319 }; | |
| 320 var per_func_equals = { | |
| 321 // exp not required to have a particular precision. | |
| 322 'exp_f64': function(x, y) { return looseEqual(x, y, 1e55); }, | |
| 323 }; | |
| 324 for (var func in funcs) { | |
| 325 var types = funcs[func]; | |
| 326 for (var i = 0; i < types.length; i++) { | |
| 327 var type = types[i]; | |
| 328 var interesting = values[type]; | |
| 329 for (var j = 0; j < interesting.length; j++) { | |
| 330 for (var k = 0; k < interesting.length; k++) { | |
| 331 var val0 = interesting[j]; | |
| 332 var val1 = interesting[k]; | |
| 333 var input0 = converts[type](val0); | |
| 334 var input1 = converts[type](val1); | |
| 335 var name = func + '_' + type; | |
| 336 if (two_args[func]) { | |
| 337 var expected = converts[type](Math[func](input0, input1)); | |
| 338 var actual = m[name](input0, input1); | |
| 339 } else { | |
| 340 var expected = converts[type](Math[func](input0, input1)); | |
| 341 var actual = m[name](input0, input1); | |
| 342 } | |
| 343 var compare = per_func_equals[name]; | |
| 344 if (compare === undefined) { | |
| 345 compare = equals[type]; | |
| 346 } | |
| 347 if (!compare(expected, actual)) { | |
| 348 print(expected + ' !== ' + actual + ' for ' + name + | |
| 349 ' with input ' + input0 + ' ' + input1); | |
| 350 assertTrue(false); | |
| 351 } | |
| 352 } | |
| 353 } | |
| 354 } | |
| 355 } | |
| 356 })(); | |
| OLD | NEW |