summary refs log tree commit diff
path: root/sysdeps/ieee754/ldbl-128ibm/s_lroundl.c
blob: 24b4f46e6c669a30b6873cfcad665c9dd9f90d38 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
/* Round to long int long double floating-point values.
   IBM extended format long double version.
   Copyright (C) 2006, 2012 Free Software Foundation, Inc.
   This file is part of the GNU C Library.

   The GNU C Library is free software; you can redistribute it and/or
   modify it under the terms of the GNU Lesser General Public
   License as published by the Free Software Foundation; either
   version 2.1 of the License, or (at your option) any later version.

   The GNU C Library is distributed in the hope that it will be useful,
   but WITHOUT ANY WARRANTY; without even the implied warranty of
   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
   Lesser General Public License for more details.

   You should have received a copy of the GNU Lesser General Public
   License along with the GNU C Library; if not, write to the Free
   Software Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA
   02111-1307 USA.  */

#include <math.h>
#include <fenv_libc.h>
#include <math_ldbl_opt.h>
#include <float.h>
#include <ieee754.h>

long
__lroundl (long double x)
{
  double xh, xl;
  long res, hi, lo;

  ldbl_unpack (x, &xh, &xl);

  /* Limit the range of values handled by the conversion to long.
     We do this because we aren't sure whether that conversion properly
     raises FE_INVALID.  */
  if (
#if __LONG_MAX__ == 2147483647
      __builtin_expect
      ((__builtin_fabs (xh) <= (double) __LONG_MAX__ + 2), 1)
#else
      __builtin_expect
      ((__builtin_fabs (xh) <= -(double) (-__LONG_MAX__ - 1)), 1)
#endif
#if !defined (FE_INVALID)
      || 1
#endif
    )
    {
#if __LONG_MAX__ == 2147483647
      long long llhi = (long long) xh;
      if (llhi != (long) llhi)
	hi = llhi < 0 ? -__LONG_MAX__ - 1 : __LONG_MAX__;
      else
	hi = llhi;
      xh -= hi;
#else
      if (__builtin_expect ((xh == -(double) (-__LONG_MAX__ - 1)), 0))
	{
	  /* When XH is 9223372036854775808.0, converting to long long will
	     overflow, resulting in an invalid operation.  However, XL might
	     be negative and of sufficient magnitude that the overall long
	     double is in fact in range.  Avoid raising an exception.  In any
	     case we need to convert this value specially, because
	     the converted value is not exactly represented as a double
	     thus subtracting HI from XH suffers rounding error.  */
	  hi = __LONG_MAX__;
	  xh = 1.0;
	}
      else
	{
	  hi = (long) xh;
	  xh -= hi;
	}
#endif
      ldbl_canonicalize (&xh, &xl);

      lo = (long) xh;

      /* Peg at max/min values, assuming that the above conversions do so.
         Strictly speaking, we can return anything for values that overflow,
         but this is more useful.  */
      res = hi + lo;

      /* This is just sign(hi) == sign(lo) && sign(res) != sign(hi).  */
      if (__builtin_expect (((~(hi ^ lo) & (res ^ hi)) < 0), 0))
	goto overflow;

      xh -= lo;
      ldbl_canonicalize (&xh, &xl);

      hi = res;
      if (xh > 0.5)
	{
	  res += 1;
	}
      else if (xh == 0.5)
	{
	  if (xl > 0.0 || (xl == 0.0 && res >= 0))
	    res += 1;
	}
      else if (-xh > 0.5)
	{
	  res -= 1;
	}
      else if (-xh == 0.5)
	{
	  if (xl < 0.0 || (xl == 0.0 && res <= 0))
	    res -= 1;
	}

      if (__builtin_expect (((~(hi ^ (res - hi)) & (res ^ hi)) < 0), 0))
	goto overflow;

      return res;
    }
  else
    {
      if (xh > 0.0)
	hi = __LONG_MAX__;
      else if (xh < 0.0)
	hi = -__LONG_MAX__ - 1;
      else
	/* Nan */
	hi = 0;
    }

overflow:
#ifdef FE_INVALID
  feraiseexcept (FE_INVALID);
#endif
  return hi;
}

long_double_symbol (libm, __lroundl, lroundl);