author | Stefan Vogel <sv@exept.de> |
Tue, 14 Jul 2015 14:59:39 +0200 | |
changeset 18577 | 72ff8078cb76 |
parent 17198 | 7518cd20d9d3 |
child 18608 | 7d521f25267c |
child 19003 | 6ac2cb393fd7 |
permissions | -rw-r--r-- |
18577 | 1 |
"{ Encoding: utf8 }" |
2 |
||
1 | 3 |
" |
4 |
COPYRIGHT (c) 1993 by Claus Gittinger |
|
5 |
All Rights Reserved |
|
6 |
||
7 |
This software is furnished under a license and may be used |
|
8 |
only in accordance with the terms of that license and with the |
|
9 |
inclusion of the above copyright notice. This software may not |
|
10 |
be provided or otherwise made available to, or used by, any |
|
11 |
other person. No title to or ownership of the software is |
|
12 |
hereby transferred. |
|
13 |
" |
|
5737 | 14 |
"{ Package: 'stx:libbasic' }" |
15 |
||
18577 | 16 |
"{ NameSpace: Smalltalk }" |
17 |
||
13826 | 18 |
AbstractNumberVector variableFloatSubclass:#FloatArray |
1263 | 19 |
instanceVariableNames:'' |
20 |
classVariableNames:'' |
|
21 |
poolDictionaries:'' |
|
22 |
category:'Collections-Arrayed' |
|
1 | 23 |
! |
24 |
||
88 | 25 |
!FloatArray class methodsFor:'documentation'! |
26 |
||
27 |
copyright |
|
28 |
" |
|
29 |
COPYRIGHT (c) 1993 by Claus Gittinger |
|
1 | 30 |
All Rights Reserved |
31 |
||
88 | 32 |
This software is furnished under a license and may be used |
33 |
only in accordance with the terms of that license and with the |
|
34 |
inclusion of the above copyright notice. This software may not |
|
35 |
be provided or otherwise made available to, or used by, any |
|
36 |
other person. No title to or ownership of the software is |
|
37 |
hereby transferred. |
|
38 |
" |
|
39 |
! |
|
1 | 40 |
|
88 | 41 |
documentation |
42 |
" |
|
43 |
FloatArrays store floats (and nothing else). |
|
16638 | 44 |
They have been added to support heavy duty number crunching and |
45 |
data exchange with openGL frameworks and other mass data libraries. |
|
88 | 46 |
See documentation in DoubleArray for more information. |
1263 | 47 |
|
4062 | 48 |
[memory requirements:] |
16638 | 49 |
OBJ-HEADER + (size * float-size) |
4062 | 50 |
|
1263 | 51 |
[See also:] |
16638 | 52 |
DoubleArray Array |
1290 | 53 |
|
54 |
[author:] |
|
16638 | 55 |
Claus Gittinger |
88 | 56 |
" |
628 | 57 |
! ! |
58 |
||
13707
a8f395adfac2
faster elementByteSize query
Claus Gittinger <cg@exept.de>
parents:
12577
diff
changeset
|
59 |
!FloatArray class methodsFor:'queries'! |
a8f395adfac2
faster elementByteSize query
Claus Gittinger <cg@exept.de>
parents:
12577
diff
changeset
|
60 |
|
a8f395adfac2
faster elementByteSize query
Claus Gittinger <cg@exept.de>
parents:
12577
diff
changeset
|
61 |
elementByteSize |
16849 | 62 |
"for bit-like containers, return the number of bytes stored per element. |
63 |
Here, 4 is returned" |
|
64 |
||
13707
a8f395adfac2
faster elementByteSize query
Claus Gittinger <cg@exept.de>
parents:
12577
diff
changeset
|
65 |
^ 4 |
a8f395adfac2
faster elementByteSize query
Claus Gittinger <cg@exept.de>
parents:
12577
diff
changeset
|
66 |
|
a8f395adfac2
faster elementByteSize query
Claus Gittinger <cg@exept.de>
parents:
12577
diff
changeset
|
67 |
"Created: / 15-09-2011 / 14:12:39 / cg" |
a8f395adfac2
faster elementByteSize query
Claus Gittinger <cg@exept.de>
parents:
12577
diff
changeset
|
68 |
! ! |
a8f395adfac2
faster elementByteSize query
Claus Gittinger <cg@exept.de>
parents:
12577
diff
changeset
|
69 |
|
5737 | 70 |
!FloatArray methodsFor:'copying'! |
71 |
||
72 |
clone |
|
73 |
"return a copy of the receiver" |
|
74 |
||
75 |
|newArr| |
|
76 |
||
77 |
newArr := self class new:(self size). |
|
78 |
%{ |
|
8913 | 79 |
if (__ClassInstPtr(__qClass(self))->c_ninstvars == __mkSmallInteger(0)) { |
14692 | 80 |
INT _sz = __floatArraySize(self); |
5737 | 81 |
|
14692 | 82 |
bcopy(__FloatArrayInstPtr(self)->f_element, |
83 |
__FloatArrayInstPtr(newArr)->f_element, |
|
84 |
sizeof(float) * _sz); |
|
5737 | 85 |
|
14692 | 86 |
RETURN (newArr); |
5737 | 87 |
} |
88 |
%}. |
|
89 |
newArr replaceFrom:1 to:self size with:self startingAt:1. |
|
90 |
^ newArr |
|
91 |
||
92 |
" |
|
93 |
|f1 f2| |
|
94 |
||
95 |
f1 := FloatArray withAll:#(1 2 3 4 5). |
|
14692 | 96 |
f1 clone |
5737 | 97 |
" |
98 |
! |
|
99 |
||
100 |
copyFrom:start to:stop |
|
101 |
"return a partial copy of the receiver" |
|
102 |
||
103 |
%{ |
|
8913 | 104 |
if (__ClassInstPtr(__qClass(self))->c_ninstvars == __mkSmallInteger(0)) { |
14692 | 105 |
if (__bothSmallInteger(start, stop)) { |
106 |
INT __start = __intVal(start) - 1; |
|
107 |
INT __stop = __intVal(stop) - 1 ; |
|
5737 | 108 |
|
14692 | 109 |
if (__stop >= __start) { |
110 |
INT __sz = __floatArraySize(self); |
|
5737 | 111 |
|
14692 | 112 |
if (((unsigned INT)__start < __sz) |
113 |
&& ((unsigned INT)__stop < __sz)) { |
|
114 |
INT __n = __stop - __start + 1; |
|
115 |
OBJ __nObj; |
|
5737 | 116 |
|
14692 | 117 |
__nObj = __STX___new(sizeof(struct __FloatArray) + (__n - 1) * sizeof(float)); |
118 |
if (__nObj != nil) { |
|
119 |
__objPtr(__nObj)->o_class = __qClass(self); |
|
120 |
__STORE(__nObj, __qClass(self)); |
|
121 |
bcopy(__FloatArrayInstPtr(self)->f_element + __start, |
|
122 |
__FloatArrayInstPtr(__nObj)->f_element, |
|
123 |
sizeof(float) * __n); |
|
124 |
RETURN(__nObj); |
|
125 |
} |
|
126 |
} |
|
127 |
} |
|
128 |
} |
|
5737 | 129 |
} |
14692 | 130 |
%}. |
5737 | 131 |
^ super copyFrom:start to:stop |
132 |
||
133 |
" |
|
134 |
|f1 f2| |
|
135 |
||
136 |
f1 := FloatArray withAll:#(1 2 3 4 5 6). |
|
14692 | 137 |
f2 := f1 copyFrom:2 to:4. |
138 |
f2 |
|
5737 | 139 |
" |
140 |
! |
|
141 |
||
142 |
replaceFrom:start to:stop with:aCollection startingAt:replStart |
|
143 |
%{ |
|
8913 | 144 |
if ((__ClassInstPtr(__qClass(self))->c_ninstvars == __mkSmallInteger(0)) |
18577 | 145 |
&& __isFloats(aCollection) |
146 |
&& (__ClassInstPtr(__qClass(aCollection))->c_ninstvars == __mkSmallInteger(0)) |
|
147 |
&& __bothSmallInteger(start, stop) |
|
148 |
&& __isSmallInteger(replStart) |
|
149 |
) { |
|
150 |
INT __start = __intVal(start) - 1; |
|
151 |
INT __stop = __intVal(stop) - 1 ; |
|
152 |
INT __replStart = __intVal(replStart) - 1 ; |
|
5737 | 153 |
|
18577 | 154 |
if (__stop >= __start) { |
155 |
INT __sz = __floatArraySize(self); |
|
156 |
INT __otherSz = __floatArraySize(aCollection); |
|
157 |
INT __replStop = __replStart + (__stop-__start); |
|
5737 | 158 |
|
18577 | 159 |
if (((unsigned INT)__start < __sz) |
160 |
&& ((unsigned INT)__stop < __sz) |
|
161 |
&& ((unsigned INT)__replStart < __otherSz) |
|
162 |
&& ((unsigned INT)__replStop < __otherSz)) { |
|
163 |
INT __n = __stop - __start + 1; |
|
5737 | 164 |
|
18577 | 165 |
if (aCollection == self) { |
166 |
memmove(&(__FloatArrayInstPtr(self)->f_element[__start]), |
|
167 |
&(__FloatArrayInstPtr(aCollection)->f_element[__replStart]), |
|
168 |
sizeof(float) * __n); |
|
169 |
} else { |
|
170 |
memcpy(&(__FloatArrayInstPtr(self)->f_element[__start]), |
|
171 |
&(__FloatArrayInstPtr(aCollection)->f_element[__replStart]), |
|
172 |
sizeof(float) * __n); |
|
173 |
} |
|
174 |
RETURN(self); |
|
175 |
} |
|
176 |
} |
|
5737 | 177 |
} |
178 |
%}. |
|
179 |
^ super replaceFrom:start to:stop with:aCollection startingAt:replStart |
|
180 |
||
181 |
" |
|
182 |
|f1 f2| |
|
183 |
||
184 |
f1 := (1 to:5) asFloatArray. |
|
185 |
f2 := #(10 9 8 7 6) asFloatArray. |
|
14692 | 186 |
f1 replaceFrom:1 to:3 with:f2 startingAt:3 |
5737 | 187 |
" |
188 |
! ! |
|
189 |
||
17198 | 190 |
!FloatArray methodsFor:'destructive arithmetic support'! |
191 |
||
192 |
primAbs |
|
193 |
"destructive absolute value of each element" |
|
194 |
||
195 |
%{ |
|
196 |
if (__ClassInstPtr(__qClass(self))->c_ninstvars == __mkSmallInteger(0)) { |
|
197 |
INT _sz = __floatArraySize(self); |
|
198 |
INT i; |
|
199 |
float *_p = __FloatArrayInstPtr(self)->f_element; |
|
200 |
float prev_p; |
|
201 |
||
202 |
if (_sz > 0) { |
|
203 |
/* how about inline-mmx-asm for this ... */ |
|
204 |
prev_p = _p[0]; |
|
205 |
for (i=1; i<_sz; i++) { |
|
206 |
float next_p = _p[i]; |
|
207 |
||
208 |
if (prev_p < 0) { |
|
209 |
_p[i-1] = -prev_p; |
|
210 |
} |
|
211 |
prev_p = next_p; |
|
212 |
} |
|
213 |
if (prev_p < 0) { |
|
214 |
_p[i-1] = -prev_p; |
|
215 |
} |
|
216 |
} |
|
217 |
RETURN (self); |
|
218 |
} |
|
219 |
%}. |
|
220 |
super primAbs |
|
221 |
||
222 |
" |
|
223 |
|f| |
|
224 |
||
225 |
f := FloatArray withAll:#(-1 2 -3 4 -5). |
|
226 |
f abs. |
|
227 |
f |
|
228 |
" |
|
229 |
||
230 |
"Created: / 30-05-2007 / 17:50:17 / cg" |
|
231 |
! |
|
232 |
||
233 |
primAddArray: floatArray |
|
234 |
"add the vector argument into the receiver (destructive). |
|
235 |
The argument must be another vector" |
|
236 |
||
237 |
%{ |
|
238 |
if ((__ClassInstPtr(__qClass(self))->c_ninstvars == __mkSmallInteger(0)) |
|
239 |
&& __isFloats(floatArray) |
|
240 |
&& (__ClassInstPtr(__qClass(floatArray))->c_ninstvars == __mkSmallInteger(0))) { |
|
241 |
INT _sz1 = __floatArraySize(self); |
|
242 |
INT _sz2 = __floatArraySize(floatArray); |
|
243 |
INT i; |
|
244 |
float *_p1 = __FloatArrayInstPtr(self)->f_element; |
|
245 |
float *_p2 = __FloatArrayInstPtr(floatArray)->f_element; |
|
246 |
||
247 |
if (_sz1 > 0) { |
|
248 |
if (_sz2 >= _sz1) { |
|
249 |
/* how about inline-mmx-asm for this ... */ |
|
250 |
float prev_p1 = _p1[0]; |
|
251 |
float prev_p2 = _p2[0]; |
|
252 |
||
253 |
for (i=1; i<_sz1; i++) { |
|
254 |
float next_p1 = _p1[i]; |
|
255 |
float next_p2 = _p2[i]; |
|
256 |
_p1[i-1] = prev_p1 + prev_p2; |
|
257 |
prev_p1 = next_p1; |
|
258 |
prev_p2 = next_p2; |
|
259 |
} |
|
260 |
_p1[i-1] = prev_p1 + prev_p2; |
|
261 |
} |
|
262 |
} |
|
263 |
RETURN (self); |
|
264 |
} |
|
265 |
%}. |
|
266 |
super primAddArray:floatArray |
|
267 |
||
268 |
" |
|
269 |
|f1 f2| |
|
270 |
||
271 |
f1 := FloatArray withAll:#(1 2 3 4 5). |
|
272 |
f2 := FloatArray withAll:#(2 2 2 3 3). |
|
273 |
f1 += f2. |
|
274 |
f1 |
|
275 |
" |
|
276 |
! |
|
277 |
||
278 |
primAddScalar: aScalar |
|
279 |
"add the scalar argument into the receiver (destructive)." |
|
280 |
||
281 |
%{ |
|
282 |
if (__ClassInstPtr(__qClass(self))->c_ninstvars == __mkSmallInteger(0)) { |
|
283 |
INT _sz1 = __floatArraySize(self); |
|
284 |
INT i; |
|
285 |
float *_p1 = __FloatArrayInstPtr(self)->f_element; |
|
286 |
float v; |
|
287 |
float prev_p1; |
|
288 |
||
289 |
if (_sz1 > 0) { |
|
290 |
if (__isFloat(aScalar)) { |
|
291 |
v = (float)(__floatVal(aScalar)); |
|
292 |
} else if (__isShortFloat(aScalar)) { |
|
293 |
v = __shortFloatVal(aScalar); |
|
294 |
} else if (__isSmallInteger(aScalar)) { |
|
295 |
v = (float)(__intVal(aScalar)); |
|
296 |
} else |
|
297 |
goto badArg; |
|
298 |
||
299 |
/* how about inline-mmx-asm for this ... */ |
|
300 |
prev_p1 = _p1[0]; |
|
301 |
for (i=1; i<_sz1; i++) { |
|
302 |
float next_p1 = _p1[i]; |
|
303 |
_p1[i-1] = prev_p1 + v; |
|
304 |
prev_p1 = next_p1; |
|
305 |
} |
|
306 |
_p1[i-1] = prev_p1 + v; |
|
307 |
} |
|
308 |
RETURN (self); |
|
309 |
} |
|
310 |
badArg: ; |
|
311 |
%}. |
|
312 |
super primAddScalar:aScalar |
|
313 |
||
314 |
" |
|
315 |
|f1 f2| |
|
316 |
||
317 |
f1 := FloatArray withAll:#(1 2 3 4 5). |
|
318 |
f1 += 2.0. |
|
319 |
Transcript showCR:f1. |
|
320 |
f1 += 2.0 asShortFloat. |
|
321 |
Transcript showCR:f1. |
|
322 |
f1 += 2. |
|
323 |
Transcript showCR:f1. |
|
324 |
" |
|
325 |
! |
|
326 |
||
327 |
primDivArray: floatArray |
|
328 |
"divide the vector argument into the receiver (destructive). |
|
329 |
The argument must be another vector" |
|
330 |
||
331 |
%{ |
|
332 |
if ((__ClassInstPtr(__qClass(self))->c_ninstvars == __mkSmallInteger(0)) |
|
333 |
&& __isFloats(floatArray) |
|
334 |
&& (__ClassInstPtr(__qClass(floatArray))->c_ninstvars == __mkSmallInteger(0))) { |
|
335 |
INT _sz1 = __floatArraySize(self); |
|
336 |
INT _sz2 = __floatArraySize(floatArray); |
|
337 |
INT i; |
|
338 |
float *_p1 = __FloatArrayInstPtr(self)->f_element; |
|
339 |
float *_p2 = __FloatArrayInstPtr(floatArray)->f_element; |
|
340 |
||
341 |
if (_sz1 > 0) { |
|
342 |
if (_sz2 >= _sz1) { |
|
343 |
/* how about inline-mmx-asm for this ... */ |
|
344 |
float prev_p1 = _p1[0]; |
|
345 |
float prev_p2 = _p2[0]; |
|
346 |
||
347 |
for (i=1; i<_sz1; i++) { |
|
348 |
float next_p1 = _p1[i]; |
|
349 |
float next_p2 = _p2[i]; |
|
350 |
_p1[i-1] = prev_p1 / prev_p2; |
|
351 |
prev_p1 = next_p1; |
|
352 |
prev_p2 = next_p2; |
|
353 |
} |
|
354 |
_p1[i-1] = prev_p1 / prev_p2; |
|
355 |
} |
|
356 |
} |
|
357 |
RETURN (self); |
|
358 |
} |
|
359 |
%}. |
|
360 |
super primDivArray: floatArray |
|
361 |
||
362 |
" |
|
363 |
|f1 f2| |
|
364 |
||
365 |
f1 := FloatArray withAll:#(1 2 3 4 5). |
|
366 |
f2 := FloatArray withAll:#(2 2 2 3 3). |
|
367 |
f1 /= f2. |
|
368 |
f1 |
|
369 |
" |
|
370 |
||
371 |
"Modified: / 29-05-2007 / 16:01:34 / cg" |
|
372 |
! |
|
373 |
||
374 |
primDivScalar: aScalar |
|
375 |
"divide the scalar argument into the receiver (destructive)." |
|
376 |
||
377 |
%{ |
|
378 |
if (__ClassInstPtr(__qClass(self))->c_ninstvars == __mkSmallInteger(0)) { |
|
379 |
INT _sz1 = __floatArraySize(self); |
|
380 |
INT i; |
|
381 |
float *_p1 = __FloatArrayInstPtr(self)->f_element; |
|
382 |
float v; |
|
383 |
float prev_p1; |
|
384 |
||
385 |
if (_sz1 > 0) { |
|
386 |
if (__isFloat(aScalar)) { |
|
387 |
v = (float)(__floatVal(aScalar)); |
|
388 |
} else if (__isShortFloat(aScalar)) { |
|
389 |
v = __shortFloatVal(aScalar); |
|
390 |
} else if (__isSmallInteger(aScalar)) { |
|
391 |
v = (float)(__intVal(aScalar)); |
|
392 |
} else |
|
393 |
goto badArg; |
|
394 |
||
395 |
/* how about inline-mmx-asm for this ... */ |
|
396 |
prev_p1 = _p1[0]; |
|
397 |
for (i=1; i<_sz1; i++) { |
|
398 |
float next_p1 = _p1[i]; |
|
399 |
_p1[i-1] = prev_p1 / v; |
|
400 |
prev_p1 = next_p1; |
|
401 |
} |
|
402 |
_p1[i-1] = prev_p1 / v; |
|
403 |
} |
|
404 |
RETURN (self); |
|
405 |
} |
|
406 |
badArg: ; |
|
407 |
%}. |
|
408 |
super primDivScalar:aScalar |
|
409 |
||
410 |
" |
|
411 |
|f1 f2| |
|
412 |
||
413 |
f1 := FloatArray withAll:#(1 2 3 4 5). |
|
414 |
f1 /= 2.0. |
|
415 |
Transcript showCR:f1. |
|
416 |
f1 /= 2.0 asShortFloat. |
|
417 |
Transcript showCR:f1. |
|
418 |
f1 /= 2. |
|
419 |
Transcript showCR:f1. |
|
420 |
" |
|
421 |
||
422 |
"Modified: / 29-05-2007 / 16:01:39 / cg" |
|
423 |
! |
|
424 |
||
425 |
primMulArray: floatArray |
|
426 |
"multiply the vector argument into the receiver (destructive). |
|
427 |
The argument must be another vector" |
|
428 |
||
429 |
%{ |
|
430 |
if ((__ClassInstPtr(__qClass(self))->c_ninstvars == __mkSmallInteger(0)) |
|
431 |
&& __isFloats(floatArray) |
|
432 |
&& (__ClassInstPtr(__qClass(floatArray))->c_ninstvars == __mkSmallInteger(0))) { |
|
433 |
INT _sz1 = __floatArraySize(self); |
|
434 |
INT _sz2 = __floatArraySize(floatArray); |
|
435 |
INT i; |
|
436 |
float *_p1 = __FloatArrayInstPtr(self)->f_element; |
|
437 |
float *_p2 = __FloatArrayInstPtr(floatArray)->f_element; |
|
438 |
||
439 |
if (_sz1 > 0) { |
|
440 |
if (_sz2 >= _sz1) { |
|
441 |
float prev_p1 = _p1[0]; |
|
442 |
float prev_p2 = _p2[0]; |
|
443 |
||
444 |
for (i=1; i<_sz1; i++) { |
|
445 |
float next_p1 = _p1[i]; |
|
446 |
float next_p2 = _p2[i]; |
|
447 |
_p1[i-1] = prev_p1 * prev_p2; |
|
448 |
prev_p1 = next_p1; |
|
449 |
prev_p2 = next_p2; |
|
450 |
} |
|
451 |
_p1[i-1] = prev_p1 * prev_p2; |
|
452 |
} |
|
453 |
} |
|
454 |
RETURN (self); |
|
455 |
} |
|
456 |
%}. |
|
457 |
super primMulArray: floatArray |
|
458 |
||
459 |
" |
|
460 |
|f1 f2| |
|
461 |
||
462 |
f1 := FloatArray withAll:#(1 2 3 4 5). |
|
463 |
f2 := FloatArray withAll:#(2 2 2 3 3). |
|
464 |
f1 *= f2. |
|
465 |
f1 |
|
466 |
" |
|
467 |
! |
|
468 |
||
469 |
primMulScalar: aScalar |
|
470 |
"multiply the scalar argument into the receiver (destructive)." |
|
471 |
||
472 |
%{ |
|
473 |
if (__ClassInstPtr(__qClass(self))->c_ninstvars == __mkSmallInteger(0)) { |
|
474 |
INT _sz1 = __floatArraySize(self); |
|
475 |
INT i; |
|
476 |
float *_p1 = __FloatArrayInstPtr(self)->f_element; |
|
477 |
float v; |
|
478 |
float prev_p1; |
|
479 |
||
480 |
if (_sz1 > 0) { |
|
481 |
if (__isFloat(aScalar)) { |
|
482 |
v = (float)(__floatVal(aScalar)); |
|
483 |
} else if (__isShortFloat(aScalar)) { |
|
484 |
v = __shortFloatVal(aScalar); |
|
485 |
} else if (__isSmallInteger(aScalar)) { |
|
486 |
v = (float)(__intVal(aScalar)); |
|
487 |
} else |
|
488 |
goto badArg; |
|
489 |
||
490 |
/* how about inline-mmx-asm for this ... */ |
|
491 |
prev_p1 = _p1[0]; |
|
492 |
for (i=1; i<_sz1; i++) { |
|
493 |
float next_p1 = _p1[i]; |
|
494 |
_p1[i-1] = prev_p1 * v; |
|
495 |
prev_p1 = next_p1; |
|
496 |
} |
|
497 |
_p1[i-1] = prev_p1 * v; |
|
498 |
} |
|
499 |
RETURN (self); |
|
500 |
} |
|
501 |
badArg: ; |
|
502 |
%}. |
|
503 |
super primMulScalar:aScalar |
|
504 |
||
505 |
" |
|
506 |
|f1 f2| |
|
507 |
||
508 |
f1 := FloatArray withAll:#(1 2 3 4 5). |
|
509 |
f1 *= 2.0. |
|
510 |
Transcript showCR:f1. |
|
511 |
f1 *= 2.0 asShortFloat. |
|
512 |
Transcript showCR:f1. |
|
513 |
f1 *= 2. |
|
514 |
Transcript showCR:f1. |
|
515 |
" |
|
516 |
! |
|
517 |
||
518 |
primNegated |
|
519 |
"destructive negative value of each element" |
|
520 |
||
521 |
%{ |
|
522 |
if (__ClassInstPtr(__qClass(self))->c_ninstvars == __mkSmallInteger(0)) { |
|
523 |
INT _sz = __floatArraySize(self); |
|
524 |
INT i; |
|
525 |
float *_p = __FloatArrayInstPtr(self)->f_element; |
|
526 |
float prev_p; |
|
527 |
||
528 |
if (_sz > 0) { |
|
529 |
/* how about inline-mmx-asm for this ... */ |
|
530 |
prev_p = _p[0]; |
|
531 |
for (i=1; i<_sz; i++) { |
|
532 |
float next_p = _p[i]; |
|
533 |
||
534 |
_p[i-1] = -prev_p; |
|
535 |
prev_p = next_p; |
|
536 |
} |
|
537 |
_p[i-1] = -prev_p; |
|
538 |
} |
|
539 |
RETURN (self); |
|
540 |
} |
|
541 |
%}. |
|
542 |
super primNegated |
|
543 |
||
544 |
" |
|
545 |
|f| |
|
546 |
||
547 |
f := FloatArray withAll:#(-1 2 -3 4 -5). |
|
548 |
f negated. |
|
549 |
f |
|
550 |
" |
|
551 |
||
552 |
"Created: / 30-05-2007 / 17:51:29 / cg" |
|
553 |
! |
|
554 |
||
555 |
primSubtractArray: floatArray |
|
556 |
"subtract the vector argument from the receiver (destructive). |
|
557 |
The argument must be another vector" |
|
558 |
||
559 |
%{ |
|
560 |
if ((__ClassInstPtr(__qClass(self))->c_ninstvars == __mkSmallInteger(0)) |
|
561 |
&& __isFloats(floatArray) |
|
562 |
&& (__ClassInstPtr(__qClass(floatArray))->c_ninstvars == __mkSmallInteger(0))) { |
|
563 |
INT _sz1 = __floatArraySize(self); |
|
564 |
INT _sz2 = __floatArraySize(floatArray); |
|
565 |
INT i; |
|
566 |
float *_p1 = __FloatArrayInstPtr(self)->f_element; |
|
567 |
float *_p2 = __FloatArrayInstPtr(floatArray)->f_element; |
|
568 |
||
569 |
if (_sz1 > 0) { |
|
570 |
if (_sz2 >= _sz1) { |
|
571 |
float prev_p1 = _p1[0]; |
|
572 |
float prev_p2 = _p2[0]; |
|
573 |
||
574 |
for (i=1; i<_sz1; i++) { |
|
575 |
float next_p1 = _p1[i]; |
|
576 |
float next_p2 = _p2[i]; |
|
577 |
_p1[i-1] = prev_p1 - prev_p2; |
|
578 |
prev_p1 = next_p1; |
|
579 |
prev_p2 = next_p2; |
|
580 |
} |
|
581 |
_p1[i-1] = prev_p1 - prev_p2; |
|
582 |
} |
|
583 |
} |
|
584 |
RETURN (self); |
|
585 |
} |
|
586 |
%}. |
|
587 |
super primSubtractArray: floatArray |
|
588 |
||
589 |
" |
|
590 |
|f1 f2| |
|
591 |
||
592 |
f1 := FloatArray withAll:#(1 2 3 4 5). |
|
593 |
f2 := FloatArray withAll:#(2 2 2 3 3). |
|
594 |
f1 -= f2. |
|
595 |
f1 |
|
596 |
" |
|
597 |
||
598 |
"Created: / 30-05-2007 / 17:42:41 / cg" |
|
599 |
! |
|
600 |
||
601 |
primSubtractScalar: aScalar |
|
602 |
"subtract the scalar argument from the receiver (destructive)." |
|
603 |
||
604 |
%{ |
|
605 |
if (__ClassInstPtr(__qClass(self))->c_ninstvars == __mkSmallInteger(0)) { |
|
606 |
INT _sz1 = __floatArraySize(self); |
|
607 |
INT i; |
|
608 |
float *_p1 = __FloatArrayInstPtr(self)->f_element; |
|
609 |
float v; |
|
610 |
float prev_p1; |
|
611 |
||
612 |
if (_sz1 > 0) { |
|
613 |
if (__isFloat(aScalar)) { |
|
614 |
v = (float)(__floatVal(aScalar)); |
|
615 |
} else if (__isShortFloat(aScalar)) { |
|
616 |
v = __shortFloatVal(aScalar); |
|
617 |
} else if (__isSmallInteger(aScalar)) { |
|
618 |
v = (float)(__intVal(aScalar)); |
|
619 |
} else |
|
620 |
goto badArg; |
|
621 |
||
622 |
/* how about inline-mmx-asm for this... ? */ |
|
623 |
prev_p1 = _p1[0]; |
|
624 |
for (i=1; i<_sz1; i++) { |
|
625 |
float next_p1 = _p1[i]; |
|
626 |
_p1[i-1] = prev_p1 - v; |
|
627 |
prev_p1 = next_p1; |
|
628 |
} |
|
629 |
_p1[i-1] = prev_p1 - v; |
|
630 |
} |
|
631 |
RETURN (self); |
|
632 |
} |
|
633 |
badArg: ; |
|
634 |
%}. |
|
635 |
super primSubtractScalar:aScalar |
|
636 |
||
637 |
" |
|
638 |
|f1 f2| |
|
639 |
||
640 |
f1 := FloatArray withAll:#(1 2 3 4 5). |
|
641 |
f1 -= 2.0. |
|
642 |
Transcript showCR:f1. |
|
643 |
f1 -= 2.0 asShortFloat. |
|
644 |
Transcript showCR:f1. |
|
645 |
f1 -= 2. |
|
646 |
Transcript showCR:f1. |
|
647 |
" |
|
648 |
||
649 |
"Created: / 30-05-2007 / 17:43:06 / cg" |
|
650 |
! ! |
|
651 |
||
5737 | 652 |
!FloatArray methodsFor:'queries'! |
653 |
||
7220 | 654 |
defaultElement |
655 |
^ ShortFloat zero |
|
656 |
! |
|
657 |
||
5737 | 658 |
max |
659 |
"return the largest element; |
|
660 |
redefined for speed" |
|
661 |
%{ |
|
8913 | 662 |
if (__ClassInstPtr(__qClass(self))->c_ninstvars == __mkSmallInteger(0)) { |
14692 | 663 |
INT _sz = __floatArraySize(self); |
5737 | 664 |
|
14692 | 665 |
if (_sz > 0) { |
666 |
float *_p = __FloatArrayInstPtr(self)->f_element; |
|
667 |
float _max; |
|
5737 | 668 |
|
14692 | 669 |
_max = _p[0]; |
670 |
if (_sz > 1) { |
|
671 |
INT _i; |
|
672 |
float _prev, _this; |
|
5737 | 673 |
|
14692 | 674 |
/* how about inline-mmx-asm for this ... */ |
675 |
_this = _p[1]; |
|
676 |
for (_i=2; _i<_sz; _i++) { |
|
677 |
_prev = _this; |
|
678 |
_this = _p[_i]; |
|
679 |
if (_prev > _max) _max = _prev; |
|
680 |
} |
|
681 |
if (_this > _max) _max = _this; |
|
682 |
} |
|
683 |
RETURN (__MKFLOAT(_max)); |
|
684 |
} |
|
7220 | 685 |
} |
5737 | 686 |
%}. |
7220 | 687 |
^ super max |
5737 | 688 |
|
7220 | 689 |
" |
690 |
|f1| |
|
5737 | 691 |
|
7220 | 692 |
f1 := (1 to:1000) asFloatArray. |
693 |
Time millisecondsToRun:[ 1000 timesRepeat:[ f1 max ] ] |
|
694 |
" |
|
695 |
" |
|
696 |
|f1| |
|
5737 | 697 |
|
7220 | 698 |
f1 := FloatArray withAll:#(1 2 3 4 5). |
14692 | 699 |
f1 max |
7220 | 700 |
" |
701 |
" |
|
702 |
|f1| |
|
5737 | 703 |
|
7220 | 704 |
f1 := FloatArray withAll:#(5 4 3 2 1). |
14692 | 705 |
f1 max |
7220 | 706 |
" |
5737 | 707 |
! |
708 |
||
709 |
min |
|
710 |
"return the largest element; |
|
711 |
redefined for speed" |
|
712 |
%{ |
|
8913 | 713 |
if (__ClassInstPtr(__qClass(self))->c_ninstvars == __mkSmallInteger(0)) { |
14692 | 714 |
INT _sz = __floatArraySize(self); |
5737 | 715 |
|
14692 | 716 |
if (_sz > 0) { |
717 |
float *_p = __FloatArrayInstPtr(self)->f_element; |
|
718 |
float _min; |
|
5737 | 719 |
|
14692 | 720 |
_min = _p[0]; |
721 |
if (_sz > 1) { |
|
722 |
INT _i; |
|
723 |
float _prev, _this; |
|
5737 | 724 |
|
14692 | 725 |
/* how about inline-mmx-asm for this ... */ |
726 |
_this = _p[1]; |
|
727 |
for (_i=2; _i<_sz; _i++) { |
|
728 |
_prev = _this; |
|
729 |
_this = _p[_i]; |
|
730 |
if (_prev < _min) _min = _prev; |
|
731 |
} |
|
732 |
if (_this < _min) _min = _this; |
|
733 |
} |
|
734 |
RETURN (__MKFLOAT(_min)); |
|
735 |
} |
|
7220 | 736 |
} |
5737 | 737 |
%}. |
7220 | 738 |
^ super min |
5737 | 739 |
|
7220 | 740 |
" |
741 |
|f1| |
|
5737 | 742 |
|
7220 | 743 |
f1 := (1 to:1000) asFloatArray. |
744 |
Time millisecondsToRun:[ 1000 timesRepeat:[ f1 min ] ] |
|
745 |
" |
|
746 |
" |
|
747 |
|f1| |
|
5737 | 748 |
|
7220 | 749 |
f1 := FloatArray withAll:#(1 2 3 4 5). |
14692 | 750 |
f1 min |
7220 | 751 |
" |
752 |
" |
|
753 |
|f1| |
|
5737 | 754 |
|
7220 | 755 |
f1 := FloatArray withAll:#(5 4 3 2 1). |
14692 | 756 |
f1 min |
7220 | 757 |
" |
5737 | 758 |
! |
759 |
||
760 |
minMax |
|
11212 | 761 |
"return a Tuple holding the smallest and largest element; |
5737 | 762 |
redefined for speed" |
763 |
||
764 |
|min max| |
|
765 |
||
766 |
%{ |
|
8913 | 767 |
if (__ClassInstPtr(__qClass(self))->c_ninstvars == __mkSmallInteger(0)) { |
14692 | 768 |
INT _sz = __floatArraySize(self); |
769 |
if (_sz > 0) { |
|
770 |
INT _i; |
|
771 |
float *_p = __FloatArrayInstPtr(self)->f_element; |
|
772 |
float _min, _max; |
|
5737 | 773 |
|
14692 | 774 |
_min = _max = _p[0]; |
13774 | 775 |
#ifdef PRE_4_OCT_2011 |
14692 | 776 |
if (_sz > 1) { |
777 |
float _this = _p[1]; |
|
778 |
float _prev; |
|
7220 | 779 |
|
14692 | 780 |
/* how about inline-mmx-asm for this ... */ |
781 |
for (_i=2; _i<_sz; _i++) { |
|
782 |
_prev = _this; |
|
783 |
_this = _p[_i]; |
|
784 |
if (_prev < _min) { |
|
785 |
_min = _prev; |
|
786 |
} else if (_prev > _max) { |
|
787 |
_max = _prev; |
|
788 |
} |
|
789 |
} |
|
790 |
if (_this < _min) { |
|
791 |
_min = _this; |
|
792 |
} else if (_this > _max) { |
|
793 |
_max = _this; |
|
794 |
} |
|
795 |
} |
|
13774 | 796 |
#else |
14692 | 797 |
for (_i=_sz-1; _i>0; _i-=2) { |
798 |
float _v1 = _p[_i]; |
|
799 |
float _v2 = _p[_i-1]; |
|
800 |
if (_v1 < _v2) { |
|
801 |
if (_v1 < _min) _min = _v1; |
|
802 |
if (_v2 > _max) _max = _v2; |
|
803 |
} else { |
|
804 |
if (_v2 < _min) _min = _v2; |
|
805 |
if (_v1 > _max) _max = _v1; |
|
806 |
} |
|
807 |
} |
|
13774 | 808 |
#endif |
14692 | 809 |
min = __MKFLOAT(_min); |
810 |
max = __MKFLOAT(_max); |
|
811 |
RETURN (__ARRAY_WITH2(min, max)); |
|
812 |
} |
|
7220 | 813 |
} |
5737 | 814 |
%}. |
7220 | 815 |
^ Array with:(super min) with:(super max) |
5737 | 816 |
|
7220 | 817 |
" |
818 |
|f1| |
|
819 |
||
13774 | 820 |
f1 := (1 to:10000) asFloatArray. |
14692 | 821 |
Time millisecondsToRun:[ 1000 timesRepeat:[ f1 minMax ] ] |
7220 | 822 |
" |
823 |
" |
|
824 |
|f1| |
|
5737 | 825 |
|
7220 | 826 |
f1 := FloatArray withAll:#(1 2 3 4 5). |
14692 | 827 |
f1 minMax |
7220 | 828 |
" |
829 |
" |
|
830 |
|f1| |
|
5737 | 831 |
|
7220 | 832 |
f1 := FloatArray withAll:#(5 4 3 2 1). |
14692 | 833 |
f1 minMax |
7220 | 834 |
" |
13774 | 835 |
|
836 |
"Modified (comment): / 07-10-2011 / 13:03:30 / cg" |
|
10561 | 837 |
! |
838 |
||
12577 | 839 |
numFloats |
840 |
^ self size |
|
10561 | 841 |
! ! |
842 |
||
843 |
!FloatArray methodsFor:'vector arithmetic'! |
|
844 |
||
845 |
dot: aFloatVector |
|
846 |
"Return the dot product of the receiver and the argument. |
|
847 |
Fail if the argument is not of the same size as the receiver." |
|
848 |
||
849 |
| mySize result | |
|
850 |
||
851 |
"/ <primitive:'primitiveDotProduct' module: 'FloatArrayPlugin'> |
|
852 |
%{ |
|
11212 | 853 |
if (__isFloats(aFloatVector) |
854 |
&& (__ClassInstPtr(__qClass(self))->c_ninstvars == __mkSmallInteger(0)) |
|
855 |
&& (__ClassInstPtr(__qClass(aFloatVector))->c_ninstvars == __mkSmallInteger(0))) { |
|
15935 | 856 |
INT __mySize = __floatArraySize(self); |
857 |
INT __otherSize = __floatArraySize(aFloatVector); |
|
17198 | 858 |
float __result = 0.0; |
859 |
||
15935 | 860 |
if (__mySize == __otherSize) { |
17198 | 861 |
if (__mySize > 0) { |
862 |
float *__p1 = __FloatArrayInstPtr(self)->f_element; |
|
863 |
float *__p2 = __FloatArrayInstPtr(aFloatVector)->f_element; |
|
864 |
INT __i; |
|
865 |
/* how about inline-mmx-asm for this ... */ |
|
866 |
for (__i=0; __i<__mySize; __i++) { |
|
867 |
__result += (__p1[__i] * __p2[__i]); |
|
868 |
} |
|
15935 | 869 |
} |
870 |
RETURN (__MKFLOAT(__result)); |
|
871 |
} |
|
10561 | 872 |
} |
873 |
%}. |
|
15935 | 874 |
^ super dot:aFloatVector |
10561 | 875 |
|
876 |
" |
|
877 |
|v| |
|
878 |
||
11212 | 879 |
v := #(2.0 2.0 1.0) asFloatArray. |
17198 | 880 |
v dot:v. |
881 |
" |
|
882 |
" |
|
883 |
|v1 v2| |
|
884 |
||
885 |
v1 := FloatArray new:10000 withAll:2. |
|
886 |
v2 := FloatArray new:10000 withAll:3. |
|
887 |
Time millisecondsToRun:[ |
|
888 |
10000 timesRepeat:[ |
|
889 |
v1 dot:v2. |
|
890 |
] |
|
891 |
] |
|
10561 | 892 |
" |
893 |
||
894 |
"Created: / 29-05-2007 / 13:13:39 / cg" |
|
5737 | 895 |
! ! |
896 |
||
628 | 897 |
!FloatArray class methodsFor:'documentation'! |
603 | 898 |
|
899 |
version |
|
18577 | 900 |
^ '$Header$' |
12577 | 901 |
! |
902 |
||
903 |
version_CVS |
|
18577 | 904 |
^ '$Header$' |
88 | 905 |
! ! |
15935 | 906 |