1 2 /******************************************************************************* 3 MIT License 4 ----------- 5 6 Copyright (c) 2002-2019 Advanced Micro Devices, Inc. 7 8 Permission is hereby granted, free of charge, to any person obtaining a copy 9 of this Software and associated documentaon files (the "Software"), to deal 10 in the Software without restriction, including without limitation the rights 11 to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 12 copies of the Software, and to permit persons to whom the Software is 13 furnished to do so, subject to the following conditions: 14 15 The above copyright notice and this permission notice shall be included in 16 all copies or substantial portions of the Software. 17 18 THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 19 IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 20 FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 21 AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 22 LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 23 OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN 24 THE SOFTWARE. 25 *******************************************************************************/ 26 27 #include "libm.h" 28 #include "libm_util.h" 29 30 #define USE_INFINITYF_WITH_FLAGS 31 #define USE_HANDLE_ERRORF 32 #include "libm_inlines.h" 33 #undef USE_INFINITYF_WITH_FLAGS 34 #undef USE_HANDLE_ERRORF 35 36 #include "libm_errno.h" 37 38 float _logbf(float x) 39 { 40 unsigned int ux; 41 int u; 42 GET_BITS_SP32(x, ux); 43 u = ((ux & EXPBITS_SP32) >> EXPSHIFTBITS_SP32) - EXPBIAS_SP32; 44 if ((ux & ~SIGNBIT_SP32) == 0) 45 /* x is +/-zero. Return -infinity with div-by-zero flag. */ 46 return _handle_errorf("_logbf", OP_LOGB, NINFBITPATT_SP32, _SING, 47 AMD_F_DIVBYZERO, ERANGE, x, 0.0F, 1); 48 else if (EMIN_SP32 <= u && u <= EMAX_SP32) 49 /* x is a normal number */ 50 return (float)u; 51 else if (u > EMAX_SP32) 52 { 53 /* x is infinity or NaN */ 54 if ((ux & MANTBITS_SP32) == 0) 55 /* x is +/-infinity. For VC++, return infinity of same sign. */ 56 return x; 57 else 58 /* x is NaN, result is NaN */ 59 return _handle_errorf("_logbf", OP_LOGB, ux|0x00400000, _DOMAIN, 60 0, EDOM, x, 0.0F, 1); 61 } 62 else 63 { 64 /* x is denormalized. */ 65 #ifdef FOLLOW_IEEE754_LOGB 66 /* Return the value of the minimum exponent to ensure that 67 the relationship between logb and scalb, defined in 68 IEEE 754, holds. */ 69 return EMIN_SP32; 70 #else 71 /* Follow the rule set by IEEE 854 for logb */ 72 ux &= MANTBITS_SP32; 73 u = EMIN_SP32; 74 while (ux < IMPBIT_SP32) 75 { 76 ux <<= 1; 77 u--; 78 } 79 return (float)u; 80 #endif 81 } 82 } 83