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