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