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)63DE_INLINE uint16_t deHalfExponent(deFloat16 x) 64 { 65 return (uint16_t)((x & 0x7c00u) >> 10); 66 } 67 deHalfMantissa(deFloat16 x)68DE_INLINE uint16_t deHalfMantissa(deFloat16 x) 69 { 70 return (uint16_t)(x & 0x03ffu); 71 } 72 deHalfHighestMantissaBit(deFloat16 x)73DE_INLINE uint16_t deHalfHighestMantissaBit(deFloat16 x) 74 { 75 return (uint16_t)(x & (1u << 9)); 76 } 77 deHalfSign(deFloat16 x)78DE_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)85DE_INLINE bool deHalfIsZero(deFloat16 x) 86 { 87 return deHalfExponent(x) == 0 && deHalfMantissa(x) == 0; 88 } 89 deHalfIsPositiveZero(deFloat16 x)90DE_INLINE bool deHalfIsPositiveZero(deFloat16 x) 91 { 92 return deHalfIsZero(x) && (deHalfSign(x) == 0); 93 } 94 deHalfIsNegativeZero(deFloat16 x)95DE_INLINE bool deHalfIsNegativeZero(deFloat16 x) 96 { 97 return deHalfIsZero(x) && (deHalfSign(x) != 0); 98 } 99 100 static const deFloat16 deFloat16SignalingNaN = 0x7c01; 101 static const deFloat16 deFloat16QuietNaN = 0x7e01; 102 deHalfIsIEEENaN(deFloat16 x)103DE_INLINE bool deHalfIsIEEENaN(deFloat16 x) 104 { 105 return deHalfExponent(x) == deHalfMaxExponent && deHalfMantissa(x) != 0; 106 } 107 deHalfIsSignalingNaN(deFloat16 x)108DE_INLINE bool deHalfIsSignalingNaN(deFloat16 x) 109 { 110 return deHalfIsIEEENaN(x) && deHalfHighestMantissaBit(x) == 0; 111 } 112 deHalfIsQuietNaN(deFloat16 x)113DE_INLINE bool deHalfIsQuietNaN(deFloat16 x) 114 { 115 return deHalfIsIEEENaN(x) && deHalfHighestMantissaBit(x) != 0; 116 } 117 deHalfIsInf(deFloat16 x)118DE_INLINE bool deHalfIsInf(deFloat16 x) 119 { 120 return deHalfExponent(x) == deHalfMaxExponent && deHalfMantissa(x) == 0; 121 } 122 deHalfIsPositiveInf(deFloat16 x)123DE_INLINE bool deHalfIsPositiveInf(deFloat16 x) 124 { 125 return deHalfIsInf(x) && (deHalfSign(x) == 0); 126 } 127 deHalfIsNegativeInf(deFloat16 x)128DE_INLINE bool deHalfIsNegativeInf(deFloat16 x) 129 { 130 return deHalfIsInf(x) && (deHalfSign(x) != 0); 131 } 132 deHalfIsDenormal(deFloat16 x)133DE_INLINE bool deHalfIsDenormal(deFloat16 x) 134 { 135 return deHalfExponent(x) == 0 && deHalfMantissa(x) != 0; 136 } 137 138 DE_END_EXTERN_C 139 140 #endif /* _DEFLOAT16_H */ 141