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