Normally division algorithms follow the way children are tought to operate. Let's take an example:
With RISC-technology, at assembler level, the tests are operated with subtractions, checking whether the results are negative, zero or positive. The integer-division is done by successive subtractions until the result is negative. A counter then indicates how often subtractions were made.
There is a faster way; CORDIC methods are a very different approach to mathematical operations. The higher speed of the algorithms is the result of a divide and conquer approach. Let's see how CORDIC division works:
Suppose you want to integer-divide 8710 = 10101112 through 610 = 1102.
numerator 01010111 base_index := 00000001 = 1 divisor 00000110 result:=0
- rotate divisor and base_index until the most significant bits of numerator and divisor are equal:
00001100 00000010 = 2 00011000 00000100 = 4 00110000 00001000 = 8 01100000 00010000 = 16 - now subtract both numerator and altered divisor: 01010111- 01100000 ---------- < 0
- if negative -which is the case here- rotate back divisor and base_index one digit to the right:
00110000 00001000 = 8 - substract again rotated divisor from numerator: 01010111- 00110000 ---------- 00100111, positive remainder - now replace the divisor by the remainder: new numerator:= 00100111 - this time add the base_index to result: result:= result(0) + 8 = 8 - now rotate to the right divisor and base_index one digit: 00011000 00000100 = 4 - subtract again: 00100111- 00011000 ---------- 00001111, remainder positive, so new numerator:=00001111 result:=result + base_index = 8+4 = 12 - rotate: 00001100 00000010 = 2 - substract : 00001111- 00001100 ---------- 00000011, remainder positive, so new numerator:=00000011 result:=result + base_index = 12+2 = 14 - rotate: 00000110 00000001 = 1 - subtract: 00000011- 00000110 ---------- < 0, so do nothing - stop
Here is PIC 16F84 and 628 code:
DIVV8 MOVF TEMPY8,F BTFSC STATUS,Z ;SKIP IF NON-ZERO RETURN CLRF RESULT8 MOVLW 1 MOVWF IDX16 SHIFT_IT8 BCF STATUS,C RLF IDX16,F BCF STATUS,C RLF TEMPY8,F BTFSS TEMPY8,7 GOTO SHIFT_IT8
DIVU8LOOP MOVF TEMPY8,W SUBWF TEMPX8 BTFSC STATUS,C GOTO COUNT8 ADDWF TEMPX8 GOTO FINAL8 COUNT8 MOVF IDX16,W ADDWF RESULT8 FINAL8 BCF STATUS,C RRF TEMPY8,F BCF STATUS,C RRF IDX16,F BTFSS STATUS,C GOTO DIVU8LOOP RETURN
SUB16 MOVF TEMPY16_H,W MOVWF TEMPYY MOVF TEMPY16,W SUBWF TEMPX16 BTFSS STATUS,C INCF TEMPYY,F MOVF TEMPYY,W SUBWF TEMPX16_H RETURN
ADD16BIS MOVF TEMPY16,W ADDWF TEMPX16 BTFSC STATUS,C INCF TEMPX16_H,F MOVF TEMPY16_H,W ADDWF TEMPX16_H RETURN
DIVV16 MOVF TEMPY16,F BTFSS STATUS,Z GOTO ZERO_TEST_SKIPPED MOVF TEMPY16_H,F BTFSC STATUS,Z RETURN
ZERO_TEST_SKIPPED MOVLW 1 MOVWF IDX16 CLRF IDX16_H CLRF RESULT16 CLRF RESULT16_H
SHIFT_IT16 BCF STATUS,C RLF IDX16,F RLF IDX16_H,F BCF STATUS,C RLF TEMPY16,F RLF TEMPY16_H,F BTFSS TEMPY16_H,7 GOTO SHIFT_IT16
DIVU16LOOP CALL SUB16 BTFSC STATUS,C GOTO COUNTX CALL ADD16BIS GOTO FINALX COUNTX MOVF IDX16,W ADDWF RESULT16 BTFSC STATUS,C INCF RESULT16_H,F MOVF IDX16_H,W ADDWF RESULT16_H FINALX BCF STATUS,C RRF TEMPY16_H,F RRF TEMPY16,F BCF STATUS,C RRF IDX16_H,F RRF IDX16,F BTFSS STATUS,C GOTO DIVU16LOOP RETURN
... somewhere in the code CALL DIVV16
Note that these programs work only for unsigned variables. Worst case for DIVV8 is about 144 cycles, which at 20 MHz is about 30 microseconds. The advantage of this algorithm appears more clearly, if larger variables should be used.
Bergthaller C Iulian Says: " Based on this tutorial I make a subroutine for 24bits by 16 division. " +
Thank you very much for software that actually works. I used to do 16 bit binary (TMR1) to BCD by testing each bit and then adding its decimal value to the output. About 250 lines of code. The new algo I got here, does it in 50 lines!+
|file: /Techref/microchip/math/div/tutorial.htm, 7KB, , updated: 2018/1/4 17:37, local time: 2018/4/21 08:34,
|©2018 These pages are served without commercial sponsorship. (No popup ads, etc...).Bandwidth abuse increases hosting cost forcing sponsorship or shutdown. This server aggressively defends against automated copying for any reason including offline viewing, duplication, etc... Please respect this requirement and DO NOT RIP THIS SITE. Questions?|
<A HREF="http://www.piclist.com/Techref/microchip/math/div/tutorial.htm"> Fast division for PICs</A>
|Did you find what you needed?|
PICList 2018 contributors:
o List host: MIT, Site host massmind.org, Top posters @20180421 RussellMc, Van Horn, David, Sean Breheny, Isaac M. Bavaresco, Bob Blick, Neil, David C Brown, alan.b.pearce, Brent Brown, John Gardner,
* Page Editors: James Newton, David Cary, and YOU!
* Roman Black of Black Robotics donates from sales of Linistep stepper controller kits.
* Ashley Roll of Digital Nemesis donates from sales of RCL-1 RS232 to TTL converters.
* Monthly Subscribers: Gregg Rew. on-going support is MOST appreciated!
* Contributors: Richard Seriani, Sr.
Welcome to www.piclist.com!