sqrtf.c
1 /* 2 * Copyright (C) 2008-2020 Advanced Micro Devices, Inc. All rights reserved. 3 * 4 * Redistribution and use in source and binary forms, with or without modification, 5 * are permitted provided that the following conditions are met: 6 * 1. Redistributions of source code must retain the above copyright notice, 7 * this list of conditions and the following disclaimer. 8 * 2. Redistributions in binary form must reproduce the above copyright notice, 9 * this list of conditions and the following disclaimer in the documentation 10 * and/or other materials provided with the distribution. 11 * 3. Neither the name of the copyright holder nor the names of its contributors 12 * may be used to endorse or promote products derived from this software without 13 * specific prior written permission. 14 * 15 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND 16 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED 17 * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. 18 * IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, 19 * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, 20 * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, 21 * OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, 22 * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) 23 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE 24 * POSSIBILITY OF SUCH DAMAGE. 25 * 26 */ 27 28 #include <emmintrin.h> 29 #include "fn_macros.h" 30 #include "libm_util_amd.h" 31 #include "libm_special.h" 32 33 /*SSE2 contains an instruction SQRTSS. This instruction Computes the square root 34 of the low-order single-precision floating-point value in an XMM register 35 or in a 32-bit memory location and writes the result in the low-order doubleword 36 of another XMM register. The corresponding intrinsic is _mm_sqrt_ss()*/ 37 /*SSE2 contains an instruction SQRTSS. This instruction Computes the square root 38 of the low-order single-precision floating-point value in an XMM register 39 or in a 32-bit memory location and writes the result in the low-order doubleword 40 of another XMM register. The corresponding intrinsic is _mm_sqrt_ss()*/ 41 float FN_PROTOTYPE_REF(sqrtf)(float x) 42 { 43 __m128 X128; 44 float result; 45 int xneg; 46 unsigned int ux, ax; 47 //UT32 uresult; 48 GET_BITS_SP32(x, ux); 49 ax = ux & (~SIGNBIT_SP32); 50 xneg = (ux != ax); 51 52 if (ax > 0x7f800000) /* x is NaN */ 53 #ifdef WINDOWS 54 return __amd_handle_errorf("sqrtf", __amd_squareroot, ux|0x00400000, _DOMAIN, 0, EDOM, x, 0.0, 1); 55 #else 56 { 57 if(!(ax & 0x00400000)) //x is snan 58 return __amd_handle_errorf("sqrtf", __amd_squareroot, ux|0x00400000, _DOMAIN, AMD_F_INVALID, EDOM, x, 0.0, 1); 59 else 60 return x; 61 } 62 #endif 63 64 if(xneg) 65 { 66 if(ax == 0x0) /* x == -0*/ 67 return -0.0; 68 return __amd_handle_errorf("sqrtf", __amd_squareroot, 0x00000000ffc00000, _DOMAIN, AMD_F_INVALID, EDOM, x, 0.0, 1); 69 } 70 71 /*Load x into an XMM register*/ 72 X128 = _mm_load_ss(&x); 73 /*Calculate sqrt using SQRTSS instrunction*/ 74 X128 = _mm_sqrt_ss(X128); 75 /*Store back the result into a single precision floating point number*/ 76 _mm_store_ss(&result, X128); 77 return result; 78 } 79 80 81