Menu

[r142]: / trunk / LWJGL / src / native / win32 / MatrixOpCommon.cpp  Maximize  Restore  History

Download this file

333 lines (279 with data), 9.0 kB

  1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
//#include <iostream>
#include <jni.h>
#include "MatrixOpCommon.h"
bool Matrix::identicalDataSpaces(Matrix & other)
{
if (address != other.address)
return JNI_FALSE;
if (stride != other.stride)
return JNI_FALSE;
if ((width * height) != (other.width * other.height))
return JNI_FALSE;
return JNI_TRUE;
}
bool Matrix::intersectingDataSpaces(Matrix & other)
{
char * my_max_address = &address[ stride * elements ];
char * other_max_address = &other.address[ other.stride * other.elements];
if (address >= other.address || address <= other_max_address) return JNI_TRUE;
if (other.address >= address || other.address <= my_max_address) return JNI_TRUE;
return JNI_FALSE;
}
void Matrix::transposeMatrix(float * src, float * dst, int src_width, int src_height)
{
// square matrix transpose
if (src_width == src_height)
{
for (int i = 0; i < src_width; i++)
for (int j = 0; j < src_width; j++)
dst[i + src_width * j] = src[j + i * src_width];
}
// non square matrix transpose
else
{
for (int i = 0; i < src_width; i ++)
for (int j = 0; j < src_height; j++)
dst[i + src_height * j] = src[j + i * src_height];
}
}
void Matrix::transposeMatrix(float * mat, int src_width, int src_height)
{
float temp;
// square matrix transpose
if (src_width == src_height)
{
for (int col = 0; col < src_width; col++)
{
for (int row = col+1; row < src_height; row++)
{
// swap the two elements
temp = mat [col * src_height + row];
mat[col * src_height + row] = mat[row * src_width + col];
mat[row * src_width + col] = temp;
}
}
}
// non square matrix transpose
else
{
transposeMatrix(mat, transpose_record, src_width, src_height);
memcpy(mat, transpose_record, src_width * src_height * sizeof(float));
}
}
SrcMatrix::SrcMatrix ( jint addr, jint s,
jint w, jint h,
jint e, jboolean t):
Matrix(addr, s, e),
record_offset((char *) addr),
record_size (w*h)
{
if (t) {
width = h;
height = w;
}
else {
width = w;
height = h;
}
elements = e;
record = new float[width * height];
// vectors do not need to be transposed
transpose = (t == JNI_TRUE)
&& (w != 1)
&& (h != 1);
if (transpose && (width != height))
// only need temp storage for transpose if the matrix is not square
transpose_record = new float[width*height];
else
transpose_record = 0;
if (elements == 1)
{
// fool the nextRecord function into returning a value
elements = 2;
nextRecord();
elements = 1;
}
}
SrcMatrix::~SrcMatrix()
{
//cout << "SrcMatrix destructor \n";
delete [] record;
if (transpose_record != 0)
delete [] transpose_record;
}
float * SrcMatrix::nextRecord()
{
if (elements > 1)
{
//cout << "Elements: " << elements << "\n";
//cout << "Address: " << (unsigned int) (record_offset) << "\n";
// the record is not properly aligned
if ((unsigned int) (record_offset) & FLOAT_ALIGNMENT)
{
// copy the floats into a buffer so that they are aligned
// on 4 byte margins (not necessary on intel, but a good thing)
memcpy (record, record_offset, record_size * sizeof(float));
if (transpose)
transposeMatrix (record, height, width);
record_offset = &record_offset[stride];
current_record_ptr = record;
}
// the record is aligned but it has to be transposed
else if (transpose)
{
transposeMatrix ((float *) (record_offset), record, height, width);
record_offset = &record_offset[stride];
current_record_ptr = record;
}
// nothing has to be done to the record
else
{
// the floats are aligned in memory
current_record_ptr = (float *) record_offset;
record_offset = &record_offset[stride];
}
}
return current_record_ptr;
}
DstMatrix::DstMatrix (jint addr, jint s, jint w, jint h, jint e, jboolean t):
Matrix(addr, s, e)
{
width = w;
height = h;
record_size = width * height;
record = new float[record_size];
// vectors do not need to be transposed
transpose = (t) && (w != 1) && (h != 1);
if (transpose)
transpose_record = new float[width*height];
else
transpose_record = 0;
data_buffered = JNI_FALSE;
record_buffered = JNI_FALSE;
record_offset = address - stride;
}
DstMatrix::~DstMatrix()
{
//cout << "DstMatrix destructor \n";
delete [] record;
if (transpose_record != 0)
delete [] transpose_record;
// copy back any buffered data
if (data_buffered)
{
char * src = buffer;
char * dest = address;
for (int i = 0; i < elements; i++)
{
memcpy(dest, src, record_size * sizeof(float));
src += stride;
dest += stride;
}
delete [] buffer;
}
}
void DstMatrix::configureBuffer(SrcMatrix & a, SrcMatrix & b)
{
if (!a.intersectingDataSpaces(b))
{
// as long as the output only overlays 1 of the sources, and the other
// source only has 1 matrix in it, only a record_buffer is required
if (a.elements == 1 && identicalDataSpaces(b))
record_buffered = JNI_TRUE;
else if (b.elements == 1 && identicalDataSpaces(a))
record_buffered = JNI_TRUE;
else
// otherwise all of the output has to be buffered
createBuffer();
}
else
createBuffer();
}
void DstMatrix::configureBuffer(SrcMatrix & a)
{
if (identicalDataSpaces(a))
record_buffered = JNI_TRUE;
else if (intersectingDataSpaces(a))
createBuffer();
}
void DstMatrix::createBuffer()
{
data_buffered = JNI_TRUE;
buffer = new char[ elements * stride ];
record_offset = buffer - stride;
}
float * DstMatrix::nextRecord()
{
record_offset = &record_offset[stride];
if ((((unsigned int)(record_offset)) & FLOAT_ALIGNMENT) || transpose || record_buffered)
{
last_record_in_temp = JNI_TRUE;
return record;
}
else
{
last_record_in_temp = JNI_FALSE;
return (float *) record_offset;
}
}
void DstMatrix::writeRecord()
{
if (last_record_in_temp)
{
// 3 reasons why the record would be in temp
//
// 1. The record is not aligned
// 2. The result will need to be transposed
// 3. Direct Mode where result would overlay an operand
if (((unsigned int)(record_offset)) & FLOAT_ALIGNMENT)
{
if (transpose)
transposeMatrix(record, width, height);
memcpy (record, record_offset, record_size * sizeof(jfloat));
}
else if (transpose)
{
transposeMatrix(record, (float *) record_offset, width, height);
}
else
memcpy (record_offset, record, record_size * sizeof(jfloat));
}
}
///////////////////////////////////////////////////////////////////////////
void subMatrix (const float * src, int side, float * dst , int col_omit, int row_omit)
{
int index = 0;
for (int c = 0; c < side; c++)
{
if (c == col_omit) continue;
for (int r = 0; r < side; r++)
{
if (r == row_omit) continue;
dst[index++] = src[r + c * side];
}
}
}
float determinant (const float * matrix , int side)
{
// we are assuming for this case that the data is in column major format
float det = 0;
if (side == 2)
// your basic cross product
det = matrix[0] * matrix[3] - matrix[2] * matrix[1];
else
{
int temp_side = side - 1;
float temp_matrix [temp_side * temp_side];
float sign = 1;
for (int i = 0; i < side; i++)
{
// get a sub matrix by eliminating the ith row and the 0th column
subMatrix(matrix, side, temp_matrix, 0, i);
// add to the determinant sign * [a]i0 * [M]i0
det += sign * matrix[i] * determinant (temp_matrix, temp_side);
// alternate the sign
sign = (sign == 1) ? -1 : 1;
}
}
return det;
}
Want the latest updates on software, tech news, and AI?
Get latest updates about software, tech news, and AI from SourceForge directly in your inbox once a month.