LAL  7.5.0.1-b72065a
VectorMath.h
Go to the documentation of this file.
1 /*
2  * Copyright (C) 2015 Reinhard Prix, Karl Wette
3  *
4  * This program is free software; you can redistribute it and/or modify
5  * it under the terms of the GNU General Public License as published by
6  * the Free Software Foundation; either version 2 of the License, or
7  * (at your option) any later version.
8  *
9  * This program is distributed in the hope that it will be useful,
10  * but WITHOUT ANY WARRANTY; without even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12  * GNU General Public License for more details.
13  *
14  * You should have received a copy of the GNU General Public License
15  * along with with program; see the file COPYING. If not, write to the
16  * Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
17  * MA 02110-1301 USA
18  *
19  */
20 
21 #ifndef _VECTORMATH_H
22 #define _VECTORMATH_H
23 
24 #include <lal/LALStdlib.h>
25 
26 #ifdef __cplusplus
27 extern "C" {
28 #endif
29 
30 /**
31  * \defgroup VectorMath_h Header VectorMath.h
32  * \ingroup lal_vectorops
33  * \author Reinhard Prix, Karl Wette
34  *
35  * \brief Functions for performing fast math on vectors of numbers, using SIMD (SSE, AVX, ...) instructions if available.
36  *
37  * ### Synopsis ###
38  *
39  * \code
40  * #include <lal/VectorMath.h>
41  * \endcode
42  *
43  * ### Alignment ###
44  *
45  * Neither input nor output vectors are \b required to have any particular memory alignment. Nevertheless, performance
46  * \e may be improved if vectors are 16-byte aligned for SSE, and 32-byte aligned for AVX.
47  */
48 /** @{ */
49 
50 /** \name Failsafe Aligned Memory Handling */
51 /** @{ */
52 
53 /** A special \c UINT4Vector with n-byte aligned memory \c data array */
54 typedef struct tagUINT4VectorAligned {
55  UINT4 length; /**< number of 'usable' array entries (fully aligned) */
56  UINT4 *data; /**< start of aligned memory block */
57  UINT4 *data0; /**< actual physical start of memory block, possibly not aligned */
59 
60 /** Create a new \c UINT4VectorAligned struct with length \c length and alignment \c align */
62 
63 /** Resize an existing \c UINT4VectorAligned struct to length \c length and alignment \c align */
65 
66 /** Free a \c UINT4VectorAligned struct */
68 
69 /** A special \c REAL4Vector with n-byte aligned memory \c data array */
70 typedef struct tagREAL4VectorAligned {
71  UINT4 length; /**< number of 'usable' array entries (fully aligned) */
72  REAL4 *data; /**< start of aligned memory block */
73  REAL4 *data0; /**< actual physical start of memory block, possibly not aligned */
75 
76 /** Create a new \c REAL4VectorAligned struct with length \c length and alignment \c align */
78 
79 /** Resize an existing \c REAL4VectorAligned struct to length \c length and alignment \c align */
81 
82 /** Free a \c REAL4VectorAligned struct */
84 
85 /** A special \c REAL8Vector with n-byte aligned memory \c data array */
86 typedef struct tagREAL8VectorAligned {
87  UINT4 length; /**< number of 'usable' array entries (fully aligned) */
88  REAL8 *data; /**< start of aligned memory block */
89  REAL8 *data0; /**< actual physical start of memory block, possibly not aligned */
91 
92 /** Create a new \c REAL8VectorAligned struct with length \c length and alignment \c align */
94 
95 /** Resize an existing \c REAL8VectorAligned struct to length \c length and alignment \c align */
97 
98 /** Free a \c REAL8VectorAligned struct */
100 
101 /** A special \c COMPLEX8Vector with n-byte aligned memory \c data array */
102 typedef struct tagCOMPLEX8VectorAligned {
103  UINT4 length; /**< number of 'usable' array entries (fully aligned) */
104  COMPLEX8 *data; /**< start of aligned memory block */
105  COMPLEX8 *data0; /**< actual physical start of memory block, possibly not aligned */
107 
108 /** Create a new \c COMPLEX8VectorAligned struct with length \c length and alignment \c align */
110 
111 /** Resize an existing \c COMPLEX8VectorAligned struct to length \c length and alignment \c align */
113 
114 /** Free a \c COMPLEX8VectorAligned struct */
116 
117 /** A special \c COMPLEX16Vector with n-byte aligned memory \c data array */
118 typedef struct tagCOMPLEX16VectorAligned {
119  UINT4 length; /**< number of 'usable' array entries (fully aligned) */
120  COMPLEX16 *data; /**< start of aligned memory block */
121  COMPLEX16 *data0; /**< actual physical start of memory block, possibly not aligned */
123 
124 /** Create a new \c COMPLEX16VectorAligned struct with length \c length and alignment \c align */
126 
127 /** Resize an existing \c COMPLEX16VectorAligned struct to length \c length and alignment \c align */
129 
130 /** Free a \c COMPLEX16VectorAligned struct */
132 
133 /** @} */
134 
135 /** \name Vector Math Functions */
136 /** @{ */
137 
138 /** Cast (i.e. truncate) REAL4 vector \c in to INT4 vector \c out, with \c len elements. */
139 int XLALVectorINT4FromREAL4 ( INT4 *out, const REAL4 *in, const UINT4 len );
140 
141 /** Compute \f$\text{out} = \sin(\text{in})\f$ over REAL4 vectors \c out, \c in with \c len elements */
142 int XLALVectorSinREAL4 ( REAL4 *out, const REAL4 *in, const UINT4 len );
143 
144 /** Compute \f$\text{out} = \cos(\text{in})\f$ over REAL4 vectors \c out, \c in with \c len elements */
145 int XLALVectorCosREAL4 ( REAL4 *out, const REAL4 *in, const UINT4 len );
146 
147 /** Compute \f$\text{out} = \exp(\text{in})\f$ over REAL4 vectors \c out, \c in with \c len elements */
148 int XLALVectorExpREAL4 ( REAL4 *out, const REAL4 *in, const UINT4 len );
149 
150 /** Compute \f$\text{out} = \log(\text{in})\f$ over REAL4 vectors \c out, \c in with \c len elements */
151 int XLALVectorLogREAL4 ( REAL4 *out, const REAL4 *in, const UINT4 len );
152 
153 /** Compute \f$\text{out} = round ( \text{in} )\f$ over REAL4 vectors \c out, \c in with \c len elements */
154 int XLALVectorRoundREAL4 ( REAL4 *out, const REAL4 *in, const UINT4 len);
155 
156 /** Compute \f$\text{out} = round ( \text{in} )\f$ over REAL8 vectors \c out, \c in with \c len elements */
157 int XLALVectorRoundREAL8 ( REAL8 *out, const REAL8 *in, const UINT4 len);
158 
159 /** Compute \f$\text{out1} = \sin(\text{in}), \text{out2} = \cos(\text{in})\f$ over REAL4 vectors \c out1, \c out2, \c in with \c len elements */
160 int XLALVectorSinCosREAL4 ( REAL4 *out1, REAL4 *out2, const REAL4 *in, const UINT4 len );
161 
162 /** Compute \f$\text{out1} = \sin(2\pi \text{in}), \text{out2} = \cos(2\pi \text{in})\f$ over REAL4 vectors \c out1, \c out2, \c in with \c len elements */
163 int XLALVectorSinCos2PiREAL4 ( REAL4 *out1, REAL4 *out2, const REAL4 *in, const UINT4 len );
164 
165 /** @} */
166 
167 /** \name Vector by Vector Operations */
168 /** @{ */
169 
170 /** Compute \f$\text{out} = \text{in1} + \text{in2}\f$ over REAL4 vectors \c in1 and \c in2 with \c len elements */
171 int XLALVectorAddREAL4 ( REAL4 *out, const REAL4 *in1, const REAL4 *in2, const UINT4 len);
172 
173 /** Compute \f$\text{out} = \text{in1} - \text{in2}\f$ over REAL4 vectors \c in1 and \c in2 with \c len elements */
174 int XLALVectorSubREAL4 ( REAL4 *out, const REAL4 *in1, const REAL4 *in2, const UINT4 len);
175 
176 /** Compute \f$\text{out} = \text{in1} \times \text{in2}\f$ over REAL4 vectors \c in1 and \c in2 with \c len elements */
177 int XLALVectorMultiplyREAL4 ( REAL4 *out, const REAL4 *in1, const REAL4 *in2, const UINT4 len);
178 
179 /** Compute \f$\text{out} = max ( \text{in1}, \text{in2} )\f$ over REAL4 vectors \c in1 and \c in2 with \c len elements */
180 int XLALVectorMaxREAL4 ( REAL4 *out, const REAL4 *in1, const REAL4 *in2, const UINT4 len);
181 
182 /** Compute \f$\text{out} = \text{in1} + \text{in2}\f$ over REAL8 vectors \c in1 and \c in2 with \c len elements */
183 int XLALVectorAddREAL8 ( REAL8 *out, const REAL8 *in1, const REAL8 *in2, const UINT4 len);
184 
185 /** Compute \f$\text{out} = \text{in1} - \text{in2}\f$ over REAL8 vectors \c in1 and \c in2 with \c len elements */
186 int XLALVectorSubREAL8 ( REAL8 *out, const REAL8 *in1, const REAL8 *in2, const UINT4 len);
187 
188 /** Compute \f$\text{out} = \text{in1} \times \text{in2}\f$ over REAL8 vectors \c in1 and \c in2 with \c len elements */
189 int XLALVectorMultiplyREAL8 ( REAL8 *out, const REAL8 *in1, const REAL8 *in2, const UINT4 len);
190 
191 /** Compute \f$\text{out} = max ( \text{in1}, \text{in2} )\f$ over REAL8 vectors \c in1 and \c in2 with \c len elements */
192 int XLALVectorMaxREAL8 ( REAL8 *out, const REAL8 *in1, const REAL8 *in2, const UINT4 len);
193 
194 /** Compute \f$\text{out} = \text{in1} \times \text{in2}\f$ over COMPLEX8 vectors \c in1 and \c in2 with \c len elements */
195 int XLALVectorMultiplyCOMPLEX8 ( COMPLEX8 *out, const COMPLEX8 *in1, const COMPLEX8 *in2, const UINT4 len );
196 
197 /** Compute \f$\text{out} = \text{in1} + \text{in2}\f$ over COMPLEX8 vectors \c in1 and \c in2 with \c len elements */
198 int XLALVectorAddCOMPLEX8 ( COMPLEX8 *out, const COMPLEX8 *in1, const COMPLEX8 *in2, const UINT4 len);
199 
200 /** @} */
201 
202 /** \name Vector by Scalar Operations */
203 /** @{ */
204 
205 /** Compute \f$\text{out} = \text{scalar} + \text{in}\f$ over REAL4 vector \c in with \c len elements */
206 int XLALVectorShiftREAL4 ( REAL4 *out, REAL4 scalar, const REAL4 *in, const UINT4 len);
207 
208 /** Compute \f$\text{out} = \text{scalar} \times \text{in}\f$ over REAL4 vector \c in with \c len elements */
209 int XLALVectorScaleREAL4 ( REAL4 *out, REAL4 scalar, const REAL4 *in, const UINT4 len);
210 
211 /** Compute \f$\text{out} = \text{scalar} \times \text{in}\f$ over REAL8 vector \c in with \c len elements */
212 int XLALVectorScaleREAL8 ( REAL8 *out, REAL8 scalar, const REAL8 *in, const UINT4 len);
213 
214 /** Compute \f$\text{out} = \text{scalar} + \text{in}\f$ over REAL8 vector \c in with \c len elements */
215 int XLALVectorShiftREAL8 ( REAL8 *out, REAL8 scalar, const REAL8 *in, const UINT4 len);
216 
217 /** Compute \f$\text{out} = \text{scalar} \times \text{in}\f$ over COMPLEX8 vector \c in with \c len elements */
218 int XLALVectorScaleCOMPLEX8 ( COMPLEX8 *out, COMPLEX8 scalar, const COMPLEX8 *in, const UINT4 len);
219 
220 /** Compute \f$\text{out} = \text{scalar} + \text{in}\f$ over COMPLEX8 vector \c in with \c len elements */
221 int XLALVectorShiftCOMPLEX8 ( COMPLEX8 *out, COMPLEX8 scalar, const COMPLEX8 *in, const UINT4 len);
222 
223 /** @} */
224 
225 /** \name Vector Element Finding Operations */
226 /** @{ */
227 
228 /** Count and return indexes (in \c count and \c out respectively) of vector elements where \f$\text{in1} \le \text{in2}\f$ */
229 int XLALVectorFindVectorLessEqualREAL4( UINT4* count, UINT4 *out, const REAL4 *in1, const REAL4 *in2, const UINT4 len );
230 
231 /** Count and return indexes (in \c count and \c out respectively) of vector elements where \f$\text{scalar} \le \text{in2}\f$ */
232 int XLALVectorFindScalarLessEqualREAL4( UINT4* count, UINT4 *out, REAL4 scalar, const REAL4 *in, const UINT4 len );
233 
234 /** @} */
235 
236 /** @} */
237 
238 #ifdef __cplusplus
239 }
240 #endif
241 
242 #endif /* _VECTORMATH_H */
double complex COMPLEX16
Double-precision floating-point complex number (16 bytes total)
double REAL8
Double precision real floating-point number (8 bytes).
uint32_t UINT4
Four-byte unsigned integer.
float complex COMPLEX8
Single-precision floating-point complex number (8 bytes total)
int32_t INT4
Four-byte signed integer.
float REAL4
Single precision real floating-point number (4 bytes).
COMPLEX8VectorAligned * XLALResizeCOMPLEX8VectorAligned(COMPLEX8VectorAligned *in, const UINT4 length, const UINT4 align)
Resize an existing COMPLEX8VectorAligned struct to length length and alignment align.
UINT4VectorAligned * XLALCreateUINT4VectorAligned(const UINT4 length, const UINT4 align)
Create a new UINT4VectorAligned struct with length length and alignment align.
int XLALVectorSinCosREAL4(REAL4 *out1, REAL4 *out2, const REAL4 *in, const UINT4 len)
Compute over REAL4 vectors out1, out2, in with len elements.
int XLALVectorScaleREAL4(REAL4 *out, REAL4 scalar, const REAL4 *in, const UINT4 len)
Compute over REAL4 vector in with len elements.
int XLALVectorLogREAL4(REAL4 *out, const REAL4 *in, const UINT4 len)
Compute over REAL4 vectors out, in with len elements.
REAL8VectorAligned * XLALResizeREAL8VectorAligned(REAL8VectorAligned *in, const UINT4 length, const UINT4 align)
Resize an existing REAL8VectorAligned struct to length length and alignment align.
void XLALDestroyREAL4VectorAligned(REAL4VectorAligned *in)
Free a REAL4VectorAligned struct.
int XLALVectorRoundREAL4(REAL4 *out, const REAL4 *in, const UINT4 len)
Compute over REAL4 vectors out, in with len elements.
int XLALVectorSubREAL4(REAL4 *out, const REAL4 *in1, const REAL4 *in2, const UINT4 len)
Compute over REAL4 vectors in1 and in2 with len elements.
int XLALVectorSubREAL8(REAL8 *out, const REAL8 *in1, const REAL8 *in2, const UINT4 len)
Compute over REAL8 vectors in1 and in2 with len elements.
COMPLEX8VectorAligned * XLALCreateCOMPLEX8VectorAligned(const UINT4 length, const UINT4 align)
Create a new COMPLEX8VectorAligned struct with length length and alignment align.
int XLALVectorMultiplyCOMPLEX8(COMPLEX8 *out, const COMPLEX8 *in1, const COMPLEX8 *in2, const UINT4 len)
Compute over COMPLEX8 vectors in1 and in2 with len elements.
int XLALVectorAddCOMPLEX8(COMPLEX8 *out, const COMPLEX8 *in1, const COMPLEX8 *in2, const UINT4 len)
Compute over COMPLEX8 vectors in1 and in2 with len elements.
int XLALVectorShiftCOMPLEX8(COMPLEX8 *out, COMPLEX8 scalar, const COMPLEX8 *in, const UINT4 len)
Compute over COMPLEX8 vector in with len elements.
void XLALDestroyREAL8VectorAligned(REAL8VectorAligned *in)
Free a REAL8VectorAligned struct.
int XLALVectorMaxREAL8(REAL8 *out, const REAL8 *in1, const REAL8 *in2, const UINT4 len)
Compute over REAL8 vectors in1 and in2 with len elements.
void XLALDestroyCOMPLEX16VectorAligned(COMPLEX16VectorAligned *in)
Free a COMPLEX16VectorAligned struct.
REAL8VectorAligned * XLALCreateREAL8VectorAligned(const UINT4 length, const UINT4 align)
Create a new REAL8VectorAligned struct with length length and alignment align.
int XLALVectorMultiplyREAL8(REAL8 *out, const REAL8 *in1, const REAL8 *in2, const UINT4 len)
Compute over REAL8 vectors in1 and in2 with len elements.
COMPLEX16VectorAligned * XLALResizeCOMPLEX16VectorAligned(COMPLEX16VectorAligned *in, const UINT4 length, const UINT4 align)
Resize an existing COMPLEX16VectorAligned struct to length length and alignment align.
REAL4VectorAligned * XLALResizeREAL4VectorAligned(REAL4VectorAligned *in, const UINT4 length, const UINT4 align)
Resize an existing REAL4VectorAligned struct to length length and alignment align.
int XLALVectorFindVectorLessEqualREAL4(UINT4 *count, UINT4 *out, const REAL4 *in1, const REAL4 *in2, const UINT4 len)
Count and return indexes (in count and out respectively) of vector elements where .
int XLALVectorINT4FromREAL4(INT4 *out, const REAL4 *in, const UINT4 len)
Cast (i.e.
UINT4VectorAligned * XLALResizeUINT4VectorAligned(UINT4VectorAligned *in, const UINT4 length, const UINT4 align)
Resize an existing UINT4VectorAligned struct to length length and alignment align.
int XLALVectorSinCos2PiREAL4(REAL4 *out1, REAL4 *out2, const REAL4 *in, const UINT4 len)
Compute over REAL4 vectors out1, out2, in with len elements.
void XLALDestroyUINT4VectorAligned(UINT4VectorAligned *in)
Free a UINT4VectorAligned struct.
REAL4VectorAligned * XLALCreateREAL4VectorAligned(const UINT4 length, const UINT4 align)
Create a new REAL4VectorAligned struct with length length and alignment align.
int XLALVectorShiftREAL8(REAL8 *out, REAL8 scalar, const REAL8 *in, const UINT4 len)
Compute over REAL8 vector in with len elements.
int XLALVectorMaxREAL4(REAL4 *out, const REAL4 *in1, const REAL4 *in2, const UINT4 len)
Compute over REAL4 vectors in1 and in2 with len elements.
int XLALVectorScaleREAL8(REAL8 *out, REAL8 scalar, const REAL8 *in, const UINT4 len)
Compute over REAL8 vector in with len elements.
int XLALVectorRoundREAL8(REAL8 *out, const REAL8 *in, const UINT4 len)
Compute over REAL8 vectors out, in with len elements.
int XLALVectorAddREAL4(REAL4 *out, const REAL4 *in1, const REAL4 *in2, const UINT4 len)
Compute over REAL4 vectors in1 and in2 with len elements.
int XLALVectorAddREAL8(REAL8 *out, const REAL8 *in1, const REAL8 *in2, const UINT4 len)
Compute over REAL8 vectors in1 and in2 with len elements.
int XLALVectorExpREAL4(REAL4 *out, const REAL4 *in, const UINT4 len)
Compute over REAL4 vectors out, in with len elements.
void XLALDestroyCOMPLEX8VectorAligned(COMPLEX8VectorAligned *in)
Free a COMPLEX8VectorAligned struct.
int XLALVectorMultiplyREAL4(REAL4 *out, const REAL4 *in1, const REAL4 *in2, const UINT4 len)
Compute over REAL4 vectors in1 and in2 with len elements.
COMPLEX16VectorAligned * XLALCreateCOMPLEX16VectorAligned(const UINT4 length, const UINT4 align)
Create a new COMPLEX16VectorAligned struct with length length and alignment align.
int XLALVectorCosREAL4(REAL4 *out, const REAL4 *in, const UINT4 len)
Compute over REAL4 vectors out, in with len elements.
int XLALVectorShiftREAL4(REAL4 *out, REAL4 scalar, const REAL4 *in, const UINT4 len)
Compute over REAL4 vector in with len elements.
int XLALVectorFindScalarLessEqualREAL4(UINT4 *count, UINT4 *out, REAL4 scalar, const REAL4 *in, const UINT4 len)
Count and return indexes (in count and out respectively) of vector elements where .
int XLALVectorSinREAL4(REAL4 *out, const REAL4 *in, const UINT4 len)
Compute over REAL4 vectors out, in with len elements.
int XLALVectorScaleCOMPLEX8(COMPLEX8 *out, COMPLEX8 scalar, const COMPLEX8 *in, const UINT4 len)
Compute over COMPLEX8 vector in with len elements.
A special COMPLEX16Vector with n-byte aligned memory data array.
Definition: VectorMath.h:118
COMPLEX16 * data
start of aligned memory block
Definition: VectorMath.h:120
UINT4 length
number of 'usable' array entries (fully aligned)
Definition: VectorMath.h:119
COMPLEX16 * data0
actual physical start of memory block, possibly not aligned
Definition: VectorMath.h:121
A special COMPLEX8Vector with n-byte aligned memory data array.
Definition: VectorMath.h:102
COMPLEX8 * data
start of aligned memory block
Definition: VectorMath.h:104
COMPLEX8 * data0
actual physical start of memory block, possibly not aligned
Definition: VectorMath.h:105
UINT4 length
number of 'usable' array entries (fully aligned)
Definition: VectorMath.h:103
A special REAL4Vector with n-byte aligned memory data array.
Definition: VectorMath.h:70
REAL4 * data0
actual physical start of memory block, possibly not aligned
Definition: VectorMath.h:73
UINT4 length
number of 'usable' array entries (fully aligned)
Definition: VectorMath.h:71
REAL4 * data
start of aligned memory block
Definition: VectorMath.h:72
A special REAL8Vector with n-byte aligned memory data array.
Definition: VectorMath.h:86
REAL8 * data
start of aligned memory block
Definition: VectorMath.h:88
REAL8 * data0
actual physical start of memory block, possibly not aligned
Definition: VectorMath.h:89
UINT4 length
number of 'usable' array entries (fully aligned)
Definition: VectorMath.h:87
A special UINT4Vector with n-byte aligned memory data array.
Definition: VectorMath.h:54
UINT4 * data0
actual physical start of memory block, possibly not aligned
Definition: VectorMath.h:57
UINT4 length
number of 'usable' array entries (fully aligned)
Definition: VectorMath.h:55
UINT4 * data
start of aligned memory block
Definition: VectorMath.h:56