• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 #ifndef _DEFLOAT16_H
2 #define _DEFLOAT16_H
3 /*-------------------------------------------------------------------------
4  * drawElements Base Portability Library
5  * -------------------------------------
6  *
7  * Copyright 2014 The Android Open Source Project
8  *
9  * Licensed under the Apache License, Version 2.0 (the "License");
10  * you may not use this file except in compliance with the License.
11  * You may obtain a copy of the License at
12  *
13  *      http://www.apache.org/licenses/LICENSE-2.0
14  *
15  * Unless required by applicable law or agreed to in writing, software
16  * distributed under the License is distributed on an "AS IS" BASIS,
17  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
18  * See the License for the specific language governing permissions and
19  * limitations under the License.
20  *
21  *//*!
22  * \file
23  * \brief 16-bit floating-point math.
24  *//*--------------------------------------------------------------------*/
25 
26 #include "deDefs.h"
27 #include "deMath.h"
28 
29 DE_BEGIN_EXTERN_C
30 
31 typedef uint16_t deFloat16;
32 
33 #if defined(DE_DEPRECATED_TYPES)
34 typedef deFloat16 DEfloat16;
35 #endif
36 
37 /*--------------------------------------------------------------------*//*!
38  * \brief Convert 32-bit floating point number to 16 bit.
39  * \param val32    Input value.
40  * \return Converted 16-bit floating-point value.
41  *//*--------------------------------------------------------------------*/
42 deFloat16 deFloat32To16(float val32);
43 deFloat16 deFloat32To16Round(float val32, deRoundingMode mode);
44 void deFloat16_selfTest(void);
45 
46 deFloat16 deFloat64To16(double val64);
47 deFloat16 deFloat64To16Round(double val64, deRoundingMode mode);
48 
49 /*--------------------------------------------------------------------*//*!
50  * \brief Convert 16-bit floating point number to 32 bit.
51  * \param val16    Input value.
52  * \return Converted 32-bit floating-point value.
53  *//*--------------------------------------------------------------------*/
54 float deFloat16To32(deFloat16 val16);
55 
56 /*--------------------------------------------------------------------*//*!
57  * \brief Convert 16-bit floating point number to 64 bit.
58  * \param val16    Input value.
59  * \return Converted 64-bit floating-point value.
60  *//*--------------------------------------------------------------------*/
61 double deFloat16To64(deFloat16 val16);
62 
deHalfExponent(deFloat16 x)63 static inline uint16_t deHalfExponent(deFloat16 x)
64 {
65     return (uint16_t)((x & 0x7c00u) >> 10);
66 }
67 
deHalfMantissa(deFloat16 x)68 static inline uint16_t deHalfMantissa(deFloat16 x)
69 {
70     return (uint16_t)(x & 0x03ffu);
71 }
72 
deHalfHighestMantissaBit(deFloat16 x)73 static inline uint16_t deHalfHighestMantissaBit(deFloat16 x)
74 {
75     return (uint16_t)(x & (1u << 9));
76 }
77 
deHalfSign(deFloat16 x)78 static inline uint16_t deHalfSign(deFloat16 x)
79 {
80     return (uint16_t)(x >> 15);
81 }
82 
83 static const uint16_t deHalfMaxExponent = 0x1f;
84 
deHalfIsZero(deFloat16 x)85 static inline bool deHalfIsZero(deFloat16 x)
86 {
87     return deHalfExponent(x) == 0 && deHalfMantissa(x) == 0;
88 }
89 
deHalfIsPositiveZero(deFloat16 x)90 static inline bool deHalfIsPositiveZero(deFloat16 x)
91 {
92     return deHalfIsZero(x) && (deHalfSign(x) == 0);
93 }
94 
deHalfIsNegativeZero(deFloat16 x)95 static inline bool deHalfIsNegativeZero(deFloat16 x)
96 {
97     return deHalfIsZero(x) && (deHalfSign(x) != 0);
98 }
99 
deHalfIsIEEENaN(deFloat16 x)100 static inline bool deHalfIsIEEENaN(deFloat16 x)
101 {
102     return deHalfExponent(x) == deHalfMaxExponent && deHalfMantissa(x) != 0;
103 }
104 
deHalfIsSignalingNaN(deFloat16 x)105 static inline bool deHalfIsSignalingNaN(deFloat16 x)
106 {
107     return deHalfIsIEEENaN(x) && deHalfHighestMantissaBit(x) == 0;
108 }
109 
deHalfIsQuietNaN(deFloat16 x)110 static inline bool deHalfIsQuietNaN(deFloat16 x)
111 {
112     return deHalfIsIEEENaN(x) && deHalfHighestMantissaBit(x) != 0;
113 }
114 
deHalfIsInf(deFloat16 x)115 static inline bool deHalfIsInf(deFloat16 x)
116 {
117     return deHalfExponent(x) == deHalfMaxExponent && deHalfMantissa(x) == 0;
118 }
119 
deHalfIsPositiveInf(deFloat16 x)120 static inline bool deHalfIsPositiveInf(deFloat16 x)
121 {
122     return deHalfIsInf(x) && (deHalfSign(x) == 0);
123 }
124 
deHalfIsNegativeInf(deFloat16 x)125 static inline bool deHalfIsNegativeInf(deFloat16 x)
126 {
127     return deHalfIsInf(x) && (deHalfSign(x) != 0);
128 }
129 
deHalfIsDenormal(deFloat16 x)130 static inline bool deHalfIsDenormal(deFloat16 x)
131 {
132     return deHalfExponent(x) == 0 && deHalfMantissa(x) != 0;
133 }
134 
135 DE_END_EXTERN_C
136 
137 #endif /* _DEFLOAT16_H */
138