Update repo

This commit is contained in:
hangpersonal committed 2026-08-30 23:04:35 -07:00
1 parent 749dab5721
commit ce65a0f59a
14950 files changed
+4408250 -1

No files matched your search

@@ -0,0 +1,344 @@
***********************************************************************
* File: IQNacos.asm
*
* Description: IQmath function for inverse cosine
*
* Devices: C28x family
*
* Function Prototype: long IQNacos(long)
*
* C Useage: y = IQNacos(x);
*
* Input Parameters: x = angle (radians)
*
* Return Value: y = result
*
* Author: DA, Texas Instruments Inc.
*
* History:
* 06/10/04 - original
*
* Notes:
* 1) IQ format is limited to I31Q1 to I3Q29 inclusive. Formats
* greater than Q29 are excluded since they would overflow return
* angles greater than 2 (e.g., y = pi).
*
* 2) This function uses the identity: acos(x) = pi/2 - asin(x).
* It simply calls IQasin(x), and then forms (pi/2 - IQasin(x))
*
***********************************************************************
;;#############################################################################
;;!
;;! Copyright: Copyright (C) 2023 Texas Instruments Incorporated -
;;! All rights reserved not granted herein.
;;! Limited License.
;;!
;;! Texas Instruments Incorporated grants a world-wide, royalty-free,
;;! non-exclusive license under copyrights and patents it now or hereafter
;;! owns or controls to make, have made, use, import, offer to sell and sell
;;! ("Utilize") this software subject to the terms herein. With respect to the
;;! foregoing patent license, such license is granted solely to the extent that
;;! any such patent is necessary to Utilize the software alone. The patent
;;! license shall not apply to any combinations which include this software,
;;! other than combinations with devices manufactured by or for TI
;;! ("TI Devices").
;;! No hardware patent is licensed hereunder.
;;!
;;! Redistributions must preserve existing copyright notices and reproduce this
;;! license (including the above copyright notice and the disclaimer and
;;! (if applicable) source code license limitations below) in the documentation
;;! and/or other materials provided with the distribution.
;;!
;;! Redistribution and use in binary form, without modification, are permitted
;;! provided that the following conditions are met:
;;!
;;! * No reverse engineering, decompilation, or disassembly of this software is
;;! permitted with respect to any software provided in binary form.
;;! * Any redistribution and use are licensed by TI for use only
;;! with TI Devices.
;;! * Nothing shall obligate TI to provide you with source code for the
;;! software licensed and provided to you in object code.
;;!
;;! If software source code is provided to you, modification and redistribution
;;! of the source code are permitted provided that the following conditions
;;! are met:
;;!
;;! * any redistribution and use of the source code, including any resulting
;;! derivative works, are licensed by TI for use only with TI Devices.
;;! * any redistribution and use of any object code compiled from the source
;;! code and any resulting derivative works, are licensed by TI for use
;;! only with TI Devices.
;;!
;;! Neither the name of Texas Instruments Incorporated nor the names of its
;;! suppliers may be used to endorse or promote products derived from this
;;! software without specific prior written permission.
;;#############################################################################
***********************************************************************
* Macro start
***********************************************************************
IQNacos: .macro q_value, __IQasin
;----------------------------------------------------------------------
; Call IQasin()
;----------------------------------------------------------------------
LCR #__IQasin ; call _IQasin()
;----------------------------------------------------------------------
; Convert y to I1Q29 format
;----------------------------------------------------------------------
.if q_value == 29
;do nothing
.elseif q_value >= 13
LSL ACC, (29 - q_value)
.else
LSL ACC, #16
LSL ACC, (13 - q_value)
.endif
PUSH ACC ; save y on stack
;----------------------------------------------------------------------
; Form (pi/2 - y) to apply acos(x) = pi/2 - asin(x)
;----------------------------------------------------------------------
MOV AH, #12867
MOV AL, #63144 ; ACC = _IQ29(pi/2)
SUBL ACC, *--SP ; ACC = (pi/2 - y)
;----------------------------------------------------------------------
; Convert back to original IQ format
;----------------------------------------------------------------------
.if q_value == 29
;do nothing
.elseif q_value >= 13
SFR ACC, (29 - q_value)
.else
SFR ACC, #16
SFR ACC, (13 - q_value)
.endif
;----------------------------------------------------------------------
; Return
;----------------------------------------------------------------------
LRETR ; return
.endm
;----------------------------------------------------------------------
; End of macro
;----------------------------------------------------------------------
***********************************************************************
* The value "GLOBAL_Q" needs to be supplied by the assembler using the
* "-dGLOBAL_Q=q_value" directive:
***********************************************************************
.sect "IQmath"
.if GLOBAL_Q == 29
.def __IQ29acos
.ref __IQ29asin
__IQ29acos:
IQNacos GLOBAL_Q, __IQ29asin
.endif
.if GLOBAL_Q == 28
.def __IQ28acos
.ref __IQ28asin
__IQ28acos:
IQNacos GLOBAL_Q, __IQ28asin
.endif
.if GLOBAL_Q == 27
.def __IQ27acos
.ref __IQ27asin
__IQ27acos:
IQNacos GLOBAL_Q, __IQ27asin
.endif
.if GLOBAL_Q == 26
.def __IQ26acos
.ref __IQ26asin
__IQ26acos:
IQNacos GLOBAL_Q, __IQ26asin
.endif
.if GLOBAL_Q == 25
.def __IQ25acos
.ref __IQ25asin
__IQ25acos:
IQNacos GLOBAL_Q, __IQ25asin
.endif
.if GLOBAL_Q == 24
.def __IQ24acos
.ref __IQ24asin
__IQ24acos:
IQNacos GLOBAL_Q, __IQ24asin
.endif
.if GLOBAL_Q == 23
.def __IQ23acos
.ref __IQ23asin
__IQ23acos:
IQNacos GLOBAL_Q, __IQ23asin
.endif
.if GLOBAL_Q == 22
.def __IQ22acos
.ref __IQ22asin
__IQ22acos:
IQNacos GLOBAL_Q, __IQ22asin
.endif
.if GLOBAL_Q == 21
.def __IQ21acos
.ref __IQ21asin
__IQ21acos:
IQNacos GLOBAL_Q, __IQ21asin
.endif
.if GLOBAL_Q == 20
.def __IQ20acos
.ref __IQ20asin
__IQ20acos:
IQNacos GLOBAL_Q, __IQ20asin
.endif
.if GLOBAL_Q == 19
.def __IQ19acos
.ref __IQ19asin
__IQ19acos:
IQNacos GLOBAL_Q, __IQ19asin
.endif
.if GLOBAL_Q == 18
.def __IQ18acos
.ref __IQ18asin
__IQ18acos:
IQNacos GLOBAL_Q, __IQ18asin
.endif
.if GLOBAL_Q == 17
.def __IQ17acos
.ref __IQ17asin
__IQ17acos:
IQNacos GLOBAL_Q, __IQ17asin
.endif
.if GLOBAL_Q == 16
.def __IQ16acos
.ref __IQ16asin
__IQ16acos:
IQNacos GLOBAL_Q, __IQ16asin
.endif
.if GLOBAL_Q == 15
.def __IQ15acos
.ref __IQ15asin
__IQ15acos:
IQNacos GLOBAL_Q, __IQ15asin
.endif
.if GLOBAL_Q == 14
.def __IQ14acos
.ref __IQ14asin
__IQ14acos:
IQNacos GLOBAL_Q, __IQ14asin
.endif
.if GLOBAL_Q == 13
.def __IQ13acos
.ref __IQ13asin
__IQ13acos:
IQNacos GLOBAL_Q, __IQ13asin
.endif
.if GLOBAL_Q == 12
.def __IQ12acos
.ref __IQ12asin
__IQ12acos:
IQNacos GLOBAL_Q, __IQ12asin
.endif
.if GLOBAL_Q == 11
.def __IQ11acos
.ref __IQ11asin
__IQ11acos:
IQNacos GLOBAL_Q, __IQ11asin
.endif
.if GLOBAL_Q == 10
.def __IQ10acos
.ref __IQ10asin
__IQ10acos:
IQNacos GLOBAL_Q, __IQ10asin
.endif
.if GLOBAL_Q == 9
.def __IQ9acos
.ref __IQ9asin
__IQ9acos:
IQNacos GLOBAL_Q, __IQ9asin
.endif
.if GLOBAL_Q == 8
.def __IQ8acos
.ref __IQ8asin
__IQ8acos:
IQNacos GLOBAL_Q, __IQ8asin
.endif
.if GLOBAL_Q == 7
.def __IQ7acos
.ref __IQ7asin
__IQ7acos:
IQNacos GLOBAL_Q, __IQ7asin
.endif
.if GLOBAL_Q == 6
.def __IQ6acos
.ref __IQ6asin
__IQ6acos:
IQNacos GLOBAL_Q, __IQ6asin
.endif
.if GLOBAL_Q == 5
.def __IQ5acos
.ref __IQ5asin
__IQ5acos:
IQNacos GLOBAL_Q, __IQ5asin
.endif
.if GLOBAL_Q == 4
.def __IQ4acos
.ref __IQ4asin
__IQ4acos:
IQNacos GLOBAL_Q, __IQ4asin
.endif
.if GLOBAL_Q == 3
.def __IQ3acos
.ref __IQ3asin
__IQ3acos:
IQNacos GLOBAL_Q, __IQ3asin
.endif
.if GLOBAL_Q == 2
.def __IQ2acos
.ref __IQ2asin
__IQ2acos:
IQNacos GLOBAL_Q, __IQ2asin
.endif
.if GLOBAL_Q == 1
.def __IQ1acos
.ref __IQ1asin
__IQ1acos:
IQNacos GLOBAL_Q, __IQ1asin
.endif
;----------------------------------------------------------------------
; End of file
;----------------------------------------------------------------------
@@ -0,0 +1,425 @@
***********************************************************************
* File: IQNasin.asm
*
* Description: IQmath function for inverse sine
*
* Devices: C28x family
*
* Function Prototype: long IQNasin(long)
*
* C Useage: y = IQNasin(x);
*
* Input Parameters: x = angle (radians)
*
* Return Value: y = result
*
* Author: DA, Texas Instruments Inc.
*
* History:
* 06/10/04 - original
*
* Notes:
* 1) IQ format is limited to I31Q1 to I3Q29 inclusive. Formats
* greater than Q29 are excluded since they would overflow return
* angles greater than 2 (e.g., y = pi).
*
***********************************************************************
;;#############################################################################
;;!
;;! Copyright: Copyright (C) 2023 Texas Instruments Incorporated -
;;! All rights reserved not granted herein.
;;! Limited License.
;;!
;;! Texas Instruments Incorporated grants a world-wide, royalty-free,
;;! non-exclusive license under copyrights and patents it now or hereafter
;;! owns or controls to make, have made, use, import, offer to sell and sell
;;! ("Utilize") this software subject to the terms herein. With respect to the
;;! foregoing patent license, such license is granted solely to the extent that
;;! any such patent is necessary to Utilize the software alone. The patent
;;! license shall not apply to any combinations which include this software,
;;! other than combinations with devices manufactured by or for TI
;;! ("TI Devices").
;;! No hardware patent is licensed hereunder.
;;!
;;! Redistributions must preserve existing copyright notices and reproduce this
;;! license (including the above copyright notice and the disclaimer and
;;! (if applicable) source code license limitations below) in the documentation
;;! and/or other materials provided with the distribution.
;;!
;;! Redistribution and use in binary form, without modification, are permitted
;;! provided that the following conditions are met:
;;!
;;! * No reverse engineering, decompilation, or disassembly of this software is
;;! permitted with respect to any software provided in binary form.
;;! * Any redistribution and use are licensed by TI for use only
;;! with TI Devices.
;;! * Nothing shall obligate TI to provide you with source code for the
;;! software licensed and provided to you in object code.
;;!
;;! If software source code is provided to you, modification and redistribution
;;! of the source code are permitted provided that the following conditions
;;! are met:
;;!
;;! * any redistribution and use of the source code, including any resulting
;;! derivative works, are licensed by TI for use only with TI Devices.
;;! * any redistribution and use of any object code compiled from the source
;;! code and any resulting derivative works, are licensed by TI for use
;;! only with TI Devices.
;;!
;;! Neither the name of Texas Instruments Incorporated nor the names of its
;;! suppliers may be used to endorse or promote products derived from this
;;! software without specific prior written permission.
;;#############################################################################
***********************************************************************
* References and Definitions
***********************************************************************
.ref __IQ29sqrt
.ref _IQasinTable
.asg XT, Xsign
.asg T, sign
.asg XAR5, XiTransform
.asg AR5, iTransform
.asg XAR6, x
.asg XAR6, y
.asg XAR7, c
TaylorSeriesOrder .set 5
***********************************************************************
* Macro start
***********************************************************************
IQNasin: .macro q_value
;----------------------------------------------------------------------
; Miscellaneous initialization
;----------------------------------------------------------------------
SETC SXM ; enable sign-extension
;----------------------------------------------------------------------
; Convert x to I3Q29 format
;----------------------------------------------------------------------
.if q_value == 29
;do nothing
.elseif q_value >= 13
LSL ACC, (29 - q_value)
.else
LSL ACC, #16
LSL ACC, (13 - q_value)
.endif
;----------------------------------------------------------------------
; Determine if x<0 (we will apply the identity sin(-x) = -sin(x))
;----------------------------------------------------------------------
MOV sign, #1 ; assume sign = 1
BF L1, GEQ ; branch if x >= 0
MOV sign, #(-1) ; sign = -1
NEG ACC ; x = -x
L1:
MOVL x, ACC ; save x
PUSH Xsign ; save sign on stack
;----------------------------------------------------------------------
; If x > 0.5, we need to apply the transform
;----------------------------------------------------------------------
MOVB XiTransform, #0 ; assume no transform needed
SUB ACC, #2000h << 15 ; subtract _IQ29(0.5) from x
BF L2, LT ; branch if x < 0.5
;----------------------------------------------------------------------
; Apply the transform: x = _IQ29sqrt( (_IQ29(1.0) - x) >> 1 )
;----------------------------------------------------------------------
MOV ACC, #4000h << 15 ; ACC = _IQ29(1.0)
SUBL ACC, x ; subtract x
SFR ACC, 1 ; shift right by 1
LCR #__IQ29sqrt ; call _IQ29sqrt()
MOVL x, ACC ; save x
MOVB XiTransform, #1 ; iTransform = 1
;----------------------------------------------------------------------
; Determine the lookup table index. The table contains 2^N+1 = 17 points
; over the interval 0 to 0.5, inclusive (i.e., pt.0 = 0, pt.17 = 0.5).
; Therefore:
; index = bits(28:24) of (x + half the interval)
; = bits(28:24) of [x + (0.5/2^N)/2]
; = bits(28:24) of [x + (0.5/2^4)/2]
; = bits(28:24) of [x + 0.015625]
;
; LutIndex = ((x + _IQ29(0.015625)) >> 24) & 0x001F;
;----------------------------------------------------------------------
L2:
MOVL ACC, x ; load x
ADD ACC, #0100h << 15 ; add _IQ29(0.015625)
MOV T,#24 ; T = 24
ASRL ACC,T ; ACC >> 24
ANDB AL, #001fh ; ACC = LutIndex
;----------------------------------------------------------------------
; Setup the coefficient pointer
; c = &IQasinTable[0] + (TaylorSeriesOrder * LutIndex)
;----------------------------------------------------------------------
MOV T,AL ; T = LutIndex
MPYB ACC, T, #TaylorSeriesOrder ; ACC = TaylorSeriesOrder * LutIndex
MOVL c, #_IQasinTable ; c = &IQasinTable[0]
LSL ACC, 1 ; shift offset by 1 since offset is for 32-bit data values
ADDL c, ACC ; c = &IQasinTable[0] + (TaylorSeriesOrder * LutIndex)
;----------------------------------------------------------------------
; Perform the computations:
; y = _IQ29mpy((_IQ29mpy((_IQ29mpy((_IQ29mpy(*c++, x) + *c++), x) + *c++) , x) + *c++), x) + *c;
;----------------------------------------------------------------------
MOVL XT,*c++
IMPYL P,XT,x
QMPYL ACC,XT,x
LSL64 ACC:P,#3 ; _IQ29mpy(*c++, x)
ADDL ACC,*c++ ; _IQ29mpy(*c++, x) + *c++
MOVL XT,ACC
IMPYL P,XT,x
QMPYL ACC,XT,x
LSL64 ACC:P,#3 ; _IQ29mpy((_IQ29mpy(*c++, x) + *c++), x)
ADDL ACC,*c++ ; _IQ29mpy((_IQ29mpy(*c++, x) + *c++), x) + *c++
MOVL XT,ACC
IMPYL P,XT,x
QMPYL ACC,XT,x
LSL64 ACC:P,#3 ; _IQ29mpy((_IQ29mpy((_IQ29mpy(*c++, x) + *c++), x) + *c++) , x)
ADDL ACC,*c++ ; _IQ29mpy((_IQ29mpy((_IQ29mpy(*c++, x) + *c++), x) + *c++) , x) + *c++
MOVL XT,ACC
IMPYL P,XT,x
QMPYL ACC,XT,x
LSL64 ACC:P,#3 ; _IQ29mpy((_IQ29mpy((_IQ29mpy((_IQ29mpy(*c++, x) + *c++), x) + *c++) , x) + *c++), x)
ADDL ACC,*+c[0] ; _IQ29mpy((_IQ29mpy((_IQ29mpy((_IQ29mpy(*c++, x) + *c++), x) + *c++) , x) + *c++), x) + *c
;----------------------------------------------------------------------
; Further computation needed if the transform was applied:
; y = _IQ29(pi/2) - (y << 1);
;----------------------------------------------------------------------
TBIT iTransform, #0 ; test bit 0
SBF L3, NTC ; branch if transform not applied
LSL ACC, 1 ; ACC = y << 1
MOVL y, ACC ; store (y << 1)
MOV AH, #12867
MOV AL, #63144 ; ACC = _IQ29(pi/2)
SUBL ACC, y ; ACC = _IQ29(pi/2) - (y << 1)
;----------------------------------------------------------------------
; Apply the sign, convert back to original IQ format, and return:
; return((sign * y) >> (29 - GLOBAL_Q))
;----------------------------------------------------------------------
L3:
POP Xsign ; retrieve the sign
MOVX TL, sign ; get the XT properly sign extended
IMPYL ACC, XT, ACC ; ACC = sign * y
.if q_value == 29
;do nothing
.elseif q_value >= 13
SFR ACC, (29 - q_value) ; ACC = (sign * y) >> (29 - GLOBAL_Q)
.else
SFR ACC, #16
SFR ACC, (13 - q_value) ; ACC = (sign * y) >> (29 - GLOBAL_Q)
.endif
LRETR ; return
.endm
;----------------------------------------------------------------------
; End of macro
;----------------------------------------------------------------------
***********************************************************************
* The value "GLOBAL_Q" needs to be supplied by the assembler using the
* "-dGLOBAL_Q=q_value" directive:
***********************************************************************
.sect "IQmath"
.if GLOBAL_Q == 29
.def __IQ29asin
__IQ29asin:
IQNasin GLOBAL_Q
.endif
.if GLOBAL_Q == 28
.def __IQ28asin
__IQ28asin:
IQNasin GLOBAL_Q
.endif
.if GLOBAL_Q == 27
.def __IQ27asin
__IQ27asin:
IQNasin GLOBAL_Q
.endif
.if GLOBAL_Q == 26
.def __IQ26asin
__IQ26asin:
IQNasin GLOBAL_Q
.endif
.if GLOBAL_Q == 25
.def __IQ25asin
__IQ25asin:
IQNasin GLOBAL_Q
.endif
.if GLOBAL_Q == 24
.def __IQ24asin
__IQ24asin:
IQNasin GLOBAL_Q
.endif
.if GLOBAL_Q == 23
.def __IQ23asin
__IQ23asin:
IQNasin GLOBAL_Q
.endif
.if GLOBAL_Q == 22
.def __IQ22asin
__IQ22asin:
IQNasin GLOBAL_Q
.endif
.if GLOBAL_Q == 21
.def __IQ21asin
__IQ21asin:
IQNasin GLOBAL_Q
.endif
.if GLOBAL_Q == 20
.def __IQ20asin
__IQ20asin:
IQNasin GLOBAL_Q
.endif
.if GLOBAL_Q == 19
.def __IQ19asin
__IQ19asin:
IQNasin GLOBAL_Q
.endif
.if GLOBAL_Q == 18
.def __IQ18asin
__IQ18asin:
IQNasin GLOBAL_Q
.endif
.if GLOBAL_Q == 17
.def __IQ17asin
__IQ17asin:
IQNasin GLOBAL_Q
.endif
.if GLOBAL_Q == 16
.def __IQ16asin
__IQ16asin:
IQNasin GLOBAL_Q
.endif
.if GLOBAL_Q == 15
.def __IQ15asin
__IQ15asin:
IQNasin GLOBAL_Q
.endif
.if GLOBAL_Q == 14
.def __IQ14asin
__IQ14asin:
IQNasin GLOBAL_Q
.endif
.if GLOBAL_Q == 13
.def __IQ13asin
__IQ13asin:
IQNasin GLOBAL_Q
.endif
.if GLOBAL_Q == 12
.def __IQ12asin
__IQ12asin:
IQNasin GLOBAL_Q
.endif
.if GLOBAL_Q == 11
.def __IQ11asin
__IQ11asin:
IQNasin GLOBAL_Q
.endif
.if GLOBAL_Q == 10
.def __IQ10asin
__IQ10asin:
IQNasin GLOBAL_Q
.endif
.if GLOBAL_Q == 9
.def __IQ9asin
__IQ9asin:
IQNasin GLOBAL_Q
.endif
.if GLOBAL_Q == 8
.def __IQ8asin
__IQ8asin:
IQNasin GLOBAL_Q
.endif
.if GLOBAL_Q == 7
.def __IQ7asin
__IQ7asin:
IQNasin GLOBAL_Q
.endif
.if GLOBAL_Q == 6
.def __IQ6asin
__IQ6asin:
IQNasin GLOBAL_Q
.endif
.if GLOBAL_Q == 5
.def __IQ5asin
__IQ5asin:
IQNasin GLOBAL_Q
.endif
.if GLOBAL_Q == 4
.def __IQ4asin
__IQ4asin:
IQNasin GLOBAL_Q
.endif
.if GLOBAL_Q == 3
.def __IQ3asin
__IQ3asin:
IQNasin GLOBAL_Q
.endif
.if GLOBAL_Q == 2
.def __IQ2asin
__IQ2asin:
IQNasin GLOBAL_Q
.endif
.if GLOBAL_Q == 1
.def __IQ1asin
__IQ1asin:
IQNasin GLOBAL_Q
.endif
;----------------------------------------------------------------------
; End of file
;----------------------------------------------------------------------
@@ -0,0 +1,188 @@
***********************************************************************
* File: IQNasinTable.asm
* Devices: TMS320F2812, TMS320F2811, TMS320F2810
* Author: DA, Texas Instruments Inc.
* History:
* 04/29/04 - original (DA)
***********************************************************************
;;#############################################################################
;;!
;;! Copyright: Copyright (C) 2023 Texas Instruments Incorporated -
;;! All rights reserved not granted herein.
;;! Limited License.
;;!
;;! Texas Instruments Incorporated grants a world-wide, royalty-free,
;;! non-exclusive license under copyrights and patents it now or hereafter
;;! owns or controls to make, have made, use, import, offer to sell and sell
;;! ("Utilize") this software subject to the terms herein. With respect to the
;;! foregoing patent license, such license is granted solely to the extent that
;;! any such patent is necessary to Utilize the software alone. The patent
;;! license shall not apply to any combinations which include this software,
;;! other than combinations with devices manufactured by or for TI
;;! ("TI Devices").
;;! No hardware patent is licensed hereunder.
;;!
;;! Redistributions must preserve existing copyright notices and reproduce this
;;! license (including the above copyright notice and the disclaimer and
;;! (if applicable) source code license limitations below) in the documentation
;;! and/or other materials provided with the distribution.
;;!
;;! Redistribution and use in binary form, without modification, are permitted
;;! provided that the following conditions are met:
;;!
;;! * No reverse engineering, decompilation, or disassembly of this software is
;;! permitted with respect to any software provided in binary form.
;;! * Any redistribution and use are licensed by TI for use only
;;! with TI Devices.
;;! * Nothing shall obligate TI to provide you with source code for the
;;! software licensed and provided to you in object code.
;;!
;;! If software source code is provided to you, modification and redistribution
;;! of the source code are permitted provided that the following conditions
;;! are met:
;;!
;;! * any redistribution and use of the source code, including any resulting
;;! derivative works, are licensed by TI for use only with TI Devices.
;;! * any redistribution and use of any object code compiled from the source
;;! code and any resulting derivative works, are licensed by TI for use
;;! only with TI Devices.
;;!
;;! Neither the name of Texas Instruments Incorporated nor the names of its
;;! suppliers may be used to endorse or promote products derived from this
;;! software without specific prior written permission.
;;#############################################################################
;;==========================================================================
;; IQasin Function Table, Size Of Table = 85x16
;;==========================================================================
.def _IQasinTable
.sect "IQmathTablesRam"
_IQasinTable:
; for f(x) = c4*x^4 + c3*x^3 + c2*x^2 + c1*x + c0
;point 0 (x=0)
point0:
.long 0 ; c4 for point 0 = 0.00000000000000 in Q29
.long 89478485 ; c3 for point 0 = 0.16666666666667 in Q29
.long 0 ; c2 for point 0 = 0.00000000000000 in Q29
.long 536870912 ; c1 for point 0 = 1.00000000000000 in Q29
.long 0 ; c0 for point 0 = 0.00000000000000 in Q29
;point 1 (x=0.03125)
point1:
.long 6317117 ; c4 for point 1 = 0.01176654801806 in Q29
.long 89082863 ; c3 for point 1 = 0.16592976253570 in Q29
.long 12378 ; c2 for point 1 = 0.00002305630012 in Q29
.long 536870718 ; c1 for point 1 = 0.99999963945303 in Q29
.long 1 ; c0 for point 1 = 0.00000000225472 in Q29
;point 2 (x=0.06250)
point2:
.long 12789687 ; c4 for point 2 = 0.02382264819581 in Q29
.long 87866810 ; c3 for point 2 = 0.16366468695565 in Q29
.long 101217 ; c2 for point 2 = 0.00018853150967 in Q29
.long 536867739 ; c1 for point 2 = 0.99999408944239 in Q29
.long 40 ; c0 for point 2 = 0.00000007405140 in Q29
;point 3 (x=0.09375)
point3:
.long 19580708 ; c4 for point 3 = 0.03647191042431 in Q29
.long 85740406 ; c3 for point 3 = 0.15970395152535 in Q29
.long 354217 ; c2 for point 3 = 0.00065978084316 in Q29
.long 536854190 ; c1 for point 3 = 0.99996885226392 in Q29
.long 315 ; c0 for point 3 = 0.00000058698337 in Q29
;point 4 (x=0.1250)
point4:
.long 26868885 ; c4 for point 4 = 0.05004719802685 in Q29
.long 82545646 ; c3 for point 4 = 0.15375324754641 in Q29
.long 882934 ; c2 for point 4 = 0.00164459220320 in Q29
.long 536815043 ; c1 for point 4 = 0.99989593609382 in Q29
.long 1409 ; c0 for point 4 = 0.00000262453734 in Q29
;point 5 (x=0.15625)
point5:
.long 34858083 ; c4 for point 5 = 0.06492823848730 in Q29
.long 78043250 ; c3 for point 5 = 0.14536688064656 in Q29
.long 1838350 ; c2 for point 5 = 0.00342419379134 in Q29
.long 536724572 ; c1 for point 5 = 0.99972742028566 in Q29
.long 4634 ; c0 for point 5 = 0.00000863223157 in Q29
;point 6 (x=0.18750)
point6:
.long 43788925 ; c4 for point 6 = 0.08156322849045 in Q29
.long 71892080 ; c3 for point 6 = 0.13390943393951 in Q29
.long 3431456 ; c2 for point 6 = 0.00639158410284 in Q29
.long 536540695 ; c1 for point 6 = 0.99938492317316 in Q29
.long 12614 ; c0 for point 6 = 0.00002349611964 in Q29
;point 7 (x=0.21875)
point7:
.long 53953665 ; c4 for point 7 = 0.10049653164769 in Q29
.long 63618641 ; c3 for point 7 = 0.11849895225606 in Q29
.long 5961683 ; c2 for point 7 = 0.01110450018835 in Q29
.long 536196110 ; c1 for point 7 = 0.99874308390293 in Q29
.long 30246 ; c0 for point 7 = 0.00005633823023 in Q29
;point 8 (x=0.25000)
point8:
.long 65715899 ; c4 for point 8 = 0.12240539958384 in Q29
.long 52572719 ; c3 for point 8 = 0.09792431966707 in Q29
.long 9857385 ; c2 for point 8 = 0.01836080993758 in Q29
.long 535584573 ; c1 for point 8 = 0.99760400660829 in Q29
.long 66298 ; c0 for point 8 = 0.00012348928199 in Q29
;point 9 (x=0.28125)
point9:
.long 79537402 ; c4 for point 9 = 0.14814995596061 in Q29
.long 37863112 ; c3 for point 9 = 0.07052554134789 in Q29
.long 15734668 ; c2 for point 9 = 0.02930810346023 in Q29
.long 534539695 ; c1 for point 9 = 0.99565777006874 in Q29
.long 136037 ; c0 for point 9 = 0.00025338900082 in Q29
;point 10 (x=0.31250)
point10:
.long 96015466 ; c4 for point 10 = 0.17884274290911 in Q29
.long 18264067 ; c3 for point 10 = 0.03401947530154 in Q29
.long 24484384 ; c2 for point 10 = 0.04560571931992 in Q29
.long 532802024 ; c1 for point 10 = 0.99242110469066 in Q29
.long 265566 ; c0 for point 10 = 0.00049465588355 in Q29
;point 11 (x=0.34375)
point11:
.long 115935880 ; c4 for point 11 = 0.21594740492715 in Q29
.long -7922211 ; c3 for point 11 = -0.01475626778059 in Q29
.long 37402735 ; c2 for point 11 = 0.06966802370550 in Q29
.long 529967480 ; c1 for point 11 = 0.98714135522368 in Q29
.long 498974 ; c0 for point 11 = 0.00092941064669 in Q29
;point 12 (x=0.37500)
point12:
.long 140349533 ; c4 for point 12 = 0.26142137657237 in Q29
.long -43070227 ; c3 for point 12 = -0.08022454942433 in Q29
.long 56390437 ; c2 for point 12 = 0.10503537451569 in Q29
.long 525405717 ; c1 for point 12 = 0.97864440922208 in Q29
.long 910213 ; c0 for point 12 = 0.00169540313020 in Q29
;point 13 (x=0.40625)
point13:
.long 170685304 ; c4 for point 13 = 0.31792615376935 in Q29
.long -90540603 ; c3 for point 13 = -0.16864501479554 in Q29
.long 84261400 ; c2 for point 13 = 0.15694908812191 in Q29
.long 518129119 ; c1 for point 13 = 0.96509068936447 in Q29
.long 1623008 ; c0 for point 13 = 0.00302308763644 in Q29
;point 14 (x=0.43750)
point14:
.long 208919771 ; c4 for point 14 = 0.38914339775461 in Q29
.long -155156187 ; c3 for point 14 = -0.28900091994247 in Q29
.long 125229731 ; c2 for point 14 = 0.23325855153370 in Q29
.long 506579341 ; c1 for point 14 = 0.94357755204347 in Q29
.long 2844600 ; c0 for point 14 = 0.00529848115859 in Q29
;point 15 (x=0.46875)
point15:
.long 257837857 ; c4 for point 15 = 0.48026043442295 in Q29
.long -243949525 ; c3 for point 15 = -0.45439139960266 in Q29
.long 185693290 ; c2 for point 15 = 0.34588070642185 in Q29
.long 488273269 ; c1 for point 15 = 0.90947983687988 in Q29
.long 4923808 ; c0 for point 15 = 0.00917130774387 in Q29
;point 16 (x=0.50000)
point16:
.long 321442661 ; c4 for point 16 = 0.59873361249295 in Q29
.long -367363041 ; c3 for point 16 = -0.68426698570622 in Q29
.long 275522281 ; c2 for point 16 = 0.51320023927967 in Q29
.long 459203801 ; c1 for point 16 = 0.85533373213278 in Q29
.long 8452695 ; c0 for point 16 = 0.01574437214446 in Q29
;end of IQasinTable
.end
;end file IQNasinTable.asm
@@ -0,0 +1,480 @@
;;###########################################################################
;;
;; FILE: IQNatan2.asm
;;
;; TITLE: C Callable IQ Math Function
;;
;;###########################################################################
;;
;; Ver | Date | Who | Description of changes
;; =====|=============|=======|==============================================
;; 1.3 | 19 Nov 2001 | A. T. | Original Release.
;; -----|-------------|-------|----------------------------------------------
;; 1.4 | 17 May 2002 | A. T. | Added note that a value of Q30 is not
;; | | | feasible for this operation. Q range is
;; | | | limited to Q29 to Q1.
;; -----|-------------|-------|----------------------------------------------
;; | | |
;;
;;###########################################################################
;;#############################################################################
;;!
;;! Copyright: Copyright (C) 2023 Texas Instruments Incorporated -
;;! All rights reserved not granted herein.
;;! Limited License.
;;!
;;! Texas Instruments Incorporated grants a world-wide, royalty-free,
;;! non-exclusive license under copyrights and patents it now or hereafter
;;! owns or controls to make, have made, use, import, offer to sell and sell
;;! ("Utilize") this software subject to the terms herein. With respect to the
;;! foregoing patent license, such license is granted solely to the extent that
;;! any such patent is necessary to Utilize the software alone. The patent
;;! license shall not apply to any combinations which include this software,
;;! other than combinations with devices manufactured by or for TI
;;! ("TI Devices").
;;! No hardware patent is licensed hereunder.
;;!
;;! Redistributions must preserve existing copyright notices and reproduce this
;;! license (including the above copyright notice and the disclaimer and
;;! (if applicable) source code license limitations below) in the documentation
;;! and/or other materials provided with the distribution.
;;!
;;! Redistribution and use in binary form, without modification, are permitted
;;! provided that the following conditions are met:
;;!
;;! * No reverse engineering, decompilation, or disassembly of this software is
;;! permitted with respect to any software provided in binary form.
;;! * Any redistribution and use are licensed by TI for use only
;;! with TI Devices.
;;! * Nothing shall obligate TI to provide you with source code for the
;;! software licensed and provided to you in object code.
;;!
;;! If software source code is provided to you, modification and redistribution
;;! of the source code are permitted provided that the following conditions
;;! are met:
;;!
;;! * any redistribution and use of the source code, including any resulting
;;! derivative works, are licensed by TI for use only with TI Devices.
;;! * any redistribution and use of any object code compiled from the source
;;! code and any resulting derivative works, are licensed by TI for use
;;! only with TI Devices.
;;!
;;! Neither the name of Texas Instruments Incorporated nor the names of its
;;! suppliers may be used to endorse or promote products derived from this
;;! software without specific prior written permission.
;;#############################################################################
;;===========================================================================
;; Function: _IQNatan2
;;===========================================================================
;;
;; C Usage: extern long _IQNatan2(long Y, long X);
;;
;;---------------------------------------------------------------------------
;;
;; On Entry: ACC = Y in IQ format
;; SP-4 = X in IQ format
;;
;; Regs Used: XAR7, XAR6, XAR5, XAR4, XAR0, XT, P
;;
;; On Exit: ACC = Radian result in IQ format
;; if( X == 0.0 and Y == 0.0 ), ACC = 0.0
;; if( Y >= 0.0 ), 0.0 <= ACC <= PI
;; if( Y < 0.0 ), -PI < ACC < 0.0
;;
;; Q range: 29 to 1
;;
;; Note: A value of Q30 is not feasible since the result would
;; saturate for angles greater then +/- 2.0 radians.
;;
;;---------------------------------------------------------------------------
;; Algorithm: The algorithm steps to calculate the "atan2" of the given
;; input X and Y is as follows:
;;
;; Step(1): if( abs(X) >= abs(Y) )
;; Numerator = abs(Y)
;; Denominator = abs(X)
;; else
;; Numerator = abs(X)
;; Denominator = abs(Y)
;;
;; Step(2): Ratio = Numerator/Denominator
;;
;; Note: Ratio range = 0.0 to 1.0
;;
;; Step(3): Use the upper 6-bits of the "Ratio" value as an
;; index into the table to obtain the coefficients
;; for a second order equation:
;;
;; _IQNatan2Table:
;; CoeffA0[0]
;; CoeffA1[0]
;; CoeffA2[0]
;; .
;; .
;; CoeffA0[63]
;; CoeffA1[63]
;; CoeffA2[63]
;;
;; Step(4): Calculate the angle using the folowing equation:
;;
;; arctan(Ratio) = A0 + A1*Ratio + A2*Ratio*Ratio
;; arctan(Ratio) = A0 + Ratio(A1 + A2*Ratio)
;;
;; Step(5): The final angle is determined as follows:
;;
;; if( X >= 0 and Y >= 0 and abs(X) >= abs(Y) )
;; Angle = arctan(abs(Y)/abs(X))
;; if( X >= 0 and Y >= 0 and abs(X) < abs(Y) )
;; Angle = PI/2 - arctan(abs(X)/abs(Y))
;; if( X < 0 and Y >= 0 and abs(X) < abs(Y) )
;; Angle = PI/2 + arctan(abs(X)/abs(Y))
;; if( X < 0 and Y >= 0 and abs(X) >= abs(Y) )
;; Angle = PI - arctan(abs(Y)/abs(X))
;; if( Y < 0 )
;; Angle = -Angle
;;
;;---------------------------------------------------------------------------
;; Benchmark:
;; * Code executes from 0-wait SARAM block.
;; * _IQatan2Table & _IQdivTable located in seperate SARAM or ROM block.
;; * Stack located in seperate SARAM block.
;; * Includes LCR/LRETR.
;;
;; Cycles = 108 (if Q == 30 and _IQatan2Table & _IQdivTable in 0-wait SARAM)
;; = 109 (if 16 <= Q <= 29 and _IQatan2Table & _IQdivTable in 0-wait SARAM)
;; = 112 (if 1 <= Q <= 15 and _IQatan2Table & _IQdivTable in 0-wait SARAM)
;; Cycles = 114 (if Q == 30 and _IQatan2Table & _IQdivTable in 1-wait ROM)
;; = 115 (if 16 <= Q <= 29 and _IQatan2Table & _IQdivTable in 1-wait ROM)
;; = 118 (if 1 <= Q <= 15 and _IQatan2Table & _IQdivTable in 1-wait ROM)
;;
;;===========================================================================
IQNatan2 .macro q_value
;; Perform Step 1:
SETC OVM ; Make sure overflow mode is on
MOVL *SP++,ACC ; SP-6 = Y
ABS ACC ; ACC = abs(Y)
MOVL XT,@ACC ; XT = abs(Y)
MOVL ACC,*-SP[6] ; ACC = X
ABS ACC ; ACC = abs(X)
MOVL P,@ACC ; P = abs(X)
SUBL ACC,@XT ; ACC = abs(X) - abs(Y)
MOVL *SP++,ACC ; SP-4 = abs(X) - abs(Y)
MOVL ACC,@XT ; ACC = abs(Y)
MINL ACC,@P ; if( abs(Y) > abs(X) ), ACC = abs(X)
MOVL *SP++,ACC ; SP-2 = Numerator
MOVL ACC,@P ; ACC = abs(X)
MAXL ACC,@XT ; if( abs(X) < abs(Y) ), ACC = abs(Y)
; ACC = Denominator
; SP-10 = X
; SP-8 = Return Address
; SP-6 = Y
; SP-4 = abs(X) - abs(Y)
; SP-2 = Numerator
;; Perform Step 2:
CSB ACC ; Count sign bits, T = n
LSLL ACC,T ; ACC = Dnorm (Q31)
MOVL @XAR4,ACC ; Temp save "Dnorm"
LSR AH,#6
MOVB @AH,#0xFE,EQ ; If Den = 0, load zero value
MOVZ AR0,@AH ; AR0 = index into _IQdivTable
MOVL XAR7,#(_IQdivTable-254)
MOVB AH,#(62 - q_value) ; AH = 62 - q_value
SUBR @T,AH ; T = 62 - q_value - n
MOVL XAR5,@XT ; AR5H = 62 - q_value - n
MOVL XT,*+XAR7[AR0] ; (1) XT = initial Dm estimate (Q29)
MOV @AH,#0x4000
MOVB AL,#0
MOVL XAR6,@ACC ; XAR6 = 2.0 (Q29)
IMPYL P,XT,@XAR4 ; (2)
QMPYL ACC,XT,@XAR4 ; (2) ACC:P = Dm*Dnorm (Q60)
LSL64 ACC:P,#1 ; (3) ACC:P = Dm*Dnorm (Q61)
SUBL ACC,@XAR6 ; (4) ACC = Dm*Dnorm - 2.0 (Q29)
NEG ACC ; (5) ACC = 2.0 - Dm*Dnorm (Q29)
IMPYL P,XT,@ACC ; (6)
QMPYL ACC,XT,@ACC ; (6) ACC:P = Dm(2.0 - Dm*Dnorm) (Q58)
LSL64 ACC:P,#3 ; (7) ACC:P = Dm(2.0 - Dm*Dnorm) (Q61)
MOVL XT,@ACC ; (1) XT = Dm (Q29)
IMPYL P,XT,@XAR4 ; (2)
QMPYL ACC,XT,@XAR4 ; (2) ACC:P = Dm*Dnorm (Q60)
LSL64 ACC:P,#1 ; (3) ACC:P = Dm*Dnorm (Q61)
SUBL ACC,@XAR6 ; (4) ACC = Dm*Dnorm - 2.0 (Q29)
NEG ACC ; (5) ACC = 2.0 - Dm*Dnorm (Q29)
IMPYL P,XT,@ACC ; (6)
QMPYL ACC,XT,@ACC ; (6) ACC:P = Dm(2.0 - Dm*Dnorm) (Q58)
LSL64 ACC:P,#3 ; (7) ACC:P = Dm(2.0 - Dm*Dnorm) (Q61)
MOVL XT,*-SP[2] ; XT = Numerator
IMPYL P,XT,@ACC
QMPYL ACC,XT,@ACC ; ACC:P = N * Dm
LSL64 ACC:P,#2
MOVL XT,@XAR5 ; T = 62 - q_value - n
ASR64 ACC:P,T ; scale back result
; P = Ratio in IQ format
;; Perform Step 3:
MOVL XT,@P ; XT = Ratio
MOVB ACC,#64 ; ACC = 64 = Elements In Table
IMPYL P,XT,@ACC
QMPYL ACC,XT,@ACC ; ACC:P = 64*Ratio
.if q_value >= 16
LSL64 ACC:P,#(32 - q_value) ; ACC = int(64*Ratio)
.else
LSL64 ACC:P,#16
LSL64 ACC:P,#(16 - q_value) ; ACC = int(64*Ratio)
.endif
MPY P,@AL,#6 ; P = 6 * int(64*Ratio)
MOVZ AR0,@PL ; AR0 = _IQatan2Table index
MOVL XAR5,#_IQatan2Table ; Point to A0
MOVL XAR6,#_IQatan2Table+2 ; Point to A1
MOVL XAR7,#_IQatan2Table+4 ; Point to A2
;; Perform Step 4:
IMPYL P,XT,*+XAR7[AR0]
QMPYL ACC,XT,*+XAR7[AR0] ; ACC:P = A2*Ratio (q_value + Q30)
.if q_value >= 16
LSL64 ACC:P,#(32-q_value) ; ACC = A2*Ratio (Q30)
.else
LSL64 ACC:P,#16
LSL64 ACC:P,#(16-q_value) ; ACC = A2*Ratio (Q30)
.endif
ADDL ACC,*+XAR6[AR0] ; ACC = A1 + A2*Ratio (Q30)
IMPYL P,XT,@ACC
QMPYL ACC,XT,@ACC ; ACC:P = Ratio(A1 + A2*Ratio) (q_value + Q30)
.if q_value >= 16
LSL64 ACC:P,#(32-q_value) ; ACC:P = Ratio(A1 + A2*Ratio) (Q30)
.else
LSL64 ACC:P,#16
LSL64 ACC:P,#(16-q_value) ; ACC:P = Ratio(A1 + A2*Ratio) (Q30)
.endif
ADDL ACC,*+XAR5[AR0] ; ACC = A0 + Ratio(A1 + A2*Ratio) (Q30)
.if (q_value >= 14) & (q_value <= 29)
LSR64 ACC:P,#(30-q_value) ; ACC = A0 + Ratio(A1 + A2*Ratio) (q_value)
.endif
.if q_value <= 13
LSR64 ACC:P,#16
LSR64 ACC:P,#(14-q_value) ; ACC = A0 + Ratio(A1 + A2*Ratio) (q_value)
.endif
;; Perform Step 5:
MOVL XAR7,#(_IQatan2HalfPITable + q_value*2)
MOVL XAR6,*XAR7 ; XAR6 = PI/2
MOVL P,@ACC ; P = arctan(Ratio)
NEG ACC ; ACC = -arctan(Ratio)
TBIT *-SP[9],#15 ; Test if(X >= 0)
MOVL @P,ACC,NTC ; if(X >= 0), P = -arctan(Ratio)
MOVL ACC,@P
NEG ACC ; ACC = -P
TBIT *-SP[3],#15 ; Test if(abs(X) >= abs(Y))
MOVL @P,ACC,NTC ; if(abs(X) >= abs(Y)), P = -P
MOVL ACC,@XAR6 ; ACC = PI/2
NEG ACC ; ACC = -PI/2
TBIT *-SP[9],#15 ; Test if(X < 0)
NEGTC ACC ; if(X < 0), ACC = PI/2
ADDL ACC,@XAR6 ; ACC = ACC + PI/2
TBIT *-SP[3],#15 ; Test if(abs(X) >= abs(Y))
MOVL @XAR6,ACC,NTC ; if(abs(X) >= abs(Y)), XAR6 = ACC
MOVL ACC,@XAR6
ADDL ACC,@P ; ACC = arctan(abs(Y),X)
TBIT *-SP[5],#15 ; Test if(Y < 0)
NEGTC ACC ; if(Y < 0), ACC = arctan(Y,X)
SUBB SP,#6 ; Deallocate stack
CLRC OVM ; Turn off overflow mode
LRETR
.endm
;; The value "GLOBAL_Q" needs to be supplied by the assembler
;; using the "-dGLOBAL_Q=q_value" directive:
.sect "IQmath"
.ref _IQatan2HalfPITable
.ref _IQatan2Table
.ref _IQdivTable
.if GLOBAL_Q == 30
.def __IQ30atan2
__IQ30atan2:
IQNatan2 GLOBAL_Q
.endif
.if GLOBAL_Q == 29
.def __IQ29atan2
__IQ29atan2:
IQNatan2 GLOBAL_Q
.endif
.if GLOBAL_Q == 28
.def __IQ28atan2
__IQ28atan2:
IQNatan2 GLOBAL_Q
.endif
.if GLOBAL_Q == 27
.def __IQ27atan2
__IQ27atan2:
IQNatan2 GLOBAL_Q
.endif
.if GLOBAL_Q == 26
.def __IQ26atan2
__IQ26atan2:
IQNatan2 GLOBAL_Q
.endif
.if GLOBAL_Q == 25
.def __IQ25atan2
__IQ25atan2:
IQNatan2 GLOBAL_Q
.endif
.if GLOBAL_Q == 24
.def __IQ24atan2
__IQ24atan2:
IQNatan2 GLOBAL_Q
.endif
.if GLOBAL_Q == 23
.def __IQ23atan2
__IQ23atan2:
IQNatan2 GLOBAL_Q
.endif
.if GLOBAL_Q == 22
.def __IQ22atan2
__IQ22atan2:
IQNatan2 GLOBAL_Q
.endif
.if GLOBAL_Q == 21
.def __IQ21atan2
__IQ21atan2:
IQNatan2 GLOBAL_Q
.endif
.if GLOBAL_Q == 20
.def __IQ20atan2
__IQ20atan2:
IQNatan2 GLOBAL_Q
.endif
.if GLOBAL_Q == 19
.def __IQ19atan2
__IQ19atan2:
IQNatan2 GLOBAL_Q
.endif
.if GLOBAL_Q == 18
.def __IQ18atan2
__IQ18atan2:
IQNatan2 GLOBAL_Q
.endif
.if GLOBAL_Q == 17
.def __IQ17atan2
__IQ17atan2:
IQNatan2 GLOBAL_Q
.endif
.if GLOBAL_Q == 16
.def __IQ16atan2
__IQ16atan2:
IQNatan2 GLOBAL_Q
.endif
.if GLOBAL_Q == 15
.def __IQ15atan2
__IQ15atan2:
IQNatan2 GLOBAL_Q
.endif
.if GLOBAL_Q == 14
.def __IQ14atan2
__IQ14atan2:
IQNatan2 GLOBAL_Q
.endif
.if GLOBAL_Q == 13
.def __IQ13atan2
__IQ13atan2:
IQNatan2 GLOBAL_Q
.endif
.if GLOBAL_Q == 12
.def __IQ12atan2
__IQ12atan2:
IQNatan2 GLOBAL_Q
.endif
.if GLOBAL_Q == 11
.def __IQ11atan2
__IQ11atan2:
IQNatan2 GLOBAL_Q
.endif
.if GLOBAL_Q == 10
.def __IQ10atan2
__IQ10atan2:
IQNatan2 GLOBAL_Q
.endif
.if GLOBAL_Q == 9
.def __IQ9atan2
__IQ9atan2:
IQNatan2 GLOBAL_Q
.endif
.if GLOBAL_Q == 8
.def __IQ8atan2
__IQ8atan2:
IQNatan2 GLOBAL_Q
.endif
.if GLOBAL_Q == 7
.def __IQ7atan2
__IQ7atan2:
IQNatan2 GLOBAL_Q
.endif
.if GLOBAL_Q == 6
.def __IQ6atan2
__IQ6atan2:
IQNatan2 GLOBAL_Q
.endif
.if GLOBAL_Q == 5
.def __IQ5atan2
__IQ5atan2:
IQNatan2 GLOBAL_Q
.endif
.if GLOBAL_Q == 4
.def __IQ4atan2
__IQ4atan2:
IQNatan2 GLOBAL_Q
.endif
.if GLOBAL_Q == 3
.def __IQ3atan2
__IQ3atan2:
IQNatan2 GLOBAL_Q
.endif
.if GLOBAL_Q == 2
.def __IQ2atan2
__IQ2atan2:
IQNatan2 GLOBAL_Q
.endif
.if GLOBAL_Q == 1
.def __IQ1atan2
__IQ1atan2:
IQNatan2 GLOBAL_Q
.endif
;;###########################################################################
;; No More.
;;###########################################################################
@@ -0,0 +1,505 @@
;;###########################################################################
;;
;; FILE: IQNatan2PU.asm
;;
;; TITLE: C Callable IQ Math Function
;;
;;###########################################################################
;;
;; Ver | Date | Who | Description of changes
;; =====|=============|=======|==============================================
;; 1.4 | 17 May 2002 | A. T. | Original Release.
;; -----|-------------|-------|----------------------------------------------
;; | | |
;;
;;###########################################################################
;;#############################################################################
;;!
;;! Copyright: Copyright (C) 2023 Texas Instruments Incorporated -
;;! All rights reserved not granted herein.
;;! Limited License.
;;!
;;! Texas Instruments Incorporated grants a world-wide, royalty-free,
;;! non-exclusive license under copyrights and patents it now or hereafter
;;! owns or controls to make, have made, use, import, offer to sell and sell
;;! ("Utilize") this software subject to the terms herein. With respect to the
;;! foregoing patent license, such license is granted solely to the extent that
;;! any such patent is necessary to Utilize the software alone. The patent
;;! license shall not apply to any combinations which include this software,
;;! other than combinations with devices manufactured by or for TI
;;! ("TI Devices").
;;! No hardware patent is licensed hereunder.
;;!
;;! Redistributions must preserve existing copyright notices and reproduce this
;;! license (including the above copyright notice and the disclaimer and
;;! (if applicable) source code license limitations below) in the documentation
;;! and/or other materials provided with the distribution.
;;!
;;! Redistribution and use in binary form, without modification, are permitted
;;! provided that the following conditions are met:
;;!
;;! * No reverse engineering, decompilation, or disassembly of this software is
;;! permitted with respect to any software provided in binary form.
;;! * Any redistribution and use are licensed by TI for use only
;;! with TI Devices.
;;! * Nothing shall obligate TI to provide you with source code for the
;;! software licensed and provided to you in object code.
;;!
;;! If software source code is provided to you, modification and redistribution
;;! of the source code are permitted provided that the following conditions
;;! are met:
;;!
;;! * any redistribution and use of the source code, including any resulting
;;! derivative works, are licensed by TI for use only with TI Devices.
;;! * any redistribution and use of any object code compiled from the source
;;! code and any resulting derivative works, are licensed by TI for use
;;! only with TI Devices.
;;!
;;! Neither the name of Texas Instruments Incorporated nor the names of its
;;! suppliers may be used to endorse or promote products derived from this
;;! software without specific prior written permission.
;;#############################################################################
;;===========================================================================
;; Function: _IQNatan2PU
;;===========================================================================
;;
;; C Usage: extern long _IQNatan2PU(long Y, long X);
;;
;;---------------------------------------------------------------------------
;;
;; On Entry: ACC = Y in IQ format
;; SP-4 = X in IQ format
;;
;; Regs Used: XAR7, XAR6, XAR5, XAR4, XAR0, XT, P
;;
;; On Exit: ACC = Per Unit result in IQ format (0.0=0Deg, 1.0=360Deg)
;; if( X == 0.0 and Y == 0.0 ), ACC = 0.0
;; if( Y >= 0.0 ), 0.0 <= ACC <= 0.5
;; if( Y < 0.0 ), 0.5 < ACC < 1.0
;;
;; Q range: 29 to 1
;;
;; Note: A value of Q30 is not feasible since the result would
;; saturate for angles greater then +/- 2.0 radians.
;;
;;---------------------------------------------------------------------------
;; Algorithm: The algorithm steps to calculate the "atan2" of the given
;; input X and Y is as follows:
;;
;; Step(1): if( abs(X) >= abs(Y) )
;; Numerator = abs(Y)
;; Denominator = abs(X)
;; else
;; Numerator = abs(X)
;; Denominator = abs(Y)
;;
;; Step(2): Ratio = Numerator/Denominator
;;
;; Note: Ratio range = 0.0 to 1.0
;;
;; Step(3): Use the upper 6-bits of the "Ratio" value as an
;; index into the table to obtain the coefficients
;; for a second order equation:
;;
;; _IQNatan2Table:
;; CoeffA0[0]
;; CoeffA1[0]
;; CoeffA2[0]
;; .
;; .
;; CoeffA0[63]
;; CoeffA1[63]
;; CoeffA2[63]
;;
;; Step(4): Calculate the angle using the folowing equation:
;;
;; arctan(Ratio) = A0 + A1*Ratio + A2*Ratio*Ratio
;; arctan(Ratio) = A0 + Ratio(A1 + A2*Ratio)
;;
;;
;; Step(5): The final angle is determined as follows:
;;
;; if( X >= 0 and Y >= 0 and abs(X) >= abs(Y) )
;; Angle = arctan(abs(Y)/abs(X))
;; if( X >= 0 and Y >= 0 and abs(X) < abs(Y) )
;; Angle = PI/2 - arctan(abs(X)/abs(Y))
;; if( X < 0 and Y >= 0 and abs(X) < abs(Y) )
;; Angle = PI/2 + arctan(abs(X)/abs(Y))
;; if( X < 0 and Y >= 0 and abs(X) >= abs(Y) )
;; Angle = PI - arctan(abs(Y)/abs(X))
;;
;; Step(6): Convert to Per Unit (PU) value:
;;
;; PUangle(Ratio) = arctan(Ratio)/(2*pi)
;; PUangle(Ratio) = arctan(Ratio) * 0.159154943
;; PUangle(Ratio) = 0.0 to 0.5
;; if( Y < 0 )
;; PUAngle(Ratio) = 1.0 - PUAngle(Ratio);
;;
;; PUangle(Ratio) = 0.0 (0deg) to 1.0 (360deg)
;;
;;---------------------------------------------------------------------------
;; Benchmark:
;; * Code executes from 0-wait SARAM block.
;; * _IQatan2 & _IQdiv Tables located in seperate 0-wait SARAM or 1-wait ROM block.
;; * Stack located in seperate 0-wait SARAM block.
;; * Includes LCR/LRETR.
;;
;; Cycles = 121 (if 16 <= Q <= 29 and _IQatan2 & _IQdiv Tables in 0-wait SARAM)
;; = 124 (if 1 <= Q <= 15 and _IQatan2 & _IQdiv Tables in 0-wait SARAM)
;;
;; Cycles = 127 (if 16 <= Q <= 29 and _IQatan2Table & _IQdivTable in 1-wait ROM)
;; = 130 (if 1 <= Q <= 15 and _IQatan2Table & _IQdivTable in 1-wait ROM)
;;
;;===========================================================================
IQNatan2PU .macro q_value
;; Perform Step 1:
SETC OVM ; Make sure overflow mode is on
MOVL *SP++,ACC ; SP-6 = Y
ABS ACC ; ACC = abs(Y)
MOVL XT,@ACC ; XT = abs(Y)
MOVL ACC,*-SP[6] ; ACC = X
ABS ACC ; ACC = abs(X)
MOVL P,@ACC ; P = abs(X)
SUBL ACC,@XT ; ACC = abs(X) - abs(Y)
MOVL *SP++,ACC ; SP-4 = abs(X) - abs(Y)
MOVL ACC,@XT ; ACC = abs(Y)
MINL ACC,@P ; if( abs(Y) > abs(X) ), ACC = abs(X)
MOVL *SP++,ACC ; SP-2 = Numerator
MOVL ACC,@P ; ACC = abs(X)
MAXL ACC,@XT ; if( abs(X) < abs(Y) ), ACC = abs(Y)
; ACC = Denominator
; SP-10 = X
; SP-8 = Return Address
; SP-6 = Y
; SP-4 = abs(X) - abs(Y)
; SP-2 = Numerator
;; Perform Step 2:
CSB ACC ; Count sign bits, T = n
LSLL ACC,T ; ACC = Dnorm (Q31)
MOVL @XAR4,ACC ; Temp save "Dnorm"
LSR AH,#6
MOVB @AH,#0xFE,EQ ; If Den = 0, load zero value
MOVZ AR0,@AH ; AR0 = index into _IQdivTable
MOVL XAR7,#(_IQdivTable-254)
MOVB AH,#(62 - q_value) ; AH = 62 - q_value
SUBR @T,AH ; T = 62 - q_value - n
MOVL XAR5,@XT ; AR5H = 62 - q_value - n
MOVL XT,*+XAR7[AR0] ; (1) XT = initial Dm estimate (Q29)
MOV @AH,#0x4000
MOVB AL,#0
MOVL XAR6,@ACC ; XAR6 = 2.0 (Q29)
IMPYL P,XT,@XAR4 ; (2)
QMPYL ACC,XT,@XAR4 ; (2) ACC:P = Dm*Dnorm (Q60)
LSL64 ACC:P,#1 ; (3) ACC:P = Dm*Dnorm (Q61)
SUBL ACC,@XAR6 ; (4) ACC = Dm*Dnorm - 2.0 (Q29)
NEG ACC ; (5) ACC = 2.0 - Dm*Dnorm (Q29)
IMPYL P,XT,@ACC ; (6)
QMPYL ACC,XT,@ACC ; (6) ACC:P = Dm(2.0 - Dm*Dnorm) (Q58)
LSL64 ACC:P,#3 ; (7) ACC:P = Dm(2.0 - Dm*Dnorm) (Q61)
MOVL XT,@ACC ; (1) XT = Dm (Q29)
IMPYL P,XT,@XAR4 ; (2)
QMPYL ACC,XT,@XAR4 ; (2) ACC:P = Dm*Dnorm (Q60)
LSL64 ACC:P,#1 ; (3) ACC:P = Dm*Dnorm (Q61)
SUBL ACC,@XAR6 ; (4) ACC = Dm*Dnorm - 2.0 (Q29)
NEG ACC ; (5) ACC = 2.0 - Dm*Dnorm (Q29)
IMPYL P,XT,@ACC ; (6)
QMPYL ACC,XT,@ACC ; (6) ACC:P = Dm(2.0 - Dm*Dnorm) (Q58)
LSL64 ACC:P,#3 ; (7) ACC:P = Dm(2.0 - Dm*Dnorm) (Q61)
MOVL XT,*-SP[2] ; XT = Numerator
IMPYL P,XT,@ACC
QMPYL ACC,XT,@ACC ; ACC:P = N * Dm
LSL64 ACC:P,#2
MOVL XT,@XAR5 ; T = 62 - q_value - n
ASR64 ACC:P,T ; scale back result
; P = Ratio in IQ format
;; Perform Step 3:
MOVL XT,@P ; XT = Ratio
MOVB ACC,#64 ; ACC = 64 = Elements In Table
IMPYL P,XT,@ACC
QMPYL ACC,XT,@ACC ; ACC:P = 64*Ratio
.if q_value >= 16
LSL64 ACC:P,#(32 - q_value) ; ACC = int(64*Ratio)
.else
LSL64 ACC:P,#16
LSL64 ACC:P,#(16 - q_value) ; ACC = int(64*Ratio)
.endif
MPY P,@AL,#6 ; P = 6 * int(64*Ratio)
MOVZ AR0,@PL ; AR0 = _IQatan2Table index
MOVL XAR5,#_IQatan2Table ; Point to A0
MOVL XAR6,#_IQatan2Table+2 ; Point to A1
MOVL XAR7,#_IQatan2Table+4 ; Point to A2
;; Perform Step 4:
IMPYL P,XT,*+XAR7[AR0]
QMPYL ACC,XT,*+XAR7[AR0] ; ACC:P = A2*Ratio (q_value + Q30)
.if q_value >= 16
LSL64 ACC:P,#(32-q_value) ; ACC = A2*Ratio (Q30)
.else
LSL64 ACC:P,#16
LSL64 ACC:P,#(16-q_value) ; ACC = A2*Ratio (Q30)
.endif
ADDL ACC,*+XAR6[AR0] ; ACC = A1 + A2*Ratio (Q30)
IMPYL P,XT,@ACC
QMPYL ACC,XT,@ACC ; ACC:P = Ratio(A1 + A2*Ratio) (q_value + Q30)
.if q_value >= 16
LSL64 ACC:P,#(32-q_value) ; ACC:P = Ratio(A1 + A2*Ratio) (Q30)
.else
LSL64 ACC:P,#16
LSL64 ACC:P,#(16-q_value) ; ACC:P = Ratio(A1 + A2*Ratio) (Q30)
.endif
ADDL ACC,*+XAR5[AR0] ; ACC = A0 + Ratio(A1 + A2*Ratio) (Q30)
.if (q_value >= 14) & (q_value <= 29)
LSR64 ACC:P,#(30-q_value) ; ACC = A0 + Ratio(A1 + A2*Ratio) (q_value)
.endif
.if q_value <= 13
LSR64 ACC:P,#16
LSR64 ACC:P,#(14-q_value) ; ACC = A0 + Ratio(A1 + A2*Ratio) (q_value)
.endif
;; Perform Step 5:
MOVL XAR7,#(_IQatan2HalfPITable + q_value*2)
MOVL XAR6,*XAR7 ; XAR6 = PI/2
MOVL P,@ACC ; P = arctan(Ratio)
NEG ACC ; ACC = -arctan(Ratio)
TBIT *-SP[9],#15 ; Test if(X >= 0)
MOVL @P,ACC,NTC ; if(X >= 0), P = -arctan(Ratio)
MOVL ACC,@P
NEG ACC ; ACC = -P
TBIT *-SP[3],#15 ; Test if(abs(X) >= abs(Y))
MOVL @P,ACC,NTC ; if(abs(X) >= abs(Y)), P = -P
MOVL ACC,@XAR6 ; ACC = PI/2
NEG ACC ; ACC = -PI/2
TBIT *-SP[9],#15 ; Test if(X < 0)
NEGTC ACC ; if(X < 0), ACC = PI/2
ADDL ACC,@XAR6 ; ACC = ACC + PI/2
TBIT *-SP[3],#15 ; Test if(abs(X) >= abs(Y))
MOVL @XAR6,ACC,NTC ; if(abs(X) >= abs(Y)), XAR6 = ACC
MOVL ACC,@XAR6
ADDL ACC,@P ; ACC = arctan(abs(Y),X)
;; Perform Step 6:
MOVL XT,@ACC
MOV @PL,#0x60DC ; P = 1/(2*pi) (Q32)
MOV @PH,#0x28BE
QMPYL P,XT,@P ; P = arctan(Ratio) * 1/(2*pi) = PUangle (q_value)
.if q_value <= 15 ; ACC = 1.0 (q_value)
MOV @AL,#(1 << q_value)
MOV @AH,#0x0000
.endif
.if q_value == 16
MOV @AL,#0x0000
MOV @AH,#0x0001
.endif
.if q_value >= 17
MOV @AL,#0x0000
MOV @AH,#(1 << (q_value - 16))
.endif
SUBL ACC,@P ; ACC = 1.0 - PUangle
TBIT *-SP[5],#15 ; Test if(Y < 0)
MOVL @P,ACC,TC ; if(Y < 0), P = 1.0 - PUangle
MOVL ACC,@P ; ACC = PUangle
SUBB SP,#6 ; Deallocate stack
CLRC OVM ; Turn off overflow mode
LRETR
.endm
;; The value "GLOBAL_Q" needs to be supplied by the assembler
;; using the "-dGLOBAL_Q=q_value" directive:
.sect "IQmath"
.ref _IQatan2HalfPITable
.ref _IQatan2Table
.ref _IQdivTable
.if GLOBAL_Q == 30
.def __IQ30atan2PU
__IQ30atan2PU:
IQNatan2PU GLOBAL_Q
.endif
.if GLOBAL_Q == 29
.def __IQ29atan2PU
__IQ29atan2PU:
IQNatan2PU GLOBAL_Q
.endif
.if GLOBAL_Q == 28
.def __IQ28atan2PU
__IQ28atan2PU:
IQNatan2PU GLOBAL_Q
.endif
.if GLOBAL_Q == 27
.def __IQ27atan2PU
__IQ27atan2PU:
IQNatan2PU GLOBAL_Q
.endif
.if GLOBAL_Q == 26
.def __IQ26atan2PU
__IQ26atan2PU:
IQNatan2PU GLOBAL_Q
.endif
.if GLOBAL_Q == 25
.def __IQ25atan2PU
__IQ25atan2PU:
IQNatan2PU GLOBAL_Q
.endif
.if GLOBAL_Q == 24
.def __IQ24atan2PU
__IQ24atan2PU:
IQNatan2PU GLOBAL_Q
.endif
.if GLOBAL_Q == 23
.def __IQ23atan2PU
__IQ23atan2PU:
IQNatan2PU GLOBAL_Q
.endif
.if GLOBAL_Q == 22
.def __IQ22atan2PU
__IQ22atan2PU:
IQNatan2PU GLOBAL_Q
.endif
.if GLOBAL_Q == 21
.def __IQ21atan2PU
__IQ21atan2PU:
IQNatan2PU GLOBAL_Q
.endif
.if GLOBAL_Q == 20
.def __IQ20atan2PU
__IQ20atan2PU:
IQNatan2PU GLOBAL_Q
.endif
.if GLOBAL_Q == 19
.def __IQ19atan2PU
__IQ19atan2PU:
IQNatan2PU GLOBAL_Q
.endif
.if GLOBAL_Q == 18
.def __IQ18atan2PU
__IQ18atan2PU:
IQNatan2PU GLOBAL_Q
.endif
.if GLOBAL_Q == 17
.def __IQ17atan2PU
__IQ17atan2PU:
IQNatan2PU GLOBAL_Q
.endif
.if GLOBAL_Q == 16
.def __IQ16atan2PU
__IQ16atan2PU:
IQNatan2PU GLOBAL_Q
.endif
.if GLOBAL_Q == 15
.def __IQ15atan2PU
__IQ15atan2PU:
IQNatan2PU GLOBAL_Q
.endif
.if GLOBAL_Q == 14
.def __IQ14atan2PU
__IQ14atan2PU:
IQNatan2PU GLOBAL_Q
.endif
.if GLOBAL_Q == 13
.def __IQ13atan2PU
__IQ13atan2PU:
IQNatan2PU GLOBAL_Q
.endif
.if GLOBAL_Q == 12
.def __IQ12atan2PU
__IQ12atan2PU:
IQNatan2PU GLOBAL_Q
.endif
.if GLOBAL_Q == 11
.def __IQ11atan2PU
__IQ11atan2PU:
IQNatan2PU GLOBAL_Q
.endif
.if GLOBAL_Q == 10
.def __IQ10atan2PU
__IQ10atan2PU:
IQNatan2PU GLOBAL_Q
.endif
.if GLOBAL_Q == 9
.def __IQ9atan2PU
__IQ9atan2PU:
IQNatan2PU GLOBAL_Q
.endif
.if GLOBAL_Q == 8
.def __IQ8atan2PU
__IQ8atan2PU:
IQNatan2PU GLOBAL_Q
.endif
.if GLOBAL_Q == 7
.def __IQ7atan2PU
__IQ7atan2PU:
IQNatan2PU GLOBAL_Q
.endif
.if GLOBAL_Q == 6
.def __IQ6atan2PU
__IQ6atan2PU:
IQNatan2PU GLOBAL_Q
.endif
.if GLOBAL_Q == 5
.def __IQ5atan2PU
__IQ5atan2PU:
IQNatan2PU GLOBAL_Q
.endif
.if GLOBAL_Q == 4
.def __IQ4atan2PU
__IQ4atan2PU:
IQNatan2PU GLOBAL_Q
.endif
.if GLOBAL_Q == 3
.def __IQ3atan2PU
__IQ3atan2PU:
IQNatan2PU GLOBAL_Q
.endif
.if GLOBAL_Q == 2
.def __IQ2atan2PU
__IQ2atan2PU:
IQNatan2PU GLOBAL_Q
.endif
.if GLOBAL_Q == 1
.def __IQ1atan2PU
__IQ1atan2PU:
IQNatan2PU GLOBAL_Q
.endif
;;###########################################################################
;; No More.
;;###########################################################################
@@ -0,0 +1,378 @@
;;###########################################################################
;;
;; FILE: IQNcos.asm
;;
;; TITLE: C Callable IQ Math Function
;;
;;###########################################################################
;;
;; Ver | Date | Who | Description of changes
;; =====|=============|=======|==============================================
;; 1.2 | 11 Oct 2001 | A. T. | Original Release.
;; -----|-------------|-------|----------------------------------------------
;; 1.3 | 19 Nov 2001 | A. T. | No changes.
;; -----|-------------|-------|----------------------------------------------
;; 1.4 | 17 May 2002 | A. T. | Improved accuracy from 16 to about 29 bits
;; | | | (thanks to Settu for writing original code).
;; | | | Modified code so that calculation constants
;; | | | are embedded in code and not in table.
;; -----|-------------|-------|----------------------------------------------
;; | | |
;;
;;###########################################################################
;;#############################################################################
;;!
;;! Copyright: Copyright (C) 2023 Texas Instruments Incorporated -
;;! All rights reserved not granted herein.
;;! Limited License.
;;!
;;! Texas Instruments Incorporated grants a world-wide, royalty-free,
;;! non-exclusive license under copyrights and patents it now or hereafter
;;! owns or controls to make, have made, use, import, offer to sell and sell
;;! ("Utilize") this software subject to the terms herein. With respect to the
;;! foregoing patent license, such license is granted solely to the extent that
;;! any such patent is necessary to Utilize the software alone. The patent
;;! license shall not apply to any combinations which include this software,
;;! other than combinations with devices manufactured by or for TI
;;! ("TI Devices").
;;! No hardware patent is licensed hereunder.
;;!
;;! Redistributions must preserve existing copyright notices and reproduce this
;;! license (including the above copyright notice and the disclaimer and
;;! (if applicable) source code license limitations below) in the documentation
;;! and/or other materials provided with the distribution.
;;!
;;! Redistribution and use in binary form, without modification, are permitted
;;! provided that the following conditions are met:
;;!
;;! * No reverse engineering, decompilation, or disassembly of this software is
;;! permitted with respect to any software provided in binary form.
;;! * Any redistribution and use are licensed by TI for use only
;;! with TI Devices.
;;! * Nothing shall obligate TI to provide you with source code for the
;;! software licensed and provided to you in object code.
;;!
;;! If software source code is provided to you, modification and redistribution
;;! of the source code are permitted provided that the following conditions
;;! are met:
;;!
;;! * any redistribution and use of the source code, including any resulting
;;! derivative works, are licensed by TI for use only with TI Devices.
;;! * any redistribution and use of any object code compiled from the source
;;! code and any resulting derivative works, are licensed by TI for use
;;! only with TI Devices.
;;!
;;! Neither the name of Texas Instruments Incorporated nor the names of its
;;! suppliers may be used to endorse or promote products derived from this
;;! software without specific prior written permission.
;;#############################################################################
;;===========================================================================
;; Function: _IQNcos
;;===========================================================================
;;
;; C Usage: extern long _IQNcos(long Rad);
;;
;;---------------------------------------------------------------------------
;;
;; On Entry: ACC = Radian in IQ format
;;
;; Regs Used: XAR7, XAR6, XAR5, XAR4, XAR0
;; XT, P
;;
;; On Exit: ACC = cos(Radian) result in IQ format
;;
;; Q range: 30 to 1
;;
;;---------------------------------------------------------------------------
;; Algorithm: The "cos" value is calculated as follows:
;;
;; 1) The offset into a 512 word sin/cos table is calculated:
;;
;; k = 0x1FF & (int(Radian*512/(2*pi)))
;;
;;
;; 2) The fractional component between table samples is
;; calculated:
;;
;; x = fract(Radian*512/2*pi) * (2*pi)/512
;;
;; 3) The output sine value is calculated as follows:
;;
;; cos(Radian) = C(k) + x*(-S(k) + x*(-0.5*C(k) + 0.166*x*S(k)))
;;
;; where S(k) = Sin table value at offset "k"
;; C(k) = Cos table value at offset "k"
;;
;; Using the above method, with a 512x32 full wave sin/cos table,
;; will give an accuracy of approximately 29 bits.
;;
;;---------------------------------------------------------------------------
;; Benchmark:
;;
;; Assumptions: * Code executes from 0-wait SARAM block.
;; * _IQsin/cosTable located in seperate SARAM or ROM block.
;;
;; Cycles = 43 ( Q == 30, tables in 0-wait SARAM, includes LCR/LRETR)
;; = 44 (29 >= Q >= 25, tables in 0-wait SARAM, includes LCR/LRETR)
;; = 43 (24 >= Q >= 14, tables in 0-wait SARAM, includes LCR/LRETR)
;; = 44 (13 >= Q >= 0, tables in 0-wait SARAM, includes LCR/LRETR)
;;
;; Cycles = 45 ( Q == 30, tables in 1-wait ROM, includes LCR/LRETR)
;; = 46 (29 >= Q >= 25, tables in 1-wait ROM, includes LCR/LRETR)
;; = 45 (24 >= Q >= 14, tables in 1-wait ROM, includes LCR/LRETR)
;; = 46 (13 >= Q >= 0, tables in 1-wait ROM, includes LCR/LRETR)
;;
;;===========================================================================
K1 .set 02AAAh ; 1/6 in Q16
IQNcos .macro q_value
MOV *SP++,#0x3F6B ; (2*pi)/512 = 0x03243F6B (Q32)
MOV *SP++,#0x0324
MOV *SP++,#0xC1B7 ; 512/(2*pi) = 0x517CC1B7 (I8Q24)
MOV *SP++,#0x517C
MOVL XAR6,#_IQcosTable
MOVL XAR7,#_IQsinTable
SETC OVM ; turn overflow mode on
ABS ACC ; abs(Rad), TC = sign
MOVL XT,*--SP ; XT = 512/(2*pi) (I8Q24)
IMPYL P,XT,@ACC
QMPYL ACC,XT,@ACC ; ACC:P = Rad * 512 / (2pi)
.if q_value >= 25
ASR64 ACC:P,#16
ASR64 ACC:P,#(q_value - 24) ; ACC = Integer, P = fract
.endif
.if (q_value <= 24) & (q_value >= 9)
ASR64 ACC:P,#(q_value - 8) ; ACC = Integer, P = fract
.endif
.if q_value <= 7
LSL64 ACC:P,#(8 - q_value) ; ACC = Integer, P = fract
.endif
AND @AL,#0x1FF
LSL AL,#1
MOVZ AR0,@AL ; AR0 = Index into "sin/cos" table = k
MOVL XT,*--SP ; XT = (2*pi)/512 (Q32)
QMPYUL P,XT,@P ; P = x (Q32)
MOVB ACC,#0
MOVL XT,@P ; XT = x (Q32)
MOVL XAR4,*+XAR6[AR0] ; XAR4 = C(k) (Q30)
MOVL XAR5,*+XAR7[AR0] ; XAR5 = S(k) (Q30)
SUBL ACC,@XAR4 ; ACC = -C(k) (Q30)
ASR64 ACC:P,#1 ; ACC = -0.5*C(k) (Q30)
QMPYL P,XT,@XAR5 ; P = x*S(k) (Q30)
CLRC OVM ; Turn overflow mode off
MPY P,@PH,#K1 ; P = 0.166*x*S(k) (Q30)
ADDL ACC,P ; ACC = -0.5*C(k) + 0.166*x*S(k) (Q30)
QMPYL ACC,XT,@ACC ; ACC = x*(-0.5*C(k) + 0.166*x*S(k)) (Q30)
SUBL ACC,@XAR5 ; ACC = -S(k) + x*(-0.5*C(k) + 0.166*x*S(k)) (Q30)
QMPYL ACC,XT,@ACC ; ACC = x*(-S(k) + x*(-0.5*C(k) + 0.166*x*S(k))) (Q30)
ADDL ACC,@XAR4 ; ACC = C(k) + x*(-S(k) + x*(-0.5*C(k) + 0.166*x*S(k))) (Q30)
.if (q_value <= 29) & (q_value >= 14)
ASR64 ACC:P,#(30 - q_value) ; Scale back to required IQ value
.endif
.if q_value <= 13
ASR64 ACC:P,#16
ASR64 ACC:P,#(14 - q_value) ; Scale back to required IQ value
.endif
LRETR
.endm
;; The value "GLOBAL_Q" needs to be supplied by the assembler
;; using the "-dGLOBAL_Q=q_value" directive:
.sect "IQmath"
.ref _IQsinTable
.ref _IQcosTable
.if GLOBAL_Q == 30
.def __IQ30cos
__IQ30cos:
IQNcos GLOBAL_Q
.endif
.if GLOBAL_Q == 29
.def __IQ29cos
__IQ29cos:
IQNcos GLOBAL_Q
.endif
.if GLOBAL_Q == 28
.def __IQ28cos
__IQ28cos:
IQNcos GLOBAL_Q
.endif
.if GLOBAL_Q == 27
.def __IQ27cos
__IQ27cos:
IQNcos GLOBAL_Q
.endif
.if GLOBAL_Q == 26
.def __IQ26cos
__IQ26cos:
IQNcos GLOBAL_Q
.endif
.if GLOBAL_Q == 25
.def __IQ25cos
__IQ25cos:
IQNcos GLOBAL_Q
.endif
.if GLOBAL_Q == 24
.def __IQ24cos
__IQ24cos:
IQNcos GLOBAL_Q
.endif
.if GLOBAL_Q == 23
.def __IQ23cos
__IQ23cos:
IQNcos GLOBAL_Q
.endif
.if GLOBAL_Q == 22
.def __IQ22cos
__IQ22cos:
IQNcos GLOBAL_Q
.endif
.if GLOBAL_Q == 21
.def __IQ21cos
__IQ21cos:
IQNcos GLOBAL_Q
.endif
.if GLOBAL_Q == 20
.def __IQ20cos
__IQ20cos:
IQNcos GLOBAL_Q
.endif
.if GLOBAL_Q == 19
.def __IQ19cos
__IQ19cos:
IQNcos GLOBAL_Q
.endif
.if GLOBAL_Q == 18
.def __IQ18cos
__IQ18cos:
IQNcos GLOBAL_Q
.endif
.if GLOBAL_Q == 17
.def __IQ17cos
__IQ17cos:
IQNcos GLOBAL_Q
.endif
.if GLOBAL_Q == 16
.def __IQ16cos
__IQ16cos:
IQNcos GLOBAL_Q
.endif
.if GLOBAL_Q == 15
.def __IQ15cos
__IQ15cos:
IQNcos GLOBAL_Q
.endif
.if GLOBAL_Q == 14
.def __IQ14cos
__IQ14cos:
IQNcos GLOBAL_Q
.endif
.if GLOBAL_Q == 13
.def __IQ13cos
__IQ13cos:
IQNcos GLOBAL_Q
.endif
.if GLOBAL_Q == 12
.def __IQ12cos
__IQ12cos:
IQNcos GLOBAL_Q
.endif
.if GLOBAL_Q == 11
.def __IQ11cos
__IQ11cos:
IQNcos GLOBAL_Q
.endif
.if GLOBAL_Q == 10
.def __IQ10cos
__IQ10cos:
IQNcos GLOBAL_Q
.endif
.if GLOBAL_Q == 9
.def __IQ9cos
__IQ9cos:
IQNcos GLOBAL_Q
.endif
.if GLOBAL_Q == 8
.def __IQ8cos
__IQ8cos:
IQNcos GLOBAL_Q
.endif
.if GLOBAL_Q == 7
.def __IQ7cos
__IQ7cos:
IQNcos GLOBAL_Q
.endif
.if GLOBAL_Q == 6
.def __IQ6cos
__IQ6cos:
IQNcos GLOBAL_Q
.endif
.if GLOBAL_Q == 5
.def __IQ5cos
__IQ5cos:
IQNcos GLOBAL_Q
.endif
.if GLOBAL_Q == 4
.def __IQ4cos
__IQ4cos:
IQNcos GLOBAL_Q
.endif
.if GLOBAL_Q == 3
.def __IQ3cos
__IQ3cos:
IQNcos GLOBAL_Q
.endif
.if GLOBAL_Q == 2
.def __IQ2cos
__IQ2cos:
IQNcos GLOBAL_Q
.endif
.if GLOBAL_Q == 1
.def __IQ1cos
__IQ1cos:
IQNcos GLOBAL_Q
.endif
;;###########################################################################
;; No More.
;;###########################################################################
@@ -0,0 +1,366 @@
;;###########################################################################
;;
;; FILE: IQNcosPU.asm
;;
;; TITLE: C Callable IQ Math Function
;;
;;###########################################################################
;;
;; Ver | Date | Who | Description of changes
;; =====|=============|=======|==============================================
;; 1.4 | 17 May 2002 | A. T. | Original Release.
;; -----|-------------|-------|----------------------------------------------
;; | | |
;;
;;###########################################################################
;;#############################################################################
;;!
;;! Copyright: Copyright (C) 2023 Texas Instruments Incorporated -
;;! All rights reserved not granted herein.
;;! Limited License.
;;!
;;! Texas Instruments Incorporated grants a world-wide, royalty-free,
;;! non-exclusive license under copyrights and patents it now or hereafter
;;! owns or controls to make, have made, use, import, offer to sell and sell
;;! ("Utilize") this software subject to the terms herein. With respect to the
;;! foregoing patent license, such license is granted solely to the extent that
;;! any such patent is necessary to Utilize the software alone. The patent
;;! license shall not apply to any combinations which include this software,
;;! other than combinations with devices manufactured by or for TI
;;! ("TI Devices").
;;! No hardware patent is licensed hereunder.
;;!
;;! Redistributions must preserve existing copyright notices and reproduce this
;;! license (including the above copyright notice and the disclaimer and
;;! (if applicable) source code license limitations below) in the documentation
;;! and/or other materials provided with the distribution.
;;!
;;! Redistribution and use in binary form, without modification, are permitted
;;! provided that the following conditions are met:
;;!
;;! * No reverse engineering, decompilation, or disassembly of this software is
;;! permitted with respect to any software provided in binary form.
;;! * Any redistribution and use are licensed by TI for use only
;;! with TI Devices.
;;! * Nothing shall obligate TI to provide you with source code for the
;;! software licensed and provided to you in object code.
;;!
;;! If software source code is provided to you, modification and redistribution
;;! of the source code are permitted provided that the following conditions
;;! are met:
;;!
;;! * any redistribution and use of the source code, including any resulting
;;! derivative works, are licensed by TI for use only with TI Devices.
;;! * any redistribution and use of any object code compiled from the source
;;! code and any resulting derivative works, are licensed by TI for use
;;! only with TI Devices.
;;!
;;! Neither the name of Texas Instruments Incorporated nor the names of its
;;! suppliers may be used to endorse or promote products derived from this
;;! software without specific prior written permission.
;;#############################################################################
;;===========================================================================
;; Function: _IQNcosPU
;;===========================================================================
;;
;; C Usage: extern long _IQNcosPU(long PU);
;;
;;---------------------------------------------------------------------------
;;
;; On Entry: ACC = Per Unit in IQ format
;;
;; Regs Used: XAR7, XAR6, XAR5, XAR4, XAR0
;; XT, P
;;
;; On Exit: ACC = cos(PU) result in IQ format
;;
;; Q range: 30 to 1
;;
;;---------------------------------------------------------------------------
;; Algorithm: The "cos" value is calculated as follows:
;;
;; 1) The offset into a 512 word sin/cos table is calculated:
;;
;; k = 0x1FF & (int(PU*512))
;;
;;
;; 2) The fractional component between table samples is
;; calculated:
;;
;; x = fract(PU*512) * (2*pi)/512
;;
;; 3) The output sine value is calculated as follows:
;;
;; cos(Radian) = C(k) + x*(-S(k) + x*(-0.5*C(k) + 0.166*x*S(k)))
;;
;; where S(k) = Sin table value at offset "k"
;; C(k) = Cos table value at offset "k"
;;
;; Using the above method, with a 512x32 full wave sin/cos table,
;; will give an accuracy of approximately 29 bits.
;;
;;---------------------------------------------------------------------------
;; Benchmark:
;;
;; Assumptions: * Code executes from 0-wait SARAM block.
;; * _IQsin/cosTable located in seperate SARAM or ROM block.
;;
;; Cycles = 37 ( Q == 30, tables in 0-wait SARAM, includes LCR/LRETR)
;; = 38 (29 >= Q >= 26, tables in 0-wait SARAM, includes LCR/LRETR)
;; = 37 (25 >= Q >= 14, tables in 0-wait SARAM, includes LCR/LRETR)
;; = 38 (13 >= Q >= 0, tables in 0-wait SARAM, includes LCR/LRETR)
;;
;; Cycles = 39 ( Q == 30, tables in 1-wait ROM, includes LCR/LRETR)
;; = 40 (29 >= Q >= 26, tables in 1-wait ROM, includes LCR/LRETR)
;; = 39 (25 >= Q >= 14, tables in 1-wait ROM, includes LCR/LRETR)
;; = 40 (13 >= Q >= 0, tables in 1-wait ROM, includes LCR/LRETR)
;;
;;===========================================================================
K1 .set 02AAAh ; 1/6 in Q16
IQNcosPU .macro q_value
MOV *SP++,#0x3F6B ; (2*pi)/512 = 0x03243F6B (Q32)
MOV *SP++,#0x0324
MOVL XAR6,#_IQcosTable
MOVL XAR7,#_IQsinTable
SETC OVM ; turn overflow mode on
ABS ACC ; abs(Rad), TC = sign
MPYB P,T,#0
.if q_value >= 26
ASR64 ACC:P,#16
ASR64 ACC:P,#(q_value - 25) ; ACC = Integer, P = fract
.endif
.if (q_value <= 25) & (q_value >= 10)
ASR64 ACC:P,#(q_value - 9) ; ACC = Integer, P = fract
.endif
.if q_value <= 8
LSL64 ACC:P,#(9 - q_value) ; ACC = Integer, P = fract
.endif
AND @AL,#0x1FF
LSL AL,#1
MOVZ AR0,@AL ; AR0 = Index into "sin/cos" table = k
MOVL XT,*--SP ; XT = (2*pi)/512 (Q32)
QMPYUL P,XT,@P ; P = x (Q32)
MOVB ACC,#0
MOVL XT,@P ; XT = x (Q32)
MOVL XAR4,*+XAR6[AR0] ; XAR4 = C(k) (Q30)
MOVL XAR5,*+XAR7[AR0] ; XAR5 = S(k) (Q30)
SUBL ACC,@XAR4 ; ACC = -C(k) (Q30)
ASR64 ACC:P,#1 ; ACC = -0.5*C(k) (Q30)
QMPYL P,XT,@XAR5 ; P = x*S(k) (Q30)
CLRC OVM ; Turn overflow mode off
MPY P,@PH,#K1 ; P = 0.166*x*S(k) (Q30)
ADDL ACC,P ; ACC = -0.5*C(k) + 0.166*x*S(k) (Q30)
QMPYL ACC,XT,@ACC ; ACC = x*(-0.5*C(k) + 0.166*x*S(k)) (Q30)
SUBL ACC,@XAR5 ; ACC = -S(k) + x*(-0.5*C(k) + 0.166*x*S(k)) (Q30)
QMPYL ACC,XT,@ACC ; ACC = x*(-S(k) + x*(-0.5*C(k) + 0.166*x*S(k))) (Q30)
ADDL ACC,@XAR4 ; ACC = C(k) + x*(-S(k) + x*(-0.5*C(k) + 0.166*x*S(k))) (Q30)
.if (q_value <= 29) & (q_value >= 14)
ASR64 ACC:P,#(30 - q_value) ; Scale back to required IQ value
.endif
.if q_value <= 13
ASR64 ACC:P,#16
ASR64 ACC:P,#(14 - q_value) ; Scale back to required IQ value
.endif
LRETR
.endm
;; The value "GLOBAL_Q" needs to be supplied by the assembler
;; using the "-dGLOBAL_Q=q_value" directive:
.sect "IQmath"
.ref _IQsinTable
.ref _IQcosTable
.if GLOBAL_Q == 30
.def __IQ30cosPU
__IQ30cosPU:
IQNcosPU GLOBAL_Q
.endif
.if GLOBAL_Q == 29
.def __IQ29cosPU
__IQ29cosPU:
IQNcosPU GLOBAL_Q
.endif
.if GLOBAL_Q == 28
.def __IQ28cosPU
__IQ28cosPU:
IQNcosPU GLOBAL_Q
.endif
.if GLOBAL_Q == 27
.def __IQ27cosPU
__IQ27cosPU:
IQNcosPU GLOBAL_Q
.endif
.if GLOBAL_Q == 26
.def __IQ26cosPU
__IQ26cosPU:
IQNcosPU GLOBAL_Q
.endif
.if GLOBAL_Q == 25
.def __IQ25cosPU
__IQ25cosPU:
IQNcosPU GLOBAL_Q
.endif
.if GLOBAL_Q == 24
.def __IQ24cosPU
__IQ24cosPU:
IQNcosPU GLOBAL_Q
.endif
.if GLOBAL_Q == 23
.def __IQ23cosPU
__IQ23cosPU:
IQNcosPU GLOBAL_Q
.endif
.if GLOBAL_Q == 22
.def __IQ22cosPU
__IQ22cosPU:
IQNcosPU GLOBAL_Q
.endif
.if GLOBAL_Q == 21
.def __IQ21cosPU
__IQ21cosPU:
IQNcosPU GLOBAL_Q
.endif
.if GLOBAL_Q == 20
.def __IQ20cosPU
__IQ20cosPU:
IQNcosPU GLOBAL_Q
.endif
.if GLOBAL_Q == 19
.def __IQ19cosPU
__IQ19cosPU:
IQNcosPU GLOBAL_Q
.endif
.if GLOBAL_Q == 18
.def __IQ18cosPU
__IQ18cosPU:
IQNcosPU GLOBAL_Q
.endif
.if GLOBAL_Q == 17
.def __IQ17cosPU
__IQ17cosPU:
IQNcosPU GLOBAL_Q
.endif
.if GLOBAL_Q == 16
.def __IQ16cosPU
__IQ16cosPU:
IQNcosPU GLOBAL_Q
.endif
.if GLOBAL_Q == 15
.def __IQ15cosPU
__IQ15cosPU:
IQNcosPU GLOBAL_Q
.endif
.if GLOBAL_Q == 14
.def __IQ14cosPU
__IQ14cosPU:
IQNcosPU GLOBAL_Q
.endif
.if GLOBAL_Q == 13
.def __IQ13cosPU
__IQ13cosPU:
IQNcosPU GLOBAL_Q
.endif
.if GLOBAL_Q == 12
.def __IQ12cosPU
__IQ12cosPU:
IQNcosPU GLOBAL_Q
.endif
.if GLOBAL_Q == 11
.def __IQ11cosPU
__IQ11cosPU:
IQNcosPU GLOBAL_Q
.endif
.if GLOBAL_Q == 10
.def __IQ10cosPU
__IQ10cosPU:
IQNcosPU GLOBAL_Q
.endif
.if GLOBAL_Q == 9
.def __IQ9cosPU
__IQ9cosPU:
IQNcosPU GLOBAL_Q
.endif
.if GLOBAL_Q == 8
.def __IQ8cosPU
__IQ8cosPU:
IQNcosPU GLOBAL_Q
.endif
.if GLOBAL_Q == 7
.def __IQ7cosPU
__IQ7cosPU:
IQNcosPU GLOBAL_Q
.endif
.if GLOBAL_Q == 6
.def __IQ6cosPU
__IQ6cosPU:
IQNcosPU GLOBAL_Q
.endif
.if GLOBAL_Q == 5
.def __IQ5cosPU
__IQ5cosPU:
IQNcosPU GLOBAL_Q
.endif
.if GLOBAL_Q == 4
.def __IQ4cosPU
__IQ4cosPU:
IQNcosPU GLOBAL_Q
.endif
.if GLOBAL_Q == 3
.def __IQ3cosPU
__IQ3cosPU:
IQNcosPU GLOBAL_Q
.endif
.if GLOBAL_Q == 2
.def __IQ2cosPU
__IQ2cosPU:
IQNcosPU GLOBAL_Q
.endif
.if GLOBAL_Q == 1
.def __IQ1cosPU
__IQ1cosPU:
IQNcosPU GLOBAL_Q
.endif
;;###########################################################################
;; No More.
;;###########################################################################
@@ -0,0 +1,380 @@
;;###########################################################################
;;
;; FILE: IQNdiv.asm
;;
;; TITLE: C Callable IQ Divide Math Function
;;
;;###########################################################################
;;
;; Ver | Date | Who | Description of changes
;; =====|==============|=======|=============================================
;; 1.2 | 11 Oct 2001 | A. T. | Original Release.
;; -----|--------------|-------|---------------------------------------------
;; 1.3 | 19 Nov 2001 | A. T. | Improved accuracy of function.
;; -----|--------------|-------|---------------------------------------------
;; 1.4 | 17 May 2002 | A. T. | Modified result when Den==0. Gives most
;; | | | positive result rather then zero.
;; -----|--------------|-------|---------------------------------------------
;; | | |
;;
;;###########################################################################
;;#############################################################################
;;!
;;! Copyright: Copyright (C) 2023 Texas Instruments Incorporated -
;;! All rights reserved not granted herein.
;;! Limited License.
;;!
;;! Texas Instruments Incorporated grants a world-wide, royalty-free,
;;! non-exclusive license under copyrights and patents it now or hereafter
;;! owns or controls to make, have made, use, import, offer to sell and sell
;;! ("Utilize") this software subject to the terms herein. With respect to the
;;! foregoing patent license, such license is granted solely to the extent that
;;! any such patent is necessary to Utilize the software alone. The patent
;;! license shall not apply to any combinations which include this software,
;;! other than combinations with devices manufactured by or for TI
;;! ("TI Devices").
;;! No hardware patent is licensed hereunder.
;;!
;;! Redistributions must preserve existing copyright notices and reproduce this
;;! license (including the above copyright notice and the disclaimer and
;;! (if applicable) source code license limitations below) in the documentation
;;! and/or other materials provided with the distribution.
;;!
;;! Redistribution and use in binary form, without modification, are permitted
;;! provided that the following conditions are met:
;;!
;;! * No reverse engineering, decompilation, or disassembly of this software is
;;! permitted with respect to any software provided in binary form.
;;! * Any redistribution and use are licensed by TI for use only
;;! with TI Devices.
;;! * Nothing shall obligate TI to provide you with source code for the
;;! software licensed and provided to you in object code.
;;!
;;! If software source code is provided to you, modification and redistribution
;;! of the source code are permitted provided that the following conditions
;;! are met:
;;!
;;! * any redistribution and use of the source code, including any resulting
;;! derivative works, are licensed by TI for use only with TI Devices.
;;! * any redistribution and use of any object code compiled from the source
;;! code and any resulting derivative works, are licensed by TI for use
;;! only with TI Devices.
;;!
;;! Neither the name of Texas Instruments Incorporated nor the names of its
;;! suppliers may be used to endorse or promote products derived from this
;;! software without specific prior written permission.
;;#############################################################################
;;===========================================================================
;; Function: _IQNdiv
;;===========================================================================
;;
;; C Usage: extern long _IQNdiv(long Num, long Den);
;;
;;---------------------------------------------------------------------------
;;
;; On Entry: ACC = Numerator in IQ format
;; [SP-4] = Denominator in IQ format
;;
;; Regs Used: XAR7, XAR6, XAR5, XAR4, XAR0
;; XT, P
;;
;; On Exit: ACC = Num/Den result in IQ format
;; = 0x7FFFFFFF if +ve overflow
;; = 0x80000000 if -ve overflow
;; = 0x7FFFFFFF if Den == 0
;;
;; Q range: 30 to 1
;;
;;---------------------------------------------------------------------------
;; Algorithm: The procedure for calculating Y = N/D is as follows:
;;
;; Step 1) Normalize Input:
;;
;; Dnorm = D * 2^n
;;
;; Step 2) Obtain initial estimate of Dm = 1/Dnorm from
;; "_IQdivTable" using the upper 8-bits of the
;; normalized value.
;;
;; Step 3) Use Newton-Raphson algorithm to improve accuracy.
;; Repeat following equation two times. First iteration
;; gives 16-bit accuracy. Second iteration gives 32-bit
;; accuracy:
;;
;; Dm = 2(Dm - Dm^2*Dnorm/2)
;;
;; Step 4) Multiply by numerator value:
;;
;; Ym = N * Dm
;;
;; Step 5) Denormalize result, saturate and round:
;;
;; Y = Ym >> n
;;
;;---------------------------------------------------------------------------
;; Benchmark:
;;
;; Assumptions:
;; * Code executes from 0-wait SARAM block.
;; * _IQdiv Tables located in seperate 0-wait SARAM or 1-wait ROM block.
;; * Stack located in seperate 0-wait SARAM block.
;; * Includes LCR/LRETR.
;;
;; Cycles = 62 (if _IQdiv Tables in 0-wait SARAM)
;; 70 (if _IQdiv Tables in 1-wait ROM)
;; 22 (if Den == 0)
;;
;;===========================================================================
IQNdiv .macro q_value
MOVL *SP++,ACC ; Temp save Numerator on stack
MOVL ACC,*-SP[6] ; Load ACC with Denominator
CSB ACC ; Count sign bits, T = n
LSLL ACC,T ; ACC = Dnorm (Q31)
MOVL @XAR4,ACC ; Temp save "Dnorm"
LSR AH,#6
SBF $10,EQ ; If Den = 0, exit and load max value
MOVZ AR0,@AH ; AR0 = index into _IQdivTable
MOVL XAR7,#(_IQdivTable-254)
MOVB AH,#(61 - q_value) ; AH = 61 - q_value
SUBR @T,AH ; T = 61 - q_value - n
MOVL XAR5,@XT ; AR5H = 61 - q_value - n
MOVL XT,*+XAR7[AR0] ; (1) XT = initial Dm estimate (Q29)
MOVL XAR7,#_IQdivRoundSatTable
MOVL XAR6,*XAR7++ ; XAR6 = 2.0 (Q29)
IMPYL P,XT,@XAR4 ; (2)
QMPYL ACC,XT,@XAR4 ; (2) ACC:P = Dm*Dnorm (Q60)
LSL64 ACC:P,#1 ; (3) ACC:P = Dm*Dnorm (Q61)
SUBL ACC,@XAR6 ; (4) ACC = Dm*Dnorm - 2.0 (Q29)
NEG ACC ; (5) ACC = 2.0 - Dm*Dnorm (Q29)
IMPYL P,XT,@ACC ; (6)
QMPYL ACC,XT,@ACC ; (6) ACC:P = Dm(2.0 - Dm*Dnorm) (Q58)
LSL64 ACC:P,#3 ; (7) ACC:P = Dm(2.0 - Dm*Dnorm) (Q61)
MOVL XT,@ACC ; (1) XT = Dm (Q29)
IMPYL P,XT,@XAR4 ; (2)
QMPYL ACC,XT,@XAR4 ; (2) ACC:P = Dm*Dnorm (Q60)
LSL64 ACC:P,#1 ; (3) ACC:P = Dm*Dnorm (Q61)
SUBL ACC,@XAR6 ; (4) ACC = Dm*Dnorm - 2.0 (Q29)
NEG ACC ; (5) ACC = 2.0 - Dm*Dnorm (Q29)
IMPYL P,XT,@ACC ; (6)
QMPYL ACC,XT,@ACC ; (6) ACC:P = Dm(2.0 - Dm*Dnorm) (Q58)
LSL64 ACC:P,#3 ; (7) ACC:P = Dm(2.0 - Dm*Dnorm) (Q61)
MOVL XT,*--SP ; XT = Numerator
IMPYL P,XT,@ACC
QMPYL ACC,XT,@ACC ; ACC:P = N * Dm
LSL64 ACC:P,#2
MOVL XT,@XAR5 ; T = 61 - q_value - n
ASR64 ACC:P,T ; scale back result
ADDUL P,*XAR7++ ; Round result
ADDCL ACC,*XAR7++
MINL ACC,*+XAR7[2] ; Saturate result
MINCUL P,*+XAR7[0]
MAXL ACC,*+XAR7[6]
MAXCUL P,*+XAR7[4]
ASR64 ACC:P,#1 ; ACC = final IQ result
MOVL ACC,@P
LRETR
$10:
MOV @AL,#0xFFFF
MOV @AH,#0x7FFF
SUBB SP,#2
LRETR
.endm
;; The value "GLOBAL_Q" needs to be supplied by the assembler
;; using the "-dGLOBAL_Q=q_value" directive:
.sect "IQmath"
.ref _IQdivTable
.ref _IQdivRoundSatTable
.if GLOBAL_Q == 30
.def __IQ30div
__IQ30div:
IQNdiv GLOBAL_Q
.endif
.if GLOBAL_Q == 29
.def __IQ29div
__IQ29div:
IQNdiv GLOBAL_Q
.endif
.if GLOBAL_Q == 28
.def __IQ28div
__IQ28div:
IQNdiv GLOBAL_Q
.endif
.if GLOBAL_Q == 27
.def __IQ27div
__IQ27div:
IQNdiv GLOBAL_Q
.endif
.if GLOBAL_Q == 26
.def __IQ26div
__IQ26div:
IQNdiv GLOBAL_Q
.endif
.if GLOBAL_Q == 25
.def __IQ25div
__IQ25div:
IQNdiv GLOBAL_Q
.endif
.if GLOBAL_Q == 24
.def __IQ24div
__IQ24div:
IQNdiv GLOBAL_Q
.endif
.if GLOBAL_Q == 23
.def __IQ23div
__IQ23div:
IQNdiv GLOBAL_Q
.endif
.if GLOBAL_Q == 22
.def __IQ22div
__IQ22div:
IQNdiv GLOBAL_Q
.endif
.if GLOBAL_Q == 21
.def __IQ21div
__IQ21div:
IQNdiv GLOBAL_Q
.endif
.if GLOBAL_Q == 20
.def __IQ20div
__IQ20div:
IQNdiv GLOBAL_Q
.endif
.if GLOBAL_Q == 19
.def __IQ19div
__IQ19div:
IQNdiv GLOBAL_Q
.endif
.if GLOBAL_Q == 18
.def __IQ18div
__IQ18div:
IQNdiv GLOBAL_Q
.endif
.if GLOBAL_Q == 17
.def __IQ17div
__IQ17div:
IQNdiv GLOBAL_Q
.endif
.if GLOBAL_Q == 16
.def __IQ16div
__IQ16div:
IQNdiv GLOBAL_Q
.endif
.if GLOBAL_Q == 15
.def __IQ15div
__IQ15div:
IQNdiv GLOBAL_Q
.endif
.if GLOBAL_Q == 14
.def __IQ14div
__IQ14div:
IQNdiv GLOBAL_Q
.endif
.if GLOBAL_Q == 13
.def __IQ13div
__IQ13div:
IQNdiv GLOBAL_Q
.endif
.if GLOBAL_Q == 12
.def __IQ12div
__IQ12div:
IQNdiv GLOBAL_Q
.endif
.if GLOBAL_Q == 11
.def __IQ11div
__IQ11div:
IQNdiv GLOBAL_Q
.endif
.if GLOBAL_Q == 10
.def __IQ10div
__IQ10div:
IQNdiv GLOBAL_Q
.endif
.if GLOBAL_Q == 9
.def __IQ9div
__IQ9div:
IQNdiv GLOBAL_Q
.endif
.if GLOBAL_Q == 8
.def __IQ8div
__IQ8div:
IQNdiv GLOBAL_Q
.endif
.if GLOBAL_Q == 7
.def __IQ7div
__IQ7div:
IQNdiv GLOBAL_Q
.endif
.if GLOBAL_Q == 6
.def __IQ6div
__IQ6div:
IQNdiv GLOBAL_Q
.endif
.if GLOBAL_Q == 5
.def __IQ5div
__IQ5div:
IQNdiv GLOBAL_Q
.endif
.if GLOBAL_Q == 4
.def __IQ4div
__IQ4div:
IQNdiv GLOBAL_Q
.endif
.if GLOBAL_Q == 3
.def __IQ3div
__IQ3div:
IQNdiv GLOBAL_Q
.endif
.if GLOBAL_Q == 2
.def __IQ2div
__IQ2div:
IQNdiv GLOBAL_Q
.endif
.if GLOBAL_Q == 1
.def __IQ1div
__IQ1div:
IQNdiv GLOBAL_Q
.endif
;;###########################################################################
;; No More.
;;###########################################################################
@@ -0,0 +1,368 @@
;;###########################################################################
;;
;; FILE: IQNexp.asm
;;
;; TITLE: C Callable IQ Exponent Math Function
;;
;;###########################################################################
;;
;; Ver | Date | Who | Description of changes
;; =====|=============|=======|==============================================
;; 1.0 | 11 Jul 2002 | A. T. | Original Release.
;; -----|-------------|-------|----------------------------------------------
;; 1.1 | 10 Mar 2005 | A. T. | Fixed Bug #1. "LSL ACC,T" instruction
;; | | | should have been "LSLL ACC,T".
;; -----|-------------|-------|----------------------------------------------
;; | | |
;;
;;###########################################################################
;;#############################################################################
;;!
;;! Copyright: Copyright (C) 2023 Texas Instruments Incorporated -
;;! All rights reserved not granted herein.
;;! Limited License.
;;!
;;! Texas Instruments Incorporated grants a world-wide, royalty-free,
;;! non-exclusive license under copyrights and patents it now or hereafter
;;! owns or controls to make, have made, use, import, offer to sell and sell
;;! ("Utilize") this software subject to the terms herein. With respect to the
;;! foregoing patent license, such license is granted solely to the extent that
;;! any such patent is necessary to Utilize the software alone. The patent
;;! license shall not apply to any combinations which include this software,
;;! other than combinations with devices manufactured by or for TI
;;! ("TI Devices").
;;! No hardware patent is licensed hereunder.
;;!
;;! Redistributions must preserve existing copyright notices and reproduce this
;;! license (including the above copyright notice and the disclaimer and
;;! (if applicable) source code license limitations below) in the documentation
;;! and/or other materials provided with the distribution.
;;!
;;! Redistribution and use in binary form, without modification, are permitted
;;! provided that the following conditions are met:
;;!
;;! * No reverse engineering, decompilation, or disassembly of this software is
;;! permitted with respect to any software provided in binary form.
;;! * Any redistribution and use are licensed by TI for use only
;;! with TI Devices.
;;! * Nothing shall obligate TI to provide you with source code for the
;;! software licensed and provided to you in object code.
;;!
;;! If software source code is provided to you, modification and redistribution
;;! of the source code are permitted provided that the following conditions
;;! are met:
;;!
;;! * any redistribution and use of the source code, including any resulting
;;! derivative works, are licensed by TI for use only with TI Devices.
;;! * any redistribution and use of any object code compiled from the source
;;! code and any resulting derivative works, are licensed by TI for use
;;! only with TI Devices.
;;!
;;! Neither the name of Texas Instruments Incorporated nor the names of its
;;! suppliers may be used to endorse or promote products derived from this
;;! software without specific prior written permission.
;;#############################################################################
;;===========================================================================
;; Function: _IQNexp
;;===========================================================================
;;
;; C Usage: extern long _IQNexp(long X); // Saturated
;;
;;---------------------------------------------------------------------------
;;
;; On Entry: ACC = X in IQ format
;;
;; Regs Used: XAR7, XAR6, XAR5, XAR4, XAR3
;; XT, P
;;
;; On Exit: ACC = exp(X) result in IQ format
;;
;; Q range: 29 to 1
;;
;;---------------------------------------------------------------------------
;; Algorithm: The procedure for calculating "Y = exp(X)" is as follows:
;;
;; Step 1) Saturate input to avoid overflow.
;;
;; Step 2) Normalize Input:
;;
;; X' = X * 2^n -1.0 <= X' < 1.0
;;
;; Step 3) Obtain Y' = exp(X') using taylor series expansion:
;;
;; Y' = 1 + X' + X'^2/2! + .... + X^11/11!
;;
;; Step 4) Denormalize value to get output Y:
;;
;; if( n == 0 ) Y = Y'
;; if( n == 1 ) Y = Y'^2
;; if( n == 2 ) Y = Y'^4
;; if( n == 3 ) Y = Y'^8
;; if( n == 4 ) Y = Y'^16
;;
;;---------------------------------------------------------------------------
;; Benchmark:
;;
;; Assumptions:
;; * Code executes from 0-wait SARAM block.
;; * _IQexp Tables located in seperate 0-wait SARAM or 1-wait ROM block.
;; * Stack located in seperate SARAM block.
;; * Includes LCR/LRETR.
;;
;; Cycles = 168 to 215
;;
;;===========================================================================
IQNexp .macro q_value
PUSH XAR3
; Step 1: Saturate input to avoid overflow:
MOVL XAR7,#(_IQexpTableMinMax + (q_value - 1)*4)
MINL ACC,*+XAR7[0] ; Saturate Input To Max Value
MAXL ACC,*+XAR7[2] ; Saturate Input To Min Value
MOVL XAR6,@ACC ; XAR6 = X (QG)
; Step 2: Normalize Input:
CSB ACC
MOVB AL,#(31 - q_value)
SUB AL,@T
MOVB @AL,#0,LT
MOVZ AR3,@AL ; AR3 = n
MOV T,@AL
MOVL ACC,@XAR6
ASR64 ACC:P,T ; ACC = X' (QG)
.if (q_value <= 29)
MOV @T,#(29 - q_value)
LSLL ACC,T ; (Fixed Bug #1) ACC = X' (Q29)
.endif
MOVL XAR6,@ACC ; XAR6 = X' (Q29)
; Step 3: Repeat 10 Times: ACC = X*(1 + ACC/Coeff)
MOV @PH,#(1 << 12)
MOV @PL,#0
MOVL XAR5,@P ; XAR5 = 1.0 (Q28)
MOVL XAR7,#_IQexpTableCoeff ; XAR7 -> 1/10 Coeff (Q31)
MOVB XAR4,#9
IQNexpLoop1:
MOVL XT,@ACC ; XT = ACC (Q29)
QMPYL ACC,XT,*XAR7++ ; ACC = ACC/Coeff (Q28)
ADDL ACC,@XAR5 ; ACC = 1 + ACC/Coeff (Q28)
MOVL XT,@XAR6 ; XT = X (Q29)
IMPYL P,XT,@ACC
QMPYL ACC,XT,@ACC ; ACC:P = X(1 + ACC/Coeff) (Q57)
LSL64 ACC:P,#4 ; ACC = X(1 + ACC/Coeff) (Q29)
BANZ IQNexpLoop1,AR4--
ADDL ACC,@XAR5
ADDL ACC,@XAR5
.if (q_value <= 29)
MOV @T,#(29 - q_value)
ASR64 ACC:P,T ; ACC = 1 + ACC = exp(X) (QG)
.endif
; Step 4: Denormalize result:
MOVB XAR6,#0
BAR IQNexpExit,AR3,AR6,EQ ;if( n == 0 ) Y = ACC
SUBB XAR3,#1
IQNexpLoop2:
MOVL XT,@ACC
IMPYL P,XT,@ACC
QMPYL ACC,XT,@ACC
MOV @T,#(32 - q_value)
LSL64 ACC:P,T ; ACC = ACC*ACC (QG)
BANZ IQNexpLoop2,AR3-- ; Loop until n == 0
IQNexpExit:
POP XAR3
LRETR
.endm
;; The value "GLOBAL_Q" needs to be supplied by the assembler
;; using the "-dGLOBAL_Q=q_value" directive:
.sect "IQmath"
.ref _IQexpTableMinMax
.ref _IQexpTableCoeff
.if GLOBAL_Q == 30
.def __IQ30exp
__IQ30exp:
IQNexp GLOBAL_Q
.endif
.if GLOBAL_Q == 29
.def __IQ29exp
__IQ29exp:
IQNexp GLOBAL_Q
.endif
.if GLOBAL_Q == 28
.def __IQ28exp
__IQ28exp:
IQNexp GLOBAL_Q
.endif
.if GLOBAL_Q == 27
.def __IQ27exp
__IQ27exp:
IQNexp GLOBAL_Q
.endif
.if GLOBAL_Q == 26
.def __IQ26exp
__IQ26exp:
IQNexp GLOBAL_Q
.endif
.if GLOBAL_Q == 25
.def __IQ25exp
__IQ25exp:
IQNexp GLOBAL_Q
.endif
.if GLOBAL_Q == 24
.def __IQ24exp
__IQ24exp:
IQNexp GLOBAL_Q
.endif
.if GLOBAL_Q == 23
.def __IQ23exp
__IQ23exp:
IQNexp GLOBAL_Q
.endif
.if GLOBAL_Q == 22
.def __IQ22exp
__IQ22exp:
IQNexp GLOBAL_Q
.endif
.if GLOBAL_Q == 21
.def __IQ21exp
__IQ21exp:
IQNexp GLOBAL_Q
.endif
.if GLOBAL_Q == 20
.def __IQ20exp
__IQ20exp:
IQNexp GLOBAL_Q
.endif
.if GLOBAL_Q == 19
.def __IQ19exp
__IQ19exp:
IQNexp GLOBAL_Q
.endif
.if GLOBAL_Q == 18
.def __IQ18exp
__IQ18exp:
IQNexp GLOBAL_Q
.endif
.if GLOBAL_Q == 17
.def __IQ17exp
__IQ17exp:
IQNexp GLOBAL_Q
.endif
.if GLOBAL_Q == 16
.def __IQ16exp
__IQ16exp:
IQNexp GLOBAL_Q
.endif
.if GLOBAL_Q == 15
.def __IQ15exp
__IQ15exp:
IQNexp GLOBAL_Q
.endif
.if GLOBAL_Q == 14
.def __IQ14exp
__IQ14exp:
IQNexp GLOBAL_Q
.endif
.if GLOBAL_Q == 13
.def __IQ13exp
__IQ13exp:
IQNexp GLOBAL_Q
.endif
.if GLOBAL_Q == 12
.def __IQ12exp
__IQ12exp:
IQNexp GLOBAL_Q
.endif
.if GLOBAL_Q == 11
.def __IQ11exp
__IQ11exp:
IQNexp GLOBAL_Q
.endif
.if GLOBAL_Q == 10
.def __IQ10exp
__IQ10exp:
IQNexp GLOBAL_Q
.endif
.if GLOBAL_Q == 9
.def __IQ9exp
__IQ9exp:
IQNexp GLOBAL_Q
.endif
.if GLOBAL_Q == 8
.def __IQ8exp
__IQ8exp:
IQNexp GLOBAL_Q
.endif
.if GLOBAL_Q == 7
.def __IQ7exp
__IQ7exp:
IQNexp GLOBAL_Q
.endif
.if GLOBAL_Q == 6
.def __IQ6exp
__IQ6exp:
IQNexp GLOBAL_Q
.endif
.if GLOBAL_Q == 5
.def __IQ5exp
__IQ5exp:
IQNexp GLOBAL_Q
.endif
.if GLOBAL_Q == 4
.def __IQ4exp
__IQ4exp:
IQNexp GLOBAL_Q
.endif
.if GLOBAL_Q == 3
.def __IQ3exp
__IQ3exp:
IQNexp GLOBAL_Q
.endif
.if GLOBAL_Q == 2
.def __IQ2exp
__IQ2exp:
IQNexp GLOBAL_Q
.endif
.if GLOBAL_Q == 1
.def __IQ1exp
__IQ1exp:
IQNexp GLOBAL_Q
.endif
;;###########################################################################
;; No More.
;;###########################################################################
@@ -0,0 +1,150 @@
;;##########################################################################
;;
;;Title: Table 2 For IQmath Functions
;;
;;Version: 1.0
;;
;;Contents: IQexp Function Table, Size Of Table = 140x16
;;
;;##########################################################################
;;==========================================================================
;; IQexp Function Table, Size Of Table = 140x16
;;==========================================================================
;;#############################################################################
;;!
;;! Copyright: Copyright (C) 2023 Texas Instruments Incorporated -
;;! All rights reserved not granted herein.
;;! Limited License.
;;!
;;! Texas Instruments Incorporated grants a world-wide, royalty-free,
;;! non-exclusive license under copyrights and patents it now or hereafter
;;! owns or controls to make, have made, use, import, offer to sell and sell
;;! ("Utilize") this software subject to the terms herein. With respect to the
;;! foregoing patent license, such license is granted solely to the extent that
;;! any such patent is necessary to Utilize the software alone. The patent
;;! license shall not apply to any combinations which include this software,
;;! other than combinations with devices manufactured by or for TI
;;! ("TI Devices").
;;! No hardware patent is licensed hereunder.
;;!
;;! Redistributions must preserve existing copyright notices and reproduce this
;;! license (including the above copyright notice and the disclaimer and
;;! (if applicable) source code license limitations below) in the documentation
;;! and/or other materials provided with the distribution.
;;!
;;! Redistribution and use in binary form, without modification, are permitted
;;! provided that the following conditions are met:
;;!
;;! * No reverse engineering, decompilation, or disassembly of this software is
;;! permitted with respect to any software provided in binary form.
;;! * Any redistribution and use are licensed by TI for use only
;;! with TI Devices.
;;! * Nothing shall obligate TI to provide you with source code for the
;;! software licensed and provided to you in object code.
;;!
;;! If software source code is provided to you, modification and redistribution
;;! of the source code are permitted provided that the following conditions
;;! are met:
;;!
;;! * any redistribution and use of the source code, including any resulting
;;! derivative works, are licensed by TI for use only with TI Devices.
;;! * any redistribution and use of any object code compiled from the source
;;! code and any resulting derivative works, are licensed by TI for use
;;! only with TI Devices.
;;!
;;! Neither the name of Texas Instruments Incorporated nor the names of its
;;! suppliers may be used to endorse or promote products derived from this
;;! software without specific prior written permission.
;;#############################################################################
.def _IQexpTable
.def _IQexpTableMinMax
.def _IQexpTableMinMaxEnd
.def _IQexpTableCoeff
.def _IQexpTableCoeffEnd
.sect "IQmathTablesRam"
_IQexpTable:
_IQexpTableMinMax:
.long 42 ; Q 1 Max Input Value = 20.794415416333
.long -1 ; Q 1 Min Input Value = -0.693147180560
.long 80 ; Q 2 Max Input Value = 20.101268235773
.long -6 ; Q 2 Min Input Value = -1.386294361120
.long 155 ; Q 3 Max Input Value = 19.408121055213
.long -17 ; Q 3 Min Input Value = -2.079441541680
.long 299 ; Q 4 Max Input Value = 18.714973874653
.long -44 ; Q 4 Min Input Value = -2.772588722240
.long 577 ; Q 5 Max Input Value = 18.021826694093
.long -111 ; Q 5 Min Input Value = -3.465735902800
.long 1109 ; Q 6 Max Input Value = 17.328679513533
.long -266 ; Q 6 Min Input Value = -4.158883083360
.long 2129 ; Q 7 Max Input Value = 16.635532332973
.long -621 ; Q 7 Min Input Value = -4.852030263920
.long 4081 ; Q 8 Max Input Value = 15.942385152413
.long -1420 ; Q 8 Min Input Value = -5.545177444480
.long 7808 ; Q 9 Max Input Value = 15.249237971853
.long -3194 ; Q 9 Min Input Value = -6.238324625040
.long 14905 ; Q10 Max Input Value = 14.556090791293
.long -7098 ; Q10 Min Input Value = -6.931471805599
.long 28391 ; Q11 Max Input Value = 13.862943610733
.long -15615 ; Q11 Min Input Value = -7.624618986159
.long 53943 ; Q12 Max Input Value = 13.169796430173
.long -34070 ; Q12 Min Input Value = -8.317766166719
.long 102209 ; Q13 Max Input Value = 12.476649249613
.long -73817 ; Q13 Min Input Value = -9.010913347279
.long 193061 ; Q14 Max Input Value = 11.783502069053
.long -158991 ; Q14 Min Input Value = -9.704060527839
.long 363409 ; Q15 Max Input Value = 11.090354888493
.long -340696 ; Q15 Min Input Value = -10.397207708399
.long 681391 ; Q16 Max Input Value = 10.397207707934
.long -726817 ; Q16 Min Input Value = -11.090354888959
.long 1271931 ; Q17 Max Input Value = 9.704060527374
.long -1544487 ; Q17 Min Input Value = -11.783502069519
.long 2362157 ; Q18 Max Input Value = 9.010913346814
.long -3270679 ; Q18 Min Input Value = -12.476649250079
.long 4360905 ; Q19 Max Input Value = 8.317766166254
.long -6904766 ; Q19 Min Input Value = -13.169796430639
.long 7994992 ; Q20 Max Input Value = 7.624618985694
.long -14536350 ; Q20 Min Input Value = -13.862943611199
.long 14536350 ; Q21 Max Input Value = 6.931471805134
.long -30526335 ; Q21 Min Input Value = -14.556090791759
.long 26165430 ; Q22 Max Input Value = 6.238324624574
.long -63959940 ; Q22 Min Input Value = -15.249237972319
.long 46516320 ; Q23 Max Input Value = 5.545177444014
.long -133734420 ; Q23 Min Input Value = -15.942385152879
.long 81403560 ; Q24 Max Input Value = 4.852030263454
.long -279097919 ; Q24 Min Input Value = -16.635532333439
.long 139548960 ; Q25 Max Input Value = 4.158883082894
.long -581453998 ; Q25 Min Input Value = -17.328679513999
.long 232581599 ; Q26 Max Input Value = 3.465735902334
.long -1209424317 ; Q26 Min Input Value = -18.021826694559
.long 372130559 ; Q27 Max Input Value = 2.772588721774
.long -2147483648 ; Q27 Min Input Value = -16.000000000000
.long 558195838 ; Q28 Max Input Value = 2.079441541214
.long -2147483648 ; Q28 Min Input Value = -8.000000000000
.long 744261118 ; Q29 Max Input Value = 1.386294360654
.long -2147483648 ; Q29 Min Input Value = -4.000000000000
.long 744261117 ; Q30 Max Input Value = 0.693147180094
.long -2147483648 ; Q30 Min Input Value = -2.000000000000
_IQexpTableMinMaxEnd:
_IQexpTableCoeff:
.long 0x0BA2E8BA ; 1/11 in Q31
.long 0x0CCCCCCD ; 1/10 in Q31
.long 0x0E38E38E ; 1/9 in Q31
.long 0x10000000 ; 1/8 in Q31
.long 0x12492492 ; 1/7 in Q31
.long 0x15555555 ; 1/6 in Q31
.long 0x1999999A ; 1/5 in Q31
.long 0x20000000 ; 1/4 in Q31
.long 0x2AAAAAAB ; 1/3 in Q31
.long 0x40000000 ; 1/2 in Q31
_IQexpTableCoeffEnd:
_IQexpTableEnd:
;;
;; End Of File.
;;
@@ -0,0 +1,298 @@
;;###########################################################################
;;
;; FILE: IQNfrac.asm
;;
;; TITLE: C Callable IQ Fractional Math Function
;;
;;###########################################################################
;;
;; Ver | Date | Who | Description of changes
;; =====|=============|=======|==============================================
;; 1.4 | 17 May 2002 | A. T. | Original Release.
;; -----|-------------|-------|----------------------------------------------
;; 1.4c | 20 Jun 2002 | A. T. | Fixed problem with function when operating
;; | | | on negative values with a zero fractional
;; | | | part, incorrect result was given.
;; -----|-------------|-------|----------------------------------------------
;; 1.4d | 30 Mar 2002 | SD | Frac was implemented as ((A >> q_value) << q_value) - A
;; | | | Hence sign was changed. Reimplemented it correctly.
;;------|-------------|-------|----------------------------------------------
;;
;;###########################################################################
;;===========================================================================
;; Function: _IQNfrac
;;===========================================================================
;;
;; C Usage: extern long _IQfrac(long A); // no round or sat
;;
;;---------------------------------------------------------------------------
;;
;; On Entry: ACC = A in IQ format
;;
;; Regs Used: XT, XAR7
;;
;; On Exit: ACC = fractional portion of A stored as IQ
;;
;; Q range: 30 to 1
;;
;;---------------------------------------------------------------------------
;; Algorithm: The fractional portion of the IQ number is calculated
;; as follows:
;;
;; frac = A - ((A >> q_value) << q_value);
;;
;;---------------------------------------------------------------------------
;; Benchmark:
;;
;; Cycles = 18 (includes LCR/LRETR)
;;
;;---------------------------------------------------------------------------
;;#############################################################################
;;!
;;! Copyright: Copyright (C) 2023 Texas Instruments Incorporated -
;;! All rights reserved not granted herein.
;;! Limited License.
;;!
;;! Texas Instruments Incorporated grants a world-wide, royalty-free,
;;! non-exclusive license under copyrights and patents it now or hereafter
;;! owns or controls to make, have made, use, import, offer to sell and sell
;;! ("Utilize") this software subject to the terms herein. With respect to the
;;! foregoing patent license, such license is granted solely to the extent that
;;! any such patent is necessary to Utilize the software alone. The patent
;;! license shall not apply to any combinations which include this software,
;;! other than combinations with devices manufactured by or for TI
;;! ("TI Devices").
;;! No hardware patent is licensed hereunder.
;;!
;;! Redistributions must preserve existing copyright notices and reproduce this
;;! license (including the above copyright notice and the disclaimer and
;;! (if applicable) source code license limitations below) in the documentation
;;! and/or other materials provided with the distribution.
;;!
;;! Redistribution and use in binary form, without modification, are permitted
;;! provided that the following conditions are met:
;;!
;;! * No reverse engineering, decompilation, or disassembly of this software is
;;! permitted with respect to any software provided in binary form.
;;! * Any redistribution and use are licensed by TI for use only
;;! with TI Devices.
;;! * Nothing shall obligate TI to provide you with source code for the
;;! software licensed and provided to you in object code.
;;!
;;! If software source code is provided to you, modification and redistribution
;;! of the source code are permitted provided that the following conditions
;;! are met:
;;!
;;! * any redistribution and use of the source code, including any resulting
;;! derivative works, are licensed by TI for use only with TI Devices.
;;! * any redistribution and use of any object code compiled from the source
;;! code and any resulting derivative works, are licensed by TI for use
;;! only with TI Devices.
;;!
;;! Neither the name of Texas Instruments Incorporated nor the names of its
;;! suppliers may be used to endorse or promote products derived from this
;;! software without specific prior written permission.
;;#############################################################################
IQNfrac .macro q_value
SETC OVM ; Turn overflow mode on
CLRC TC ; Clear sign flag
ABSTC ACC ; Take abs value of ACC
MOV @T,#q_value ; Extract the fractional bits.
LSR64 ACC:P,T ; P=frac bits (aligned to MSB)
MOVB ACC,#0
LSL64 ACC:P,T ; ACC=frac bits (aligned to LSB)
NEGTC ACC ; Negate if sign set
CLRC OVM ; Clear overflow mode
LRETR
.endm
;; The value "GLOBAL_Q" needs to be supplied by the assembler
;; using the "-dGLOBAL_Q=q_value" directive:
.sect "IQmath"
.if GLOBAL_Q == 30
.def __IQ30frac
__IQ30frac:
IQNfrac GLOBAL_Q
.endif
.if GLOBAL_Q == 29
.def __IQ29frac
__IQ29frac:
IQNfrac GLOBAL_Q
.endif
.if GLOBAL_Q == 28
.def __IQ28frac
__IQ28frac:
IQNfrac GLOBAL_Q
.endif
.if GLOBAL_Q == 27
.def __IQ27frac
__IQ27frac:
IQNfrac GLOBAL_Q
.endif
.if GLOBAL_Q == 26
.def __IQ26frac
__IQ26frac:
IQNfrac GLOBAL_Q
.endif
.if GLOBAL_Q == 25
.def __IQ25frac
__IQ25frac:
IQNfrac GLOBAL_Q
.endif
.if GLOBAL_Q == 24
.def __IQ24frac
__IQ24frac:
IQNfrac GLOBAL_Q
.endif
.if GLOBAL_Q == 23
.def __IQ23frac
__IQ23frac:
IQNfrac GLOBAL_Q
.endif
.if GLOBAL_Q == 22
.def __IQ22frac
__IQ22frac:
IQNfrac GLOBAL_Q
.endif
.if GLOBAL_Q == 21
.def __IQ21frac
__IQ21frac:
IQNfrac GLOBAL_Q
.endif
.if GLOBAL_Q == 20
.def __IQ20frac
__IQ20frac:
IQNfrac GLOBAL_Q
.endif
.if GLOBAL_Q == 19
.def __IQ19frac
__IQ19frac:
IQNfrac GLOBAL_Q
.endif
.if GLOBAL_Q == 18
.def __IQ18frac
__IQ18frac:
IQNfrac GLOBAL_Q
.endif
.if GLOBAL_Q == 17
.def __IQ17frac
__IQ17frac:
IQNfrac GLOBAL_Q
.endif
.if GLOBAL_Q == 16
.def __IQ16frac
__IQ16frac:
IQNfrac GLOBAL_Q
.endif
.if GLOBAL_Q == 15
.def __IQ15frac
__IQ15frac:
IQNfrac GLOBAL_Q
.endif
.if GLOBAL_Q == 14
.def __IQ14frac
__IQ14frac:
IQNfrac GLOBAL_Q
.endif
.if GLOBAL_Q == 13
.def __IQ13frac
__IQ13frac:
IQNfrac GLOBAL_Q
.endif
.if GLOBAL_Q == 12
.def __IQ12frac
__IQ12frac:
IQNfrac GLOBAL_Q
.endif
.if GLOBAL_Q == 11
.def __IQ11frac
__IQ11frac:
IQNfrac GLOBAL_Q
.endif
.if GLOBAL_Q == 10
.def __IQ10frac
__IQ10frac:
IQNfrac GLOBAL_Q
.endif
.if GLOBAL_Q == 9
.def __IQ9frac
__IQ9frac:
IQNfrac GLOBAL_Q
.endif
.if GLOBAL_Q == 8
.def __IQ8frac
__IQ8frac:
IQNfrac GLOBAL_Q
.endif
.if GLOBAL_Q == 7
.def __IQ7frac
__IQ7frac:
IQNfrac GLOBAL_Q
.endif
.if GLOBAL_Q == 6
.def __IQ6frac
__IQ6frac:
IQNfrac GLOBAL_Q
.endif
.if GLOBAL_Q == 5
.def __IQ5frac
__IQ5frac:
IQNfrac GLOBAL_Q
.endif
.if GLOBAL_Q == 4
.def __IQ4frac
__IQ4frac:
IQNfrac GLOBAL_Q
.endif
.if GLOBAL_Q == 3
.def __IQ3frac
__IQ3frac:
IQNfrac GLOBAL_Q
.endif
.if GLOBAL_Q == 2
.def __IQ2frac
__IQ2frac:
IQNfrac GLOBAL_Q
.endif
.if GLOBAL_Q == 1
.def __IQ1frac
__IQ1frac:
IQNfrac GLOBAL_Q
.endif
;;###########################################################################
;; No More.
;;###########################################################################
@@ -0,0 +1,292 @@
;;###########################################################################
;;
;; FILE: IQNint.asm
;;
;; TITLE: C Callable IQ Integer Math Function
;;
;;###########################################################################
;;
;; Ver | Date | Who | Description of changes
;; =====|=============|=======|==============================================
;; 1.4 | 17 May 2002 | A. T. | Original Release.
;; -----|-------------|-------|----------------------------------------------
;; 1.4c | 20 Jun 2002 | A. T. | Fixed problem with function when operating
;; | | | on negative values with a zero fractional
;; | | | part, incorrect result was given.
;; -----|-------------|-------|----------------------------------------------
;; | | |
;;
;;###########################################################################
;;===========================================================================
;; Function: _IQNint
;;===========================================================================
;;
;; C Usage: extern long _IQint(long A); // no round or sat
;;
;;---------------------------------------------------------------------------
;;
;; On Entry: ACC = A in IQ format
;;
;; Regs Used: XT
;;
;; On Exit: ACC = integer portion of A stored as "long"
;;
;; Q range: 30 to 1
;;
;;---------------------------------------------------------------------------
;; Algorithm: The integer portion of the IQ number is calculated
;; as follows:
;;
;; integer = A >> q_value;
;;
;;---------------------------------------------------------------------------
;; Benchmark:
;;
;; Cycles = 15 (includes LCR/LRETR)
;;
;;---------------------------------------------------------------------------
;;#############################################################################
;;!
;;! Copyright: Copyright (C) 2023 Texas Instruments Incorporated -
;;! All rights reserved not granted herein.
;;! Limited License.
;;!
;;! Texas Instruments Incorporated grants a world-wide, royalty-free,
;;! non-exclusive license under copyrights and patents it now or hereafter
;;! owns or controls to make, have made, use, import, offer to sell and sell
;;! ("Utilize") this software subject to the terms herein. With respect to the
;;! foregoing patent license, such license is granted solely to the extent that
;;! any such patent is necessary to Utilize the software alone. The patent
;;! license shall not apply to any combinations which include this software,
;;! other than combinations with devices manufactured by or for TI
;;! ("TI Devices").
;;! No hardware patent is licensed hereunder.
;;!
;;! Redistributions must preserve existing copyright notices and reproduce this
;;! license (including the above copyright notice and the disclaimer and
;;! (if applicable) source code license limitations below) in the documentation
;;! and/or other materials provided with the distribution.
;;!
;;! Redistribution and use in binary form, without modification, are permitted
;;! provided that the following conditions are met:
;;!
;;! * No reverse engineering, decompilation, or disassembly of this software is
;;! permitted with respect to any software provided in binary form.
;;! * Any redistribution and use are licensed by TI for use only
;;! with TI Devices.
;;! * Nothing shall obligate TI to provide you with source code for the
;;! software licensed and provided to you in object code.
;;!
;;! If software source code is provided to you, modification and redistribution
;;! of the source code are permitted provided that the following conditions
;;! are met:
;;!
;;! * any redistribution and use of the source code, including any resulting
;;! derivative works, are licensed by TI for use only with TI Devices.
;;! * any redistribution and use of any object code compiled from the source
;;! code and any resulting derivative works, are licensed by TI for use
;;! only with TI Devices.
;;!
;;! Neither the name of Texas Instruments Incorporated nor the names of its
;;! suppliers may be used to endorse or promote products derived from this
;;! software without specific prior written permission.
;;#############################################################################
IQNint .macro q_value
SETC OVM ; Turn overflow mode on
CLRC TC ; Clear sign flag
ABSTC ACC ; Take abs value and set sign
MOV @T,#q_value
ASRL ACC,T ; ACC = ACC >> q_value
NEGTC ACC ; Negate if sign set
CLRC OVM ; Turn overflow mode off
LRETR
.endm
;; The value "GLOBAL_Q" needs to be supplied by the assembler
;; using the "-dGLOBAL_Q=q_value" directive:
.sect "IQmath"
.if GLOBAL_Q == 30
.def __IQ30int
__IQ30int:
IQNint GLOBAL_Q
.endif
.if GLOBAL_Q == 29
.def __IQ29int
__IQ29int:
IQNint GLOBAL_Q
.endif
.if GLOBAL_Q == 28
.def __IQ28int
__IQ28int:
IQNint GLOBAL_Q
.endif
.if GLOBAL_Q == 27
.def __IQ27int
__IQ27int:
IQNint GLOBAL_Q
.endif
.if GLOBAL_Q == 26
.def __IQ26int
__IQ26int:
IQNint GLOBAL_Q
.endif
.if GLOBAL_Q == 25
.def __IQ25int
__IQ25int:
IQNint GLOBAL_Q
.endif
.if GLOBAL_Q == 24
.def __IQ24int
__IQ24int:
IQNint GLOBAL_Q
.endif
.if GLOBAL_Q == 23
.def __IQ23int
__IQ23int:
IQNint GLOBAL_Q
.endif
.if GLOBAL_Q == 22
.def __IQ22int
__IQ22int:
IQNint GLOBAL_Q
.endif
.if GLOBAL_Q == 21
.def __IQ21int
__IQ21int:
IQNint GLOBAL_Q
.endif
.if GLOBAL_Q == 20
.def __IQ20int
__IQ20int:
IQNint GLOBAL_Q
.endif
.if GLOBAL_Q == 19
.def __IQ19int
__IQ19int:
IQNint GLOBAL_Q
.endif
.if GLOBAL_Q == 18
.def __IQ18int
__IQ18int:
IQNint GLOBAL_Q
.endif
.if GLOBAL_Q == 17
.def __IQ17int
__IQ17int:
IQNint GLOBAL_Q
.endif
.if GLOBAL_Q == 16
.def __IQ16int
__IQ16int:
IQNint GLOBAL_Q
.endif
.if GLOBAL_Q == 15
.def __IQ15int
__IQ15int:
IQNint GLOBAL_Q
.endif
.if GLOBAL_Q == 14
.def __IQ14int
__IQ14int:
IQNint GLOBAL_Q
.endif
.if GLOBAL_Q == 13
.def __IQ13int
__IQ13int:
IQNint GLOBAL_Q
.endif
.if GLOBAL_Q == 12
.def __IQ12int
__IQ12int:
IQNint GLOBAL_Q
.endif
.if GLOBAL_Q == 11
.def __IQ11int
__IQ11int:
IQNint GLOBAL_Q
.endif
.if GLOBAL_Q == 10
.def __IQ10int
__IQ10int:
IQNint GLOBAL_Q
.endif
.if GLOBAL_Q == 9
.def __IQ9int
__IQ9int:
IQNint GLOBAL_Q
.endif
.if GLOBAL_Q == 8
.def __IQ8int
__IQ8int:
IQNint GLOBAL_Q
.endif
.if GLOBAL_Q == 7
.def __IQ7int
__IQ7int:
IQNint GLOBAL_Q
.endif
.if GLOBAL_Q == 6
.def __IQ6int
__IQ6int:
IQNint GLOBAL_Q
.endif
.if GLOBAL_Q == 5
.def __IQ5int
__IQ5int:
IQNint GLOBAL_Q
.endif
.if GLOBAL_Q == 4
.def __IQ4int
__IQ4int:
IQNint GLOBAL_Q
.endif
.if GLOBAL_Q == 3
.def __IQ3int
__IQ3int:
IQNint GLOBAL_Q
.endif
.if GLOBAL_Q == 2
.def __IQ2int
__IQ2int:
IQNint GLOBAL_Q
.endif
.if GLOBAL_Q == 1
.def __IQ1int
__IQ1int:
IQNint GLOBAL_Q
.endif
;;###########################################################################
;; No More.
;;###########################################################################
@@ -0,0 +1,395 @@
;;###########################################################################
;;
;; FILE: IQNisqrt.asm
;;
;; TITLE: C Callable IQ Inverse Square Root Math Function
;;
;;###########################################################################
;;
;; Ver | Date | Who | Description of changes
;; =====|==============|=======|=============================================
;; 1.2 | 11 Oct 2001 | A. T. | Original Release.
;; -----|--------------|-------|---------------------------------------------
;; 1.3 | 19 Nov 2001 | A. T. | Improved accuracy.
;; -----|--------------|-------|---------------------------------------------
;; 1.4 | 17 May 2002 | A. T. | Corrected cycle benchmarks.
;; -----|--------------|-------|---------------------------------------------
;; | | |
;;
;;###########################################################################
;;===========================================================================
;; Function: _IQNisqrt
;;===========================================================================
;;
;; C Usage: extern long _IQNisqrt(long X); // with saturation and rounding
;;
;;---------------------------------------------------------------------------
;;
;; On Entry: ACC = Val in IQ format
;;
;; Regs Used: XAR7, XAR6, XAR5, XAR4
;; XT, P, XAR0
;;
;; On Exit: ACC = 1/sqrt(X) result in IQ format
;; ACC = 0 if input is -ve or 0
;; ACC = max value "0x7FFFFFFF" if result saturates
;;
;; Q range: 30 to 1
;;
;;---------------------------------------------------------------------------
;; Algorithm: The procedure for calculating "Y = 1/sqrt(X)" is as follows:
;;
;; Step 1) Normalize Input:
;;
;; V = X * 2^n
;;
;; Step 2) Obtain initial estimate from "_IQisqrtTable"
;; using the upper 8-bits of the normalized V value.
;;
;; Step 3) Use Newton-Raphson algorithm to improve accuracy.
;; Repeat following equation two times. First iteration
;; gives 16-bit accuracy. Second iteration gives 32-bit
;; accuracy:
;;
;; Yn = Yn(1.5 - Yn*Yn*V/2)
;;
;; Yn = 1/sqrt(V)
;;
;; Step 4) Denormalize result, round and saturate:
;;
;; Y = Yn / sqrt(2^n)
;;
;;---------------------------------------------------------------------------
;; Benchmark:
;;
;; Assumptions:
;; * Code executes from 0-wait SARAM block.
;; * _IQisqrt Tables located in seperate 0-wait SARAM or 1-wait ROM block.
;; * Stack located in seperate 0-wait SARAM block.
;; * Includes LCR/LRETR.
;;
;; Cycles = 66 (30 >= Q >= 18 if _IQisqrt Tables in 0-wait SARAM)
;; 67 (17 >= Q >= 0 if _IQisqrt Tables in 0-wait SARAM)
;;
;; Cycles = 73 (30 >= Q >= 18 if _IQisqrt Tables in 1-wait ROM)
;; 74 (17 >= Q >= 0 if _IQisqrt Tables in 1-wait ROM)
;;
;;===========================================================================
;;#############################################################################
;;!
;;! Copyright: Copyright (C) 2023 Texas Instruments Incorporated -
;;! All rights reserved not granted herein.
;;! Limited License.
;;!
;;! Texas Instruments Incorporated grants a world-wide, royalty-free,
;;! non-exclusive license under copyrights and patents it now or hereafter
;;! owns or controls to make, have made, use, import, offer to sell and sell
;;! ("Utilize") this software subject to the terms herein. With respect to the
;;! foregoing patent license, such license is granted solely to the extent that
;;! any such patent is necessary to Utilize the software alone. The patent
;;! license shall not apply to any combinations which include this software,
;;! other than combinations with devices manufactured by or for TI
;;! ("TI Devices").
;;! No hardware patent is licensed hereunder.
;;!
;;! Redistributions must preserve existing copyright notices and reproduce this
;;! license (including the above copyright notice and the disclaimer and
;;! (if applicable) source code license limitations below) in the documentation
;;! and/or other materials provided with the distribution.
;;!
;;! Redistribution and use in binary form, without modification, are permitted
;;! provided that the following conditions are met:
;;!
;;! * No reverse engineering, decompilation, or disassembly of this software is
;;! permitted with respect to any software provided in binary form.
;;! * Any redistribution and use are licensed by TI for use only
;;! with TI Devices.
;;! * Nothing shall obligate TI to provide you with source code for the
;;! software licensed and provided to you in object code.
;;!
;;! If software source code is provided to you, modification and redistribution
;;! of the source code are permitted provided that the following conditions
;;! are met:
;;!
;;! * any redistribution and use of the source code, including any resulting
;;! derivative works, are licensed by TI for use only with TI Devices.
;;! * any redistribution and use of any object code compiled from the source
;;! code and any resulting derivative works, are licensed by TI for use
;;! only with TI Devices.
;;!
;;! Neither the name of Texas Instruments Incorporated nor the names of its
;;! suppliers may be used to endorse or promote products derived from this
;;! software without specific prior written permission.
;;#############################################################################
IQNisqrt .macro q_value
CSB ACC ; Count sign bits, T = n
LSLL ACC,T ; ACC = V/2 (Q32)
MOVL XAR6,@ACC ; XAR6 = V/2 (Q32)
ASR AH,#6
MOVB @AH,#0xFE,LEQ ; AR0 -> zero value if -ve or 0 input
SUB @AH,#254 ; Scale to table offset
MOVZ AR0,@AH ; AR0 -> index into _IQisqrtTable for
; initial estimate of Yn
TBIT @T,#0 ; TC = odd/even shift indicator
MOV AH,@T ; AH = n
LSR AH,#1 ; AH = n/2
MOVL *SP++,ACC ; SP-2 = n/2
MOVL XAR7,#_IQisqrtTable ; XAR7 = pointer to _IQisqrtTable
MOVL XAR4,*+XAR7[AR0] ; (1) XAR4 = initial Yn estimate (Q30)
MOVL XAR7,#_IQisqrtRoundSatTable
MOVL XAR5,*XAR7++ ; XAR5 = 1.5 (Q30)
.if (q_value & 0x0001) == 0 ; Q even
MOVB @AR0,#12,NTC ; AR0 -> 1/sqrt(2) if n == even (Q30)
MOVB @AR0,#8,TC ; AR0 -> 1.0 if n == odd (Q30)
.endif
.if (q_value & 0x0001) == 1 ; Q odd
MOVB @AR0,#12,TC ; AR0 -> 1/sqrt(2) if n == odd (Q30)
MOVB @AR0,#10,NTC ; AR0 -> 0.5 if n == even (Q30)
.endif
MOVL XT,@XAR4 ;(2) XT = Yn (Q30)
QMPYL ACC,XT,@XT ;(3) ACC = Yn*Yn (Q28)
MOVL XT,@XAR6 ;(4) XT = V/2 (Q32)
LSL ACC,#2 ;(5) ACC = Yn*Yn (Q30)
QMPYL ACC,XT,@ACC ;(6) ACC = Yn*Yn*V/2 (Q30)
MOVL XT,@XAR5 ;(7) XT = 1.5 (Q30)
SUBL @XT,ACC ;(8) XT = 1.5 - Yn*Yn*V/2 (Q30)
QMPYL ACC,XT,@XAR4 ;(9) ACC = Yn(1.5 - Yn*Yn*V/2) (Q28)
LSL ACC,#2 ;(10) ACC = Yn(1.5 - Yn*Yn*V/2) (Q30)
MOVL XAR4,@ACC ;(1) XAR4 = Yn (Q30)
MOVL XT,@XAR4 ;(2) XT = Yn (Q30)
QMPYL ACC,XT,@XT ;(3) ACC = Yn*Yn (Q28)
MOVL XT,@XAR6 ;(4) XT = V/2 (Q32)
LSL ACC,#2 ;(5) ACC = Yn*Yn (Q30)
QMPYL ACC,XT,@ACC ;(6) ACC = Yn*Yn*V/2 (Q30)
MOVL XT,@XAR5 ;(7) XT = 1.5 (Q30)
SUBL @XT,ACC ;(8) XT = 1.5 - Yn*Yn*V/2 (Q30)
QMPYL ACC,XT,@XAR4 ;(9) ACC = Yn(1.5 - Yn*Yn*V/2) (Q28)
LSL ACC,#2 ;(10) ACC = Yn(1.5 - Yn*Yn*V/2) (Q30)
MOVL XT,*+XAR7[AR0] ; Scale with 1.0 or 1/sqrt(X) or 0.5
IMPYL P,XT,@ACC
QMPYL ACC,XT,@ACC
.if q_value == 30 ; Scale final result based on Q value
LSL64 ACC:P,#2
.endif
.if q_value == 29
LSL64 ACC:P,#1
.endif
.if (q_value >= 18) & (q_value <= 28)
ASR64 ACC:P,#((86 - q_value*3)/2)
.endif
.if (q_value >= 7) & (q_value <= 17)
ASR64 ACC:P,#16
ASR64 ACC:P,#(((86 - q_value*3)/2) - 16)
.endif
.if q_value <= 6
ASR64 ACC:P,#16
ASR64 ACC:P,#16
ASR64 ACC:P,#(((86 - q_value*3)/2) - 32)
.endif
ASR64 ACC:P,#16
MOVL XT,*--SP ; T = n/2
LSL64 ACC:P,T ; Scale final result based on n/2
ADDUL P,*+XAR7[0] ; Round result
ADDCL ACC,*+XAR7[2]
MINL ACC,*+XAR7[6] ; Saturate if exceeds max pos value
MINCUL P,*+XAR7[4]
LSL64 ACC:P,#16
LRETR
.endm
;; The value "GLOBAL_Q" needs to be supplied by the assembler
;; using the "-dGLOBAL_Q=q_value" directive:
.sect "IQmath"
.ref _IQisqrtTable
.ref _IQisqrtRoundSatTable
.if GLOBAL_Q == 30
.def __IQ30isqrt
__IQ30isqrt:
IQNisqrt GLOBAL_Q
.endif
.if GLOBAL_Q == 29
.def __IQ29isqrt
__IQ29isqrt:
IQNisqrt GLOBAL_Q
.endif
.if GLOBAL_Q == 28
.def __IQ28isqrt
__IQ28isqrt:
IQNisqrt GLOBAL_Q
.endif
.if GLOBAL_Q == 27
.def __IQ27isqrt
__IQ27isqrt:
IQNisqrt GLOBAL_Q
.endif
.if GLOBAL_Q == 26
.def __IQ26isqrt
__IQ26isqrt:
IQNisqrt GLOBAL_Q
.endif
.if GLOBAL_Q == 25
.def __IQ25isqrt
__IQ25isqrt:
IQNisqrt GLOBAL_Q
.endif
.if GLOBAL_Q == 24
.def __IQ24isqrt
__IQ24isqrt:
IQNisqrt GLOBAL_Q
.endif
.if GLOBAL_Q == 23
.def __IQ23isqrt
__IQ23isqrt:
IQNisqrt GLOBAL_Q
.endif
.if GLOBAL_Q == 22
.def __IQ22isqrt
__IQ22isqrt:
IQNisqrt GLOBAL_Q
.endif
.if GLOBAL_Q == 21
.def __IQ21isqrt
__IQ21isqrt:
IQNisqrt GLOBAL_Q
.endif
.if GLOBAL_Q == 20
.def __IQ20isqrt
__IQ20isqrt:
IQNisqrt GLOBAL_Q
.endif
.if GLOBAL_Q == 19
.def __IQ19isqrt
__IQ19isqrt:
IQNisqrt GLOBAL_Q
.endif
.if GLOBAL_Q == 18
.def __IQ18isqrt
__IQ18isqrt:
IQNisqrt GLOBAL_Q
.endif
.if GLOBAL_Q == 17
.def __IQ17isqrt
__IQ17isqrt:
IQNisqrt GLOBAL_Q
.endif
.if GLOBAL_Q == 16
.def __IQ16isqrt
__IQ16isqrt:
IQNisqrt GLOBAL_Q
.endif
.if GLOBAL_Q == 15
.def __IQ15isqrt
__IQ15isqrt:
IQNisqrt GLOBAL_Q
.endif
.if GLOBAL_Q == 14
.def __IQ14isqrt
__IQ14isqrt:
IQNisqrt GLOBAL_Q
.endif
.if GLOBAL_Q == 13
.def __IQ13isqrt
__IQ13isqrt:
IQNisqrt GLOBAL_Q
.endif
.if GLOBAL_Q == 12
.def __IQ12isqrt
__IQ12isqrt:
IQNisqrt GLOBAL_Q
.endif
.if GLOBAL_Q == 11
.def __IQ11isqrt
__IQ11isqrt:
IQNisqrt GLOBAL_Q
.endif
.if GLOBAL_Q == 10
.def __IQ10isqrt
__IQ10isqrt:
IQNisqrt GLOBAL_Q
.endif
.if GLOBAL_Q == 9
.def __IQ9isqrt
__IQ9isqrt:
IQNisqrt GLOBAL_Q
.endif
.if GLOBAL_Q == 8
.def __IQ8isqrt
__IQ8isqrt:
IQNisqrt GLOBAL_Q
.endif
.if GLOBAL_Q == 7
.def __IQ7isqrt
__IQ7isqrt:
IQNisqrt GLOBAL_Q
.endif
.if GLOBAL_Q == 6
.def __IQ6isqrt
__IQ6isqrt:
IQNisqrt GLOBAL_Q
.endif
.if GLOBAL_Q == 5
.def __IQ5isqrt
__IQ5isqrt:
IQNisqrt GLOBAL_Q
.endif
.if GLOBAL_Q == 4
.def __IQ4isqrt
__IQ4isqrt:
IQNisqrt GLOBAL_Q
.endif
.if GLOBAL_Q == 3
.def __IQ3isqrt
__IQ3isqrt:
IQNisqrt GLOBAL_Q
.endif
.if GLOBAL_Q == 2
.def __IQ2isqrt
__IQ2isqrt:
IQNisqrt GLOBAL_Q
.endif
.if GLOBAL_Q == 1
.def __IQ1isqrt
__IQ1isqrt:
IQNisqrt GLOBAL_Q
.endif
;;###########################################################################
;; No More.
;;###########################################################################
@@ -0,0 +1,466 @@
;;###########################################################################
;;
;; FILE: IQNlog.asm
;;
;; TITLE: IQ Natural Logarithmic Math Function
;;
;;###########################################################################
;;
;; Ver | dd-mmm-yyyy | Who | Description of changes
;; =====|=============|=======|==============================================
;; 1.0 | 2 Aug 2010 | A. T. | Original Release.
;; -----|-------------|-------|----------------------------------------------
;;
;;###########################################################################
;;
;; On Entry: ACC = X in IQ format
;;
;; On Exit: ACC = log(X) result in IQ format
;;
;; Regs Used: ACC, P, XT
;; XAR2, XAR3, XAR4, XAR5, XAR6, XAR7
;;
;; Q range: 29 to 1
;;
;;---------------------------------------------------------------------------
;; Algorithm: The procedure for calculating "Y = log(X)" is as follows:
;;
;; Step 1) Check to see if Input is less than zero..
;;
;; Return = most negative number possible for given Q value.
;;
;; Step 2) Normalize Input:
;;
;; X' = X * 2^n 0.5 <= X' < 1.0
;;
;; Step 3) Obtain Y' = log(X') using taylor series expansion:
;;
;; y = (X'-1)/(X'+1) -0.3333.. <= y < -1.0
;;
;; Y' = 2y (1 + y^2(1/3 + y^2(1/7 + y^2(1/9 + ........)))
;;
;; Step 4) Denormalize value to get output
;;
;; Result = Y' + n*log(2)
;;
;;--------------------------------------------------------------------------
;; Cycles: ~148 (includes call & return)
;;--------------------------------------------------------------------------
;;#############################################################################
;;!
;;! Copyright: Copyright (C) 2023 Texas Instruments Incorporated -
;;! All rights reserved not granted herein.
;;! Limited License.
;;!
;;! Texas Instruments Incorporated grants a world-wide, royalty-free,
;;! non-exclusive license under copyrights and patents it now or hereafter
;;! owns or controls to make, have made, use, import, offer to sell and sell
;;! ("Utilize") this software subject to the terms herein. With respect to the
;;! foregoing patent license, such license is granted solely to the extent that
;;! any such patent is necessary to Utilize the software alone. The patent
;;! license shall not apply to any combinations which include this software,
;;! other than combinations with devices manufactured by or for TI
;;! ("TI Devices").
;;! No hardware patent is licensed hereunder.
;;!
;;! Redistributions must preserve existing copyright notices and reproduce this
;;! license (including the above copyright notice and the disclaimer and
;;! (if applicable) source code license limitations below) in the documentation
;;! and/or other materials provided with the distribution.
;;!
;;! Redistribution and use in binary form, without modification, are permitted
;;! provided that the following conditions are met:
;;!
;;! * No reverse engineering, decompilation, or disassembly of this software is
;;! permitted with respect to any software provided in binary form.
;;! * Any redistribution and use are licensed by TI for use only
;;! with TI Devices.
;;! * Nothing shall obligate TI to provide you with source code for the
;;! software licensed and provided to you in object code.
;;!
;;! If software source code is provided to you, modification and redistribution
;;! of the source code are permitted provided that the following conditions
;;! are met:
;;!
;;! * any redistribution and use of the source code, including any resulting
;;! derivative works, are licensed by TI for use only with TI Devices.
;;! * any redistribution and use of any object code compiled from the source
;;! code and any resulting derivative works, are licensed by TI for use
;;! only with TI Devices.
;;!
;;! Neither the name of Texas Instruments Incorporated nor the names of its
;;! suppliers may be used to endorse or promote products derived from this
;;! software without specific prior written permission.
;;#############################################################################
IQNlog .macro q_value
PUSH XAR2
PUSH XAR3
MOVL XAR6,@ACC ; XAR6 = X (QG)
;;-------------------------------------------------------------------------------
;; checking to see if input is < 0
;;-------------------------------------------------------------------------------
MOVL ACC,@ACC ; Set Z, N flags on ACC content
BF $90,LEQ ; Exit if X <= 0
;;-------------------------------------------------------------------------------
;; Step 1: Normalize Input:
;;-------------------------------------------------------------------------------
CSB ACC ; TH = normalization shift
MOV TL,#0 ; TL = 0
MOVL XAR3,@XT ; XAR3 = save shift value for de-norm operation.
LSLL ACC,T ; ACC = Xnorm (0.5 to 0.99999 range in Q31)
LSR64 ACC:P,#1 ; ACC = Xnorm (Q30)
;;---------------------------------------------------------------------------------
;; y = (Xnorm - 1)/(Xnorm + 1)
;;---------------------------------------------------------------------------------
MOV @PL,#0x0000 ; P = 1.0 (Q30)
MOV @PH,#0x4000
MOVL @XAR7,ACC ; XAR7 = Xnorm
ADDL ACC,@P ; ACC = Xnorm + 1.0 (Q30)
MOVL @XAR6,ACC ; XAR6 = Xnorm + 1.0 (Q30)
MOVL ACC,@XAR7 ; ACC = Xnorm
SUBL ACC,@P ; ACC = Xnorm - 1.0 (Q30)
;;---------------------------------------------------------------------------------
;; Perform Q30 Division
;;---------------------------------------------------------------------------------
ABS ACC ; ACC = Num = abs(Xnorm - 1.0)
MOVL P,@ACC ; P = abs(Num)
MOVB ACC,#0 ; ACC:P = abs(0:Num)
LSL64 ACC:P,#16
LSL64 ACC:P,#14
RPT #31
||SUBCUL ACC,@XAR6 ; Den = XAR6 = Xnorm + 1
MOVL ACC,@P ; ACC = abs(Result) = y (Q30)
NEG ACC ; Negate result (Q30)
MOVL XT,@ACC ; XT = y (Q30)
ADDL ACC,@ACC ; ACC = 2*y (Q30)
MOVL XAR2,@ACC ; XAR2 = 2*y (Q30)
LSL64 ACC:P,#1 ; ACC = 2*y (Q31)
MOVL XAR4,@ACC ; XAR4 = 2*y (Q31)
;;--------------------------------------------------------------------------------
;; Step 3) Obtain Y' = log(y) using taylor series expansion:
;; Y' = 2y (1 + y^2(1/3 + y^2(1/5 + y^2(1/7 + ........)))
;;--------------------------------------------------------------------------------
IMPYL P,XT,@XT
QMPYL ACC,XT,@XT
LSL64 ACC:P,#4 ; ACC = y^2 (Q32 = Q30 + Q30 + Q4 - 32)
MOVL XAR5,@ACC ; XAR5 = y^2 (Q32)
MOV @AL,#0x6186
MOV @AH,#0x0618 ; ACC = 1/21 (Q31)
MOVL XT,@ACC ; XT = 1/21 (Q31)
QMPYL P,XT,@XAR5 ; P = y^2*1/21 (Q31 = Q31 + Q32 - 32)
MOV @AL,#0xA1AF
MOV @AH,#0x06BC ; ACC = 1/19 (Q31)
ADDL ACC,@P ; ACC = 1/19 + Y^2*1/21 (Q31)
MOVL XT,@ACC
QMPYL P,XT,@XAR5 ; P = y^2(1/19 + Y^2*1/21)
MOV @AL,#0x8788
MOV @AH,#0x0787 ; ACC = 1/17 (Q31)
ADDL ACC,@P ; ACC = 1/17 + y^2(1/19 + Y^2*1/21)
MOVL XT,@ACC
QMPYL P,XT,@XAR5 ; P = y^2(17 + y^2(1/19 + Y^2*1/21))
MOV @AL,#0x8889
MOV @AH,#0x0888 ; ACC = 1/15 (Q31)
ADDL ACC,@P ; ACC = 1/15 + y^2(17 + y^2(1/19 + Y^2*1/21))
MOVL XT,@ACC
QMPYL P,XT,@XAR5 ; P = y^2(1/15 + y^2(17 + y^2(1/19 + Y^2*1/21)))
MOV @AL,#0x9D8A
MOV @AH,#0x09D8 ; ACC = 1/13 (Q31)
ADDL ACC,@P ; ACC = 1/13 + y^2(1/15 + y^2(17 + y^2(1/19 + Y^2*1/21)))
MOVL XT,@ACC
QMPYL P,XT,@XAR5 ; P = y^2(1/13 + y^2(1/15 + y^2(17 + y^2(1/19 + Y^2*1/21))))
MOV @AL,#0xE8BA
MOV @AH,#0x0BA2 ; ACC = 1/11 (Q31)
ADDL ACC,@P ; ACC = 1/11 + y^2(1/13 + y^2(1/15 + y^2(17 + y^2(1/19 + Y^2*1/21))))
MOVL XT,@ACC
QMPYL P,XT,@XAR5 ; P = y^2(1/11 + y^2(1/13 + y^2(1/15 + y^2(17 + y^2(1/19 + Y^2*1/21)))))
MOV @AL,#0xE38E
MOV @AH,#0x0E38 ; ACC = 1/9 (Q31)
ADDL ACC,@P ; ACC = 1/9 + y^2(1/11 + y^2(1/13 + y^2(1/15 + y^2(17 + y^2(1/19 + Y^2*1/21)))))
MOVL XT,@ACC
QMPYL P,XT,@XAR5 ; P = y^2(1/9 + y^2(1/11 + y^2(1/13 + y^2(1/15 + y^2(17 + y^2(1/19 + Y^2*1/21))))))
MOV @AL,#0x2492
MOV @AH,#0x1249 ; ACC = 1/7 (Q31)
ADDL ACC,@P ; ACC = 1/7 + y^2(1/9 + y^2(1/11 + y^2(1/13 + y^2(1/15 + y^2(17 + y^2(1/19 + Y^2*1/21))))))
MOVL XT,@ACC
QMPYL P,XT,@XAR5 ; P = y^2(1/7 + y^2(1/9 + y^2(1/11 + y^2(1/13 + y^2(1/15 + y^2(17 + y^2(1/19 + Y^2*1/21)))))))
MOV @AL,#0x999A
MOV @AH,#0x1999 ; ACC = 1/5 (Q31)
ADDL ACC,@P ; ACC = 1/5 + y^2(1/7 + y^2(1/9 + y^2(1/11 + y^2(1/13 + y^2(1/15 + y^2(17 + y^2(1/19 + Y^2*1/21)))))))
MOVL XT,@ACC
QMPYL P,XT,@XAR5 ; P = y^2(1/5 + y^2(1/7 + y^2(1/9 + y^2(1/11 + y^2(1/13 + y^2(1/15 + y^2(17 + y^2(1/19 + Y^2*1/21))))))))
MOV @AL,#0xAAAB
MOV @AH,#0x2AAA ; ACC = 1/3 (Q31)
ADDL ACC,@P ; ACC = 1/3 + y^2(1/5 + y^2(1/7 + y^2(1/9 + y^2(1/11 + y^2(1/13 + y^2(1/15 + y^2(17 + y^2(1/19 + Y^2*1/21))))))))
MOVL XT,@ACC
QMPYL P,XT,@XAR5 ; P = y^2(1/3 + y^2(1/5 + y^2(1/7 + y^2(1/9 + y^2(1/11 + y^2(1/13 + y^2(1/15 + y^2(17 + y^2(1/19 + Y^2*1/21)))))))))
MOVL XT,@XAR4 ; XT = 2*y (Q31)
QMPYL P,XT,@P ; P = 2*y(y^2(1/3 + y^2(1/5 + y^2(1/7 + y^2(1/9 + y^2(1/11 + y^2(1/13 + y^2(1/15 + y^2(17 + y^2(1/19 + Y^2*1/21))))))))))
; P = (Q30 = Q31 + Q31 - 32)
MOVL ACC,@XAR2 ; ACC = 2*y (Q30)
ADDL ACC,@P ; ACC = 2*y + 2*y(y^2(1/3 + y^2(1/5 + y^2(1/7 + y^2(1/11 + y^2(1/13 + y^2(1/15 + y^2(17 + y^2(1/19 + Y^2*1/21)))))))))
; ACC = Y' (Q30)
MPYB P,T,#0 ; P = 0
.if q_value > 13
ASR64 ACC:P,#(30 - q_value) ; ACC:P = Y' (q_value)
.else
ASR64 ACC:P,#16
ASR64 ACC:P,#(14 - q_value)
.endif
MOVL XAR6,@P
MOVL XAR7,ACC ; temp save Y'
;;---------------------------------------------------------------------------------
;; Step 4) Denormalize value to get output
;;---------------------------------------------------------------------------------
MOV @AL,#0x0000
MOV @AH,#(31 - q_value) ; ACC = (31 - q_value) << 16
SUBL ACC,@XAR3 ; ACC = ((31 - q-value) - norm shift) << 16
ASR64 ACC:P,#16 ; ACC = (31 - q-value) - norm shift
MOVL XT,@ACC ; XT = (31 - q-value) - norm shift
MOV @AL,#0x0BFC
MOV @AH,#0x58B9 ; ACC = ln(2) (Q31)
IMPYL P,XT,@ACC
QMPYL ACC,XT,@ACC ; ACC:P = ((31 - q-value) - norm shift) * ln(2) (Q31)
.if q_value > 15
LSL64 ACC:P,#16
LSL64 ACC:P,#(q_value - 15)
.else
LSL64 ACC:P,#(q_value + 1)
.endif ; ACC:P = ((31 - q-value) - norm shift) * ln(2) (q_value)
ADDUL P,@XAR6
ADDCL ACC,@XAR7 ; ACC:P = Y' + ((31 - q-value) - norm shift) * ln(2) (q_value)
POP XAR3
POP XAR2
LRETR
; Exit for X <= 0
$90:
MOV @AL,#0x0000 ; Return most negative value.
MOV @AH,#0x8000
POP XAR3
POP XAR2
LRETR
.endm
;; The value "GLOBAL_Q" needs to be supplied by the assembler
;; using the "-dGLOBAL_Q=q_value" directive:
.sect "IQmath"
.if GLOBAL_Q == 30
.def __IQ30log
__IQ30log:
IQNlog GLOBAL_Q
.endif
.if GLOBAL_Q == 29
.def __IQ29log
__IQ29log:
IQNlog GLOBAL_Q
.endif
.if GLOBAL_Q == 28
.def __IQ28log
__IQ28log:
IQNlog GLOBAL_Q
.endif
.if GLOBAL_Q == 27
.def __IQ27log
__IQ27log:
IQNlog GLOBAL_Q
.endif
.if GLOBAL_Q == 26
.def __IQ26log
__IQ26log:
IQNlog GLOBAL_Q
.endif
.if GLOBAL_Q == 25
.def __IQ25log
__IQ25log:
IQNlog GLOBAL_Q
.endif
.if GLOBAL_Q == 24
.def __IQ24log
__IQ24log:
IQNlog GLOBAL_Q
.endif
.if GLOBAL_Q == 23
.def __IQ23log
__IQ23log:
IQNlog GLOBAL_Q
.endif
.if GLOBAL_Q == 22
.def __IQ22log
__IQ22log:
IQNlog GLOBAL_Q
.endif
.if GLOBAL_Q == 21
.def __IQ21log
__IQ21log:
IQNlog GLOBAL_Q
.endif
.if GLOBAL_Q == 20
.def __IQ20log
__IQ20log:
IQNlog GLOBAL_Q
.endif
.if GLOBAL_Q == 19
.def __IQ19log
__IQ19log:
IQNlog GLOBAL_Q
.endif
.if GLOBAL_Q == 18
.def __IQ18log
__IQ18log:
IQNlog GLOBAL_Q
.endif
.if GLOBAL_Q == 17
.def __IQ17log
__IQ17log:
IQNlog GLOBAL_Q
.endif
.if GLOBAL_Q == 16
.def __IQ16log
__IQ16log:
IQNlog GLOBAL_Q
.endif
.if GLOBAL_Q == 15
.def __IQ15log
__IQ15log:
IQNlog GLOBAL_Q
.endif
.if GLOBAL_Q == 14
.def __IQ14log
__IQ14log:
IQNlog GLOBAL_Q
.endif
.if GLOBAL_Q == 13
.def __IQ13log
__IQ13log:
IQNlog GLOBAL_Q
.endif
.if GLOBAL_Q == 12
.def __IQ12log
__IQ12log:
IQNlog GLOBAL_Q
.endif
.if GLOBAL_Q == 11
.def __IQ11log
__IQ11log:
IQNlog GLOBAL_Q
.endif
.if GLOBAL_Q == 10
.def __IQ10log
__IQ10log:
IQNlog GLOBAL_Q
.endif
.if GLOBAL_Q == 9
.def __IQ9log
__IQ9log:
IQNlog GLOBAL_Q
.endif
.if GLOBAL_Q == 8
.def __IQ8log
__IQ8log:
IQNlog GLOBAL_Q
.endif
.if GLOBAL_Q == 7
.def __IQ7log
__IQ7log:
IQNlog GLOBAL_Q
.endif
.if GLOBAL_Q == 6
.def __IQ6log
__IQ6log:
IQNlog GLOBAL_Q
.endif
.if GLOBAL_Q == 5
.def __IQ5log
__IQ5log:
IQNlog GLOBAL_Q
.endif
.if GLOBAL_Q == 4
.def __IQ4log
__IQ4log:
IQNlog GLOBAL_Q
.endif
.if GLOBAL_Q == 3
.def __IQ3log
__IQ3log:
IQNlog GLOBAL_Q
.endif
.if GLOBAL_Q == 2
.def __IQ2log
__IQ2log:
IQNlog GLOBAL_Q
.endif
.if GLOBAL_Q == 1
.def __IQ1log
__IQ1log:
IQNlog GLOBAL_Q
.endif
;;###########################################################################
;; No More.
;;###########################################################################
@@ -0,0 +1,403 @@
;;###########################################################################
;;
;; FILE: IQNmag.asm
;;
;; TITLE: C Callable IQ Magnitude Math Function
;;
;;###########################################################################
;;
;; Ver | Date | Who | Description of changes
;; =====|=============|=======|==============================================
;; 1.4 | 17 May 2002 | A. T. | Original Release.
;; -----|-------------|-------|----------------------------------------------
;; | | |
;;
;;###########################################################################
;;===========================================================================
;; Function: _IQNmag
;;===========================================================================
;;
;; C Usage: extern long _IQNmag(long A, long B); // with rounding & sat
;;
;;---------------------------------------------------------------------------
;;
;; On Entry: ACC = A in IQ format
;; SP-4 = B in IQ format
;;
;; Regs Used: XAR7, XAR6, XAR5, XAR4
;; XT, P, XAR0
;;
;; On Exit: ACC = sqrt(A*A + B*B) result in IQ format
;;
;; Q range: 30 to 1
;;
;;---------------------------------------------------------------------------
;; Algorithm: To calculate the magnitute we basically use the sqrt function.
;; The procedure for calculating "Y = sqrt(X)" is similar
;; to calculating the inverse sqare root due to the
;; following relationship:
;;
;; sqrt(X) = X * 1/sqrt(X)
;;
;; The procedure is as follows:
;;
;; Step 1) Calculate value to square root:
;;
;; X = (A*A + B*B)
;;
;; Step 2) Normalize Input:
;;
;; V = X * 2^n
;;
;; Step 3) Obtain initial estimate from "_IQsqrtTable"
;; using the upper 8-bits of the normalized V value.
;;
;; Step 4) Use Newton-Raphson algorithm to improve accuracy.
;; Repeat following equation two times. First iteration
;; gives 16-bit accuracy. Second iteration gives 32-bit
;; accuracy:
;;
;; Yn = Yn(1.5 - Yn*Yn*V/2)
;;
;; Yn = 1/sqrt(V)
;;
;; Step 5) Caculate the square root of the normalized number:
;;
;; Yn = V * Yn
;;
;; Step 6) Denormalize result and round:
;;
;; Y = Yn * 2^n / sqrt(2^n)
;;
;;---------------------------------------------------------------------------
;; Benchmark:
;;
;; Assumptions:
;; * Code executes from 0-wait SARAM block.
;; * _IQsqrt Tables located in seperate 0-wait SARAM or 1-wait ROM block.
;; * Stack located in seperate 0-wait SARAM block.
;; * Includes LCR/LRETR.
;;
;; Cycles = 89 (if _IQsqrt Tables in 0-wait SARAM)
;; = 93 (if _IQsqrt Tables in 1-wait ROM)
;;
;;===========================================================================
;;#############################################################################
;;!
;;! Copyright: Copyright (C) 2023 Texas Instruments Incorporated -
;;! All rights reserved not granted herein.
;;! Limited License.
;;!
;;! Texas Instruments Incorporated grants a world-wide, royalty-free,
;;! non-exclusive license under copyrights and patents it now or hereafter
;;! owns or controls to make, have made, use, import, offer to sell and sell
;;! ("Utilize") this software subject to the terms herein. With respect to the
;;! foregoing patent license, such license is granted solely to the extent that
;;! any such patent is necessary to Utilize the software alone. The patent
;;! license shall not apply to any combinations which include this software,
;;! other than combinations with devices manufactured by or for TI
;;! ("TI Devices").
;;! No hardware patent is licensed hereunder.
;;!
;;! Redistributions must preserve existing copyright notices and reproduce this
;;! license (including the above copyright notice and the disclaimer and
;;! (if applicable) source code license limitations below) in the documentation
;;! and/or other materials provided with the distribution.
;;!
;;! Redistribution and use in binary form, without modification, are permitted
;;! provided that the following conditions are met:
;;!
;;! * No reverse engineering, decompilation, or disassembly of this software is
;;! permitted with respect to any software provided in binary form.
;;! * Any redistribution and use are licensed by TI for use only
;;! with TI Devices.
;;! * Nothing shall obligate TI to provide you with source code for the
;;! software licensed and provided to you in object code.
;;!
;;! If software source code is provided to you, modification and redistribution
;;! of the source code are permitted provided that the following conditions
;;! are met:
;;!
;;! * any redistribution and use of the source code, including any resulting
;;! derivative works, are licensed by TI for use only with TI Devices.
;;! * any redistribution and use of any object code compiled from the source
;;! code and any resulting derivative works, are licensed by TI for use
;;! only with TI Devices.
;;!
;;! Neither the name of Texas Instruments Incorporated nor the names of its
;;! suppliers may be used to endorse or promote products derived from this
;;! software without specific prior written permission.
;;#############################################################################
IQNmag .macro q_value
MOVL XT,@ACC
IMPYL P,XT,@ACC
QMPYL ACC,XT,@ACC ; ACC:P = A*A
MOVL XAR6,@P
MOVL XAR7,@ACC ; XAR7:XAR6 = B*B
MOVL XT,*-SP[4]
IMPYL P,XT,*-SP[4]
QMPYL ACC,XT,*-SP[4] ; ACC:P = B*B
ADDUL P,@XAR6
ZAP OVC
ADDCL ACC,@XAR7 ; ACC:P = A*A + B*B (2*q_value)
SAT64 ACC:P ; ACC:P = saturated(A*A + B*B) = X
CSB ACC
LSL64 ACC:P,T
MOV @AR7,T
CSB ACC
LSL64 ACC:P,T ; ACC:P = V/2 (Q32)
MOVL XAR6,@ACC ; XAR6 = V/2 (Q32)
ASR AH,#6
MOVB @AH,#0xFE,LEQ ; AR0 -> zero value if -ve or 0 input
SUB @AH,#254 ; Scale to table offset
MOVZ AR0,@AH ; AR0 -> index into _IQisqrtTable for
; initial estimate of Yn
MOV AH,@AR7
ADD AH,@T ; AH = n
TBIT @AH,#0 ; TC = odd/even shift indicator
LSR AH,#1 ; AH = n/2
MOVL *SP++,ACC ; SP-2 = n/2
MOVL XAR7,#_IQsqrtTable ; XAR7 = pointer to _IQisqrtTable
MOVL XAR4,*+XAR7[AR0] ; (1) XAR4 = initial Yn estimate (Q30)
MOVL XAR7,#_IQsqrtRoundSatTable
MOVL XAR5,*XAR7++ ; XAR5 = 1.5 (Q30)
MOVB @AR0,#12,NTC ; AR0 -> 1/sqrt(2) if n == even (Q30)
MOVB @AR0,#10,TC ; AR0 -> 0.5 if n == odd (Q30)
MOVL XT,@XAR4 ;(2) XT = Yn (Q30)
QMPYL ACC,XT,@XT ;(3) ACC = Yn*Yn (Q28)
MOVL XT,@XAR6 ;(4) XT = V/2 (Q32)
LSL ACC,#2 ;(5) ACC = Yn*Yn (Q30)
QMPYL ACC,XT,@ACC ;(6) ACC = Yn*Yn*V/2 (Q30)
MOVL XT,@XAR5 ;(7) XT = 1.5 (Q30)
SUBL @XT,ACC ;(8) XT = 1.5 - Yn*Yn*V/2 (Q30)
QMPYL ACC,XT,@XAR4 ;(9) ACC = Yn(1.5 - Yn*Yn*V/2) (Q28)
LSL ACC,#2 ;(10) ACC = Yn(1.5 - Yn*Yn*V/2) (Q30)
MOVL XAR4,@ACC ;(1) XAR4 = Yn (Q30)
MOVL XT,@XAR4 ;(2) XT = Yn (Q30)
QMPYL ACC,XT,@XT ;(3) ACC = Yn*Yn (Q28)
MOVL XT,@XAR6 ;(4) XT = V/2 (Q32)
LSL ACC,#2 ;(5) ACC = Yn*Yn (Q30)
QMPYL ACC,XT,@ACC ;(6) ACC = Yn*Yn*V/2 (Q30)
MOVL XT,@XAR5 ;(7) XT = 1.5 (Q30)
SUBL @XT,ACC ;(8) XT = 1.5 - Yn*Yn*V/2 (Q30)
QMPYL ACC,XT,@XAR4 ;(9) ACC = Yn(1.5 - Yn*Yn*V/2) (Q28)
LSL ACC,#2 ;(10) ACC = Yn(1.5 - Yn*Yn*V/2) (Q30)
MOVL XT,@XAR6
QMPYL ACC,XT,@ACC ; sqrt(X) = X * 1/sqrt(X)
MOVL XT,*+XAR7[AR0] ; Scale with 1.0 or 1/sqrt(X) or 0.5
IMPYL P,XT,@ACC
QMPYL ACC,XT,@ACC
MOVL XT,*--SP ; T = n/2
ASR64 ACC:P,T ; Scale final result based on n/2
LSL64 ACC:P,#4 ; Scale final result based on Q value
ADDUL P,@P
ZAP OVC
ADDCL ACC,@ACC
SAT64 ACC:P
ADD @PH,#-32768 ; Round final result (ACC:P += 0x0000000080000000)
ZAP OVC ; Zero overflow counter
ADDCL ACC,*+XAR7[2]
SAT64 ACC:P ; Satuate if overflow
LRETR
.endm
;; The value "GLOBAL_Q" needs to be supplied by the assembler
;; using the "-dGLOBAL_Q=q_value" directive:
.sect "IQmath"
.ref _IQsqrtTable
.ref _IQsqrtRoundSatTable
.if GLOBAL_Q == 30
.def __IQ30mag
__IQ30mag:
IQNmag GLOBAL_Q
.endif
.if GLOBAL_Q == 29
.def __IQ29mag
__IQ29mag:
IQNmag GLOBAL_Q
.endif
.if GLOBAL_Q == 28
.def __IQ28mag
__IQ28mag:
IQNmag GLOBAL_Q
.endif
.if GLOBAL_Q == 27
.def __IQ27mag
__IQ27mag:
IQNmag GLOBAL_Q
.endif
.if GLOBAL_Q == 26
.def __IQ26mag
__IQ26mag:
IQNmag GLOBAL_Q
.endif
.if GLOBAL_Q == 25
.def __IQ25mag
__IQ25mag:
IQNmag GLOBAL_Q
.endif
.if GLOBAL_Q == 24
.def __IQ24mag
__IQ24mag:
IQNmag GLOBAL_Q
.endif
.if GLOBAL_Q == 23
.def __IQ23mag
__IQ23mag:
IQNmag GLOBAL_Q
.endif
.if GLOBAL_Q == 22
.def __IQ22mag
__IQ22mag:
IQNmag GLOBAL_Q
.endif
.if GLOBAL_Q == 21
.def __IQ21mag
__IQ21mag:
IQNmag GLOBAL_Q
.endif
.if GLOBAL_Q == 20
.def __IQ20mag
__IQ20mag:
IQNmag GLOBAL_Q
.endif
.if GLOBAL_Q == 19
.def __IQ19mag
__IQ19mag:
IQNmag GLOBAL_Q
.endif
.if GLOBAL_Q == 18
.def __IQ18mag
__IQ18mag:
IQNmag GLOBAL_Q
.endif
.if GLOBAL_Q == 17
.def __IQ17mag
__IQ17mag:
IQNmag GLOBAL_Q
.endif
.if GLOBAL_Q == 16
.def __IQ16mag
__IQ16mag:
IQNmag GLOBAL_Q
.endif
.if GLOBAL_Q == 15
.def __IQ15mag
__IQ15mag:
IQNmag GLOBAL_Q
.endif
.if GLOBAL_Q == 14
.def __IQ14mag
__IQ14mag:
IQNmag GLOBAL_Q
.endif
.if GLOBAL_Q == 13
.def __IQ13mag
__IQ13mag:
IQNmag GLOBAL_Q
.endif
.if GLOBAL_Q == 12
.def __IQ12mag
__IQ12mag:
IQNmag GLOBAL_Q
.endif
.if GLOBAL_Q == 11
.def __IQ11mag
__IQ11mag:
IQNmag GLOBAL_Q
.endif
.if GLOBAL_Q == 10
.def __IQ10mag
__IQ10mag:
IQNmag GLOBAL_Q
.endif
.if GLOBAL_Q == 9
.def __IQ9mag
__IQ9mag:
IQNmag GLOBAL_Q
.endif
.if GLOBAL_Q == 8
.def __IQ8mag
__IQ8mag:
IQNmag GLOBAL_Q
.endif
.if GLOBAL_Q == 7
.def __IQ7mag
__IQ7mag:
IQNmag GLOBAL_Q
.endif
.if GLOBAL_Q == 6
.def __IQ6mag
__IQ6mag:
IQNmag GLOBAL_Q
.endif
.if GLOBAL_Q == 5
.def __IQ5mag
__IQ5mag:
IQNmag GLOBAL_Q
.endif
.if GLOBAL_Q == 4
.def __IQ4mag
__IQ4mag:
IQNmag GLOBAL_Q
.endif
.if GLOBAL_Q == 3
.def __IQ3mag
__IQ3mag:
IQNmag GLOBAL_Q
.endif
.if GLOBAL_Q == 2
.def __IQ2mag
__IQ2mag:
IQNmag GLOBAL_Q
.endif
.if GLOBAL_Q == 1
.def __IQ1mag
__IQ1mag:
IQNmag GLOBAL_Q
.endif
;;###########################################################################
;; No More.
;;###########################################################################
@@ -0,0 +1,311 @@
;;###########################################################################
;;
;; FILE: IQNmpy.asm
;;
;; TITLE: C Callable IQ Multiply Math Function
;;
;;###########################################################################
;;
;; Ver | Date | Who | Description of changes
;; =====|=============|=======|==============================================
;; 1.2 | 11 Oct 2001 | A. T. | Original Release.
;; -----|-------------|-------|----------------------------------------------
;; 1.3 | 19 Nov 2001 | A. T. | No changes.
;; -----|-------------|-------|----------------------------------------------
;; 1.4 | 4 May 2002 | A. T. | No changes.
;; | | |
;; | | | Note: In CCS 2.01 and above, the codegen
;; | | | tools support an __IQmpy(..) intrinsic
;; | | | which replaces the IQNmpy(..) function.
;; -----|-------------|-------|----------------------------------------------
;; | | |
;;
;;###########################################################################
;;===========================================================================
;; Function: _IQNmpy
;;===========================================================================
;;
;; C Usage: extern long _IQmpy(long M, long X); // no round or sat
;;
;;---------------------------------------------------------------------------
;;
;; On Entry: ACC = M in IQ format
;; [SP-4] = X in IQ format
;;
;; Regs Used: XT, P
;;
;; On Exit: ACC = M * X in IQ format
;;
;; Q range: 30 to 1
;;
;;---------------------------------------------------------------------------
;; Algorithm: The IQmpy operation generates a 64-bit result. The 64-bit
;; number must then be scaled back to a 32-bit value with the
;; same Q value as the original inputs. For example: if Q = 27,
;; then the multiplication of two I5Q27 numbers results in the
;; following 64-bit value:
;;
;; I5Q27 * I5Q27 = I10Q54
;;
;; The value is then scaled back to an I5Q27 value as follows:
;;
;; I5Q27 = I10Q54 >> Q
;;
;; For some of the operations, the value is either rounded or
;; saturated (or both) to its maximum value before scaling.
;;
;;---------------------------------------------------------------------------
;; Benchmark:
;;
;; Cycles = 13 (Q >= 16, includes LCR/LRETR)
;; 14 (Q <= 15, includes LCR/LRETR)
;;
;;---------------------------------------------------------------------------
;;#############################################################################
;;!
;;! Copyright: Copyright (C) 2023 Texas Instruments Incorporated -
;;! All rights reserved not granted herein.
;;! Limited License.
;;!
;;! Texas Instruments Incorporated grants a world-wide, royalty-free,
;;! non-exclusive license under copyrights and patents it now or hereafter
;;! owns or controls to make, have made, use, import, offer to sell and sell
;;! ("Utilize") this software subject to the terms herein. With respect to the
;;! foregoing patent license, such license is granted solely to the extent that
;;! any such patent is necessary to Utilize the software alone. The patent
;;! license shall not apply to any combinations which include this software,
;;! other than combinations with devices manufactured by or for TI
;;! ("TI Devices").
;;! No hardware patent is licensed hereunder.
;;!
;;! Redistributions must preserve existing copyright notices and reproduce this
;;! license (including the above copyright notice and the disclaimer and
;;! (if applicable) source code license limitations below) in the documentation
;;! and/or other materials provided with the distribution.
;;!
;;! Redistribution and use in binary form, without modification, are permitted
;;! provided that the following conditions are met:
;;!
;;! * No reverse engineering, decompilation, or disassembly of this software is
;;! permitted with respect to any software provided in binary form.
;;! * Any redistribution and use are licensed by TI for use only
;;! with TI Devices.
;;! * Nothing shall obligate TI to provide you with source code for the
;;! software licensed and provided to you in object code.
;;!
;;! If software source code is provided to you, modification and redistribution
;;! of the source code are permitted provided that the following conditions
;;! are met:
;;!
;;! * any redistribution and use of the source code, including any resulting
;;! derivative works, are licensed by TI for use only with TI Devices.
;;! * any redistribution and use of any object code compiled from the source
;;! code and any resulting derivative works, are licensed by TI for use
;;! only with TI Devices.
;;!
;;! Neither the name of Texas Instruments Incorporated nor the names of its
;;! suppliers may be used to endorse or promote products derived from this
;;! software without specific prior written permission.
;;#############################################################################
IQNmpy .macro q_value
MOVL XT,@ACC ; XT = M
IMPYL P,XT,*-SP[4]
QMPYL ACC,XT,*-SP[4] ; ACC:P = M * X
.if q_value >= 16
LSL64 ACC:P,#(32 - q_value) ; Scale back to required IQ value
.endif
.if q_value <= 15
LSL64 ACC:P,#16 ; Scale back to required IQ value
LSL64 ACC:P,#(16 - q_value)
.endif
LRETR
.endm
;; The value "GLOBAL_Q" needs to be supplied by the assembler
;; using the "-dGLOBAL_Q=q_value" directive:
.sect "IQmath"
.if GLOBAL_Q == 30
.def __IQ30mpy
__IQ30mpy:
IQNmpy GLOBAL_Q
.endif
.if GLOBAL_Q == 29
.def __IQ29mpy
__IQ29mpy:
IQNmpy GLOBAL_Q
.endif
.if GLOBAL_Q == 28
.def __IQ28mpy
__IQ28mpy:
IQNmpy GLOBAL_Q
.endif
.if GLOBAL_Q == 27
.def __IQ27mpy
__IQ27mpy:
IQNmpy GLOBAL_Q
.endif
.if GLOBAL_Q == 26
.def __IQ26mpy
__IQ26mpy:
IQNmpy GLOBAL_Q
.endif
.if GLOBAL_Q == 25
.def __IQ25mpy
__IQ25mpy:
IQNmpy GLOBAL_Q
.endif
.if GLOBAL_Q == 24
.def __IQ24mpy
__IQ24mpy:
IQNmpy GLOBAL_Q
.endif
.if GLOBAL_Q == 23
.def __IQ23mpy
__IQ23mpy:
IQNmpy GLOBAL_Q
.endif
.if GLOBAL_Q == 22
.def __IQ22mpy
__IQ22mpy:
IQNmpy GLOBAL_Q
.endif
.if GLOBAL_Q == 21
.def __IQ21mpy
__IQ21mpy:
IQNmpy GLOBAL_Q
.endif
.if GLOBAL_Q == 20
.def __IQ20mpy
__IQ20mpy:
IQNmpy GLOBAL_Q
.endif
.if GLOBAL_Q == 19
.def __IQ19mpy
__IQ19mpy:
IQNmpy GLOBAL_Q
.endif
.if GLOBAL_Q == 18
.def __IQ18mpy
__IQ18mpy:
IQNmpy GLOBAL_Q
.endif
.if GLOBAL_Q == 17
.def __IQ17mpy
__IQ17mpy:
IQNmpy GLOBAL_Q
.endif
.if GLOBAL_Q == 16
.def __IQ16mpy
__IQ16mpy:
IQNmpy GLOBAL_Q
.endif
.if GLOBAL_Q == 15
.def __IQ15mpy
__IQ15mpy:
IQNmpy GLOBAL_Q
.endif
.if GLOBAL_Q == 14
.def __IQ14mpy
__IQ14mpy:
IQNmpy GLOBAL_Q
.endif
.if GLOBAL_Q == 13
.def __IQ13mpy
__IQ13mpy:
IQNmpy GLOBAL_Q
.endif
.if GLOBAL_Q == 12
.def __IQ12mpy
__IQ12mpy:
IQNmpy GLOBAL_Q
.endif
.if GLOBAL_Q == 11
.def __IQ11mpy
__IQ11mpy:
IQNmpy GLOBAL_Q
.endif
.if GLOBAL_Q == 10
.def __IQ10mpy
__IQ10mpy:
IQNmpy GLOBAL_Q
.endif
.if GLOBAL_Q == 9
.def __IQ9mpy
__IQ9mpy:
IQNmpy GLOBAL_Q
.endif
.if GLOBAL_Q == 8
.def __IQ8mpy
__IQ8mpy:
IQNmpy GLOBAL_Q
.endif
.if GLOBAL_Q == 7
.def __IQ7mpy
__IQ7mpy:
IQNmpy GLOBAL_Q
.endif
.if GLOBAL_Q == 6
.def __IQ6mpy
__IQ6mpy:
IQNmpy GLOBAL_Q
.endif
.if GLOBAL_Q == 5
.def __IQ5mpy
__IQ5mpy:
IQNmpy GLOBAL_Q
.endif
.if GLOBAL_Q == 4
.def __IQ4mpy
__IQ4mpy:
IQNmpy GLOBAL_Q
.endif
.if GLOBAL_Q == 3
.def __IQ3mpy
__IQ3mpy:
IQNmpy GLOBAL_Q
.endif
.if GLOBAL_Q == 2
.def __IQ2mpy
__IQ2mpy:
IQNmpy GLOBAL_Q
.endif
.if GLOBAL_Q == 1
.def __IQ1mpy
__IQ1mpy:
IQNmpy GLOBAL_Q
.endif
;;###########################################################################
;; No More.
;;###########################################################################
@@ -0,0 +1,307 @@
;;###########################################################################
;;
;; FILE: IQNmpyI32frac.asm
;;
;; TITLE: C Callable IQ Multiply I32 Return Fraction Math Function
;;
;;###########################################################################
;;
;; Ver | Date | Who | Description of changes
;; =====|=============|=======|==============================================
;; 1.4 | 17 May 2002 | A. T. | Original Release.
;; -----|-------------|-------|----------------------------------------------
;; 1.4c | 20 Jun 2002 | A. T. | Fixed problem with function when operating
;; | | | on negative values with a zero fractional
;; | | | part, incorrect result was given.
;; -----|-------------|-------|----------------------------------------------
;; 1.4d | 30 Mar 2002 | SD | Re-implemented the function without branch
;; -----|-------------|-------|----------------------------------------------
;;
;;###########################################################################
;;===========================================================================
;; Function: _IQNmpyI32frac
;;===========================================================================
;;
;; C Usage: extern long _IQmpyI32frac(long A, long B); // no round or sat
;;
;;---------------------------------------------------------------------------
;;
;; On Entry: ACC = A in IQ format
;; SP-4 = B in 32-bit integer (long) format
;;
;; Regs Used: XT, P, XAR6, XAR7
;;
;; On Exit: ACC = fractional portion of A*B stored as IQ
;;
;; Q range: 30 to 1
;;
;;---------------------------------------------------------------------------
;; Algorithm: The fractional portion of the IQ*I32 number is calculated
;; as follows:
;;
;; fraction = A*B - ((A*B >> q_value) << q_value)
;;
;;---------------------------------------------------------------------------
;; Benchmark:
;;
;; Cycles = 34 (worst case, includes LCR/LRETR)
;;
;;---------------------------------------------------------------------------
;;#############################################################################
;;!
;;! Copyright: Copyright (C) 2023 Texas Instruments Incorporated -
;;! All rights reserved not granted herein.
;;! Limited License.
;;!
;;! Texas Instruments Incorporated grants a world-wide, royalty-free,
;;! non-exclusive license under copyrights and patents it now or hereafter
;;! owns or controls to make, have made, use, import, offer to sell and sell
;;! ("Utilize") this software subject to the terms herein. With respect to the
;;! foregoing patent license, such license is granted solely to the extent that
;;! any such patent is necessary to Utilize the software alone. The patent
;;! license shall not apply to any combinations which include this software,
;;! other than combinations with devices manufactured by or for TI
;;! ("TI Devices").
;;! No hardware patent is licensed hereunder.
;;!
;;! Redistributions must preserve existing copyright notices and reproduce this
;;! license (including the above copyright notice and the disclaimer and
;;! (if applicable) source code license limitations below) in the documentation
;;! and/or other materials provided with the distribution.
;;!
;;! Redistribution and use in binary form, without modification, are permitted
;;! provided that the following conditions are met:
;;!
;;! * No reverse engineering, decompilation, or disassembly of this software is
;;! permitted with respect to any software provided in binary form.
;;! * Any redistribution and use are licensed by TI for use only
;;! with TI Devices.
;;! * Nothing shall obligate TI to provide you with source code for the
;;! software licensed and provided to you in object code.
;;!
;;! If software source code is provided to you, modification and redistribution
;;! of the source code are permitted provided that the following conditions
;;! are met:
;;!
;;! * any redistribution and use of the source code, including any resulting
;;! derivative works, are licensed by TI for use only with TI Devices.
;;! * any redistribution and use of any object code compiled from the source
;;! code and any resulting derivative works, are licensed by TI for use
;;! only with TI Devices.
;;!
;;! Neither the name of Texas Instruments Incorporated nor the names of its
;;! suppliers may be used to endorse or promote products derived from this
;;! software without specific prior written permission.
;;#############################################################################
IQNmpyI32frac .macro q_value
SETC OVM
CLRC TC
ABSTC ACC
MOVL XT,ACC
MOVL ACC,*-SP[4]
ABSTC ACC
IMPYL P,XT,@ACC
QMPYL ACC,XT,@ACC ; ACC:P = IQ*I32 (Q=q_value)
MOV @T,#(32-q_value)
LSL64 ACC:P,T ; P=frac bits (aligned to MSB)
MOVB ACC,#0
MOV @T,#(q_value)
LSL64 ACC:P,T ; ACC=frac bits (aligned to LSB)
NEGTC ACC
CLRC OVM ; Clear overflow mode
LRETR
.endm
;; The value "GLOBAL_Q" needs to be supplied by the assembler
;; using the "-dGLOBAL_Q=q_value" directive:
.sect "IQmath"
.if GLOBAL_Q == 30
.def __IQ30mpyI32frac
__IQ30mpyI32frac:
IQNmpyI32frac GLOBAL_Q
.endif
.if GLOBAL_Q == 29
.def __IQ29mpyI32frac
__IQ29mpyI32frac:
IQNmpyI32frac GLOBAL_Q
.endif
.if GLOBAL_Q == 28
.def __IQ28mpyI32frac
__IQ28mpyI32frac:
IQNmpyI32frac GLOBAL_Q
.endif
.if GLOBAL_Q == 27
.def __IQ27mpyI32frac
__IQ27mpyI32frac:
IQNmpyI32frac GLOBAL_Q
.endif
.if GLOBAL_Q == 26
.def __IQ26mpyI32frac
__IQ26mpyI32frac:
IQNmpyI32frac GLOBAL_Q
.endif
.if GLOBAL_Q == 25
.def __IQ25mpyI32frac
__IQ25mpyI32frac:
IQNmpyI32frac GLOBAL_Q
.endif
.if GLOBAL_Q == 24
.def __IQ24mpyI32frac
__IQ24mpyI32frac:
IQNmpyI32frac GLOBAL_Q
.endif
.if GLOBAL_Q == 23
.def __IQ23mpyI32frac
__IQ23mpyI32frac:
IQNmpyI32frac GLOBAL_Q
.endif
.if GLOBAL_Q == 22
.def __IQ22mpyI32frac
__IQ22mpyI32frac:
IQNmpyI32frac GLOBAL_Q
.endif
.if GLOBAL_Q == 21
.def __IQ21mpyI32frac
__IQ21mpyI32frac:
IQNmpyI32frac GLOBAL_Q
.endif
.if GLOBAL_Q == 20
.def __IQ20mpyI32frac
__IQ20mpyI32frac:
IQNmpyI32frac GLOBAL_Q
.endif
.if GLOBAL_Q == 19
.def __IQ19mpyI32frac
__IQ19mpyI32frac:
IQNmpyI32frac GLOBAL_Q
.endif
.if GLOBAL_Q == 18
.def __IQ18mpyI32frac
__IQ18mpyI32frac:
IQNmpyI32frac GLOBAL_Q
.endif
.if GLOBAL_Q == 17
.def __IQ17mpyI32frac
__IQ17mpyI32frac:
IQNmpyI32frac GLOBAL_Q
.endif
.if GLOBAL_Q == 16
.def __IQ16mpyI32frac
__IQ16mpyI32frac:
IQNmpyI32frac GLOBAL_Q
.endif
.if GLOBAL_Q == 15
.def __IQ15mpyI32frac
__IQ15mpyI32frac:
IQNmpyI32frac GLOBAL_Q
.endif
.if GLOBAL_Q == 14
.def __IQ14mpyI32frac
__IQ14mpyI32frac:
IQNmpyI32frac GLOBAL_Q
.endif
.if GLOBAL_Q == 13
.def __IQ13mpyI32frac
__IQ13mpyI32frac:
IQNmpyI32frac GLOBAL_Q
.endif
.if GLOBAL_Q == 12
.def __IQ12mpyI32frac
__IQ12mpyI32frac:
IQNmpyI32frac GLOBAL_Q
.endif
.if GLOBAL_Q == 11
.def __IQ11mpyI32frac
__IQ11mpyI32frac:
IQNmpyI32frac GLOBAL_Q
.endif
.if GLOBAL_Q == 10
.def __IQ10mpyI32frac
__IQ10mpyI32frac:
IQNmpyI32frac GLOBAL_Q
.endif
.if GLOBAL_Q == 9
.def __IQ9mpyI32frac
__IQ9mpyI32frac:
IQNmpyI32frac GLOBAL_Q
.endif
.if GLOBAL_Q == 8
.def __IQ8mpyI32frac
__IQ8mpyI32frac:
IQNmpyI32frac GLOBAL_Q
.endif
.if GLOBAL_Q == 7
.def __IQ7mpyI32frac
__IQ7mpyI32frac:
IQNmpyI32frac GLOBAL_Q
.endif
.if GLOBAL_Q == 6
.def __IQ6mpyI32frac
__IQ6mpyI32frac:
IQNmpyI32frac GLOBAL_Q
.endif
.if GLOBAL_Q == 5
.def __IQ5mpyI32frac
__IQ5mpyI32frac:
IQNmpyI32frac GLOBAL_Q
.endif
.if GLOBAL_Q == 4
.def __IQ4mpyI32frac
__IQ4mpyI32frac:
IQNmpyI32frac GLOBAL_Q
.endif
.if GLOBAL_Q == 3
.def __IQ3mpyI32frac
__IQ3mpyI32frac:
IQNmpyI32frac GLOBAL_Q
.endif
.if GLOBAL_Q == 2
.def __IQ2mpyI32frac
__IQ2mpyI32frac:
IQNmpyI32frac GLOBAL_Q
.endif
.if GLOBAL_Q == 1
.def __IQ1mpyI32frac
__IQ1mpyI32frac:
IQNmpyI32frac GLOBAL_Q
.endif
;;###########################################################################
;; No More.
;;###########################################################################
@@ -0,0 +1,305 @@
;;###########################################################################
;;
;; FILE: IQNmpyI32int.asm
;;
;; TITLE: C Callable IQ Multiply I32 Return Integer Math Function
;;
;;###########################################################################
;;
;; Ver | Date | Who | Description of changes
;; =====|=============|=======|==============================================
;; 1.4 | 17 May 2002 | A. T. | Original Release.
;; -----|-------------|-------|----------------------------------------------
;; 1.4c | 20 Jun 2002 | A. T. | Fixed problem with function when operating
;; | | | on negative values with a zero fractional
;; | | | part, incorrect result was given.
;; -----|-------------|-------|----------------------------------------------
;; 1.4d | 30 Mar 2002 | SD | Re-implemented the function without branch
;; -----|-------------|-------|----------------------------------------------
;;
;;###########################################################################
;;===========================================================================
;; Function: _IQNmpyI32int
;;===========================================================================
;;
;; C Usage: extern long _IQmpyI32int(long A, long B); // no round or sat
;;
;;---------------------------------------------------------------------------
;;
;; On Entry: ACC = A in IQ format
;; SP-4 = B in 32-bit integer (long) format
;;
;; Regs Used: XT, P
;;
;; On Exit: ACC = integer portion of A*B stored as "long"
;;
;; Q range: 30 to 1
;;
;;---------------------------------------------------------------------------
;; Algorithm: The integer portion of the IQ*I32 number is calculated
;; as follows:
;;
;; integer = A*B >> q_value;
;;
;;---------------------------------------------------------------------------
;; Benchmark:
;;
;; Cycles = 28 (worst case, includes LCR/LRETR)
;;
;;---------------------------------------------------------------------------
;;#############################################################################
;;!
;;! Copyright: Copyright (C) 2023 Texas Instruments Incorporated -
;;! All rights reserved not granted herein.
;;! Limited License.
;;!
;;! Texas Instruments Incorporated grants a world-wide, royalty-free,
;;! non-exclusive license under copyrights and patents it now or hereafter
;;! owns or controls to make, have made, use, import, offer to sell and sell
;;! ("Utilize") this software subject to the terms herein. With respect to the
;;! foregoing patent license, such license is granted solely to the extent that
;;! any such patent is necessary to Utilize the software alone. The patent
;;! license shall not apply to any combinations which include this software,
;;! other than combinations with devices manufactured by or for TI
;;! ("TI Devices").
;;! No hardware patent is licensed hereunder.
;;!
;;! Redistributions must preserve existing copyright notices and reproduce this
;;! license (including the above copyright notice and the disclaimer and
;;! (if applicable) source code license limitations below) in the documentation
;;! and/or other materials provided with the distribution.
;;!
;;! Redistribution and use in binary form, without modification, are permitted
;;! provided that the following conditions are met:
;;!
;;! * No reverse engineering, decompilation, or disassembly of this software is
;;! permitted with respect to any software provided in binary form.
;;! * Any redistribution and use are licensed by TI for use only
;;! with TI Devices.
;;! * Nothing shall obligate TI to provide you with source code for the
;;! software licensed and provided to you in object code.
;;!
;;! If software source code is provided to you, modification and redistribution
;;! of the source code are permitted provided that the following conditions
;;! are met:
;;!
;;! * any redistribution and use of the source code, including any resulting
;;! derivative works, are licensed by TI for use only with TI Devices.
;;! * any redistribution and use of any object code compiled from the source
;;! code and any resulting derivative works, are licensed by TI for use
;;! only with TI Devices.
;;!
;;! Neither the name of Texas Instruments Incorporated nor the names of its
;;! suppliers may be used to endorse or promote products derived from this
;;! software without specific prior written permission.
;;#############################################################################
IQNmpyI32int .macro q_value
SETC OVM ; Turn overflow mode on
CLRC TC ; Clear sign flag
ABSTC ACC
MOVL XT,ACC
MOVL ACC,*-SP[4]
ABSTC ACC
IMPYL P,XT,@ACC
QMPYL ACC,XT,@ACC ; ACC:P = IQ*I32 (Q=q_value)
MOV @T,#(q_value)
LSR64 ACC:P,T ; P=integer bits
MOVL ACC,P
NEGTC ACC
CLRC OVM ; Turn overflow mode off
LRETR
.endm
;; The value "GLOBAL_Q" needs to be supplied by the assembler
;; using the "-dGLOBAL_Q=q_value" directive:
.sect "IQmath"
.if GLOBAL_Q == 30
.def __IQ30mpyI32int
__IQ30mpyI32int:
IQNmpyI32int GLOBAL_Q
.endif
.if GLOBAL_Q == 29
.def __IQ29mpyI32int
__IQ29mpyI32int:
IQNmpyI32int GLOBAL_Q
.endif
.if GLOBAL_Q == 28
.def __IQ28mpyI32int
__IQ28mpyI32int:
IQNmpyI32int GLOBAL_Q
.endif
.if GLOBAL_Q == 27
.def __IQ27mpyI32int
__IQ27mpyI32int:
IQNmpyI32int GLOBAL_Q
.endif
.if GLOBAL_Q == 26
.def __IQ26mpyI32int
__IQ26mpyI32int:
IQNmpyI32int GLOBAL_Q
.endif
.if GLOBAL_Q == 25
.def __IQ25mpyI32int
__IQ25mpyI32int:
IQNmpyI32int GLOBAL_Q
.endif
.if GLOBAL_Q == 24
.def __IQ24mpyI32int
__IQ24mpyI32int:
IQNmpyI32int GLOBAL_Q
.endif
.if GLOBAL_Q == 23
.def __IQ23mpyI32int
__IQ23mpyI32int:
IQNmpyI32int GLOBAL_Q
.endif
.if GLOBAL_Q == 22
.def __IQ22mpyI32int
__IQ22mpyI32int:
IQNmpyI32int GLOBAL_Q
.endif
.if GLOBAL_Q == 21
.def __IQ21mpyI32int
__IQ21mpyI32int:
IQNmpyI32int GLOBAL_Q
.endif
.if GLOBAL_Q == 20
.def __IQ20mpyI32int
__IQ20mpyI32int:
IQNmpyI32int GLOBAL_Q
.endif
.if GLOBAL_Q == 19
.def __IQ19mpyI32int
__IQ19mpyI32int:
IQNmpyI32int GLOBAL_Q
.endif
.if GLOBAL_Q == 18
.def __IQ18mpyI32int
__IQ18mpyI32int:
IQNmpyI32int GLOBAL_Q
.endif
.if GLOBAL_Q == 17
.def __IQ17mpyI32int
__IQ17mpyI32int:
IQNmpyI32int GLOBAL_Q
.endif
.if GLOBAL_Q == 16
.def __IQ16mpyI32int
__IQ16mpyI32int:
IQNmpyI32int GLOBAL_Q
.endif
.if GLOBAL_Q == 15
.def __IQ15mpyI32int
__IQ15mpyI32int:
IQNmpyI32int GLOBAL_Q
.endif
.if GLOBAL_Q == 14
.def __IQ14mpyI32int
__IQ14mpyI32int:
IQNmpyI32int GLOBAL_Q
.endif
.if GLOBAL_Q == 13
.def __IQ13mpyI32int
__IQ13mpyI32int:
IQNmpyI32int GLOBAL_Q
.endif
.if GLOBAL_Q == 12
.def __IQ12mpyI32int
__IQ12mpyI32int:
IQNmpyI32int GLOBAL_Q
.endif
.if GLOBAL_Q == 11
.def __IQ11mpyI32int
__IQ11mpyI32int:
IQNmpyI32int GLOBAL_Q
.endif
.if GLOBAL_Q == 10
.def __IQ10mpyI32int
__IQ10mpyI32int:
IQNmpyI32int GLOBAL_Q
.endif
.if GLOBAL_Q == 9
.def __IQ9mpyI32int
__IQ9mpyI32int:
IQNmpyI32int GLOBAL_Q
.endif
.if GLOBAL_Q == 8
.def __IQ8mpyI32int
__IQ8mpyI32int:
IQNmpyI32int GLOBAL_Q
.endif
.if GLOBAL_Q == 7
.def __IQ7mpyI32int
__IQ7mpyI32int:
IQNmpyI32int GLOBAL_Q
.endif
.if GLOBAL_Q == 6
.def __IQ6mpyI32int
__IQ6mpyI32int:
IQNmpyI32int GLOBAL_Q
.endif
.if GLOBAL_Q == 5
.def __IQ5mpyI32int
__IQ5mpyI32int:
IQNmpyI32int GLOBAL_Q
.endif
.if GLOBAL_Q == 4
.def __IQ4mpyI32int
__IQ4mpyI32int:
IQNmpyI32int GLOBAL_Q
.endif
.if GLOBAL_Q == 3
.def __IQ3mpyI32int
__IQ3mpyI32int:
IQNmpyI32int GLOBAL_Q
.endif
.if GLOBAL_Q == 2
.def __IQ2mpyI32int
__IQ2mpyI32int:
IQNmpyI32int GLOBAL_Q
.endif
.if GLOBAL_Q == 1
.def __IQ1mpyI32int
__IQ1mpyI32int:
IQNmpyI32int GLOBAL_Q
.endif
;;###########################################################################
;; No More.
;;###########################################################################
@@ -0,0 +1,344 @@
;;###########################################################################
;;
;; FILE: IQNrmpy.asm
;;
;; TITLE: C Callable IQ Multiply With Rounding Math Function
;;
;;###########################################################################
;;
;; Ver | Date | Who | Description of changes
;; =====|=============|=======|==============================================
;; 1.2 | 11 Oct 2001 | A. T. | Original Release.
;; -----|-------------|-------|----------------------------------------------
;; 1.3 | 19 Nov 2001 | A. T. | No changes.
;; -----|-------------|-------|----------------------------------------------
;; 1.4 | 17 May 2002 | A. T. | Made correction to table pointer.
;; | | | Corrected benchmark cycles.
;; -----|-------------|-------|----------------------------------------------
;; | | |
;;
;;###########################################################################
;;===========================================================================
;; Function: _IQNrmpy
;;===========================================================================
;;
;; C Usage: extern long _IQNrmpy(long M, long X); // with rounding only
;;
;;---------------------------------------------------------------------------
;;
;; On Entry: ACC = M in IQ format
;; [SP-4] = X in IQ format
;;
;; Regs Used: XT, P
;;
;; On Exit: ACC = M * X in IQ format
;;
;; Q range: 30 to 1
;;
;;---------------------------------------------------------------------------
;; Algorithm: The IQmpy operation generates a 64-bit result. The 64-bit
;; number must then be scaled back to a 32-bit value with the
;; same Q value as the original inputs. For example: if Q = 27,
;; then the multiplication of two I5Q27 numbers results in the
;; following 64-bit value:
;;
;; I5Q27 * I5Q27 = I10Q54
;;
;; The value is then scaled back to an I5Q27 value as follows:
;;
;; I5Q27 = I10Q54 >> Q
;;
;; For some of the operations, the value is either rounded or
;; saturated (or both) to its maximum value before scaling.
;;
;;---------------------------------------------------------------------------
;; Benchmark:
;;
;; Cycles = 16 (Q >= 16, if Table in 0-wait SARAM, includes LCR/LRETR)
;; 17 (Q <= 15, if Table in 0-wait SARAM, includes LCR/LRETR)
;;
;; Cycles = 18 (Q >= 16, if Table in 1-wait ROM, includes LCR/LRETR)
;; 19 (Q <= 15, if Table in 1-wait ROM, includes LCR/LRETR)
;;
;;---------------------------------------------------------------------------
;;#############################################################################
;;!
;;! Copyright: Copyright (C) 2023 Texas Instruments Incorporated -
;;! All rights reserved not granted herein.
;;! Limited License.
;;!
;;! Texas Instruments Incorporated grants a world-wide, royalty-free,
;;! non-exclusive license under copyrights and patents it now or hereafter
;;! owns or controls to make, have made, use, import, offer to sell and sell
;;! ("Utilize") this software subject to the terms herein. With respect to the
;;! foregoing patent license, such license is granted solely to the extent that
;;! any such patent is necessary to Utilize the software alone. The patent
;;! license shall not apply to any combinations which include this software,
;;! other than combinations with devices manufactured by or for TI
;;! ("TI Devices").
;;! No hardware patent is licensed hereunder.
;;!
;;! Redistributions must preserve existing copyright notices and reproduce this
;;! license (including the above copyright notice and the disclaimer and
;;! (if applicable) source code license limitations below) in the documentation
;;! and/or other materials provided with the distribution.
;;!
;;! Redistribution and use in binary form, without modification, are permitted
;;! provided that the following conditions are met:
;;!
;;! * No reverse engineering, decompilation, or disassembly of this software is
;;! permitted with respect to any software provided in binary form.
;;! * Any redistribution and use are licensed by TI for use only
;;! with TI Devices.
;;! * Nothing shall obligate TI to provide you with source code for the
;;! software licensed and provided to you in object code.
;;!
;;! If software source code is provided to you, modification and redistribution
;;! of the source code are permitted provided that the following conditions
;;! are met:
;;!
;;! * any redistribution and use of the source code, including any resulting
;;! derivative works, are licensed by TI for use only with TI Devices.
;;! * any redistribution and use of any object code compiled from the source
;;! code and any resulting derivative works, are licensed by TI for use
;;! only with TI Devices.
;;!
;;! Neither the name of Texas Instruments Incorporated nor the names of its
;;! suppliers may be used to endorse or promote products derived from this
;;! software without specific prior written permission.
;;#############################################################################
IQNrmpy .macro q_value, table
MOVL XT,@ACC ; XT = M
IMPYL P,XT,*-SP[4]
QMPYL ACC,XT,*-SP[4] ; ACC:P = M * X
MOVL XAR7,#table
ADDUL P,*XAR7++ ; Round value
ADDCL ACC,*XAR7++
.if q_value >= 16
LSL64 ACC:P,#(32 - q_value) ; Scale back to required IQ value
.endif
.if q_value <= 15
LSL64 ACC:P,#16 ; Scale back to required IQ value
LSL64 ACC:P,#(16 - q_value)
.endif
LRETR
.endm
;; The value "GLOBAL_Q" needs to be supplied by the assembler
;; using the "-dGLOBAL_Q=q_value" directive:
.sect "IQmath"
.if GLOBAL_Q == 30
.ref _IQ30mpyRndSatTable
.def __IQ30rmpy
__IQ30rmpy:
IQNrmpy GLOBAL_Q, _IQ30mpyRndSatTable
.endif
.if GLOBAL_Q == 29
.ref _IQ29mpyRndSatTable
.def __IQ29rmpy
__IQ29rmpy:
IQNrmpy GLOBAL_Q, _IQ29mpyRndSatTable
.endif
.if GLOBAL_Q == 28
.ref _IQ28mpyRndSatTable
.def __IQ28rmpy
__IQ28rmpy:
IQNrmpy GLOBAL_Q, _IQ28mpyRndSatTable
.endif
.if GLOBAL_Q == 27
.ref _IQ27mpyRndSatTable
.def __IQ27rmpy
__IQ27rmpy:
IQNrmpy GLOBAL_Q, _IQ27mpyRndSatTable
.endif
.if GLOBAL_Q == 26
.ref _IQ26mpyRndSatTable
.def __IQ26rmpy
__IQ26rmpy:
IQNrmpy GLOBAL_Q, _IQ26mpyRndSatTable
.endif
.if GLOBAL_Q == 25
.ref _IQ25mpyRndSatTable
.def __IQ25rmpy
__IQ25rmpy:
IQNrmpy GLOBAL_Q, _IQ25mpyRndSatTable
.endif
.if GLOBAL_Q == 24
.ref _IQ24mpyRndSatTable
.def __IQ24rmpy
__IQ24rmpy:
IQNrmpy GLOBAL_Q, _IQ24mpyRndSatTable
.endif
.if GLOBAL_Q == 23
.ref _IQ23mpyRndSatTable
.def __IQ23rmpy
__IQ23rmpy:
IQNrmpy GLOBAL_Q, _IQ23mpyRndSatTable
.endif
.if GLOBAL_Q == 22
.ref _IQ22mpyRndSatTable
.def __IQ22rmpy
__IQ22rmpy:
IQNrmpy GLOBAL_Q, _IQ22mpyRndSatTable
.endif
.if GLOBAL_Q == 21
.ref _IQ21mpyRndSatTable
.def __IQ21rmpy
__IQ21rmpy:
IQNrmpy GLOBAL_Q, _IQ21mpyRndSatTable
.endif
.if GLOBAL_Q == 20
.ref _IQ20mpyRndSatTable
.def __IQ20rmpy
__IQ20rmpy:
IQNrmpy GLOBAL_Q, _IQ20mpyRndSatTable
.endif
.if GLOBAL_Q == 19
.ref _IQ19mpyRndSatTable
.def __IQ19rmpy
__IQ19rmpy:
IQNrmpy GLOBAL_Q, _IQ19mpyRndSatTable
.endif
.if GLOBAL_Q == 18
.ref _IQ18mpyRndSatTable
.def __IQ18rmpy
__IQ18rmpy:
IQNrmpy GLOBAL_Q, _IQ18mpyRndSatTable
.endif
.if GLOBAL_Q == 17
.ref _IQ17mpyRndSatTable
.def __IQ17rmpy
__IQ17rmpy:
IQNrmpy GLOBAL_Q, _IQ17mpyRndSatTable
.endif
.if GLOBAL_Q == 16
.ref _IQ16mpyRndSatTable
.def __IQ16rmpy
__IQ16rmpy:
IQNrmpy GLOBAL_Q, _IQ16mpyRndSatTable
.endif
.if GLOBAL_Q == 15
.ref _IQ15mpyRndSatTable
.def __IQ15rmpy
__IQ15rmpy:
IQNrmpy GLOBAL_Q, _IQ15mpyRndSatTable
.endif
.if GLOBAL_Q == 14
.ref _IQ14mpyRndSatTable
.def __IQ14rmpy
__IQ14rmpy:
IQNrmpy GLOBAL_Q, _IQ14mpyRndSatTable
.endif
.if GLOBAL_Q == 13
.ref _IQ13mpyRndSatTable
.def __IQ13rmpy
__IQ13rmpy:
IQNrmpy GLOBAL_Q, _IQ13mpyRndSatTable
.endif
.if GLOBAL_Q == 12
.ref _IQ12mpyRndSatTable
.def __IQ12rmpy
__IQ12rmpy:
IQNrmpy GLOBAL_Q, _IQ12mpyRndSatTable
.endif
.if GLOBAL_Q == 11
.ref _IQ11mpyRndSatTable
.def __IQ11rmpy
__IQ11rmpy:
IQNrmpy GLOBAL_Q, _IQ11mpyRndSatTable
.endif
.if GLOBAL_Q == 10
.ref _IQ10mpyRndSatTable
.def __IQ10rmpy
__IQ10rmpy:
IQNrmpy GLOBAL_Q, _IQ10mpyRndSatTable
.endif
.if GLOBAL_Q == 9
.ref _IQ9mpyRndSatTable
.def __IQ9rmpy
__IQ9rmpy:
IQNrmpy GLOBAL_Q, _IQ9mpyRndSatTable
.endif
.if GLOBAL_Q == 8
.ref _IQ8mpyRndSatTable
.def __IQ8rmpy
__IQ8rmpy:
IQNrmpy GLOBAL_Q, _IQ8mpyRndSatTable
.endif
.if GLOBAL_Q == 7
.ref _IQ7mpyRndSatTable
.def __IQ7rmpy
__IQ7rmpy:
IQNrmpy GLOBAL_Q, _IQ7mpyRndSatTable
.endif
.if GLOBAL_Q == 6
.ref _IQ6mpyRndSatTable
.def __IQ6rmpy
__IQ6rmpy:
IQNrmpy GLOBAL_Q, _IQ6mpyRndSatTable
.endif
.if GLOBAL_Q == 5
.ref _IQ5mpyRndSatTable
.def __IQ5rmpy
__IQ5rmpy:
IQNrmpy GLOBAL_Q, _IQ5mpyRndSatTable
.endif
.if GLOBAL_Q == 4
.ref _IQ4mpyRndSatTable
.def __IQ4rmpy
__IQ4rmpy:
IQNrmpy GLOBAL_Q, _IQ4mpyRndSatTable
.endif
.if GLOBAL_Q == 3
.ref _IQ3mpyRndSatTable
.def __IQ3rmpy
__IQ3rmpy:
IQNrmpy GLOBAL_Q, _IQ3mpyRndSatTable
.endif
.if GLOBAL_Q == 2
.ref _IQ2mpyRndSatTable
.def __IQ2rmpy
__IQ2rmpy:
IQNrmpy GLOBAL_Q, _IQ2mpyRndSatTable
.endif
.if GLOBAL_Q == 1
.ref _IQ1mpyRndSatTable
.def __IQ1rmpy
__IQ1rmpy:
IQNrmpy GLOBAL_Q, _IQ1mpyRndSatTable
.endif
;;###########################################################################
;; No More.
;;###########################################################################
@@ -0,0 +1,348 @@
;;###########################################################################
;;
;; FILE: IQNrsmpy.asm
;;
;; TITLE: C Callable IQ Multiply With Rounding & Saturation Math Function
;;
;;###########################################################################
;;
;; Ver | Date | Who | Description of changes
;; =====|=============|=======|==============================================
;; 1.2 | 11 Oct 2001 | A. T. | Original Release.
;; -----|-------------|-------|----------------------------------------------
;; 1.3 | 19 Nov 2001 | A. T. | No changes.
;; -----|-------------|-------|----------------------------------------------
;; 1.4 | 17 May 2002 | A. T. | Made correction to table pointer.
;; | | | Corrected benchmark cycles.
;; -----|-------------|-------|----------------------------------------------
;; | | |
;;
;;###########################################################################
;;===========================================================================
;; Function: _IQNrsmpy
;;===========================================================================
;;
;; C Usage: extern long _IQNrsmpy(long M, long X); // with sat & round
;;
;;---------------------------------------------------------------------------
;;
;; On Entry: ACC = M in IQ format
;; [SP-4] = X in IQ format
;;
;; Regs Used: XT, P
;;
;; On Exit: ACC = M * X in IQ format
;;
;; Q range: 30 to 1
;;
;;---------------------------------------------------------------------------
;; Algorithm: The IQmpy operation generates a 64-bit result. The 64-bit
;; number must then be scaled back to a 32-bit value with the
;; same Q value as the original inputs. For example: if Q = 27,
;; then the multiplication of two I5Q27 numbers results in the
;; following 64-bit value:
;;
;; I5Q27 * I5Q27 = I10Q54
;;
;; The value is then scaled back to an I5Q27 value as follows:
;;
;; I5Q27 = I10Q54 >> Q
;;
;; For some of the operations, the value is either rounded or
;; saturated (or both) to its maximum value before scaling.
;;
;;---------------------------------------------------------------------------
;; Benchmark:
;;
;; Cycles = 20 (Q >= 16, if Table in 0-wait SARAM, includes LCR/LRETR)
;; 21 (Q <= 15, if Table in 0-wait SARAM, includes LCR/LRETR)
;;
;; Cycles = 26 (Q >= 16, if Table in 1-wait ROM, includes LCR/LRETR)
;; 27 (Q <= 15, if Table in 1-wait ROM, includes LCR/LRETR)
;;
;;---------------------------------------------------------------------------
;;#############################################################################
;;!
;;! Copyright: Copyright (C) 2023 Texas Instruments Incorporated -
;;! All rights reserved not granted herein.
;;! Limited License.
;;!
;;! Texas Instruments Incorporated grants a world-wide, royalty-free,
;;! non-exclusive license under copyrights and patents it now or hereafter
;;! owns or controls to make, have made, use, import, offer to sell and sell
;;! ("Utilize") this software subject to the terms herein. With respect to the
;;! foregoing patent license, such license is granted solely to the extent that
;;! any such patent is necessary to Utilize the software alone. The patent
;;! license shall not apply to any combinations which include this software,
;;! other than combinations with devices manufactured by or for TI
;;! ("TI Devices").
;;! No hardware patent is licensed hereunder.
;;!
;;! Redistributions must preserve existing copyright notices and reproduce this
;;! license (including the above copyright notice and the disclaimer and
;;! (if applicable) source code license limitations below) in the documentation
;;! and/or other materials provided with the distribution.
;;!
;;! Redistribution and use in binary form, without modification, are permitted
;;! provided that the following conditions are met:
;;!
;;! * No reverse engineering, decompilation, or disassembly of this software is
;;! permitted with respect to any software provided in binary form.
;;! * Any redistribution and use are licensed by TI for use only
;;! with TI Devices.
;;! * Nothing shall obligate TI to provide you with source code for the
;;! software licensed and provided to you in object code.
;;!
;;! If software source code is provided to you, modification and redistribution
;;! of the source code are permitted provided that the following conditions
;;! are met:
;;!
;;! * any redistribution and use of the source code, including any resulting
;;! derivative works, are licensed by TI for use only with TI Devices.
;;! * any redistribution and use of any object code compiled from the source
;;! code and any resulting derivative works, are licensed by TI for use
;;! only with TI Devices.
;;!
;;! Neither the name of Texas Instruments Incorporated nor the names of its
;;! suppliers may be used to endorse or promote products derived from this
;;! software without specific prior written permission.
;;#############################################################################
IQNrsmpy .macro q_value, table
MOVL XT,@ACC ; XT = M
IMPYL P,XT,*-SP[4]
QMPYL ACC,XT,*-SP[4] ; ACC:P = M * X
MOVL XAR7,#table
ADDUL P,*XAR7++ ; Round value
ADDCL ACC,*XAR7++
MINL ACC,*+XAR7[2] ; Saturate value
MINCUL P,*+XAR7[0]
MAXL ACC,*+XAR7[6]
MAXCUL P,*+XAR7[4]
.if q_value >= 16
LSL64 ACC:P,#(32 - q_value) ; Scale back to required IQ value
.endif
.if q_value <= 15
LSL64 ACC:P,#16 ; Scale back to required IQ value
LSL64 ACC:P,#(16 - q_value)
.endif
LRETR
.endm
;; The value "GLOBAL_Q" needs to be supplied by the assembler
;; using the "-dGLOBAL_Q=q_value" directive:
.sect "IQmath"
.if GLOBAL_Q == 30
.ref _IQ30mpyRndSatTable
.def __IQ30rsmpy
__IQ30rsmpy:
IQNrsmpy GLOBAL_Q, _IQ30mpyRndSatTable
.endif
.if GLOBAL_Q == 29
.ref _IQ29mpyRndSatTable
.def __IQ29rsmpy
__IQ29rsmpy:
IQNrsmpy GLOBAL_Q, _IQ29mpyRndSatTable
.endif
.if GLOBAL_Q == 28
.ref _IQ28mpyRndSatTable
.def __IQ28rsmpy
__IQ28rsmpy:
IQNrsmpy GLOBAL_Q, _IQ28mpyRndSatTable
.endif
.if GLOBAL_Q == 27
.ref _IQ27mpyRndSatTable
.def __IQ27rsmpy
__IQ27rsmpy:
IQNrsmpy GLOBAL_Q, _IQ27mpyRndSatTable
.endif
.if GLOBAL_Q == 26
.ref _IQ26mpyRndSatTable
.def __IQ26rsmpy
__IQ26rsmpy:
IQNrsmpy GLOBAL_Q, _IQ26mpyRndSatTable
.endif
.if GLOBAL_Q == 25
.ref _IQ25mpyRndSatTable
.def __IQ25rsmpy
__IQ25rsmpy:
IQNrsmpy GLOBAL_Q, _IQ25mpyRndSatTable
.endif
.if GLOBAL_Q == 24
.ref _IQ24mpyRndSatTable
.def __IQ24rsmpy
__IQ24rsmpy:
IQNrsmpy GLOBAL_Q, _IQ24mpyRndSatTable
.endif
.if GLOBAL_Q == 23
.ref _IQ23mpyRndSatTable
.def __IQ23rsmpy
__IQ23rsmpy:
IQNrsmpy GLOBAL_Q, _IQ23mpyRndSatTable
.endif
.if GLOBAL_Q == 22
.ref _IQ22mpyRndSatTable
.def __IQ22rsmpy
__IQ22rsmpy:
IQNrsmpy GLOBAL_Q, _IQ22mpyRndSatTable
.endif
.if GLOBAL_Q == 21
.ref _IQ21mpyRndSatTable
.def __IQ21rsmpy
__IQ21rsmpy:
IQNrsmpy GLOBAL_Q, _IQ21mpyRndSatTable
.endif
.if GLOBAL_Q == 20
.ref _IQ20mpyRndSatTable
.def __IQ20rsmpy
__IQ20rsmpy:
IQNrsmpy GLOBAL_Q, _IQ20mpyRndSatTable
.endif
.if GLOBAL_Q == 19
.ref _IQ19mpyRndSatTable
.def __IQ19rsmpy
__IQ19rsmpy:
IQNrsmpy GLOBAL_Q, _IQ19mpyRndSatTable
.endif
.if GLOBAL_Q == 18
.ref _IQ18mpyRndSatTable
.def __IQ18rsmpy
__IQ18rsmpy:
IQNrsmpy GLOBAL_Q, _IQ18mpyRndSatTable
.endif
.if GLOBAL_Q == 17
.ref _IQ17mpyRndSatTable
.def __IQ17rsmpy
__IQ17rsmpy:
IQNrsmpy GLOBAL_Q, _IQ17mpyRndSatTable
.endif
.if GLOBAL_Q == 16
.ref _IQ16mpyRndSatTable
.def __IQ16rsmpy
__IQ16rsmpy:
IQNrsmpy GLOBAL_Q, _IQ16mpyRndSatTable
.endif
.if GLOBAL_Q == 15
.ref _IQ15mpyRndSatTable
.def __IQ15rsmpy
__IQ15rsmpy:
IQNrsmpy GLOBAL_Q, _IQ15mpyRndSatTable
.endif
.if GLOBAL_Q == 14
.ref _IQ14mpyRndSatTable
.def __IQ14rsmpy
__IQ14rsmpy:
IQNrsmpy GLOBAL_Q, _IQ14mpyRndSatTable
.endif
.if GLOBAL_Q == 13
.ref _IQ13mpyRndSatTable
.def __IQ13rsmpy
__IQ13rsmpy:
IQNrsmpy GLOBAL_Q, _IQ13mpyRndSatTable
.endif
.if GLOBAL_Q == 12
.ref _IQ12mpyRndSatTable
.def __IQ12rsmpy
__IQ12rsmpy:
IQNrsmpy GLOBAL_Q, _IQ12mpyRndSatTable
.endif
.if GLOBAL_Q == 11
.ref _IQ11mpyRndSatTable
.def __IQ11rsmpy
__IQ11rsmpy:
IQNrsmpy GLOBAL_Q, _IQ11mpyRndSatTable
.endif
.if GLOBAL_Q == 10
.ref _IQ10mpyRndSatTable
.def __IQ10rsmpy
__IQ10rsmpy:
IQNrsmpy GLOBAL_Q, _IQ10mpyRndSatTable
.endif
.if GLOBAL_Q == 9
.ref _IQ9mpyRndSatTable
.def __IQ9rsmpy
__IQ9rsmpy:
IQNrsmpy GLOBAL_Q, _IQ9mpyRndSatTable
.endif
.if GLOBAL_Q == 8
.ref _IQ8mpyRndSatTable
.def __IQ8rsmpy
__IQ8rsmpy:
IQNrsmpy GLOBAL_Q, _IQ8mpyRndSatTable
.endif
.if GLOBAL_Q == 7
.ref _IQ7mpyRndSatTable
.def __IQ7rsmpy
__IQ7rsmpy:
IQNrsmpy GLOBAL_Q, _IQ7mpyRndSatTable
.endif
.if GLOBAL_Q == 6
.ref _IQ6mpyRndSatTable
.def __IQ6rsmpy
__IQ6rsmpy:
IQNrsmpy GLOBAL_Q, _IQ6mpyRndSatTable
.endif
.if GLOBAL_Q == 5
.ref _IQ5mpyRndSatTable
.def __IQ5rsmpy
__IQ5rsmpy:
IQNrsmpy GLOBAL_Q, _IQ5mpyRndSatTable
.endif
.if GLOBAL_Q == 4
.ref _IQ4mpyRndSatTable
.def __IQ4rsmpy
__IQ4rsmpy:
IQNrsmpy GLOBAL_Q, _IQ4mpyRndSatTable
.endif
.if GLOBAL_Q == 3
.ref _IQ3mpyRndSatTable
.def __IQ3rsmpy
__IQ3rsmpy:
IQNrsmpy GLOBAL_Q, _IQ3mpyRndSatTable
.endif
.if GLOBAL_Q == 2
.ref _IQ2mpyRndSatTable
.def __IQ2rsmpy
__IQ2rsmpy:
IQNrsmpy GLOBAL_Q, _IQ2mpyRndSatTable
.endif
.if GLOBAL_Q == 1
.ref _IQ1mpyRndSatTable
.def __IQ1rsmpy
__IQ1rsmpy:
IQNrsmpy GLOBAL_Q, _IQ1mpyRndSatTable
.endif
;;###########################################################################
;; No More.
;;###########################################################################
@@ -0,0 +1,380 @@
;;###########################################################################
;;
;; FILE: IQNsin.asm
;;
;; TITLE: C Callable IQ Sin Math Function
;;
;;###########################################################################
;;
;; Ver | Date | Who | Description of changes
;; =====|=============|=======|==============================================
;; 1.2 | 11 Oct 2001 | A. T. | Original Release.
;; -----|-------------|-------|----------------------------------------------
;; 1.3 | 19 Nov 2001 | A. T. | No changes.
;; -----|-------------|-------|----------------------------------------------
;; 1.4 | 17 May 2002 | A. T. | Improved accuracy from 16 to about 29 bits
;; | | | (thanks to Settu for writing original code).
;; | | | Modified code so that calculation constants
;; | | | are embedded in code and not in table.
;; -----|-------------|-------|----------------------------------------------
;; | | |
;;
;;###########################################################################
;;===========================================================================
;; Function: _IQNsin
;;===========================================================================
;;
;; C Usage: extern long _IQNsin(long Rad);
;;
;;---------------------------------------------------------------------------
;;
;; On Entry: ACC = Radian in IQ format
;;
;; Regs Used: XAR7, XAR6, XAR5, XAR4, XAR0
;; XT, P
;;
;; On Exit: ACC = sin(Radian) result in IQ format
;;
;; Q range: 30 to 1
;;
;;---------------------------------------------------------------------------
;; Algorithm: The "sin" value is calculated as follows:
;;
;; 1) The offset into a 512 word sin/cos table is calculated:
;;
;; k = 0x1FF & (int(Radian*512/(2*pi)))
;;
;;
;; 2) The fractional component between table samples is
;; calculated:
;;
;; x = fract(Radian*512/2*pi) * (2*pi)/512
;;
;; 3) The output sine value is calculated as follows:
;;
;; sin(Radian) = S(k) + x*(C(k) + x*(-0.5*S(k) - 0.166*x*C(k)))
;;
;; where S(k) = Sin table value at offset "k"
;; C(k) = Cos table value at offset "k"
;;
;; Using the above method, with a 512x32 full wave sin/cos table,
;; will give an accuracy of approximately 29 bits.
;;
;;---------------------------------------------------------------------------
;; Benchmark:
;;
;; Assumptions: * Code executes from 0-wait SARAM block.
;; * _IQsin/cos Table located in seperate SARAM or ROM block.
;;
;; Cycles = 45 ( Q == 30, tables in 0-wait SARAM, includes LCR/LRETR)
;; = 46 (29 >= Q >= 25, tables in 0-wait SARAM, includes LCR/LRETR)
;; = 45 (24 >= Q >= 14, tables in 0-wait SARAM, includes LCR/LRETR)
;; = 46 (13 >= Q >= 0, tables in 0-wait SARAM, includes LCR/LRETR)
;;
;; Cycles = 47 ( Q == 30, tables in 1-wait ROM, includes LCR/LRETR)
;; = 48 (29 >= Q >= 25, tables in 1-wait ROM, includes LCR/LRETR)
;; = 47 (24 >= Q >= 14, tables in 1-wait ROM, includes LCR/LRETR)
;; = 48 (13 >= Q >= 0, tables in 1-wait ROM, includes LCR/LRETR)
;;
;;===========================================================================
;;#############################################################################
;;!
;;! Copyright: Copyright (C) 2023 Texas Instruments Incorporated -
;;! All rights reserved not granted herein.
;;! Limited License.
;;!
;;! Texas Instruments Incorporated grants a world-wide, royalty-free,
;;! non-exclusive license under copyrights and patents it now or hereafter
;;! owns or controls to make, have made, use, import, offer to sell and sell
;;! ("Utilize") this software subject to the terms herein. With respect to the
;;! foregoing patent license, such license is granted solely to the extent that
;;! any such patent is necessary to Utilize the software alone. The patent
;;! license shall not apply to any combinations which include this software,
;;! other than combinations with devices manufactured by or for TI
;;! ("TI Devices").
;;! No hardware patent is licensed hereunder.
;;!
;;! Redistributions must preserve existing copyright notices and reproduce this
;;! license (including the above copyright notice and the disclaimer and
;;! (if applicable) source code license limitations below) in the documentation
;;! and/or other materials provided with the distribution.
;;!
;;! Redistribution and use in binary form, without modification, are permitted
;;! provided that the following conditions are met:
;;!
;;! * No reverse engineering, decompilation, or disassembly of this software is
;;! permitted with respect to any software provided in binary form.
;;! * Any redistribution and use are licensed by TI for use only
;;! with TI Devices.
;;! * Nothing shall obligate TI to provide you with source code for the
;;! software licensed and provided to you in object code.
;;!
;;! If software source code is provided to you, modification and redistribution
;;! of the source code are permitted provided that the following conditions
;;! are met:
;;!
;;! * any redistribution and use of the source code, including any resulting
;;! derivative works, are licensed by TI for use only with TI Devices.
;;! * any redistribution and use of any object code compiled from the source
;;! code and any resulting derivative works, are licensed by TI for use
;;! only with TI Devices.
;;!
;;! Neither the name of Texas Instruments Incorporated nor the names of its
;;! suppliers may be used to endorse or promote products derived from this
;;! software without specific prior written permission.
;;#############################################################################
K1 .set 02AAAh ; 1/6 in Q16
IQNsin .macro q_value
MOV *SP++,#0x3F6B ; (2*pi)/512 = 0x03243F6B (Q32)
MOV *SP++,#0x0324
MOV *SP++,#0xC1B7 ; 512/(2*pi) = 0x517CC1B7 (I8Q24)
MOV *SP++,#0x517C
MOVL XAR6,#_IQcosTable
MOVL XAR7,#_IQsinTable
CLRC TC
SETC OVM ; turn overflow mode on
ABSTC ACC ; abs(Rad), TC = sign
MOVL XT,*--SP ; XT = 512/(2*pi) (I8Q24)
IMPYL P,XT,@ACC
QMPYL ACC,XT,@ACC ; ACC:P = Rad * 512 / (2pi)
.if q_value >= 25
ASR64 ACC:P,#16
ASR64 ACC:P,#(q_value - 24) ; ACC = Integer, P = fract
.endif
.if (q_value <= 24) & (q_value >= 9)
ASR64 ACC:P,#(q_value - 8) ; ACC = Integer, P = fract
.endif
.if q_value <= 7
LSL64 ACC:P,#(8 - q_value) ; ACC = Integer, P = fract
.endif
AND @AL,#0x1FF
LSL AL,#1
MOVZ AR0,@AL ; AR0 = Index into "sin/cos" table = k
MOVL XT,*--SP ; XT = (2*pi)/512 (Q32)
QMPYUL P,XT,@P ; P = x (Q32)
MOVB ACC,#0
MOVL XT,@P ; XT = x (Q32)
MOVL XAR4,*+XAR6[AR0] ; XAR4 = C(k) (Q30)
MOVL XAR5,*+XAR7[AR0] ; XAR5 = S(k) (Q30)
SUBL ACC,@XAR5 ; ACC = -S(k) (Q30)
ASR64 ACC:P,#1 ; ACC = -0.5*S(k) (Q30)
QMPYL P,XT,@XAR4 ; P = x*C(k) (Q30)
CLRC OVM ; Turn overflow mode off
MPY P,@PH,#K1 ; P = 0.166*x*C(k) (Q30)
SUBL ACC,@P ; ACC = -0.5*S(k) - 0.166*x*C(k) (Q30)
QMPYL ACC,XT,@ACC ; ACC = x*(-0.5*S(k) - 0.166*x*C(k)) (Q30)
ADDL ACC,@XAR4 ; ACC = C(k) + x*(-0.5*S(k) - 0.166*x*C(k)) (Q30)
QMPYL ACC,XT,@ACC ; ACC = x*(C(k) + x*(-0.5*S(k) - 0.166*x*C(k))) (Q30)
ADDL ACC,@XAR5 ; ACC = S(k) + x*(C(k) + x*(-0.5*S(k) - 0.166*x*C(k))) (Q30)
NEGTC ACC ; Negate result if necessary
.if (q_value <= 29) & (q_value >= 14)
ASR64 ACC:P,#(30 - q_value) ; Scale back to required IQ value
.endif
.if q_value <= 13
ASR64 ACC:P,#16
ASR64 ACC:P,#(14 - q_value) ; Scale back to required IQ value
.endif
LRETR
.endm
;; The value "GLOBAL_Q" needs to be supplied by the assembler
;; using the "-dGLOBAL_Q=q_value" directive:
.sect "IQmath"
.ref _IQsinTable
.ref _IQcosTable
.if GLOBAL_Q == 30
.def __IQ30sin
__IQ30sin:
IQNsin GLOBAL_Q
.endif
.if GLOBAL_Q == 29
.def __IQ29sin
__IQ29sin:
IQNsin GLOBAL_Q
.endif
.if GLOBAL_Q == 28
.def __IQ28sin
__IQ28sin:
IQNsin GLOBAL_Q
.endif
.if GLOBAL_Q == 27
.def __IQ27sin
__IQ27sin:
IQNsin GLOBAL_Q
.endif
.if GLOBAL_Q == 26
.def __IQ26sin
__IQ26sin:
IQNsin GLOBAL_Q
.endif
.if GLOBAL_Q == 25
.def __IQ25sin
__IQ25sin:
IQNsin GLOBAL_Q
.endif
.if GLOBAL_Q == 24
.def __IQ24sin
__IQ24sin:
IQNsin GLOBAL_Q
.endif
.if GLOBAL_Q == 23
.def __IQ23sin
__IQ23sin:
IQNsin GLOBAL_Q
.endif
.if GLOBAL_Q == 22
.def __IQ22sin
__IQ22sin:
IQNsin GLOBAL_Q
.endif
.if GLOBAL_Q == 21
.def __IQ21sin
__IQ21sin:
IQNsin GLOBAL_Q
.endif
.if GLOBAL_Q == 20
.def __IQ20sin
__IQ20sin:
IQNsin GLOBAL_Q
.endif
.if GLOBAL_Q == 19
.def __IQ19sin
__IQ19sin:
IQNsin GLOBAL_Q
.endif
.if GLOBAL_Q == 18
.def __IQ18sin
__IQ18sin:
IQNsin GLOBAL_Q
.endif
.if GLOBAL_Q == 17
.def __IQ17sin
__IQ17sin:
IQNsin GLOBAL_Q
.endif
.if GLOBAL_Q == 16
.def __IQ16sin
__IQ16sin:
IQNsin GLOBAL_Q
.endif
.if GLOBAL_Q == 15
.def __IQ15sin
__IQ15sin:
IQNsin GLOBAL_Q
.endif
.if GLOBAL_Q == 14
.def __IQ14sin
__IQ14sin:
IQNsin GLOBAL_Q
.endif
.if GLOBAL_Q == 13
.def __IQ13sin
__IQ13sin:
IQNsin GLOBAL_Q
.endif
.if GLOBAL_Q == 12
.def __IQ12sin
__IQ12sin:
IQNsin GLOBAL_Q
.endif
.if GLOBAL_Q == 11
.def __IQ11sin
__IQ11sin:
IQNsin GLOBAL_Q
.endif
.if GLOBAL_Q == 10
.def __IQ10sin
__IQ10sin:
IQNsin GLOBAL_Q
.endif
.if GLOBAL_Q == 9
.def __IQ9sin
__IQ9sin:
IQNsin GLOBAL_Q
.endif
.if GLOBAL_Q == 8
.def __IQ8sin
__IQ8sin:
IQNsin GLOBAL_Q
.endif
.if GLOBAL_Q == 7
.def __IQ7sin
__IQ7sin:
IQNsin GLOBAL_Q
.endif
.if GLOBAL_Q == 6
.def __IQ6sin
__IQ6sin:
IQNsin GLOBAL_Q
.endif
.if GLOBAL_Q == 5
.def __IQ5sin
__IQ5sin:
IQNsin GLOBAL_Q
.endif
.if GLOBAL_Q == 4
.def __IQ4sin
__IQ4sin:
IQNsin GLOBAL_Q
.endif
.if GLOBAL_Q == 3
.def __IQ3sin
__IQ3sin:
IQNsin GLOBAL_Q
.endif
.if GLOBAL_Q == 2
.def __IQ2sin
__IQ2sin:
IQNsin GLOBAL_Q
.endif
.if GLOBAL_Q == 1
.def __IQ1sin
__IQ1sin:
IQNsin GLOBAL_Q
.endif
;;###########################################################################
;; No More.
;;###########################################################################
@@ -0,0 +1,368 @@
;;###########################################################################
;;
;; FILE: IQNsinPU.asm
;;
;; TITLE: C Callable IQ Sin Per Unit Math Function
;;
;;###########################################################################
;;
;; Ver | Date | Who | Description of changes
;; =====|=============|=======|==============================================
;; 1.4 | 17 May 2002 | A. T. | Original Release.
;; -----|-------------|-------|----------------------------------------------
;; | | |
;;
;;###########################################################################
;;===========================================================================
;; Function: _IQNsinPU
;;===========================================================================
;;
;; C Usage: extern long _IQNsinPU(long PU);
;;
;;---------------------------------------------------------------------------
;;
;; On Entry: ACC = Per Unit in IQ format
;;
;; Regs Used: XAR7, XAR6, XAR5, XAR4, XAR0
;; XT, P
;;
;; On Exit: ACC = sin(PU) result in IQ format
;;
;; Q range: 30 to 1
;;
;;---------------------------------------------------------------------------
;; Algorithm: The "sinPU" value is calculated as follows:
;;
;; 1) The offset into a 512 word sin/cos table is calculated:
;;
;; k = 0x1FF & (int(PU*512))
;;
;;
;; 2) The fractional component between table samples is
;; calculated:
;;
;; x = fract(PU*512) * (2*pi)/512
;;
;; 3) The output sine value is calculated as follows:
;;
;; sin(PU) = S(k) + x*(C(k) + x*(-0.5*S(k) - 0.166*x*C(k)))
;;
;; where S(k) = Sin table value at offset "k"
;; C(k) = Cos table value at offset "k"
;;
;; Using the above method, with a 512x32 full wave sin/cos table,
;; will give an accuracy of approximately 29 bits.
;;
;;---------------------------------------------------------------------------
;; Benchmark:
;;
;; Assumptions: * Code executes from 0-wait SARAM block.
;; * _IQsin/cos Table located in seperate SARAM or ROM block.
;;
;; Cycles = 39 ( Q == 30, tables in 0-wait SARAM, includes LCR/LRETR)
;; = 40 (29 >= Q >= 26, tables in 0-wait SARAM, includes LCR/LRETR)
;; = 39 (25 >= Q >= 14, tables in 0-wait SARAM, includes LCR/LRETR)
;; = 40 (13 >= Q >= 0, tables in 0-wait SARAM, includes LCR/LRETR)
;;
;; Cycles = 41 ( Q == 30, tables in 1-wait ROM, includes LCR/LRETR)
;; = 42 (29 >= Q >= 26, tables in 1-wait ROM, includes LCR/LRETR)
;; = 41 (25 >= Q >= 14, tables in 1-wait ROM, includes LCR/LRETR)
;; = 42 (13 >= Q >= 0, tables in 1-wait ROM, includes LCR/LRETR)
;;
;;===========================================================================
;;#############################################################################
;;!
;;! Copyright: Copyright (C) 2023 Texas Instruments Incorporated -
;;! All rights reserved not granted herein.
;;! Limited License.
;;!
;;! Texas Instruments Incorporated grants a world-wide, royalty-free,
;;! non-exclusive license under copyrights and patents it now or hereafter
;;! owns or controls to make, have made, use, import, offer to sell and sell
;;! ("Utilize") this software subject to the terms herein. With respect to the
;;! foregoing patent license, such license is granted solely to the extent that
;;! any such patent is necessary to Utilize the software alone. The patent
;;! license shall not apply to any combinations which include this software,
;;! other than combinations with devices manufactured by or for TI
;;! ("TI Devices").
;;! No hardware patent is licensed hereunder.
;;!
;;! Redistributions must preserve existing copyright notices and reproduce this
;;! license (including the above copyright notice and the disclaimer and
;;! (if applicable) source code license limitations below) in the documentation
;;! and/or other materials provided with the distribution.
;;!
;;! Redistribution and use in binary form, without modification, are permitted
;;! provided that the following conditions are met:
;;!
;;! * No reverse engineering, decompilation, or disassembly of this software is
;;! permitted with respect to any software provided in binary form.
;;! * Any redistribution and use are licensed by TI for use only
;;! with TI Devices.
;;! * Nothing shall obligate TI to provide you with source code for the
;;! software licensed and provided to you in object code.
;;!
;;! If software source code is provided to you, modification and redistribution
;;! of the source code are permitted provided that the following conditions
;;! are met:
;;!
;;! * any redistribution and use of the source code, including any resulting
;;! derivative works, are licensed by TI for use only with TI Devices.
;;! * any redistribution and use of any object code compiled from the source
;;! code and any resulting derivative works, are licensed by TI for use
;;! only with TI Devices.
;;!
;;! Neither the name of Texas Instruments Incorporated nor the names of its
;;! suppliers may be used to endorse or promote products derived from this
;;! software without specific prior written permission.
;;#############################################################################
K1 .set 02AAAh ; 1/6 in Q16
IQNsinPU .macro q_value
MOV *SP++,#0x3F6B ; (2*pi)/512 = 0x03243F6B (Q32)
MOV *SP++,#0x0324
MOVL XAR6,#_IQcosTable
MOVL XAR7,#_IQsinTable
CLRC TC
SETC OVM ; turn overflow mode on
ABSTC ACC ; abs(Rad), TC = sign
MPYB P,T,#0
.if q_value >= 26
ASR64 ACC:P,#16
ASR64 ACC:P,#(q_value - 25) ; ACC = Integer, P = fract
.endif
.if (q_value <= 25) & (q_value >= 10)
ASR64 ACC:P,#(q_value - 9) ; ACC = Integer, P = fract
.endif
.if q_value <= 8
LSL64 ACC:P,#(9 - q_value) ; ACC = Integer, P = fract
.endif
AND @AL,#0x1FF
LSL AL,#1
MOVZ AR0,@AL ; AR0 = Index into "sin/cos" table = k
MOVL XT,*--SP ; XT = (2*pi)/512 (Q32)
QMPYUL P,XT,@P ; P = x (Q32)
MOVB ACC,#0
MOVL XT,@P ; XT = x (Q32)
MOVL XAR4,*+XAR6[AR0] ; XAR4 = C(k) (Q30)
MOVL XAR5,*+XAR7[AR0] ; XAR5 = S(k) (Q30)
SUBL ACC,@XAR5 ; ACC = -S(k) (Q30)
ASR64 ACC:P,#1 ; ACC = -0.5*S(k) (Q30)
QMPYL P,XT,@XAR4 ; P = x*C(k) (Q30)
CLRC OVM ; Turn overflow mode off
MPY P,@PH,#K1 ; P = 0.166*x*C(k) (Q30)
SUBL ACC,@P ; ACC = -0.5*S(k) - 0.166*x*C(k) (Q30)
QMPYL ACC,XT,@ACC ; ACC = x*(-0.5*S(k) - 0.166*x*C(k)) (Q30)
ADDL ACC,@XAR4 ; ACC = C(k) + x*(-0.5*S(k) - 0.166*x*C(k)) (Q30)
QMPYL ACC,XT,@ACC ; ACC = x*(C(k) + x*(-0.5*S(k) - 0.166*x*C(k))) (Q30)
ADDL ACC,@XAR5 ; ACC = S(k) + x*(C(k) + x*(-0.5*S(k) - 0.166*x*C(k))) (Q30)
NEGTC ACC ; Negate result if necessary
.if (q_value <= 29) & (q_value >= 14)
ASR64 ACC:P,#(30 - q_value) ; Scale back to required IQ value
.endif
.if q_value <= 13
ASR64 ACC:P,#16
ASR64 ACC:P,#(14 - q_value) ; Scale back to required IQ value
.endif
LRETR
.endm
;; The value "GLOBAL_Q" needs to be supplied by the assembler
;; using the "-dGLOBAL_Q=q_value" directive:
.sect "IQmath"
.ref _IQsinTable
.ref _IQcosTable
.if GLOBAL_Q == 30
.def __IQ30sinPU
__IQ30sinPU:
IQNsinPU GLOBAL_Q
.endif
.if GLOBAL_Q == 29
.def __IQ29sinPU
__IQ29sinPU:
IQNsinPU GLOBAL_Q
.endif
.if GLOBAL_Q == 28
.def __IQ28sinPU
__IQ28sinPU:
IQNsinPU GLOBAL_Q
.endif
.if GLOBAL_Q == 27
.def __IQ27sinPU
__IQ27sinPU:
IQNsinPU GLOBAL_Q
.endif
.if GLOBAL_Q == 26
.def __IQ26sinPU
__IQ26sinPU:
IQNsinPU GLOBAL_Q
.endif
.if GLOBAL_Q == 25
.def __IQ25sinPU
__IQ25sinPU:
IQNsinPU GLOBAL_Q
.endif
.if GLOBAL_Q == 24
.def __IQ24sinPU
__IQ24sinPU:
IQNsinPU GLOBAL_Q
.endif
.if GLOBAL_Q == 23
.def __IQ23sinPU
__IQ23sinPU:
IQNsinPU GLOBAL_Q
.endif
.if GLOBAL_Q == 22
.def __IQ22sinPU
__IQ22sinPU:
IQNsinPU GLOBAL_Q
.endif
.if GLOBAL_Q == 21
.def __IQ21sinPU
__IQ21sinPU:
IQNsinPU GLOBAL_Q
.endif
.if GLOBAL_Q == 20
.def __IQ20sinPU
__IQ20sinPU:
IQNsinPU GLOBAL_Q
.endif
.if GLOBAL_Q == 19
.def __IQ19sinPU
__IQ19sinPU:
IQNsinPU GLOBAL_Q
.endif
.if GLOBAL_Q == 18
.def __IQ18sinPU
__IQ18sinPU:
IQNsinPU GLOBAL_Q
.endif
.if GLOBAL_Q == 17
.def __IQ17sinPU
__IQ17sinPU:
IQNsinPU GLOBAL_Q
.endif
.if GLOBAL_Q == 16
.def __IQ16sinPU
__IQ16sinPU:
IQNsinPU GLOBAL_Q
.endif
.if GLOBAL_Q == 15
.def __IQ15sinPU
__IQ15sinPU:
IQNsinPU GLOBAL_Q
.endif
.if GLOBAL_Q == 14
.def __IQ14sinPU
__IQ14sinPU:
IQNsinPU GLOBAL_Q
.endif
.if GLOBAL_Q == 13
.def __IQ13sinPU
__IQ13sinPU:
IQNsinPU GLOBAL_Q
.endif
.if GLOBAL_Q == 12
.def __IQ12sinPU
__IQ12sinPU:
IQNsinPU GLOBAL_Q
.endif
.if GLOBAL_Q == 11
.def __IQ11sinPU
__IQ11sinPU:
IQNsinPU GLOBAL_Q
.endif
.if GLOBAL_Q == 10
.def __IQ10sinPU
__IQ10sinPU:
IQNsinPU GLOBAL_Q
.endif
.if GLOBAL_Q == 9
.def __IQ9sinPU
__IQ9sinPU:
IQNsinPU GLOBAL_Q
.endif
.if GLOBAL_Q == 8
.def __IQ8sinPU
__IQ8sinPU:
IQNsinPU GLOBAL_Q
.endif
.if GLOBAL_Q == 7
.def __IQ7sinPU
__IQ7sinPU:
IQNsinPU GLOBAL_Q
.endif
.if GLOBAL_Q == 6
.def __IQ6sinPU
__IQ6sinPU:
IQNsinPU GLOBAL_Q
.endif
.if GLOBAL_Q == 5
.def __IQ5sinPU
__IQ5sinPU:
IQNsinPU GLOBAL_Q
.endif
.if GLOBAL_Q == 4
.def __IQ4sinPU
__IQ4sinPU:
IQNsinPU GLOBAL_Q
.endif
.if GLOBAL_Q == 3
.def __IQ3sinPU
__IQ3sinPU:
IQNsinPU GLOBAL_Q
.endif
.if GLOBAL_Q == 2
.def __IQ2sinPU
__IQ2sinPU:
IQNsinPU GLOBAL_Q
.endif
.if GLOBAL_Q == 1
.def __IQ1sinPU
__IQ1sinPU:
IQNsinPU GLOBAL_Q
.endif
;;###########################################################################
;; No More.
;;###########################################################################
@@ -0,0 +1,396 @@
;;###########################################################################
;;
;; FILE: IQNsqrt.asm
;;
;; TITLE: C Callable IQ Square Root Math Function
;;
;;###########################################################################
;;
;; Ver | Date | Who | Description of changes
;; =====|=============|=======|==============================================
;; 1.2 | 11 Oct 2001 | A. T. | Original Release.
;; -----|-------------|-------|----------------------------------------------
;; 1.3 | 19 Nov 2001 | A. T. | Improved accuracy.
;; -----|-------------|-------|----------------------------------------------
;; 1.4 | 17 May 2002 | A. T. | Corrected benchmark cycles.
;; -----|-------------|-------|----------------------------------------------
;; | | |
;;
;;###########################################################################
;;===========================================================================
;; Function: _IQNsqrt
;;===========================================================================
;;
;; C Usage: extern long _IQNsqrt(long X); // with rounding
;;
;;---------------------------------------------------------------------------
;;
;; On Entry: ACC = X in IQ format
;;
;; Regs Used: XAR7, XAR6, XAR5, XAR4
;; XT, P, XAR0
;;
;; On Exit: ACC = sqrt(X) result in IQ format
;; ACC = 0 if input is -ve or 0
;;
;; Note: The square root of any number will never
;; overflow, hence saturation is not required.
;;
;; Q range: 30 to 1
;;
;;---------------------------------------------------------------------------
;; Algorithm: The procedure for calculating "Y = sqrt(X)" is similar
;; to calculating the inverse sqare root due to the
;; following relationship:
;;
;; sqrt(X) = X * 1/sqrt(X)
;;
;; The procedure is as follows:
;;
;; Step 1) Normalize Input:
;;
;; V = X * 2^n
;;
;; Step 2) Obtain initial estimate from "_IQsqrtTable"
;; using the upper 8-bits of the normalized V value.
;;
;; Step 3) Use Newton-Raphson algorithm to improve accuracy.
;; Repeat following equation two times. First iteration
;; gives 16-bit accuracy. Second iteration gives 32-bit
;; accuracy:
;;
;; Yn = Yn(1.5 - Yn*Yn*V/2)
;;
;; Yn = 1/sqrt(V)
;;
;; Step 4) Caculate the square root of the normalized number:
;;
;; Yn = V * Yn
;;
;; Step 5) Denormalize result and round:
;;
;; Y = Yn * 2^n / sqrt(2^n)
;;
;;---------------------------------------------------------------------------
;; Benchmark:
;;
;; Assumptions:
;; * Code executes from 0-wait SARAM block.
;; * _IQsqrt Tables located in seperate 0-wait SARAM or 1-wait ROM block.
;; * Stack located in seperate SARAM block.
;; * Includes LCR/LRETR.
;;
;; Cycles = 65 (30 >= Q >= 24 if _IQsqrt Tables in 0-wait SARAM)
;; 64 (23 >= Q >= 22 if _IQsqrt Tables in 0-wait SARAM)
;; 65 (21 >= Q >= 0 if _IQsqrt Tables in 0-wait SARAM)
;;
;; Cycles = 69 (30 >= Q >= 24 if _IQsqrt Tables in 1-wait ROM)
;; 68 (23 >= Q >= 22 if _IQsqrt Tables in 1-wait ROM)
;; 69 (21 >= Q >= 0 if _IQsqrt Tables in 1-wait ROM)
;;
;;===========================================================================
;;#############################################################################
;;!
;;! Copyright: Copyright (C) 2023 Texas Instruments Incorporated -
;;! All rights reserved not granted herein.
;;! Limited License.
;;!
;;! Texas Instruments Incorporated grants a world-wide, royalty-free,
;;! non-exclusive license under copyrights and patents it now or hereafter
;;! owns or controls to make, have made, use, import, offer to sell and sell
;;! ("Utilize") this software subject to the terms herein. With respect to the
;;! foregoing patent license, such license is granted solely to the extent that
;;! any such patent is necessary to Utilize the software alone. The patent
;;! license shall not apply to any combinations which include this software,
;;! other than combinations with devices manufactured by or for TI
;;! ("TI Devices").
;;! No hardware patent is licensed hereunder.
;;!
;;! Redistributions must preserve existing copyright notices and reproduce this
;;! license (including the above copyright notice and the disclaimer and
;;! (if applicable) source code license limitations below) in the documentation
;;! and/or other materials provided with the distribution.
;;!
;;! Redistribution and use in binary form, without modification, are permitted
;;! provided that the following conditions are met:
;;!
;;! * No reverse engineering, decompilation, or disassembly of this software is
;;! permitted with respect to any software provided in binary form.
;;! * Any redistribution and use are licensed by TI for use only
;;! with TI Devices.
;;! * Nothing shall obligate TI to provide you with source code for the
;;! software licensed and provided to you in object code.
;;!
;;! If software source code is provided to you, modification and redistribution
;;! of the source code are permitted provided that the following conditions
;;! are met:
;;!
;;! * any redistribution and use of the source code, including any resulting
;;! derivative works, are licensed by TI for use only with TI Devices.
;;! * any redistribution and use of any object code compiled from the source
;;! code and any resulting derivative works, are licensed by TI for use
;;! only with TI Devices.
;;!
;;! Neither the name of Texas Instruments Incorporated nor the names of its
;;! suppliers may be used to endorse or promote products derived from this
;;! software without specific prior written permission.
;;#############################################################################
IQNsqrt .macro q_value
CSB ACC ; Count sign bits, T = n
LSLL ACC,T ; ACC = V/2 (Q32)
MOVL XAR6,@ACC ; XAR6 = V/2 (Q32)
ASR AH,#6
MOVB @AH,#0xFE,LEQ ; AR0 -> zero value if -ve or 0 input
SUB @AH,#254 ; Scale to table offset
MOVZ AR0,@AH ; AR0 -> index into _IQisqrtTable for
; initial estimate of Yn
TBIT @T,#0 ; TC = odd/even shift indicator
MOV AH,@T ; AH = n
LSR AH,#1 ; AH = n/2
MOVL *SP++,ACC ; SP-2 = n/2
MOVL XAR7,#_IQsqrtTable ; XAR7 = pointer to _IQisqrtTable
MOVL XAR4,*+XAR7[AR0] ; (1) XAR4 = initial Yn estimate (Q30)
MOVL XAR7,#_IQsqrtRoundSatTable
MOVL XAR5,*XAR7++ ; XAR5 = 1.5 (Q30)
.if (q_value & 0x0001) == 0 ; Q even
MOVB @AR0,#12,NTC ; AR0 -> 1/sqrt(2) if n == even (Q30)
MOVB @AR0,#10,TC ; AR0 -> 0.5 if n == odd (Q30)
.endif
.if (q_value & 0x0001) == 1 ; Q odd
MOVB @AR0,#12,TC ; AR0 -> 1/sqrt(2) if n == odd (Q30)
MOVB @AR0,#8,NTC ; AR0 -> 1.0 if n == even (Q30)
.endif
MOVL XT,@XAR4 ;(2) XT = Yn (Q30)
QMPYL ACC,XT,@XT ;(3) ACC = Yn*Yn (Q28)
MOVL XT,@XAR6 ;(4) XT = V/2 (Q32)
LSL ACC,#2 ;(5) ACC = Yn*Yn (Q30)
QMPYL ACC,XT,@ACC ;(6) ACC = Yn*Yn*V/2 (Q30)
MOVL XT,@XAR5 ;(7) XT = 1.5 (Q30)
SUBL @XT,ACC ;(8) XT = 1.5 - Yn*Yn*V/2 (Q30)
QMPYL ACC,XT,@XAR4 ;(9) ACC = Yn(1.5 - Yn*Yn*V/2) (Q28)
LSL ACC,#2 ;(10) ACC = Yn(1.5 - Yn*Yn*V/2) (Q30)
MOVL XAR4,@ACC ;(1) XAR4 = Yn (Q30)
MOVL XT,@XAR4 ;(2) XT = Yn (Q30)
QMPYL ACC,XT,@XT ;(3) ACC = Yn*Yn (Q28)
MOVL XT,@XAR6 ;(4) XT = V/2 (Q32)
LSL ACC,#2 ;(5) ACC = Yn*Yn (Q30)
QMPYL ACC,XT,@ACC ;(6) ACC = Yn*Yn*V/2 (Q30)
MOVL XT,@XAR5 ;(7) XT = 1.5 (Q30)
SUBL @XT,ACC ;(8) XT = 1.5 - Yn*Yn*V/2 (Q30)
QMPYL ACC,XT,@XAR4 ;(9) ACC = Yn(1.5 - Yn*Yn*V/2) (Q28)
LSL ACC,#2 ;(10) ACC = Yn(1.5 - Yn*Yn*V/2) (Q30)
MOVL XT,@XAR6
QMPYL ACC,XT,@ACC ; sqrt(X) = X * 1/sqrt(X)
MOVL XT,*+XAR7[AR0] ; Scale with 1.0 or 1/sqrt(X) or 0.5
IMPYL P,XT,@ACC
QMPYL ACC,XT,@ACC
.if q_value >= 24 ; Scale final result based on Q value
LSL64 ACC:P,#((q_value - 22)/2)
.endif
.if q_value <= 21
ASR64 ACC:P,#((23 - q_value)/2)
.endif
MOVL XT,*--SP ; T = n/2
ASR64 ACC:P,T ; Scale final result based on n/2
ADD @PH,#-32768 ; Round final result
ADDCL ACC,*+XAR7[2]
LRETR
.endm
;; The value "GLOBAL_Q" needs to be supplied by the assembler
;; using the "-dGLOBAL_Q=q_value" directive:
.sect "IQmath"
.ref _IQsqrtTable
.ref _IQsqrtRoundSatTable
.if GLOBAL_Q == 30
.def __IQ30sqrt
__IQ30sqrt:
IQNsqrt GLOBAL_Q
.endif
.if GLOBAL_Q == 29
.def __IQ29sqrt
__IQ29sqrt:
IQNsqrt GLOBAL_Q
.endif
.if GLOBAL_Q == 28
.def __IQ28sqrt
__IQ28sqrt:
IQNsqrt GLOBAL_Q
.endif
.if GLOBAL_Q == 27
.def __IQ27sqrt
__IQ27sqrt:
IQNsqrt GLOBAL_Q
.endif
.if GLOBAL_Q == 26
.def __IQ26sqrt
__IQ26sqrt:
IQNsqrt GLOBAL_Q
.endif
.if GLOBAL_Q == 25
.def __IQ25sqrt
__IQ25sqrt:
IQNsqrt GLOBAL_Q
.endif
.if GLOBAL_Q == 24
.def __IQ24sqrt
__IQ24sqrt:
IQNsqrt GLOBAL_Q
.endif
.if GLOBAL_Q == 23
.def __IQ23sqrt
__IQ23sqrt:
IQNsqrt GLOBAL_Q
.endif
.if GLOBAL_Q == 22
.def __IQ22sqrt
__IQ22sqrt:
IQNsqrt GLOBAL_Q
.endif
.if GLOBAL_Q == 21
.def __IQ21sqrt
__IQ21sqrt:
IQNsqrt GLOBAL_Q
.endif
.if GLOBAL_Q == 20
.def __IQ20sqrt
__IQ20sqrt:
IQNsqrt GLOBAL_Q
.endif
.if GLOBAL_Q == 19
.def __IQ19sqrt
__IQ19sqrt:
IQNsqrt GLOBAL_Q
.endif
.if GLOBAL_Q == 18
.def __IQ18sqrt
__IQ18sqrt:
IQNsqrt GLOBAL_Q
.endif
.if GLOBAL_Q == 17
.def __IQ17sqrt
__IQ17sqrt:
IQNsqrt GLOBAL_Q
.endif
.if GLOBAL_Q == 16
.def __IQ16sqrt
__IQ16sqrt:
IQNsqrt GLOBAL_Q
.endif
.if GLOBAL_Q == 15
.def __IQ15sqrt
__IQ15sqrt:
IQNsqrt GLOBAL_Q
.endif
.if GLOBAL_Q == 14
.def __IQ14sqrt
__IQ14sqrt:
IQNsqrt GLOBAL_Q
.endif
.if GLOBAL_Q == 13
.def __IQ13sqrt
__IQ13sqrt:
IQNsqrt GLOBAL_Q
.endif
.if GLOBAL_Q == 12
.def __IQ12sqrt
__IQ12sqrt:
IQNsqrt GLOBAL_Q
.endif
.if GLOBAL_Q == 11
.def __IQ11sqrt
__IQ11sqrt:
IQNsqrt GLOBAL_Q
.endif
.if GLOBAL_Q == 10
.def __IQ10sqrt
__IQ10sqrt:
IQNsqrt GLOBAL_Q
.endif
.if GLOBAL_Q == 9
.def __IQ9sqrt
__IQ9sqrt:
IQNsqrt GLOBAL_Q
.endif
.if GLOBAL_Q == 8
.def __IQ8sqrt
__IQ8sqrt:
IQNsqrt GLOBAL_Q
.endif
.if GLOBAL_Q == 7
.def __IQ7sqrt
__IQ7sqrt:
IQNsqrt GLOBAL_Q
.endif
.if GLOBAL_Q == 6
.def __IQ6sqrt
__IQ6sqrt:
IQNsqrt GLOBAL_Q
.endif
.if GLOBAL_Q == 5
.def __IQ5sqrt
__IQ5sqrt:
IQNsqrt GLOBAL_Q
.endif
.if GLOBAL_Q == 4
.def __IQ4sqrt
__IQ4sqrt:
IQNsqrt GLOBAL_Q
.endif
.if GLOBAL_Q == 3
.def __IQ3sqrt
__IQ3sqrt:
IQNsqrt GLOBAL_Q
.endif
.if GLOBAL_Q == 2
.def __IQ2sqrt
__IQ2sqrt:
IQNsqrt GLOBAL_Q
.endif
.if GLOBAL_Q == 1
.def __IQ1sqrt
__IQ1sqrt:
IQNsqrt GLOBAL_Q
.endif
;;###########################################################################
;; No More.
;;###########################################################################
@@ -0,0 +1,306 @@
;;###########################################################################
;;
;; FILE: IQNtoF.asm
;;
;; TITLE: C Callable IQ to Float Math Function
;;
;;###########################################################################
;;
;; Ver | Date | Who | Description of changes
;; =====|=============|=======|==============================================
;; 1.4 | 17 May 2002 | A. T. | Original Release.
;; -----|-------------|-------|----------------------------------------------
;; | | |
;;
;;###########################################################################
;;===========================================================================
;; Function: _IQNtoF
;;===========================================================================
;;
;; C Usage: extern float _IQtoF(long A); // no round or sat
;;
;;---------------------------------------------------------------------------
;;
;; On Entry: ACC = A in IQ format
;;
;; Regs Used: XT, P
;;
;; On Exit: ACC = IEEE 754 floating-point equivalent of A
;;
;; Q range: 30 to 1
;;
;;---------------------------------------------------------------------------
;; Algorithm: This operation converts an IQ number to the equivalent
;; IEEE 754 Single-Precision floating-point format. This
;; format is shown below:
;;
;; 31 30 23 22 0
;; +-----------------------------------------------------+
;; | s | e | f |
;; +-----------------------------------------------------+
;;
;; Value = (-1)^s * 2^(e-127) * 1.f
;;
;; where: e = 1 to 254, f = 0.000000000 to ~1.0
;; e = 0, f = 0, s = 0, Value = 0.0
;; e = 0 and f != 0 case cannot occur in IQ math
;; e = 255 case cannot occur in IQ math
;;
;;---------------------------------------------------------------------------
;; Benchmark:
;;
;; Cycles = 22 (includes LCR/LRETR)
;;
;;---------------------------------------------------------------------------
;;#############################################################################
;;!
;;! Copyright: Copyright (C) 2023 Texas Instruments Incorporated -
;;! All rights reserved not granted herein.
;;! Limited License.
;;!
;;! Texas Instruments Incorporated grants a world-wide, royalty-free,
;;! non-exclusive license under copyrights and patents it now or hereafter
;;! owns or controls to make, have made, use, import, offer to sell and sell
;;! ("Utilize") this software subject to the terms herein. With respect to the
;;! foregoing patent license, such license is granted solely to the extent that
;;! any such patent is necessary to Utilize the software alone. The patent
;;! license shall not apply to any combinations which include this software,
;;! other than combinations with devices manufactured by or for TI
;;! ("TI Devices").
;;! No hardware patent is licensed hereunder.
;;!
;;! Redistributions must preserve existing copyright notices and reproduce this
;;! license (including the above copyright notice and the disclaimer and
;;! (if applicable) source code license limitations below) in the documentation
;;! and/or other materials provided with the distribution.
;;!
;;! Redistribution and use in binary form, without modification, are permitted
;;! provided that the following conditions are met:
;;!
;;! * No reverse engineering, decompilation, or disassembly of this software is
;;! permitted with respect to any software provided in binary form.
;;! * Any redistribution and use are licensed by TI for use only
;;! with TI Devices.
;;! * Nothing shall obligate TI to provide you with source code for the
;;! software licensed and provided to you in object code.
;;!
;;! If software source code is provided to you, modification and redistribution
;;! of the source code are permitted provided that the following conditions
;;! are met:
;;!
;;! * any redistribution and use of the source code, including any resulting
;;! derivative works, are licensed by TI for use only with TI Devices.
;;! * any redistribution and use of any object code compiled from the source
;;! code and any resulting derivative works, are licensed by TI for use
;;! only with TI Devices.
;;!
;;! Neither the name of Texas Instruments Incorporated nor the names of its
;;! suppliers may be used to endorse or promote products derived from this
;;! software without specific prior written permission.
;;#############################################################################
IQNtoF .macro q_value
CSB ACC
LSLL ACC,T
MOVL P,@ACC
XOR @PL,#0xFFFF
XOR @PH,#0x7FFF
MOVL @P,ACC,GEQ
MOVB AH,#(127 + (30-q_value))
SUBR @T,AH
MOVL ACC,@P
MOVB @T,#0,EQ
ASR64 ACC:P,#14
AND @AH,#0xFF00
ADD AH,@T
LSL64 ACC:P,#7
LRETR
.endm
;; The value "GLOBAL_Q" needs to be supplied by the assembler
;; using the "-dGLOBAL_Q=q_value" directive:
.sect "IQmath"
.if GLOBAL_Q == 30
.def __IQ30toF
__IQ30toF:
IQNtoF GLOBAL_Q
.endif
.if GLOBAL_Q == 29
.def __IQ29toF
__IQ29toF:
IQNtoF GLOBAL_Q
.endif
.if GLOBAL_Q == 28
.def __IQ28toF
__IQ28toF:
IQNtoF GLOBAL_Q
.endif
.if GLOBAL_Q == 27
.def __IQ27toF
__IQ27toF:
IQNtoF GLOBAL_Q
.endif
.if GLOBAL_Q == 26
.def __IQ26toF
__IQ26toF:
IQNtoF GLOBAL_Q
.endif
.if GLOBAL_Q == 25
.def __IQ25toF
__IQ25toF:
IQNtoF GLOBAL_Q
.endif
.if GLOBAL_Q == 24
.def __IQ24toF
__IQ24toF:
IQNtoF GLOBAL_Q
.endif
.if GLOBAL_Q == 23
.def __IQ23toF
__IQ23toF:
IQNtoF GLOBAL_Q
.endif
.if GLOBAL_Q == 22
.def __IQ22toF
__IQ22toF:
IQNtoF GLOBAL_Q
.endif
.if GLOBAL_Q == 21
.def __IQ21toF
__IQ21toF:
IQNtoF GLOBAL_Q
.endif
.if GLOBAL_Q == 20
.def __IQ20toF
__IQ20toF:
IQNtoF GLOBAL_Q
.endif
.if GLOBAL_Q == 19
.def __IQ19toF
__IQ19toF:
IQNtoF GLOBAL_Q
.endif
.if GLOBAL_Q == 18
.def __IQ18toF
__IQ18toF:
IQNtoF GLOBAL_Q
.endif
.if GLOBAL_Q == 17
.def __IQ17toF
__IQ17toF:
IQNtoF GLOBAL_Q
.endif
.if GLOBAL_Q == 16
.def __IQ16toF
__IQ16toF:
IQNtoF GLOBAL_Q
.endif
.if GLOBAL_Q == 15
.def __IQ15toF
__IQ15toF:
IQNtoF GLOBAL_Q
.endif
.if GLOBAL_Q == 14
.def __IQ14toF
__IQ14toF:
IQNtoF GLOBAL_Q
.endif
.if GLOBAL_Q == 13
.def __IQ13toF
__IQ13toF:
IQNtoF GLOBAL_Q
.endif
.if GLOBAL_Q == 12
.def __IQ12toF
__IQ12toF:
IQNtoF GLOBAL_Q
.endif
.if GLOBAL_Q == 11
.def __IQ11toF
__IQ11toF:
IQNtoF GLOBAL_Q
.endif
.if GLOBAL_Q == 10
.def __IQ10toF
__IQ10toF:
IQNtoF GLOBAL_Q
.endif
.if GLOBAL_Q == 9
.def __IQ9toF
__IQ9toF:
IQNtoF GLOBAL_Q
.endif
.if GLOBAL_Q == 8
.def __IQ8toF
__IQ8toF:
IQNtoF GLOBAL_Q
.endif
.if GLOBAL_Q == 7
.def __IQ7toF
__IQ7toF:
IQNtoF GLOBAL_Q
.endif
.if GLOBAL_Q == 6
.def __IQ6toF
__IQ6toF:
IQNtoF GLOBAL_Q
.endif
.if GLOBAL_Q == 5
.def __IQ5toF
__IQ5toF:
IQNtoF GLOBAL_Q
.endif
.if GLOBAL_Q == 4
.def __IQ4toF
__IQ4toF:
IQNtoF GLOBAL_Q
.endif
.if GLOBAL_Q == 3
.def __IQ3toF
__IQ3toF:
IQNtoF GLOBAL_Q
.endif
.if GLOBAL_Q == 2
.def __IQ2toF
__IQ2toF:
IQNtoF GLOBAL_Q
.endif
.if GLOBAL_Q == 1
.def __IQ1toF
__IQ1toF:
IQNtoF GLOBAL_Q
.endif
;;###########################################################################
;; No More.
;;###########################################################################
@@ -0,0 +1,307 @@
;;###########################################################################
;;
;; FILE: IQNtoF_fpu32.asm
;;
;; TITLE: C Callable IQ to Float Math Function
;;
;;###########################################################################
;;
;; Ver | Date | Who | Description of changes
;; =====|=============|=======|==============================================
;; 1.5 | 01 May 2008 | L. H | Original Release.
;; -----|-------------|-------|----------------------------------------------
;; | | |
;;
;;###########################################################################
;;===========================================================================
;; Function: _IQNtoF
;;===========================================================================
;;
;; C Usage: extern float _IQtoF(long A); // no round or sat
;;
;;---------------------------------------------------------------------------
;;
;; On Entry: ACC = A in IQ format
;;
;; Regs Used: XT, P
;;
;; On Exit: ACC = IEEE 754 floating-point equivalent of A
;;
;; Q range: 30 to 1
;;
;;---------------------------------------------------------------------------
;; Algorithm: This operation converts an IQ number to the equivalent
;; IEEE 754 Single-Precision floating-point format. This
;; format is shown below:
;;
;; 31 30 23 22 0
;; +-----------------------------------------------------+
;; | s | e | f |
;; +-----------------------------------------------------+
;;
;; Value = (-1)^s * 2^(e-127) * 1.f
;;
;; where: e = 1 to 254, f = 0.000000000 to ~1.0
;; e = 0, f = 0, s = 0, Value = 0.0
;; e = 0 and f != 0 case cannot occur in IQ math
;; e = 255 case cannot occur in IQ math
;;
;;---------------------------------------------------------------------------
;; Benchmark:
;;
;; Cycles = 22 (includes LCR/LRETR)
;;
;;---------------------------------------------------------------------------
;;#############################################################################
;;!
;;! Copyright: Copyright (C) 2023 Texas Instruments Incorporated -
;;! All rights reserved not granted herein.
;;! Limited License.
;;!
;;! Texas Instruments Incorporated grants a world-wide, royalty-free,
;;! non-exclusive license under copyrights and patents it now or hereafter
;;! owns or controls to make, have made, use, import, offer to sell and sell
;;! ("Utilize") this software subject to the terms herein. With respect to the
;;! foregoing patent license, such license is granted solely to the extent that
;;! any such patent is necessary to Utilize the software alone. The patent
;;! license shall not apply to any combinations which include this software,
;;! other than combinations with devices manufactured by or for TI
;;! ("TI Devices").
;;! No hardware patent is licensed hereunder.
;;!
;;! Redistributions must preserve existing copyright notices and reproduce this
;;! license (including the above copyright notice and the disclaimer and
;;! (if applicable) source code license limitations below) in the documentation
;;! and/or other materials provided with the distribution.
;;!
;;! Redistribution and use in binary form, without modification, are permitted
;;! provided that the following conditions are met:
;;!
;;! * No reverse engineering, decompilation, or disassembly of this software is
;;! permitted with respect to any software provided in binary form.
;;! * Any redistribution and use are licensed by TI for use only
;;! with TI Devices.
;;! * Nothing shall obligate TI to provide you with source code for the
;;! software licensed and provided to you in object code.
;;!
;;! If software source code is provided to you, modification and redistribution
;;! of the source code are permitted provided that the following conditions
;;! are met:
;;!
;;! * any redistribution and use of the source code, including any resulting
;;! derivative works, are licensed by TI for use only with TI Devices.
;;! * any redistribution and use of any object code compiled from the source
;;! code and any resulting derivative works, are licensed by TI for use
;;! only with TI Devices.
;;!
;;! Neither the name of Texas Instruments Incorporated nor the names of its
;;! suppliers may be used to endorse or promote products derived from this
;;! software without specific prior written permission.
;;#############################################################################
IQNtoF .macro q_value
CSB ACC
LSLL ACC,T
MOVL P,@ACC
XOR @PL,#0xFFFF
XOR @PH,#0x7FFF
MOVL @P,ACC,GEQ
MOVB AH,#(127 + (30-q_value))
SUBR @T,AH
MOVL ACC,@P
MOVB @T,#0,EQ
ASR64 ACC:P,#14
AND @AH,#0xFF00
ADD AH,@T
LSL64 ACC:P,#7
MOV32 R0H, ACC
LRETR
.endm
;; The value "GLOBAL_Q" needs to be supplied by the assembler
;; using the "-dGLOBAL_Q=q_value" directive:
.sect "IQmath"
.if GLOBAL_Q == 30
.def __IQ30toF
__IQ30toF:
IQNtoF GLOBAL_Q
.endif
.if GLOBAL_Q == 29
.def __IQ29toF
__IQ29toF:
IQNtoF GLOBAL_Q
.endif
.if GLOBAL_Q == 28
.def __IQ28toF
__IQ28toF:
IQNtoF GLOBAL_Q
.endif
.if GLOBAL_Q == 27
.def __IQ27toF
__IQ27toF:
IQNtoF GLOBAL_Q
.endif
.if GLOBAL_Q == 26
.def __IQ26toF
__IQ26toF:
IQNtoF GLOBAL_Q
.endif
.if GLOBAL_Q == 25
.def __IQ25toF
__IQ25toF:
IQNtoF GLOBAL_Q
.endif
.if GLOBAL_Q == 24
.def __IQ24toF
__IQ24toF:
IQNtoF GLOBAL_Q
.endif
.if GLOBAL_Q == 23
.def __IQ23toF
__IQ23toF:
IQNtoF GLOBAL_Q
.endif
.if GLOBAL_Q == 22
.def __IQ22toF
__IQ22toF:
IQNtoF GLOBAL_Q
.endif
.if GLOBAL_Q == 21
.def __IQ21toF
__IQ21toF:
IQNtoF GLOBAL_Q
.endif
.if GLOBAL_Q == 20
.def __IQ20toF
__IQ20toF:
IQNtoF GLOBAL_Q
.endif
.if GLOBAL_Q == 19
.def __IQ19toF
__IQ19toF:
IQNtoF GLOBAL_Q
.endif
.if GLOBAL_Q == 18
.def __IQ18toF
__IQ18toF:
IQNtoF GLOBAL_Q
.endif
.if GLOBAL_Q == 17
.def __IQ17toF
__IQ17toF:
IQNtoF GLOBAL_Q
.endif
.if GLOBAL_Q == 16
.def __IQ16toF
__IQ16toF:
IQNtoF GLOBAL_Q
.endif
.if GLOBAL_Q == 15
.def __IQ15toF
__IQ15toF:
IQNtoF GLOBAL_Q
.endif
.if GLOBAL_Q == 14
.def __IQ14toF
__IQ14toF:
IQNtoF GLOBAL_Q
.endif
.if GLOBAL_Q == 13
.def __IQ13toF
__IQ13toF:
IQNtoF GLOBAL_Q
.endif
.if GLOBAL_Q == 12
.def __IQ12toF
__IQ12toF:
IQNtoF GLOBAL_Q
.endif
.if GLOBAL_Q == 11
.def __IQ11toF
__IQ11toF:
IQNtoF GLOBAL_Q
.endif
.if GLOBAL_Q == 10
.def __IQ10toF
__IQ10toF:
IQNtoF GLOBAL_Q
.endif
.if GLOBAL_Q == 9
.def __IQ9toF
__IQ9toF:
IQNtoF GLOBAL_Q
.endif
.if GLOBAL_Q == 8
.def __IQ8toF
__IQ8toF:
IQNtoF GLOBAL_Q
.endif
.if GLOBAL_Q == 7
.def __IQ7toF
__IQ7toF:
IQNtoF GLOBAL_Q
.endif
.if GLOBAL_Q == 6
.def __IQ6toF
__IQ6toF:
IQNtoF GLOBAL_Q
.endif
.if GLOBAL_Q == 5
.def __IQ5toF
__IQ5toF:
IQNtoF GLOBAL_Q
.endif
.if GLOBAL_Q == 4
.def __IQ4toF
__IQ4toF:
IQNtoF GLOBAL_Q
.endif
.if GLOBAL_Q == 3
.def __IQ3toF
__IQ3toF:
IQNtoF GLOBAL_Q
.endif
.if GLOBAL_Q == 2
.def __IQ2toF
__IQ2toF:
IQNtoF GLOBAL_Q
.endif
.if GLOBAL_Q == 1
.def __IQ1toF
__IQ1toF:
IQNtoF GLOBAL_Q
.endif
;;###########################################################################
;; No More.
;;###########################################################################
@@ -0,0 +1,251 @@
/**********************************************************************
* Function: IQNtoa()
*
* Description: Converts IQ number to an ASCII string
*
* DSP: TMS320F28x
*
* Author: DA
*
* Include files: stdio.h
*
* Function Prototype: int __IQNtoa(char *, const char *, long, int)
*
* Usage: error = __IQNtoa(char *string, const char *format,
* long x, int qvalue);
*
* Input Parameters:
* string = output string
* format = conversion format. Must be of form "%xx.yyf" with
* xx and yy at most 2 characters in length.
* Examples: "%10.12f", "%2.4f", "%11.6f"
* Maximum supported integer field width (xx) is 11
* (including any negative sign). This captures the full
* integer range for I2Q30 to I31Q1 numbers.
* x = input value (IQ format)
* qvalue = IQ format of x (e.g., 24 means I8Q24)
*
* Return Value:
* error = error code:
* 0 = no error
* 1 = iwidth too small to hold integer characters
* 2 = illegal format specified
*
* Notes:
*
* 1) Leading zeros are not printed for the integer part. Hence, format
* specifies the maximum width of the integer field. The field may
* be smaller.
* 2) The output string is terminated with the null character.
* 3) The integer width in "format" includes the negative sign for
* negative numbers, e.g. -12.3456 is "%3.5f"
* 4) The decimal width in "format" includes the decimal point, e.g.
* -12.3456 is "%3.5f"
* 5) "string" must be large enough to hold the output (including the
* negative sign, and the terminating null character). The program
* does not check for overrun. Memory corruption will occur if
* "string" is too small.
* 6) A non-zero return value indicates that the output string is invalid.
* 7) Benchmark is roughly 200 to 800 cycles, depending on the format and
* the input value.
*
* History: December 23, 2004 - original (D. Alter)
* : April 27, 2019 - fixed bug with not able to handle value 0x80000000 (Arav)
**********************************************************************/
//#############################################################################
//!
//! Copyright: Copyright (C) 2023 Texas Instruments Incorporated -
//! All rights reserved not granted herein.
//! Limited License.
//!
//! Texas Instruments Incorporated grants a world-wide, royalty-free,
//! non-exclusive license under copyrights and patents it now or hereafter
//! owns or controls to make, have made, use, import, offer to sell and sell
//! ("Utilize") this software subject to the terms herein. With respect to the
//! foregoing patent license, such license is granted solely to the extent that
//! any such patent is necessary to Utilize the software alone. The patent
//! license shall not apply to any combinations which include this software,
//! other than combinations with devices manufactured by or for TI
//! ("TI Devices").
//! No hardware patent is licensed hereunder.
//!
//! Redistributions must preserve existing copyright notices and reproduce this
//! license (including the above copyright notice and the disclaimer and
//! (if applicable) source code license limitations below) in the documentation
//! and/or other materials provided with the distribution.
//!
//! Redistribution and use in binary form, without modification, are permitted
//! provided that the following conditions are met:
//!
//! * No reverse engineering, decompilation, or disassembly of this software is
//! permitted with respect to any software provided in binary form.
//! * Any redistribution and use are licensed by TI for use only
//! with TI Devices.
//! * Nothing shall obligate TI to provide you with source code for the
//! software licensed and provided to you in object code.
//!
//! If software source code is provided to you, modification and redistribution
//! of the source code are permitted provided that the following conditions
//! are met:
//!
//! * any redistribution and use of the source code, including any resulting
//! derivative works, are licensed by TI for use only with TI Devices.
//! * any redistribution and use of any object code compiled from the source
//! code and any resulting derivative works, are licensed by TI for use
//! only with TI Devices.
//!
//! Neither the name of Texas Instruments Incorporated nor the names of its
//! suppliers may be used to endorse or promote products derived from this
//! software without specific prior written permission.
//#############################################################################
// Function prototypes
extern char _IQNtoa_get_frac_c(unsigned long);
/**********************************************************************/
#pragma CODE_SECTION(__IQNtoa, "IQmath");
int __IQNtoa(char *string, const char *format, long x, int qvalue)
{
unsigned long uval; // working variable
char *bufptr; // buffer pointer
char buf[11]; // temporary char buffer (worst case length)
int count; // conversion character counter
int error = 0; // error value
int neg = 0; // negative flag
int iwidth; // integer format width
int dwidth; // decimal format width
int scale; // format parsing scaling value
//--------------------------------------------------------------------
// Parse the format parameter
//
// Check if x == 0x80000000 and return error if yes
if(x == 0x80000000) return(2); // error: cannot handle 0x80000000
// Check that 1st character is "%"
if(*format++ != '%') return(2); // error: missing the leading '%'
// Extract iwidth
if(*format == '.') return(2); // error: no iwidth field
count = 0; // zero the counter
scale = 1; // init scale
iwidth = 0; // init iwidth
while(*format != '.') // find the '.'
{
count++; // increment the count
iwidth = iwidth*scale + (*format++ - '0');
scale = scale * 10;
// If we don't find the '.' after, say, 4 counts, something is wrong
if(count > 2) return(2); // error: iwidth field too many characters
}
if(iwidth > 11) return(2); // error: iwidth too large
if(iwidth < 0) return(2); // error: iwidth negative
// Extract dwidth
format++; // advance format off the '.'
if(*format == 0) return(2); // error: no dwidth field
count = 0; // zero the counter
scale = 1; // init scale
dwidth = 0; // init dwidth
while((*format != 'f') && (*format != 'F')) // find the conversion character
{
count++; // increment the count
dwidth = dwidth*scale + (*format++ - '0');
scale = scale * 10;
// If we don't find the conversion character after, say, 4 counts, something is wrong
if(count > 2) return(2); // error: dwidth field too large
}
if(dwidth < 0) return(2); // error: dwidth negative
// Check that the next character is the NULL string terminator
if(*++format != 0) return(2); // error: missing null terminator
//--------------------------------------------------------------------
// Check for negative value
//
if(x < 0)
{
x = -x; // make it positive
neg = 1; // set negative flag
}
//--------------------------------------------------------------------
// Process the integer part
//
// Isolate the integer part
uval = x >> qvalue;
// Convert the integer part
bufptr = buf; // init the pointer
count = 0; // zero the counter
do
{
*bufptr++ = (uval % 10) + '0'; // extract the next character
count++; // increment the counter
} while(uval = uval/10); // remove the character value from uval
if(neg)
{
*bufptr++ = '-'; // insert a '-'
count++; // increment the counter
}
if(count > iwidth)
{
count = iwidth; // set count = iwidth
error = 1; // error: integer format too small
}
while(count--)
{
*string++ = *--bufptr; // copy the character to the string
}
//--------------------------------------------------------------------
// Process the fractional part
//
if(dwidth > 0)
{
// Insert the decimal point
*string++ = '.'; // insert the decimal point
dwidth--; // decrement the fraction length
// Isolate the fractional part
uval = x << (32-qvalue); // I0Q32 format
// Convert the fractional part
for(count = 0; count < dwidth; count++)
{
*string++ = _IQNtoa_get_frac_c(uval) + '0'; // extract the character
uval = uval * 10; // remove the character value from uval
}
} // end if
//--------------------------------------------------------------------
// Finish up
//
*string = 0; // add the NUL character to terminate string
return(error); // return
} // end of __IQNtoa()
/*** end of file *****************************************************/
File diff suppressed because it is too large. Load diff
@@ -0,0 +1,263 @@
;;###########################################################################
;;
;; FILE: atoIQN.asm
;;
;; TITLE: C Callable IQ Math Function
;;
;;###########################################################################
;;
;; Ver | Date | Who | Description of changes
;; =====|=============|=======|==============================================
;; 1.2 | 11 Oct 2001 | A. T. | Original Release.
;; -----|-------------|-------|----------------------------------------------
;; 1.3 | 19 Nov 2001 | A. T. | No changes.
;; -----|-------------|-------|----------------------------------------------
;; 1.4 | 17 May 2002 | A. T. | Fixed bug (thanks Settu) with input char
;; | | | range comparison.
;; -----|-------------|-------|----------------------------------------------
;; | | |
;;
;;###########################################################################
;;===========================================================================
;; Function: _atoIQN
;;===========================================================================
;;
;; C Usage: extern long _atoIQN(const char *st, long q_value);
;;
;;---------------------------------------------------------------------------
;;
;; On Entry: XAR4 = pointer to character string
;; ACC = q_value
;;
;; Regs Used: ACC, P, XT
;; XAR7, XAR6, XAR5
;;
;; On Exit: ACC = IQ value from string (0 if an error occured)
;;
;; Q Range: 30 to 1
;;
;;---------------------------------------------------------------------------
;; Algorithm: The input string must only contain decimal characters
;; '0' to '9' and the characters '-' for negative value
;; if applicable and '.' character for delimiting integer and
;; fraction components. Some examples of valid inputs are:
;;
;; 12.23456
;; -12.23456
;; 0.2345
;; 0.0
;; 0
;; 127
;; -89
;;
;; If the input string converts to a number greater then
;; the max/min values for the given Q value, then the returned
;; value will be limited to the min/max values.
;;
;; If the string contains illegal characters, it will return
;; the value zero.
;;
;;---------------------------------------------------------------------------
;; Benchmark:
;; Cycles = Can be approximately from 200 to 800 cycles based on number
;; of characters in string.
;;
;;===========================================================================
;;#############################################################################
;;!
;;! Copyright: Copyright (C) 2023 Texas Instruments Incorporated -
;;! All rights reserved not granted herein.
;;! Limited License.
;;!
;;! Texas Instruments Incorporated grants a world-wide, royalty-free,
;;! non-exclusive license under copyrights and patents it now or hereafter
;;! owns or controls to make, have made, use, import, offer to sell and sell
;;! ("Utilize") this software subject to the terms herein. With respect to the
;;! foregoing patent license, such license is granted solely to the extent that
;;! any such patent is necessary to Utilize the software alone. The patent
;;! license shall not apply to any combinations which include this software,
;;! other than combinations with devices manufactured by or for TI
;;! ("TI Devices").
;;! No hardware patent is licensed hereunder.
;;!
;;! Redistributions must preserve existing copyright notices and reproduce this
;;! license (including the above copyright notice and the disclaimer and
;;! (if applicable) source code license limitations below) in the documentation
;;! and/or other materials provided with the distribution.
;;!
;;! Redistribution and use in binary form, without modification, are permitted
;;! provided that the following conditions are met:
;;!
;;! * No reverse engineering, decompilation, or disassembly of this software is
;;! permitted with respect to any software provided in binary form.
;;! * Any redistribution and use are licensed by TI for use only
;;! with TI Devices.
;;! * Nothing shall obligate TI to provide you with source code for the
;;! software licensed and provided to you in object code.
;;!
;;! If software source code is provided to you, modification and redistribution
;;! of the source code are permitted provided that the following conditions
;;! are met:
;;!
;;! * any redistribution and use of the source code, including any resulting
;;! derivative works, are licensed by TI for use only with TI Devices.
;;! * any redistribution and use of any object code compiled from the source
;;! code and any resulting derivative works, are licensed by TI for use
;;! only with TI Devices.
;;!
;;! Neither the name of Texas Instruments Incorporated nor the names of its
;;! suppliers may be used to endorse or promote products derived from this
;;! software without specific prior written permission.
;;#############################################################################
.sect "IQmath"
.def __atoIQN
__atoIQN:
NEG ACC ; ACC = - q_value
ADDB ACC,#32 ; ACC = 32 - q_value
MOV AH,@AL ; AH = 32 - q_value
MOVL *SP++,ACC ; Save on stack
ADDB SP,#8
CMP @AH,#31 ; Check to make sure that q_value range
SB _atoIQerror,GT ; is 30 to 1
CMP @AH,#2
SB _atoIQerror,LT
MOVB ACC,#0
MOVL *-SP[8],ACC ; I64l
MOVL *-SP[6],ACC ; I64h, I64 = integer portion
MOVL *-SP[4],ACC ; F64l
MOVL *-SP[2],ACC ; F64h, F64 = fraction portion
MOV AL,#0x999A
MOV AH,#0x1999
MOVL XAR7,@ACC ; XAR7 = 0.1 * 2^32 = scale frac
MOVB XAR6,#10 ; XAR6 = 10 = scale int
ZAPA ; ACC, P = 0
CLRC TC ; TC = 0 = sign flag (default +ve)
_atoIQloop:
MOVZ AR0,*XAR4++ ; AR0 = char
CMP @AR0,#0x00
SB _atoIQend,EQ ; Exit if zero (end of string)
CMP @AR0,#'-'
SB _atoIQneg,EQ ; Set sign flag if char == "-"
CMP @AR0,#'.'
SB _atoIQdot,EQ ; End of integer component if char == "."
SUB @AR0,#'0'
SB _atoIQerror,LT
CMP @AR0,#9
SB _atoIQerror,GT ; Error if char not between "0".."9"
MOVL XT,@XAR6
IMPYXUL P,XT,*-SP[8]
ADDUL P,@XAR0
MOVL ACC,@P
QMPYXUL P,XT,*-SP[8]
MOVL *-SP[8],ACC
IMPYL ACC,XT,*-SP[6]
ADDCL ACC,@P
MOVL *-SP[6],ACC ; I64 = I64*10 + char dec value
SB _atoIQloop,UNC
;-------------------------------------------------------------------------
; Search for end of string and count frac characters:
_atoIQdot:
MOVB XAR5,#0 ; clear frac character counter
_atoIQdotloop:
INC @AR5 ; Increment character counter
MOVZ AR0,*XAR4++ ; AR0 = char
CMP @AR0,#0x00
SB _atoIQfrac,EQ ; Exit if end of string
SUB @AR0,#'0'
SB _atoIQerror,LT
CMP @AR0,#9
SB _atoIQerror,GT ; Error if char not between "0".."9"
SB _atoIQdotloop,UNC
;-------------------------------------------------------------------------
_atoIQfrac:
SUBB XAR4,#1
CMP @AR5,#10
SB _atoIQfracloop,LT
SUBB XAR5,#10
MOVL ACC,@XAR4
SUBL ACC,@XAR5
MOVL @XAR4,ACC
_atoIQfracloop:
MOVZ AR0,*--XAR4 ; AR0 = char
CMP @AR0,#'.'
SB _atoIQend,EQ ; Exit if char == "."
SUB @AR0,#'0'
SB _atoIQerror,LT
CMP @AR0,#9
SB _atoIQerror,GT ; Error if char not between "0".."9"
MOVL ACC,*-SP[2]
MOV AH,@AR0
MOVL *-SP[2],ACC
MOVL XT,@XAR7
QMPYXUL P,XT,*-SP[4]
IMPYL ACC,XT,*-SP[2]
ADDUL P,@ACC
MOVL *-SP[4],P
MPYB P,T,#0
QMPYL ACC,XT,*-SP[2]
ADDCL ACC,@P
MOVL *-SP[2],ACC ; F64 = F64*0.1 + char dec value
SB _atoIQfracloop,UNC
;-------------------------------------------------------------------------
_atoIQneg:
SB _atoIQerror,TC ; Error if more then one -ve sign detected
SETC TC ; Set -ve sign value
SB _atoIQloop,UNC
;-------------------------------------------------------------------------
_atoIQerror:
MOV AH,#0x0000
MOV AL,#0x0000 ; Return zero if error detected
SUBB SP,#10
LRETR
;-------------------------------------------------------------------------
_atoIQend:
MOVL P,*-SP[4]
MOVL ACC,*-SP[2]
LSR64 ACC:P,#16
MOVL ACC,*-SP[8] ; ACC = int, P =frac
MOVL XT,*-SP[10] ; T = 32 - q_value
LSR64 ACC:P,T ; P = IQ value, ACC = overflow
SB _atoIQendPos,NTC
NEG64 ACC:P
_atoIQendPos:
; Set saturation limits:
MOV *-SP[8],#0xFFFF
MOV *-SP[7],#0x7FFF
MOV *-SP[6],#0x0000
MOV *-SP[5],#0x0000
MOV *-SP[4],#0x0000
MOV *-SP[3],#0x8000
MOV *-SP[2],#0xFFFF
MOV *-SP[1],#0xFFFF
; Saturate result:
MINL ACC,*-SP[6]
MINCUL P,*-SP[8]
MAXL ACC,*-SP[2]
MAXCUL P,*-SP[4]
; Return value in ACC:
MOVL ACC,@P
SUBB SP,#10
LRETR
;;###########################################################################
;; No More.
;;###########################################################################
@@ -0,0 +1,91 @@
***********************************************************************
* Function: __IQNtoa_get_frac_c
*
* Description: Called by _IQNtoa(). Not for user use.
*
* DSP: TMS320F28x
*
* Author: David M. Alter
*
* Include files: stdio.h
*
* Function Prototype: extern char _IQNtoa_get_frac_c(unsigned long);
*
* Usage: character = _IQNtoa_get_frac_c(value);
*
* Input Parameters:
* value = fraction in I0Q32 format
*
* Return Value:
* character = the 1st base-10 digit in the fraction
*
* Notes:
* 1) This is an internal function to the IQmath library. It is not
* intended to be called by the user.
*
* History: December 23, 2004 - original (DA)
*
***********************************************************************
;;#############################################################################
;;!
;;! Copyright: Copyright (C) 2023 Texas Instruments Incorporated -
;;! All rights reserved not granted herein.
;;! Limited License.
;;!
;;! Texas Instruments Incorporated grants a world-wide, royalty-free,
;;! non-exclusive license under copyrights and patents it now or hereafter
;;! owns or controls to make, have made, use, import, offer to sell and sell
;;! ("Utilize") this software subject to the terms herein. With respect to the
;;! foregoing patent license, such license is granted solely to the extent that
;;! any such patent is necessary to Utilize the software alone. The patent
;;! license shall not apply to any combinations which include this software,
;;! other than combinations with devices manufactured by or for TI
;;! ("TI Devices").
;;! No hardware patent is licensed hereunder.
;;!
;;! Redistributions must preserve existing copyright notices and reproduce this
;;! license (including the above copyright notice and the disclaimer and
;;! (if applicable) source code license limitations below) in the documentation
;;! and/or other materials provided with the distribution.
;;!
;;! Redistribution and use in binary form, without modification, are permitted
;;! provided that the following conditions are met:
;;!
;;! * No reverse engineering, decompilation, or disassembly of this software is
;;! permitted with respect to any software provided in binary form.
;;! * Any redistribution and use are licensed by TI for use only
;;! with TI Devices.
;;! * Nothing shall obligate TI to provide you with source code for the
;;! software licensed and provided to you in object code.
;;!
;;! If software source code is provided to you, modification and redistribution
;;! of the source code are permitted provided that the following conditions
;;! are met:
;;!
;;! * any redistribution and use of the source code, including any resulting
;;! derivative works, are licensed by TI for use only with TI Devices.
;;! * any redistribution and use of any object code compiled from the source
;;! code and any resulting derivative works, are licensed by TI for use
;;! only with TI Devices.
;;!
;;! Neither the name of Texas Instruments Incorporated nor the names of its
;;! suppliers may be used to endorse or promote products derived from this
;;! software without specific prior written permission.
;;#############################################################################
.def __IQNtoa_get_frac_c
.sect "IQmath"
__IQNtoa_get_frac_c:
MOVB XAR0, #10 ;XAR0 = 10
MOVL XT, XAR0 ;XT = 10
QMPYUL P, XT, @ACC ;P = XT*ACC upper 32-bits
MOVL @ACC, P ;ACC = the value we want
LRETR ;return
.end
;----------------------------------------------------------------------
; End of file
;----------------------------------------------------------------------
@@ -0,0 +1,86 @@
//###########################################################################
//
// FILE: satf.c
//
// TITLE: C Function To Saturate A Floating-Point Value
//
//###########################################################################
//
// Ver | Date | Who | Description of changes
// =====|=============|=======|==============================================
// 1.4 | 17 May 2002 | A. T. | Original Release.
// -----|-------------|-------|----------------------------------------------
// | | |
//
//###########################################################################
//===========================================================================
// Function: _satf
//===========================================================================
//
// C Usage: extern float _satf(float A, float Pos, float Neg);
//
//---------------------------------------------------------------------------
// Algorithm: Saturates the value "A" between the values "Pos" and "Neg".
//
//---------------------------------------------------------------------------
//#############################################################################
//!
//! Copyright: Copyright (C) 2023 Texas Instruments Incorporated -
//! All rights reserved not granted herein.
//! Limited License.
//!
//! Texas Instruments Incorporated grants a world-wide, royalty-free,
//! non-exclusive license under copyrights and patents it now or hereafter
//! owns or controls to make, have made, use, import, offer to sell and sell
//! ("Utilize") this software subject to the terms herein. With respect to the
//! foregoing patent license, such license is granted solely to the extent that
//! any such patent is necessary to Utilize the software alone. The patent
//! license shall not apply to any combinations which include this software,
//! other than combinations with devices manufactured by or for TI
//! ("TI Devices").
//! No hardware patent is licensed hereunder.
//!
//! Redistributions must preserve existing copyright notices and reproduce this
//! license (including the above copyright notice and the disclaimer and
//! (if applicable) source code license limitations below) in the documentation
//! and/or other materials provided with the distribution.
//!
//! Redistribution and use in binary form, without modification, are permitted
//! provided that the following conditions are met:
//!
//! * No reverse engineering, decompilation, or disassembly of this software is
//! permitted with respect to any software provided in binary form.
//! * Any redistribution and use are licensed by TI for use only
//! with TI Devices.
//! * Nothing shall obligate TI to provide you with source code for the
//! software licensed and provided to you in object code.
//!
//! If software source code is provided to you, modification and redistribution
//! of the source code are permitted provided that the following conditions
//! are met:
//!
//! * any redistribution and use of the source code, including any resulting
//! derivative works, are licensed by TI for use only with TI Devices.
//! * any redistribution and use of any object code compiled from the source
//! code and any resulting derivative works, are licensed by TI for use
//! only with TI Devices.
//!
//! Neither the name of Texas Instruments Incorporated nor the names of its
//! suppliers may be used to endorse or promote products derived from this
//! software without specific prior written permission.
//#############################################################################
float _satf(float A, float Pos, float Neg)
{
if( A > Pos )
A = Pos;
if( A < Neg )
A = Neg;
return A;
}
//###########################################################################
// No More.
//###########################################################################
@@ -0,0 +1,298 @@
***********************************************************************
* File: IQNacos.asm
*
* Description: IQmath function for inverse cosine
*
* Devices: C28x family
*
* Function Prototype: long IQNacos(long)
*
* C Useage: y = IQNacos(x);
*
* Input Parameters: x = angle (radians)
*
* Return Value: y = result
*
* Author: David M. Alter, Texas Instruments Inc.
*
* History:
* 06/10/04 - original
*
* Notes:
* 1) IQ format is limited to I31Q1 to I3Q29 inclusive. Formats
* greater than Q29 are excluded since they would overflow return
* angles greater than 2 (e.g., y = pi).
*
* 2) This function uses the identity: acos(x) = pi/2 - asin(x).
* It simply calls IQasin(x), and then forms (pi/2 - IQasin(x))
*
***********************************************************************
***********************************************************************
* Macro start
***********************************************************************
IQNacos: .macro q_value, _IQasin
;----------------------------------------------------------------------
; Call IQasin()
;----------------------------------------------------------------------
LCR #_IQasin ; call _IQasin()
;----------------------------------------------------------------------
; Convert y to I1Q29 format
;----------------------------------------------------------------------
.if q_value == 29
;do nothing
.elseif q_value >= 13
LSL ACC, (29 - q_value)
.else
LSL ACC, #16
LSL ACC, (13 - q_value)
.endif
PUSH ACC ; save y on stack
;----------------------------------------------------------------------
; Form (pi/2 - y) to apply acos(x) = pi/2 - asin(x)
;----------------------------------------------------------------------
MOV AH, #12867
MOV AL, #63144 ; ACC = _IQ29(pi/2)
SUBL ACC, *--SP ; ACC = (pi/2 - y)
;----------------------------------------------------------------------
; Convert back to original IQ format
;----------------------------------------------------------------------
.if q_value == 29
;do nothing
.elseif q_value >= 13
SFR ACC, (29 - q_value)
.else
SFR ACC, #16
SFR ACC, (13 - q_value)
.endif
;----------------------------------------------------------------------
; Return
;----------------------------------------------------------------------
LRETR ; return
.endm
;----------------------------------------------------------------------
; End of macro
;----------------------------------------------------------------------
***********************************************************************
* The value "GLOBAL_Q" needs to be supplied by the assembler using the
* "-dGLOBAL_Q=q_value" directive:
***********************************************************************
.sect "IQmath"
.if GLOBAL_Q == 29
.def _IQ29acos
.ref _IQ29asin
_IQ29acos:
IQNacos GLOBAL_Q, _IQ29asin
.endif
.if GLOBAL_Q == 28
.def _IQ28acos
.ref _IQ28asin
_IQ28acos:
IQNacos GLOBAL_Q, _IQ28asin
.endif
.if GLOBAL_Q == 27
.def _IQ27acos
.ref _IQ27asin
_IQ27acos:
IQNacos GLOBAL_Q, _IQ27asin
.endif
.if GLOBAL_Q == 26
.def _IQ26acos
.ref _IQ26asin
_IQ26acos:
IQNacos GLOBAL_Q, _IQ26asin
.endif
.if GLOBAL_Q == 25
.def _IQ25acos
.ref _IQ25asin
_IQ25acos:
IQNacos GLOBAL_Q, _IQ25asin
.endif
.if GLOBAL_Q == 24
.def _IQ24acos
.ref _IQ24asin
_IQ24acos:
IQNacos GLOBAL_Q, _IQ24asin
.endif
.if GLOBAL_Q == 23
.def _IQ23acos
.ref _IQ23asin
_IQ23acos:
IQNacos GLOBAL_Q, _IQ23asin
.endif
.if GLOBAL_Q == 22
.def _IQ22acos
.ref _IQ22asin
_IQ22acos:
IQNacos GLOBAL_Q, _IQ22asin
.endif
.if GLOBAL_Q == 21
.def _IQ21acos
.ref _IQ21asin
_IQ21acos:
IQNacos GLOBAL_Q, _IQ21asin
.endif
.if GLOBAL_Q == 20
.def _IQ20acos
.ref _IQ20asin
_IQ20acos:
IQNacos GLOBAL_Q, _IQ20asin
.endif
.if GLOBAL_Q == 19
.def _IQ19acos
.ref _IQ19asin
_IQ19acos:
IQNacos GLOBAL_Q, _IQ19asin
.endif
.if GLOBAL_Q == 18
.def _IQ18acos
.ref _IQ18asin
_IQ18acos:
IQNacos GLOBAL_Q, _IQ18asin
.endif
.if GLOBAL_Q == 17
.def _IQ17acos
.ref _IQ17asin
_IQ17acos:
IQNacos GLOBAL_Q, _IQ17asin
.endif
.if GLOBAL_Q == 16
.def _IQ16acos
.ref _IQ16asin
_IQ16acos:
IQNacos GLOBAL_Q, _IQ16asin
.endif
.if GLOBAL_Q == 15
.def _IQ15acos
.ref _IQ15asin
_IQ15acos:
IQNacos GLOBAL_Q, _IQ15asin
.endif
.if GLOBAL_Q == 14
.def _IQ14acos
.ref _IQ14asin
_IQ14acos:
IQNacos GLOBAL_Q, _IQ14asin
.endif
.if GLOBAL_Q == 13
.def _IQ13acos
.ref _IQ13asin
_IQ13acos:
IQNacos GLOBAL_Q, _IQ13asin
.endif
.if GLOBAL_Q == 12
.def _IQ12acos
.ref _IQ12asin
_IQ12acos:
IQNacos GLOBAL_Q, _IQ12asin
.endif
.if GLOBAL_Q == 11
.def _IQ11acos
.ref _IQ11asin
_IQ11acos:
IQNacos GLOBAL_Q, _IQ11asin
.endif
.if GLOBAL_Q == 10
.def _IQ10acos
.ref _IQ10asin
_IQ10acos:
IQNacos GLOBAL_Q, _IQ10asin
.endif
.if GLOBAL_Q == 9
.def _IQ9acos
.ref _IQ9asin
_IQ9acos:
IQNacos GLOBAL_Q, _IQ9asin
.endif
.if GLOBAL_Q == 8
.def _IQ8acos
.ref _IQ8asin
_IQ8acos:
IQNacos GLOBAL_Q, _IQ8asin
.endif
.if GLOBAL_Q == 7
.def _IQ7acos
.ref _IQ7asin
_IQ7acos:
IQNacos GLOBAL_Q, _IQ7asin
.endif
.if GLOBAL_Q == 6
.def _IQ6acos
.ref _IQ6asin
_IQ6acos:
IQNacos GLOBAL_Q, _IQ6asin
.endif
.if GLOBAL_Q == 5
.def _IQ5acos
.ref _IQ5asin
_IQ5acos:
IQNacos GLOBAL_Q, _IQ5asin
.endif
.if GLOBAL_Q == 4
.def _IQ4acos
.ref _IQ4asin
_IQ4acos:
IQNacos GLOBAL_Q, _IQ4asin
.endif
.if GLOBAL_Q == 3
.def _IQ3acos
.ref _IQ3asin
_IQ3acos:
IQNacos GLOBAL_Q, _IQ3asin
.endif
.if GLOBAL_Q == 2
.def _IQ2acos
.ref _IQ2asin
_IQ2acos:
IQNacos GLOBAL_Q, _IQ2asin
.endif
.if GLOBAL_Q == 1
.def _IQ1acos
.ref _IQ1asin
_IQ1acos:
IQNacos GLOBAL_Q, _IQ1asin
.endif
;----------------------------------------------------------------------
; End of file
;----------------------------------------------------------------------
@@ -0,0 +1,379 @@
***********************************************************************
* File: IQNasin.asm
*
* Description: IQmath function for inverse sine
*
* Devices: C28x family
*
* Function Prototype: long IQNasin(long)
*
* C Useage: y = IQNasin(x);
*
* Input Parameters: x = angle (radians)
*
* Return Value: y = result
*
* Author: David M. Alter, Texas Instruments Inc.
*
* History:
* 06/10/04 - original
*
* Notes:
* 1) IQ format is limited to I31Q1 to I3Q29 inclusive. Formats
* greater than Q29 are excluded since they would overflow return
* angles greater than 2 (e.g., y = pi).
*
***********************************************************************
***********************************************************************
* References and Definitions
***********************************************************************
.ref _IQ29sqrt
.ref _IQasinTable
.asg XT, Xsign
.asg T, sign
.asg XAR5, XiTransform
.asg AR5, iTransform
.asg XAR6, x
.asg XAR6, y
.asg XAR7, c
TaylorSeriesOrder .set 5
***********************************************************************
* Macro start
***********************************************************************
IQNasin: .macro q_value
;----------------------------------------------------------------------
; Miscellaneous initialization
;----------------------------------------------------------------------
SETC SXM ; enable sign-extension
;----------------------------------------------------------------------
; Convert x to I3Q29 format
;----------------------------------------------------------------------
.if q_value == 29
;do nothing
.elseif q_value >= 13
LSL ACC, (29 - q_value)
.else
LSL ACC, #16
LSL ACC, (13 - q_value)
.endif
;----------------------------------------------------------------------
; Determine if x<0 (we will apply the identity sin(-x) = -sin(x))
;----------------------------------------------------------------------
MOV sign, #1 ; assume sign = 1
BF L1, GEQ ; branch if x >= 0
MOV sign, #(-1) ; sign = -1
NEG ACC ; x = -x
L1:
MOVL x, ACC ; save x
PUSH Xsign ; save sign on stack
;----------------------------------------------------------------------
; If x > 0.5, we need to apply the transform
;----------------------------------------------------------------------
MOVB XiTransform, #0 ; assume no transform needed
SUB ACC, #2000h << 15 ; subtract _IQ29(0.5) from x
BF L2, LT ; branch if x < 0.5
;----------------------------------------------------------------------
; Apply the transform: x = _IQ29sqrt( (_IQ29(1.0) - x) >> 1 )
;----------------------------------------------------------------------
MOV ACC, #4000h << 15 ; ACC = _IQ29(1.0)
SUBL ACC, x ; subtract x
SFR ACC, 1 ; shift right by 1
LCR #_IQ29sqrt ; call _IQ29sqrt()
MOVL x, ACC ; save x
MOVB XiTransform, #1 ; iTransform = 1
;----------------------------------------------------------------------
; Determine the lookup table index. The table contains 2^N+1 = 17 points
; over the interval 0 to 0.5, inclusive (i.e., pt.0 = 0, pt.17 = 0.5).
; Therefore:
; index = bits(28:24) of (x + half the interval)
; = bits(28:24) of [x + (0.5/2^N)/2]
; = bits(28:24) of [x + (0.5/2^4)/2]
; = bits(28:24) of [x + 0.015625]
;
; LutIndex = ((x + _IQ29(0.015625)) >> 24) & 0x001F;
;----------------------------------------------------------------------
L2:
MOVL ACC, x ; load x
ADD ACC, #0100h << 15 ; add _IQ29(0.015625)
MOV T,#24 ; T = 24
ASRL ACC,T ; ACC >> 24
ANDB AL, #001fh ; ACC = LutIndex
;----------------------------------------------------------------------
; Setup the coefficient pointer
; c = &IQasinTable[0] + (TaylorSeriesOrder * LutIndex)
;----------------------------------------------------------------------
MOV T,AL ; T = LutIndex
MPYB ACC, T, #TaylorSeriesOrder ; ACC = TaylorSeriesOrder * LutIndex
MOVL c, #_IQasinTable ; c = &IQasinTable[0]
LSL ACC, 1 ; shift offset by 1 since offset is for 32-bit data values
ADDL c, ACC ; c = &IQasinTable[0] + (TaylorSeriesOrder * LutIndex)
;----------------------------------------------------------------------
; Perform the computations:
; y = _IQ29mpy((_IQ29mpy((_IQ29mpy((_IQ29mpy(*c++, x) + *c++), x) + *c++) , x) + *c++), x) + *c;
;----------------------------------------------------------------------
MOVL XT,*c++
IMPYL P,XT,x
QMPYL ACC,XT,x
LSL64 ACC:P,#3 ; _IQ29mpy(*c++, x)
ADDL ACC,*c++ ; _IQ29mpy(*c++, x) + *c++
MOVL XT,ACC
IMPYL P,XT,x
QMPYL ACC,XT,x
LSL64 ACC:P,#3 ; _IQ29mpy((_IQ29mpy(*c++, x) + *c++), x)
ADDL ACC,*c++ ; _IQ29mpy((_IQ29mpy(*c++, x) + *c++), x) + *c++
MOVL XT,ACC
IMPYL P,XT,x
QMPYL ACC,XT,x
LSL64 ACC:P,#3 ; _IQ29mpy((_IQ29mpy((_IQ29mpy(*c++, x) + *c++), x) + *c++) , x)
ADDL ACC,*c++ ; _IQ29mpy((_IQ29mpy((_IQ29mpy(*c++, x) + *c++), x) + *c++) , x) + *c++
MOVL XT,ACC
IMPYL P,XT,x
QMPYL ACC,XT,x
LSL64 ACC:P,#3 ; _IQ29mpy((_IQ29mpy((_IQ29mpy((_IQ29mpy(*c++, x) + *c++), x) + *c++) , x) + *c++), x)
ADDL ACC,*+c[0] ; _IQ29mpy((_IQ29mpy((_IQ29mpy((_IQ29mpy(*c++, x) + *c++), x) + *c++) , x) + *c++), x) + *c
;----------------------------------------------------------------------
; Further computation needed if the transform was applied:
; y = _IQ29(pi/2) - (y << 1);
;----------------------------------------------------------------------
TBIT iTransform, #0 ; test bit 0
SBF L3, NTC ; branch if transform not applied
LSL ACC, 1 ; ACC = y << 1
MOVL y, ACC ; store (y << 1)
MOV AH, #12867
MOV AL, #63144 ; ACC = _IQ29(pi/2)
SUBL ACC, y ; ACC = _IQ29(pi/2) - (y << 1)
;----------------------------------------------------------------------
; Apply the sign, convert back to original IQ format, and return:
; return((sign * y) >> (29 - GLOBAL_Q))
;----------------------------------------------------------------------
L3:
POP Xsign ; retrieve the sign
MOVX TL, sign ; get the XT properly sign extended
IMPYL ACC, XT, ACC ; ACC = sign * y
.if q_value == 29
;do nothing
.elseif q_value >= 13
SFR ACC, (29 - q_value) ; ACC = (sign * y) >> (29 - GLOBAL_Q)
.else
SFR ACC, #16
SFR ACC, (13 - q_value) ; ACC = (sign * y) >> (29 - GLOBAL_Q)
.endif
LRETR ; return
.endm
;----------------------------------------------------------------------
; End of macro
;----------------------------------------------------------------------
***********************************************************************
* The value "GLOBAL_Q" needs to be supplied by the assembler using the
* "-dGLOBAL_Q=q_value" directive:
***********************************************************************
.sect "IQmath"
.if GLOBAL_Q == 29
.def _IQ29asin
_IQ29asin:
IQNasin GLOBAL_Q
.endif
.if GLOBAL_Q == 28
.def _IQ28asin
_IQ28asin:
IQNasin GLOBAL_Q
.endif
.if GLOBAL_Q == 27
.def _IQ27asin
_IQ27asin:
IQNasin GLOBAL_Q
.endif
.if GLOBAL_Q == 26
.def _IQ26asin
_IQ26asin:
IQNasin GLOBAL_Q
.endif
.if GLOBAL_Q == 25
.def _IQ25asin
_IQ25asin:
IQNasin GLOBAL_Q
.endif
.if GLOBAL_Q == 24
.def _IQ24asin
_IQ24asin:
IQNasin GLOBAL_Q
.endif
.if GLOBAL_Q == 23
.def _IQ23asin
_IQ23asin:
IQNasin GLOBAL_Q
.endif
.if GLOBAL_Q == 22
.def _IQ22asin
_IQ22asin:
IQNasin GLOBAL_Q
.endif
.if GLOBAL_Q == 21
.def _IQ21asin
_IQ21asin:
IQNasin GLOBAL_Q
.endif
.if GLOBAL_Q == 20
.def _IQ20asin
_IQ20asin:
IQNasin GLOBAL_Q
.endif
.if GLOBAL_Q == 19
.def _IQ19asin
_IQ19asin:
IQNasin GLOBAL_Q
.endif
.if GLOBAL_Q == 18
.def _IQ18asin
_IQ18asin:
IQNasin GLOBAL_Q
.endif
.if GLOBAL_Q == 17
.def _IQ17asin
_IQ17asin:
IQNasin GLOBAL_Q
.endif
.if GLOBAL_Q == 16
.def _IQ16asin
_IQ16asin:
IQNasin GLOBAL_Q
.endif
.if GLOBAL_Q == 15
.def _IQ15asin
_IQ15asin:
IQNasin GLOBAL_Q
.endif
.if GLOBAL_Q == 14
.def _IQ14asin
_IQ14asin:
IQNasin GLOBAL_Q
.endif
.if GLOBAL_Q == 13
.def _IQ13asin
_IQ13asin:
IQNasin GLOBAL_Q
.endif
.if GLOBAL_Q == 12
.def _IQ12asin
_IQ12asin:
IQNasin GLOBAL_Q
.endif
.if GLOBAL_Q == 11
.def _IQ11asin
_IQ11asin:
IQNasin GLOBAL_Q
.endif
.if GLOBAL_Q == 10
.def _IQ10asin
_IQ10asin:
IQNasin GLOBAL_Q
.endif
.if GLOBAL_Q == 9
.def _IQ9asin
_IQ9asin:
IQNasin GLOBAL_Q
.endif
.if GLOBAL_Q == 8
.def _IQ8asin
_IQ8asin:
IQNasin GLOBAL_Q
.endif
.if GLOBAL_Q == 7
.def _IQ7asin
_IQ7asin:
IQNasin GLOBAL_Q
.endif
.if GLOBAL_Q == 6
.def _IQ6asin
_IQ6asin:
IQNasin GLOBAL_Q
.endif
.if GLOBAL_Q == 5
.def _IQ5asin
_IQ5asin:
IQNasin GLOBAL_Q
.endif
.if GLOBAL_Q == 4
.def _IQ4asin
_IQ4asin:
IQNasin GLOBAL_Q
.endif
.if GLOBAL_Q == 3
.def _IQ3asin
_IQ3asin:
IQNasin GLOBAL_Q
.endif
.if GLOBAL_Q == 2
.def _IQ2asin
_IQ2asin:
IQNasin GLOBAL_Q
.endif
.if GLOBAL_Q == 1
.def _IQ1asin
_IQ1asin:
IQNasin GLOBAL_Q
.endif
;----------------------------------------------------------------------
; End of file
;----------------------------------------------------------------------
@@ -0,0 +1,142 @@
***********************************************************************
* File: IQNasinTable.asm
* Devices: TMS320F2812, TMS320F2811, TMS320F2810
* Author: David M. Alter, Texas Instruments Inc.
* History:
* 04/29/04 - original (D. Alter)
***********************************************************************
;;==========================================================================
;; IQasin Function Table, Size Of Table = 85x16
;;==========================================================================
.def _IQasinTable
.sect "IQmathTablesRam"
_IQasinTable:
; for f(x) = c4*x^4 + c3*x^3 + c2*x^2 + c1*x + c0
;point 0 (x=0)
point0:
.long 0 ; c4 for point 0 = 0.00000000000000 in Q29
.long 89478485 ; c3 for point 0 = 0.16666666666667 in Q29
.long 0 ; c2 for point 0 = 0.00000000000000 in Q29
.long 536870912 ; c1 for point 0 = 1.00000000000000 in Q29
.long 0 ; c0 for point 0 = 0.00000000000000 in Q29
;point 1 (x=0.03125)
point1:
.long 6317117 ; c4 for point 1 = 0.01176654801806 in Q29
.long 89082863 ; c3 for point 1 = 0.16592976253570 in Q29
.long 12378 ; c2 for point 1 = 0.00002305630012 in Q29
.long 536870718 ; c1 for point 1 = 0.99999963945303 in Q29
.long 1 ; c0 for point 1 = 0.00000000225472 in Q29
;point 2 (x=0.06250)
point2:
.long 12789687 ; c4 for point 2 = 0.02382264819581 in Q29
.long 87866810 ; c3 for point 2 = 0.16366468695565 in Q29
.long 101217 ; c2 for point 2 = 0.00018853150967 in Q29
.long 536867739 ; c1 for point 2 = 0.99999408944239 in Q29
.long 40 ; c0 for point 2 = 0.00000007405140 in Q29
;point 3 (x=0.09375)
point3:
.long 19580708 ; c4 for point 3 = 0.03647191042431 in Q29
.long 85740406 ; c3 for point 3 = 0.15970395152535 in Q29
.long 354217 ; c2 for point 3 = 0.00065978084316 in Q29
.long 536854190 ; c1 for point 3 = 0.99996885226392 in Q29
.long 315 ; c0 for point 3 = 0.00000058698337 in Q29
;point 4 (x=0.1250)
point4:
.long 26868885 ; c4 for point 4 = 0.05004719802685 in Q29
.long 82545646 ; c3 for point 4 = 0.15375324754641 in Q29
.long 882934 ; c2 for point 4 = 0.00164459220320 in Q29
.long 536815043 ; c1 for point 4 = 0.99989593609382 in Q29
.long 1409 ; c0 for point 4 = 0.00000262453734 in Q29
;point 5 (x=0.15625)
point5:
.long 34858083 ; c4 for point 5 = 0.06492823848730 in Q29
.long 78043250 ; c3 for point 5 = 0.14536688064656 in Q29
.long 1838350 ; c2 for point 5 = 0.00342419379134 in Q29
.long 536724572 ; c1 for point 5 = 0.99972742028566 in Q29
.long 4634 ; c0 for point 5 = 0.00000863223157 in Q29
;point 6 (x=0.18750)
point6:
.long 43788925 ; c4 for point 6 = 0.08156322849045 in Q29
.long 71892080 ; c3 for point 6 = 0.13390943393951 in Q29
.long 3431456 ; c2 for point 6 = 0.00639158410284 in Q29
.long 536540695 ; c1 for point 6 = 0.99938492317316 in Q29
.long 12614 ; c0 for point 6 = 0.00002349611964 in Q29
;point 7 (x=0.21875)
point7:
.long 53953665 ; c4 for point 7 = 0.10049653164769 in Q29
.long 63618641 ; c3 for point 7 = 0.11849895225606 in Q29
.long 5961683 ; c2 for point 7 = 0.01110450018835 in Q29
.long 536196110 ; c1 for point 7 = 0.99874308390293 in Q29
.long 30246 ; c0 for point 7 = 0.00005633823023 in Q29
;point 8 (x=0.25000)
point8:
.long 65715899 ; c4 for point 8 = 0.12240539958384 in Q29
.long 52572719 ; c3 for point 8 = 0.09792431966707 in Q29
.long 9857385 ; c2 for point 8 = 0.01836080993758 in Q29
.long 535584573 ; c1 for point 8 = 0.99760400660829 in Q29
.long 66298 ; c0 for point 8 = 0.00012348928199 in Q29
;point 9 (x=0.28125)
point9:
.long 79537402 ; c4 for point 9 = 0.14814995596061 in Q29
.long 37863112 ; c3 for point 9 = 0.07052554134789 in Q29
.long 15734668 ; c2 for point 9 = 0.02930810346023 in Q29
.long 534539695 ; c1 for point 9 = 0.99565777006874 in Q29
.long 136037 ; c0 for point 9 = 0.00025338900082 in Q29
;point 10 (x=0.31250)
point10:
.long 96015466 ; c4 for point 10 = 0.17884274290911 in Q29
.long 18264067 ; c3 for point 10 = 0.03401947530154 in Q29
.long 24484384 ; c2 for point 10 = 0.04560571931992 in Q29
.long 532802024 ; c1 for point 10 = 0.99242110469066 in Q29
.long 265566 ; c0 for point 10 = 0.00049465588355 in Q29
;point 11 (x=0.34375)
point11:
.long 115935880 ; c4 for point 11 = 0.21594740492715 in Q29
.long -7922211 ; c3 for point 11 = -0.01475626778059 in Q29
.long 37402735 ; c2 for point 11 = 0.06966802370550 in Q29
.long 529967480 ; c1 for point 11 = 0.98714135522368 in Q29
.long 498974 ; c0 for point 11 = 0.00092941064669 in Q29
;point 12 (x=0.37500)
point12:
.long 140349533 ; c4 for point 12 = 0.26142137657237 in Q29
.long -43070227 ; c3 for point 12 = -0.08022454942433 in Q29
.long 56390437 ; c2 for point 12 = 0.10503537451569 in Q29
.long 525405717 ; c1 for point 12 = 0.97864440922208 in Q29
.long 910213 ; c0 for point 12 = 0.00169540313020 in Q29
;point 13 (x=0.40625)
point13:
.long 170685304 ; c4 for point 13 = 0.31792615376935 in Q29
.long -90540603 ; c3 for point 13 = -0.16864501479554 in Q29
.long 84261400 ; c2 for point 13 = 0.15694908812191 in Q29
.long 518129119 ; c1 for point 13 = 0.96509068936447 in Q29
.long 1623008 ; c0 for point 13 = 0.00302308763644 in Q29
;point 14 (x=0.43750)
point14:
.long 208919771 ; c4 for point 14 = 0.38914339775461 in Q29
.long -155156187 ; c3 for point 14 = -0.28900091994247 in Q29
.long 125229731 ; c2 for point 14 = 0.23325855153370 in Q29
.long 506579341 ; c1 for point 14 = 0.94357755204347 in Q29
.long 2844600 ; c0 for point 14 = 0.00529848115859 in Q29
;point 15 (x=0.46875)
point15:
.long 257837857 ; c4 for point 15 = 0.48026043442295 in Q29
.long -243949525 ; c3 for point 15 = -0.45439139960266 in Q29
.long 185693290 ; c2 for point 15 = 0.34588070642185 in Q29
.long 488273269 ; c1 for point 15 = 0.90947983687988 in Q29
.long 4923808 ; c0 for point 15 = 0.00917130774387 in Q29
;point 16 (x=0.50000)
point16:
.long 321442661 ; c4 for point 16 = 0.59873361249295 in Q29
.long -367363041 ; c3 for point 16 = -0.68426698570622 in Q29
.long 275522281 ; c2 for point 16 = 0.51320023927967 in Q29
.long 459203801 ; c1 for point 16 = 0.85533373213278 in Q29
.long 8452695 ; c0 for point 16 = 0.01574437214446 in Q29
;end of IQasinTable
.end
;end file IQNasinTable.asm
@@ -0,0 +1,433 @@
;;###########################################################################
;;
;; FILE: IQNatan2.asm
;;
;; TITLE: C Callable IQ Math Function
;;
;;###########################################################################
;;
;; Ver | Date | Who | Description of changes
;; =====|=============|=======|==============================================
;; 1.3 | 19 Nov 2001 | A. T. | Original Release.
;; -----|-------------|-------|----------------------------------------------
;; 1.4 | 17 May 2002 | A. T. | Added note that a value of Q30 is not
;; | | | feasible for this operation. Q range is
;; | | | limited to Q29 to Q1.
;; -----|-------------|-------|----------------------------------------------
;; | | |
;;
;;###########################################################################
;;===========================================================================
;; Function: _IQNatan2
;;===========================================================================
;;
;; C Usage: extern long _IQNatan2(long Y, long X);
;;
;;---------------------------------------------------------------------------
;;
;; On Entry: ACC = Y in IQ format
;; SP-4 = X in IQ format
;;
;; Regs Used: XAR7, XAR6, XAR5, XAR4, XAR0, XT, P
;;
;; On Exit: ACC = Radian result in IQ format
;; if( X == 0.0 and Y == 0.0 ), ACC = 0.0
;; if( Y >= 0.0 ), 0.0 <= ACC <= PI
;; if( Y < 0.0 ), -PI < ACC < 0.0
;;
;; Q range: 29 to 1
;;
;; Note: A value of Q30 is not feasible since the result would
;; saturate for angles greater then +/- 2.0 radians.
;;
;;---------------------------------------------------------------------------
;; Algorithm: The algorithm steps to calculate the "atan2" of the given
;; input X and Y is as follows:
;;
;; Step(1): if( abs(X) >= abs(Y) )
;; Numerator = abs(Y)
;; Denominator = abs(X)
;; else
;; Numerator = abs(X)
;; Denominator = abs(Y)
;;
;; Step(2): Ratio = Numerator/Denominator
;;
;; Note: Ratio range = 0.0 to 1.0
;;
;; Step(3): Use the upper 6-bits of the "Ratio" value as an
;; index into the table to obtain the coefficients
;; for a second order equation:
;;
;; _IQNatan2Table:
;; CoeffA0[0]
;; CoeffA1[0]
;; CoeffA2[0]
;; .
;; .
;; CoeffA0[63]
;; CoeffA1[63]
;; CoeffA2[63]
;;
;; Step(4): Calculate the angle using the folowing equation:
;;
;; arctan(Ratio) = A0 + A1*Ratio + A2*Ratio*Ratio
;; arctan(Ratio) = A0 + Ratio(A1 + A2*Ratio)
;;
;; Step(5): The final angle is determined as follows:
;;
;; if( X >= 0 and Y >= 0 and abs(X) >= abs(Y) )
;; Angle = arctan(abs(Y)/abs(X))
;; if( X >= 0 and Y >= 0 and abs(X) < abs(Y) )
;; Angle = PI/2 - arctan(abs(X)/abs(Y))
;; if( X < 0 and Y >= 0 and abs(X) < abs(Y) )
;; Angle = PI/2 + arctan(abs(X)/abs(Y))
;; if( X < 0 and Y >= 0 and abs(X) >= abs(Y) )
;; Angle = PI - arctan(abs(Y)/abs(X))
;; if( Y < 0 )
;; Angle = -Angle
;;
;;---------------------------------------------------------------------------
;; Benchmark:
;; * Code executes from 0-wait SARAM block.
;; * _IQatan2Table & _IQdivTable located in seperate SARAM or ROM block.
;; * Stack located in seperate SARAM block.
;; * Includes LCR/LRETR.
;;
;; Cycles = 108 (if Q == 30 and _IQatan2Table & _IQdivTable in 0-wait SARAM)
;; = 109 (if 16 <= Q <= 29 and _IQatan2Table & _IQdivTable in 0-wait SARAM)
;; = 112 (if 1 <= Q <= 15 and _IQatan2Table & _IQdivTable in 0-wait SARAM)
;; Cycles = 114 (if Q == 30 and _IQatan2Table & _IQdivTable in 1-wait ROM)
;; = 115 (if 16 <= Q <= 29 and _IQatan2Table & _IQdivTable in 1-wait ROM)
;; = 118 (if 1 <= Q <= 15 and _IQatan2Table & _IQdivTable in 1-wait ROM)
;;
;;===========================================================================
IQNatan2 .macro q_value
;; Perform Step 1:
SETC OVM ; Make sure overflow mode is on
MOVL *SP++,ACC ; SP-6 = Y
ABS ACC ; ACC = abs(Y)
MOVL XT,@ACC ; XT = abs(Y)
MOVL ACC,*-SP[6] ; ACC = X
ABS ACC ; ACC = abs(X)
MOVL P,@ACC ; P = abs(X)
SUBL ACC,@XT ; ACC = abs(X) - abs(Y)
MOVL *SP++,ACC ; SP-4 = abs(X) - abs(Y)
MOVL ACC,@XT ; ACC = abs(Y)
MINL ACC,@P ; if( abs(Y) > abs(X) ), ACC = abs(X)
MOVL *SP++,ACC ; SP-2 = Numerator
MOVL ACC,@P ; ACC = abs(X)
MAXL ACC,@XT ; if( abs(X) < abs(Y) ), ACC = abs(Y)
; ACC = Denominator
; SP-10 = X
; SP-8 = Return Address
; SP-6 = Y
; SP-4 = abs(X) - abs(Y)
; SP-2 = Numerator
;; Perform Step 2:
CSB ACC ; Count sign bits, T = n
LSLL ACC,T ; ACC = Dnorm (Q31)
MOVL @XAR4,ACC ; Temp save "Dnorm"
LSR AH,#6
MOVB @AH,#0xFE,EQ ; If Den = 0, load zero value
MOVZ AR0,@AH ; AR0 = index into _IQdivTable
MOVL XAR7,#(_IQdivTable-254)
MOVB AH,#(62 - q_value) ; AH = 62 - q_value
SUBR @T,AH ; T = 62 - q_value - n
MOVL XAR5,@XT ; AR5H = 62 - q_value - n
MOVL XT,*+XAR7[AR0] ; (1) XT = initial Dm estimate (Q29)
MOV @AH,#0x4000
MOVB AL,#0
MOVL XAR6,@ACC ; XAR6 = 2.0 (Q29)
IMPYL P,XT,@XAR4 ; (2)
QMPYL ACC,XT,@XAR4 ; (2) ACC:P = Dm*Dnorm (Q60)
LSL64 ACC:P,#1 ; (3) ACC:P = Dm*Dnorm (Q61)
SUBL ACC,@XAR6 ; (4) ACC = Dm*Dnorm - 2.0 (Q29)
NEG ACC ; (5) ACC = 2.0 - Dm*Dnorm (Q29)
IMPYL P,XT,@ACC ; (6)
QMPYL ACC,XT,@ACC ; (6) ACC:P = Dm(2.0 - Dm*Dnorm) (Q58)
LSL64 ACC:P,#3 ; (7) ACC:P = Dm(2.0 - Dm*Dnorm) (Q61)
MOVL XT,@ACC ; (1) XT = Dm (Q29)
IMPYL P,XT,@XAR4 ; (2)
QMPYL ACC,XT,@XAR4 ; (2) ACC:P = Dm*Dnorm (Q60)
LSL64 ACC:P,#1 ; (3) ACC:P = Dm*Dnorm (Q61)
SUBL ACC,@XAR6 ; (4) ACC = Dm*Dnorm - 2.0 (Q29)
NEG ACC ; (5) ACC = 2.0 - Dm*Dnorm (Q29)
IMPYL P,XT,@ACC ; (6)
QMPYL ACC,XT,@ACC ; (6) ACC:P = Dm(2.0 - Dm*Dnorm) (Q58)
LSL64 ACC:P,#3 ; (7) ACC:P = Dm(2.0 - Dm*Dnorm) (Q61)
MOVL XT,*-SP[2] ; XT = Numerator
IMPYL P,XT,@ACC
QMPYL ACC,XT,@ACC ; ACC:P = N * Dm
LSL64 ACC:P,#2
MOVL XT,@XAR5 ; T = 62 - q_value - n
ASR64 ACC:P,T ; scale back result
; P = Ratio in IQ format
;; Perform Step 3:
MOVL XT,@P ; XT = Ratio
MOVB ACC,#64 ; ACC = 64 = Elements In Table
IMPYL P,XT,@ACC
QMPYL ACC,XT,@ACC ; ACC:P = 64*Ratio
.if q_value >= 16
LSL64 ACC:P,#(32 - q_value) ; ACC = int(64*Ratio)
.else
LSL64 ACC:P,#16
LSL64 ACC:P,#(16 - q_value) ; ACC = int(64*Ratio)
.endif
MPY P,@AL,#6 ; P = 6 * int(64*Ratio)
MOVZ AR0,@PL ; AR0 = _IQatan2Table index
MOVL XAR5,#_IQatan2Table ; Point to A0
MOVL XAR6,#_IQatan2Table+2 ; Point to A1
MOVL XAR7,#_IQatan2Table+4 ; Point to A2
;; Perform Step 4:
IMPYL P,XT,*+XAR7[AR0]
QMPYL ACC,XT,*+XAR7[AR0] ; ACC:P = A2*Ratio (q_value + Q30)
.if q_value >= 16
LSL64 ACC:P,#(32-q_value) ; ACC = A2*Ratio (Q30)
.else
LSL64 ACC:P,#16
LSL64 ACC:P,#(16-q_value) ; ACC = A2*Ratio (Q30)
.endif
ADDL ACC,*+XAR6[AR0] ; ACC = A1 + A2*Ratio (Q30)
IMPYL P,XT,@ACC
QMPYL ACC,XT,@ACC ; ACC:P = Ratio(A1 + A2*Ratio) (q_value + Q30)
.if q_value >= 16
LSL64 ACC:P,#(32-q_value) ; ACC:P = Ratio(A1 + A2*Ratio) (Q30)
.else
LSL64 ACC:P,#16
LSL64 ACC:P,#(16-q_value) ; ACC:P = Ratio(A1 + A2*Ratio) (Q30)
.endif
ADDL ACC,*+XAR5[AR0] ; ACC = A0 + Ratio(A1 + A2*Ratio) (Q30)
.if (q_value >= 14) & (q_value <= 29)
LSR64 ACC:P,#(30-q_value) ; ACC = A0 + Ratio(A1 + A2*Ratio) (q_value)
.endif
.if q_value <= 13
LSR64 ACC:P,#16
LSR64 ACC:P,#(14-q_value) ; ACC = A0 + Ratio(A1 + A2*Ratio) (q_value)
.endif
;; Perform Step 5:
MOVL XAR7,#(_IQatan2HalfPITable + q_value*2)
MOVL XAR6,*XAR7 ; XAR6 = PI/2
MOVL P,@ACC ; P = arctan(Ratio)
NEG ACC ; ACC = -arctan(Ratio)
TBIT *-SP[9],#15 ; Test if(X >= 0)
MOVL @P,ACC,NTC ; if(X >= 0), P = -arctan(Ratio)
MOVL ACC,@P
NEG ACC ; ACC = -P
TBIT *-SP[3],#15 ; Test if(abs(X) >= abs(Y))
MOVL @P,ACC,NTC ; if(abs(X) >= abs(Y)), P = -P
MOVL ACC,@XAR6 ; ACC = PI/2
NEG ACC ; ACC = -PI/2
TBIT *-SP[9],#15 ; Test if(X < 0)
NEGTC ACC ; if(X < 0), ACC = PI/2
ADDL ACC,@XAR6 ; ACC = ACC + PI/2
TBIT *-SP[3],#15 ; Test if(abs(X) >= abs(Y))
MOVL @XAR6,ACC,NTC ; if(abs(X) >= abs(Y)), XAR6 = ACC
MOVL ACC,@XAR6
ADDL ACC,@P ; ACC = arctan(abs(Y),X)
TBIT *-SP[5],#15 ; Test if(Y < 0)
NEGTC ACC ; if(Y < 0), ACC = arctan(Y,X)
SUBB SP,#6 ; Deallocate stack
CLRC OVM ; Turn off overflow mode
LRETR
.endm
;; The value "GLOBAL_Q" needs to be supplied by the assembler
;; using the "-dGLOBAL_Q=q_value" directive:
.sect "IQmath"
.ref _IQatan2HalfPITable
.ref _IQatan2Table
.ref _IQdivTable
.if GLOBAL_Q == 30
.def _IQ30atan2
_IQ30atan2:
IQNatan2 GLOBAL_Q
.endif
.if GLOBAL_Q == 29
.def _IQ29atan2
_IQ29atan2:
IQNatan2 GLOBAL_Q
.endif
.if GLOBAL_Q == 28
.def _IQ28atan2
_IQ28atan2:
IQNatan2 GLOBAL_Q
.endif
.if GLOBAL_Q == 27
.def _IQ27atan2
_IQ27atan2:
IQNatan2 GLOBAL_Q
.endif
.if GLOBAL_Q == 26
.def _IQ26atan2
_IQ26atan2:
IQNatan2 GLOBAL_Q
.endif
.if GLOBAL_Q == 25
.def _IQ25atan2
_IQ25atan2:
IQNatan2 GLOBAL_Q
.endif
.if GLOBAL_Q == 24
.def _IQ24atan2
_IQ24atan2:
IQNatan2 GLOBAL_Q
.endif
.if GLOBAL_Q == 23
.def _IQ23atan2
_IQ23atan2:
IQNatan2 GLOBAL_Q
.endif
.if GLOBAL_Q == 22
.def _IQ22atan2
_IQ22atan2:
IQNatan2 GLOBAL_Q
.endif
.if GLOBAL_Q == 21
.def _IQ21atan2
_IQ21atan2:
IQNatan2 GLOBAL_Q
.endif
.if GLOBAL_Q == 20
.def _IQ20atan2
_IQ20atan2:
IQNatan2 GLOBAL_Q
.endif
.if GLOBAL_Q == 19
.def _IQ19atan2
_IQ19atan2:
IQNatan2 GLOBAL_Q
.endif
.if GLOBAL_Q == 18
.def _IQ18atan2
_IQ18atan2:
IQNatan2 GLOBAL_Q
.endif
.if GLOBAL_Q == 17
.def _IQ17atan2
_IQ17atan2:
IQNatan2 GLOBAL_Q
.endif
.if GLOBAL_Q == 16
.def _IQ16atan2
_IQ16atan2:
IQNatan2 GLOBAL_Q
.endif
.if GLOBAL_Q == 15
.def _IQ15atan2
_IQ15atan2:
IQNatan2 GLOBAL_Q
.endif
.if GLOBAL_Q == 14
.def _IQ14atan2
_IQ14atan2:
IQNatan2 GLOBAL_Q
.endif
.if GLOBAL_Q == 13
.def _IQ13atan2
_IQ13atan2:
IQNatan2 GLOBAL_Q
.endif
.if GLOBAL_Q == 12
.def _IQ12atan2
_IQ12atan2:
IQNatan2 GLOBAL_Q
.endif
.if GLOBAL_Q == 11
.def _IQ11atan2
_IQ11atan2:
IQNatan2 GLOBAL_Q
.endif
.if GLOBAL_Q == 10
.def _IQ10atan2
_IQ10atan2:
IQNatan2 GLOBAL_Q
.endif
.if GLOBAL_Q == 9
.def _IQ9atan2
_IQ9atan2:
IQNatan2 GLOBAL_Q
.endif
.if GLOBAL_Q == 8
.def _IQ8atan2
_IQ8atan2:
IQNatan2 GLOBAL_Q
.endif
.if GLOBAL_Q == 7
.def _IQ7atan2
_IQ7atan2:
IQNatan2 GLOBAL_Q
.endif
.if GLOBAL_Q == 6
.def _IQ6atan2
_IQ6atan2:
IQNatan2 GLOBAL_Q
.endif
.if GLOBAL_Q == 5
.def _IQ5atan2
_IQ5atan2:
IQNatan2 GLOBAL_Q
.endif
.if GLOBAL_Q == 4
.def _IQ4atan2
_IQ4atan2:
IQNatan2 GLOBAL_Q
.endif
.if GLOBAL_Q == 3
.def _IQ3atan2
_IQ3atan2:
IQNatan2 GLOBAL_Q
.endif
.if GLOBAL_Q == 2
.def _IQ2atan2
_IQ2atan2:
IQNatan2 GLOBAL_Q
.endif
.if GLOBAL_Q == 1
.def _IQ1atan2
_IQ1atan2:
IQNatan2 GLOBAL_Q
.endif
;;###########################################################################
;; No More.
;;###########################################################################
@@ -0,0 +1,458 @@
;;###########################################################################
;;
;; FILE: IQNatan2PU.asm
;;
;; TITLE: C Callable IQ Math Function
;;
;;###########################################################################
;;
;; Ver | Date | Who | Description of changes
;; =====|=============|=======|==============================================
;; 1.4 | 17 May 2002 | A. T. | Original Release.
;; -----|-------------|-------|----------------------------------------------
;; | | |
;;
;;###########################################################################
;;===========================================================================
;; Function: _IQNatan2PU
;;===========================================================================
;;
;; C Usage: extern long _IQNatan2PU(long Y, long X);
;;
;;---------------------------------------------------------------------------
;;
;; On Entry: ACC = Y in IQ format
;; SP-4 = X in IQ format
;;
;; Regs Used: XAR7, XAR6, XAR5, XAR4, XAR0, XT, P
;;
;; On Exit: ACC = Per Unit result in IQ format (0.0=0Deg, 1.0=360Deg)
;; if( X == 0.0 and Y == 0.0 ), ACC = 0.0
;; if( Y >= 0.0 ), 0.0 <= ACC <= 0.5
;; if( Y < 0.0 ), 0.5 < ACC < 1.0
;;
;; Q range: 29 to 1
;;
;; Note: A value of Q30 is not feasible since the result would
;; saturate for angles greater then +/- 2.0 radians.
;;
;;---------------------------------------------------------------------------
;; Algorithm: The algorithm steps to calculate the "atan2" of the given
;; input X and Y is as follows:
;;
;; Step(1): if( abs(X) >= abs(Y) )
;; Numerator = abs(Y)
;; Denominator = abs(X)
;; else
;; Numerator = abs(X)
;; Denominator = abs(Y)
;;
;; Step(2): Ratio = Numerator/Denominator
;;
;; Note: Ratio range = 0.0 to 1.0
;;
;; Step(3): Use the upper 6-bits of the "Ratio" value as an
;; index into the table to obtain the coefficients
;; for a second order equation:
;;
;; _IQNatan2Table:
;; CoeffA0[0]
;; CoeffA1[0]
;; CoeffA2[0]
;; .
;; .
;; CoeffA0[63]
;; CoeffA1[63]
;; CoeffA2[63]
;;
;; Step(4): Calculate the angle using the folowing equation:
;;
;; arctan(Ratio) = A0 + A1*Ratio + A2*Ratio*Ratio
;; arctan(Ratio) = A0 + Ratio(A1 + A2*Ratio)
;;
;;
;; Step(5): The final angle is determined as follows:
;;
;; if( X >= 0 and Y >= 0 and abs(X) >= abs(Y) )
;; Angle = arctan(abs(Y)/abs(X))
;; if( X >= 0 and Y >= 0 and abs(X) < abs(Y) )
;; Angle = PI/2 - arctan(abs(X)/abs(Y))
;; if( X < 0 and Y >= 0 and abs(X) < abs(Y) )
;; Angle = PI/2 + arctan(abs(X)/abs(Y))
;; if( X < 0 and Y >= 0 and abs(X) >= abs(Y) )
;; Angle = PI - arctan(abs(Y)/abs(X))
;;
;; Step(6): Convert to Per Unit (PU) value:
;;
;; PUangle(Ratio) = arctan(Ratio)/(2*pi)
;; PUangle(Ratio) = arctan(Ratio) * 0.159154943
;; PUangle(Ratio) = 0.0 to 0.5
;; if( Y < 0 )
;; PUAngle(Ratio) = 1.0 - PUAngle(Ratio);
;;
;; PUangle(Ratio) = 0.0 (0deg) to 1.0 (360deg)
;;
;;---------------------------------------------------------------------------
;; Benchmark:
;; * Code executes from 0-wait SARAM block.
;; * _IQatan2 & _IQdiv Tables located in seperate 0-wait SARAM or 1-wait ROM block.
;; * Stack located in seperate 0-wait SARAM block.
;; * Includes LCR/LRETR.
;;
;; Cycles = 121 (if 16 <= Q <= 29 and _IQatan2 & _IQdiv Tables in 0-wait SARAM)
;; = 124 (if 1 <= Q <= 15 and _IQatan2 & _IQdiv Tables in 0-wait SARAM)
;;
;; Cycles = 127 (if 16 <= Q <= 29 and _IQatan2Table & _IQdivTable in 1-wait ROM)
;; = 130 (if 1 <= Q <= 15 and _IQatan2Table & _IQdivTable in 1-wait ROM)
;;
;;===========================================================================
IQNatan2PU .macro q_value
;; Perform Step 1:
SETC OVM ; Make sure overflow mode is on
MOVL *SP++,ACC ; SP-6 = Y
ABS ACC ; ACC = abs(Y)
MOVL XT,@ACC ; XT = abs(Y)
MOVL ACC,*-SP[6] ; ACC = X
ABS ACC ; ACC = abs(X)
MOVL P,@ACC ; P = abs(X)
SUBL ACC,@XT ; ACC = abs(X) - abs(Y)
MOVL *SP++,ACC ; SP-4 = abs(X) - abs(Y)
MOVL ACC,@XT ; ACC = abs(Y)
MINL ACC,@P ; if( abs(Y) > abs(X) ), ACC = abs(X)
MOVL *SP++,ACC ; SP-2 = Numerator
MOVL ACC,@P ; ACC = abs(X)
MAXL ACC,@XT ; if( abs(X) < abs(Y) ), ACC = abs(Y)
; ACC = Denominator
; SP-10 = X
; SP-8 = Return Address
; SP-6 = Y
; SP-4 = abs(X) - abs(Y)
; SP-2 = Numerator
;; Perform Step 2:
CSB ACC ; Count sign bits, T = n
LSLL ACC,T ; ACC = Dnorm (Q31)
MOVL @XAR4,ACC ; Temp save "Dnorm"
LSR AH,#6
MOVB @AH,#0xFE,EQ ; If Den = 0, load zero value
MOVZ AR0,@AH ; AR0 = index into _IQdivTable
MOVL XAR7,#(_IQdivTable-254)
MOVB AH,#(62 - q_value) ; AH = 62 - q_value
SUBR @T,AH ; T = 62 - q_value - n
MOVL XAR5,@XT ; AR5H = 62 - q_value - n
MOVL XT,*+XAR7[AR0] ; (1) XT = initial Dm estimate (Q29)
MOV @AH,#0x4000
MOVB AL,#0
MOVL XAR6,@ACC ; XAR6 = 2.0 (Q29)
IMPYL P,XT,@XAR4 ; (2)
QMPYL ACC,XT,@XAR4 ; (2) ACC:P = Dm*Dnorm (Q60)
LSL64 ACC:P,#1 ; (3) ACC:P = Dm*Dnorm (Q61)
SUBL ACC,@XAR6 ; (4) ACC = Dm*Dnorm - 2.0 (Q29)
NEG ACC ; (5) ACC = 2.0 - Dm*Dnorm (Q29)
IMPYL P,XT,@ACC ; (6)
QMPYL ACC,XT,@ACC ; (6) ACC:P = Dm(2.0 - Dm*Dnorm) (Q58)
LSL64 ACC:P,#3 ; (7) ACC:P = Dm(2.0 - Dm*Dnorm) (Q61)
MOVL XT,@ACC ; (1) XT = Dm (Q29)
IMPYL P,XT,@XAR4 ; (2)
QMPYL ACC,XT,@XAR4 ; (2) ACC:P = Dm*Dnorm (Q60)
LSL64 ACC:P,#1 ; (3) ACC:P = Dm*Dnorm (Q61)
SUBL ACC,@XAR6 ; (4) ACC = Dm*Dnorm - 2.0 (Q29)
NEG ACC ; (5) ACC = 2.0 - Dm*Dnorm (Q29)
IMPYL P,XT,@ACC ; (6)
QMPYL ACC,XT,@ACC ; (6) ACC:P = Dm(2.0 - Dm*Dnorm) (Q58)
LSL64 ACC:P,#3 ; (7) ACC:P = Dm(2.0 - Dm*Dnorm) (Q61)
MOVL XT,*-SP[2] ; XT = Numerator
IMPYL P,XT,@ACC
QMPYL ACC,XT,@ACC ; ACC:P = N * Dm
LSL64 ACC:P,#2
MOVL XT,@XAR5 ; T = 62 - q_value - n
ASR64 ACC:P,T ; scale back result
; P = Ratio in IQ format
;; Perform Step 3:
MOVL XT,@P ; XT = Ratio
MOVB ACC,#64 ; ACC = 64 = Elements In Table
IMPYL P,XT,@ACC
QMPYL ACC,XT,@ACC ; ACC:P = 64*Ratio
.if q_value >= 16
LSL64 ACC:P,#(32 - q_value) ; ACC = int(64*Ratio)
.else
LSL64 ACC:P,#16
LSL64 ACC:P,#(16 - q_value) ; ACC = int(64*Ratio)
.endif
MPY P,@AL,#6 ; P = 6 * int(64*Ratio)
MOVZ AR0,@PL ; AR0 = _IQatan2Table index
MOVL XAR5,#_IQatan2Table ; Point to A0
MOVL XAR6,#_IQatan2Table+2 ; Point to A1
MOVL XAR7,#_IQatan2Table+4 ; Point to A2
;; Perform Step 4:
IMPYL P,XT,*+XAR7[AR0]
QMPYL ACC,XT,*+XAR7[AR0] ; ACC:P = A2*Ratio (q_value + Q30)
.if q_value >= 16
LSL64 ACC:P,#(32-q_value) ; ACC = A2*Ratio (Q30)
.else
LSL64 ACC:P,#16
LSL64 ACC:P,#(16-q_value) ; ACC = A2*Ratio (Q30)
.endif
ADDL ACC,*+XAR6[AR0] ; ACC = A1 + A2*Ratio (Q30)
IMPYL P,XT,@ACC
QMPYL ACC,XT,@ACC ; ACC:P = Ratio(A1 + A2*Ratio) (q_value + Q30)
.if q_value >= 16
LSL64 ACC:P,#(32-q_value) ; ACC:P = Ratio(A1 + A2*Ratio) (Q30)
.else
LSL64 ACC:P,#16
LSL64 ACC:P,#(16-q_value) ; ACC:P = Ratio(A1 + A2*Ratio) (Q30)
.endif
ADDL ACC,*+XAR5[AR0] ; ACC = A0 + Ratio(A1 + A2*Ratio) (Q30)
.if (q_value >= 14) & (q_value <= 29)
LSR64 ACC:P,#(30-q_value) ; ACC = A0 + Ratio(A1 + A2*Ratio) (q_value)
.endif
.if q_value <= 13
LSR64 ACC:P,#16
LSR64 ACC:P,#(14-q_value) ; ACC = A0 + Ratio(A1 + A2*Ratio) (q_value)
.endif
;; Perform Step 5:
MOVL XAR7,#(_IQatan2HalfPITable + q_value*2)
MOVL XAR6,*XAR7 ; XAR6 = PI/2
MOVL P,@ACC ; P = arctan(Ratio)
NEG ACC ; ACC = -arctan(Ratio)
TBIT *-SP[9],#15 ; Test if(X >= 0)
MOVL @P,ACC,NTC ; if(X >= 0), P = -arctan(Ratio)
MOVL ACC,@P
NEG ACC ; ACC = -P
TBIT *-SP[3],#15 ; Test if(abs(X) >= abs(Y))
MOVL @P,ACC,NTC ; if(abs(X) >= abs(Y)), P = -P
MOVL ACC,@XAR6 ; ACC = PI/2
NEG ACC ; ACC = -PI/2
TBIT *-SP[9],#15 ; Test if(X < 0)
NEGTC ACC ; if(X < 0), ACC = PI/2
ADDL ACC,@XAR6 ; ACC = ACC + PI/2
TBIT *-SP[3],#15 ; Test if(abs(X) >= abs(Y))
MOVL @XAR6,ACC,NTC ; if(abs(X) >= abs(Y)), XAR6 = ACC
MOVL ACC,@XAR6
ADDL ACC,@P ; ACC = arctan(abs(Y),X)
;; Perform Step 6:
MOVL XT,@ACC
MOV @PL,#0x60DC ; P = 1/(2*pi) (Q32)
MOV @PH,#0x28BE
QMPYL P,XT,@P ; P = arctan(Ratio) * 1/(2*pi) = PUangle (q_value)
.if q_value <= 15 ; ACC = 1.0 (q_value)
MOV @AL,#(1 << q_value)
MOV @AH,#0x0000
.endif
.if q_value == 16
MOV @AL,#0x0000
MOV @AH,#0x0001
.endif
.if q_value >= 17
MOV @AL,#0x0000
MOV @AH,#(1 << (q_value - 16))
.endif
SUBL ACC,@P ; ACC = 1.0 - PUangle
TBIT *-SP[5],#15 ; Test if(Y < 0)
MOVL @P,ACC,TC ; if(Y < 0), P = 1.0 - PUangle
MOVL ACC,@P ; ACC = PUangle
SUBB SP,#6 ; Deallocate stack
CLRC OVM ; Turn off overflow mode
LRETR
.endm
;; The value "GLOBAL_Q" needs to be supplied by the assembler
;; using the "-dGLOBAL_Q=q_value" directive:
.sect "IQmath"
.ref _IQatan2HalfPITable
.ref _IQatan2Table
.ref _IQdivTable
.if GLOBAL_Q == 30
.def _IQ30atan2PU
_IQ30atan2PU:
IQNatan2PU GLOBAL_Q
.endif
.if GLOBAL_Q == 29
.def _IQ29atan2PU
_IQ29atan2PU:
IQNatan2PU GLOBAL_Q
.endif
.if GLOBAL_Q == 28
.def _IQ28atan2PU
_IQ28atan2PU:
IQNatan2PU GLOBAL_Q
.endif
.if GLOBAL_Q == 27
.def _IQ27atan2PU
_IQ27atan2PU:
IQNatan2PU GLOBAL_Q
.endif
.if GLOBAL_Q == 26
.def _IQ26atan2PU
_IQ26atan2PU:
IQNatan2PU GLOBAL_Q
.endif
.if GLOBAL_Q == 25
.def _IQ25atan2PU
_IQ25atan2PU:
IQNatan2PU GLOBAL_Q
.endif
.if GLOBAL_Q == 24
.def _IQ24atan2PU
_IQ24atan2PU:
IQNatan2PU GLOBAL_Q
.endif
.if GLOBAL_Q == 23
.def _IQ23atan2PU
_IQ23atan2PU:
IQNatan2PU GLOBAL_Q
.endif
.if GLOBAL_Q == 22
.def _IQ22atan2PU
_IQ22atan2PU:
IQNatan2PU GLOBAL_Q
.endif
.if GLOBAL_Q == 21
.def _IQ21atan2PU
_IQ21atan2PU:
IQNatan2PU GLOBAL_Q
.endif
.if GLOBAL_Q == 20
.def _IQ20atan2PU
_IQ20atan2PU:
IQNatan2PU GLOBAL_Q
.endif
.if GLOBAL_Q == 19
.def _IQ19atan2PU
_IQ19atan2PU:
IQNatan2PU GLOBAL_Q
.endif
.if GLOBAL_Q == 18
.def _IQ18atan2PU
_IQ18atan2PU:
IQNatan2PU GLOBAL_Q
.endif
.if GLOBAL_Q == 17
.def _IQ17atan2PU
_IQ17atan2PU:
IQNatan2PU GLOBAL_Q
.endif
.if GLOBAL_Q == 16
.def _IQ16atan2PU
_IQ16atan2PU:
IQNatan2PU GLOBAL_Q
.endif
.if GLOBAL_Q == 15
.def _IQ15atan2PU
_IQ15atan2PU:
IQNatan2PU GLOBAL_Q
.endif
.if GLOBAL_Q == 14
.def _IQ14atan2PU
_IQ14atan2PU:
IQNatan2PU GLOBAL_Q
.endif
.if GLOBAL_Q == 13
.def _IQ13atan2PU
_IQ13atan2PU:
IQNatan2PU GLOBAL_Q
.endif
.if GLOBAL_Q == 12
.def _IQ12atan2PU
_IQ12atan2PU:
IQNatan2PU GLOBAL_Q
.endif
.if GLOBAL_Q == 11
.def _IQ11atan2PU
_IQ11atan2PU:
IQNatan2PU GLOBAL_Q
.endif
.if GLOBAL_Q == 10
.def _IQ10atan2PU
_IQ10atan2PU:
IQNatan2PU GLOBAL_Q
.endif
.if GLOBAL_Q == 9
.def _IQ9atan2PU
_IQ9atan2PU:
IQNatan2PU GLOBAL_Q
.endif
.if GLOBAL_Q == 8
.def _IQ8atan2PU
_IQ8atan2PU:
IQNatan2PU GLOBAL_Q
.endif
.if GLOBAL_Q == 7
.def _IQ7atan2PU
_IQ7atan2PU:
IQNatan2PU GLOBAL_Q
.endif
.if GLOBAL_Q == 6
.def _IQ6atan2PU
_IQ6atan2PU:
IQNatan2PU GLOBAL_Q
.endif
.if GLOBAL_Q == 5
.def _IQ5atan2PU
_IQ5atan2PU:
IQNatan2PU GLOBAL_Q
.endif
.if GLOBAL_Q == 4
.def _IQ4atan2PU
_IQ4atan2PU:
IQNatan2PU GLOBAL_Q
.endif
.if GLOBAL_Q == 3
.def _IQ3atan2PU
_IQ3atan2PU:
IQNatan2PU GLOBAL_Q
.endif
.if GLOBAL_Q == 2
.def _IQ2atan2PU
_IQ2atan2PU:
IQNatan2PU GLOBAL_Q
.endif
.if GLOBAL_Q == 1
.def _IQ1atan2PU
_IQ1atan2PU:
IQNatan2PU GLOBAL_Q
.endif
;;###########################################################################
;; No More.
;;###########################################################################
@@ -0,0 +1,332 @@
;;###########################################################################
;;
;; FILE: IQNcos.asm
;;
;; TITLE: C Callable IQ Math Function
;;
;;###########################################################################
;;
;; Ver | Date | Who | Description of changes
;; =====|=============|=======|==============================================
;; 1.2 | 11 Oct 2001 | A. T. | Original Release.
;; -----|-------------|-------|----------------------------------------------
;; 1.3 | 19 Nov 2001 | A. T. | No changes.
;; -----|-------------|-------|----------------------------------------------
;; 1.4 | 17 May 2002 | A. T. | Improved accuracy from 16 to about 29 bits
;; | | | (thanks to Settu for writing original code).
;; | | | Modified code so that calculation constants
;; | | | are embedded in code and not in table.
;; -----|-------------|-------|----------------------------------------------
;; | | |
;;
;;###########################################################################
;;===========================================================================
;; Function: _IQNcos
;;===========================================================================
;;
;; C Usage: extern long _IQNcos(long Rad);
;;
;;---------------------------------------------------------------------------
;;
;; On Entry: ACC = Radian in IQ format
;;
;; Regs Used: XAR7, XAR6, XAR5, XAR4, XAR0
;; XT, P
;;
;; On Exit: ACC = cos(Radian) result in IQ format
;;
;; Q range: 30 to 1
;;
;;---------------------------------------------------------------------------
;; Algorithm: The "cos" value is calculated as follows:
;;
;; 1) The offset into a 512 word sin/cos table is calculated:
;;
;; k = 0x1FF & (int(Radian*512/(2*pi)))
;;
;;
;; 2) The fractional component between table samples is
;; calculated:
;;
;; x = fract(Radian*512/2*pi) * (2*pi)/512
;;
;; 3) The output sine value is calculated as follows:
;;
;; cos(Radian) = C(k) + x*(-S(k) + x*(-0.5*C(k) + 0.166*x*S(k)))
;;
;; where S(k) = Sin table value at offset "k"
;; C(k) = Cos table value at offset "k"
;;
;; Using the above method, with a 512x32 full wave sin/cos table,
;; will give an accuracy of approximately 29 bits.
;;
;;---------------------------------------------------------------------------
;; Benchmark:
;;
;; Assumptions: * Code executes from 0-wait SARAM block.
;; * _IQsin/cosTable located in seperate SARAM or ROM block.
;;
;; Cycles = 43 ( Q == 30, tables in 0-wait SARAM, includes LCR/LRETR)
;; = 44 (29 >= Q >= 25, tables in 0-wait SARAM, includes LCR/LRETR)
;; = 43 (24 >= Q >= 14, tables in 0-wait SARAM, includes LCR/LRETR)
;; = 44 (13 >= Q >= 0, tables in 0-wait SARAM, includes LCR/LRETR)
;;
;; Cycles = 45 ( Q == 30, tables in 1-wait ROM, includes LCR/LRETR)
;; = 46 (29 >= Q >= 25, tables in 1-wait ROM, includes LCR/LRETR)
;; = 45 (24 >= Q >= 14, tables in 1-wait ROM, includes LCR/LRETR)
;; = 46 (13 >= Q >= 0, tables in 1-wait ROM, includes LCR/LRETR)
;;
;;===========================================================================
K1 .set 02AAAh ; 1/6 in Q16
IQNcos .macro q_value
MOV *SP++,#0x3F6B ; (2*pi)/512 = 0x03243F6B (Q32)
MOV *SP++,#0x0324
MOV *SP++,#0xC1B7 ; 512/(2*pi) = 0x517CC1B7 (I8Q24)
MOV *SP++,#0x517C
MOVL XAR6,#_IQcosTable
MOVL XAR7,#_IQsinTable
SETC OVM ; turn overflow mode on
ABS ACC ; abs(Rad), TC = sign
MOVL XT,*--SP ; XT = 512/(2*pi) (I8Q24)
IMPYL P,XT,@ACC
QMPYL ACC,XT,@ACC ; ACC:P = Rad * 512 / (2pi)
.if q_value >= 25
ASR64 ACC:P,#16
ASR64 ACC:P,#(q_value - 24) ; ACC = Integer, P = fract
.endif
.if (q_value <= 24) & (q_value >= 9)
ASR64 ACC:P,#(q_value - 8) ; ACC = Integer, P = fract
.endif
.if q_value <= 7
LSL64 ACC:P,#(8 - q_value) ; ACC = Integer, P = fract
.endif
AND @AL,#0x1FF
LSL AL,#1
MOVZ AR0,@AL ; AR0 = Index into "sin/cos" table = k
MOVL XT,*--SP ; XT = (2*pi)/512 (Q32)
QMPYUL P,XT,@P ; P = x (Q32)
MOVB ACC,#0
MOVL XT,@P ; XT = x (Q32)
MOVL XAR4,*+XAR6[AR0] ; XAR4 = C(k) (Q30)
MOVL XAR5,*+XAR7[AR0] ; XAR5 = S(k) (Q30)
SUBL ACC,@XAR4 ; ACC = -C(k) (Q30)
ASR64 ACC:P,#1 ; ACC = -0.5*C(k) (Q30)
QMPYL P,XT,@XAR5 ; P = x*S(k) (Q30)
CLRC OVM ; Turn overflow mode off
MPY P,@PH,#K1 ; P = 0.166*x*S(k) (Q30)
ADDL ACC,P ; ACC = -0.5*C(k) + 0.166*x*S(k) (Q30)
QMPYL ACC,XT,@ACC ; ACC = x*(-0.5*C(k) + 0.166*x*S(k)) (Q30)
SUBL ACC,@XAR5 ; ACC = -S(k) + x*(-0.5*C(k) + 0.166*x*S(k)) (Q30)
QMPYL ACC,XT,@ACC ; ACC = x*(-S(k) + x*(-0.5*C(k) + 0.166*x*S(k))) (Q30)
ADDL ACC,@XAR4 ; ACC = C(k) + x*(-S(k) + x*(-0.5*C(k) + 0.166*x*S(k))) (Q30)
.if (q_value <= 29) & (q_value >= 14)
ASR64 ACC:P,#(30 - q_value) ; Scale back to required IQ value
.endif
.if q_value <= 13
ASR64 ACC:P,#16
ASR64 ACC:P,#(14 - q_value) ; Scale back to required IQ value
.endif
LRETR
.endm
;; The value "GLOBAL_Q" needs to be supplied by the assembler
;; using the "-dGLOBAL_Q=q_value" directive:
.sect "IQmath"
.ref _IQsinTable
.ref _IQcosTable
.if GLOBAL_Q == 30
.def _IQ30cos
_IQ30cos:
IQNcos GLOBAL_Q
.endif
.if GLOBAL_Q == 29
.def _IQ29cos
_IQ29cos:
IQNcos GLOBAL_Q
.endif
.if GLOBAL_Q == 28
.def _IQ28cos
_IQ28cos:
IQNcos GLOBAL_Q
.endif
.if GLOBAL_Q == 27
.def _IQ27cos
_IQ27cos:
IQNcos GLOBAL_Q
.endif
.if GLOBAL_Q == 26
.def _IQ26cos
_IQ26cos:
IQNcos GLOBAL_Q
.endif
.if GLOBAL_Q == 25
.def _IQ25cos
_IQ25cos:
IQNcos GLOBAL_Q
.endif
.if GLOBAL_Q == 24
.def _IQ24cos
_IQ24cos:
IQNcos GLOBAL_Q
.endif
.if GLOBAL_Q == 23
.def _IQ23cos
_IQ23cos:
IQNcos GLOBAL_Q
.endif
.if GLOBAL_Q == 22
.def _IQ22cos
_IQ22cos:
IQNcos GLOBAL_Q
.endif
.if GLOBAL_Q == 21
.def _IQ21cos
_IQ21cos:
IQNcos GLOBAL_Q
.endif
.if GLOBAL_Q == 20
.def _IQ20cos
_IQ20cos:
IQNcos GLOBAL_Q
.endif
.if GLOBAL_Q == 19
.def _IQ19cos
_IQ19cos:
IQNcos GLOBAL_Q
.endif
.if GLOBAL_Q == 18
.def _IQ18cos
_IQ18cos:
IQNcos GLOBAL_Q
.endif
.if GLOBAL_Q == 17
.def _IQ17cos
_IQ17cos:
IQNcos GLOBAL_Q
.endif
.if GLOBAL_Q == 16
.def _IQ16cos
_IQ16cos:
IQNcos GLOBAL_Q
.endif
.if GLOBAL_Q == 15
.def _IQ15cos
_IQ15cos:
IQNcos GLOBAL_Q
.endif
.if GLOBAL_Q == 14
.def _IQ14cos
_IQ14cos:
IQNcos GLOBAL_Q
.endif
.if GLOBAL_Q == 13
.def _IQ13cos
_IQ13cos:
IQNcos GLOBAL_Q
.endif
.if GLOBAL_Q == 12
.def _IQ12cos
_IQ12cos:
IQNcos GLOBAL_Q
.endif
.if GLOBAL_Q == 11
.def _IQ11cos
_IQ11cos:
IQNcos GLOBAL_Q
.endif
.if GLOBAL_Q == 10
.def _IQ10cos
_IQ10cos:
IQNcos GLOBAL_Q
.endif
.if GLOBAL_Q == 9
.def _IQ9cos
_IQ9cos:
IQNcos GLOBAL_Q
.endif
.if GLOBAL_Q == 8
.def _IQ8cos
_IQ8cos:
IQNcos GLOBAL_Q
.endif
.if GLOBAL_Q == 7
.def _IQ7cos
_IQ7cos:
IQNcos GLOBAL_Q
.endif
.if GLOBAL_Q == 6
.def _IQ6cos
_IQ6cos:
IQNcos GLOBAL_Q
.endif
.if GLOBAL_Q == 5
.def _IQ5cos
_IQ5cos:
IQNcos GLOBAL_Q
.endif
.if GLOBAL_Q == 4
.def _IQ4cos
_IQ4cos:
IQNcos GLOBAL_Q
.endif
.if GLOBAL_Q == 3
.def _IQ3cos
_IQ3cos:
IQNcos GLOBAL_Q
.endif
.if GLOBAL_Q == 2
.def _IQ2cos
_IQ2cos:
IQNcos GLOBAL_Q
.endif
.if GLOBAL_Q == 1
.def _IQ1cos
_IQ1cos:
IQNcos GLOBAL_Q
.endif
;;###########################################################################
;; No More.
;;###########################################################################
@@ -0,0 +1,320 @@
;;###########################################################################
;;
;; FILE: IQNcosPU.asm
;;
;; TITLE: C Callable IQ Math Function
;;
;;###########################################################################
;;
;; Ver | Date | Who | Description of changes
;; =====|=============|=======|==============================================
;; 1.4 | 17 May 2002 | A. T. | Original Release.
;; -----|-------------|-------|----------------------------------------------
;; | | |
;;
;;###########################################################################
;;===========================================================================
;; Function: _IQNcosPU
;;===========================================================================
;;
;; C Usage: extern long _IQNcosPU(long PU);
;;
;;---------------------------------------------------------------------------
;;
;; On Entry: ACC = Per Unit in IQ format
;;
;; Regs Used: XAR7, XAR6, XAR5, XAR4, XAR0
;; XT, P
;;
;; On Exit: ACC = cos(PU) result in IQ format
;;
;; Q range: 30 to 1
;;
;;---------------------------------------------------------------------------
;; Algorithm: The "cos" value is calculated as follows:
;;
;; 1) The offset into a 512 word sin/cos table is calculated:
;;
;; k = 0x1FF & (int(PU*512))
;;
;;
;; 2) The fractional component between table samples is
;; calculated:
;;
;; x = fract(PU*512) * (2*pi)/512
;;
;; 3) The output sine value is calculated as follows:
;;
;; cos(Radian) = C(k) + x*(-S(k) + x*(-0.5*C(k) + 0.166*x*S(k)))
;;
;; where S(k) = Sin table value at offset "k"
;; C(k) = Cos table value at offset "k"
;;
;; Using the above method, with a 512x32 full wave sin/cos table,
;; will give an accuracy of approximately 29 bits.
;;
;;---------------------------------------------------------------------------
;; Benchmark:
;;
;; Assumptions: * Code executes from 0-wait SARAM block.
;; * _IQsin/cosTable located in seperate SARAM or ROM block.
;;
;; Cycles = 37 ( Q == 30, tables in 0-wait SARAM, includes LCR/LRETR)
;; = 38 (29 >= Q >= 26, tables in 0-wait SARAM, includes LCR/LRETR)
;; = 37 (25 >= Q >= 14, tables in 0-wait SARAM, includes LCR/LRETR)
;; = 38 (13 >= Q >= 0, tables in 0-wait SARAM, includes LCR/LRETR)
;;
;; Cycles = 39 ( Q == 30, tables in 1-wait ROM, includes LCR/LRETR)
;; = 40 (29 >= Q >= 26, tables in 1-wait ROM, includes LCR/LRETR)
;; = 39 (25 >= Q >= 14, tables in 1-wait ROM, includes LCR/LRETR)
;; = 40 (13 >= Q >= 0, tables in 1-wait ROM, includes LCR/LRETR)
;;
;;===========================================================================
K1 .set 02AAAh ; 1/6 in Q16
IQNcosPU .macro q_value
MOV *SP++,#0x3F6B ; (2*pi)/512 = 0x03243F6B (Q32)
MOV *SP++,#0x0324
MOVL XAR6,#_IQcosTable
MOVL XAR7,#_IQsinTable
SETC OVM ; turn overflow mode on
ABS ACC ; abs(Rad), TC = sign
MPYB P,T,#0
.if q_value >= 26
ASR64 ACC:P,#16
ASR64 ACC:P,#(q_value - 25) ; ACC = Integer, P = fract
.endif
.if (q_value <= 25) & (q_value >= 10)
ASR64 ACC:P,#(q_value - 9) ; ACC = Integer, P = fract
.endif
.if q_value <= 8
LSL64 ACC:P,#(9 - q_value) ; ACC = Integer, P = fract
.endif
AND @AL,#0x1FF
LSL AL,#1
MOVZ AR0,@AL ; AR0 = Index into "sin/cos" table = k
MOVL XT,*--SP ; XT = (2*pi)/512 (Q32)
QMPYUL P,XT,@P ; P = x (Q32)
MOVB ACC,#0
MOVL XT,@P ; XT = x (Q32)
MOVL XAR4,*+XAR6[AR0] ; XAR4 = C(k) (Q30)
MOVL XAR5,*+XAR7[AR0] ; XAR5 = S(k) (Q30)
SUBL ACC,@XAR4 ; ACC = -C(k) (Q30)
ASR64 ACC:P,#1 ; ACC = -0.5*C(k) (Q30)
QMPYL P,XT,@XAR5 ; P = x*S(k) (Q30)
CLRC OVM ; Turn overflow mode off
MPY P,@PH,#K1 ; P = 0.166*x*S(k) (Q30)
ADDL ACC,P ; ACC = -0.5*C(k) + 0.166*x*S(k) (Q30)
QMPYL ACC,XT,@ACC ; ACC = x*(-0.5*C(k) + 0.166*x*S(k)) (Q30)
SUBL ACC,@XAR5 ; ACC = -S(k) + x*(-0.5*C(k) + 0.166*x*S(k)) (Q30)
QMPYL ACC,XT,@ACC ; ACC = x*(-S(k) + x*(-0.5*C(k) + 0.166*x*S(k))) (Q30)
ADDL ACC,@XAR4 ; ACC = C(k) + x*(-S(k) + x*(-0.5*C(k) + 0.166*x*S(k))) (Q30)
.if (q_value <= 29) & (q_value >= 14)
ASR64 ACC:P,#(30 - q_value) ; Scale back to required IQ value
.endif
.if q_value <= 13
ASR64 ACC:P,#16
ASR64 ACC:P,#(14 - q_value) ; Scale back to required IQ value
.endif
LRETR
.endm
;; The value "GLOBAL_Q" needs to be supplied by the assembler
;; using the "-dGLOBAL_Q=q_value" directive:
.sect "IQmath"
.ref _IQsinTable
.ref _IQcosTable
.if GLOBAL_Q == 30
.def _IQ30cosPU
_IQ30cosPU:
IQNcosPU GLOBAL_Q
.endif
.if GLOBAL_Q == 29
.def _IQ29cosPU
_IQ29cosPU:
IQNcosPU GLOBAL_Q
.endif
.if GLOBAL_Q == 28
.def _IQ28cosPU
_IQ28cosPU:
IQNcosPU GLOBAL_Q
.endif
.if GLOBAL_Q == 27
.def _IQ27cosPU
_IQ27cosPU:
IQNcosPU GLOBAL_Q
.endif
.if GLOBAL_Q == 26
.def _IQ26cosPU
_IQ26cosPU:
IQNcosPU GLOBAL_Q
.endif
.if GLOBAL_Q == 25
.def _IQ25cosPU
_IQ25cosPU:
IQNcosPU GLOBAL_Q
.endif
.if GLOBAL_Q == 24
.def _IQ24cosPU
_IQ24cosPU:
IQNcosPU GLOBAL_Q
.endif
.if GLOBAL_Q == 23
.def _IQ23cosPU
_IQ23cosPU:
IQNcosPU GLOBAL_Q
.endif
.if GLOBAL_Q == 22
.def _IQ22cosPU
_IQ22cosPU:
IQNcosPU GLOBAL_Q
.endif
.if GLOBAL_Q == 21
.def _IQ21cosPU
_IQ21cosPU:
IQNcosPU GLOBAL_Q
.endif
.if GLOBAL_Q == 20
.def _IQ20cosPU
_IQ20cosPU:
IQNcosPU GLOBAL_Q
.endif
.if GLOBAL_Q == 19
.def _IQ19cosPU
_IQ19cosPU:
IQNcosPU GLOBAL_Q
.endif
.if GLOBAL_Q == 18
.def _IQ18cosPU
_IQ18cosPU:
IQNcosPU GLOBAL_Q
.endif
.if GLOBAL_Q == 17
.def _IQ17cosPU
_IQ17cosPU:
IQNcosPU GLOBAL_Q
.endif
.if GLOBAL_Q == 16
.def _IQ16cosPU
_IQ16cosPU:
IQNcosPU GLOBAL_Q
.endif
.if GLOBAL_Q == 15
.def _IQ15cosPU
_IQ15cosPU:
IQNcosPU GLOBAL_Q
.endif
.if GLOBAL_Q == 14
.def _IQ14cosPU
_IQ14cosPU:
IQNcosPU GLOBAL_Q
.endif
.if GLOBAL_Q == 13
.def _IQ13cosPU
_IQ13cosPU:
IQNcosPU GLOBAL_Q
.endif
.if GLOBAL_Q == 12
.def _IQ12cosPU
_IQ12cosPU:
IQNcosPU GLOBAL_Q
.endif
.if GLOBAL_Q == 11
.def _IQ11cosPU
_IQ11cosPU:
IQNcosPU GLOBAL_Q
.endif
.if GLOBAL_Q == 10
.def _IQ10cosPU
_IQ10cosPU:
IQNcosPU GLOBAL_Q
.endif
.if GLOBAL_Q == 9
.def _IQ9cosPU
_IQ9cosPU:
IQNcosPU GLOBAL_Q
.endif
.if GLOBAL_Q == 8
.def _IQ8cosPU
_IQ8cosPU:
IQNcosPU GLOBAL_Q
.endif
.if GLOBAL_Q == 7
.def _IQ7cosPU
_IQ7cosPU:
IQNcosPU GLOBAL_Q
.endif
.if GLOBAL_Q == 6
.def _IQ6cosPU
_IQ6cosPU:
IQNcosPU GLOBAL_Q
.endif
.if GLOBAL_Q == 5
.def _IQ5cosPU
_IQ5cosPU:
IQNcosPU GLOBAL_Q
.endif
.if GLOBAL_Q == 4
.def _IQ4cosPU
_IQ4cosPU:
IQNcosPU GLOBAL_Q
.endif
.if GLOBAL_Q == 3
.def _IQ3cosPU
_IQ3cosPU:
IQNcosPU GLOBAL_Q
.endif
.if GLOBAL_Q == 2
.def _IQ2cosPU
_IQ2cosPU:
IQNcosPU GLOBAL_Q
.endif
.if GLOBAL_Q == 1
.def _IQ1cosPU
_IQ1cosPU:
IQNcosPU GLOBAL_Q
.endif
;;###########################################################################
;; No More.
;;###########################################################################
@@ -0,0 +1,334 @@
;;###########################################################################
;;
;; FILE: IQNdiv.asm
;;
;; TITLE: C Callable IQ Divide Math Function
;;
;;###########################################################################
;;
;; Ver | Date | Who | Description of changes
;; =====|==============|=======|=============================================
;; 1.2 | 11 Oct 2001 | A. T. | Original Release.
;; -----|--------------|-------|---------------------------------------------
;; 1.3 | 19 Nov 2001 | A. T. | Improved accuracy of function.
;; -----|--------------|-------|---------------------------------------------
;; 1.4 | 17 May 2002 | A. T. | Modified result when Den==0. Gives most
;; | | | positive result rather then zero.
;; -----|--------------|-------|---------------------------------------------
;; | | |
;;
;;###########################################################################
;;===========================================================================
;; Function: _IQNdiv
;;===========================================================================
;;
;; C Usage: extern long _IQNdiv(long Num, long Den);
;;
;;---------------------------------------------------------------------------
;;
;; On Entry: ACC = Numerator in IQ format
;; [SP-4] = Denominator in IQ format
;;
;; Regs Used: XAR7, XAR6, XAR5, XAR4, XAR0
;; XT, P
;;
;; On Exit: ACC = Num/Den result in IQ format
;; = 0x7FFFFFFF if +ve overflow
;; = 0x80000000 if -ve overflow
;; = 0x7FFFFFFF if Den == 0
;;
;; Q range: 30 to 1
;;
;;---------------------------------------------------------------------------
;; Algorithm: The procedure for calculating Y = N/D is as follows:
;;
;; Step 1) Normalize Input:
;;
;; Dnorm = D * 2^n
;;
;; Step 2) Obtain initial estimate of Dm = 1/Dnorm from
;; "_IQdivTable" using the upper 8-bits of the
;; normalized value.
;;
;; Step 3) Use Newton-Raphson algorithm to improve accuracy.
;; Repeat following equation two times. First iteration
;; gives 16-bit accuracy. Second iteration gives 32-bit
;; accuracy:
;;
;; Dm = 2(Dm - Dm^2*Dnorm/2)
;;
;; Step 4) Multiply by numerator value:
;;
;; Ym = N * Dm
;;
;; Step 5) Denormalize result, saturate and round:
;;
;; Y = Ym >> n
;;
;;---------------------------------------------------------------------------
;; Benchmark:
;;
;; Assumptions:
;; * Code executes from 0-wait SARAM block.
;; * _IQdiv Tables located in seperate 0-wait SARAM or 1-wait ROM block.
;; * Stack located in seperate 0-wait SARAM block.
;; * Includes LCR/LRETR.
;;
;; Cycles = 62 (if _IQdiv Tables in 0-wait SARAM)
;; 70 (if _IQdiv Tables in 1-wait ROM)
;; 22 (if Den == 0)
;;
;;===========================================================================
IQNdiv .macro q_value
MOVL *SP++,ACC ; Temp save Numerator on stack
MOVL ACC,*-SP[6] ; Load ACC with Denominator
CSB ACC ; Count sign bits, T = n
LSLL ACC,T ; ACC = Dnorm (Q31)
MOVL @XAR4,ACC ; Temp save "Dnorm"
LSR AH,#6
SBF $10,EQ ; If Den = 0, exit and load max value
MOVZ AR0,@AH ; AR0 = index into _IQdivTable
MOVL XAR7,#(_IQdivTable-254)
MOVB AH,#(61 - q_value) ; AH = 61 - q_value
SUBR @T,AH ; T = 61 - q_value - n
MOVL XAR5,@XT ; AR5H = 61 - q_value - n
MOVL XT,*+XAR7[AR0] ; (1) XT = initial Dm estimate (Q29)
MOVL XAR7,#_IQdivRoundSatTable
MOVL XAR6,*XAR7++ ; XAR6 = 2.0 (Q29)
IMPYL P,XT,@XAR4 ; (2)
QMPYL ACC,XT,@XAR4 ; (2) ACC:P = Dm*Dnorm (Q60)
LSL64 ACC:P,#1 ; (3) ACC:P = Dm*Dnorm (Q61)
SUBL ACC,@XAR6 ; (4) ACC = Dm*Dnorm - 2.0 (Q29)
NEG ACC ; (5) ACC = 2.0 - Dm*Dnorm (Q29)
IMPYL P,XT,@ACC ; (6)
QMPYL ACC,XT,@ACC ; (6) ACC:P = Dm(2.0 - Dm*Dnorm) (Q58)
LSL64 ACC:P,#3 ; (7) ACC:P = Dm(2.0 - Dm*Dnorm) (Q61)
MOVL XT,@ACC ; (1) XT = Dm (Q29)
IMPYL P,XT,@XAR4 ; (2)
QMPYL ACC,XT,@XAR4 ; (2) ACC:P = Dm*Dnorm (Q60)
LSL64 ACC:P,#1 ; (3) ACC:P = Dm*Dnorm (Q61)
SUBL ACC,@XAR6 ; (4) ACC = Dm*Dnorm - 2.0 (Q29)
NEG ACC ; (5) ACC = 2.0 - Dm*Dnorm (Q29)
IMPYL P,XT,@ACC ; (6)
QMPYL ACC,XT,@ACC ; (6) ACC:P = Dm(2.0 - Dm*Dnorm) (Q58)
LSL64 ACC:P,#3 ; (7) ACC:P = Dm(2.0 - Dm*Dnorm) (Q61)
MOVL XT,*--SP ; XT = Numerator
IMPYL P,XT,@ACC
QMPYL ACC,XT,@ACC ; ACC:P = N * Dm
LSL64 ACC:P,#2
MOVL XT,@XAR5 ; T = 61 - q_value - n
ASR64 ACC:P,T ; scale back result
ADDUL P,*XAR7++ ; Round result
ADDCL ACC,*XAR7++
MINL ACC,*+XAR7[2] ; Saturate result
MINCUL P,*+XAR7[0]
MAXL ACC,*+XAR7[6]
MAXCUL P,*+XAR7[4]
ASR64 ACC:P,#1 ; ACC = final IQ result
MOVL ACC,@P
LRETR
$10:
MOV @AL,#0xFFFF
MOV @AH,#0x7FFF
SUBB SP,#2
LRETR
.endm
;; The value "GLOBAL_Q" needs to be supplied by the assembler
;; using the "-dGLOBAL_Q=q_value" directive:
.sect "IQmath"
.ref _IQdivTable
.ref _IQdivRoundSatTable
.if GLOBAL_Q == 30
.def _IQ30div
_IQ30div:
IQNdiv GLOBAL_Q
.endif
.if GLOBAL_Q == 29
.def _IQ29div
_IQ29div:
IQNdiv GLOBAL_Q
.endif
.if GLOBAL_Q == 28
.def _IQ28div
_IQ28div:
IQNdiv GLOBAL_Q
.endif
.if GLOBAL_Q == 27
.def _IQ27div
_IQ27div:
IQNdiv GLOBAL_Q
.endif
.if GLOBAL_Q == 26
.def _IQ26div
_IQ26div:
IQNdiv GLOBAL_Q
.endif
.if GLOBAL_Q == 25
.def _IQ25div
_IQ25div:
IQNdiv GLOBAL_Q
.endif
.if GLOBAL_Q == 24
.def _IQ24div
_IQ24div:
IQNdiv GLOBAL_Q
.endif
.if GLOBAL_Q == 23
.def _IQ23div
_IQ23div:
IQNdiv GLOBAL_Q
.endif
.if GLOBAL_Q == 22
.def _IQ22div
_IQ22div:
IQNdiv GLOBAL_Q
.endif
.if GLOBAL_Q == 21
.def _IQ21div
_IQ21div:
IQNdiv GLOBAL_Q
.endif
.if GLOBAL_Q == 20
.def _IQ20div
_IQ20div:
IQNdiv GLOBAL_Q
.endif
.if GLOBAL_Q == 19
.def _IQ19div
_IQ19div:
IQNdiv GLOBAL_Q
.endif
.if GLOBAL_Q == 18
.def _IQ18div
_IQ18div:
IQNdiv GLOBAL_Q
.endif
.if GLOBAL_Q == 17
.def _IQ17div
_IQ17div:
IQNdiv GLOBAL_Q
.endif
.if GLOBAL_Q == 16
.def _IQ16div
_IQ16div:
IQNdiv GLOBAL_Q
.endif
.if GLOBAL_Q == 15
.def _IQ15div
_IQ15div:
IQNdiv GLOBAL_Q
.endif
.if GLOBAL_Q == 14
.def _IQ14div
_IQ14div:
IQNdiv GLOBAL_Q
.endif
.if GLOBAL_Q == 13
.def _IQ13div
_IQ13div:
IQNdiv GLOBAL_Q
.endif
.if GLOBAL_Q == 12
.def _IQ12div
_IQ12div:
IQNdiv GLOBAL_Q
.endif
.if GLOBAL_Q == 11
.def _IQ11div
_IQ11div:
IQNdiv GLOBAL_Q
.endif
.if GLOBAL_Q == 10
.def _IQ10div
_IQ10div:
IQNdiv GLOBAL_Q
.endif
.if GLOBAL_Q == 9
.def _IQ9div
_IQ9div:
IQNdiv GLOBAL_Q
.endif
.if GLOBAL_Q == 8
.def _IQ8div
_IQ8div:
IQNdiv GLOBAL_Q
.endif
.if GLOBAL_Q == 7
.def _IQ7div
_IQ7div:
IQNdiv GLOBAL_Q
.endif
.if GLOBAL_Q == 6
.def _IQ6div
_IQ6div:
IQNdiv GLOBAL_Q
.endif
.if GLOBAL_Q == 5
.def _IQ5div
_IQ5div:
IQNdiv GLOBAL_Q
.endif
.if GLOBAL_Q == 4
.def _IQ4div
_IQ4div:
IQNdiv GLOBAL_Q
.endif
.if GLOBAL_Q == 3
.def _IQ3div
_IQ3div:
IQNdiv GLOBAL_Q
.endif
.if GLOBAL_Q == 2
.def _IQ2div
_IQ2div:
IQNdiv GLOBAL_Q
.endif
.if GLOBAL_Q == 1
.def _IQ1div
_IQ1div:
IQNdiv GLOBAL_Q
.endif
;;###########################################################################
;; No More.
;;###########################################################################
@@ -0,0 +1,322 @@
;;###########################################################################
;;
;; FILE: IQNexp.asm
;;
;; TITLE: C Callable IQ Exponent Math Function
;;
;;###########################################################################
;;
;; Ver | Date | Who | Description of changes
;; =====|=============|=======|==============================================
;; 1.0 | 11 Jul 2002 | A. T. | Original Release.
;; -----|-------------|-------|----------------------------------------------
;; 1.1 | 10 Mar 2005 | A. T. | Fixed Bug #1. "LSL ACC,T" instruction
;; | | | should have been "LSLL ACC,T".
;; -----|-------------|-------|----------------------------------------------
;; | | |
;;
;;###########################################################################
;;===========================================================================
;; Function: _IQNexp
;;===========================================================================
;;
;; C Usage: extern long _IQNexp(long X); // Saturated
;;
;;---------------------------------------------------------------------------
;;
;; On Entry: ACC = X in IQ format
;;
;; Regs Used: XAR7, XAR6, XAR5, XAR4, XAR3
;; XT, P
;;
;; On Exit: ACC = exp(X) result in IQ format
;;
;; Q range: 29 to 1
;;
;;---------------------------------------------------------------------------
;; Algorithm: The procedure for calculating "Y = exp(X)" is as follows:
;;
;; Step 1) Saturate input to avoid overflow.
;;
;; Step 2) Normalize Input:
;;
;; X' = X * 2^n -1.0 <= X' < 1.0
;;
;; Step 3) Obtain Y' = exp(X') using taylor series expansion:
;;
;; Y' = 1 + X' + X'^2/2! + .... + X^11/11!
;;
;; Step 4) Denormalize value to get output Y:
;;
;; if( n == 0 ) Y = Y'
;; if( n == 1 ) Y = Y'^2
;; if( n == 2 ) Y = Y'^4
;; if( n == 3 ) Y = Y'^8
;; if( n == 4 ) Y = Y'^16
;;
;;---------------------------------------------------------------------------
;; Benchmark:
;;
;; Assumptions:
;; * Code executes from 0-wait SARAM block.
;; * _IQexp Tables located in seperate 0-wait SARAM or 1-wait ROM block.
;; * Stack located in seperate SARAM block.
;; * Includes LCR/LRETR.
;;
;; Cycles = 168 to 215
;;
;;===========================================================================
IQNexp .macro q_value
PUSH XAR3
; Step 1: Saturate input to avoid overflow:
MOVL XAR7,#(_IQexpTableMinMax + (q_value - 1)*4)
MINL ACC,*+XAR7[0] ; Saturate Input To Max Value
MAXL ACC,*+XAR7[2] ; Saturate Input To Min Value
MOVL XAR6,@ACC ; XAR6 = X (QG)
; Step 2: Normalize Input:
CSB ACC
MOVB AL,#(31 - q_value)
SUB AL,@T
MOVB @AL,#0,LT
MOVZ AR3,@AL ; AR3 = n
MOV T,@AL
MOVL ACC,@XAR6
ASR64 ACC:P,T ; ACC = X' (QG)
.if (q_value <= 29)
MOV @T,#(29 - q_value)
LSLL ACC,T ; (Fixed Bug #1) ACC = X' (Q29)
.endif
MOVL XAR6,@ACC ; XAR6 = X' (Q29)
; Step 3: Repeat 10 Times: ACC = X*(1 + ACC/Coeff)
MOV @PH,#(1 << 12)
MOV @PL,#0
MOVL XAR5,@P ; XAR5 = 1.0 (Q28)
MOVL XAR7,#_IQexpTableCoeff ; XAR7 -> 1/10 Coeff (Q31)
MOVB XAR4,#9
IQNexpLoop1:
MOVL XT,@ACC ; XT = ACC (Q29)
QMPYL ACC,XT,*XAR7++ ; ACC = ACC/Coeff (Q28)
ADDL ACC,@XAR5 ; ACC = 1 + ACC/Coeff (Q28)
MOVL XT,@XAR6 ; XT = X (Q29)
IMPYL P,XT,@ACC
QMPYL ACC,XT,@ACC ; ACC:P = X(1 + ACC/Coeff) (Q57)
LSL64 ACC:P,#4 ; ACC = X(1 + ACC/Coeff) (Q29)
BANZ IQNexpLoop1,AR4--
ADDL ACC,@XAR5
ADDL ACC,@XAR5
.if (q_value <= 29)
MOV @T,#(29 - q_value)
ASR64 ACC:P,T ; ACC = 1 + ACC = exp(X) (QG)
.endif
; Step 4: Denormalize result:
MOVB XAR6,#0
BAR IQNexpExit,AR3,AR6,EQ ;if( n == 0 ) Y = ACC
SUBB XAR3,#1
IQNexpLoop2:
MOVL XT,@ACC
IMPYL P,XT,@ACC
QMPYL ACC,XT,@ACC
MOV @T,#(32 - q_value)
LSL64 ACC:P,T ; ACC = ACC*ACC (QG)
BANZ IQNexpLoop2,AR3-- ; Loop until n == 0
IQNexpExit:
POP XAR3
LRETR
.endm
;; The value "GLOBAL_Q" needs to be supplied by the assembler
;; using the "-dGLOBAL_Q=q_value" directive:
.sect "IQmath"
.ref _IQexpTableMinMax
.ref _IQexpTableCoeff
.if GLOBAL_Q == 30
.def _IQ30exp
_IQ30exp:
IQNexp GLOBAL_Q
.endif
.if GLOBAL_Q == 29
.def _IQ29exp
_IQ29exp:
IQNexp GLOBAL_Q
.endif
.if GLOBAL_Q == 28
.def _IQ28exp
_IQ28exp:
IQNexp GLOBAL_Q
.endif
.if GLOBAL_Q == 27
.def _IQ27exp
_IQ27exp:
IQNexp GLOBAL_Q
.endif
.if GLOBAL_Q == 26
.def _IQ26exp
_IQ26exp:
IQNexp GLOBAL_Q
.endif
.if GLOBAL_Q == 25
.def _IQ25exp
_IQ25exp:
IQNexp GLOBAL_Q
.endif
.if GLOBAL_Q == 24
.def _IQ24exp
_IQ24exp:
IQNexp GLOBAL_Q
.endif
.if GLOBAL_Q == 23
.def _IQ23exp
_IQ23exp:
IQNexp GLOBAL_Q
.endif
.if GLOBAL_Q == 22
.def _IQ22exp
_IQ22exp:
IQNexp GLOBAL_Q
.endif
.if GLOBAL_Q == 21
.def _IQ21exp
_IQ21exp:
IQNexp GLOBAL_Q
.endif
.if GLOBAL_Q == 20
.def _IQ20exp
_IQ20exp:
IQNexp GLOBAL_Q
.endif
.if GLOBAL_Q == 19
.def _IQ19exp
_IQ19exp:
IQNexp GLOBAL_Q
.endif
.if GLOBAL_Q == 18
.def _IQ18exp
_IQ18exp:
IQNexp GLOBAL_Q
.endif
.if GLOBAL_Q == 17
.def _IQ17exp
_IQ17exp:
IQNexp GLOBAL_Q
.endif
.if GLOBAL_Q == 16
.def _IQ16exp
_IQ16exp:
IQNexp GLOBAL_Q
.endif
.if GLOBAL_Q == 15
.def _IQ15exp
_IQ15exp:
IQNexp GLOBAL_Q
.endif
.if GLOBAL_Q == 14
.def _IQ14exp
_IQ14exp:
IQNexp GLOBAL_Q
.endif
.if GLOBAL_Q == 13
.def _IQ13exp
_IQ13exp:
IQNexp GLOBAL_Q
.endif
.if GLOBAL_Q == 12
.def _IQ12exp
_IQ12exp:
IQNexp GLOBAL_Q
.endif
.if GLOBAL_Q == 11
.def _IQ11exp
_IQ11exp:
IQNexp GLOBAL_Q
.endif
.if GLOBAL_Q == 10
.def _IQ10exp
_IQ10exp:
IQNexp GLOBAL_Q
.endif
.if GLOBAL_Q == 9
.def _IQ9exp
_IQ9exp:
IQNexp GLOBAL_Q
.endif
.if GLOBAL_Q == 8
.def _IQ8exp
_IQ8exp:
IQNexp GLOBAL_Q
.endif
.if GLOBAL_Q == 7
.def _IQ7exp
_IQ7exp:
IQNexp GLOBAL_Q
.endif
.if GLOBAL_Q == 6
.def _IQ6exp
_IQ6exp:
IQNexp GLOBAL_Q
.endif
.if GLOBAL_Q == 5
.def _IQ5exp
_IQ5exp:
IQNexp GLOBAL_Q
.endif
.if GLOBAL_Q == 4
.def _IQ4exp
_IQ4exp:
IQNexp GLOBAL_Q
.endif
.if GLOBAL_Q == 3
.def _IQ3exp
_IQ3exp:
IQNexp GLOBAL_Q
.endif
.if GLOBAL_Q == 2
.def _IQ2exp
_IQ2exp:
IQNexp GLOBAL_Q
.endif
.if GLOBAL_Q == 1
.def _IQ1exp
_IQ1exp:
IQNexp GLOBAL_Q
.endif
;;###########################################################################
;; No More.
;;###########################################################################
@@ -0,0 +1,104 @@
;;##########################################################################
;;
;;Title: Table 2 For IQmath Functions
;;
;;Version: 1.0
;;
;;Contents: IQexp Function Table, Size Of Table = 140x16
;;
;;##########################################################################
;;==========================================================================
;; IQexp Function Table, Size Of Table = 140x16
;;==========================================================================
.def _IQexpTable
.def _IQexpTableMinMax
.def _IQexpTableMinMaxEnd
.def _IQexpTableCoeff
.def _IQexpTableCoeffEnd
.sect "IQmathTablesRam"
_IQexpTable:
_IQexpTableMinMax:
.long 42 ; Q 1 Max Input Value = 20.794415416333
.long -1 ; Q 1 Min Input Value = -0.693147180560
.long 80 ; Q 2 Max Input Value = 20.101268235773
.long -6 ; Q 2 Min Input Value = -1.386294361120
.long 155 ; Q 3 Max Input Value = 19.408121055213
.long -17 ; Q 3 Min Input Value = -2.079441541680
.long 299 ; Q 4 Max Input Value = 18.714973874653
.long -44 ; Q 4 Min Input Value = -2.772588722240
.long 577 ; Q 5 Max Input Value = 18.021826694093
.long -111 ; Q 5 Min Input Value = -3.465735902800
.long 1109 ; Q 6 Max Input Value = 17.328679513533
.long -266 ; Q 6 Min Input Value = -4.158883083360
.long 2129 ; Q 7 Max Input Value = 16.635532332973
.long -621 ; Q 7 Min Input Value = -4.852030263920
.long 4081 ; Q 8 Max Input Value = 15.942385152413
.long -1420 ; Q 8 Min Input Value = -5.545177444480
.long 7808 ; Q 9 Max Input Value = 15.249237971853
.long -3194 ; Q 9 Min Input Value = -6.238324625040
.long 14905 ; Q10 Max Input Value = 14.556090791293
.long -7098 ; Q10 Min Input Value = -6.931471805599
.long 28391 ; Q11 Max Input Value = 13.862943610733
.long -15615 ; Q11 Min Input Value = -7.624618986159
.long 53943 ; Q12 Max Input Value = 13.169796430173
.long -34070 ; Q12 Min Input Value = -8.317766166719
.long 102209 ; Q13 Max Input Value = 12.476649249613
.long -73817 ; Q13 Min Input Value = -9.010913347279
.long 193061 ; Q14 Max Input Value = 11.783502069053
.long -158991 ; Q14 Min Input Value = -9.704060527839
.long 363409 ; Q15 Max Input Value = 11.090354888493
.long -340696 ; Q15 Min Input Value = -10.397207708399
.long 681391 ; Q16 Max Input Value = 10.397207707934
.long -726817 ; Q16 Min Input Value = -11.090354888959
.long 1271931 ; Q17 Max Input Value = 9.704060527374
.long -1544487 ; Q17 Min Input Value = -11.783502069519
.long 2362157 ; Q18 Max Input Value = 9.010913346814
.long -3270679 ; Q18 Min Input Value = -12.476649250079
.long 4360905 ; Q19 Max Input Value = 8.317766166254
.long -6904766 ; Q19 Min Input Value = -13.169796430639
.long 7994992 ; Q20 Max Input Value = 7.624618985694
.long -14536350 ; Q20 Min Input Value = -13.862943611199
.long 14536350 ; Q21 Max Input Value = 6.931471805134
.long -30526335 ; Q21 Min Input Value = -14.556090791759
.long 26165430 ; Q22 Max Input Value = 6.238324624574
.long -63959940 ; Q22 Min Input Value = -15.249237972319
.long 46516320 ; Q23 Max Input Value = 5.545177444014
.long -133734420 ; Q23 Min Input Value = -15.942385152879
.long 81403560 ; Q24 Max Input Value = 4.852030263454
.long -279097919 ; Q24 Min Input Value = -16.635532333439
.long 139548960 ; Q25 Max Input Value = 4.158883082894
.long -581453998 ; Q25 Min Input Value = -17.328679513999
.long 232581599 ; Q26 Max Input Value = 3.465735902334
.long -1209424317 ; Q26 Min Input Value = -18.021826694559
.long 372130559 ; Q27 Max Input Value = 2.772588721774
.long -2147483648 ; Q27 Min Input Value = -16.000000000000
.long 558195838 ; Q28 Max Input Value = 2.079441541214
.long -2147483648 ; Q28 Min Input Value = -8.000000000000
.long 744261118 ; Q29 Max Input Value = 1.386294360654
.long -2147483648 ; Q29 Min Input Value = -4.000000000000
.long 744261117 ; Q30 Max Input Value = 0.693147180094
.long -2147483648 ; Q30 Min Input Value = -2.000000000000
_IQexpTableMinMaxEnd:
_IQexpTableCoeff:
.long 0x0BA2E8BA ; 1/11 in Q31
.long 0x0CCCCCCD ; 1/10 in Q31
.long 0x0E38E38E ; 1/9 in Q31
.long 0x10000000 ; 1/8 in Q31
.long 0x12492492 ; 1/7 in Q31
.long 0x15555555 ; 1/6 in Q31
.long 0x1999999A ; 1/5 in Q31
.long 0x20000000 ; 1/4 in Q31
.long 0x2AAAAAAB ; 1/3 in Q31
.long 0x40000000 ; 1/2 in Q31
_IQexpTableCoeffEnd:
_IQexpTableEnd:
;;
;; End Of File.
;;
@@ -0,0 +1,252 @@
;;###########################################################################
;;
;; FILE: IQNfrac.asm
;;
;; TITLE: C Callable IQ Fractional Math Function
;;
;;###########################################################################
;;
;; Ver | Date | Who | Description of changes
;; =====|=============|=======|==============================================
;; 1.4 | 17 May 2002 | A. T. | Original Release.
;; -----|-------------|-------|----------------------------------------------
;; 1.4c | 20 Jun 2002 | A. T. | Fixed problem with function when operating
;; | | | on negative values with a zero fractional
;; | | | part, incorrect result was given.
;; -----|-------------|-------|----------------------------------------------
;; 1.4d | 30 Mar 2002 | SD | Frac was implemented as ((A >> q_value) << q_value) - A
;; | | | Hence sign was changed. Reimplemented it correctly.
;;------|-------------|-------|----------------------------------------------
;;
;;###########################################################################
;;===========================================================================
;; Function: _IQNfrac
;;===========================================================================
;;
;; C Usage: extern long _IQfrac(long A); // no round or sat
;;
;;---------------------------------------------------------------------------
;;
;; On Entry: ACC = A in IQ format
;;
;; Regs Used: XT, XAR7
;;
;; On Exit: ACC = fractional portion of A stored as IQ
;;
;; Q range: 30 to 1
;;
;;---------------------------------------------------------------------------
;; Algorithm: The fractional portion of the IQ number is calculated
;; as follows:
;;
;; frac = A - ((A >> q_value) << q_value);
;;
;;---------------------------------------------------------------------------
;; Benchmark:
;;
;; Cycles = 18 (includes LCR/LRETR)
;;
;;---------------------------------------------------------------------------
IQNfrac .macro q_value
SETC OVM ; Turn overflow mode on
CLRC TC ; Clear sign flag
ABSTC ACC ; Take abs value of ACC
MOV @T,#q_value ; Extract the fractional bits.
LSR64 ACC:P,T ; P=frac bits (aligned to MSB)
MOVB ACC,#0
LSL64 ACC:P,T ; ACC=frac bits (aligned to LSB)
NEGTC ACC ; Negate if sign set
CLRC OVM ; Clear overflow mode
LRETR
.endm
;; The value "GLOBAL_Q" needs to be supplied by the assembler
;; using the "-dGLOBAL_Q=q_value" directive:
.sect "IQmath"
.if GLOBAL_Q == 30
.def _IQ30frac
_IQ30frac:
IQNfrac GLOBAL_Q
.endif
.if GLOBAL_Q == 29
.def _IQ29frac
_IQ29frac:
IQNfrac GLOBAL_Q
.endif
.if GLOBAL_Q == 28
.def _IQ28frac
_IQ28frac:
IQNfrac GLOBAL_Q
.endif
.if GLOBAL_Q == 27
.def _IQ27frac
_IQ27frac:
IQNfrac GLOBAL_Q
.endif
.if GLOBAL_Q == 26
.def _IQ26frac
_IQ26frac:
IQNfrac GLOBAL_Q
.endif
.if GLOBAL_Q == 25
.def _IQ25frac
_IQ25frac:
IQNfrac GLOBAL_Q
.endif
.if GLOBAL_Q == 24
.def _IQ24frac
_IQ24frac:
IQNfrac GLOBAL_Q
.endif
.if GLOBAL_Q == 23
.def _IQ23frac
_IQ23frac:
IQNfrac GLOBAL_Q
.endif
.if GLOBAL_Q == 22
.def _IQ22frac
_IQ22frac:
IQNfrac GLOBAL_Q
.endif
.if GLOBAL_Q == 21
.def _IQ21frac
_IQ21frac:
IQNfrac GLOBAL_Q
.endif
.if GLOBAL_Q == 20
.def _IQ20frac
_IQ20frac:
IQNfrac GLOBAL_Q
.endif
.if GLOBAL_Q == 19
.def _IQ19frac
_IQ19frac:
IQNfrac GLOBAL_Q
.endif
.if GLOBAL_Q == 18
.def _IQ18frac
_IQ18frac:
IQNfrac GLOBAL_Q
.endif
.if GLOBAL_Q == 17
.def _IQ17frac
_IQ17frac:
IQNfrac GLOBAL_Q
.endif
.if GLOBAL_Q == 16
.def _IQ16frac
_IQ16frac:
IQNfrac GLOBAL_Q
.endif
.if GLOBAL_Q == 15
.def _IQ15frac
_IQ15frac:
IQNfrac GLOBAL_Q
.endif
.if GLOBAL_Q == 14
.def _IQ14frac
_IQ14frac:
IQNfrac GLOBAL_Q
.endif
.if GLOBAL_Q == 13
.def _IQ13frac
_IQ13frac:
IQNfrac GLOBAL_Q
.endif
.if GLOBAL_Q == 12
.def _IQ12frac
_IQ12frac:
IQNfrac GLOBAL_Q
.endif
.if GLOBAL_Q == 11
.def _IQ11frac
_IQ11frac:
IQNfrac GLOBAL_Q
.endif
.if GLOBAL_Q == 10
.def _IQ10frac
_IQ10frac:
IQNfrac GLOBAL_Q
.endif
.if GLOBAL_Q == 9
.def _IQ9frac
_IQ9frac:
IQNfrac GLOBAL_Q
.endif
.if GLOBAL_Q == 8
.def _IQ8frac
_IQ8frac:
IQNfrac GLOBAL_Q
.endif
.if GLOBAL_Q == 7
.def _IQ7frac
_IQ7frac:
IQNfrac GLOBAL_Q
.endif
.if GLOBAL_Q == 6
.def _IQ6frac
_IQ6frac:
IQNfrac GLOBAL_Q
.endif
.if GLOBAL_Q == 5
.def _IQ5frac
_IQ5frac:
IQNfrac GLOBAL_Q
.endif
.if GLOBAL_Q == 4
.def _IQ4frac
_IQ4frac:
IQNfrac GLOBAL_Q
.endif
.if GLOBAL_Q == 3
.def _IQ3frac
_IQ3frac:
IQNfrac GLOBAL_Q
.endif
.if GLOBAL_Q == 2
.def _IQ2frac
_IQ2frac:
IQNfrac GLOBAL_Q
.endif
.if GLOBAL_Q == 1
.def _IQ1frac
_IQ1frac:
IQNfrac GLOBAL_Q
.endif
;;###########################################################################
;; No More.
;;###########################################################################
@@ -0,0 +1,246 @@
;;###########################################################################
;;
;; FILE: IQNint.asm
;;
;; TITLE: C Callable IQ Integer Math Function
;;
;;###########################################################################
;;
;; Ver | Date | Who | Description of changes
;; =====|=============|=======|==============================================
;; 1.4 | 17 May 2002 | A. T. | Original Release.
;; -----|-------------|-------|----------------------------------------------
;; 1.4c | 20 Jun 2002 | A. T. | Fixed problem with function when operating
;; | | | on negative values with a zero fractional
;; | | | part, incorrect result was given.
;; -----|-------------|-------|----------------------------------------------
;; | | |
;;
;;###########################################################################
;;===========================================================================
;; Function: _IQNint
;;===========================================================================
;;
;; C Usage: extern long _IQint(long A); // no round or sat
;;
;;---------------------------------------------------------------------------
;;
;; On Entry: ACC = A in IQ format
;;
;; Regs Used: XT
;;
;; On Exit: ACC = integer portion of A stored as "long"
;;
;; Q range: 30 to 1
;;
;;---------------------------------------------------------------------------
;; Algorithm: The integer portion of the IQ number is calculated
;; as follows:
;;
;; integer = A >> q_value;
;;
;;---------------------------------------------------------------------------
;; Benchmark:
;;
;; Cycles = 15 (includes LCR/LRETR)
;;
;;---------------------------------------------------------------------------
IQNint .macro q_value
SETC OVM ; Turn overflow mode on
CLRC TC ; Clear sign flag
ABSTC ACC ; Take abs value and set sign
MOV @T,#q_value
ASRL ACC,T ; ACC = ACC >> q_value
NEGTC ACC ; Negate if sign set
CLRC OVM ; Turn overflow mode off
LRETR
.endm
;; The value "GLOBAL_Q" needs to be supplied by the assembler
;; using the "-dGLOBAL_Q=q_value" directive:
.sect "IQmath"
.if GLOBAL_Q == 30
.def _IQ30int
_IQ30int:
IQNint GLOBAL_Q
.endif
.if GLOBAL_Q == 29
.def _IQ29int
_IQ29int:
IQNint GLOBAL_Q
.endif
.if GLOBAL_Q == 28
.def _IQ28int
_IQ28int:
IQNint GLOBAL_Q
.endif
.if GLOBAL_Q == 27
.def _IQ27int
_IQ27int:
IQNint GLOBAL_Q
.endif
.if GLOBAL_Q == 26
.def _IQ26int
_IQ26int:
IQNint GLOBAL_Q
.endif
.if GLOBAL_Q == 25
.def _IQ25int
_IQ25int:
IQNint GLOBAL_Q
.endif
.if GLOBAL_Q == 24
.def _IQ24int
_IQ24int:
IQNint GLOBAL_Q
.endif
.if GLOBAL_Q == 23
.def _IQ23int
_IQ23int:
IQNint GLOBAL_Q
.endif
.if GLOBAL_Q == 22
.def _IQ22int
_IQ22int:
IQNint GLOBAL_Q
.endif
.if GLOBAL_Q == 21
.def _IQ21int
_IQ21int:
IQNint GLOBAL_Q
.endif
.if GLOBAL_Q == 20
.def _IQ20int
_IQ20int:
IQNint GLOBAL_Q
.endif
.if GLOBAL_Q == 19
.def _IQ19int
_IQ19int:
IQNint GLOBAL_Q
.endif
.if GLOBAL_Q == 18
.def _IQ18int
_IQ18int:
IQNint GLOBAL_Q
.endif
.if GLOBAL_Q == 17
.def _IQ17int
_IQ17int:
IQNint GLOBAL_Q
.endif
.if GLOBAL_Q == 16
.def _IQ16int
_IQ16int:
IQNint GLOBAL_Q
.endif
.if GLOBAL_Q == 15
.def _IQ15int
_IQ15int:
IQNint GLOBAL_Q
.endif
.if GLOBAL_Q == 14
.def _IQ14int
_IQ14int:
IQNint GLOBAL_Q
.endif
.if GLOBAL_Q == 13
.def _IQ13int
_IQ13int:
IQNint GLOBAL_Q
.endif
.if GLOBAL_Q == 12
.def _IQ12int
_IQ12int:
IQNint GLOBAL_Q
.endif
.if GLOBAL_Q == 11
.def _IQ11int
_IQ11int:
IQNint GLOBAL_Q
.endif
.if GLOBAL_Q == 10
.def _IQ10int
_IQ10int:
IQNint GLOBAL_Q
.endif
.if GLOBAL_Q == 9
.def _IQ9int
_IQ9int:
IQNint GLOBAL_Q
.endif
.if GLOBAL_Q == 8
.def _IQ8int
_IQ8int:
IQNint GLOBAL_Q
.endif
.if GLOBAL_Q == 7
.def _IQ7int
_IQ7int:
IQNint GLOBAL_Q
.endif
.if GLOBAL_Q == 6
.def _IQ6int
_IQ6int:
IQNint GLOBAL_Q
.endif
.if GLOBAL_Q == 5
.def _IQ5int
_IQ5int:
IQNint GLOBAL_Q
.endif
.if GLOBAL_Q == 4
.def _IQ4int
_IQ4int:
IQNint GLOBAL_Q
.endif
.if GLOBAL_Q == 3
.def _IQ3int
_IQ3int:
IQNint GLOBAL_Q
.endif
.if GLOBAL_Q == 2
.def _IQ2int
_IQ2int:
IQNint GLOBAL_Q
.endif
.if GLOBAL_Q == 1
.def _IQ1int
_IQ1int:
IQNint GLOBAL_Q
.endif
;;###########################################################################
;; No More.
;;###########################################################################
@@ -0,0 +1,349 @@
;;###########################################################################
;;
;; FILE: IQNisqrt.asm
;;
;; TITLE: C Callable IQ Inverse Square Root Math Function
;;
;;###########################################################################
;;
;; Ver | Date | Who | Description of changes
;; =====|==============|=======|=============================================
;; 1.2 | 11 Oct 2001 | A. T. | Original Release.
;; -----|--------------|-------|---------------------------------------------
;; 1.3 | 19 Nov 2001 | A. T. | Improved accuracy.
;; -----|--------------|-------|---------------------------------------------
;; 1.4 | 17 May 2002 | A. T. | Corrected cycle benchmarks.
;; -----|--------------|-------|---------------------------------------------
;; | | |
;;
;;###########################################################################
;;===========================================================================
;; Function: _IQNisqrt
;;===========================================================================
;;
;; C Usage: extern long _IQNisqrt(long X); // with saturation and rounding
;;
;;---------------------------------------------------------------------------
;;
;; On Entry: ACC = Val in IQ format
;;
;; Regs Used: XAR7, XAR6, XAR5, XAR4
;; XT, P, XAR0
;;
;; On Exit: ACC = 1/sqrt(X) result in IQ format
;; ACC = 0 if input is -ve or 0
;; ACC = max value "0x7FFFFFFF" if result saturates
;;
;; Q range: 30 to 1
;;
;;---------------------------------------------------------------------------
;; Algorithm: The procedure for calculating "Y = 1/sqrt(X)" is as follows:
;;
;; Step 1) Normalize Input:
;;
;; V = X * 2^n
;;
;; Step 2) Obtain initial estimate from "_IQisqrtTable"
;; using the upper 8-bits of the normalized V value.
;;
;; Step 3) Use Newton-Raphson algorithm to improve accuracy.
;; Repeat following equation two times. First iteration
;; gives 16-bit accuracy. Second iteration gives 32-bit
;; accuracy:
;;
;; Yn = Yn(1.5 - Yn*Yn*V/2)
;;
;; Yn = 1/sqrt(V)
;;
;; Step 4) Denormalize result, round and saturate:
;;
;; Y = Yn / sqrt(2^n)
;;
;;---------------------------------------------------------------------------
;; Benchmark:
;;
;; Assumptions:
;; * Code executes from 0-wait SARAM block.
;; * _IQisqrt Tables located in seperate 0-wait SARAM or 1-wait ROM block.
;; * Stack located in seperate 0-wait SARAM block.
;; * Includes LCR/LRETR.
;;
;; Cycles = 66 (30 >= Q >= 18 if _IQisqrt Tables in 0-wait SARAM)
;; 67 (17 >= Q >= 0 if _IQisqrt Tables in 0-wait SARAM)
;;
;; Cycles = 73 (30 >= Q >= 18 if _IQisqrt Tables in 1-wait ROM)
;; 74 (17 >= Q >= 0 if _IQisqrt Tables in 1-wait ROM)
;;
;;===========================================================================
IQNisqrt .macro q_value
CSB ACC ; Count sign bits, T = n
LSLL ACC,T ; ACC = V/2 (Q32)
MOVL XAR6,@ACC ; XAR6 = V/2 (Q32)
ASR AH,#6
MOVB @AH,#0xFE,LEQ ; AR0 -> zero value if -ve or 0 input
SUB @AH,#254 ; Scale to table offset
MOVZ AR0,@AH ; AR0 -> index into _IQisqrtTable for
; initial estimate of Yn
TBIT @T,#0 ; TC = odd/even shift indicator
MOV AH,@T ; AH = n
LSR AH,#1 ; AH = n/2
MOVL *SP++,ACC ; SP-2 = n/2
MOVL XAR7,#_IQisqrtTable ; XAR7 = pointer to _IQisqrtTable
MOVL XAR4,*+XAR7[AR0] ; (1) XAR4 = initial Yn estimate (Q30)
MOVL XAR7,#_IQisqrtRoundSatTable
MOVL XAR5,*XAR7++ ; XAR5 = 1.5 (Q30)
.if (q_value & 0x0001) == 0 ; Q even
MOVB @AR0,#12,NTC ; AR0 -> 1/sqrt(2) if n == even (Q30)
MOVB @AR0,#8,TC ; AR0 -> 1.0 if n == odd (Q30)
.endif
.if (q_value & 0x0001) == 1 ; Q odd
MOVB @AR0,#12,TC ; AR0 -> 1/sqrt(2) if n == odd (Q30)
MOVB @AR0,#10,NTC ; AR0 -> 0.5 if n == even (Q30)
.endif
MOVL XT,@XAR4 ;(2) XT = Yn (Q30)
QMPYL ACC,XT,@XT ;(3) ACC = Yn*Yn (Q28)
MOVL XT,@XAR6 ;(4) XT = V/2 (Q32)
LSL ACC,#2 ;(5) ACC = Yn*Yn (Q30)
QMPYL ACC,XT,@ACC ;(6) ACC = Yn*Yn*V/2 (Q30)
MOVL XT,@XAR5 ;(7) XT = 1.5 (Q30)
SUBL @XT,ACC ;(8) XT = 1.5 - Yn*Yn*V/2 (Q30)
QMPYL ACC,XT,@XAR4 ;(9) ACC = Yn(1.5 - Yn*Yn*V/2) (Q28)
LSL ACC,#2 ;(10) ACC = Yn(1.5 - Yn*Yn*V/2) (Q30)
MOVL XAR4,@ACC ;(1) XAR4 = Yn (Q30)
MOVL XT,@XAR4 ;(2) XT = Yn (Q30)
QMPYL ACC,XT,@XT ;(3) ACC = Yn*Yn (Q28)
MOVL XT,@XAR6 ;(4) XT = V/2 (Q32)
LSL ACC,#2 ;(5) ACC = Yn*Yn (Q30)
QMPYL ACC,XT,@ACC ;(6) ACC = Yn*Yn*V/2 (Q30)
MOVL XT,@XAR5 ;(7) XT = 1.5 (Q30)
SUBL @XT,ACC ;(8) XT = 1.5 - Yn*Yn*V/2 (Q30)
QMPYL ACC,XT,@XAR4 ;(9) ACC = Yn(1.5 - Yn*Yn*V/2) (Q28)
LSL ACC,#2 ;(10) ACC = Yn(1.5 - Yn*Yn*V/2) (Q30)
MOVL XT,*+XAR7[AR0] ; Scale with 1.0 or 1/sqrt(X) or 0.5
IMPYL P,XT,@ACC
QMPYL ACC,XT,@ACC
.if q_value == 30 ; Scale final result based on Q value
LSL64 ACC:P,#2
.endif
.if q_value == 29
LSL64 ACC:P,#1
.endif
.if (q_value >= 18) & (q_value <= 28)
ASR64 ACC:P,#((86 - q_value*3)/2)
.endif
.if (q_value >= 7) & (q_value <= 17)
ASR64 ACC:P,#16
ASR64 ACC:P,#(((86 - q_value*3)/2) - 16)
.endif
.if q_value <= 6
ASR64 ACC:P,#16
ASR64 ACC:P,#16
ASR64 ACC:P,#(((86 - q_value*3)/2) - 32)
.endif
ASR64 ACC:P,#16
MOVL XT,*--SP ; T = n/2
LSL64 ACC:P,T ; Scale final result based on n/2
ADDUL P,*+XAR7[0] ; Round result
ADDCL ACC,*+XAR7[2]
MINL ACC,*+XAR7[6] ; Saturate if exceeds max pos value
MINCUL P,*+XAR7[4]
LSL64 ACC:P,#16
LRETR
.endm
;; The value "GLOBAL_Q" needs to be supplied by the assembler
;; using the "-dGLOBAL_Q=q_value" directive:
.sect "IQmath"
.ref _IQisqrtTable
.ref _IQisqrtRoundSatTable
.if GLOBAL_Q == 30
.def _IQ30isqrt
_IQ30isqrt:
IQNisqrt GLOBAL_Q
.endif
.if GLOBAL_Q == 29
.def _IQ29isqrt
_IQ29isqrt:
IQNisqrt GLOBAL_Q
.endif
.if GLOBAL_Q == 28
.def _IQ28isqrt
_IQ28isqrt:
IQNisqrt GLOBAL_Q
.endif
.if GLOBAL_Q == 27
.def _IQ27isqrt
_IQ27isqrt:
IQNisqrt GLOBAL_Q
.endif
.if GLOBAL_Q == 26
.def _IQ26isqrt
_IQ26isqrt:
IQNisqrt GLOBAL_Q
.endif
.if GLOBAL_Q == 25
.def _IQ25isqrt
_IQ25isqrt:
IQNisqrt GLOBAL_Q
.endif
.if GLOBAL_Q == 24
.def _IQ24isqrt
_IQ24isqrt:
IQNisqrt GLOBAL_Q
.endif
.if GLOBAL_Q == 23
.def _IQ23isqrt
_IQ23isqrt:
IQNisqrt GLOBAL_Q
.endif
.if GLOBAL_Q == 22
.def _IQ22isqrt
_IQ22isqrt:
IQNisqrt GLOBAL_Q
.endif
.if GLOBAL_Q == 21
.def _IQ21isqrt
_IQ21isqrt:
IQNisqrt GLOBAL_Q
.endif
.if GLOBAL_Q == 20
.def _IQ20isqrt
_IQ20isqrt:
IQNisqrt GLOBAL_Q
.endif
.if GLOBAL_Q == 19
.def _IQ19isqrt
_IQ19isqrt:
IQNisqrt GLOBAL_Q
.endif
.if GLOBAL_Q == 18
.def _IQ18isqrt
_IQ18isqrt:
IQNisqrt GLOBAL_Q
.endif
.if GLOBAL_Q == 17
.def _IQ17isqrt
_IQ17isqrt:
IQNisqrt GLOBAL_Q
.endif
.if GLOBAL_Q == 16
.def _IQ16isqrt
_IQ16isqrt:
IQNisqrt GLOBAL_Q
.endif
.if GLOBAL_Q == 15
.def _IQ15isqrt
_IQ15isqrt:
IQNisqrt GLOBAL_Q
.endif
.if GLOBAL_Q == 14
.def _IQ14isqrt
_IQ14isqrt:
IQNisqrt GLOBAL_Q
.endif
.if GLOBAL_Q == 13
.def _IQ13isqrt
_IQ13isqrt:
IQNisqrt GLOBAL_Q
.endif
.if GLOBAL_Q == 12
.def _IQ12isqrt
_IQ12isqrt:
IQNisqrt GLOBAL_Q
.endif
.if GLOBAL_Q == 11
.def _IQ11isqrt
_IQ11isqrt:
IQNisqrt GLOBAL_Q
.endif
.if GLOBAL_Q == 10
.def _IQ10isqrt
_IQ10isqrt:
IQNisqrt GLOBAL_Q
.endif
.if GLOBAL_Q == 9
.def _IQ9isqrt
_IQ9isqrt:
IQNisqrt GLOBAL_Q
.endif
.if GLOBAL_Q == 8
.def _IQ8isqrt
_IQ8isqrt:
IQNisqrt GLOBAL_Q
.endif
.if GLOBAL_Q == 7
.def _IQ7isqrt
_IQ7isqrt:
IQNisqrt GLOBAL_Q
.endif
.if GLOBAL_Q == 6
.def _IQ6isqrt
_IQ6isqrt:
IQNisqrt GLOBAL_Q
.endif
.if GLOBAL_Q == 5
.def _IQ5isqrt
_IQ5isqrt:
IQNisqrt GLOBAL_Q
.endif
.if GLOBAL_Q == 4
.def _IQ4isqrt
_IQ4isqrt:
IQNisqrt GLOBAL_Q
.endif
.if GLOBAL_Q == 3
.def _IQ3isqrt
_IQ3isqrt:
IQNisqrt GLOBAL_Q
.endif
.if GLOBAL_Q == 2
.def _IQ2isqrt
_IQ2isqrt:
IQNisqrt GLOBAL_Q
.endif
.if GLOBAL_Q == 1
.def _IQ1isqrt
_IQ1isqrt:
IQNisqrt GLOBAL_Q
.endif
;;###########################################################################
;; No More.
;;###########################################################################
@@ -0,0 +1,466 @@
;;###########################################################################
;;
;; FILE: IQNlog.asm
;;
;; TITLE: IQ Natural Logarithmic Math Function
;;
;;###########################################################################
;;
;; Ver | dd-mmm-yyyy | Who | Description of changes
;; =====|=============|=======|==============================================
;; 1.0 | 2 Aug 2010 | A. T. | Original Release.
;; -----|-------------|-------|----------------------------------------------
;;
;;###########################################################################
;;
;; On Entry: ACC = X in IQ format
;;
;; On Exit: ACC = log(X) result in IQ format
;;
;; Regs Used: ACC, P, XT
;; XAR2, XAR3, XAR4, XAR5, XAR6, XAR7
;;
;; Q range: 29 to 1
;;
;;---------------------------------------------------------------------------
;; Algorithm: The procedure for calculating "Y = log(X)" is as follows:
;;
;; Step 1) Check to see if Input is less than zero..
;;
;; Return = most negative number possible for given Q value.
;;
;; Step 2) Normalize Input:
;;
;; X' = X * 2^n 0.5 <= X' < 1.0
;;
;; Step 3) Obtain Y' = log(X') using taylor series expansion:
;;
;; y = (X'-1)/(X'+1) -0.3333.. <= y < -1.0
;;
;; Y' = 2y (1 + y^2(1/3 + y^2(1/7 + y^2(1/9 + ........)))
;;
;; Step 4) Denormalize value to get output
;;
;; Result = Y' + n*log(2)
;;
;;--------------------------------------------------------------------------
;; Cycles: ~148 (includes call & return)
;;--------------------------------------------------------------------------
;;#############################################################################
;;!
;;! Copyright: Copyright (C) 2023 Texas Instruments Incorporated -
;;! All rights reserved not granted herein.
;;! Limited License.
;;!
;;! Texas Instruments Incorporated grants a world-wide, royalty-free,
;;! non-exclusive license under copyrights and patents it now or hereafter
;;! owns or controls to make, have made, use, import, offer to sell and sell
;;! ("Utilize") this software subject to the terms herein. With respect to the
;;! foregoing patent license, such license is granted solely to the extent that
;;! any such patent is necessary to Utilize the software alone. The patent
;;! license shall not apply to any combinations which include this software,
;;! other than combinations with devices manufactured by or for TI
;;! ("TI Devices").
;;! No hardware patent is licensed hereunder.
;;!
;;! Redistributions must preserve existing copyright notices and reproduce this
;;! license (including the above copyright notice and the disclaimer and
;;! (if applicable) source code license limitations below) in the documentation
;;! and/or other materials provided with the distribution.
;;!
;;! Redistribution and use in binary form, without modification, are permitted
;;! provided that the following conditions are met:
;;!
;;! * No reverse engineering, decompilation, or disassembly of this software is
;;! permitted with respect to any software provided in binary form.
;;! * Any redistribution and use are licensed by TI for use only
;;! with TI Devices.
;;! * Nothing shall obligate TI to provide you with source code for the
;;! software licensed and provided to you in object code.
;;!
;;! If software source code is provided to you, modification and redistribution
;;! of the source code are permitted provided that the following conditions
;;! are met:
;;!
;;! * any redistribution and use of the source code, including any resulting
;;! derivative works, are licensed by TI for use only with TI Devices.
;;! * any redistribution and use of any object code compiled from the source
;;! code and any resulting derivative works, are licensed by TI for use
;;! only with TI Devices.
;;!
;;! Neither the name of Texas Instruments Incorporated nor the names of its
;;! suppliers may be used to endorse or promote products derived from this
;;! software without specific prior written permission.
;;#############################################################################
IQNlog .macro q_value
PUSH XAR2
PUSH XAR3
MOVL XAR6,@ACC ; XAR6 = X (QG)
;;-------------------------------------------------------------------------------
;; checking to see if input is < 0
;;-------------------------------------------------------------------------------
MOVL ACC,@ACC ; Set Z, N flags on ACC content
BF $90,LEQ ; Exit if X <= 0
;;-------------------------------------------------------------------------------
;; Step 1: Normalize Input:
;;-------------------------------------------------------------------------------
CSB ACC ; TH = normalization shift
MOV TL,#0 ; TL = 0
MOVL XAR3,@XT ; XAR3 = save shift value for de-norm operation.
LSLL ACC,T ; ACC = Xnorm (0.5 to 0.99999 range in Q31)
LSR64 ACC:P,#1 ; ACC = Xnorm (Q30)
;;---------------------------------------------------------------------------------
;; y = (Xnorm - 1)/(Xnorm + 1)
;;---------------------------------------------------------------------------------
MOV @PL,#0x0000 ; P = 1.0 (Q30)
MOV @PH,#0x4000
MOVL @XAR7,ACC ; XAR7 = Xnorm
ADDL ACC,@P ; ACC = Xnorm + 1.0 (Q30)
MOVL @XAR6,ACC ; XAR6 = Xnorm + 1.0 (Q30)
MOVL ACC,@XAR7 ; ACC = Xnorm
SUBL ACC,@P ; ACC = Xnorm - 1.0 (Q30)
;;---------------------------------------------------------------------------------
;; Perform Q30 Division
;;---------------------------------------------------------------------------------
ABS ACC ; ACC = Num = abs(Xnorm - 1.0)
MOVL P,@ACC ; P = abs(Num)
MOVB ACC,#0 ; ACC:P = abs(0:Num)
LSL64 ACC:P,#16
LSL64 ACC:P,#14
RPT #31
||SUBCUL ACC,@XAR6 ; Den = XAR6 = Xnorm + 1
MOVL ACC,@P ; ACC = abs(Result) = y (Q30)
NEG ACC ; Negate result (Q30)
MOVL XT,@ACC ; XT = y (Q30)
ADDL ACC,@ACC ; ACC = 2*y (Q30)
MOVL XAR2,@ACC ; XAR2 = 2*y (Q30)
LSL64 ACC:P,#1 ; ACC = 2*y (Q31)
MOVL XAR4,@ACC ; XAR4 = 2*y (Q31)
;;--------------------------------------------------------------------------------
;; Step 3) Obtain Y' = log(y) using taylor series expansion:
;; Y' = 2y (1 + y^2(1/3 + y^2(1/5 + y^2(1/7 + ........)))
;;--------------------------------------------------------------------------------
IMPYL P,XT,@XT
QMPYL ACC,XT,@XT
LSL64 ACC:P,#4 ; ACC = y^2 (Q32 = Q30 + Q30 + Q4 - 32)
MOVL XAR5,@ACC ; XAR5 = y^2 (Q32)
MOV @AL,#0x6186
MOV @AH,#0x0618 ; ACC = 1/21 (Q31)
MOVL XT,@ACC ; XT = 1/21 (Q31)
QMPYL P,XT,@XAR5 ; P = y^2*1/21 (Q31 = Q31 + Q32 - 32)
MOV @AL,#0xA1AF
MOV @AH,#0x06BC ; ACC = 1/19 (Q31)
ADDL ACC,@P ; ACC = 1/19 + Y^2*1/21 (Q31)
MOVL XT,@ACC
QMPYL P,XT,@XAR5 ; P = y^2(1/19 + Y^2*1/21)
MOV @AL,#0x8788
MOV @AH,#0x0787 ; ACC = 1/17 (Q31)
ADDL ACC,@P ; ACC = 1/17 + y^2(1/19 + Y^2*1/21)
MOVL XT,@ACC
QMPYL P,XT,@XAR5 ; P = y^2(17 + y^2(1/19 + Y^2*1/21))
MOV @AL,#0x8889
MOV @AH,#0x0888 ; ACC = 1/15 (Q31)
ADDL ACC,@P ; ACC = 1/15 + y^2(17 + y^2(1/19 + Y^2*1/21))
MOVL XT,@ACC
QMPYL P,XT,@XAR5 ; P = y^2(1/15 + y^2(17 + y^2(1/19 + Y^2*1/21)))
MOV @AL,#0x9D8A
MOV @AH,#0x09D8 ; ACC = 1/13 (Q31)
ADDL ACC,@P ; ACC = 1/13 + y^2(1/15 + y^2(17 + y^2(1/19 + Y^2*1/21)))
MOVL XT,@ACC
QMPYL P,XT,@XAR5 ; P = y^2(1/13 + y^2(1/15 + y^2(17 + y^2(1/19 + Y^2*1/21))))
MOV @AL,#0xE8BA
MOV @AH,#0x0BA2 ; ACC = 1/11 (Q31)
ADDL ACC,@P ; ACC = 1/11 + y^2(1/13 + y^2(1/15 + y^2(17 + y^2(1/19 + Y^2*1/21))))
MOVL XT,@ACC
QMPYL P,XT,@XAR5 ; P = y^2(1/11 + y^2(1/13 + y^2(1/15 + y^2(17 + y^2(1/19 + Y^2*1/21)))))
MOV @AL,#0xE38E
MOV @AH,#0x0E38 ; ACC = 1/9 (Q31)
ADDL ACC,@P ; ACC = 1/9 + y^2(1/11 + y^2(1/13 + y^2(1/15 + y^2(17 + y^2(1/19 + Y^2*1/21)))))
MOVL XT,@ACC
QMPYL P,XT,@XAR5 ; P = y^2(1/9 + y^2(1/11 + y^2(1/13 + y^2(1/15 + y^2(17 + y^2(1/19 + Y^2*1/21))))))
MOV @AL,#0x2492
MOV @AH,#0x1249 ; ACC = 1/7 (Q31)
ADDL ACC,@P ; ACC = 1/7 + y^2(1/9 + y^2(1/11 + y^2(1/13 + y^2(1/15 + y^2(17 + y^2(1/19 + Y^2*1/21))))))
MOVL XT,@ACC
QMPYL P,XT,@XAR5 ; P = y^2(1/7 + y^2(1/9 + y^2(1/11 + y^2(1/13 + y^2(1/15 + y^2(17 + y^2(1/19 + Y^2*1/21)))))))
MOV @AL,#0x999A
MOV @AH,#0x1999 ; ACC = 1/5 (Q31)
ADDL ACC,@P ; ACC = 1/5 + y^2(1/7 + y^2(1/9 + y^2(1/11 + y^2(1/13 + y^2(1/15 + y^2(17 + y^2(1/19 + Y^2*1/21)))))))
MOVL XT,@ACC
QMPYL P,XT,@XAR5 ; P = y^2(1/5 + y^2(1/7 + y^2(1/9 + y^2(1/11 + y^2(1/13 + y^2(1/15 + y^2(17 + y^2(1/19 + Y^2*1/21))))))))
MOV @AL,#0xAAAB
MOV @AH,#0x2AAA ; ACC = 1/3 (Q31)
ADDL ACC,@P ; ACC = 1/3 + y^2(1/5 + y^2(1/7 + y^2(1/9 + y^2(1/11 + y^2(1/13 + y^2(1/15 + y^2(17 + y^2(1/19 + Y^2*1/21))))))))
MOVL XT,@ACC
QMPYL P,XT,@XAR5 ; P = y^2(1/3 + y^2(1/5 + y^2(1/7 + y^2(1/9 + y^2(1/11 + y^2(1/13 + y^2(1/15 + y^2(17 + y^2(1/19 + Y^2*1/21)))))))))
MOVL XT,@XAR4 ; XT = 2*y (Q31)
QMPYL P,XT,@P ; P = 2*y(y^2(1/3 + y^2(1/5 + y^2(1/7 + y^2(1/9 + y^2(1/11 + y^2(1/13 + y^2(1/15 + y^2(17 + y^2(1/19 + Y^2*1/21))))))))))
; P = (Q30 = Q31 + Q31 - 32)
MOVL ACC,@XAR2 ; ACC = 2*y (Q30)
ADDL ACC,@P ; ACC = 2*y + 2*y(y^2(1/3 + y^2(1/5 + y^2(1/7 + y^2(1/11 + y^2(1/13 + y^2(1/15 + y^2(17 + y^2(1/19 + Y^2*1/21)))))))))
; ACC = Y' (Q30)
MPYB P,T,#0 ; P = 0
.if q_value > 13
ASR64 ACC:P,#(30 - q_value) ; ACC:P = Y' (q_value)
.else
ASR64 ACC:P,#16
ASR64 ACC:P,#(14 - q_value)
.endif
MOVL XAR6,@P
MOVL XAR7,ACC ; temp save Y'
;;---------------------------------------------------------------------------------
;; Step 4) Denormalize value to get output
;;---------------------------------------------------------------------------------
MOV @AL,#0x0000
MOV @AH,#(31 - q_value) ; ACC = (31 - q_value) << 16
SUBL ACC,@XAR3 ; ACC = ((31 - q-value) - norm shift) << 16
ASR64 ACC:P,#16 ; ACC = (31 - q-value) - norm shift
MOVL XT,@ACC ; XT = (31 - q-value) - norm shift
MOV @AL,#0x0BFC
MOV @AH,#0x58B9 ; ACC = ln(2) (Q31)
IMPYL P,XT,@ACC
QMPYL ACC,XT,@ACC ; ACC:P = ((31 - q-value) - norm shift) * ln(2) (Q31)
.if q_value > 15
LSL64 ACC:P,#16
LSL64 ACC:P,#(q_value - 15)
.else
LSL64 ACC:P,#(q_value + 1)
.endif ; ACC:P = ((31 - q-value) - norm shift) * ln(2) (q_value)
ADDUL P,@XAR6
ADDCL ACC,@XAR7 ; ACC:P = Y' + ((31 - q-value) - norm shift) * ln(2) (q_value)
POP XAR3
POP XAR2
LRETR
; Exit for X <= 0
$90:
MOV @AL,#0x0000 ; Return most negative value.
MOV @AH,#0x8000
POP XAR3
POP XAR2
LRETR
.endm
;; The value "GLOBAL_Q" needs to be supplied by the assembler
;; using the "-dGLOBAL_Q=q_value" directive:
.sect "IQmath"
.if GLOBAL_Q == 30
.def _IQ30log
_IQ30log:
IQNlog GLOBAL_Q
.endif
.if GLOBAL_Q == 29
.def _IQ29log
_IQ29log:
IQNlog GLOBAL_Q
.endif
.if GLOBAL_Q == 28
.def _IQ28log
_IQ28log:
IQNlog GLOBAL_Q
.endif
.if GLOBAL_Q == 27
.def _IQ27log
_IQ27log:
IQNlog GLOBAL_Q
.endif
.if GLOBAL_Q == 26
.def _IQ26log
_IQ26log:
IQNlog GLOBAL_Q
.endif
.if GLOBAL_Q == 25
.def _IQ25log
_IQ25log:
IQNlog GLOBAL_Q
.endif
.if GLOBAL_Q == 24
.def _IQ24log
_IQ24log:
IQNlog GLOBAL_Q
.endif
.if GLOBAL_Q == 23
.def _IQ23log
_IQ23log:
IQNlog GLOBAL_Q
.endif
.if GLOBAL_Q == 22
.def _IQ22log
_IQ22log:
IQNlog GLOBAL_Q
.endif
.if GLOBAL_Q == 21
.def _IQ21log
_IQ21log:
IQNlog GLOBAL_Q
.endif
.if GLOBAL_Q == 20
.def _IQ20log
_IQ20log:
IQNlog GLOBAL_Q
.endif
.if GLOBAL_Q == 19
.def _IQ19log
_IQ19log:
IQNlog GLOBAL_Q
.endif
.if GLOBAL_Q == 18
.def _IQ18log
_IQ18log:
IQNlog GLOBAL_Q
.endif
.if GLOBAL_Q == 17
.def _IQ17log
_IQ17log:
IQNlog GLOBAL_Q
.endif
.if GLOBAL_Q == 16
.def _IQ16log
_IQ16log:
IQNlog GLOBAL_Q
.endif
.if GLOBAL_Q == 15
.def _IQ15log
_IQ15log:
IQNlog GLOBAL_Q
.endif
.if GLOBAL_Q == 14
.def _IQ14log
_IQ14log:
IQNlog GLOBAL_Q
.endif
.if GLOBAL_Q == 13
.def _IQ13log
_IQ13log:
IQNlog GLOBAL_Q
.endif
.if GLOBAL_Q == 12
.def _IQ12log
_IQ12log:
IQNlog GLOBAL_Q
.endif
.if GLOBAL_Q == 11
.def _IQ11log
_IQ11log:
IQNlog GLOBAL_Q
.endif
.if GLOBAL_Q == 10
.def _IQ10log
_IQ10log:
IQNlog GLOBAL_Q
.endif
.if GLOBAL_Q == 9
.def _IQ9log
_IQ9log:
IQNlog GLOBAL_Q
.endif
.if GLOBAL_Q == 8
.def _IQ8log
_IQ8log:
IQNlog GLOBAL_Q
.endif
.if GLOBAL_Q == 7
.def _IQ7log
_IQ7log:
IQNlog GLOBAL_Q
.endif
.if GLOBAL_Q == 6
.def _IQ6log
_IQ6log:
IQNlog GLOBAL_Q
.endif
.if GLOBAL_Q == 5
.def _IQ5log
_IQ5log:
IQNlog GLOBAL_Q
.endif
.if GLOBAL_Q == 4
.def _IQ4log
_IQ4log:
IQNlog GLOBAL_Q
.endif
.if GLOBAL_Q == 3
.def _IQ3log
_IQ3log:
IQNlog GLOBAL_Q
.endif
.if GLOBAL_Q == 2
.def _IQ2log
_IQ2log:
IQNlog GLOBAL_Q
.endif
.if GLOBAL_Q == 1
.def _IQ1log
_IQ1log:
IQNlog GLOBAL_Q
.endif
;;###########################################################################
;; No More.
;;###########################################################################
@@ -0,0 +1,357 @@
;;###########################################################################
;;
;; FILE: IQNmag.asm
;;
;; TITLE: C Callable IQ Magnitude Math Function
;;
;;###########################################################################
;;
;; Ver | Date | Who | Description of changes
;; =====|=============|=======|==============================================
;; 1.4 | 17 May 2002 | A. T. | Original Release.
;; -----|-------------|-------|----------------------------------------------
;; | | |
;;
;;###########################################################################
;;===========================================================================
;; Function: _IQNmag
;;===========================================================================
;;
;; C Usage: extern long _IQNmag(long A, long B); // with rounding & sat
;;
;;---------------------------------------------------------------------------
;;
;; On Entry: ACC = A in IQ format
;; SP-4 = B in IQ format
;;
;; Regs Used: XAR7, XAR6, XAR5, XAR4
;; XT, P, XAR0
;;
;; On Exit: ACC = sqrt(A*A + B*B) result in IQ format
;;
;; Q range: 30 to 1
;;
;;---------------------------------------------------------------------------
;; Algorithm: To calculate the magnitute we basically use the sqrt function.
;; The procedure for calculating "Y = sqrt(X)" is similar
;; to calculating the inverse sqare root due to the
;; following relationship:
;;
;; sqrt(X) = X * 1/sqrt(X)
;;
;; The procedure is as follows:
;;
;; Step 1) Calculate value to square root:
;;
;; X = (A*A + B*B)
;;
;; Step 2) Normalize Input:
;;
;; V = X * 2^n
;;
;; Step 3) Obtain initial estimate from "_IQsqrtTable"
;; using the upper 8-bits of the normalized V value.
;;
;; Step 4) Use Newton-Raphson algorithm to improve accuracy.
;; Repeat following equation two times. First iteration
;; gives 16-bit accuracy. Second iteration gives 32-bit
;; accuracy:
;;
;; Yn = Yn(1.5 - Yn*Yn*V/2)
;;
;; Yn = 1/sqrt(V)
;;
;; Step 5) Caculate the square root of the normalized number:
;;
;; Yn = V * Yn
;;
;; Step 6) Denormalize result and round:
;;
;; Y = Yn * 2^n / sqrt(2^n)
;;
;;---------------------------------------------------------------------------
;; Benchmark:
;;
;; Assumptions:
;; * Code executes from 0-wait SARAM block.
;; * _IQsqrt Tables located in seperate 0-wait SARAM or 1-wait ROM block.
;; * Stack located in seperate 0-wait SARAM block.
;; * Includes LCR/LRETR.
;;
;; Cycles = 89 (if _IQsqrt Tables in 0-wait SARAM)
;; = 93 (if _IQsqrt Tables in 1-wait ROM)
;;
;;===========================================================================
IQNmag .macro q_value
MOVL XT,@ACC
IMPYL P,XT,@ACC
QMPYL ACC,XT,@ACC ; ACC:P = A*A
MOVL XAR6,@P
MOVL XAR7,@ACC ; XAR7:XAR6 = B*B
MOVL XT,*-SP[4]
IMPYL P,XT,*-SP[4]
QMPYL ACC,XT,*-SP[4] ; ACC:P = B*B
ADDUL P,@XAR6
ZAP OVC
ADDCL ACC,@XAR7 ; ACC:P = A*A + B*B (2*q_value)
SAT64 ACC:P ; ACC:P = saturated(A*A + B*B) = X
CSB ACC
LSL64 ACC:P,T
MOV @AR7,T
CSB ACC
LSL64 ACC:P,T ; ACC:P = V/2 (Q32)
MOVL XAR6,@ACC ; XAR6 = V/2 (Q32)
ASR AH,#6
MOVB @AH,#0xFE,LEQ ; AR0 -> zero value if -ve or 0 input
SUB @AH,#254 ; Scale to table offset
MOVZ AR0,@AH ; AR0 -> index into _IQisqrtTable for
; initial estimate of Yn
MOV AH,@AR7
ADD AH,@T ; AH = n
TBIT @AH,#0 ; TC = odd/even shift indicator
LSR AH,#1 ; AH = n/2
MOVL *SP++,ACC ; SP-2 = n/2
MOVL XAR7,#_IQsqrtTable ; XAR7 = pointer to _IQisqrtTable
MOVL XAR4,*+XAR7[AR0] ; (1) XAR4 = initial Yn estimate (Q30)
MOVL XAR7,#_IQsqrtRoundSatTable
MOVL XAR5,*XAR7++ ; XAR5 = 1.5 (Q30)
MOVB @AR0,#12,NTC ; AR0 -> 1/sqrt(2) if n == even (Q30)
MOVB @AR0,#10,TC ; AR0 -> 0.5 if n == odd (Q30)
MOVL XT,@XAR4 ;(2) XT = Yn (Q30)
QMPYL ACC,XT,@XT ;(3) ACC = Yn*Yn (Q28)
MOVL XT,@XAR6 ;(4) XT = V/2 (Q32)
LSL ACC,#2 ;(5) ACC = Yn*Yn (Q30)
QMPYL ACC,XT,@ACC ;(6) ACC = Yn*Yn*V/2 (Q30)
MOVL XT,@XAR5 ;(7) XT = 1.5 (Q30)
SUBL @XT,ACC ;(8) XT = 1.5 - Yn*Yn*V/2 (Q30)
QMPYL ACC,XT,@XAR4 ;(9) ACC = Yn(1.5 - Yn*Yn*V/2) (Q28)
LSL ACC,#2 ;(10) ACC = Yn(1.5 - Yn*Yn*V/2) (Q30)
MOVL XAR4,@ACC ;(1) XAR4 = Yn (Q30)
MOVL XT,@XAR4 ;(2) XT = Yn (Q30)
QMPYL ACC,XT,@XT ;(3) ACC = Yn*Yn (Q28)
MOVL XT,@XAR6 ;(4) XT = V/2 (Q32)
LSL ACC,#2 ;(5) ACC = Yn*Yn (Q30)
QMPYL ACC,XT,@ACC ;(6) ACC = Yn*Yn*V/2 (Q30)
MOVL XT,@XAR5 ;(7) XT = 1.5 (Q30)
SUBL @XT,ACC ;(8) XT = 1.5 - Yn*Yn*V/2 (Q30)
QMPYL ACC,XT,@XAR4 ;(9) ACC = Yn(1.5 - Yn*Yn*V/2) (Q28)
LSL ACC,#2 ;(10) ACC = Yn(1.5 - Yn*Yn*V/2) (Q30)
MOVL XT,@XAR6
QMPYL ACC,XT,@ACC ; sqrt(X) = X * 1/sqrt(X)
MOVL XT,*+XAR7[AR0] ; Scale with 1.0 or 1/sqrt(X) or 0.5
IMPYL P,XT,@ACC
QMPYL ACC,XT,@ACC
MOVL XT,*--SP ; T = n/2
ASR64 ACC:P,T ; Scale final result based on n/2
LSL64 ACC:P,#4 ; Scale final result based on Q value
ADDUL P,@P
ZAP OVC
ADDCL ACC,@ACC
SAT64 ACC:P
ADD @PH,#-32768 ; Round final result (ACC:P += 0x0000000080000000)
ZAP OVC ; Zero overflow counter
ADDCL ACC,*+XAR7[2]
SAT64 ACC:P ; Satuate if overflow
LRETR
.endm
;; The value "GLOBAL_Q" needs to be supplied by the assembler
;; using the "-dGLOBAL_Q=q_value" directive:
.sect "IQmath"
.ref _IQsqrtTable
.ref _IQsqrtRoundSatTable
.if GLOBAL_Q == 30
.def _IQ30mag
_IQ30mag:
IQNmag GLOBAL_Q
.endif
.if GLOBAL_Q == 29
.def _IQ29mag
_IQ29mag:
IQNmag GLOBAL_Q
.endif
.if GLOBAL_Q == 28
.def _IQ28mag
_IQ28mag:
IQNmag GLOBAL_Q
.endif
.if GLOBAL_Q == 27
.def _IQ27mag
_IQ27mag:
IQNmag GLOBAL_Q
.endif
.if GLOBAL_Q == 26
.def _IQ26mag
_IQ26mag:
IQNmag GLOBAL_Q
.endif
.if GLOBAL_Q == 25
.def _IQ25mag
_IQ25mag:
IQNmag GLOBAL_Q
.endif
.if GLOBAL_Q == 24
.def _IQ24mag
_IQ24mag:
IQNmag GLOBAL_Q
.endif
.if GLOBAL_Q == 23
.def _IQ23mag
_IQ23mag:
IQNmag GLOBAL_Q
.endif
.if GLOBAL_Q == 22
.def _IQ22mag
_IQ22mag:
IQNmag GLOBAL_Q
.endif
.if GLOBAL_Q == 21
.def _IQ21mag
_IQ21mag:
IQNmag GLOBAL_Q
.endif
.if GLOBAL_Q == 20
.def _IQ20mag
_IQ20mag:
IQNmag GLOBAL_Q
.endif
.if GLOBAL_Q == 19
.def _IQ19mag
_IQ19mag:
IQNmag GLOBAL_Q
.endif
.if GLOBAL_Q == 18
.def _IQ18mag
_IQ18mag:
IQNmag GLOBAL_Q
.endif
.if GLOBAL_Q == 17
.def _IQ17mag
_IQ17mag:
IQNmag GLOBAL_Q
.endif
.if GLOBAL_Q == 16
.def _IQ16mag
_IQ16mag:
IQNmag GLOBAL_Q
.endif
.if GLOBAL_Q == 15
.def _IQ15mag
_IQ15mag:
IQNmag GLOBAL_Q
.endif
.if GLOBAL_Q == 14
.def _IQ14mag
_IQ14mag:
IQNmag GLOBAL_Q
.endif
.if GLOBAL_Q == 13
.def _IQ13mag
_IQ13mag:
IQNmag GLOBAL_Q
.endif
.if GLOBAL_Q == 12
.def _IQ12mag
_IQ12mag:
IQNmag GLOBAL_Q
.endif
.if GLOBAL_Q == 11
.def _IQ11mag
_IQ11mag:
IQNmag GLOBAL_Q
.endif
.if GLOBAL_Q == 10
.def _IQ10mag
_IQ10mag:
IQNmag GLOBAL_Q
.endif
.if GLOBAL_Q == 9
.def _IQ9mag
_IQ9mag:
IQNmag GLOBAL_Q
.endif
.if GLOBAL_Q == 8
.def _IQ8mag
_IQ8mag:
IQNmag GLOBAL_Q
.endif
.if GLOBAL_Q == 7
.def _IQ7mag
_IQ7mag:
IQNmag GLOBAL_Q
.endif
.if GLOBAL_Q == 6
.def _IQ6mag
_IQ6mag:
IQNmag GLOBAL_Q
.endif
.if GLOBAL_Q == 5
.def _IQ5mag
_IQ5mag:
IQNmag GLOBAL_Q
.endif
.if GLOBAL_Q == 4
.def _IQ4mag
_IQ4mag:
IQNmag GLOBAL_Q
.endif
.if GLOBAL_Q == 3
.def _IQ3mag
_IQ3mag:
IQNmag GLOBAL_Q
.endif
.if GLOBAL_Q == 2
.def _IQ2mag
_IQ2mag:
IQNmag GLOBAL_Q
.endif
.if GLOBAL_Q == 1
.def _IQ1mag
_IQ1mag:
IQNmag GLOBAL_Q
.endif
;;###########################################################################
;; No More.
;;###########################################################################
@@ -0,0 +1,265 @@
;;###########################################################################
;;
;; FILE: IQNmpy.asm
;;
;; TITLE: C Callable IQ Multiply Math Function
;;
;;###########################################################################
;;
;; Ver | Date | Who | Description of changes
;; =====|=============|=======|==============================================
;; 1.2 | 11 Oct 2001 | A. T. | Original Release.
;; -----|-------------|-------|----------------------------------------------
;; 1.3 | 19 Nov 2001 | A. T. | No changes.
;; -----|-------------|-------|----------------------------------------------
;; 1.4 | 4 May 2002 | A. T. | No changes.
;; | | |
;; | | | Note: In CCS 2.01 and above, the codegen
;; | | | tools support an _IQmpy(..) intrinsic
;; | | | which replaces the IQNmpy(..) function.
;; -----|-------------|-------|----------------------------------------------
;; | | |
;;
;;###########################################################################
;;===========================================================================
;; Function: _IQNmpy
;;===========================================================================
;;
;; C Usage: extern long _IQmpy(long M, long X); // no round or sat
;;
;;---------------------------------------------------------------------------
;;
;; On Entry: ACC = M in IQ format
;; [SP-4] = X in IQ format
;;
;; Regs Used: XT, P
;;
;; On Exit: ACC = M * X in IQ format
;;
;; Q range: 30 to 1
;;
;;---------------------------------------------------------------------------
;; Algorithm: The IQmpy operation generates a 64-bit result. The 64-bit
;; number must then be scaled back to a 32-bit value with the
;; same Q value as the original inputs. For example: if Q = 27,
;; then the multiplication of two I5Q27 numbers results in the
;; following 64-bit value:
;;
;; I5Q27 * I5Q27 = I10Q54
;;
;; The value is then scaled back to an I5Q27 value as follows:
;;
;; I5Q27 = I10Q54 >> Q
;;
;; For some of the operations, the value is either rounded or
;; saturated (or both) to its maximum value before scaling.
;;
;;---------------------------------------------------------------------------
;; Benchmark:
;;
;; Cycles = 13 (Q >= 16, includes LCR/LRETR)
;; 14 (Q <= 15, includes LCR/LRETR)
;;
;;---------------------------------------------------------------------------
IQNmpy .macro q_value
MOVL XT,@ACC ; XT = M
IMPYL P,XT,*-SP[4]
QMPYL ACC,XT,*-SP[4] ; ACC:P = M * X
.if q_value >= 16
LSL64 ACC:P,#(32 - q_value) ; Scale back to required IQ value
.endif
.if q_value <= 15
LSL64 ACC:P,#16 ; Scale back to required IQ value
LSL64 ACC:P,#(16 - q_value)
.endif
LRETR
.endm
;; The value "GLOBAL_Q" needs to be supplied by the assembler
;; using the "-dGLOBAL_Q=q_value" directive:
.sect "IQmath"
.if GLOBAL_Q == 30
.def _IQ30mpy
_IQ30mpy:
IQNmpy GLOBAL_Q
.endif
.if GLOBAL_Q == 29
.def _IQ29mpy
_IQ29mpy:
IQNmpy GLOBAL_Q
.endif
.if GLOBAL_Q == 28
.def _IQ28mpy
_IQ28mpy:
IQNmpy GLOBAL_Q
.endif
.if GLOBAL_Q == 27
.def _IQ27mpy
_IQ27mpy:
IQNmpy GLOBAL_Q
.endif
.if GLOBAL_Q == 26
.def _IQ26mpy
_IQ26mpy:
IQNmpy GLOBAL_Q
.endif
.if GLOBAL_Q == 25
.def _IQ25mpy
_IQ25mpy:
IQNmpy GLOBAL_Q
.endif
.if GLOBAL_Q == 24
.def _IQ24mpy
_IQ24mpy:
IQNmpy GLOBAL_Q
.endif
.if GLOBAL_Q == 23
.def _IQ23mpy
_IQ23mpy:
IQNmpy GLOBAL_Q
.endif
.if GLOBAL_Q == 22
.def _IQ22mpy
_IQ22mpy:
IQNmpy GLOBAL_Q
.endif
.if GLOBAL_Q == 21
.def _IQ21mpy
_IQ21mpy:
IQNmpy GLOBAL_Q
.endif
.if GLOBAL_Q == 20
.def _IQ20mpy
_IQ20mpy:
IQNmpy GLOBAL_Q
.endif
.if GLOBAL_Q == 19
.def _IQ19mpy
_IQ19mpy:
IQNmpy GLOBAL_Q
.endif
.if GLOBAL_Q == 18
.def _IQ18mpy
_IQ18mpy:
IQNmpy GLOBAL_Q
.endif
.if GLOBAL_Q == 17
.def _IQ17mpy
_IQ17mpy:
IQNmpy GLOBAL_Q
.endif
.if GLOBAL_Q == 16
.def _IQ16mpy
_IQ16mpy:
IQNmpy GLOBAL_Q
.endif
.if GLOBAL_Q == 15
.def _IQ15mpy
_IQ15mpy:
IQNmpy GLOBAL_Q
.endif
.if GLOBAL_Q == 14
.def _IQ14mpy
_IQ14mpy:
IQNmpy GLOBAL_Q
.endif
.if GLOBAL_Q == 13
.def _IQ13mpy
_IQ13mpy:
IQNmpy GLOBAL_Q
.endif
.if GLOBAL_Q == 12
.def _IQ12mpy
_IQ12mpy:
IQNmpy GLOBAL_Q
.endif
.if GLOBAL_Q == 11
.def _IQ11mpy
_IQ11mpy:
IQNmpy GLOBAL_Q
.endif
.if GLOBAL_Q == 10
.def _IQ10mpy
_IQ10mpy:
IQNmpy GLOBAL_Q
.endif
.if GLOBAL_Q == 9
.def _IQ9mpy
_IQ9mpy:
IQNmpy GLOBAL_Q
.endif
.if GLOBAL_Q == 8
.def _IQ8mpy
_IQ8mpy:
IQNmpy GLOBAL_Q
.endif
.if GLOBAL_Q == 7
.def _IQ7mpy
_IQ7mpy:
IQNmpy GLOBAL_Q
.endif
.if GLOBAL_Q == 6
.def _IQ6mpy
_IQ6mpy:
IQNmpy GLOBAL_Q
.endif
.if GLOBAL_Q == 5
.def _IQ5mpy
_IQ5mpy:
IQNmpy GLOBAL_Q
.endif
.if GLOBAL_Q == 4
.def _IQ4mpy
_IQ4mpy:
IQNmpy GLOBAL_Q
.endif
.if GLOBAL_Q == 3
.def _IQ3mpy
_IQ3mpy:
IQNmpy GLOBAL_Q
.endif
.if GLOBAL_Q == 2
.def _IQ2mpy
_IQ2mpy:
IQNmpy GLOBAL_Q
.endif
.if GLOBAL_Q == 1
.def _IQ1mpy
_IQ1mpy:
IQNmpy GLOBAL_Q
.endif
;;###########################################################################
;; No More.
;;###########################################################################
@@ -0,0 +1,261 @@
;;###########################################################################
;;
;; FILE: IQNmpyI32frac.asm
;;
;; TITLE: C Callable IQ Multiply I32 Return Fraction Math Function
;;
;;###########################################################################
;;
;; Ver | Date | Who | Description of changes
;; =====|=============|=======|==============================================
;; 1.4 | 17 May 2002 | A. T. | Original Release.
;; -----|-------------|-------|----------------------------------------------
;; 1.4c | 20 Jun 2002 | A. T. | Fixed problem with function when operating
;; | | | on negative values with a zero fractional
;; | | | part, incorrect result was given.
;; -----|-------------|-------|----------------------------------------------
;; 1.4d | 30 Mar 2002 | SD | Re-implemented the function without branch
;; -----|-------------|-------|----------------------------------------------
;;
;;###########################################################################
;;===========================================================================
;; Function: _IQNmpyI32frac
;;===========================================================================
;;
;; C Usage: extern long _IQmpyI32frac(long A, long B); // no round or sat
;;
;;---------------------------------------------------------------------------
;;
;; On Entry: ACC = A in IQ format
;; SP-4 = B in 32-bit integer (long) format
;;
;; Regs Used: XT, P, XAR6, XAR7
;;
;; On Exit: ACC = fractional portion of A*B stored as IQ
;;
;; Q range: 30 to 1
;;
;;---------------------------------------------------------------------------
;; Algorithm: The fractional portion of the IQ*I32 number is calculated
;; as follows:
;;
;; fraction = A*B - ((A*B >> q_value) << q_value)
;;
;;---------------------------------------------------------------------------
;; Benchmark:
;;
;; Cycles = 34 (worst case, includes LCR/LRETR)
;;
;;---------------------------------------------------------------------------
IQNmpyI32frac .macro q_value
SETC OVM
CLRC TC
ABSTC ACC
MOVL XT,ACC
MOVL ACC,*-SP[4]
ABSTC ACC
IMPYL P,XT,@ACC
QMPYL ACC,XT,@ACC ; ACC:P = IQ*I32 (Q=q_value)
MOV @T,#(32-q_value)
LSL64 ACC:P,T ; P=frac bits (aligned to MSB)
MOVB ACC,#0
MOV @T,#(q_value)
LSL64 ACC:P,T ; ACC=frac bits (aligned to LSB)
NEGTC ACC
CLRC OVM ; Clear overflow mode
LRETR
.endm
;; The value "GLOBAL_Q" needs to be supplied by the assembler
;; using the "-dGLOBAL_Q=q_value" directive:
.sect "IQmath"
.if GLOBAL_Q == 30
.def _IQ30mpyI32frac
_IQ30mpyI32frac:
IQNmpyI32frac GLOBAL_Q
.endif
.if GLOBAL_Q == 29
.def _IQ29mpyI32frac
_IQ29mpyI32frac:
IQNmpyI32frac GLOBAL_Q
.endif
.if GLOBAL_Q == 28
.def _IQ28mpyI32frac
_IQ28mpyI32frac:
IQNmpyI32frac GLOBAL_Q
.endif
.if GLOBAL_Q == 27
.def _IQ27mpyI32frac
_IQ27mpyI32frac:
IQNmpyI32frac GLOBAL_Q
.endif
.if GLOBAL_Q == 26
.def _IQ26mpyI32frac
_IQ26mpyI32frac:
IQNmpyI32frac GLOBAL_Q
.endif
.if GLOBAL_Q == 25
.def _IQ25mpyI32frac
_IQ25mpyI32frac:
IQNmpyI32frac GLOBAL_Q
.endif
.if GLOBAL_Q == 24
.def _IQ24mpyI32frac
_IQ24mpyI32frac:
IQNmpyI32frac GLOBAL_Q
.endif
.if GLOBAL_Q == 23
.def _IQ23mpyI32frac
_IQ23mpyI32frac:
IQNmpyI32frac GLOBAL_Q
.endif
.if GLOBAL_Q == 22
.def _IQ22mpyI32frac
_IQ22mpyI32frac:
IQNmpyI32frac GLOBAL_Q
.endif
.if GLOBAL_Q == 21
.def _IQ21mpyI32frac
_IQ21mpyI32frac:
IQNmpyI32frac GLOBAL_Q
.endif
.if GLOBAL_Q == 20
.def _IQ20mpyI32frac
_IQ20mpyI32frac:
IQNmpyI32frac GLOBAL_Q
.endif
.if GLOBAL_Q == 19
.def _IQ19mpyI32frac
_IQ19mpyI32frac:
IQNmpyI32frac GLOBAL_Q
.endif
.if GLOBAL_Q == 18
.def _IQ18mpyI32frac
_IQ18mpyI32frac:
IQNmpyI32frac GLOBAL_Q
.endif
.if GLOBAL_Q == 17
.def _IQ17mpyI32frac
_IQ17mpyI32frac:
IQNmpyI32frac GLOBAL_Q
.endif
.if GLOBAL_Q == 16
.def _IQ16mpyI32frac
_IQ16mpyI32frac:
IQNmpyI32frac GLOBAL_Q
.endif
.if GLOBAL_Q == 15
.def _IQ15mpyI32frac
_IQ15mpyI32frac:
IQNmpyI32frac GLOBAL_Q
.endif
.if GLOBAL_Q == 14
.def _IQ14mpyI32frac
_IQ14mpyI32frac:
IQNmpyI32frac GLOBAL_Q
.endif
.if GLOBAL_Q == 13
.def _IQ13mpyI32frac
_IQ13mpyI32frac:
IQNmpyI32frac GLOBAL_Q
.endif
.if GLOBAL_Q == 12
.def _IQ12mpyI32frac
_IQ12mpyI32frac:
IQNmpyI32frac GLOBAL_Q
.endif
.if GLOBAL_Q == 11
.def _IQ11mpyI32frac
_IQ11mpyI32frac:
IQNmpyI32frac GLOBAL_Q
.endif
.if GLOBAL_Q == 10
.def _IQ10mpyI32frac
_IQ10mpyI32frac:
IQNmpyI32frac GLOBAL_Q
.endif
.if GLOBAL_Q == 9
.def _IQ9mpyI32frac
_IQ9mpyI32frac:
IQNmpyI32frac GLOBAL_Q
.endif
.if GLOBAL_Q == 8
.def _IQ8mpyI32frac
_IQ8mpyI32frac:
IQNmpyI32frac GLOBAL_Q
.endif
.if GLOBAL_Q == 7
.def _IQ7mpyI32frac
_IQ7mpyI32frac:
IQNmpyI32frac GLOBAL_Q
.endif
.if GLOBAL_Q == 6
.def _IQ6mpyI32frac
_IQ6mpyI32frac:
IQNmpyI32frac GLOBAL_Q
.endif
.if GLOBAL_Q == 5
.def _IQ5mpyI32frac
_IQ5mpyI32frac:
IQNmpyI32frac GLOBAL_Q
.endif
.if GLOBAL_Q == 4
.def _IQ4mpyI32frac
_IQ4mpyI32frac:
IQNmpyI32frac GLOBAL_Q
.endif
.if GLOBAL_Q == 3
.def _IQ3mpyI32frac
_IQ3mpyI32frac:
IQNmpyI32frac GLOBAL_Q
.endif
.if GLOBAL_Q == 2
.def _IQ2mpyI32frac
_IQ2mpyI32frac:
IQNmpyI32frac GLOBAL_Q
.endif
.if GLOBAL_Q == 1
.def _IQ1mpyI32frac
_IQ1mpyI32frac:
IQNmpyI32frac GLOBAL_Q
.endif
;;###########################################################################
;; No More.
;;###########################################################################
@@ -0,0 +1,259 @@
;;###########################################################################
;;
;; FILE: IQNmpyI32int.asm
;;
;; TITLE: C Callable IQ Multiply I32 Return Integer Math Function
;;
;;###########################################################################
;;
;; Ver | Date | Who | Description of changes
;; =====|=============|=======|==============================================
;; 1.4 | 17 May 2002 | A. T. | Original Release.
;; -----|-------------|-------|----------------------------------------------
;; 1.4c | 20 Jun 2002 | A. T. | Fixed problem with function when operating
;; | | | on negative values with a zero fractional
;; | | | part, incorrect result was given.
;; -----|-------------|-------|----------------------------------------------
;; 1.4d | 30 Mar 2002 | SD | Re-implemented the function without branch
;; -----|-------------|-------|----------------------------------------------
;;
;;###########################################################################
;;===========================================================================
;; Function: _IQNmpyI32int
;;===========================================================================
;;
;; C Usage: extern long _IQmpyI32int(long A, long B); // no round or sat
;;
;;---------------------------------------------------------------------------
;;
;; On Entry: ACC = A in IQ format
;; SP-4 = B in 32-bit integer (long) format
;;
;; Regs Used: XT, P
;;
;; On Exit: ACC = integer portion of A*B stored as "long"
;;
;; Q range: 30 to 1
;;
;;---------------------------------------------------------------------------
;; Algorithm: The integer portion of the IQ*I32 number is calculated
;; as follows:
;;
;; integer = A*B >> q_value;
;;
;;---------------------------------------------------------------------------
;; Benchmark:
;;
;; Cycles = 28 (worst case, includes LCR/LRETR)
;;
;;---------------------------------------------------------------------------
IQNmpyI32int .macro q_value
SETC OVM ; Turn overflow mode on
CLRC TC ; Clear sign flag
ABSTC ACC
MOVL XT,ACC
MOVL ACC,*-SP[4]
ABSTC ACC
IMPYL P,XT,@ACC
QMPYL ACC,XT,@ACC ; ACC:P = IQ*I32 (Q=q_value)
MOV @T,#(q_value)
LSR64 ACC:P,T ; P=integer bits
MOVL ACC,P
NEGTC ACC
CLRC OVM ; Turn overflow mode off
LRETR
.endm
;; The value "GLOBAL_Q" needs to be supplied by the assembler
;; using the "-dGLOBAL_Q=q_value" directive:
.sect "IQmath"
.if GLOBAL_Q == 30
.def _IQ30mpyI32int
_IQ30mpyI32int:
IQNmpyI32int GLOBAL_Q
.endif
.if GLOBAL_Q == 29
.def _IQ29mpyI32int
_IQ29mpyI32int:
IQNmpyI32int GLOBAL_Q
.endif
.if GLOBAL_Q == 28
.def _IQ28mpyI32int
_IQ28mpyI32int:
IQNmpyI32int GLOBAL_Q
.endif
.if GLOBAL_Q == 27
.def _IQ27mpyI32int
_IQ27mpyI32int:
IQNmpyI32int GLOBAL_Q
.endif
.if GLOBAL_Q == 26
.def _IQ26mpyI32int
_IQ26mpyI32int:
IQNmpyI32int GLOBAL_Q
.endif
.if GLOBAL_Q == 25
.def _IQ25mpyI32int
_IQ25mpyI32int:
IQNmpyI32int GLOBAL_Q
.endif
.if GLOBAL_Q == 24
.def _IQ24mpyI32int
_IQ24mpyI32int:
IQNmpyI32int GLOBAL_Q
.endif
.if GLOBAL_Q == 23
.def _IQ23mpyI32int
_IQ23mpyI32int:
IQNmpyI32int GLOBAL_Q
.endif
.if GLOBAL_Q == 22
.def _IQ22mpyI32int
_IQ22mpyI32int:
IQNmpyI32int GLOBAL_Q
.endif
.if GLOBAL_Q == 21
.def _IQ21mpyI32int
_IQ21mpyI32int:
IQNmpyI32int GLOBAL_Q
.endif
.if GLOBAL_Q == 20
.def _IQ20mpyI32int
_IQ20mpyI32int:
IQNmpyI32int GLOBAL_Q
.endif
.if GLOBAL_Q == 19
.def _IQ19mpyI32int
_IQ19mpyI32int:
IQNmpyI32int GLOBAL_Q
.endif
.if GLOBAL_Q == 18
.def _IQ18mpyI32int
_IQ18mpyI32int:
IQNmpyI32int GLOBAL_Q
.endif
.if GLOBAL_Q == 17
.def _IQ17mpyI32int
_IQ17mpyI32int:
IQNmpyI32int GLOBAL_Q
.endif
.if GLOBAL_Q == 16
.def _IQ16mpyI32int
_IQ16mpyI32int:
IQNmpyI32int GLOBAL_Q
.endif
.if GLOBAL_Q == 15
.def _IQ15mpyI32int
_IQ15mpyI32int:
IQNmpyI32int GLOBAL_Q
.endif
.if GLOBAL_Q == 14
.def _IQ14mpyI32int
_IQ14mpyI32int:
IQNmpyI32int GLOBAL_Q
.endif
.if GLOBAL_Q == 13
.def _IQ13mpyI32int
_IQ13mpyI32int:
IQNmpyI32int GLOBAL_Q
.endif
.if GLOBAL_Q == 12
.def _IQ12mpyI32int
_IQ12mpyI32int:
IQNmpyI32int GLOBAL_Q
.endif
.if GLOBAL_Q == 11
.def _IQ11mpyI32int
_IQ11mpyI32int:
IQNmpyI32int GLOBAL_Q
.endif
.if GLOBAL_Q == 10
.def _IQ10mpyI32int
_IQ10mpyI32int:
IQNmpyI32int GLOBAL_Q
.endif
.if GLOBAL_Q == 9
.def _IQ9mpyI32int
_IQ9mpyI32int:
IQNmpyI32int GLOBAL_Q
.endif
.if GLOBAL_Q == 8
.def _IQ8mpyI32int
_IQ8mpyI32int:
IQNmpyI32int GLOBAL_Q
.endif
.if GLOBAL_Q == 7
.def _IQ7mpyI32int
_IQ7mpyI32int:
IQNmpyI32int GLOBAL_Q
.endif
.if GLOBAL_Q == 6
.def _IQ6mpyI32int
_IQ6mpyI32int:
IQNmpyI32int GLOBAL_Q
.endif
.if GLOBAL_Q == 5
.def _IQ5mpyI32int
_IQ5mpyI32int:
IQNmpyI32int GLOBAL_Q
.endif
.if GLOBAL_Q == 4
.def _IQ4mpyI32int
_IQ4mpyI32int:
IQNmpyI32int GLOBAL_Q
.endif
.if GLOBAL_Q == 3
.def _IQ3mpyI32int
_IQ3mpyI32int:
IQNmpyI32int GLOBAL_Q
.endif
.if GLOBAL_Q == 2
.def _IQ2mpyI32int
_IQ2mpyI32int:
IQNmpyI32int GLOBAL_Q
.endif
.if GLOBAL_Q == 1
.def _IQ1mpyI32int
_IQ1mpyI32int:
IQNmpyI32int GLOBAL_Q
.endif
;;###########################################################################
;; No More.
;;###########################################################################
@@ -0,0 +1,298 @@
;;###########################################################################
;;
;; FILE: IQNrmpy.asm
;;
;; TITLE: C Callable IQ Multiply With Rounding Math Function
;;
;;###########################################################################
;;
;; Ver | Date | Who | Description of changes
;; =====|=============|=======|==============================================
;; 1.2 | 11 Oct 2001 | A. T. | Original Release.
;; -----|-------------|-------|----------------------------------------------
;; 1.3 | 19 Nov 2001 | A. T. | No changes.
;; -----|-------------|-------|----------------------------------------------
;; 1.4 | 17 May 2002 | A. T. | Made correction to table pointer.
;; | | | Corrected benchmark cycles.
;; -----|-------------|-------|----------------------------------------------
;; | | |
;;
;;###########################################################################
;;===========================================================================
;; Function: _IQNrmpy
;;===========================================================================
;;
;; C Usage: extern long _IQNrmpy(long M, long X); // with rounding only
;;
;;---------------------------------------------------------------------------
;;
;; On Entry: ACC = M in IQ format
;; [SP-4] = X in IQ format
;;
;; Regs Used: XT, P
;;
;; On Exit: ACC = M * X in IQ format
;;
;; Q range: 30 to 1
;;
;;---------------------------------------------------------------------------
;; Algorithm: The IQmpy operation generates a 64-bit result. The 64-bit
;; number must then be scaled back to a 32-bit value with the
;; same Q value as the original inputs. For example: if Q = 27,
;; then the multiplication of two I5Q27 numbers results in the
;; following 64-bit value:
;;
;; I5Q27 * I5Q27 = I10Q54
;;
;; The value is then scaled back to an I5Q27 value as follows:
;;
;; I5Q27 = I10Q54 >> Q
;;
;; For some of the operations, the value is either rounded or
;; saturated (or both) to its maximum value before scaling.
;;
;;---------------------------------------------------------------------------
;; Benchmark:
;;
;; Cycles = 16 (Q >= 16, if Table in 0-wait SARAM, includes LCR/LRETR)
;; 17 (Q <= 15, if Table in 0-wait SARAM, includes LCR/LRETR)
;;
;; Cycles = 18 (Q >= 16, if Table in 1-wait ROM, includes LCR/LRETR)
;; 19 (Q <= 15, if Table in 1-wait ROM, includes LCR/LRETR)
;;
;;---------------------------------------------------------------------------
IQNrmpy .macro q_value, table
MOVL XT,@ACC ; XT = M
IMPYL P,XT,*-SP[4]
QMPYL ACC,XT,*-SP[4] ; ACC:P = M * X
MOVL XAR7,#table
ADDUL P,*XAR7++ ; Round value
ADDCL ACC,*XAR7++
.if q_value >= 16
LSL64 ACC:P,#(32 - q_value) ; Scale back to required IQ value
.endif
.if q_value <= 15
LSL64 ACC:P,#16 ; Scale back to required IQ value
LSL64 ACC:P,#(16 - q_value)
.endif
LRETR
.endm
;; The value "GLOBAL_Q" needs to be supplied by the assembler
;; using the "-dGLOBAL_Q=q_value" directive:
.sect "IQmath"
.if GLOBAL_Q == 30
.ref _IQ30mpyRndSatTable
.def _IQ30rmpy
_IQ30rmpy:
IQNrmpy GLOBAL_Q, _IQ30mpyRndSatTable
.endif
.if GLOBAL_Q == 29
.ref _IQ29mpyRndSatTable
.def _IQ29rmpy
_IQ29rmpy:
IQNrmpy GLOBAL_Q, _IQ29mpyRndSatTable
.endif
.if GLOBAL_Q == 28
.ref _IQ28mpyRndSatTable
.def _IQ28rmpy
_IQ28rmpy:
IQNrmpy GLOBAL_Q, _IQ28mpyRndSatTable
.endif
.if GLOBAL_Q == 27
.ref _IQ27mpyRndSatTable
.def _IQ27rmpy
_IQ27rmpy:
IQNrmpy GLOBAL_Q, _IQ27mpyRndSatTable
.endif
.if GLOBAL_Q == 26
.ref _IQ26mpyRndSatTable
.def _IQ26rmpy
_IQ26rmpy:
IQNrmpy GLOBAL_Q, _IQ26mpyRndSatTable
.endif
.if GLOBAL_Q == 25
.ref _IQ25mpyRndSatTable
.def _IQ25rmpy
_IQ25rmpy:
IQNrmpy GLOBAL_Q, _IQ25mpyRndSatTable
.endif
.if GLOBAL_Q == 24
.ref _IQ24mpyRndSatTable
.def _IQ24rmpy
_IQ24rmpy:
IQNrmpy GLOBAL_Q, _IQ24mpyRndSatTable
.endif
.if GLOBAL_Q == 23
.ref _IQ23mpyRndSatTable
.def _IQ23rmpy
_IQ23rmpy:
IQNrmpy GLOBAL_Q, _IQ23mpyRndSatTable
.endif
.if GLOBAL_Q == 22
.ref _IQ22mpyRndSatTable
.def _IQ22rmpy
_IQ22rmpy:
IQNrmpy GLOBAL_Q, _IQ22mpyRndSatTable
.endif
.if GLOBAL_Q == 21
.ref _IQ21mpyRndSatTable
.def _IQ21rmpy
_IQ21rmpy:
IQNrmpy GLOBAL_Q, _IQ21mpyRndSatTable
.endif
.if GLOBAL_Q == 20
.ref _IQ20mpyRndSatTable
.def _IQ20rmpy
_IQ20rmpy:
IQNrmpy GLOBAL_Q, _IQ20mpyRndSatTable
.endif
.if GLOBAL_Q == 19
.ref _IQ19mpyRndSatTable
.def _IQ19rmpy
_IQ19rmpy:
IQNrmpy GLOBAL_Q, _IQ19mpyRndSatTable
.endif
.if GLOBAL_Q == 18
.ref _IQ18mpyRndSatTable
.def _IQ18rmpy
_IQ18rmpy:
IQNrmpy GLOBAL_Q, _IQ18mpyRndSatTable
.endif
.if GLOBAL_Q == 17
.ref _IQ17mpyRndSatTable
.def _IQ17rmpy
_IQ17rmpy:
IQNrmpy GLOBAL_Q, _IQ17mpyRndSatTable
.endif
.if GLOBAL_Q == 16
.ref _IQ16mpyRndSatTable
.def _IQ16rmpy
_IQ16rmpy:
IQNrmpy GLOBAL_Q, _IQ16mpyRndSatTable
.endif
.if GLOBAL_Q == 15
.ref _IQ15mpyRndSatTable
.def _IQ15rmpy
_IQ15rmpy:
IQNrmpy GLOBAL_Q, _IQ15mpyRndSatTable
.endif
.if GLOBAL_Q == 14
.ref _IQ14mpyRndSatTable
.def _IQ14rmpy
_IQ14rmpy:
IQNrmpy GLOBAL_Q, _IQ14mpyRndSatTable
.endif
.if GLOBAL_Q == 13
.ref _IQ13mpyRndSatTable
.def _IQ13rmpy
_IQ13rmpy:
IQNrmpy GLOBAL_Q, _IQ13mpyRndSatTable
.endif
.if GLOBAL_Q == 12
.ref _IQ12mpyRndSatTable
.def _IQ12rmpy
_IQ12rmpy:
IQNrmpy GLOBAL_Q, _IQ12mpyRndSatTable
.endif
.if GLOBAL_Q == 11
.ref _IQ11mpyRndSatTable
.def _IQ11rmpy
_IQ11rmpy:
IQNrmpy GLOBAL_Q, _IQ11mpyRndSatTable
.endif
.if GLOBAL_Q == 10
.ref _IQ10mpyRndSatTable
.def _IQ10rmpy
_IQ10rmpy:
IQNrmpy GLOBAL_Q, _IQ10mpyRndSatTable
.endif
.if GLOBAL_Q == 9
.ref _IQ9mpyRndSatTable
.def _IQ9rmpy
_IQ9rmpy:
IQNrmpy GLOBAL_Q, _IQ9mpyRndSatTable
.endif
.if GLOBAL_Q == 8
.ref _IQ8mpyRndSatTable
.def _IQ8rmpy
_IQ8rmpy:
IQNrmpy GLOBAL_Q, _IQ8mpyRndSatTable
.endif
.if GLOBAL_Q == 7
.ref _IQ7mpyRndSatTable
.def _IQ7rmpy
_IQ7rmpy:
IQNrmpy GLOBAL_Q, _IQ7mpyRndSatTable
.endif
.if GLOBAL_Q == 6
.ref _IQ6mpyRndSatTable
.def _IQ6rmpy
_IQ6rmpy:
IQNrmpy GLOBAL_Q, _IQ6mpyRndSatTable
.endif
.if GLOBAL_Q == 5
.ref _IQ5mpyRndSatTable
.def _IQ5rmpy
_IQ5rmpy:
IQNrmpy GLOBAL_Q, _IQ5mpyRndSatTable
.endif
.if GLOBAL_Q == 4
.ref _IQ4mpyRndSatTable
.def _IQ4rmpy
_IQ4rmpy:
IQNrmpy GLOBAL_Q, _IQ4mpyRndSatTable
.endif
.if GLOBAL_Q == 3
.ref _IQ3mpyRndSatTable
.def _IQ3rmpy
_IQ3rmpy:
IQNrmpy GLOBAL_Q, _IQ3mpyRndSatTable
.endif
.if GLOBAL_Q == 2
.ref _IQ2mpyRndSatTable
.def _IQ2rmpy
_IQ2rmpy:
IQNrmpy GLOBAL_Q, _IQ2mpyRndSatTable
.endif
.if GLOBAL_Q == 1
.ref _IQ1mpyRndSatTable
.def _IQ1rmpy
_IQ1rmpy:
IQNrmpy GLOBAL_Q, _IQ1mpyRndSatTable
.endif
;;###########################################################################
;; No More.
;;###########################################################################
@@ -0,0 +1,302 @@
;;###########################################################################
;;
;; FILE: IQNrsmpy.asm
;;
;; TITLE: C Callable IQ Multiply With Rounding & Saturation Math Function
;;
;;###########################################################################
;;
;; Ver | Date | Who | Description of changes
;; =====|=============|=======|==============================================
;; 1.2 | 11 Oct 2001 | A. T. | Original Release.
;; -----|-------------|-------|----------------------------------------------
;; 1.3 | 19 Nov 2001 | A. T. | No changes.
;; -----|-------------|-------|----------------------------------------------
;; 1.4 | 17 May 2002 | A. T. | Made correction to table pointer.
;; | | | Corrected benchmark cycles.
;; -----|-------------|-------|----------------------------------------------
;; | | |
;;
;;###########################################################################
;;===========================================================================
;; Function: _IQNrsmpy
;;===========================================================================
;;
;; C Usage: extern long _IQNrsmpy(long M, long X); // with sat & round
;;
;;---------------------------------------------------------------------------
;;
;; On Entry: ACC = M in IQ format
;; [SP-4] = X in IQ format
;;
;; Regs Used: XT, P
;;
;; On Exit: ACC = M * X in IQ format
;;
;; Q range: 30 to 1
;;
;;---------------------------------------------------------------------------
;; Algorithm: The IQmpy operation generates a 64-bit result. The 64-bit
;; number must then be scaled back to a 32-bit value with the
;; same Q value as the original inputs. For example: if Q = 27,
;; then the multiplication of two I5Q27 numbers results in the
;; following 64-bit value:
;;
;; I5Q27 * I5Q27 = I10Q54
;;
;; The value is then scaled back to an I5Q27 value as follows:
;;
;; I5Q27 = I10Q54 >> Q
;;
;; For some of the operations, the value is either rounded or
;; saturated (or both) to its maximum value before scaling.
;;
;;---------------------------------------------------------------------------
;; Benchmark:
;;
;; Cycles = 20 (Q >= 16, if Table in 0-wait SARAM, includes LCR/LRETR)
;; 21 (Q <= 15, if Table in 0-wait SARAM, includes LCR/LRETR)
;;
;; Cycles = 26 (Q >= 16, if Table in 1-wait ROM, includes LCR/LRETR)
;; 27 (Q <= 15, if Table in 1-wait ROM, includes LCR/LRETR)
;;
;;---------------------------------------------------------------------------
IQNrsmpy .macro q_value, table
MOVL XT,@ACC ; XT = M
IMPYL P,XT,*-SP[4]
QMPYL ACC,XT,*-SP[4] ; ACC:P = M * X
MOVL XAR7,#table
ADDUL P,*XAR7++ ; Round value
ADDCL ACC,*XAR7++
MINL ACC,*+XAR7[2] ; Saturate value
MINCUL P,*+XAR7[0]
MAXL ACC,*+XAR7[6]
MAXCUL P,*+XAR7[4]
.if q_value >= 16
LSL64 ACC:P,#(32 - q_value) ; Scale back to required IQ value
.endif
.if q_value <= 15
LSL64 ACC:P,#16 ; Scale back to required IQ value
LSL64 ACC:P,#(16 - q_value)
.endif
LRETR
.endm
;; The value "GLOBAL_Q" needs to be supplied by the assembler
;; using the "-dGLOBAL_Q=q_value" directive:
.sect "IQmath"
.if GLOBAL_Q == 30
.ref _IQ30mpyRndSatTable
.def _IQ30rsmpy
_IQ30rsmpy:
IQNrsmpy GLOBAL_Q, _IQ30mpyRndSatTable
.endif
.if GLOBAL_Q == 29
.ref _IQ29mpyRndSatTable
.def _IQ29rsmpy
_IQ29rsmpy:
IQNrsmpy GLOBAL_Q, _IQ29mpyRndSatTable
.endif
.if GLOBAL_Q == 28
.ref _IQ28mpyRndSatTable
.def _IQ28rsmpy
_IQ28rsmpy:
IQNrsmpy GLOBAL_Q, _IQ28mpyRndSatTable
.endif
.if GLOBAL_Q == 27
.ref _IQ27mpyRndSatTable
.def _IQ27rsmpy
_IQ27rsmpy:
IQNrsmpy GLOBAL_Q, _IQ27mpyRndSatTable
.endif
.if GLOBAL_Q == 26
.ref _IQ26mpyRndSatTable
.def _IQ26rsmpy
_IQ26rsmpy:
IQNrsmpy GLOBAL_Q, _IQ26mpyRndSatTable
.endif
.if GLOBAL_Q == 25
.ref _IQ25mpyRndSatTable
.def _IQ25rsmpy
_IQ25rsmpy:
IQNrsmpy GLOBAL_Q, _IQ25mpyRndSatTable
.endif
.if GLOBAL_Q == 24
.ref _IQ24mpyRndSatTable
.def _IQ24rsmpy
_IQ24rsmpy:
IQNrsmpy GLOBAL_Q, _IQ24mpyRndSatTable
.endif
.if GLOBAL_Q == 23
.ref _IQ23mpyRndSatTable
.def _IQ23rsmpy
_IQ23rsmpy:
IQNrsmpy GLOBAL_Q, _IQ23mpyRndSatTable
.endif
.if GLOBAL_Q == 22
.ref _IQ22mpyRndSatTable
.def _IQ22rsmpy
_IQ22rsmpy:
IQNrsmpy GLOBAL_Q, _IQ22mpyRndSatTable
.endif
.if GLOBAL_Q == 21
.ref _IQ21mpyRndSatTable
.def _IQ21rsmpy
_IQ21rsmpy:
IQNrsmpy GLOBAL_Q, _IQ21mpyRndSatTable
.endif
.if GLOBAL_Q == 20
.ref _IQ20mpyRndSatTable
.def _IQ20rsmpy
_IQ20rsmpy:
IQNrsmpy GLOBAL_Q, _IQ20mpyRndSatTable
.endif
.if GLOBAL_Q == 19
.ref _IQ19mpyRndSatTable
.def _IQ19rsmpy
_IQ19rsmpy:
IQNrsmpy GLOBAL_Q, _IQ19mpyRndSatTable
.endif
.if GLOBAL_Q == 18
.ref _IQ18mpyRndSatTable
.def _IQ18rsmpy
_IQ18rsmpy:
IQNrsmpy GLOBAL_Q, _IQ18mpyRndSatTable
.endif
.if GLOBAL_Q == 17
.ref _IQ17mpyRndSatTable
.def _IQ17rsmpy
_IQ17rsmpy:
IQNrsmpy GLOBAL_Q, _IQ17mpyRndSatTable
.endif
.if GLOBAL_Q == 16
.ref _IQ16mpyRndSatTable
.def _IQ16rsmpy
_IQ16rsmpy:
IQNrsmpy GLOBAL_Q, _IQ16mpyRndSatTable
.endif
.if GLOBAL_Q == 15
.ref _IQ15mpyRndSatTable
.def _IQ15rsmpy
_IQ15rsmpy:
IQNrsmpy GLOBAL_Q, _IQ15mpyRndSatTable
.endif
.if GLOBAL_Q == 14
.ref _IQ14mpyRndSatTable
.def _IQ14rsmpy
_IQ14rsmpy:
IQNrsmpy GLOBAL_Q, _IQ14mpyRndSatTable
.endif
.if GLOBAL_Q == 13
.ref _IQ13mpyRndSatTable
.def _IQ13rsmpy
_IQ13rsmpy:
IQNrsmpy GLOBAL_Q, _IQ13mpyRndSatTable
.endif
.if GLOBAL_Q == 12
.ref _IQ12mpyRndSatTable
.def _IQ12rsmpy
_IQ12rsmpy:
IQNrsmpy GLOBAL_Q, _IQ12mpyRndSatTable
.endif
.if GLOBAL_Q == 11
.ref _IQ11mpyRndSatTable
.def _IQ11rsmpy
_IQ11rsmpy:
IQNrsmpy GLOBAL_Q, _IQ11mpyRndSatTable
.endif
.if GLOBAL_Q == 10
.ref _IQ10mpyRndSatTable
.def _IQ10rsmpy
_IQ10rsmpy:
IQNrsmpy GLOBAL_Q, _IQ10mpyRndSatTable
.endif
.if GLOBAL_Q == 9
.ref _IQ9mpyRndSatTable
.def _IQ9rsmpy
_IQ9rsmpy:
IQNrsmpy GLOBAL_Q, _IQ9mpyRndSatTable
.endif
.if GLOBAL_Q == 8
.ref _IQ8mpyRndSatTable
.def _IQ8rsmpy
_IQ8rsmpy:
IQNrsmpy GLOBAL_Q, _IQ8mpyRndSatTable
.endif
.if GLOBAL_Q == 7
.ref _IQ7mpyRndSatTable
.def _IQ7rsmpy
_IQ7rsmpy:
IQNrsmpy GLOBAL_Q, _IQ7mpyRndSatTable
.endif
.if GLOBAL_Q == 6
.ref _IQ6mpyRndSatTable
.def _IQ6rsmpy
_IQ6rsmpy:
IQNrsmpy GLOBAL_Q, _IQ6mpyRndSatTable
.endif
.if GLOBAL_Q == 5
.ref _IQ5mpyRndSatTable
.def _IQ5rsmpy
_IQ5rsmpy:
IQNrsmpy GLOBAL_Q, _IQ5mpyRndSatTable
.endif
.if GLOBAL_Q == 4
.ref _IQ4mpyRndSatTable
.def _IQ4rsmpy
_IQ4rsmpy:
IQNrsmpy GLOBAL_Q, _IQ4mpyRndSatTable
.endif
.if GLOBAL_Q == 3
.ref _IQ3mpyRndSatTable
.def _IQ3rsmpy
_IQ3rsmpy:
IQNrsmpy GLOBAL_Q, _IQ3mpyRndSatTable
.endif
.if GLOBAL_Q == 2
.ref _IQ2mpyRndSatTable
.def _IQ2rsmpy
_IQ2rsmpy:
IQNrsmpy GLOBAL_Q, _IQ2mpyRndSatTable
.endif
.if GLOBAL_Q == 1
.ref _IQ1mpyRndSatTable
.def _IQ1rsmpy
_IQ1rsmpy:
IQNrsmpy GLOBAL_Q, _IQ1mpyRndSatTable
.endif
;;###########################################################################
;; No More.
;;###########################################################################
@@ -0,0 +1,334 @@
;;###########################################################################
;;
;; FILE: IQNsin.asm
;;
;; TITLE: C Callable IQ Sin Math Function
;;
;;###########################################################################
;;
;; Ver | Date | Who | Description of changes
;; =====|=============|=======|==============================================
;; 1.2 | 11 Oct 2001 | A. T. | Original Release.
;; -----|-------------|-------|----------------------------------------------
;; 1.3 | 19 Nov 2001 | A. T. | No changes.
;; -----|-------------|-------|----------------------------------------------
;; 1.4 | 17 May 2002 | A. T. | Improved accuracy from 16 to about 29 bits
;; | | | (thanks to Settu for writing original code).
;; | | | Modified code so that calculation constants
;; | | | are embedded in code and not in table.
;; -----|-------------|-------|----------------------------------------------
;; | | |
;;
;;###########################################################################
;;===========================================================================
;; Function: _IQNsin
;;===========================================================================
;;
;; C Usage: extern long _IQNsin(long Rad);
;;
;;---------------------------------------------------------------------------
;;
;; On Entry: ACC = Radian in IQ format
;;
;; Regs Used: XAR7, XAR6, XAR5, XAR4, XAR0
;; XT, P
;;
;; On Exit: ACC = sin(Radian) result in IQ format
;;
;; Q range: 30 to 1
;;
;;---------------------------------------------------------------------------
;; Algorithm: The "sin" value is calculated as follows:
;;
;; 1) The offset into a 512 word sin/cos table is calculated:
;;
;; k = 0x1FF & (int(Radian*512/(2*pi)))
;;
;;
;; 2) The fractional component between table samples is
;; calculated:
;;
;; x = fract(Radian*512/2*pi) * (2*pi)/512
;;
;; 3) The output sine value is calculated as follows:
;;
;; sin(Radian) = S(k) + x*(C(k) + x*(-0.5*S(k) - 0.166*x*C(k)))
;;
;; where S(k) = Sin table value at offset "k"
;; C(k) = Cos table value at offset "k"
;;
;; Using the above method, with a 512x32 full wave sin/cos table,
;; will give an accuracy of approximately 29 bits.
;;
;;---------------------------------------------------------------------------
;; Benchmark:
;;
;; Assumptions: * Code executes from 0-wait SARAM block.
;; * _IQsin/cos Table located in seperate SARAM or ROM block.
;;
;; Cycles = 45 ( Q == 30, tables in 0-wait SARAM, includes LCR/LRETR)
;; = 46 (29 >= Q >= 25, tables in 0-wait SARAM, includes LCR/LRETR)
;; = 45 (24 >= Q >= 14, tables in 0-wait SARAM, includes LCR/LRETR)
;; = 46 (13 >= Q >= 0, tables in 0-wait SARAM, includes LCR/LRETR)
;;
;; Cycles = 47 ( Q == 30, tables in 1-wait ROM, includes LCR/LRETR)
;; = 48 (29 >= Q >= 25, tables in 1-wait ROM, includes LCR/LRETR)
;; = 47 (24 >= Q >= 14, tables in 1-wait ROM, includes LCR/LRETR)
;; = 48 (13 >= Q >= 0, tables in 1-wait ROM, includes LCR/LRETR)
;;
;;===========================================================================
K1 .set 02AAAh ; 1/6 in Q16
IQNsin .macro q_value
MOV *SP++,#0x3F6B ; (2*pi)/512 = 0x03243F6B (Q32)
MOV *SP++,#0x0324
MOV *SP++,#0xC1B7 ; 512/(2*pi) = 0x517CC1B7 (I8Q24)
MOV *SP++,#0x517C
MOVL XAR6,#_IQcosTable
MOVL XAR7,#_IQsinTable
CLRC TC
SETC OVM ; turn overflow mode on
ABSTC ACC ; abs(Rad), TC = sign
MOVL XT,*--SP ; XT = 512/(2*pi) (I8Q24)
IMPYL P,XT,@ACC
QMPYL ACC,XT,@ACC ; ACC:P = Rad * 512 / (2pi)
.if q_value >= 25
ASR64 ACC:P,#16
ASR64 ACC:P,#(q_value - 24) ; ACC = Integer, P = fract
.endif
.if (q_value <= 24) & (q_value >= 9)
ASR64 ACC:P,#(q_value - 8) ; ACC = Integer, P = fract
.endif
.if q_value <= 7
LSL64 ACC:P,#(8 - q_value) ; ACC = Integer, P = fract
.endif
AND @AL,#0x1FF
LSL AL,#1
MOVZ AR0,@AL ; AR0 = Index into "sin/cos" table = k
MOVL XT,*--SP ; XT = (2*pi)/512 (Q32)
QMPYUL P,XT,@P ; P = x (Q32)
MOVB ACC,#0
MOVL XT,@P ; XT = x (Q32)
MOVL XAR4,*+XAR6[AR0] ; XAR4 = C(k) (Q30)
MOVL XAR5,*+XAR7[AR0] ; XAR5 = S(k) (Q30)
SUBL ACC,@XAR5 ; ACC = -S(k) (Q30)
ASR64 ACC:P,#1 ; ACC = -0.5*S(k) (Q30)
QMPYL P,XT,@XAR4 ; P = x*C(k) (Q30)
CLRC OVM ; Turn overflow mode off
MPY P,@PH,#K1 ; P = 0.166*x*C(k) (Q30)
SUBL ACC,@P ; ACC = -0.5*S(k) - 0.166*x*C(k) (Q30)
QMPYL ACC,XT,@ACC ; ACC = x*(-0.5*S(k) - 0.166*x*C(k)) (Q30)
ADDL ACC,@XAR4 ; ACC = C(k) + x*(-0.5*S(k) - 0.166*x*C(k)) (Q30)
QMPYL ACC,XT,@ACC ; ACC = x*(C(k) + x*(-0.5*S(k) - 0.166*x*C(k))) (Q30)
ADDL ACC,@XAR5 ; ACC = S(k) + x*(C(k) + x*(-0.5*S(k) - 0.166*x*C(k))) (Q30)
NEGTC ACC ; Negate result if necessary
.if (q_value <= 29) & (q_value >= 14)
ASR64 ACC:P,#(30 - q_value) ; Scale back to required IQ value
.endif
.if q_value <= 13
ASR64 ACC:P,#16
ASR64 ACC:P,#(14 - q_value) ; Scale back to required IQ value
.endif
LRETR
.endm
;; The value "GLOBAL_Q" needs to be supplied by the assembler
;; using the "-dGLOBAL_Q=q_value" directive:
.sect "IQmath"
.ref _IQsinTable
.ref _IQcosTable
.if GLOBAL_Q == 30
.def _IQ30sin
_IQ30sin:
IQNsin GLOBAL_Q
.endif
.if GLOBAL_Q == 29
.def _IQ29sin
_IQ29sin:
IQNsin GLOBAL_Q
.endif
.if GLOBAL_Q == 28
.def _IQ28sin
_IQ28sin:
IQNsin GLOBAL_Q
.endif
.if GLOBAL_Q == 27
.def _IQ27sin
_IQ27sin:
IQNsin GLOBAL_Q
.endif
.if GLOBAL_Q == 26
.def _IQ26sin
_IQ26sin:
IQNsin GLOBAL_Q
.endif
.if GLOBAL_Q == 25
.def _IQ25sin
_IQ25sin:
IQNsin GLOBAL_Q
.endif
.if GLOBAL_Q == 24
.def _IQ24sin
_IQ24sin:
IQNsin GLOBAL_Q
.endif
.if GLOBAL_Q == 23
.def _IQ23sin
_IQ23sin:
IQNsin GLOBAL_Q
.endif
.if GLOBAL_Q == 22
.def _IQ22sin
_IQ22sin:
IQNsin GLOBAL_Q
.endif
.if GLOBAL_Q == 21
.def _IQ21sin
_IQ21sin:
IQNsin GLOBAL_Q
.endif
.if GLOBAL_Q == 20
.def _IQ20sin
_IQ20sin:
IQNsin GLOBAL_Q
.endif
.if GLOBAL_Q == 19
.def _IQ19sin
_IQ19sin:
IQNsin GLOBAL_Q
.endif
.if GLOBAL_Q == 18
.def _IQ18sin
_IQ18sin:
IQNsin GLOBAL_Q
.endif
.if GLOBAL_Q == 17
.def _IQ17sin
_IQ17sin:
IQNsin GLOBAL_Q
.endif
.if GLOBAL_Q == 16
.def _IQ16sin
_IQ16sin:
IQNsin GLOBAL_Q
.endif
.if GLOBAL_Q == 15
.def _IQ15sin
_IQ15sin:
IQNsin GLOBAL_Q
.endif
.if GLOBAL_Q == 14
.def _IQ14sin
_IQ14sin:
IQNsin GLOBAL_Q
.endif
.if GLOBAL_Q == 13
.def _IQ13sin
_IQ13sin:
IQNsin GLOBAL_Q
.endif
.if GLOBAL_Q == 12
.def _IQ12sin
_IQ12sin:
IQNsin GLOBAL_Q
.endif
.if GLOBAL_Q == 11
.def _IQ11sin
_IQ11sin:
IQNsin GLOBAL_Q
.endif
.if GLOBAL_Q == 10
.def _IQ10sin
_IQ10sin:
IQNsin GLOBAL_Q
.endif
.if GLOBAL_Q == 9
.def _IQ9sin
_IQ9sin:
IQNsin GLOBAL_Q
.endif
.if GLOBAL_Q == 8
.def _IQ8sin
_IQ8sin:
IQNsin GLOBAL_Q
.endif
.if GLOBAL_Q == 7
.def _IQ7sin
_IQ7sin:
IQNsin GLOBAL_Q
.endif
.if GLOBAL_Q == 6
.def _IQ6sin
_IQ6sin:
IQNsin GLOBAL_Q
.endif
.if GLOBAL_Q == 5
.def _IQ5sin
_IQ5sin:
IQNsin GLOBAL_Q
.endif
.if GLOBAL_Q == 4
.def _IQ4sin
_IQ4sin:
IQNsin GLOBAL_Q
.endif
.if GLOBAL_Q == 3
.def _IQ3sin
_IQ3sin:
IQNsin GLOBAL_Q
.endif
.if GLOBAL_Q == 2
.def _IQ2sin
_IQ2sin:
IQNsin GLOBAL_Q
.endif
.if GLOBAL_Q == 1
.def _IQ1sin
_IQ1sin:
IQNsin GLOBAL_Q
.endif
;;###########################################################################
;; No More.
;;###########################################################################
@@ -0,0 +1,322 @@
;;###########################################################################
;;
;; FILE: IQNsinPU.asm
;;
;; TITLE: C Callable IQ Sin Per Unit Math Function
;;
;;###########################################################################
;;
;; Ver | Date | Who | Description of changes
;; =====|=============|=======|==============================================
;; 1.4 | 17 May 2002 | A. T. | Original Release.
;; -----|-------------|-------|----------------------------------------------
;; | | |
;;
;;###########################################################################
;;===========================================================================
;; Function: _IQNsinPU
;;===========================================================================
;;
;; C Usage: extern long _IQNsinPU(long PU);
;;
;;---------------------------------------------------------------------------
;;
;; On Entry: ACC = Per Unit in IQ format
;;
;; Regs Used: XAR7, XAR6, XAR5, XAR4, XAR0
;; XT, P
;;
;; On Exit: ACC = sin(PU) result in IQ format
;;
;; Q range: 30 to 1
;;
;;---------------------------------------------------------------------------
;; Algorithm: The "sinPU" value is calculated as follows:
;;
;; 1) The offset into a 512 word sin/cos table is calculated:
;;
;; k = 0x1FF & (int(PU*512))
;;
;;
;; 2) The fractional component between table samples is
;; calculated:
;;
;; x = fract(PU*512) * (2*pi)/512
;;
;; 3) The output sine value is calculated as follows:
;;
;; sin(PU) = S(k) + x*(C(k) + x*(-0.5*S(k) - 0.166*x*C(k)))
;;
;; where S(k) = Sin table value at offset "k"
;; C(k) = Cos table value at offset "k"
;;
;; Using the above method, with a 512x32 full wave sin/cos table,
;; will give an accuracy of approximately 29 bits.
;;
;;---------------------------------------------------------------------------
;; Benchmark:
;;
;; Assumptions: * Code executes from 0-wait SARAM block.
;; * _IQsin/cos Table located in seperate SARAM or ROM block.
;;
;; Cycles = 39 ( Q == 30, tables in 0-wait SARAM, includes LCR/LRETR)
;; = 40 (29 >= Q >= 26, tables in 0-wait SARAM, includes LCR/LRETR)
;; = 39 (25 >= Q >= 14, tables in 0-wait SARAM, includes LCR/LRETR)
;; = 40 (13 >= Q >= 0, tables in 0-wait SARAM, includes LCR/LRETR)
;;
;; Cycles = 41 ( Q == 30, tables in 1-wait ROM, includes LCR/LRETR)
;; = 42 (29 >= Q >= 26, tables in 1-wait ROM, includes LCR/LRETR)
;; = 41 (25 >= Q >= 14, tables in 1-wait ROM, includes LCR/LRETR)
;; = 42 (13 >= Q >= 0, tables in 1-wait ROM, includes LCR/LRETR)
;;
;;===========================================================================
K1 .set 02AAAh ; 1/6 in Q16
IQNsinPU .macro q_value
MOV *SP++,#0x3F6B ; (2*pi)/512 = 0x03243F6B (Q32)
MOV *SP++,#0x0324
MOVL XAR6,#_IQcosTable
MOVL XAR7,#_IQsinTable
CLRC TC
SETC OVM ; turn overflow mode on
ABSTC ACC ; abs(Rad), TC = sign
MPYB P,T,#0
.if q_value >= 26
ASR64 ACC:P,#16
ASR64 ACC:P,#(q_value - 25) ; ACC = Integer, P = fract
.endif
.if (q_value <= 25) & (q_value >= 10)
ASR64 ACC:P,#(q_value - 9) ; ACC = Integer, P = fract
.endif
.if q_value <= 8
LSL64 ACC:P,#(9 - q_value) ; ACC = Integer, P = fract
.endif
AND @AL,#0x1FF
LSL AL,#1
MOVZ AR0,@AL ; AR0 = Index into "sin/cos" table = k
MOVL XT,*--SP ; XT = (2*pi)/512 (Q32)
QMPYUL P,XT,@P ; P = x (Q32)
MOVB ACC,#0
MOVL XT,@P ; XT = x (Q32)
MOVL XAR4,*+XAR6[AR0] ; XAR4 = C(k) (Q30)
MOVL XAR5,*+XAR7[AR0] ; XAR5 = S(k) (Q30)
SUBL ACC,@XAR5 ; ACC = -S(k) (Q30)
ASR64 ACC:P,#1 ; ACC = -0.5*S(k) (Q30)
QMPYL P,XT,@XAR4 ; P = x*C(k) (Q30)
CLRC OVM ; Turn overflow mode off
MPY P,@PH,#K1 ; P = 0.166*x*C(k) (Q30)
SUBL ACC,@P ; ACC = -0.5*S(k) - 0.166*x*C(k) (Q30)
QMPYL ACC,XT,@ACC ; ACC = x*(-0.5*S(k) - 0.166*x*C(k)) (Q30)
ADDL ACC,@XAR4 ; ACC = C(k) + x*(-0.5*S(k) - 0.166*x*C(k)) (Q30)
QMPYL ACC,XT,@ACC ; ACC = x*(C(k) + x*(-0.5*S(k) - 0.166*x*C(k))) (Q30)
ADDL ACC,@XAR5 ; ACC = S(k) + x*(C(k) + x*(-0.5*S(k) - 0.166*x*C(k))) (Q30)
NEGTC ACC ; Negate result if necessary
.if (q_value <= 29) & (q_value >= 14)
ASR64 ACC:P,#(30 - q_value) ; Scale back to required IQ value
.endif
.if q_value <= 13
ASR64 ACC:P,#16
ASR64 ACC:P,#(14 - q_value) ; Scale back to required IQ value
.endif
LRETR
.endm
;; The value "GLOBAL_Q" needs to be supplied by the assembler
;; using the "-dGLOBAL_Q=q_value" directive:
.sect "IQmath"
.ref _IQsinTable
.ref _IQcosTable
.if GLOBAL_Q == 30
.def _IQ30sinPU
_IQ30sinPU:
IQNsinPU GLOBAL_Q
.endif
.if GLOBAL_Q == 29
.def _IQ29sinPU
_IQ29sinPU:
IQNsinPU GLOBAL_Q
.endif
.if GLOBAL_Q == 28
.def _IQ28sinPU
_IQ28sinPU:
IQNsinPU GLOBAL_Q
.endif
.if GLOBAL_Q == 27
.def _IQ27sinPU
_IQ27sinPU:
IQNsinPU GLOBAL_Q
.endif
.if GLOBAL_Q == 26
.def _IQ26sinPU
_IQ26sinPU:
IQNsinPU GLOBAL_Q
.endif
.if GLOBAL_Q == 25
.def _IQ25sinPU
_IQ25sinPU:
IQNsinPU GLOBAL_Q
.endif
.if GLOBAL_Q == 24
.def _IQ24sinPU
_IQ24sinPU:
IQNsinPU GLOBAL_Q
.endif
.if GLOBAL_Q == 23
.def _IQ23sinPU
_IQ23sinPU:
IQNsinPU GLOBAL_Q
.endif
.if GLOBAL_Q == 22
.def _IQ22sinPU
_IQ22sinPU:
IQNsinPU GLOBAL_Q
.endif
.if GLOBAL_Q == 21
.def _IQ21sinPU
_IQ21sinPU:
IQNsinPU GLOBAL_Q
.endif
.if GLOBAL_Q == 20
.def _IQ20sinPU
_IQ20sinPU:
IQNsinPU GLOBAL_Q
.endif
.if GLOBAL_Q == 19
.def _IQ19sinPU
_IQ19sinPU:
IQNsinPU GLOBAL_Q
.endif
.if GLOBAL_Q == 18
.def _IQ18sinPU
_IQ18sinPU:
IQNsinPU GLOBAL_Q
.endif
.if GLOBAL_Q == 17
.def _IQ17sinPU
_IQ17sinPU:
IQNsinPU GLOBAL_Q
.endif
.if GLOBAL_Q == 16
.def _IQ16sinPU
_IQ16sinPU:
IQNsinPU GLOBAL_Q
.endif
.if GLOBAL_Q == 15
.def _IQ15sinPU
_IQ15sinPU:
IQNsinPU GLOBAL_Q
.endif
.if GLOBAL_Q == 14
.def _IQ14sinPU
_IQ14sinPU:
IQNsinPU GLOBAL_Q
.endif
.if GLOBAL_Q == 13
.def _IQ13sinPU
_IQ13sinPU:
IQNsinPU GLOBAL_Q
.endif
.if GLOBAL_Q == 12
.def _IQ12sinPU
_IQ12sinPU:
IQNsinPU GLOBAL_Q
.endif
.if GLOBAL_Q == 11
.def _IQ11sinPU
_IQ11sinPU:
IQNsinPU GLOBAL_Q
.endif
.if GLOBAL_Q == 10
.def _IQ10sinPU
_IQ10sinPU:
IQNsinPU GLOBAL_Q
.endif
.if GLOBAL_Q == 9
.def _IQ9sinPU
_IQ9sinPU:
IQNsinPU GLOBAL_Q
.endif
.if GLOBAL_Q == 8
.def _IQ8sinPU
_IQ8sinPU:
IQNsinPU GLOBAL_Q
.endif
.if GLOBAL_Q == 7
.def _IQ7sinPU
_IQ7sinPU:
IQNsinPU GLOBAL_Q
.endif
.if GLOBAL_Q == 6
.def _IQ6sinPU
_IQ6sinPU:
IQNsinPU GLOBAL_Q
.endif
.if GLOBAL_Q == 5
.def _IQ5sinPU
_IQ5sinPU:
IQNsinPU GLOBAL_Q
.endif
.if GLOBAL_Q == 4
.def _IQ4sinPU
_IQ4sinPU:
IQNsinPU GLOBAL_Q
.endif
.if GLOBAL_Q == 3
.def _IQ3sinPU
_IQ3sinPU:
IQNsinPU GLOBAL_Q
.endif
.if GLOBAL_Q == 2
.def _IQ2sinPU
_IQ2sinPU:
IQNsinPU GLOBAL_Q
.endif
.if GLOBAL_Q == 1
.def _IQ1sinPU
_IQ1sinPU:
IQNsinPU GLOBAL_Q
.endif
;;###########################################################################
;; No More.
;;###########################################################################
@@ -0,0 +1,350 @@
;;###########################################################################
;;
;; FILE: IQNsqrt.asm
;;
;; TITLE: C Callable IQ Square Root Math Function
;;
;;###########################################################################
;;
;; Ver | Date | Who | Description of changes
;; =====|=============|=======|==============================================
;; 1.2 | 11 Oct 2001 | A. T. | Original Release.
;; -----|-------------|-------|----------------------------------------------
;; 1.3 | 19 Nov 2001 | A. T. | Improved accuracy.
;; -----|-------------|-------|----------------------------------------------
;; 1.4 | 17 May 2002 | A. T. | Corrected benchmark cycles.
;; -----|-------------|-------|----------------------------------------------
;; | | |
;;
;;###########################################################################
;;===========================================================================
;; Function: _IQNsqrt
;;===========================================================================
;;
;; C Usage: extern long _IQNsqrt(long X); // with rounding
;;
;;---------------------------------------------------------------------------
;;
;; On Entry: ACC = X in IQ format
;;
;; Regs Used: XAR7, XAR6, XAR5, XAR4
;; XT, P, XAR0
;;
;; On Exit: ACC = sqrt(X) result in IQ format
;; ACC = 0 if input is -ve or 0
;;
;; Note: The square root of any number will never
;; overflow, hence saturation is not required.
;;
;; Q range: 30 to 1
;;
;;---------------------------------------------------------------------------
;; Algorithm: The procedure for calculating "Y = sqrt(X)" is similar
;; to calculating the inverse sqare root due to the
;; following relationship:
;;
;; sqrt(X) = X * 1/sqrt(X)
;;
;; The procedure is as follows:
;;
;; Step 1) Normalize Input:
;;
;; V = X * 2^n
;;
;; Step 2) Obtain initial estimate from "_IQsqrtTable"
;; using the upper 8-bits of the normalized V value.
;;
;; Step 3) Use Newton-Raphson algorithm to improve accuracy.
;; Repeat following equation two times. First iteration
;; gives 16-bit accuracy. Second iteration gives 32-bit
;; accuracy:
;;
;; Yn = Yn(1.5 - Yn*Yn*V/2)
;;
;; Yn = 1/sqrt(V)
;;
;; Step 4) Caculate the square root of the normalized number:
;;
;; Yn = V * Yn
;;
;; Step 5) Denormalize result and round:
;;
;; Y = Yn * 2^n / sqrt(2^n)
;;
;;---------------------------------------------------------------------------
;; Benchmark:
;;
;; Assumptions:
;; * Code executes from 0-wait SARAM block.
;; * _IQsqrt Tables located in seperate 0-wait SARAM or 1-wait ROM block.
;; * Stack located in seperate SARAM block.
;; * Includes LCR/LRETR.
;;
;; Cycles = 65 (30 >= Q >= 24 if _IQsqrt Tables in 0-wait SARAM)
;; 64 (23 >= Q >= 22 if _IQsqrt Tables in 0-wait SARAM)
;; 65 (21 >= Q >= 0 if _IQsqrt Tables in 0-wait SARAM)
;;
;; Cycles = 69 (30 >= Q >= 24 if _IQsqrt Tables in 1-wait ROM)
;; 68 (23 >= Q >= 22 if _IQsqrt Tables in 1-wait ROM)
;; 69 (21 >= Q >= 0 if _IQsqrt Tables in 1-wait ROM)
;;
;;===========================================================================
IQNsqrt .macro q_value
CSB ACC ; Count sign bits, T = n
LSLL ACC,T ; ACC = V/2 (Q32)
MOVL XAR6,@ACC ; XAR6 = V/2 (Q32)
ASR AH,#6
MOVB @AH,#0xFE,LEQ ; AR0 -> zero value if -ve or 0 input
SUB @AH,#254 ; Scale to table offset
MOVZ AR0,@AH ; AR0 -> index into _IQisqrtTable for
; initial estimate of Yn
TBIT @T,#0 ; TC = odd/even shift indicator
MOV AH,@T ; AH = n
LSR AH,#1 ; AH = n/2
MOVL *SP++,ACC ; SP-2 = n/2
MOVL XAR7,#_IQsqrtTable ; XAR7 = pointer to _IQisqrtTable
MOVL XAR4,*+XAR7[AR0] ; (1) XAR4 = initial Yn estimate (Q30)
MOVL XAR7,#_IQsqrtRoundSatTable
MOVL XAR5,*XAR7++ ; XAR5 = 1.5 (Q30)
.if (q_value & 0x0001) == 0 ; Q even
MOVB @AR0,#12,NTC ; AR0 -> 1/sqrt(2) if n == even (Q30)
MOVB @AR0,#10,TC ; AR0 -> 0.5 if n == odd (Q30)
.endif
.if (q_value & 0x0001) == 1 ; Q odd
MOVB @AR0,#12,TC ; AR0 -> 1/sqrt(2) if n == odd (Q30)
MOVB @AR0,#8,NTC ; AR0 -> 1.0 if n == even (Q30)
.endif
MOVL XT,@XAR4 ;(2) XT = Yn (Q30)
QMPYL ACC,XT,@XT ;(3) ACC = Yn*Yn (Q28)
MOVL XT,@XAR6 ;(4) XT = V/2 (Q32)
LSL ACC,#2 ;(5) ACC = Yn*Yn (Q30)
QMPYL ACC,XT,@ACC ;(6) ACC = Yn*Yn*V/2 (Q30)
MOVL XT,@XAR5 ;(7) XT = 1.5 (Q30)
SUBL @XT,ACC ;(8) XT = 1.5 - Yn*Yn*V/2 (Q30)
QMPYL ACC,XT,@XAR4 ;(9) ACC = Yn(1.5 - Yn*Yn*V/2) (Q28)
LSL ACC,#2 ;(10) ACC = Yn(1.5 - Yn*Yn*V/2) (Q30)
MOVL XAR4,@ACC ;(1) XAR4 = Yn (Q30)
MOVL XT,@XAR4 ;(2) XT = Yn (Q30)
QMPYL ACC,XT,@XT ;(3) ACC = Yn*Yn (Q28)
MOVL XT,@XAR6 ;(4) XT = V/2 (Q32)
LSL ACC,#2 ;(5) ACC = Yn*Yn (Q30)
QMPYL ACC,XT,@ACC ;(6) ACC = Yn*Yn*V/2 (Q30)
MOVL XT,@XAR5 ;(7) XT = 1.5 (Q30)
SUBL @XT,ACC ;(8) XT = 1.5 - Yn*Yn*V/2 (Q30)
QMPYL ACC,XT,@XAR4 ;(9) ACC = Yn(1.5 - Yn*Yn*V/2) (Q28)
LSL ACC,#2 ;(10) ACC = Yn(1.5 - Yn*Yn*V/2) (Q30)
MOVL XT,@XAR6
QMPYL ACC,XT,@ACC ; sqrt(X) = X * 1/sqrt(X)
MOVL XT,*+XAR7[AR0] ; Scale with 1.0 or 1/sqrt(X) or 0.5
IMPYL P,XT,@ACC
QMPYL ACC,XT,@ACC
.if q_value >= 24 ; Scale final result based on Q value
LSL64 ACC:P,#((q_value - 22)/2)
.endif
.if q_value <= 21
ASR64 ACC:P,#((23 - q_value)/2)
.endif
MOVL XT,*--SP ; T = n/2
ASR64 ACC:P,T ; Scale final result based on n/2
ADD @PH,#-32768 ; Round final result
ADDCL ACC,*+XAR7[2]
LRETR
.endm
;; The value "GLOBAL_Q" needs to be supplied by the assembler
;; using the "-dGLOBAL_Q=q_value" directive:
.sect "IQmath"
.ref _IQsqrtTable
.ref _IQsqrtRoundSatTable
.if GLOBAL_Q == 30
.def _IQ30sqrt
_IQ30sqrt:
IQNsqrt GLOBAL_Q
.endif
.if GLOBAL_Q == 29
.def _IQ29sqrt
_IQ29sqrt:
IQNsqrt GLOBAL_Q
.endif
.if GLOBAL_Q == 28
.def _IQ28sqrt
_IQ28sqrt:
IQNsqrt GLOBAL_Q
.endif
.if GLOBAL_Q == 27
.def _IQ27sqrt
_IQ27sqrt:
IQNsqrt GLOBAL_Q
.endif
.if GLOBAL_Q == 26
.def _IQ26sqrt
_IQ26sqrt:
IQNsqrt GLOBAL_Q
.endif
.if GLOBAL_Q == 25
.def _IQ25sqrt
_IQ25sqrt:
IQNsqrt GLOBAL_Q
.endif
.if GLOBAL_Q == 24
.def _IQ24sqrt
_IQ24sqrt:
IQNsqrt GLOBAL_Q
.endif
.if GLOBAL_Q == 23
.def _IQ23sqrt
_IQ23sqrt:
IQNsqrt GLOBAL_Q
.endif
.if GLOBAL_Q == 22
.def _IQ22sqrt
_IQ22sqrt:
IQNsqrt GLOBAL_Q
.endif
.if GLOBAL_Q == 21
.def _IQ21sqrt
_IQ21sqrt:
IQNsqrt GLOBAL_Q
.endif
.if GLOBAL_Q == 20
.def _IQ20sqrt
_IQ20sqrt:
IQNsqrt GLOBAL_Q
.endif
.if GLOBAL_Q == 19
.def _IQ19sqrt
_IQ19sqrt:
IQNsqrt GLOBAL_Q
.endif
.if GLOBAL_Q == 18
.def _IQ18sqrt
_IQ18sqrt:
IQNsqrt GLOBAL_Q
.endif
.if GLOBAL_Q == 17
.def _IQ17sqrt
_IQ17sqrt:
IQNsqrt GLOBAL_Q
.endif
.if GLOBAL_Q == 16
.def _IQ16sqrt
_IQ16sqrt:
IQNsqrt GLOBAL_Q
.endif
.if GLOBAL_Q == 15
.def _IQ15sqrt
_IQ15sqrt:
IQNsqrt GLOBAL_Q
.endif
.if GLOBAL_Q == 14
.def _IQ14sqrt
_IQ14sqrt:
IQNsqrt GLOBAL_Q
.endif
.if GLOBAL_Q == 13
.def _IQ13sqrt
_IQ13sqrt:
IQNsqrt GLOBAL_Q
.endif
.if GLOBAL_Q == 12
.def _IQ12sqrt
_IQ12sqrt:
IQNsqrt GLOBAL_Q
.endif
.if GLOBAL_Q == 11
.def _IQ11sqrt
_IQ11sqrt:
IQNsqrt GLOBAL_Q
.endif
.if GLOBAL_Q == 10
.def _IQ10sqrt
_IQ10sqrt:
IQNsqrt GLOBAL_Q
.endif
.if GLOBAL_Q == 9
.def _IQ9sqrt
_IQ9sqrt:
IQNsqrt GLOBAL_Q
.endif
.if GLOBAL_Q == 8
.def _IQ8sqrt
_IQ8sqrt:
IQNsqrt GLOBAL_Q
.endif
.if GLOBAL_Q == 7
.def _IQ7sqrt
_IQ7sqrt:
IQNsqrt GLOBAL_Q
.endif
.if GLOBAL_Q == 6
.def _IQ6sqrt
_IQ6sqrt:
IQNsqrt GLOBAL_Q
.endif
.if GLOBAL_Q == 5
.def _IQ5sqrt
_IQ5sqrt:
IQNsqrt GLOBAL_Q
.endif
.if GLOBAL_Q == 4
.def _IQ4sqrt
_IQ4sqrt:
IQNsqrt GLOBAL_Q
.endif
.if GLOBAL_Q == 3
.def _IQ3sqrt
_IQ3sqrt:
IQNsqrt GLOBAL_Q
.endif
.if GLOBAL_Q == 2
.def _IQ2sqrt
_IQ2sqrt:
IQNsqrt GLOBAL_Q
.endif
.if GLOBAL_Q == 1
.def _IQ1sqrt
_IQ1sqrt:
IQNsqrt GLOBAL_Q
.endif
;;###########################################################################
;; No More.
;;###########################################################################
@@ -0,0 +1,260 @@
;;###########################################################################
;;
;; FILE: IQNtoF.asm
;;
;; TITLE: C Callable IQ to Float Math Function
;;
;;###########################################################################
;;
;; Ver | Date | Who | Description of changes
;; =====|=============|=======|==============================================
;; 1.4 | 17 May 2002 | A. T. | Original Release.
;; -----|-------------|-------|----------------------------------------------
;; | | |
;;
;;###########################################################################
;;===========================================================================
;; Function: _IQNtoF
;;===========================================================================
;;
;; C Usage: extern float _IQtoF(long A); // no round or sat
;;
;;---------------------------------------------------------------------------
;;
;; On Entry: ACC = A in IQ format
;;
;; Regs Used: XT, P
;;
;; On Exit: ACC = IEEE 754 floating-point equivalent of A
;;
;; Q range: 30 to 1
;;
;;---------------------------------------------------------------------------
;; Algorithm: This operation converts an IQ number to the equivalent
;; IEEE 754 Single-Precision floating-point format. This
;; format is shown below:
;;
;; 31 30 23 22 0
;; +-----------------------------------------------------+
;; | s | e | f |
;; +-----------------------------------------------------+
;;
;; Value = (-1)^s * 2^(e-127) * 1.f
;;
;; where: e = 1 to 254, f = 0.000000000 to ~1.0
;; e = 0, f = 0, s = 0, Value = 0.0
;; e = 0 and f != 0 case cannot occur in IQ math
;; e = 255 case cannot occur in IQ math
;;
;;---------------------------------------------------------------------------
;; Benchmark:
;;
;; Cycles = 22 (includes LCR/LRETR)
;;
;;---------------------------------------------------------------------------
IQNtoF .macro q_value
CSB ACC
LSLL ACC,T
MOVL P,@ACC
XOR @PL,#0xFFFF
XOR @PH,#0x7FFF
MOVL @P,ACC,GEQ
MOVB AH,#(127 + (30-q_value))
SUBR @T,AH
MOVL ACC,@P
MOVB @T,#0,EQ
ASR64 ACC:P,#14
AND @AH,#0xFF00
ADD AH,@T
LSL64 ACC:P,#7
LRETR
.endm
;; The value "GLOBAL_Q" needs to be supplied by the assembler
;; using the "-dGLOBAL_Q=q_value" directive:
.sect "IQmath"
.if GLOBAL_Q == 30
.def _IQ30toF
_IQ30toF:
IQNtoF GLOBAL_Q
.endif
.if GLOBAL_Q == 29
.def _IQ29toF
_IQ29toF:
IQNtoF GLOBAL_Q
.endif
.if GLOBAL_Q == 28
.def _IQ28toF
_IQ28toF:
IQNtoF GLOBAL_Q
.endif
.if GLOBAL_Q == 27
.def _IQ27toF
_IQ27toF:
IQNtoF GLOBAL_Q
.endif
.if GLOBAL_Q == 26
.def _IQ26toF
_IQ26toF:
IQNtoF GLOBAL_Q
.endif
.if GLOBAL_Q == 25
.def _IQ25toF
_IQ25toF:
IQNtoF GLOBAL_Q
.endif
.if GLOBAL_Q == 24
.def _IQ24toF
_IQ24toF:
IQNtoF GLOBAL_Q
.endif
.if GLOBAL_Q == 23
.def _IQ23toF
_IQ23toF:
IQNtoF GLOBAL_Q
.endif
.if GLOBAL_Q == 22
.def _IQ22toF
_IQ22toF:
IQNtoF GLOBAL_Q
.endif
.if GLOBAL_Q == 21
.def _IQ21toF
_IQ21toF:
IQNtoF GLOBAL_Q
.endif
.if GLOBAL_Q == 20
.def _IQ20toF
_IQ20toF:
IQNtoF GLOBAL_Q
.endif
.if GLOBAL_Q == 19
.def _IQ19toF
_IQ19toF:
IQNtoF GLOBAL_Q
.endif
.if GLOBAL_Q == 18
.def _IQ18toF
_IQ18toF:
IQNtoF GLOBAL_Q
.endif
.if GLOBAL_Q == 17
.def _IQ17toF
_IQ17toF:
IQNtoF GLOBAL_Q
.endif
.if GLOBAL_Q == 16
.def _IQ16toF
_IQ16toF:
IQNtoF GLOBAL_Q
.endif
.if GLOBAL_Q == 15
.def _IQ15toF
_IQ15toF:
IQNtoF GLOBAL_Q
.endif
.if GLOBAL_Q == 14
.def _IQ14toF
_IQ14toF:
IQNtoF GLOBAL_Q
.endif
.if GLOBAL_Q == 13
.def _IQ13toF
_IQ13toF:
IQNtoF GLOBAL_Q
.endif
.if GLOBAL_Q == 12
.def _IQ12toF
_IQ12toF:
IQNtoF GLOBAL_Q
.endif
.if GLOBAL_Q == 11
.def _IQ11toF
_IQ11toF:
IQNtoF GLOBAL_Q
.endif
.if GLOBAL_Q == 10
.def _IQ10toF
_IQ10toF:
IQNtoF GLOBAL_Q
.endif
.if GLOBAL_Q == 9
.def _IQ9toF
_IQ9toF:
IQNtoF GLOBAL_Q
.endif
.if GLOBAL_Q == 8
.def _IQ8toF
_IQ8toF:
IQNtoF GLOBAL_Q
.endif
.if GLOBAL_Q == 7
.def _IQ7toF
_IQ7toF:
IQNtoF GLOBAL_Q
.endif
.if GLOBAL_Q == 6
.def _IQ6toF
_IQ6toF:
IQNtoF GLOBAL_Q
.endif
.if GLOBAL_Q == 5
.def _IQ5toF
_IQ5toF:
IQNtoF GLOBAL_Q
.endif
.if GLOBAL_Q == 4
.def _IQ4toF
_IQ4toF:
IQNtoF GLOBAL_Q
.endif
.if GLOBAL_Q == 3
.def _IQ3toF
_IQ3toF:
IQNtoF GLOBAL_Q
.endif
.if GLOBAL_Q == 2
.def _IQ2toF
_IQ2toF:
IQNtoF GLOBAL_Q
.endif
.if GLOBAL_Q == 1
.def _IQ1toF
_IQ1toF:
IQNtoF GLOBAL_Q
.endif
;;###########################################################################
;; No More.
;;###########################################################################
@@ -0,0 +1,261 @@
;;###########################################################################
;;
;; FILE: IQNtoF_fpu32.asm
;;
;; TITLE: C Callable IQ to Float Math Function
;;
;;###########################################################################
;;
;; Ver | Date | Who | Description of changes
;; =====|=============|=======|==============================================
;; 1.5 | 01 May 2008 | L. H | Original Release.
;; -----|-------------|-------|----------------------------------------------
;; | | |
;;
;;###########################################################################
;;===========================================================================
;; Function: _IQNtoF
;;===========================================================================
;;
;; C Usage: extern float _IQtoF(long A); // no round or sat
;;
;;---------------------------------------------------------------------------
;;
;; On Entry: ACC = A in IQ format
;;
;; Regs Used: XT, P
;;
;; On Exit: ACC = IEEE 754 floating-point equivalent of A
;;
;; Q range: 30 to 1
;;
;;---------------------------------------------------------------------------
;; Algorithm: This operation converts an IQ number to the equivalent
;; IEEE 754 Single-Precision floating-point format. This
;; format is shown below:
;;
;; 31 30 23 22 0
;; +-----------------------------------------------------+
;; | s | e | f |
;; +-----------------------------------------------------+
;;
;; Value = (-1)^s * 2^(e-127) * 1.f
;;
;; where: e = 1 to 254, f = 0.000000000 to ~1.0
;; e = 0, f = 0, s = 0, Value = 0.0
;; e = 0 and f != 0 case cannot occur in IQ math
;; e = 255 case cannot occur in IQ math
;;
;;---------------------------------------------------------------------------
;; Benchmark:
;;
;; Cycles = 22 (includes LCR/LRETR)
;;
;;---------------------------------------------------------------------------
IQNtoF .macro q_value
CSB ACC
LSLL ACC,T
MOVL P,@ACC
XOR @PL,#0xFFFF
XOR @PH,#0x7FFF
MOVL @P,ACC,GEQ
MOVB AH,#(127 + (30-q_value))
SUBR @T,AH
MOVL ACC,@P
MOVB @T,#0,EQ
ASR64 ACC:P,#14
AND @AH,#0xFF00
ADD AH,@T
LSL64 ACC:P,#7
MOV32 R0H, ACC
LRETR
.endm
;; The value "GLOBAL_Q" needs to be supplied by the assembler
;; using the "-dGLOBAL_Q=q_value" directive:
.sect "IQmath"
.if GLOBAL_Q == 30
.def _IQ30toF
_IQ30toF:
IQNtoF GLOBAL_Q
.endif
.if GLOBAL_Q == 29
.def _IQ29toF
_IQ29toF:
IQNtoF GLOBAL_Q
.endif
.if GLOBAL_Q == 28
.def _IQ28toF
_IQ28toF:
IQNtoF GLOBAL_Q
.endif
.if GLOBAL_Q == 27
.def _IQ27toF
_IQ27toF:
IQNtoF GLOBAL_Q
.endif
.if GLOBAL_Q == 26
.def _IQ26toF
_IQ26toF:
IQNtoF GLOBAL_Q
.endif
.if GLOBAL_Q == 25
.def _IQ25toF
_IQ25toF:
IQNtoF GLOBAL_Q
.endif
.if GLOBAL_Q == 24
.def _IQ24toF
_IQ24toF:
IQNtoF GLOBAL_Q
.endif
.if GLOBAL_Q == 23
.def _IQ23toF
_IQ23toF:
IQNtoF GLOBAL_Q
.endif
.if GLOBAL_Q == 22
.def _IQ22toF
_IQ22toF:
IQNtoF GLOBAL_Q
.endif
.if GLOBAL_Q == 21
.def _IQ21toF
_IQ21toF:
IQNtoF GLOBAL_Q
.endif
.if GLOBAL_Q == 20
.def _IQ20toF
_IQ20toF:
IQNtoF GLOBAL_Q
.endif
.if GLOBAL_Q == 19
.def _IQ19toF
_IQ19toF:
IQNtoF GLOBAL_Q
.endif
.if GLOBAL_Q == 18
.def _IQ18toF
_IQ18toF:
IQNtoF GLOBAL_Q
.endif
.if GLOBAL_Q == 17
.def _IQ17toF
_IQ17toF:
IQNtoF GLOBAL_Q
.endif
.if GLOBAL_Q == 16
.def _IQ16toF
_IQ16toF:
IQNtoF GLOBAL_Q
.endif
.if GLOBAL_Q == 15
.def _IQ15toF
_IQ15toF:
IQNtoF GLOBAL_Q
.endif
.if GLOBAL_Q == 14
.def _IQ14toF
_IQ14toF:
IQNtoF GLOBAL_Q
.endif
.if GLOBAL_Q == 13
.def _IQ13toF
_IQ13toF:
IQNtoF GLOBAL_Q
.endif
.if GLOBAL_Q == 12
.def _IQ12toF
_IQ12toF:
IQNtoF GLOBAL_Q
.endif
.if GLOBAL_Q == 11
.def _IQ11toF
_IQ11toF:
IQNtoF GLOBAL_Q
.endif
.if GLOBAL_Q == 10
.def _IQ10toF
_IQ10toF:
IQNtoF GLOBAL_Q
.endif
.if GLOBAL_Q == 9
.def _IQ9toF
_IQ9toF:
IQNtoF GLOBAL_Q
.endif
.if GLOBAL_Q == 8
.def _IQ8toF
_IQ8toF:
IQNtoF GLOBAL_Q
.endif
.if GLOBAL_Q == 7
.def _IQ7toF
_IQ7toF:
IQNtoF GLOBAL_Q
.endif
.if GLOBAL_Q == 6
.def _IQ6toF
_IQ6toF:
IQNtoF GLOBAL_Q
.endif
.if GLOBAL_Q == 5
.def _IQ5toF
_IQ5toF:
IQNtoF GLOBAL_Q
.endif
.if GLOBAL_Q == 4
.def _IQ4toF
_IQ4toF:
IQNtoF GLOBAL_Q
.endif
.if GLOBAL_Q == 3
.def _IQ3toF
_IQ3toF:
IQNtoF GLOBAL_Q
.endif
.if GLOBAL_Q == 2
.def _IQ2toF
_IQ2toF:
IQNtoF GLOBAL_Q
.endif
.if GLOBAL_Q == 1
.def _IQ1toF
_IQ1toF:
IQNtoF GLOBAL_Q
.endif
;;###########################################################################
;; No More.
;;###########################################################################
@@ -0,0 +1,205 @@
/**********************************************************************
* Function: IQNtoa()
*
* Description: Converts IQ number to an ASCII string
*
* DSP: TMS320F28x
*
* Author: David M. Alter
*
* Include files: stdio.h
*
* Function Prototype: int __IQNtoa(char *, const char *, long, int)
*
* Usage: error = __IQNtoa(char *string, const char *format,
* long x, int qvalue);
*
* Input Parameters:
* string = output string
* format = conversion format. Must be of form "%xx.yyf" with
* xx and yy at most 2 characters in length.
* Examples: "%10.12f", "%2.4f", "%11.6f"
* Maximum supported integer field width (xx) is 11
* (including any negative sign). This captures the full
* integer range for I2Q30 to I31Q1 numbers.
* x = input value (IQ format)
* qvalue = IQ format of x (e.g., 24 means I8Q24)
*
* Return Value:
* error = error code:
* 0 = no error
* 1 = iwidth too small to hold integer characters
* 2 = illegal format specified
*
* Notes:
*
* 1) Leading zeros are not printed for the integer part. Hence, format
* specifies the maximum width of the integer field. The field may
* be smaller.
* 2) The output string is terminated with the null character.
* 3) The integer width in "format" includes the negative sign for
* negative numbers, e.g. -12.3456 is "%3.5f"
* 4) The decimal width in "format" includes the decimal point, e.g.
* -12.3456 is "%3.5f"
* 5) "string" must be large enough to hold the output (including the
* negative sign, and the terminating null character). The program
* does not check for overrun. Memory corruption will occur if
* "string" is too small.
* 6) A non-zero return value indicates that the output string is invalid.
* 7) Benchmark is roughly 200 to 800 cycles, depending on the format and
* the input value.
*
* History: December 23, 2004 - original (D. Alter)
* : April 27, 2019 - fixed bug for not handling 0x80000000 (Arav)
**********************************************************************/
// Function prototypes
extern char _IQNtoa_get_frac_c(unsigned long);
/**********************************************************************/
#pragma CODE_SECTION(__IQNtoa, "IQmath");
int __IQNtoa(char *string, const char *format, long x, int qvalue)
{
unsigned long uval; // working variable
char *bufptr; // buffer pointer
char buf[11]; // temporary char buffer (worst case length)
int count; // conversion character counter
int error = 0; // error value
int neg = 0; // negative flag
int iwidth; // integer format width
int dwidth; // decimal format width
int scale; // format parsing scaling value
//--------------------------------------------------------------------
// Parse the format parameter
//
// Check if x == 0x80000000 and return error if yes
if(x == 0x80000000) return(2); // error: cannot handle 0x80000000
// Check that 1st character is "%"
if(*format++ != '%') return(2); // error: missing the leading '%'
// Extract iwidth
if(*format == '.') return(2); // error: no iwidth field
count = 0; // zero the counter
scale = 1; // init scale
iwidth = 0; // init iwidth
while(*format != '.') // find the '.'
{
count++; // increment the count
iwidth = iwidth*scale + (*format++ - '0');
scale = scale * 10;
// If we don't find the '.' after, say, 4 counts, something is wrong
if(count > 2) return(2); // error: iwidth field too many characters
}
if(iwidth > 11) return(2); // error: iwidth too large
if(iwidth < 0) return(2); // error: iwidth negative
// Extract dwidth
format++; // advance format off the '.'
if(*format == 0) return(2); // error: no dwidth field
count = 0; // zero the counter
scale = 1; // init scale
dwidth = 0; // init dwidth
while((*format != 'f') && (*format != 'F')) // find the conversion character
{
count++; // increment the count
dwidth = dwidth*scale + (*format++ - '0');
scale = scale * 10;
// If we don't find the conversion character after, say, 4 counts, something is wrong
if(count > 2) return(2); // error: dwidth field too large
}
if(dwidth < 0) return(2); // error: dwidth negative
// Check that the next character is the NULL string terminator
if(*++format != 0) return(2); // error: missing null terminator
//--------------------------------------------------------------------
// Check for negative value
//
if(x < 0)
{
x = -x; // make it positive
neg = 1; // set negative flag
}
//--------------------------------------------------------------------
// Process the integer part
//
// Isolate the integer part
uval = x >> qvalue;
// Convert the integer part
bufptr = buf; // init the pointer
count = 0; // zero the counter
do
{
*bufptr++ = (uval % 10) + '0'; // extract the next character
count++; // increment the counter
} while(uval = uval/10); // remove the character value from uval
if(neg)
{
*bufptr++ = '-'; // insert a '-'
count++; // increment the counter
}
if(count > iwidth)
{
count = iwidth; // set count = iwidth
error = 1; // error: integer format too small
}
while(count--)
{
*string++ = *--bufptr; // copy the character to the string
}
//--------------------------------------------------------------------
// Process the fractional part
//
if(dwidth > 0)
{
// Insert the decimal point
*string++ = '.'; // insert the decimal point
dwidth--; // decrement the fraction length
// Isolate the fractional part
uval = x << (32-qvalue); // I0Q32 format
// Convert the fractional part
for(count = 0; count < dwidth; count++)
{
*string++ = _IQNtoa_get_frac_c(uval) + '0'; // extract the character
uval = uval * 10; // remove the character value from uval
}
} // end if
//--------------------------------------------------------------------
// Finish up
//
*string = 0; // add the NUL character to terminate string
return(error); // return
} // end of __IQNtoa()
/*** end of file *****************************************************/
File diff suppressed because it is too large. Load diff
@@ -0,0 +1,217 @@
;;###########################################################################
;;
;; FILE: atoIQN.asm
;;
;; TITLE: C Callable IQ Math Function
;;
;;###########################################################################
;;
;; Ver | Date | Who | Description of changes
;; =====|=============|=======|==============================================
;; 1.2 | 11 Oct 2001 | A. T. | Original Release.
;; -----|-------------|-------|----------------------------------------------
;; 1.3 | 19 Nov 2001 | A. T. | No changes.
;; -----|-------------|-------|----------------------------------------------
;; 1.4 | 17 May 2002 | A. T. | Fixed bug (thanks Settu) with input char
;; | | | range comparison.
;; -----|-------------|-------|----------------------------------------------
;; | | |
;;
;;###########################################################################
;;===========================================================================
;; Function: _atoIQN
;;===========================================================================
;;
;; C Usage: extern long _atoIQN(const char *st, long q_value);
;;
;;---------------------------------------------------------------------------
;;
;; On Entry: XAR4 = pointer to character string
;; ACC = q_value
;;
;; Regs Used: ACC, P, XT
;; XAR7, XAR6, XAR5
;;
;; On Exit: ACC = IQ value from string (0 if an error occured)
;;
;; Q Range: 30 to 1
;;
;;---------------------------------------------------------------------------
;; Algorithm: The input string must only contain decimal characters
;; '0' to '9' and the characters '-' for negative value
;; if applicable and '.' character for delimiting integer and
;; fraction components. Some examples of valid inputs are:
;;
;; 12.23456
;; -12.23456
;; 0.2345
;; 0.0
;; 0
;; 127
;; -89
;;
;; If the input string converts to a number greater then
;; the max/min values for the given Q value, then the returned
;; value will be limited to the min/max values.
;;
;; If the string contains illegal characters, it will return
;; the value zero.
;;
;;---------------------------------------------------------------------------
;; Benchmark:
;; Cycles = Can be approximately from 200 to 800 cycles based on number
;; of characters in string.
;;
;;===========================================================================
.sect "IQmath"
.def _atoIQN
_atoIQN:
NEG ACC ; ACC = - q_value
ADDB ACC,#32 ; ACC = 32 - q_value
MOV AH,@AL ; AH = 32 - q_value
MOVL *SP++,ACC ; Save on stack
ADDB SP,#8
CMP @AH,#31 ; Check to make sure that q_value range
SB _atoIQerror,GT ; is 30 to 1
CMP @AH,#2
SB _atoIQerror,LT
MOVB ACC,#0
MOVL *-SP[8],ACC ; I64l
MOVL *-SP[6],ACC ; I64h, I64 = integer portion
MOVL *-SP[4],ACC ; F64l
MOVL *-SP[2],ACC ; F64h, F64 = fraction portion
MOV AL,#0x999A
MOV AH,#0x1999
MOVL XAR7,@ACC ; XAR7 = 0.1 * 2^32 = scale frac
MOVB XAR6,#10 ; XAR6 = 10 = scale int
ZAPA ; ACC, P = 0
CLRC TC ; TC = 0 = sign flag (default +ve)
_atoIQloop:
MOVZ AR0,*XAR4++ ; AR0 = char
CMP @AR0,#0x00
SB _atoIQend,EQ ; Exit if zero (end of string)
CMP @AR0,#'-'
SB _atoIQneg,EQ ; Set sign flag if char == "-"
CMP @AR0,#'.'
SB _atoIQdot,EQ ; End of integer component if char == "."
SUB @AR0,#'0'
SB _atoIQerror,LT
CMP @AR0,#9
SB _atoIQerror,GT ; Error if char not between "0".."9"
MOVL XT,@XAR6
IMPYXUL P,XT,*-SP[8]
ADDUL P,@XAR0
MOVL ACC,@P
QMPYXUL P,XT,*-SP[8]
MOVL *-SP[8],ACC
IMPYL ACC,XT,*-SP[6]
ADDCL ACC,@P
MOVL *-SP[6],ACC ; I64 = I64*10 + char dec value
SB _atoIQloop,UNC
;-------------------------------------------------------------------------
; Search for end of string and count frac characters:
_atoIQdot:
MOVB XAR5,#0 ; clear frac character counter
_atoIQdotloop:
INC @AR5 ; Increment character counter
MOVZ AR0,*XAR4++ ; AR0 = char
CMP @AR0,#0x00
SB _atoIQfrac,EQ ; Exit if end of string
SUB @AR0,#'0'
SB _atoIQerror,LT
CMP @AR0,#9
SB _atoIQerror,GT ; Error if char not between "0".."9"
SB _atoIQdotloop,UNC
;-------------------------------------------------------------------------
_atoIQfrac:
SUBB XAR4,#1
CMP @AR5,#10
SB _atoIQfracloop,LT
SUBB XAR5,#10
MOVL ACC,@XAR4
SUBL ACC,@XAR5
MOVL @XAR4,ACC
_atoIQfracloop:
MOVZ AR0,*--XAR4 ; AR0 = char
CMP @AR0,#'.'
SB _atoIQend,EQ ; Exit if char == "."
SUB @AR0,#'0'
SB _atoIQerror,LT
CMP @AR0,#9
SB _atoIQerror,GT ; Error if char not between "0".."9"
MOVL ACC,*-SP[2]
MOV AH,@AR0
MOVL *-SP[2],ACC
MOVL XT,@XAR7
QMPYXUL P,XT,*-SP[4]
IMPYL ACC,XT,*-SP[2]
ADDUL P,@ACC
MOVL *-SP[4],P
MPYB P,T,#0
QMPYL ACC,XT,*-SP[2]
ADDCL ACC,@P
MOVL *-SP[2],ACC ; F64 = F64*0.1 + char dec value
SB _atoIQfracloop,UNC
;-------------------------------------------------------------------------
_atoIQneg:
SB _atoIQerror,TC ; Error if more then one -ve sign detected
SETC TC ; Set -ve sign value
SB _atoIQloop,UNC
;-------------------------------------------------------------------------
_atoIQerror:
MOV AH,#0x0000
MOV AL,#0x0000 ; Return zero if error detected
SUBB SP,#10
LRETR
;-------------------------------------------------------------------------
_atoIQend:
MOVL P,*-SP[4]
MOVL ACC,*-SP[2]
LSR64 ACC:P,#16
MOVL ACC,*-SP[8] ; ACC = int, P =frac
MOVL XT,*-SP[10] ; T = 32 - q_value
LSR64 ACC:P,T ; P = IQ value, ACC = overflow
SB _atoIQendPos,NTC
NEG64 ACC:P
_atoIQendPos:
; Set saturation limits:
MOV *-SP[8],#0xFFFF
MOV *-SP[7],#0x7FFF
MOV *-SP[6],#0x0000
MOV *-SP[5],#0x0000
MOV *-SP[4],#0x0000
MOV *-SP[3],#0x8000
MOV *-SP[2],#0xFFFF
MOV *-SP[1],#0xFFFF
; Saturate result:
MINL ACC,*-SP[6]
MINCUL P,*-SP[8]
MAXL ACC,*-SP[2]
MAXCUL P,*-SP[4]
; Return value in ACC:
MOVL ACC,@P
SUBB SP,#10
LRETR
;;###########################################################################
;; No More.
;;###########################################################################
@@ -0,0 +1,45 @@
***********************************************************************
* Function: _IQNtoa_get_frac_c
*
* Description: Called by _IQNtoa(). Not for user use.
*
* DSP: TMS320F28x
*
* Author: David M. Alter
*
* Include files: stdio.h
*
* Function Prototype: extern char _IQNtoa_get_frac_c(unsigned long);
*
* Usage: character = _IQNtoa_get_frac_c(value);
*
* Input Parameters:
* value = fraction in I0Q32 format
*
* Return Value:
* character = the 1st base-10 digit in the fraction
*
* Notes:
* 1) This is an internal function to the IQmath library. It is not
* intended to be called by the user.
*
* History: December 23, 2004 - original (D. Alter)
*
***********************************************************************
.def _IQNtoa_get_frac_c
.sect "IQmath"
_IQNtoa_get_frac_c:
MOVB XAR0, #10 ;XAR0 = 10
MOVL XT, XAR0 ;XT = 10
QMPYUL P, XT, @ACC ;P = XT*ACC upper 32-bits
MOVL @ACC, P ;ACC = the value we want
LRETR ;return
.end
;----------------------------------------------------------------------
; End of file
;----------------------------------------------------------------------
@@ -0,0 +1,40 @@
//###########################################################################
//
// FILE: satf.c
//
// TITLE: C Function To Saturate A Floating-Point Value
//
//###########################################################################
//
// Ver | Date | Who | Description of changes
// =====|=============|=======|==============================================
// 1.4 | 17 May 2002 | A. T. | Original Release.
// -----|-------------|-------|----------------------------------------------
// | | |
//
//###########################################################################
//===========================================================================
// Function: _satf
//===========================================================================
//
// C Usage: extern float _satf(float A, float Pos, float Neg);
//
//---------------------------------------------------------------------------
// Algorithm: Saturates the value "A" between the values "Pos" and "Neg".
//
//---------------------------------------------------------------------------
float _satf(float A, float Pos, float Neg)
{
if( A > Pos )
A = Pos;
if( A < Neg )
A = Neg;
return A;
}
//###########################################################################
// No More.
//###########################################################################