~cosme/ubuntu/precise/freeimage/freeimage-3.15.1

« back to all changes in this revision

Viewing changes to Source/OpenEXR/Half/half.h

  • Committer: Stefano Rivera
  • Date: 2010-07-24 15:35:51 UTC
  • mto: This revision was merged to the branch mainline in revision 5.
  • Revision ID: stefanor@ubuntu.com-20100724153551-6s3fth1653huk31a
Tags: upstream-3.13.1
ImportĀ upstreamĀ versionĀ 3.31.1

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
 
///////////////////////////////////////////////////////////////////////////
2
 
//
3
 
// Copyright (c) 2002, Industrial Light & Magic, a division of Lucas
4
 
// Digital Ltd. LLC
5
 
// 
6
 
// All rights reserved.
7
 
// 
8
 
// Redistribution and use in source and binary forms, with or without
9
 
// modification, are permitted provided that the following conditions are
10
 
// met:
11
 
// *       Redistributions of source code must retain the above copyright
12
 
// notice, this list of conditions and the following disclaimer.
13
 
// *       Redistributions in binary form must reproduce the above
14
 
// copyright notice, this list of conditions and the following disclaimer
15
 
// in the documentation and/or other materials provided with the
16
 
// distribution.
17
 
// *       Neither the name of Industrial Light & Magic nor the names of
18
 
// its contributors may be used to endorse or promote products derived
19
 
// from this software without specific prior written permission. 
20
 
// 
21
 
// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
22
 
// "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
23
 
// LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
24
 
// A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
25
 
// OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
26
 
// SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
27
 
// LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
28
 
// DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
29
 
// THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
30
 
// (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
31
 
// OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
32
 
//
33
 
///////////////////////////////////////////////////////////////////////////
34
 
 
35
 
// Primary authors:
36
 
//     Florian Kainz <kainz@ilm.com>
37
 
//     Rod Bogart <rgb@ilm.com>
38
 
 
39
 
//---------------------------------------------------------------------------
40
 
//
41
 
//      half -- a 16-bit floating point number class:
42
 
//
43
 
//      Type half can represent positive and negative numbers whose
44
 
//      magnitude is between roughly 6.1e-5 and 6.5e+4 with a relative
45
 
//      error of 9.8e-4; numbers smaller than 6.1e-5 can be represented
46
 
//      with an absolute error of 6.0e-8.  All integers from -2048 to
47
 
//      +2048 can be represented exactly.
48
 
//
49
 
//      Type half behaves (almost) like the built-in C++ floating point
50
 
//      types.  In arithmetic expressions, half, float and double can be
51
 
//      mixed freely.  Here are a few examples:
52
 
//
53
 
//          half a (3.5);
54
 
//          float b (a + sqrt (a));
55
 
//          a += b;
56
 
//          b += a;
57
 
//          b = a + 7;
58
 
//
59
 
//      Conversions from half to float are lossless; all half numbers
60
 
//      are exactly representable as floats.
61
 
//
62
 
//      Conversions from float to half may not preserve the float's
63
 
//      value exactly.  If a float is not representable as a half, the
64
 
//      float value is rounded to the nearest representable half.  If
65
 
//      a float value is exactly in the middle between the two closest
66
 
//      representable half values, then the float value is rounded to
67
 
//      the half with the greater magnitude.
68
 
//
69
 
//      Overflows during float-to-half conversions cause arithmetic
70
 
//      exceptions.  An overflow occurs when the float value to be
71
 
//      converted is too large to be represented as a half, or if the
72
 
//      float value is an infinity or a NAN.
73
 
//
74
 
//      The implementation of type half makes the following assumptions
75
 
//      about the implementation of the built-in C++ types:
76
 
//
77
 
//          float is an IEEE 754 single-precision number
78
 
//          sizeof (float) == 4
79
 
//          sizeof (unsigned int) == sizeof (float)
80
 
//          alignof (unsigned int) == alignof (float)
81
 
//          sizeof (unsigned short) == 2
82
 
//
83
 
//---------------------------------------------------------------------------
84
 
 
85
 
#ifndef _HALF_H_
86
 
#define _HALF_H_
87
 
 
88
 
#include <iostream>
89
 
 
90
 
class half
91
 
{
92
 
  public:
93
 
 
94
 
    //-------------
95
 
    // Constructors
96
 
    //-------------
97
 
 
98
 
    half ();                    // no initialization
99
 
    half (float f);
100
 
 
101
 
 
102
 
    //--------------------
103
 
    // Conversion to float
104
 
    //--------------------
105
 
 
106
 
    operator            float () const;
107
 
 
108
 
 
109
 
    //------------
110
 
    // Unary minus
111
 
    //------------
112
 
 
113
 
    half                operator - () const;
114
 
 
115
 
 
116
 
    //-----------
117
 
    // Assignment
118
 
    //-----------
119
 
 
120
 
    half &              operator = (half  h);
121
 
    half &              operator = (float f);
122
 
 
123
 
    half &              operator += (half  h);
124
 
    half &              operator += (float f);
125
 
 
126
 
    half &              operator -= (half  h);
127
 
    half &              operator -= (float f);
128
 
 
129
 
    half &              operator *= (half  h);
130
 
    half &              operator *= (float f);
131
 
 
132
 
    half &              operator /= (half  h);
133
 
    half &              operator /= (float f);
134
 
 
135
 
 
136
 
    //---------------------------------------------------------
137
 
    // Round to n-bit precision (n should be between 0 and 10).
138
 
    // After rounding, the significand's 10-n least significant
139
 
    // bits will be zero.
140
 
    //---------------------------------------------------------
141
 
 
142
 
    half                round (unsigned int n) const;
143
 
 
144
 
 
145
 
    //--------------------------------------------------------------------
146
 
    // Classification:
147
 
    //
148
 
    //  h.isFinite()            returns true if h is a normalized number,
149
 
    //                          a denormalized number or zero
150
 
    //
151
 
    //  h.isNormalized()        returns true if h is a normalized number
152
 
    //
153
 
    //  h.isDenormalized()      returns true if h is a denormalized number
154
 
    //
155
 
    //  h.isZero()              returns true if h is zero
156
 
    //
157
 
    //  h.isNan()               returns true if h is a NAN
158
 
    //
159
 
    //  h.isInfinity()          returns true if h is a positive
160
 
    //                          or a negative infinity
161
 
    //
162
 
    //  h.isNegative()          returns true if the sign bit of h
163
 
    //                          is set (negative)
164
 
    //--------------------------------------------------------------------
165
 
 
166
 
    bool                isFinite () const;
167
 
    bool                isNormalized () const;
168
 
    bool                isDenormalized () const;
169
 
    bool                isZero () const;
170
 
    bool                isNan () const;
171
 
    bool                isInfinity () const;
172
 
    bool                isNegative () const;
173
 
 
174
 
 
175
 
    //--------------------------------------------
176
 
    // Special values
177
 
    //
178
 
    //  posInf()        returns +infinity
179
 
    //
180
 
    //  negInf()        returns -infinity
181
 
    //
182
 
    //  qNan()          returns a NAN with the bit
183
 
    //                  pattern 0111111111111111
184
 
    //
185
 
    //  sNan()          returns a NAN with the bit
186
 
    //                  pattern 0111110111111111
187
 
    //--------------------------------------------
188
 
 
189
 
    static half         posInf ();
190
 
    static half         negInf ();
191
 
    static half         qNan ();
192
 
    static half         sNan ();
193
 
 
194
 
 
195
 
    //--------------------------------------
196
 
    // Access to the internal representation
197
 
    //--------------------------------------
198
 
 
199
 
    unsigned short      bits () const;
200
 
    void                setBits (unsigned short bits);
201
 
 
202
 
 
203
 
  public:
204
 
 
205
 
    union uif
206
 
    {
207
 
        unsigned int    i;
208
 
        float           f;
209
 
    };
210
 
 
211
 
  private:
212
 
 
213
 
    static short        convert (int i);
214
 
    static float        overflow ();
215
 
 
216
 
    unsigned short      _h;
217
 
 
218
 
    //---------------------------------------------------
219
 
    // Windows dynamic libraries don't like static
220
 
    // member variables.
221
 
    //---------------------------------------------------
222
 
#ifndef OPENEXR_DLL
223
 
    static const uif            _toFloat[1 << 16];
224
 
    static const unsigned short _eLut[1 << 9];
225
 
#endif
226
 
};
227
 
 
228
 
