Dr. Dobb's is part of the Informa Tech Division of Informa PLC

This site is operated by a business or businesses owned by Informa PLC and all copyright resides with them. Informa PLC's registered office is 5 Howick Place, London SW1P 1WG. Registered in England and Wales. Number 8860726.


Channels ▼
RSS

Design

Software Implementation of Trigonometric Functions Using CORDIC Algorithm


In the Tasking tool chain, the parameter transforms of the first four arguments of the function will be in R12 to R16. Using C166SV2 instruction set, the micro rotations according to Equation (8) is given below as reference implementation. A fixed point number representation is used for the implementation. The registers R1, R2, R13 are assigned with X, Y, shift value respectively.

Label3: 
    MOV     R12,#0h 
    MOV     R3,R1
    MOV     R5,R2
    ASHR    R5,#0fh
    CMP     R5,#0
    JMPA    cc_NZ, Label1


; Micro rotation 1 


;I=I+(Q>>K) 
    MOV     R7,R2
    ASHR    R7,R13
    MOV     MAH,R1
    CoADD R12,R7
CoSTORE R1,MAS
;Q=Q-(I_tmp>>K) 
    ASHR     R3,R13
    MOV     MAH,R2
    CoSUB      R12,R3
    CoSTORE R2,MAS
JMPA         cc_NZ,Label 2


; Micro rotation 2


Label1: 
;I=I-(Q>>K) 
    MOV     R7, R2
    ASHR     R7,R13
    MOV     MAH,R1
    CoSUB      R12,R7
    CoSTORE R1,MAS
; Q=Q+ (I_tmp>>K) 
    ASHR     R3, R13
    MOV     MAH, R2
    CoADD     R12, R3
    CoSTORE R2,MAS


Label2: 
    ADD      R13, #1h
    CMPD1 R6,#0h
    JMPR     cc_NZ, Label3

The number of iterations is fixed to 15 and the direction of rotation is depended on Y, therefore there is no need to record the degree of rotation (i.e. the value of Z). This reduces the latency from n + 25 to n + 19 cycles, where 'n' is the number of iterations. In Figure 2 (in the attached PDF file ), the program flowchart of the full assembler program of the complex magnitude is shown.

Table 2 Output

Table 3 Cycle count

Table 4 Code Size (Bytes)

Computation of Sine and Cosine for an input angle

Sine and cosine of the input angles is calculated using CORDIC. If the initial Y component of rotation transform is set to zero the rotation mode reduces to:

Where, K is the CORDIC Gain. By setting initial X component to 0.60725 the rotation process produces an unscaled version of sine and cosine term. Since the rotational angle is limited to "90o and +90o additional rotation is required. This is done by exploiting the symmetry property of the sine wave.

The values in other Quadrants are computed by using the relations, Sine (-Z) = -Sine (Z) and Sine (180-Z) = Sine (Z). The absolute value of the input is calculated. If the input is negative (III/IV Quadrant), then sign=1. If absolute value of the input is greater than 1/2 (II/III Quadrant), it is subtracted from 1. If sign=1, the result is negated to give the final sine result.

Implementation.

The input vector Z contains the angle in radians between [-,] which is normalized between (-1, 1) in 1Q15 format (Z=Z rad/). For example, 45o=/4 is equivalent to Z =1/4 =0.25 (8192 in 1Q15 format. Denormalisation is done in the algorithm.

The algorithm is presented in a 'C' like pseudo code. Note that the Cos Θ constant for this algorithm is 0.60725. We also assume that the 12 values of tan-1 (1/2i) are stored as a look up table in 4Q12 format.

Using C166SV2 instruction set, the micro rotations according to Equation (7) is given below as reference implementation. A fixed point number representation is used for the implementation. The registers R1, R2, R11 and R13 are assigned with X, Y, Z, shift value respectively.

Label3: 
    MOV     R12,#0h 
    MOV     R3,R1
    MOV     R5,R2
    ASHR    R5,#0fh
    CMP     R5,#0
    JMPA     cc_NZ, Label1


; Micro rotation 1


;I=I+(Q>>K) MOV R7,R2
    ASHR     R7,R13
    MOV     MAH,R1
    CoADD   R12,R7
CoSTORE   R1,MAS
;Q=Q-(I_tmp>>K) 
    ASHR     R3,R13
    MOV     MAH,R2
    CoSUB   R12,R3
CoSTORE  R2,MAS
;Z=Z+atan(K[L]) 
    MOV     MAH,R11
    CoADD   R12,[R4+] 
CoSTORE   R11,MAS 
    JMPA     cc_NZ,Label 2 
    

; Micro rotation 2


Label1: 
;I=I-(Q>>K) 
    MOV     R7, R2
    ASHR     R7,R13
    MOV     MAH,R1
    CoSUB       R12,R7
    CoSTORE  R1,MAS
; Q=Q+ (I_tmp>>K) 
    ASHR     R3, R13
    MOV     MAH, R2
    CoADD      R12, R3
    CoSTORE  R2,MAS
;Z=Z-atan(K[L]) 
    MOV     MAH,R11
    CoSUB       
R12,[R4+] 
    CoSTORE   R11,MAS


Label2: 
    ADD       R13, #1h
    CMPD1  R6,#0h
    JMPR     cc_NZ, Label3

We cannot neglect the angle information as in complex magnitude, since the direction of rotation is dependent on Z Parameter. To denormalize, the input is multiplied by 4DBAh (4Q12 format). Therefore the lookup table value (tan-1 (1/2i)) has to be in 4Q12 format. Due to this the number of iterations is reduced to 12.


Related Reading


More Insights






Currently we allow the following HTML tags in comments:

Single tags

These tags can be used alone and don't need an ending tag.

<br> Defines a single line break

<hr> Defines a horizontal line

Matching tags

These require an ending tag - e.g. <i>italic text</i>

<a> Defines an anchor

<b> Defines bold text

<big> Defines big text

<blockquote> Defines a long quotation

<caption> Defines a table caption

<cite> Defines a citation

<code> Defines computer code text

<em> Defines emphasized text

<fieldset> Defines a border around elements in a form

<h1> This is heading 1

<h2> This is heading 2

<h3> This is heading 3

<h4> This is heading 4

<h5> This is heading 5

<h6> This is heading 6

<i> Defines italic text

<p> Defines a paragraph

<pre> Defines preformatted text

<q> Defines a short quotation

<samp> Defines sample computer code text

<small> Defines small text

<span> Defines a section in a document

<s> Defines strikethrough text

<strike> Defines strikethrough text

<strong> Defines strong text

<sub> Defines subscripted text

<sup> Defines superscripted text

<u> Defines underlined text

Dr. Dobb's encourages readers to engage in spirited, healthy debate, including taking us to task. However, Dr. Dobb's moderates all comments posted to our site, and reserves the right to modify or remove any content that it determines to be derogatory, offensive, inflammatory, vulgar, irrelevant/off-topic, racist or obvious marketing or spam. Dr. Dobb's further reserves the right to disable the profile of any commenter participating in said activities.

 
Disqus Tips To upload an avatar photo, first complete your Disqus profile. | View the list of supported HTML tags you can use to style comments. | Please read our commenting policy.