OpenCores
URL https://opencores.org/ocsvn/openrisc_2011-10-31/openrisc_2011-10-31/trunk

Subversion Repositories openrisc_2011-10-31

[/] [openrisc/] [tags/] [gnu-src/] [newlib-1.18.0/] [newlib-1.18.0-or32-1.0rc1/] [newlib/] [libm/] [common/] [s_llrint.c] - Blame information for rev 345

Details | Compare with Previous | View Log

Line No. Rev Author Line
1 207 jeremybenn
/* lrint adapted to be llrint for Newlib, 2009 by Craig Howland.  */
2
/* @(#)s_lrint.c 5.1 93/09/24 */
3
/*
4
 * ====================================================
5
 * Copyright (C) 1993 by Sun Microsystems, Inc. All rights reserved.
6
 *
7
 * Developed at SunPro, a Sun Microsystems, Inc. business.
8
 * Permission to use, copy, modify, and distribute this
9
 * software is freely granted, provided that this notice
10
 * is preserved.
11
 * ====================================================
12
 */
13
 
14
/*
15
 * llrint(x)
16
 * Return x rounded to integral value according to the prevailing
17
 * rounding mode.
18
 * Method:
19
 *      Using floating addition.
20
 * Exception:
21
 *      Inexact flag raised if x not equal to llrint(x).
22
 */
23
 
24
#include "fdlibm.h"
25
 
26
#ifndef _DOUBLE_IS_32BITS
27
 
28
#ifdef __STDC__
29
static const double
30
#else
31
static double
32
#endif
33
 
34
/* Adding a double, x, to 2^52 will cause the result to be rounded based on
35
   the fractional part of x, according to the implementation's current rounding
36
   mode.  2^52 is the smallest double that can be represented using all 52 significant
37
   digits. */
38
TWO52[2]={
39
  4.50359962737049600000e+15, /* 0x43300000, 0x00000000 */
40
 -4.50359962737049600000e+15, /* 0xC3300000, 0x00000000 */
41
};
42
 
43
long long int
44
#ifdef __STDC__
45
        llrint(double x)
46
#else
47
        llrint(x)
48
        double x;
49
#endif
50
{
51
  __int32_t i0,j0,sx;
52
  __uint32_t i1;
53
  double t;
54
  volatile double w;
55
  long long int result;
56
 
57
  EXTRACT_WORDS(i0,i1,x);
58
 
59
  /* Extract sign bit. */
60
  sx = (i0>>31)&1;
61
 
62
  /* Extract exponent field. */
63
  j0 = ((i0 & 0x7ff00000) >> 20) - 1023;
64
 
65
  if(j0 < 20)
66
    {
67
      if(j0 < -1)
68
        return 0;
69
      else
70
        {
71
          w = TWO52[sx] + x;
72
          t = w - TWO52[sx];
73
          GET_HIGH_WORD(i0, t);
74
          /* Detect the all-zeros representation of plus and
75
             minus zero, which fails the calculation below. */
76
          if ((i0 & ~(1 << 31)) == 0)
77
              return 0;
78
          j0 = ((i0 & 0x7ff00000) >> 20) - 1023;
79
          i0 &= 0x000fffff;
80
          i0 |= 0x00100000;
81
          result = i0 >> (20 - j0);
82
        }
83
    }
84
  else if (j0 < (int)(8 * sizeof (long long int)) - 1)
85
    {
86
      if (j0 >= 52)
87
        result = ((long long int) ((i0 & 0x000fffff) | 0x0010000) << (j0 - 20)) |
88
                   (i1 << (j0 - 52));
89
      else
90
        {
91
          w = TWO52[sx] + x;
92
          t = w - TWO52[sx];
93
          EXTRACT_WORDS (i0, i1, t);
94
          j0 = ((i0 & 0x7ff00000) >> 20) - 1023;
95
          i0 &= 0x000fffff;
96
          i0 |= 0x00100000;
97
          result = ((long long int) i0 << (j0 - 20)) | (i1 >> (52 - j0));
98
        }
99
    }
100
  else
101
    {
102
      return (long long int) x;
103
    }
104
 
105
  return sx ? -result : result;
106
}
107
 
108
#endif /* _DOUBLE_IS_32BITS */

powered by: WebSVN 2.1.0

© copyright 1999-2024 OpenCores.org, equivalent to Oliscience, all rights reserved. OpenCores®, registered trademark.