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_lround.c] - Blame information for rev 345

Details | Compare with Previous | View Log

Line No. Rev Author Line
1 207 jeremybenn
/*
2
 * ====================================================
3
 * Copyright (C) 1993 by Sun Microsystems, Inc. All rights reserved.
4
 *
5
 * Developed at SunPro, a Sun Microsystems, Inc. business.
6
 * Permission to use, copy, modify, and distribute this
7
 * software is freely granted, provided that this notice
8
 * is preserved.
9
 * ====================================================
10
 */
11
/*
12
FUNCTION
13
<<lround>>, <<lroundf>>, <<llround>>, <<llroundf>>--round to integer, to nearest
14
INDEX
15
        lround
16
INDEX
17
        lroundf
18
INDEX
19
        llround
20
INDEX
21
        llroundf
22
 
23
ANSI_SYNOPSIS
24
        #include <math.h>
25
        long int lround(double <[x]>);
26
        long int lroundf(float <[x]>);
27
        long long int llround(double <[x]>);
28
        long long int llroundf(float <[x]>);
29
 
30
DESCRIPTION
31
        The <<lround>> and <<llround>> functions round their argument to the
32
        nearest integer value, rounding halfway cases away from zero, regardless
33
        of the current rounding direction.  If the rounded value is outside the
34
        range of the return type, the numeric result is unspecified (depending
35
        upon the floating-point implementation, not the library).  A range
36
        error may occur if the magnitude of x is too large.
37
 
38
RETURNS
39
<[x]> rounded to an integral value as an integer.
40
 
41
SEEALSO
42
See the <<round>> functions for the return being the same floating-point type
43
as the argument.  <<lrint>>, <<llrint>>.
44
 
45
PORTABILITY
46
ANSI C, POSIX
47
 
48
*/
49
 
50
#include "fdlibm.h"
51
 
52
#ifndef _DOUBLE_IS_32BITS
53
 
54
#ifdef __STDC__
55
        long int lround(double x)
56
#else
57
        long int lround(x)
58
        double x;
59
#endif
60
{
61
  __int32_t sign, exponent_less_1023;
62
  /* Most significant word, least significant word. */
63
  __uint32_t msw, lsw;
64
  long int result;
65
 
66
  EXTRACT_WORDS(msw, lsw, x);
67
 
68
  /* Extract sign. */
69
  sign = ((msw & 0x80000000) ? -1 : 1);
70
  /* Extract exponent field. */
71
  exponent_less_1023 = ((msw & 0x7ff00000) >> 20) - 1023;
72
  msw &= 0x000fffff;
73
  msw |= 0x00100000;
74
 
75
  if (exponent_less_1023 < 20)
76
    {
77
      if (exponent_less_1023 < 0)
78
        {
79
          if (exponent_less_1023 < -1)
80
            return 0;
81
          else
82
            return sign;
83
        }
84
      else
85
        {
86
          msw += 0x80000 >> exponent_less_1023;
87
          result = msw >> (20 - exponent_less_1023);
88
        }
89
    }
90
  else if (exponent_less_1023 < (8 * sizeof (long int)) - 1)
91
    {
92
      if (exponent_less_1023 >= 52)
93
        result = ((long int) msw << (exponent_less_1023 - 20)) | (lsw << (exponent_less_1023 - 52));
94
      else
95
        {
96
          unsigned int tmp = lsw + (0x80000000 >> (exponent_less_1023 - 20));
97
          if (tmp < lsw)
98
            ++msw;
99
          result = ((long int) msw << (exponent_less_1023 - 20)) | (tmp >> (52 - exponent_less_1023));
100
        }
101
    }
102
  else
103
    /* Result is too large to be represented by a long int. */
104
    return (long int)x;
105
 
106
  return sign * result;
107
}
108
 
109
#endif /* _DOUBLE_IS_32BITS */

powered by: WebSVN 2.1.0

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