Subversion Repositories Kolibri OS

Rev

Details | Last modification | View Log | RSS feed

Rev Author Line No. Line
5564 serge 1
/**************************************************************************
2
 *
3
 * Copyright 2010 Luca Barbieri
4
 *
5
 * Permission is hereby granted, free of charge, to any person obtaining
6
 * a copy of this software and associated documentation files (the
7
 * "Software"), to deal in the Software without restriction, including
8
 * without limitation the rights to use, copy, modify, merge, publish,
9
 * distribute, sublicense, and/or sell copies of the Software, and to
10
 * permit persons to whom the Software is furnished to do so, subject to
11
 * the following conditions:
12
 *
13
 * The above copyright notice and this permission notice (including the
14
 * next paragraph) shall be included in all copies or substantial
15
 * portions of the Software.
16
 *
17
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
18
 * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
19
 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
20
 * IN NO EVENT SHALL THE COPYRIGHT OWNER(S) AND/OR ITS SUPPLIERS BE
21
 * LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
22
 * OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
23
 * WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
24
 *
25
 **************************************************************************/
26
 
27
 
28
#ifndef U_HALF_H
29
#define U_HALF_H
30
 
31
#include "pipe/p_compiler.h"
32
#include "util/u_math.h"
33
 
34
#ifdef __cplusplus
35
extern "C" {
36
#endif
37
 
38
/*
39
 * References for float <-> half conversions
40
 *
41
 *  http://fgiesen.wordpress.com/2012/03/28/half-to-float-done-quic/
42
 *  https://gist.github.com/2156668
43
 *  https://gist.github.com/2144712
44
 */
45
 
46
static INLINE uint16_t
47
util_float_to_half(float f)
48
{
49
   uint32_t sign_mask  = 0x80000000;
50
   uint32_t round_mask = ~0xfff;
51
   uint32_t f32inf = 0xff << 23;
52
   uint32_t f16inf = 0x1f << 23;
53
   uint32_t sign;
54
   union fi magic;
55
   union fi f32;
56
   uint16_t f16;
57
 
58
   magic.ui = 0xf << 23;
59
 
60
   f32.f = f;
61
 
62
   /* Sign */
63
   sign = f32.ui & sign_mask;
64
   f32.ui ^= sign;
65
 
66
   if (f32.ui == f32inf) {
67
      /* Inf */
68
      f16 = 0x7c00;
69
   } else if (f32.ui > f32inf) {
70
      /* NaN */
71
      f16 = 0x7e00;
72
   } else {
73
      /* Number */
74
      f32.ui &= round_mask;
75
      f32.f  *= magic.f;
76
      f32.ui -= round_mask;
77
 
78
      /*
79
       * Clamp to max finite value if overflowed.
80
       * OpenGL has completely undefined rounding behavior for float to
81
       * half-float conversions, and this matches what is mandated for float
82
       * to fp11/fp10, which recommend round-to-nearest-finite too.
83
       * (d3d10 is deeply unhappy about flushing such values to infinity, and
84
       * while it also mandates round-to-zero it doesn't care nearly as much
85
       * about that.)
86
       */
87
      if (f32.ui > f16inf)
88
         f32.ui = f16inf - 1;
89
 
90
      f16 = f32.ui >> 13;
91
   }
92
 
93
   /* Sign */
94
   f16 |= sign >> 16;
95
 
96
   return f16;
97
}
98
 
99
static INLINE float
100
util_half_to_float(uint16_t f16)
101
{
102
   union fi infnan;
103
   union fi magic;
104
   union fi f32;
105
 
106
   infnan.ui = 0x8f << 23;
107
   infnan.f = 65536.0f;
108
   magic.ui  = 0xef << 23;
109
 
110
   /* Exponent / Mantissa */
111
   f32.ui = (f16 & 0x7fff) << 13;
112
 
113
   /* Adjust */
114
   f32.f *= magic.f;
115
 
116
   /* Inf / NaN */
117
   if (f32.f >= infnan.f)
118
      f32.ui |= 0xff << 23;
119
 
120
   /* Sign */
121
   f32.ui |= (f16 & 0x8000) << 16;
122
 
123
   return f32.f;
124
}
125
 
126
#ifdef __cplusplus
127
}
128
#endif
129
 
130
#endif /* U_HALF_H */
131