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 <<nearbyint>>, <<nearbyintf>>---round to integer
14 INDEX
15 	nearbyint
16 INDEX
17 	nearbyintf
18 
19 SYNOPSIS
20 	#include <math.h>
21 	double nearbyint(double <[x]>);
22 	float nearbyintf(float <[x]>);
23 
24 DESCRIPTION
25 The <<nearbyint>> functions round their argument to an integer value in
26 floating-point format, using the current rounding direction and
27 (supposedly) without raising the "inexact" floating-point exception.
28 See the <<rint>> functions for the same function with the "inexact"
29 floating-point exception being raised when appropriate.
30 
31 BUGS
32 Newlib does not support the floating-point exception model, so that
33 the floating-point exception control is not present and thereby what may
34 be seen will be compiler and hardware dependent in this regard.
35 The Newlib <<nearbyint>> functions are identical to the <<rint>>
36 functions with respect to the floating-point exception behavior, and
37 will cause the "inexact" exception to be raised for most targets.
38 
39 RETURNS
40 <[x]> rounded to an integral value, using the current rounding direction.
41 
42 PORTABILITY
43 ANSI C, POSIX
44 
45 SEEALSO
46 <<rint>>, <<round>>
47 */
48 
49 #include <math.h>
50 #include "fdlibm.h"
51 
52 #ifdef _NEED_FLOAT64
53 
54 __float64
nearbyint64(__float64 x)55 nearbyint64(__float64 x)
56 {
57     if (isnan(x)) return x + x;
58 #if defined(FE_INEXACT) && !defined(PICOLIBC_DOUBLE_NOEXECPT)
59     fenv_t env;
60     fegetenv(&env);
61 #endif
62     x = rint64(x);
63 #if defined(FE_INEXACT) && !defined(PICOLIBC_DOUBLE_NOEXECPT)
64     fesetenv(&env);
65 #endif
66     return x;
67 }
68 
69 _MATH_ALIAS_d_d(nearbyint)
70 
71 #endif /* _NEED_FLOAT64 */
72