1 #ifndef PtexHalf_h
2 #define PtexHalf_h
3 
4 /*
5 PTEX SOFTWARE
6 Copyright 2014 Disney Enterprises, Inc.  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 
12   * Redistributions of source code must retain the above copyright
13     notice, this list of conditions and the following disclaimer.
14 
15   * Redistributions in binary form must reproduce the above copyright
16     notice, this list of conditions and the following disclaimer in
17     the documentation and/or other materials provided with the
18     distribution.
19 
20   * The names "Disney", "Walt Disney Pictures", "Walt Disney Animation
21     Studios" or the names of its contributors may NOT be used to
22     endorse or promote products derived from this software without
23     specific prior written permission from Walt Disney Pictures.
24 
25 Disclaimer: THIS SOFTWARE IS PROVIDED BY WALT DISNEY PICTURES AND
26 CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING,
27 BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY, FITNESS
28 FOR A PARTICULAR PURPOSE, NONINFRINGEMENT AND TITLE ARE DISCLAIMED.
29 IN NO EVENT SHALL WALT DISNEY PICTURES, THE COPYRIGHT HOLDER OR
30 CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
31 EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
32 PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
33 PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND BASED ON ANY
34 THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
35 (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
36 OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGES.
37 */
38 
39 /**
40   @file PtexHalf.h
41   @brief Half-precision floating-point type.
42 */
43 
44 #ifndef PTEXAPI
45 #  if defined(_WIN32) || defined(_WINDOWS) || defined(_MSC_VER)
46 #    ifndef PTEX_STATIC
47 #      ifdef PTEX_EXPORTS
48 #         define PTEXAPI __declspec(dllexport)
49 #      else
50 #         define PTEXAPI __declspec(dllimport)
51 #      endif
52 #    else
53 #      define PTEXAPI
54 #    endif
55 #  else
56 #    define PTEXAPI
57 #  endif
58 #endif
59 
60 #include "PtexInt.h"
61 
62 #include "PtexVersion.h"
63 
64 PTEX_NAMESPACE_BEGIN
65 
66 /**
67    @class PtexHalf
68    @brief Half-precision (16-bit) floating-point type.
69 
70    This type should be compatible with opengl, openexr, and IEEE 754r.
71    The range is [-65504.0, 65504.0] and the precision is about 1 part
72    in 2000 (3.3 decimal places).
73 
74    From OpenGL spec 2.1.2:
75 
76    A 16-bit floating-point number has a 1-bit sign (S), a 5-bit
77    exponent (E), and a 10-bit mantissa (M).  The value of a 16-bit
78    floating-point number is determined by the following:
79 
80    \verbatim
81         (-1)^S * 0.0,                        if E == 0 and M == 0,
82         (-1)^S * 2^-14 * (M/2^10),           if E == 0 and M != 0,
83         (-1)^S * 2^(E-15) * (1 + M/2^10),    if 0 < E < 31,
84         (-1)^S * INF,                        if E == 31 and M == 0, or
85         NaN,                                 if E == 31 and M != 0 \endverbatim
86 */
87 
88 struct PtexHalf {
89     uint16_t bits;
90 
91     /// Default constructor, value is undefined
PtexHalfPtexHalf92     PtexHalf() {}
PtexHalfPtexHalf93     PtexHalf(float val) : bits(fromFloat(val)) {}
PtexHalfPtexHalf94     PtexHalf(double val) : bits(fromFloat(float(val))) {}
95     operator float() const { return toFloat(bits); }
96     PtexHalf& operator=(float val) { bits = fromFloat(val); return *this; }
97 
toFloatPtexHalf98     static float toFloat(uint16_t h)
99     {
100         union { uint32_t i; float f; } u;
101         u.i = h2fTable[h];
102         return u.f;
103     }
104 
fromFloatPtexHalf105     static uint16_t fromFloat(float val)
106     {
107         if (val==0) return 0;
108         union { uint32_t i; float f; } u;
109         u.f = val;
110         int e = f2hTable[(u.i>>23)&0x1ff];
111         if (e) return (uint16_t)(e + (((u.i&0x7fffff) + 0x1000) >> 13));
112         return fromFloat_except(u.i);
113     }
114 
115  private:
116     PTEXAPI static uint16_t fromFloat_except(uint32_t val);
117 #ifndef DOXYGEN
118     /* internal */ public:
119 #endif
120     PTEXAPI static uint32_t h2fTable[65536];
121     PTEXAPI static uint16_t f2hTable[512];
122 };
123 
124 PTEX_NAMESPACE_END
125 
126 using Ptex::PtexHalf;
127 
128 #endif
129