OpenCores
URL https://opencores.org/ocsvn/openrisc_me/openrisc_me/trunk

Subversion Repositories openrisc_me

[/] [openrisc/] [trunk/] [gnu-src/] [newlib-1.17.0/] [newlib/] [libm/] [common/] [s_lrint.c] - Blame information for rev 158

Details | Compare with Previous | View Log

Line No. Rev Author Line
1 148 jeremybenn
 
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
 * lrint(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 lrint(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
#ifdef __STDC__
44
        long int lrint(double x)
45
#else
46
        long int lrint(x)
47
        double x;
48
#endif
49
{
50
  __int32_t i0,j0,sx;
51
  __uint32_t i1;
52
  double t;
53
  volatile double w;
54
  long int result;
55
 
56
  EXTRACT_WORDS(i0,i1,x);
57
 
58
  /* Extract sign bit. */
59
  sx = (i0>>31)&1;
60
 
61
  /* Extract exponent field. */
62
  j0 = ((i0 & 0x7ff00000) >> 20) - 1023;
63
 
64
  if(j0 < 20)
65
    {
66
      if(j0 < -1)
67
        return 0;
68
      else
69
        {
70
          w = TWO52[sx] + x;
71
          t = w - TWO52[sx];
72
          GET_HIGH_WORD(i0, t);
73
          /* Detect the all-zeros representation of plus and
74
             minus zero, which fails the calculation below. */
75
          if ((i0 & ~(1 << 31)) == 0)
76
              return 0;
77
          j0 = ((i0 & 0x7ff00000) >> 20) - 1023;
78
          i0 &= 0x000fffff;
79
          i0 |= 0x00100000;
80
          result = i0 >> (20 - j0);
81
        }
82
    }
83
  else if (j0 < (int)(8 * sizeof (long int)) - 1)
84
    {
85
      if (j0 >= 52)
86
        result = ((long int) ((i0 & 0x000fffff) | 0x0010000) << (j0 - 20)) |
87
                   (i1 << (j0 - 52));
88
      else
89
        {
90
          w = TWO52[sx] + x;
91
          t = w - TWO52[sx];
92
          EXTRACT_WORDS (i0, i1, t);
93
          j0 = ((i0 & 0x7ff00000) >> 20) - 1023;
94
          i0 &= 0x000fffff;
95
          i0 |= 0x00100000;
96
          result = ((long int) i0 << (j0 - 20)) | (i1 >> (52 - j0));
97
        }
98
    }
99
  else
100
    {
101
      return (long int) x;
102
    }
103
 
104
  return sx ? -result : result;
105
}
106
 
107
#endif /* _DOUBLE_IS_32BITS */

powered by: WebSVN 2.1.0

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