Mads Ager | 418d1ca | 2017-05-22 09:35:49 +0200 | [diff] [blame] | 1 | // Copyright (c) 2016, the R8 project authors. Please see the AUTHORS file |
| 2 | // for details. All rights reserved. Use of this source code is governed by a |
| 3 | // BSD-style license that can be found in the LICENSE file. |
| 4 | |
| 5 | // This code is not run directly. It needs to be compiled to dex code. |
| 6 | // 'arithmetic.dex' is what is run. |
| 7 | |
| 8 | package arithmetic; |
| 9 | |
| 10 | import java.util.Arrays; |
| 11 | |
| 12 | public class Arithmetic { |
| 13 | static void addInts(int[] ints) { |
| 14 | int result = 0; |
| 15 | for (int i : ints) { |
| 16 | result += i; |
| 17 | } |
| 18 | System.out.println("addInts: " + Arrays.toString(ints) + " = " + result); |
| 19 | } |
| 20 | |
| 21 | static void addDoubles(double[] doubles) { |
| 22 | double result = 0; |
| 23 | for (double d : doubles) { |
| 24 | result += d; |
| 25 | } |
| 26 | System.out.println("addDoubles: " + Arrays.toString(doubles) + " = " + result); |
| 27 | } |
| 28 | |
| 29 | static void addLongs(long[] longs) { |
| 30 | long result = 0; |
| 31 | for (long l : longs) { |
| 32 | result += l; |
| 33 | } |
| 34 | System.out.println("addLongs: " + Arrays.toString(longs) + " = " + result); |
| 35 | } |
| 36 | |
| 37 | static void binaryOps() { |
| 38 | int i = 0; |
| 39 | System.out.println("i values:"); |
| 40 | i = i + 1; |
| 41 | System.out.println(i); |
| 42 | i = 1 + i; |
| 43 | System.out.println(i); |
| 44 | i = i * 4; |
| 45 | System.out.println(i); |
| 46 | i = i * i; |
| 47 | System.out.println(i); |
| 48 | i = 4 * i; |
| 49 | System.out.println(i); |
| 50 | i = i / 4; |
| 51 | System.out.println(i); |
| 52 | i = i / i; |
| 53 | System.out.println(i); |
| 54 | i = i % i; |
| 55 | System.out.println(i); |
| 56 | |
| 57 | long l = 0; |
| 58 | System.out.println("l values:"); |
| 59 | l = l + 1; |
| 60 | System.out.println(l); |
| 61 | l = 1 + l; |
| 62 | System.out.println(l); |
| 63 | l = l * 4; |
| 64 | System.out.println(l); |
| 65 | l = l * l; |
| 66 | System.out.println(l); |
| 67 | l = 4 * l; |
| 68 | System.out.println(l); |
| 69 | l = l / 4; |
| 70 | System.out.println(l); |
| 71 | l = l / l; |
| 72 | System.out.println(l); |
| 73 | l = l % l; |
| 74 | System.out.println(l); |
| 75 | |
| 76 | double d = 0.0; |
| 77 | System.out.println("d values: "); |
| 78 | d = d + 1.0; |
| 79 | System.out.println(d); |
| 80 | d = 1.0 + d; |
| 81 | System.out.println(d); |
| 82 | d = d * 4.0; |
| 83 | System.out.println(d); |
| 84 | d = d * d; |
| 85 | System.out.println(d); |
| 86 | d = 4.0 * d; |
| 87 | System.out.println(d); |
| 88 | d = d / 4.0; |
| 89 | System.out.println(d); |
| 90 | d = d / d; |
| 91 | System.out.println(d); |
| 92 | d = d % d; |
| 93 | System.out.println(d); |
| 94 | |
| 95 | float f = 0.0f; |
| 96 | System.out.println("f values: "); |
| 97 | f = f + 1.0f; |
| 98 | System.out.println(f); |
| 99 | f = 1.0f + f; |
| 100 | System.out.println(f); |
| 101 | f = f * 4.0f; |
| 102 | System.out.println(f); |
| 103 | f = f * f; |
| 104 | System.out.println(f); |
| 105 | f = 4.0f * f; |
| 106 | System.out.println(f); |
| 107 | f = f / 4.0f; |
| 108 | System.out.println(f); |
| 109 | f = f / f; |
| 110 | System.out.println(f); |
| 111 | f = f % f; |
| 112 | System.out.println(f); |
| 113 | } |
| 114 | |
| 115 | public static void moreOps() { |
| 116 | int a = 42; |
| 117 | int b = -a; |
| 118 | int shiftLeftA = a << 5; |
| 119 | int shiftRightA = a >> 5; |
| 120 | int uShiftRightA = -a >>> 5; |
| 121 | System.out.println(a + b + shiftLeftA + shiftRightA + uShiftRightA); |
| 122 | float c = 42.42f; |
| 123 | float d = -c; |
| 124 | System.out.println(c + d); |
| 125 | double e = 43.43; |
| 126 | double f = -e; |
| 127 | System.out.println(e + f); |
| 128 | long g = 5000000000L; |
| 129 | long h = -g; |
| 130 | long shiftLeftG = g << 8; |
| 131 | long shiftRightG = g >> 8; |
| 132 | long uShiftRightG = -g >>> 8; |
| 133 | System.out.println(g + h + shiftLeftG + shiftRightG + uShiftRightG); |
| 134 | } |
| 135 | |
| 136 | public static void bitwiseInts(int x, int y) { |
| 137 | System.out.println(x & y); |
| 138 | System.out.println(x | y); |
| 139 | System.out.println(x ^ y); |
| 140 | System.out.println(~x); |
| 141 | } |
| 142 | |
| 143 | public static void bitwiseLongs(long x, long y) { |
| 144 | System.out.println(x & y); |
| 145 | System.out.println(x | y); |
| 146 | System.out.println(x ^ y); |
| 147 | System.out.println(~x); |
| 148 | } |
| 149 | |
| 150 | public static void main(String[] args) { |
| 151 | addInts(new int[] { }); |
| 152 | addInts(new int[] { 1 }); |
| 153 | addInts(new int[] { 0, 1, 2, 3 }); |
| 154 | addDoubles(new double[] { 0.0 }); |
| 155 | addDoubles(new double[] { 0.0, 1.0, 2.0 }); |
| 156 | addDoubles(new double[] { 0.0, 1.0, 2.0, 3.0 }); |
| 157 | long l = 0x0000000100000000L; |
| 158 | addLongs(new long[] { }); |
| 159 | addLongs(new long[] { l }); |
| 160 | addLongs(new long[] { l, l + 1, l + 2 }); |
| 161 | binaryOps(); |
| 162 | moreOps(); |
| 163 | bitwiseInts(12345, 54321); |
| 164 | bitwiseLongs(54321, 12345); |
| 165 | } |
| 166 | } |