Square Root of a 32-Bit Binary Number (SQRT)

advertisement
To our customers,
Old Company Name in Catalogs and Other Documents
On April 1st, 2010, NEC Electronics Corporation merged with Renesas Technology
Corporation, and Renesas Electronics Corporation took over all the business of both
companies. Therefore, although the old company name remains in this document, it is a valid
Renesas Electronics document. We appreciate your understanding.
Renesas Electronics website: http://www.renesas.com
April 1st, 2010
Renesas Electronics Corporation
Issued by: Renesas Electronics Corporation (http://www.renesas.com)
Send any inquiries to http://www.renesas.com/inquiry.
Notice
1.
2.
3.
4.
5.
6.
7.
All information included in this document is current as of the date this document is issued. Such information, however, is
subject to change without any prior notice. Before purchasing or using any Renesas Electronics products listed herein, please
confirm the latest product information with a Renesas Electronics sales office. Also, please pay regular and careful attention to
additional and different information to be disclosed by Renesas Electronics such as that disclosed through our website.
Renesas Electronics does not assume any liability for infringement of patents, copyrights, or other intellectual property rights
of third parties by or arising from the use of Renesas Electronics products or technical information described in this document.
No license, express, implied or otherwise, is granted hereby under any patents, copyrights or other intellectual property rights
of Renesas Electronics or others.
You should not alter, modify, copy, or otherwise misappropriate any Renesas Electronics product, whether in whole or in part.
Descriptions of circuits, software and other related information in this document are provided only to illustrate the operation of
semiconductor products and application examples. You are fully responsible for the incorporation of these circuits, software,
and information in the design of your equipment. Renesas Electronics assumes no responsibility for any losses incurred by
you or third parties arising from the use of these circuits, software, or information.
When exporting the products or technology described in this document, you should comply with the applicable export control
laws and regulations and follow the procedures required by such laws and regulations. You should not use Renesas
Electronics products or the technology described in this document for any purpose relating to military applications or use by
the military, including but not limited to the development of weapons of mass destruction. Renesas Electronics products and
technology may not be used for or incorporated into any products or systems whose manufacture, use, or sale is prohibited
under any applicable domestic or foreign laws or regulations.
Renesas Electronics has used reasonable care in preparing the information included in this document, but Renesas Electronics
does not warrant that such information is error free. Renesas Electronics assumes no liability whatsoever for any damages
incurred by you resulting from errors in or omissions from the information included herein.
Renesas Electronics products are classified according to the following three quality grades: “Standard”, “High Quality”, and
“Specific”. The recommended applications for each Renesas Electronics product depends on the product’s quality grade, as
indicated below. You must check the quality grade of each Renesas Electronics product before using it in a particular
application. You may not use any Renesas Electronics product for any application categorized as “Specific” without the prior
written consent of Renesas Electronics. Further, you may not use any Renesas Electronics product for any application for
which it is not intended without the prior written consent of Renesas Electronics. Renesas Electronics shall not be in any way
liable for any damages or losses incurred by you or third parties arising from the use of any Renesas Electronics product for an
application categorized as “Specific” or for which the product is not intended where you have failed to obtain the prior written
consent of Renesas Electronics. The quality grade of each Renesas Electronics product is “Standard” unless otherwise
expressly specified in a Renesas Electronics data sheets or data books, etc.
“Standard”:
8.
9.
10.
11.
12.
Computers; office equipment; communications equipment; test and measurement equipment; audio and visual
equipment; home electronic appliances; machine tools; personal electronic equipment; and industrial robots.
“High Quality”: Transportation equipment (automobiles, trains, ships, etc.); traffic control systems; anti-disaster systems; anticrime systems; safety equipment; and medical equipment not specifically designed for life support.
“Specific”:
Aircraft; aerospace equipment; submersible repeaters; nuclear reactor control systems; medical equipment or
systems for life support (e.g. artificial life support devices or systems), surgical implantations, or healthcare
intervention (e.g. excision, etc.), and any other applications or purposes that pose a direct threat to human life.
You should use the Renesas Electronics products described in this document within the range specified by Renesas Electronics,
especially with respect to the maximum rating, operating supply voltage range, movement power voltage range, heat radiation
characteristics, installation and other product characteristics. Renesas Electronics shall have no liability for malfunctions or
damages arising out of the use of Renesas Electronics products beyond such specified ranges.
Although Renesas Electronics endeavors to improve the quality and reliability of its products, semiconductor products have
specific characteristics such as the occurrence of failure at a certain rate and malfunctions under certain use conditions. Further,
Renesas Electronics products are not subject to radiation resistance design. Please be sure to implement safety measures to
guard them against the possibility of physical injury, and injury or damage caused by fire in the event of the failure of a
Renesas Electronics product, such as safety design for hardware and software including but not limited to redundancy, fire
control and malfunction prevention, appropriate treatment for aging degradation or any other appropriate measures. Because
the evaluation of microcomputer software alone is very difficult, please evaluate the safety of the final products or system
manufactured by you.
Please contact a Renesas Electronics sales office for details as to environmental matters such as the environmental
compatibility of each Renesas Electronics product. Please use Renesas Electronics products in compliance with all applicable
laws and regulations that regulate the inclusion or use of controlled substances, including without limitation, the EU RoHS
Directive. Renesas Electronics assumes no liability for damages or losses occurring as a result of your noncompliance with
applicable laws and regulations.
This document may not be reproduced or duplicated, in any form, in whole or in part, without prior written consent of Renesas
Electronics.
Please contact a Renesas Electronics sales office if you have any questions regarding the information contained in this
document or Renesas Electronics products, or if you have any other inquiries.
(Note 1) “Renesas Electronics” as used in this document means Renesas Electronics Corporation and also includes its majorityowned subsidiaries.
(Note 2) “Renesas Electronics product(s)” means any product developed or manufactured by or for Renesas Electronics.
APPLICATION NOTE
H8/300H Tiny Series
Square Root of a 32-Bit Binary Number (SQRT)
Introduction
Produces the square root of a 32-bit binary number as a 16-bit binary number.
Target Device
H8/300H Tiny Series
Contents
1.
Function ............................................................................................................................................ 2
2.
Arguments......................................................................................................................................... 2
3.
Changes to Internal Registers and Flags ......................................................................................... 2
4.
Programming Specifications ............................................................................................................. 3
5.
Note................................................................................................................................................... 3
6.
Description ........................................................................................................................................ 4
7.
Flowchart........................................................................................................................................... 6
8.
Program Listing................................................................................................................................. 8
REJ06B0074-0200/Rev.2.00
June 2006
Page 1 of 11
H8/300H Tiny Series
Square Root of a 32-Bit Binary Number (SQRT)
1.
Function
1. Produces the square root of a 32-bit binary number as a 16-bit binary number.
2. Finds and outputs the square root of a 32-bit binary number as a 16-bit binary number.
3. The arguments are all unsigned integers. All data operations are on the general registers.
2.
Arguments
Input
Output
3.
Contents
Original number
(binary number for square-root extraction)
Square root
Storage Location
R4, R5
Data Length (Bytes)
4
R3
2
Changes to Internal Registers and Flags
31
16 15
8 7
ER0
Work
ER1
Work
ER2
Work
ER3
Result
ER4
Input
ER5
Input
0
Work
ER6
ER7 (SP)
: No change
I
UI
H
U
N
Z
V
C
: Varies
0 : Fixed to 0
1 : Fixed to 1
REJ06B0074-0200/Rev.2.00
June 2006
Page 2 of 11
H8/300H Tiny Series
Square Root of a 32-Bit Binary Number (SQRT)
4.
Programming Specifications
Program memory (bytes)
94
Data memory (bytes)
0
Stack (bytes)
0
Number of cycles
1340
Re-entrant
Yes
Relocatalbe
Yes
Interrupts during execution
Yes
5.
Note
The number of cycles in the programming specifications is the value in the execution of SQRT as shown in figure 1.
REJ06B0074-0200/Rev.2.00
June 2006
Page 3 of 11
H8/300H Tiny Series
Square Root of a 32-Bit Binary Number (SQRT)
6.
6.1
Description
Description of Functions
1. The arguments are as follows.
R4: Set the higher-order word of the original number in 32-bit binary here, as an input argument.
R5: Set the lower-order word of the original number here.
R3: The square root (16-bit binary) is set here, as an output argument.
2. The following figure illustrates the execution of the SQRT subroutine. When the input arguments are set as shown
below, the corresponding square root is set in R3.
R4
1
Input arguments
R4, R5
(H'FFFFFFFF)
Output argument
R3
(H'FFFF)
F
R5
F
F
F
F
F
F
F
F
F
R3
2
F
F
Figure 1 Example of SQRT Execution
6.2
Usage Notes
1. Any higher-order bits of the input argument that are unused must be explicitly set to "0", as shown in figure 2.
Otherwise, the correct result might not be obtained because undefined data in the higher-order bits is included in
computation of the square root.
R4
0
0
R5
0
F
F
F
F
F
F
F
R3
0
3
Figure 2 Example when Higher-order Bits are not Used
2. The fractional part of the result is discarded.
6.3
Description of Data Memory
No data memory is used by SQRT.
REJ06B0074-0200/Rev.2.00
June 2006
Page 4 of 11
H8/300H Tiny Series
Square Root of a 32-Bit Binary Number (SQRT)
6.4
Example of Usage
After setting the number for which the square root is to be extracted, call the SQRT subroutine.
WORK1
. RES. W 2
Reservation of the data memory area for setting by the user program of the 32-bit binary
number for square-root extraction.
WORK2
. RES. W 2
.
.
.
Reservation of a data memory area to hold the 16-bit binary square root for the user program.
MOV. W @WORK1, R4
Sets, as the input argument, the 32-bit binary number for square-root extraction that is specified
by the user program.
MOV. W @WORK1+2, R5
JSR
@SQRT
Subroutine call of SQRT.
Transfers , to the data memory of the user program, the 16-bit binary square root the output by
the subroutine.
MOV. W R3, @WORK 2
.
.
.
6.5
Principles of Operation
1. The following figure shows the method used to calculate the square root (H'05) of the hexadecimal number H'22.
C
A
1
1
B
10
0
0
10
0
1
1
α...... 10
1
0
D
1
10
1
E
0
00
1
00
0
1
F
1
10
(1) ......Square root
(2) ......Original number
00
10
10
01
(3)
(4)
(5)
(6)
(7)
10
01
(8)
Figure 3 Calculation of Square Root
1) As shown in the figure, the square root can be found by processing every two-bit unit, from highest to lowest
order, of the original number.
2) The square root (1) is equivalent to the quotient when α is divided by two. Parameter α is found through the
operations A, B, and C in the figure. The SQRT subroutine finds the square root by calculating the value of α
and then dividing it by two.
2. Details on the program are given below.
The program:
a) sets D'16 in R6L, which is the number of two-bit units in a 32-bit binary number;
b) clears the area for storage of the square root (R2, R3) and the working area (R0, R1);
c) extracts the two highest-order bits of the input binary number to R0 and R1, by rotating R4, R5, R0, and R1 two
bits to the left;
d) places "1" in R2, R3;
e) subtracts R2, R3 from R0, R1 to find the difference (D, (2), (3), and (4)), then sets difference thus found in R0,
R1; and
f) if the result is positive, increments R2, R3 (A to (4)); if the result is negative, decrements R2, R3, and adds R2,
R3 to R0, R1 (D, E, and (6)).
3. In SQRT, R6 is decremented each time steps c) through f) above are performed, and this processing is repeated until
R6 has reached "0".
REJ06B0074-0200/Rev.2.00
June 2006
Page 5 of 11
H8/300H Tiny Series
Square Root of a 32-Bit Binary Number (SQRT)
7.
Flowchart
SQRT
1
#D'16 → R6L
......
Load D'16 (number of times to repeat two-bit processing)
into the loop counter (R6L).
#H'0000 → R0
R0 → R1
R0 → R2
R0 → R3
......
Clear the working area (R0, R1) and calculation result
area (R2, R3).
......
Shift the original number 2 bits left to take out the upper
2-bit value. Rotate the working area 2 bits left to set the
lower 2 bits.
LBL1
#H'02 → R6H
LBL2
Shift R5L 1-bit left
Rotate R5H 1-bit left
Rotate R4L 1-bit left
Rotate R4H 1-bit left
Rotate R1L 1-bit left
Rotate R1H 1-bit left
Rotate R0L 1-bit left
Rotate R0H 1-bit left
R6H - #1 → R6H
No
R6H = 0
Yes
2
REJ06B0074-0200/Rev.2.00
June 2006
Page 6 of 11
H8/300H Tiny Series
Square Root of a 32-Bit Binary Number (SQRT)
2
1 → C flag
Rotate R3L 1-bit left
Rotate R3H 1-bit left
Rotate R2L 1-bit left
Rotate R2H 1-bit left
R1 - R3 → R1
R0L - R2L - C → R0L
R0H - R2H - C → R0H
......
Rotate the calculation-result area 1 bit left,
and set "1" in the LSB.
......
Subtract the value in the calculation-result area
from that in the working area, and set the result
in the working area. Branch if there is no borrow.
......
Add the value in the working area to that in the
calculation-result area,and restore the working
area's value.
......
Clear the LSB of the calculation-result area,
which was set to "1".
Yes
C=0
No
R1 + R3 → R1
R0L + R2L + C → R0L
R0H + R2H + C → R0H
1 → C flag
R3L - #H'00 - C → R3L
R3H - #H'00 - C → R3H
R2L - #H'00 - C → R2L
R2H - #H'00 - C → R2H
1 → C flag
R3L + #H'00 + C → R3L
R3H + #H'00 + C → R3H
R2L + #H'00 + C → R2L
R2H + #H'00 + C → R2H
......
Add "1" to the value in the calculation-result area.
......
Branch 16 times.
......
Divide the value in the calculation-result area
by 2 and take the result as the square root.
LBL4
R6L - #1 → R6L
LBL1 Yes
1
R6L ≠ 0
No
Shift R2H 1-bit right
Rotate R2L 1-bit right
Rotate R3H 1-bit right
Rotate R3L 1-bit right
RTS
REJ06B0074-0200/Rev.2.00
June 2006
Page 7 of 11
H8/300H Tiny Series
Square Root of a 32-Bit Binary Number (SQRT)
8.
Program Listing
1
1
;********************************************************************
2
2
;*
3
3
;*
4
4
;*
5
5
;********************************************************************
6
6
;*
7
7
;*
8
8
;*
*
NAME : 32 BIT SQUARE ROOT (SQRT)
*
*
*
ENTRY
: R4,R5
(32 BIT BINARY)
*
*
9
9
;*
10
10
;*
11
11
;********************************************************************
12
12
;
13
13
.CPU
300HN
14
.SECTION
SQRT_code,CODE,ALIGN=2
.EXPORT
SQRT
14
0000
15
15
16
16
;
SQRT
17
RETURNS : R3
(SQUARE ROOT)
*
*
00000000
17
.EQU
$
;Entry point
18
0000
FE10
18
MOV.B
#D'16,R6L
;Set shift counter
19
0002
79000000
19
MOV.W
#H'0000,R0
;Clear R0
20
0006
0D01
20
MOV.W
R0,R1
;Clear R1
21
0008
0D02
21
MOV.W
R0,R2
;Clear R2
22
000A
0D03
22
MOV.W
R0,R3
;Clear R3
23
000C
24
000C
MOV.B
#H'02,R6H
25
000E
26
000E
100D
26
SHLL.B
R5L
27
0010
1205
27
ROTXL.B
R5H
28
0012
120C
28
ROTXL.B
R4L
29
0014
1204
29
ROTXL.B
R4H
30
0016
1209
30
ROTXL.B
R1L
31
0018
1201
31
ROTXL.B
R1H
32
001A
1208
32
ROTXL.B
R0L
33
001C
1200
33
ROTXL.B
R0H
34
001E
1A06
34
DEC.B
R6H
35
0020
46EC
35
BNE
LBL2
;Branch if Z=0
36
0022
0401
36
ORC.B
#H'01,CCR
;Set C flag of CCR
37
0024
120B
37
ROTXL.B
R3L
;Rotate square root
38
0026
1203
38
ROTXL.B
R3H
39
0028
120A
39
ROTXL.B
R2L
40
002A
1202
40
ROTXL.B
R2H
41
002C
1931
41
SUB.W
R3,R1
;R1
42
002E
1EA8
42
SUBX.B
R2L,R0L
;R0L - R2L - C -> R0L
43
0030
1E20
43
SUBX.B
R2H,R0H
;R0H - R2H - C -> R0H
44
0032
4412
44
BCC
LBL3
;Branch if C=0
45
0034
0931
45
ADD.W
R3,R1
;R1
46
0036
0EA8
46
ADDX.B
R2L,R0L
;R0L + R2L + C -> R0L
47
0038
0E20
47
ADDX.B
R2H,R0H
;R0H + R2H + C -> R0H
48
003A
0401
48
ORC.B
#H'01,CCR
;Bit set C flag of CCR
49
003C
BB00
49
SUBX.B
#H'00,R3L
;R3L - #H'00 - C -> R3L
50
003E
B300
50
SUBX.B
#H'00,R3H
;R3H - #H'00 - C -> R3H
51
0040
BA00
51
SUBX.B
#H'00,R2L
;R2L - #H'00 - C -> R2L
52
0042
B200
52
SUBX.B
#H'00,R2H
;R2H - #H'00 - C -> R2H
23
F602
24
25
REJ06B0074-0200/Rev.2.00
LBL1
LBL2
June 2006
;Shift 32 bit binary 1 bit left
;Decrement R6H
- R3
+ R3
-> R1
-> R1
Page 8 of 11
H8/300H Tiny Series
Square Root of a 32-Bit Binary Number (SQRT)
53
0044
54
0046
400A
55
0046
0401
56
0048
9B00
57
004A
58
53
BRA
LBL4
;
55
ORC.B
#H'01,CCR
;Bit set C flag of CCR
56
ADDX.B
#H'00,R3L
;R3L + #H'00 + C -> R3L
9300
57
ADDX.B
#H'00,R3H
;R3H + #H'00 + C -> R3H
004C
9A00
58
ADDX.B
#H'00,R2L
;R2L + #H'00 + C -> R2L
59
004E
9200
59
ADDX.B
#H'00,R2H
;R2H + #H'00 + C -> R2H
60
0050
61
0050
61
DEC.B
R6L
;Decrement shift counter
62
0052
46B8
62
BNE
LBL1
;Branch if
63
0054
1102
63
SHLR.B
R2H
64
0056
130A
64
ROTXR.B
R2L
65
0058
1303
65
ROTXR.B
R3H
66
005A
130B
66
ROTXR.B
R3L
67
005C
5470
67
54
60
1A0E
68
68
69
69
*****
TOTAL ERRORS
*****
TOTAL WARNINGS 0
LBL3
LBL4
;Rotate square root
RTS
;
.END
0
REJ06B0074-0200/Rev.2.00
June 2006
Page 9 of 11
H8/300H Tiny Series
Square Root of a 32-Bit Binary Number (SQRT)
Revision Record
Rev.
2.00
Date
Jun.12.06
REJ06B0074-0200/Rev.2.00
Description
Page
Summary
—
Format has been changed from Hitachi version to Renesas
version.
June 2006
Page 10 of 11
H8/300H Tiny Series
Square Root of a 32-Bit Binary Number (SQRT)
Keep safety first in your circuit designs!
1. Renesas Technology Corp. puts the maximum effort into making semiconductor products better and
more reliable, but there is always the possibility that trouble may occur with them. Trouble with
semiconductors may lead to personal injury, fire or property damage.
Remember to give due consideration to safety when making your circuit designs, with appropriate
measures such as (i) placement of substitutive, auxiliary circuits, (ii) use of nonflammable material or
(iii) prevention against any malfunction or mishap.
Notes regarding these materials
1. These materials are intended as a reference to assist our customers in the selection of the Renesas
Technology Corp. product best suited to the customer's application; they do not convey any license
under any intellectual property rights, or any other rights, belonging to Renesas Technology Corp. or
a third party.
2. Renesas Technology Corp. assumes no responsibility for any damage, or infringement of any thirdparty's rights, originating in the use of any product data, diagrams, charts, programs, algorithms, or
circuit application examples contained in these materials.
3. All information contained in these materials, including product data, diagrams, charts, programs and
algorithms represents information on products at the time of publication of these materials, and are
subject to change by Renesas Technology Corp. without notice due to product improvements or
other reasons. It is therefore recommended that customers contact Renesas Technology Corp. or
an authorized Renesas Technology Corp. product distributor for the latest product information
before purchasing a product listed herein.
The information described here may contain technical inaccuracies or typographical errors.
Renesas Technology Corp. assumes no responsibility for any damage, liability, or other loss rising
from these inaccuracies or errors.
Please also pay attention to information published by Renesas Technology Corp. by various means,
including the Renesas Technology Corp. Semiconductor home page (http://www.renesas.com).
4. When using any or all of the information contained in these materials, including product data,
diagrams, charts, programs, and algorithms, please be sure to evaluate all information as a total
system before making a final decision on the applicability of the information and products. Renesas
Technology Corp. assumes no responsibility for any damage, liability or other loss resulting from the
information contained herein.
5. Renesas Technology Corp. semiconductors are not designed or manufactured for use in a device or
system that is used under circumstances in which human life is potentially at stake. Please contact
Renesas Technology Corp. or an authorized Renesas Technology Corp. product distributor when
considering the use of a product contained herein for any specific purposes, such as apparatus or
systems for transportation, vehicular, medical, aerospace, nuclear, or undersea repeater use.
6. The prior written approval of Renesas Technology Corp. is necessary to reprint or reproduce in
whole or in part these materials.
7. If these products or technologies are subject to the Japanese export control restrictions, they must
be exported under a license from the Japanese government and cannot be imported into a country
other than the approved destination.
Any diversion or reexport contrary to the export control laws and regulations of Japan and/or the
country of destination is prohibited.
8. Please contact Renesas Technology Corp. for further details on these materials or the products
contained therein.
REJ06B0074-0200/Rev.2.00
June 2006
Page 11 of 11
Download