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 #ifdef USE_SOFTWARE_SQRT 31 #define USE_SQRTF_AMD_INLINE 32 #endif 33 #define USE_INFINITYF_WITH_FLAGS 34 #define USE_HANDLE_ERRORF 35 #include "libm_inlines.h" 36 #ifdef USE_SOFTWARE_SQRT 37 #undef USE_SQRTF_AMD_INLINE 38 #endif 39 #undef USE_INFINITYF_WITH_FLAGS 40 #undef USE_HANDLE_ERRORF 41 42 #include "libm_errno.h" 43 44 45 float FN_PROTOTYPE(_hypotf)(float x, float y) 46 { 47 /* Returns sqrt(x*x + y*y) with no overflow or underflow unless 48 the result warrants it */ 49 50 /* Do intermediate computations in double precision 51 and use sqrt instruction from chip if available. */ 52 double dx = x, dy = y, dr, retval; 53 54 /* The largest finite float, stored as a double */ 55 const double large = 3.40282346638528859812e+38; /* 0x47efffffe0000000 */ 56 57 58 unsigned long long ux, uy, avx, avy; 59 60 GET_BITS_DP64(x, avx); 61 avx &= ~SIGNBIT_DP64; 62 GET_BITS_DP64(y, avy); 63 avy &= ~SIGNBIT_DP64; 64 ux = (avx >> EXPSHIFTBITS_DP64); 65 uy = (avy >> EXPSHIFTBITS_DP64); 66 67 if (ux == BIASEDEMAX_DP64 + 1 || uy == BIASEDEMAX_DP64 + 1) 68 { 69 retval = x*x + y*y; 70 /* One or both of the arguments are NaN or infinity. The 71 result will also be NaN or infinity. */ 72 if (((ux == BIASEDEMAX_DP64 + 1) && !(avx & MANTBITS_DP64)) || 73 ((uy == BIASEDEMAX_DP64 + 1) && !(avy & MANTBITS_DP64))) 74 /* x or y is infinity. ISO C99 defines that we must 75 return +infinity, even if the other argument is NaN. 76 Note that the computation of x*x + y*y above will already 77 have raised invalid if either x or y is a signalling NaN. */ 78 return infinityf_with_flags(0); 79 else 80 /* One or both of x or y is NaN, and neither is infinity. 81 Raise invalid if it's a signalling NaN */ 82 return (float)retval; 83 } 84 85 dr = (dx*dx + dy*dy); 86 87 #if USE_SOFTWARE_SQRT 88 retval = sqrtf_amd_inline(r); 89 #else 90 /* VC++ intrinsic call */ 91 _mm_store_sd(&retval, _mm_sqrt_sd(_mm_setzero_pd(), _mm_load_sd(&dr))); 92 #endif 93 94 if (retval > large) 95 return _handle_errorf("_hypotf", OP_HYPOT, PINFBITPATT_SP32, _OVERFLOW, 96 AMD_F_OVERFLOW | AMD_F_INEXACT, ERANGE, x, y, 2); 97 else 98 return (float)retval; 99 } 100