MATHLIB User Guide
MATHLIB_lut.h
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 #ifndef MATHLIB_LUT_H_
35 #define MATHLIB_LUT_H_ 1
36 
37 /******************************************************************************/
38 /* */
39 /* Includes */
40 /* */
41 /******************************************************************************/
42 #include "MATHLIB_debugPrint.h"
43 #include "MATHLIB_types.h"
44 #include "c7x.h"
45 #include "c7x_scalable.h"
46 
58 /******************************************************************************/
59 /* */
60 /* Defines */
61 /* */
62 /******************************************************************************/
63 
64 #define MATHLIB_KTABLE_OFFSET 0
65 #define MATHLIB_JTABLE_OFFSET 4
66 #define MATHLIB_LOGTABLE_OFFSET 8
67 #define MATHLIB_VTABLE_OFFSET 16
68 
69 /******************************************************************************/
70 /* */
71 /* MATHLIB_LUTReadUpperBits */
72 /* */
73 /******************************************************************************/
74 
86 template <typename vecType> static inline c7x::uint_vec MATHLIB_LUTReadUpperBits(vecType vecOffset)
87 {
88  c7x::uint_vec vecOut;
89  vecOut = c7x::reinterpret<c7x::uint_vec>(__lookup_read_int(__LUT_SET0, vecOffset));
90 
91  return vecOut;
92 }
93 
94 /******************************************************************************/
95 /* */
96 /* MATHLIB_LUTReadLowerBits */
97 /* */
98 /******************************************************************************/
99 
111 template <typename vecType> static inline c7x::uint_vec MATHLIB_LUTReadLowerBits(vecType vecOffset)
112 {
113  c7x::uint_vec vecOut;
114  vecOut = c7x::reinterpret<c7x::uint_vec>(__lookup_read_int(__LUT_SET1, vecOffset));
115 
116  return vecOut;
117 }
118 
119 /******************************************************************************/
120 /* */
121 /* MATHLIB_LUTInit */
122 /* */
123 /******************************************************************************/
124 
130 static inline void MATHLIB_LUTInit()
131 {
132  const uint32_t tableSize = 20;
133 
134  // Upper Bits LUT - Contains bits 63-32 for all lookup table values
135 
136  uint32_t MATHLIB_LUTHigh[tableSize] = {
137  0x3FF00000u, // 2^(0/4) 1.000000000 ti_math_kTable
138  0x3FF306FEu, // 2^(1/4) 1.189207115
139  0x3FF6A09Eu, // 2^(2/4) 1.414213562
140  0x3FFAE89Fu, // 2^(3/4) 1.681792831
141  0x3FF00000u, // 2^(0/16) 1.000000000 ti_math_jTable
142  0x3FF0B558u, // 2^(1/16) 1.044273782
143  0x3FF172B8u, // 2^(2/16) 1.090507733
144  0x3FF2387Au, // 2^(3/16) 1.138788635
145  0x00000000u, // 0.0000000000 ti_math_logtable
146  0xBFBE2707u, // -0.1177830356
147  0xBFCC8FF7u, // -0.2231435513
148  0xBFD4618Bu, // -0.3184537311
149  0xBFD9F323u, // -0.4054651081
150  0xBFDF128Fu, // -0.4855078157
151  0xBFE1E85Fu, // -0.5596157879
152  0xBFE41D8Fu, // -0.6286086594
153  0x00000000u, // 0.00000000000000000000 ti_math_vTable
154  0x3FE0C152u, // 0.52359877559829887308
155  0x3FF921FBu, // 1.57079632679489661923
156  0x3FF0C152u // 1.04719755119659774615
157  };
158 
159  // Lower Bits LUT - Contains bits 31-0 for all lookup table values
160 
161  uint32_t MATHLIB_LUTLow[tableSize] = {
162  0x00000000u, // 2^(0/4) 1.000000000 ti_math_kTable
163  0x0A318737u, // 2^(1/4) 1.189207115
164  0x6665983Eu, // 2^(2/4) 1.414213562
165  0x997CAD14u, // 2^(3/4) 1.681792831
166  0x00000000u, // 2^(0/16) 1.000000000 ti_math_jTable
167  0x6CDC29EBu, // 2^(1/16) 1.044273782
168  0x3C945254u, // 2^(2/16) 1.090507733
169  0x6E861A8Cu, // 2^(3/16) 1.138788635
170  0x00000000u, // 0.0000000000 ti_math_logtable
171  0x6DECF45Eu, // -0.1177830356
172  0xC792CA49u, // -0.2231435513
173  0xC2174681u, // -0.3184537311
174  0xECBD59C8u, // -0.4054651081
175  0x5F9891BEu, // -0.4855078157
176  0x5E6B627Du, // -0.5596157879
177  0xE8435F76u, // -0.6286086594
178  0x00000000u, // 0.00000000000000000000 ti_math_vTable
179  0x382D7366u, // 0.52359877559829887308
180  0x54442D18u, // 1.57079632679489661923
181  0x382D7366u // 1.04719755119659774615
182  };
183 
184  // Intialize Upper Bits LUT
185  __sLTCRFlags_t set0flags;
186 
187  // 16 way lut for 32-bit data with 8 entries
188  set0flags.INTERPOLATION = __LUT_INTERP_OFF;
189  set0flags.SATURATION = __LUT_SAT_OFF;
190  set0flags.SIGNUNSIGN = __LUT_SIGNED; // data elements are signed
191  set0flags.ELETYPE = __LUT_ELETYPE_32BIT; // 32-bit elements
192  set0flags.NUMTBLS = __LUT_NUM_TBL_16; // 16 parallel lookup every cycle
193  set0flags.TBLSIZE = __LUT_TBL_SIZE_2KBYTES; // Total table size
194  set0flags.WEIGHTSIZE = __LUT_WSIZE_8BIT;
195  set0flags.PROMOTION = __LUT_PROMOTE_OFF;
196 
197  // Set configuration register for SET0
198  __LUT_SET_LTCR(__LUT_SET0, __lut_set_param(&set0flags));
199 
200  // Start the table at offset 0 from the beginning of L1D-SRAM
201  __LUT_SET_LTBR(__LUT_SET0, 0x0000);
202 
203  // Enable set 0
204  __LUT_SET_LTER(__LUT_ENABLE_0);
205 
206  // Read the elements as a quantum of unsigned 64-bits
207  uint64_t *pData = (uint64_t *) &MATHLIB_LUTHigh[0];
208  c7x::ulong_vec vData;
209  uint32_t rowOffset = 0;
210  uint32_t rowIncrement = 2;
211 
212  for (uint32_t j = 0; j < (tableSize / rowIncrement); j++) {
213  // Read the quantum into a vector of ulong8
214  vData = (c7x::ulong_vec)(pData[j]);
215 
216  // Update the quanta in all N ways
217  __lookup_init(__LUT_SET0, __as_uint16(vData), rowOffset);
218 
219  // Increment the row_offset by 2 as we are initializing 2 32-bit elements at a time
220  rowOffset += rowIncrement;
221  }
222 
223  // Intialize Lower Bits LUT
224  __sLTCRFlags_t set1flags;
225 
226  // 16 way lut for 32-bit data with 8 entries
227  set1flags.INTERPOLATION = __LUT_INTERP_OFF;
228  set1flags.SATURATION = __LUT_SAT_OFF;
229  set1flags.SIGNUNSIGN = __LUT_SIGNED; // data elements are signed
230  set1flags.ELETYPE = __LUT_ELETYPE_32BIT; // working with 32-bit elements
231  set1flags.NUMTBLS = __LUT_NUM_TBL_16; // 16 parallel lookup every cycle
232  set1flags.TBLSIZE = __LUT_TBL_SIZE_2KBYTES; // Total table size
233  set1flags.WEIGHTSIZE = __LUT_WSIZE_8BIT;
234  set1flags.PROMOTION = __LUT_PROMOTE_OFF;
235 
236  // Set configuration register for SET1
237  __LUT_SET_LTCR(__LUT_SET1, __lut_set_param(&set1flags));
238 
239  // Start the table at offset 0x2000 from the beginning of L1D-SRAM
240  __LUT_SET_LTBR(__LUT_SET1, 0x2000);
241 
242  // Enable set 1
243  __LUT_SET_LTER(__LUT_ENABLE_1);
244 
245  // Read the elements as a quantum of unsigned 64-bits
246  pData = (uint64_t *) &MATHLIB_LUTLow[0];
247  rowOffset = 0;
248 
249  for (uint32_t j = 0; j < (tableSize / rowIncrement); j++) {
250  // Read the quantum into a vector of ulong8
251  vData = (c7x::ulong_vec)(pData[j]);
252 
253  // Update the quanta in all N ways
254  __lookup_init(__LUT_SET1, __as_uint16(vData), rowOffset);
255 
256  pData = (uint64_t *) &MATHLIB_LUTLow[0];
257  rowOffset = 0;
258 
259  for (uint32_t i = 0; i < (tableSize / rowIncrement); i++) {
260  // Read the quantum into a vector of ulong8
261  vData = (c7x::ulong_vec)(pData[i]);
262 
263  // Update the quanta in all N ways
264  __lookup_init(__LUT_SET1, __as_uint16(vData), rowOffset);
265 
266  // Increment the row_offset by 2 as we are initializing 2 32-bit elements at a time
267  rowOffset += rowIncrement;
268  }
269 
270 #if MATHLIB_DEBUGPRINT
271  // Print all LUT Values
272  c7x::uint_vec indices1 = c7x::uint_vec(0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15);
273  c7x::uint_vec indices2 = c7x::uint_vec(16, 17, 18, 19, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0);
274  printf("\nUpper Bits\n");
277  printf("\nLower Bits\n");
280 #endif
281  }
282 }
283 
286 #endif
static void MATHLIB_printVec(vecType vec)
This method prints the contents of a vector datatype variable.
static void MATHLIB_LUTInit()
This method intializes the upper and lower bit lookup tables.
Definition: MATHLIB_lut.h:130
static c7x::uint_vec MATHLIB_LUTReadLowerBits(vecType vecOffset)
This method reads bits 31-0 of LUT value at vecOffset.
Definition: MATHLIB_lut.h:111
static c7x::uint_vec MATHLIB_LUTReadUpperBits(vecType vecOffset)
This method reads bits 63-32 of LUT value at vecOffset.
Definition: MATHLIB_lut.h:86