-
Notifications
You must be signed in to change notification settings - Fork 2
/
halffloat.c
622 lines (572 loc) · 17.6 KB
/
halffloat.c
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
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
/*
* IEEE Half-Precision Floating Point Conversions
* Copyright (c) 2010, Mark Wiebe
*
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions are met:
* * Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
* * 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.
* * Neither the name of the NumPy Developers nor the
* names of its contributors may be used to endorse or promote products
* derived from this software without specific prior written permission.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "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 THE COPYRIGHT OWNER OR CONTRIBUTERS 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.
*/
#include "halffloat.h"
#include "numpy/ufuncobject.h"
/*
* This chooses between 'ties to even' and 'ties away from zero'.
*/
#define HALF_ROUND_TIES_TO_EVEN 1
/*
* If these are 1, the conversions try to trigger underflow
* and overflow in the FP system when needed.
*/
#define HALF_GENERATE_OVERFLOW 1
#define HALF_GENERATE_UNDERFLOW 1
#define HALF_GENERATE_INVALID 1
#if !defined(generate_overflow_error)
static double numeric_over_big = 1e300;
static void generate_overflow_error(void) {
double dummy;
dummy = numeric_over_big * 1e300;
if (dummy)
return;
else
numeric_over_big += 0.1;
return;
return;
}
#endif
#if !defined(generate_underflow_error)
static double numeric_under_small = 1e-300;
static void generate_underflow_error(void) {
double dummy;
dummy = numeric_under_small * 1e-300;
if (!dummy)
return;
else
numeric_under_small += 1e-300;
return;
}
#endif
#if !defined(generate_invalid_error)
static double numeric_inv_inf = 1e1000;
static void generate_invalid_error(void) {
double dummy;
dummy = numeric_inv_inf - 1e1000;
if (!dummy)
return;
else
numeric_inv_inf += 1.0;
return;
}
#endif
/*
********************************************************************
* HALF-PRECISION ROUTINES *
********************************************************************
*/
float
half_to_float(npy_half h)
{
float ret;
*((npy_uint32*)&ret) = halfbits_to_floatbits(h);
return ret;
}
double
half_to_double(npy_half h)
{
double ret;
*((npy_uint64*)&ret) = halfbits_to_doublebits(h);
return ret;
}
npy_half
float_to_half(float f)
{
return floatbits_to_halfbits(*((npy_uint32*)&f));
}
npy_half
double_to_half(double d)
{
return doublebits_to_halfbits(*((npy_uint64*)&d));
}
int
half_isnonzero(npy_half h)
{
return (h&0x7fff) != 0;
}
int
half_isnan(npy_half h)
{
return ((h&0x7c00u) == 0x7c00u) && ((h&0x03ffu) != 0x0000u);
}
int
half_isinf(npy_half h)
{
return ((h&0x7c00u) == 0x7c00u) && ((h&0x03ffu) == 0x0000u);
}
int
half_isfinite(npy_half h)
{
return ((h&0x7c00u) != 0x7c00u);
}
int
half_signbit(npy_half h)
{
return (h&0x8000u) != 0;
}
npy_half
half_spacing(npy_half h)
{
npy_half ret;
npy_uint16 h_exp = h&0x7c00u;
npy_uint16 h_man = h&0x03ffu;
if (h_exp == 0x7c00u || h == 0x7bffu) {
#if HALF_GENERATE_INVALID
generate_invalid_error();
#endif
ret = HALF_NAN;
} else if ((h&0x8000u) && h_man == 0) { /* Negative boundary case */
if (h_exp > 0x2c00u) { /* If result is normalized */
ret = h_exp - 0x2c00u;
} else if(h_exp > 0x0400u) { /* The result is denormalized, but not the smallest */
ret = 1 << ((h_exp >> 10) - 2);
} else {
ret = 0x0001u; /* Smallest denormalized half */
}
} else if (h_exp > 0x2800u) { /* If result is still normalized */
ret = h_exp - 0x2800u;
} else if (h_exp > 0x0400u) { /* The result is denormalized, but not the smallest */
ret = 1 << ((h_exp >> 10) - 1);
} else {
ret = 0x0001u;
}
return ret;
}
npy_half
half_copysign(npy_half x, npy_half y)
{
return (x&0x7fffu) | (y&0x8000u);
}
npy_half
half_nextafter(npy_half x, npy_half y)
{
npy_half ret;
if (!half_isfinite(x) || half_isnan(y)) {
#if HALF_GENERATE_INVALID
generate_invalid_error();
#endif
ret = HALF_NAN;
} else if (half_eq_nonan(x, y)) {
ret = x;
} else if (!half_isnonzero(x)) {
ret = (y&0x8000u) + 1; /* Smallest denormalized half */
} else if (!(x&0x8000u)) { /* x > 0 */
if ((npy_int16)x > (npy_int16)y) { /* x > y */
ret = x-1;
} else {
ret = x+1;
}
} else {
if (!(y&0x8000u) || (x&0x7fffu) > (y&0x7fffu)) { /* x < y */
ret = x-1;
} else {
ret = x+1;
}
}
#ifdef HALF_GENERATE_OVERFLOW
if (half_isinf(ret)) {
generate_overflow_error();
}
#endif
return ret;
}
int
half_eq_nonan(npy_half h1, npy_half h2)
{
return (h1 == h2 || ((h1 | h2) & 0x7fff) == 0);
}
int
half_eq(npy_half h1, npy_half h2)
{
/*
* The equality cases are as follows:
* - If either value is NaN, never equal.
* - If the values are equal, equal.
* - If the values are both signed zeros, equal.
*/
return (!half_isnan(h1) && !half_isnan(h2)) &&
(h1 == h2 || ((h1 | h2) & 0x7fff) == 0);
}
int
half_ne(npy_half h1, npy_half h2)
{
return !half_eq(h1, h2);
}
int
half_lt_nonan(npy_half h1, npy_half h2)
{
if (h1&0x8000u) {
if (h2&0x8000u) {
return (h1&0x7fffu) > (h2&0x7fffu);
} else {
/* Signed zeros are equal, have to check for it */
return (h1 != 0x8000u) || (h2 != 0x0000u);
}
} else {
if (h2&0x8000u) {
return 0;
} else {
return (h1&0x7fffu) < (h2&0x7fffu);
}
}
}
int
half_lt(npy_half h1, npy_half h2)
{
return (!half_isnan(h1) && !half_isnan(h2)) && half_lt_nonan(h1, h2);
}
int
half_gt(npy_half h1, npy_half h2)
{
return half_lt(h2, h1);
}
int
half_le_nonan(npy_half h1, npy_half h2)
{
if (h1&0x8000u) {
if (h2&0x8000u) {
return (h1&0x7fffu) >= (h2&0x7fffu);
} else {
return 1;
}
} else {
if (h2&0x8000u) {
/* Signed zeros are equal, have to check for it */
return (h1 == 0x0000u) && (h2 == 0x8000u);
} else {
return (h1&0x7fffu) <= (h2&0x7fffu);
}
}
}
int
half_le(npy_half h1, npy_half h2)
{
return (!half_isnan(h1) && !half_isnan(h2)) && half_le_nonan(h1, h2);
}
int
half_ge(npy_half h1, npy_half h2)
{
return half_le(h2, h1);
}
/*
********************************************************************
* BIT-LEVEL CONVERSIONS *
********************************************************************
*/
/*TODO
* Should these routines query the CPU float rounding flags?
* The routine currently does 'ties to even', or 'ties away
* from zero', depending on a #define above.
*/
npy_uint16
floatbits_to_halfbits(npy_uint32 f)
{
npy_uint32 f_exp, f_man;
npy_uint16 h_sgn, h_exp, h_man;
h_sgn = (npy_uint16) ((f&0x80000000u) >> 16);
f_exp = (f&0x7f800000u);
/* Exponent overflow/NaN converts to signed inf/NaN */
if (f_exp >= 0x47800000u) {
if (f_exp == 0x7f800000u) {
/*
* No need to generate FP_INVALID or FP_OVERFLOW here, as
* the float/double routine should have done that.
*/
f_man = (f&0x007fffffu);
if (f_man != 0) {
/* NaN - propagate the flag in the mantissa... */
npy_uint16 ret = (npy_uint16) (0x7c00u + (f_man >> 13));
/* ...but make sure it stays a NaN */
if (ret == 0x7c00u) {
ret++;
}
return h_sgn + ret;
} else {
/* signed inf */
return (npy_uint16) (h_sgn + 0x7c00u);
}
} else {
/* overflow to signed inf */
#if HALF_GENERATE_OVERFLOW
generate_overflow_error();
#endif
return (npy_uint16) (h_sgn + 0x7c00u);
}
}
/* Exponent underflow converts to denormalized half or signed zero */
if (f_exp <= 0x38000000u) {
/*
* Signed zeros, denormalized floats, and floats with small
* exponents all convert to signed zero halfs.
*/
if (f_exp < 0x33000000u) {
#if HALF_GENERATE_UNDERFLOW
/* If f != 0, we underflowed to 0 */
if ((f&0x7fffffff) != 0) {
generate_underflow_error();
}
#endif
return h_sgn;
}
/* It underflowed to a denormalized value */
#if HALF_GENERATE_UNDERFLOW
generate_underflow_error();
#endif
/* Make the denormalized mantissa */
f_exp >>= 23;
f_man = (0x00800000u + (f&0x007fffffu)) >> (113 - f_exp);
/* Handle rounding by adding 1 to the bit beyond half precision */
#if HALF_ROUND_TIES_TO_EVEN
/*
* If the last bit in the half mantissa is 0 (already even), and
* the remaining bit pattern is 1000...0, then we do not add one
* to the bit after the half mantissa. In all other cases, we do.
*/
if ((f_man&0x00003fffu) != 0x00001000u) {
f_man += 0x00001000u;
}
#else
f_man += 0x00001000u;
#endif
h_man = (npy_uint16) (f_man >> 13);
/*
* If the rounding causes a bit to spill into h_exp, it will
* increment h_exp from zero to one and h_man will be zero.
* This is the correct result.
*/
return (npy_uint16) (h_sgn + h_man);
}
/* Regular case with no overflow or underflow */
h_exp = (npy_uint16) ((f_exp - 0x38000000u) >> 13);
/* Handle rounding by adding 1 to the bit beyond half precision */
f_man = (f&0x007fffffu);
#if HALF_ROUND_TIES_TO_EVEN
/*
* If the last bit in the half mantissa is 0 (already even), and
* the remaining bit pattern is 1000...0, then we do not add one
* to the bit after the half mantissa. In all other cases, we do.
*/
if ((f_man&0x00003fffu) != 0x00001000u) {
f_man += 0x00001000u;
}
#else
f_man += 0x00001000u;
#endif
h_man = (npy_uint16) (f_man >> 13);
/*
* If the rounding causes a bit to spill into h_exp, it will
* increment h_exp by one and h_man will be zero. This is the
* correct result. h_exp may increment to 15, at greatest, in
* which case the result overflows to a signed inf.
*/
#if HALF_GENERATE_OVERFLOW
h_man += h_exp;
if (h_man == 0x7c00u) {
generate_overflow_error();
}
return h_sgn + h_man;
#else
return h_sgn + h_exp + h_man;
#endif
}
npy_uint16
doublebits_to_halfbits(npy_uint64 d)
{
npy_uint64 d_exp, d_man;
npy_uint16 h_sgn, h_exp, h_man;
h_sgn = (d&0x8000000000000000u) >> 48;
d_exp = (d&0x7ff0000000000000u);
/* Exponent overflow/NaN converts to signed inf/NaN */
if (d_exp >= 0x40f0000000000000u) {
if (d_exp == 0x7ff0000000000000u) {
/*
* No need to generate FP_INVALID or FP_OVERFLOW here, as
* the float/double routine should have done that.
*/
d_man = (d&0x000fffffffffffffu);
if (d_man != 0) {
/* NaN - propagate the flag in the mantissa... */
npy_uint16 ret = (npy_uint16) (0x7c00u + (d_man >> 42));
/* ...but make sure it stays a NaN */
if (ret == 0x7c00u) {
ret++;
}
return h_sgn + ret;
} else {
/* signed inf */
return h_sgn + 0x7c00u;
}
} else {
/* overflow to signed inf */
#if HALF_GENERATE_OVERFLOW
generate_overflow_error();
#endif
return h_sgn + 0x7c00u;
}
}
/* Exponent underflow converts to denormalized half or signed zero */
if (d_exp <= 0x3f00000000000000u) {
/*
* Signed zeros, denormalized floats, and floats with small
* exponents all convert to signed zero halfs.
*/
if (d_exp < 0x3e60000000000000u) {
#if HALF_GENERATE_UNDERFLOW
/* If d != 0, we underflowed to 0 */
if ((d&0x7fffffffffffffff) != 0) {
generate_underflow_error();
}
#endif
return h_sgn;
}
/* It underflowed to a denormalized value */
#if HALF_GENERATE_UNDERFLOW
generate_underflow_error();
#endif
/* Make the denormalized mantissa */
d_exp >>= 52;
d_man = (0x0010000000000000u + (d&0x000fffffffffffffu))
>> (1009 - d_exp);
/* Handle rounding by adding 1 to the bit beyond half precision */
#if HALF_ROUND_TIES_TO_EVEN
/*
* If the last bit in the half mantissa is 0 (already even), and
* the remaining bit pattern is 1000...0, then we do not add one
* to the bit after the half mantissa. In all other cases, we do.
*/
if ((d_man&0x000007ffffffffffu) != 0x0000020000000000u) {
d_man += 0x0000020000000000u;
}
#else
d_man += 0x0000020000000000u;
#endif
h_man = (npy_uint16) (d_man >> 42);
/*
* If the rounding causes a bit to spill into h_exp, it will
* increment h_exp from zero to one and h_man will be zero.
* This is the correct result.
*/
return h_sgn + h_man;
}
/* Regular case with no overflow or underflow */
h_exp = (npy_uint16) ((d_exp - 0x3f00000000000000u) >> 42);
/* Handle rounding by adding 1 to the bit beyond half precision */
d_man = (d&0x000fffffffffffffu);
#if HALF_ROUND_TIES_TO_EVEN
/*
* If the last bit in the half mantissa is 0 (already even), and
* the remaining bit pattern is 1000...0, then we do not add one
* to the bit after the half mantissa. In all other cases, we do.
*/
if ((d_man&0x000007ffffffffffu) != 0x0000020000000000u) {
d_man += 0x0000020000000000u;
}
#else
d_man += 0x0000020000000000u;
#endif
h_man = (npy_uint16) (d_man >> 42);
/*
* If the rounding causes a bit to spill into h_exp, it will
* increment h_exp by one and h_man will be zero. This is the
* correct result. h_exp may increment to 15, at greatest, in
* which case the result overflows to a signed inf.
*/
#if HALF_GENERATE_OVERFLOW
h_man += h_exp;
if (h_man == 0x7c00u) {
generate_overflow_error();
}
return h_sgn + h_man;
#else
return h_sgn + h_exp + h_man;
#endif
}
npy_uint32
halfbits_to_floatbits(npy_uint16 h)
{
npy_uint16 h_exp, h_man;
npy_uint32 f_sgn, f_exp, f_man;
h_exp = (h&0x7c00u);
f_sgn = ((npy_uint32)h&0x8000u) << 16;
switch (h_exp) {
case 0x0000u: /* 0 or denormalized */
h_man = (h&0x03ffu);
/* Signed zero */
if (h_man == 0) {
return f_sgn;
}
/* Denormalized */
h_man <<= 1;
while ((h_man&0x0400u) == 0) {
h_man <<= 1;
h_exp++;
}
f_exp = ((npy_uint32)(127 - 15 - h_exp)) << 23;
f_man = ((npy_uint32)(h_man&0x03ffu)) << 13;
return f_sgn + f_exp + f_man;
case 0x7c00u: /* inf or NaN */
/* All-ones exponent and a copy of the mantissa */
return f_sgn + 0x7f800000u + (((npy_uint32)(h&0x03ffu)) << 13);
default: /* normalized */
/* Just need to adjust the exponent and shift */
return f_sgn + (((npy_uint32)(h&0x7fffu) + 0x1c000u) << 13);
}
}
npy_uint64
halfbits_to_doublebits(npy_uint16 h)
{
npy_uint16 h_exp, h_man;
npy_uint64 d_sgn, d_exp, d_man;
h_exp = (h&0x7c00u);
d_sgn = ((npy_uint64)h&0x8000u) << 48;
switch (h_exp) {
case 0x0000u: /* 0 or denormalized */
h_man = (h&0x03ffu);
/* Signed zero */
if (h_man == 0) {
return d_sgn;
}
/* Denormalized */
h_man <<= 1;
while ((h_man&0x0400u) == 0) {
h_man <<= 1;
h_exp++;
}
d_exp = ((npy_uint64)(1023 - 15 - h_exp)) << 52;
d_man = ((npy_uint64)(h_man&0x03ffu)) << 42;
return d_sgn + d_exp + d_man;
case 0x7c00u: /* inf or NaN */
/* All-ones exponent and a copy of the mantissa */
return d_sgn + 0x7ff0000000000000u +
(((npy_uint64)(h&0x03ffu)) << 42);
default: /* normalized */
/* Just need to adjust the exponent and shift */
return d_sgn + (((npy_uint64)(h&0x7fffu) + 0xfc000u) << 42);
}
}