Searching \ for 'Absolute Difference Algorithm' in subject line. ()
Make payments with PayPal - it's fast, free and secure! Help us get a faster server
FAQ page: techref.massmind.org/techref/index.htm?key=absolute+difference
Search entire site for: 'Absolute Difference Algorithm'.

Truncated match.
PICList Thread
'Absolute Difference Algorithm'
1998\01\13@115337 by Jennifer Wilson

picon face
Happy New Year,
  I was just wondering if anyone has an elegant method for determining
whether two numbers are within an absolute difference, in the 12 bit PIC
universe.

 For example...  the two numbers, X & Y, are in registers which must
remain unchanged are to be tested to determine whether they are within
DIFF of each other.

 My first try this year is:

         MOVF       X,W        ;Try it this way
         SUBWF   Y,W

         SKPNC                  ; Positive?
         GOTO      POS      ; Yes

         MOVF       Y,W       ; No, then do it the other way
         SUBWF   X,W

POS  SUBWF  DIFF,W  ; Compare against the limit

         SKPNC                  ; The carry knows...
         GOTO     LESS


Thanks in advance,
  Jen :-)

1998\01\14@071252 by REMOVE_TO_REPLY_jnlrich

flavicon
face
Not elegant but seven instructions and free:

   movf    X,W             ; get first number.
   subwf   Y,W             ; sub second number.
   bc      AA              ; result positive? - Yes, go to AA.
   xorlw   h'FF'           ; convert from two's compliment
   addlw   1               ;   negative to positive.
AA  subwf   DIFF,w          ; sub difference from DIFF.
   bc      LESS            ; result + or 0? - Yes, goto LESS.


On Tue, 13 Jan 1998 11:49:18 EST, you wrote:

->Happy New Year,
->   I was just wondering if anyone has an elegant method for
determining
->whether two numbers are within an absolute difference, in the 12 bit
PIC
->universe.
->
->  For example...  the two numbers, X & Y, are in registers which
must
->remain unchanged are to be tested to determine whether they are
within
->DIFF of each other.
->
->  My first try this year is:
->
->          MOVF       X,W        ;Try it this way
->          SUBWF   Y,W
->
->          SKPNC                  ; Positive?
->          GOTO      POS      ; Yes
->
->          MOVF       Y,W       ; No, then do it the other way
->          SUBWF   X,W
->
->POS  SUBWF  DIFF,W  ; Compare against the limit
->
->          SKPNC                  ; The carry knows...
->          GOTO     LESS
->
->
->Thanks in advance,
->   Jen :-)

1998\01\14@123415 by Justin Crooks

flavicon
face
That's pretty close to perfect.  You could also try this...

       movf    X,W
       subwf   Y,W

       skpc
       sublw   0x00    ;If it is negative, make it positive.

       subwf   DIFF,W
       skpnc
       goto    LESS...

       Not much of an improvement, but maybe a little cleaner :)


----------
{Quote hidden}

1998\01\14@151214 by Jennifer Wilson

picon face
On Wed, 14 Jan 1998 12:02:07 GMT Lynn Richardson
<REMOVE_TO_REPLY_jnlrichspamKILLspamqni.com> writes:
>Not elegant but seven instructions and free:

>    movf    X,W             ; get first number.
>    subwf   Y,W             ; sub second number.
>    bc      AA              ; result positive? - Yes, go to AA.
>    xorlw   h'FF'           ; convert from two's compliment
>    addlw   1               ;   negative to positive.
>AA  subwf   DIFF,w          ; sub difference from DIFF.
>    bc      LESS            ; result + or 0? - Yes, goto LESS.
>

   I think that will assemble to 9 instructions when the psuedo-ops are
expanded, equal to the original...

{Quote hidden}

On Wed, 14 Jan 1998 10:22:48 -0700 Justin Crooks <.....jcrooksKILLspamspam.....WYOMING.COM>
writes:
{Quote hidden}

   Correct me if wrong but, I don't believe that "SUBLW" is part of the
12 bit PIC world...

   Andy Warren, who reputation exceeds himself in this venue, sent me a
version using the same logic employing a file register either
conveniently or purposefully cleared to 0.

               ********************************

  Perhaps, I should expand a bit on the conditions...

  If it helps, DIFF can be a constant instead of a file register.

  X & Y are in the range of 0 to 32

  DIFF ranges between 4 & 9

  It would be nice to optimize this, as there are several similar
operations in the application.

Still more thanks in advance!
  Jen :-)

1998\01\14@155138 by Justin Crooks

flavicon
face
Sorry, I live in the 14 bit PIC world... I guess I should do a little
research before putting in my 2 bits!!!
:)

----------
{Quote hidden}

1998\01\14@183647 by Dmitry Kiryashov

flavicon
face
Hello Jennifer.

Slightly different variant: (i don't like to use GOTO operator ;)))

;1
       MOVFW   X
       SUBWF   Y,W
;2
       SKPC
       MOVFW   Y
       SKPC
       SUBWF   X,W
;3
       SUBWF   DIFF,W
;....

But i think code with with Zero_constant register will be shorter

;2
       SKPC
       SUBWF   ZERO_VAL,W
;3

WBR Dmitry.


{Quote hidden}

1998\01\15@012311 by John Payson

picon face
[Goal: See how |X-Y| compares with DIFF]

> Slightly different variant: (i don't like to use GOTO operator ;)))
>
> ;1
>         MOVFW   X
>         SUBWF   Y,W
> ;2
>         SKPC
>         MOVFW   Y
>         SKPC
>         SUBWF   X,W
> ;3
>         SUBWF   DIFF,W
> ;....

If you can spare another register which holds the value (2diff) and you
use the same value of "diff" a lot, you could use:

       ; First get the difference
       movf    x,w
       subwf   y,w
       ; Now see if it's in the range -diff..diff
       addwf   diff,w                  ; i.e. in 0..2diff
       subwf   diff2,w                 ; If was in range, result >= 0

Perhaps more of a sacrifice than a known-zero register, but I don't think
you can do it in four cycles any other way.

1998\01\16@111658 by Jennifer Wilson

picon face
On Wed, 14 Jan 1998 23:57:38 -0600 John Payson <RemoveMEsupercatTakeThisOuTspamMCS.NET> writes:
>If you can spare another register which holds the value (2diff) and you
>use the same value of "diff" a lot, you could use:
>
>        ; First get the difference
>        movf    x,w
>        subwf   y,w
>        ; Now see if it's in the range -diff..diff
>        addwf   diff,w                  ; i.e. in 0..2diff
>        subwf   diff2,w                 ; If was in range, result >= 0
>
>Perhaps more of a sacrifice than a known-zero register, but I don't
think
>you can do it in four cycles any other way.

   Cute!   Only 6 instructions after the conditional test is appended.
Very useful in the target application.   It is worth the couple extra
instructions of overhead to initialize the 2DIFF register, since there
are many tests with the same DIFF value.

Thanks for the suggested algorithms!
  Jen :-)

More... (looser matching)
- Last day of these posts
- In 1998 , 1999 only
- Today
- New search...