MATHLIB User Guide
MATHLIB_cos.cpp
Go to the documentation of this file.
1 /******************************************************************************
2  * Copyright (C) 2023 Texas Instruments Incorporated - https://www.ti.com/
3  *
4  * Redistribution and use in source and binary forms, with or without
5  * modification, are permitted provided that the following conditions
6  * are met:
7  *
8  * Redistributions of source code must retain the above copyright
9  * notice, this list of conditions and the following disclaimer.
10  *
11  * Redistributions in binary form must reproduce the above copyright
12  * notice, this list of conditions and the following disclaimer in the
13  * documentation and/or other materials provided with the
14  * distribution.
15  *
16  * Neither the name of Texas Instruments Incorporated nor the names of
17  * its contributors may be used to endorse or promote products derived
18  * from this software without specific prior written permission.
19  *
20  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
21  * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
22  * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
23  * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
24  * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
25  * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
26  * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
27  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
28  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
29  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
30  * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
31  *
32  ******************************************************************************/
33 
34 #define ELEMENT_COUNT(x) c7x::element_count_of<x>::value
35 
36 /******************************************************************************/
37 /* */
38 /* Includes */
39 /* */
40 /******************************************************************************/
41 
42 #include "MATHLIB_types.h"
43 #include "MATHLIB_utility.h"
44 
45 /******************************************************************************/
46 /* */
47 /* MATHLIB_cos */
48 /* */
49 /******************************************************************************/
50 
51 // this method performs cosine coputation of input vector
52 template <typename T> MATHLIB_STATUS MATHLIB_cos(size_t length, T *pSrc, T *pDst)
53 {
54 
55  // variables
56  MATHLIB_STATUS status = MATHLIB_SUCCESS; // return function status
57  size_t numBlocks = 0; // compute loop's iteration count
58  size_t remNumBlocks = 0; // when numBlocks is not a multiple of SIMD width
59 
60  // derive c7x vector type from template typename
61  typedef typename c7x::make_full_vector<T>::type vec;
62 
63  // Compile-time decision: float_vec => int_vec and double_vec=> long_vec
64  typedef
65  typename std::conditional<ELEMENT_COUNT(c7x::float_vec) == ELEMENT_COUNT(vec), c7x::int_vec, c7x::long_vec>::type
66  vec_type;
67 
68  __SE_TEMPLATE_v1 se0Params = __gen_SE_TEMPLATE_v1();
69  __SA_TEMPLATE_v1 sa0Params = __gen_SA_TEMPLATE_v1();
70 
71  // check for null pointers and non-zero length
72  status = MATHLIB_checkParams(length, pSrc, pDst);
73 
74  if (status == MATHLIB_SUCCESS) {
75 
76  MATHLIB_SE0SA01DSequentialInit(&se0Params, &sa0Params, length, pSrc, pDst);
77 
78  // calculate compute loop's iteration counter
79  numBlocks = length / c7x::element_count_of<vec>::value;
80  remNumBlocks = length % c7x::element_count_of<vec>::value;
81  if (remNumBlocks) {
82  numBlocks++;
83  }
84 
85  // open SE0, SE1, and SA0 for reading and writing operands
86  MATHLIB_SE0SA0Open(&se0Params, &sa0Params, pSrc);
87 
88  /**********************************************************************/
89  /* Create and assign values for constants employed on cos computation */
90  /**********************************************************************/
91 
92  vec InvPI, HalfPI, One, MAX, Zero, s1, s2, s3, s4, C1, C2;
93 
94  InvPI = (vec) 0.318309886183791;
95  HalfPI = (vec) 1.5707963268;
96  One = (vec) 1.0;
97  MAX = (vec) 1048576.0;
98 
99  Zero = (vec) 0.0;
100  s1 = (vec) -1.666665668e-1;
101  s2 = (vec) 8.333025139e-3;
102  s3 = (vec) -1.980741872e-4;
103  s4 = (vec) 2.601903036e-6;
104  C1 = (vec) 3.140625;
105  C2 = (vec) 9.67653589793e-4;
106 
107  // compute loop to perform vector cos
108  for (size_t i = 0; i < numBlocks; i++) {
109  vec inVec = c7x::strm_eng<0, vec>::get_adv();
110 
111  /**********************************************************************/
112  /* Create and assign values for variables employed on cos computation */
113  /**********************************************************************/
114 
115  vec Sign, X, Y, Z, F, G, R;
116 
117  vec_type int_one = (vec_type) 1;
118  vec_type N;
119 
120  vec negativeOne = (vec) -1;
121  vec negativeR;
122  Sign = One;
123 
124  // if (Y > MAX) {
125  // Y = HalfPI;
126  // }
127  Y = __abs(inVec) + HalfPI;
128  __vpred cmp_gt = __cmp_lt_pred((vec) MAX, Y);
129  Y = __select(cmp_gt, HalfPI, Y);
130 
131  // X = Y * (1/PI)
132  X = Y * InvPI;
133 
134  N = __float_to_int(X);
135  Z = c7x::convert<vec>(N);
136 
137  /**********************************************************************/
138  /* Sign checking for quadrant 3 or 4 */
139  /**********************************************************************/
140 
141  // if ((N % 2) != 0) {
142  // Sign = -Sign;
143  // }
144  vec_type andN = N & int_one;
145  vec convert_andN = c7x::convert<vec>(andN);
146  __vpred cmp_mod = __cmp_le_pred(convert_andN, Zero);
147  vec Sign_T = __select(cmp_mod, Sign, negativeOne);
148 
149  F = (Y - (Z * C1)) - (Z * C2);
150  R = F;
151 
152  // if (F < Zero) {
153  // R = -R;
154  // }
155  negativeR = -R;
156  __vpred cmp_F = __cmp_lt_pred(F, Zero);
157  R = __select(cmp_F, R, negativeR);
158 
159  G = F * F;
160 
161  __vpred cmp_RMin = __cmp_lt_pred(R, Zero);
162 
163  vec outputRMin = R * Sign_T;
164  R = ((((((s4 * G) + s3) * G) + s2) * G) + s1) * G;
165  vec outVec = ((F + (F * R)) * Sign_T);
166 
167  outVec = __select(cmp_RMin, outVec, outputRMin);
168 
169  // outVec.print();
170 
171  __vpred tmp = c7x::strm_agen<0, vec>::get_vpred();
172  vec *addr = c7x::strm_agen<0, vec>::get_adv(pDst);
173  __vstore_pred(tmp, addr, outVec);
174  }
175 
177  }
178 
179  return status;
180 }
181 
182 /******************************************************************************/
183 /* */
184 /* Explicit templatization for datatypes supported by MATHLIB_cos */
185 /* */
186 /******************************************************************************/
187 
188 // single precision
189 template MATHLIB_STATUS MATHLIB_cos<float>(size_t length, float *pSrc, float *pDst);
190 
191 /******************************************************************************/
192 /* */
193 /* C-interface wrapper functions */
194 /* */
195 /******************************************************************************/
196 
197 extern "C" {
198 
199 // single-precision wrapper
200 MATHLIB_STATUS MATHLIB_cos_sp(size_t length, float *pSrc, float *pDst)
201 {
202  MATHLIB_STATUS status = MATHLIB_cos(length, pSrc, pDst);
203  return status;
204 }
205 
206 } // extern "C"
template MATHLIB_STATUS MATHLIB_cos< float >(size_t length, float *pSrc, float *pDst)
#define ELEMENT_COUNT(x)
Definition: MATHLIB_cos.cpp:34
MATHLIB_STATUS MATHLIB_cos_sp(size_t length, float *pSrc, float *pDst)
This function is the C interface for MATHLIB_cos. Function accepts float pointers.
MATHLIB_STATUS MATHLIB_cos(size_t length, T *pSrc, T *pDst)
Performs the elementwise cosition of an input vector. Function can be overloaded with float pointers,...
Definition: MATHLIB_cos.cpp:52
static void MATHLIB_SE0SA0Close()
This method performs SE0 and SA0 close.
static void MATHLIB_SE0SA01DSequentialInit(__SE_TEMPLATE_v1 *se0Params, __SA_TEMPLATE_v1 *sa0Params, size_t length, T *pSrc, T *pDst)
static MATHLIB_STATUS MATHLIB_checkParams(size_t length, T *pSrc, T *pDst)
This method performs parameter checks for MATHLIB function.
static void MATHLIB_SE0SA0Open(__SE_TEMPLATE_v1 *se0Params, __SA_TEMPLATE_v1 *sa0Params, T *pSrc)
This method performs SE0 and SA0 open.
MATHLIB_STATUS_NAME
The enumeration of all status codes.
@ MATHLIB_SUCCESS