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