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
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
|
/**
* Copyright 2013 JogAmp Community. All rights reserved.
*
* Redistribution and use in source and binary forms, with or without modification, are
* permitted provided that the following conditions are met:
*
* 1. Redistributions of source code must retain the above copyright notice, this list of
* conditions and the following disclaimer.
*
* 2. Redistributions in binary form must reproduce the above copyright notice, this list
* of conditions and the following disclaimer in the documentation and/or other materials
* provided with the distribution.
*
* THIS SOFTWARE IS PROVIDED BY JogAmp Community ``AS IS'' AND ANY EXPRESS OR IMPLIED
* WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND
* FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL JogAmp Community OR
* CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
* CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
* SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON
* ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
* NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
* ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*
* The views and conclusions contained in the software and documentation are those of the
* authors and should not be interpreted as representing official policies, either expressed
* or implied, of JogAmp Community.
*/
package com.jogamp.opengl.math;
/**
* <p>
* Functions to convert values to/from the <code>binary16</code> format
* specified in <code>IEEE 754 2008</code>.
* </p>
*/
public final class Binary16
{
/**
* The encoded form of negative infinity <code>-∞</code>.
*/
public static final char NEGATIVE_INFINITY;
/**
* The encoded form of positive infinity <code>∞</code>.
*/
public static final char POSITIVE_INFINITY;
/**
* The encoded form of positive zero <code>0</code>.
*/
public static final char POSITIVE_ZERO;
/**
* The encoded form of negative zero <code>-0</code>.
*/
public static final char NEGATIVE_ZERO;
/**
* The <i>bias</i> value used to offset the encoded exponent. A given
* exponent <code>e</code> is encoded as <code>{@link #BIAS} + e</code>.
*/
public static final int BIAS;
static {
NEGATIVE_INFINITY = 0xFC00;
POSITIVE_INFINITY = 0x7C00;
POSITIVE_ZERO = 0x0000;
NEGATIVE_ZERO = 0x8000;
BIAS = 15;
}
private static final int MASK_SIGN;
private static final int MASK_EXPONENT;
private static final int MASK_SIGNIFICAND;
static {
MASK_SIGN = 0x8000;
MASK_EXPONENT = 0x7C00;
MASK_SIGNIFICAND = 0x03FF;
}
/**
* One possible not-a-number value.
*/
public static char exampleNaN()
{
final int n =
Binary16.packSetExponentUnbiasedUnchecked(16)
| Binary16.packSetSignificandUnchecked(1);
final char c = (char) n;
return c;
}
/**
* Return <code>true</code> if the given packed <code>binary16</code> value
* is infinite.
*/
public static boolean isInfinite(
final char k)
{
if (Binary16.unpackGetExponentUnbiased(k) == 16) {
if (Binary16.unpackGetSignificand(k) == 0) {
return true;
}
}
return false;
}
/**
* Return <code>true</code> if the given packed <code>binary16</code> value
* is not a number (<code>NaN</code>).
*/
public static boolean isNaN(
final char k)
{
final int e = Binary16.unpackGetExponentUnbiased(k);
final int s = Binary16.unpackGetSignificand(k);
return (e == 16) && (s > 0);
}
/**
* <p>
* Convert a double precision floating point value to a packed
* <code>binary16</code> value.
* </p>
* <p>
* For the following specific cases, the function returns:
* </p>
* <ul>
* <li><code>NaN</code> iff <code>isNaN(k)</code></li>
* <li>{@link #POSITIVE_INFINITY} iff
* <code>k == {@link Double#POSITIVE_INFINITY}</code></li>
* <li>{@link #NEGATIVE_INFINITY} iff
* <code>k == {@link Double#NEGATIVE_INFINITY}</code></li>
* <li>{@link #NEGATIVE_ZERO} iff <code>k == -0.0</code></li>
* <li>{@link #POSITIVE_ZERO} iff <code>k == 0.0</code></li>
* </ul>
* <p>
* Otherwise, the <code>binary16</code> value that most closely represents
* <code>k</code> is returned. This may obviously be an infinite value as
* the interval of double precision values is far larger than that of the
* <code>binary16</code> type.
* </p>
*
* @see #unpackDouble(char)
*/
public static char packDouble(
final double k)
{
if (Double.isNaN(k)) {
return Binary16.exampleNaN();
}
if (k == Double.POSITIVE_INFINITY) {
return Binary16.POSITIVE_INFINITY;
}
if (k == Double.NEGATIVE_INFINITY) {
return Binary16.NEGATIVE_INFINITY;
}
if (Double.doubleToLongBits(k) == Binary64.NEGATIVE_ZERO_BITS) {
return Binary16.NEGATIVE_ZERO;
}
if (k == 0.0) {
return Binary16.POSITIVE_ZERO;
}
final long de = Binary64.unpackGetExponentUnbiased(k);
final long ds = Binary64.unpackGetSign(k);
final long dn = Binary64.unpackGetSignificand(k);
final char rsr = Binary16.packSetSignUnchecked((int) ds);
/**
* Extract the 5 least-significant bits of the exponent.
*/
final int rem = (int) (de & 0x001F);
final char rer = Binary16.packSetExponentUnbiasedUnchecked(rem);
/**
* Extract the 10 most-significant bits of the significand.
*/
final long rnm = dn & 0xFFC0000000000L;
final long rns = rnm >> 42;
final char rnr = Binary16.packSetSignificandUnchecked((int) rns);
/**
* Combine the results.
*/
return (char) (rsr | rer | rnr);
}
/**
* <p>
* Convert a single precision floating point value to a packed
* <code>binary16</code> value.
* </p>
* <p>
* For the following specific cases, the function returns:
* </p>
* <ul>
* <li><code>NaN</code> iff <code>isNaN(k)</code></li>
* <li>{@link #POSITIVE_INFINITY} iff
* <code>k == {@link Float#POSITIVE_INFINITY}</code></li>
* <li>{@link #NEGATIVE_INFINITY} iff
* <code>k == {@link Float#NEGATIVE_INFINITY}</code></li>
* <li>{@link #NEGATIVE_ZERO} iff <code>k == -0.0</code></li>
* <li>{@link #POSITIVE_ZERO} iff <code>k == 0.0</code></li>
* </ul>
* <p>
* Otherwise, the <code>binary16</code> value that most closely represents
* <code>k</code> is returned. This may obviously be an infinite value as
* the interval of single precision values is far larger than that of the
* <code>binary16</code> type.
* </p>
*
* @see #unpackFloat(char)
*/
public static char packFloat(
final float k)
{
if (Float.isNaN(k)) {
return Binary16.exampleNaN();
}
if (k == Float.POSITIVE_INFINITY) {
return Binary16.POSITIVE_INFINITY;
}
if (k == Float.NEGATIVE_INFINITY) {
return Binary16.NEGATIVE_INFINITY;
}
if (Float.floatToIntBits(k) == Binary32.NEGATIVE_ZERO_BITS) {
return Binary16.NEGATIVE_ZERO;
}
if (k == 0.0) {
return Binary16.POSITIVE_ZERO;
}
final long de = Binary32.unpackGetExponentUnbiased(k);
final long ds = Binary32.unpackGetSign(k);
final long dn = Binary32.unpackGetSignificand(k);
final char rsr = Binary16.packSetSignUnchecked((int) ds);
/**
* Extract the 5 least-significant bits of the exponent.
*/
final int rem = (int) (de & 0x001F);
final char rer = Binary16.packSetExponentUnbiasedUnchecked(rem);
/**
* Extract the 10 most-significant bits of the significand.
*/
final long rnm = dn & 0x7FE000L;
final long rns = rnm >> 13;
final char rnr = Binary16.packSetSignificandUnchecked((int) rns);
/**
* Combine the results.
*/
return (char) (rsr | rer | rnr);
}
/**
* <p>
* Encode the unbiased exponent <code>e</code>. Values should be in the
* range <code>[-15, 16]</code> - values outside of this range will be
* truncated.
* </p>
*
* @see #unpackGetExponentUnbiased(char)
*/
public static char packSetExponentUnbiasedUnchecked(
final int e)
{
final int eb = e + Binary16.BIAS;
final int es = eb << 10;
final int em = es & Binary16.MASK_EXPONENT;
return (char) em;
}
/**
* <p>
* Encode the significand <code>s</code>. Values should be in the range
* <code>[0, 1023]</code>. Values outside of this range will be truncated.
* </p>
*
* @see #unpackGetSignificand(char)
*/
public static char packSetSignificandUnchecked(
final int s)
{
final int sm = s & Binary16.MASK_SIGNIFICAND;
return (char) sm;
}
/**
* <p>
* Encode the sign bit <code>s</code>. Values should be in the range
* <code>[0, 1]</code>, with <code>0</code> ironically denoting a positive
* value. Values outside of this range will be truncated.
* </p>
*
* @see #unpackGetSign(char)
*/
public static char packSetSignUnchecked(
final int s)
{
final int ss = s << 15;
final int sm = ss & Binary16.MASK_SIGN;
return (char) sm;
}
/**
* Show the given raw packed <code>binary16</code> value as a string of
* binary digits.
*/
public static String toRawBinaryString(
final char k)
{
final StringBuilder b = new StringBuilder();
int z = k;
for (int i = 0; i < 16; ++i) {
if ((z & 1) == 1) {
b.insert(0, "1");
} else {
b.insert(0, "0");
}
z >>= 1;
}
return b.toString();
}
/**
* <p>
* Convert a packed <code>binary16</code> value <code>k</code> to a
* double-precision floating point value.
* </p>
* <p>
* The function returns:
* </p>
* <ul>
* <li><code>NaN</code> iff <code>isNaN(k)</code></li>
* <li>{@link Double#POSITIVE_INFINITY} iff
* <code>k == {@link #POSITIVE_INFINITY}</code></li>
* <li>{@link Double#NEGATIVE_INFINITY} iff
* <code>k == {@link #NEGATIVE_INFINITY}</code></li>
* <li><code>-0.0</code> iff <code>k == {@link #NEGATIVE_ZERO}</code></li>
* <li><code>0.0</code> iff <code>k == {@link #POSITIVE_ZERO}</code></li>
* <li><code>(-1.0 * n) * (2 ^ e) * 1.s</code>, for the decoded sign
* <code>n</code> of <code>k</code>, the decoded exponent <code>e</code> of
* <code>k</code>, and the decoded significand <code>s</code> of
* <code>k</code>.</li>
* </ul>
*
* @see #packDouble(double)
*/
public static double unpackDouble(
final char k)
{
if (Binary16.isNaN(k)) {
return Double.NaN;
}
if (k == Binary16.POSITIVE_INFINITY) {
return Double.POSITIVE_INFINITY;
}
if (k == Binary16.NEGATIVE_INFINITY) {
return Double.NEGATIVE_INFINITY;
}
if (k == Binary16.NEGATIVE_ZERO) {
return -0.0;
}
if (k == Binary16.POSITIVE_ZERO) {
return 0.0;
}
final long e = Binary16.unpackGetExponentUnbiased(k);
final long s = Binary16.unpackGetSign(k);
final long n = Binary16.unpackGetSignificand(k);
/**
* Shift the sign bit to the position at which it will appear in the
* resulting value.
*/
final long rsr = s << 63;
/**
* 1. Bias the exponent.
*
* 2. Shift the result left to the position at which it will appear in the
* resulting value.
*/
final long reb = (e + Binary64.BIAS);
final long rer = reb << 52;
/**
* Shift the significand left to the position at which it will appear in
* the resulting value.
*/
final long rnr = n << 42;
return Double.longBitsToDouble(rsr | rer | rnr);
}
/**
* <p>
* Convert a packed <code>binary16</code> value <code>k</code> to a
* single-precision floating point value.
* </p>
* <p>
* The function returns:
* </p>
* <ul>
* <li><code>NaN</code> iff <code>isNaN(k)</code></li>
* <li>{@link Float#POSITIVE_INFINITY} iff
* <code>k == {@link #POSITIVE_INFINITY}</code></li>
* <li>{@link Float#NEGATIVE_INFINITY} iff
* <code>k == {@link #NEGATIVE_INFINITY}</code></li>
* <li><code>-0.0</code> iff <code>k == {@link #NEGATIVE_ZERO}</code></li>
* <li><code>0.0</code> iff <code>k == {@link #POSITIVE_ZERO}</code></li>
* <li><code>(-1.0 * n) * (2 ^ e) * 1.s</code>, for the decoded sign
* <code>n</code> of <code>k</code>, the decoded exponent <code>e</code> of
* <code>k</code>, and the decoded significand <code>s</code> of
* <code>k</code>.</li>
* </ul>
*
* @see #packFloat(float)
*/
public static float unpackFloat(
final char k)
{
if (Binary16.isNaN(k)) {
return Float.NaN;
}
if (k == Binary16.POSITIVE_INFINITY) {
return Float.POSITIVE_INFINITY;
}
if (k == Binary16.NEGATIVE_INFINITY) {
return Float.NEGATIVE_INFINITY;
}
if (k == Binary16.NEGATIVE_ZERO) {
return -0.0f;
}
if (k == Binary16.POSITIVE_ZERO) {
return 0.0f;
}
final int e = Binary16.unpackGetExponentUnbiased(k);
final int s = Binary16.unpackGetSign(k);
final int n = Binary16.unpackGetSignificand(k);
/**
* Shift the sign bit to the position at which it will appear in the
* resulting value.
*/
final int rsr = s << 31;
/**
* 1. Bias the exponent.
*
* 2. Shift the result left to the position at which it will appear in the
* resulting value.
*/
final int reb = (e + Binary32.BIAS);
final int rer = reb << 23;
/**
* Shift the significand left to the position at which it will appear in
* the resulting value.
*/
final int rnr = n << 13;
return Float.intBitsToFloat(rsr | rer | rnr);
}
/**
* <p>
* Extract and unbias the exponent of the given packed <code>binary16</code>
* value.
* </p>
* <p>
* The exponent is encoded <i>biased</i> as a number in the range
* <code>[0, 31]</code>, with <code>0</code> indicating that the number is
* <i>subnormal</i> and <code>[1, 30]</code> denoting the actual exponent
* plus {@link #BIAS}. Infinite and <code>NaN</code> values always have an
* exponent of <code>31</code>.
* </p>
* <p>
* This function will therefore return:
* </p>
* <ul>
* <li>
* <code>0 - {@link #BIAS} = -15</code> iff the input is a <i>subnormal</i>
* number.</li>
* <li>An integer in the range
* <code>[1 - {@link #BIAS}, 30 - {@link #BIAS}] = [-14, 15]</code> iff the
* input is a <i>normal</i> number.</li>
* <li>
* <code>16</code> iff the input is {@link #POSITIVE_INFINITY},
* {@link #NEGATIVE_INFINITY}, or <code>NaN</code>.</li>
* </ul>
*
* @see #packSetExponentUnbiasedUnchecked(int)
*/
public static int unpackGetExponentUnbiased(
final char k)
{
final int em = k & Binary16.MASK_EXPONENT;
final int es = em >> 10;
return es - Binary16.BIAS;
}
/**
* Retrieve the sign bit of the given packed <code>binary16</code> value, as
* an integer in the range <code>[0, 1]</code>.
*
* @see Binary16#packSetSignUnchecked(int)
*/
public static int unpackGetSign(
final char k)
{
return (k & Binary16.MASK_SIGN) >> 15;
}
/**
* <p>
* Return the significand of the given packed <code>binary16</code> value as
* an integer in the range <code>[0, 1023]</code>.
* </p>
*
* @see Binary16#packSetSignificandUnchecked(int)
*/
public static int unpackGetSignificand(
final char k)
{
return k & Binary16.MASK_SIGNIFICAND;
}
private Binary16()
{
throw new AssertionError("Unreachable code, report this bug!");
}
}
|