author | Claus Gittinger <cg@exept.de> |
Tue, 25 Jun 2019 14:28:51 +0200 | |
changeset 5050 | 44fa8672d102 |
parent 5015 | c4c87284e2ed |
child 5055 | f27b17f602ed |
permissions | -rw-r--r-- |
4982 | 1 |
"{ Encoding: utf8 }" |
2 |
||
3159 | 3 |
" |
3221 | 4 |
COPYRIGHT (c) 2014 by Claus Gittinger |
4980 | 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 |
||
4835 | 18 |
UnboxedFloatArray variableWordSubclass:#HalfFloatArray |
3159 | 19 |
instanceVariableNames:'' |
20 |
classVariableNames:'' |
|
21 |
poolDictionaries:'' |
|
22 |
category:'Collections-Arrayed' |
|
23 |
! |
|
24 |
||
25 |
!HalfFloatArray primitiveFunctions! |
|
26 |
%{ |
|
27 |
typedef unsigned short uint16; |
|
28 |
typedef unsigned int uint32; |
|
4889 | 29 |
#if 0 |
4888 | 30 |
#if defined(__GNUC__) || defined(__CLANG__) || defined(__MINGW__) |
4980 | 31 |
inline float |
4991 | 32 |
XMConvertHalfToFloat(float16_t Value) { |
4980 | 33 |
__m128i V1 = _mm_cvtsi32_si128( (uint32)(Value) ); |
4888 | 34 |
__m128 V2 = _mm_cvtph_ps( V1 ); |
35 |
return _mm_cvtss_f32( V2 ); |
|
36 |
} |
|
37 |
||
4991 | 38 |
inline float16_t |
4888 | 39 |
XMConvertFloatToHalf(float Value) { |
40 |
__m128 V1 = _mm_set_ss( Value ); |
|
41 |
__m128i V2 = _mm_cvtps_ph( V1, 0 ); |
|
4991 | 42 |
return (float16_t)( _mm_cvtsi128_si32(V2) ); |
4888 | 43 |
} |
44 |
#endif |
|
4889 | 45 |
#endif |
3159 | 46 |
// |
47 |
// convert a halffloat (16-bit float) to a float |
|
48 |
// |
|
4991 | 49 |
extern float __STX_halffloat_to_float(float16_t h); |
3159 | 50 |
|
51 |
// |
|
52 |
// convert a float to a halffloat (16-bit float) |
|
53 |
// |
|
4991 | 54 |
extern float16_t __STX_float_to_halffloat(float f32); |
3159 | 55 |
|
56 |
%} |
|
3160 | 57 |
! ! |
3159 | 58 |
|
59 |
!HalfFloatArray class methodsFor:'documentation'! |
|
60 |
||
61 |
copyright |
|
62 |
" |
|
3221 | 63 |
COPYRIGHT (c) 2014 by Claus Gittinger |
4980 | 64 |
All Rights Reserved |
3159 | 65 |
|
66 |
This software is furnished under a license and may be used |
|
67 |
only in accordance with the terms of that license and with the |
|
68 |
inclusion of the above copyright notice. This software may not |
|
69 |
be provided or otherwise made available to, or used by, any |
|
70 |
other person. No title to or ownership of the software is |
|
71 |
hereby transferred. |
|
72 |
" |
|
73 |
! |
|
74 |
||
75 |
documentation |
|
76 |
" |
|
77 |
HalfFloatArrays store half precision (16bit) floats (and nothing else). |
|
3221 | 78 |
HalfFloats were traditionally seldom used, but seem to become more popular |
3159 | 79 |
these days, as some 3D graphics accelerators and game engines use them |
3304 | 80 |
for very dense and compact storage of texture and vertex data. |
3160 | 81 |
|
3304 | 82 |
Notice, that HalfFloats are not supported as first class objects by the ST/X system; |
3160 | 83 |
i.e. outside of a HalfFloatArray, these values are represented as floats |
3304 | 84 |
or doubles. When accessing a HalfFloatArray's element via getters/setters, |
85 |
shortFloat (i.e. single precision 32bit floats) are exchanged. |
|
86 |
||
3160 | 87 |
Be aware that the numeric range of a half-float is very very limited. |
3159 | 88 |
|
89 |
[memory requirements:] |
|
4991 | 90 |
OBJ-HEADER + (size * 2) |
3159 | 91 |
|
92 |
[See also:] |
|
4991 | 93 |
FloatArray DoubleArray Array |
94 |
http://www.opengl.org/wiki/Small_Float_Formats |
|
95 |
https://en.wikipedia.org/wiki/Half-precision |
|
96 |
||
3159 | 97 |
[author:] |
4991 | 98 |
Claus Gittinger |
3159 | 99 |
" |
100 |
! ! |
|
101 |
||
102 |
!HalfFloatArray class methodsFor:'queries'! |
|
103 |
||
104 |
elementByteSize |
|
3335
deeaf91b9227
comment/format in: #elementByteSize
Claus Gittinger <cg@exept.de>
parents:
3305
diff
changeset
|
105 |
"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
|
106 |
Here, 2 is returned" |
deeaf91b9227
comment/format in: #elementByteSize
Claus Gittinger <cg@exept.de>
parents:
3305
diff
changeset
|
107 |
|
3159 | 108 |
^ 2 |
109 |
! ! |
|
110 |
||
111 |
!HalfFloatArray methodsFor:'accessing'! |
|
112 |
||
113 |
at:index |
|
114 |
%{ /* NOCONTEXT */ |
|
115 |
if (__isSmallInteger(index)) { |
|
116 |
int i = __intVal(index); |
|
3169 | 117 |
int n = __wordArraySize(self); |
3159 | 118 |
|
3169 | 119 |
if ((unsigned)i <= n) { |
4991 | 120 |
float16_t h; |
3159 | 121 |
OBJ newFloat; |
122 |
float f; |
|
123 |
||
4991 | 124 |
h = (float16_t)(__WordArrayInstPtr(self)->s_element[i-1]); |
3159 | 125 |
|
126 |
f = __STX_halffloat_to_float(h); |
|
127 |
__qMKSFLOAT(newFloat, f); |
|
128 |
RETURN ( newFloat ); |
|
129 |
} |
|
130 |
} |
|
131 |
%}. |
|
132 |
self primitiveFailed |
|
133 |
! |
|
134 |
||
135 |
at:index put:aFloat |
|
136 |
%{ |
|
5015 | 137 |
if (! __isImmutable(self)) { |
138 |
if (__isSmallInteger(index)) { |
|
139 |
int i = __intVal(index); |
|
140 |
int n = __wordArraySize(self); |
|
3159 | 141 |
|
5015 | 142 |
if ((unsigned)i <= n) { |
143 |
float16_t h; |
|
144 |
float f; |
|
3159 | 145 |
|
5015 | 146 |
if (__isFloat(aFloat)) { |
147 |
f = (float)(__floatVal(aFloat)); |
|
148 |
} else if (__isShortFloat(aFloat)) { |
|
149 |
f = __shortFloatVal(aFloat); |
|
150 |
} else if (__isSmallInteger(aFloat)) { |
|
151 |
f = (float)(__intVal(aFloat)); |
|
152 |
} else |
|
153 |
goto error; |
|
3159 | 154 |
|
5015 | 155 |
h = __STX_float_to_halffloat(f); |
156 |
__WordArrayInstPtr(self)->s_element[i-1] = h; |
|
157 |
RETURN (aFloat); |
|
158 |
} |
|
159 |
} |
|
3159 | 160 |
} |
161 |
error: ; |
|
162 |
%}. |
|
5015 | 163 |
"/ arrive here only in case of an error |
164 |
self isImmutable ifTrue:[ |
|
165 |
self noModificationError |
|
166 |
]. |
|
3159 | 167 |
self primitiveFailed |
5015 | 168 |
|
169 |
"Modified: / 09-06-2019 / 15:32:32 / Claus Gittinger" |
|
3159 | 170 |
! ! |
171 |
||
172 |
!HalfFloatArray methodsFor:'queries'! |
|
173 |
||
174 |
defaultElement |
|
175 |
^ ShortFloat zero |
|
176 |
! |
|
177 |
||
4044 | 178 |
isValidElement:anObject |
179 |
"return true, if I can hold this kind of object" |
|
180 |
||
181 |
^ anObject isNumber |
|
182 |
! |
|
183 |
||
3159 | 184 |
numFloats |
185 |
^ self size |
|
186 |
! ! |
|
187 |
||
4834 | 188 |
!HalfFloatArray methodsFor:'testing'! |
189 |
||
190 |
isFloatArray |
|
191 |
"return true if the receiver has float elements. |
|
192 |
These are Float, Double- and HalfFloat arrays" |
|
193 |
||
194 |
^ true |
|
195 |
||
196 |
"Created: / 02-03-2019 / 23:14:00 / Claus Gittinger" |
|
197 |
! ! |
|
198 |
||
3159 | 199 |
!HalfFloatArray class methodsFor:'documentation'! |
200 |
||
3305 | 201 |
version |
4044 | 202 |
^ '$Header$' |
3305 | 203 |
! |
204 |
||
3159 | 205 |
version_CVS |
4044 | 206 |
^ '$Header$' |
3159 | 207 |
! ! |
5015 | 208 |