1 /*
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 <<trunc>>, <<truncf>>---round to integer, towards zero
14 INDEX
15 trunc
16 INDEX
17 truncf
18
19 SYNOPSIS
20 #include <math.h>
21 double trunc(double <[x]>);
22 float truncf(float <[x]>);
23
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.)
32
33 RETURNS
34 <[x]> truncated to an integral value.
35
36 PORTABILITY
37 ANSI C, POSIX
38
39 */
40
41 #include "fdlibm.h"
42
43 #ifdef _NEED_FLOAT64
44
45 __float64
trunc64(__float64 x)46 trunc64(__float64 x)
47 {
48 int64_t ix = _asint64(x);
49 int64_t mask;
50 int exp;
51
52 /* Un-biased exponent */
53 exp = _exponent64(ix) - 1023;
54
55 /* Inf/NaN, evaluate value */
56 if (unlikely(exp == 1024))
57 return x + x;
58
59 /* compute portion of value with useful bits */
60 if (exp < 0)
61 /* less than one, save sign bit */
62 mask = 0x8000000000000000LL;
63 else
64 /* otherwise, save sign, exponent and any useful bits */
65 mask = ~(0x000fffffffffffffLL >> exp);
66
67 return _asdouble(ix & mask);
68 }
69
70 _MATH_ALIAS_d_d(trunc)
71
72 #endif /* _NEED_FLOAT64 */
73