Shop OBEX P1 Docs P2 Docs Learn Events
Cmpsubx — Parallax Forums

Cmpsubx

ericballericball Posts: 774
edited 2007-06-05 15:41 in Propeller 1
I've been working on a divide routine (to calculate FRQA) and I noticed that CMPSUBX would give a better result than CMPSUB for the following routine:

:shmax··SHL·ofrac, #1·wc·' maximize the two values
··RCL·ofreq, #1
··SHL·ifrac, #1·wc
··RCL·ifreq, #1·wc
·IF_NC·JMP·#:shmax
··RCR·ifreq, #1··' undo overshoot
:div··CMPSUBX·ofreq, ifreq·wc·' do division
··RCL·pfreq, #1
··SHR·ifreq, #1·wz,wc
·IF_NZ·JMP·#:div


Unfortunately, CMPSUBX doesn't seem to exist, nor does it appear that it could exist as an undocumented opcode based on SUB/CMP vs SUBX/CMPX.· Or might it?
·

Comments

  • Paul BakerPaul Baker Posts: 6,351
    edited 2007-06-04 21:22
    Here is the recommended routine to divide a 32 bit number by a 16 bit value to yield a 16 bit quotient and 16 bit remainder, the routine can be adapted to other data sizes:

    [color=#000000]'[/color]
    [color=#000000]' Divide x[noparse][[/noparse]31..0] by y[noparse][[/noparse]15..0] (y[noparse][[/noparse]16] must be 0)[/color]
    [color=#000000]' on exit, quotient is in x[noparse][[/noparse]15..0] and remainder is in x[noparse][[/noparse]31..16][/color]
    [color=#000000]'[/color]
    [color=#000000]divide          shl     y,#15           'get divisor into y[noparse][[/noparse]30..15][/color]
    [color=#000000]                mov     t,#16           'ready for 16 quotient bits[/color]
     
    [color=#000000]:loop           cmpsub  x,y     wc      'if y =< x then subtract it, quotient bit into c[/color]
    [color=#000000]                rcl     x,#1            'rotate c into quotient, shift dividend[/color]
    [color=#000000]                djnz    t,#:loop        'loop until done[/color]
     
    [color=#000000]divide_ret      ret                     'quotient in x[noparse][[/noparse]15..0], remainder in x[noparse][[/noparse]31..16][/color]
     
    
    

    ▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
    Paul Baker
    Propeller Applications Engineer

    Parallax, Inc.
  • ericballericball Posts: 774
    edited 2007-06-05 15:30
    That's fine if you only want a 16 bit quotient, but I am trying to calculate FRQA := 2^32 * ofreq / CLKFREQ as precisely as possible. I was just noting if CMPSUBX did exist, then it would provide a more accurate result (by automatically rounding up/down) than CMPSUB. Then I was speculating such an instruction could exist as an undocumented instruction, given the correct opcode decoder conditions. (The 6502 has several useful undocumented opcodes which exist for this reason.)

    Hmm... your code is interesting since it uses the same register for both the divisor and the quotient/remainder. Ahh... I see it now. Since the divisor is shifted to y[noparse][[/noparse]30..15] the remainer isn't impacted by the CMPSUB.
  • Paul BakerPaul Baker Posts: 6,351
    edited 2007-06-05 15:41
    Sorry there are no undocumented assembly instructions, the only unused values are reserved for MUL, MULS, ENC and ONES.

    ▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
    Paul Baker
    Propeller Applications Engineer

    Parallax, Inc.
Sign In or Register to comment.