/ src / ref / llrint.c
llrint.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 "fn_macros.h"
29  #include "libm_util_amd.h"
30  #include "libm_special.h"
31  
32  long long int FN_PROTOTYPE_REF(llrint)(double x)
33  {
34  
35  
36      UT64 checkbits,val_2p52;
37      checkbits.f64=x;
38  
39      /* Clear the sign bit and check if the value can be rounded */
40  
41      if( (checkbits.u64 & 0x7FFFFFFFFFFFFFFF) > 0x4330000000000000)
42      {
43          /* number cant be rounded raise an exception */
44          /* Number exceeds the representable range could be nan or inf also*/
45         // __amd_handle_error(DOMAIN, EDOM, "llrint", x,0.0 ,(double)x);
46  		__amd_handle_error("llrint", __amd_lrint, (long long int)x, _DOMAIN, 0, EDOM, x, 0.0, 1);
47  		return (long long int) x;
48      }
49  
50      val_2p52.u32[1] = (checkbits.u32[1] & 0x80000000) | 0x43300000;
51      val_2p52.u32[0] = 0;
52  
53  
54  	/* Add and sub 2^52 to round the number according to the current rounding direction */
55  
56      return (long long int) ((x + val_2p52.f64) - val_2p52.f64);
57  }
58