Chromium Code Reviews| OLD | NEW |
|---|---|
| (Empty) | |
| 1 // Copyright 2010 the V8 project authors. All rights reserved. | |
|
fschneider
2013/06/11 10:22:37
2013
sra1
2013/06/12 00:12:24
Done.
| |
| 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 #include <stdlib.h> | |
| 29 | |
| 30 #include "v8.h" | |
| 31 | |
| 32 #include "hydrogen-instructions.h" | |
| 33 #include "cctest.h" | |
| 34 | |
| 35 using namespace v8::internal; | |
| 36 | |
| 37 static int32_t GetLo(const BitRange& range) { | |
| 38 int32_t lo = kMaxInt, hi = kMinInt; | |
| 39 range.ExtendRange(&lo, &hi); | |
| 40 return lo; | |
| 41 } | |
| 42 | |
| 43 | |
| 44 static int32_t GetHi(const BitRange& range) { | |
| 45 int32_t lo = kMaxInt, hi = kMinInt; | |
| 46 range.ExtendRange(&lo, &hi); | |
| 47 return hi; | |
| 48 } | |
| 49 | |
| 50 | |
| 51 static void CheckOp(int32_t a_lo, int32_t a_hi, | |
| 52 int32_t b_lo, int32_t b_hi, | |
| 53 BitRange op(BitRange, BitRange), | |
| 54 int32_t expected_lo, int32_t expected_hi) { | |
| 55 BitRange a_range; | |
| 56 BitRange b_range; | |
| 57 BitRange::SetFromRange(&a_range, a_lo, a_hi); | |
| 58 BitRange::SetFromRange(&b_range, b_lo, b_hi); | |
| 59 BitRange result = op(a_range, b_range); | |
| 60 CHECK_EQ(expected_lo, GetLo(result)); | |
| 61 CHECK_EQ(expected_hi, GetHi(result)); | |
| 62 } | |
| 63 | |
| 64 | |
| 65 TEST(BitRangeConstants) { | |
| 66 // Converting a constant to BitRange and back is lossless. | |
| 67 for (int32_t i = -100; i <= 100; i++) { | |
| 68 BitRange r; | |
| 69 BitRange::SetFromRange(&r, i, i); | |
| 70 int32_t lo = kMaxInt, hi = kMinInt; | |
| 71 r.ExtendRange(&lo, &hi); | |
| 72 CHECK_EQ(i, lo); | |
| 73 CHECK_EQ(i, hi); | |
| 74 } | |
| 75 } | |
| 76 | |
| 77 | |
| 78 TEST(BitRangeConstantOps) { | |
| 79 for (int32_t a = -10; a <= 10; a++) { | |
| 80 for (int32_t b = -10; b <= 10; b++) { | |
| 81 CheckOp(a, a, b, b, &BitRange::And, a & b, a & b); | |
| 82 CheckOp(a, a, b, b, &BitRange::Or, a | b, a | b); | |
| 83 CheckOp(a, a, b, b, &BitRange::Xor, a ^ b, a ^ b); | |
| 84 } | |
| 85 } | |
| 86 } | |
| 87 | |
| 88 | |
| 89 static void CheckConvert(int32_t lo, int32_t hi, | |
| 90 int32_t expected_lo, int32_t expected_hi) { | |
| 91 BitRange range; | |
| 92 BitRange::SetFromRange(&range, lo, hi); | |
| 93 CHECK_EQ(expected_lo, GetLo(range)); | |
| 94 CHECK_EQ(expected_hi, GetHi(range)); | |
| 95 } | |
| 96 | |
| 97 | |
| 98 TEST(BitRangeConversion) { | |
| 99 // [0, 4] --> 000xxx | |
| 100 CheckConvert(0, 4, 0, 7); | |
| 101 CheckConvert(0, 5, 0, 7); | |
| 102 CheckConvert(0, 6, 0, 7); | |
| 103 CheckConvert(0, 7, 0, 7); | |
| 104 | |
| 105 CheckConvert(1, 4, 0, 7); | |
| 106 CheckConvert(1, 5, 0, 7); | |
| 107 CheckConvert(1, 6, 0, 7); | |
| 108 CheckConvert(1, 7, 0, 7); | |
| 109 } | |
| 110 | |
| 111 | |
| 112 TEST(BitRangeMultiRange) { | |
| 113 // Multiple ranges can be unioned with multiple calls to ExtendRange. | |
| 114 // | |
| 115 // HBitWise::InferRange is a 1x1 decomposition. Each input range is | |
| 116 // 'decomposed' into 1 BitRange. It is possible to do a more precise | |
| 117 // decompostion into several BitRanges. 2 BitRanges might be the sweet-spot | |
| 118 // since it prevents change-of-sign polluting the result. | |
| 119 // | |
| 120 // E.g. [-2,3] = {xxxxxxxx} as one BitRange, but is {1111111x, 000000xx} as | |
| 121 // two. | |
| 122 // | |
| 123 // [-2,3] ^ [-1,5] = {xxxxxxxx} ^ {xxxxxxxx} = xxxxxxxx | |
| 124 // | |
| 125 // With a 2x2 decomposition, there are 4 intermediate results. | |
| 126 // | |
| 127 // [-2,3] ^ [-1,5] = {1111111x, 000000xx} ^ {11111111, 00000xxx} | |
| 128 // result11 = 1111111x ^ 11111111 = 0000000x | |
| 129 // result12 = 1111111x ^ 00000xxx = 11111xxx | |
| 130 // result21 = 000000xx ^ 11111111 = 111111xx | |
| 131 // result22 = 000000xx ^ 00000xxx = 00000xxx | |
| 132 // | |
| 133 // These can be accumulated into a range as follows: | |
| 134 // | |
| 135 // result11.ExtendRange(&lower, &upper); // 0, 1 | |
| 136 // result12.ExtendRange(&lower, &upper); // -8, 1 | |
| 137 // result21.ExtendRange(&lower, &upper); // -8, 1 | |
| 138 // result22.ExtendRange(&lower, &upper); // -8, 7 | |
| 139 // = [-8,7] | |
| 140 | |
| 141 { | |
| 142 BitRange r1(~0x000C, 0x0022); // 0010xx10 | |
| 143 BitRange r2(~0x0003, 0x0004); // 0000x1xx | |
| 144 int32_t lo = kMaxInt, hi = kMinInt; | |
| 145 r1.ExtendRange(&lo, &hi); | |
| 146 CHECK_EQ(0x22, lo); | |
| 147 CHECK_EQ(0x2E, hi); | |
| 148 | |
| 149 r2.ExtendRange(&lo, &hi); | |
| 150 CHECK_EQ(0x04, lo); | |
| 151 CHECK_EQ(0x2E, hi); | |
| 152 } | |
| 153 | |
| 154 { | |
| 155 BitRange r1(~0, -1); // 11111111 | |
| 156 BitRange r2(~1, 0); // 0000000x | |
| 157 int32_t lo = kMaxInt, hi = kMinInt; | |
| 158 r1.ExtendRange(&lo, &hi); | |
| 159 CHECK_EQ(-1, lo); | |
| 160 CHECK_EQ(-1, hi); | |
| 161 | |
| 162 r2.ExtendRange(&lo, &hi); | |
| 163 CHECK_EQ(-1, lo); | |
| 164 CHECK_EQ(1, hi); | |
| 165 } | |
| 166 } | |
| 167 | |
| 168 | |
| 169 TEST(BitRangeOps) { | |
| 170 // xxxx & 000x => 000x | |
| 171 CheckOp(kMinInt, kMaxInt, 0, 1, &BitRange::And, 0, 1); | |
| 172 | |
| 173 CheckOp(3, 7, 0, 0, &BitRange::Or, 0, 7); | |
| 174 CheckOp(4, 5, 0, 0, &BitRange::Or, 4, 5); | |
| 175 CheckOp(3, 7, 4, 4, &BitRange::Or, 4, 7); | |
| 176 CheckOp(0, 99, 4, 4, &BitRange::Or, 4, 127); | |
|
fschneider
2013/06/11 10:22:37
Add a test for xor as well, e.g. 00xx ^ 0100 => 01
| |
| 177 } | |
| OLD | NEW |