1 /*
2 *	Copyright (C) 2010,2019 Thorsten Liebig (Thorsten.Liebig@gmx.de)
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 3 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 this program.  If not, see <http://www.gnu.org/licenses/>.
16 */
17 
18 #ifndef ARRAY_OPS_H
19 #define ARRAY_OPS_H
20 
21 #ifdef __SIZEOF_FLOAT__
22 #if __SIZEOF_FLOAT__ != 4
23 #error wrong size of float
24 #endif
25 #endif
26 
27 #include <stdio.h>
28 #include <stdlib.h>
29 #include <iostream>
30 #include <string>
31 #include <math.h>
32 #include "constants.h"
33 
34 #define F4VECTOR_SIZE 16 // sizeof(typeid(f4vector))
35 
36 #ifdef __GNUC__ // GCC
37 typedef float v4sf __attribute__ ((vector_size (F4VECTOR_SIZE))); // vector of four single floats
38 union f4vector
39 {
40 	v4sf v;
41 	float f[4];
42 };
43 #else // MSVC
44 #include <emmintrin.h>
45 union f4vector
46 {
47 	__m128 v;
48 	float f[4];
49 };
50 inline __m128 operator + (__m128 a, __m128 b) {return _mm_add_ps(a, b);}
51 inline __m128 operator - (__m128 a, __m128 b) {return _mm_sub_ps(a, b);}
52 inline __m128 operator * (__m128 a, __m128 b) {return _mm_mul_ps(a, b);}
53 inline __m128 operator / (__m128 a, __m128 b) {return _mm_div_ps(a, b);}
54 
55 inline __m128 & operator += (__m128 & a, __m128 b){a = a + b; return a;}
56 inline __m128 & operator -= (__m128 & a, __m128 b){a = a - b; return a;}
57 inline __m128 & operator *= (__m128 & a, __m128 b){a = a * b; return a;}
58 inline __m128 & operator /= (__m128 & a, __m128 b){a = a / b; return a;}
59 #endif
60 
61 void Delete1DArray_v4sf(f4vector* array);
62 void Delete3DArray_v4sf(f4vector*** array, const unsigned int* numLines);
63 void Delete_N_3DArray_v4sf(f4vector**** array, const unsigned int* numLines);
64 f4vector* Create1DArray_v4sf(const unsigned int numLines);
65 f4vector*** Create3DArray_v4sf(const unsigned int* numLines);
66 f4vector**** Create_N_3DArray_v4sf(const unsigned int* numLines);
67 
68 // *************************************************************************************
69 // templates
70 // *************************************************************************************
71 template <typename T>
Create2DArray(const unsigned int * numLines)72 T** Create2DArray(const unsigned int* numLines)
73 {
74 	T** array=NULL;
75 	unsigned int pos[3];
76 	array = new T*[numLines[0]];
77 	for (pos[0]=0; pos[0]<numLines[0]; ++pos[0])
78 	{
79 		array[pos[0]] = new T[numLines[1]];
80 		for (pos[1]=0; pos[1]<numLines[1]; ++pos[1])
81 		{
82 			array[pos[0]][pos[1]] = 0;
83 		}
84 	}
85 	return array;
86 }
87 
88 template <typename T>
Delete2DArray(T ** array,const unsigned int * numLines)89 void Delete2DArray(T** array, const unsigned int* numLines)
90 {
91 	if (array==NULL) return;
92 	unsigned int pos[3];
93 	for (pos[0]=0; pos[0]<numLines[0]; ++pos[0])
94 	{
95 		delete[] array[pos[0]];
96 	}
97 	delete[] array;
98 }
99 
100 template <typename T>
Access_N_3DArray(T **** array,unsigned int n,unsigned int * pos)101 inline T& Access_N_3DArray(T**** array, unsigned int n, unsigned int* pos)
102 {
103 	return array[n][pos[0]][pos[1]][pos[2]];
104 }
105 
106 template <typename T>
Access_N_3DArray(T **** array,unsigned int n,unsigned int x,unsigned int y,unsigned int z)107 inline T& Access_N_3DArray(T**** array, unsigned int n, unsigned int x, unsigned int y, unsigned int z )
108 {
109 	return array[n][x][y][z];
110 }
111 
112 template <typename T>
Create3DArray(const unsigned int * numLines)113 T*** Create3DArray(const unsigned int* numLines)
114 {
115 	T*** array=NULL;
116 	unsigned int pos[3];
117 	array = new T**[numLines[0]];
118 	for (pos[0]=0; pos[0]<numLines[0]; ++pos[0])
119 	{
120 		array[pos[0]] = new T*[numLines[1]];
121 		for (pos[1]=0; pos[1]<numLines[1]; ++pos[1])
122 		{
123 			array[pos[0]][pos[1]] = new T[numLines[2]];
124 			for (pos[2]=0; pos[2]<numLines[2]; ++pos[2])
125 			{
126 				array[pos[0]][pos[1]][pos[2]] = 0;
127 			}
128 		}
129 	}
130 	return array;
131 }
132 
133 template <typename T>
Copy3DArray(T *** array_in,T *** array_out,const unsigned int * numLines)134 T*** Copy3DArray(T*** array_in, T*** array_out, const unsigned int* numLines)
135 {
136 	if (array_out==NULL)
137 		array_out = Create3DArray<T>(numLines);
138 	unsigned int pos[3];
139 	for (pos[0]=0; pos[0]<numLines[0]; ++pos[0])
140 		for (pos[1]=0; pos[1]<numLines[1]; ++pos[1])
141 			for (pos[2]=0; pos[2]<numLines[2]; ++pos[2])
142 				array_out[pos[0]][pos[1]][pos[2]] = array_in[pos[0]][pos[1]][pos[2]];
143 	return array_out;
144 }
145 
146 template <typename T>
Create_N_3DArray(const unsigned int * numLines)147 T**** Create_N_3DArray(const unsigned int* numLines)
148 {
149 	T**** array=NULL;
150 	array = new T***[3];
151 	for (int n=0; n<3; ++n)
152 	{
153 		array[n]=Create3DArray<T>( numLines );
154 	}
155 	return array;
156 }
157 
158 template <typename T>
Copy_N_3DArray(T **** array_in,T **** array_out,const unsigned int * numLines)159 T**** Copy_N_3DArray(T**** array_in, T**** array_out, const unsigned int* numLines)
160 {
161 	if (array_out==NULL)
162 		array_out = Create_N_3DArray<T>(numLines);
163 	for (int n=0; n<3; ++n)
164 		array_out[n]=Copy3DArray<T>( array_in[n], array_out[n], numLines);
165 	return array_out;
166 }
167 
168 template <typename T>
Delete3DArray(T *** array,const unsigned int * numLines)169 void Delete3DArray(T*** array, const unsigned int* numLines)
170 {
171 	if (!array) return;
172 	unsigned int pos[3];
173 	for (pos[0]=0; pos[0]<numLines[0]; ++pos[0])
174 	{
175 		for (pos[1]=0; pos[1]<numLines[1]; ++pos[1])
176 		{
177 			delete[] array[pos[0]][pos[1]];
178 		}
179 		delete[] array[pos[0]];
180 	}
181 	delete[] array;
182 }
183 
184 template <typename T>
Delete_N_3DArray(T **** array,const unsigned int * numLines)185 void Delete_N_3DArray(T**** array, const unsigned int* numLines)
186 {
187 	if (!array) return;
188 	for (int n=0; n<3; ++n)
189 	{
190 		Delete3DArray<T>(array[n],numLines);
191 	}
192 	delete[] array;
193 }
194 
195 template <typename T>
Dump_N_3DArray2File(std::ostream & file,T **** array,const unsigned int * numLines)196 void Dump_N_3DArray2File(std::ostream &file, T**** array, const unsigned int* numLines)
197 {
198 	unsigned int pos[3];
199 	for (pos[0]=0; pos[0]<numLines[0]; ++pos[0])
200 	{
201 		for (pos[1]=0; pos[1]<numLines[1]; ++pos[1])
202 		{
203 			for (pos[2]=0; pos[2]<numLines[2]; ++pos[2])
204 			{
205 				file << pos[0] << "\t" << pos[1] << "\t" << pos[2];
206 				for (int n=0; n<3; ++n)
207 					file << "\t" << (float)array[n][pos[0]][pos[1]][pos[2]];
208 				file << std::endl;
209 			}
210 		}
211 	}
212 }
213 
214 #endif // ARRAY_OPS_H
215