12faa5f1271587cda765f26bcf2951065300a01ffElliott Hughes/* 22faa5f1271587cda765f26bcf2951065300a01ffElliott Hughes * Copyright (C) 2006 The Android Open Source Project 32faa5f1271587cda765f26bcf2951065300a01ffElliott Hughes * 42faa5f1271587cda765f26bcf2951065300a01ffElliott Hughes * Licensed under the Apache License, Version 2.0 (the "License"); 52faa5f1271587cda765f26bcf2951065300a01ffElliott Hughes * you may not use this file except in compliance with the License. 62faa5f1271587cda765f26bcf2951065300a01ffElliott Hughes * You may obtain a copy of the License at 72faa5f1271587cda765f26bcf2951065300a01ffElliott Hughes * 82faa5f1271587cda765f26bcf2951065300a01ffElliott Hughes * http://www.apache.org/licenses/LICENSE-2.0 92faa5f1271587cda765f26bcf2951065300a01ffElliott Hughes * 102faa5f1271587cda765f26bcf2951065300a01ffElliott Hughes * Unless required by applicable law or agreed to in writing, software 112faa5f1271587cda765f26bcf2951065300a01ffElliott Hughes * distributed under the License is distributed on an "AS IS" BASIS, 122faa5f1271587cda765f26bcf2951065300a01ffElliott Hughes * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 132faa5f1271587cda765f26bcf2951065300a01ffElliott Hughes * See the License for the specific language governing permissions and 142faa5f1271587cda765f26bcf2951065300a01ffElliott Hughes * limitations under the License. 152faa5f1271587cda765f26bcf2951065300a01ffElliott Hughes */ 165d1ac920fdaef5d4ec8f66bb734488cd9660b024jeffhao 175d1ac920fdaef5d4ec8f66bb734488cd9660b024jeffhao/** 185d1ac920fdaef5d4ec8f66bb734488cd9660b024jeffhao * Test arithmetic operations. 195d1ac920fdaef5d4ec8f66bb734488cd9660b024jeffhao */ 205d1ac920fdaef5d4ec8f66bb734488cd9660b024jeffhaopublic class IntMath { 215d1ac920fdaef5d4ec8f66bb734488cd9660b024jeffhao 225d1ac920fdaef5d4ec8f66bb734488cd9660b024jeffhao static void shiftTest1() { 235d1ac920fdaef5d4ec8f66bb734488cd9660b024jeffhao System.out.println("IntMath.shiftTest1"); 245d1ac920fdaef5d4ec8f66bb734488cd9660b024jeffhao 255d1ac920fdaef5d4ec8f66bb734488cd9660b024jeffhao final int[] mBytes = { 265d1ac920fdaef5d4ec8f66bb734488cd9660b024jeffhao 0x11, 0x22, 0x33, 0x44, 0x88, 0x99, 0xaa, 0xbb 275d1ac920fdaef5d4ec8f66bb734488cd9660b024jeffhao }; 285d1ac920fdaef5d4ec8f66bb734488cd9660b024jeffhao long l; 295d1ac920fdaef5d4ec8f66bb734488cd9660b024jeffhao int i1, i2; 305d1ac920fdaef5d4ec8f66bb734488cd9660b024jeffhao 315d1ac920fdaef5d4ec8f66bb734488cd9660b024jeffhao i1 = mBytes[0] | mBytes[1] << 8 | mBytes[2] << 16 | mBytes[3] << 24; 325d1ac920fdaef5d4ec8f66bb734488cd9660b024jeffhao i2 = mBytes[4] | mBytes[5] << 8 | mBytes[6] << 16 | mBytes[7] << 24; 335d1ac920fdaef5d4ec8f66bb734488cd9660b024jeffhao l = i1 | ((long)i2 << 32); 345d1ac920fdaef5d4ec8f66bb734488cd9660b024jeffhao 35795d78f4d04c8d007bf2bdf2ed4131379bcf19c7jeffhao Main.assertTrue(i1 == 0x44332211); 36795d78f4d04c8d007bf2bdf2ed4131379bcf19c7jeffhao Main.assertTrue(i2 == 0xbbaa9988); 37795d78f4d04c8d007bf2bdf2ed4131379bcf19c7jeffhao Main.assertTrue(l == 0xbbaa998844332211L); 385d1ac920fdaef5d4ec8f66bb734488cd9660b024jeffhao 395d1ac920fdaef5d4ec8f66bb734488cd9660b024jeffhao l = (long)mBytes[0] 405d1ac920fdaef5d4ec8f66bb734488cd9660b024jeffhao | (long)mBytes[1] << 8 415d1ac920fdaef5d4ec8f66bb734488cd9660b024jeffhao | (long)mBytes[2] << 16 425d1ac920fdaef5d4ec8f66bb734488cd9660b024jeffhao | (long)mBytes[3] << 24 435d1ac920fdaef5d4ec8f66bb734488cd9660b024jeffhao | (long)mBytes[4] << 32 445d1ac920fdaef5d4ec8f66bb734488cd9660b024jeffhao | (long)mBytes[5] << 40 455d1ac920fdaef5d4ec8f66bb734488cd9660b024jeffhao | (long)mBytes[6] << 48 465d1ac920fdaef5d4ec8f66bb734488cd9660b024jeffhao | (long)mBytes[7] << 56; 475d1ac920fdaef5d4ec8f66bb734488cd9660b024jeffhao 48795d78f4d04c8d007bf2bdf2ed4131379bcf19c7jeffhao Main.assertTrue(l == 0xbbaa998844332211L); 495d1ac920fdaef5d4ec8f66bb734488cd9660b024jeffhao } 505d1ac920fdaef5d4ec8f66bb734488cd9660b024jeffhao 515d1ac920fdaef5d4ec8f66bb734488cd9660b024jeffhao static void shiftTest2() { 525d1ac920fdaef5d4ec8f66bb734488cd9660b024jeffhao System.out.println("IntMath.shiftTest2"); 535d1ac920fdaef5d4ec8f66bb734488cd9660b024jeffhao 545d1ac920fdaef5d4ec8f66bb734488cd9660b024jeffhao long a = 0x11; 555d1ac920fdaef5d4ec8f66bb734488cd9660b024jeffhao long b = 0x22; 565d1ac920fdaef5d4ec8f66bb734488cd9660b024jeffhao long c = 0x33; 575d1ac920fdaef5d4ec8f66bb734488cd9660b024jeffhao long d = 0x44; 585d1ac920fdaef5d4ec8f66bb734488cd9660b024jeffhao long e = 0x55; 595d1ac920fdaef5d4ec8f66bb734488cd9660b024jeffhao long f = 0x66; 605d1ac920fdaef5d4ec8f66bb734488cd9660b024jeffhao long g = 0x77; 615d1ac920fdaef5d4ec8f66bb734488cd9660b024jeffhao long h = 0x88; 625d1ac920fdaef5d4ec8f66bb734488cd9660b024jeffhao 635d1ac920fdaef5d4ec8f66bb734488cd9660b024jeffhao long result = ((a << 56) | (b << 48) | (c << 40) | (d << 32) | 645d1ac920fdaef5d4ec8f66bb734488cd9660b024jeffhao (e << 24) | (f << 16) | (g << 8) | h); 655d1ac920fdaef5d4ec8f66bb734488cd9660b024jeffhao 66795d78f4d04c8d007bf2bdf2ed4131379bcf19c7jeffhao Main.assertTrue(result == 0x1122334455667788L); 675d1ac920fdaef5d4ec8f66bb734488cd9660b024jeffhao } 685d1ac920fdaef5d4ec8f66bb734488cd9660b024jeffhao 695d1ac920fdaef5d4ec8f66bb734488cd9660b024jeffhao static void unsignedShiftTest() { 705d1ac920fdaef5d4ec8f66bb734488cd9660b024jeffhao System.out.println("IntMath.unsignedShiftTest"); 715d1ac920fdaef5d4ec8f66bb734488cd9660b024jeffhao 725d1ac920fdaef5d4ec8f66bb734488cd9660b024jeffhao byte b = -4; 735d1ac920fdaef5d4ec8f66bb734488cd9660b024jeffhao short s = -4; 745d1ac920fdaef5d4ec8f66bb734488cd9660b024jeffhao char c = 0xfffc; 755d1ac920fdaef5d4ec8f66bb734488cd9660b024jeffhao int i = -4; 765d1ac920fdaef5d4ec8f66bb734488cd9660b024jeffhao 775d1ac920fdaef5d4ec8f66bb734488cd9660b024jeffhao b >>>= 4; 785d1ac920fdaef5d4ec8f66bb734488cd9660b024jeffhao s >>>= 4; 795d1ac920fdaef5d4ec8f66bb734488cd9660b024jeffhao c >>>= 4; 805d1ac920fdaef5d4ec8f66bb734488cd9660b024jeffhao i >>>= 4; 815d1ac920fdaef5d4ec8f66bb734488cd9660b024jeffhao 82795d78f4d04c8d007bf2bdf2ed4131379bcf19c7jeffhao Main.assertTrue((int) b == -1); 83795d78f4d04c8d007bf2bdf2ed4131379bcf19c7jeffhao Main.assertTrue((int) s == -1); 84795d78f4d04c8d007bf2bdf2ed4131379bcf19c7jeffhao Main.assertTrue((int) c == 0x0fff); 85795d78f4d04c8d007bf2bdf2ed4131379bcf19c7jeffhao Main.assertTrue(i == 268435455); 865d1ac920fdaef5d4ec8f66bb734488cd9660b024jeffhao } 875d1ac920fdaef5d4ec8f66bb734488cd9660b024jeffhao 8855b16ceff1068cce61c86f2c9ca011a97c869bd4Ian Rogers static void shiftTest3(int thirtyTwo) { 8955b16ceff1068cce61c86f2c9ca011a97c869bd4Ian Rogers System.out.println("IntMath.shiftTest3"); 9055b16ceff1068cce61c86f2c9ca011a97c869bd4Ian Rogers 9155b16ceff1068cce61c86f2c9ca011a97c869bd4Ian Rogers int one = thirtyTwo / 32; 9255b16ceff1068cce61c86f2c9ca011a97c869bd4Ian Rogers int sixteen = thirtyTwo / 2; 9355b16ceff1068cce61c86f2c9ca011a97c869bd4Ian Rogers int thirtyThree = thirtyTwo + 1; 9455b16ceff1068cce61c86f2c9ca011a97c869bd4Ian Rogers int sixtyFour = thirtyTwo * 2; 9555b16ceff1068cce61c86f2c9ca011a97c869bd4Ian Rogers 9655b16ceff1068cce61c86f2c9ca011a97c869bd4Ian Rogers Main.assertTrue(1 << thirtyTwo == 1); 9755b16ceff1068cce61c86f2c9ca011a97c869bd4Ian Rogers Main.assertTrue((1 << sixteen) << sixteen == 0); 9855b16ceff1068cce61c86f2c9ca011a97c869bd4Ian Rogers Main.assertTrue(1 << thirtyThree == 2); 9955b16ceff1068cce61c86f2c9ca011a97c869bd4Ian Rogers Main.assertTrue(1 << -one == -2147483648); 10055b16ceff1068cce61c86f2c9ca011a97c869bd4Ian Rogers Main.assertTrue(1 << -thirtyTwo == 1); 10155b16ceff1068cce61c86f2c9ca011a97c869bd4Ian Rogers Main.assertTrue(1 << -thirtyThree == -2147483648); 10255b16ceff1068cce61c86f2c9ca011a97c869bd4Ian Rogers Main.assertTrue(1 << thirtyThree == 2); 10355b16ceff1068cce61c86f2c9ca011a97c869bd4Ian Rogers 10455b16ceff1068cce61c86f2c9ca011a97c869bd4Ian Rogers Main.assertTrue(1 >> thirtyTwo == 1); 10555b16ceff1068cce61c86f2c9ca011a97c869bd4Ian Rogers Main.assertTrue((1 >> sixteen) >> sixteen == 0); 10655b16ceff1068cce61c86f2c9ca011a97c869bd4Ian Rogers Main.assertTrue(1 >> thirtyThree == 0); 10755b16ceff1068cce61c86f2c9ca011a97c869bd4Ian Rogers Main.assertTrue(1 >> -one == 0); 10855b16ceff1068cce61c86f2c9ca011a97c869bd4Ian Rogers Main.assertTrue(1 >> -thirtyTwo == 1); 10955b16ceff1068cce61c86f2c9ca011a97c869bd4Ian Rogers Main.assertTrue(1 >> -thirtyThree == 0); 11055b16ceff1068cce61c86f2c9ca011a97c869bd4Ian Rogers Main.assertTrue(-4 >> thirtyThree == -2); 11155b16ceff1068cce61c86f2c9ca011a97c869bd4Ian Rogers 11255b16ceff1068cce61c86f2c9ca011a97c869bd4Ian Rogers Main.assertTrue(1 >>> thirtyTwo == 1); 11355b16ceff1068cce61c86f2c9ca011a97c869bd4Ian Rogers Main.assertTrue((1 >>> sixteen) >>> sixteen == 0); 11455b16ceff1068cce61c86f2c9ca011a97c869bd4Ian Rogers Main.assertTrue(1 >>> thirtyThree == 0); 11555b16ceff1068cce61c86f2c9ca011a97c869bd4Ian Rogers Main.assertTrue(1 >>> -one == 0); 11655b16ceff1068cce61c86f2c9ca011a97c869bd4Ian Rogers Main.assertTrue(1 >>> -thirtyTwo == 1); 11755b16ceff1068cce61c86f2c9ca011a97c869bd4Ian Rogers Main.assertTrue(1 >>> -thirtyThree == 0); 11855b16ceff1068cce61c86f2c9ca011a97c869bd4Ian Rogers Main.assertTrue(-4 >>> thirtyThree == 2147483646); 11955b16ceff1068cce61c86f2c9ca011a97c869bd4Ian Rogers } 12055b16ceff1068cce61c86f2c9ca011a97c869bd4Ian Rogers 1215d1ac920fdaef5d4ec8f66bb734488cd9660b024jeffhao static void convTest() { 1225d1ac920fdaef5d4ec8f66bb734488cd9660b024jeffhao System.out.println("IntMath.convTest"); 1235d1ac920fdaef5d4ec8f66bb734488cd9660b024jeffhao 1245d1ac920fdaef5d4ec8f66bb734488cd9660b024jeffhao float f; 1255d1ac920fdaef5d4ec8f66bb734488cd9660b024jeffhao double d; 1265d1ac920fdaef5d4ec8f66bb734488cd9660b024jeffhao int i; 1275d1ac920fdaef5d4ec8f66bb734488cd9660b024jeffhao long l; 1285d1ac920fdaef5d4ec8f66bb734488cd9660b024jeffhao 1295d1ac920fdaef5d4ec8f66bb734488cd9660b024jeffhao /* int --> long */ 1305d1ac920fdaef5d4ec8f66bb734488cd9660b024jeffhao i = 7654; 1315d1ac920fdaef5d4ec8f66bb734488cd9660b024jeffhao l = (long) i; 132795d78f4d04c8d007bf2bdf2ed4131379bcf19c7jeffhao Main.assertTrue(l == 7654L); 1335d1ac920fdaef5d4ec8f66bb734488cd9660b024jeffhao 1345d1ac920fdaef5d4ec8f66bb734488cd9660b024jeffhao i = -7654; 1355d1ac920fdaef5d4ec8f66bb734488cd9660b024jeffhao l = (long) i; 136795d78f4d04c8d007bf2bdf2ed4131379bcf19c7jeffhao Main.assertTrue(l == -7654L); 1375d1ac920fdaef5d4ec8f66bb734488cd9660b024jeffhao 1385d1ac920fdaef5d4ec8f66bb734488cd9660b024jeffhao /* long --> int (with truncation) */ 1395d1ac920fdaef5d4ec8f66bb734488cd9660b024jeffhao l = 5678956789L; 1405d1ac920fdaef5d4ec8f66bb734488cd9660b024jeffhao i = (int) l; 141795d78f4d04c8d007bf2bdf2ed4131379bcf19c7jeffhao Main.assertTrue(i == 1383989493); 1425d1ac920fdaef5d4ec8f66bb734488cd9660b024jeffhao 1435d1ac920fdaef5d4ec8f66bb734488cd9660b024jeffhao l = -5678956789L; 1445d1ac920fdaef5d4ec8f66bb734488cd9660b024jeffhao i = (int) l; 145795d78f4d04c8d007bf2bdf2ed4131379bcf19c7jeffhao Main.assertTrue(i == -1383989493); 1465d1ac920fdaef5d4ec8f66bb734488cd9660b024jeffhao } 1475d1ac920fdaef5d4ec8f66bb734488cd9660b024jeffhao 1485d1ac920fdaef5d4ec8f66bb734488cd9660b024jeffhao static void charSubTest() { 1495d1ac920fdaef5d4ec8f66bb734488cd9660b024jeffhao System.out.println("IntMath.charSubTest"); 1505d1ac920fdaef5d4ec8f66bb734488cd9660b024jeffhao 1515d1ac920fdaef5d4ec8f66bb734488cd9660b024jeffhao char char1 = 0x00e9; 1525d1ac920fdaef5d4ec8f66bb734488cd9660b024jeffhao char char2 = 0xffff; 1535d1ac920fdaef5d4ec8f66bb734488cd9660b024jeffhao int i; 1545d1ac920fdaef5d4ec8f66bb734488cd9660b024jeffhao 1555d1ac920fdaef5d4ec8f66bb734488cd9660b024jeffhao /* chars are unsigned-expanded to ints before subtraction */ 1565d1ac920fdaef5d4ec8f66bb734488cd9660b024jeffhao i = char1 - char2; 157795d78f4d04c8d007bf2bdf2ed4131379bcf19c7jeffhao Main.assertTrue(i == 0xffff00ea); 1585d1ac920fdaef5d4ec8f66bb734488cd9660b024jeffhao } 1595d1ac920fdaef5d4ec8f66bb734488cd9660b024jeffhao 1605d1ac920fdaef5d4ec8f66bb734488cd9660b024jeffhao /* 1615d1ac920fdaef5d4ec8f66bb734488cd9660b024jeffhao * We pass in the arguments and return the results so the compiler 1625d1ac920fdaef5d4ec8f66bb734488cd9660b024jeffhao * doesn't do the math for us. (x=70000, y=-3) 1635d1ac920fdaef5d4ec8f66bb734488cd9660b024jeffhao */ 1645d1ac920fdaef5d4ec8f66bb734488cd9660b024jeffhao static int[] intOperTest(int x, int y) { 1655d1ac920fdaef5d4ec8f66bb734488cd9660b024jeffhao System.out.println("IntMath.intOperTest"); 1665d1ac920fdaef5d4ec8f66bb734488cd9660b024jeffhao 1675d1ac920fdaef5d4ec8f66bb734488cd9660b024jeffhao int[] results = new int[10]; 1685d1ac920fdaef5d4ec8f66bb734488cd9660b024jeffhao 1695d1ac920fdaef5d4ec8f66bb734488cd9660b024jeffhao /* this seems to generate "op-int" instructions */ 1705d1ac920fdaef5d4ec8f66bb734488cd9660b024jeffhao results[0] = x + y; 1715d1ac920fdaef5d4ec8f66bb734488cd9660b024jeffhao results[1] = x - y; 1725d1ac920fdaef5d4ec8f66bb734488cd9660b024jeffhao results[2] = x * y; 1735d1ac920fdaef5d4ec8f66bb734488cd9660b024jeffhao results[3] = x * x; 1745d1ac920fdaef5d4ec8f66bb734488cd9660b024jeffhao results[4] = x / y; 1755d1ac920fdaef5d4ec8f66bb734488cd9660b024jeffhao results[5] = x % -y; 1765d1ac920fdaef5d4ec8f66bb734488cd9660b024jeffhao results[6] = x & y; 1775d1ac920fdaef5d4ec8f66bb734488cd9660b024jeffhao results[7] = x | y; 1785d1ac920fdaef5d4ec8f66bb734488cd9660b024jeffhao results[8] = x ^ y; 1795d1ac920fdaef5d4ec8f66bb734488cd9660b024jeffhao 1805d1ac920fdaef5d4ec8f66bb734488cd9660b024jeffhao /* this seems to generate "op-int/2addr" instructions */ 1815d1ac920fdaef5d4ec8f66bb734488cd9660b024jeffhao results[9] = x + ((((((((x + y) - y) * y) / y) % y) & y) | y) ^ y); 1825d1ac920fdaef5d4ec8f66bb734488cd9660b024jeffhao 1835d1ac920fdaef5d4ec8f66bb734488cd9660b024jeffhao return results; 1845d1ac920fdaef5d4ec8f66bb734488cd9660b024jeffhao } 1855d1ac920fdaef5d4ec8f66bb734488cd9660b024jeffhao static void intOperCheck(int[] results) { 1865d1ac920fdaef5d4ec8f66bb734488cd9660b024jeffhao System.out.println("IntMath.intOperCheck"); 1875d1ac920fdaef5d4ec8f66bb734488cd9660b024jeffhao 1885d1ac920fdaef5d4ec8f66bb734488cd9660b024jeffhao /* check this edge case while we're here (div-int/2addr) */ 1895d1ac920fdaef5d4ec8f66bb734488cd9660b024jeffhao int minInt = -2147483648; 1905d1ac920fdaef5d4ec8f66bb734488cd9660b024jeffhao int negOne = -results[5]; 1915d1ac920fdaef5d4ec8f66bb734488cd9660b024jeffhao int plusOne = 1; 1925d1ac920fdaef5d4ec8f66bb734488cd9660b024jeffhao int result = (((minInt + plusOne) - plusOne) / negOne) / negOne; 193795d78f4d04c8d007bf2bdf2ed4131379bcf19c7jeffhao Main.assertTrue(result == minInt); 194795d78f4d04c8d007bf2bdf2ed4131379bcf19c7jeffhao 195795d78f4d04c8d007bf2bdf2ed4131379bcf19c7jeffhao Main.assertTrue(results[0] == 69997); 196795d78f4d04c8d007bf2bdf2ed4131379bcf19c7jeffhao Main.assertTrue(results[1] == 70003); 197795d78f4d04c8d007bf2bdf2ed4131379bcf19c7jeffhao Main.assertTrue(results[2] == -210000); 198795d78f4d04c8d007bf2bdf2ed4131379bcf19c7jeffhao Main.assertTrue(results[3] == 605032704); // overflow / truncate 199795d78f4d04c8d007bf2bdf2ed4131379bcf19c7jeffhao Main.assertTrue(results[4] == -23333); 200795d78f4d04c8d007bf2bdf2ed4131379bcf19c7jeffhao Main.assertTrue(results[5] == 1); 201795d78f4d04c8d007bf2bdf2ed4131379bcf19c7jeffhao Main.assertTrue(results[6] == 70000); 202795d78f4d04c8d007bf2bdf2ed4131379bcf19c7jeffhao Main.assertTrue(results[7] == -3); 203795d78f4d04c8d007bf2bdf2ed4131379bcf19c7jeffhao Main.assertTrue(results[8] == -70003); 204795d78f4d04c8d007bf2bdf2ed4131379bcf19c7jeffhao Main.assertTrue(results[9] == 70000); 2055d1ac920fdaef5d4ec8f66bb734488cd9660b024jeffhao } 2065d1ac920fdaef5d4ec8f66bb734488cd9660b024jeffhao 2075d1ac920fdaef5d4ec8f66bb734488cd9660b024jeffhao /* 2085d1ac920fdaef5d4ec8f66bb734488cd9660b024jeffhao * More operations, this time with 16-bit constants. (x=77777) 2095d1ac920fdaef5d4ec8f66bb734488cd9660b024jeffhao */ 2105d1ac920fdaef5d4ec8f66bb734488cd9660b024jeffhao static int[] lit16Test(int x) { 2115d1ac920fdaef5d4ec8f66bb734488cd9660b024jeffhao System.out.println("IntMath.lit16Test"); 2125d1ac920fdaef5d4ec8f66bb734488cd9660b024jeffhao 2135d1ac920fdaef5d4ec8f66bb734488cd9660b024jeffhao int[] results = new int[8]; 2145d1ac920fdaef5d4ec8f66bb734488cd9660b024jeffhao 2155d1ac920fdaef5d4ec8f66bb734488cd9660b024jeffhao /* try to generate op-int/lit16" instructions */ 2165d1ac920fdaef5d4ec8f66bb734488cd9660b024jeffhao results[0] = x + 1000; 2175d1ac920fdaef5d4ec8f66bb734488cd9660b024jeffhao results[1] = 1000 - x; 2185d1ac920fdaef5d4ec8f66bb734488cd9660b024jeffhao results[2] = x * 1000; 2195d1ac920fdaef5d4ec8f66bb734488cd9660b024jeffhao results[3] = x / 1000; 2205d1ac920fdaef5d4ec8f66bb734488cd9660b024jeffhao results[4] = x % 1000; 2215d1ac920fdaef5d4ec8f66bb734488cd9660b024jeffhao results[5] = x & 1000; 2225d1ac920fdaef5d4ec8f66bb734488cd9660b024jeffhao results[6] = x | -1000; 2235d1ac920fdaef5d4ec8f66bb734488cd9660b024jeffhao results[7] = x ^ -1000; 2245d1ac920fdaef5d4ec8f66bb734488cd9660b024jeffhao return results; 2255d1ac920fdaef5d4ec8f66bb734488cd9660b024jeffhao } 2265d1ac920fdaef5d4ec8f66bb734488cd9660b024jeffhao static void lit16Check(int[] results) { 227795d78f4d04c8d007bf2bdf2ed4131379bcf19c7jeffhao Main.assertTrue(results[0] == 78777); 228795d78f4d04c8d007bf2bdf2ed4131379bcf19c7jeffhao Main.assertTrue(results[1] == -76777); 229795d78f4d04c8d007bf2bdf2ed4131379bcf19c7jeffhao Main.assertTrue(results[2] == 77777000); 230795d78f4d04c8d007bf2bdf2ed4131379bcf19c7jeffhao Main.assertTrue(results[3] == 77); 231795d78f4d04c8d007bf2bdf2ed4131379bcf19c7jeffhao Main.assertTrue(results[4] == 777); 232795d78f4d04c8d007bf2bdf2ed4131379bcf19c7jeffhao Main.assertTrue(results[5] == 960); 233795d78f4d04c8d007bf2bdf2ed4131379bcf19c7jeffhao Main.assertTrue(results[6] == -39); 234795d78f4d04c8d007bf2bdf2ed4131379bcf19c7jeffhao Main.assertTrue(results[7] == -76855); 2355d1ac920fdaef5d4ec8f66bb734488cd9660b024jeffhao } 2365d1ac920fdaef5d4ec8f66bb734488cd9660b024jeffhao 2375d1ac920fdaef5d4ec8f66bb734488cd9660b024jeffhao /* 2385d1ac920fdaef5d4ec8f66bb734488cd9660b024jeffhao * More operations, this time with 8-bit constants. (x=-55555) 2395d1ac920fdaef5d4ec8f66bb734488cd9660b024jeffhao */ 2405d1ac920fdaef5d4ec8f66bb734488cd9660b024jeffhao static int[] lit8Test(int x) { 2415d1ac920fdaef5d4ec8f66bb734488cd9660b024jeffhao System.out.println("IntMath.lit8Test"); 2425d1ac920fdaef5d4ec8f66bb734488cd9660b024jeffhao 2435d1ac920fdaef5d4ec8f66bb734488cd9660b024jeffhao int[] results = new int[8]; 2445d1ac920fdaef5d4ec8f66bb734488cd9660b024jeffhao 2455d1ac920fdaef5d4ec8f66bb734488cd9660b024jeffhao /* try to generate op-int/lit8" instructions */ 2465d1ac920fdaef5d4ec8f66bb734488cd9660b024jeffhao results[0] = x + 10; 2475d1ac920fdaef5d4ec8f66bb734488cd9660b024jeffhao results[1] = 10 - x; 2485d1ac920fdaef5d4ec8f66bb734488cd9660b024jeffhao results[2] = x * 10; 2495d1ac920fdaef5d4ec8f66bb734488cd9660b024jeffhao results[3] = x / 10; 2505d1ac920fdaef5d4ec8f66bb734488cd9660b024jeffhao results[4] = x % 10; 2515d1ac920fdaef5d4ec8f66bb734488cd9660b024jeffhao results[5] = x & 10; 2525d1ac920fdaef5d4ec8f66bb734488cd9660b024jeffhao results[6] = x | -10; 2535d1ac920fdaef5d4ec8f66bb734488cd9660b024jeffhao results[7] = x ^ -10; 2545d1ac920fdaef5d4ec8f66bb734488cd9660b024jeffhao return results; 2555d1ac920fdaef5d4ec8f66bb734488cd9660b024jeffhao } 2565d1ac920fdaef5d4ec8f66bb734488cd9660b024jeffhao static void lit8Check(int[] results) { 2575d1ac920fdaef5d4ec8f66bb734488cd9660b024jeffhao //for (int i = 0; i < results.length; i++) 2585d1ac920fdaef5d4ec8f66bb734488cd9660b024jeffhao // System.out.println(" " + i + ": " + results[i]); 2595d1ac920fdaef5d4ec8f66bb734488cd9660b024jeffhao 2605d1ac920fdaef5d4ec8f66bb734488cd9660b024jeffhao /* check this edge case while we're here (div-int/lit8) */ 2615d1ac920fdaef5d4ec8f66bb734488cd9660b024jeffhao int minInt = -2147483648; 2625d1ac920fdaef5d4ec8f66bb734488cd9660b024jeffhao int result = minInt / -1; 263795d78f4d04c8d007bf2bdf2ed4131379bcf19c7jeffhao Main.assertTrue(result == minInt); 264795d78f4d04c8d007bf2bdf2ed4131379bcf19c7jeffhao 265795d78f4d04c8d007bf2bdf2ed4131379bcf19c7jeffhao Main.assertTrue(results[0] == -55545); 266795d78f4d04c8d007bf2bdf2ed4131379bcf19c7jeffhao Main.assertTrue(results[1] == 55565); 267795d78f4d04c8d007bf2bdf2ed4131379bcf19c7jeffhao Main.assertTrue(results[2] == -555550); 268795d78f4d04c8d007bf2bdf2ed4131379bcf19c7jeffhao Main.assertTrue(results[3] == -5555); 269795d78f4d04c8d007bf2bdf2ed4131379bcf19c7jeffhao Main.assertTrue(results[4] == -5); 270795d78f4d04c8d007bf2bdf2ed4131379bcf19c7jeffhao Main.assertTrue(results[5] == 8); 271795d78f4d04c8d007bf2bdf2ed4131379bcf19c7jeffhao Main.assertTrue(results[6] == -1); 272795d78f4d04c8d007bf2bdf2ed4131379bcf19c7jeffhao Main.assertTrue(results[7] == 55563); 2735d1ac920fdaef5d4ec8f66bb734488cd9660b024jeffhao } 2745d1ac920fdaef5d4ec8f66bb734488cd9660b024jeffhao 275f3aac973bb944885a1a4779ba04a97faa88b7ed0buzbee /* 276f3aac973bb944885a1a4779ba04a97faa88b7ed0buzbee * Make sure special-cased literal division matches 277f3aac973bb944885a1a4779ba04a97faa88b7ed0buzbee * normal division. 278f3aac973bb944885a1a4779ba04a97faa88b7ed0buzbee */ 279f3aac973bb944885a1a4779ba04a97faa88b7ed0buzbee static void divLiteralTestBody(int start, int count) { 280f3aac973bb944885a1a4779ba04a97faa88b7ed0buzbee int normal = 0; 281f3aac973bb944885a1a4779ba04a97faa88b7ed0buzbee int special = 0; 282f3aac973bb944885a1a4779ba04a97faa88b7ed0buzbee for (int i = 0; i < count; i++) { 283f3aac973bb944885a1a4779ba04a97faa88b7ed0buzbee for (int j = 3; j < 16; j++) { 284f3aac973bb944885a1a4779ba04a97faa88b7ed0buzbee switch(j) { 285f3aac973bb944885a1a4779ba04a97faa88b7ed0buzbee case 3: 286f3aac973bb944885a1a4779ba04a97faa88b7ed0buzbee normal = (start+i) / j; 287f3aac973bb944885a1a4779ba04a97faa88b7ed0buzbee special = (start+i) / 3; 288f3aac973bb944885a1a4779ba04a97faa88b7ed0buzbee break; 289f3aac973bb944885a1a4779ba04a97faa88b7ed0buzbee case 4: 290f3aac973bb944885a1a4779ba04a97faa88b7ed0buzbee normal = (start+i) / j; 291f3aac973bb944885a1a4779ba04a97faa88b7ed0buzbee special = (start+i) / 4; 292f3aac973bb944885a1a4779ba04a97faa88b7ed0buzbee break; 293f3aac973bb944885a1a4779ba04a97faa88b7ed0buzbee case 5: 294f3aac973bb944885a1a4779ba04a97faa88b7ed0buzbee normal = (start+i) / j; 295f3aac973bb944885a1a4779ba04a97faa88b7ed0buzbee special = (start+i) / 5; 296f3aac973bb944885a1a4779ba04a97faa88b7ed0buzbee break; 297f3aac973bb944885a1a4779ba04a97faa88b7ed0buzbee case 6: 298f3aac973bb944885a1a4779ba04a97faa88b7ed0buzbee normal = (start+i) / j; 299f3aac973bb944885a1a4779ba04a97faa88b7ed0buzbee special = (start+i) / 6; 300f3aac973bb944885a1a4779ba04a97faa88b7ed0buzbee break; 301f3aac973bb944885a1a4779ba04a97faa88b7ed0buzbee case 7: 302f3aac973bb944885a1a4779ba04a97faa88b7ed0buzbee normal = (start+i) / j; 303f3aac973bb944885a1a4779ba04a97faa88b7ed0buzbee special = (start+i) / 7; 304f3aac973bb944885a1a4779ba04a97faa88b7ed0buzbee break; 305f3aac973bb944885a1a4779ba04a97faa88b7ed0buzbee case 8: 306f3aac973bb944885a1a4779ba04a97faa88b7ed0buzbee normal = (start+i) / j; 307f3aac973bb944885a1a4779ba04a97faa88b7ed0buzbee special = (start+i) / 8; 308f3aac973bb944885a1a4779ba04a97faa88b7ed0buzbee break; 309f3aac973bb944885a1a4779ba04a97faa88b7ed0buzbee case 9: 310f3aac973bb944885a1a4779ba04a97faa88b7ed0buzbee normal = (start+i) / j; 311f3aac973bb944885a1a4779ba04a97faa88b7ed0buzbee special = (start+i) / 9; 312f3aac973bb944885a1a4779ba04a97faa88b7ed0buzbee break; 313f3aac973bb944885a1a4779ba04a97faa88b7ed0buzbee case 10: 314f3aac973bb944885a1a4779ba04a97faa88b7ed0buzbee normal = (start+i) / j; 315f3aac973bb944885a1a4779ba04a97faa88b7ed0buzbee special = (start+i) / 10; 316f3aac973bb944885a1a4779ba04a97faa88b7ed0buzbee break; 317f3aac973bb944885a1a4779ba04a97faa88b7ed0buzbee case 11: 318f3aac973bb944885a1a4779ba04a97faa88b7ed0buzbee normal = (start+i) / j; 319f3aac973bb944885a1a4779ba04a97faa88b7ed0buzbee special = (start+i) / 11; 320f3aac973bb944885a1a4779ba04a97faa88b7ed0buzbee break; 321f3aac973bb944885a1a4779ba04a97faa88b7ed0buzbee case 12: 322f3aac973bb944885a1a4779ba04a97faa88b7ed0buzbee normal = (start+i) / j; 323f3aac973bb944885a1a4779ba04a97faa88b7ed0buzbee special = (start+i) / 12; 324f3aac973bb944885a1a4779ba04a97faa88b7ed0buzbee break; 325f3aac973bb944885a1a4779ba04a97faa88b7ed0buzbee case 13: 326f3aac973bb944885a1a4779ba04a97faa88b7ed0buzbee normal = (start+i) / j; 327f3aac973bb944885a1a4779ba04a97faa88b7ed0buzbee special = (start+i) / 13; 328f3aac973bb944885a1a4779ba04a97faa88b7ed0buzbee break; 329f3aac973bb944885a1a4779ba04a97faa88b7ed0buzbee case 14: 330f3aac973bb944885a1a4779ba04a97faa88b7ed0buzbee normal = (start+i) / j; 331f3aac973bb944885a1a4779ba04a97faa88b7ed0buzbee special = (start+i) / 14; 332f3aac973bb944885a1a4779ba04a97faa88b7ed0buzbee break; 333f3aac973bb944885a1a4779ba04a97faa88b7ed0buzbee case 15: 334f3aac973bb944885a1a4779ba04a97faa88b7ed0buzbee normal = (start+i) / j; 335f3aac973bb944885a1a4779ba04a97faa88b7ed0buzbee special = (start+i) / 15; 336f3aac973bb944885a1a4779ba04a97faa88b7ed0buzbee break; 337f3aac973bb944885a1a4779ba04a97faa88b7ed0buzbee } 3386bbf0967d217ab2b7bdbb78bfd076b8fb07a44e8Alexei Zavjalov Main.assertTrue(normal == special); 339f3aac973bb944885a1a4779ba04a97faa88b7ed0buzbee } 340f3aac973bb944885a1a4779ba04a97faa88b7ed0buzbee } 341f3aac973bb944885a1a4779ba04a97faa88b7ed0buzbee } 342f3aac973bb944885a1a4779ba04a97faa88b7ed0buzbee 343f3aac973bb944885a1a4779ba04a97faa88b7ed0buzbee static void divLiteralTest() { 344f3aac973bb944885a1a4779ba04a97faa88b7ed0buzbee System.out.println("IntMath.divLiteralTest"); 345f3aac973bb944885a1a4779ba04a97faa88b7ed0buzbee divLiteralTestBody(-1000, 2000); 346f3aac973bb944885a1a4779ba04a97faa88b7ed0buzbee divLiteralTestBody(0x7fffffff-2000, 2000); 347f3aac973bb944885a1a4779ba04a97faa88b7ed0buzbee divLiteralTestBody(0xfff0ffff, 2000); 348f3aac973bb944885a1a4779ba04a97faa88b7ed0buzbee } 3495d1ac920fdaef5d4ec8f66bb734488cd9660b024jeffhao 3505d1ac920fdaef5d4ec8f66bb734488cd9660b024jeffhao /* 3515d1ac920fdaef5d4ec8f66bb734488cd9660b024jeffhao * Shift some data. (value=0xff00aa01, dist=8) 3525d1ac920fdaef5d4ec8f66bb734488cd9660b024jeffhao */ 3535d1ac920fdaef5d4ec8f66bb734488cd9660b024jeffhao static int[] intShiftTest(int value, int dist) { 3545d1ac920fdaef5d4ec8f66bb734488cd9660b024jeffhao System.out.println("IntMath.intShiftTest"); 3555d1ac920fdaef5d4ec8f66bb734488cd9660b024jeffhao 3565d1ac920fdaef5d4ec8f66bb734488cd9660b024jeffhao int results[] = new int[4]; 3575d1ac920fdaef5d4ec8f66bb734488cd9660b024jeffhao 3585d1ac920fdaef5d4ec8f66bb734488cd9660b024jeffhao results[0] = value << dist; 3595d1ac920fdaef5d4ec8f66bb734488cd9660b024jeffhao results[1] = value >> dist; 3605d1ac920fdaef5d4ec8f66bb734488cd9660b024jeffhao results[2] = value >>> dist; 3615d1ac920fdaef5d4ec8f66bb734488cd9660b024jeffhao 3625d1ac920fdaef5d4ec8f66bb734488cd9660b024jeffhao results[3] = (((value << dist) >> dist) >>> dist) << dist; 3635d1ac920fdaef5d4ec8f66bb734488cd9660b024jeffhao return results; 3645d1ac920fdaef5d4ec8f66bb734488cd9660b024jeffhao } 3655d1ac920fdaef5d4ec8f66bb734488cd9660b024jeffhao static void intShiftCheck(int[] results) { 3665d1ac920fdaef5d4ec8f66bb734488cd9660b024jeffhao System.out.println("IntMath.intShiftCheck"); 3675d1ac920fdaef5d4ec8f66bb734488cd9660b024jeffhao 368795d78f4d04c8d007bf2bdf2ed4131379bcf19c7jeffhao Main.assertTrue(results[0] == 0x00aa0100); 369795d78f4d04c8d007bf2bdf2ed4131379bcf19c7jeffhao Main.assertTrue(results[1] == 0xffff00aa); 370795d78f4d04c8d007bf2bdf2ed4131379bcf19c7jeffhao Main.assertTrue(results[2] == 0x00ff00aa); 371795d78f4d04c8d007bf2bdf2ed4131379bcf19c7jeffhao Main.assertTrue(results[3] == 0xaa00); 3725d1ac920fdaef5d4ec8f66bb734488cd9660b024jeffhao } 3735d1ac920fdaef5d4ec8f66bb734488cd9660b024jeffhao 3745d1ac920fdaef5d4ec8f66bb734488cd9660b024jeffhao /* 3755d1ac920fdaef5d4ec8f66bb734488cd9660b024jeffhao * We pass in the arguments and return the results so the compiler 3765d1ac920fdaef5d4ec8f66bb734488cd9660b024jeffhao * doesn't do the math for us. (x=70000000000, y=-3) 3775d1ac920fdaef5d4ec8f66bb734488cd9660b024jeffhao */ 3785d1ac920fdaef5d4ec8f66bb734488cd9660b024jeffhao static long[] longOperTest(long x, long y) { 3795d1ac920fdaef5d4ec8f66bb734488cd9660b024jeffhao System.out.println("IntMath.longOperTest"); 3805d1ac920fdaef5d4ec8f66bb734488cd9660b024jeffhao 3815d1ac920fdaef5d4ec8f66bb734488cd9660b024jeffhao long[] results = new long[10]; 3825d1ac920fdaef5d4ec8f66bb734488cd9660b024jeffhao 3835d1ac920fdaef5d4ec8f66bb734488cd9660b024jeffhao /* this seems to generate "op-long" instructions */ 3845d1ac920fdaef5d4ec8f66bb734488cd9660b024jeffhao results[0] = x + y; 3855d1ac920fdaef5d4ec8f66bb734488cd9660b024jeffhao results[1] = x - y; 3865d1ac920fdaef5d4ec8f66bb734488cd9660b024jeffhao results[2] = x * y; 3875d1ac920fdaef5d4ec8f66bb734488cd9660b024jeffhao results[3] = x * x; 3885d1ac920fdaef5d4ec8f66bb734488cd9660b024jeffhao results[4] = x / y; 3895d1ac920fdaef5d4ec8f66bb734488cd9660b024jeffhao results[5] = x % -y; 3905d1ac920fdaef5d4ec8f66bb734488cd9660b024jeffhao results[6] = x & y; 3915d1ac920fdaef5d4ec8f66bb734488cd9660b024jeffhao results[7] = x | y; 3925d1ac920fdaef5d4ec8f66bb734488cd9660b024jeffhao results[8] = x ^ y; 3935d1ac920fdaef5d4ec8f66bb734488cd9660b024jeffhao 3945d1ac920fdaef5d4ec8f66bb734488cd9660b024jeffhao /* this seems to generate "op-long/2addr" instructions */ 3955d1ac920fdaef5d4ec8f66bb734488cd9660b024jeffhao results[9] = x + ((((((((x + y) - y) * y) / y) % y) & y) | y) ^ y); 3965d1ac920fdaef5d4ec8f66bb734488cd9660b024jeffhao 3975d1ac920fdaef5d4ec8f66bb734488cd9660b024jeffhao return results; 3985d1ac920fdaef5d4ec8f66bb734488cd9660b024jeffhao } 3995d1ac920fdaef5d4ec8f66bb734488cd9660b024jeffhao static void longOperCheck(long[] results) { 4005d1ac920fdaef5d4ec8f66bb734488cd9660b024jeffhao System.out.println("IntMath.longOperCheck"); 4015d1ac920fdaef5d4ec8f66bb734488cd9660b024jeffhao 4025d1ac920fdaef5d4ec8f66bb734488cd9660b024jeffhao /* check this edge case while we're here (div-long/2addr) */ 4035d1ac920fdaef5d4ec8f66bb734488cd9660b024jeffhao long minLong = -9223372036854775808L; 4045d1ac920fdaef5d4ec8f66bb734488cd9660b024jeffhao long negOne = -results[5]; 4055d1ac920fdaef5d4ec8f66bb734488cd9660b024jeffhao long plusOne = 1; 4065d1ac920fdaef5d4ec8f66bb734488cd9660b024jeffhao long result = (((minLong + plusOne) - plusOne) / negOne) / negOne; 407795d78f4d04c8d007bf2bdf2ed4131379bcf19c7jeffhao Main.assertTrue(result == minLong); 408795d78f4d04c8d007bf2bdf2ed4131379bcf19c7jeffhao 409795d78f4d04c8d007bf2bdf2ed4131379bcf19c7jeffhao Main.assertTrue(results[0] == 69999999997L); 410795d78f4d04c8d007bf2bdf2ed4131379bcf19c7jeffhao Main.assertTrue(results[1] == 70000000003L); 411795d78f4d04c8d007bf2bdf2ed4131379bcf19c7jeffhao Main.assertTrue(results[2] == -210000000000L); 412795d78f4d04c8d007bf2bdf2ed4131379bcf19c7jeffhao Main.assertTrue(results[3] == -6833923606740729856L); // overflow 413795d78f4d04c8d007bf2bdf2ed4131379bcf19c7jeffhao Main.assertTrue(results[4] == -23333333333L); 414795d78f4d04c8d007bf2bdf2ed4131379bcf19c7jeffhao Main.assertTrue(results[5] == 1); 415795d78f4d04c8d007bf2bdf2ed4131379bcf19c7jeffhao Main.assertTrue(results[6] == 70000000000L); 416795d78f4d04c8d007bf2bdf2ed4131379bcf19c7jeffhao Main.assertTrue(results[7] == -3); 417795d78f4d04c8d007bf2bdf2ed4131379bcf19c7jeffhao Main.assertTrue(results[8] == -70000000003L); 418795d78f4d04c8d007bf2bdf2ed4131379bcf19c7jeffhao Main.assertTrue(results[9] == 70000000000L); 419795d78f4d04c8d007bf2bdf2ed4131379bcf19c7jeffhao 420795d78f4d04c8d007bf2bdf2ed4131379bcf19c7jeffhao Main.assertTrue(results.length == 10); 4215d1ac920fdaef5d4ec8f66bb734488cd9660b024jeffhao } 4225d1ac920fdaef5d4ec8f66bb734488cd9660b024jeffhao 4235d1ac920fdaef5d4ec8f66bb734488cd9660b024jeffhao /* 4245d1ac920fdaef5d4ec8f66bb734488cd9660b024jeffhao * Shift some data. (value=0xd5aa96deff00aa01, dist=8) 4255d1ac920fdaef5d4ec8f66bb734488cd9660b024jeffhao */ 4265d1ac920fdaef5d4ec8f66bb734488cd9660b024jeffhao static long[] longShiftTest(long value, int dist) { 4275d1ac920fdaef5d4ec8f66bb734488cd9660b024jeffhao System.out.println("IntMath.longShiftTest"); 4285d1ac920fdaef5d4ec8f66bb734488cd9660b024jeffhao 4295d1ac920fdaef5d4ec8f66bb734488cd9660b024jeffhao long results[] = new long[4]; 4305d1ac920fdaef5d4ec8f66bb734488cd9660b024jeffhao 4315d1ac920fdaef5d4ec8f66bb734488cd9660b024jeffhao results[0] = value << dist; 4325d1ac920fdaef5d4ec8f66bb734488cd9660b024jeffhao results[1] = value >> dist; 4335d1ac920fdaef5d4ec8f66bb734488cd9660b024jeffhao results[2] = value >>> dist; 4345d1ac920fdaef5d4ec8f66bb734488cd9660b024jeffhao 4355d1ac920fdaef5d4ec8f66bb734488cd9660b024jeffhao results[3] = (((value << dist) >> dist) >>> dist) << dist; 4365d1ac920fdaef5d4ec8f66bb734488cd9660b024jeffhao return results; 4375d1ac920fdaef5d4ec8f66bb734488cd9660b024jeffhao } 4385d1ac920fdaef5d4ec8f66bb734488cd9660b024jeffhao static long longShiftCheck(long[] results) { 4395d1ac920fdaef5d4ec8f66bb734488cd9660b024jeffhao System.out.println("IntMath.longShiftCheck"); 4405d1ac920fdaef5d4ec8f66bb734488cd9660b024jeffhao 441795d78f4d04c8d007bf2bdf2ed4131379bcf19c7jeffhao Main.assertTrue(results[0] == 0x96deff00aa010000L); 442795d78f4d04c8d007bf2bdf2ed4131379bcf19c7jeffhao Main.assertTrue(results[1] == 0xffffd5aa96deff00L); 443795d78f4d04c8d007bf2bdf2ed4131379bcf19c7jeffhao Main.assertTrue(results[2] == 0x0000d5aa96deff00L); 444795d78f4d04c8d007bf2bdf2ed4131379bcf19c7jeffhao Main.assertTrue(results[3] == 0xffff96deff000000L); 4455d1ac920fdaef5d4ec8f66bb734488cd9660b024jeffhao 446795d78f4d04c8d007bf2bdf2ed4131379bcf19c7jeffhao Main.assertTrue(results.length == 4); 4475d1ac920fdaef5d4ec8f66bb734488cd9660b024jeffhao 4485d1ac920fdaef5d4ec8f66bb734488cd9660b024jeffhao return results[0]; // test return-long 4495d1ac920fdaef5d4ec8f66bb734488cd9660b024jeffhao } 4505d1ac920fdaef5d4ec8f66bb734488cd9660b024jeffhao 4515d1ac920fdaef5d4ec8f66bb734488cd9660b024jeffhao 4525d1ac920fdaef5d4ec8f66bb734488cd9660b024jeffhao /* 4535d1ac920fdaef5d4ec8f66bb734488cd9660b024jeffhao * Try to cause some unary operations. 4545d1ac920fdaef5d4ec8f66bb734488cd9660b024jeffhao */ 4555d1ac920fdaef5d4ec8f66bb734488cd9660b024jeffhao static int unopTest(int x) { 4565d1ac920fdaef5d4ec8f66bb734488cd9660b024jeffhao x = -x; 4575d1ac920fdaef5d4ec8f66bb734488cd9660b024jeffhao x ^= 0xffffffff; 4585d1ac920fdaef5d4ec8f66bb734488cd9660b024jeffhao return x; 4595d1ac920fdaef5d4ec8f66bb734488cd9660b024jeffhao } 4605d1ac920fdaef5d4ec8f66bb734488cd9660b024jeffhao static void unopCheck(int result) { 461795d78f4d04c8d007bf2bdf2ed4131379bcf19c7jeffhao Main.assertTrue(result == 37); 4625d1ac920fdaef5d4ec8f66bb734488cd9660b024jeffhao } 4635d1ac920fdaef5d4ec8f66bb734488cd9660b024jeffhao 4645d1ac920fdaef5d4ec8f66bb734488cd9660b024jeffhao static class Shorty { 4655d1ac920fdaef5d4ec8f66bb734488cd9660b024jeffhao public short mShort; 4665d1ac920fdaef5d4ec8f66bb734488cd9660b024jeffhao public char mChar; 4675d1ac920fdaef5d4ec8f66bb734488cd9660b024jeffhao public byte mByte; 4685d1ac920fdaef5d4ec8f66bb734488cd9660b024jeffhao }; 4695d1ac920fdaef5d4ec8f66bb734488cd9660b024jeffhao 4705d1ac920fdaef5d4ec8f66bb734488cd9660b024jeffhao /* 4715d1ac920fdaef5d4ec8f66bb734488cd9660b024jeffhao * Truncate an int. 4725d1ac920fdaef5d4ec8f66bb734488cd9660b024jeffhao */ 4735d1ac920fdaef5d4ec8f66bb734488cd9660b024jeffhao static Shorty truncateTest(int x) { 4745d1ac920fdaef5d4ec8f66bb734488cd9660b024jeffhao System.out.println("IntMath.truncateTest"); 4755d1ac920fdaef5d4ec8f66bb734488cd9660b024jeffhao Shorty shorts = new Shorty(); 4765d1ac920fdaef5d4ec8f66bb734488cd9660b024jeffhao 4775d1ac920fdaef5d4ec8f66bb734488cd9660b024jeffhao shorts.mShort = (short) x; 4785d1ac920fdaef5d4ec8f66bb734488cd9660b024jeffhao shorts.mChar = (char) x; 4795d1ac920fdaef5d4ec8f66bb734488cd9660b024jeffhao shorts.mByte = (byte) x; 4805d1ac920fdaef5d4ec8f66bb734488cd9660b024jeffhao return shorts; 4815d1ac920fdaef5d4ec8f66bb734488cd9660b024jeffhao } 4825d1ac920fdaef5d4ec8f66bb734488cd9660b024jeffhao static void truncateCheck(Shorty shorts) { 483795d78f4d04c8d007bf2bdf2ed4131379bcf19c7jeffhao Main.assertTrue(shorts.mShort == -5597); // 0xea23 484795d78f4d04c8d007bf2bdf2ed4131379bcf19c7jeffhao Main.assertTrue(shorts.mChar == 59939); // 0xea23 485795d78f4d04c8d007bf2bdf2ed4131379bcf19c7jeffhao Main.assertTrue(shorts.mByte == 35); // 0x23 4865d1ac920fdaef5d4ec8f66bb734488cd9660b024jeffhao } 4875d1ac920fdaef5d4ec8f66bb734488cd9660b024jeffhao 4885d1ac920fdaef5d4ec8f66bb734488cd9660b024jeffhao /* 4895d1ac920fdaef5d4ec8f66bb734488cd9660b024jeffhao * Verify that we get a divide-by-zero exception. 4905d1ac920fdaef5d4ec8f66bb734488cd9660b024jeffhao */ 4915d1ac920fdaef5d4ec8f66bb734488cd9660b024jeffhao static void divideByZero(int z) { 4925d1ac920fdaef5d4ec8f66bb734488cd9660b024jeffhao System.out.println("IntMath.divideByZero"); 4935d1ac920fdaef5d4ec8f66bb734488cd9660b024jeffhao 4945d1ac920fdaef5d4ec8f66bb734488cd9660b024jeffhao try { 4955d1ac920fdaef5d4ec8f66bb734488cd9660b024jeffhao int x = 100 / z; 496795d78f4d04c8d007bf2bdf2ed4131379bcf19c7jeffhao Main.assertTrue(false); 4975d1ac920fdaef5d4ec8f66bb734488cd9660b024jeffhao } catch (ArithmeticException ae) { 4985d1ac920fdaef5d4ec8f66bb734488cd9660b024jeffhao } 4995d1ac920fdaef5d4ec8f66bb734488cd9660b024jeffhao 5005d1ac920fdaef5d4ec8f66bb734488cd9660b024jeffhao try { 5015d1ac920fdaef5d4ec8f66bb734488cd9660b024jeffhao int x = 100 % z; 502795d78f4d04c8d007bf2bdf2ed4131379bcf19c7jeffhao Main.assertTrue(false); 5035d1ac920fdaef5d4ec8f66bb734488cd9660b024jeffhao } catch (ArithmeticException ae) { 5045d1ac920fdaef5d4ec8f66bb734488cd9660b024jeffhao } 5055d1ac920fdaef5d4ec8f66bb734488cd9660b024jeffhao 5065d1ac920fdaef5d4ec8f66bb734488cd9660b024jeffhao try { 5075d1ac920fdaef5d4ec8f66bb734488cd9660b024jeffhao long x = 100L / z; 508795d78f4d04c8d007bf2bdf2ed4131379bcf19c7jeffhao Main.assertTrue(false); 5095d1ac920fdaef5d4ec8f66bb734488cd9660b024jeffhao } catch (ArithmeticException ae) { 5105d1ac920fdaef5d4ec8f66bb734488cd9660b024jeffhao } 5115d1ac920fdaef5d4ec8f66bb734488cd9660b024jeffhao 5125d1ac920fdaef5d4ec8f66bb734488cd9660b024jeffhao try { 5135d1ac920fdaef5d4ec8f66bb734488cd9660b024jeffhao long x = 100L % z; 514795d78f4d04c8d007bf2bdf2ed4131379bcf19c7jeffhao Main.assertTrue(false); 5155d1ac920fdaef5d4ec8f66bb734488cd9660b024jeffhao } catch (ArithmeticException ae) { 5165d1ac920fdaef5d4ec8f66bb734488cd9660b024jeffhao } 5175d1ac920fdaef5d4ec8f66bb734488cd9660b024jeffhao } 5185d1ac920fdaef5d4ec8f66bb734488cd9660b024jeffhao 5195d1ac920fdaef5d4ec8f66bb734488cd9660b024jeffhao /* 5205d1ac920fdaef5d4ec8f66bb734488cd9660b024jeffhao * Check an edge condition: dividing the most-negative integer by -1 5215d1ac920fdaef5d4ec8f66bb734488cd9660b024jeffhao * returns the most-negative integer, and doesn't cause an exception. 5225d1ac920fdaef5d4ec8f66bb734488cd9660b024jeffhao * 5235d1ac920fdaef5d4ec8f66bb734488cd9660b024jeffhao * Pass in -1, -1L. 5245d1ac920fdaef5d4ec8f66bb734488cd9660b024jeffhao */ 5255d1ac920fdaef5d4ec8f66bb734488cd9660b024jeffhao static void bigDivideOverflow(int idiv, long ldiv) { 5265d1ac920fdaef5d4ec8f66bb734488cd9660b024jeffhao System.out.println("IntMath.bigDivideOverflow"); 5275d1ac920fdaef5d4ec8f66bb734488cd9660b024jeffhao int mostNegInt = (int) 0x80000000; 5285d1ac920fdaef5d4ec8f66bb734488cd9660b024jeffhao long mostNegLong = (long) 0x8000000000000000L; 5295d1ac920fdaef5d4ec8f66bb734488cd9660b024jeffhao 5305d1ac920fdaef5d4ec8f66bb734488cd9660b024jeffhao int intDivResult = mostNegInt / idiv; 5315d1ac920fdaef5d4ec8f66bb734488cd9660b024jeffhao int intModResult = mostNegInt % idiv; 5325d1ac920fdaef5d4ec8f66bb734488cd9660b024jeffhao long longDivResult = mostNegLong / ldiv; 5335d1ac920fdaef5d4ec8f66bb734488cd9660b024jeffhao long longModResult = mostNegLong % ldiv; 5345d1ac920fdaef5d4ec8f66bb734488cd9660b024jeffhao 535795d78f4d04c8d007bf2bdf2ed4131379bcf19c7jeffhao Main.assertTrue(intDivResult == mostNegInt); 536795d78f4d04c8d007bf2bdf2ed4131379bcf19c7jeffhao Main.assertTrue(intModResult == 0); 537795d78f4d04c8d007bf2bdf2ed4131379bcf19c7jeffhao Main.assertTrue(longDivResult == mostNegLong); 538795d78f4d04c8d007bf2bdf2ed4131379bcf19c7jeffhao Main.assertTrue(longModResult == 0); 5395d1ac920fdaef5d4ec8f66bb734488cd9660b024jeffhao } 5405d1ac920fdaef5d4ec8f66bb734488cd9660b024jeffhao 5415d1ac920fdaef5d4ec8f66bb734488cd9660b024jeffhao /* 5425d1ac920fdaef5d4ec8f66bb734488cd9660b024jeffhao * Check "const" instructions. We use negative values to ensure that 5435d1ac920fdaef5d4ec8f66bb734488cd9660b024jeffhao * sign-extension is happening. 5445d1ac920fdaef5d4ec8f66bb734488cd9660b024jeffhao */ 5455d1ac920fdaef5d4ec8f66bb734488cd9660b024jeffhao static void checkConsts(byte small, short medium, int large, long huge) { 5465d1ac920fdaef5d4ec8f66bb734488cd9660b024jeffhao System.out.println("IntMath.checkConsts"); 5475d1ac920fdaef5d4ec8f66bb734488cd9660b024jeffhao 548795d78f4d04c8d007bf2bdf2ed4131379bcf19c7jeffhao Main.assertTrue(small == 1); // const/4 549795d78f4d04c8d007bf2bdf2ed4131379bcf19c7jeffhao Main.assertTrue(medium == -256); // const/16 550795d78f4d04c8d007bf2bdf2ed4131379bcf19c7jeffhao Main.assertTrue(medium == -256L); // const-wide/16 551795d78f4d04c8d007bf2bdf2ed4131379bcf19c7jeffhao Main.assertTrue(large == -88888); // const 552795d78f4d04c8d007bf2bdf2ed4131379bcf19c7jeffhao Main.assertTrue(large == -88888L); // const-wide/32 553795d78f4d04c8d007bf2bdf2ed4131379bcf19c7jeffhao Main.assertTrue(huge == 0x9922334455667788L); // const-wide 5545d1ac920fdaef5d4ec8f66bb734488cd9660b024jeffhao } 5555d1ac920fdaef5d4ec8f66bb734488cd9660b024jeffhao 5565d1ac920fdaef5d4ec8f66bb734488cd9660b024jeffhao /* 5575d1ac920fdaef5d4ec8f66bb734488cd9660b024jeffhao * Test some java.lang.Math functions. 5585d1ac920fdaef5d4ec8f66bb734488cd9660b024jeffhao * 5595d1ac920fdaef5d4ec8f66bb734488cd9660b024jeffhao * The method arguments are positive values. 5605d1ac920fdaef5d4ec8f66bb734488cd9660b024jeffhao */ 5615d1ac920fdaef5d4ec8f66bb734488cd9660b024jeffhao static void jlmTests(int ii, long ll) { 5625d1ac920fdaef5d4ec8f66bb734488cd9660b024jeffhao System.out.println("IntMath.jlmTests"); 5635d1ac920fdaef5d4ec8f66bb734488cd9660b024jeffhao 564795d78f4d04c8d007bf2bdf2ed4131379bcf19c7jeffhao Main.assertTrue(Math.abs(ii) == ii); 565795d78f4d04c8d007bf2bdf2ed4131379bcf19c7jeffhao Main.assertTrue(Math.abs(-ii) == ii); 566795d78f4d04c8d007bf2bdf2ed4131379bcf19c7jeffhao Main.assertTrue(Math.min(ii, -5) == -5); 567795d78f4d04c8d007bf2bdf2ed4131379bcf19c7jeffhao Main.assertTrue(Math.max(ii, -5) == ii); 5685d1ac920fdaef5d4ec8f66bb734488cd9660b024jeffhao 569795d78f4d04c8d007bf2bdf2ed4131379bcf19c7jeffhao Main.assertTrue(Math.abs(ll) == ll); 570795d78f4d04c8d007bf2bdf2ed4131379bcf19c7jeffhao Main.assertTrue(Math.abs(-ll) == ll); 571795d78f4d04c8d007bf2bdf2ed4131379bcf19c7jeffhao Main.assertTrue(Math.min(ll, -5L) == -5L); 572795d78f4d04c8d007bf2bdf2ed4131379bcf19c7jeffhao Main.assertTrue(Math.max(ll, -5L) == ll); 5735d1ac920fdaef5d4ec8f66bb734488cd9660b024jeffhao } 5745d1ac920fdaef5d4ec8f66bb734488cd9660b024jeffhao 5755d1ac920fdaef5d4ec8f66bb734488cd9660b024jeffhao public static void run() { 5765d1ac920fdaef5d4ec8f66bb734488cd9660b024jeffhao shiftTest1(); 5775d1ac920fdaef5d4ec8f66bb734488cd9660b024jeffhao shiftTest2(); 5785d1ac920fdaef5d4ec8f66bb734488cd9660b024jeffhao unsignedShiftTest(); 57955b16ceff1068cce61c86f2c9ca011a97c869bd4Ian Rogers shiftTest3(32); 5805d1ac920fdaef5d4ec8f66bb734488cd9660b024jeffhao convTest(); 5815d1ac920fdaef5d4ec8f66bb734488cd9660b024jeffhao charSubTest(); 5825d1ac920fdaef5d4ec8f66bb734488cd9660b024jeffhao 5835d1ac920fdaef5d4ec8f66bb734488cd9660b024jeffhao int[] intResults; 5845d1ac920fdaef5d4ec8f66bb734488cd9660b024jeffhao long[] longResults; 5855d1ac920fdaef5d4ec8f66bb734488cd9660b024jeffhao 5865d1ac920fdaef5d4ec8f66bb734488cd9660b024jeffhao intResults = intOperTest(70000, -3); 5875d1ac920fdaef5d4ec8f66bb734488cd9660b024jeffhao intOperCheck(intResults); 5885d1ac920fdaef5d4ec8f66bb734488cd9660b024jeffhao longResults = longOperTest(70000000000L, -3L); 5895d1ac920fdaef5d4ec8f66bb734488cd9660b024jeffhao longOperCheck(longResults); 5905d1ac920fdaef5d4ec8f66bb734488cd9660b024jeffhao 5915d1ac920fdaef5d4ec8f66bb734488cd9660b024jeffhao intResults = lit16Test(77777); 5925d1ac920fdaef5d4ec8f66bb734488cd9660b024jeffhao lit16Check(intResults); 5935d1ac920fdaef5d4ec8f66bb734488cd9660b024jeffhao intResults = lit8Test(-55555); 5945d1ac920fdaef5d4ec8f66bb734488cd9660b024jeffhao lit8Check(intResults); 595f3aac973bb944885a1a4779ba04a97faa88b7ed0buzbee divLiteralTest(); 5965d1ac920fdaef5d4ec8f66bb734488cd9660b024jeffhao 5975d1ac920fdaef5d4ec8f66bb734488cd9660b024jeffhao intResults = intShiftTest(0xff00aa01, 8); 5985d1ac920fdaef5d4ec8f66bb734488cd9660b024jeffhao intShiftCheck(intResults); 5995d1ac920fdaef5d4ec8f66bb734488cd9660b024jeffhao longResults = longShiftTest(0xd5aa96deff00aa01L, 16); 6005d1ac920fdaef5d4ec8f66bb734488cd9660b024jeffhao long longRet = longShiftCheck(longResults); 601795d78f4d04c8d007bf2bdf2ed4131379bcf19c7jeffhao Main.assertTrue(longRet == 0x96deff00aa010000L); 6025d1ac920fdaef5d4ec8f66bb734488cd9660b024jeffhao 6035d1ac920fdaef5d4ec8f66bb734488cd9660b024jeffhao Shorty shorts = truncateTest(-16717277); // 0xff00ea23 6045d1ac920fdaef5d4ec8f66bb734488cd9660b024jeffhao truncateCheck(shorts); 6055d1ac920fdaef5d4ec8f66bb734488cd9660b024jeffhao 6065d1ac920fdaef5d4ec8f66bb734488cd9660b024jeffhao divideByZero(0); 6075d1ac920fdaef5d4ec8f66bb734488cd9660b024jeffhao bigDivideOverflow(-1, -1L); 6085d1ac920fdaef5d4ec8f66bb734488cd9660b024jeffhao 6095d1ac920fdaef5d4ec8f66bb734488cd9660b024jeffhao checkConsts((byte) 1, (short) -256, -88888, 0x9922334455667788L); 6105d1ac920fdaef5d4ec8f66bb734488cd9660b024jeffhao 6115d1ac920fdaef5d4ec8f66bb734488cd9660b024jeffhao unopCheck(unopTest(38)); 6125d1ac920fdaef5d4ec8f66bb734488cd9660b024jeffhao 6135d1ac920fdaef5d4ec8f66bb734488cd9660b024jeffhao jlmTests(12345, 0x1122334455667788L); 6145d1ac920fdaef5d4ec8f66bb734488cd9660b024jeffhao } 6155d1ac920fdaef5d4ec8f66bb734488cd9660b024jeffhao} 616