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