author | Claus Gittinger <cg@exept.de> |
Wed, 24 Aug 2016 17:06:14 +0200 | |
changeset 20300 | b67a6d272129 |
parent 19032 | 67c02dbf742d |
child 20303 | 060fea1cfebe |
child 20343 | 0719a15ae26d |
permissions | -rw-r--r-- |
20300 | 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 |
19003 | 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:] |
19003 | 49 |
OBJ-HEADER + (size * float-size) |
4062 | 50 |
|
1263 | 51 |
[See also:] |
19003 | 52 |
DoubleArray Array |
1290 | 53 |
|
54 |
[author:] |
|
19003 | 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)) |
19003 | 145 |
&& __isFloats(aCollection) |
146 |
&& (__ClassInstPtr(__qClass(aCollection))->c_ninstvars == __mkSmallInteger(0)) |
|
147 |
&& __bothSmallInteger(start, stop) |
|
148 |
&& __isSmallInteger(replStart) |
|
18577 | 149 |
) { |
19003 | 150 |
INT __start = __intVal(start) - 1; |
151 |
INT __stop = __intVal(stop) - 1 ; |
|
152 |
INT __replStart = __intVal(replStart) - 1 ; |
|
5737 | 153 |
|
19003 | 154 |
if (__stop >= __start) { |
155 |
INT __sz = __floatArraySize(self); |
|
156 |
INT __otherSz = __floatArraySize(aCollection); |
|
157 |
INT __replStop = __replStart + (__stop-__start); |
|
5737 | 158 |
|
19003 | 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 |
|
19003 | 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)) { |
|
19003 | 197 |
INT _sz = __floatArraySize(self); |
198 |
INT i; |
|
199 |
float *_p = __FloatArrayInstPtr(self)->f_element; |
|
200 |
float prev_p; |
|
17198 | 201 |
|
19003 | 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]; |
|
17198 | 207 |
|
19003 | 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); |
|
17198 | 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))) { |
|
19003 | 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; |
|
17198 | 246 |
|
19003 | 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]; |
|
17198 | 252 |
|
19003 | 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); |
|
17198 | 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)) { |
|
19003 | 283 |
INT _sz1 = __floatArraySize(self); |
284 |
INT i; |
|
285 |
float *_p1 = __FloatArrayInstPtr(self)->f_element; |
|
286 |
float v; |
|
287 |
float prev_p1; |
|
17198 | 288 |
|
19003 | 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; |
|
17198 | 298 |
|
19003 | 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); |
|
17198 | 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))) { |
|
19003 | 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; |
|
17198 | 340 |
|
19003 | 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]; |
|
17198 | 346 |
|
19003 | 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); |
|
17198 | 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)) { |
|
19003 | 379 |
INT _sz1 = __floatArraySize(self); |
380 |
INT i; |
|
381 |
float *_p1 = __FloatArrayInstPtr(self)->f_element; |
|
382 |
float v; |
|
383 |
float prev_p1; |
|
17198 | 384 |
|
19003 | 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; |
|
17198 | 394 |
|
19003 | 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); |
|
17198 | 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))) { |
|
19003 | 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; |
|
17198 | 438 |
|
19003 | 439 |
if (_sz1 > 0) { |
440 |
if (_sz2 >= _sz1) { |
|
441 |
float prev_p1 = _p1[0]; |
|
442 |
float prev_p2 = _p2[0]; |
|
17198 | 443 |
|
19003 | 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); |
|
17198 | 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)) { |
|
19003 | 474 |
INT _sz1 = __floatArraySize(self); |
475 |
INT i; |
|
476 |
float *_p1 = __FloatArrayInstPtr(self)->f_element; |
|
477 |
float v; |
|
478 |
float prev_p1; |
|
17198 | 479 |
|
19003 | 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; |
|
17198 | 489 |
|
19003 | 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); |
|
17198 | 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)) { |
|
19003 | 523 |
INT _sz = __floatArraySize(self); |
524 |
INT i; |
|
525 |
float *_p = __FloatArrayInstPtr(self)->f_element; |
|
526 |
float prev_p; |
|
17198 | 527 |
|
19003 | 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]; |
|
17198 | 533 |
|
19003 | 534 |
_p[i-1] = -prev_p; |
535 |
prev_p = next_p; |
|
536 |
} |
|
537 |
_p[i-1] = -prev_p; |
|
538 |
} |
|
539 |
RETURN (self); |
|
17198 | 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))) { |
|
19003 | 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; |
|
17198 | 568 |
|
19003 | 569 |
if (_sz1 > 0) { |
570 |
if (_sz2 >= _sz1) { |
|
571 |
float prev_p1 = _p1[0]; |
|
572 |
float prev_p2 = _p2[0]; |
|
17198 | 573 |
|
19003 | 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); |
|
17198 | 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)) { |
|
19003 | 606 |
INT _sz1 = __floatArraySize(self); |
607 |
INT i; |
|
608 |
float *_p1 = __FloatArrayInstPtr(self)->f_element; |
|
609 |
float v; |
|
610 |
float prev_p1; |
|
17198 | 611 |
|
19003 | 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; |
|
17198 | 621 |
|
19003 | 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); |
|
17198 | 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 |
||
20300 | 658 |
isValidElement:anObject |
659 |
"return true, if I can hold this kind of object" |
|
660 |
||
661 |
^ anObject isNumber |
|
662 |
! |
|
663 |
||
5737 | 664 |
max |
665 |
"return the largest element; |
|
666 |
redefined for speed" |
|
667 |
%{ |
|
8913 | 668 |
if (__ClassInstPtr(__qClass(self))->c_ninstvars == __mkSmallInteger(0)) { |
14692 | 669 |
INT _sz = __floatArraySize(self); |
5737 | 670 |
|
14692 | 671 |
if (_sz > 0) { |
672 |
float *_p = __FloatArrayInstPtr(self)->f_element; |
|
673 |
float _max; |
|
5737 | 674 |
|
14692 | 675 |
_max = _p[0]; |
676 |
if (_sz > 1) { |
|
677 |
INT _i; |
|
678 |
float _prev, _this; |
|
5737 | 679 |
|
14692 | 680 |
/* how about inline-mmx-asm for this ... */ |
681 |
_this = _p[1]; |
|
682 |
for (_i=2; _i<_sz; _i++) { |
|
683 |
_prev = _this; |
|
684 |
_this = _p[_i]; |
|
685 |
if (_prev > _max) _max = _prev; |
|
686 |
} |
|
687 |
if (_this > _max) _max = _this; |
|
688 |
} |
|
689 |
RETURN (__MKFLOAT(_max)); |
|
690 |
} |
|
7220 | 691 |
} |
5737 | 692 |
%}. |
7220 | 693 |
^ super max |
5737 | 694 |
|
7220 | 695 |
" |
696 |
|f1| |
|
5737 | 697 |
|
7220 | 698 |
f1 := (1 to:1000) asFloatArray. |
699 |
Time millisecondsToRun:[ 1000 timesRepeat:[ f1 max ] ] |
|
700 |
" |
|
701 |
" |
|
702 |
|f1| |
|
5737 | 703 |
|
7220 | 704 |
f1 := FloatArray withAll:#(1 2 3 4 5). |
14692 | 705 |
f1 max |
7220 | 706 |
" |
707 |
" |
|
708 |
|f1| |
|
5737 | 709 |
|
7220 | 710 |
f1 := FloatArray withAll:#(5 4 3 2 1). |
14692 | 711 |
f1 max |
7220 | 712 |
" |
5737 | 713 |
! |
714 |
||
715 |
min |
|
19003 | 716 |
"return the smallest element; |
5737 | 717 |
redefined for speed" |
718 |
%{ |
|
8913 | 719 |
if (__ClassInstPtr(__qClass(self))->c_ninstvars == __mkSmallInteger(0)) { |
14692 | 720 |
INT _sz = __floatArraySize(self); |
5737 | 721 |
|
14692 | 722 |
if (_sz > 0) { |
723 |
float *_p = __FloatArrayInstPtr(self)->f_element; |
|
724 |
float _min; |
|
5737 | 725 |
|
14692 | 726 |
_min = _p[0]; |
727 |
if (_sz > 1) { |
|
728 |
INT _i; |
|
729 |
float _prev, _this; |
|
5737 | 730 |
|
14692 | 731 |
/* how about inline-mmx-asm for this ... */ |
732 |
_this = _p[1]; |
|
733 |
for (_i=2; _i<_sz; _i++) { |
|
734 |
_prev = _this; |
|
735 |
_this = _p[_i]; |
|
736 |
if (_prev < _min) _min = _prev; |
|
737 |
} |
|
738 |
if (_this < _min) _min = _this; |
|
739 |
} |
|
740 |
RETURN (__MKFLOAT(_min)); |
|
741 |
} |
|
7220 | 742 |
} |
5737 | 743 |
%}. |
7220 | 744 |
^ super min |
5737 | 745 |
|
7220 | 746 |
" |
747 |
|f1| |
|
5737 | 748 |
|
7220 | 749 |
f1 := (1 to:1000) asFloatArray. |
750 |
Time millisecondsToRun:[ 1000 timesRepeat:[ f1 min ] ] |
|
751 |
" |
|
752 |
" |
|
753 |
|f1| |
|
5737 | 754 |
|
7220 | 755 |
f1 := FloatArray withAll:#(1 2 3 4 5). |
14692 | 756 |
f1 min |
7220 | 757 |
" |
758 |
" |
|
759 |
|f1| |
|
5737 | 760 |
|
7220 | 761 |
f1 := FloatArray withAll:#(5 4 3 2 1). |
14692 | 762 |
f1 min |
7220 | 763 |
" |
5737 | 764 |
! |
765 |
||
766 |
minMax |
|
11212 | 767 |
"return a Tuple holding the smallest and largest element; |
5737 | 768 |
redefined for speed" |
769 |
||
770 |
|min max| |
|
771 |
||
772 |
%{ |
|
8913 | 773 |
if (__ClassInstPtr(__qClass(self))->c_ninstvars == __mkSmallInteger(0)) { |
14692 | 774 |
INT _sz = __floatArraySize(self); |
775 |
if (_sz > 0) { |
|
776 |
INT _i; |
|
777 |
float *_p = __FloatArrayInstPtr(self)->f_element; |
|
778 |
float _min, _max; |
|
5737 | 779 |
|
14692 | 780 |
_min = _max = _p[0]; |
13774 | 781 |
#ifdef PRE_4_OCT_2011 |
14692 | 782 |
if (_sz > 1) { |
783 |
float _this = _p[1]; |
|
784 |
float _prev; |
|
7220 | 785 |
|
14692 | 786 |
/* how about inline-mmx-asm for this ... */ |
787 |
for (_i=2; _i<_sz; _i++) { |
|
788 |
_prev = _this; |
|
789 |
_this = _p[_i]; |
|
790 |
if (_prev < _min) { |
|
791 |
_min = _prev; |
|
792 |
} else if (_prev > _max) { |
|
793 |
_max = _prev; |
|
794 |
} |
|
795 |
} |
|
796 |
if (_this < _min) { |
|
797 |
_min = _this; |
|
798 |
} else if (_this > _max) { |
|
799 |
_max = _this; |
|
800 |
} |
|
801 |
} |
|
13774 | 802 |
#else |
14692 | 803 |
for (_i=_sz-1; _i>0; _i-=2) { |
804 |
float _v1 = _p[_i]; |
|
805 |
float _v2 = _p[_i-1]; |
|
806 |
if (_v1 < _v2) { |
|
807 |
if (_v1 < _min) _min = _v1; |
|
808 |
if (_v2 > _max) _max = _v2; |
|
809 |
} else { |
|
810 |
if (_v2 < _min) _min = _v2; |
|
811 |
if (_v1 > _max) _max = _v1; |
|
812 |
} |
|
813 |
} |
|
13774 | 814 |
#endif |
14692 | 815 |
min = __MKFLOAT(_min); |
816 |
max = __MKFLOAT(_max); |
|
817 |
RETURN (__ARRAY_WITH2(min, max)); |
|
818 |
} |
|
7220 | 819 |
} |
5737 | 820 |
%}. |
7220 | 821 |
^ Array with:(super min) with:(super max) |
5737 | 822 |
|
7220 | 823 |
" |
824 |
|f1| |
|
825 |
||
13774 | 826 |
f1 := (1 to:10000) asFloatArray. |
14692 | 827 |
Time millisecondsToRun:[ 1000 timesRepeat:[ f1 minMax ] ] |
7220 | 828 |
" |
829 |
" |
|
830 |
|f1| |
|
5737 | 831 |
|
7220 | 832 |
f1 := FloatArray withAll:#(1 2 3 4 5). |
14692 | 833 |
f1 minMax |
7220 | 834 |
" |
835 |
" |
|
836 |
|f1| |
|
5737 | 837 |
|
7220 | 838 |
f1 := FloatArray withAll:#(5 4 3 2 1). |
14692 | 839 |
f1 minMax |
7220 | 840 |
" |
13774 | 841 |
|
842 |
"Modified (comment): / 07-10-2011 / 13:03:30 / cg" |
|
10561 | 843 |
! |
844 |
||
12577 | 845 |
numFloats |
846 |
^ self size |
|
10561 | 847 |
! ! |
848 |
||
849 |
!FloatArray methodsFor:'vector arithmetic'! |
|
850 |
||
851 |
dot: aFloatVector |
|
852 |
"Return the dot product of the receiver and the argument. |
|
19032 | 853 |
Raises an error, if the argument is not of the same size as the receiver." |
10561 | 854 |
|
855 |
| mySize result | |
|
856 |
||
857 |
"/ <primitive:'primitiveDotProduct' module: 'FloatArrayPlugin'> |
|
858 |
%{ |
|
19032 | 859 |
if ((__ClassInstPtr(__qClass(self))->c_ninstvars == __mkSmallInteger(0)) |
860 |
&& (__ClassInstPtr(__qClass(aFloatVector))->c_ninstvars == __mkSmallInteger(0))) { |
|
861 |
INT __mySize = __floatArraySize(self); |
|
862 |
double __result = 0.0; |
|
863 |
float *__p1 = __FloatArrayInstPtr(self)->f_element; |
|
864 |
||
865 |
if (__mySize > 0) { |
|
866 |
if (__isFloats(aFloatVector)) { |
|
867 |
INT __otherSize = __floatArraySize(aFloatVector); |
|
17198 | 868 |
|
19032 | 869 |
if (__mySize == __otherSize) { |
870 |
float *__p2 = __FloatArrayInstPtr(aFloatVector)->f_element; |
|
871 |
INT __i; |
|
872 |
/* how about inline-mmx-asm for this ... */ |
|
873 |
for (__i=0; __i<__mySize; __i++) { |
|
874 |
__result += (__p1[__i] * __p2[__i]); |
|
875 |
} |
|
876 |
RETURN (__MKFLOAT(__result)); |
|
877 |
} |
|
878 |
} else if (__isDoubles(aFloatVector)) { |
|
879 |
INT __otherSize = __doubleArraySize(aFloatVector); |
|
880 |
||
881 |
if (__mySize == __otherSize) { |
|
882 |
double *__p2 = __DoubleArrayInstPtr(aFloatVector)->d_element; |
|
883 |
INT __i; |
|
884 |
/* how about inline-mmx-asm for this ... */ |
|
885 |
for (__i=0; __i<__mySize; __i++) { |
|
886 |
__result += (__p1[__i] * __p2[__i]); |
|
887 |
} |
|
888 |
RETURN (__MKFLOAT(__result)); |
|
889 |
} |
|
890 |
} |
|
891 |
} |
|
10561 | 892 |
} |
893 |
%}. |
|
15935 | 894 |
^ super dot:aFloatVector |
10561 | 895 |
|
896 |
" |
|
897 |
|v| |
|
11212 | 898 |
v := #(2.0 2.0 1.0) asFloatArray. |
19003 | 899 |
v dot:v. |
19032 | 900 |
|
901 |
|v| |
|
902 |
v := #(2.0 2.0 1.0) asDoubleArray. |
|
903 |
v dot:v. |
|
904 |
||
905 |
|v| |
|
906 |
v := #(2.0 2.0 1.0). |
|
907 |
v dot:v. |
|
908 |
||
909 |
|v1 v2| |
|
910 |
v1 := #(2.0 2.0 1.0) asFloatArray. |
|
911 |
v2 := #(2.0 2.0 1.0) asDoubleArray. |
|
912 |
v1 dot:v2. |
|
913 |
||
914 |
|v1 v2| |
|
915 |
v1 := #(2.0 2.0 1.0) asDoubleArray. |
|
916 |
v2 := #(2.0 2.0 1.0) asFloatArray. |
|
917 |
v1 dot:v2. |
|
918 |
||
919 |
|v1 v2| |
|
920 |
v1 := #(2.0 2.0 1.0). |
|
921 |
v2 := #(2.0 2.0 1.0) asFloatArray. |
|
922 |
v1 dot:v2. |
|
923 |
||
924 |
|v1 v2| |
|
925 |
v1 := #(2.0 2.0 1.0) asFloatArray. |
|
926 |
v2 := #(2.0 2.0 1.0). |
|
927 |
v1 dot:v2. |
|
928 |
||
929 |
|v1 v2| |
|
930 |
v1 := #(2.0 2.0 1.0). |
|
931 |
v2 := #(2.0 2.0 1.0) asDoubleArray. |
|
932 |
v1 dot:v2. |
|
933 |
||
934 |
|v1 v2| |
|
935 |
v1 := #(2.0 2.0 1.0) asDoubleArray. |
|
936 |
v2 := #(2.0 2.0 1.0). |
|
937 |
v1 dot:v2. |
|
938 |
" |
|
939 |
||
940 |
" |
|
941 |
|v1 v2| |
|
942 |
||
943 |
v1 := Array new:10000 withAll:2. |
|
944 |
v2 := Array new:10000 withAll:3. |
|
945 |
Time millisecondsToRun:[ |
|
946 |
10000 timesRepeat:[ |
|
947 |
v1 dot:v2. |
|
948 |
] |
|
949 |
] |
|
17198 | 950 |
" |
951 |
" |
|
952 |
|v1 v2| |
|
953 |
||
954 |
v1 := FloatArray new:10000 withAll:2. |
|
19003 | 955 |
v2 := FloatArray new:10000 withAll:3. |
17198 | 956 |
Time millisecondsToRun:[ |
19032 | 957 |
10000 timesRepeat:[ |
958 |
v1 dot:v2. |
|
959 |
] |
|
19003 | 960 |
] |
10561 | 961 |
" |
962 |
||
19032 | 963 |
" |
964 |
|v1 v2| |
|
965 |
v1 := #(2.0 2.0 1.0) asFloatArray. |
|
966 |
v2 := #(2.0 2.0 1.0 0.0) asDoubleArray. |
|
967 |
v1 dot:v2. |
|
968 |
" |
|
969 |
||
970 |
||
10561 | 971 |
"Created: / 29-05-2007 / 13:13:39 / cg" |
19032 | 972 |
! |
973 |
||
974 |
hornerMultiplyAndAdd:x |
|
975 |
"primitive support for horner's-method computation of polynomials. |
|
976 |
The vector is interpreted as providing the factors for a polynomial, |
|
977 |
an*x^n + (an-1)*x^(n-1) + ... + a2(x) + a1 |
|
978 |
where the ai are the elements of the Array. |
|
979 |
The highest rank factor is at the first position, the 0-rank constant at last. |
|
980 |
This is inlined c-code, which may get compiled to fast machine code, |
|
981 |
using multiply-and-add or vector instructions, if the CPU/Compiler support them." |
|
982 |
||
983 |
| mySize result | |
|
984 |
||
985 |
%{ |
|
986 |
double __x; |
|
987 |
||
988 |
if (__isFloat(x)) { |
|
989 |
__x = __floatVal(x); |
|
990 |
} else if (__isShortFloat(x)) { |
|
991 |
__x = (double)__shortFloatVal(x); |
|
992 |
} else if (__isSmallInteger(x)) { |
|
993 |
__x = (double)(__intVal(x)); |
|
994 |
} else { |
|
995 |
goto getOutOfHere; |
|
996 |
} |
|
997 |
||
998 |
if (__ClassInstPtr(__qClass(self))->c_ninstvars == __mkSmallInteger(0)) { |
|
999 |
INT __mySize = __floatArraySize(self); |
|
1000 |
float *__elements = __FloatArrayInstPtr(self)->f_element; |
|
1001 |
double __result = __elements[0]; |
|
1002 |
||
1003 |
if (__mySize > 1) { |
|
1004 |
INT __i; |
|
1005 |
/* how about inline-mmx-asm for this ... */ |
|
1006 |
for (__i=1; __i<__mySize; __i++) { |
|
1007 |
__result = (__result * __x) + __elements[__i]; |
|
1008 |
} |
|
1009 |
} |
|
1010 |
RETURN (__MKFLOAT(__result)); |
|
1011 |
} |
|
1012 |
getOutOfHere: ; |
|
1013 |
%}. |
|
1014 |
^ super hornerMultiplyAndAdd:x |
|
1015 |
||
1016 |
" |
|
1017 |
|v| |
|
1018 |
v := #(2.0 3.0 4.0) asFloatArray. |
|
1019 |
v hornerMultiplyAndAdd:10. |
|
1020 |
||
1021 |
|v| |
|
1022 |
v := Array new:100 withAll:2.0. |
|
1023 |
v hornerMultiplyAndAdd:10 |
|
1024 |
||
1025 |
|v| |
|
1026 |
v := FloatArray new:100 withAll:2.0. |
|
1027 |
v hornerMultiplyAndAdd:10 |
|
1028 |
||
1029 |
|v| |
|
1030 |
v := Array new:100 withAll:2.0. |
|
1031 |
Time millisecondsToRun:[ |
|
1032 |
10000 timesRepeat:[ v hornerMultiplyAndAdd:10] |
|
1033 |
] |
|
1034 |
||
1035 |
|v| |
|
1036 |
v := FloatArray new:100 withAll:2. |
|
1037 |
Time millisecondsToRun:[ |
|
1038 |
10000 timesRepeat:[ v hornerMultiplyAndAdd:10] |
|
1039 |
] |
|
1040 |
||
1041 |
|v| |
|
1042 |
v := DoubleArray new:100 withAll:2. |
|
1043 |
Time millisecondsToRun:[ |
|
1044 |
10000 timesRepeat:[ v hornerMultiplyAndAdd:10] |
|
1045 |
] |
|
1046 |
" |
|
5737 | 1047 |
! ! |
1048 |
||
628 | 1049 |
!FloatArray class methodsFor:'documentation'! |
603 | 1050 |
|
1051 |
version |
|
18577 | 1052 |
^ '$Header$' |
12577 | 1053 |
! |
1054 |
||
1055 |
version_CVS |
|
18577 | 1056 |
^ '$Header$' |
88 | 1057 |
! ! |
19032 | 1058 |