# PICMicrocontollerMathMethod

## Square 10 bits

Code:

• Martin Sturm shares this code:
• ```; Square 10bit unsigned
;  by Martin Sturm  2010
; Tested over full 10bit input range
;
;   a*a --> r
;   a = aH:aL		(10bit, right justified) (not modified)
;   r = rH:rM:rL	(20bit result)
;
; algorithm
;  r = 2^16*(aH*aH) + 2^8*(2*aH*aL) + aL*aL
;             2x2            2x8       8x8
; all multiplications are unrolled
;
;   62 instructions, 49-62 cycles, 56 avg
;
; incorrect result if A has non-zero bits above the 10th
;  use optional ANDLW 0x03 to correct for this if necessary
;

; helper macro
mmac MACRO A,bit, uH,uL
BTFSC	A,bit
RRF	uH,F
RRF	uL,F
ENDM

SQR_10 MACRO aH,aL, rH,rM,rL
LOCAL g1, g2

; rM:rL = aL*aL  [8b x 8b mult] (36 instr, 36 cyc)
CLRF	rM
CLRF	rL
CLRC
MOVFW	aL
mmac	aL,0, rM,rL
mmac	aL,1, rM,rL
mmac	aL,2, rM,rL
mmac	aL,3, rM,rL
mmac	aL,4, rM,rL
mmac	aL,5, rM,rL
mmac	aL,6, rM,rL
mmac	aL,7, rM,rL

; rH = aH*aH   [2b x 2b square] (8 instr, 8 cyc)
CLRF	rH
MOVFW	aH	; multiplicand in W
;	ANDLW	0x03	; prevent errors if aH non-zero above 10th bit
BTFSC	aH,1
ADDWF	rH,F	; never sets carry
RLF	rH,F
BTFSC	aH,0
ADDWF	rH,F	; never sets carry

; rH:rM += 2*aH*aL  [2b x 8b mult] (19 instr, 7-19 cyc, avg. 13)
RLF	aL,W	; W = 2*aL   (carry is always clear before here)
BTFSS	aH,0
GOTO	g1
SKPNC
INCF	rH,F	; add upper bit of 2*aL
SKPNC
INCF	rH,F
g1
BTFSS	aH,1
GOTO	g2
; W still holds (2*aL & 0xFF)
SKPNC
INCF	rH,F
SKPNC
INCF	rH,F
MOVLW	0x02
BTFSC	aL,7	;
g2
ENDM
```

 file: /Techref/microchip/math/sq/10b-ms.htm, 2KB, , updated: 2010/4/14 23:15, local time: 2024/4/12 22:26, owner: MDS-gmail-IE8, TOP NEW HELP FIND:  3.227.240.72:LOG IN

 ©2024 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?Please DO link to this page! Digg it! / MAKE! PIC Microcontoller Math Method Square 10 bits by Martin Sturm

After you find an appropriate page, you are invited to your to this massmind site! (posts will be visible only to you before review) Just type a nice message (short messages are blocked as spam) in the box and press the Post button. (HTML welcomed, but not the <A tag: Instead, use the link box to link to another page. A tutorial is available Members can login to post directly, become page editors, and be credited for their posts.

Attn spammers: All posts are reviewed before being made visible to anyone other than the poster.
 Did you find what you needed? "No. I'm looking for: " "No. Take me to the search page." "No. Take me to the top so I can drill down by catagory" "No. I'm willing to pay for help, please refer me to a qualified consultant"

 PICList 2024 contributors: o List host: MIT, Site host massmind.org, Top posters @none found - 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.

.