#if defined(OPENEXR_DLL)
229
 
    //--------------------------------------
230
 
    // Lookup tables defined for Windows DLL
231
 
    //--------------------------------------
232
 
    #if defined(HALF_EXPORTS)
233
 
        extern __declspec(dllexport) half::uif          _toFloat[1 << 16];
234
 
        extern __declspec(dllexport) unsigned short     _eLut[1 << 9];
235
 
    #else
236
 
        extern __declspec(dllimport) half::uif          _toFloat[1 << 16];
237
 
        extern __declspec(dllimport) unsigned short     _eLut[1 << 9];
238
 
    #endif
239
 
#endif
240
 
 
241
 
 
242
 
//-----------
243
 
// Stream I/O
244
 
//-----------
245
 
 
246
 
std::ostream &          operator << (std::ostream &os, half  h);
247
 
std::istream &          operator >> (std::istream &is, half &h);
248
 
 
249
 
 
250
 
//----------
251
 
// Debugging
252
 
//----------
253
 
 
254
 
void                    printBits   (std::ostream &os, half  h);
255
 
void                    printBits   (std::ostream &os, float f);
256
 
void                    printBits   (char  c[19], half  h);
257
 
void                    printBits   (char  c[35], float f);
258
 
 
259
 
 
260
 
//-------------------------------------------------------------------------
261
 
// Limits
262
 
//
263
 
// Visual C++ will complain if HALF_MIN, HALF_NRM_MIN etc. are not float
264
 
// constants, but at least one other compiler (gcc 2.96) produces incorrect
265
 
// results if they are.
266
 
//-------------------------------------------------------------------------
267
 
 
268
 
#if (defined _WIN32 || defined _WIN64) && defined _MSC_VER
269
 
 
270
 
  #define HALF_MIN      5.96046448e-08f // Smallest positive half
271
 
 
272
 
  #define HALF_NRM_MIN  6.10351562e-05f // Smallest positive normalized half
273
 
 
274
 
  #define HALF_MAX      65504.0f        // Largest positive half
275
 
 
276
 
  #define HALF_EPSILON  0.00097656f     // Smallest positive e for which
277
 
                                        // half (1.0 + e) != half (1.0)
278
 
#else
279
 
 
280
 
  #define HALF_MIN      5.96046448e-08  // Smallest positive half
281
 
 
282
 
  #define HALF_NRM_MIN  6.10351562e-05  // Smallest positive normalized half
283
 
 
284
 
  #define HALF_MAX      65504.0         // Largest positive half
285
 
 
286
 
  #define HALF_EPSILON  0.00097656      // Smallest positive e for which
287
 
                                        // half (1.0 + e) != half (1.0)
288
 
#endif
289
 
 
290
 
 
291
 
#define HALF_MANT_DIG   11              // Number of digits in mantissa
292
 
                                        // (significand + hidden leading 1)
293
 
 
294
 
#define HALF_DIG        2               // Number of base 10 digits that
295
 
                                        // can be represented without change
296
 
 
297
 
#define HALF_RADIX      2               // Base of the exponent
298
 
 
299
 
#define HALF_MIN_EXP    -13             // Minimum negative integer such that
300
 
                                        // HALF_RADIX raised to the power of
301
 
                                        // one less than that integer is a
302
 
                                        // normalized half
303
 
 
304
 
#define HALF_MAX_EXP    16              // Maximum positive integer such that
305
 
                                        // HALF_RADIX raised to the power of
306
 
                                        // one less than that integer is a
307
 
                                        // normalized half
308
 
 
309
 
#define HALF_MIN_10_EXP -4              // Minimum positive integer such
310
 
                                        // that 10 raised to that power is
311
 
                                        // a normalized half
312
 
 
313
 
#define HALF_MAX_10_EXP 4               // Maximum positive integer such
314
 
                                        // that 10 raised to that power is
315
 
                                        // a normalized half
316
 
 
317
 
 
318
 
//---------------------------------------------------------------------------
319
 
//
320
 
// Implementation --
321
 
//
322
 
// Representation of a float:
323
 
//
324
 
//      We assume that a float, f, is an IEEE 754 single-precision
325
 
//      floating point number, whose bits are arranged as follows:
326
 
//
327
 
//          31 (msb)
328
 
//          | 
329
 
//          | 30     23
330
 
//          | |      | 
331
 
//          | |      | 22                    0 (lsb)
332
 
//          | |      | |                     |
333
 
