MATHLIB User Guide
MATHLIB_types.h
Go to the documentation of this file.
1 /******************************************************************************
2  * *
3  * module name :MATHLIB *
4  * *
5  * module descripton :Matrix Multiply Accelerator Library module for C7x+MMA *
6  * *
7  * Copyright (C) 2017-2018 Texas Instruments Incorporated - http://www.ti.com/ *
8  * ALL RIGHTS RESERVED *
9  * *
10  ******************************************************************************/
11 
23 #ifndef COMMON_MATHLIB_TYPES_H_
24 #define COMMON_MATHLIB_TYPES_H_ 1
25 
26 /* This header is needed to be included in OpenCL programs which link
27  * against MATHLIB, but OpenCL doesn't need the following headers */
28 #ifndef __OPENCL_VERSION__
29 #include <stdint.h>
30 #include <stdlib.h>
31 #include <inttypes.h>
32 #include <stddef.h> // for NULL
33 #include <stdio.h> // for printf
34 #endif
35 #include <c7x.h> // for streaming engine, streaming address gen.
36 
37 
38 #include "MATHLIB_bufParams.h"
39 #include "TI_platforms.h"
40 
41 #include "c71/MATHLIB_defines.h"
42 
43 #include "c7100/MATHLIB_defines.h"
44 #include "c7100/MATHLIB_types.h"
45 
49 #if !defined(MATHLIB_DEBUGPRINT)
50 #define MATHLIB_DEBUGPRINT 0
51 #endif
52 
53 #if (defined(_HOST_BUILD) && (MATHLIB_COMPILER_VERSION < 1003999))
54 #define MATHLIB_const
55 #else
56 #define MATHLIB_const const
57 #endif
58 
59 #define __PRAGMA(x) _Pragma(#x)
60 #ifdef __cplusplus
61 # define __INLINE_FUNC(x) __PRAGMA(FUNC_ALWAYS_INLINE)
62 #else
63 # define __INLINE_FUNC(x) __PRAGMA(FUNC_ALWAYS_INLINE(x))
64 #endif
65 
66 /* ---------------------------------------------------------------- */
67 /* MISRAC Rule 4.9(DEFINE.FUNC) Deviation: The following two */
68 /* function-like macros do not have equivalent function */
69 /* implementations. */
70 /* ---------------------------------------------------------------- */
71 #if defined(EVM_TEST)
72 #define MATHLIB_PRINTF(fmt,...) \
73  do { \
74  fprintf(stdout, fmt, __VA_ARGS__); \
75  } while (0)
76 
77 #if MATHLIB_DEBUGPRINT > 0
78 #define MATHLIB_DEBUGPRINTFN(N, fmt, ...) \
79  do { \
80  if (MATHLIB_DEBUGPRINT >= (N)) { \
81  fprintf(stdout, "MATHLIB debug %s - %d: " fmt, __FUNCTION__, __LINE__, \
82  __VA_ARGS__); \
83  } \
84  } while (0)
85 
86 #else // MATHLIB_DEBUGPRINT == 0
87 
88 #define MATHLIB_DEBUGPRINTFN(N, fmt, ...)
89 //#define MATHLIB_PRINTF(fmt, ...)
90 #endif // #if MATHLIB_DEBUGPRINT
91 
92 #else
93 
94 #if MATHLIB_DEBUGPRINT > 0
95 #define MATHLIB_DEBUGPRINTFN(N, fmt, ...) \
96  if (MATHLIB_DEBUGPRINT >= (N)) { \
97  fprintf(stderr, "MATHLIB debug %s - %d: " fmt, __FUNCTION__, __LINE__, \
98  __VA_ARGS__); \
99  } \
100 
101 #define MATHLIB_PRINTF(fmt, ...) \
102  fprintf(stderr, fmt, __VA_ARGS__)
103 
104 #else // MATHLIB_DEBUGPRINT == 0
105 
106 #define MATHLIB_DEBUGPRINTFN(N, fmt, ...)
107 #define MATHLIB_PRINTF(fmt, ...)
108 
109 #endif // #if MATHLIB_DEBUGPRINT
110 #endif // #if defined(EVM_TEST)
111 
112 #if defined(MATHLIB_MESSAGES)
113 // if enabled, display messages to the library user
114 // MATHLIB_MESSAGES should only be defined when TARGET_BUILD=debug or CHECKPARAMS=1
115 #define MATHLIB_MESG(fmt, ...) fprintf(stderr, fmt, __VA_ARGS__)
116 #else
117 #define MATHLIB_MESG(fmt, ...)
118 #endif //#if defined(MATHLIB_MESSAGES)
119 
120 /* Original implementation that had the benefit of always being compiled and thus
121  receiving error checking. However, the fprintf generates a MISRA-C violation.
122 #define MATHLIB_DEBUGPRINTFN(N, fmt, ...) \
123  do { \
124  if (MATHLIB_DEBUGPRINT >= (N)) { \
125  fprintf(stderr, "MATHLIB debug %s - %d: " fmt, __FUNCTION__, __LINE__, \
126  __VA_ARGS__); \
127  } \
128  } while (0)
129 
130 #define MATHLIB_PRINTF(fmt, ...) \
131  do { \
132  fprintf(stderr, fmt, __VA_ARGS__); \
133  } while (0)
134 #endif
135  */
136 
137 
138 #define MATHLIB_SOFT_MMA_RESET 0
139 #define RTS 1
140 
141 // switch to enable or disable static inline for MATHLIB functions defined in .c files (so not many functions)
142 #define MATHLIB_STATIC_INLINE static inline
143 
144 
145 #ifdef __cplusplus
146 #include <c7x_scalable.h> // for device scalability
147 
148 extern const c7x::uchar_vec MATHLIB_zeroVect_uchar_vec;
149 extern const c7x::ushort_vec MATHLIB_zeroVect_ushort_vec;
150 extern const c7x::uchar_vec MATHLIB_vperm_data_0_63;
151 
152 extern "C" {
153 #endif /* __cplusplus */
154 
162 /* @{ */
163 
164 /*******************************************************************************
165  *
166  * TYPEDEFS
167  *
168  ******************************************************************************/
169 
170 typedef double MATHLIB_D64;
171 typedef float MATHLIB_F32;
172 
174 typedef enum {
190 
192 
193 typedef void *MATHLIB_kernelHandle;
194 
196 typedef enum {
202 
205 typedef enum {
210 } MATHLIB_buffer_mode_e; // formerly eMMABufferMode;
211 
212 
213 
214 /*******************************************************************************
215  *
216  * DEFINES
217  *
218  ******************************************************************************/
219 
220 #define MATHLIB_PARAM_SIZE 128
221 //#define MATHLIB_BYTE_WIDTH 64
222 #define MATHLIB_MMA_ACCUMULATOR_BYTE_WIDTH_8_BIT 4
223 #define MATHLIB_MMA_ACCUMULATOR_BYTE_WIDTH_16_BIT 8
224 #define MATHLIB_MMA_ACCUMULATOR_BYTE_WIDTH_32_BIT 16
225 
226 
227 /*******************************************************************************
228  *
229  * Macros
230  *
231  ******************************************************************************/
232 
233 #define MATHLIB_ALIGN_SHIFT_64BYTES \
234  6
235 #define MATHLIB_ALIGN_SHIFT_128BYTES \
236  7
237 #define MATHLIB_ALIGN_SHIFT_256BYTES \
238  8
239 
240 #define MATHLIB_ALIGN_64BYTES \
241  (1 << MATHLIB_ALIGN_SHIFT_64BYTES)
242 #define MATHLIB_ALIGN_128BYTES \
243  (1 << MATHLIB_ALIGN_SHIFT_128BYTES)
244 #define MATHLIB_ALIGN_256BYTES \
245  (1 << MATHLIB_ALIGN_SHIFT_256BYTES)
246 
247 #define MATHLIB_L2DATA_ALIGN_SHIFT \
248  MATHLIB_ALIGN_SHIFT_64BYTES
249 
250 
252 #define MATHLIB_L2DATA_ALIGNMENT (((uint32_t)1) << ((uint32_t)MATHLIB_L2DATA_ALIGN_SHIFT))
253 
254 /* ---------------------------------------------------------------- */
255 /* MISRAC Rule 4.9(DEFINE.FUNC) Deviation: The advisory is not */
256 /* being addressed */
257 /* ---------------------------------------------------------------- */
260 /* @} */
261 
262 /******************************************************************************
263  *
264  * Do not document these in the User Guide
265  ******************************************************************************/
266 
270 /* @{ */
271 
272 
273 /* ---------------------------------------------------------------- */
274 /* MISRAC Rule 4.9(DEFINE.FUNC) Deviation: The advisory is not */
275 /* being addressed */
276 /* ---------------------------------------------------------------- */
277 // remove asm comments for Loki testing as they may add cycles
278 #if defined(QT_TEST) || defined(RTL_TEST) || defined(EVM_TEST)
279  #if defined(_HOST_BUILD)
280  #define MATHLIB_asm(string) ;
281  #else
282  #define MATHLIB_asm(string); asm(string);
283  #endif // _HOST_BUILD
284 #else
285  #define MATHLIB_asm(string) ;
286 #endif
287 
288 /* ---------------------------------------------------------------- */
289 /* MISRAC Rule 4.9(DEFINE.FUNC) Deviation: The advisory is not */
290 /* being addressed so as not to lose portability across different */
291 /* platforms. */
292 /* ---------------------------------------------------------------- */
293 // cl7x unroll attributes not recognized by gcc/gpp compiler and generate
294 // warnings. Remove them with macro
295 #if defined(_HOST_BUILD)
296 #define str(x) #x
297 #define xstr(x) str(x)
298 #ifdef WIN32
299 #define __attribute__()
300 #define MATHLIB_UNROLL(count)
301 #else
302 #define MATHLIB_UNROLL(count) _Pragma(str(UNROLL(count)))
303 #endif
304 #else
305 #define MATHLIB_UNROLL(COUNT) [[TI::unroll(COUNT)]]
306 #endif
307 
308 #if defined(_HOST_BUILD)
309 #ifdef WIN32
310 #define __attribute__()
311 #define MATHLIB_MUST_ITERATE(initial, max, multiple)
312 #else
313 #define MATHLIB_MUST_ITERATE(initial, max, multiple) _Pragma(str(MUST_ITERATE(initial, max, multiple)))
314 #endif
315 #else
316 #define MATHLIB_MUST_ITERATE(INTIAL, MAX, MULTIPLE) [[TI::must_iterate(INTIAL, MAX, MULTIPLE)]]
317 #endif
318 
319 #ifndef MATHLIB_NUMBER_RANDOM_DIM_TESTS
320 #define MATHLIB_NUMBER_RANDOM_DIM_TESTS 25
321 #endif
322 
323 #if defined(PERFORMANCE_TEST)
324 #define MATHLIB_PERFORMANCE_TEST_PATTERN SEQUENTIAL
325 #else
326 #define MATHLIB_PERFORMANCE_TEST_PATTERN RANDOM_SIGNED
327 #endif
328 
329 #define MATHLIB_TEST_OUTPUT_HEAP 0
330 #if defined(_HOST_BUILD)
331  // Valgrind works better when output is in the heap (it can't track statically allocated memory), so
332  // in host emulation mode, place test outputs in the heap rather than statically allocated MSMC
333  #define MATHLIB_TEST_OUTPUT_MSMC MATHLIB_TEST_OUTPUT_HEAP
334 #else
335  #define MATHLIB_TEST_OUTPUT_MSMC 1
336 #endif
337 
338 #define STRIDE_OPT_4CYCLE 1
339 
340 /*******************************************************************************
341  *
342  * MACROS
343  *
344  ******************************************************************************/
345 
346 /* ---------------------------------------------------------------- */
347 /* MISRAC Rule 4.9(DEFINE.FUNC) Deviation: The following */
348 /* function-like macros are intended to be used across different */
349 /* data types. */
350 /* ---------------------------------------------------------------- */
351 #define MATHLIB_min(x, y) \
352  (((x) < (y)) ? (x) : (y))
353 #define MATHLIB_max(x, y) \
354  (((x) < (y)) ? (y) : (x))
355 #define MATHLIB_ceilingDiv(x, y) \
356  (((x) + (y)-1) / \
357  (y))
360 /* @} */
361 
363 #ifdef __cplusplus
364 }
365 #endif /* __cplusplus */
366 #endif /* COMMON_MATHLIB_TYPES_H_ */
367 
368 /* ======================================================================== */
369 /* End of file: MATHLIB_types.h */
370 /* ======================================================================== */
Circular buffer for coefficients.
float MATHLIB_F32
Single precision floating point.
double MATHLIB_D64
Double precision floating point.
streaming engine
Linear buffer.
MATHLIB_FUNCTION_STYLE
Enumeration for the style of function implementation.
MATHLIB_STATUS_NAME
The enumeration of all status codes.
MATHLIB_STATUS_NAME MATHLIB_STATUS
Return value for MATHLIB functions.
MATHLIB_buffer_mode_e
Buffer structure for input feature maps and coefficients of MATHLIB_CNN_convolve_row_ixX_ixX_oxX.
void * MATHLIB_kernelHandle
Handle type for MATHLIB operations.