/ src / ref / rint.c
rint.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 "libm_amd.h"
29  #include "libm_util_amd.h"
30  #include "libm_errno_amd.h"
31  #include "libm_special.h"
32  
33  
34  double FN_PROTOTYPE_REF(rint)(double x)
35  {
36  
37      UT64 checkbits,val_2p52;
38  	UT32 sign;
39      checkbits.f64=x;
40  
41      /* Clear the sign bit and check if the value can be rounded(i.e check if exponent less than 52) */
42      if( (checkbits.u64 & 0x7FFFFFFFFFFFFFFF) > 0x4330000000000000)
43      {
44        /* take care of nan or inf */
45  	if(	(checkbits.u64 & EXPBITS_DP64) == EXPBITS_DP64)
46  	{
47          if((checkbits.u64 & MANTBITS_DP64) == 0x0)
48          {
49              // x is Inf
50  #ifdef WINDOWS
51              return  __amd_handle_error("rint", __amd_rint, checkbits.u64, _DOMAIN, AMD_F_INVALID, EDOM, x, 0.0, 1);
52  #else
53              return  __amd_handle_error("rint", __amd_rint, checkbits.u64, _DOMAIN, AMD_F_NONE, EDOM, x, 0.0, 1);
54  #endif
55  		}
56  		else {
57  			// x is NaN
58  			// QNAN_MASK_32
59  #ifdef WINDOWS
60  		return  __amd_handle_error("rint", __amd_rint, checkbits.u64 | QNAN_MASK_64, _DOMAIN, AMD_F_NONE, EDOM, x, 0.0, 1);
61  #else
62  		if (checkbits.u64 & QNAN_MASK_64)
63  		return  __amd_handle_error("rint", __amd_rint, checkbits.u64 | QNAN_MASK_64, _DOMAIN, AMD_F_NONE, EDOM, x, 0.0, 1);
64  		else
65  		return  __amd_handle_error("rint", __amd_rint, checkbits.u64 | QNAN_MASK_64, _DOMAIN, AMD_F_INVALID, EDOM, x, 0.0, 1);
66  #endif
67  		}
68  	}
69  	else
70  		return x;
71      }
72  
73      sign.u32 =  checkbits.u32[1] & 0x80000000;
74      val_2p52.u32[1] = sign.u32 | 0x43300000;
75      val_2p52.u32[0] = 0;
76  
77  	/* Add and sub 2^52 to round the number according to the current rounding direction */
78      val_2p52.f64 = (x + val_2p52.f64) - val_2p52.f64;
79  
80      /*This extra line is to take care of denormals and various rounding modes*/
81      val_2p52.u32[1] = ((val_2p52.u32[1] << 1) >> 1) | sign.u32;
82  
83       if(x!=val_2p52.f64)
84  	{
85     	     /* Raise floating-point inexact exception if the result differs in value from the argument */
86        	    checkbits.u64 = QNANBITPATT_DP64;
87       	    checkbits.f64 = checkbits.f64 +  checkbits.f64;        /* raise inexact exception by adding two nan numbers.*/
88  	}
89  
90  
91      return (val_2p52.f64);
92  }
93  
94  
95  
96  
97