type-speculative-safe-integer-add.js 1.27 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21
// Copyright 2017 the V8 project authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

// Flags: --allow-natives-syntax

(function(){
    function f(x){
        return 1/(x+x);
    }

    function forgetAboutMinus0(i) {
        var x = 0;
        var y;
        for(; i > 0; --i) {
            y = f(x);
            x = -0;
        }
        return y;
    }

22
    %PrepareFunctionForOptimization(forgetAboutMinus0);
23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46
    forgetAboutMinus0(1);
    assertEquals(Infinity, forgetAboutMinus0(1));
    %OptimizeFunctionOnNextCall(forgetAboutMinus0);
    assertEquals(Infinity, forgetAboutMinus0(1));
    assertEquals(-Infinity, forgetAboutMinus0(2));
})();

(function(){
    function f(x){
        return x+x;
    }

    function NumberAdd(x,y) {
        return x + y;
    }
    NumberAdd(1,0.5);
    NumberAdd(0.5, 1);
    NumberAdd(NaN, Infinity);

    function forgetAboutNaN(b) {
        var x = b ? NaN : 1;
        return NumberAdd(f(x), 0);
    }

47
    %PrepareFunctionForOptimization(forgetAboutNaN);
48 49 50 51 52 53
    forgetAboutNaN(false);
    assertEquals(2, forgetAboutNaN(false));
    %OptimizeFunctionOnNextCall(forgetAboutNaN);
    assertEquals(2, forgetAboutNaN(false));
    assertEquals(NaN, forgetAboutNaN(true));
})();