• Forums
• Posts
Latest Posts
Active Posts
Recently Visited
Search Results
• Page Extras
• Forum Themes
• AVR Freaks

### Hot!Signed 32 by 32 divide routine in assembler

Page: < 12 Showing page 2 of 2
Author
Spinlectrix
Junior Member
• Total Posts : 72
• Reward points : 0
• Joined: 2016/03/20 14:48:37
• Location: 0
• Status: offline
Re: Signed 32 by 32 divide routine in assembler 2020/09/21 17:23:13 (permalink)
0
And here is an inline c function which performs the same algorithm.  Just include the function in a .h file and then you can use
(long variable) quotient = longDIV(long numerator, long divisor)
and get long division results in just 65 cycles. (More than 8x faster than standard library routines!)

`struct DoubleWord {    int16_t LoWord;    int16_t HiWord;};struct UDoubleWord {    uint16_t ULoWord;    uint16_t UHiWord;};union DoubleWordUnion {    long Longword;    unsigned long ULongword;    struct DoubleWord Words;    struct UDoubleWord UWords;};#define HasMULW true        // Set to 0 on F-class cores, 1 on E-class or C-class#define MulRepeats (18-1) // 17 for F- and E-class cores, 5 on C-classstatic inline __attribute__ ((always_inline))long longDIV(long numerator, long denominator) {union DoubleWordUnion quotient, absNum, absDenom;asm (  " xor %d4,%d5,[w15++] \n" // Store sign of quotient  " btsc %d4,#15 \n neg %4,%2 \n btsc %d4,#15 \n subbr %d4,#0,%d2 \n" // abs(Num)  " btsc %d5,#15 \n neg %5,%3 \n btsc %d5,#15 \n subbr %d5,#0,%d3 \n" // abs(Denom)  " ff1l %d3, w3 \n bra NC,0f \n" // Switch to appropriate method  " repeat #%6 \n div.uw %d2,%3 \n exch %0, %2 \n" // Calc Quotient Hi word  " repeat #%6 \n div.ud %0,%3 \n mov %2, %1 \n" // Calc Quotient Lo word  " btss [--w15],#15 \n bra 9f \n" // return iff Quotient not negative  " neg %0,%0 \n subbr %1,#0,%1 \n bra 9f \n" // Negate Quotient and return"0: subr w3,#17,w3 \n subr w3,#16,w0 \n" // Calculate shifts to capture 16 MSbs of denom  " sl %d3,w0,w0 \n lsr %3,w3,w2 \n ior w0,w2,w2 \n" // Shift Denominator by n  " lsr %d4,w1 \n rrc %4,w0 \n" // Shift Numerator by 1  " repeat #%6 \n div.ud w0,w2 \n" // Calculate n/d ratio  " dec w3, w3 \n lsr w0,w3,w1 \n" // Shift back magnitude by (n-1)#if HasMULW  " mulw.uu %d3,w1,w0 \n" #else  " mul.uu %d3,w1,w2 \n mov w2,w0 \n" #endif  " mul.uu %3,w1,w2 \n" // Check result magnitude  " sub %2,w2,w2 \n subb %d2,w3,w3 \n sub w3,w0,w3 \n subb w1,#0,w0 \n" // Adjust Quotient  " pop w1 \n asr w1,#15,w1 \n rlc w1,w1 \n mul.us w0,w1,w0 \n" // Restore Quotient sign"9:"/*                     w0 %0                             w1 %1       */:/*outputs*/ "=&a" (quotient.Words.LoWord), "=&b" (quotient.Words.HiWord),/*                    %d2:%2                     %d3:%3          */             "=r" (absNum.ULongword), "=r" (absDenom.ULongword) /*                    %d4:%4           %d5:%5              %6     */:/*inputs*/   "2" (numerator), "3" (denominator), "i" (MulRepeats) :/*clobbers*/ "cc","w2","w3");return (quotient.Longword);}`

post edited by Spinlectrix - 2020/09/22 19:39:32
ric
Super Member
• Total Posts : 28975
• Reward points : 0
• Joined: 2003/11/07 12:41:26
• Location: Australia, Melbourne
• Status: online
Re: Signed 32 by 32 divide routine in assembler 2020/09/21 17:25:22 (permalink)
0
Spinlectrix
And here is an inline c function which performs the same operation:

Wow that's short! ;)

I also post at: PicForum
Links to useful PIC information: http://picforum.ric323.co...opic.php?f=59&t=15
NEW USERS: Posting images, links and code - workaround for restrictions.
To get a useful answer, always state which PIC you are using!
Spinlectrix
Junior Member
• Total Posts : 72
• Reward points : 0
• Joined: 2016/03/20 14:48:37
• Location: 0
• Status: offline
Re: Signed 32 by 32 divide routine in assembler 2020/09/21 17:33:48 (permalink)
0
ric
[Wow that's short! ;)

Yeah, in order to post code, I have to Submit a post, then go back and edit the post where the options will now be expanded for adding code, and attaching files.  If someone looks at my post before my added edit is complete, well, you saw the result.  How can I add [code] and attach files when initially placing the post?
post edited by Spinlectrix - 2020/09/21 19:41:25
ric
Super Member
• Total Posts : 28975
• Reward points : 0
• Joined: 2003/11/07 12:41:26
• Location: Australia, Melbourne
• Status: online
Re: Signed 32 by 32 divide routine in assembler 2020/09/21 17:36:28 (permalink)
0
Spinlectrix
How can I add [code] and attach files when initially placing the post?

Click on the "Open Full Version" link just to the right of the "Quick Reply" title.
(You can just type code tags in by hand at any time, but you need the full version to attach files.)

I also post at: PicForum
Links to useful PIC information: http://picforum.ric323.co...opic.php?f=59&t=15
NEW USERS: Posting images, links and code - workaround for restrictions.
To get a useful answer, always state which PIC you are using!
Spinlectrix
Junior Member
• Total Posts : 72
• Reward points : 0
• Joined: 2016/03/20 14:48:37
• Location: 0
• Status: offline
Re: Signed 32 by 32 divide routine in assembler 2020/09/21 17:40:10 (permalink)
0
ric
Click on the "Open Full Version" link just to the right of the "Quick Reply" title.

So simple!  Thanks, I never saw the option until you pointed it out!
Page: < 12 Showing page 2 of 2