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