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