-
Notifications
You must be signed in to change notification settings - Fork 5
/
fp64-to-fp32-conversion.shader_test
277 lines (250 loc) · 8.53 KB
/
fp64-to-fp32-conversion.shader_test
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
# Conversion from double to float
# IEEE 754 compliant
[require]
GLSL >= 1.30
[vertex shader]
#version 130
void main()
{
gl_Position = gl_Vertex;
}
[fragment shader]
#version 130
/* Software IEEE floating-point rounding mode. */
uint float_rounding_mode;
const uint float_round_nearest_even = 0u;
const uint float_round_to_zero = 1u;
const uint float_round_down = 2u;
const uint float_round_up = 3u;
/* Packs the sign `zSign', exponent `zExp', and significand `zFrac' into a
* single-precision floating-point value, returning the result. After being
* shifted into the proper positions, the three fields are simply added
* together to form the result. This means that any integer portion of `zSig'
* will be added into the exponent. Since a properly normalized significand
* will have an integer portion equal to 1, the `zExp' input should be 1 less
* than the desired result exponent whenever `zFrac' is a complete, normalized
* significand.
*/
uint
packFloat32( uint zSign, uint zExp, uint zFrac )
{
return ( zSign<<31 ) + ( zExp<<23 ) + zFrac;
}
/* Shifts `a' right by the number of bits given in `count'. If any nonzero
* bits are shifted off, they are "jammed" into the least significant bit of
* the result by setting the least significant bit to 1. The value of `count'
* can be arbitrarily large; in particular, if `count' is greater than 32, the
* result will be either 0 or 1, depending on whether `a' is zero or nonzero.
* The result is stored in the location pointed to by `zPtr'.
*/
void
shift32RightJamming( uint a, int count, inout uint zPtr )
{
uint z;
if( count == 0 ) {
z = a;
} else if( count < 32 ) {
z = ( a>>count ) | uint ( ( a<<( ( - count ) & 31 ) ) != 0u );
} else {
z = uint ( a != 0u );
}
zPtr = z;
}
/* Shifts the 64-bit value formed by concatenating `a.x' and `a.y' right by the
* number of bits given in `count'. If any nonzero bits are shifted off, they
* are "jammed" into the least significant bit of the result by setting the
* least significant bit to 1. The value of `count' can be arbitrarily large;
* in particular, if `count' is greater than 64, the result will be either 0
* or 1, depending on whether the concatenation of `a.x' and `a.y' is zero or
* nonzero. The result is broken into two 32-bit pieces which are stored at
* the locations pointed to by `z0Ptr' and `z1Ptr'.
*/
void
shift64RightJamming( uvec2 a,
int count,
inout uint z0Ptr,
inout uint z1Ptr )
{
uint z0;
uint z1;
int negCount = ( - count ) & 31;
if ( count == 0 ) {
z1 = a.y;
z0 = a.x;
} else if ( count < 32 ) {
z1 = ( a.x<<negCount ) |
( a.y>>count ) |
uint ( ( a.y<<negCount ) != 0u );
z0 = a.x>>count;
} else {
if ( count == 32 ) {
z1 = a.x | uint ( a.y != 0u );
} else if ( count < 64 ) {
z1 = ( a.x>>( count & 31 ) ) |
uint ( ( ( a.x<<negCount ) | a.y ) != 0u );
} else {
z1 = uint ( ( a.x | a.y ) != 0u );
}
z0 = 0u;
}
z1Ptr = z1;
z0Ptr = z0;
}
/* Takes an abstract floating-point value having sign `zSign', exponent `zExp',
* and significand `zFrac', and returns the proper single-precision floating-
* point value corresponding to the abstract input. Ordinarily, the abstract
* value is simply rounded and packed into the single-precision format, with
* the inexact exception raised if the abstract input cannot be represented
* exactly. However, if the abstract value is too large, the overflow and
* inexact exceptions are raised and an infinity or maximal finite value is
* returned. If the abstract value is too small, the input value is rounded to
* a subnormal number, and the underflow and inexact exceptions are raised if
* the abstract input cannot be represented exactly as a subnormal single-
* precision floating-point number.
* The input significand `zFrac' has its binary point between bits 30
* and 29, which is 7 bits to the left of the usual location. This shifted
* significand must be normalized or smaller. If `zFrac' is not normalized,
* `zExp' must be 0; in that case, the result returned is a subnormal number,
* and it must not require rounding. In the usual case that `zFrac' is
* normalized, `zExp' must be 1 less than the "true" floating-point exponent.
* The handling of underflow and overflow follows the IEEE Standard for
* Floating-Point Arithmetic.
*/
uint
roundAndPackFloat32( uint zSign, uint zExp, uint zFrac )
{
uint roundingMode;
uint roundNearestEven;
uint roundIncrement;
uint roundBits;
roundingMode = float_rounding_mode;
roundNearestEven = uint ( roundingMode == float_round_nearest_even );
roundIncrement = 0x40u;
if ( roundNearestEven == 0u ) {
if ( roundingMode == float_round_to_zero ) {
roundIncrement = 0u;
} else {
roundIncrement = 0x7Fu;
if ( zSign != 0u ) {
if ( roundingMode == float_round_up ) {
roundIncrement = 0u;
}
} else {
if ( roundingMode == float_round_down ) {
roundIncrement = 0u;
}
}
}
}
roundBits = zFrac & 0x7Fu;
if ( 0xFDu <= zExp ) {
if ( ( 0xFDu < zExp ) ||
( ( zExp == 0xFDu ) && ( ( zFrac + roundIncrement ) < 0u ) ) ) {
return packFloat32(
zSign, 0xFFu, 0u ) - uint ( roundIncrement == 0u );
}
if ( zExp < 0u ) {
shift32RightJamming( zFrac, - int (zExp), zFrac );
zExp = 0u;
roundBits = zFrac & 0x7Fu;
}
}
zFrac = ( zFrac + roundIncrement )>>7;
zFrac &= ~ ( uint ( ( roundBits ^ 0x40u ) == 0u ) & roundNearestEven );
if ( zFrac == 0u ) {
zExp = 0u;
}
return packFloat32( zSign, zExp, zFrac );
}
/* Returns the fraction bits of the double-precision floating-point value `a'.*/
uvec2
extractFloat64Frac( uvec2 a )
{
return uvec2( a.x & 0x000FFFFFu, a.y );
}
/* Returns the exponent bits of the double-precision floating-point value `a'.*/
uint
extractFloat64Exp( uvec2 a )
{
return (a.x>>20) & 0x7FFu;
}
/* Returns the sign bit of the double-precision floating-point value `a'.*/
uint
extractFloat64Sign( uvec2 a )
{
return (a.x>>31);
}
/* Returns the result of converting the double-precision floating-point value
* `a' to the single-precision floating-point format. The conversion is
* performed according to the IEEE Standard for Floating-Point Arithmetic.
*/
uint
fp64_to_fp32( uvec2 a )
{
uint aSign;
uint aExp;
uint zFrac;
uint allZero;
uvec2 aFrac;
aFrac = extractFloat64Frac( a );
aExp = extractFloat64Exp( a );
aSign = extractFloat64Sign( a );
if ( aExp == 0x7FFu ) {
if ( ( aFrac.x | aFrac.y ) != 0u ) {
return ( aSign<<31 ) | 0x7FC00000u |
( ( aFrac.x & 0x000FFFFFu )<<3 ) | ( aFrac.y>>29 );
}
return packFloat32( aSign, 0xFFu, 0u );
}
shift64RightJamming( aFrac, 22, allZero, zFrac );
if ( aExp != 0u ) {
zFrac |= 0x40000000u;
}
return roundAndPackFloat32( aSign, aExp - 0x381u, zFrac );
}
uniform uvec2 a;
uniform uint expected;
void main()
{
/* Generate green if the expected value is producted, red
* otherwise.
*/
gl_FragColor = fp64_to_fp32(a) == expected
? vec4(0.0, 1.0, 0.0, 1.0)
: vec4(1.0, 0.0, 0.0, 1.0);
}
[test]
# A bunch of tests to run. The 'uniform' lines set the uniforms. The
# 'draw rect' line draws a rectangle that covers the whole window.
# The 'probe all' line verifies that every pixel contains the expected
# color.
# Try +Inf
uniform uvec2 a 0x7FF00000 0x00000000
uniform uint expected 0x7F800000
draw rect -1 -1 2 2
probe all rgba 0.0 1.0 0.0 1.0
# Try -Inf
uniform uvec2 a 0xFFF00000 0x00000000
uniform uint expected 0xFF800000
draw rect -1 -1 2 2
probe all rgba 0.0 1.0 0.0 1.0
# Try 50
uniform uvec2 a 0x40490000 0x00000000
uniform uint expected 0x42480000
draw rect -1 -1 2 2
probe all rgba 0.0 1.0 0.0 1.0
# Try -50
uniform uvec2 a 0xC0490000 0x00000000
uniform uint expected 0xC2480000
draw rect -1 -1 2 2
probe all rgba 0.0 1.0 0.0 1.0
# Try 1
uniform uvec2 a 0x3FF00000 0x00000000
uniform uint expected 0x3F800000
draw rect -1 -1 2 2
probe all rgba 0.0 1.0 0.0 1.0
# Try 0.4
uniform uvec2 a 0x3FD99999 0x9999999A
uniform uint expected 0x3ECCCCCD
draw rect -1 -1 2 2
probe all rgba 0.0 1.0 0.0 1.0