Cygwin: mmap: allow remapping part of an existing anonymous mapping
[newlib-cygwin.git] / newlib / libm / common / s_round.c
blobe3a91d6c28ee9cdec0751dee311c8db75ebb399a
1 /*
2 * ====================================================
3 * Copyright (C) 1993 by Sun Microsystems, Inc. All rights reserved.
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 * ====================================================
12 FUNCTION
13 <<round>>, <<roundf>>---round to integer, to nearest
14 INDEX
15 round
16 INDEX
17 roundf
19 SYNOPSIS
20 #include <math.h>
21 double round(double <[x]>);
22 float roundf(float <[x]>);
24 DESCRIPTION
25 The <<round>> functions round their argument to the nearest integer
26 value in floating-point format, rounding halfway cases away from zero,
27 regardless of the current rounding direction. (While the "inexact"
28 floating-point exception behavior is unspecified by the C standard, the
29 <<round>> functions are written so that "inexact" is not raised if the
30 result does not equal the argument, which behavior is as recommended by
31 IEEE 754 for its related functions.)
33 RETURNS
34 <[x]> rounded to an integral value.
36 PORTABILITY
37 ANSI C, POSIX
39 SEEALSO
40 <<nearbyint>>, <<rint>>
44 #include "fdlibm.h"
46 #ifndef _DOUBLE_IS_32BITS
48 #ifdef __STDC__
49 double round(double x)
50 #else
51 double round(x)
52 double x;
53 #endif
55 /* Most significant word, least significant word. */
56 __int32_t msw, exponent_less_1023;
57 __uint32_t lsw;
59 EXTRACT_WORDS(msw, lsw, x);
61 /* Extract exponent field. */
62 exponent_less_1023 = ((msw & 0x7ff00000) >> 20) - 1023;
64 if (exponent_less_1023 < 20)
66 if (exponent_less_1023 < 0)
68 msw &= 0x80000000;
69 if (exponent_less_1023 == -1)
70 /* Result is +1.0 or -1.0. */
71 msw |= ((__int32_t)1023 << 20);
72 lsw = 0;
74 else
76 __uint32_t exponent_mask = 0x000fffff >> exponent_less_1023;
77 if ((msw & exponent_mask) == 0 && lsw == 0)
78 /* x in an integral value. */
79 return x;
81 msw += 0x00080000 >> exponent_less_1023;
82 msw &= ~exponent_mask;
83 lsw = 0;
86 else if (exponent_less_1023 > 51)
88 if (exponent_less_1023 == 1024)
89 /* x is NaN or infinite. */
90 return x + x;
91 else
92 return x;
94 else
96 __uint32_t exponent_mask = 0xffffffff >> (exponent_less_1023 - 20);
97 __uint32_t tmp;
99 if ((lsw & exponent_mask) == 0)
100 /* x is an integral value. */
101 return x;
103 tmp = lsw + (1 << (51 - exponent_less_1023));
104 if (tmp < lsw)
105 msw += 1;
106 lsw = tmp;
108 lsw &= ~exponent_mask;
110 INSERT_WORDS(x, msw, lsw);
112 return x;
115 #endif /* _DOUBLE_IS_32BITS */