//          X XXXXXXXX XXXXXXXXXXXXXXXXXXXXXXX
334
 
//
335
 
//          s e        m
336
 
//
337
 
//      S is the sign-bit, e is the exponent and m is the significand.
338
 
//
339
 
//      If e is between 1 and 254, f is a normalized number:
340
 
//
341
 
//                  s    e-127
342
 
//          f = (-1)  * 2      * 1.m
343
 
//
344
 
//      If e is 0, and m is not zero, f is a denormalized number:
345
 
//
346
 
//                  s    -126
347
 
//          f = (-1)  * 2      * 0.m
348
 
//
349
 
//      If e and m are both zero, f is zero:
350
 
//
351
 
//          f = 0.0
352
 
//
353
 
//      If e is 255, f is an "infinity" or "not a number" (NAN),
354
 
//      depending on whether m is zero or not.
355
 
//
356
 
//      Examples:
357
 
//
358
 
//          0 00000000 00000000000000000000000 = 0.0
359
 
//          0 01111110 00000000000000000000000 = 0.5
360
 
//          0 01111111 00000000000000000000000 = 1.0
361
 
//          0 10000000 00000000000000000000000 = 2.0
362
 
//          0 10000000 10000000000000000000000 = 3.0
363
 
//          1 10000101 11110000010000000000000 = -124.0625
364
 
//          0 11111111 00000000000000000000000 = +infinity
365
 
//          1 11111111 00000000000000000000000 = -infinity
366
 
//          0 11111111 10000000000000000000000 = NAN
367
 
//          1 11111111 11111111111111111111111 = NAN
368
 
//
369
 
// Representation of a half:
370
 
//
371
 
//      Here is the bit-layout for a half number, h:
372
 
//
373
 
//          15 (msb)
374
 
//          | 
375
 
//          | 14  10
376
 
//          | |   |
377
 
//          | |   | 9        0 (lsb)
378
 
//          | |   | |        |
379
 
//          X XXXXX XXXXXXXXXX
380
 
//
381
 
//          s e     m
382
 
//
383
 
//      S is the sign-bit, e is the exponent and m is the significand.
384
 
//
385
 
//      If e is between 1 and 30, h is a normalized number:
386
 
//
387
 
//                  s    e-15
388
 
//          h = (-1)  * 2     * 1.m
389
 
//
390
 
//      If e is 0, and m is not zero, h is a denormalized number:
391
 
//
392
 
//                  S    -14
393
 
//          h = (-1)  * 2     * 0.m
394
 
//
395
 
//      If e and m are both zero, h is zero:
396
 
//
397
 
//          h = 0.0
398
 
//
399
 
//      If e is 31, h is an "infinity" or "not a number" (NAN),
400
 
//      depending on whether m is zero or not.
401
 
//
402
 
//      Examples:
403
 
//
404
 
//          0 00000 0000000000 = 0.0
405
 
//          0 01110 0000000000 = 0.5
406
 
//          0 01111 0000000000 = 1.0
407
 
//          0 10000 0000000000 = 2.0
408
 
//          0 10000 1000000000 = 3.0
409
 
//          1 10101 1111000001 = -124.0625
410
 
//          0 11111 0000000000 = +infinity
411
 
//          1 11111 0000000000 = -infinity
412
 
//          0 11111 1000000000 = NAN
413
 
//          1 11111 1111111111 = NAN
414
 
//
415
 
// Conversion:
416
 
//
417
 
//      Converting from a float to a half requires some non-trivial bit
418
 
//      manipulations.  In some cases, this makes conversion relatively
419
 
//      slow, but the most common case is accelerated via table lookups.
420
 
//
421
 
//      Converting back from a half to a float is easier because we don't
422
 
//      have to do any rounding.  In addition, there are only 65536
423
 
//      different half numbers; we can convert each of those numbers once
424
 
//      and store the results in a table.  Later, all conversions can be
425
 
//      done using only simple table lookups.
426
 
//
427
 
//---------------------------------------------------------------------------
428
 
 
429
 
 
430
 
//--------------------
431
 
// Simple constructors
432
 
//--------------------
433
 
 
434
 
inline
435
 
half::half ()
436
 
{
437
 
    // no initialization
438
 
}
439
 
 
440
 
 
441
 
//----------------------------
442
 
// Half-from-float constructor
443
 
//----------------------------
444
 
 
445
 
inline
446
 
half::half (float f)
447
 
{
448
 
    uif x;
449
 
 
450
 
    x.f = f;
451
 
 
452
 
    if (f == 0)
453
 
    {
454
 
        //
455
 
        // Common special case - zero.
456
 
        // Preserve the zero's sign bit.
457
 
        //
458
 
 
459
 
        _h = (x.i >> 16);
460
 
    }
461
 
    else
462
 
    {
463
 
        //
464
 
        // We extract the combined sign and exponent, e, from our
465
 
        // floating-point number, f.  Then we convert e to the sign
466
 
        // and exponent of the half number via a table lookup.
467
 
        //
468
 
        // For the most common case, where a normalized half is produced,
469
 
        // the table lookup returns a non-zero value; in this case, all
470
 
        // we have to do is round f's significand to 10 bits and combine
471
 
        // the result with e.
472
 
        //
473
 
        // For all other cases (overflow, zeroes, denormalized numbers
474
 
        // resulting from underflow, infinities and NANs), the table
475
 
        // lookup returns zero, and we call a longer, non-inline function
476
 
        // to do the float-to-half conversion.
477
 
        //
478
 
 
479
 
        register int e = (x.i >> 23) & 0x000001ff;
480
 
 
481
 
        e = _eLut[e];
482
 
 
483
 
        if (e)
484
 
        {
485
 
            //
486
 
            // Simple case - round the significand, m, to 10
487
 
            // bits and combine it with the sign and exponent.
488
 
            //
489
 
 
490
 
            register int m = x.i & 0x007fffff;
491
 
            _h = e + ((m + 0x00000fff + ((m >> 13) & 1)) >> 13);
492
 
        }
493
 
        else
494
 
        {
495
 
            //
496
 
            // Difficult case - call a function.
497
 
            //
498
 
 
499
 
            _h = convert (x.i);
500
 
        }
501
 
    }
502
 
}
503
 
 
504
 
 
505
 
//------------------------------------------
506
 
// Half-to-float conversion via table lookup
507
 
//------------------------------------------
508
 
 
509
 
inline
510
 
half::operator float () const
511
 
{
512
 
    return _toFloat[_h].f;
513
 
}
514
 
 
515
 
 
516
 
