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 #include "libm_errno.h" 31 #define USE_HANDLE_ERROR 32 #include "libm_inlines.h" 33 #undef USE_HANDLE_ERROR 34 35 #pragma function(floor) 36 37 double FN_PROTOTYPE(floor)(double x) 38 { 39 double r; 40 long long rexp, xneg; 41 42 43 unsigned long long ux, ax, ur, mask; 44 45 GET_BITS_DP64(x, ux); 46 ax = ux & (~SIGNBIT_DP64); 47 xneg = (ux != ax); 48 49 if (ax >= 0x4340000000000000) 50 { 51 /* abs(x) is either NaN, infinity, or >= 2^53 */ 52 if (ax > 0x7ff0000000000000) 53 /* x is NaN */ 54 return _handle_error("floor", OP_FLOOR, ux|0x0008000000000000, _DOMAIN, 55 0, EDOM, x, 0.0, 1); 56 else 57 return x; 58 } 59 else if (ax < 0x3ff0000000000000) /* abs(x) < 1.0 */ 60 { 61 if (ax == 0x0000000000000000) 62 /* x is +zero or -zero; return the same zero */ 63 return x; 64 else if (xneg) /* x < 0.0 */ 65 return -1.0; 66 else 67 return 0.0; 68 } 69 else 70 { 71 r = x; 72 rexp = ((ux & EXPBITS_DP64) >> EXPSHIFTBITS_DP64) - EXPBIAS_DP64; 73 /* Mask out the bits of r that we don't want */ 74 mask = 1; 75 mask = (mask << (EXPSHIFTBITS_DP64 - rexp)) - 1; 76 ur = (ux & ~mask); 77 PUT_BITS_DP64(ur, r); 78 if (xneg && (ur != ux)) 79 /* We threw some bits away and x was negative */ 80 return r - 1.0; 81 else 82 return r; 83 } 84 85 } 86