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
9 * ====================================================
13 <<trunc>>, <<truncf>>---round to integer, towards zero
21 double trunc(double <[x]>);
22 float truncf(float <[x]>);
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.)
34 <[x]> truncated to an integral value.
43 #ifndef _DOUBLE_IS_32BITS
46 double trunc(double x
)
53 /* Most significant word, least significant word. */
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);
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. */
87 /* All bits in the fraction fields of the msw and lsw are needed in the result. */
91 /* All fraction bits in msw are relevant. Truncate irrelevant
93 INSERT_WORDS(x
, msw
, lsw
& ~(0xffffffffu
>> (exponent_less_1023
- 20)));
98 #endif /* _DOUBLE_IS_32BITS */