fixed a CI mode segfault, minor clean-ups
[mesa.git] / src / mesa / math / m_vector.c
1 /* $Id: m_vector.c,v 1.3 2001/01/24 00:04:59 brianp Exp $ */
2
3 /*
4 * Mesa 3-D graphics library
5 * Version: 3.5
6 *
7 * Copyright (C) 1999-2001 Brian Paul All Rights Reserved.
8 *
9 * Permission is hereby granted, free of charge, to any person obtaining a
10 * copy of this software and associated documentation files (the "Software"),
11 * to deal in the Software without restriction, including without limitation
12 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
13 * and/or sell copies of the Software, and to permit persons to whom the
14 * Software is furnished to do so, subject to the following conditions:
15 *
16 * The above copyright notice and this permission notice shall be included
17 * in all copies or substantial portions of the Software.
18 *
19 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
20 * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
21 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
22 * BRIAN PAUL BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN
23 * AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
24 * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
25 */
26
27 /*
28 * New (3.1) transformation code written by Keith Whitwell.
29 */
30
31
32 #include "glheader.h"
33 #include "macros.h"
34 #include "mem.h"
35
36 #include "m_vector.h"
37
38
39
40 /*
41 * Given a vector [count][4] of floats, set all the [][elt] values
42 * to 0 (if elt = 0, 1, 2) or 1.0 (if elt = 3).
43 */
44 void gl_vector4f_clean_elem( GLvector4f *vec, GLuint count, GLuint elt )
45 {
46 static const GLubyte elem_bits[4] = {
47 VEC_DIRTY_0,
48 VEC_DIRTY_1,
49 VEC_DIRTY_2,
50 VEC_DIRTY_3
51 };
52 static const GLfloat clean[4] = { 0, 0, 0, 1 };
53 const GLfloat v = clean[elt];
54 GLfloat (*data)[4] = (GLfloat (*)[4])vec->start;
55 GLuint i;
56
57 for (i = 0 ; i < count ; i++)
58 data[i][elt] = v;
59
60 vec->flags &= ~elem_bits[elt];
61 }
62
63 static const GLubyte size_bits[5] = {
64 0,
65 VEC_SIZE_1,
66 VEC_SIZE_2,
67 VEC_SIZE_3,
68 VEC_SIZE_4,
69 };
70
71
72
73 /*
74 * Initialize GLvector objects.
75 * Input: v - the vector object to initialize.
76 * flags - bitwise-OR of VEC_* flags
77 * storage - pointer to storage for the vector's data
78 */
79
80
81 void gl_vector4f_init( GLvector4f *v, GLuint flags, GLfloat (*storage)[4] )
82 {
83 v->stride = 4 * sizeof(GLfloat);
84 v->size = 2; /* may change: 2-4 for vertices and 1-4 for texcoords */
85 v->data = storage;
86 v->start = (GLfloat *) storage;
87 v->count = 0;
88 v->flags = size_bits[4] | flags ;
89 }
90
91 void gl_vector3f_init( GLvector3f *v, GLuint flags, GLfloat (*storage)[3] )
92 {
93 v->stride = 3 * sizeof(GLfloat);
94 v->data = storage;
95 v->start = (GLfloat *) storage;
96 v->count = 0;
97 v->flags = flags ;
98 }
99
100 void gl_vector1f_init( GLvector1f *v, GLuint flags, GLfloat *storage )
101 {
102 v->stride = 1*sizeof(GLfloat);
103 v->data = storage;
104 v->start = (GLfloat *)storage;
105 v->count = 0;
106 v->flags = flags ;
107 }
108
109 void gl_vector4ub_init( GLvector4ub *v, GLuint flags, GLubyte (*storage)[4] )
110 {
111 v->stride = 4 * sizeof(GLubyte);
112 v->data = storage;
113 v->start = (GLubyte *) storage;
114 v->count = 0;
115 v->flags = flags ;
116 }
117
118 void gl_vector4us_init( GLvector4us *v, GLuint flags, GLushort (*storage)[4] )
119 {
120 v->stride = 4 * sizeof(GLushort);
121 v->data = storage;
122 v->start = (GLushort *) storage;
123 v->count = 0;
124 v->flags = flags ;
125 }
126
127 void gl_vector1ub_init( GLvector1ub *v, GLuint flags, GLubyte *storage )
128 {
129 v->stride = 1 * sizeof(GLubyte);
130 v->data = storage;
131 v->start = (GLubyte *) storage;
132 v->count = 0;
133 v->flags = flags ;
134 }
135
136 void gl_vector1ui_init( GLvector1ui *v, GLuint flags, GLuint *storage )
137 {
138 v->stride = 1 * sizeof(GLuint);
139 v->data = storage;
140 v->start = (GLuint *) storage;
141 v->count = 0;
142 v->flags = flags ;
143 }
144
145
146 /*
147 * Initialize GLvector objects and allocate storage.
148 * Input: v - the vector object
149 * sz - unused????
150 * flags - bitwise-OR of VEC_* flags
151 * count - number of elements to allocate in vector
152 * alignment - desired memory alignment for the data (in bytes)
153 */
154
155
156 void gl_vector4f_alloc( GLvector4f *v, GLuint flags, GLuint count,
157 GLuint alignment )
158 {
159 v->stride = 4 * sizeof(GLfloat);
160 v->size = 2;
161 v->storage = ALIGN_MALLOC( count * 4 * sizeof(GLfloat), alignment );
162 v->start = (GLfloat *) v->storage;
163 v->data = (GLfloat (*)[4]) v->storage;
164 v->count = 0;
165 v->flags = size_bits[4] | flags | VEC_MALLOC ;
166 }
167
168 void gl_vector3f_alloc( GLvector3f *v, GLuint flags, GLuint count,
169 GLuint alignment )
170 {
171 v->stride = 3 * sizeof(GLfloat);
172 v->storage = ALIGN_MALLOC( count * 3 * sizeof(GLfloat), alignment );
173 v->start = (GLfloat *) v->storage;
174 v->data = (GLfloat (*)[3]) v->storage;
175 v->count = 0;
176 v->flags = flags | VEC_MALLOC ;
177 }
178
179 void gl_vector1f_alloc( GLvector1f *v, GLuint flags, GLuint count,
180 GLuint alignment )
181 {
182 v->stride = sizeof(GLfloat);
183 v->storage = v->start = (GLfloat *)
184 ALIGN_MALLOC( count * sizeof(GLfloat), alignment );
185 v->data = v->start;
186 v->count = 0;
187 v->flags = flags | VEC_MALLOC ;
188 }
189
190 void gl_vector4ub_alloc( GLvector4ub *v, GLuint flags, GLuint count,
191 GLuint alignment )
192 {
193 v->stride = 4 * sizeof(GLubyte);
194 v->storage = ALIGN_MALLOC( count * 4 * sizeof(GLubyte), alignment );
195 v->start = (GLubyte *) v->storage;
196 v->data = (GLubyte (*)[4]) v->storage;
197 v->count = 0;
198 v->flags = flags | VEC_MALLOC ;
199 }
200
201 void gl_vector4us_alloc( GLvector4us *v, GLuint flags, GLuint count,
202 GLuint alignment )
203 {
204 v->stride = 4 * sizeof(GLushort);
205 v->storage = ALIGN_MALLOC( count * 4 * sizeof(GLushort), alignment );
206 v->start = (GLushort *) v->storage;
207 v->data = (GLushort (*)[4]) v->storage;
208 v->count = 0;
209 v->flags = flags | VEC_MALLOC ;
210 }
211
212 void gl_vector1ub_alloc( GLvector1ub *v, GLuint flags, GLuint count,
213 GLuint alignment )
214 {
215 v->stride = 1 * sizeof(GLubyte);
216 v->storage = ALIGN_MALLOC( count * sizeof(GLubyte), alignment );
217 v->start = (GLubyte *) v->storage;
218 v->data = (GLubyte *) v->storage;
219 v->count = 0;
220 v->flags = flags | VEC_MALLOC ;
221 }
222
223 void gl_vector1ui_alloc( GLvector1ui *v, GLuint flags, GLuint count,
224 GLuint alignment )
225 {
226 v->stride = 1 * sizeof(GLuint);
227 v->storage = ALIGN_MALLOC( count * sizeof(GLuint), alignment );
228 v->start = (GLuint *) v->storage;
229 v->data = (GLuint *) v->storage;
230 v->count = 0;
231 v->flags = flags | VEC_MALLOC ;
232 }
233
234
235
236 /*
237 * Vector deallocation. Free whatever memory is pointed to by the
238 * vector's storage field if the VEC_MALLOC flag is set.
239 * DO NOT free the GLvector object itself, though.
240 */
241
242
243 void gl_vector4f_free( GLvector4f *v )
244 {
245 if (v->flags & VEC_MALLOC) {
246 ALIGN_FREE( v->storage );
247 v->data = NULL;
248 v->start = NULL;
249 v->storage = NULL;
250 v->flags &= ~VEC_MALLOC;
251 }
252 }
253
254 void gl_vector3f_free( GLvector3f *v )
255 {
256 if (v->flags & VEC_MALLOC) {
257 ALIGN_FREE( v->storage );
258 v->data = 0;
259 v->start = 0;
260 v->storage = 0;
261 v->flags &= ~VEC_MALLOC;
262 }
263 }
264
265 void gl_vector1f_free( GLvector1f *v )
266 {
267 if (v->flags & VEC_MALLOC) {
268 ALIGN_FREE( v->storage );
269 v->data = NULL;
270 v->start = NULL;
271 v->storage = NULL;
272 v->flags &= ~VEC_MALLOC;
273 }
274 }
275
276 void gl_vector4ub_free( GLvector4ub *v )
277 {
278 if (v->flags & VEC_MALLOC) {
279 ALIGN_FREE( v->storage );
280 v->data = NULL;
281 v->start = NULL;
282 v->storage = NULL;
283 v->flags &= ~VEC_MALLOC;
284 }
285 }
286
287 void gl_vector4us_free( GLvector4us *v )
288 {
289 if (v->flags & VEC_MALLOC) {
290 ALIGN_FREE( v->storage );
291 v->data = NULL;
292 v->start = NULL;
293 v->storage = NULL;
294 v->flags &= ~VEC_MALLOC;
295 }
296 }
297
298 void gl_vector1ub_free( GLvector1ub *v )
299 {
300 if (v->flags & VEC_MALLOC) {
301 ALIGN_FREE( v->storage );
302 v->data = NULL;
303 v->start = NULL;
304 v->storage = NULL;
305 v->flags &= ~VEC_MALLOC;
306 }
307 }
308
309 void gl_vector1ui_free( GLvector1ui *v )
310 {
311 if (v->flags & VEC_MALLOC) {
312 ALIGN_FREE( v->storage );
313 v->data = NULL;
314 v->start = NULL;
315 v->storage = NULL;
316 v->flags &= ~VEC_MALLOC;
317 }
318 }
319
320
321 /*
322 * For debugging
323 */
324 void gl_vector4f_print( GLvector4f *v, GLubyte *cullmask, GLboolean culling )
325 {
326 GLfloat c[4] = { 0, 0, 0, 1 };
327 const char *templates[5] = {
328 "%d:\t0, 0, 0, 1\n",
329 "%d:\t%f, 0, 0, 1\n",
330 "%d:\t%f, %f, 0, 1\n",
331 "%d:\t%f, %f, %f, 1\n",
332 "%d:\t%f, %f, %f, %f\n"
333 };
334
335 const char *t = templates[v->size];
336 GLfloat *d = (GLfloat *)v->data;
337 GLuint j, i = 0, count;
338
339 printf("data-start\n");
340 for ( ; d != v->start ; STRIDE_F(d, v->stride), i++)
341 printf( t, i, d[0], d[1], d[2], d[3]);
342
343 printf("start-count(%u)\n", v->count);
344 count = i + v->count;
345
346 if (culling) {
347 for ( ; i < count ; STRIDE_F(d, v->stride), i++)
348 if (cullmask[i])
349 printf( t, i, d[0], d[1], d[2], d[3]);
350 }
351 else {
352 for ( ; i < count ; STRIDE_F(d, v->stride), i++)
353 printf( t, i, d[0], d[1], d[2], d[3]);
354 }
355
356 for (j = v->size ; j < 4; j++) {
357 if ((v->flags & (1<<j)) == 0) {
358
359 printf("checking col %u is clean as advertised ", j);
360
361 for (i = 0, d = (GLfloat *) v->data ;
362 i < count && d[j] == c[j] ;
363 i++, STRIDE_F(d, v->stride)) {};
364
365 if (i == count)
366 printf(" --> ok\n");
367 else
368 printf(" --> Failed at %u ******\n", i);
369 }
370 }
371 }
372
373
374 /*
375 * For debugging
376 */
377 void gl_vector3f_print( GLvector3f *v, GLubyte *cullmask, GLboolean culling )
378 {
379 GLfloat *d = (GLfloat *)v->data;
380 GLuint i = 0, count;
381
382 printf("data-start\n");
383 for ( ; d != v->start ; STRIDE_F(d,v->stride), i++)
384 printf( "%u:\t%f, %f, %f\n", i, d[0], d[1], d[2]);
385
386 printf("start-count(%u)\n", v->count);
387 count = i + v->count;
388
389 if (culling) {
390 for ( ; i < count ; STRIDE_F(d,v->stride), i++)
391 if (cullmask[i])
392 printf( "%u:\t%f, %f, %f\n", i, d[0], d[1], d[2]);
393 }
394 else {
395 for ( ; i < count ; STRIDE_F(d,v->stride), i++)
396 printf( "%u:\t%f, %f, %f\n", i, d[0], d[1], d[2]);
397 }
398 }