//-------------------------
517
 
// Round to n-bit precision
518
 
//-------------------------
519
 
 
520
 
inline half
521
 
half::round (unsigned int n) const
522
 
{
523
 
    //
524
 
    // Parameter check.
525
 
    //
526
 
 
527
 
    if (n >= 10)
528
 
        return *this;
529
 
 
530
 
    //
531
 
    // Disassemble h into the sign, s,
532
 
    // and the combined exponent and significand, e.
533
 
    //
534
 
 
535
 
    unsigned short s = _h & 0x8000;
536
 
    unsigned short e = _h & 0x7fff;
537
 
 
538
 
    //
539
 
    // Round the exponent and significand to the nearest value
540
 
    // where ones occur only in the (10-n) most significant bits.
541
 
    // Note that the exponent adjusts automatically if rounding
542
 
    // up causes the significand to overflow.
543
 
    //
544
 
 
545
 
    e >>= 9 - n;
546
 
    e  += e & 1;
547
 
    e <<= 9 - n;
548
 
 
549
 
    //
550
 
    // Check for exponent overflow.
551
 
    //
552
 
 
553
 
    if (e >= 0x7c00)
554
 
    {
555
 
        //
556
 
        // Overflow occurred -- truncate instead of rounding.
557
 
        //
558
 
 
559
 
        e = _h;
560
 
        e >>= 10 - n;
561
 
        e <<= 10 - n;
562
 
    }
563
 
 
564
 
    //
565
 
    // Put the original sign bit back.
566
 
    //
567
 
 
568
 
    half h;
569
 
    h._h = s | e;
570
 
 
571
 
    return h;
572
 
}
573
 
 
574
 
 
575
 
//-----------------------
576
 
// Other inline functions
577
 
//-----------------------
578
 
 
579
 
inline half     
580
 
half::operator - () const
581
 
{
582
 
    half h;
583
 
    h._h = _h ^ 0x8000;
584
 
    return h;
585
 
}
586
 
 
587
 
 
588
 
inline half &
589
 
half::operator = (half h)
590
 
{
591
 
    _h = h._h;
592
 
    return *this;
593
 
}
594
 
 
595
 
 
596
 
inline half &
597
 
half::operator = (float f)
598
 
{
599
 
    *this = half (f);
600
 
    return *this;
601
 
}
602
 
 
603
 
 
604
 
inline half &
605
 
half::operator += (half h)
606
 
{
607
 
    *this = half (float (*this) + float (h));
608
 
    return *this;
609
 
}
610
 
 
611
 
 
612
 
inline half &
613
 
half::operator += (float f)
614
 
{
615
 
    *this = half (float (*this) + f);
616
 
    return *this;
617
 
}
618
 
 
619
 
 
620
 
inline half &
621
 
half::operator -= (half h)
622
 
{
623
 
    *this = half (float (*this) - float (h));
624
 
    return *this;
625
 
}
626
 
 
627
 
 
628
 
inline half &
629
 
half::operator -= (float f)
630
 
{
631
 
    *this = half (float (*this) - f);
632
 
    return *this;
633
 
}
634
 
 
635
 
 
636
 
inline half &
637
 
half::operator *= (half h)
638
 
{
639
 
    *this = half (float (*this) * float (h));
640
 
    return *this;
641
 
}
642
 
 
643
 
 
644
 
inline half &
645
 
half::operator *= (float f)
646
 
{
647
 
    *this = half (float (*this) * f);
648
 
    return *this;
649
 
}
650
 
 
651
 
 
652
 
inline half &
653
 
half::operator /= (half h)
654
 
{
655
 
    *this = half (float (*this) / float (h));
656
 
    return *this;
657
 
}
658
 
 
659
 
 
660
 
inline half &
661
 
half::operator /= (float f)
662
 
{
663
 
    *this = half (float (*this) / f);
664
 
    return *this;
665
 
}
666
 
 
667
 
 
668
 
inline bool     
669
 
half::isFinite () const
670
 
{
671
 
    unsigned short e = (_h >> 10) & 0x001f;
672
 
    return e < 31;
673
 
}
674
 
 
675
 
 
676
 
inline bool
677
 
half::isNormalized () const
678
 
{
679
 
    unsigned short e = (_h >> 10) & 0x001f;
680
 
    return e > 0 && e < 31;
681
 
}
682
 
 
683
 
 
684
 
inline bool
685
 
half::isDenormalized () const
686
 
{
687
 
    unsigned short e = (_h >> 10) & 0x001f;
688
 
    unsigned short m =  _h & 0x3ff;
689
 
    return e == 0 && m != 0;
690
 
}
691
 
 
692
 
 
693
 
inline bool
694
 
half::isZero () const
695
 
{
696
 
    return (_h & 0x7fff) == 0;
697
 
}
698
 
 
699
 
 
700
 
inline bool
701
 
half::isNan () const
702
 
{
703
 
    unsigned short e = (_h >> 10) & 0x001f;
704
 
    unsigned short m =  _h & 0x3ff;
705
 
    return e == 31 && m != 0;
706
 
}
707
 
 
708
 
 
709
 
inline bool
710
 
half::isInfinity () const
711
 
{
712
 
    unsigned short e = (_h >> 10) & 0x001f;
713
 
    unsigned short m =  _h & 0x3ff;
714
 
    return e == 31 && m == 0;
715
 
}
716
 
 
717
 
 
718
 
inline bool     
719
 
half::isNegative () const
720
 
{
721
 
    return (_h & 0x8000) != 0;
722
 
}
723
 
 
724
 
 
725
 
inline half
726
 
half::posInf ()
727
 
{
728
 
    half h;
729
 
    h._h = 0x7c00;
730
 
    return h;
731
 
}
732
 
 
733
 
 
734
 
inline half
735
 
half::negInf ()
736
 
{
737
 
    half h;
738
 
    h._h = 0xfc00;
739
 
    return h;
740
 
}
741
 
 
742
 
 
743
 
inline half
744
 
half::qNan ()
745
 
{
746
 
    half h;
747
 
    h._h = 0x7fff;
748
 
    return h;
749
 
}
750
 
 
751
 
 
752
 
inline half
753
 
half::sNan ()
754
 
{
755
 
    half h;
756
 
    h._h = 0x7dff;
757
 
    return h;
758
 
}
759
 
 
760
 
 
761
 
inline unsigned short
762
 
half::bits () const
763
 
{
764
 
    return _h;
765
 
}
766
 
 
767
 
 
768
 
