 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.

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
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
CoSTORE R2,MAS

Label2:
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
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
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
CoSTORE  R2,MAS
;Z=Z-atan(K[L])
MOV     MAH,R11
CoSUB
R12,[R4+]
CoSTORE   R11,MAS

Label2:
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.

More Insights 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.

Featured Reports Featured Whitepapers 