Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 1 | // Copyright 2008 the V8 project authors. All rights reserved. |
| 2 | // Redistribution and use in source and binary forms, with or without |
| 3 | // modification, are permitted provided that the following conditions are |
| 4 | // met: |
| 5 | // |
| 6 | // * Redistributions of source code must retain the above copyright |
| 7 | // notice, this list of conditions and the following disclaimer. |
| 8 | // * Redistributions in binary form must reproduce the above |
| 9 | // copyright notice, this list of conditions and the following |
| 10 | // disclaimer in the documentation and/or other materials provided |
| 11 | // with the distribution. |
| 12 | // * Neither the name of Google Inc. nor the names of its |
| 13 | // contributors may be used to endorse or promote products derived |
| 14 | // from this software without specific prior written permission. |
| 15 | // |
| 16 | // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS |
| 17 | // "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT |
| 18 | // LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR |
| 19 | // A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT |
| 20 | // OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, |
| 21 | // SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT |
| 22 | // LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, |
| 23 | // DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY |
| 24 | // THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT |
| 25 | // (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE |
| 26 | // OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
| 27 | |
| 28 | // Ensure that we can correctly change the sign of the most negative smi. |
| 29 | |
Steve Block | 3ce2e20 | 2009-11-05 08:53:23 +0000 | [diff] [blame] | 30 | // Possible Smi ranges. |
| 31 | var ranges = [{min: -1073741824, max: 1073741823, bits: 31}, |
| 32 | {min: -2147483648, max: 2147483647, bits: 32}]; |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 33 | |
Steve Block | 3ce2e20 | 2009-11-05 08:53:23 +0000 | [diff] [blame] | 34 | for (var i = 0; i < ranges.length; i++) { |
| 35 | var range = ranges[i]; |
| 36 | var min_smi = range.min; |
| 37 | var max_smi = range.max; |
| 38 | var bits = range.bits; |
| 39 | var name = bits + "-bit"; |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 40 | |
Steve Block | 3ce2e20 | 2009-11-05 08:53:23 +0000 | [diff] [blame] | 41 | var result = max_smi + 1; |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 42 | |
Steve Block | 3ce2e20 | 2009-11-05 08:53:23 +0000 | [diff] [blame] | 43 | // Min smi as literal |
| 44 | assertEquals(result, eval(min_smi + " * -1"), name + "-litconmult"); |
| 45 | assertEquals(result, eval(min_smi + " / -1"), name + "-litcondiv"); |
| 46 | assertEquals(result, eval("-(" + min_smi + ")"), name + "-litneg"); |
| 47 | assertEquals(result, eval("0 - (" + min_smi + ")")), name + "-conlitsub"; |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 48 | |
Steve Block | 3ce2e20 | 2009-11-05 08:53:23 +0000 | [diff] [blame] | 49 | // As variable: |
| 50 | assertEquals(result, min_smi * -1, name + "-varconmult"); |
| 51 | assertEquals(result, min_smi / -1, name + "-varcondiv"); |
| 52 | assertEquals(result, -min_smi, name + "-varneg"); |
| 53 | assertEquals(result, 0 - min_smi, name + "-convarsub"); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 54 | |
Steve Block | 3ce2e20 | 2009-11-05 08:53:23 +0000 | [diff] [blame] | 55 | // Only variables: |
| 56 | var zero = 0; |
| 57 | var minus_one = -1; |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 58 | |
Steve Block | 3ce2e20 | 2009-11-05 08:53:23 +0000 | [diff] [blame] | 59 | assertEquals(result, min_smi * minus_one, name + "-varvarmult"); |
| 60 | assertEquals(result, min_smi / minus_one, name + "-varvardiv"); |
| 61 | assertEquals(result, zero - min_smi, name + "-varvarsub"); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 62 | |
Steve Block | 3ce2e20 | 2009-11-05 08:53:23 +0000 | [diff] [blame] | 63 | // Constants as variables |
| 64 | assertEquals(result, eval(min_smi + " * minus_one"), name + "-litvarmult"); |
| 65 | assertEquals(result, eval(min_smi + " / minus_one"), name + "-litvarmdiv"); |
| 66 | assertEquals(result, eval("0 - (" + min_smi + ")"), name + "-varlitsub"); |
| 67 | |
| 68 | var half_min_smi = -(1 << (bits >> 1)); |
| 69 | var half_max_smi = 1 << ((bits - 1) >> 1); |
| 70 | |
| 71 | assertEquals(max_smi + 1, -half_min_smi * half_max_smi, name + "-half1"); |
| 72 | assertEquals(max_smi + 1, half_min_smi * -half_max_smi, name + "-half2"); |
| 73 | assertEquals(max_smi + 1, half_max_smi * -half_min_smi, name + "-half3"); |
| 74 | assertEquals(max_smi + 1, -half_max_smi * half_min_smi, name + "-half4"); |
| 75 | } |