inline void
769
 
half::setBits (unsigned short bits)
770
 
{
771
 
    _h = bits;
772
 
}
773
 
 
774
 
#undef HALF_EXPORT_CONST
775
 
 
776
 
#endif
 
1
///////////////////////////////////////////////////////////////////////////
 
2
//
 
3
// Copyright (c) 2002, Industrial Light & Magic, a division of Lucas
 
4
// Digital Ltd. LLC
 
5
// 
 
6
// All rights reserved.
 
7
// 
 
8
// Redistribution and use in source and binary forms, with or without
 
9
// modification, are permitted provided that the following conditions are
 
10
// met:
 
11
// *       Redistributions of source code must retain the above copyright
 
12
// notice, this list of conditions and the following disclaimer.
 
13
// *       Redistributions in binary form must reproduce the above
 
14
// copyright notice, this list of conditions and the following disclaimer
 
15
// in the documentation and/or other materials provided with the
 
16
// distribution.
 
17
// *       Neither the name of Industrial Light & Magic nor the names of
 
18
// its contributors may be used to endorse or promote products derived
 
19
// from this software without specific prior written permission. 
 
20
// 
 
21
// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
 
22
// "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
 
23
// LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
 
24
// A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
 
25
// OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
 
26
// SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
 
27
// LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
 
28
// DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
 
29
// THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
 
30
// (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
 
31
// OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
 
32
//
 
33
///////////////////////////////////////////////////////////////////////////
 
34
 
 
35
// Primary authors:
 
36
//     Florian Kainz <kainz@ilm.com>
 
37
//     Rod Bogart <rgb@ilm.com>
 
38
 
 
39
//---------------------------------------------------------------------------
 
40
//
 
41
//      half -- a 16-bit floating point number class:
 
42
//
 
43
//      Type half can represent positive and negative numbers whose
 
44
//      magnitude is between roughly 6.1e-5 and 6.5e+4 with a relative
 
45
//      error of 9.8e-4; numbers smaller than 6.1e-5 can be represented
 
46
//      with an absolute error of 6.0e-8.  All integers from -2048 to
 
47
//      +2048 can be represented exactly.
 
48
//
 
49
//      Type half behaves (almost) like the built-in C++ floating point
 
50
//      types.  In arithmetic expressions, half, float and double can be
 
51
//      mixed freely.  Here are a few examples:
 
52
//
 
53
//          half a (3.5);
 
54
//          float b (a + sqrt (a));
 
55
//          a += b;
 
56
//          b += a;
 
57
//          b = a + 7;
 
58
//
 
59
//      Conversions from half to float are lossless; all half numbers
 
60
//      are exactly representable as floats.
 
61
//
 
62
//      Conversions from float to half may not preserve the float's
 
63
//      value exactly.  If a float is not representable as a half, the
 
64
//      float value is rounded to the nearest representable half.  If
 
65
//      a float value is exactly in the middle between the two closest
 
66
//      representable half values, then the float value is rounded to
 
67
//      the half with the greater magnitude.
 
68
//
 
69
//      Overflows during float-to-half conversions cause arithmetic
 
70
//      exceptions.  An overflow occurs when the float value to be
 
71
//      converted is too large to be represented as a half, or if the
 
72
//      float value is an infinity or a NAN.
 
73
//
 
74
//      The implementation of type half makes the following assumptions
 
75
//      about the implementation of the built-in C++ types:
 
76
//
 
77
//          float is an IEEE 754 single-precision number
 
78
//          sizeof (float) == 4
 
79
//          sizeof (unsigned int) == sizeof (float)
 
80
//          alignof (unsigned int) == alignof (float)
 
81
//          sizeof (unsigned short) == 2
 
82
//
 
83
//---------------------------------------------------------------------------
 
84
 
 
85
#ifndef _HALF_H_
 
86
#define _HALF_H_
 
87
 
 
88
#include <iostream>
 
89
 
 
90
class half
 
91
{
 
92
  public:
 
93
 
 
94
    //-------------
 
95
    // Constructors
 
96
    //-------------
 
97
 
 
98
    half ();                    // no initialization
 
99
    half (float f);
 
100
 
 
101
 
 
102
    //--------------------
 
103
    // Conversion to float
 
104
    //--------------------
 
105
 
 
106
    operator            float () const;
 
107
 
 
108
 
 
109
    //------------
 
110
    // Unary minus
 
111
    //------------
 
112
 
 
113
    half                operator - () const;
 
114
 
 
115
 
 
116
    //-----------
 
117
    // Assignment
 
118
    //-----------
 
119
 
 
120
    half &              operator = (half  h);
 
121
    half &              operator = (float f);
 
122
 
 
123
    half &              operator += (half  h);
 
124
    half &              operator += (float f);
 
125
 
 
126
    half &              operator -= (half  h);
 
127
    half &              operator -= (float f);
 
128
 
 
129
    half &              operator *= (half  h);
 
130
    half &              operator *= (float f);
 
131
 
 
132
    half &              operator /= (half  h);
 
133
    half &              operator /= (float f);
 
134
 
 
135
 
 
136
    //---------------------------------------------------------
 
137
    // Round to n-bit precision (n should be between 0 and 10).
 
138
    // After rounding, the significand's 10-n least significant
 
139
    // bits will be zero.
 
140
    //---------------------------------------------------------
 
141
 
 
142
    half                round (unsigned int n) const;
 
143
 
 
144
 
 
145
    //--------------------------------------------------------------------
 
146
    // Classification:
 
147
    //
 
148
    //  h.isFinite()            returns true if h is a normalized number,
 
149
    //                          a denormalized number or zero
 
150
    //
 
151
    //  h.isNormalized()        returns true if h is a normalized number
 
152
    //
 
153
    //  h.isDenormalized()      returns true if h is a denormalized number
 
154
    //
 
155
    //  h.isZero()              returns true if h is zero
 
156
    //
 
157
    //  h.isNan()               returns true if h is a NAN
 
158
    //
 
159
    //  h.isInfinity()          returns true if h is a positive
 
160
    //                          or a negative infinity
 
161
    //
 
162
    //  h.isNegative()          returns true if the sign bit of h
 
163
    //                          is set (negative)
 
164
    //--------------------------------------------------------------------
 
165
 
 
166
    bool                isFinite () const;
 
167
    bool                isNormalized () const;
 
168
    bool                isDenormalized () const;
 
169
    bool                isZero () const;
 
170
    bool                isNan () const;
 
171
    bool                isInfinity () const;
 
172
    bool                isNegative () const;
 
173
 
 
174
 
 
175
    //--------------------------------------------
 
176
    // Special values
 
177
    //
 
178
    //  posInf()        returns +infinity
 
179
    //
 
180
    //  negInf()        returns -infinity
 
181
    //
 
182
    //  qNan()          returns a NAN with the bit
 
183
    //                  pattern 0111111111111111
 
184
    //
 
185
    //  sNan()          returns a NAN with the bit
 
186
    //                  pattern 0111110111111111
 
187
    //--------------------------------------------
 
188
 
 
189
    static half         posInf ();
 
190
    static half         negInf ();
 
191
    static half         qNan ();
 
192
    static half         sNan ();
 
193
 
 
194
 
 
195
    //--------------------------------------
 
196
    // Access to the internal representation
 
197
    //--------------------------------------
 
198
 
 
199
    unsigned short      bits () const;
 
200
    void                setBits (unsigned short bits);
 
201
 
 
202
 
 
203
  public:
 
204
 
 
205
    union uif
 
206
    {
 
207
        unsigned int    i;
 
208
        float           f;
 
209
    };
 
210
 
 
211
  private:
 
212
 
 
213
    static short        convert (int i);
 
214
    static float        overflow ();
 
215
 
 
216
    unsigned short      _h;
 
217
 
 
218
    //---------------------------------------------------
 
219
    // Windows dynamic libraries don't like static
 
220
    // member variables.
 
221
    //---------------------------------------------------
 
222
#ifndef OPENEXR_DLL
 
223
    static const uif            _toFloat[1 << 16];
 
224
    static const unsigned short _eLut[1 << 9];
 
225
#endif
 
226
};
 
