JS: use optimized emulation of int32 multiplication. Add test for emulated int32 multiplication

This commit is contained in:
Alexey Andreev
2016-12-20 12:50:37 +03:00
parent 7d8df5c81c
commit d3d406356c
3 changed files with 45 additions and 7 deletions
@@ -6065,6 +6065,12 @@ public class BoxJsTestGenerated extends AbstractBoxJsTest {
doTest(fileName);
}
@TestMetadata("mulInt32.kt")
public void testMulInt32() throws Exception {
String fileName = KotlinTestUtils.navigationMetadata("js/js.translator/testData/box/number/mulInt32.kt");
doTest(fileName);
}
@TestMetadata("numberCompareTo.kt")
public void testNumberCompareTo() throws Exception {
String fileName = KotlinTestUtils.navigationMetadata("js/js.translator/testData/box/number/numberCompareTo.kt");
+32
View File
@@ -0,0 +1,32 @@
fun imul32(a: Int, b: Int): Int = js("Kotlin").imulEmulated(a, b)
fun imul64(a: Int, b: Int): Int = (a.toLong() * b.toLong()).toInt()
fun generateSeries(data: List<Int>): List<Pair<Int, Int>> {
val result = mutableListOf<Pair<Int, Int>>()
for (i in data.indices) {
for (j in i..data.lastIndex) {
result += Pair(data[i], data[j])
}
}
return result
}
fun test(series: List<Pair<Int, Int>>): String? {
for ((a, b) in series) {
(testSingle(a, b) ?: testSingle(a, -b) ?: testSingle(-a, b) ?: testSingle(-a, -b))?.let { return it }
}
return null
}
fun testSingle(a: Int, b: Int): String? {
var actual = imul32(a, b)
var expected = imul64(a, b)
return if (actual != expected) "$a * $b = $expected, got $actual" else null
}
fun box(): String {
val error = test(generateSeries(listOf(3, 0x1234, 0xFFFF, 0xABCD, 0xABCDEF, 0x43211234, 0x7FFFFFFC, 0x7FFFFFFF)))
if (error != null) return "fail: error"
return "OK"
}
+7 -7
View File
@@ -313,11 +313,11 @@
Kotlin.kotlinModuleMetadata = function (abiVersion, moduleName, data) {
};
Kotlin.imul = Math.imul || function (a, b) {
var ah = (a >>> 16) & 0xffff;
var al = a & 0xffff;
var bh = (b >>> 16) & 0xffff;
var bl = b & 0xffff;
return ((al * bl) + (((ah * bl + al * bh) << 16) >>> 0) | 0);
};
Kotlin.imul = Math.imul || imul;
Kotlin.imulEmulated = imul;
function imul(a, b) {
return ((a & 0xffff0000) * (b & 0xffff) + (a & 0xffff) * (b | 0)) | 0;
}
})();