diff --git a/kmath-ast/src/jvmMain/kotlin/scientifik/kmath/asm/AsmGenerationContext.kt b/kmath-ast/src/jvmMain/kotlin/scientifik/kmath/asm/AsmGenerationContext.kt index c10e56ba8..cc7874e37 100644 --- a/kmath-ast/src/jvmMain/kotlin/scientifik/kmath/asm/AsmGenerationContext.kt +++ b/kmath-ast/src/jvmMain/kotlin/scientifik/kmath/asm/AsmGenerationContext.kt @@ -17,7 +17,7 @@ import scientifik.kmath.operations.Algebra * @param className the unique class name of new loaded class. */ class AsmGenerationContext( - classOfT: Class<*>, + private val classOfT: Class<*>, private val algebra: Algebra, private val className: String ) { @@ -186,7 +186,15 @@ class AsmGenerationContext( return new } - internal fun visitLoadFromConstants(value: T) = visitLoadAnyFromConstants(value as Any, T_CLASS) + internal fun visitLoadFromConstants(value: T) { + if (classOfT in INLINABLE_NUMBERS) { + visitNumberConstant(value as Number) + visitCastToT() + return + } + + visitLoadAnyFromConstants(value as Any, T_CLASS) + } private fun visitLoadAnyFromConstants(value: Any, type: String) { val idx = if (value in constants) constants.indexOf(value) else constants.apply { add(value) }.lastIndex @@ -207,7 +215,6 @@ class AsmGenerationContext( maxStack++ val clazz = value.javaClass val c = clazz.name.replace('.', '/') - val sigLetter = SIGNATURE_LETTERS[clazz] if (sigLetter != null) { @@ -284,14 +291,18 @@ class AsmGenerationContext( } internal companion object { - private val SIGNATURE_LETTERS = mapOf( - java.lang.Byte::class.java to "B", - java.lang.Short::class.java to "S", - java.lang.Integer::class.java to "I", - java.lang.Long::class.java to "J", - java.lang.Float::class.java to "F", - java.lang.Double::class.java to "D" - ) + private val SIGNATURE_LETTERS by lazy { + mapOf( + java.lang.Byte::class.java to "B", + java.lang.Short::class.java to "S", + java.lang.Integer::class.java to "I", + java.lang.Long::class.java to "J", + java.lang.Float::class.java to "F", + java.lang.Double::class.java to "D" + ) + } + + private val INLINABLE_NUMBERS by lazy { SIGNATURE_LETTERS.keys } internal const val FUNCTIONAL_COMPILED_EXPRESSION_CLASS = "scientifik/kmath/asm/FunctionalCompiledExpression" diff --git a/kmath-ast/src/jvmMain/kotlin/scientifik/kmath/asm/Optimization.kt b/kmath-ast/src/jvmMain/kotlin/scientifik/kmath/asm/Optimization.kt index 3198fa8ec..51048c77c 100644 --- a/kmath-ast/src/jvmMain/kotlin/scientifik/kmath/asm/Optimization.kt +++ b/kmath-ast/src/jvmMain/kotlin/scientifik/kmath/asm/Optimization.kt @@ -2,6 +2,7 @@ package scientifik.kmath.asm import org.objectweb.asm.Opcodes import scientifik.kmath.operations.Algebra +import scientifik.kmath.operations.ByteRing private val methodNameAdapters: Map = mapOf("+" to "add", "*" to "multiply", "/" to "divide")