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