MATHLIB User Guide
MATHLIB_div.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 
47 /******************************************************************************/
48 /* */
49 /* MATHLIB_div */
50 /* */
51 /******************************************************************************/
52 
53 // this method performs division computation of input vector
54 template <typename T> MATHLIB_STATUS MATHLIB_div(size_t length, T *pSrc0, T *pSrc1, T *pDst)
55 {
56 
57  // variables
58  MATHLIB_STATUS status = MATHLIB_SUCCESS; // return function status
59  size_t numBlocks = 0; // compute loop's iteration count
60  size_t remNumBlocks = 0; // when numBlocks is not a multiple of SIMD width
61 
62  // derive c7x vector type from template typename
63  typedef typename c7x::make_full_vector<T>::type vec;
64 
65  __SE_TEMPLATE_v1 se0Params = __gen_SE_TEMPLATE_v1();
66  __SA_TEMPLATE_v1 sa0Params = __gen_SA_TEMPLATE_v1();
67 
68  // check for null pointers and non-zero length, pSrc0 check
69  status = MATHLIB_checkParams(length, pSrc0, pSrc1, pDst);
70 
71  if (status == MATHLIB_SUCCESS) {
72 
73  MATHLIB_SE0SA01DSequentialInit(&se0Params, &sa0Params, length, pSrc0, pDst);
74 
75  // calculate compute loop's iteration counter
76  numBlocks = length / c7x::element_count_of<vec>::value;
77  remNumBlocks = length % c7x::element_count_of<vec>::value;
78  if (remNumBlocks) {
79  numBlocks++;
80  }
81 
82  // open SE0, SE1, and SA0 for reading and writing operands
83  MATHLIB_SE0SE1SA0Open(&se0Params, &sa0Params, pSrc0, pSrc1);
84 
85  /**********************************************************************/
86  /* Create and assign values for constants employed on div computation */
87  /**********************************************************************/
88 
89  vec Two;
90 
91  Two = (vec) 2.0;
92 
93  // compute loop to perform vector div
94  for (size_t i = 0; i < numBlocks; i++) {
95  vec a = c7x::strm_eng<0, vec>::get_adv();
96  vec b = c7x::strm_eng<1, vec>::get_adv();
97 
98  /**********************************************************************/
99  /* Create variables employed on div computation */
100  /**********************************************************************/
101 
102  vec d0, d1, p0, p1;
103 
104  /**********************************************************************/
105  /* Div computation */
106  /**********************************************************************/
107 
108  // Calculate reciprocal
109  p0 = __recip(b);
110  d0 = p0 * b;
111 
112  p1 = Two - d0;
113  d1 = p0 * p1;
114 
115  vec outVec = a * d1;
116 
117  __vpred tmp = c7x::strm_agen<0, vec>::get_vpred();
118  vec *addr = c7x::strm_agen<0, vec>::get_adv(pDst);
119  __vstore_pred(tmp, addr, outVec);
120  }
121 
123  }
124 
125  return status;
126 }
127 
128 /******************************************************************************/
129 /* */
130 /* Explicit templatization for datatypes supported by MATHLIB_div */
131 /* */
132 /******************************************************************************/
133 
134 // single precision
135 template MATHLIB_STATUS MATHLIB_div<float>(size_t length, float *pSrc0, float *pSrc1, float *pDst);
136 
137 /******************************************************************************/
138 /* */
139 /* C-interface wrapper functions */
140 /* */
141 /******************************************************************************/
142 
143 extern "C" {
144 
145 // single-precision wrapper
146 MATHLIB_STATUS MATHLIB_div_sp(size_t length, float *pSrc0, float *pSrc1, float *pDst)
147 {
148  MATHLIB_STATUS status = MATHLIB_div(length, pSrc0, pSrc1, pDst);
149  return status;
150 }
151 
152 } // extern "C"
template MATHLIB_STATUS MATHLIB_div< float >(size_t length, float *pSrc0, float *pSrc1, float *pDst)
MATHLIB_STATUS MATHLIB_div(size_t length, T *pSrc0, T *pSrc1, T *pDst)
Performs the elementwise divide of two input vectors. Function can be overloaded with float or double...
Definition: MATHLIB_div.cpp:54
MATHLIB_STATUS MATHLIB_div_sp(size_t length, float *pSrc0, float *pSrc1, float *pDst)
This function is the C interface for MATHLIB_div. Function accepts float pointers.
static void MATHLIB_SE0SE1SA0Open(__SE_TEMPLATE_v1 *se0Params, __SA_TEMPLATE_v1 *sa0Params, T *pSrc0, T *pSrc1)
This method performs SE0, SE1, and SA0 open.
static void MATHLIB_SE0SE1SA0Close()
This method performs SE0, SE1, 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.
MATHLIB_STATUS_NAME
The enumeration of all status codes.
@ MATHLIB_SUCCESS