227
 
 
228
#if defined(OPENEXR_DLL)
 
229
    //--------------------------------------
 
230
    // Lookup tables defined for Windows DLL
 
231
    //--------------------------------------
 
232
    #if defined(HALF_EXPORTS)
 
233
        extern __declspec(dllexport) half::uif          _toFloat[1 << 16];
 
234
        extern __declspec(dllexport) unsigned short     _eLut[1 << 9];
 
235
    #else
 
236
        extern __declspec(dllimport) half::uif          _toFloat[1 << 16];
 
237
        extern __declspec(dllimport) unsigned short     _eLut[1 << 9];
 
238
    #endif
 
239
#endif
 
240
 
 
241
 
 
242
//-----------
 
243
// Stream I/O
 
244
//-----------
 
245
 
 
246
std::ostream &          operator << (std::ostream &os, half  h);
 
247
std::istream &          operator >> (std::istream &is, half &h);
 
248
 
 
249
 
 
250
//----------
 
251
// Debugging
 
252
//----------
 
253
 
 
254
void                    printBits   (std::ostream &os, half  h);
 
255
void                    printBits   (std::ostream &os, float f);
 
256
void                    printBits   (char  c[19], half  h);
 
257
void                    printBits   (char  c[35], float f);
 
258
 
 
259
 
 
260
//-------------------------------------------------------------------------
 
261
// Limits
 
262
//
 
263
// Visual C++ will complain if HALF_MIN, HALF_NRM_MIN etc. are not float
 
264
// constants, but at least one other compiler (gcc 2.96) produces incorrect
 
265
// results if they are.
 
266
//-------------------------------------------------------------------------
 
267
 
 
268
#if (defined _WIN32 || defined _WIN64) && defined _MSC_VER
 
269
 
 
270
  #define HALF_MIN      5.96046448e-08f // Smallest positive half
 
271
 
 
272
  #define HALF_NRM_MIN  6.10351562e-05f // Smallest positive normalized half
 
273
 
 
274
  #define HALF_MAX      65504.0f        // Largest positive half
 
275
 
 
276
  #define HALF_EPSILON  0.00097656f     // Smallest positive e for which
 
277
                                        // half (1.0 + e) != half (1.0)
 
278
#else
 
279
 
 
280
  #define HALF_MIN      5.96046448e-08  // Smallest positive half
 
281
 
 
282
  #define HALF_NRM_MIN  6.10351562e-05  // Smallest positive normalized half
 
283
 
 
284
  #define HALF_MAX      65504.0         // Largest positive half
 
285
 
 
286
  #define HALF_EPSILON  0.00097656      // Smallest positive e for which
 
287
                                        // half (1.0 + e) != half (1.0)
 
288
#endif
 
289
 
 
290
 
 
291
#define HALF_MANT_DIG   11              // Number of digits in mantissa
 
292
                                        // (significand + hidden leading 1)
 
293
 
 
294
#define HALF_DIG        2               // Number of base 10 digits that
 
295
                                        // can be represented without change
 
296
 
 
297
#define HALF_RADIX      2               // Base of the exponent
 
298
 
 
299
#define HALF_MIN_EXP    -13             // Minimum negative integer such that
 
300
                                        // HALF_RADIX raised to the power of
 
301
                                        // one less than that integer is a
 
302
                                        // normalized half
 
303
 
 
304
#define HALF_MAX_EXP    16              // Maximum positive integer such that
 
305
                                        // HALF_RADIX raised to the power of
 
306
                                        // one less than that integer is a
 
307
                                        // normalized half
 
308
 
 
309
#define HALF_MIN_10_EXP -4              // Minimum positive integer such
 
310
                                        // that 10 raised to that power is
 
311
                                        // a normalized half
 
312
 
 
313
#define HALF_MAX_10_EXP 4               // Maximum positive integer such
 
314
                                        // that 10 raised to that power is
 
315
                                        // a normalized half
 
316
 
 
317
 
 
318
//---------------------------------------------------------------------------
 
319
//
 
320
// Implementation --
 
321
//
 
322
// Representation of a float:
 
323
//
 
324
//      We assume that a float, f, is an IEEE 754 single-precision
 
325
//      floating point number, whose bits are arranged as follows:
 
326
//
 
327
//          31 (msb)
 
328
//          | 
 
329
//          | 30     23
 
330
//          | |      | 
 
331
//          | |      | 22                    0 (lsb)
 
332
//          | |      | |                     |
 
333
//          X XXXXXXXX XXXXXXXXXXXXXXXXXXXXXXX
 
334
//
 
335
//          s e        m
 
336
//
 
337
//      S is the sign-bit, e is the exponent and m is the significand.
 
338
//
 
339
//      If e is between 1 and 254, f is a normalized number:
 
