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 29DE_BEGIN_EXTERN_C 30 31typedef deUint16 deFloat16; 32 33#if defined(DE_DEPRECATED_TYPES) 34typedef 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 *//*--------------------------------------------------------------------*/ 42deFloat16 deFloat32To16 (float val32); 43deFloat16 deFloat32To16Round (float val32, deRoundingMode mode); 44void deFloat16_selfTest (void); 45 46deFloat16 deFloat64To16 (double val64); 47deFloat16 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 *//*--------------------------------------------------------------------*/ 54float 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 *//*--------------------------------------------------------------------*/ 61double deFloat16To64 (deFloat16 val16); 62 63DE_INLINE deUint16 deHalfExponent(deFloat16 x) 64{ 65 return (deUint16)((x & 0x7c00u) >> 10); 66} 67 68DE_INLINE deUint16 deHalfMantissa(deFloat16 x) 69{ 70 return (deUint16)(x & 0x03ffu); 71} 72 73DE_INLINE deUint16 deHalfHighestMantissaBit(deFloat16 x) 74{ 75 return (deUint16)(x & (1u << 9)); 76} 77 78DE_INLINE deUint16 deHalfSign(deFloat16 x) 79{ 80 return (deUint16)(x >> 15); 81} 82 83static const deUint16 deHalfMaxExponent = 0x1f; 84 85DE_INLINE deBool deHalfIsZero(deFloat16 x) 86{ 87 return deHalfExponent(x) == 0 && deHalfMantissa(x) == 0; 88} 89 90DE_INLINE deBool deHalfIsPositiveZero(deFloat16 x) 91{ 92 return deHalfIsZero(x) && (deHalfSign(x) == 0); 93} 94 95DE_INLINE deBool deHalfIsNegativeZero(deFloat16 x) 96{ 97 return deHalfIsZero(x) && (deHalfSign(x) != 0); 98} 99 100static const deFloat16 deFloat16SignalingNaN = 0x7c01; 101static const deFloat16 deFloat16QuietNaN = 0x7e01; 102 103DE_INLINE deBool deHalfIsIEEENaN(deFloat16 x) 104{ 105 return deHalfExponent(x) == deHalfMaxExponent && deHalfMantissa(x) != 0; 106} 107 108DE_INLINE deBool deHalfIsSignalingNaN(deFloat16 x) 109{ 110 return deHalfIsIEEENaN(x) && deHalfHighestMantissaBit(x) == 0; 111} 112 113DE_INLINE deBool deHalfIsQuietNaN(deFloat16 x) 114{ 115 return deHalfIsIEEENaN(x) && deHalfHighestMantissaBit(x) != 0; 116} 117 118DE_INLINE deBool deHalfIsInf(deFloat16 x) 119{ 120 return deHalfExponent(x) == deHalfMaxExponent && deHalfMantissa(x) == 0; 121} 122 123DE_INLINE deBool deHalfIsPositiveInf(deFloat16 x) 124{ 125 return deHalfIsInf(x) && (deHalfSign(x) == 0); 126} 127 128DE_INLINE deBool deHalfIsNegativeInf(deFloat16 x) 129{ 130 return deHalfIsInf(x) && (deHalfSign(x) != 0); 131} 132 133DE_INLINE deBool deHalfIsDenormal(deFloat16 x) 134{ 135 return deHalfExponent(x) == 0 && deHalfMantissa(x) != 0; 136} 137 138DE_END_EXTERN_C 139 140#endif /* _DEFLOAT16_H */ 141