Here's a quick sneak-peek on a hobby project I've ...
# random
y
Here's a quick sneak-peek on a hobby project I've been working on just for the fun of it. This is 4 different ways to do multiplication using add and shift, all in a DSL I'm working on that generates ARM code out of Dsl calls:
Copy code
fun ArmBuilder.multiply2(
    first: ArmTransferableValueOperand, second: ArmTransferableValueOperand
): ArmCalculationOperand = buildSubroutine("multiply2", first, second) {
    var operand1 by register(first)
    var operand2 by register(second)
    operand1.compare(operand2, "optimizeSmallerValue") {
        greaterThan("swapValues") {
            swap(operand1 as ArmStorageOperand, operand2 as ArmStorageOperand)
        }
    }
    result = constant(0)
    val mainLoop by labeled
    val mainLoopEnd by labeled
    label(mainLoop)
    operand1.compare(constant(0), "rangeCheck") {
        lessThan = mainLoopEnd
        equal = lessThan
    }
    If(operand1 and constant(1), "oddCheck") {
        result += operand2
    }
    operand2 = operand2 shl constant(1)
    operand1 = operand1 shr constant(1)
    branch(mainLoop)
    label(mainLoopEnd)
}

fun ArmBuilder.multiply3(
    first: ArmTransferableValueOperand, second: ArmTransferableValueOperand
): ArmCalculationOperand = buildSubroutine("multiply3", first, second) {
    var operand1 by register(first)
    var operand2 by register(second)
    If(operand1 greaterThan operand2, "swapValues") {
        swap(operand1 as ArmStorageOperand, operand2 as ArmStorageOperand)
    }
    result = constant(0)
    val mainLoop by labeled
    val mainLoopEnd by labeled
    label(mainLoop)
    If(operand1 lessThanOrEqual constant(0), "rangeCheck") {
        branch(mainLoopEnd)
    } Else If(operand1 and constant(1), "oddCheck") {
        result += operand2
    }
    operand2 = operand2 shl constant(1)
    operand1 = operand1 shr constant(1)
    branch(mainLoop)
    label(mainLoopEnd)
}

fun ArmBuilder.multiply4(
    first: ArmTransferableValueOperand, second: ArmTransferableValueOperand
): ArmCalculationOperand = buildSubroutine("multiply4", first, second) {
    var operand1 by register(first)
    var operand2 by register(second)
    If(operand1 `>` operand2, "swapValues") {
        operand1.out `<->` operand2.out
    }
    result = 0.c
    While(operand1 `>` 0.c, "mainLoop") {
        If(operand1 `&` 1.c, "oddCheck") {
            result += operand2
        }
        operand2 = operand2 `<<` 1.c
        operand1 = operand1 `>>` 1.c
    }
}

fun ArmBuilder.multiply5(
    first: ArmTransferableValueOperand, second: ArmTransferableValueOperand
): ArmCalculationOperand = buildSubroutine("multiply4", first, second) {
    val operand2 = register(second)
    result = 0.c
    For(first, { it `>` 0.c }, { it `>>=` 1.c; operand2 `<<=` 1.c }, "mainLoop") { operand1 ->
        If(operand1 `&` 1.c, "oddCheck") {
            result += operand2
        }
    }
}
(Yes some of these look horrendous, but it's all meant just to show off the current features with a trivial example)
For school for my final project, I've decided instead of doing the sensible thing and using python like everyone else, instead I'm going to use ARM on a Raspberry PI just for the sake of exploring an area I'm very unfamiliar with.