author | Claus Gittinger <cg@exept.de> |
Wed, 24 Aug 2016 17:06:12 +0200 | |
changeset 4044 | 1752b3c659b3 |
parent 3335 | deeaf91b9227 |
child 4769 | 89914ccfcf7d |
child 4834 | 1a3b148d9442 |
permissions | -rw-r--r-- |
4044 | 1 |
"{ Encoding: utf8 }" |
2 |
||
3159 | 3 |
" |
3221 | 4 |
COPYRIGHT (c) 2014 by Claus Gittinger |
5 |
All Rights Reserved |
|
3159 | 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 |
" |
|
14 |
"{ Package: 'stx:libbasic2' }" |
|
15 |
||
4044 | 16 |
"{ NameSpace: Smalltalk }" |
17 |
||
3159 | 18 |
AbstractNumberVector variableWordSubclass:#HalfFloatArray |
19 |
instanceVariableNames:'' |
|
20 |
classVariableNames:'' |
|
21 |
poolDictionaries:'' |
|
22 |
category:'Collections-Arrayed' |
|
23 |
! |
|
24 |
||
25 |
!HalfFloatArray primitiveFunctions! |
|
26 |
%{ |
|
27 |
typedef unsigned short halffloat; |
|
28 |
typedef unsigned short uint16; |
|
29 |
typedef unsigned int uint32; |
|
30 |
||
31 |
// |
|
32 |
// convert a halffloat (16-bit float) to a float |
|
33 |
// |
|
34 |
float |
|
35 |
__STX_halffloat_to_float(halffloat h) { |
|
36 |
int e; |
|
37 |
uint16 hs, he, hm; |
|
38 |
uint32 xs, xe, xm; |
|
39 |
int32 xes; |
|
40 |
union { |
|
41 |
uint32 u32; |
|
42 |
float f32; |
|
43 |
} u; |
|
44 |
||
45 |
if( (h & 0x7FFFu) == 0 ) { // Signed zero |
|
46 |
u.u32 = ((uint32) h) << 16; // Return the signed zero |
|
47 |
} else { // Not zero |
|
48 |
hs = h & 0x8000u; // Pick off sign bit |
|
49 |
he = h & 0x7C00u; // Pick off exponent bits |
|
50 |
hm = h & 0x03FFu; // Pick off mantissa bits |
|
51 |
if( he == 0 ) { // Denormal will convert to normalized |
|
52 |
e = -1; // The following loop figures out how much extra to adjust the exponent |
|
53 |
do { |
|
54 |
e++; |
|
55 |
hm <<= 1; |
|
56 |
} while( (hm & 0x0400u) == 0 ); // Shift until leading bit overflows into exponent bit |
|
57 |
xs = ((uint32) hs) << 16; // Sign bit |
|
58 |
xes = ((uint32) (he >> 10)) - 15 + 127 - e; // Exponent unbias the halfp, then bias the single |
|
59 |
xe = (uint32) (xes << 23); // Exponent |
|
60 |
xm = ((uint32) (hm & 0x03FFu)) << 13; // Mantissa |
|
61 |
u.u32 = (xs | xe | xm); // Combine sign bit, exponent bits, and mantissa bits |
|
62 |
} else if( he == 0x7C00u ) { // Inf or NaN (all the exponent bits are set) |
|
63 |
if( hm == 0 ) { // If mantissa is zero ... |
|
64 |
u.u32 = (((uint32) hs) << 16) | ((uint32) 0x7F800000u); // Signed Inf |
|
65 |
} else { |
|
66 |
u.u32 = (uint32) 0xFFC00000u; // NaN, only 1st mantissa bit set |
|
67 |
} |
|
68 |
} else { // Normalized number |
|
69 |
xs = ((uint32) hs) << 16; // Sign bit |
|
70 |
xes = ((uint32) (he >> 10)) - 15 + 127; // Exponent unbias the halfp, then bias the single |
|
71 |
xe = (uint32) (xes << 23); // Exponent |
|
72 |
xm = ((uint32) hm) << 13; // Mantissa |
|
73 |
u.u32 = (xs | xe | xm); // Combine sign bit, exponent bits, and mantissa bits |
|
74 |
} |
|
75 |
} |
|
76 |
return u.f32; |
|
77 |
} |
|
78 |
||
79 |
// |
|
80 |
// convert a float to a halffloat (16-bit float) |
|
81 |
// |
|
82 |
halffloat |
|
83 |
__STX_float_to_halffloat(float f32) { |
|
84 |
uint16 hs, he, hm; |
|
85 |
uint32 x, xs, xe, xm; |
|
86 |
int hes; |
|
87 |
union { |
|
88 |
uint32 u32; |
|
89 |
float f32; |
|
90 |
} u; |
|
91 |
halffloat h; |
|
92 |
||
93 |
u.f32 = f32; |
|
94 |
x = u.u32; |
|
95 |
if( (x & 0x7FFFFFFFu) == 0 ) { // Signed zero |
|
96 |
h = (uint16) (x >> 16); // Return the signed zero |
|
97 |
} else { // Not zero |
|
98 |
xs = x & 0x80000000u; // Pick off sign bit |
|
99 |
xe = x & 0x7F800000u; // Pick off exponent bits |
|
100 |
xm = x & 0x007FFFFFu; // Pick off mantissa bits |
|
101 |
if( xe == 0 ) { // Denormal will underflow, return a signed zero |
|
102 |
h = (uint16) (xs >> 16); |
|
103 |
} else if( xe == 0x7F800000u ) { // Inf or NaN (all the exponent bits are set) |
|
104 |
if( xm == 0 ) { // If mantissa is zero ... |
|
105 |
h = (uint16) ((xs >> 16) | 0x7C00u); // Signed Inf |
|
106 |
} else { |
|
107 |
h = (uint16) 0xFE00u; // NaN, only 1st mantissa bit set |
|
108 |
} |
|
109 |
} else { // Normalized number |
|
110 |
hs = (uint16) (xs >> 16); // Sign bit |
|
111 |
hes = ((int)(xe >> 23)) - 127 + 15; // Exponent unbias the single, then bias the halfp |
|
112 |
if( hes >= 0x1F ) { // Overflow |
|
113 |
h = (uint16) ((xs >> 16) | 0x7C00u); // Signed Inf |
|
114 |
} else if( hes <= 0 ) { // Underflow |
|
115 |
if( (14 - hes) > 24 ) { // Mantissa shifted all the way off & no rounding possibility |
|
116 |
hm = (uint16) 0u; // Set mantissa to zero |
|
117 |
} else { |
|
118 |
xm |= 0x00800000u; // Add the hidden leading bit |
|
119 |
hm = (uint16) (xm >> (14 - hes)); // Mantissa |
|
120 |
if( (xm >> (13 - hes)) & 0x00000001u ) // Check for rounding |
|
121 |
hm += (uint16) 1u; // Round, might overflow into exp bit, but this is OK |
|
122 |
} |
|
123 |
h = (hs | hm); // Combine sign bit and mantissa bits, biased exponent is zero |
|
124 |
} else { |
|
125 |
he = (uint16) (hes << 10); // Exponent |
|
126 |
hm = (uint16) (xm >> 13); // Mantissa |
|
127 |
if( xm & 0x00001000u ) // Check for rounding |
|
128 |
h = (hs | he | hm) + (uint16) 1u; // Round, might overflow to inf, this is OK |
|
129 |
else |
|
130 |
h = (hs | he | hm); // No rounding |
|
131 |
} |
|
132 |
} |
|
133 |
} |
|
134 |
return h; |
|
135 |
} |
|
136 |
||
137 |
%} |
|
3160 | 138 |
! ! |
3159 | 139 |
|
140 |
!HalfFloatArray class methodsFor:'documentation'! |
|
141 |
||
142 |
copyright |
|
143 |
" |
|
3221 | 144 |
COPYRIGHT (c) 2014 by Claus Gittinger |
145 |
All Rights Reserved |
|
3159 | 146 |
|
147 |
This software is furnished under a license and may be used |
|
148 |
only in accordance with the terms of that license and with the |
|
149 |
inclusion of the above copyright notice. This software may not |
|
150 |
be provided or otherwise made available to, or used by, any |
|
151 |
other person. No title to or ownership of the software is |
|
152 |
hereby transferred. |
|
153 |
" |
|
154 |
! |
|
155 |
||
156 |
documentation |
|
157 |
" |
|
158 |
HalfFloatArrays store half precision (16bit) floats (and nothing else). |
|
3221 | 159 |
HalfFloats were traditionally seldom used, but seem to become more popular |
3159 | 160 |
these days, as some 3D graphics accelerators and game engines use them |
3304 | 161 |
for very dense and compact storage of texture and vertex data. |
3160 | 162 |
|
3304 | 163 |
Notice, that HalfFloats are not supported as first class objects by the ST/X system; |
3160 | 164 |
i.e. outside of a HalfFloatArray, these values are represented as floats |
3304 | 165 |
or doubles. When accessing a HalfFloatArray's element via getters/setters, |
166 |
shortFloat (i.e. single precision 32bit floats) are exchanged. |
|
167 |
||
3160 | 168 |
Be aware that the numeric range of a half-float is very very limited. |
3159 | 169 |
|
170 |
[memory requirements:] |
|
3160 | 171 |
OBJ-HEADER + (size * 2) |
3159 | 172 |
|
173 |
[See also:] |
|
3160 | 174 |
FloatArray DoubleArray Array |
3304 | 175 |
http://www.opengl.org/wiki/Small_Float_Formats |
3159 | 176 |
|
177 |
[author:] |
|
3160 | 178 |
Claus Gittinger |
3159 | 179 |
" |
180 |
! ! |
|
181 |
||
182 |
!HalfFloatArray class methodsFor:'queries'! |
|
183 |
||
184 |
elementByteSize |
|
3335
deeaf91b9227
comment/format in: #elementByteSize
Claus Gittinger <cg@exept.de>
parents:
3305
diff
changeset
|
185 |
"for bit-like containers, return the number of bytes stored per element. |
deeaf91b9227
comment/format in: #elementByteSize
Claus Gittinger <cg@exept.de>
parents:
3305
diff
changeset
|
186 |
Here, 2 is returned" |
deeaf91b9227
comment/format in: #elementByteSize
Claus Gittinger <cg@exept.de>
parents:
3305
diff
changeset
|
187 |
|
3159 | 188 |
^ 2 |
189 |
! ! |
|
190 |
||
191 |
!HalfFloatArray methodsFor:'accessing'! |
|
192 |
||
193 |
at:index |
|
194 |
%{ /* NOCONTEXT */ |
|
195 |
if (__isSmallInteger(index)) { |
|
196 |
int i = __intVal(index); |
|
3169 | 197 |
int n = __wordArraySize(self); |
3159 | 198 |
|
3169 | 199 |
if ((unsigned)i <= n) { |
3159 | 200 |
unsigned short h; |
201 |
OBJ newFloat; |
|
202 |
float f; |
|
203 |
||
3169 | 204 |
h = __WordArrayInstPtr(self)->s_element[i-1]; |
3159 | 205 |
|
206 |
f = __STX_halffloat_to_float(h); |
|
207 |
__qMKSFLOAT(newFloat, f); |
|
208 |
RETURN ( newFloat ); |
|
209 |
} |
|
210 |
} |
|
211 |
%}. |
|
212 |
self primitiveFailed |
|
213 |
! |
|
214 |
||
215 |
at:index put:aFloat |
|
216 |
%{ |
|
217 |
if (__isSmallInteger(index)) { |
|
218 |
int i = __intVal(index); |
|
3169 | 219 |
int n = __wordArraySize(self); |
3159 | 220 |
|
3169 | 221 |
if ((unsigned)i <= n) { |
3159 | 222 |
unsigned short h; |
223 |
float f; |
|
224 |
||
225 |
if (__isFloat(aFloat)) { |
|
226 |
f = (float)(__floatVal(aFloat)); |
|
227 |
} else if (__isShortFloat(aFloat)) { |
|
228 |
f = __shortFloatVal(aFloat); |
|
229 |
} else if (__isSmallInteger(aFloat)) { |
|
230 |
f = (float)(__intVal(aFloat)); |
|
231 |
} else |
|
232 |
goto error; |
|
233 |
||
234 |
h = __STX_float_to_halffloat(f); |
|
3169 | 235 |
__WordArrayInstPtr(self)->s_element[i-1] = h; |
3159 | 236 |
RETURN (aFloat); |
237 |
} |
|
238 |
} |
|
239 |
error: ; |
|
240 |
%}. |
|
241 |
self primitiveFailed |
|
242 |
! ! |
|
243 |
||
244 |
!HalfFloatArray methodsFor:'queries'! |
|
245 |
||
246 |
defaultElement |
|
247 |
^ ShortFloat zero |
|
248 |
! |
|
249 |
||
4044 | 250 |
isValidElement:anObject |
251 |
"return true, if I can hold this kind of object" |
|
252 |
||
253 |
^ anObject isNumber |
|
254 |
! |
|
255 |
||
3159 | 256 |
numFloats |
257 |
^ self size |
|
258 |
! ! |
|
259 |
||
260 |
!HalfFloatArray class methodsFor:'documentation'! |
|
261 |
||
3305 | 262 |
version |
4044 | 263 |
^ '$Header$' |
3305 | 264 |
! |
265 |
||
3159 | 266 |
version_CVS |
4044 | 267 |
^ '$Header$' |
3159 | 268 |
! ! |
3160 | 269 |