Shop Learn
Hardware multiplier access in SPIN2 language — Parallax Forums

Hardware multiplier access in SPIN2 language

Is there a way to make use of the P2 hardware's MUL or MULS instructions in SPIN2 or does it always have to use the CORDIC engine for this?

I've found cases where I know the items being multiplied are both 16 bit quantities and it would be nice to be able to get a fast 2 cycle multiply done instead of the slower / general purpose CORDIC operation. How is this achievable? If it currently is not part of the language could it be extended to support it or is that too hard to fit in these days?

If it needs to be coded in inline PASM is there so much overhead to setting up the inline PASM vs using the CORDIC that it wouldn't be worth it?

Comments

  • pik33pik33 Posts: 1,014
    edited 2021-04-03 19:25

    Yes, I saw what the compiler (Flexspin) did with my functions. They may be 20xfaster. The temporary solutions may be

    • try and goto C (and hope if 16-bit int declared, no cordic is used)
    • make all function in inline PASM so the register saving, etc overhead stuff will be done once for all the function, and not on every loop
    • make a dedicated cog to do the work in its RAM - fast(for functions like putpixel, draw line, circle etc)
  • Wuerfel_21Wuerfel_21 Posts: 1,643
    edited 2021-04-03 19:31

    Flexspin indeed uses MUL for 16 bit values.

    PUB main() | long a, long b
    a := ina
    b := inb
    outa := a*b
    

    compiles to

    _main
        mov _var01, ina
        mov _var02, inb
        qmul    _var01, _var02
        getqx   outa
    _main_ret
        ret
    

    and

    PUB main() | word a, word b
    a := ina
    b := inb
    outa := a*b
    

    compiles to

    _main
        mov _var01, ina
        mov _var02, inb
        mul _var01, _var02
        mov outa, _var01
    _main_ret
        ret
    

    Sadly it doesn't seem to have the ability to squeeze other instructions inbetween the QMUL and GETQX.

  • pik33pik33 Posts: 1,014

    Good to remember that Spin2 allows declaring the local variable type :) I will replace all longs where they are not needed.

  • cgraceycgracey Posts: 13,573

    I would need to make the interpreter look at the input data sizes and dynamically do MUL or QMUL.

Sign In or Register to comment.