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