1 /**************************************************************************************************
2 *                                                                                                 *
3 * This file is part of BLASFEO.                                                                   *
4 *                                                                                                 *
5 * BLASFEO -- BLAS For Embedded Optimization.                                                      *
6 * Copyright (C) 2019 by Gianluca Frison.                                                          *
7 * Developed at IMTEK (University of Freiburg) under the supervision of Moritz Diehl.              *
8 * All rights reserved.                                                                            *
9 *                                                                                                 *
10 * The 2-Clause BSD License                                                                        *
11 *                                                                                                 *
12 * Redistribution and use in source and binary forms, with or without                              *
13 * modification, are permitted provided that the following conditions are met:                     *
14 *                                                                                                 *
15 * 1. Redistributions of source code must retain the above copyright notice, this                  *
16 *    list of conditions and the following disclaimer.                                             *
17 * 2. Redistributions in binary form must reproduce the above copyright notice,                    *
18 *    this list of conditions and the following disclaimer in the documentation                    *
19 *    and/or other materials provided with the distribution.                                       *
20 *                                                                                                 *
21 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND                 *
22 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED                   *
23 * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE                          *
24 * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR                 *
25 * ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES                  *
26 * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;                    *
27 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND                     *
28 * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT                      *
29 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS                   *
30 * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.                                    *
31 *                                                                                                 *
32 * Author: Gianluca Frison, gianluca.frison (at) imtek.uni-freiburg.de                             *
33 *                                                                                                 *
34 **************************************************************************************************/
35 
36 #ifndef BLASFEO_COMMON_H_
37 #define BLASFEO_COMMON_H_
38 
39 
40 
41 #include "blasfeo_target.h"
42 
43 
44 
45 #ifdef __cplusplus
46 extern "C" {
47 #endif
48 
49 
50 
51 #if defined(__GNUC__) || defined(__clang__) || defined(__INTEL_COMPILER) || defined(__ICL) || defined(__ICC) || defined(__INTEL_LLVM_COMPILER)
52 #define ALIGNED(VEC, BYTES) VEC __attribute__ ((aligned ( BYTES )))
53 #elif defined (_MSC_VER)
54 #define ALIGNED(VEC, BYTES) __declspec(align( BYTES )) VEC
55 #else
56 #define ALIGNED(VEC, BYTES) VEC
57 #endif
58 
59 
60 
61 
62 #if defined(LA_HIGH_PERFORMANCE)
63 
64 #include "blasfeo_block_size.h"
65 
66 // matrix structure
67 struct blasfeo_dmat
68 	{
69 	int m; // rows
70 	int n; // cols
71 	int pm; // packed number or rows
72 	int cn; // packed number or cols
73 	double *pA; // pointer to a pm*pn array of doubles, the first is aligned to cache line size
74 	double *dA; // pointer to a min(m,n) (or max???) array of doubles
75 	int use_dA; // flag to tell if dA can be used
76 	int memsize; // size of needed memory
77 	};
78 
79 struct blasfeo_smat
80 	{
81 	int m; // rows
82 	int n; // cols
83 	int pm; // packed number or rows
84 	int cn; // packed number or cols
85 	float *pA; // pointer to a pm*pn array of floats, the first is aligned to cache line size
86 	float *dA; // pointer to a min(m,n) (or max???) array of floats
87 	int use_dA; // flag to tell if dA can be used
88 	int memsize; // size of needed memory
89 	};
90 
91 // vector structure
92 struct blasfeo_dvec
93 	{
94 	int m; // size
95 	int pm; // packed size
96 	double *pa; // pointer to a pm array of doubles, the first is aligned to cache line size
97 	int memsize; // size of needed memory
98 	};
99 
100 struct blasfeo_svec
101 	{
102 	int m; // size
103 	int pm; // packed size
104 	float *pa; // pointer to a pm array of floats, the first is aligned to cache line size
105 	int memsize; // size of needed memory
106 	};
107 
108 #define BLASFEO_DMATEL(sA,ai,aj) ((sA)->pA[((ai)-((ai)&(D_PS-1)))*(sA)->cn+(aj)*D_PS+((ai)&(D_PS-1))])
109 #define BLASFEO_SMATEL(sA,ai,aj) ((sA)->pA[((ai)-((ai)&(S_PS-1)))*(sA)->cn+(aj)*S_PS+((ai)&(S_PS-1))])
110 #define BLASFEO_DVECEL(sa,ai) ((sa)->pa[ai])
111 #define BLASFEO_SVECEL(sa,ai) ((sa)->pa[ai])
112 
113 #elif defined(LA_EXTERNAL_BLAS_WRAPPER) | defined(LA_REFERENCE)
114 
115 // matrix structure
116 struct blasfeo_dmat
117 	{
118 	int m; // rows
119 	int n; // cols
120 	double *pA; // pointer to a m*n array of doubles
121 	double *dA; // pointer to a min(m,n) (or max???) array of doubles
122 	int use_dA; // flag to tell if dA can be used
123 	int memsize; // size of needed memory
124 	};
125 
126 struct blasfeo_smat
127 	{
128 	int m; // rows
129 	int n; // cols
130 	float *pA; // pointer to a m*n array of floats
131 	float *dA; // pointer to a min(m,n) (or max???) array of floats
132 	int use_dA; // flag to tell if dA can be used
133 	int memsize; // size of needed memory
134 	};
135 
136 // vector structure
137 struct blasfeo_dvec
138 	{
139 	int m; // size
140 	double *pa; // pointer to a m array of doubles, the first is aligned to cache line size
141 	int memsize; // size of needed memory
142 	};
143 
144 struct blasfeo_svec
145 	{
146 	int m; // size
147 	float *pa; // pointer to a m array of floats, the first is aligned to cache line size
148 	int memsize; // size of needed memory
149 	};
150 
151 #define BLASFEO_DMATEL(sA,ai,aj) ((sA)->pA[(ai)+(aj)*(sA)->m])
152 #define BLASFEO_SMATEL(sA,ai,aj) ((sA)->pA[(ai)+(aj)*(sA)->m])
153 #define BLASFEO_DVECEL(sa,ai) ((sa)->pa[ai])
154 #define BLASFEO_SVECEL(sa,ai) ((sa)->pa[ai])
155 
156 #else
157 
158 #error : wrong LA choice
159 
160 #endif
161 
162 
163 
164 #if defined(TESTING_MODE)
165 
166 // matrix structure
167 struct blasfeo_dmat_ref
168 	{
169 	int m; // rows
170 	int n; // cols
171 	double *pA; // pointer to a m*n array of doubles
172 	double *dA; // pointer to a min(m,n) (or max???) array of doubles
173 	int use_dA; // flag to tell if dA can be used
174 	int memsize; // size of needed memory
175 	};
176 
177 struct blasfeo_smat_ref
178 	{
179 	int m; // rows
180 	int n; // cols
181 	float *pA; // pointer to a m*n array of floats
182 	float *dA; // pointer to a min(m,n) (or max???) array of floats
183 	int use_dA; // flag to tell if dA can be used
184 	int memsize; // size of needed memory
185 	};
186 
187 // vector structure
188 struct blasfeo_dvec_ref
189 	{
190 	int m; // size
191 	double *pa; // pointer to a m array of doubles, the first is aligned to cache line size
192 	int memsize; // size of needed memory
193 	};
194 
195 struct blasfeo_svec_ref
196 	{
197 	int m; // size
198 	float *pa; // pointer to a m array of floats, the first is aligned to cache line size
199 	int memsize; // size of needed memory
200 	};
201 
202 #define MATEL_REF(sA,ai,aj) ((sA)->pA[(ai)+(aj)*(sA)->m])
203 #define VECEL_REF(sa,ai) ((sa)->pa[ai])
204 
205 #endif // TESTING_MODE
206 
207 #ifdef __cplusplus
208 }
209 #endif
210 
211 #endif  // BLASFEO_COMMON_H_
212