SwitchOptimization: generating `lookupswitch' opcode for 'when' in case of integral types

This commit is contained in:
Denis Zharkov
2014-04-05 16:26:01 +04:00
committed by Evgeny Gerashchenko
parent c73533d214
commit 1952781e4b
2 changed files with 50 additions and 34 deletions
@@ -1,3 +1,4 @@
fun foo1(x : Int) : Int {
return when (x % 10) {
1,2,3 -> 1
@@ -7,43 +8,44 @@ fun foo1(x : Int) : Int {
}
}
fun foo2(x : Int) : Int {
when (x % 10) {
1,2,3 -> return 1
4,5,6 -> return 2
7,8,9 -> return 3
fun foo2(x : Short) : Int {
when ((x % 10).toShort()) {
1.toShort(),2.toShort(),3.toShort() -> return 1
4.toShort(),5.toShort(),6.toShort() -> return 2
7.toShort(),8.toShort(),9.toShort() -> return 3
}
return 4
}
fun foo3(x : Int) : Int {
when (x % 10) {
1 -> return 1
2 -> return 1
3 -> return 1
4 -> return 2
5 -> return 2
6 -> return 2
7 -> return 3
8 -> return 3
9 -> return 3
fun foo3(x : Char) : Int {
when (x) {
'1' -> return 1
'2' -> return 1
'3' -> return 1
'4' -> return 2
'5' -> return 2
'6' -> return 2
'7' -> return 3
'8' -> return 3
'9' -> return 3
}
return 4
}
fun foo4(x : Int) : Int {
return when (x % 10) {
1 -> 1
2 -> 1
3 -> 1
4 -> 2
5 -> 2
6 -> 2
7 -> 3
8 -> 3
9 -> 3
fun foo4(x : Byte) : Int {
return when ((x % 10).toByte()) {
1.toByte() -> 1
2.toByte() -> 1
3.toByte() -> 1
4.toByte() -> 2
5.toByte() -> 2
6.toByte() -> 2
7.toByte() -> 3
8.toByte() -> 3
9.toByte() -> 3
else -> 4
}
}
@@ -66,9 +68,9 @@ fun test(foo : (Int) -> Int, name : String) : String {
fun box(): String {
val testResult = test({x -> foo1(x)}, "foo1") +
test({x -> foo2(x)}, "foo2") +
test({x -> foo3(x)}, "foo3") +
test({x -> foo4(x)}, "foo4")
test({x -> foo2(x.toShort())}, "foo2") +
test({x -> foo3((x % 10 + '0'.toInt()).toChar())}, "foo3") +
test({x -> foo4(x.toByte())}, "foo4")
if (testResult != "") return testResult
return "OK"
}