Move specific optimization functions to Optimization

This commit is contained in:
Iaroslav 2020-06-13 15:53:25 +07:00
parent 1582fde091
commit 834d1e1397
No known key found for this signature in database
GPG Key ID: 46E15E4A31B3BCD7
2 changed files with 35 additions and 31 deletions

View File

@ -11,37 +11,6 @@ interface AsmExpression<T> {
fun invoke(gen: AsmGenerationContext<T>)
}
private val methodNameAdapters: Map<String, String> = mapOf("+" to "add", "*" to "multiply", "/" to "divide")
internal fun <T> hasSpecific(context: Algebra<T>, name: String, arity: Int): Boolean {
val aName = methodNameAdapters[name] ?: name
context::class.memberFunctions.find { it.name == aName && it.parameters.size == arity }
?: return false
return true
}
internal fun <T> AsmGenerationContext<T>.tryInvokeSpecific(context: Algebra<T>, name: String, arity: Int): Boolean {
val aName = methodNameAdapters[name] ?: name
context::class.memberFunctions.find { it.name == aName && it.parameters.size == arity }
?: return false
val owner = context::class.jvmName.replace('.', '/')
val sig = buildString {
append('(')
repeat(arity) { append("L${AsmGenerationContext.OBJECT_CLASS};") }
append(')')
append("L${AsmGenerationContext.OBJECT_CLASS};")
}
visitAlgebraOperation(owner = owner, method = aName, descriptor = sig)
return true
}
internal class AsmUnaryOperation<T>(private val context: Algebra<T>, private val name: String, expr: AsmExpression<T>) :
AsmExpression<T> {
private val expr: AsmExpression<T> = expr.optimize()

View File

@ -1,5 +1,40 @@
package scientifik.kmath.asm
import scientifik.kmath.operations.Algebra
import kotlin.reflect.full.memberFunctions
import kotlin.reflect.jvm.jvmName
private val methodNameAdapters: Map<String, String> = mapOf("+" to "add", "*" to "multiply", "/" to "divide")
internal fun <T> hasSpecific(context: Algebra<T>, name: String, arity: Int): Boolean {
val aName = methodNameAdapters[name] ?: name
context::class.memberFunctions.find { it.name == aName && it.parameters.size == arity }
?: return false
return true
}
internal fun <T> AsmGenerationContext<T>.tryInvokeSpecific(context: Algebra<T>, name: String, arity: Int): Boolean {
val aName = methodNameAdapters[name] ?: name
context::class.memberFunctions.find { it.name == aName && it.parameters.size == arity }
?: return false
val owner = context::class.jvmName.replace('.', '/')
val sig = buildString {
append('(')
repeat(arity) { append("L${AsmGenerationContext.OBJECT_CLASS};") }
append(')')
append("L${AsmGenerationContext.OBJECT_CLASS};")
}
visitAlgebraOperation(owner = owner, method = aName, descriptor = sig)
return true
}
@PublishedApi
internal fun <T> AsmExpression<T>.optimize(): AsmExpression<T> {
val a = tryEvaluate()