Ptex
PtexHalf.h
Go to the documentation of this file.
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 
44 #include "PtexExports.h"
45 #include "PtexInt.h"
46 #include "PtexVersion.h"
47 
49 
72 struct PtexHalf {
73  uint16_t bits;
74 
76  PtexHalf() {}
77  PtexHalf(float val) : bits(fromFloat(val)) {}
78  PtexHalf(double val) : bits(fromFloat(float(val))) {}
79  operator float() const { return toFloat(bits); }
80  PtexHalf& operator=(float val) { bits = fromFloat(val); return *this; }
81 
82  static float toFloat(uint16_t h)
83  {
84  union { uint32_t i; float f; } u;
85  u.i = h2fTable[h];
86  return u.f;
87  }
88 
89  static uint16_t fromFloat(float val)
90  {
91  if (val==0) return 0;
92  union { uint32_t i; float f; } u;
93  u.f = val;
94  int e = f2hTable[(u.i>>23)&0x1ff];
95  if (e) return (uint16_t)(e + (((u.i&0x7fffff) + 0x1000) >> 13));
96  return fromFloat_except(u.i);
97  }
98 
99  private:
100  PTEXAPI static uint16_t fromFloat_except(uint32_t val);
101 #ifndef DOXYGEN
102  /* internal */ public:
103 #endif
104  PTEXAPI static uint32_t h2fTable[65536];
105  PTEXAPI static uint16_t f2hTable[512];
106 };
107 
109 
110 using Ptex::PtexHalf;
111 
112 #endif
Definitions related to exported Ptex API symbol visibility.
#define PTEXAPI
Definition: PtexExports.h:60
Portable fixed-width integer types.
#define PTEX_NAMESPACE_END
Definition: PtexVersion.h:62
Half-precision (16-bit) floating-point type.
Definition: PtexHalf.h:72
uint16_t bits
Definition: PtexHalf.h:73
PtexHalf()
Default constructor, value is undefined.
Definition: PtexHalf.h:76
static uint16_t fromFloat(float val)
Definition: PtexHalf.h:89
PtexHalf(double val)
Definition: PtexHalf.h:78
static uint32_t h2fTable[65536]
Definition: PtexHalf.h:104
static uint16_t fromFloat_except(uint32_t val)
Handle exceptional cases for half-to-float conversion.
Definition: PtexHalf.cpp:44
static uint16_t f2hTable[512]
Definition: PtexHalf.h:105
static float toFloat(uint16_t h)
Definition: PtexHalf.h:82
PtexHalf & operator=(float val)
Definition: PtexHalf.h:80
PtexHalf(float val)
Definition: PtexHalf.h:77