340
//
 
341
//                  s    e-127
 
342
//          f = (-1)  * 2      * 1.m
 
343
//
 
344
//      If e is 0, and m is not zero, f is a denormalized number:
 
345
//
 
346
//                  s    -126
 
347
//          f = (-1)  * 2      * 0.m
 
348
//
 
349
//      If e and m are both zero, f is zero:
 
350
//
 
351
//          f = 0.0
 
352
//
 
353
//      If e is 255, f is an "infinity" or "not a number" (NAN),
 
354
//      depending on whether m is zero or not.
 
355
//
 
356
//      Examples:
 
357
//
 
358
//          0 00000000 00000000000000000000000 = 0.0
 
359
//          0 01111110 00000000000000000000000 = 0.5
 
360
//          0 01111111 00000000000000000000000 = 1.0
 
361
//          0 10000000 00000000000000000000000 = 2.0
 
362
//          0 10000000 10000000000000000000000 = 3.0
 
363
//          1 10000101 11110000010000000000000 = -124.0625
 
364
//          0 11111111 00000000000000000000000 = +infinity
 
365
//          1 11111111 00000000000000000000000 = -infinity
 
366
//          0 11111111 10000000000000000000000 = NAN
 
367
//          1 11111111 11111111111111111111111 = NAN
 
368
//
 
369
// Representation of a half:
 
370
//
 
371
//      Here is the bit-layout for a half number, h:
 
372
//
 
373
//          15 (msb)
 
374
//          | 
 
375
//          | 14  10
 
376
//          | |   |
 
377
//          | |   | 9        0 (lsb)
 
378
//          | |   | |        |
 
379
//          X XXXXX XXXXXXXXXX
 
380
//
 
381
//          s e     m
 
382
//
 
383
//      S is the sign-bit, e is the exponent and m is the significand.
 
384
//
 
385
//      If e is between 1 and 30, h is a normalized number:
 
386
//
 
387
//                  s    e-15
 
388
//          h = (-1)  * 2     * 1.m
 
389
//
 
390
//      If e is 0, and m is not zero, h is a denormalized number:
 
391
//
 
392
//                  S    -14
 
393
//          h = (-1)  * 2     * 0.m
 
394
//
 
395
//      If e and m are both zero, h is zero:
 
396
//
 
397
//          h = 0.0
 
398
//
 
399
//      If e is 31, h is an "infinity" or "not a number" (NAN),
 
400
//      depending on whether m is zero or not.
 
401
//
 
402
//      Examples:
 
403
//
 
404
//          0 00000 0000000000 = 0.0
 
405
//          0 01110 0000000000 = 0.5
 
406
//          0 01111 0000000000 = 1.0
 
407
//          0 10000 0000000000 = 2.0
 
408
//          0 10000 1000000000 = 3.0
 
409
//          1 10101 1111000001 = -124.0625
 
410
//          0 11111 0000000000 = +infinity
 
411
//          1 11111 0000000000 = -infinity
 
412
//          0 11111 1000000000 = NAN
 
413
//          1 11111 1111111111 = NAN
 
414
//
 
415
// Conversion:
 
416
//
 
417
//      Converting from a float to a half requires some non-trivial bit
 
418
//      manipulations.  In some cases, this makes conversion relatively
 
419
//      slow, but the most common case is accelerated via table lookups.
 
420
//
 
421
//      Converting back from a half to a float is easier because we don't
 
422
//      have to do any rounding.  In addition, there are only 65536
 
423
//      different half numbers; we can convert each of those numbers once
 
424
//      and store the results in a table.  Later, all conversions can be
 
425
//      done using only simple table lookups.
 
426
//
 
427
//---------------------------------------------------------------------------
 
428
 
 
429
 
 
430
//--------------------
 
431
// Simple constructors
 
432
//--------------------
 
433
 
 
434
inline
 
435
half::half ()
 
436
{
 
437
    // no initialization
 
438
}
 
439
 
 
440
 
 
441
//----------------------------
 
442
// Half-from-float constructor
 
443
//----------------------------
 
444
 
 
445
inline
 
446
half::half (float f)
 
447
{
 
448
    uif x;
 
449
 
 
450
    x.f = f;
 
451
 
 
452
    if (f == 0)
 
453
    {
 
454
        //
 
455
        // Common special case - zero.
 
456
        // Preserve the zero's sign bit.
 
457
        //
 
458
 
 
459
        _h = (x.i >> 16);
 
460
    }
 
461
    else
 
462
    {
 
463
        //
 
464
        // We extract the combined sign and exponent, e, from our
 
465
        // floating-point number, f.  Then we convert e to the sign
 
466
        // and exponent of the half number via a table lookup.
 
467
        //
 
468
        // For the most common case, where a normalized half is produced,
 
469
        // the table lookup returns a non-zero value; in this case, all
 
470
        // we have to do is round f's significand to 10 bits and combine
 
471
        // the result with e.
 
472
        //
 
473
        // For all other cases (overflow, zeroes, denormalized numbers
 
474
        // resulting from underflow, infinities and NANs), the table
 
475
        // lookup returns zero, and we call a longer, non-inline function
 
476
        // to do the float-to-half conversion.
 
477
        //
 
478
 
 
479
        register int e = (x.i >> 23) & 0x000001ff;
 
480
 
 
481
        e = _eLut[e];
 
482
 
 
483
        if (e)
 
484
        {
 
485
            //
 
486
            // Simple case - round the significand, m, to 10
 
487
            // bits and combine it with the sign and exponent.
 
488
            //
 
489
 
 
490
            register int m = x.i & 0x007fffff;
 
491
            _h = e + ((m + 0x00000fff + ((m >> 13) & 1)) >> 13);
 
492
        }
 
493
        else
 
494
        {
 
495
            //
 
496
            // Difficult case - call a function.
 
497
            //
 
498
 
 
499
            _h = convert (x.i);
 
500
        }
 
501
    }
 
502
}
 
503
 
 
504
 
 
505
//------------------------------------------
 
506
// Half-to-float conversion via table lookup
 
507
//------------------------------------------
 
508
 
 
509
inline
 
510
half::operator float () const
 
511
{
 
512
    return _toFloat[_h].f;
 
513
}
 
514
 
 
515
 
 
516
//-------------------------
 
517
// Round to n-bit precision
 
518
//-------------------------
 
519
 
 
520
inline half
 
521
half::round (unsigned int n) const
 
