1 /*
2  * Copyright (C) 2014 The Android Open Source Project
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  *      http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16 
17 // Note that $opt$ is a marker for the optimizing compiler to ensure
18 // it does compile the method.
19 public class Main {
20 
assertByteEquals(byte expected, byte result)21   public static void assertByteEquals(byte expected, byte result) {
22     if (expected != result) {
23       throw new Error("Expected: " + expected + ", found: " + result);
24     }
25   }
26 
assertShortEquals(short expected, short result)27   public static void assertShortEquals(short expected, short result) {
28     if (expected != result) {
29       throw new Error("Expected: " + expected + ", found: " + result);
30     }
31   }
32 
assertIntEquals(int expected, int result)33   public static void assertIntEquals(int expected, int result) {
34     if (expected != result) {
35       throw new Error("Expected: " + expected + ", found: " + result);
36     }
37   }
38 
assertLongEquals(long expected, long result)39   public static void assertLongEquals(long expected, long result) {
40     if (expected != result) {
41       throw new Error("Expected: " + expected + ", found: " + result);
42     }
43   }
44 
assertCharEquals(char expected, char result)45   public static void assertCharEquals(char expected, char result) {
46     if (expected != result) {
47       // Values are cast to int to display numeric values instead of
48       // (UTF-16 encoded) characters.
49       throw new Error("Expected: " + (int)expected + ", found: " + (int)result);
50     }
51   }
52 
assertFloatEquals(float expected, float result)53   public static void assertFloatEquals(float expected, float result) {
54     if (expected != result) {
55       throw new Error("Expected: " + expected + ", found: " + result);
56     }
57   }
58 
assertDoubleEquals(double expected, double result)59   public static void assertDoubleEquals(double expected, double result) {
60     if (expected != result) {
61       throw new Error("Expected: " + expected + ", found: " + result);
62     }
63   }
64 
assertFloatIsNaN(float result)65   public static void assertFloatIsNaN(float result) {
66     if (!Float.isNaN(result)) {
67       throw new Error("Expected: NaN, found: " + result);
68     }
69   }
70 
assertDoubleIsNaN(double result)71   public static void assertDoubleIsNaN(double result) {
72     if (!Double.isNaN(result)) {
73       throw new Error("Expected: NaN, found: " + result);
74     }
75   }
76 
77 
main(String[] args)78   public static void main(String[] args) {
79     // Generate, compile and check int-to-long Dex instructions.
80     byteToLong();
81     shortToLong();
82     intToLong();
83     charToLong();
84 
85     // Generate, compile and check int-to-float Dex instructions.
86     byteToFloat();
87     shortToFloat();
88     intToFloat();
89     charToFloat();
90 
91     // Generate, compile and check int-to-double Dex instructions.
92     byteToDouble();
93     shortToDouble();
94     intToDouble();
95     charToDouble();
96 
97     // Generate, compile and check long-to-int Dex instructions.
98     longToInt();
99 
100     // Generate, compile and check long-to-float Dex instructions.
101     longToFloat();
102 
103     // Generate, compile and check long-to-double Dex instructions.
104     longToDouble();
105 
106     // Generate, compile and check float-to-int Dex instructions.
107     floatToInt();
108 
109     // Generate, compile and check float-to-long Dex instructions.
110     floatToLong();
111 
112     // Generate, compile and check float-to-double Dex instructions.
113     floatToDouble();
114 
115     // Generate, compile and check double-to-int Dex instructions.
116     doubleToInt();
117 
118     // Generate, compile and check double-to-long Dex instructions.
119     doubleToLong();
120 
121     // Generate, compile and check double-to-float Dex instructions.
122     doubleToFloat();
123 
124     // Generate, compile and check int-to-byte Dex instructions.
125     shortToByte();
126     intToByte();
127     charToByte();
128 
129     // Generate, compile and check int-to-short Dex instructions.
130     byteToShort();
131     intToShort();
132     charToShort();
133 
134     // Generate, compile and check int-to-char Dex instructions.
135     byteToChar();
136     shortToChar();
137     intToChar();
138   }
139 
byteToLong()140   private static void byteToLong() {
141     assertLongEquals(1L, $opt$ByteToLong((byte)1));
142     assertLongEquals(0L, $opt$ByteToLong((byte)0));
143     assertLongEquals(-1L, $opt$ByteToLong((byte)-1));
144     assertLongEquals(51L, $opt$ByteToLong((byte)51));
145     assertLongEquals(-51L, $opt$ByteToLong((byte)-51));
146     assertLongEquals(127L, $opt$ByteToLong((byte)127));  // 2^7 - 1
147     assertLongEquals(-127L, $opt$ByteToLong((byte)-127));  // -(2^7 - 1)
148     assertLongEquals(-128L, $opt$ByteToLong((byte)-128));  // -(2^7)
149   }
150 
shortToLong()151   private static void shortToLong() {
152     assertLongEquals(1L, $opt$ShortToLong((short)1));
153     assertLongEquals(0L, $opt$ShortToLong((short)0));
154     assertLongEquals(-1L, $opt$ShortToLong((short)-1));
155     assertLongEquals(51L, $opt$ShortToLong((short)51));
156     assertLongEquals(-51L, $opt$ShortToLong((short)-51));
157     assertLongEquals(32767L, $opt$ShortToLong((short)32767));  // 2^15 - 1
158     assertLongEquals(-32767L, $opt$ShortToLong((short)-32767));  // -(2^15 - 1)
159     assertLongEquals(-32768L, $opt$ShortToLong((short)-32768));  // -(2^15)
160   }
161 
intToLong()162   private static void intToLong() {
163     assertLongEquals(1L, $opt$IntToLong(1));
164     assertLongEquals(0L, $opt$IntToLong(0));
165     assertLongEquals(-1L, $opt$IntToLong(-1));
166     assertLongEquals(51L, $opt$IntToLong(51));
167     assertLongEquals(-51L, $opt$IntToLong(-51));
168     assertLongEquals(2147483647L, $opt$IntToLong(2147483647));  // 2^31 - 1
169     assertLongEquals(-2147483647L, $opt$IntToLong(-2147483647));  // -(2^31 - 1)
170     assertLongEquals(-2147483648L, $opt$IntToLong(-2147483648));  // -(2^31)
171   }
172 
charToLong()173   private static void charToLong() {
174     assertLongEquals(1L, $opt$CharToLong((char)1));
175     assertLongEquals(0L, $opt$CharToLong((char)0));
176     assertLongEquals(51L, $opt$CharToLong((char)51));
177     assertLongEquals(32767L, $opt$CharToLong((char)32767));  // 2^15 - 1
178     assertLongEquals(65535L, $opt$CharToLong((char)65535));  // 2^16 - 1
179     assertLongEquals(65535L, $opt$CharToLong((char)-1));
180     assertLongEquals(65485L, $opt$CharToLong((char)-51));
181     assertLongEquals(32769L, $opt$CharToLong((char)-32767));  // -(2^15 - 1)
182     assertLongEquals(32768L, $opt$CharToLong((char)-32768));  // -(2^15)
183   }
184 
byteToFloat()185   private static void byteToFloat() {
186     assertFloatEquals(1F, $opt$ByteToFloat((byte)1));
187     assertFloatEquals(0F, $opt$ByteToFloat((byte)0));
188     assertFloatEquals(-1F, $opt$ByteToFloat((byte)-1));
189     assertFloatEquals(51F, $opt$ByteToFloat((byte)51));
190     assertFloatEquals(-51F, $opt$ByteToFloat((byte)-51));
191     assertFloatEquals(127F, $opt$ByteToFloat((byte)127));  // 2^7 - 1
192     assertFloatEquals(-127F, $opt$ByteToFloat((byte)-127));  // -(2^7 - 1)
193     assertFloatEquals(-128F, $opt$ByteToFloat((byte)-128));  // -(2^7)
194   }
195 
shortToFloat()196   private static void shortToFloat() {
197     assertFloatEquals(1F, $opt$ShortToFloat((short)1));
198     assertFloatEquals(0F, $opt$ShortToFloat((short)0));
199     assertFloatEquals(-1F, $opt$ShortToFloat((short)-1));
200     assertFloatEquals(51F, $opt$ShortToFloat((short)51));
201     assertFloatEquals(-51F, $opt$ShortToFloat((short)-51));
202     assertFloatEquals(32767F, $opt$ShortToFloat((short)32767));  // 2^15 - 1
203     assertFloatEquals(-32767F, $opt$ShortToFloat((short)-32767));  // -(2^15 - 1)
204     assertFloatEquals(-32768F, $opt$ShortToFloat((short)-32768));  // -(2^15)
205   }
206 
intToFloat()207   private static void intToFloat() {
208     assertFloatEquals(1F, $opt$IntToFloat(1));
209     assertFloatEquals(0F, $opt$IntToFloat(0));
210     assertFloatEquals(-1F, $opt$IntToFloat(-1));
211     assertFloatEquals(51F, $opt$IntToFloat(51));
212     assertFloatEquals(-51F, $opt$IntToFloat(-51));
213     assertFloatEquals(16777215F, $opt$IntToFloat(16777215));  // 2^24 - 1
214     assertFloatEquals(-16777215F, $opt$IntToFloat(-16777215));  // -(2^24 - 1)
215     assertFloatEquals(16777216F, $opt$IntToFloat(16777216));  // 2^24
216     assertFloatEquals(-16777216F, $opt$IntToFloat(-16777216));  // -(2^24)
217     assertFloatEquals(2147483647F, $opt$IntToFloat(2147483647));  // 2^31 - 1
218     assertFloatEquals(-2147483648F, $opt$IntToFloat(-2147483648));  // -(2^31)
219   }
220 
charToFloat()221   private static void charToFloat() {
222     assertFloatEquals(1F, $opt$CharToFloat((char)1));
223     assertFloatEquals(0F, $opt$CharToFloat((char)0));
224     assertFloatEquals(51F, $opt$CharToFloat((char)51));
225     assertFloatEquals(32767F, $opt$CharToFloat((char)32767));  // 2^15 - 1
226     assertFloatEquals(65535F, $opt$CharToFloat((char)65535));  // 2^16 - 1
227     assertFloatEquals(65535F, $opt$CharToFloat((char)-1));
228     assertFloatEquals(65485F, $opt$CharToFloat((char)-51));
229     assertFloatEquals(32769F, $opt$CharToFloat((char)-32767));  // -(2^15 - 1)
230     assertFloatEquals(32768F, $opt$CharToFloat((char)-32768));  // -(2^15)
231   }
232 
byteToDouble()233   private static void byteToDouble() {
234     assertDoubleEquals(1D, $opt$ByteToDouble((byte)1));
235     assertDoubleEquals(0D, $opt$ByteToDouble((byte)0));
236     assertDoubleEquals(-1D, $opt$ByteToDouble((byte)-1));
237     assertDoubleEquals(51D, $opt$ByteToDouble((byte)51));
238     assertDoubleEquals(-51D, $opt$ByteToDouble((byte)-51));
239     assertDoubleEquals(127D, $opt$ByteToDouble((byte)127));  // 2^7 - 1
240     assertDoubleEquals(-127D, $opt$ByteToDouble((byte)-127));  // -(2^7 - 1)
241     assertDoubleEquals(-128D, $opt$ByteToDouble((byte)-128));  // -(2^7)
242   }
243 
shortToDouble()244   private static void shortToDouble() {
245     assertDoubleEquals(1D, $opt$ShortToDouble((short)1));
246     assertDoubleEquals(0D, $opt$ShortToDouble((short)0));
247     assertDoubleEquals(-1D, $opt$ShortToDouble((short)-1));
248     assertDoubleEquals(51D, $opt$ShortToDouble((short)51));
249     assertDoubleEquals(-51D, $opt$ShortToDouble((short)-51));
250     assertDoubleEquals(32767D, $opt$ShortToDouble((short)32767));  // 2^15 - 1
251     assertDoubleEquals(-32767D, $opt$ShortToDouble((short)-32767));  // -(2^15 - 1)
252     assertDoubleEquals(-32768D, $opt$ShortToDouble((short)-32768));  // -(2^15)
253   }
254 
intToDouble()255   private static void intToDouble() {
256     assertDoubleEquals(1D, $opt$IntToDouble(1));
257     assertDoubleEquals(0D, $opt$IntToDouble(0));
258     assertDoubleEquals(-1D, $opt$IntToDouble(-1));
259     assertDoubleEquals(51D, $opt$IntToDouble(51));
260     assertDoubleEquals(-51D, $opt$IntToDouble(-51));
261     assertDoubleEquals(16777216D, $opt$IntToDouble(16777216));  // 2^24
262     assertDoubleEquals(-16777216D, $opt$IntToDouble(-16777216));  // -(2^24)
263     assertDoubleEquals(2147483647D, $opt$IntToDouble(2147483647));  // 2^31 - 1
264     assertDoubleEquals(-2147483648D, $opt$IntToDouble(-2147483648));  // -(2^31)
265   }
266 
charToDouble()267   private static void charToDouble() {
268     assertDoubleEquals(1D, $opt$CharToDouble((char)1));
269     assertDoubleEquals(0D, $opt$CharToDouble((char)0));
270     assertDoubleEquals(51D, $opt$CharToDouble((char)51));
271     assertDoubleEquals(32767D, $opt$CharToDouble((char)32767));  // 2^15 - 1
272     assertDoubleEquals(65535D, $opt$CharToDouble((char)65535));  // 2^16 - 1
273     assertDoubleEquals(65535D, $opt$CharToDouble((char)-1));
274     assertDoubleEquals(65485D, $opt$CharToDouble((char)-51));
275     assertDoubleEquals(32769D, $opt$CharToDouble((char)-32767));  // -(2^15 - 1)
276     assertDoubleEquals(32768D, $opt$CharToDouble((char)-32768));  // -(2^15)
277   }
278 
longToInt()279   private static void longToInt() {
280     assertIntEquals(1, $opt$LongToInt(1L));
281     assertIntEquals(0, $opt$LongToInt(0L));
282     assertIntEquals(-1, $opt$LongToInt(-1L));
283     assertIntEquals(51, $opt$LongToInt(51L));
284     assertIntEquals(-51, $opt$LongToInt(-51L));
285     assertIntEquals(2147483647, $opt$LongToInt(2147483647L));  // 2^31 - 1
286     assertIntEquals(-2147483647, $opt$LongToInt(-2147483647L));  // -(2^31 - 1)
287     assertIntEquals(-2147483648, $opt$LongToInt(-2147483648L));  // -(2^31)
288     assertIntEquals(-2147483648, $opt$LongToInt(2147483648L));  // (2^31)
289     assertIntEquals(2147483647, $opt$LongToInt(-2147483649L));  // -(2^31 + 1)
290     assertIntEquals(-1, $opt$LongToInt(9223372036854775807L));  // 2^63 - 1
291     assertIntEquals(1, $opt$LongToInt(-9223372036854775807L));  // -(2^63 - 1)
292     assertIntEquals(0, $opt$LongToInt(-9223372036854775808L));  // -(2^63)
293 
294     assertIntEquals(42, $opt$LongLiteralToInt());
295 
296     // Ensure long-to-int conversions truncates values as expected.
297     assertLongEquals(1L, $opt$IntToLong($opt$LongToInt(4294967297L)));  // 2^32 + 1
298     assertLongEquals(0L, $opt$IntToLong($opt$LongToInt(4294967296L)));  // 2^32
299     assertLongEquals(-1L, $opt$IntToLong($opt$LongToInt(4294967295L)));  // 2^32 - 1
300     assertLongEquals(0L, $opt$IntToLong($opt$LongToInt(0L)));
301     assertLongEquals(1L, $opt$IntToLong($opt$LongToInt(-4294967295L)));  // -(2^32 - 1)
302     assertLongEquals(0L, $opt$IntToLong($opt$LongToInt(-4294967296L)));  // -(2^32)
303     assertLongEquals(-1, $opt$IntToLong($opt$LongToInt(-4294967297L)));  // -(2^32 + 1)
304   }
305 
longToFloat()306   private static void longToFloat() {
307     assertFloatEquals(1F, $opt$LongToFloat(1L));
308     assertFloatEquals(0F, $opt$LongToFloat(0L));
309     assertFloatEquals(-1F, $opt$LongToFloat(-1L));
310     assertFloatEquals(51F, $opt$LongToFloat(51L));
311     assertFloatEquals(-51F, $opt$LongToFloat(-51L));
312     assertFloatEquals(2147483647F, $opt$LongToFloat(2147483647L));  // 2^31 - 1
313     assertFloatEquals(-2147483647F, $opt$LongToFloat(-2147483647L));  // -(2^31 - 1)
314     assertFloatEquals(-2147483648F, $opt$LongToFloat(-2147483648L));  // -(2^31)
315     assertFloatEquals(2147483648F, $opt$LongToFloat(2147483648L));  // (2^31)
316     assertFloatEquals(-2147483649F, $opt$LongToFloat(-2147483649L));  // -(2^31 + 1)
317     assertFloatEquals(4294967296F, $opt$LongToFloat(4294967296L));  // (2^32)
318     assertFloatEquals(-4294967296F, $opt$LongToFloat(-4294967296L));  // -(2^32)
319     assertFloatEquals(140739635871745F, $opt$LongToFloat(140739635871745L));  // 1 + 2^15 + 2^31 + 2^47
320     assertFloatEquals(-140739635871745F, $opt$LongToFloat(-140739635871745L));  // -(1 + 2^15 + 2^31 + 2^47)
321     assertFloatEquals(9223372036854775807F, $opt$LongToFloat(9223372036854775807L));  // 2^63 - 1
322     assertFloatEquals(-9223372036854775807F, $opt$LongToFloat(-9223372036854775807L));  // -(2^63 - 1)
323     assertFloatEquals(-9223372036854775808F, $opt$LongToFloat(-9223372036854775808L));  // -(2^63)
324   }
325 
longToDouble()326   private static void longToDouble() {
327     assertDoubleEquals(1D, $opt$LongToDouble(1L));
328     assertDoubleEquals(0D, $opt$LongToDouble(0L));
329     assertDoubleEquals(-1D, $opt$LongToDouble(-1L));
330     assertDoubleEquals(51D, $opt$LongToDouble(51L));
331     assertDoubleEquals(-51D, $opt$LongToDouble(-51L));
332     assertDoubleEquals(2147483647D, $opt$LongToDouble(2147483647L));  // 2^31 - 1
333     assertDoubleEquals(-2147483647D, $opt$LongToDouble(-2147483647L));  // -(2^31 - 1)
334     assertDoubleEquals(-2147483648D, $opt$LongToDouble(-2147483648L));  // -(2^31)
335     assertDoubleEquals(2147483648D, $opt$LongToDouble(2147483648L));  // (2^31)
336     assertDoubleEquals(-2147483649D, $opt$LongToDouble(-2147483649L));  // -(2^31 + 1)
337     assertDoubleEquals(4294967296D, $opt$LongToDouble(4294967296L));  // (2^32)
338     assertDoubleEquals(-4294967296D, $opt$LongToDouble(-4294967296L));  // -(2^32)
339     assertDoubleEquals(140739635871745D, $opt$LongToDouble(140739635871745L));  // 1 + 2^15 + 2^31 + 2^47
340     assertDoubleEquals(-140739635871745D, $opt$LongToDouble(-140739635871745L));  // -(1 + 2^15 + 2^31 + 2^47)
341     assertDoubleEquals(9223372036854775807D, $opt$LongToDouble(9223372036854775807L));  // 2^63 - 1
342     assertDoubleEquals(-9223372036854775807D, $opt$LongToDouble(-9223372036854775807L));  // -(2^63 - 1)
343     assertDoubleEquals(-9223372036854775808D, $opt$LongToDouble(-9223372036854775808L));  // -(2^63)
344   }
345 
floatToInt()346   private static void floatToInt() {
347     assertIntEquals(1, $opt$FloatToInt(1F));
348     assertIntEquals(0, $opt$FloatToInt(0F));
349     assertIntEquals(0, $opt$FloatToInt(-0F));
350     assertIntEquals(-1, $opt$FloatToInt(-1F));
351     assertIntEquals(51, $opt$FloatToInt(51F));
352     assertIntEquals(-51, $opt$FloatToInt(-51F));
353     assertIntEquals(0, $opt$FloatToInt(0.5F));
354     assertIntEquals(0, $opt$FloatToInt(0.4999999F));
355     assertIntEquals(0, $opt$FloatToInt(-0.4999999F));
356     assertIntEquals(0, $opt$FloatToInt(-0.5F));
357     assertIntEquals(42, $opt$FloatToInt(42.199F));
358     assertIntEquals(-42, $opt$FloatToInt(-42.199F));
359     assertIntEquals(2147483647, $opt$FloatToInt(2147483647F));  // 2^31 - 1
360     assertIntEquals(-2147483648, $opt$FloatToInt(-2147483647F));  // -(2^31 - 1)
361     assertIntEquals(-2147483648, $opt$FloatToInt(-2147483648F));  // -(2^31)
362     assertIntEquals(2147483647, $opt$FloatToInt(2147483648F));  // (2^31)
363     assertIntEquals(-2147483648, $opt$FloatToInt(-2147483649F));  // -(2^31 + 1)
364     assertIntEquals(2147483647, $opt$FloatToInt(9223372036854775807F));  // 2^63 - 1
365     assertIntEquals(-2147483648, $opt$FloatToInt(-9223372036854775807F));  // -(2^63 - 1)
366     assertIntEquals(-2147483648, $opt$FloatToInt(-9223372036854775808F));  // -(2^63)
367     assertIntEquals(0, $opt$FloatToInt(Float.NaN));
368     assertIntEquals(2147483647, $opt$FloatToInt(Float.POSITIVE_INFINITY));
369     assertIntEquals(-2147483648, $opt$FloatToInt(Float.NEGATIVE_INFINITY));
370   }
371 
floatToLong()372   private static void floatToLong() {
373     assertLongEquals(1L, $opt$FloatToLong(1F));
374     assertLongEquals(0L, $opt$FloatToLong(0F));
375     assertLongEquals(0L, $opt$FloatToLong(-0F));
376     assertLongEquals(-1L, $opt$FloatToLong(-1F));
377     assertLongEquals(51L, $opt$FloatToLong(51F));
378     assertLongEquals(-51L, $opt$FloatToLong(-51F));
379     assertLongEquals(0L, $opt$FloatToLong(0.5F));
380     assertLongEquals(0L, $opt$FloatToLong(0.4999999F));
381     assertLongEquals(0L, $opt$FloatToLong(-0.4999999F));
382     assertLongEquals(0L, $opt$FloatToLong(-0.5F));
383     assertLongEquals(42L, $opt$FloatToLong(42.199F));
384     assertLongEquals(-42L, $opt$FloatToLong(-42.199F));
385     assertLongEquals(2147483648L, $opt$FloatToLong(2147483647F));  // 2^31 - 1
386     assertLongEquals(-2147483648L, $opt$FloatToLong(-2147483647F));  // -(2^31 - 1)
387     assertLongEquals(-2147483648L, $opt$FloatToLong(-2147483648F));  // -(2^31)
388     assertLongEquals(2147483648L, $opt$FloatToLong(2147483648F));  // (2^31)
389     assertLongEquals(-2147483648L, $opt$FloatToLong(-2147483649F));  // -(2^31 + 1)
390     assertLongEquals(9223372036854775807L, $opt$FloatToLong(9223372036854775807F));  // 2^63 - 1
391     assertLongEquals(-9223372036854775808L, $opt$FloatToLong(-9223372036854775807F));  // -(2^63 - 1)
392     assertLongEquals(-9223372036854775808L, $opt$FloatToLong(-9223372036854775808F));  // -(2^63)
393     assertLongEquals(0L, $opt$FloatToLong(Float.NaN));
394     assertLongEquals(9223372036854775807L, $opt$FloatToLong(Float.POSITIVE_INFINITY));
395     assertLongEquals(-9223372036854775808L, $opt$FloatToLong(Float.NEGATIVE_INFINITY));
396   }
397 
floatToDouble()398   private static void floatToDouble() {
399     assertDoubleEquals(1D, $opt$FloatToDouble(1F));
400     assertDoubleEquals(0D, $opt$FloatToDouble(0F));
401     assertDoubleEquals(0D, $opt$FloatToDouble(-0F));
402     assertDoubleEquals(-1D, $opt$FloatToDouble(-1F));
403     assertDoubleEquals(51D, $opt$FloatToDouble(51F));
404     assertDoubleEquals(-51D, $opt$FloatToDouble(-51F));
405     assertDoubleEquals(0.5D, $opt$FloatToDouble(0.5F));
406     assertDoubleEquals(0.49999991059303284D, $opt$FloatToDouble(0.4999999F));
407     assertDoubleEquals(-0.49999991059303284D, $opt$FloatToDouble(-0.4999999F));
408     assertDoubleEquals(-0.5D, $opt$FloatToDouble(-0.5F));
409     assertDoubleEquals(42.19900131225586D, $opt$FloatToDouble(42.199F));
410     assertDoubleEquals(-42.19900131225586D, $opt$FloatToDouble(-42.199F));
411     assertDoubleEquals(2147483648D, $opt$FloatToDouble(2147483647F));  // 2^31 - 1
412     assertDoubleEquals(-2147483648D, $opt$FloatToDouble(-2147483647F));  // -(2^31 - 1)
413     assertDoubleEquals(-2147483648D, $opt$FloatToDouble(-2147483648F));  // -(2^31)
414     assertDoubleEquals(2147483648D, $opt$FloatToDouble(2147483648F));  // (2^31)
415     assertDoubleEquals(-2147483648D, $opt$FloatToDouble(-2147483649F));  // -(2^31 + 1)
416     assertDoubleEquals(9223372036854775807D, $opt$FloatToDouble(9223372036854775807F));  // 2^63 - 1
417     assertDoubleEquals(-9223372036854775807D, $opt$FloatToDouble(-9223372036854775807F));  // -(2^63 - 1)
418     assertDoubleEquals(-9223372036854775808D, $opt$FloatToDouble(-9223372036854775808F));  // -(2^63)
419     assertDoubleIsNaN($opt$FloatToDouble(Float.NaN));
420     assertDoubleEquals(Double.POSITIVE_INFINITY, $opt$FloatToDouble(Float.POSITIVE_INFINITY));
421     assertDoubleEquals(Double.NEGATIVE_INFINITY, $opt$FloatToDouble(Float.NEGATIVE_INFINITY));
422   }
423 
doubleToInt()424   private static void doubleToInt() {
425     assertIntEquals(1, $opt$DoubleToInt(1D));
426     assertIntEquals(0, $opt$DoubleToInt(0D));
427     assertIntEquals(0, $opt$DoubleToInt(-0D));
428     assertIntEquals(-1, $opt$DoubleToInt(-1D));
429     assertIntEquals(51, $opt$DoubleToInt(51D));
430     assertIntEquals(-51, $opt$DoubleToInt(-51D));
431     assertIntEquals(0, $opt$DoubleToInt(0.5D));
432     assertIntEquals(0, $opt$DoubleToInt(0.4999999D));
433     assertIntEquals(0, $opt$DoubleToInt(-0.4999999D));
434     assertIntEquals(0, $opt$DoubleToInt(-0.5D));
435     assertIntEquals(42, $opt$DoubleToInt(42.199D));
436     assertIntEquals(-42, $opt$DoubleToInt(-42.199D));
437     assertIntEquals(2147483647, $opt$DoubleToInt(2147483647D));  // 2^31 - 1
438     assertIntEquals(-2147483647, $opt$DoubleToInt(-2147483647D));  // -(2^31 - 1)
439     assertIntEquals(-2147483648, $opt$DoubleToInt(-2147483648D));  // -(2^31)
440     assertIntEquals(2147483647, $opt$DoubleToInt(2147483648D));  // (2^31)
441     assertIntEquals(-2147483648, $opt$DoubleToInt(-2147483649D));  // -(2^31 + 1)
442     assertIntEquals(2147483647, $opt$DoubleToInt(9223372036854775807D));  // 2^63 - 1
443     assertIntEquals(-2147483648, $opt$DoubleToInt(-9223372036854775807D));  // -(2^63 - 1)
444     assertIntEquals(-2147483648, $opt$DoubleToInt(-9223372036854775808D));  // -(2^63)
445     assertIntEquals(0, $opt$DoubleToInt(Double.NaN));
446     assertIntEquals(2147483647, $opt$DoubleToInt(Double.POSITIVE_INFINITY));
447     assertIntEquals(-2147483648, $opt$DoubleToInt(Double.NEGATIVE_INFINITY));
448   }
449 
doubleToLong()450   private static void doubleToLong() {
451     assertLongEquals(1L, $opt$DoubleToLong(1D));
452     assertLongEquals(0L, $opt$DoubleToLong(0D));
453     assertLongEquals(0L, $opt$DoubleToLong(-0D));
454     assertLongEquals(-1L, $opt$DoubleToLong(-1D));
455     assertLongEquals(51L, $opt$DoubleToLong(51D));
456     assertLongEquals(-51L, $opt$DoubleToLong(-51D));
457     assertLongEquals(0L, $opt$DoubleToLong(0.5D));
458     assertLongEquals(0L, $opt$DoubleToLong(0.4999999D));
459     assertLongEquals(0L, $opt$DoubleToLong(-0.4999999D));
460     assertLongEquals(0L, $opt$DoubleToLong(-0.5D));
461     assertLongEquals(42L, $opt$DoubleToLong(42.199D));
462     assertLongEquals(-42L, $opt$DoubleToLong(-42.199D));
463     assertLongEquals(2147483647L, $opt$DoubleToLong(2147483647D));  // 2^31 - 1
464     assertLongEquals(-2147483647L, $opt$DoubleToLong(-2147483647D));  // -(2^31 - 1)
465     assertLongEquals(-2147483648L, $opt$DoubleToLong(-2147483648D));  // -(2^31)
466     assertLongEquals(2147483648L, $opt$DoubleToLong(2147483648D));  // (2^31)
467     assertLongEquals(-2147483649L, $opt$DoubleToLong(-2147483649D));  // -(2^31 + 1)
468     assertLongEquals(9223372036854775807L, $opt$DoubleToLong(9223372036854775807D));  // 2^63 - 1
469     assertLongEquals(-9223372036854775808L, $opt$DoubleToLong(-9223372036854775807D));  // -(2^63 - 1)
470     assertLongEquals(-9223372036854775808L, $opt$DoubleToLong(-9223372036854775808D));  // -(2^63)
471     assertLongEquals(0L, $opt$DoubleToLong(Double.NaN));
472     assertLongEquals(9223372036854775807L, $opt$DoubleToLong(Double.POSITIVE_INFINITY));
473     assertLongEquals(-9223372036854775808L, $opt$DoubleToLong(Double.NEGATIVE_INFINITY));
474   }
475 
doubleToFloat()476   private static void doubleToFloat() {
477     assertFloatEquals(1F, $opt$DoubleToFloat(1D));
478     assertFloatEquals(0F, $opt$DoubleToFloat(0D));
479     assertFloatEquals(0F, $opt$DoubleToFloat(-0D));
480     assertFloatEquals(-1F, $opt$DoubleToFloat(-1D));
481     assertFloatEquals(51F, $opt$DoubleToFloat(51D));
482     assertFloatEquals(-51F, $opt$DoubleToFloat(-51D));
483     assertFloatEquals(0.5F, $opt$DoubleToFloat(0.5D));
484     assertFloatEquals(0.4999999F, $opt$DoubleToFloat(0.4999999D));
485     assertFloatEquals(-0.4999999F, $opt$DoubleToFloat(-0.4999999D));
486     assertFloatEquals(-0.5F, $opt$DoubleToFloat(-0.5D));
487     assertFloatEquals(42.199F, $opt$DoubleToFloat(42.199D));
488     assertFloatEquals(-42.199F, $opt$DoubleToFloat(-42.199D));
489     assertFloatEquals(2147483648F, $opt$DoubleToFloat(2147483647D));  // 2^31 - 1
490     assertFloatEquals(-2147483648F, $opt$DoubleToFloat(-2147483647D));  // -(2^31 - 1)
491     assertFloatEquals(-2147483648F, $opt$DoubleToFloat(-2147483648D));  // -(2^31)
492     assertFloatEquals(2147483648F, $opt$DoubleToFloat(2147483648D));  // (2^31)
493     assertFloatEquals(-2147483648F, $opt$DoubleToFloat(-2147483649D));  // -(2^31 + 1)
494     assertFloatEquals(9223372036854775807F, $opt$DoubleToFloat(9223372036854775807D));  // 2^63 - 1
495     assertFloatEquals(-9223372036854775807F, $opt$DoubleToFloat(-9223372036854775807D));  // -(2^63 - 1)
496     assertFloatEquals(-9223372036854775808F, $opt$DoubleToFloat(-9223372036854775808D));  // -(2^63)
497     assertFloatIsNaN($opt$DoubleToFloat(Float.NaN));
498     assertFloatEquals(Float.POSITIVE_INFINITY, $opt$DoubleToFloat(Double.POSITIVE_INFINITY));
499     assertFloatEquals(Float.NEGATIVE_INFINITY, $opt$DoubleToFloat(Double.NEGATIVE_INFINITY));
500   }
501 
shortToByte()502   private static void shortToByte() {
503     assertByteEquals((byte)1, $opt$ShortToByte((short)1));
504     assertByteEquals((byte)0, $opt$ShortToByte((short)0));
505     assertByteEquals((byte)-1, $opt$ShortToByte((short)-1));
506     assertByteEquals((byte)51, $opt$ShortToByte((short)51));
507     assertByteEquals((byte)-51, $opt$ShortToByte((short)-51));
508     assertByteEquals((byte)127, $opt$ShortToByte((short)127));  // 2^7 - 1
509     assertByteEquals((byte)-127, $opt$ShortToByte((short)-127));  // -(2^7 - 1)
510     assertByteEquals((byte)-128, $opt$ShortToByte((short)-128));  // -(2^7)
511     assertByteEquals((byte)-128, $opt$ShortToByte((short)128));  // 2^7
512     assertByteEquals((byte)127, $opt$ShortToByte((short)-129));  // -(2^7 + 1)
513     assertByteEquals((byte)-1, $opt$ShortToByte((short)32767));  // 2^15 - 1
514     assertByteEquals((byte)0, $opt$ShortToByte((short)-32768));  // -(2^15)
515   }
516 
intToByte()517   private static void intToByte() {
518     assertByteEquals((byte)1, $opt$IntToByte(1));
519     assertByteEquals((byte)0, $opt$IntToByte(0));
520     assertByteEquals((byte)-1, $opt$IntToByte(-1));
521     assertByteEquals((byte)51, $opt$IntToByte(51));
522     assertByteEquals((byte)-51, $opt$IntToByte(-51));
523     assertByteEquals((byte)127, $opt$IntToByte(127));  // 2^7 - 1
524     assertByteEquals((byte)-127, $opt$IntToByte(-127));  // -(2^7 - 1)
525     assertByteEquals((byte)-128, $opt$IntToByte(-128));  // -(2^7)
526     assertByteEquals((byte)-128, $opt$IntToByte(128));  // 2^7
527     assertByteEquals((byte)127, $opt$IntToByte(-129));  // -(2^7 + 1)
528     assertByteEquals((byte)-1, $opt$IntToByte(2147483647));  // 2^31 - 1
529     assertByteEquals((byte)0, $opt$IntToByte(-2147483648));  // -(2^31)
530   }
531 
charToByte()532   private static void charToByte() {
533     assertByteEquals((byte)1, $opt$CharToByte((char)1));
534     assertByteEquals((byte)0, $opt$CharToByte((char)0));
535     assertByteEquals((byte)51, $opt$CharToByte((char)51));
536     assertByteEquals((byte)127, $opt$CharToByte((char)127));  // 2^7 - 1
537     assertByteEquals((byte)-128, $opt$CharToByte((char)128));  // 2^7
538     assertByteEquals((byte)-1, $opt$CharToByte((char)32767));  // 2^15 - 1
539     assertByteEquals((byte)-1, $opt$CharToByte((char)65535));  // 2^16 - 1
540     assertByteEquals((byte)-1, $opt$CharToByte((char)-1));
541     assertByteEquals((byte)-51, $opt$CharToByte((char)-51));
542     assertByteEquals((byte)-127, $opt$CharToByte((char)-127));  // -(2^7 - 1)
543     assertByteEquals((byte)-128, $opt$CharToByte((char)-128));  // -(2^7)
544     assertByteEquals((byte)127, $opt$CharToByte((char)-129));  // -(2^7 + 1)
545   }
546 
byteToShort()547   private static void byteToShort() {
548     assertShortEquals((short)1, $opt$ByteToShort((byte)1));
549     assertShortEquals((short)0, $opt$ByteToShort((byte)0));
550     assertShortEquals((short)-1, $opt$ByteToShort((byte)-1));
551     assertShortEquals((short)51, $opt$ByteToShort((byte)51));
552     assertShortEquals((short)-51, $opt$ByteToShort((byte)-51));
553     assertShortEquals((short)127, $opt$ByteToShort((byte)127));  // 2^7 - 1
554     assertShortEquals((short)-127, $opt$ByteToShort((byte)-127));  // -(2^7 - 1)
555     assertShortEquals((short)-128, $opt$ByteToShort((byte)-128));  // -(2^7)
556   }
557 
intToShort()558   private static void intToShort() {
559     assertShortEquals((short)1, $opt$IntToShort(1));
560     assertShortEquals((short)0, $opt$IntToShort(0));
561     assertShortEquals((short)-1, $opt$IntToShort(-1));
562     assertShortEquals((short)51, $opt$IntToShort(51));
563     assertShortEquals((short)-51, $opt$IntToShort(-51));
564     assertShortEquals((short)32767, $opt$IntToShort(32767));  // 2^15 - 1
565     assertShortEquals((short)-32767, $opt$IntToShort(-32767));  // -(2^15 - 1)
566     assertShortEquals((short)-32768, $opt$IntToShort(-32768));  // -(2^15)
567     assertShortEquals((short)-32768, $opt$IntToShort(32768));  // 2^15
568     assertShortEquals((short)32767, $opt$IntToShort(-32769));  // -(2^15 + 1)
569     assertShortEquals((short)-1, $opt$IntToShort(2147483647));  // 2^31 - 1
570     assertShortEquals((short)0, $opt$IntToShort(-2147483648));  // -(2^31)
571   }
572 
charToShort()573   private static void charToShort() {
574     assertShortEquals((short)1, $opt$CharToShort((char)1));
575     assertShortEquals((short)0, $opt$CharToShort((char)0));
576     assertShortEquals((short)51, $opt$CharToShort((char)51));
577     assertShortEquals((short)32767, $opt$CharToShort((char)32767));  // 2^15 - 1
578     assertShortEquals((short)-32768, $opt$CharToShort((char)32768));  // 2^15
579     assertShortEquals((short)-32767, $opt$CharToShort((char)32769));  // 2^15
580     assertShortEquals((short)-1, $opt$CharToShort((char)65535));  // 2^16 - 1
581     assertShortEquals((short)-1, $opt$CharToShort((char)-1));
582     assertShortEquals((short)-51, $opt$CharToShort((char)-51));
583     assertShortEquals((short)-32767, $opt$CharToShort((char)-32767));  // -(2^15 - 1)
584     assertShortEquals((short)-32768, $opt$CharToShort((char)-32768));  // -(2^15)
585     assertShortEquals((short)32767, $opt$CharToShort((char)-32769));  // -(2^15 + 1)
586   }
587 
byteToChar()588   private static void byteToChar() {
589     assertCharEquals((char)1, $opt$ByteToChar((byte)1));
590     assertCharEquals((char)0, $opt$ByteToChar((byte)0));
591     assertCharEquals((char)65535, $opt$ByteToChar((byte)-1));
592     assertCharEquals((char)51, $opt$ByteToChar((byte)51));
593     assertCharEquals((char)65485, $opt$ByteToChar((byte)-51));
594     assertCharEquals((char)127, $opt$ByteToChar((byte)127));  // 2^7 - 1
595     assertCharEquals((char)65409, $opt$ByteToChar((byte)-127));  // -(2^7 - 1)
596     assertCharEquals((char)65408, $opt$ByteToChar((byte)-128));  // -(2^7)
597   }
598 
shortToChar()599   private static void shortToChar() {
600     assertCharEquals((char)1, $opt$ShortToChar((short)1));
601     assertCharEquals((char)0, $opt$ShortToChar((short)0));
602     assertCharEquals((char)65535, $opt$ShortToChar((short)-1));
603     assertCharEquals((char)51, $opt$ShortToChar((short)51));
604     assertCharEquals((char)65485, $opt$ShortToChar((short)-51));
605     assertCharEquals((char)32767, $opt$ShortToChar((short)32767));  // 2^15 - 1
606     assertCharEquals((char)32769, $opt$ShortToChar((short)-32767));  // -(2^15 - 1)
607     assertCharEquals((char)32768, $opt$ShortToChar((short)-32768));  // -(2^15)
608   }
609 
intToChar()610   private static void intToChar() {
611     assertCharEquals((char)1, $opt$IntToChar(1));
612     assertCharEquals((char)0, $opt$IntToChar(0));
613     assertCharEquals((char)65535, $opt$IntToChar(-1));
614     assertCharEquals((char)51, $opt$IntToChar(51));
615     assertCharEquals((char)65485, $opt$IntToChar(-51));
616     assertCharEquals((char)32767, $opt$IntToChar(32767));  // 2^15 - 1
617     assertCharEquals((char)32769, $opt$IntToChar(-32767));  // -(2^15 - 1)
618     assertCharEquals((char)32768, $opt$IntToChar(32768));  // 2^15
619     assertCharEquals((char)32768, $opt$IntToChar(-32768));  // -(2^15)
620     assertCharEquals((char)65535, $opt$IntToChar(65535));  // 2^16 - 1
621     assertCharEquals((char)1, $opt$IntToChar(-65535));  // -(2^16 - 1)
622     assertCharEquals((char)0, $opt$IntToChar(65536));  // 2^16
623     assertCharEquals((char)0, $opt$IntToChar(-65536));  // -(2^16)
624     assertCharEquals((char)65535, $opt$IntToChar(2147483647));  // 2^31 - 1
625     assertCharEquals((char)0, $opt$IntToChar(-2147483648));  // -(2^31)
626   }
627 
628 
629   // These methods produce int-to-long Dex instructions.
$opt$ByteToLong(byte a)630   static long $opt$ByteToLong(byte a) { return (long)a; }
$opt$ShortToLong(short a)631   static long $opt$ShortToLong(short a) { return (long)a; }
$opt$IntToLong(int a)632   static long $opt$IntToLong(int a) { return (long)a; }
$opt$CharToLong(int a)633   static long $opt$CharToLong(int a) { return (long)a; }
634 
635   // These methods produce int-to-float Dex instructions.
$opt$ByteToFloat(byte a)636   static float $opt$ByteToFloat(byte a) { return (float)a; }
$opt$ShortToFloat(short a)637   static float $opt$ShortToFloat(short a) { return (float)a; }
$opt$IntToFloat(int a)638   static float $opt$IntToFloat(int a) { return (float)a; }
$opt$CharToFloat(char a)639   static float $opt$CharToFloat(char a) { return (float)a; }
640 
641   // These methods produce int-to-double Dex instructions.
$opt$ByteToDouble(byte a)642   static double $opt$ByteToDouble(byte a) { return (double)a; }
$opt$ShortToDouble(short a)643   static double $opt$ShortToDouble(short a) { return (double)a; }
$opt$IntToDouble(int a)644   static double $opt$IntToDouble(int a) { return (double)a; }
$opt$CharToDouble(int a)645   static double $opt$CharToDouble(int a) { return (double)a; }
646 
647   // These methods produce long-to-int Dex instructions.
$opt$LongToInt(long a)648   static int $opt$LongToInt(long a) { return (int)a; }
$opt$LongLiteralToInt()649   static int $opt$LongLiteralToInt() { return (int)42L; }
650 
651   // This method produces a long-to-float Dex instruction.
$opt$LongToFloat(long a)652   static float $opt$LongToFloat(long a) { return (float)a; }
653 
654   // This method produces a long-to-double Dex instruction.
$opt$LongToDouble(long a)655   static double $opt$LongToDouble(long a) { return (double)a; }
656 
657   // This method produces a float-to-int Dex instruction.
$opt$FloatToInt(float a)658   static int $opt$FloatToInt(float a) { return (int)a; }
659 
660   // This method produces a float-to-long Dex instruction.
$opt$FloatToLong(float a)661   static long $opt$FloatToLong(float a){ return (long)a; }
662 
663   // This method produces a float-to-double Dex instruction.
$opt$FloatToDouble(float a)664   static double $opt$FloatToDouble(float a) { return (double)a; }
665 
666   // This method produces a double-to-int Dex instruction.
$opt$DoubleToInt(double a)667   static int $opt$DoubleToInt(double a){ return (int)a; }
668 
669   // This method produces a double-to-long Dex instruction.
$opt$DoubleToLong(double a)670   static long $opt$DoubleToLong(double a){ return (long)a; }
671 
672   // This method produces a double-to-float Dex instruction.
$opt$DoubleToFloat(double a)673   static float $opt$DoubleToFloat(double a) { return (float)a; }
674 
675   // These methods produce int-to-byte Dex instructions.
$opt$ShortToByte(short a)676   static byte $opt$ShortToByte(short a) { return (byte)a; }
$opt$IntToByte(int a)677   static byte $opt$IntToByte(int a) { return (byte)a; }
$opt$CharToByte(char a)678   static byte $opt$CharToByte(char a) { return (byte)a; }
679 
680   // These methods produce int-to-short Dex instructions.
$opt$ByteToShort(byte a)681   static short $opt$ByteToShort(byte a) { return (short)a; }
$opt$IntToShort(int a)682   static short $opt$IntToShort(int a) { return (short)a; }
$opt$CharToShort(char a)683   static short $opt$CharToShort(char a) { return (short)a; }
684 
685   // These methods produce int-to-char Dex instructions.
$opt$ByteToChar(byte a)686   static char $opt$ByteToChar(byte a) { return (char)a; }
$opt$ShortToChar(short a)687   static char $opt$ShortToChar(short a) { return (char)a; }
$opt$IntToChar(int a)688   static char $opt$IntToChar(int a) { return (char)a; }
689 }
690