nextafter.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 * Copyright (C) 2008-2020 Advanced Micro Devices, Inc. All rights reserved. 28 */ 29 30 #include "libm_amd.h" 31 #include "libm_util_amd.h" 32 #include "libm_special.h" 33 34 35 double FN_PROTOTYPE_REF(nextafter)(double x, double y) 36 { 37 38 39 UT64 checkbits,checkbitsy; 40 double dy = y; 41 checkbits.f64=x; 42 checkbitsy.f64 = y; 43 44 45 46 47 /* check if the number is nan */ 48 if(((checkbits.u64 & ~SIGNBIT_DP64) > EXPBITS_DP64 )) 49 { 50 #ifdef WINDOWS 51 return __amd_handle_error("nextafter", __amd_nextafter, checkbits.u64 | QNAN_MASK_64 , _DOMAIN, AMD_F_NONE, EDOM, x, 0.0, 1); 52 #else 53 if (checkbits.u64 & QNAN_MASK_64) 54 55 return __amd_handle_error("nextafter", __amd_nextafter, checkbits.u64 | QNAN_MASK_64 , _DOMAIN, AMD_F_NONE, EDOM, x, 0.0, 1); 56 else 57 return __amd_handle_error("nextafter", __amd_nextafter, checkbits.u64 | QNAN_MASK_64 , _DOMAIN, AMD_F_INVALID, EDOM, x, 0.0, 1); 58 #endif 59 } 60 61 62 /* check if y is nan */ 63 if(((checkbitsy.u64 & ~SIGNBIT_DP64) > EXPBITS_DP64 )) 64 { 65 #ifdef WINDOWS 66 return __amd_handle_error("nextafter", __amd_nextafter, checkbitsy.u64 | QNAN_MASK_64, _DOMAIN, AMD_F_NONE, EDOM, y, 0.0, 1); 67 #else 68 if (checkbitsy.u64 & QNAN_MASK_64) 69 return __amd_handle_error("nextafter", __amd_nextafter, checkbitsy.u64 | QNAN_MASK_64, _DOMAIN, AMD_F_NONE, EDOM, y, 0.0, 1); 70 else 71 return __amd_handle_error("nextafter", __amd_nextafter, checkbits.u64 | QNAN_MASK_64 , _DOMAIN, AMD_F_INVALID, EDOM, x, 0.0, 1); 72 #endif 73 } 74 75 76 /* if x == y return y in the type of x */ 77 if( x == dy ) 78 { 79 return dy; 80 } 81 82 if( x == 0.0) 83 { 84 checkbits.u64 = 1; 85 if( dy > 0.0 ) 86 return checkbits.f64; 87 else 88 return -checkbits.f64; 89 } 90 91 92 /* compute the next heigher or lower value */ 93 94 if(((x>0.0) ^ (dy>x)) == 0) 95 { 96 checkbits.u64++; 97 } 98 else 99 { 100 checkbits.u64--; 101 } 102 103 /* check if the result is nan or inf */ 104 if(((checkbits.u64 & ~SIGNBIT_DP64) >= EXPBITS_DP64 )) 105 { 106 return __amd_handle_error("nextafter", __amd_nextafter, checkbits.u64 | QNAN_MASK_64, _DOMAIN, AMD_F_NONE, EDOM, x, 0.0, 1); 107 } 108 109 return checkbits.f64; 110 } 111