author | Claus Gittinger <cg@exept.de> |
Fri, 15 Jan 2016 13:17:45 +0100 | |
changeset 19031 | b8bbe1983d3a |
parent 16853 | e3e062d68503 |
child 19035 | 06aeb1259ba6 |
child 20301 | af76baeacf78 |
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 |
" |
|
7220 | 12 |
"{ Package: 'stx:libbasic' }" |
13 |
||
19031 | 14 |
"{ NameSpace: Smalltalk }" |
15 |
||
13825 | 16 |
AbstractNumberVector variableDoubleSubclass:#DoubleArray |
1263 | 17 |
instanceVariableNames:'' |
18 |
classVariableNames:'' |
|
19 |
poolDictionaries:'' |
|
20 |
category:'Collections-Arrayed' |
|
1 | 21 |
! |
22 |
||
88 | 23 |
!DoubleArray class methodsFor:'documentation'! |
24 |
||
25 |
copyright |
|
26 |
" |
|
27 |
COPYRIGHT (c) 1993 by Claus Gittinger |
|
1 | 28 |
All Rights Reserved |
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 |
" |
|
13825 | 41 |
DoubleArrays store doubleFloat values (and nothing else). |
16639 | 42 |
They have been added to support heavy duty number crunching and |
43 |
data exchange with openGL frameworks and other mass data libraries |
|
44 |
somewhat better than other smalltalks do. |
|
362 | 45 |
Storing Floats & Doubles in these objects (instead of Arrays) |
46 |
has some benefits: |
|
1 | 47 |
|
88 | 48 |
1) since the values are stored directly (instead of pointers to them) |
49 |
both access overhead and garbage collect overhead is minimized. |
|
50 |
||
51 |
2) they can be much faster passed to c functions (such as graphics |
|
52 |
libraries or heavy duty math packages), since the double values |
|
53 |
come packed and can be used in C by using a (double *) or double[]. |
|
362 | 54 |
There is no need to loop over the array extracting doubles. |
88 | 55 |
|
56 |
3) they could (in theory) be much more easily be processed by things like |
|
57 |
vector and array processors |
|
1 | 58 |
|
88 | 59 |
Be aware however, that Float- and DoubleArrays are not supported in other |
60 |
smalltalks - your program will thus become somewhat less portable. |
|
61 |
(since their protocol is the same as normal arrays filled with floats, |
|
62 |
they can of course be easily simulated - a bit slower though) |
|
1 | 63 |
|
2820 | 64 |
However, they could be simulated by a ByteArray, using doubleAt: and |
92 | 65 |
doubleAtPut: messages to access the elements, but that seems a bit |
66 |
clumsy and unelegant. Also, the stc-compiler may learn how to deal |
|
67 |
with Float- and DoubleArrays, making accesses very fast in the future. |
|
2820 | 68 |
Hint: if you use doubleArrays in your application and must port it |
69 |
to some other smalltalk, define a DoubleArray class there, which is derived |
|
70 |
from ByteArray, and add access methods. |
|
1 | 71 |
|
2145 | 72 |
Of course, DoubleArray can be subclassed, |
73 |
and named instance variables can be added there. |
|
1 | 74 |
|
362 | 75 |
See example uses in the GLX interface and GLDemos. |
1263 | 76 |
|
4062 | 77 |
[memory requirements:] |
78 |
OBJ-HEADER + (size * double-size) |
|
79 |
||
1263 | 80 |
[See also:] |
81 |
FloatArray Array |
|
1290 | 82 |
|
83 |
[author:] |
|
84 |
Claus Gittinger |
|
88 | 85 |
" |
627 | 86 |
! ! |
87 |
||
13708
079c1e1c7594
faster elementByteSize query
Claus Gittinger <cg@exept.de>
parents:
7220
diff
changeset
|
88 |
!DoubleArray class methodsFor:'queries'! |
079c1e1c7594
faster elementByteSize query
Claus Gittinger <cg@exept.de>
parents:
7220
diff
changeset
|
89 |
|
079c1e1c7594
faster elementByteSize query
Claus Gittinger <cg@exept.de>
parents:
7220
diff
changeset
|
90 |
elementByteSize |
16850
4011fc651793
comment/format in: #elementByteSize
Claus Gittinger <cg@exept.de>
parents:
16639
diff
changeset
|
91 |
"for bit-like containers, return the number of bytes stored per element. |
4011fc651793
comment/format in: #elementByteSize
Claus Gittinger <cg@exept.de>
parents:
16639
diff
changeset
|
92 |
Here, 8 is returned" |
4011fc651793
comment/format in: #elementByteSize
Claus Gittinger <cg@exept.de>
parents:
16639
diff
changeset
|
93 |
|
13708
079c1e1c7594
faster elementByteSize query
Claus Gittinger <cg@exept.de>
parents:
7220
diff
changeset
|
94 |
^ 8 |
079c1e1c7594
faster elementByteSize query
Claus Gittinger <cg@exept.de>
parents:
7220
diff
changeset
|
95 |
|
079c1e1c7594
faster elementByteSize query
Claus Gittinger <cg@exept.de>
parents:
7220
diff
changeset
|
96 |
"Created: / 15-09-2011 / 14:12:46 / cg" |
079c1e1c7594
faster elementByteSize query
Claus Gittinger <cg@exept.de>
parents:
7220
diff
changeset
|
97 |
! ! |
079c1e1c7594
faster elementByteSize query
Claus Gittinger <cg@exept.de>
parents:
7220
diff
changeset
|
98 |
|
7220 | 99 |
!DoubleArray methodsFor:'queries'! |
100 |
||
101 |
defaultElement |
|
102 |
^ Float zero |
|
103 |
! ! |
|
104 |
||
19031 | 105 |
!DoubleArray methodsFor:'vector arithmetic'! |
106 |
||
107 |
dot: aFloatVector |
|
108 |
"Return the dot product of the receiver and the argument. |
|
109 |
Raises an error, if the argument is not of the same size as the receiver." |
|
110 |
||
111 |
| mySize result | |
|
112 |
||
113 |
%{ |
|
114 |
if ((__ClassInstPtr(__qClass(self))->c_ninstvars == __mkSmallInteger(0)) |
|
115 |
&& (__ClassInstPtr(__qClass(aFloatVector))->c_ninstvars == __mkSmallInteger(0))) { |
|
116 |
INT __mySize = __doubleArraySize(self); |
|
117 |
double __result = 0.0; |
|
118 |
double *__p1 = __DoubleArrayInstPtr(self)->d_element; |
|
119 |
||
120 |
if (__mySize > 0) { |
|
121 |
if (__isFloats(aFloatVector)) { |
|
122 |
INT __otherSize = __floatArraySize(aFloatVector); |
|
123 |
||
124 |
if (__mySize == __otherSize) { |
|
125 |
float *__p2 = __FloatArrayInstPtr(aFloatVector)->f_element; |
|
126 |
INT __i; |
|
127 |
/* how about inline-mmx-asm for this ... */ |
|
128 |
for (__i=0; __i<__mySize; __i++) { |
|
129 |
__result += (__p1[__i] * __p2[__i]); |
|
130 |
} |
|
131 |
RETURN (__MKFLOAT(__result)); |
|
132 |
} |
|
133 |
} else if (__isDoubles(aFloatVector)) { |
|
134 |
INT __otherSize = __doubleArraySize(aFloatVector); |
|
135 |
||
136 |
if (__mySize == __otherSize) { |
|
137 |
double *__p2 = __DoubleArrayInstPtr(aFloatVector)->d_element; |
|
138 |
INT __i; |
|
139 |
/* how about inline-mmx-asm for this ... */ |
|
140 |
for (__i=0; __i<__mySize; __i++) { |
|
141 |
__result += (__p1[__i] * __p2[__i]); |
|
142 |
} |
|
143 |
RETURN (__MKFLOAT(__result)); |
|
144 |
} |
|
145 |
} |
|
146 |
} |
|
147 |
} |
|
148 |
%}. |
|
149 |
^ super dot:aFloatVector |
|
150 |
||
151 |
" |
|
152 |
|v| |
|
153 |
||
154 |
v := #(2.0 2.0 1.0) asFloatArray. |
|
155 |
v dot:v. |
|
156 |
" |
|
157 |
" |
|
158 |
|v1 v2| |
|
159 |
||
160 |
v1 := FloatArray new:10000 withAll:2. |
|
161 |
v2 := FloatArray new:10000 withAll:3. |
|
162 |
Time millisecondsToRun:[ |
|
163 |
10000 timesRepeat:[ |
|
164 |
v1 dot:v2. |
|
165 |
] |
|
166 |
] |
|
167 |
" |
|
168 |
||
169 |
"Created: / 29-05-2007 / 13:13:39 / cg" |
|
170 |
! |
|
171 |
||
172 |
hornerMultiplyAndAdd:x |
|
173 |
"primitive support for horner's-method computation of polynomials. |
|
174 |
The vector is interpreted as providing the factors for a polynomial, |
|
175 |
an*x^n + (an-1)*x^(n-1) + ... + a2(x) + a1 |
|
176 |
where the ai are the elements of the Array. |
|
177 |
The highest rank factor is at the first position, the 0-rank constant at last. |
|
178 |
This is inlined c-code, which may get compiled to fast machine code, |
|
179 |
using multiply-and-add or vector instructions, if the CPU/Compiler support them." |
|
180 |
||
181 |
| mySize result | |
|
182 |
||
183 |
%{ |
|
184 |
double __x; |
|
185 |
||
186 |
if (__isFloat(x)) { |
|
187 |
__x = __floatVal(x); |
|
188 |
} else if (__isShortFloat(x)) { |
|
189 |
__x = (double)__shortFloatVal(x); |
|
190 |
} else if (__isSmallInteger(x)) { |
|
191 |
__x = (double)(__intVal(x)); |
|
192 |
} else { |
|
193 |
goto getOutOfHere; |
|
194 |
} |
|
195 |
||
196 |
if (__ClassInstPtr(__qClass(self))->c_ninstvars == __mkSmallInteger(0)) { |
|
197 |
INT __mySize = __doubleArraySize(self); |
|
198 |
double *__elements = __DoubleArrayInstPtr(self)->d_element; |
|
199 |
double __result = __elements[0]; |
|
200 |
||
201 |
if (__mySize > 1) { |
|
202 |
INT __i; |
|
203 |
/* how about inline-mmx-asm for this ... */ |
|
204 |
for (__i=1; __i<__mySize; __i++) { |
|
205 |
__result = (__result * __x) + __elements[__i]; |
|
206 |
} |
|
207 |
} |
|
208 |
RETURN (__MKFLOAT(__result)); |
|
209 |
} |
|
210 |
getOutOfHere: ; |
|
211 |
%}. |
|
212 |
^ super hornerMultiplyAndAdd:x |
|
213 |
||
214 |
" |
|
215 |
|v| |
|
216 |
v := #(2.0 3.0 4.0) asDoubleArray. |
|
217 |
v hornerMultiplyAndAdd:10. |
|
218 |
||
219 |
|v| |
|
220 |
v := Array new:100 withAll:2.0. |
|
221 |
v hornerMultiplyAndAdd:10 |
|
222 |
||
223 |
|v| |
|
224 |
v := Array new:100 withAll:2.0. |
|
225 |
Time millisecondsToRun:[ |
|
226 |
10000 timesRepeat:[ v hornerMultiplyAndAdd:10] |
|
227 |
] |
|
228 |
||
229 |
|v| |
|
230 |
v := FloatArray new:100 withAll:2. |
|
231 |
Time millisecondsToRun:[ |
|
232 |
10000 timesRepeat:[ v hornerMultiplyAndAdd:10] |
|
233 |
] |
|
234 |
||
235 |
|v| |
|
236 |
v := DoubleArray new:100 withAll:2. |
|
237 |
Time millisecondsToRun:[ |
|
238 |
10000 timesRepeat:[ v hornerMultiplyAndAdd:10] |
|
239 |
] |
|
240 |
" |
|
241 |
! ! |
|
242 |
||
627 | 243 |
!DoubleArray class methodsFor:'documentation'! |
565 | 244 |
|
245 |
version |
|
19031 | 246 |
^ '$Header$' |
16853 | 247 |
! |
248 |
||
249 |
version_CVS |
|
19031 | 250 |
^ '$Header$' |
88 | 251 |
! ! |
16639 | 252 |