Ptex
PtexHalf.cpp
Go to the documentation of this file.
1/*
2PTEX SOFTWARE
3Copyright 2014 Disney Enterprises, Inc. All rights reserved
4
5Redistribution and use in source and binary forms, with or without
6modification, are permitted provided that the following conditions are
7met:
8
9 * Redistributions of source code must retain the above copyright
10 notice, this list of conditions and the following disclaimer.
11
12 * Redistributions in binary form must reproduce the above copyright
13 notice, this list of conditions and the following disclaimer in
14 the documentation and/or other materials provided with the
15 distribution.
16
17 * The names "Disney", "Walt Disney Pictures", "Walt Disney Animation
18 Studios" or the names of its contributors may NOT be used to
19 endorse or promote products derived from this software without
20 specific prior written permission from Walt Disney Pictures.
21
22Disclaimer: THIS SOFTWARE IS PROVIDED BY WALT DISNEY PICTURES AND
23CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING,
24BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY, FITNESS
25FOR A PARTICULAR PURPOSE, NONINFRINGEMENT AND TITLE ARE DISCLAIMED.
26IN NO EVENT SHALL WALT DISNEY PICTURES, THE COPYRIGHT HOLDER OR
27CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
28EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
29PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
30PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND BASED ON ANY
31THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
32(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
33OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGES.
34*/
35
36#include <cmath>
37#include "PtexHalf.h"
38
40
41#include "PtexHalfTables.h"
42
44uint16_t PtexHalf::fromFloat_except(uint32_t i)
45{
46 uint32_t s = ((i>>16) & 0x8000);
47 int32_t e = ((i>>13) & 0x3fc00) - 0x1c000;
48
49 if (e <= 0) {
50 // denormalized
51 union { uint32_t i; float f; } u;
52 u.i = i;
53 return (uint16_t)(s|int(fabs(u.f)*1.6777216e7 + .5));
54 }
55
56 if (e == 0x23c00)
57 // inf/nan, preserve msb bits of m for nan code
58 return (uint16_t)(s|0x7c00|((i&0x7fffff)>>13));
59 else
60 // overflow - convert to inf
61 return (uint16_t)(s|0x7c00);
62}
63
Half-precision floating-point type.
#define PTEX_NAMESPACE_END
Definition PtexVersion.h:62
static uint16_t fromFloat_except(uint32_t val)
Handle exceptional cases for half-to-float conversion.
Definition PtexHalf.cpp:44