522
{
 
523
    //
 
524
    // Parameter check.
 
525
    //
 
526
 
 
527
    if (n >= 10)
 
528
        return *this;
 
529
 
 
530
    //
 
531
    // Disassemble h into the sign, s,
 
532
    // and the combined exponent and significand, e.
 
533
    //
 
534
 
 
535
    unsigned short s = _h & 0x8000;
 
536
    unsigned short e = _h & 0x7fff;
 
537
 
 
538
    //
 
539
    // Round the exponent and significand to the nearest value
 
540
    // where ones occur only in the (10-n) most significant bits.
 
541
    // Note that the exponent adjusts automatically if rounding
 
542
    // up causes the significand to overflow.
 
543
    //
 
544
 
 
545
    e >>= 9 - n;
 
546
    e  += e & 1;
 
547
    e <<= 9 - n;
 
548
 
 
549
    //
 
550
    // Check for exponent overflow.
 
551
    //
 
552
 
 
553
    if (e >= 0x7c00)
 
554
    {
 
555
        //
 
556
        // Overflow occurred -- truncate instead of rounding.
 
557
        //
 
558
 
 
559
        e = _h;
 
560
        e >>= 10 - n;
 
561
        e <<= 10 - n;
 
562
    }
 
563
 
 
564
    //
 
565
    // Put the original sign bit back.
 
566
    //
 
567
 
 
568
    half h;
 
569
    h._h = s | e;
 
570
 
 
571
    return h;
 
572
}
 
573
 
 
574
 
 
575
//-----------------------
 
576
// Other inline functions
 
577
//-----------------------
 
578
 
 
579
inline half     
 
580
half::operator - () const
 
581
{
 
582
    half h;
 
583
    h._h = _h ^ 0x8000;
 
584
    return h;
 
585
}
 
586
 
 
587
 
 
588
inline half &
 
589
half::operator = (half h)
 
590
{
 
591
    _h = h._h;
 
592
    return *this;
 
593
}
 
594
 
 
595
 
 
596
inline half &
 
597
half::operator = (float f)
 
598
{
 
599
    *this = half (f);
 
600
    return *this;
 
601
}
 
602
 
 
603
 
 
604
inline half &
 
605
half::operator += (half h)
 
606
{
 
607
    *this = half (float (*this) + float (h));
 
608
    return *this;
 
609
}
 
610
 
 
611
 
 
612
inline half &
 
613
half::operator += (float f)
 
614
{
 
615
    *this = half (float (*this) + f);
 
616
    return *this;
 
617
}
 
618
 
 
619
 
 
620
inline half &
 
621
half::operator -= (half h)
 
622
{
 
623
    *this = half (float (*this) - float (h));
 
624
    return *this;
 
625
}
 
626
 
 
627
 
 
628
inline half &
 
629
half::operator -= (float f)
 
630
{
 
631
    *this = half (float (*this) - f);
 
632
    return *this;
 
633
}
 
634
 
 
635
 
 
636
inline half &
 
637
half::operator *= (half h)
 
638
{
 
639
    *this = half (float (*this) * float (h));
 
640
    return *this;
 
641
}
 
642
 
 
643
 
 
644
inline half &
 
645
half::operator *= (float f)
 
646
{
 
647
    *this = half (float (*this) * f);
 
648
    return *this;
 
649
}
 
650
 
 
651
 
 
652
inline half &
 
653
half::operator /= (half h)
 
654
{
 
655
    *this = half (float (*this) / float (h));
 
656
    return *this;
 
657
}
 
658
 
 
659
 
 
660
inline half &
 
661
half::operator /= (float f)
 
662
{
 
663
    *this = half (float (*this) / f);
 
664
    return *this;
 
665
}
 
666
 
 
667
 
 
668
inline bool     
 
669
half::isFinite () const
 
670
{
 
671
    unsigned short e = (_h >> 10) & 0x001f;
 
672
    return e < 31;
 
673
}
 
674
 
 
675
 
 
676
inline bool
 
677
half::isNormalized () const
 
678
{
 
679
    unsigned short e = (_h >> 10) & 0x001f;
 
680
    return e > 0 && e < 31;
 
681
}
 
682
 
 
683
 
 
684
inline bool
 
685
half::isDenormalized () const
 
686
{
 
687
    unsigned short e = (_h >> 10) & 0x001f;
 
688
    unsigned short m =  _h & 0x3ff;
 
689
    return e == 0 && m != 0;
 
690
}
 
691
 
 
692
 
 
693
inline bool
 
694
half::isZero () const
 
695
{
 
696
    return (_h & 0x7fff) == 0;
 
697
}
 
698
 
 
699
 
 
700
inline bool
 
701
half::isNan () const
 
702
{
 
703
    unsigned short e = (_h >> 10) & 0x001f;
 
704
    unsigned short m =  _h & 0x3ff;
 
705
    return e == 31 && m != 0;
 
706
}
 
707
 
 
708
 
 
709
inline bool
 
710
half::isInfinity () const
 
711
{
 
712
    unsigned short e = (_h >> 10) & 0x001f;
 
713
    unsigned short m =  _h & 0x3ff;
 
714
    return e == 31 && m == 0;
 
715
}
 
716
 
 
717
 
 
718
inline bool     
 
719
half::isNegative () const
 
720
{
 
721
    return (_h & 0x8000) != 0;
 
722
}
 
723
 
 
724
 
 
725
inline half
 
726
half::posInf ()
 
727
{
 
728
    half h;
 
729
    h._h = 0x7c00;
 
730
    return h;
 
731
}
 
732
 
 
733
 
 
734
inline half
 
735
half::negInf ()
 
736
{
 
737
    half h;
 
738
    h._h = 0xfc00;
 
739
    return h;
 
740
}
 
741
 
 
742
 
 
743
inline half
 
744
half::qNan ()
 
745
{
 
746
    half h;
 
747
    h._h = 0x7fff;
 
748
    return h;
 
749
}
 
750
 
 
751
 
 
752
inline half
 
753
half::sNan ()
 
754
{
 
755
    half h;
 
756
    h._h = 0x7dff;
 
757
    return h;
 
758
}
 
759
 
 
760
 
 
761
inline unsigned short
 
762
half::bits () const
 
763
{
 
764
    return _h;
 
765
}
 
766
 
 
767
 
 
768
inline void
 
769
half::setBits (unsigned short bits)
 
770
{
 
771
    _h = bits;
 
772
}
 
773
 
 
774
#undef HALF_EXPORT_CONST
 
775
 
 
776
#endif