MATHLIB User Guide
MATHLIB_sqrt.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 #define ELEMENT_TYPE(x) typename c7x::element_type_of<x>::type
36 
37 /******************************************************************************/
38 /* */
39 /* Includes */
40 /* */
41 /******************************************************************************/
42 
43 #include "MATHLIB_types.h"
44 #include "MATHLIB_utility.h"
45 #include <cstddef>
46 #include <limits>
47 
48 /******************************************************************************/
49 /* */
50 /* MATHLIB_sqrt */
51 /* */
52 /******************************************************************************/
53 
54 // this method performs sqrt computation of input vector
55 template <typename T> MATHLIB_STATUS MATHLIB_sqrt(size_t length, T *pSrc, T *pDst)
56 {
57 
58  // variables
59  MATHLIB_STATUS status = MATHLIB_SUCCESS; // return function status
60  size_t numBlocks = 0; // compute loop's iteration count
61  size_t remNumBlocks = 0; // when numBlocks is not a multiple of SIMD width
62 
63  // derive c7x vector type from template typename
64  typedef typename c7x::make_full_vector<T>::type vec;
65 
66  /* define type of elements vec vector holds as elemType */
67  typedef ELEMENT_TYPE(vec) elemType;
68 
69  __SE_TEMPLATE_v1 se0Params = __gen_SE_TEMPLATE_v1();
70  __SA_TEMPLATE_v1 sa0Params = __gen_SA_TEMPLATE_v1();
71 
72  status = MATHLIB_checkParams(length, pSrc, pDst);
73 
74  if (status == MATHLIB_SUCCESS) {
75  MATHLIB_SE0SA01DSequentialInit(&se0Params, &sa0Params, length, pSrc, pDst);
76 
77  // calculate compute loop's iteration counter
78  numBlocks = length / c7x::element_count_of<vec>::value;
79  remNumBlocks = length % c7x::element_count_of<vec>::value;
80  if (remNumBlocks) {
81  numBlocks++;
82  }
83 
84  // open SE0, SE1, and SA0 for reading and writing operands
85  MATHLIB_SE0SA0Open(&se0Params, &sa0Params, pSrc);
86 
87  /***********************************************************************/
88  /* Create and assign values for constants employed on sqrt computation */
89  /***********************************************************************/
90 
91  vec half, OneP5, zero, maxValue;
92 
93  half = (vec) 0.5;
94  OneP5 = (vec) 1.5;
95  zero = (vec) 0.0;
96  maxValue = (vec) std::numeric_limits<elemType>::max();
97 
98  // compute loop to perform vector sqrt
99  for (size_t i = 0; i < numBlocks; i++) {
100  vec inVec = c7x::strm_eng<0, vec>::get_adv();
101 
102  /**********************************************************************/
103  /* Create variables employed on sqrt computation */
104  /**********************************************************************/
105 
106  vec y, p0, p1, d0;
107 
108  /**********************************************************************/
109  /* Sqrt computation */
110  /**********************************************************************/
111 
112  // Reciprocal square root calculation
113  p0 = __recip_sqrt(inVec);
114  d0 = p0 * inVec;
115  p1 = OneP5 - d0 * p0 * half;
116  y = inVec * p0 * p1;
117 
118  /**********************************************************************/
119  /* Bounds checking */
120  /**********************************************************************/
121 
122  // If input is <= 0, output defaults to 0
123  __vpred cmp_lezero = __cmp_le_pred(inVec, zero);
124  y = __select(cmp_lezero, zero, y);
125 
126  // If input is greater than maximum allowed by data type, then output is max of datatype
127  __vpred cmp_gtmax = __cmp_le_pred(maxValue, inVec);
128  vec outVec = __select(cmp_gtmax, maxValue, y);
129 
130  __vpred tmp = c7x::strm_agen<0, vec>::get_vpred();
131  vec *addr = c7x::strm_agen<0, vec>::get_adv(pDst);
132  __vstore_pred(tmp, addr, outVec);
133  }
134 
136  }
137 
138  return status;
139 }
140 
141 /******************************************************************************/
142 /* */
143 /* Explicit templatization for datatypes supported by MATHLIB_sqrt */
144 /* */
145 /******************************************************************************/
146 
147 // single precision
148 template MATHLIB_STATUS MATHLIB_sqrt<float>(size_t length, float *pSrc, float *pDst);
149 
150 /******************************************************************************/
151 /* */
152 /* C-interface wrapper functions */
153 /* */
154 /******************************************************************************/
155 
156 extern "C" {
157 
158 // single-precision wrapper
159 MATHLIB_STATUS MATHLIB_sqrt_sp(size_t length, float *pSrc, float *pDst)
160 {
161  MATHLIB_STATUS status = MATHLIB_sqrt(length, pSrc, pDst);
162  return status;
163 }
164 
165 } // extern "C"
#define ELEMENT_TYPE(x)
template MATHLIB_STATUS MATHLIB_sqrt< float >(size_t length, float *pSrc, float *pDst)
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 MATHLIB_sqrt_sp(size_t length, float *pSrc, float *pDst)
This function is the C interface for MATHLIB_sqrt. Function accepts float pointers.
MATHLIB_STATUS MATHLIB_sqrt(size_t length, T *pSrc, T *pDst)
Performs the elementwise square root of an input vectors. Function can be overloaded with float point...
MATHLIB_STATUS_NAME
The enumeration of all status codes.
@ MATHLIB_SUCCESS