1.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000 1.2 +++ b/js/src/jit-test/tests/ion/testFloat32.js Wed Dec 31 06:09:35 2014 +0100 1.3 @@ -0,0 +1,361 @@ 1.4 +// Fuzz tests 1.5 +(function(){ 1.6 + // 1.7 + (function(){ 1.8 + var g = {}; 1.9 + x = new Float32Array() 1.10 + Function('g', "g.o = x[1]")(g); 1.11 + })(); 1.12 + // 1.13 + (function() { 1.14 + var g = new Float32Array(16); 1.15 + var h = new Float64Array(16); 1.16 + var farrays = [ g, h ]; 1.17 + for (aridx = 0; aridx < farrays.length; ++aridx) { 1.18 + ar = farrays[aridx]; 1.19 + !(ar[ar.length-2] == (NaN / Infinity)[ar.length-2]) 1.20 + } 1.21 + })(); 1.22 + // 1.23 + (function () { 1.24 + var v = new Float32Array(32); 1.25 + for (var i = 0; i < v.length; ++i) 1.26 + v[i] = i; 1.27 + var t = (false ); 1.28 + for (var i = 0; i < i .length; ++i) 1.29 + t += v[i]; 1.30 + })(); 1.31 + // 1.32 + (function() { 1.33 + if (typeof ParallelArray !== "undefined") 1.34 + ParallelArray([1606], Math.fround) 1.35 + })(); 1.36 + // 1.37 + (function() { 1.38 + x = y = {}; 1.39 + z = new Float32Array(6) 1.40 + for (c in this) { 1.41 + Array.prototype.unshift.call(x, new ArrayBuffer()) 1.42 + } 1.43 + Array.prototype.sort.call(x, (function (j) { 1.44 + y.s = z[2] 1.45 + })) 1.46 + })(); 1.47 + // 1.48 +})(); 1.49 +// 1.50 +// ION TESTS 1.51 +// 1.52 +// The assertFloat32 function is deactivated in --ion-eager mode, as the first time, the function Math.fround 1.53 +// would be guarded against modifications (typeguard on Math and then on fround). In this case, Math.fround is 1.54 +// not inlined and the compiler will consider the return value to be a double, not a float32, making the 1.55 +// assertions fail. Note that as assertFloat32 is declared unsafe for fuzzing, this can't happen in fuzzed code. 1.56 +// 1.57 +// To be able to test it, we still need ion compilation though. A nice solution is to manually lower the ion usecount. 1.58 +setJitCompilerOption("ion.usecount.trigger", 50); 1.59 + 1.60 +function test(f) { 1.61 + f32[0] = .5; 1.62 + for(var n = 110; n; n--) 1.63 + f(); 1.64 +} 1.65 + 1.66 +var f32 = new Float32Array(2); 1.67 +var f64 = new Float64Array(2); 1.68 + 1.69 +function acceptAdd() { 1.70 + var use = f32[0] + 1; 1.71 + assertFloat32(use, true); 1.72 + f32[0] = use; 1.73 +} 1.74 +test(acceptAdd); 1.75 + 1.76 +function acceptAddSeveral() { 1.77 + var sum1 = f32[0] + 0.5; 1.78 + var sum2 = f32[0] + 0.5; 1.79 + f32[0] = sum1; 1.80 + f32[0] = sum2; 1.81 + assertFloat32(sum1, true); 1.82 + assertFloat32(sum2, true); 1.83 +} 1.84 +test(acceptAddSeveral); 1.85 + 1.86 +function acceptAddVar() { 1.87 + var x = f32[0] + 1; 1.88 + f32[0] = x; 1.89 + f32[1] = x; 1.90 + assertFloat32(x, true); 1.91 +} 1.92 +test(acceptAddVar); 1.93 + 1.94 +function refuseAddCst() { 1.95 + var x = f32[0] + 1234567890; // this constant can't be precisely represented as a float32 1.96 + f32[0] = x; 1.97 + assertFloat32(x, false); 1.98 +} 1.99 +test(refuseAddCst); 1.100 + 1.101 +function refuseAddVar() { 1.102 + var x = f32[0] + 1; 1.103 + f32[0] = x; 1.104 + f32[1] = x; 1.105 + f64[1] = x; // non consumer 1.106 + assertFloat32(x, false); 1.107 +} 1.108 +test(refuseAddVar); 1.109 + 1.110 +function refuseAddStore64() { 1.111 + var x = f32[0] + 1; 1.112 + f64[0] = x; // non consumer 1.113 + f32[0] = f64[0]; 1.114 + assertFloat32(x, false); 1.115 +} 1.116 +test(refuseAddStore64); 1.117 + 1.118 +function refuseAddStoreObj() { 1.119 + var o = {} 1.120 + var x = f32[0] + 1; 1.121 + o.x = x; // non consumer 1.122 + f32[0] = o['x']; 1.123 + assertFloat32(x, false); 1.124 +} 1.125 +test(refuseAddStoreObj); 1.126 + 1.127 +function refuseAddSeveral() { 1.128 + var sum = (f32[0] + 2) - 1; // second addition is not a consumer 1.129 + f32[0] = sum; 1.130 + assertFloat32(sum, false); 1.131 +} 1.132 +test(refuseAddSeveral); 1.133 + 1.134 +function refuseAddFunctionCall() { 1.135 + function plusOne(x) { return Math.cos(x+1)*13.37; } 1.136 + var res = plusOne(f32[0]); // func call is not a consumer 1.137 + f32[0] = res; 1.138 + assertFloat32(res, false); 1.139 +} 1.140 +test(refuseAddFunctionCall); 1.141 + 1.142 +function acceptSqrt() { 1.143 + var res = Math.sqrt(f32[0]); 1.144 + assertFloat32(res, true); 1.145 + f32[0] = res; 1.146 +} 1.147 +test(acceptSqrt); 1.148 + 1.149 +function refuseSqrt() { 1.150 + var res = Math.sqrt(f32[0]); 1.151 + assertFloat32(res, false); 1.152 + f32[0] = res + 1; 1.153 +} 1.154 +test(refuseSqrt); 1.155 + 1.156 +function acceptAbs() { 1.157 + var res = Math.abs(f32[0]); 1.158 + assertFloat32(res, true); 1.159 + f32[0] = res; 1.160 +} 1.161 +test(acceptAbs); 1.162 + 1.163 +function refuseAbs() { 1.164 + var res = Math.abs(f32[0]); 1.165 + assertFloat32(res, false); 1.166 + f64[0] = res + 1; 1.167 +} 1.168 +test(refuseAbs); 1.169 + 1.170 +function refuseTrigo() { 1.171 + var res = Math.cos(f32[0]); 1.172 + f32[0] = res; 1.173 + assertFloat32(res, false); 1.174 + 1.175 + var res = Math.sin(f32[0]); 1.176 + f32[0] = res; 1.177 + assertFloat32(res, false); 1.178 + 1.179 + var res = Math.tan(f32[0]); 1.180 + f32[0] = res; 1.181 + assertFloat32(res, false); 1.182 + 1.183 + var res = Math.acos(f32[0]); 1.184 + f32[0] = res; 1.185 + assertFloat32(res, false); 1.186 + 1.187 + var res = Math.asin(f32[0]); 1.188 + f32[0] = res; 1.189 + assertFloat32(res, false); 1.190 + 1.191 + res = Math.atan(f32[0]); 1.192 + f32[0] = res; 1.193 + assertFloat32(res, false); 1.194 +} 1.195 +test(refuseTrigo); 1.196 + 1.197 +function refuseMath() { 1.198 + var res = Math.log10(f32[0]); 1.199 + f32[0] = res; 1.200 + assertFloat32(res, false); 1.201 + 1.202 + res = Math.log2(f32[0]); 1.203 + f32[0] = res; 1.204 + assertFloat32(res, false); 1.205 + 1.206 + res = Math.log1p(f32[0]); 1.207 + f32[0] = res; 1.208 + assertFloat32(res, false); 1.209 + 1.210 + res = Math.expm1(f32[0]); 1.211 + f32[0] = res; 1.212 + assertFloat32(res, false); 1.213 + 1.214 + res = Math.cosh(f32[0]); 1.215 + f32[0] = res; 1.216 + assertFloat32(res, false); 1.217 + 1.218 + res = Math.sinh(f32[0]); 1.219 + f32[0] = res; 1.220 + assertFloat32(res, false); 1.221 + 1.222 + res = Math.tanh(f32[0]); 1.223 + f32[0] = res; 1.224 + assertFloat32(res, false); 1.225 + 1.226 + res = Math.acosh(f32[0]); 1.227 + f32[0] = res; 1.228 + assertFloat32(res, false); 1.229 + 1.230 + res = Math.asinh(f32[0]); 1.231 + f32[0] = res; 1.232 + assertFloat32(res, false); 1.233 + 1.234 + res = Math.atanh(f32[0]); 1.235 + f32[0] = res; 1.236 + assertFloat32(res, false); 1.237 + 1.238 + res = Math.cbrt(f32[0]); 1.239 + f32[0] = res; 1.240 + assertFloat32(res, false); 1.241 + 1.242 + res = Math.sign(f32[0]); 1.243 + f32[0] = res; 1.244 + assertFloat32(res, false); 1.245 + 1.246 + res = Math.trunc(f32[0]); 1.247 + f32[0] = res; 1.248 + assertFloat32(res, false); 1.249 +} 1.250 +test(refuseMath); 1.251 + 1.252 +function refuseLoop() { 1.253 + var res = f32[0], 1.254 + n = 10; 1.255 + while (n--) { 1.256 + res = res + 1; // this loop is equivalent to several additions => second addition is not a consumer 1.257 + assertFloat32(res, false); 1.258 + } 1.259 + assertFloat32(res, false); 1.260 + f32[0] = res; 1.261 +} 1.262 +test(refuseLoop); 1.263 + 1.264 +function acceptLoop() { 1.265 + var res = f32[0], 1.266 + n = 10; 1.267 + while (n--) { 1.268 + var sum = res + 1; 1.269 + res = Math.fround(sum); 1.270 + assertFloat32(sum, true); 1.271 + } 1.272 + assertFloat32(res, true); 1.273 + f32[0] = res; 1.274 +} 1.275 +test(acceptLoop); 1.276 + 1.277 +function alternateCond(n) { 1.278 + var x = f32[0]; 1.279 + if (n > 0) { 1.280 + var s1 = x + 1; 1.281 + f32[0] = s1; 1.282 + assertFloat32(s1, true); 1.283 + } else { 1.284 + var s2 = x + 1; 1.285 + f64[0] = s2; // non consumer 1.286 + assertFloat32(s2, false); 1.287 + } 1.288 +} 1.289 +(function() { 1.290 + f32[0] = 0; 1.291 + for (var n = 110; n; n--) { 1.292 + alternateCond(n % 2); 1.293 + } 1.294 +})(); 1.295 + 1.296 +function phiTest(n) { 1.297 + var x = (f32[0]); 1.298 + var y = n; 1.299 + if (n > 0) { 1.300 + x = x + 2; 1.301 + assertFloat32(x, true); 1.302 + } else { 1.303 + if (n < -10) { 1.304 + x = Math.fround(Math.sqrt(y)); 1.305 + assertFloat32(x, true); 1.306 + } else { 1.307 + x = x - 1; 1.308 + assertFloat32(x, true); 1.309 + } 1.310 + } 1.311 + assertFloat32(x, true); 1.312 + f32[0] = x; 1.313 +} 1.314 +(function() { 1.315 + f32[0] = 0; 1.316 + for (var n = 100; n; n--) { 1.317 + phiTest( ((n % 3) - 1) * 15 ); 1.318 + } 1.319 +})(); 1.320 + 1.321 +function mixedPhiTest(n) { 1.322 + var x = (f32[0]); 1.323 + var y = n; 1.324 + if (n > 0) { 1.325 + x = x + 2; // non consumer because of (1) 1.326 + assertFloat32(x, false); 1.327 + } else { 1.328 + if (n < -10) { 1.329 + x = Math.fround(Math.sqrt(y)); // new producer 1.330 + assertFloat32(x, true); 1.331 + } else { 1.332 + x = x - 1; // non consumer because of (1) 1.333 + assertFloat32(x, false); 1.334 + } 1.335 + } 1.336 + assertFloat32(x, false); 1.337 + x = x + 1; // (1) non consumer 1.338 + f32[0] = x; 1.339 +} 1.340 +(function() { 1.341 + f32[0] = 0; 1.342 + for (var n = 100; n; n--) { 1.343 + mixedPhiTest( ((n % 3) - 1) * 15 ); 1.344 + } 1.345 +})(); 1.346 + 1.347 +function phiTest2(n) { 1.348 + var x = f32[0]; 1.349 + while (n >= 0) { 1.350 + x = Math.fround(Math.fround(x) + 1); 1.351 + assertFloat32(x, true); 1.352 + if (n < 10) { 1.353 + x = f32[0] + 1; 1.354 + assertFloat32(x, true); 1.355 + } 1.356 + n = n - 1; 1.357 + } 1.358 +} 1.359 +(function(){ 1.360 + f32[0] = 0; 1.361 + for (var n = 100; n > 10; n--) { 1.362 + phiTest2(n); 1.363 + } 1.364 +})();