Cygwin: mmap: allow remapping part of an existing anonymous mapping
[newlib-cygwin.git] / newlib / libm / common / s_trunc.c
blob0e06add2201b30b1fd14cbc749ad601ec3797a51
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 <<trunc>>, <<truncf>>---round to integer, towards zero
14 INDEX
15 trunc
16 INDEX
17 truncf
19 SYNOPSIS
20 #include <math.h>
21 double trunc(double <[x]>);
22 float truncf(float <[x]>);
24 DESCRIPTION
25 The <<trunc>> functions round their argument to the integer value, in
26 floating format, nearest to but no larger in magnitude than the
27 argument, regardless of the current rounding direction. (While the
28 "inexact" floating-point exception behavior is unspecified by the C
29 standard, the <<trunc>> functions are written so that "inexact" is not
30 raised if the result does not equal the argument, which behavior is as
31 recommended by IEEE 754 for its related functions.)
33 RETURNS
34 <[x]> truncated to an integral value.
36 PORTABILITY
37 ANSI C, POSIX
41 #include "fdlibm.h"
43 #ifndef _DOUBLE_IS_32BITS
45 #ifdef __STDC__
46 double trunc(double x)
47 #else
48 double trunc(x)
49 double x;
50 #endif
52 int signbit;
53 /* Most significant word, least significant word. */
54 int msw;
55 unsigned int lsw;
56 int exponent_less_1023;
58 EXTRACT_WORDS(msw, lsw, x);
60 /* Extract sign bit. */
61 signbit = msw & 0x80000000;
63 /* Extract exponent field. */
64 exponent_less_1023 = ((msw & 0x7ff00000) >> 20) - 1023;
66 if (exponent_less_1023 < 20)
68 /* All significant digits are in msw. */
69 if (exponent_less_1023 < 0)
71 /* -1 < x < 1, so result is +0 or -0. */
72 INSERT_WORDS(x, signbit, 0);
74 else
76 /* All relevant fraction bits are in msw, so lsw of the result is 0. */
77 INSERT_WORDS(x, signbit | (msw & ~(0x000fffff >> exponent_less_1023)), 0);
80 else if (exponent_less_1023 > 51)
82 if (exponent_less_1023 == 1024)
84 /* x is infinite, or not a number, so trigger an exception. */
85 return x + x;
87 /* All bits in the fraction fields of the msw and lsw are needed in the result. */
89 else
91 /* All fraction bits in msw are relevant. Truncate irrelevant
92 bits from lsw. */
93 INSERT_WORDS(x, msw, lsw & ~(0xffffffffu >> (exponent_less_1023 - 20)));
95 return x;
98 #endif /* _DOUBLE_IS_32BITS */